1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock * You may not use this file except in compliance with the License.
7fa9e4066Sahrens *
8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens * See the License for the specific language governing permissions
11fa9e4066Sahrens * and limitations under the License.
12fa9e4066Sahrens *
13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens *
19fa9e4066Sahrens * CDDL HEADER END
20fa9e4066Sahrens */
21f3861e1aSahl
22fa9e4066Sahrens /*
2336db6475SEric Taylor * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
240dfe541eSEvan Layton */
250dfe541eSEvan Layton
260dfe541eSEvan Layton /*
270dfe541eSEvan Layton * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28ad2760acSMatthew Ahrens * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
29f0f3ef5aSGarrett D'Amore * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
300d8fa8f8SMartin Matuska * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
31013023d4SMartin Matuska * Copyright (c) 2013 Martin Matuska. All rights reserved.
32a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
33c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
340dfe541eSEvan Layton * Copyright 2018 Nexenta Systems, Inc.
3588f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
36f62db44dSAndrew Stormont * Copyright 2017-2018 RackTop Systems.
3799ea293eSMatt Fiddaman * Copyright (c) 2021 Matt Fiddaman
38fa9e4066Sahrens */
39fa9e4066Sahrens
40fa9e4066Sahrens #include <ctype.h>
41fa9e4066Sahrens #include <errno.h>
42fa9e4066Sahrens #include <libintl.h>
43fa9e4066Sahrens #include <stdio.h>
44fa9e4066Sahrens #include <stdlib.h>
45fa9e4066Sahrens #include <strings.h>
46fa9e4066Sahrens #include <unistd.h>
473cb34c60Sahrens #include <stddef.h>
48fa9e4066Sahrens #include <zone.h>
4999653d4eSeschrock #include <fcntl.h>
50fa9e4066Sahrens #include <sys/mntent.h>
51b12a1c38Slling #include <sys/mount.h>
52ecd6cf80Smarks #include <priv.h>
53ecd6cf80Smarks #include <pwd.h>
54ecd6cf80Smarks #include <grp.h>
55ecd6cf80Smarks #include <stddef.h>
56ecd6cf80Smarks #include <ucred.h>
5714843421SMatthew Ahrens #include <idmap.h>
5814843421SMatthew Ahrens #include <aclutils.h>
593b12c289SMatthew Ahrens #include <directory.h>
60591e0e13SSebastien Roy #include <time.h>
61fa9e4066Sahrens
62c1449561SEric Taylor #include <sys/dnode.h>
63fa9e4066Sahrens #include <sys/spa.h>
64e9dbad6fSeschrock #include <sys/zap.h>
65eb633035STom Caputi #include <sys/dsl_crypt.h>
66fa9e4066Sahrens #include <libzfs.h>
67d8ab6e12SDon Brady #include <libzutil.h>
68fa9e4066Sahrens
69fa9e4066Sahrens #include "zfs_namecheck.h"
70fa9e4066Sahrens #include "zfs_prop.h"
71fa9e4066Sahrens #include "libzfs_impl.h"
72ecd6cf80Smarks #include "zfs_deleg.h"
73fa9e4066Sahrens
7414843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned,
7514843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
76cdf5b4caSmmusante
77fa9e4066Sahrens /*
78fa9e4066Sahrens * Given a single type (not a mask of types), return the type in a human
79fa9e4066Sahrens * readable form.
80fa9e4066Sahrens */
81fa9e4066Sahrens const char *
zfs_type_to_name(zfs_type_t type)82fa9e4066Sahrens zfs_type_to_name(zfs_type_t type)
83fa9e4066Sahrens {
84fa9e4066Sahrens switch (type) {
85fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM:
86fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem"));
87fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT:
88fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot"));
89fa9e4066Sahrens case ZFS_TYPE_VOLUME:
90fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume"));
9188f61deeSIgor Kozhukhov case ZFS_TYPE_POOL:
9288f61deeSIgor Kozhukhov return (dgettext(TEXT_DOMAIN, "pool"));
9388f61deeSIgor Kozhukhov case ZFS_TYPE_BOOKMARK:
9488f61deeSIgor Kozhukhov return (dgettext(TEXT_DOMAIN, "bookmark"));
9588f61deeSIgor Kozhukhov default:
9688f61deeSIgor Kozhukhov assert(!"unhandled zfs_type_t");
97fa9e4066Sahrens }
98fa9e4066Sahrens
99fa9e4066Sahrens return (NULL);
100fa9e4066Sahrens }
101fa9e4066Sahrens
102fa9e4066Sahrens /*
103fa9e4066Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to
10414843421SMatthew Ahrens * provide a more meaningful error message. We call zfs_error_aux() to
10514843421SMatthew Ahrens * explain exactly why the name was not valid.
106fa9e4066Sahrens */
10799d5e173STim Haley int
zfs_validate_name(libzfs_handle_t * hdl,const char * path,int type,boolean_t modifying)108f18faf3fSek zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
109f18faf3fSek boolean_t modifying)
110fa9e4066Sahrens {
111fa9e4066Sahrens namecheck_err_t why;
112fa9e4066Sahrens char what;
113fa9e4066Sahrens
114edb901aaSMarcel Telka if (entity_namecheck(path, &why, &what) != 0) {
11599653d4eSeschrock if (hdl != NULL) {
116fa9e4066Sahrens switch (why) {
117b81d61a6Slling case NAME_ERR_TOOLONG:
11899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11999653d4eSeschrock "name is too long"));
120b81d61a6Slling break;
121b81d61a6Slling
122fa9e4066Sahrens case NAME_ERR_LEADING_SLASH:
12399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12499653d4eSeschrock "leading slash in name"));
125fa9e4066Sahrens break;
126fa9e4066Sahrens
127fa9e4066Sahrens case NAME_ERR_EMPTY_COMPONENT:
12899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12999653d4eSeschrock "empty component in name"));
130fa9e4066Sahrens break;
131fa9e4066Sahrens
132fa9e4066Sahrens case NAME_ERR_TRAILING_SLASH:
13399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13499653d4eSeschrock "trailing slash in name"));
135fa9e4066Sahrens break;
136fa9e4066Sahrens
137fa9e4066Sahrens case NAME_ERR_INVALCHAR:
13899653d4eSeschrock zfs_error_aux(hdl,
139fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character "
14099653d4eSeschrock "'%c' in name"), what);
141fa9e4066Sahrens break;
142fa9e4066Sahrens
143edb901aaSMarcel Telka case NAME_ERR_MULTIPLE_DELIMITERS:
14499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
145edb901aaSMarcel Telka "multiple '@' and/or '#' delimiters in "
146edb901aaSMarcel Telka "name"));
147fa9e4066Sahrens break;
1485ad82045Snd
1495ad82045Snd case NAME_ERR_NOLETTER:
1505ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1515ad82045Snd "pool doesn't begin with a letter"));
1525ad82045Snd break;
1535ad82045Snd
1545ad82045Snd case NAME_ERR_RESERVED:
1555ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1565ad82045Snd "name is reserved"));
1575ad82045Snd break;
1585ad82045Snd
1595ad82045Snd case NAME_ERR_DISKLIKE:
1605ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1615ad82045Snd "reserved disk name"));
1625ad82045Snd break;
16388f61deeSIgor Kozhukhov
16488f61deeSIgor Kozhukhov default:
16588f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16688f61deeSIgor Kozhukhov "(%d) not defined"), why);
16788f61deeSIgor Kozhukhov break;
168fa9e4066Sahrens }
169fa9e4066Sahrens }
170fa9e4066Sahrens
171fa9e4066Sahrens return (0);
172fa9e4066Sahrens }
173fa9e4066Sahrens
174fa9e4066Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
17599653d4eSeschrock if (hdl != NULL)
17699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
177edb901aaSMarcel Telka "snapshot delimiter '@' is not expected here"));
178fa9e4066Sahrens return (0);
179fa9e4066Sahrens }
180fa9e4066Sahrens
1811d452cf5Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
1821d452cf5Sahrens if (hdl != NULL)
1831d452cf5Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
184d7d4af51Smmusante "missing '@' delimiter in snapshot name"));
1851d452cf5Sahrens return (0);
1861d452cf5Sahrens }
1871d452cf5Sahrens
188edb901aaSMarcel Telka if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
189edb901aaSMarcel Telka if (hdl != NULL)
190edb901aaSMarcel Telka zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
191edb901aaSMarcel Telka "bookmark delimiter '#' is not expected here"));
192edb901aaSMarcel Telka return (0);
193edb901aaSMarcel Telka }
194edb901aaSMarcel Telka
195edb901aaSMarcel Telka if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
196edb901aaSMarcel Telka if (hdl != NULL)
197edb901aaSMarcel Telka zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
198edb901aaSMarcel Telka "missing '#' delimiter in bookmark name"));
199edb901aaSMarcel Telka return (0);
200edb901aaSMarcel Telka }
201edb901aaSMarcel Telka
202f18faf3fSek if (modifying && strchr(path, '%') != NULL) {
203f18faf3fSek if (hdl != NULL)
204f18faf3fSek zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
205f18faf3fSek "invalid character %c in name"), '%');
206f18faf3fSek return (0);
207f18faf3fSek }
208f18faf3fSek
20999653d4eSeschrock return (-1);
210fa9e4066Sahrens }
211fa9e4066Sahrens
212fa9e4066Sahrens int
zfs_name_valid(const char * name,zfs_type_t type)213fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type)
214fa9e4066Sahrens {
215e7cbe64fSgw if (type == ZFS_TYPE_POOL)
216e7cbe64fSgw return (zpool_name_valid(NULL, B_FALSE, name));
217f18faf3fSek return (zfs_validate_name(NULL, name, type, B_FALSE));
218fa9e4066Sahrens }
219fa9e4066Sahrens
220e9dbad6fSeschrock /*
221e9dbad6fSeschrock * This function takes the raw DSL properties, and filters out the user-defined
222e9dbad6fSeschrock * properties into a separate nvlist.
223e9dbad6fSeschrock */
224fac3008cSeschrock static nvlist_t *
process_user_props(zfs_handle_t * zhp,nvlist_t * props)225fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props)
226e9dbad6fSeschrock {
227e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl;
228e9dbad6fSeschrock nvpair_t *elem;
229e9dbad6fSeschrock nvlist_t *propval;
230fac3008cSeschrock nvlist_t *nvl;
231e9dbad6fSeschrock
232fac3008cSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
233fac3008cSeschrock (void) no_memory(hdl);
234fac3008cSeschrock return (NULL);
235fac3008cSeschrock }
236e9dbad6fSeschrock
237e9dbad6fSeschrock elem = NULL;
238fac3008cSeschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
239e9dbad6fSeschrock if (!zfs_prop_user(nvpair_name(elem)))
240e9dbad6fSeschrock continue;
241e9dbad6fSeschrock
242e9dbad6fSeschrock verify(nvpair_value_nvlist(elem, &propval) == 0);
243fac3008cSeschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
244fac3008cSeschrock nvlist_free(nvl);
245fac3008cSeschrock (void) no_memory(hdl);
246fac3008cSeschrock return (NULL);
247fac3008cSeschrock }
248e9dbad6fSeschrock }
249e9dbad6fSeschrock
250fac3008cSeschrock return (nvl);
251e9dbad6fSeschrock }
252e9dbad6fSeschrock
25329ab75c9Srm static zpool_handle_t *
zpool_add_handle(zfs_handle_t * zhp,const char * pool_name)25429ab75c9Srm zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
25529ab75c9Srm {
25629ab75c9Srm libzfs_handle_t *hdl = zhp->zfs_hdl;
25729ab75c9Srm zpool_handle_t *zph;
25829ab75c9Srm
25929ab75c9Srm if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
26029ab75c9Srm if (hdl->libzfs_pool_handles != NULL)
26129ab75c9Srm zph->zpool_next = hdl->libzfs_pool_handles;
26229ab75c9Srm hdl->libzfs_pool_handles = zph;
26329ab75c9Srm }
26429ab75c9Srm return (zph);
26529ab75c9Srm }
26629ab75c9Srm
26729ab75c9Srm static zpool_handle_t *
zpool_find_handle(zfs_handle_t * zhp,const char * pool_name,int len)26829ab75c9Srm zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
26929ab75c9Srm {
27029ab75c9Srm libzfs_handle_t *hdl = zhp->zfs_hdl;
27129ab75c9Srm zpool_handle_t *zph = hdl->libzfs_pool_handles;
27229ab75c9Srm
27329ab75c9Srm while ((zph != NULL) &&
27429ab75c9Srm (strncmp(pool_name, zpool_get_name(zph), len) != 0))
27529ab75c9Srm zph = zph->zpool_next;
27629ab75c9Srm return (zph);
27729ab75c9Srm }
27829ab75c9Srm
27929ab75c9Srm /*
28029ab75c9Srm * Returns a handle to the pool that contains the provided dataset.
28129ab75c9Srm * If a handle to that pool already exists then that handle is returned.
28229ab75c9Srm * Otherwise, a new handle is created and added to the list of handles.
28329ab75c9Srm */
28429ab75c9Srm static zpool_handle_t *
zpool_handle(zfs_handle_t * zhp)28529ab75c9Srm zpool_handle(zfs_handle_t *zhp)
28629ab75c9Srm {
28729ab75c9Srm char *pool_name;
28829ab75c9Srm int len;
28929ab75c9Srm zpool_handle_t *zph;
29029ab75c9Srm
29178f17100SMatthew Ahrens len = strcspn(zhp->zfs_name, "/@#") + 1;
29229ab75c9Srm pool_name = zfs_alloc(zhp->zfs_hdl, len);
29329ab75c9Srm (void) strlcpy(pool_name, zhp->zfs_name, len);
29429ab75c9Srm
29529ab75c9Srm zph = zpool_find_handle(zhp, pool_name, len);
29629ab75c9Srm if (zph == NULL)
29729ab75c9Srm zph = zpool_add_handle(zhp, pool_name);
29829ab75c9Srm
29929ab75c9Srm free(pool_name);
30029ab75c9Srm return (zph);
30129ab75c9Srm }
30229ab75c9Srm
30329ab75c9Srm void
zpool_free_handles(libzfs_handle_t * hdl)30429ab75c9Srm zpool_free_handles(libzfs_handle_t *hdl)
30529ab75c9Srm {
30629ab75c9Srm zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
30729ab75c9Srm
30829ab75c9Srm while (zph != NULL) {
30929ab75c9Srm next = zph->zpool_next;
31029ab75c9Srm zpool_close(zph);
31129ab75c9Srm zph = next;
31229ab75c9Srm }
31329ab75c9Srm hdl->libzfs_pool_handles = NULL;
31429ab75c9Srm }
31529ab75c9Srm
316fa9e4066Sahrens /*
317fa9e4066Sahrens * Utility function to gather stats (objset and zpl) for the given object.
318fa9e4066Sahrens */
319fa9e4066Sahrens static int
get_stats_ioctl(zfs_handle_t * zhp,zfs_cmd_t * zc)320ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
321fa9e4066Sahrens {
322e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl;
323fa9e4066Sahrens
324ebedde84SEric Taylor (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
325fa9e4066Sahrens
326ebedde84SEric Taylor while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
3277f7322feSeschrock if (errno == ENOMEM) {
328ebedde84SEric Taylor if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
32999653d4eSeschrock return (-1);
330e9dbad6fSeschrock }
3317f7322feSeschrock } else {
3327f7322feSeschrock return (-1);
3337f7322feSeschrock }
3347f7322feSeschrock }
335ebedde84SEric Taylor return (0);
336ebedde84SEric Taylor }
337fa9e4066Sahrens
33892241e0bSTom Erickson /*
33992241e0bSTom Erickson * Utility function to get the received properties of the given object.
34092241e0bSTom Erickson */
34192241e0bSTom Erickson static int
get_recvd_props_ioctl(zfs_handle_t * zhp)34292241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp)
34392241e0bSTom Erickson {
34492241e0bSTom Erickson libzfs_handle_t *hdl = zhp->zfs_hdl;
34592241e0bSTom Erickson nvlist_t *recvdprops;
34692241e0bSTom Erickson zfs_cmd_t zc = { 0 };
34792241e0bSTom Erickson int err;
34892241e0bSTom Erickson
34992241e0bSTom Erickson if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
35092241e0bSTom Erickson return (-1);
35192241e0bSTom Erickson
35292241e0bSTom Erickson (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
35392241e0bSTom Erickson
35492241e0bSTom Erickson while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
35592241e0bSTom Erickson if (errno == ENOMEM) {
35692241e0bSTom Erickson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
35792241e0bSTom Erickson return (-1);
35892241e0bSTom Erickson }
35992241e0bSTom Erickson } else {
36092241e0bSTom Erickson zcmd_free_nvlists(&zc);
36192241e0bSTom Erickson return (-1);
36292241e0bSTom Erickson }
36392241e0bSTom Erickson }
36492241e0bSTom Erickson
36592241e0bSTom Erickson err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
36692241e0bSTom Erickson zcmd_free_nvlists(&zc);
36792241e0bSTom Erickson if (err != 0)
36892241e0bSTom Erickson return (-1);
36992241e0bSTom Erickson
37092241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props);
37192241e0bSTom Erickson zhp->zfs_recvd_props = recvdprops;
37292241e0bSTom Erickson
37392241e0bSTom Erickson return (0);
37492241e0bSTom Erickson }
37592241e0bSTom Erickson
376ebedde84SEric Taylor static int
put_stats_zhdl(zfs_handle_t * zhp,zfs_cmd_t * zc)377ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
378ebedde84SEric Taylor {
379ebedde84SEric Taylor nvlist_t *allprops, *userprops;
380fa9e4066Sahrens
381ebedde84SEric Taylor zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
382ebedde84SEric Taylor
383ebedde84SEric Taylor if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
38499653d4eSeschrock return (-1);
38599653d4eSeschrock }
386fa9e4066Sahrens
38714843421SMatthew Ahrens /*
38814843421SMatthew Ahrens * XXX Why do we store the user props separately, in addition to
38914843421SMatthew Ahrens * storing them in zfs_props?
39014843421SMatthew Ahrens */
391fac3008cSeschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) {
392fac3008cSeschrock nvlist_free(allprops);
393e9dbad6fSeschrock return (-1);
394fac3008cSeschrock }
395fac3008cSeschrock
396fac3008cSeschrock nvlist_free(zhp->zfs_props);
397fac3008cSeschrock nvlist_free(zhp->zfs_user_props);
398fac3008cSeschrock
399fac3008cSeschrock zhp->zfs_props = allprops;
400fac3008cSeschrock zhp->zfs_user_props = userprops;
40199653d4eSeschrock
402fa9e4066Sahrens return (0);
403fa9e4066Sahrens }
404fa9e4066Sahrens
405ebedde84SEric Taylor static int
get_stats(zfs_handle_t * zhp)406ebedde84SEric Taylor get_stats(zfs_handle_t *zhp)
407ebedde84SEric Taylor {
408ebedde84SEric Taylor int rc = 0;
409ebedde84SEric Taylor zfs_cmd_t zc = { 0 };
410ebedde84SEric Taylor
411ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
412ebedde84SEric Taylor return (-1);
413ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) != 0)
414ebedde84SEric Taylor rc = -1;
415ebedde84SEric Taylor else if (put_stats_zhdl(zhp, &zc) != 0)
416ebedde84SEric Taylor rc = -1;
417ebedde84SEric Taylor zcmd_free_nvlists(&zc);
418ebedde84SEric Taylor return (rc);
419ebedde84SEric Taylor }
420ebedde84SEric Taylor
421fa9e4066Sahrens /*
422fa9e4066Sahrens * Refresh the properties currently stored in the handle.
423fa9e4066Sahrens */
424fa9e4066Sahrens void
zfs_refresh_properties(zfs_handle_t * zhp)425fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp)
426fa9e4066Sahrens {
427fa9e4066Sahrens (void) get_stats(zhp);
428fa9e4066Sahrens }
429fa9e4066Sahrens
430fa9e4066Sahrens /*
431fa9e4066Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and
432fa9e4066Sahrens * zfs_iter_* to create child handles on the fly.
433fa9e4066Sahrens */
434ebedde84SEric Taylor static int
make_dataset_handle_common(zfs_handle_t * zhp,zfs_cmd_t * zc)435ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
436fa9e4066Sahrens {
437503ad85cSMatthew Ahrens if (put_stats_zhdl(zhp, zc) != 0)
438ebedde84SEric Taylor return (-1);
43931fd60d3Sahrens
440fa9e4066Sahrens /*
441fa9e4066Sahrens * We've managed to open the dataset and gather statistics. Determine
442fa9e4066Sahrens * the high-level type.
443fa9e4066Sahrens */
444a2eea2e1Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
445a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME;
446a2eea2e1Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
447a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
448e0f1c0afSOlaf Faaland else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
449e0f1c0afSOlaf Faaland return (-1);
450a2eea2e1Sahrens else
451a2eea2e1Sahrens abort();
452a2eea2e1Sahrens
453fa9e4066Sahrens if (zhp->zfs_dmustats.dds_is_snapshot)
454fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
455fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
456fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME;
457fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
458fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
459fa9e4066Sahrens else
46099653d4eSeschrock abort(); /* we should never see any other types */
461fa9e4066Sahrens
4629fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
4639fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (-1);
4649fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States
465ebedde84SEric Taylor return (0);
466ebedde84SEric Taylor }
467ebedde84SEric Taylor
468ebedde84SEric Taylor zfs_handle_t *
make_dataset_handle(libzfs_handle_t * hdl,const char * path)469ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path)
470ebedde84SEric Taylor {
471ebedde84SEric Taylor zfs_cmd_t zc = { 0 };
472ebedde84SEric Taylor
473ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
474ebedde84SEric Taylor
475ebedde84SEric Taylor if (zhp == NULL)
476ebedde84SEric Taylor return (NULL);
477ebedde84SEric Taylor
478ebedde84SEric Taylor zhp->zfs_hdl = hdl;
479ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
480ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
481ebedde84SEric Taylor free(zhp);
482ebedde84SEric Taylor return (NULL);
483ebedde84SEric Taylor }
484ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) == -1) {
485ebedde84SEric Taylor zcmd_free_nvlists(&zc);
486ebedde84SEric Taylor free(zhp);
487ebedde84SEric Taylor return (NULL);
488ebedde84SEric Taylor }
489ebedde84SEric Taylor if (make_dataset_handle_common(zhp, &zc) == -1) {
490ebedde84SEric Taylor free(zhp);
491ebedde84SEric Taylor zhp = NULL;
492ebedde84SEric Taylor }
493ebedde84SEric Taylor zcmd_free_nvlists(&zc);
494ebedde84SEric Taylor return (zhp);
495ebedde84SEric Taylor }
496ebedde84SEric Taylor
49719b94df9SMatthew Ahrens zfs_handle_t *
make_dataset_handle_zc(libzfs_handle_t * hdl,zfs_cmd_t * zc)498ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
499ebedde84SEric Taylor {
500ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
501ebedde84SEric Taylor
502ebedde84SEric Taylor if (zhp == NULL)
503ebedde84SEric Taylor return (NULL);
504ebedde84SEric Taylor
505ebedde84SEric Taylor zhp->zfs_hdl = hdl;
506ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
507ebedde84SEric Taylor if (make_dataset_handle_common(zhp, zc) == -1) {
508ebedde84SEric Taylor free(zhp);
509ebedde84SEric Taylor return (NULL);
510ebedde84SEric Taylor }
511fa9e4066Sahrens return (zhp);
512fa9e4066Sahrens }
513fa9e4066Sahrens
5140d8fa8f8SMartin Matuska zfs_handle_t *
make_dataset_simple_handle_zc(zfs_handle_t * pzhp,zfs_cmd_t * zc)5150d8fa8f8SMartin Matuska make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
5160d8fa8f8SMartin Matuska {
5170d8fa8f8SMartin Matuska zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
5180d8fa8f8SMartin Matuska
5190d8fa8f8SMartin Matuska if (zhp == NULL)
5200d8fa8f8SMartin Matuska return (NULL);
5210d8fa8f8SMartin Matuska
5220d8fa8f8SMartin Matuska zhp->zfs_hdl = pzhp->zfs_hdl;
5230d8fa8f8SMartin Matuska (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
5240d8fa8f8SMartin Matuska zhp->zfs_head_type = pzhp->zfs_type;
5250d8fa8f8SMartin Matuska zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
5260d8fa8f8SMartin Matuska zhp->zpool_hdl = zpool_handle(zhp);
5270d8fa8f8SMartin Matuska return (zhp);
5280d8fa8f8SMartin Matuska }
5290d8fa8f8SMartin Matuska
53019b94df9SMatthew Ahrens zfs_handle_t *
zfs_handle_dup(zfs_handle_t * zhp_orig)53119b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig)
53219b94df9SMatthew Ahrens {
53319b94df9SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
53419b94df9SMatthew Ahrens
53519b94df9SMatthew Ahrens if (zhp == NULL)
53619b94df9SMatthew Ahrens return (NULL);
53719b94df9SMatthew Ahrens
53819b94df9SMatthew Ahrens zhp->zfs_hdl = zhp_orig->zfs_hdl;
53919b94df9SMatthew Ahrens zhp->zpool_hdl = zhp_orig->zpool_hdl;
54019b94df9SMatthew Ahrens (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
54119b94df9SMatthew Ahrens sizeof (zhp->zfs_name));
54219b94df9SMatthew Ahrens zhp->zfs_type = zhp_orig->zfs_type;
54319b94df9SMatthew Ahrens zhp->zfs_head_type = zhp_orig->zfs_head_type;
54419b94df9SMatthew Ahrens zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
54519b94df9SMatthew Ahrens if (zhp_orig->zfs_props != NULL) {
54619b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
54719b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl);
54819b94df9SMatthew Ahrens zfs_close(zhp);
54919b94df9SMatthew Ahrens return (NULL);
55019b94df9SMatthew Ahrens }
55119b94df9SMatthew Ahrens }
55219b94df9SMatthew Ahrens if (zhp_orig->zfs_user_props != NULL) {
55319b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_user_props,
55419b94df9SMatthew Ahrens &zhp->zfs_user_props, 0) != 0) {
55519b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl);
55619b94df9SMatthew Ahrens zfs_close(zhp);
55719b94df9SMatthew Ahrens return (NULL);
55819b94df9SMatthew Ahrens }
55919b94df9SMatthew Ahrens }
56019b94df9SMatthew Ahrens if (zhp_orig->zfs_recvd_props != NULL) {
56119b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_recvd_props,
56219b94df9SMatthew Ahrens &zhp->zfs_recvd_props, 0)) {
56319b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl);
56419b94df9SMatthew Ahrens zfs_close(zhp);
56519b94df9SMatthew Ahrens return (NULL);
56619b94df9SMatthew Ahrens }
56719b94df9SMatthew Ahrens }
56819b94df9SMatthew Ahrens zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
56919b94df9SMatthew Ahrens if (zhp_orig->zfs_mntopts != NULL) {
57019b94df9SMatthew Ahrens zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
57119b94df9SMatthew Ahrens zhp_orig->zfs_mntopts);
57219b94df9SMatthew Ahrens }
57319b94df9SMatthew Ahrens zhp->zfs_props_table = zhp_orig->zfs_props_table;
57419b94df9SMatthew Ahrens return (zhp);
57519b94df9SMatthew Ahrens }
57619b94df9SMatthew Ahrens
57778f17100SMatthew Ahrens boolean_t
zfs_bookmark_exists(const char * path)57878f17100SMatthew Ahrens zfs_bookmark_exists(const char *path)
57978f17100SMatthew Ahrens {
58078f17100SMatthew Ahrens nvlist_t *bmarks;
58178f17100SMatthew Ahrens nvlist_t *props;
5829adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN];
58378f17100SMatthew Ahrens char *bmark_name;
58478f17100SMatthew Ahrens char *pound;
58578f17100SMatthew Ahrens int err;
58678f17100SMatthew Ahrens boolean_t rv;
58778f17100SMatthew Ahrens
58878f17100SMatthew Ahrens
58978f17100SMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname));
59078f17100SMatthew Ahrens pound = strchr(fsname, '#');
59178f17100SMatthew Ahrens if (pound == NULL)
59278f17100SMatthew Ahrens return (B_FALSE);
59378f17100SMatthew Ahrens
59478f17100SMatthew Ahrens *pound = '\0';
59578f17100SMatthew Ahrens bmark_name = pound + 1;
59678f17100SMatthew Ahrens props = fnvlist_alloc();
59778f17100SMatthew Ahrens err = lzc_get_bookmarks(fsname, props, &bmarks);
59878f17100SMatthew Ahrens nvlist_free(props);
59978f17100SMatthew Ahrens if (err != 0) {
60078f17100SMatthew Ahrens nvlist_free(bmarks);
60178f17100SMatthew Ahrens return (B_FALSE);
60278f17100SMatthew Ahrens }
60378f17100SMatthew Ahrens
60478f17100SMatthew Ahrens rv = nvlist_exists(bmarks, bmark_name);
60578f17100SMatthew Ahrens nvlist_free(bmarks);
60678f17100SMatthew Ahrens return (rv);
60778f17100SMatthew Ahrens }
60878f17100SMatthew Ahrens
60978f17100SMatthew Ahrens zfs_handle_t *
make_bookmark_handle(zfs_handle_t * parent,const char * path,nvlist_t * bmark_props)61078f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path,
61178f17100SMatthew Ahrens nvlist_t *bmark_props)
61278f17100SMatthew Ahrens {
61378f17100SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
61478f17100SMatthew Ahrens
61578f17100SMatthew Ahrens if (zhp == NULL)
61678f17100SMatthew Ahrens return (NULL);
61778f17100SMatthew Ahrens
61878f17100SMatthew Ahrens /* Fill in the name. */
61978f17100SMatthew Ahrens zhp->zfs_hdl = parent->zfs_hdl;
62078f17100SMatthew Ahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
62178f17100SMatthew Ahrens
62278f17100SMatthew Ahrens /* Set the property lists. */
62378f17100SMatthew Ahrens if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
62478f17100SMatthew Ahrens free(zhp);
62578f17100SMatthew Ahrens return (NULL);
62678f17100SMatthew Ahrens }
62778f17100SMatthew Ahrens
62878f17100SMatthew Ahrens /* Set the types. */
62978f17100SMatthew Ahrens zhp->zfs_head_type = parent->zfs_head_type;
63078f17100SMatthew Ahrens zhp->zfs_type = ZFS_TYPE_BOOKMARK;
63178f17100SMatthew Ahrens
63278f17100SMatthew Ahrens if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
63378f17100SMatthew Ahrens nvlist_free(zhp->zfs_props);
63478f17100SMatthew Ahrens free(zhp);
63578f17100SMatthew Ahrens return (NULL);
63678f17100SMatthew Ahrens }
63778f17100SMatthew Ahrens
63878f17100SMatthew Ahrens return (zhp);
63978f17100SMatthew Ahrens }
64078f17100SMatthew Ahrens
641edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data {
642edb901aaSMarcel Telka const char *path;
643edb901aaSMarcel Telka zfs_handle_t *zhp;
644edb901aaSMarcel Telka };
645edb901aaSMarcel Telka
646edb901aaSMarcel Telka static int
zfs_open_bookmarks_cb(zfs_handle_t * zhp,void * data)647edb901aaSMarcel Telka zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
648edb901aaSMarcel Telka {
649edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data *dp = data;
650edb901aaSMarcel Telka
651edb901aaSMarcel Telka /*
652edb901aaSMarcel Telka * Is it the one we are looking for?
653edb901aaSMarcel Telka */
654edb901aaSMarcel Telka if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
655edb901aaSMarcel Telka /*
656edb901aaSMarcel Telka * We found it. Save it and let the caller know we are done.
657edb901aaSMarcel Telka */
658edb901aaSMarcel Telka dp->zhp = zhp;
659edb901aaSMarcel Telka return (EEXIST);
660edb901aaSMarcel Telka }
661edb901aaSMarcel Telka
662edb901aaSMarcel Telka /*
663edb901aaSMarcel Telka * Not found. Close the handle and ask for another one.
664edb901aaSMarcel Telka */
665edb901aaSMarcel Telka zfs_close(zhp);
666edb901aaSMarcel Telka return (0);
667edb901aaSMarcel Telka }
668edb901aaSMarcel Telka
669fa9e4066Sahrens /*
670edb901aaSMarcel Telka * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
671fa9e4066Sahrens * argument is a mask of acceptable types. The function will print an
672fa9e4066Sahrens * appropriate error message and return NULL if it can't be opened.
673fa9e4066Sahrens */
674fa9e4066Sahrens zfs_handle_t *
zfs_open(libzfs_handle_t * hdl,const char * path,int types)67599653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types)
676fa9e4066Sahrens {
677fa9e4066Sahrens zfs_handle_t *zhp;
67899653d4eSeschrock char errbuf[1024];
679edb901aaSMarcel Telka char *bookp;
68099653d4eSeschrock
68199653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf),
68299653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
683fa9e4066Sahrens
684fa9e4066Sahrens /*
68599653d4eSeschrock * Validate the name before we even try to open it.
686fa9e4066Sahrens */
687edb901aaSMarcel Telka if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
68899653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
689fa9e4066Sahrens return (NULL);
690fa9e4066Sahrens }
691fa9e4066Sahrens
692fa9e4066Sahrens /*
693edb901aaSMarcel Telka * Bookmarks needs to be handled separately.
694fa9e4066Sahrens */
695edb901aaSMarcel Telka bookp = strchr(path, '#');
696edb901aaSMarcel Telka if (bookp == NULL) {
697edb901aaSMarcel Telka /*
698edb901aaSMarcel Telka * Try to get stats for the dataset, which will tell us if it
699edb901aaSMarcel Telka * exists.
700edb901aaSMarcel Telka */
701edb901aaSMarcel Telka errno = 0;
702edb901aaSMarcel Telka if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
703edb901aaSMarcel Telka (void) zfs_standard_error(hdl, errno, errbuf);
704edb901aaSMarcel Telka return (NULL);
705edb901aaSMarcel Telka }
706edb901aaSMarcel Telka } else {
707edb901aaSMarcel Telka char dsname[ZFS_MAX_DATASET_NAME_LEN];
708edb901aaSMarcel Telka zfs_handle_t *pzhp;
709edb901aaSMarcel Telka struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
710edb901aaSMarcel Telka
711edb901aaSMarcel Telka /*
712edb901aaSMarcel Telka * We need to cut out '#' and everything after '#'
713edb901aaSMarcel Telka * to get the parent dataset name only.
714edb901aaSMarcel Telka */
715edb901aaSMarcel Telka assert(bookp - path < sizeof (dsname));
716edb901aaSMarcel Telka (void) strncpy(dsname, path, bookp - path);
717edb901aaSMarcel Telka dsname[bookp - path] = '\0';
718edb901aaSMarcel Telka
719edb901aaSMarcel Telka /*
720edb901aaSMarcel Telka * Create handle for the parent dataset.
721edb901aaSMarcel Telka */
722edb901aaSMarcel Telka errno = 0;
723edb901aaSMarcel Telka if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
724edb901aaSMarcel Telka (void) zfs_standard_error(hdl, errno, errbuf);
725edb901aaSMarcel Telka return (NULL);
726edb901aaSMarcel Telka }
727edb901aaSMarcel Telka
728edb901aaSMarcel Telka /*
729edb901aaSMarcel Telka * Iterate bookmarks to find the right one.
730edb901aaSMarcel Telka */
731edb901aaSMarcel Telka errno = 0;
732edb901aaSMarcel Telka if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
733edb901aaSMarcel Telka &cb_data) == 0) && (cb_data.zhp == NULL)) {
734edb901aaSMarcel Telka (void) zfs_error(hdl, EZFS_NOENT, errbuf);
735edb901aaSMarcel Telka zfs_close(pzhp);
736edb901aaSMarcel Telka return (NULL);
737edb901aaSMarcel Telka }
738edb901aaSMarcel Telka if (cb_data.zhp == NULL) {
739edb901aaSMarcel Telka (void) zfs_standard_error(hdl, errno, errbuf);
740edb901aaSMarcel Telka zfs_close(pzhp);
741edb901aaSMarcel Telka return (NULL);
742edb901aaSMarcel Telka }
743edb901aaSMarcel Telka zhp = cb_data.zhp;
744edb901aaSMarcel Telka
745edb901aaSMarcel Telka /*
746edb901aaSMarcel Telka * Cleanup.
747edb901aaSMarcel Telka */
748edb901aaSMarcel Telka zfs_close(pzhp);
749fa9e4066Sahrens }
750fa9e4066Sahrens
751fa9e4066Sahrens if (!(types & zhp->zfs_type)) {
75299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
75394de1d4cSeschrock zfs_close(zhp);
754fa9e4066Sahrens return (NULL);
755fa9e4066Sahrens }
756fa9e4066Sahrens
757fa9e4066Sahrens return (zhp);
758fa9e4066Sahrens }
759fa9e4066Sahrens
760fa9e4066Sahrens /*
761fa9e4066Sahrens * Release a ZFS handle. Nothing to do but free the associated memory.
762fa9e4066Sahrens */
763fa9e4066Sahrens void
zfs_close(zfs_handle_t * zhp)764fa9e4066Sahrens zfs_close(zfs_handle_t *zhp)
765fa9e4066Sahrens {
766fa9e4066Sahrens if (zhp->zfs_mntopts)
767fa9e4066Sahrens free(zhp->zfs_mntopts);
768e9dbad6fSeschrock nvlist_free(zhp->zfs_props);
769e9dbad6fSeschrock nvlist_free(zhp->zfs_user_props);
77092241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props);
771fa9e4066Sahrens free(zhp);
772fa9e4066Sahrens }
773fa9e4066Sahrens
774ebedde84SEric Taylor typedef struct mnttab_node {
775ebedde84SEric Taylor struct mnttab mtn_mt;
776ebedde84SEric Taylor avl_node_t mtn_node;
777ebedde84SEric Taylor } mnttab_node_t;
778ebedde84SEric Taylor
779ebedde84SEric Taylor static int
libzfs_mnttab_cache_compare(const void * arg1,const void * arg2)780ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
781ebedde84SEric Taylor {
782c4ab0d3fSGvozden Neskovic const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1;
783c4ab0d3fSGvozden Neskovic const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2;
784ebedde84SEric Taylor int rv;
785ebedde84SEric Taylor
786ebedde84SEric Taylor rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
787ebedde84SEric Taylor
7884d7988d6SPaul Dagnelie if (rv == 0)
7894d7988d6SPaul Dagnelie return (0);
7904d7988d6SPaul Dagnelie return (rv > 0 ? 1 : -1);
791ebedde84SEric Taylor }
792ebedde84SEric Taylor
793ebedde84SEric Taylor void
libzfs_mnttab_init(libzfs_handle_t * hdl)794ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl)
795ebedde84SEric Taylor {
796591e0e13SSebastien Roy (void) mutex_init(&hdl->libzfs_mnttab_cache_lock,
797591e0e13SSebastien Roy LOCK_NORMAL | LOCK_ERRORCHECK, NULL);
798ebedde84SEric Taylor assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
799ebedde84SEric Taylor avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
800ebedde84SEric Taylor sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
801b2634b9cSEric Taylor }
802b2634b9cSEric Taylor
803b2634b9cSEric Taylor void
libzfs_mnttab_update(libzfs_handle_t * hdl)804b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl)
805b2634b9cSEric Taylor {
806b2634b9cSEric Taylor struct mnttab entry;
807ebedde84SEric Taylor
808ebedde84SEric Taylor rewind(hdl->libzfs_mnttab);
809ebedde84SEric Taylor while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
810ebedde84SEric Taylor mnttab_node_t *mtn;
811ebedde84SEric Taylor
812ebedde84SEric Taylor if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
813ebedde84SEric Taylor continue;
814ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
815ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
816ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
817ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
818ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
819ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn);
820ebedde84SEric Taylor }
821ebedde84SEric Taylor }
822ebedde84SEric Taylor
823ebedde84SEric Taylor void
libzfs_mnttab_fini(libzfs_handle_t * hdl)824ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl)
825ebedde84SEric Taylor {
826ebedde84SEric Taylor void *cookie = NULL;
827ebedde84SEric Taylor mnttab_node_t *mtn;
828ebedde84SEric Taylor
82988f61deeSIgor Kozhukhov while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
83088f61deeSIgor Kozhukhov != NULL) {
831ebedde84SEric Taylor free(mtn->mtn_mt.mnt_special);
832ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mountp);
833ebedde84SEric Taylor free(mtn->mtn_mt.mnt_fstype);
834ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mntopts);
835ebedde84SEric Taylor free(mtn);
836ebedde84SEric Taylor }
837ebedde84SEric Taylor avl_destroy(&hdl->libzfs_mnttab_cache);
838591e0e13SSebastien Roy (void) mutex_destroy(&hdl->libzfs_mnttab_cache_lock);
839ebedde84SEric Taylor }
840ebedde84SEric Taylor
841b2634b9cSEric Taylor void
libzfs_mnttab_cache(libzfs_handle_t * hdl,boolean_t enable)842b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
843b2634b9cSEric Taylor {
844b2634b9cSEric Taylor hdl->libzfs_mnttab_enable = enable;
845b2634b9cSEric Taylor }
846b2634b9cSEric Taylor
847ebedde84SEric Taylor int
libzfs_mnttab_find(libzfs_handle_t * hdl,const char * fsname,struct mnttab * entry)848ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
849ebedde84SEric Taylor struct mnttab *entry)
850ebedde84SEric Taylor {
851ebedde84SEric Taylor mnttab_node_t find;
852ebedde84SEric Taylor mnttab_node_t *mtn;
853591e0e13SSebastien Roy int ret = ENOENT;
854ebedde84SEric Taylor
855b2634b9cSEric Taylor if (!hdl->libzfs_mnttab_enable) {
856b2634b9cSEric Taylor struct mnttab srch = { 0 };
857b2634b9cSEric Taylor
858b2634b9cSEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache))
859b2634b9cSEric Taylor libzfs_mnttab_fini(hdl);
860b2634b9cSEric Taylor rewind(hdl->libzfs_mnttab);
861b2634b9cSEric Taylor srch.mnt_special = (char *)fsname;
862b2634b9cSEric Taylor srch.mnt_fstype = MNTTYPE_ZFS;
863b2634b9cSEric Taylor if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
864b2634b9cSEric Taylor return (0);
865b2634b9cSEric Taylor else
866b2634b9cSEric Taylor return (ENOENT);
867b2634b9cSEric Taylor }
868b2634b9cSEric Taylor
869591e0e13SSebastien Roy mutex_enter(&hdl->libzfs_mnttab_cache_lock);
870ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
871b2634b9cSEric Taylor libzfs_mnttab_update(hdl);
872ebedde84SEric Taylor
873ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname;
874ebedde84SEric Taylor mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
875ebedde84SEric Taylor if (mtn) {
876ebedde84SEric Taylor *entry = mtn->mtn_mt;
877591e0e13SSebastien Roy ret = 0;
878ebedde84SEric Taylor }
879591e0e13SSebastien Roy mutex_exit(&hdl->libzfs_mnttab_cache_lock);
880591e0e13SSebastien Roy return (ret);
881ebedde84SEric Taylor }
882ebedde84SEric Taylor
883ebedde84SEric Taylor void
libzfs_mnttab_add(libzfs_handle_t * hdl,const char * special,const char * mountp,const char * mntopts)884ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
885ebedde84SEric Taylor const char *mountp, const char *mntopts)
886ebedde84SEric Taylor {
887ebedde84SEric Taylor mnttab_node_t *mtn;
888ebedde84SEric Taylor
889591e0e13SSebastien Roy mutex_enter(&hdl->libzfs_mnttab_cache_lock);
890591e0e13SSebastien Roy if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) {
891591e0e13SSebastien Roy mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
892591e0e13SSebastien Roy mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
893591e0e13SSebastien Roy mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
894591e0e13SSebastien Roy mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
895591e0e13SSebastien Roy mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
896591e0e13SSebastien Roy avl_add(&hdl->libzfs_mnttab_cache, mtn);
897591e0e13SSebastien Roy }
898591e0e13SSebastien Roy mutex_exit(&hdl->libzfs_mnttab_cache_lock);
899ebedde84SEric Taylor }
900ebedde84SEric Taylor
901ebedde84SEric Taylor void
libzfs_mnttab_remove(libzfs_handle_t * hdl,const char * fsname)902ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
903ebedde84SEric Taylor {
904ebedde84SEric Taylor mnttab_node_t find;
905ebedde84SEric Taylor mnttab_node_t *ret;
906ebedde84SEric Taylor
907591e0e13SSebastien Roy mutex_enter(&hdl->libzfs_mnttab_cache_lock);
908ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname;
90988f61deeSIgor Kozhukhov if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
91088f61deeSIgor Kozhukhov != NULL) {
911ebedde84SEric Taylor avl_remove(&hdl->libzfs_mnttab_cache, ret);
912ebedde84SEric Taylor free(ret->mtn_mt.mnt_special);
913ebedde84SEric Taylor free(ret->mtn_mt.mnt_mountp);
914ebedde84SEric Taylor free(ret->mtn_mt.mnt_fstype);
915ebedde84SEric Taylor free(ret->mtn_mt.mnt_mntopts);
916ebedde84SEric Taylor free(ret);
917ebedde84SEric Taylor }
918591e0e13SSebastien Roy mutex_exit(&hdl->libzfs_mnttab_cache_lock);
919ebedde84SEric Taylor }
920ebedde84SEric Taylor
9217b97dc1aSrm int
zfs_spa_version(zfs_handle_t * zhp,int * spa_version)9227b97dc1aSrm zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
9237b97dc1aSrm {
92429ab75c9Srm zpool_handle_t *zpool_handle = zhp->zpool_hdl;
9257b97dc1aSrm
9267b97dc1aSrm if (zpool_handle == NULL)
9277b97dc1aSrm return (-1);
9287b97dc1aSrm
9297b97dc1aSrm *spa_version = zpool_get_prop_int(zpool_handle,
9307b97dc1aSrm ZPOOL_PROP_VERSION, NULL);
9317b97dc1aSrm return (0);
9327b97dc1aSrm }
9337b97dc1aSrm
9347b97dc1aSrm /*
9357b97dc1aSrm * The choice of reservation property depends on the SPA version.
9367b97dc1aSrm */
9377b97dc1aSrm static int
zfs_which_resv_prop(zfs_handle_t * zhp,zfs_prop_t * resv_prop)9387b97dc1aSrm zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
9397b97dc1aSrm {
9407b97dc1aSrm int spa_version;
9417b97dc1aSrm
9427b97dc1aSrm if (zfs_spa_version(zhp, &spa_version) < 0)
9437b97dc1aSrm return (-1);
9447b97dc1aSrm
9457b97dc1aSrm if (spa_version >= SPA_VERSION_REFRESERVATION)
9467b97dc1aSrm *resv_prop = ZFS_PROP_REFRESERVATION;
9477b97dc1aSrm else
9487b97dc1aSrm *resv_prop = ZFS_PROP_RESERVATION;
9497b97dc1aSrm
9507b97dc1aSrm return (0);
9517b97dc1aSrm }
9527b97dc1aSrm
953e9dbad6fSeschrock /*
954e9dbad6fSeschrock * Given an nvlist of properties to set, validates that they are correct, and
955e9dbad6fSeschrock * parses any numeric properties (index, boolean, etc) if they are specified as
956e9dbad6fSeschrock * strings.
957e9dbad6fSeschrock */
9580a48a24eStimh nvlist_t *
zfs_valid_proplist(libzfs_handle_t * hdl,zfs_type_t type,nvlist_t * nvl,uint64_t zoned,zfs_handle_t * zhp,zpool_handle_t * zpool_hdl,boolean_t key_params_ok,const char * errbuf)9590a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
960e9316f76SJoe Stein uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
961eb633035STom Caputi boolean_t key_params_ok, const char *errbuf)
962e9dbad6fSeschrock {
963e9dbad6fSeschrock nvpair_t *elem;
964e9dbad6fSeschrock uint64_t intval;
965e9dbad6fSeschrock char *strval;
966990b4856Slling zfs_prop_t prop;
967e9dbad6fSeschrock nvlist_t *ret;
968da6c28aaSamw int chosen_normal = -1;
969da6c28aaSamw int chosen_utf = -1;
970e9dbad6fSeschrock
971990b4856Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
972990b4856Slling (void) no_memory(hdl);
973990b4856Slling return (NULL);
974e9dbad6fSeschrock }
975e9dbad6fSeschrock
97614843421SMatthew Ahrens /*
97714843421SMatthew Ahrens * Make sure this property is valid and applies to this type.
97814843421SMatthew Ahrens */
97914843421SMatthew Ahrens
980e9dbad6fSeschrock elem = NULL;
981e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
982990b4856Slling const char *propname = nvpair_name(elem);
983e9dbad6fSeschrock
98414843421SMatthew Ahrens prop = zfs_name_to_prop(propname);
98514843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
986990b4856Slling /*
98714843421SMatthew Ahrens * This is a user property: make sure it's a
988990b4856Slling * string, and that it's less than ZAP_MAXNAMELEN.
989990b4856Slling */
990990b4856Slling if (nvpair_type(elem) != DATA_TYPE_STRING) {
991990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
992990b4856Slling "'%s' must be a string"), propname);
993990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
994990b4856Slling goto error;
995990b4856Slling }
996990b4856Slling
997990b4856Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
998990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
999990b4856Slling "property name '%s' is too long"),
1000990b4856Slling propname);
1001990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1002990b4856Slling goto error;
1003fa9e4066Sahrens }
1004fa9e4066Sahrens
1005e9dbad6fSeschrock (void) nvpair_value_string(elem, &strval);
1006e9dbad6fSeschrock if (nvlist_add_string(ret, propname, strval) != 0) {
1007e9dbad6fSeschrock (void) no_memory(hdl);
1008e9dbad6fSeschrock goto error;
1009e9dbad6fSeschrock }
1010e9dbad6fSeschrock continue;
1011e9dbad6fSeschrock }
1012e9dbad6fSeschrock
101314843421SMatthew Ahrens /*
101414843421SMatthew Ahrens * Currently, only user properties can be modified on
101514843421SMatthew Ahrens * snapshots.
101614843421SMatthew Ahrens */
1017bb0ade09Sahrens if (type == ZFS_TYPE_SNAPSHOT) {
1018bb0ade09Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1019bb0ade09Sahrens "this property can not be modified for snapshots"));
1020bb0ade09Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1021bb0ade09Sahrens goto error;
1022bb0ade09Sahrens }
1023bb0ade09Sahrens
102414843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
102514843421SMatthew Ahrens zfs_userquota_prop_t uqtype;
102614843421SMatthew Ahrens char newpropname[128];
102714843421SMatthew Ahrens char domain[128];
102814843421SMatthew Ahrens uint64_t rid;
102914843421SMatthew Ahrens uint64_t valary[3];
103014843421SMatthew Ahrens
103114843421SMatthew Ahrens if (userquota_propname_decode(propname, zoned,
103214843421SMatthew Ahrens &uqtype, domain, sizeof (domain), &rid) != 0) {
103314843421SMatthew Ahrens zfs_error_aux(hdl,
103414843421SMatthew Ahrens dgettext(TEXT_DOMAIN,
103514843421SMatthew Ahrens "'%s' has an invalid user/group name"),
103614843421SMatthew Ahrens propname);
103714843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
103814843421SMatthew Ahrens goto error;
103914843421SMatthew Ahrens }
104014843421SMatthew Ahrens
104114843421SMatthew Ahrens if (uqtype != ZFS_PROP_USERQUOTA &&
1042f67950b2SNasf-Fan uqtype != ZFS_PROP_GROUPQUOTA &&
1043f67950b2SNasf-Fan uqtype != ZFS_PROP_USEROBJQUOTA &&
1044f67950b2SNasf-Fan uqtype != ZFS_PROP_GROUPOBJQUOTA &&
1045f67950b2SNasf-Fan uqtype != ZFS_PROP_PROJECTQUOTA &&
1046f67950b2SNasf-Fan uqtype != ZFS_PROP_PROJECTOBJQUOTA) {
104714843421SMatthew Ahrens zfs_error_aux(hdl,
104814843421SMatthew Ahrens dgettext(TEXT_DOMAIN, "'%s' is readonly"),
104914843421SMatthew Ahrens propname);
105014843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY,
105114843421SMatthew Ahrens errbuf);
105214843421SMatthew Ahrens goto error;
105314843421SMatthew Ahrens }
105414843421SMatthew Ahrens
105514843421SMatthew Ahrens if (nvpair_type(elem) == DATA_TYPE_STRING) {
105614843421SMatthew Ahrens (void) nvpair_value_string(elem, &strval);
105714843421SMatthew Ahrens if (strcmp(strval, "none") == 0) {
105814843421SMatthew Ahrens intval = 0;
105914843421SMatthew Ahrens } else if (zfs_nicestrtonum(hdl,
106014843421SMatthew Ahrens strval, &intval) != 0) {
106114843421SMatthew Ahrens (void) zfs_error(hdl,
106214843421SMatthew Ahrens EZFS_BADPROP, errbuf);
106314843421SMatthew Ahrens goto error;
106414843421SMatthew Ahrens }
106514843421SMatthew Ahrens } else if (nvpair_type(elem) ==
106614843421SMatthew Ahrens DATA_TYPE_UINT64) {
106714843421SMatthew Ahrens (void) nvpair_value_uint64(elem, &intval);
106814843421SMatthew Ahrens if (intval == 0) {
106914843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
107014843421SMatthew Ahrens "use 'none' to disable "
1071f67950b2SNasf-Fan "{user|group|project}quota"));
107214843421SMatthew Ahrens goto error;
107314843421SMatthew Ahrens }
107414843421SMatthew Ahrens } else {
107514843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
107614843421SMatthew Ahrens "'%s' must be a number"), propname);
107714843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
107814843421SMatthew Ahrens goto error;
107914843421SMatthew Ahrens }
108014843421SMatthew Ahrens
10812d5843dbSMatthew Ahrens /*
10822d5843dbSMatthew Ahrens * Encode the prop name as
10832d5843dbSMatthew Ahrens * userquota@<hex-rid>-domain, to make it easy
10842d5843dbSMatthew Ahrens * for the kernel to decode.
10852d5843dbSMatthew Ahrens */
108614843421SMatthew Ahrens (void) snprintf(newpropname, sizeof (newpropname),
10872d5843dbSMatthew Ahrens "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
10882d5843dbSMatthew Ahrens (longlong_t)rid, domain);
108914843421SMatthew Ahrens valary[0] = uqtype;
109014843421SMatthew Ahrens valary[1] = rid;
109114843421SMatthew Ahrens valary[2] = intval;
109214843421SMatthew Ahrens if (nvlist_add_uint64_array(ret, newpropname,
109314843421SMatthew Ahrens valary, 3) != 0) {
109414843421SMatthew Ahrens (void) no_memory(hdl);
109514843421SMatthew Ahrens goto error;
109614843421SMatthew Ahrens }
109714843421SMatthew Ahrens continue;
109819b94df9SMatthew Ahrens } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
109919b94df9SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
110019b94df9SMatthew Ahrens "'%s' is readonly"),
110119b94df9SMatthew Ahrens propname);
110219b94df9SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
110319b94df9SMatthew Ahrens goto error;
110414843421SMatthew Ahrens }
110514843421SMatthew Ahrens
110614843421SMatthew Ahrens if (prop == ZPROP_INVAL) {
110714843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
110814843421SMatthew Ahrens "invalid property '%s'"), propname);
110914843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
111014843421SMatthew Ahrens goto error;
111114843421SMatthew Ahrens }
111214843421SMatthew Ahrens
11134c2bdae2STim Chase if (!zfs_prop_valid_for_type(prop, type, B_FALSE)) {
1114e9dbad6fSeschrock zfs_error_aux(hdl,
1115e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' does not "
1116e9dbad6fSeschrock "apply to datasets of this type"), propname);
1117e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1118e9dbad6fSeschrock goto error;
1119e9dbad6fSeschrock }
1120e9dbad6fSeschrock
1121e9dbad6fSeschrock if (zfs_prop_readonly(prop) &&
1122eb633035STom Caputi !(zfs_prop_setonce(prop) && zhp == NULL) &&
1123eb633035STom Caputi !(zfs_prop_encryption_key_param(prop) && key_params_ok)) {
1124e9dbad6fSeschrock zfs_error_aux(hdl,
1125e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1126e9dbad6fSeschrock propname);
1127e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1128e9dbad6fSeschrock goto error;
1129e9dbad6fSeschrock }
1130e9dbad6fSeschrock
1131990b4856Slling if (zprop_parse_value(hdl, elem, prop, type, ret,
1132990b4856Slling &strval, &intval, errbuf) != 0)
1133e9dbad6fSeschrock goto error;
1134fa9e4066Sahrens
1135e9dbad6fSeschrock /*
1136e9dbad6fSeschrock * Perform some additional checks for specific properties.
1137e9dbad6fSeschrock */
1138e9dbad6fSeschrock switch (prop) {
1139e7437265Sahrens case ZFS_PROP_VERSION:
1140e7437265Sahrens {
1141e7437265Sahrens int version;
1142e7437265Sahrens
1143e7437265Sahrens if (zhp == NULL)
1144e7437265Sahrens break;
1145e7437265Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1146e7437265Sahrens if (intval < version) {
1147e7437265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1148e7437265Sahrens "Can not downgrade; already at version %u"),
1149e7437265Sahrens version);
1150e7437265Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1151e7437265Sahrens goto error;
1152e7437265Sahrens }
1153e7437265Sahrens break;
1154e7437265Sahrens }
1155e7437265Sahrens
1156e9dbad6fSeschrock case ZFS_PROP_VOLBLOCKSIZE:
1157b5152584SMatthew Ahrens case ZFS_PROP_RECORDSIZE:
1158b5152584SMatthew Ahrens {
1159b5152584SMatthew Ahrens int maxbs = SPA_MAXBLOCKSIZE;
1160*6520eed5SToomas Soome char buf[64];
1161*6520eed5SToomas Soome
1162e9316f76SJoe Stein if (zpool_hdl != NULL) {
1163e9316f76SJoe Stein maxbs = zpool_get_prop_int(zpool_hdl,
1164b5152584SMatthew Ahrens ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1165b5152584SMatthew Ahrens }
1166b5152584SMatthew Ahrens /*
1167b5152584SMatthew Ahrens * Volumes are limited to a volblocksize of 128KB,
1168b5152584SMatthew Ahrens * because they typically service workloads with
1169b5152584SMatthew Ahrens * small random writes, which incur a large performance
1170b5152584SMatthew Ahrens * penalty with large blocks.
1171b5152584SMatthew Ahrens */
1172b5152584SMatthew Ahrens if (prop == ZFS_PROP_VOLBLOCKSIZE)
1173b5152584SMatthew Ahrens maxbs = SPA_OLD_MAXBLOCKSIZE;
1174b5152584SMatthew Ahrens /*
1175b5152584SMatthew Ahrens * The value must be a power of two between
1176b5152584SMatthew Ahrens * SPA_MINBLOCKSIZE and maxbs.
1177b5152584SMatthew Ahrens */
1178e9dbad6fSeschrock if (intval < SPA_MINBLOCKSIZE ||
1179b5152584SMatthew Ahrens intval > maxbs || !ISP2(intval)) {
1180*6520eed5SToomas Soome zfs_nicebytes(maxbs, buf, sizeof (buf));
118199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1182b5152584SMatthew Ahrens "'%s' must be power of 2 from 512B "
1183*6520eed5SToomas Soome "to %s"), propname, buf);
1184e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1185e9dbad6fSeschrock goto error;
1186fa9e4066Sahrens }
1187fa9e4066Sahrens break;
1188b5152584SMatthew Ahrens }
1189663207adSDon Brady
1190663207adSDon Brady case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
1191663207adSDon Brady if (zpool_hdl != NULL) {
1192663207adSDon Brady char state[64] = "";
1193663207adSDon Brady
1194663207adSDon Brady /*
1195663207adSDon Brady * Issue a warning but do not fail so that
1196663207adSDon Brady * tests for setable properties succeed.
1197663207adSDon Brady */
1198663207adSDon Brady if (zpool_prop_get_feature(zpool_hdl,
1199663207adSDon Brady "feature@allocation_classes", state,
1200663207adSDon Brady sizeof (state)) != 0 ||
1201663207adSDon Brady strcmp(state, ZFS_FEATURE_ACTIVE) != 0) {
1202663207adSDon Brady (void) fprintf(stderr, gettext(
1203663207adSDon Brady "%s: property requires a special "
1204663207adSDon Brady "device in the pool\n"), propname);
1205663207adSDon Brady }
1206663207adSDon Brady }
1207663207adSDon Brady if (intval != 0 &&
1208663207adSDon Brady (intval < SPA_MINBLOCKSIZE ||
1209663207adSDon Brady intval > SPA_OLD_MAXBLOCKSIZE || !ISP2(intval))) {
1210663207adSDon Brady zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1211663207adSDon Brady "invalid '%s=%d' property: must be zero or "
1212663207adSDon Brady "a power of 2 from 512B to 128K"), propname,
1213663207adSDon Brady intval);
1214663207adSDon Brady (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1215663207adSDon Brady goto error;
1216663207adSDon Brady }
1217663207adSDon Brady break;
1218663207adSDon Brady
12194201a95eSRic Aleshire case ZFS_PROP_MLSLABEL:
12204201a95eSRic Aleshire {
12214201a95eSRic Aleshire /*
12224201a95eSRic Aleshire * Verify the mlslabel string and convert to
12234201a95eSRic Aleshire * internal hex label string.
12244201a95eSRic Aleshire */
12254201a95eSRic Aleshire
12264201a95eSRic Aleshire m_label_t *new_sl;
12274201a95eSRic Aleshire char *hex = NULL; /* internal label string */
12284201a95eSRic Aleshire
12294201a95eSRic Aleshire /* Default value is already OK. */
12304201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
12314201a95eSRic Aleshire break;
12324201a95eSRic Aleshire
12334201a95eSRic Aleshire /* Verify the label can be converted to binary form */
12344201a95eSRic Aleshire if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
12354201a95eSRic Aleshire (str_to_label(strval, &new_sl, MAC_LABEL,
12364201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1)) {
12374201a95eSRic Aleshire goto badlabel;
12384201a95eSRic Aleshire }
12394201a95eSRic Aleshire
12404201a95eSRic Aleshire /* Now translate to hex internal label string */
12414201a95eSRic Aleshire if (label_to_str(new_sl, &hex, M_INTERNAL,
12424201a95eSRic Aleshire DEF_NAMES) != 0) {
12434201a95eSRic Aleshire if (hex)
12444201a95eSRic Aleshire free(hex);
12454201a95eSRic Aleshire goto badlabel;
12464201a95eSRic Aleshire }
12474201a95eSRic Aleshire m_label_free(new_sl);
12484201a95eSRic Aleshire
12494201a95eSRic Aleshire /* If string is already in internal form, we're done. */
12504201a95eSRic Aleshire if (strcmp(strval, hex) == 0) {
12514201a95eSRic Aleshire free(hex);
12524201a95eSRic Aleshire break;
12534201a95eSRic Aleshire }
12544201a95eSRic Aleshire
12554201a95eSRic Aleshire /* Replace the label string with the internal form. */
1256569038c9SRic Aleshire (void) nvlist_remove(ret, zfs_prop_to_name(prop),
12574201a95eSRic Aleshire DATA_TYPE_STRING);
12584201a95eSRic Aleshire verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
12594201a95eSRic Aleshire hex) == 0);
12604201a95eSRic Aleshire free(hex);
12614201a95eSRic Aleshire
12624201a95eSRic Aleshire break;
12634201a95eSRic Aleshire
12644201a95eSRic Aleshire badlabel:
12654201a95eSRic Aleshire zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12664201a95eSRic Aleshire "invalid mlslabel '%s'"), strval);
12674201a95eSRic Aleshire (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
12684201a95eSRic Aleshire m_label_free(new_sl); /* OK if null */
12694201a95eSRic Aleshire goto error;
12704201a95eSRic Aleshire
12714201a95eSRic Aleshire }
12724201a95eSRic Aleshire
1273e9dbad6fSeschrock case ZFS_PROP_MOUNTPOINT:
127489eef05eSrm {
127589eef05eSrm namecheck_err_t why;
127689eef05eSrm
1277e9dbad6fSeschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1278e9dbad6fSeschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1279e9dbad6fSeschrock break;
1280fa9e4066Sahrens
128189eef05eSrm if (mountpoint_namecheck(strval, &why)) {
128289eef05eSrm switch (why) {
128389eef05eSrm case NAME_ERR_LEADING_SLASH:
128489eef05eSrm zfs_error_aux(hdl,
128589eef05eSrm dgettext(TEXT_DOMAIN,
128689eef05eSrm "'%s' must be an absolute path, "
128789eef05eSrm "'none', or 'legacy'"), propname);
128889eef05eSrm break;
128989eef05eSrm case NAME_ERR_TOOLONG:
129089eef05eSrm zfs_error_aux(hdl,
129189eef05eSrm dgettext(TEXT_DOMAIN,
129289eef05eSrm "component of '%s' is too long"),
129389eef05eSrm propname);
129489eef05eSrm break;
129588f61deeSIgor Kozhukhov
129688f61deeSIgor Kozhukhov default:
129788f61deeSIgor Kozhukhov zfs_error_aux(hdl,
129888f61deeSIgor Kozhukhov dgettext(TEXT_DOMAIN,
129988f61deeSIgor Kozhukhov "(%d) not defined"),
130088f61deeSIgor Kozhukhov why);
130188f61deeSIgor Kozhukhov break;
130289eef05eSrm }
1303e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1304e9dbad6fSeschrock goto error;
1305fa9e4066Sahrens }
130689eef05eSrm }
130789eef05eSrm
1308f3861e1aSahl /*FALLTHRU*/
1309fa9e4066Sahrens
1310da6c28aaSamw case ZFS_PROP_SHARESMB:
1311f3861e1aSahl case ZFS_PROP_SHARENFS:
1312f3861e1aSahl /*
1313da6c28aaSamw * For the mountpoint and sharenfs or sharesmb
1314da6c28aaSamw * properties, check if it can be set in a
1315da6c28aaSamw * global/non-global zone based on
1316f3861e1aSahl * the zoned property value:
1317f3861e1aSahl *
1318f3861e1aSahl * global zone non-global zone
1319f3861e1aSahl * --------------------------------------------------
1320f3861e1aSahl * zoned=on mountpoint (no) mountpoint (yes)
13210dfe541eSEvan Layton * sharenfs (no) sharenfs (yes)
13220dfe541eSEvan Layton * sharesmb (no) sharesmb (yes)
1323f3861e1aSahl *
1324f3861e1aSahl * zoned=off mountpoint (yes) N/A
1325f3861e1aSahl * sharenfs (yes)
1326da6c28aaSamw * sharesmb (yes)
1327f3861e1aSahl */
1328e9dbad6fSeschrock if (zoned) {
1329e9dbad6fSeschrock if (getzoneid() == GLOBAL_ZONEID) {
1330e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1331e9dbad6fSeschrock "'%s' cannot be set on "
1332e9dbad6fSeschrock "dataset in a non-global zone"),
1333e9dbad6fSeschrock propname);
1334e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED,
1335e9dbad6fSeschrock errbuf);
1336e9dbad6fSeschrock goto error;
1337fa9e4066Sahrens }
1338e9dbad6fSeschrock } else if (getzoneid() != GLOBAL_ZONEID) {
1339e9dbad6fSeschrock /*
1340e9dbad6fSeschrock * If zoned property is 'off', this must be in
134114843421SMatthew Ahrens * a global zone. If not, something is wrong.
1342e9dbad6fSeschrock */
1343e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1344e9dbad6fSeschrock "'%s' cannot be set while dataset "
1345e9dbad6fSeschrock "'zoned' property is set"), propname);
1346e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1347e9dbad6fSeschrock goto error;
1348fa9e4066Sahrens }
1349f3861e1aSahl
135067331909Sdougm /*
135167331909Sdougm * At this point, it is legitimate to set the
135267331909Sdougm * property. Now we want to make sure that the
135367331909Sdougm * property value is valid if it is sharenfs.
135467331909Sdougm */
1355da6c28aaSamw if ((prop == ZFS_PROP_SHARENFS ||
1356da6c28aaSamw prop == ZFS_PROP_SHARESMB) &&
1357fac3008cSeschrock strcmp(strval, "on") != 0 &&
1358fac3008cSeschrock strcmp(strval, "off") != 0) {
1359da6c28aaSamw zfs_share_proto_t proto;
1360da6c28aaSamw
1361da6c28aaSamw if (prop == ZFS_PROP_SHARESMB)
1362da6c28aaSamw proto = PROTO_SMB;
1363da6c28aaSamw else
1364da6c28aaSamw proto = PROTO_NFS;
136567331909Sdougm
136667331909Sdougm /*
1367da6c28aaSamw * Must be an valid sharing protocol
1368da6c28aaSamw * option string so init the libshare
1369da6c28aaSamw * in order to enable the parser and
1370da6c28aaSamw * then parse the options. We use the
1371da6c28aaSamw * control API since we don't care about
1372da6c28aaSamw * the current configuration and don't
137367331909Sdougm * want the overhead of loading it
137467331909Sdougm * until we actually do something.
137567331909Sdougm */
137667331909Sdougm
1377fac3008cSeschrock if (zfs_init_libshare(hdl,
1378fac3008cSeschrock SA_INIT_CONTROL_API) != SA_OK) {
1379fac3008cSeschrock /*
1380fac3008cSeschrock * An error occurred so we can't do
1381fac3008cSeschrock * anything
1382fac3008cSeschrock */
1383fac3008cSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1384fac3008cSeschrock "'%s' cannot be set: problem "
1385fac3008cSeschrock "in share initialization"),
1386fac3008cSeschrock propname);
1387fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP,
1388fac3008cSeschrock errbuf);
1389fac3008cSeschrock goto error;
1390fac3008cSeschrock }
139167331909Sdougm
1392da6c28aaSamw if (zfs_parse_options(strval, proto) != SA_OK) {
1393fac3008cSeschrock /*
1394fac3008cSeschrock * There was an error in parsing so
1395fac3008cSeschrock * deal with it by issuing an error
1396fac3008cSeschrock * message and leaving after
1397fac3008cSeschrock * uninitializing the the libshare
1398fac3008cSeschrock * interface.
1399fac3008cSeschrock */
1400fac3008cSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1401fac3008cSeschrock "'%s' cannot be set to invalid "
1402fac3008cSeschrock "options"), propname);
1403fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP,
1404fac3008cSeschrock errbuf);
1405fac3008cSeschrock zfs_uninit_libshare(hdl);
1406fac3008cSeschrock goto error;
1407fac3008cSeschrock }
140867331909Sdougm zfs_uninit_libshare(hdl);
140967331909Sdougm }
141067331909Sdougm
1411da6c28aaSamw break;
141288f61deeSIgor Kozhukhov
1413eb633035STom Caputi case ZFS_PROP_KEYLOCATION:
1414eb633035STom Caputi if (!zfs_prop_valid_keylocation(strval, B_FALSE)) {
1415eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1416eb633035STom Caputi "invalid keylocation"));
1417eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1418eb633035STom Caputi goto error;
1419eb633035STom Caputi }
1420eb633035STom Caputi
1421eb633035STom Caputi if (zhp != NULL) {
1422eb633035STom Caputi uint64_t crypt =
1423eb633035STom Caputi zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1424eb633035STom Caputi
1425eb633035STom Caputi if (crypt == ZIO_CRYPT_OFF &&
1426eb633035STom Caputi strcmp(strval, "none") != 0) {
1427eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1428eb633035STom Caputi "keylocation must be 'none' "
1429eb633035STom Caputi "for unencrypted datasets"));
1430eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP,
1431eb633035STom Caputi errbuf);
1432eb633035STom Caputi goto error;
1433eb633035STom Caputi } else if (crypt != ZIO_CRYPT_OFF &&
1434eb633035STom Caputi strcmp(strval, "none") == 0) {
1435eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1436eb633035STom Caputi "keylocation must not be 'none' "
1437eb633035STom Caputi "for encrypted datasets"));
1438eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP,
1439eb633035STom Caputi errbuf);
1440eb633035STom Caputi goto error;
1441eb633035STom Caputi }
1442eb633035STom Caputi }
1443eb633035STom Caputi break;
1444eb633035STom Caputi
1445eb633035STom Caputi case ZFS_PROP_PBKDF2_ITERS:
1446eb633035STom Caputi if (intval < MIN_PBKDF2_ITERATIONS) {
1447eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1448eb633035STom Caputi "minimum pbkdf2 iterations is %u"),
1449eb633035STom Caputi MIN_PBKDF2_ITERATIONS);
1450eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1451eb633035STom Caputi goto error;
1452eb633035STom Caputi }
1453eb633035STom Caputi break;
1454eb633035STom Caputi
1455da6c28aaSamw case ZFS_PROP_UTF8ONLY:
1456da6c28aaSamw chosen_utf = (int)intval;
1457da6c28aaSamw break;
145888f61deeSIgor Kozhukhov
1459da6c28aaSamw case ZFS_PROP_NORMALIZE:
1460da6c28aaSamw chosen_normal = (int)intval;
1461f3861e1aSahl break;
146288f61deeSIgor Kozhukhov
146388f61deeSIgor Kozhukhov default:
146488f61deeSIgor Kozhukhov break;
1465e9dbad6fSeschrock }
1466fa9e4066Sahrens
1467e9dbad6fSeschrock /*
1468e9dbad6fSeschrock * For changes to existing volumes, we have some additional
1469e9dbad6fSeschrock * checks to enforce.
1470e9dbad6fSeschrock */
1471e9dbad6fSeschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1472e9dbad6fSeschrock uint64_t volsize = zfs_prop_get_int(zhp,
1473e9dbad6fSeschrock ZFS_PROP_VOLSIZE);
1474e9dbad6fSeschrock uint64_t blocksize = zfs_prop_get_int(zhp,
1475e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE);
1476e9dbad6fSeschrock char buf[64];
1477e9dbad6fSeschrock
1478e9dbad6fSeschrock switch (prop) {
1479e9dbad6fSeschrock case ZFS_PROP_RESERVATION:
1480e9dbad6fSeschrock if (intval > volsize) {
1481e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1482e9dbad6fSeschrock "'%s' is greater than current "
1483e9dbad6fSeschrock "volume size"), propname);
1484e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP,
1485e9dbad6fSeschrock errbuf);
1486e9dbad6fSeschrock goto error;
1487e9dbad6fSeschrock }
1488e9dbad6fSeschrock break;
1489e9dbad6fSeschrock
14901c10ae76SMike Gerdts case ZFS_PROP_REFRESERVATION:
14911c10ae76SMike Gerdts if (intval > volsize && intval != UINT64_MAX) {
14921c10ae76SMike Gerdts zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14931c10ae76SMike Gerdts "'%s' is greater than current "
14941c10ae76SMike Gerdts "volume size"), propname);
14951c10ae76SMike Gerdts (void) zfs_error(hdl, EZFS_BADPROP,
14961c10ae76SMike Gerdts errbuf);
14971c10ae76SMike Gerdts goto error;
14981c10ae76SMike Gerdts }
14991c10ae76SMike Gerdts break;
15001c10ae76SMike Gerdts
1501e9dbad6fSeschrock case ZFS_PROP_VOLSIZE:
1502e9dbad6fSeschrock if (intval % blocksize != 0) {
1503*6520eed5SToomas Soome zfs_nicebytes(blocksize, buf,
1504e9dbad6fSeschrock sizeof (buf));
1505e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1506e9dbad6fSeschrock "'%s' must be a multiple of "
1507e9dbad6fSeschrock "volume block size (%s)"),
1508e9dbad6fSeschrock propname, buf);
1509e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP,
1510e9dbad6fSeschrock errbuf);
1511e9dbad6fSeschrock goto error;
1512e9dbad6fSeschrock }
1513e9dbad6fSeschrock
1514e9dbad6fSeschrock if (intval == 0) {
1515e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1516e9dbad6fSeschrock "'%s' cannot be zero"),
1517e9dbad6fSeschrock propname);
1518e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP,
1519e9dbad6fSeschrock errbuf);
1520e9dbad6fSeschrock goto error;
1521e9dbad6fSeschrock }
1522f3861e1aSahl break;
152388f61deeSIgor Kozhukhov
152488f61deeSIgor Kozhukhov default:
152588f61deeSIgor Kozhukhov break;
1526fa9e4066Sahrens }
1527e9dbad6fSeschrock }
1528eb633035STom Caputi
1529eb633035STom Caputi /* check encryption properties */
1530eb633035STom Caputi if (zhp != NULL) {
1531eb633035STom Caputi int64_t crypt = zfs_prop_get_int(zhp,
1532eb633035STom Caputi ZFS_PROP_ENCRYPTION);
1533eb633035STom Caputi
1534eb633035STom Caputi switch (prop) {
1535eb633035STom Caputi case ZFS_PROP_COPIES:
1536eb633035STom Caputi if (crypt != ZIO_CRYPT_OFF && intval > 2) {
1537eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1538eb633035STom Caputi "encrypted datasets cannot have "
1539eb633035STom Caputi "3 copies"));
1540eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP,
1541eb633035STom Caputi errbuf);
1542eb633035STom Caputi goto error;
1543eb633035STom Caputi }
1544eb633035STom Caputi break;
1545eb633035STom Caputi default:
1546eb633035STom Caputi break;
1547eb633035STom Caputi }
1548eb633035STom Caputi }
1549e9dbad6fSeschrock }
1550fa9e4066Sahrens
1551da6c28aaSamw /*
1552da6c28aaSamw * If normalization was chosen, but no UTF8 choice was made,
1553da6c28aaSamw * enforce rejection of non-UTF8 names.
1554da6c28aaSamw *
1555da6c28aaSamw * If normalization was chosen, but rejecting non-UTF8 names
1556da6c28aaSamw * was explicitly not chosen, it is an error.
1557da6c28aaSamw */
1558de8267e0Stimh if (chosen_normal > 0 && chosen_utf < 0) {
1559da6c28aaSamw if (nvlist_add_uint64(ret,
1560da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1561da6c28aaSamw (void) no_memory(hdl);
1562da6c28aaSamw goto error;
1563da6c28aaSamw }
1564de8267e0Stimh } else if (chosen_normal > 0 && chosen_utf == 0) {
1565da6c28aaSamw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1566da6c28aaSamw "'%s' must be set 'on' if normalization chosen"),
1567da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1568da6c28aaSamw (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1569da6c28aaSamw goto error;
1570da6c28aaSamw }
157136db6475SEric Taylor return (ret);
157236db6475SEric Taylor
157336db6475SEric Taylor error:
157436db6475SEric Taylor nvlist_free(ret);
157536db6475SEric Taylor return (NULL);
157636db6475SEric Taylor }
157736db6475SEric Taylor
157836db6475SEric Taylor int
zfs_add_synthetic_resv(zfs_handle_t * zhp,nvlist_t * nvl)157936db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
158036db6475SEric Taylor {
158136db6475SEric Taylor uint64_t old_volsize;
158236db6475SEric Taylor uint64_t new_volsize;
158336db6475SEric Taylor uint64_t old_reservation;
158436db6475SEric Taylor uint64_t new_reservation;
158536db6475SEric Taylor zfs_prop_t resv_prop;
1586c61ea566SGeorge Wilson nvlist_t *props;
1587b73ccab0SMike Gerdts zpool_handle_t *zph = zpool_handle(zhp);
1588da6c28aaSamw
1589e9dbad6fSeschrock /*
1590e9dbad6fSeschrock * If this is an existing volume, and someone is setting the volsize,
1591e9dbad6fSeschrock * make sure that it matches the reservation, or add it if necessary.
1592e9dbad6fSeschrock */
159336db6475SEric Taylor old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
159436db6475SEric Taylor if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
159536db6475SEric Taylor return (-1);
159636db6475SEric Taylor old_reservation = zfs_prop_get_int(zhp, resv_prop);
1597c61ea566SGeorge Wilson
1598c61ea566SGeorge Wilson props = fnvlist_alloc();
1599c61ea566SGeorge Wilson fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1600c61ea566SGeorge Wilson zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1601c61ea566SGeorge Wilson
1602b73ccab0SMike Gerdts if ((zvol_volsize_to_reservation(zph, old_volsize, props) !=
1603c61ea566SGeorge Wilson old_reservation) || nvlist_exists(nvl,
1604c61ea566SGeorge Wilson zfs_prop_to_name(resv_prop))) {
1605c61ea566SGeorge Wilson fnvlist_free(props);
160636db6475SEric Taylor return (0);
1607fa9e4066Sahrens }
160836db6475SEric Taylor if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1609c61ea566SGeorge Wilson &new_volsize) != 0) {
1610c61ea566SGeorge Wilson fnvlist_free(props);
161136db6475SEric Taylor return (-1);
1612c61ea566SGeorge Wilson }
1613b73ccab0SMike Gerdts new_reservation = zvol_volsize_to_reservation(zph, new_volsize, props);
1614c61ea566SGeorge Wilson fnvlist_free(props);
1615c61ea566SGeorge Wilson
161636db6475SEric Taylor if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
161736db6475SEric Taylor new_reservation) != 0) {
161836db6475SEric Taylor (void) no_memory(zhp->zfs_hdl);
161936db6475SEric Taylor return (-1);
162036db6475SEric Taylor }
162136db6475SEric Taylor return (1);
1622fa9e4066Sahrens }
1623fa9e4066Sahrens
16241c10ae76SMike Gerdts /*
16251c10ae76SMike Gerdts * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
16261c10ae76SMike Gerdts * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
16271c10ae76SMike Gerdts * Return codes must match zfs_add_synthetic_resv().
16281c10ae76SMike Gerdts */
16291c10ae76SMike Gerdts static int
zfs_fix_auto_resv(zfs_handle_t * zhp,nvlist_t * nvl)16301c10ae76SMike Gerdts zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
16311c10ae76SMike Gerdts {
16321c10ae76SMike Gerdts uint64_t volsize;
16331c10ae76SMike Gerdts uint64_t resvsize;
16341c10ae76SMike Gerdts zfs_prop_t prop;
16351c10ae76SMike Gerdts nvlist_t *props;
16361c10ae76SMike Gerdts
16371c10ae76SMike Gerdts if (!ZFS_IS_VOLUME(zhp)) {
16381c10ae76SMike Gerdts return (0);
16391c10ae76SMike Gerdts }
16401c10ae76SMike Gerdts
16411c10ae76SMike Gerdts if (zfs_which_resv_prop(zhp, &prop) != 0) {
16421c10ae76SMike Gerdts return (-1);
16431c10ae76SMike Gerdts }
16441c10ae76SMike Gerdts
16451c10ae76SMike Gerdts if (prop != ZFS_PROP_REFRESERVATION) {
16461c10ae76SMike Gerdts return (0);
16471c10ae76SMike Gerdts }
16481c10ae76SMike Gerdts
16491c10ae76SMike Gerdts if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
16501c10ae76SMike Gerdts /* No value being set, so it can't be "auto" */
16511c10ae76SMike Gerdts return (0);
16521c10ae76SMike Gerdts }
16531c10ae76SMike Gerdts if (resvsize != UINT64_MAX) {
16541c10ae76SMike Gerdts /* Being set to a value other than "auto" */
16551c10ae76SMike Gerdts return (0);
16561c10ae76SMike Gerdts }
16571c10ae76SMike Gerdts
16581c10ae76SMike Gerdts props = fnvlist_alloc();
16591c10ae76SMike Gerdts
16601c10ae76SMike Gerdts fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
16611c10ae76SMike Gerdts zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
16621c10ae76SMike Gerdts
16631c10ae76SMike Gerdts if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
16641c10ae76SMike Gerdts &volsize) != 0) {
16651c10ae76SMike Gerdts volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
16661c10ae76SMike Gerdts }
16671c10ae76SMike Gerdts
1668b73ccab0SMike Gerdts resvsize = zvol_volsize_to_reservation(zpool_handle(zhp), volsize,
1669b73ccab0SMike Gerdts props);
16701c10ae76SMike Gerdts fnvlist_free(props);
16711c10ae76SMike Gerdts
16721c10ae76SMike Gerdts (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
16731c10ae76SMike Gerdts if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
16741c10ae76SMike Gerdts (void) no_memory(zhp->zfs_hdl);
16751c10ae76SMike Gerdts return (-1);
16761c10ae76SMike Gerdts }
16771c10ae76SMike Gerdts return (1);
16781c10ae76SMike Gerdts }
16791c10ae76SMike Gerdts
168092241e0bSTom Erickson void
zfs_setprop_error(libzfs_handle_t * hdl,zfs_prop_t prop,int err,char * errbuf)168192241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
168292241e0bSTom Erickson char *errbuf)
168392241e0bSTom Erickson {
168492241e0bSTom Erickson switch (err) {
168592241e0bSTom Erickson
168692241e0bSTom Erickson case ENOSPC:
168792241e0bSTom Erickson /*
168892241e0bSTom Erickson * For quotas and reservations, ENOSPC indicates
168992241e0bSTom Erickson * something different; setting a quota or reservation
169092241e0bSTom Erickson * doesn't use any disk space.
169192241e0bSTom Erickson */
169292241e0bSTom Erickson switch (prop) {
169392241e0bSTom Erickson case ZFS_PROP_QUOTA:
169492241e0bSTom Erickson case ZFS_PROP_REFQUOTA:
169592241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
169692241e0bSTom Erickson "size is less than current used or "
169792241e0bSTom Erickson "reserved space"));
169892241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
169992241e0bSTom Erickson break;
170092241e0bSTom Erickson
170192241e0bSTom Erickson case ZFS_PROP_RESERVATION:
170292241e0bSTom Erickson case ZFS_PROP_REFRESERVATION:
170392241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
170492241e0bSTom Erickson "size is greater than available space"));
170592241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
170692241e0bSTom Erickson break;
170792241e0bSTom Erickson
170892241e0bSTom Erickson default:
170992241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf);
171092241e0bSTom Erickson break;
171192241e0bSTom Erickson }
171292241e0bSTom Erickson break;
171392241e0bSTom Erickson
171492241e0bSTom Erickson case EBUSY:
171592241e0bSTom Erickson (void) zfs_standard_error(hdl, EBUSY, errbuf);
171692241e0bSTom Erickson break;
171792241e0bSTom Erickson
171892241e0bSTom Erickson case EROFS:
171992241e0bSTom Erickson (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
172092241e0bSTom Erickson break;
172192241e0bSTom Erickson
17226fdcb3d1SWill Andrews case E2BIG:
17236fdcb3d1SWill Andrews zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17246fdcb3d1SWill Andrews "property value too long"));
17256fdcb3d1SWill Andrews (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
17266fdcb3d1SWill Andrews break;
17276fdcb3d1SWill Andrews
172892241e0bSTom Erickson case ENOTSUP:
172992241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173092241e0bSTom Erickson "pool and or dataset must be upgraded to set this "
173192241e0bSTom Erickson "property or value"));
173292241e0bSTom Erickson (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
173392241e0bSTom Erickson break;
173492241e0bSTom Erickson
173592241e0bSTom Erickson case ERANGE:
1736b5152584SMatthew Ahrens if (prop == ZFS_PROP_COMPRESSION ||
1737b5152584SMatthew Ahrens prop == ZFS_PROP_RECORDSIZE) {
173892241e0bSTom Erickson (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173992241e0bSTom Erickson "property setting is not allowed on "
174092241e0bSTom Erickson "bootable datasets"));
174192241e0bSTom Erickson (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
174245818ee1SMatthew Ahrens } else if (prop == ZFS_PROP_CHECKSUM ||
174345818ee1SMatthew Ahrens prop == ZFS_PROP_DEDUP) {
174445818ee1SMatthew Ahrens (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174545818ee1SMatthew Ahrens "property setting is not allowed on "
174645818ee1SMatthew Ahrens "root pools"));
174745818ee1SMatthew Ahrens (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
174892241e0bSTom Erickson } else {
174992241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf);
175092241e0bSTom Erickson }
175192241e0bSTom Erickson break;
175292241e0bSTom Erickson
1753ab003da8SJim Dunham case EINVAL:
1754ab003da8SJim Dunham if (prop == ZPROP_INVAL) {
1755ab003da8SJim Dunham (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1756ab003da8SJim Dunham } else {
1757ab003da8SJim Dunham (void) zfs_standard_error(hdl, err, errbuf);
1758ab003da8SJim Dunham }
1759ab003da8SJim Dunham break;
1760ab003da8SJim Dunham
1761eb633035STom Caputi case EACCES:
1762eb633035STom Caputi if (prop == ZFS_PROP_KEYLOCATION) {
1763eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1764eb633035STom Caputi "keylocation may only be set on encryption roots"));
1765eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1766eb633035STom Caputi } else {
1767eb633035STom Caputi (void) zfs_standard_error(hdl, err, errbuf);
1768eb633035STom Caputi }
1769eb633035STom Caputi break;
1770eb633035STom Caputi
177192241e0bSTom Erickson case EOVERFLOW:
177292241e0bSTom Erickson /*
177392241e0bSTom Erickson * This platform can't address a volume this big.
177492241e0bSTom Erickson */
177592241e0bSTom Erickson #ifdef _ILP32
177692241e0bSTom Erickson if (prop == ZFS_PROP_VOLSIZE) {
177792241e0bSTom Erickson (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
177892241e0bSTom Erickson break;
177992241e0bSTom Erickson }
178092241e0bSTom Erickson #endif
178192241e0bSTom Erickson /* FALLTHROUGH */
178292241e0bSTom Erickson default:
178392241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf);
178492241e0bSTom Erickson }
178592241e0bSTom Erickson }
178692241e0bSTom Erickson
1787fa9e4066Sahrens /*
1788fa9e4066Sahrens * Given a property name and value, set the property for the given dataset.
1789fa9e4066Sahrens */
1790fa9e4066Sahrens int
zfs_prop_set(zfs_handle_t * zhp,const char * propname,const char * propval)1791e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1792fa9e4066Sahrens {
1793e9dbad6fSeschrock int ret = -1;
179499653d4eSeschrock char errbuf[1024];
179599653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl;
179630925561SChris Williamson nvlist_t *nvl = NULL;
179799653d4eSeschrock
179899653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf),
1799e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
180099653d4eSeschrock zhp->zfs_name);
180199653d4eSeschrock
1802e9dbad6fSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1803e9dbad6fSeschrock nvlist_add_string(nvl, propname, propval) != 0) {
1804e9dbad6fSeschrock (void) no_memory(hdl);
1805e9dbad6fSeschrock goto error;
1806fa9e4066Sahrens }
1807fa9e4066Sahrens
180830925561SChris Williamson ret = zfs_prop_set_list(zhp, nvl);
1809990b4856Slling
181030925561SChris Williamson error:
1811e9dbad6fSeschrock nvlist_free(nvl);
181230925561SChris Williamson return (ret);
181330925561SChris Williamson }
1814e9dbad6fSeschrock
1815e9dbad6fSeschrock
181636db6475SEric Taylor
181730925561SChris Williamson /*
181830925561SChris Williamson * Given an nvlist of property names and values, set the properties for the
181930925561SChris Williamson * given dataset.
182030925561SChris Williamson */
182130925561SChris Williamson int
zfs_prop_set_list(zfs_handle_t * zhp,nvlist_t * props)182230925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
182330925561SChris Williamson {
182430925561SChris Williamson zfs_cmd_t zc = { 0 };
182530925561SChris Williamson int ret = -1;
182630925561SChris Williamson prop_changelist_t **cls = NULL;
182730925561SChris Williamson int cl_idx;
182830925561SChris Williamson char errbuf[1024];
182930925561SChris Williamson libzfs_handle_t *hdl = zhp->zfs_hdl;
183030925561SChris Williamson nvlist_t *nvl;
183130925561SChris Williamson int nvl_len;
1832f83b46baSPaul Dagnelie int added_resv = 0;
1833fa9e4066Sahrens
183430925561SChris Williamson (void) snprintf(errbuf, sizeof (errbuf),
183530925561SChris Williamson dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
183630925561SChris Williamson zhp->zfs_name);
183730925561SChris Williamson
183830925561SChris Williamson if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1839e9316f76SJoe Stein zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1840eb633035STom Caputi B_FALSE, errbuf)) == NULL)
1841fa9e4066Sahrens goto error;
1842fa9e4066Sahrens
18430068372bSMark J Musante /*
184430925561SChris Williamson * We have to check for any extra properties which need to be added
184530925561SChris Williamson * before computing the length of the nvlist.
18460068372bSMark J Musante */
184730925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
184830925561SChris Williamson elem != NULL;
184930925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) {
185030925561SChris Williamson if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
185130925561SChris Williamson (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
185230925561SChris Williamson goto error;
185330925561SChris Williamson }
18544445fffbSMatthew Ahrens }
18551c10ae76SMike Gerdts
18561c10ae76SMike Gerdts if (added_resv != 1 &&
18571c10ae76SMike Gerdts (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
18581c10ae76SMike Gerdts goto error;
18591c10ae76SMike Gerdts }
18601c10ae76SMike Gerdts
186130925561SChris Williamson /*
186230925561SChris Williamson * Check how many properties we're setting and allocate an array to
186330925561SChris Williamson * store changelist pointers for postfix().
186430925561SChris Williamson */
186530925561SChris Williamson nvl_len = 0;
186630925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
186730925561SChris Williamson elem != NULL;
186830925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem))
186930925561SChris Williamson nvl_len++;
187030925561SChris Williamson if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
18710068372bSMark J Musante goto error;
1872fa9e4066Sahrens
187330925561SChris Williamson cl_idx = 0;
187430925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
187530925561SChris Williamson elem != NULL;
187630925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) {
187730925561SChris Williamson
187830925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
187930925561SChris Williamson
188030925561SChris Williamson assert(cl_idx < nvl_len);
188130925561SChris Williamson /*
188230925561SChris Williamson * We don't want to unmount & remount the dataset when changing
188330925561SChris Williamson * its canmount property to 'on' or 'noauto'. We only use
188430925561SChris Williamson * the changelist logic to unmount when setting canmount=off.
188530925561SChris Williamson */
1886c079fa4dSAndriy Gapon if (prop != ZFS_PROP_CANMOUNT ||
1887c079fa4dSAndriy Gapon (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1888c079fa4dSAndriy Gapon zfs_is_mounted(zhp, NULL))) {
188930925561SChris Williamson cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
189030925561SChris Williamson if (cls[cl_idx] == NULL)
189130925561SChris Williamson goto error;
189230925561SChris Williamson }
189330925561SChris Williamson
189430925561SChris Williamson if (prop == ZFS_PROP_MOUNTPOINT &&
189530925561SChris Williamson changelist_haszonedchild(cls[cl_idx])) {
189630925561SChris Williamson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
189730925561SChris Williamson "child dataset with inherited mountpoint is used "
189830925561SChris Williamson "in a non-global zone"));
189930925561SChris Williamson ret = zfs_error(hdl, EZFS_ZONED, errbuf);
190030925561SChris Williamson goto error;
190130925561SChris Williamson }
190230925561SChris Williamson
190330925561SChris Williamson if (cls[cl_idx] != NULL &&
190430925561SChris Williamson (ret = changelist_prefix(cls[cl_idx])) != 0)
190530925561SChris Williamson goto error;
190630925561SChris Williamson
190730925561SChris Williamson cl_idx++;
190830925561SChris Williamson }
190930925561SChris Williamson assert(cl_idx == nvl_len);
191030925561SChris Williamson
1911fa9e4066Sahrens /*
191230925561SChris Williamson * Execute the corresponding ioctl() to set this list of properties.
1913fa9e4066Sahrens */
1914fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1915fa9e4066Sahrens
191630925561SChris Williamson if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
191730925561SChris Williamson (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1918e9dbad6fSeschrock goto error;
1919e9dbad6fSeschrock
1920ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1921743a77edSAlan Wright
1922fa9e4066Sahrens if (ret != 0) {
1923f62db44dSAndrew Stormont if (zc.zc_nvlist_dst_filled == B_FALSE) {
1924f62db44dSAndrew Stormont (void) zfs_standard_error(hdl, errno, errbuf);
1925f62db44dSAndrew Stormont goto error;
1926f62db44dSAndrew Stormont }
1927f62db44dSAndrew Stormont
192830925561SChris Williamson /* Get the list of unset properties back and report them. */
192930925561SChris Williamson nvlist_t *errorprops = NULL;
193030925561SChris Williamson if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
193130925561SChris Williamson goto error;
1932f62db44dSAndrew Stormont for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
193330925561SChris Williamson elem != NULL;
1934f62db44dSAndrew Stormont elem = nvlist_next_nvpair(errorprops, elem)) {
193530925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
193630925561SChris Williamson zfs_setprop_error(hdl, prop, errno, errbuf);
193730925561SChris Williamson }
193830925561SChris Williamson nvlist_free(errorprops);
193930925561SChris Williamson
194036db6475SEric Taylor if (added_resv && errno == ENOSPC) {
194136db6475SEric Taylor /* clean up the volsize property we tried to set */
194236db6475SEric Taylor uint64_t old_volsize = zfs_prop_get_int(zhp,
194336db6475SEric Taylor ZFS_PROP_VOLSIZE);
194436db6475SEric Taylor nvlist_free(nvl);
194530925561SChris Williamson nvl = NULL;
194636db6475SEric Taylor zcmd_free_nvlists(&zc);
194730925561SChris Williamson
194836db6475SEric Taylor if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
194936db6475SEric Taylor goto error;
195036db6475SEric Taylor if (nvlist_add_uint64(nvl,
195136db6475SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLSIZE),
195236db6475SEric Taylor old_volsize) != 0)
195336db6475SEric Taylor goto error;
195436db6475SEric Taylor if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
195536db6475SEric Taylor goto error;
195636db6475SEric Taylor (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
195736db6475SEric Taylor }
1958fa9e4066Sahrens } else {
195930925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
196030925561SChris Williamson if (cls[cl_idx] != NULL) {
196130925561SChris Williamson int clp_err = changelist_postfix(cls[cl_idx]);
196230925561SChris Williamson if (clp_err != 0)
196330925561SChris Williamson ret = clp_err;
196430925561SChris Williamson }
196530925561SChris Williamson }
1966a227b7f4Shs
1967fa9e4066Sahrens /*
1968fa9e4066Sahrens * Refresh the statistics so the new property value
1969fa9e4066Sahrens * is reflected.
1970fa9e4066Sahrens */
1971a227b7f4Shs if (ret == 0)
1972e9dbad6fSeschrock (void) get_stats(zhp);
1973fa9e4066Sahrens }
1974fa9e4066Sahrens
1975fa9e4066Sahrens error:
1976e9dbad6fSeschrock nvlist_free(nvl);
1977e9dbad6fSeschrock zcmd_free_nvlists(&zc);
197830925561SChris Williamson if (cls != NULL) {
197930925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
198030925561SChris Williamson if (cls[cl_idx] != NULL)
198130925561SChris Williamson changelist_free(cls[cl_idx]);
198230925561SChris Williamson }
198330925561SChris Williamson free(cls);
198430925561SChris Williamson }
1985fa9e4066Sahrens return (ret);
1986fa9e4066Sahrens }
1987fa9e4066Sahrens
1988fa9e4066Sahrens /*
198992241e0bSTom Erickson * Given a property, inherit the value from the parent dataset, or if received
199092241e0bSTom Erickson * is TRUE, revert to the received value, if any.
1991fa9e4066Sahrens */
1992fa9e4066Sahrens int
zfs_prop_inherit(zfs_handle_t * zhp,const char * propname,boolean_t received)199392241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1994fa9e4066Sahrens {
1995fa9e4066Sahrens zfs_cmd_t zc = { 0 };
1996fa9e4066Sahrens int ret;
1997fa9e4066Sahrens prop_changelist_t *cl;
199899653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl;
199999653d4eSeschrock char errbuf[1024];
2000e9dbad6fSeschrock zfs_prop_t prop;
200199653d4eSeschrock
200299653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
200399653d4eSeschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
2004fa9e4066Sahrens
200592241e0bSTom Erickson zc.zc_cookie = received;
2006990b4856Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
2007e9dbad6fSeschrock /*
2008e9dbad6fSeschrock * For user properties, the amount of work we have to do is very
2009e9dbad6fSeschrock * small, so just do it here.
2010e9dbad6fSeschrock */
2011e9dbad6fSeschrock if (!zfs_prop_user(propname)) {
2012e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2013e9dbad6fSeschrock "invalid property"));
2014e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2015e9dbad6fSeschrock }
2016e9dbad6fSeschrock
2017e9dbad6fSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2018e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2019e9dbad6fSeschrock
2020e45ce728Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
2021e9dbad6fSeschrock return (zfs_standard_error(hdl, errno, errbuf));
2022e9dbad6fSeschrock
2023e9dbad6fSeschrock return (0);
2024e9dbad6fSeschrock }
2025e9dbad6fSeschrock
2026fa9e4066Sahrens /*
2027fa9e4066Sahrens * Verify that this property is inheritable.
2028fa9e4066Sahrens */
202999653d4eSeschrock if (zfs_prop_readonly(prop))
203099653d4eSeschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
2031fa9e4066Sahrens
203292241e0bSTom Erickson if (!zfs_prop_inheritable(prop) && !received)
203399653d4eSeschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
2034fa9e4066Sahrens
2035fa9e4066Sahrens /*
2036fa9e4066Sahrens * Check to see if the value applies to this type
2037fa9e4066Sahrens */
20384c2bdae2STim Chase if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
203999653d4eSeschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
2040fa9e4066Sahrens
2041bf7c2d40Srm /*
204236db6475SEric Taylor * Normalize the name, to get rid of shorthand abbreviations.
2043bf7c2d40Srm */
2044bf7c2d40Srm propname = zfs_prop_to_name(prop);
2045fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2046e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2047fa9e4066Sahrens
2048fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
2049fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
205099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
205199653d4eSeschrock "dataset is used in a non-global zone"));
205299653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf));
2053fa9e4066Sahrens }
2054fa9e4066Sahrens
2055fa9e4066Sahrens /*
2056fa9e4066Sahrens * Determine datasets which will be affected by this change, if any.
2057fa9e4066Sahrens */
20580069fd67STim Haley if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
2059fa9e4066Sahrens return (-1);
2060fa9e4066Sahrens
2061fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
206299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
206399653d4eSeschrock "child dataset with inherited mountpoint is used "
206499653d4eSeschrock "in a non-global zone"));
206599653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2066fa9e4066Sahrens goto error;
2067fa9e4066Sahrens }
2068fa9e4066Sahrens
2069fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0)
2070fa9e4066Sahrens goto error;
2071fa9e4066Sahrens
2072e45ce728Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
207399653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf));
2074fa9e4066Sahrens } else {
2075fa9e4066Sahrens
2076efc555ebSnd if ((ret = changelist_postfix(cl)) != 0)
2077fa9e4066Sahrens goto error;
2078fa9e4066Sahrens
2079fa9e4066Sahrens /*
2080fa9e4066Sahrens * Refresh the statistics so the new property is reflected.
2081fa9e4066Sahrens */
2082fa9e4066Sahrens (void) get_stats(zhp);
2083fa9e4066Sahrens }
2084fa9e4066Sahrens
2085fa9e4066Sahrens error:
2086fa9e4066Sahrens changelist_free(cl);
2087fa9e4066Sahrens return (ret);
2088fa9e4066Sahrens }
2089fa9e4066Sahrens
20907f7322feSeschrock /*
20917f7322feSeschrock * True DSL properties are stored in an nvlist. The following two functions
20927f7322feSeschrock * extract them appropriately.
20937f7322feSeschrock */
20947f7322feSeschrock static uint64_t
getprop_uint64(zfs_handle_t * zhp,zfs_prop_t prop,char ** source)20957f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
20967f7322feSeschrock {
20977f7322feSeschrock nvlist_t *nv;
20987f7322feSeschrock uint64_t value;
20997f7322feSeschrock
2100a2eea2e1Sahrens *source = NULL;
21017f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props,
21027f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) {
2103990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2104990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
21057f7322feSeschrock } else {
21062e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table ||
21072e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE);
21087f7322feSeschrock value = zfs_prop_default_numeric(prop);
21097f7322feSeschrock *source = "";
21107f7322feSeschrock }
21117f7322feSeschrock
21127f7322feSeschrock return (value);
21137f7322feSeschrock }
21147f7322feSeschrock
21159c3fd121SMatthew Ahrens static const char *
getprop_string(zfs_handle_t * zhp,zfs_prop_t prop,char ** source)21167f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
21177f7322feSeschrock {
21187f7322feSeschrock nvlist_t *nv;
21199c3fd121SMatthew Ahrens const char *value;
21207f7322feSeschrock
2121a2eea2e1Sahrens *source = NULL;
21227f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props,
21237f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) {
21249c3fd121SMatthew Ahrens value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2125990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
21267f7322feSeschrock } else {
21272e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table ||
21282e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE);
21299c3fd121SMatthew Ahrens value = zfs_prop_default_string(prop);
21307f7322feSeschrock *source = "";
21317f7322feSeschrock }
21327f7322feSeschrock
21337f7322feSeschrock return (value);
21347f7322feSeschrock }
21357f7322feSeschrock
213692241e0bSTom Erickson static boolean_t
zfs_is_recvd_props_mode(zfs_handle_t * zhp)213792241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp)
213892241e0bSTom Erickson {
213992241e0bSTom Erickson return (zhp->zfs_props == zhp->zfs_recvd_props);
214092241e0bSTom Erickson }
214192241e0bSTom Erickson
214292241e0bSTom Erickson static void
zfs_set_recvd_props_mode(zfs_handle_t * zhp,uint64_t * cookie)214392241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
214492241e0bSTom Erickson {
214592241e0bSTom Erickson *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
214692241e0bSTom Erickson zhp->zfs_props = zhp->zfs_recvd_props;
214792241e0bSTom Erickson }
214892241e0bSTom Erickson
214992241e0bSTom Erickson static void
zfs_unset_recvd_props_mode(zfs_handle_t * zhp,uint64_t * cookie)215092241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
215192241e0bSTom Erickson {
215292241e0bSTom Erickson zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
215392241e0bSTom Erickson *cookie = 0;
215492241e0bSTom Erickson }
215592241e0bSTom Erickson
2156fa9e4066Sahrens /*
2157fa9e4066Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and
2158fa9e4066Sahrens * zfs_prop_get_int() are built using this interface.
2159fa9e4066Sahrens *
2160fa9e4066Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan
2161fa9e4066Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options.
2162fa9e4066Sahrens * If they differ from the on-disk values, report the current values and mark
2163fa9e4066Sahrens * the source "temporary".
2164fa9e4066Sahrens */
216599653d4eSeschrock static int
get_numeric_property(zfs_handle_t * zhp,zfs_prop_t prop,zprop_source_t * src,char ** source,uint64_t * val)2166990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
216799653d4eSeschrock char **source, uint64_t *val)
2168fa9e4066Sahrens {
2169bd00f61bSrm zfs_cmd_t zc = { 0 };
217096510749Stimh nvlist_t *zplprops = NULL;
2171fa9e4066Sahrens struct mnttab mnt;
21723ccfa83cSahrens char *mntopt_on = NULL;
21733ccfa83cSahrens char *mntopt_off = NULL;
217492241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp);
2175fa9e4066Sahrens
2176fa9e4066Sahrens *source = NULL;
2177fa9e4066Sahrens
21784c2bdae2STim Chase /*
21794c2bdae2STim Chase * If the property is being fetched for a snapshot, check whether
21804c2bdae2STim Chase * the property is valid for the snapshot's head dataset type.
21814c2bdae2STim Chase */
21824c2bdae2STim Chase if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT &&
21834c2bdae2STim Chase !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) {
21844c2bdae2STim Chase *val = zfs_prop_default_numeric(prop);
21854c2bdae2STim Chase return (-1);
21864c2bdae2STim Chase }
21874c2bdae2STim Chase
21883ccfa83cSahrens switch (prop) {
21893ccfa83cSahrens case ZFS_PROP_ATIME:
21903ccfa83cSahrens mntopt_on = MNTOPT_ATIME;
21913ccfa83cSahrens mntopt_off = MNTOPT_NOATIME;
21923ccfa83cSahrens break;
21933ccfa83cSahrens
21943ccfa83cSahrens case ZFS_PROP_DEVICES:
21953ccfa83cSahrens mntopt_on = MNTOPT_DEVICES;
21963ccfa83cSahrens mntopt_off = MNTOPT_NODEVICES;
21973ccfa83cSahrens break;
21983ccfa83cSahrens
21993ccfa83cSahrens case ZFS_PROP_EXEC:
22003ccfa83cSahrens mntopt_on = MNTOPT_EXEC;
22013ccfa83cSahrens mntopt_off = MNTOPT_NOEXEC;
22023ccfa83cSahrens break;
22033ccfa83cSahrens
22043ccfa83cSahrens case ZFS_PROP_READONLY:
22053ccfa83cSahrens mntopt_on = MNTOPT_RO;
22063ccfa83cSahrens mntopt_off = MNTOPT_RW;
22073ccfa83cSahrens break;
22083ccfa83cSahrens
22093ccfa83cSahrens case ZFS_PROP_SETUID:
22103ccfa83cSahrens mntopt_on = MNTOPT_SETUID;
22113ccfa83cSahrens mntopt_off = MNTOPT_NOSETUID;
22123ccfa83cSahrens break;
22133ccfa83cSahrens
22143ccfa83cSahrens case ZFS_PROP_XATTR:
22153ccfa83cSahrens mntopt_on = MNTOPT_XATTR;
22163ccfa83cSahrens mntopt_off = MNTOPT_NOXATTR;
22173ccfa83cSahrens break;
2218da6c28aaSamw
2219da6c28aaSamw case ZFS_PROP_NBMAND:
2220da6c28aaSamw mntopt_on = MNTOPT_NBMAND;
2221da6c28aaSamw mntopt_off = MNTOPT_NONBMAND;
2222da6c28aaSamw break;
222388f61deeSIgor Kozhukhov
222488f61deeSIgor Kozhukhov default:
222588f61deeSIgor Kozhukhov break;
22263ccfa83cSahrens }
22273ccfa83cSahrens
22283bb79becSeschrock /*
22293bb79becSeschrock * Because looking up the mount options is potentially expensive
22303bb79becSeschrock * (iterating over all of /etc/mnttab), we defer its calculation until
22313bb79becSeschrock * we're looking up a property which requires its presence.
22323bb79becSeschrock */
22333bb79becSeschrock if (!zhp->zfs_mntcheck &&
22343ccfa83cSahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2235ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl;
2236ebedde84SEric Taylor struct mnttab entry;
22373bb79becSeschrock
2238ebedde84SEric Taylor if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2239ebedde84SEric Taylor zhp->zfs_mntopts = zfs_strdup(hdl,
22403ccfa83cSahrens entry.mnt_mntopts);
22413ccfa83cSahrens if (zhp->zfs_mntopts == NULL)
22423ccfa83cSahrens return (-1);
22433ccfa83cSahrens }
22443bb79becSeschrock
22453bb79becSeschrock zhp->zfs_mntcheck = B_TRUE;
22463bb79becSeschrock }
22473bb79becSeschrock
2248fa9e4066Sahrens if (zhp->zfs_mntopts == NULL)
2249fa9e4066Sahrens mnt.mnt_mntopts = "";
2250fa9e4066Sahrens else
2251fa9e4066Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts;
2252fa9e4066Sahrens
2253fa9e4066Sahrens switch (prop) {
2254fa9e4066Sahrens case ZFS_PROP_ATIME:
2255fa9e4066Sahrens case ZFS_PROP_DEVICES:
2256fa9e4066Sahrens case ZFS_PROP_EXEC:
22573ccfa83cSahrens case ZFS_PROP_READONLY:
22583ccfa83cSahrens case ZFS_PROP_SETUID:
22593ccfa83cSahrens case ZFS_PROP_XATTR:
2260da6c28aaSamw case ZFS_PROP_NBMAND:
226199653d4eSeschrock *val = getprop_uint64(zhp, prop, source);
2262fa9e4066Sahrens
226392241e0bSTom Erickson if (received)
226492241e0bSTom Erickson break;
226592241e0bSTom Erickson
22663ccfa83cSahrens if (hasmntopt(&mnt, mntopt_on) && !*val) {
226799653d4eSeschrock *val = B_TRUE;
2268fa9e4066Sahrens if (src)
2269990b4856Slling *src = ZPROP_SRC_TEMPORARY;
22703ccfa83cSahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) {
227199653d4eSeschrock *val = B_FALSE;
2272fa9e4066Sahrens if (src)
2273990b4856Slling *src = ZPROP_SRC_TEMPORARY;
2274fa9e4066Sahrens }
227599653d4eSeschrock break;
2276fa9e4066Sahrens
22773ccfa83cSahrens case ZFS_PROP_CANMOUNT:
2278d41c4376SMark J Musante case ZFS_PROP_VOLSIZE:
2279fa9e4066Sahrens case ZFS_PROP_QUOTA:
2280a9799022Sck case ZFS_PROP_REFQUOTA:
2281fa9e4066Sahrens case ZFS_PROP_RESERVATION:
2282a9799022Sck case ZFS_PROP_REFRESERVATION:
2283a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT:
2284a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT:
2285a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT:
2286a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT:
2287a2eea2e1Sahrens *val = getprop_uint64(zhp, prop, source);
228892241e0bSTom Erickson
228992241e0bSTom Erickson if (*source == NULL) {
229092241e0bSTom Erickson /* not default, must be local */
2291fa9e4066Sahrens *source = zhp->zfs_name;
229292241e0bSTom Erickson }
229399653d4eSeschrock break;
2294fa9e4066Sahrens
2295fa9e4066Sahrens case ZFS_PROP_MOUNTED:
229699653d4eSeschrock *val = (zhp->zfs_mntopts != NULL);
229799653d4eSeschrock break;
2298fa9e4066Sahrens
229939c23413Seschrock case ZFS_PROP_NUMCLONES:
230039c23413Seschrock *val = zhp->zfs_dmustats.dds_num_clones;
230139c23413Seschrock break;
230239c23413Seschrock
2303bd00f61bSrm case ZFS_PROP_VERSION:
2304de8267e0Stimh case ZFS_PROP_NORMALIZE:
2305de8267e0Stimh case ZFS_PROP_UTF8ONLY:
2306de8267e0Stimh case ZFS_PROP_CASE:
23074c2bdae2STim Chase if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
23083d7934e1Srm return (-1);
2309bd00f61bSrm (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2310de8267e0Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2311de8267e0Stimh zcmd_free_nvlists(&zc);
2312f4b94bdeSMatthew Ahrens return (-1);
2313bd00f61bSrm }
2314de8267e0Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2315de8267e0Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2316de8267e0Stimh val) != 0) {
2317de8267e0Stimh zcmd_free_nvlists(&zc);
2318f4b94bdeSMatthew Ahrens return (-1);
2319de8267e0Stimh }
2320aab83bb8SJosef 'Jeff' Sipek nvlist_free(zplprops);
2321de8267e0Stimh zcmd_free_nvlists(&zc);
2322bd00f61bSrm break;
2323bd00f61bSrm
2324ca48f36fSKeith M Wesolowski case ZFS_PROP_INCONSISTENT:
2325ca48f36fSKeith M Wesolowski *val = zhp->zfs_dmustats.dds_inconsistent;
2326ca48f36fSKeith M Wesolowski break;
2327ca48f36fSKeith M Wesolowski
2328fa9e4066Sahrens default:
2329e7437265Sahrens switch (zfs_prop_get_type(prop)) {
233091ebeef5Sahrens case PROP_TYPE_NUMBER:
233191ebeef5Sahrens case PROP_TYPE_INDEX:
2332e7437265Sahrens *val = getprop_uint64(zhp, prop, source);
233374e7dc98SMatthew Ahrens /*
233414843421SMatthew Ahrens * If we tried to use a default value for a
233574e7dc98SMatthew Ahrens * readonly property, it means that it was not
23364d86c0eaSMatthew Ahrens * present. Note this only applies to "truly"
23374d86c0eaSMatthew Ahrens * readonly properties, not set-once properties
23384d86c0eaSMatthew Ahrens * like volblocksize.
233974e7dc98SMatthew Ahrens */
234074e7dc98SMatthew Ahrens if (zfs_prop_readonly(prop) &&
23414d86c0eaSMatthew Ahrens !zfs_prop_setonce(prop) &&
234231f572c2STom Erickson *source != NULL && (*source)[0] == '\0') {
234331f572c2STom Erickson *source = NULL;
2344ad2760acSMatthew Ahrens return (-1);
234574e7dc98SMatthew Ahrens }
2346e7437265Sahrens break;
2347e7437265Sahrens
234891ebeef5Sahrens case PROP_TYPE_STRING:
2349e7437265Sahrens default:
2350e7437265Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2351e7437265Sahrens "cannot get non-numeric property"));
2352e7437265Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2353e7437265Sahrens dgettext(TEXT_DOMAIN, "internal error")));
2354e7437265Sahrens }
2355fa9e4066Sahrens }
2356fa9e4066Sahrens
2357fa9e4066Sahrens return (0);
2358fa9e4066Sahrens }
2359fa9e4066Sahrens
2360fa9e4066Sahrens /*
2361fa9e4066Sahrens * Calculate the source type, given the raw source string.
2362fa9e4066Sahrens */
2363fa9e4066Sahrens static void
get_source(zfs_handle_t * zhp,zprop_source_t * srctype,char * source,char * statbuf,size_t statlen)2364990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2365fa9e4066Sahrens char *statbuf, size_t statlen)
2366fa9e4066Sahrens {
2367990b4856Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2368fa9e4066Sahrens return;
2369fa9e4066Sahrens
2370fa9e4066Sahrens if (source == NULL) {
2371990b4856Slling *srctype = ZPROP_SRC_NONE;
2372fa9e4066Sahrens } else if (source[0] == '\0') {
2373990b4856Slling *srctype = ZPROP_SRC_DEFAULT;
237492241e0bSTom Erickson } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
237592241e0bSTom Erickson *srctype = ZPROP_SRC_RECEIVED;
2376fa9e4066Sahrens } else {
2377fa9e4066Sahrens if (strcmp(source, zhp->zfs_name) == 0) {
2378990b4856Slling *srctype = ZPROP_SRC_LOCAL;
2379fa9e4066Sahrens } else {
2380fa9e4066Sahrens (void) strlcpy(statbuf, source, statlen);
2381990b4856Slling *srctype = ZPROP_SRC_INHERITED;
2382fa9e4066Sahrens }
2383fa9e4066Sahrens }
2384fa9e4066Sahrens
2385fa9e4066Sahrens }
2386fa9e4066Sahrens
238792241e0bSTom Erickson int
zfs_prop_get_recvd(zfs_handle_t * zhp,const char * propname,char * propbuf,size_t proplen,boolean_t literal)238892241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
238992241e0bSTom Erickson size_t proplen, boolean_t literal)
239092241e0bSTom Erickson {
239192241e0bSTom Erickson zfs_prop_t prop;
239292241e0bSTom Erickson int err = 0;
239392241e0bSTom Erickson
239492241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL)
239592241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0)
239692241e0bSTom Erickson return (-1);
239792241e0bSTom Erickson
239892241e0bSTom Erickson prop = zfs_name_to_prop(propname);
239992241e0bSTom Erickson
240092241e0bSTom Erickson if (prop != ZPROP_INVAL) {
240192241e0bSTom Erickson uint64_t cookie;
240292241e0bSTom Erickson if (!nvlist_exists(zhp->zfs_recvd_props, propname))
240392241e0bSTom Erickson return (-1);
240492241e0bSTom Erickson zfs_set_recvd_props_mode(zhp, &cookie);
240592241e0bSTom Erickson err = zfs_prop_get(zhp, prop, propbuf, proplen,
240692241e0bSTom Erickson NULL, NULL, 0, literal);
240792241e0bSTom Erickson zfs_unset_recvd_props_mode(zhp, &cookie);
240892241e0bSTom Erickson } else {
240992241e0bSTom Erickson nvlist_t *propval;
241092241e0bSTom Erickson char *recvdval;
241192241e0bSTom Erickson if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
241292241e0bSTom Erickson propname, &propval) != 0)
241392241e0bSTom Erickson return (-1);
241492241e0bSTom Erickson verify(nvlist_lookup_string(propval, ZPROP_VALUE,
241592241e0bSTom Erickson &recvdval) == 0);
241692241e0bSTom Erickson (void) strlcpy(propbuf, recvdval, proplen);
241792241e0bSTom Erickson }
241892241e0bSTom Erickson
241992241e0bSTom Erickson return (err == 0 ? 0 : -1);
242092241e0bSTom Erickson }
242192241e0bSTom Erickson
242219b94df9SMatthew Ahrens static int
get_clones_string(zfs_handle_t * zhp,char * propbuf,size_t proplen)242319b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
242419b94df9SMatthew Ahrens {
242519b94df9SMatthew Ahrens nvlist_t *value;
242619b94df9SMatthew Ahrens nvpair_t *pair;
242719b94df9SMatthew Ahrens
242819b94df9SMatthew Ahrens value = zfs_get_clones_nvl(zhp);
242999ea293eSMatt Fiddaman if (value == NULL || nvlist_empty(value))
243019b94df9SMatthew Ahrens return (-1);
243119b94df9SMatthew Ahrens
243219b94df9SMatthew Ahrens propbuf[0] = '\0';
243319b94df9SMatthew Ahrens for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
243419b94df9SMatthew Ahrens pair = nvlist_next_nvpair(value, pair)) {
243519b94df9SMatthew Ahrens if (propbuf[0] != '\0')
243619b94df9SMatthew Ahrens (void) strlcat(propbuf, ",", proplen);
243719b94df9SMatthew Ahrens (void) strlcat(propbuf, nvpair_name(pair), proplen);
243819b94df9SMatthew Ahrens }
243919b94df9SMatthew Ahrens
244019b94df9SMatthew Ahrens return (0);
244119b94df9SMatthew Ahrens }
244219b94df9SMatthew Ahrens
244319b94df9SMatthew Ahrens struct get_clones_arg {
244419b94df9SMatthew Ahrens uint64_t numclones;
244519b94df9SMatthew Ahrens nvlist_t *value;
244619b94df9SMatthew Ahrens const char *origin;
24479adfa60dSMatthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN];
244819b94df9SMatthew Ahrens };
244919b94df9SMatthew Ahrens
245019b94df9SMatthew Ahrens int
get_clones_cb(zfs_handle_t * zhp,void * arg)245119b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg)
245219b94df9SMatthew Ahrens {
245319b94df9SMatthew Ahrens struct get_clones_arg *gca = arg;
245419b94df9SMatthew Ahrens
245519b94df9SMatthew Ahrens if (gca->numclones == 0) {
245619b94df9SMatthew Ahrens zfs_close(zhp);
245719b94df9SMatthew Ahrens return (0);
245819b94df9SMatthew Ahrens }
245919b94df9SMatthew Ahrens
246019b94df9SMatthew Ahrens if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
246119b94df9SMatthew Ahrens NULL, NULL, 0, B_TRUE) != 0)
246219b94df9SMatthew Ahrens goto out;
246319b94df9SMatthew Ahrens if (strcmp(gca->buf, gca->origin) == 0) {
24643b2aab18SMatthew Ahrens fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
246519b94df9SMatthew Ahrens gca->numclones--;
246619b94df9SMatthew Ahrens }
246719b94df9SMatthew Ahrens
246819b94df9SMatthew Ahrens out:
246919b94df9SMatthew Ahrens (void) zfs_iter_children(zhp, get_clones_cb, gca);
247019b94df9SMatthew Ahrens zfs_close(zhp);
247119b94df9SMatthew Ahrens return (0);
247219b94df9SMatthew Ahrens }
247319b94df9SMatthew Ahrens
247419b94df9SMatthew Ahrens nvlist_t *
zfs_get_clones_nvl(zfs_handle_t * zhp)247519b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp)
247619b94df9SMatthew Ahrens {
247719b94df9SMatthew Ahrens nvlist_t *nv, *value;
247819b94df9SMatthew Ahrens
247919b94df9SMatthew Ahrens if (nvlist_lookup_nvlist(zhp->zfs_props,
248019b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
248119b94df9SMatthew Ahrens struct get_clones_arg gca;
248219b94df9SMatthew Ahrens
248319b94df9SMatthew Ahrens /*
248419b94df9SMatthew Ahrens * if this is a snapshot, then the kernel wasn't able
248519b94df9SMatthew Ahrens * to get the clones. Do it by slowly iterating.
248619b94df9SMatthew Ahrens */
248719b94df9SMatthew Ahrens if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
248819b94df9SMatthew Ahrens return (NULL);
248919b94df9SMatthew Ahrens if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
249019b94df9SMatthew Ahrens return (NULL);
249119b94df9SMatthew Ahrens if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
249219b94df9SMatthew Ahrens nvlist_free(nv);
249319b94df9SMatthew Ahrens return (NULL);
249419b94df9SMatthew Ahrens }
249519b94df9SMatthew Ahrens
249619b94df9SMatthew Ahrens gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
249719b94df9SMatthew Ahrens gca.value = value;
249819b94df9SMatthew Ahrens gca.origin = zhp->zfs_name;
249919b94df9SMatthew Ahrens
250019b94df9SMatthew Ahrens if (gca.numclones != 0) {
250119b94df9SMatthew Ahrens zfs_handle_t *root;
25029adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
250319b94df9SMatthew Ahrens char *cp = pool;
250419b94df9SMatthew Ahrens
250519b94df9SMatthew Ahrens /* get the pool name */
250619b94df9SMatthew Ahrens (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
250719b94df9SMatthew Ahrens (void) strsep(&cp, "/@");
250819b94df9SMatthew Ahrens root = zfs_open(zhp->zfs_hdl, pool,
250919b94df9SMatthew Ahrens ZFS_TYPE_FILESYSTEM);
251019b94df9SMatthew Ahrens
251119b94df9SMatthew Ahrens (void) get_clones_cb(root, &gca);
251219b94df9SMatthew Ahrens }
251319b94df9SMatthew Ahrens
251419b94df9SMatthew Ahrens if (gca.numclones != 0 ||
251519b94df9SMatthew Ahrens nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
251619b94df9SMatthew Ahrens nvlist_add_nvlist(zhp->zfs_props,
251719b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
251819b94df9SMatthew Ahrens nvlist_free(nv);
251919b94df9SMatthew Ahrens nvlist_free(value);
252019b94df9SMatthew Ahrens return (NULL);
252119b94df9SMatthew Ahrens }
252219b94df9SMatthew Ahrens nvlist_free(nv);
252319b94df9SMatthew Ahrens nvlist_free(value);
252419b94df9SMatthew Ahrens verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
252519b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
252619b94df9SMatthew Ahrens }
252719b94df9SMatthew Ahrens
252819b94df9SMatthew Ahrens verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
252919b94df9SMatthew Ahrens
253019b94df9SMatthew Ahrens return (value);
253119b94df9SMatthew Ahrens }
253219b94df9SMatthew Ahrens
2533dfc11533SChris Williamson /*
2534dfc11533SChris Williamson * Accepts a property and value and checks that the value
2535dfc11533SChris Williamson * matches the one found by the channel program. If they are
2536dfc11533SChris Williamson * not equal, print both of them.
2537dfc11533SChris Williamson */
2538dfc11533SChris Williamson void
zcp_check(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t intval,const char * strval)2539dfc11533SChris Williamson zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2540dfc11533SChris Williamson const char *strval)
2541dfc11533SChris Williamson {
2542dfc11533SChris Williamson if (!zhp->zfs_hdl->libzfs_prop_debug)
2543dfc11533SChris Williamson return;
2544dfc11533SChris Williamson int error;
2545dfc11533SChris Williamson char *poolname = zhp->zpool_hdl->zpool_name;
2546dfc11533SChris Williamson const char *program =
2547dfc11533SChris Williamson "args = ...\n"
2548dfc11533SChris Williamson "ds = args['dataset']\n"
2549dfc11533SChris Williamson "prop = args['property']\n"
2550dfc11533SChris Williamson "value, setpoint = zfs.get_prop(ds, prop)\n"
2551dfc11533SChris Williamson "return {value=value, setpoint=setpoint}\n";
2552dfc11533SChris Williamson nvlist_t *outnvl;
2553dfc11533SChris Williamson nvlist_t *retnvl;
2554dfc11533SChris Williamson nvlist_t *argnvl = fnvlist_alloc();
2555dfc11533SChris Williamson
2556dfc11533SChris Williamson fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2557dfc11533SChris Williamson fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2558dfc11533SChris Williamson
2559a3b28680SSerapheim Dimitropoulos error = lzc_channel_program_nosync(poolname, program,
2560dfc11533SChris Williamson 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2561dfc11533SChris Williamson
2562dfc11533SChris Williamson if (error == 0) {
2563dfc11533SChris Williamson retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2564dfc11533SChris Williamson if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2565dfc11533SChris Williamson int64_t ans;
2566dfc11533SChris Williamson error = nvlist_lookup_int64(retnvl, "value", &ans);
2567dfc11533SChris Williamson if (error != 0) {
2568dfc11533SChris Williamson (void) fprintf(stderr, "zcp check error: %u\n",
2569dfc11533SChris Williamson error);
2570dfc11533SChris Williamson return;
2571dfc11533SChris Williamson }
2572dfc11533SChris Williamson if (ans != intval) {
2573dfc11533SChris Williamson (void) fprintf(stderr,
2574dfc11533SChris Williamson "%s: zfs found %lld, but zcp found %lld\n",
2575dfc11533SChris Williamson zfs_prop_to_name(prop),
2576dfc11533SChris Williamson (longlong_t)intval, (longlong_t)ans);
2577dfc11533SChris Williamson }
2578dfc11533SChris Williamson } else {
2579dfc11533SChris Williamson char *str_ans;
2580dfc11533SChris Williamson error = nvlist_lookup_string(retnvl, "value", &str_ans);
2581dfc11533SChris Williamson if (error != 0) {
2582dfc11533SChris Williamson (void) fprintf(stderr, "zcp check error: %u\n",
2583dfc11533SChris Williamson error);
2584dfc11533SChris Williamson return;
2585dfc11533SChris Williamson }
2586dfc11533SChris Williamson if (strcmp(strval, str_ans) != 0) {
2587dfc11533SChris Williamson (void) fprintf(stderr,
2588dfc11533SChris Williamson "%s: zfs found %s, but zcp found %s\n",
2589dfc11533SChris Williamson zfs_prop_to_name(prop),
2590dfc11533SChris Williamson strval, str_ans);
2591dfc11533SChris Williamson }
2592dfc11533SChris Williamson }
2593dfc11533SChris Williamson } else {
2594dfc11533SChris Williamson (void) fprintf(stderr,
2595dfc11533SChris Williamson "zcp check failed, channel program error: %u\n", error);
2596dfc11533SChris Williamson }
2597dfc11533SChris Williamson nvlist_free(argnvl);
2598dfc11533SChris Williamson nvlist_free(outnvl);
2599dfc11533SChris Williamson }
2600dfc11533SChris Williamson
2601fa9e4066Sahrens /*
2602fa9e4066Sahrens * Retrieve a property from the given object. If 'literal' is specified, then
2603fa9e4066Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a
2604fa9e4066Sahrens * human-readable form.
2605fa9e4066Sahrens *
2606fa9e4066Sahrens * Returns 0 on success, or -1 on error.
2607fa9e4066Sahrens */
2608fa9e4066Sahrens int
zfs_prop_get(zfs_handle_t * zhp,zfs_prop_t prop,char * propbuf,size_t proplen,zprop_source_t * src,char * statbuf,size_t statlen,boolean_t literal)2609fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2610990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2611fa9e4066Sahrens {
2612fa9e4066Sahrens char *source = NULL;
2613fa9e4066Sahrens uint64_t val;
26149c3fd121SMatthew Ahrens const char *str;
2615e9dbad6fSeschrock const char *strval;
261692241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp);
2617fa9e4066Sahrens
2618fa9e4066Sahrens /*
2619fa9e4066Sahrens * Check to see if this property applies to our object
2620fa9e4066Sahrens */
26214c2bdae2STim Chase if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2622fa9e4066Sahrens return (-1);
2623fa9e4066Sahrens
262492241e0bSTom Erickson if (received && zfs_prop_readonly(prop))
262592241e0bSTom Erickson return (-1);
262692241e0bSTom Erickson
2627fa9e4066Sahrens if (src)
2628990b4856Slling *src = ZPROP_SRC_NONE;
2629fa9e4066Sahrens
2630fa9e4066Sahrens switch (prop) {
2631fa9e4066Sahrens case ZFS_PROP_CREATION:
2632fa9e4066Sahrens /*
2633fa9e4066Sahrens * 'creation' is a time_t stored in the statistics. We convert
2634fa9e4066Sahrens * this into a string unless 'literal' is specified.
2635fa9e4066Sahrens */
2636fa9e4066Sahrens {
2637a2eea2e1Sahrens val = getprop_uint64(zhp, prop, &source);
2638a2eea2e1Sahrens time_t time = (time_t)val;
2639fa9e4066Sahrens struct tm t;
2640fa9e4066Sahrens
2641fa9e4066Sahrens if (literal ||
2642fa9e4066Sahrens localtime_r(&time, &t) == NULL ||
2643fa9e4066Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2644fa9e4066Sahrens &t) == 0)
2645a2eea2e1Sahrens (void) snprintf(propbuf, proplen, "%llu", val);
2646fa9e4066Sahrens }
2647dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL);
2648fa9e4066Sahrens break;
2649fa9e4066Sahrens
2650fa9e4066Sahrens case ZFS_PROP_MOUNTPOINT:
2651fa9e4066Sahrens /*
2652fa9e4066Sahrens * Getting the precise mountpoint can be tricky.
2653fa9e4066Sahrens *
2654fa9e4066Sahrens * - for 'none' or 'legacy', return those values.
2655fa9e4066Sahrens * - for inherited mountpoints, we want to take everything
2656fa9e4066Sahrens * after our ancestor and append it to the inherited value.
2657fa9e4066Sahrens *
2658fa9e4066Sahrens * If the pool has an alternate root, we want to prepend that
2659fa9e4066Sahrens * root to any values we return.
2660fa9e4066Sahrens */
266129ab75c9Srm
26627f7322feSeschrock str = getprop_string(zhp, prop, &source);
2663fa9e4066Sahrens
2664b21718f0Sgw if (str[0] == '/') {
266529ab75c9Srm char buf[MAXPATHLEN];
266629ab75c9Srm char *root = buf;
2667a79992aaSTom Erickson const char *relpath;
2668fa9e4066Sahrens
2669a79992aaSTom Erickson /*
2670a79992aaSTom Erickson * If we inherit the mountpoint, even from a dataset
2671a79992aaSTom Erickson * with a received value, the source will be the path of
2672a79992aaSTom Erickson * the dataset we inherit from. If source is
2673a79992aaSTom Erickson * ZPROP_SOURCE_VAL_RECVD, the received value is not
2674a79992aaSTom Erickson * inherited.
2675a79992aaSTom Erickson */
2676a79992aaSTom Erickson if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2677a79992aaSTom Erickson relpath = "";
2678a79992aaSTom Erickson } else {
2679a79992aaSTom Erickson relpath = zhp->zfs_name + strlen(source);
2680a79992aaSTom Erickson if (relpath[0] == '/')
2681a79992aaSTom Erickson relpath++;
2682a79992aaSTom Erickson }
2683b21718f0Sgw
268429ab75c9Srm if ((zpool_get_prop(zhp->zpool_hdl,
2685c58b3526SAdam Stevko ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2686c58b3526SAdam Stevko B_FALSE)) || (strcmp(root, "-") == 0))
268729ab75c9Srm root[0] = '\0';
2688b21718f0Sgw /*
2689b21718f0Sgw * Special case an alternate root of '/'. This will
2690b21718f0Sgw * avoid having multiple leading slashes in the
2691b21718f0Sgw * mountpoint path.
2692b21718f0Sgw */
2693b21718f0Sgw if (strcmp(root, "/") == 0)
2694b21718f0Sgw root++;
2695b21718f0Sgw
2696b21718f0Sgw /*
2697b21718f0Sgw * If the mountpoint is '/' then skip over this
2698b21718f0Sgw * if we are obtaining either an alternate root or
2699b21718f0Sgw * an inherited mountpoint.
2700b21718f0Sgw */
2701b21718f0Sgw if (str[1] == '\0' && (root[0] != '\0' ||
2702b21718f0Sgw relpath[0] != '\0'))
27037f7322feSeschrock str++;
2704fa9e4066Sahrens
2705fa9e4066Sahrens if (relpath[0] == '\0')
2706fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s",
27077f7322feSeschrock root, str);
2708fa9e4066Sahrens else
2709fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s",
27107f7322feSeschrock root, str, relpath[0] == '@' ? "" : "/",
2711fa9e4066Sahrens relpath);
2712fa9e4066Sahrens } else {
2713fa9e4066Sahrens /* 'legacy' or 'none' */
27147f7322feSeschrock (void) strlcpy(propbuf, str, proplen);
2715fa9e4066Sahrens }
27165b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, propbuf);
2717fa9e4066Sahrens break;
2718fa9e4066Sahrens
2719fa9e4066Sahrens case ZFS_PROP_ORIGIN:
27209c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source);
27219c3fd121SMatthew Ahrens if (str == NULL)
2722fa9e4066Sahrens return (-1);
27239c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen);
27245b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, str);
2725fa9e4066Sahrens break;
2726fa9e4066Sahrens
272719b94df9SMatthew Ahrens case ZFS_PROP_CLONES:
272819b94df9SMatthew Ahrens if (get_clones_string(zhp, propbuf, proplen) != 0)
272919b94df9SMatthew Ahrens return (-1);
273019b94df9SMatthew Ahrens break;
273119b94df9SMatthew Ahrens
2732fa9e4066Sahrens case ZFS_PROP_QUOTA:
2733a9799022Sck case ZFS_PROP_REFQUOTA:
2734fa9e4066Sahrens case ZFS_PROP_RESERVATION:
2735a9799022Sck case ZFS_PROP_REFRESERVATION:
2736a9799022Sck
273799653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
273899653d4eSeschrock return (-1);
2739fa9e4066Sahrens /*
2740fa9e4066Sahrens * If quota or reservation is 0, we translate this into 'none'
2741fa9e4066Sahrens * (unless literal is set), and indicate that it's the default
2742fa9e4066Sahrens * value. Otherwise, we print the number nicely and indicate
2743fa9e4066Sahrens * that its set locally.
2744fa9e4066Sahrens */
2745fa9e4066Sahrens if (val == 0) {
2746fa9e4066Sahrens if (literal)
2747fa9e4066Sahrens (void) strlcpy(propbuf, "0", proplen);
2748fa9e4066Sahrens else
2749fa9e4066Sahrens (void) strlcpy(propbuf, "none", proplen);
2750fa9e4066Sahrens } else {
2751fa9e4066Sahrens if (literal)
27525ad82045Snd (void) snprintf(propbuf, proplen, "%llu",
2753b1b8ab34Slling (u_longlong_t)val);
2754fa9e4066Sahrens else
2755*6520eed5SToomas Soome zfs_nicebytes(val, propbuf, proplen);
2756fa9e4066Sahrens }
2757dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL);
2758fa9e4066Sahrens break;
2759fa9e4066Sahrens
2760a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT:
2761a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT:
2762a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT:
2763a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT:
2764a2afb611SJerry Jelinek
2765a2afb611SJerry Jelinek if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2766a2afb611SJerry Jelinek return (-1);
2767a2afb611SJerry Jelinek
2768a2afb611SJerry Jelinek /*
2769a2afb611SJerry Jelinek * If limit is UINT64_MAX, we translate this into 'none' (unless
2770a2afb611SJerry Jelinek * literal is set), and indicate that it's the default value.
2771a2afb611SJerry Jelinek * Otherwise, we print the number nicely and indicate that it's
2772a2afb611SJerry Jelinek * set locally.
2773a2afb611SJerry Jelinek */
2774a2afb611SJerry Jelinek if (literal) {
2775a2afb611SJerry Jelinek (void) snprintf(propbuf, proplen, "%llu",
2776a2afb611SJerry Jelinek (u_longlong_t)val);
2777a2afb611SJerry Jelinek } else if (val == UINT64_MAX) {
2778a2afb611SJerry Jelinek (void) strlcpy(propbuf, "none", proplen);
2779a2afb611SJerry Jelinek } else {
2780a2afb611SJerry Jelinek zfs_nicenum(val, propbuf, proplen);
2781a2afb611SJerry Jelinek }
2782dfc11533SChris Williamson
2783dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL);
2784a2afb611SJerry Jelinek break;
2785a2afb611SJerry Jelinek
2786187d6ac0SMatt Ahrens case ZFS_PROP_REFRATIO:
2787fa9e4066Sahrens case ZFS_PROP_COMPRESSRATIO:
278899653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
278999653d4eSeschrock return (-1);
2790b24ab676SJeff Bonwick (void) snprintf(propbuf, proplen, "%llu.%02llux",
2791b24ab676SJeff Bonwick (u_longlong_t)(val / 100),
2792b24ab676SJeff Bonwick (u_longlong_t)(val % 100));
2793dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL);
2794fa9e4066Sahrens break;
2795fa9e4066Sahrens
2796fa9e4066Sahrens case ZFS_PROP_TYPE:
2797fa9e4066Sahrens switch (zhp->zfs_type) {
2798fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM:
2799fa9e4066Sahrens str = "filesystem";
2800fa9e4066Sahrens break;
2801fa9e4066Sahrens case ZFS_TYPE_VOLUME:
2802fa9e4066Sahrens str = "volume";
2803fa9e4066Sahrens break;
2804fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT:
2805fa9e4066Sahrens str = "snapshot";
2806fa9e4066Sahrens break;
280778f17100SMatthew Ahrens case ZFS_TYPE_BOOKMARK:
280878f17100SMatthew Ahrens str = "bookmark";
280978f17100SMatthew Ahrens break;
2810fa9e4066Sahrens default:
281199653d4eSeschrock abort();
2812fa9e4066Sahrens }
2813fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s", str);
28145b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, propbuf);
2815fa9e4066Sahrens break;
2816fa9e4066Sahrens
2817fa9e4066Sahrens case ZFS_PROP_MOUNTED:
2818fa9e4066Sahrens /*
2819fa9e4066Sahrens * The 'mounted' property is a pseudo-property that described
2820fa9e4066Sahrens * whether the filesystem is currently mounted. Even though
2821fa9e4066Sahrens * it's a boolean value, the typical values of "on" and "off"
2822fa9e4066Sahrens * don't make sense, so we translate to "yes" and "no".
2823fa9e4066Sahrens */
282499653d4eSeschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
282599653d4eSeschrock src, &source, &val) != 0)
282699653d4eSeschrock return (-1);
282799653d4eSeschrock if (val)
2828fa9e4066Sahrens (void) strlcpy(propbuf, "yes", proplen);
2829fa9e4066Sahrens else
2830fa9e4066Sahrens (void) strlcpy(propbuf, "no", proplen);
2831fa9e4066Sahrens break;
2832fa9e4066Sahrens
2833fa9e4066Sahrens case ZFS_PROP_NAME:
2834fa9e4066Sahrens /*
2835fa9e4066Sahrens * The 'name' property is a pseudo-property derived from the
2836fa9e4066Sahrens * dataset name. It is presented as a real property to simplify
2837fa9e4066Sahrens * consumers.
2838fa9e4066Sahrens */
2839fa9e4066Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen);
28405b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, propbuf);
2841fa9e4066Sahrens break;
2842fa9e4066Sahrens
28434201a95eSRic Aleshire case ZFS_PROP_MLSLABEL:
28444201a95eSRic Aleshire {
28454201a95eSRic Aleshire m_label_t *new_sl = NULL;
28464201a95eSRic Aleshire char *ascii = NULL; /* human readable label */
28474201a95eSRic Aleshire
28484201a95eSRic Aleshire (void) strlcpy(propbuf,
28494201a95eSRic Aleshire getprop_string(zhp, prop, &source), proplen);
28504201a95eSRic Aleshire
28514201a95eSRic Aleshire if (literal || (strcasecmp(propbuf,
28524201a95eSRic Aleshire ZFS_MLSLABEL_DEFAULT) == 0))
28534201a95eSRic Aleshire break;
28544201a95eSRic Aleshire
28554201a95eSRic Aleshire /*
28564201a95eSRic Aleshire * Try to translate the internal hex string to
28574201a95eSRic Aleshire * human-readable output. If there are any
28584201a95eSRic Aleshire * problems just use the hex string.
28594201a95eSRic Aleshire */
28604201a95eSRic Aleshire
28614201a95eSRic Aleshire if (str_to_label(propbuf, &new_sl, MAC_LABEL,
28624201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1) {
28634201a95eSRic Aleshire m_label_free(new_sl);
28644201a95eSRic Aleshire break;
28654201a95eSRic Aleshire }
28664201a95eSRic Aleshire
28674201a95eSRic Aleshire if (label_to_str(new_sl, &ascii, M_LABEL,
28684201a95eSRic Aleshire DEF_NAMES) != 0) {
28694201a95eSRic Aleshire if (ascii)
28704201a95eSRic Aleshire free(ascii);
28714201a95eSRic Aleshire m_label_free(new_sl);
28724201a95eSRic Aleshire break;
28734201a95eSRic Aleshire }
28744201a95eSRic Aleshire m_label_free(new_sl);
28754201a95eSRic Aleshire
28764201a95eSRic Aleshire (void) strlcpy(propbuf, ascii, proplen);
28774201a95eSRic Aleshire free(ascii);
28784201a95eSRic Aleshire }
28794201a95eSRic Aleshire break;
28804201a95eSRic Aleshire
2881f0f3ef5aSGarrett D'Amore case ZFS_PROP_GUID:
2882*6520eed5SToomas Soome case ZFS_PROP_KEY_GUID:
2883*6520eed5SToomas Soome case ZFS_PROP_IVSET_GUID:
2884e8d4a73cSJosh Paetzel case ZFS_PROP_CREATETXG:
2885*6520eed5SToomas Soome case ZFS_PROP_OBJSETID:
2886*6520eed5SToomas Soome case ZFS_PROP_PBKDF2_ITERS:
2887f0f3ef5aSGarrett D'Amore /*
2888*6520eed5SToomas Soome * These properties are stored as numbers, but they are
2889*6520eed5SToomas Soome * identifiers or counters.
2890f0f3ef5aSGarrett D'Amore * We don't want them to be pretty printed, because pretty
2891*6520eed5SToomas Soome * printing truncates their values making them useless.
2892f0f3ef5aSGarrett D'Amore */
2893f0f3ef5aSGarrett D'Amore if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2894f0f3ef5aSGarrett D'Amore return (-1);
2895f0f3ef5aSGarrett D'Amore (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2896dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL);
2897f0f3ef5aSGarrett D'Amore break;
2898f0f3ef5aSGarrett D'Amore
2899*6520eed5SToomas Soome case ZFS_PROP_REFERENCED:
2900*6520eed5SToomas Soome case ZFS_PROP_AVAILABLE:
2901*6520eed5SToomas Soome case ZFS_PROP_USED:
2902*6520eed5SToomas Soome case ZFS_PROP_USEDSNAP:
2903*6520eed5SToomas Soome case ZFS_PROP_USEDDS:
2904*6520eed5SToomas Soome case ZFS_PROP_USEDREFRESERV:
2905*6520eed5SToomas Soome case ZFS_PROP_USEDCHILD:
2906*6520eed5SToomas Soome if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2907*6520eed5SToomas Soome return (-1);
2908*6520eed5SToomas Soome if (literal) {
2909*6520eed5SToomas Soome (void) snprintf(propbuf, proplen, "%llu",
2910*6520eed5SToomas Soome (u_longlong_t)val);
2911*6520eed5SToomas Soome } else {
2912*6520eed5SToomas Soome zfs_nicebytes(val, propbuf, proplen);
2913*6520eed5SToomas Soome }
2914*6520eed5SToomas Soome zcp_check(zhp, prop, val, NULL);
2915*6520eed5SToomas Soome break;
2916*6520eed5SToomas Soome
2917fa9e4066Sahrens default:
2918e7437265Sahrens switch (zfs_prop_get_type(prop)) {
291991ebeef5Sahrens case PROP_TYPE_NUMBER:
2920e7437265Sahrens if (get_numeric_property(zhp, prop, src,
2921dfc11533SChris Williamson &source, &val) != 0) {
2922e7437265Sahrens return (-1);
2923dfc11533SChris Williamson }
2924dfc11533SChris Williamson
2925dfc11533SChris Williamson if (literal) {
2926e7437265Sahrens (void) snprintf(propbuf, proplen, "%llu",
2927e7437265Sahrens (u_longlong_t)val);
2928dfc11533SChris Williamson } else {
2929e7437265Sahrens zfs_nicenum(val, propbuf, proplen);
2930dfc11533SChris Williamson }
2931dfc11533SChris Williamson zcp_check(zhp, prop, val, NULL);
2932e7437265Sahrens break;
2933e7437265Sahrens
293491ebeef5Sahrens case PROP_TYPE_STRING:
29359c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source);
29369c3fd121SMatthew Ahrens if (str == NULL)
29379c3fd121SMatthew Ahrens return (-1);
2938dfc11533SChris Williamson
29399c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen);
29405b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, str);
2941e7437265Sahrens break;
2942e7437265Sahrens
294391ebeef5Sahrens case PROP_TYPE_INDEX:
2944fe192f76Sahrens if (get_numeric_property(zhp, prop, src,
2945fe192f76Sahrens &source, &val) != 0)
2946fe192f76Sahrens return (-1);
2947fe192f76Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2948e7437265Sahrens return (-1);
2949dfc11533SChris Williamson
2950e7437265Sahrens (void) strlcpy(propbuf, strval, proplen);
29515b8cbb8eSToomas Soome zcp_check(zhp, prop, 0, strval);
2952e7437265Sahrens break;
2953e7437265Sahrens
2954e7437265Sahrens default:
2955e7437265Sahrens abort();
2956e7437265Sahrens }
2957fa9e4066Sahrens }
2958fa9e4066Sahrens
2959fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen);
2960fa9e4066Sahrens
2961fa9e4066Sahrens return (0);
2962fa9e4066Sahrens }
2963fa9e4066Sahrens
2964fa9e4066Sahrens /*
2965fa9e4066Sahrens * Utility function to get the given numeric property. Does no validation that
2966fa9e4066Sahrens * the given property is the appropriate type; should only be used with
2967fa9e4066Sahrens * hard-coded property types.
2968fa9e4066Sahrens */
2969fa9e4066Sahrens uint64_t
zfs_prop_get_int(zfs_handle_t * zhp,zfs_prop_t prop)2970fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2971fa9e4066Sahrens {
2972fa9e4066Sahrens char *source;
297399653d4eSeschrock uint64_t val;
297499653d4eSeschrock
29753cb34c60Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2976fa9e4066Sahrens
297799653d4eSeschrock return (val);
2978fa9e4066Sahrens }
2979fa9e4066Sahrens
29807b97dc1aSrm int
zfs_prop_set_int(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t val)29817b97dc1aSrm zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
29827b97dc1aSrm {
29837b97dc1aSrm char buf[64];
29847b97dc1aSrm
298514843421SMatthew Ahrens (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
29867b97dc1aSrm return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
29877b97dc1aSrm }
29887b97dc1aSrm
2989fa9e4066Sahrens /*
2990fa9e4066Sahrens * Similar to zfs_prop_get(), but returns the value as an integer.
2991fa9e4066Sahrens */
2992fa9e4066Sahrens int
zfs_prop_get_numeric(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t * value,zprop_source_t * src,char * statbuf,size_t statlen)2993fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2994990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen)
2995fa9e4066Sahrens {
2996fa9e4066Sahrens char *source;
2997fa9e4066Sahrens
2998fa9e4066Sahrens /*
2999fa9e4066Sahrens * Check to see if this property applies to our object
3000fa9e4066Sahrens */
30014c2bdae2STim Chase if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) {
3002ece3d9b3Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
300399653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
300499653d4eSeschrock zfs_prop_to_name(prop)));
3005e45ce728Sahrens }
3006fa9e4066Sahrens
3007fa9e4066Sahrens if (src)
3008990b4856Slling *src = ZPROP_SRC_NONE;
3009fa9e4066Sahrens
301099653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0)
301199653d4eSeschrock return (-1);
3012fa9e4066Sahrens
3013fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen);
3014fa9e4066Sahrens
3015fa9e4066Sahrens return (0);
3016fa9e4066Sahrens }
3017fa9e4066Sahrens
301814843421SMatthew Ahrens static int
idmap_id_to_numeric_domain_rid(uid_t id,boolean_t isuser,char ** domainp,idmap_rid_t * ridp)301914843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
302014843421SMatthew Ahrens char **domainp, idmap_rid_t *ridp)
302114843421SMatthew Ahrens {
302214843421SMatthew Ahrens idmap_get_handle_t *get_hdl = NULL;
302314843421SMatthew Ahrens idmap_stat status;
302414843421SMatthew Ahrens int err = EINVAL;
302514843421SMatthew Ahrens
30261fdeec65Sjoyce mcintosh if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
302714843421SMatthew Ahrens goto out;
302814843421SMatthew Ahrens
302914843421SMatthew Ahrens if (isuser) {
303014843421SMatthew Ahrens err = idmap_get_sidbyuid(get_hdl, id,
303114843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
303214843421SMatthew Ahrens } else {
303314843421SMatthew Ahrens err = idmap_get_sidbygid(get_hdl, id,
303414843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
303514843421SMatthew Ahrens }
303614843421SMatthew Ahrens if (err == IDMAP_SUCCESS &&
303714843421SMatthew Ahrens idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
303814843421SMatthew Ahrens status == IDMAP_SUCCESS)
303914843421SMatthew Ahrens err = 0;
304014843421SMatthew Ahrens else
304114843421SMatthew Ahrens err = EINVAL;
304214843421SMatthew Ahrens out:
304314843421SMatthew Ahrens if (get_hdl)
304414843421SMatthew Ahrens idmap_get_destroy(get_hdl);
304514843421SMatthew Ahrens return (err);
304614843421SMatthew Ahrens }
304714843421SMatthew Ahrens
304814843421SMatthew Ahrens /*
304914843421SMatthew Ahrens * convert the propname into parameters needed by kernel
305014843421SMatthew Ahrens * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
305114843421SMatthew Ahrens * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3052f67950b2SNasf-Fan * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3053f67950b2SNasf-Fan * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3054f67950b2SNasf-Fan * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3055f67950b2SNasf-Fan * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
305614843421SMatthew Ahrens */
305714843421SMatthew Ahrens static int
userquota_propname_decode(const char * propname,boolean_t zoned,zfs_userquota_prop_t * typep,char * domain,int domainlen,uint64_t * ridp)305814843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned,
305914843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
306014843421SMatthew Ahrens {
306114843421SMatthew Ahrens zfs_userquota_prop_t type;
3062f67950b2SNasf-Fan char *cp;
306314843421SMatthew Ahrens boolean_t isuser;
3064f67950b2SNasf-Fan boolean_t isgroup;
3065f67950b2SNasf-Fan boolean_t isproject;
3066f67950b2SNasf-Fan struct passwd *pw;
3067f67950b2SNasf-Fan struct group *gr;
306814843421SMatthew Ahrens
306914843421SMatthew Ahrens domain[0] = '\0';
3070f67950b2SNasf-Fan
3071f67950b2SNasf-Fan /* Figure out the property type ({user|group|project}{quota|space}) */
307214843421SMatthew Ahrens for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
307314843421SMatthew Ahrens if (strncmp(propname, zfs_userquota_prop_prefixes[type],
307414843421SMatthew Ahrens strlen(zfs_userquota_prop_prefixes[type])) == 0)
307514843421SMatthew Ahrens break;
307614843421SMatthew Ahrens }
307714843421SMatthew Ahrens if (type == ZFS_NUM_USERQUOTA_PROPS)
307814843421SMatthew Ahrens return (EINVAL);
307914843421SMatthew Ahrens *typep = type;
308014843421SMatthew Ahrens
3081f67950b2SNasf-Fan isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED ||
3082f67950b2SNasf-Fan type == ZFS_PROP_USEROBJQUOTA ||
3083f67950b2SNasf-Fan type == ZFS_PROP_USEROBJUSED);
3084f67950b2SNasf-Fan isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED ||
3085f67950b2SNasf-Fan type == ZFS_PROP_GROUPOBJQUOTA ||
3086f67950b2SNasf-Fan type == ZFS_PROP_GROUPOBJUSED);
3087f67950b2SNasf-Fan isproject = (type == ZFS_PROP_PROJECTQUOTA ||
3088f67950b2SNasf-Fan type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA ||
3089f67950b2SNasf-Fan type == ZFS_PROP_PROJECTOBJUSED);
309014843421SMatthew Ahrens
309114843421SMatthew Ahrens cp = strchr(propname, '@') + 1;
309214843421SMatthew Ahrens
3093f67950b2SNasf-Fan if (isuser && (pw = getpwnam(cp)) != NULL) {
3094f67950b2SNasf-Fan if (zoned && getzoneid() == GLOBAL_ZONEID)
3095f67950b2SNasf-Fan return (ENOENT);
3096f67950b2SNasf-Fan *ridp = pw->pw_uid;
3097f67950b2SNasf-Fan } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
3098f67950b2SNasf-Fan if (zoned && getzoneid() == GLOBAL_ZONEID)
3099f67950b2SNasf-Fan return (ENOENT);
3100f67950b2SNasf-Fan *ridp = gr->gr_gid;
3101f67950b2SNasf-Fan } else if (!isproject && strchr(cp, '@')) {
310214843421SMatthew Ahrens /*
310314843421SMatthew Ahrens * It's a SID name (eg "user@domain") that needs to be
31043b12c289SMatthew Ahrens * turned into S-1-domainID-RID.
310514843421SMatthew Ahrens */
3106f67950b2SNasf-Fan directory_error_t e;
3107f67950b2SNasf-Fan char *numericsid = NULL;
3108f67950b2SNasf-Fan char *end;
3109f67950b2SNasf-Fan
311014843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID)
311114843421SMatthew Ahrens return (ENOENT);
31123b12c289SMatthew Ahrens if (isuser) {
3113f67950b2SNasf-Fan e = directory_sid_from_user_name(NULL,
3114f67950b2SNasf-Fan cp, &numericsid);
31153b12c289SMatthew Ahrens } else {
3116f67950b2SNasf-Fan e = directory_sid_from_group_name(NULL,
3117f67950b2SNasf-Fan cp, &numericsid);
31181ed6b69aSGordon Ross }
3119f67950b2SNasf-Fan if (e != NULL) {
3120f67950b2SNasf-Fan directory_error_free(e);
312114843421SMatthew Ahrens return (ENOENT);
31223b12c289SMatthew Ahrens }
31233b12c289SMatthew Ahrens if (numericsid == NULL)
312414843421SMatthew Ahrens return (ENOENT);
31253b12c289SMatthew Ahrens cp = numericsid;
31263b12c289SMatthew Ahrens (void) strlcpy(domain, cp, domainlen);
3127f67950b2SNasf-Fan cp = strrchr(domain, '-');
3128f67950b2SNasf-Fan *cp = '\0';
3129f67950b2SNasf-Fan cp++;
3130f67950b2SNasf-Fan
313114843421SMatthew Ahrens errno = 0;
3132f67950b2SNasf-Fan *ridp = strtoull(cp, &end, 10);
3133f67950b2SNasf-Fan free(numericsid);
3134f67950b2SNasf-Fan
3135777badbaSMatthew Ahrens if (errno != 0 || *end != '\0')
313614843421SMatthew Ahrens return (EINVAL);
313714843421SMatthew Ahrens } else {
3138f67950b2SNasf-Fan /* It's a user/group/project ID (eg "12345"). */
3139f67950b2SNasf-Fan char *end;
314014843421SMatthew Ahrens uid_t id = strtoul(cp, &end, 10);
314114843421SMatthew Ahrens if (*end != '\0')
314214843421SMatthew Ahrens return (EINVAL);
3143f67950b2SNasf-Fan if (id > MAXUID && !isproject) {
314414843421SMatthew Ahrens /* It's an ephemeral ID. */
3145f67950b2SNasf-Fan idmap_rid_t rid;
3146f67950b2SNasf-Fan char *mapdomain;
3147f67950b2SNasf-Fan
314814843421SMatthew Ahrens if (idmap_id_to_numeric_domain_rid(id, isuser,
314914843421SMatthew Ahrens &mapdomain, &rid) != 0)
315014843421SMatthew Ahrens return (ENOENT);
31513b12c289SMatthew Ahrens (void) strlcpy(domain, mapdomain, domainlen);
315214843421SMatthew Ahrens *ridp = rid;
315314843421SMatthew Ahrens } else {
315414843421SMatthew Ahrens *ridp = id;
315514843421SMatthew Ahrens }
315614843421SMatthew Ahrens }
315714843421SMatthew Ahrens
315814843421SMatthew Ahrens return (0);
315914843421SMatthew Ahrens }
316014843421SMatthew Ahrens
3161edea4b55SLin Ling static int
zfs_prop_get_userquota_common(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue,zfs_userquota_prop_t * typep)3162edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3163edea4b55SLin Ling uint64_t *propvalue, zfs_userquota_prop_t *typep)
316414843421SMatthew Ahrens {
316514843421SMatthew Ahrens int err;
316614843421SMatthew Ahrens zfs_cmd_t zc = { 0 };
316714843421SMatthew Ahrens
316819b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
316914843421SMatthew Ahrens
317014843421SMatthew Ahrens err = userquota_propname_decode(propname,
317114843421SMatthew Ahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3172edea4b55SLin Ling typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3173edea4b55SLin Ling zc.zc_objset_type = *typep;
317414843421SMatthew Ahrens if (err)
317514843421SMatthew Ahrens return (err);
317614843421SMatthew Ahrens
317714843421SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
317814843421SMatthew Ahrens if (err)
317914843421SMatthew Ahrens return (err);
318014843421SMatthew Ahrens
3181edea4b55SLin Ling *propvalue = zc.zc_cookie;
3182edea4b55SLin Ling return (0);
3183edea4b55SLin Ling }
3184edea4b55SLin Ling
3185edea4b55SLin Ling int
zfs_prop_get_userquota_int(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue)3186edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3187edea4b55SLin Ling uint64_t *propvalue)
3188edea4b55SLin Ling {
3189edea4b55SLin Ling zfs_userquota_prop_t type;
3190edea4b55SLin Ling
3191edea4b55SLin Ling return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3192edea4b55SLin Ling &type));
3193edea4b55SLin Ling }
3194edea4b55SLin Ling
3195edea4b55SLin Ling int
zfs_prop_get_userquota(zfs_handle_t * zhp,const char * propname,char * propbuf,int proplen,boolean_t literal)3196edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3197edea4b55SLin Ling char *propbuf, int proplen, boolean_t literal)
3198edea4b55SLin Ling {
3199edea4b55SLin Ling int err;
3200edea4b55SLin Ling uint64_t propvalue;
3201edea4b55SLin Ling zfs_userquota_prop_t type;
3202edea4b55SLin Ling
3203edea4b55SLin Ling err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3204edea4b55SLin Ling &type);
3205edea4b55SLin Ling
3206edea4b55SLin Ling if (err)
3207edea4b55SLin Ling return (err);
3208edea4b55SLin Ling
320914843421SMatthew Ahrens if (literal) {
3210edea4b55SLin Ling (void) snprintf(propbuf, proplen, "%llu", propvalue);
3211edea4b55SLin Ling } else if (propvalue == 0 &&
3212f67950b2SNasf-Fan (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3213f67950b2SNasf-Fan type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
3214*6520eed5SToomas Soome type == ZFS_PROP_PROJECTQUOTA ||
3215*6520eed5SToomas Soome type == ZFS_PROP_PROJECTOBJQUOTA)) {
321614843421SMatthew Ahrens (void) strlcpy(propbuf, "none", proplen);
3217f67950b2SNasf-Fan } else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3218f67950b2SNasf-Fan type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED ||
3219f67950b2SNasf-Fan type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) {
3220*6520eed5SToomas Soome zfs_nicebytes(propvalue, propbuf, proplen);
322114843421SMatthew Ahrens } else {
3222edea4b55SLin Ling zfs_nicenum(propvalue, propbuf, proplen);
322314843421SMatthew Ahrens }
322414843421SMatthew Ahrens return (0);
322514843421SMatthew Ahrens }
322614843421SMatthew Ahrens
322719b94df9SMatthew Ahrens int
zfs_prop_get_written_int(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue)322819b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
322919b94df9SMatthew Ahrens uint64_t *propvalue)
32308ac09fceSRichard Lowe {
323119b94df9SMatthew Ahrens int err;
323219b94df9SMatthew Ahrens zfs_cmd_t zc = { 0 };
323319b94df9SMatthew Ahrens const char *snapname;
3234ebedde84SEric Taylor
323519b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3236e5351341SMatthew Ahrens
323719b94df9SMatthew Ahrens snapname = strchr(propname, '@') + 1;
323819b94df9SMatthew Ahrens if (strchr(snapname, '@')) {
323919b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
324019b94df9SMatthew Ahrens } else {
324119b94df9SMatthew Ahrens /* snapname is the short name, append it to zhp's fsname */
324219b94df9SMatthew Ahrens char *cp;
3243e5351341SMatthew Ahrens
324419b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, zhp->zfs_name,
324519b94df9SMatthew Ahrens sizeof (zc.zc_value));
324619b94df9SMatthew Ahrens cp = strchr(zc.zc_value, '@');
324719b94df9SMatthew Ahrens if (cp != NULL)
324819b94df9SMatthew Ahrens *cp = '\0';
324919b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
325019b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
32518ac09fceSRichard Lowe }
325219b94df9SMatthew Ahrens
325319b94df9SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
325419b94df9SMatthew Ahrens if (err)
325519b94df9SMatthew Ahrens return (err);
325619b94df9SMatthew Ahrens
325719b94df9SMatthew Ahrens *propvalue = zc.zc_cookie;
325819b94df9SMatthew Ahrens return (0);
3259ebedde84SEric Taylor }
3260ebedde84SEric Taylor
3261fa9e4066Sahrens int
zfs_prop_get_written(zfs_handle_t * zhp,const char * propname,char * propbuf,int proplen,boolean_t literal)326219b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
326319b94df9SMatthew Ahrens char *propbuf, int proplen, boolean_t literal)
3264fa9e4066Sahrens {
326519b94df9SMatthew Ahrens int err;
326619b94df9SMatthew Ahrens uint64_t propvalue;
32673cb34c60Sahrens
326819b94df9SMatthew Ahrens err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3269ebedde84SEric Taylor
327019b94df9SMatthew Ahrens if (err)
327119b94df9SMatthew Ahrens return (err);
32728ac09fceSRichard Lowe
327319b94df9SMatthew Ahrens if (literal) {
327419b94df9SMatthew Ahrens (void) snprintf(propbuf, proplen, "%llu", propvalue);
327519b94df9SMatthew Ahrens } else {
3276*6520eed5SToomas Soome zfs_nicebytes(propvalue, propbuf, proplen);
3277fa9e4066Sahrens }
327819b94df9SMatthew Ahrens return (0);
32797f7322feSeschrock }
32807f7322feSeschrock
32817f7322feSeschrock /*
328219b94df9SMatthew Ahrens * Returns the name of the given zfs handle.
32837f7322feSeschrock */
328419b94df9SMatthew Ahrens const char *
zfs_get_name(const zfs_handle_t * zhp)328519b94df9SMatthew Ahrens zfs_get_name(const zfs_handle_t *zhp)
32867f7322feSeschrock {
328719b94df9SMatthew Ahrens return (zhp->zfs_name);
328819b94df9SMatthew Ahrens }
32898ac09fceSRichard Lowe
32908808ac5dSYuri Pankov /*
32918808ac5dSYuri Pankov * Returns the name of the parent pool for the given zfs handle.
32928808ac5dSYuri Pankov */
32938808ac5dSYuri Pankov const char *
zfs_get_pool_name(const zfs_handle_t * zhp)32948808ac5dSYuri Pankov zfs_get_pool_name(const zfs_handle_t *zhp)
32958808ac5dSYuri Pankov {
32968808ac5dSYuri Pankov return (zhp->zpool_hdl->zpool_name);
32978808ac5dSYuri Pankov }
32988808ac5dSYuri Pankov
329919b94df9SMatthew Ahrens /*
330019b94df9SMatthew Ahrens * Returns the type of the given zfs handle.
330119b94df9SMatthew Ahrens */
330219b94df9SMatthew Ahrens zfs_type_t
zfs_get_type(const zfs_handle_t * zhp)330319b94df9SMatthew Ahrens zfs_get_type(const zfs_handle_t *zhp)
330419b94df9SMatthew Ahrens {
330519b94df9SMatthew Ahrens return (zhp->zfs_type);
33067f7322feSeschrock }
33077f7322feSeschrock
3308d41c4376SMark J Musante /*
3309d41c4376SMark J Musante * Is one dataset name a child dataset of another?
3310d41c4376SMark J Musante *
3311d41c4376SMark J Musante * Needs to handle these cases:
3312d41c4376SMark J Musante * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3313d41c4376SMark J Musante * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3314d41c4376SMark J Musante * Descendant? No. No. No. Yes.
3315d41c4376SMark J Musante */
3316d41c4376SMark J Musante static boolean_t
is_descendant(const char * ds1,const char * ds2)3317d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2)
3318d41c4376SMark J Musante {
3319d41c4376SMark J Musante size_t d1len = strlen(ds1);
3320d41c4376SMark J Musante
3321d41c4376SMark J Musante /* ds2 can't be a descendant if it's smaller */
3322d41c4376SMark J Musante if (strlen(ds2) < d1len)
3323d41c4376SMark J Musante return (B_FALSE);
3324d41c4376SMark J Musante
3325d41c4376SMark J Musante /* otherwise, compare strings and verify that there's a '/' char */
3326d41c4376SMark J Musante return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3327d41c4376SMark J Musante }
3328d41c4376SMark J Musante
3329fa9e4066Sahrens /*
3330fa9e4066Sahrens * Given a complete name, return just the portion that refers to the parent.
333119b94df9SMatthew Ahrens * Will return -1 if there is no parent (path is just the name of the
333219b94df9SMatthew Ahrens * pool).
3333fa9e4066Sahrens */
3334fa9e4066Sahrens static int
parent_name(const char * path,char * buf,size_t buflen)3335fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen)
3336fa9e4066Sahrens {
333719b94df9SMatthew Ahrens char *slashp;
3338fa9e4066Sahrens
333919b94df9SMatthew Ahrens (void) strlcpy(buf, path, buflen);
3340fa9e4066Sahrens
334119b94df9SMatthew Ahrens if ((slashp = strrchr(buf, '/')) == NULL)
334219b94df9SMatthew Ahrens return (-1);
334319b94df9SMatthew Ahrens *slashp = '\0';
3344fa9e4066Sahrens
3345fa9e4066Sahrens return (0);
3346fa9e4066Sahrens }
3347fa9e4066Sahrens
3348eb633035STom Caputi int
zfs_parent_name(zfs_handle_t * zhp,char * buf,size_t buflen)3349eb633035STom Caputi zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen)
3350eb633035STom Caputi {
3351eb633035STom Caputi return (parent_name(zfs_get_name(zhp), buf, buflen));
3352eb633035STom Caputi }
3353eb633035STom Caputi
3354fa9e4066Sahrens /*
33557f1f55eaSvb * If accept_ancestor is false, then check to make sure that the given path has
33567f1f55eaSvb * a parent, and that it exists. If accept_ancestor is true, then find the
33577f1f55eaSvb * closest existing ancestor for the given path. In prefixlen return the
33587f1f55eaSvb * length of already existing prefix of the given path. We also fetch the
33597f1f55eaSvb * 'zoned' property, which is used to validate property settings when creating
33607f1f55eaSvb * new datasets.
3361fa9e4066Sahrens */
3362fa9e4066Sahrens static int
check_parents(libzfs_handle_t * hdl,const char * path,uint64_t * zoned,boolean_t accept_ancestor,int * prefixlen)33637f1f55eaSvb check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
33647f1f55eaSvb boolean_t accept_ancestor, int *prefixlen)
3365fa9e4066Sahrens {
3366fa9e4066Sahrens zfs_cmd_t zc = { 0 };
33679adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN];
3368fa9e4066Sahrens char *slash;
33697f7322feSeschrock zfs_handle_t *zhp;
337099653d4eSeschrock char errbuf[1024];
3371d41c4376SMark J Musante uint64_t is_zoned;
337299653d4eSeschrock
3373deb8317bSMark J Musante (void) snprintf(errbuf, sizeof (errbuf),
3374deb8317bSMark J Musante dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3375fa9e4066Sahrens
3376fa9e4066Sahrens /* get parent, and check to see if this is just a pool */
3377fa9e4066Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) {
337899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
337999653d4eSeschrock "missing dataset name"));
338099653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3381fa9e4066Sahrens }
3382fa9e4066Sahrens
3383fa9e4066Sahrens /* check to see if the pool exists */
3384fa9e4066Sahrens if ((slash = strchr(parent, '/')) == NULL)
3385fa9e4066Sahrens slash = parent + strlen(parent);
33868ac09fceSRichard Lowe (void) strncpy(zc.zc_name, parent, slash - parent);
3387fa9e4066Sahrens zc.zc_name[slash - parent] = '\0';
338899653d4eSeschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3389fa9e4066Sahrens errno == ENOENT) {
339099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
339199653d4eSeschrock "no such pool '%s'"), zc.zc_name);
339299653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf));
3393fa9e4066Sahrens }
3394fa9e4066Sahrens
3395fa9e4066Sahrens /* check to see if the parent dataset exists */
33967f1f55eaSvb while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
33977f1f55eaSvb if (errno == ENOENT && accept_ancestor) {
33987f1f55eaSvb /*
33997f1f55eaSvb * Go deeper to find an ancestor, give up on top level.
34007f1f55eaSvb */
34017f1f55eaSvb if (parent_name(parent, parent, sizeof (parent)) != 0) {
34027f1f55eaSvb zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34037f1f55eaSvb "no such pool '%s'"), zc.zc_name);
34047f1f55eaSvb return (zfs_error(hdl, EZFS_NOENT, errbuf));
34057f1f55eaSvb }
34067f1f55eaSvb } else if (errno == ENOENT) {
340799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
340899653d4eSeschrock "parent does not exist"));
340999653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf));
34107f1f55eaSvb } else
341199653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf));
3412fa9e4066Sahrens }
3413fa9e4066Sahrens
3414d41c4376SMark J Musante is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3415d41c4376SMark J Musante if (zoned != NULL)
3416d41c4376SMark J Musante *zoned = is_zoned;
3417d41c4376SMark J Musante
3418fa9e4066Sahrens /* we are in a non-global zone, but parent is in the global zone */
3419d41c4376SMark J Musante if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
342099653d4eSeschrock (void) zfs_standard_error(hdl, EPERM, errbuf);
34217f7322feSeschrock zfs_close(zhp);
3422fa9e4066Sahrens return (-1);
3423fa9e4066Sahrens }
3424fa9e4066Sahrens
3425fa9e4066Sahrens /* make sure parent is a filesystem */
34267f7322feSeschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
342799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
342899653d4eSeschrock "parent is not a filesystem"));
342999653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
34307f7322feSeschrock zfs_close(zhp);
3431fa9e4066Sahrens return (-1);
3432fa9e4066Sahrens }
3433fa9e4066Sahrens
34347f7322feSeschrock zfs_close(zhp);
34357f1f55eaSvb if (prefixlen != NULL)
34367f1f55eaSvb *prefixlen = strlen(parent);
34377f1f55eaSvb return (0);
34387f1f55eaSvb }
34397f1f55eaSvb
34407f1f55eaSvb /*
34417f1f55eaSvb * Finds whether the dataset of the given type(s) exists.
34427f1f55eaSvb */
34437f1f55eaSvb boolean_t
zfs_dataset_exists(libzfs_handle_t * hdl,const char * path,zfs_type_t types)34447f1f55eaSvb zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
34457f1f55eaSvb {
34467f1f55eaSvb zfs_handle_t *zhp;
34477f1f55eaSvb
3448f18faf3fSek if (!zfs_validate_name(hdl, path, types, B_FALSE))
34497f1f55eaSvb return (B_FALSE);
34507f1f55eaSvb
34517f1f55eaSvb /*
34527f1f55eaSvb * Try to get stats for the dataset, which will tell us if it exists.
34537f1f55eaSvb */
34547f1f55eaSvb if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
34557f1f55eaSvb int ds_type = zhp->zfs_type;
34567f1f55eaSvb
34577f1f55eaSvb zfs_close(zhp);
34587f1f55eaSvb if (types & ds_type)
34597f1f55eaSvb return (B_TRUE);
34607f1f55eaSvb }
34617f1f55eaSvb return (B_FALSE);
34627f1f55eaSvb }
34637f1f55eaSvb
34643cb34c60Sahrens /*
34653cb34c60Sahrens * Given a path to 'target', create all the ancestors between
34663cb34c60Sahrens * the prefixlen portion of the path, and the target itself.
34673cb34c60Sahrens * Fail if the initial prefixlen-ancestor does not already exist.
34683cb34c60Sahrens */
34693cb34c60Sahrens int
create_parents(libzfs_handle_t * hdl,char * target,int prefixlen)34703cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
34713cb34c60Sahrens {
34723cb34c60Sahrens zfs_handle_t *h;
34733cb34c60Sahrens char *cp;
34743cb34c60Sahrens const char *opname;
34753cb34c60Sahrens
34763cb34c60Sahrens /* make sure prefix exists */
34773cb34c60Sahrens cp = target + prefixlen;
34783cb34c60Sahrens if (*cp != '/') {
34793cb34c60Sahrens assert(strchr(cp, '/') == NULL);
34803cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
34813cb34c60Sahrens } else {
34823cb34c60Sahrens *cp = '\0';
34833cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
34843cb34c60Sahrens *cp = '/';
34853cb34c60Sahrens }
34863cb34c60Sahrens if (h == NULL)
34873cb34c60Sahrens return (-1);
34883cb34c60Sahrens zfs_close(h);
34893cb34c60Sahrens
34903cb34c60Sahrens /*
34913cb34c60Sahrens * Attempt to create, mount, and share any ancestor filesystems,
34923cb34c60Sahrens * up to the prefixlen-long one.
34933cb34c60Sahrens */
34943cb34c60Sahrens for (cp = target + prefixlen + 1;
349588f61deeSIgor Kozhukhov (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
34963cb34c60Sahrens
34973cb34c60Sahrens *cp = '\0';
34983cb34c60Sahrens
34993cb34c60Sahrens h = make_dataset_handle(hdl, target);
35003cb34c60Sahrens if (h) {
35013cb34c60Sahrens /* it already exists, nothing to do here */
35023cb34c60Sahrens zfs_close(h);
35033cb34c60Sahrens continue;
35043cb34c60Sahrens }
35053cb34c60Sahrens
35063cb34c60Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
35073cb34c60Sahrens NULL) != 0) {
35083cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "create");
35093cb34c60Sahrens goto ancestorerr;
35103cb34c60Sahrens }
35113cb34c60Sahrens
35123cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
35133cb34c60Sahrens if (h == NULL) {
35143cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "open");
35153cb34c60Sahrens goto ancestorerr;
35163cb34c60Sahrens }
35173cb34c60Sahrens
35183cb34c60Sahrens if (zfs_mount(h, NULL, 0) != 0) {
35193cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "mount");
35203cb34c60Sahrens goto ancestorerr;
35213cb34c60Sahrens }
35223cb34c60Sahrens
35233cb34c60Sahrens if (zfs_share(h) != 0) {
35243cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "share");
35253cb34c60Sahrens goto ancestorerr;
35263cb34c60Sahrens }
35273cb34c60Sahrens
35283cb34c60Sahrens zfs_close(h);
35293cb34c60Sahrens }
35303cb34c60Sahrens
35313cb34c60Sahrens return (0);
35323cb34c60Sahrens
35333cb34c60Sahrens ancestorerr:
35343cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35353cb34c60Sahrens "failed to %s ancestor '%s'"), opname, target);
35363cb34c60Sahrens return (-1);
35373cb34c60Sahrens }
35383cb34c60Sahrens
35397f1f55eaSvb /*
35407f1f55eaSvb * Creates non-existing ancestors of the given path.
35417f1f55eaSvb */
35427f1f55eaSvb int
zfs_create_ancestors(libzfs_handle_t * hdl,const char * path)35437f1f55eaSvb zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
35447f1f55eaSvb {
35457f1f55eaSvb int prefix;
35467f1f55eaSvb char *path_copy;
35475ac95da7SSerapheim Dimitropoulos char errbuf[1024];
3548f83b46baSPaul Dagnelie int rc = 0;
35497f1f55eaSvb
35505ac95da7SSerapheim Dimitropoulos (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
35515ac95da7SSerapheim Dimitropoulos "cannot create '%s'"), path);
35525ac95da7SSerapheim Dimitropoulos
35535ac95da7SSerapheim Dimitropoulos /*
35545ac95da7SSerapheim Dimitropoulos * Check that we are not passing the nesting limit
35555ac95da7SSerapheim Dimitropoulos * before we start creating any ancestors.
35565ac95da7SSerapheim Dimitropoulos */
35575ac95da7SSerapheim Dimitropoulos if (dataset_nestcheck(path) != 0) {
35585ac95da7SSerapheim Dimitropoulos zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35595ac95da7SSerapheim Dimitropoulos "maximum name nesting depth exceeded"));
35605ac95da7SSerapheim Dimitropoulos return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
35615ac95da7SSerapheim Dimitropoulos }
35625ac95da7SSerapheim Dimitropoulos
3563d41c4376SMark J Musante if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
35647f1f55eaSvb return (-1);
35657f1f55eaSvb
35667f1f55eaSvb if ((path_copy = strdup(path)) != NULL) {
35677f1f55eaSvb rc = create_parents(hdl, path_copy, prefix);
35687f1f55eaSvb free(path_copy);
35697f1f55eaSvb }
35707f1f55eaSvb if (path_copy == NULL || rc != 0)
35717f1f55eaSvb return (-1);
35727f1f55eaSvb
3573fa9e4066Sahrens return (0);
3574fa9e4066Sahrens }
3575fa9e4066Sahrens
3576fa9e4066Sahrens /*
3577e9dbad6fSeschrock * Create a new filesystem or volume.
3578fa9e4066Sahrens */
3579fa9e4066Sahrens int
zfs_create(libzfs_handle_t * hdl,const char * path,zfs_type_t type,nvlist_t * props)358099653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3581e9dbad6fSeschrock nvlist_t *props)
3582fa9e4066Sahrens {
3583fa9e4066Sahrens int ret;
3584fa9e4066Sahrens uint64_t size = 0;
3585fa9e4066Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3586eb633035STom Caputi uint8_t *wkeydata = NULL;
3587eb633035STom Caputi uint_t wkeylen = 0;
358899653d4eSeschrock char errbuf[1024];
3589eb633035STom Caputi char parent[MAXNAMELEN];
3590e9dbad6fSeschrock uint64_t zoned;
359126455f9eSAndriy Gapon enum lzc_dataset_type ost;
3592690031d3Sloli zpool_handle_t *zpool_handle;
359399653d4eSeschrock
359499653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
359599653d4eSeschrock "cannot create '%s'"), path);
3596fa9e4066Sahrens
3597fa9e4066Sahrens /* validate the path, taking care to note the extended error message */
3598f18faf3fSek if (!zfs_validate_name(hdl, path, type, B_TRUE))
359999653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3600fa9e4066Sahrens
36015ac95da7SSerapheim Dimitropoulos if (dataset_nestcheck(path) != 0) {
36025ac95da7SSerapheim Dimitropoulos zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36035ac95da7SSerapheim Dimitropoulos "maximum name nesting depth exceeded"));
36045ac95da7SSerapheim Dimitropoulos return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
36055ac95da7SSerapheim Dimitropoulos }
36065ac95da7SSerapheim Dimitropoulos
3607fa9e4066Sahrens /* validate parents exist */
36087f1f55eaSvb if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3609fa9e4066Sahrens return (-1);
3610fa9e4066Sahrens
3611fa9e4066Sahrens /*
3612fa9e4066Sahrens * The failure modes when creating a dataset of a different type over
3613fa9e4066Sahrens * one that already exists is a little strange. In particular, if you
3614fa9e4066Sahrens * try to create a dataset on top of an existing dataset, the ioctl()
3615fa9e4066Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we
3616fa9e4066Sahrens * first try to see if the dataset exists.
3617fa9e4066Sahrens */
36184445fffbSMatthew Ahrens if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
361999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
362099653d4eSeschrock "dataset already exists"));
362199653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3622fa9e4066Sahrens }
3623fa9e4066Sahrens
3624fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME)
362526455f9eSAndriy Gapon ost = LZC_DATSET_TYPE_ZVOL;
3626fa9e4066Sahrens else
362726455f9eSAndriy Gapon ost = LZC_DATSET_TYPE_ZFS;
3628fa9e4066Sahrens
3629e9316f76SJoe Stein /* open zpool handle for prop validation */
36309adfa60dSMatthew Ahrens char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3631e9316f76SJoe Stein (void) strlcpy(pool_path, path, sizeof (pool_path));
3632e9316f76SJoe Stein
3633e9316f76SJoe Stein /* truncate pool_path at first slash */
3634e9316f76SJoe Stein char *p = strchr(pool_path, '/');
3635e9316f76SJoe Stein if (p != NULL)
3636e9316f76SJoe Stein *p = '\0';
3637e9316f76SJoe Stein
3638690031d3Sloli if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3639690031d3Sloli return (-1);
3640e9316f76SJoe Stein
36410a48a24eStimh if (props && (props = zfs_valid_proplist(hdl, type, props,
3642eb633035STom Caputi zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) {
3643e9316f76SJoe Stein zpool_close(zpool_handle);
3644e9dbad6fSeschrock return (-1);
3645e9316f76SJoe Stein }
3646e9316f76SJoe Stein zpool_close(zpool_handle);
3647e9dbad6fSeschrock
3648fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) {
36495c5460e9Seschrock /*
36505c5460e9Seschrock * If we are creating a volume, the size and block size must
36515c5460e9Seschrock * satisfy a few restraints. First, the blocksize must be a
36525c5460e9Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
36535c5460e9Seschrock * volsize must be a multiple of the block size, and cannot be
36545c5460e9Seschrock * zero.
36555c5460e9Seschrock */
3656e9dbad6fSeschrock if (props == NULL || nvlist_lookup_uint64(props,
3657e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3658e9dbad6fSeschrock nvlist_free(props);
365999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3660e9dbad6fSeschrock "missing volume size"));
3661e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3662e9dbad6fSeschrock }
3663e9dbad6fSeschrock
3664e9dbad6fSeschrock if ((ret = nvlist_lookup_uint64(props,
3665e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3666e9dbad6fSeschrock &blocksize)) != 0) {
3667e9dbad6fSeschrock if (ret == ENOENT) {
3668e9dbad6fSeschrock blocksize = zfs_prop_default_numeric(
3669e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE);
3670e9dbad6fSeschrock } else {
3671e9dbad6fSeschrock nvlist_free(props);
3672e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3673e9dbad6fSeschrock "missing volume block size"));
3674e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3675e9dbad6fSeschrock }
3676fa9e4066Sahrens }
3677fa9e4066Sahrens
3678e9dbad6fSeschrock if (size == 0) {
3679e9dbad6fSeschrock nvlist_free(props);
368099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3681e9dbad6fSeschrock "volume size cannot be zero"));
3682e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf));
36835c5460e9Seschrock }
36845c5460e9Seschrock
36855c5460e9Seschrock if (size % blocksize != 0) {
3686e9dbad6fSeschrock nvlist_free(props);
368799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3688e9dbad6fSeschrock "volume size must be a multiple of volume block "
3689e9dbad6fSeschrock "size"));
3690e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf));
36915c5460e9Seschrock }
3692fa9e4066Sahrens }
3693fa9e4066Sahrens
3694eb633035STom Caputi (void) parent_name(path, parent, sizeof (parent));
36956ccda740Sloli if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE,
36966ccda740Sloli &wkeydata, &wkeylen) != 0) {
3697eb633035STom Caputi nvlist_free(props);
3698eb633035STom Caputi return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3699eb633035STom Caputi }
3700eb633035STom Caputi
3701fa9e4066Sahrens /* create the dataset */
3702eb633035STom Caputi ret = lzc_create(path, ost, props, wkeydata, wkeylen);
37034445fffbSMatthew Ahrens nvlist_free(props);
3704eb633035STom Caputi if (wkeydata != NULL)
3705eb633035STom Caputi free(wkeydata);
3706e9dbad6fSeschrock
3707fa9e4066Sahrens /* check for failure */
3708fa9e4066Sahrens if (ret != 0) {
3709fa9e4066Sahrens switch (errno) {
3710fa9e4066Sahrens case ENOENT:
371199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
371299653d4eSeschrock "no such parent '%s'"), parent);
371399653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf));
3714fa9e4066Sahrens
3715fa9e4066Sahrens case EINVAL:
371699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3717d7d4af51Smmusante "parent '%s' is not a filesystem"), parent);
371899653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3719fa9e4066Sahrens
372040feaa91Sahrens case ENOTSUP:
372140feaa91Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
372240feaa91Sahrens "pool must be upgraded to set this "
372340feaa91Sahrens "property or value"));
372440feaa91Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
37259fa2266dSYuri Pankov case ERANGE:
37269fa2266dSYuri Pankov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37279fa2266dSYuri Pankov "invalid property value(s) specified"));
37289fa2266dSYuri Pankov return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3729eb633035STom Caputi case EACCES:
3730eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3731eb633035STom Caputi "encryption root's key is not loaded "
3732eb633035STom Caputi "or provided"));
3733eb633035STom Caputi return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3734eb633035STom Caputi
3735fa9e4066Sahrens #ifdef _ILP32
3736fa9e4066Sahrens case EOVERFLOW:
3737fa9e4066Sahrens /*
3738fa9e4066Sahrens * This platform can't address a volume this big.
3739fa9e4066Sahrens */
374099653d4eSeschrock if (type == ZFS_TYPE_VOLUME)
374199653d4eSeschrock return (zfs_error(hdl, EZFS_VOLTOOBIG,
374299653d4eSeschrock errbuf));
3743fa9e4066Sahrens #endif
374499653d4eSeschrock /* FALLTHROUGH */
3745fa9e4066Sahrens default:
374699653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf));
3747fa9e4066Sahrens }
3748fa9e4066Sahrens }
3749fa9e4066Sahrens
3750fa9e4066Sahrens return (0);
3751fa9e4066Sahrens }
3752fa9e4066Sahrens
3753fa9e4066Sahrens /*
3754fa9e4066Sahrens * Destroys the given dataset. The caller must make sure that the filesystem
375565fec9f6SChristopher Siden * isn't mounted, and that there are no active dependents. If the file system
375665fec9f6SChristopher Siden * does not exist this function does nothing.
3757fa9e4066Sahrens */
3758fa9e4066Sahrens int
zfs_destroy(zfs_handle_t * zhp,boolean_t defer)3759842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3760fa9e4066Sahrens {
3761049ba636SAndriy Gapon int error;
3762049ba636SAndriy Gapon
3763049ba636SAndriy Gapon if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3764049ba636SAndriy Gapon return (EINVAL);
3765fa9e4066Sahrens
376678f17100SMatthew Ahrens if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
376778f17100SMatthew Ahrens nvlist_t *nv = fnvlist_alloc();
376878f17100SMatthew Ahrens fnvlist_add_boolean(nv, zhp->zfs_name);
3769049ba636SAndriy Gapon error = lzc_destroy_bookmarks(nv, NULL);
377078f17100SMatthew Ahrens fnvlist_free(nv);
377178f17100SMatthew Ahrens if (error != 0) {
3772049ba636SAndriy Gapon return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
377378f17100SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
377478f17100SMatthew Ahrens zhp->zfs_name));
377578f17100SMatthew Ahrens }
377678f17100SMatthew Ahrens return (0);
377778f17100SMatthew Ahrens }
377878f17100SMatthew Ahrens
3779049ba636SAndriy Gapon if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3780049ba636SAndriy Gapon nvlist_t *nv = fnvlist_alloc();
3781049ba636SAndriy Gapon fnvlist_add_boolean(nv, zhp->zfs_name);
3782049ba636SAndriy Gapon error = lzc_destroy_snaps(nv, defer, NULL);
3783049ba636SAndriy Gapon fnvlist_free(nv);
3784fa9e4066Sahrens } else {
3785049ba636SAndriy Gapon error = lzc_destroy(zhp->zfs_name);
3786fa9e4066Sahrens }
3787fa9e4066Sahrens
3788049ba636SAndriy Gapon if (error != 0 && error != ENOENT) {
3789ece3d9b3Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
379099653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
379199653d4eSeschrock zhp->zfs_name));
37921d452cf5Sahrens }
3793fa9e4066Sahrens
3794fa9e4066Sahrens remove_mountpoint(zhp);
3795fa9e4066Sahrens
3796fa9e4066Sahrens return (0);
3797fa9e4066Sahrens }
3798fa9e4066Sahrens
37991d452cf5Sahrens struct destroydata {
380019b94df9SMatthew Ahrens nvlist_t *nvl;
380119b94df9SMatthew Ahrens const char *snapname;
38021d452cf5Sahrens };
38031d452cf5Sahrens
38041d452cf5Sahrens static int
zfs_check_snap_cb(zfs_handle_t * zhp,void * arg)3805681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
38061d452cf5Sahrens {
38071d452cf5Sahrens struct destroydata *dd = arg;
38089adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
3809681d9761SEric Taylor int rv = 0;
38101d452cf5Sahrens
381119b94df9SMatthew Ahrens (void) snprintf(name, sizeof (name),
381219b94df9SMatthew Ahrens "%s@%s", zhp->zfs_name, dd->snapname);
38131d452cf5Sahrens
3814a7a845e4SSteven Hartland if (lzc_exists(name))
381519b94df9SMatthew Ahrens verify(nvlist_add_boolean(dd->nvl, name) == 0);
38161d452cf5Sahrens
381719b94df9SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
381819b94df9SMatthew Ahrens zfs_close(zhp);
38193ccfa83cSahrens return (rv);
38201d452cf5Sahrens }
38211d452cf5Sahrens
38221d452cf5Sahrens /*
38231d452cf5Sahrens * Destroys all snapshots with the given name in zhp & descendants.
38241d452cf5Sahrens */
38251d452cf5Sahrens int
zfs_destroy_snaps(zfs_handle_t * zhp,char * snapname,boolean_t defer)3826842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
38271d452cf5Sahrens {
38281d452cf5Sahrens int ret;
38291d452cf5Sahrens struct destroydata dd = { 0 };
38301d452cf5Sahrens
38311d452cf5Sahrens dd.snapname = snapname;
383219b94df9SMatthew Ahrens verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
383319b94df9SMatthew Ahrens (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
38341d452cf5Sahrens
3835a7a845e4SSteven Hartland if (nvlist_empty(dd.nvl)) {
383619b94df9SMatthew Ahrens ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
38371d452cf5Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
383819b94df9SMatthew Ahrens zhp->zfs_name, snapname);
383919b94df9SMatthew Ahrens } else {
38403b2aab18SMatthew Ahrens ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
38411d452cf5Sahrens }
384219b94df9SMatthew Ahrens nvlist_free(dd.nvl);
384319b94df9SMatthew Ahrens return (ret);
384419b94df9SMatthew Ahrens }
384519b94df9SMatthew Ahrens
384619b94df9SMatthew Ahrens /*
38473b2aab18SMatthew Ahrens * Destroys all the snapshots named in the nvlist.
384819b94df9SMatthew Ahrens */
384919b94df9SMatthew Ahrens int
zfs_destroy_snaps_nvl(libzfs_handle_t * hdl,nvlist_t * snaps,boolean_t defer)38503b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
385119b94df9SMatthew Ahrens {
385219b94df9SMatthew Ahrens int ret;
38534cde22c2SChris Williamson nvlist_t *errlist = NULL;
38541d452cf5Sahrens
38554445fffbSMatthew Ahrens ret = lzc_destroy_snaps(snaps, defer, &errlist);
38561d452cf5Sahrens
38574cde22c2SChris Williamson if (ret == 0) {
38584cde22c2SChris Williamson nvlist_free(errlist);
38593b2aab18SMatthew Ahrens return (0);
38604cde22c2SChris Williamson }
38614445fffbSMatthew Ahrens
3862a7a845e4SSteven Hartland if (nvlist_empty(errlist)) {
38633b2aab18SMatthew Ahrens char errbuf[1024];
38643b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf),
38653b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
38663b2aab18SMatthew Ahrens
38673b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, ret, errbuf);
38683b2aab18SMatthew Ahrens }
38693b2aab18SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
38703b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
38713b2aab18SMatthew Ahrens char errbuf[1024];
38723b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf),
38733b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
38743b2aab18SMatthew Ahrens nvpair_name(pair));
38753b2aab18SMatthew Ahrens
38763b2aab18SMatthew Ahrens switch (fnvpair_value_int32(pair)) {
38773b2aab18SMatthew Ahrens case EEXIST:
38783b2aab18SMatthew Ahrens zfs_error_aux(hdl,
38793b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "snapshot is cloned"));
38803b2aab18SMatthew Ahrens ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
38813b2aab18SMatthew Ahrens break;
38823b2aab18SMatthew Ahrens default:
38833b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, errno, errbuf);
38843b2aab18SMatthew Ahrens break;
38851d452cf5Sahrens }
38861d452cf5Sahrens }
38871d452cf5Sahrens
38884cde22c2SChris Williamson nvlist_free(errlist);
38894445fffbSMatthew Ahrens return (ret);
38901d452cf5Sahrens }
38911d452cf5Sahrens
3892fa9e4066Sahrens /*
3893fa9e4066Sahrens * Clones the given dataset. The target must be of the same type as the source.
3894fa9e4066Sahrens */
3895fa9e4066Sahrens int
zfs_clone(zfs_handle_t * zhp,const char * target,nvlist_t * props)3896e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3897fa9e4066Sahrens {
38989adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN];
3899fa9e4066Sahrens int ret;
390099653d4eSeschrock char errbuf[1024];
390199653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl;
3902e9dbad6fSeschrock uint64_t zoned;
3903fa9e4066Sahrens
3904fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3905fa9e4066Sahrens
390699653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
390799653d4eSeschrock "cannot create '%s'"), target);
390899653d4eSeschrock
390919b94df9SMatthew Ahrens /* validate the target/clone name */
3910f18faf3fSek if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
391199653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3912fa9e4066Sahrens
3913fa9e4066Sahrens /* validate parents exist */
39147f1f55eaSvb if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3915fa9e4066Sahrens return (-1);
3916fa9e4066Sahrens
3917fa9e4066Sahrens (void) parent_name(target, parent, sizeof (parent));
3918fa9e4066Sahrens
3919fa9e4066Sahrens /* do the clone */
3920e9dbad6fSeschrock
3921e9dbad6fSeschrock if (props) {
39224445fffbSMatthew Ahrens zfs_type_t type;
39231c10ae76SMike Gerdts
39244445fffbSMatthew Ahrens if (ZFS_IS_VOLUME(zhp)) {
39254445fffbSMatthew Ahrens type = ZFS_TYPE_VOLUME;
39264445fffbSMatthew Ahrens } else {
39274445fffbSMatthew Ahrens type = ZFS_TYPE_FILESYSTEM;
39284445fffbSMatthew Ahrens }
39290a48a24eStimh if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3930eb633035STom Caputi zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL)
3931e9dbad6fSeschrock return (-1);
39321c10ae76SMike Gerdts if (zfs_fix_auto_resv(zhp, props) == -1) {
39331c10ae76SMike Gerdts nvlist_free(props);
39341c10ae76SMike Gerdts return (-1);
39351c10ae76SMike Gerdts }
3936e9dbad6fSeschrock }
3937fa9e4066Sahrens
3938eb633035STom Caputi if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) {
3939eb633035STom Caputi nvlist_free(props);
3940eb633035STom Caputi return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3941eb633035STom Caputi }
3942eb633035STom Caputi
39434445fffbSMatthew Ahrens ret = lzc_clone(target, zhp->zfs_name, props);
39444445fffbSMatthew Ahrens nvlist_free(props);
3945e9dbad6fSeschrock
3946fa9e4066Sahrens if (ret != 0) {
3947fa9e4066Sahrens switch (errno) {
3948fa9e4066Sahrens
3949fa9e4066Sahrens case ENOENT:
3950fa9e4066Sahrens /*
3951fa9e4066Sahrens * The parent doesn't exist. We should have caught this
3952fa9e4066Sahrens * above, but there may a race condition that has since
3953fa9e4066Sahrens * destroyed the parent.
3954fa9e4066Sahrens *
3955fa9e4066Sahrens * At this point, we don't know whether it's the source
3956fa9e4066Sahrens * that doesn't exist anymore, or whether the target
3957fa9e4066Sahrens * dataset doesn't exist.
3958fa9e4066Sahrens */
395999653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
396099653d4eSeschrock "no such parent '%s'"), parent);
396199653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3962fa9e4066Sahrens
396399653d4eSeschrock case EXDEV:
396499653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
396599653d4eSeschrock "source and target pools differ"));
396699653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
396799653d4eSeschrock errbuf));
3968fa9e4066Sahrens
396999653d4eSeschrock default:
397099653d4eSeschrock return (zfs_standard_error(zhp->zfs_hdl, errno,
397199653d4eSeschrock errbuf));
397299653d4eSeschrock }
397399653d4eSeschrock }
3974fa9e4066Sahrens
397599653d4eSeschrock return (ret);
397699653d4eSeschrock }
397799653d4eSeschrock
397899653d4eSeschrock /*
397999653d4eSeschrock * Promotes the given clone fs to be the clone parent.
398099653d4eSeschrock */
398199653d4eSeschrock int
zfs_promote(zfs_handle_t * zhp)398299653d4eSeschrock zfs_promote(zfs_handle_t *zhp)
398399653d4eSeschrock {
398499653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl;
3985a4b8c9aaSAndrew Stormont char snapname[ZFS_MAX_DATASET_NAME_LEN];
398699653d4eSeschrock int ret;
398799653d4eSeschrock char errbuf[1024];
398899653d4eSeschrock
398999653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
399099653d4eSeschrock "cannot promote '%s'"), zhp->zfs_name);
399199653d4eSeschrock
399299653d4eSeschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
399399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
399499653d4eSeschrock "snapshots can not be promoted"));
399599653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
399699653d4eSeschrock }
399799653d4eSeschrock
3998a4b8c9aaSAndrew Stormont if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
399999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
400099653d4eSeschrock "not a cloned filesystem"));
400199653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
400299653d4eSeschrock }
400399653d4eSeschrock
4004add927f8Sloli if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4005add927f8Sloli return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4006add927f8Sloli
4007a4b8c9aaSAndrew Stormont ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
400899653d4eSeschrock
400999653d4eSeschrock if (ret != 0) {
4010a4b8c9aaSAndrew Stormont switch (ret) {
401199653d4eSeschrock case EEXIST:
4012681d9761SEric Taylor /* There is a conflicting snapshot name. */
401399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4014681d9761SEric Taylor "conflicting snapshot '%s' from parent '%s'"),
4015a4b8c9aaSAndrew Stormont snapname, zhp->zfs_dmustats.dds_origin);
401699653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf));
4017fa9e4066Sahrens
4018fa9e4066Sahrens default:
4019a4b8c9aaSAndrew Stormont return (zfs_standard_error(hdl, ret, errbuf));
4020fa9e4066Sahrens }
4021fa9e4066Sahrens }
4022e9dbad6fSeschrock return (ret);
40231d452cf5Sahrens }
40241d452cf5Sahrens
40254445fffbSMatthew Ahrens typedef struct snapdata {
40264445fffbSMatthew Ahrens nvlist_t *sd_nvl;
40274445fffbSMatthew Ahrens const char *sd_snapname;
40284445fffbSMatthew Ahrens } snapdata_t;
40294445fffbSMatthew Ahrens
40304445fffbSMatthew Ahrens static int
zfs_snapshot_cb(zfs_handle_t * zhp,void * arg)40314445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
40324445fffbSMatthew Ahrens {
40334445fffbSMatthew Ahrens snapdata_t *sd = arg;
40349adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
40354445fffbSMatthew Ahrens int rv = 0;
40364445fffbSMatthew Ahrens
4037ca48f36fSKeith M Wesolowski if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
4038ca48f36fSKeith M Wesolowski (void) snprintf(name, sizeof (name),
4039ca48f36fSKeith M Wesolowski "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
40404445fffbSMatthew Ahrens
4041ca48f36fSKeith M Wesolowski fnvlist_add_boolean(sd->sd_nvl, name);
40424445fffbSMatthew Ahrens
4043ca48f36fSKeith M Wesolowski rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
4044ca48f36fSKeith M Wesolowski }
40454445fffbSMatthew Ahrens zfs_close(zhp);
4046ca48f36fSKeith M Wesolowski
40474445fffbSMatthew Ahrens return (rv);
40484445fffbSMatthew Ahrens }
40494445fffbSMatthew Ahrens
40505cabbc6bSPrashanth Sreenivasa int
zfs_remap_indirects(libzfs_handle_t * hdl,const char * fs)40515cabbc6bSPrashanth Sreenivasa zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
40525cabbc6bSPrashanth Sreenivasa {
40535cabbc6bSPrashanth Sreenivasa int err;
40545cabbc6bSPrashanth Sreenivasa char errbuf[1024];
40555cabbc6bSPrashanth Sreenivasa
40565cabbc6bSPrashanth Sreenivasa (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
40570b2e8253Sloli "cannot remap dataset '%s'"), fs);
40585cabbc6bSPrashanth Sreenivasa
40595cabbc6bSPrashanth Sreenivasa err = lzc_remap(fs);
40605cabbc6bSPrashanth Sreenivasa
40615cabbc6bSPrashanth Sreenivasa if (err != 0) {
40620b2e8253Sloli switch (err) {
40630b2e8253Sloli case ENOTSUP:
40640b2e8253Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40650b2e8253Sloli "pool must be upgraded"));
40660b2e8253Sloli (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
40670b2e8253Sloli break;
40680b2e8253Sloli case EINVAL:
40690b2e8253Sloli (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
40700b2e8253Sloli break;
40710b2e8253Sloli default:
40720b2e8253Sloli (void) zfs_standard_error(hdl, err, errbuf);
40730b2e8253Sloli break;
40740b2e8253Sloli }
40755cabbc6bSPrashanth Sreenivasa }
40765cabbc6bSPrashanth Sreenivasa
40775cabbc6bSPrashanth Sreenivasa return (err);
40785cabbc6bSPrashanth Sreenivasa }
40795cabbc6bSPrashanth Sreenivasa
4080fa9e4066Sahrens /*
40814445fffbSMatthew Ahrens * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
40824445fffbSMatthew Ahrens * created.
4083fa9e4066Sahrens */
4084fa9e4066Sahrens int
zfs_snapshot_nvl(libzfs_handle_t * hdl,nvlist_t * snaps,nvlist_t * props)40854445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
4086fa9e4066Sahrens {
4087fa9e4066Sahrens int ret;
408899653d4eSeschrock char errbuf[1024];
40894445fffbSMatthew Ahrens nvpair_t *elem;
40904445fffbSMatthew Ahrens nvlist_t *errors;
4091fa9e4066Sahrens
409299653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
40934445fffbSMatthew Ahrens "cannot create snapshots "));
409499653d4eSeschrock
40954445fffbSMatthew Ahrens elem = NULL;
40964445fffbSMatthew Ahrens while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
40974445fffbSMatthew Ahrens const char *snapname = nvpair_name(elem);
40984445fffbSMatthew Ahrens
40994445fffbSMatthew Ahrens /* validate the target name */
41004445fffbSMatthew Ahrens if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
41014445fffbSMatthew Ahrens B_TRUE)) {
41024445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf),
41034445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN,
41044445fffbSMatthew Ahrens "cannot create snapshot '%s'"), snapname);
41054445fffbSMatthew Ahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
41064445fffbSMatthew Ahrens }
41074445fffbSMatthew Ahrens }
4108fa9e4066Sahrens
4109e9316f76SJoe Stein /*
4110e9316f76SJoe Stein * get pool handle for prop validation. assumes all snaps are in the
4111e9316f76SJoe Stein * same pool, as does lzc_snapshot (below).
4112e9316f76SJoe Stein */
41139adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
4114e9316f76SJoe Stein elem = nvlist_next_nvpair(snaps, NULL);
4115e9316f76SJoe Stein (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4116e9316f76SJoe Stein pool[strcspn(pool, "/@")] = '\0';
4117e9316f76SJoe Stein zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
4118e9316f76SJoe Stein
41194445fffbSMatthew Ahrens if (props != NULL &&
41204445fffbSMatthew Ahrens (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4121eb633035STom Caputi props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4122e9316f76SJoe Stein zpool_close(zpool_hdl);
41234445fffbSMatthew Ahrens return (-1);
41244445fffbSMatthew Ahrens }
4125e9316f76SJoe Stein zpool_close(zpool_hdl);
4126bb0ade09Sahrens
41274445fffbSMatthew Ahrens ret = lzc_snapshot(snaps, props, &errors);
41284445fffbSMatthew Ahrens
41294445fffbSMatthew Ahrens if (ret != 0) {
41304445fffbSMatthew Ahrens boolean_t printed = B_FALSE;
41314445fffbSMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL);
41324445fffbSMatthew Ahrens elem != NULL;
41334445fffbSMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) {
41344445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf),
41354445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN,
41364445fffbSMatthew Ahrens "cannot create snapshot '%s'"), nvpair_name(elem));
41374445fffbSMatthew Ahrens (void) zfs_standard_error(hdl,
41384445fffbSMatthew Ahrens fnvpair_value_int32(elem), errbuf);
41394445fffbSMatthew Ahrens printed = B_TRUE;
4140bb0ade09Sahrens }
41414445fffbSMatthew Ahrens if (!printed) {
41424445fffbSMatthew Ahrens switch (ret) {
41434445fffbSMatthew Ahrens case EXDEV:
41444445fffbSMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41454445fffbSMatthew Ahrens "multiple snapshots of same "
41464445fffbSMatthew Ahrens "fs not allowed"));
41474445fffbSMatthew Ahrens (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4148bb0ade09Sahrens
41494445fffbSMatthew Ahrens break;
41504445fffbSMatthew Ahrens default:
41514445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf);
41524445fffbSMatthew Ahrens }
41534445fffbSMatthew Ahrens }
4154bb0ade09Sahrens }
4155bb0ade09Sahrens
41564445fffbSMatthew Ahrens nvlist_free(props);
41574445fffbSMatthew Ahrens nvlist_free(errors);
41584445fffbSMatthew Ahrens return (ret);
41594445fffbSMatthew Ahrens }
41604445fffbSMatthew Ahrens
41614445fffbSMatthew Ahrens int
zfs_snapshot(libzfs_handle_t * hdl,const char * path,boolean_t recursive,nvlist_t * props)41624445fffbSMatthew Ahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
41634445fffbSMatthew Ahrens nvlist_t *props)
41644445fffbSMatthew Ahrens {
41654445fffbSMatthew Ahrens int ret;
41664445fffbSMatthew Ahrens snapdata_t sd = { 0 };
41679adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN];
41684445fffbSMatthew Ahrens char *cp;
41694445fffbSMatthew Ahrens zfs_handle_t *zhp;
41704445fffbSMatthew Ahrens char errbuf[1024];
41714445fffbSMatthew Ahrens
41724445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
41734445fffbSMatthew Ahrens "cannot snapshot %s"), path);
41744445fffbSMatthew Ahrens
41754445fffbSMatthew Ahrens if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
41764445fffbSMatthew Ahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
41774445fffbSMatthew Ahrens
41784445fffbSMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname));
41794445fffbSMatthew Ahrens cp = strchr(fsname, '@');
41804445fffbSMatthew Ahrens *cp = '\0';
41814445fffbSMatthew Ahrens sd.sd_snapname = cp + 1;
4182fa9e4066Sahrens
41834445fffbSMatthew Ahrens if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4184fa9e4066Sahrens ZFS_TYPE_VOLUME)) == NULL) {
4185fa9e4066Sahrens return (-1);
4186fa9e4066Sahrens }
4187fa9e4066Sahrens
41884445fffbSMatthew Ahrens verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
41894445fffbSMatthew Ahrens if (recursive) {
41904445fffbSMatthew Ahrens (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
41914445fffbSMatthew Ahrens } else {
41924445fffbSMatthew Ahrens fnvlist_add_boolean(sd.sd_nvl, path);
4193681d9761SEric Taylor }
4194fa9e4066Sahrens
41954445fffbSMatthew Ahrens ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
41964445fffbSMatthew Ahrens nvlist_free(sd.sd_nvl);
4197fa9e4066Sahrens zfs_close(zhp);
4198fa9e4066Sahrens return (ret);
4199fa9e4066Sahrens }
4200fa9e4066Sahrens
4201fa9e4066Sahrens /*
4202b12a1c38Slling * Destroy any more recent snapshots. We invoke this callback on any dependents
4203b12a1c38Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4204b12a1c38Slling * is a dependent and we should just destroy it without checking the transaction
4205b12a1c38Slling * group.
4206fa9e4066Sahrens */
4207b12a1c38Slling typedef struct rollback_data {
4208b12a1c38Slling const char *cb_target; /* the snapshot */
4209b12a1c38Slling uint64_t cb_create; /* creation time reference */
4210c391e322Sahrens boolean_t cb_error;
4211c391e322Sahrens boolean_t cb_force;
4212b12a1c38Slling } rollback_data_t;
4213b12a1c38Slling
4214b12a1c38Slling static int
rollback_destroy_dependent(zfs_handle_t * zhp,void * data)421578f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4216b12a1c38Slling {
4217b12a1c38Slling rollback_data_t *cbp = data;
421878f17100SMatthew Ahrens prop_changelist_t *clp;
421978f17100SMatthew Ahrens
422078f17100SMatthew Ahrens /* We must destroy this clone; first unmount it */
422178f17100SMatthew Ahrens clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
422278f17100SMatthew Ahrens cbp->cb_force ? MS_FORCE: 0);
422378f17100SMatthew Ahrens if (clp == NULL || changelist_prefix(clp) != 0) {
422478f17100SMatthew Ahrens cbp->cb_error = B_TRUE;
422578f17100SMatthew Ahrens zfs_close(zhp);
422678f17100SMatthew Ahrens return (0);
422778f17100SMatthew Ahrens }
422878f17100SMatthew Ahrens if (zfs_destroy(zhp, B_FALSE) != 0)
422978f17100SMatthew Ahrens cbp->cb_error = B_TRUE;
423078f17100SMatthew Ahrens else
423178f17100SMatthew Ahrens changelist_remove(clp, zhp->zfs_name);
423278f17100SMatthew Ahrens (void) changelist_postfix(clp);
423378f17100SMatthew Ahrens changelist_free(clp);
4234b12a1c38Slling
423578f17100SMatthew Ahrens zfs_close(zhp);
423678f17100SMatthew Ahrens return (0);
423778f17100SMatthew Ahrens }
4238b12a1c38Slling
423978f17100SMatthew Ahrens static int
rollback_destroy(zfs_handle_t * zhp,void * data)424078f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data)
424178f17100SMatthew Ahrens {
424278f17100SMatthew Ahrens rollback_data_t *cbp = data;
4243b12a1c38Slling
424478f17100SMatthew Ahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
424578f17100SMatthew Ahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
424678f17100SMatthew Ahrens rollback_destroy_dependent, cbp);
4247c391e322Sahrens
424878f17100SMatthew Ahrens cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4249b12a1c38Slling }
4250b12a1c38Slling
4251b12a1c38Slling zfs_close(zhp);
4252b12a1c38Slling return (0);
4253b12a1c38Slling }
4254b12a1c38Slling
4255b12a1c38Slling /*
4256b12a1c38Slling * Given a dataset, rollback to a specific snapshot, discarding any
4257b12a1c38Slling * data changes since then and making it the active dataset.
4258b12a1c38Slling *
425978f17100SMatthew Ahrens * Any snapshots and bookmarks more recent than the target are
426078f17100SMatthew Ahrens * destroyed, along with their dependents (i.e. clones).
4261b12a1c38Slling */
4262b12a1c38Slling int
zfs_rollback(zfs_handle_t * zhp,zfs_handle_t * snap,boolean_t force)4263c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4264b12a1c38Slling {
4265b12a1c38Slling rollback_data_t cb = { 0 };
42664ccbb6e7Sahrens int err;
42677b97dc1aSrm boolean_t restore_resv = 0;
4268f83b46baSPaul Dagnelie uint64_t old_volsize = 0, new_volsize;
42697b97dc1aSrm zfs_prop_t resv_prop;
4270b12a1c38Slling
42714ccbb6e7Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
42724ccbb6e7Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME);
4273b12a1c38Slling
4274b12a1c38Slling /*
42752e2c1355SMatthew Ahrens * Destroy all recent snapshots and their dependents.
4276b12a1c38Slling */
4277c391e322Sahrens cb.cb_force = force;
4278b12a1c38Slling cb.cb_target = snap->zfs_name;
4279b12a1c38Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
42800d8fa8f8SMartin Matuska (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
428178f17100SMatthew Ahrens (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4282b12a1c38Slling
4283c391e322Sahrens if (cb.cb_error)
4284c391e322Sahrens return (-1);
4285b12a1c38Slling
4286b12a1c38Slling /*
4287b12a1c38Slling * Now that we have verified that the snapshot is the latest,
4288b12a1c38Slling * rollback to the given snapshot.
4289b12a1c38Slling */
4290b12a1c38Slling
42917b97dc1aSrm if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
42927b97dc1aSrm if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
42937b97dc1aSrm return (-1);
42947b97dc1aSrm old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
42957b97dc1aSrm restore_resv =
42967b97dc1aSrm (old_volsize == zfs_prop_get_int(zhp, resv_prop));
42977b97dc1aSrm }
42984ccbb6e7Sahrens
4299b12a1c38Slling /*
430077b17137SAndriy Gapon * Pass both the filesystem and the wanted snapshot names,
430177b17137SAndriy Gapon * we would get an error back if the snapshot is destroyed or
430277b17137SAndriy Gapon * a new snapshot is created before this request is processed.
4303b12a1c38Slling */
430477b17137SAndriy Gapon err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
430595643f75SAndriy Gapon if (err != 0) {
430695643f75SAndriy Gapon char errbuf[1024];
430795643f75SAndriy Gapon
430895643f75SAndriy Gapon (void) snprintf(errbuf, sizeof (errbuf),
43094ccbb6e7Sahrens dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
43104ccbb6e7Sahrens zhp->zfs_name);
431195643f75SAndriy Gapon switch (err) {
431295643f75SAndriy Gapon case EEXIST:
431395643f75SAndriy Gapon zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
431495643f75SAndriy Gapon "there is a snapshot or bookmark more recent "
431595643f75SAndriy Gapon "than '%s'"), snap->zfs_name);
431695643f75SAndriy Gapon (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
431795643f75SAndriy Gapon break;
431895643f75SAndriy Gapon case ESRCH:
431995643f75SAndriy Gapon zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
432095643f75SAndriy Gapon "'%s' is not found among snapshots of '%s'"),
432195643f75SAndriy Gapon snap->zfs_name, zhp->zfs_name);
432295643f75SAndriy Gapon (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
432395643f75SAndriy Gapon break;
432495643f75SAndriy Gapon case EINVAL:
432595643f75SAndriy Gapon (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
432695643f75SAndriy Gapon break;
432795643f75SAndriy Gapon default:
432895643f75SAndriy Gapon (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
432995643f75SAndriy Gapon }
4330b9415e83Srm return (err);
4331b9415e83Srm }
43327b97dc1aSrm
43337b97dc1aSrm /*
43347b97dc1aSrm * For volumes, if the pre-rollback volsize matched the pre-
43357b97dc1aSrm * rollback reservation and the volsize has changed then set
43367b97dc1aSrm * the reservation property to the post-rollback volsize.
43377b97dc1aSrm * Make a new handle since the rollback closed the dataset.
43387b97dc1aSrm */
4339b9415e83Srm if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4340b9415e83Srm (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
43417b97dc1aSrm if (restore_resv) {
43427b97dc1aSrm new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
43437b97dc1aSrm if (old_volsize != new_volsize)
4344b9415e83Srm err = zfs_prop_set_int(zhp, resv_prop,
4345b9415e83Srm new_volsize);
43467b97dc1aSrm }
43477b97dc1aSrm zfs_close(zhp);
43484ccbb6e7Sahrens }
43494ccbb6e7Sahrens return (err);
4350b12a1c38Slling }
4351b12a1c38Slling
4352fa9e4066Sahrens /*
4353fa9e4066Sahrens * Renames the given dataset.
4354fa9e4066Sahrens */
4355fa9e4066Sahrens int
zfs_rename(zfs_handle_t * zhp,const char * target,boolean_t recursive,boolean_t force_unmount)43566a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
43576a9cb0eaSEric Schrock boolean_t force_unmount)
4358fa9e4066Sahrens {
435988f61deeSIgor Kozhukhov int ret = 0;
4360fa9e4066Sahrens zfs_cmd_t zc = { 0 };
4361fa9e4066Sahrens char *delim;
4362cdf5b4caSmmusante prop_changelist_t *cl = NULL;
4363cdf5b4caSmmusante zfs_handle_t *zhrp = NULL;
4364cdf5b4caSmmusante char *parentname = NULL;
43659adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN];
436699653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl;
436799653d4eSeschrock char errbuf[1024];
4368fa9e4066Sahrens
4369fa9e4066Sahrens /* if we have the same exact name, just return success */
4370fa9e4066Sahrens if (strcmp(zhp->zfs_name, target) == 0)
4371fa9e4066Sahrens return (0);
4372fa9e4066Sahrens
437399653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
437499653d4eSeschrock "cannot rename to '%s'"), target);
437599653d4eSeschrock
4376add927f8Sloli /* make sure source name is valid */
4377add927f8Sloli if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4378add927f8Sloli return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4379add927f8Sloli
4380fa9e4066Sahrens /*
4381fa9e4066Sahrens * Make sure the target name is valid
4382fa9e4066Sahrens */
4383fa9e4066Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
438498579b20Snd if ((strchr(target, '@') == NULL) ||
438598579b20Snd *target == '@') {
438698579b20Snd /*
438798579b20Snd * Snapshot target name is abbreviated,
438898579b20Snd * reconstruct full dataset name
438998579b20Snd */
439098579b20Snd (void) strlcpy(parent, zhp->zfs_name,
439198579b20Snd sizeof (parent));
439298579b20Snd delim = strchr(parent, '@');
439398579b20Snd if (strchr(target, '@') == NULL)
439498579b20Snd *(++delim) = '\0';
439598579b20Snd else
439698579b20Snd *delim = '\0';
439798579b20Snd (void) strlcat(parent, target, sizeof (parent));
439898579b20Snd target = parent;
439998579b20Snd } else {
440098579b20Snd /*
440198579b20Snd * Make sure we're renaming within the same dataset.
440298579b20Snd */
440398579b20Snd delim = strchr(target, '@');
440498579b20Snd if (strncmp(zhp->zfs_name, target, delim - target)
440598579b20Snd != 0 || zhp->zfs_name[delim - target] != '@') {
440698579b20Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
440798579b20Snd "snapshots must be part of same "
440898579b20Snd "dataset"));
440998579b20Snd return (zfs_error(hdl, EZFS_CROSSTARGET,
4410b1b8ab34Slling errbuf));
441198579b20Snd }
4412fa9e4066Sahrens }
44135ac95da7SSerapheim Dimitropoulos
4414f18faf3fSek if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
441598579b20Snd return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4416fa9e4066Sahrens } else {
4417cdf5b4caSmmusante if (recursive) {
4418cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4419cdf5b4caSmmusante "recursive rename must be a snapshot"));
4420cdf5b4caSmmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4421cdf5b4caSmmusante }
4422cdf5b4caSmmusante
4423f18faf3fSek if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
442498579b20Snd return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4425e9dbad6fSeschrock
4426fa9e4066Sahrens /* validate parents */
4427d41c4376SMark J Musante if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4428fa9e4066Sahrens return (-1);
4429fa9e4066Sahrens
4430fa9e4066Sahrens /* make sure we're in the same pool */
4431fa9e4066Sahrens verify((delim = strchr(target, '/')) != NULL);
4432fa9e4066Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4433fa9e4066Sahrens zhp->zfs_name[delim - target] != '/') {
443499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
443599653d4eSeschrock "datasets must be within same pool"));
443699653d4eSeschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4437fa9e4066Sahrens }
4438f2fdf992Snd
4439f2fdf992Snd /* new name cannot be a child of the current dataset name */
4440d41c4376SMark J Musante if (is_descendant(zhp->zfs_name, target)) {
4441f2fdf992Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4442d41c4376SMark J Musante "New dataset name cannot be a descendant of "
4443f2fdf992Snd "current dataset name"));
4444f2fdf992Snd return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4445f2fdf992Snd }
4446fa9e4066Sahrens }
4447fa9e4066Sahrens
444899653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf),
444999653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
445099653d4eSeschrock
4451fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID &&
4452fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
445399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
445499653d4eSeschrock "dataset is used in a non-global zone"));
445599653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf));
4456fa9e4066Sahrens }
4457fa9e4066Sahrens
44581092cc31SToomas Soome /*
44591092cc31SToomas Soome * Do not rename dataset currently mounted as "/".
44601092cc31SToomas Soome * Such rename is blocked by kernel (umount2()) anyhow,
44611092cc31SToomas Soome * but blocking rename here will also prevent us unmounting
44621092cc31SToomas Soome * /usr, /var datasets while preparing for rename, and
44631092cc31SToomas Soome * therefore disturbing our live boot environment.
44641092cc31SToomas Soome */
44651092cc31SToomas Soome char *mountpnt;
44661092cc31SToomas Soome if (zfs_is_mounted(zhp, &mountpnt)) {
44671092cc31SToomas Soome if (strcmp(mountpnt, "/") == 0) {
44681092cc31SToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44691092cc31SToomas Soome "dataset is mounted as \"/\""));
44701092cc31SToomas Soome free(mountpnt);
44711092cc31SToomas Soome return (zfs_error(hdl, EZFS_BUSY, errbuf));
44721092cc31SToomas Soome }
44731092cc31SToomas Soome free(mountpnt);
44741092cc31SToomas Soome }
44751092cc31SToomas Soome
4476cdf5b4caSmmusante if (recursive) {
4477f0c5ee21Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4478f0c5ee21Smmusante if (parentname == NULL) {
4479f0c5ee21Smmusante ret = -1;
4480f0c5ee21Smmusante goto error;
4481f0c5ee21Smmusante }
4482cdf5b4caSmmusante delim = strchr(parentname, '@');
4483cdf5b4caSmmusante *delim = '\0';
4484990b4856Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4485cdf5b4caSmmusante if (zhrp == NULL) {
4486f0c5ee21Smmusante ret = -1;
4487f0c5ee21Smmusante goto error;
4488cdf5b4caSmmusante }
448933cde0d0SMatthew Ahrens } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
44906a9cb0eaSEric Schrock if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
44916a9cb0eaSEric Schrock force_unmount ? MS_FORCE : 0)) == NULL)
4492cdf5b4caSmmusante return (-1);
4493cdf5b4caSmmusante
4494cdf5b4caSmmusante if (changelist_haszonedchild(cl)) {
4495cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4496cdf5b4caSmmusante "child dataset with inherited mountpoint is used "
4497cdf5b4caSmmusante "in a non-global zone"));
4498cdf5b4caSmmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4499f83b46baSPaul Dagnelie ret = -1;
4500cdf5b4caSmmusante goto error;
4501cdf5b4caSmmusante }
4502cdf5b4caSmmusante
4503cdf5b4caSmmusante if ((ret = changelist_prefix(cl)) != 0)
4504cdf5b4caSmmusante goto error;
4505cdf5b4caSmmusante }
4506fa9e4066Sahrens
4507e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp))
4508fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL;
4509fa9e4066Sahrens else
4510fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS;
4511fa9e4066Sahrens
451298579b20Snd (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4513e9dbad6fSeschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
451498579b20Snd
4515cdf5b4caSmmusante zc.zc_cookie = recursive;
4516cdf5b4caSmmusante
4517ecd6cf80Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4518cdf5b4caSmmusante /*
4519cdf5b4caSmmusante * if it was recursive, the one that actually failed will
4520cdf5b4caSmmusante * be in zc.zc_name
4521cdf5b4caSmmusante */
4522cdf5b4caSmmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
45233cb34c60Sahrens "cannot rename '%s'"), zc.zc_name);
4524cdf5b4caSmmusante
4525cdf5b4caSmmusante if (recursive && errno == EEXIST) {
4526cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4527cdf5b4caSmmusante "a child dataset already has a snapshot "
4528cdf5b4caSmmusante "with the new name"));
4529a10acbd6Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4530eb633035STom Caputi } else if (errno == EACCES) {
4531a60ca23dSTom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4532a60ca23dSTom Caputi "cannot move encrypted child outside of "
4533a60ca23dSTom Caputi "its encryption root"));
4534eb633035STom Caputi (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4535cdf5b4caSmmusante } else {
4536cdf5b4caSmmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4537cdf5b4caSmmusante }
4538fa9e4066Sahrens
4539fa9e4066Sahrens /*
4540fa9e4066Sahrens * On failure, we still want to remount any filesystems that
4541fa9e4066Sahrens * were previously mounted, so we don't alter the system state.
4542fa9e4066Sahrens */
454333cde0d0SMatthew Ahrens if (cl != NULL)
4544cdf5b4caSmmusante (void) changelist_postfix(cl);
4545fa9e4066Sahrens } else {
454633cde0d0SMatthew Ahrens if (cl != NULL) {
4547cdf5b4caSmmusante changelist_rename(cl, zfs_get_name(zhp), target);
4548cdf5b4caSmmusante ret = changelist_postfix(cl);
4549cdf5b4caSmmusante }
4550fa9e4066Sahrens }
4551fa9e4066Sahrens
4552fa9e4066Sahrens error:
455333cde0d0SMatthew Ahrens if (parentname != NULL) {
4554cdf5b4caSmmusante free(parentname);
4555cdf5b4caSmmusante }
455633cde0d0SMatthew Ahrens if (zhrp != NULL) {
4557cdf5b4caSmmusante zfs_close(zhrp);
4558cdf5b4caSmmusante }
455933cde0d0SMatthew Ahrens if (cl != NULL) {
4560cdf5b4caSmmusante changelist_free(cl);
4561cdf5b4caSmmusante }
4562fa9e4066Sahrens return (ret);
4563fa9e4066Sahrens }
4564fa9e4066Sahrens
4565e9dbad6fSeschrock nvlist_t *
zfs_get_user_props(zfs_handle_t * zhp)4566e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp)
4567e9dbad6fSeschrock {
4568e9dbad6fSeschrock return (zhp->zfs_user_props);
4569e9dbad6fSeschrock }
4570e9dbad6fSeschrock
457192241e0bSTom Erickson nvlist_t *
zfs_get_recvd_props(zfs_handle_t * zhp)457292241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp)
457392241e0bSTom Erickson {
457492241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL)
457592241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0)
457692241e0bSTom Erickson return (NULL);
457792241e0bSTom Erickson return (zhp->zfs_recvd_props);
457892241e0bSTom Erickson }
457992241e0bSTom Erickson
4580b1b8ab34Slling /*
4581b1b8ab34Slling * This function is used by 'zfs list' to determine the exact set of columns to
4582b1b8ab34Slling * display, and their maximum widths. This does two main things:
4583b1b8ab34Slling *
4584b1b8ab34Slling * - If this is a list of all properties, then expand the list to include
4585b1b8ab34Slling * all native properties, and set a flag so that for each dataset we look
4586b1b8ab34Slling * for new unique user properties and add them to the list.
4587b1b8ab34Slling *
4588b1b8ab34Slling * - For non fixed-width properties, keep track of the maximum width seen
458992241e0bSTom Erickson * so that we can size the column appropriately. If the user has
459092241e0bSTom Erickson * requested received property values, we also need to compute the width
459192241e0bSTom Erickson * of the RECEIVED column.
4592b1b8ab34Slling */
4593b1b8ab34Slling int
zfs_expand_proplist(zfs_handle_t * zhp,zprop_list_t ** plp,boolean_t received,boolean_t literal)459443d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
459543d68d68SYuri Pankov boolean_t literal)
4596b1b8ab34Slling {
4597b1b8ab34Slling libzfs_handle_t *hdl = zhp->zfs_hdl;
4598990b4856Slling zprop_list_t *entry;
4599990b4856Slling zprop_list_t **last, **start;
4600b1b8ab34Slling nvlist_t *userprops, *propval;
4601b1b8ab34Slling nvpair_t *elem;
4602b1b8ab34Slling char *strval;
4603b1b8ab34Slling char buf[ZFS_MAXPROPLEN];
4604b1b8ab34Slling
4605990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4606b1b8ab34Slling return (-1);
4607e9dbad6fSeschrock
4608e9dbad6fSeschrock userprops = zfs_get_user_props(zhp);
4609e9dbad6fSeschrock
4610e9dbad6fSeschrock entry = *plp;
4611e9dbad6fSeschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4612e9dbad6fSeschrock /*
4613e9dbad6fSeschrock * Go through and add any user properties as necessary. We
4614e9dbad6fSeschrock * start by incrementing our list pointer to the first
4615e9dbad6fSeschrock * non-native property.
4616e9dbad6fSeschrock */
4617e9dbad6fSeschrock start = plp;
4618e9dbad6fSeschrock while (*start != NULL) {
4619990b4856Slling if ((*start)->pl_prop == ZPROP_INVAL)
4620e9dbad6fSeschrock break;
4621e9dbad6fSeschrock start = &(*start)->pl_next;
4622e9dbad6fSeschrock }
4623e9dbad6fSeschrock
4624e9dbad6fSeschrock elem = NULL;
4625e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4626e9dbad6fSeschrock /*
4627e9dbad6fSeschrock * See if we've already found this property in our list.
4628e9dbad6fSeschrock */
4629e9dbad6fSeschrock for (last = start; *last != NULL;
4630e9dbad6fSeschrock last = &(*last)->pl_next) {
4631e9dbad6fSeschrock if (strcmp((*last)->pl_user_prop,
4632e9dbad6fSeschrock nvpair_name(elem)) == 0)
4633e9dbad6fSeschrock break;
4634e9dbad6fSeschrock }
4635e9dbad6fSeschrock
4636e9dbad6fSeschrock if (*last == NULL) {
4637e9dbad6fSeschrock if ((entry = zfs_alloc(hdl,
4638990b4856Slling sizeof (zprop_list_t))) == NULL ||
4639e9dbad6fSeschrock ((entry->pl_user_prop = zfs_strdup(hdl,
4640e9dbad6fSeschrock nvpair_name(elem)))) == NULL) {
4641e9dbad6fSeschrock free(entry);
4642e9dbad6fSeschrock return (-1);
4643e9dbad6fSeschrock }
4644e9dbad6fSeschrock
4645990b4856Slling entry->pl_prop = ZPROP_INVAL;
4646e9dbad6fSeschrock entry->pl_width = strlen(nvpair_name(elem));
4647e9dbad6fSeschrock entry->pl_all = B_TRUE;
4648e9dbad6fSeschrock *last = entry;
4649e9dbad6fSeschrock }
4650e9dbad6fSeschrock }
4651e9dbad6fSeschrock }
4652e9dbad6fSeschrock
4653e9dbad6fSeschrock /*
4654e9dbad6fSeschrock * Now go through and check the width of any non-fixed columns
4655e9dbad6fSeschrock */
4656e9dbad6fSeschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) {
465743d68d68SYuri Pankov if (entry->pl_fixed && !literal)
4658e9dbad6fSeschrock continue;
4659e9dbad6fSeschrock
4660990b4856Slling if (entry->pl_prop != ZPROP_INVAL) {
4661e9dbad6fSeschrock if (zfs_prop_get(zhp, entry->pl_prop,
466243d68d68SYuri Pankov buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4663e9dbad6fSeschrock if (strlen(buf) > entry->pl_width)
4664e9dbad6fSeschrock entry->pl_width = strlen(buf);
4665e9dbad6fSeschrock }
466692241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp,
466792241e0bSTom Erickson zfs_prop_to_name(entry->pl_prop),
466843d68d68SYuri Pankov buf, sizeof (buf), literal) == 0)
466992241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width)
467092241e0bSTom Erickson entry->pl_recvd_width = strlen(buf);
467192241e0bSTom Erickson } else {
467292241e0bSTom Erickson if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
467392241e0bSTom Erickson &propval) == 0) {
467492241e0bSTom Erickson verify(nvlist_lookup_string(propval,
467592241e0bSTom Erickson ZPROP_VALUE, &strval) == 0);
467692241e0bSTom Erickson if (strlen(strval) > entry->pl_width)
467792241e0bSTom Erickson entry->pl_width = strlen(strval);
467892241e0bSTom Erickson }
467992241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp,
468092241e0bSTom Erickson entry->pl_user_prop,
468143d68d68SYuri Pankov buf, sizeof (buf), literal) == 0)
468292241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width)
468392241e0bSTom Erickson entry->pl_recvd_width = strlen(buf);
4684e9dbad6fSeschrock }
4685e9dbad6fSeschrock }
4686e9dbad6fSeschrock
4687e9dbad6fSeschrock return (0);
4688e9dbad6fSeschrock }
4689ecd6cf80Smarks
4690ecd6cf80Smarks int
zfs_deleg_share_nfs(libzfs_handle_t * hdl,char * dataset,char * path,char * resource,void * export,void * sharetab,int sharemax,zfs_share_op_t operation)4691ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4692743a77edSAlan Wright char *resource, void *export, void *sharetab,
4693743a77edSAlan Wright int sharemax, zfs_share_op_t operation)
4694ecd6cf80Smarks {
4695ecd6cf80Smarks zfs_cmd_t zc = { 0 };
4696ecd6cf80Smarks int error;
4697ecd6cf80Smarks
4698ecd6cf80Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4699ecd6cf80Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4700743a77edSAlan Wright if (resource)
4701743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4702ecd6cf80Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4703ecd6cf80Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4704da6c28aaSamw zc.zc_share.z_sharetype = operation;
4705ecd6cf80Smarks zc.zc_share.z_sharemax = sharemax;
4706ecd6cf80Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4707ecd6cf80Smarks return (error);
4708ecd6cf80Smarks }
47092e5e9e19SSanjeev Bagewadi
47102e5e9e19SSanjeev Bagewadi void
zfs_prune_proplist(zfs_handle_t * zhp,uint8_t * props)47112e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
47122e5e9e19SSanjeev Bagewadi {
47132e5e9e19SSanjeev Bagewadi nvpair_t *curr;
47142e5e9e19SSanjeev Bagewadi
47152e5e9e19SSanjeev Bagewadi /*
47162e5e9e19SSanjeev Bagewadi * Keep a reference to the props-table against which we prune the
47172e5e9e19SSanjeev Bagewadi * properties.
47182e5e9e19SSanjeev Bagewadi */
47192e5e9e19SSanjeev Bagewadi zhp->zfs_props_table = props;
47202e5e9e19SSanjeev Bagewadi
47212e5e9e19SSanjeev Bagewadi curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
47222e5e9e19SSanjeev Bagewadi
47232e5e9e19SSanjeev Bagewadi while (curr) {
47242e5e9e19SSanjeev Bagewadi zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
47252e5e9e19SSanjeev Bagewadi nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
47262e5e9e19SSanjeev Bagewadi
472714843421SMatthew Ahrens /*
4728faaa6415SEric Schrock * User properties will result in ZPROP_INVAL, and since we
4729faaa6415SEric Schrock * only know how to prune standard ZFS properties, we always
4730faaa6415SEric Schrock * leave these in the list. This can also happen if we
4731faaa6415SEric Schrock * encounter an unknown DSL property (when running older
4732faaa6415SEric Schrock * software, for example).
473314843421SMatthew Ahrens */
473414843421SMatthew Ahrens if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
47352e5e9e19SSanjeev Bagewadi (void) nvlist_remove(zhp->zfs_props,
47362e5e9e19SSanjeev Bagewadi nvpair_name(curr), nvpair_type(curr));
47372e5e9e19SSanjeev Bagewadi curr = next;
47382e5e9e19SSanjeev Bagewadi }
47392e5e9e19SSanjeev Bagewadi }
4740743a77edSAlan Wright
4741743a77edSAlan Wright static int
zfs_smb_acl_mgmt(libzfs_handle_t * hdl,char * dataset,char * path,zfs_smb_acl_op_t cmd,char * resource1,char * resource2)4742743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4743743a77edSAlan Wright zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4744743a77edSAlan Wright {
4745743a77edSAlan Wright zfs_cmd_t zc = { 0 };
4746743a77edSAlan Wright nvlist_t *nvlist = NULL;
4747743a77edSAlan Wright int error;
4748743a77edSAlan Wright
4749743a77edSAlan Wright (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4750743a77edSAlan Wright (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4751743a77edSAlan Wright zc.zc_cookie = (uint64_t)cmd;
4752743a77edSAlan Wright
4753743a77edSAlan Wright if (cmd == ZFS_SMB_ACL_RENAME) {
4754743a77edSAlan Wright if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4755743a77edSAlan Wright (void) no_memory(hdl);
475630925561SChris Williamson return (0);
4757743a77edSAlan Wright }
4758743a77edSAlan Wright }
4759743a77edSAlan Wright
4760743a77edSAlan Wright switch (cmd) {
4761743a77edSAlan Wright case ZFS_SMB_ACL_ADD:
4762743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE:
4763743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4764743a77edSAlan Wright break;
4765743a77edSAlan Wright case ZFS_SMB_ACL_RENAME:
4766743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4767743a77edSAlan Wright resource1) != 0) {
4768743a77edSAlan Wright (void) no_memory(hdl);
4769743a77edSAlan Wright return (-1);
4770743a77edSAlan Wright }
4771743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4772743a77edSAlan Wright resource2) != 0) {
4773743a77edSAlan Wright (void) no_memory(hdl);
4774743a77edSAlan Wright return (-1);
4775743a77edSAlan Wright }
4776743a77edSAlan Wright if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4777743a77edSAlan Wright nvlist_free(nvlist);
4778743a77edSAlan Wright return (-1);
4779743a77edSAlan Wright }
4780743a77edSAlan Wright break;
4781743a77edSAlan Wright case ZFS_SMB_ACL_PURGE:
4782743a77edSAlan Wright break;
4783743a77edSAlan Wright default:
4784743a77edSAlan Wright return (-1);
4785743a77edSAlan Wright }
4786743a77edSAlan Wright error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4787aab83bb8SJosef 'Jeff' Sipek nvlist_free(nvlist);
4788743a77edSAlan Wright return (error);
4789743a77edSAlan Wright }
4790743a77edSAlan Wright
4791743a77edSAlan Wright int
zfs_smb_acl_add(libzfs_handle_t * hdl,char * dataset,char * path,char * resource)4792743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4793743a77edSAlan Wright char *path, char *resource)
4794743a77edSAlan Wright {
4795743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4796743a77edSAlan Wright resource, NULL));
4797743a77edSAlan Wright }
4798743a77edSAlan Wright
4799743a77edSAlan Wright int
zfs_smb_acl_remove(libzfs_handle_t * hdl,char * dataset,char * path,char * resource)4800743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4801743a77edSAlan Wright char *path, char *resource)
4802743a77edSAlan Wright {
4803743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4804743a77edSAlan Wright resource, NULL));
4805743a77edSAlan Wright }
4806743a77edSAlan Wright
4807743a77edSAlan Wright int
zfs_smb_acl_purge(libzfs_handle_t * hdl,char * dataset,char * path)4808743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4809743a77edSAlan Wright {
4810743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4811743a77edSAlan Wright NULL, NULL));
4812743a77edSAlan Wright }
4813743a77edSAlan Wright
4814743a77edSAlan Wright int
zfs_smb_acl_rename(libzfs_handle_t * hdl,char * dataset,char * path,char * oldname,char * newname)4815743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4816743a77edSAlan Wright char *oldname, char *newname)
4817743a77edSAlan Wright {
4818743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4819743a77edSAlan Wright oldname, newname));
4820743a77edSAlan Wright }
482114843421SMatthew Ahrens
482214843421SMatthew Ahrens int
zfs_userspace(zfs_handle_t * zhp,zfs_userquota_prop_t type,zfs_userspace_cb_t func,void * arg)482314843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
482414843421SMatthew Ahrens zfs_userspace_cb_t func, void *arg)
482514843421SMatthew Ahrens {
482614843421SMatthew Ahrens zfs_cmd_t zc = { 0 };
482714843421SMatthew Ahrens zfs_useracct_t buf[100];
482870f56fa6SYuri Pankov libzfs_handle_t *hdl = zhp->zfs_hdl;
482970f56fa6SYuri Pankov int ret;
483014843421SMatthew Ahrens
483119b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
483214843421SMatthew Ahrens
483314843421SMatthew Ahrens zc.zc_objset_type = type;
483414843421SMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)buf;
483514843421SMatthew Ahrens
483670f56fa6SYuri Pankov for (;;) {
483714843421SMatthew Ahrens zfs_useracct_t *zua = buf;
483814843421SMatthew Ahrens
483914843421SMatthew Ahrens zc.zc_nvlist_dst_size = sizeof (buf);
484070f56fa6SYuri Pankov if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
48413b2aab18SMatthew Ahrens char errbuf[1024];
484270f56fa6SYuri Pankov
4843f67950b2SNasf-Fan if ((errno == ENOTSUP &&
4844f67950b2SNasf-Fan (type == ZFS_PROP_USEROBJUSED ||
4845f67950b2SNasf-Fan type == ZFS_PROP_GROUPOBJUSED ||
4846f67950b2SNasf-Fan type == ZFS_PROP_USEROBJQUOTA ||
4847f67950b2SNasf-Fan type == ZFS_PROP_GROUPOBJQUOTA ||
4848f67950b2SNasf-Fan type == ZFS_PROP_PROJECTOBJUSED ||
4849f67950b2SNasf-Fan type == ZFS_PROP_PROJECTOBJQUOTA ||
4850f67950b2SNasf-Fan type == ZFS_PROP_PROJECTUSED ||
4851f67950b2SNasf-Fan type == ZFS_PROP_PROJECTQUOTA)))
4852f67950b2SNasf-Fan break;
4853f67950b2SNasf-Fan
485470f56fa6SYuri Pankov (void) snprintf(errbuf, sizeof (errbuf),
485570f56fa6SYuri Pankov dgettext(TEXT_DOMAIN,
485670f56fa6SYuri Pankov "cannot get used/quota for %s"), zc.zc_name);
485770f56fa6SYuri Pankov return (zfs_standard_error_fmt(hdl, errno, errbuf));
485870f56fa6SYuri Pankov }
485970f56fa6SYuri Pankov if (zc.zc_nvlist_dst_size == 0)
486014843421SMatthew Ahrens break;
486114843421SMatthew Ahrens
486214843421SMatthew Ahrens while (zc.zc_nvlist_dst_size > 0) {
486370f56fa6SYuri Pankov if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
486470f56fa6SYuri Pankov zua->zu_space)) != 0)
486570f56fa6SYuri Pankov return (ret);
486614843421SMatthew Ahrens zua++;
486714843421SMatthew Ahrens zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
486814843421SMatthew Ahrens }
486914843421SMatthew Ahrens }
487014843421SMatthew Ahrens
487170f56fa6SYuri Pankov return (0);
487214843421SMatthew Ahrens }
4873842727c2SChris Kirby
48743b2aab18SMatthew Ahrens struct holdarg {
48753b2aab18SMatthew Ahrens nvlist_t *nvl;
48763b2aab18SMatthew Ahrens const char *snapname;
48773b2aab18SMatthew Ahrens const char *tag;
48783b2aab18SMatthew Ahrens boolean_t recursive;
4879bb6e7075SMatthew Ahrens int error;
48803b2aab18SMatthew Ahrens };
48813b2aab18SMatthew Ahrens
48823b2aab18SMatthew Ahrens static int
zfs_hold_one(zfs_handle_t * zhp,void * arg)48833b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg)
48843b2aab18SMatthew Ahrens {
48853b2aab18SMatthew Ahrens struct holdarg *ha = arg;
48869adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
48873b2aab18SMatthew Ahrens int rv = 0;
48883b2aab18SMatthew Ahrens
48893b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name),
48903b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname);
48913b2aab18SMatthew Ahrens
4892a7a845e4SSteven Hartland if (lzc_exists(name))
48933b2aab18SMatthew Ahrens fnvlist_add_string(ha->nvl, name, ha->tag);
48943b2aab18SMatthew Ahrens
48953b2aab18SMatthew Ahrens if (ha->recursive)
48963b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
48973b2aab18SMatthew Ahrens zfs_close(zhp);
48983b2aab18SMatthew Ahrens return (rv);
48993b2aab18SMatthew Ahrens }
49003b2aab18SMatthew Ahrens
4901842727c2SChris Kirby int
zfs_hold(zfs_handle_t * zhp,const char * snapname,const char * tag,boolean_t recursive,int cleanup_fd)4902842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4903a7a845e4SSteven Hartland boolean_t recursive, int cleanup_fd)
4904842727c2SChris Kirby {
49053b2aab18SMatthew Ahrens int ret;
49063b2aab18SMatthew Ahrens struct holdarg ha;
4907842727c2SChris Kirby
49083b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc();
49093b2aab18SMatthew Ahrens ha.snapname = snapname;
49103b2aab18SMatthew Ahrens ha.tag = tag;
49113b2aab18SMatthew Ahrens ha.recursive = recursive;
49123b2aab18SMatthew Ahrens (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4913013023d4SMartin Matuska
4914a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) {
4915a7a845e4SSteven Hartland char errbuf[1024];
4916a7a845e4SSteven Hartland
4917013023d4SMartin Matuska fnvlist_free(ha.nvl);
4918013023d4SMartin Matuska ret = ENOENT;
4919a7a845e4SSteven Hartland (void) snprintf(errbuf, sizeof (errbuf),
4920a7a845e4SSteven Hartland dgettext(TEXT_DOMAIN,
4921a7a845e4SSteven Hartland "cannot hold snapshot '%s@%s'"),
4922a7a845e4SSteven Hartland zhp->zfs_name, snapname);
4923a7a845e4SSteven Hartland (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4924013023d4SMartin Matuska return (ret);
4925013023d4SMartin Matuska }
4926013023d4SMartin Matuska
4927a7a845e4SSteven Hartland ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
49283b2aab18SMatthew Ahrens fnvlist_free(ha.nvl);
4929a7f53a56SChris Kirby
4930a7a845e4SSteven Hartland return (ret);
4931a7a845e4SSteven Hartland }
4932a7a845e4SSteven Hartland
4933a7a845e4SSteven Hartland int
zfs_hold_nvl(zfs_handle_t * zhp,int cleanup_fd,nvlist_t * holds)4934a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4935a7a845e4SSteven Hartland {
4936a7a845e4SSteven Hartland int ret;
4937a7a845e4SSteven Hartland nvlist_t *errors;
4938a7a845e4SSteven Hartland libzfs_handle_t *hdl = zhp->zfs_hdl;
4939a7a845e4SSteven Hartland char errbuf[1024];
4940a7a845e4SSteven Hartland nvpair_t *elem;
4941a7a845e4SSteven Hartland
4942a7a845e4SSteven Hartland errors = NULL;
4943a7a845e4SSteven Hartland ret = lzc_hold(holds, cleanup_fd, &errors);
4944a7a845e4SSteven Hartland
4945a7a845e4SSteven Hartland if (ret == 0) {
4946a7a845e4SSteven Hartland /* There may be errors even in the success case. */
4947a7a845e4SSteven Hartland fnvlist_free(errors);
49483b2aab18SMatthew Ahrens return (0);
4949a7a845e4SSteven Hartland }
4950842727c2SChris Kirby
4951a7a845e4SSteven Hartland if (nvlist_empty(errors)) {
49523b2aab18SMatthew Ahrens /* no hold-specific errors */
49533b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf),
49543b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot hold"));
49553b2aab18SMatthew Ahrens switch (ret) {
49563b2aab18SMatthew Ahrens case ENOTSUP:
49573b2aab18SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
49583b2aab18SMatthew Ahrens "pool must be upgraded"));
49593b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
49603b2aab18SMatthew Ahrens break;
49613b2aab18SMatthew Ahrens case EINVAL:
49623b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
49633b2aab18SMatthew Ahrens break;
49643b2aab18SMatthew Ahrens default:
49653b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf);
49663b2aab18SMatthew Ahrens }
49673b2aab18SMatthew Ahrens }
4968842727c2SChris Kirby
49693b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL);
49703b2aab18SMatthew Ahrens elem != NULL;
49713b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) {
49723b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf),
49733b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN,
49743b2aab18SMatthew Ahrens "cannot hold snapshot '%s'"), nvpair_name(elem));
49753b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) {
497615508ac0SChris Kirby case E2BIG:
497715508ac0SChris Kirby /*
497815508ac0SChris Kirby * Temporary tags wind up having the ds object id
497915508ac0SChris Kirby * prepended. So even if we passed the length check
498015508ac0SChris Kirby * above, it's still possible for the tag to wind
498115508ac0SChris Kirby * up being slightly too long.
498215508ac0SChris Kirby */
49833b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
49843b2aab18SMatthew Ahrens break;
4985842727c2SChris Kirby case EINVAL:
49863b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
49873b2aab18SMatthew Ahrens break;
4988842727c2SChris Kirby case EEXIST:
49893b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
49903b2aab18SMatthew Ahrens break;
4991842727c2SChris Kirby default:
49923b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl,
49933b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf);
4994842727c2SChris Kirby }
4995842727c2SChris Kirby }
4996842727c2SChris Kirby
49973b2aab18SMatthew Ahrens fnvlist_free(errors);
49983b2aab18SMatthew Ahrens return (ret);
49993b2aab18SMatthew Ahrens }
50003b2aab18SMatthew Ahrens
50013b2aab18SMatthew Ahrens static int
zfs_release_one(zfs_handle_t * zhp,void * arg)50023b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg)
50033b2aab18SMatthew Ahrens {
50043b2aab18SMatthew Ahrens struct holdarg *ha = arg;
50059adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
50063b2aab18SMatthew Ahrens int rv = 0;
5007bb6e7075SMatthew Ahrens nvlist_t *existing_holds;
50083b2aab18SMatthew Ahrens
50093b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name),
50103b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname);
50113b2aab18SMatthew Ahrens
5012bb6e7075SMatthew Ahrens if (lzc_get_holds(name, &existing_holds) != 0) {
5013bb6e7075SMatthew Ahrens ha->error = ENOENT;
5014bb6e7075SMatthew Ahrens } else if (!nvlist_exists(existing_holds, ha->tag)) {
5015bb6e7075SMatthew Ahrens ha->error = ESRCH;
5016bb6e7075SMatthew Ahrens } else {
5017bb6e7075SMatthew Ahrens nvlist_t *torelease = fnvlist_alloc();
5018bb6e7075SMatthew Ahrens fnvlist_add_boolean(torelease, ha->tag);
5019bb6e7075SMatthew Ahrens fnvlist_add_nvlist(ha->nvl, name, torelease);
5020bb6e7075SMatthew Ahrens fnvlist_free(torelease);
50213b2aab18SMatthew Ahrens }
50223b2aab18SMatthew Ahrens
50233b2aab18SMatthew Ahrens if (ha->recursive)
50243b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
50253b2aab18SMatthew Ahrens zfs_close(zhp);
50263b2aab18SMatthew Ahrens return (rv);
5027842727c2SChris Kirby }
5028842727c2SChris Kirby
5029842727c2SChris Kirby int
zfs_release(zfs_handle_t * zhp,const char * snapname,const char * tag,boolean_t recursive)5030842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
5031842727c2SChris Kirby boolean_t recursive)
5032842727c2SChris Kirby {
50333b2aab18SMatthew Ahrens int ret;
50343b2aab18SMatthew Ahrens struct holdarg ha;
5035a7a845e4SSteven Hartland nvlist_t *errors = NULL;
50363b2aab18SMatthew Ahrens nvpair_t *elem;
5037842727c2SChris Kirby libzfs_handle_t *hdl = zhp->zfs_hdl;
5038013023d4SMartin Matuska char errbuf[1024];
5039842727c2SChris Kirby
50403b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc();
50413b2aab18SMatthew Ahrens ha.snapname = snapname;
50423b2aab18SMatthew Ahrens ha.tag = tag;
50433b2aab18SMatthew Ahrens ha.recursive = recursive;
5044bb6e7075SMatthew Ahrens ha.error = 0;
50453b2aab18SMatthew Ahrens (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
5046013023d4SMartin Matuska
5047a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) {
5048013023d4SMartin Matuska fnvlist_free(ha.nvl);
5049bb6e7075SMatthew Ahrens ret = ha.error;
5050013023d4SMartin Matuska (void) snprintf(errbuf, sizeof (errbuf),
5051013023d4SMartin Matuska dgettext(TEXT_DOMAIN,
5052013023d4SMartin Matuska "cannot release hold from snapshot '%s@%s'"),
5053013023d4SMartin Matuska zhp->zfs_name, snapname);
5054bb6e7075SMatthew Ahrens if (ret == ESRCH) {
5055bb6e7075SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5056bb6e7075SMatthew Ahrens } else {
5057bb6e7075SMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf);
5058bb6e7075SMatthew Ahrens }
5059013023d4SMartin Matuska return (ret);
5060013023d4SMartin Matuska }
5061013023d4SMartin Matuska
50623b2aab18SMatthew Ahrens ret = lzc_release(ha.nvl, &errors);
50633b2aab18SMatthew Ahrens fnvlist_free(ha.nvl);
5064842727c2SChris Kirby
5065a7a845e4SSteven Hartland if (ret == 0) {
5066a7a845e4SSteven Hartland /* There may be errors even in the success case. */
5067a7a845e4SSteven Hartland fnvlist_free(errors);
50683b2aab18SMatthew Ahrens return (0);
5069a7a845e4SSteven Hartland }
50703b2aab18SMatthew Ahrens
5071a7a845e4SSteven Hartland if (nvlist_empty(errors)) {
50723b2aab18SMatthew Ahrens /* no hold-specific errors */
5073842727c2SChris Kirby (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
50743b2aab18SMatthew Ahrens "cannot release"));
5075842727c2SChris Kirby switch (errno) {
5076842727c2SChris Kirby case ENOTSUP:
5077842727c2SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5078842727c2SChris Kirby "pool must be upgraded"));
50793b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
50803b2aab18SMatthew Ahrens break;
50813b2aab18SMatthew Ahrens default:
50823b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, errno, errbuf);
50833b2aab18SMatthew Ahrens }
50843b2aab18SMatthew Ahrens }
50853b2aab18SMatthew Ahrens
50863b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL);
50873b2aab18SMatthew Ahrens elem != NULL;
50883b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) {
50893b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf),
50903b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN,
50913b2aab18SMatthew Ahrens "cannot release hold from snapshot '%s'"),
50923b2aab18SMatthew Ahrens nvpair_name(elem));
50933b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) {
50943b2aab18SMatthew Ahrens case ESRCH:
50953b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
50963b2aab18SMatthew Ahrens break;
5097842727c2SChris Kirby case EINVAL:
50983b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
50993b2aab18SMatthew Ahrens break;
5100842727c2SChris Kirby default:
51013b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl,
51023b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf);
5103842727c2SChris Kirby }
5104842727c2SChris Kirby }
5105842727c2SChris Kirby
51063b2aab18SMatthew Ahrens fnvlist_free(errors);
51073b2aab18SMatthew Ahrens return (ret);
5108842727c2SChris Kirby }
5109ca45db41SChris Kirby
51101af68beaSAlexander Stetsenko int
zfs_get_fsacl(zfs_handle_t * zhp,nvlist_t ** nvl)51111af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
51121af68beaSAlexander Stetsenko {
51131af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 };
51141af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl;
51151af68beaSAlexander Stetsenko int nvsz = 2048;
51161af68beaSAlexander Stetsenko void *nvbuf;
51171af68beaSAlexander Stetsenko int err = 0;
51183b2aab18SMatthew Ahrens char errbuf[1024];
51191af68beaSAlexander Stetsenko
51201af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
51211af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
51221af68beaSAlexander Stetsenko
51231af68beaSAlexander Stetsenko tryagain:
51241af68beaSAlexander Stetsenko
51251af68beaSAlexander Stetsenko nvbuf = malloc(nvsz);
51261af68beaSAlexander Stetsenko if (nvbuf == NULL) {
51271af68beaSAlexander Stetsenko err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
51281af68beaSAlexander Stetsenko goto out;
51291af68beaSAlexander Stetsenko }
51301af68beaSAlexander Stetsenko
51311af68beaSAlexander Stetsenko zc.zc_nvlist_dst_size = nvsz;
51321af68beaSAlexander Stetsenko zc.zc_nvlist_dst = (uintptr_t)nvbuf;
51331af68beaSAlexander Stetsenko
51349adfa60dSMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
51351af68beaSAlexander Stetsenko
5136d7f601efSGeorge Wilson if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
51371af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf),
51381af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
51391af68beaSAlexander Stetsenko zc.zc_name);
51401af68beaSAlexander Stetsenko switch (errno) {
51411af68beaSAlexander Stetsenko case ENOMEM:
51421af68beaSAlexander Stetsenko free(nvbuf);
51431af68beaSAlexander Stetsenko nvsz = zc.zc_nvlist_dst_size;
51441af68beaSAlexander Stetsenko goto tryagain;
51451af68beaSAlexander Stetsenko
51461af68beaSAlexander Stetsenko case ENOTSUP:
51471af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51481af68beaSAlexander Stetsenko "pool must be upgraded"));
51491af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
51501af68beaSAlexander Stetsenko break;
51511af68beaSAlexander Stetsenko case EINVAL:
51521af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
51531af68beaSAlexander Stetsenko break;
51541af68beaSAlexander Stetsenko case ENOENT:
51551af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf);
51561af68beaSAlexander Stetsenko break;
51571af68beaSAlexander Stetsenko default:
51581af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf);
51591af68beaSAlexander Stetsenko break;
51601af68beaSAlexander Stetsenko }
51611af68beaSAlexander Stetsenko } else {
51621af68beaSAlexander Stetsenko /* success */
51631af68beaSAlexander Stetsenko int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
51641af68beaSAlexander Stetsenko if (rc) {
51651af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), dgettext(
51661af68beaSAlexander Stetsenko TEXT_DOMAIN, "cannot get permissions on '%s'"),
51671af68beaSAlexander Stetsenko zc.zc_name);
51681af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, rc, errbuf);
51691af68beaSAlexander Stetsenko }
51701af68beaSAlexander Stetsenko }
51711af68beaSAlexander Stetsenko
51721af68beaSAlexander Stetsenko free(nvbuf);
51731af68beaSAlexander Stetsenko out:
51741af68beaSAlexander Stetsenko return (err);
51751af68beaSAlexander Stetsenko }
51761af68beaSAlexander Stetsenko
51771af68beaSAlexander Stetsenko int
zfs_set_fsacl(zfs_handle_t * zhp,boolean_t un,nvlist_t * nvl)51781af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
51791af68beaSAlexander Stetsenko {
51801af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 };
51811af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl;
51821af68beaSAlexander Stetsenko char *nvbuf;
51833b2aab18SMatthew Ahrens char errbuf[1024];
51841af68beaSAlexander Stetsenko size_t nvsz;
51851af68beaSAlexander Stetsenko int err;
51861af68beaSAlexander Stetsenko
51871af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
51881af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
51891af68beaSAlexander Stetsenko
51901af68beaSAlexander Stetsenko err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
51911af68beaSAlexander Stetsenko assert(err == 0);
51921af68beaSAlexander Stetsenko
51931af68beaSAlexander Stetsenko nvbuf = malloc(nvsz);
51941af68beaSAlexander Stetsenko
51951af68beaSAlexander Stetsenko err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
51961af68beaSAlexander Stetsenko assert(err == 0);
51971af68beaSAlexander Stetsenko
51981af68beaSAlexander Stetsenko zc.zc_nvlist_src_size = nvsz;
51991af68beaSAlexander Stetsenko zc.zc_nvlist_src = (uintptr_t)nvbuf;
52001af68beaSAlexander Stetsenko zc.zc_perm_action = un;
52011af68beaSAlexander Stetsenko
52021af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
52031af68beaSAlexander Stetsenko
52041af68beaSAlexander Stetsenko if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
52051af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf),
52061af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
52071af68beaSAlexander Stetsenko zc.zc_name);
52081af68beaSAlexander Stetsenko switch (errno) {
52091af68beaSAlexander Stetsenko case ENOTSUP:
52101af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
52111af68beaSAlexander Stetsenko "pool must be upgraded"));
52121af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
52131af68beaSAlexander Stetsenko break;
52141af68beaSAlexander Stetsenko case EINVAL:
52151af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
52161af68beaSAlexander Stetsenko break;
52171af68beaSAlexander Stetsenko case ENOENT:
52181af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf);
52191af68beaSAlexander Stetsenko break;
52201af68beaSAlexander Stetsenko default:
52211af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf);
52221af68beaSAlexander Stetsenko break;
52231af68beaSAlexander Stetsenko }
52241af68beaSAlexander Stetsenko }
52251af68beaSAlexander Stetsenko
52261af68beaSAlexander Stetsenko free(nvbuf);
52271af68beaSAlexander Stetsenko
52281af68beaSAlexander Stetsenko return (err);
52291af68beaSAlexander Stetsenko }
52301af68beaSAlexander Stetsenko
52311af68beaSAlexander Stetsenko int
zfs_get_holds(zfs_handle_t * zhp,nvlist_t ** nvl)52321af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
52331af68beaSAlexander Stetsenko {
52343b2aab18SMatthew Ahrens int err;
52353b2aab18SMatthew Ahrens char errbuf[1024];
52361af68beaSAlexander Stetsenko
52373b2aab18SMatthew Ahrens err = lzc_get_holds(zhp->zfs_name, nvl);
52381af68beaSAlexander Stetsenko
52393b2aab18SMatthew Ahrens if (err != 0) {
52403b2aab18SMatthew Ahrens libzfs_handle_t *hdl = zhp->zfs_hdl;
52411af68beaSAlexander Stetsenko
52421af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf),
52431af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
52443b2aab18SMatthew Ahrens zhp->zfs_name);
52453b2aab18SMatthew Ahrens switch (err) {
52461af68beaSAlexander Stetsenko case ENOTSUP:
52471af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
52481af68beaSAlexander Stetsenko "pool must be upgraded"));
52491af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
52501af68beaSAlexander Stetsenko break;
52511af68beaSAlexander Stetsenko case EINVAL:
52521af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
52531af68beaSAlexander Stetsenko break;
52541af68beaSAlexander Stetsenko case ENOENT:
52551af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf);
52561af68beaSAlexander Stetsenko break;
52571af68beaSAlexander Stetsenko default:
52581af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf);
52591af68beaSAlexander Stetsenko break;
52601af68beaSAlexander Stetsenko }
52611af68beaSAlexander Stetsenko }
52621af68beaSAlexander Stetsenko
52631af68beaSAlexander Stetsenko return (err);
52641af68beaSAlexander Stetsenko }
52651af68beaSAlexander Stetsenko
52663e30c24aSWill Andrews /*
5267b73ccab0SMike Gerdts * The theory of raidz space accounting
5268b73ccab0SMike Gerdts *
5269b73ccab0SMike Gerdts * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5270b73ccab0SMike Gerdts * will "reference" 128KB, even though it allocates more than that, to store the
5271b73ccab0SMike Gerdts * parity information (and perhaps skip sectors). This concept of the
5272b73ccab0SMike Gerdts * "referenced" (and other DMU space accounting) being lower than the allocated
5273b73ccab0SMike Gerdts * space by a constant factor is called "raidz deflation."
5274b73ccab0SMike Gerdts *
5275b73ccab0SMike Gerdts * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5276b73ccab0SMike Gerdts * block size. However, zvols typically have a much smaller block size (default
5277b73ccab0SMike Gerdts * 8KB). These smaller blocks may require proportionally much more parity
5278b73ccab0SMike Gerdts * information (and perhaps skip sectors). In this case, the change to the
5279b73ccab0SMike Gerdts * "referenced" property may be much more than the logical block size.
5280b73ccab0SMike Gerdts *
5281b73ccab0SMike Gerdts * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written
5282b73ccab0SMike Gerdts * as follows.
5283b73ccab0SMike Gerdts *
5284b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+
5285b73ccab0SMike Gerdts * | disk1 | disk2 | disk3 | disk4 | disk5 |
5286b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+
5287b73ccab0SMike Gerdts * | P0 | D0 | D8 | D16 | D24 |
5288b73ccab0SMike Gerdts * | P1 | D1 | D9 | D17 | D25 |
5289b73ccab0SMike Gerdts * | P2 | D2 | D10 | D18 | D26 |
5290b73ccab0SMike Gerdts * | P3 | D3 | D11 | D19 | D27 |
5291b73ccab0SMike Gerdts * | P4 | D4 | D12 | D20 | D28 |
5292b73ccab0SMike Gerdts * | P5 | D5 | D13 | D21 | D29 |
5293b73ccab0SMike Gerdts * | P6 | D6 | D14 | D22 | D30 |
5294b73ccab0SMike Gerdts * | P7 | D7 | D15 | D23 | D31 |
5295b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+
5296b73ccab0SMike Gerdts *
5297b73ccab0SMike Gerdts * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5298b73ccab0SMike Gerdts * sectors. The dataset's referenced will increase by 128k and the pool's
5299b73ccab0SMike Gerdts * allocated and free properties will be adjusted by 160k.
5300b73ccab0SMike Gerdts *
5301b73ccab0SMike Gerdts * A 4k block written to the same raidz vdev will require two 4k sectors. The
5302b73ccab0SMike Gerdts * blank cells represent unallocated space.
5303b73ccab0SMike Gerdts *
5304b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+
5305b73ccab0SMike Gerdts * | disk1 | disk2 | disk3 | disk4 | disk5 |
5306b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+
5307b73ccab0SMike Gerdts * | P0 | D0 | | | |
5308b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+
5309b73ccab0SMike Gerdts *
5310b73ccab0SMike Gerdts * Above, notice that the 4k block required one sector for parity and another
5311b73ccab0SMike Gerdts * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated
5312b73ccab0SMike Gerdts * and free properties will be adjusted by 8k. The dataset will not be charged
5313b73ccab0SMike Gerdts * 8k. Rather, it will be charged a value that is scaled according to the
5314b73ccab0SMike Gerdts * overhead of the 128k block on the same vdev. This 8k allocation will be
5315b73ccab0SMike Gerdts * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5316b73ccab0SMike Gerdts * calculated in the 128k block example above.
5317b73ccab0SMike Gerdts *
5318b73ccab0SMike Gerdts * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That
5319b73ccab0SMike Gerdts * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5320b73ccab0SMike Gerdts * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5321b73ccab0SMike Gerdts * multiple of of 4 sectors. When a block does not fill the required number of
5322b73ccab0SMike Gerdts * sectors, skip blocks (sectors) are used.
5323b73ccab0SMike Gerdts *
5324b73ccab0SMike Gerdts * An 8k block being written to a raidz vdev may be written as follows:
5325b73ccab0SMike Gerdts *
5326b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+
5327b73ccab0SMike Gerdts * | disk1 | disk2 | disk3 | disk4 | disk5 |
5328b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+
5329b73ccab0SMike Gerdts * | P0 | D0 | D1 | S0 | |
5330b73ccab0SMike Gerdts * +-------+-------+-------+-------+-------+
5331b73ccab0SMike Gerdts *
5332b73ccab0SMike Gerdts * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5333b73ccab0SMike Gerdts * added. For this 8k block, the pool's allocated and free properties are
5334b73ccab0SMike Gerdts * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5335b73ccab0SMike Gerdts * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5336b73ccab0SMike Gerdts * the 128k block example above.
5337b73ccab0SMike Gerdts *
5338b73ccab0SMike Gerdts * Compression may lead to a variety of block sizes being written for the same
5339b73ccab0SMike Gerdts * volume or file. There is no clear way to reserve just the amount of space
5340b73ccab0SMike Gerdts * that will be required, so the worst case (no compression) is assumed.
5341b73ccab0SMike Gerdts * Note that metadata blocks will typically be compressed, so the reservation
5342b73ccab0SMike Gerdts * size returned by zvol_volsize_to_reservation() will generally be slightly
5343b73ccab0SMike Gerdts * larger than the maximum that the volume can reference.
5344b73ccab0SMike Gerdts */
5345b73ccab0SMike Gerdts
5346b73ccab0SMike Gerdts /*
5347b73ccab0SMike Gerdts * Derived from function of same name in uts/common/fs/zfs/vdev_raidz.c.
5348b73ccab0SMike Gerdts * Returns the amount of space (in bytes) that will be allocated for the
5349b73ccab0SMike Gerdts * specified block size. Note that the "referenced" space accounted will be less
5350b73ccab0SMike Gerdts * than this, but not necessarily equal to "blksize", due to RAIDZ deflation.
5351b73ccab0SMike Gerdts */
5352b73ccab0SMike Gerdts static uint64_t
vdev_raidz_asize(uint64_t ndisks,uint64_t nparity,uint64_t ashift,uint64_t blksize)5353b73ccab0SMike Gerdts vdev_raidz_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift,
5354b73ccab0SMike Gerdts uint64_t blksize)
5355b73ccab0SMike Gerdts {
5356b73ccab0SMike Gerdts uint64_t asize, ndata;
5357b73ccab0SMike Gerdts
5358b73ccab0SMike Gerdts ASSERT3U(ndisks, >, nparity);
5359b73ccab0SMike Gerdts ndata = ndisks - nparity;
5360b73ccab0SMike Gerdts asize = ((blksize - 1) >> ashift) + 1;
5361b73ccab0SMike Gerdts asize += nparity * ((asize + ndata - 1) / ndata);
5362b73ccab0SMike Gerdts asize = roundup(asize, nparity + 1) << ashift;
5363b73ccab0SMike Gerdts
5364b73ccab0SMike Gerdts return (asize);
5365b73ccab0SMike Gerdts }
5366b73ccab0SMike Gerdts
5367b73ccab0SMike Gerdts /*
5368b73ccab0SMike Gerdts * Determine how much space will be allocated if it lands on the most space-
5369b73ccab0SMike Gerdts * inefficient top-level vdev. Returns the size in bytes required to store one
5370b73ccab0SMike Gerdts * copy of the volume data. See theory comment above.
5371b73ccab0SMike Gerdts */
5372b73ccab0SMike Gerdts static uint64_t
volsize_from_vdevs(zpool_handle_t * zhp,uint64_t nblocks,uint64_t blksize)5373b73ccab0SMike Gerdts volsize_from_vdevs(zpool_handle_t *zhp, uint64_t nblocks, uint64_t blksize)
5374b73ccab0SMike Gerdts {
5375b73ccab0SMike Gerdts nvlist_t *config, *tree, **vdevs;
5376b73ccab0SMike Gerdts uint_t nvdevs, v;
5377b73ccab0SMike Gerdts uint64_t ret = 0;
5378b73ccab0SMike Gerdts
5379b73ccab0SMike Gerdts config = zpool_get_config(zhp, NULL);
5380b73ccab0SMike Gerdts if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 ||
5381b73ccab0SMike Gerdts nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN,
5382b73ccab0SMike Gerdts &vdevs, &nvdevs) != 0) {
5383b73ccab0SMike Gerdts return (nblocks * blksize);
5384b73ccab0SMike Gerdts }
5385b73ccab0SMike Gerdts
5386b73ccab0SMike Gerdts for (v = 0; v < nvdevs; v++) {
5387b73ccab0SMike Gerdts char *type;
5388b73ccab0SMike Gerdts uint64_t nparity, ashift, asize, tsize;
5389b73ccab0SMike Gerdts nvlist_t **disks;
5390b73ccab0SMike Gerdts uint_t ndisks;
5391b73ccab0SMike Gerdts uint64_t volsize;
5392b73ccab0SMike Gerdts
5393b73ccab0SMike Gerdts if (nvlist_lookup_string(vdevs[v], ZPOOL_CONFIG_TYPE,
5394b73ccab0SMike Gerdts &type) != 0 || strcmp(type, VDEV_TYPE_RAIDZ) != 0 ||
5395b73ccab0SMike Gerdts nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_NPARITY,
5396b73ccab0SMike Gerdts &nparity) != 0 ||
5397b73ccab0SMike Gerdts nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_ASHIFT,
5398b73ccab0SMike Gerdts &ashift) != 0 ||
5399b73ccab0SMike Gerdts nvlist_lookup_nvlist_array(vdevs[v], ZPOOL_CONFIG_CHILDREN,
5400b73ccab0SMike Gerdts &disks, &ndisks) != 0) {
5401b73ccab0SMike Gerdts continue;
5402b73ccab0SMike Gerdts }
5403b73ccab0SMike Gerdts
5404b73ccab0SMike Gerdts /* allocation size for the "typical" 128k block */
5405b73ccab0SMike Gerdts tsize = vdev_raidz_asize(ndisks, nparity, ashift,
5406b73ccab0SMike Gerdts SPA_OLD_MAXBLOCKSIZE);
5407b73ccab0SMike Gerdts /* allocation size for the blksize block */
5408b73ccab0SMike Gerdts asize = vdev_raidz_asize(ndisks, nparity, ashift, blksize);
5409b73ccab0SMike Gerdts
5410b73ccab0SMike Gerdts /*
5411b73ccab0SMike Gerdts * Scale this size down as a ratio of 128k / tsize. See theory
5412b73ccab0SMike Gerdts * statement above.
5413b73ccab0SMike Gerdts */
5414b73ccab0SMike Gerdts volsize = nblocks * asize * SPA_OLD_MAXBLOCKSIZE / tsize;
5415b73ccab0SMike Gerdts if (volsize > ret) {
5416b73ccab0SMike Gerdts ret = volsize;
5417b73ccab0SMike Gerdts }
5418b73ccab0SMike Gerdts }
5419b73ccab0SMike Gerdts
5420b73ccab0SMike Gerdts if (ret == 0) {
5421b73ccab0SMike Gerdts ret = nblocks * blksize;
5422b73ccab0SMike Gerdts }
5423b73ccab0SMike Gerdts
5424b73ccab0SMike Gerdts return (ret);
5425b73ccab0SMike Gerdts }
5426b73ccab0SMike Gerdts
5427b73ccab0SMike Gerdts /*
5428b73ccab0SMike Gerdts * Convert the zvol's volume size to an appropriate reservation. See theory
5429b73ccab0SMike Gerdts * comment above.
5430b73ccab0SMike Gerdts *
54313e30c24aSWill Andrews * Note: If this routine is updated, it is necessary to update the ZFS test
5432b73ccab0SMike Gerdts * suite's shell version in reservation.shlib.
54333e30c24aSWill Andrews */
5434c1449561SEric Taylor uint64_t
zvol_volsize_to_reservation(zpool_handle_t * zph,uint64_t volsize,nvlist_t * props)5435b73ccab0SMike Gerdts zvol_volsize_to_reservation(zpool_handle_t *zph, uint64_t volsize,
5436b73ccab0SMike Gerdts nvlist_t *props)
5437c1449561SEric Taylor {
5438c1449561SEric Taylor uint64_t numdb;
5439c1449561SEric Taylor uint64_t nblocks, volblocksize;
5440c1449561SEric Taylor int ncopies;
5441c1449561SEric Taylor char *strval;
5442c1449561SEric Taylor
5443c1449561SEric Taylor if (nvlist_lookup_string(props,
5444c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5445c1449561SEric Taylor ncopies = atoi(strval);
5446c1449561SEric Taylor else
5447c1449561SEric Taylor ncopies = 1;
5448c1449561SEric Taylor if (nvlist_lookup_uint64(props,
5449c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5450c1449561SEric Taylor &volblocksize) != 0)
5451c1449561SEric Taylor volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5452b73ccab0SMike Gerdts
5453b73ccab0SMike Gerdts nblocks = volsize / volblocksize;
5454b73ccab0SMike Gerdts /*
5455b73ccab0SMike Gerdts * Metadata defaults to using 128k blocks, not volblocksize blocks. For
5456b73ccab0SMike Gerdts * this reason, only the data blocks are scaled based on vdev config.
5457b73ccab0SMike Gerdts */
5458b73ccab0SMike Gerdts volsize = volsize_from_vdevs(zph, nblocks, volblocksize);
5459b73ccab0SMike Gerdts
5460c1449561SEric Taylor /* start with metadnode L0-L6 */
5461c1449561SEric Taylor numdb = 7;
5462c1449561SEric Taylor /* calculate number of indirects */
5463c1449561SEric Taylor while (nblocks > 1) {
5464c1449561SEric Taylor nblocks += DNODES_PER_LEVEL - 1;
5465c1449561SEric Taylor nblocks /= DNODES_PER_LEVEL;
5466c1449561SEric Taylor numdb += nblocks;
5467c1449561SEric Taylor }
5468c1449561SEric Taylor numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5469c1449561SEric Taylor volsize *= ncopies;
5470c1449561SEric Taylor /*
5471c1449561SEric Taylor * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5472c1449561SEric Taylor * compressed, but in practice they compress down to about
5473c1449561SEric Taylor * 1100 bytes
5474c1449561SEric Taylor */
5475c1449561SEric Taylor numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5476c1449561SEric Taylor volsize += numdb;
5477c1449561SEric Taylor return (volsize);
5478c1449561SEric Taylor }
5479