xref: /freebsd/sys/contrib/openzfs/module/zfs/dsl_deleg.c (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23eda14cbcSMatt Macy  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy 
26eda14cbcSMatt Macy /*
27eda14cbcSMatt Macy  * DSL permissions are stored in a two level zap attribute
28eda14cbcSMatt Macy  * mechanism.   The first level identifies the "class" of
29eda14cbcSMatt Macy  * entry.  The class is identified by the first 2 letters of
30eda14cbcSMatt Macy  * the attribute.  The second letter "l" or "d" identifies whether
31eda14cbcSMatt Macy  * it is a local or descendent permission.  The first letter
32eda14cbcSMatt Macy  * identifies the type of entry.
33eda14cbcSMatt Macy  *
34eda14cbcSMatt Macy  * ul$<id>    identifies permissions granted locally for this userid.
35eda14cbcSMatt Macy  * ud$<id>    identifies permissions granted on descendent datasets for
36eda14cbcSMatt Macy  *            this userid.
37eda14cbcSMatt Macy  * Ul$<id>    identifies permission sets granted locally for this userid.
38eda14cbcSMatt Macy  * Ud$<id>    identifies permission sets granted on descendent datasets for
39eda14cbcSMatt Macy  *            this userid.
40eda14cbcSMatt Macy  * gl$<id>    identifies permissions granted locally for this groupid.
41eda14cbcSMatt Macy  * gd$<id>    identifies permissions granted on descendent datasets for
42eda14cbcSMatt Macy  *            this groupid.
43eda14cbcSMatt Macy  * Gl$<id>    identifies permission sets granted locally for this groupid.
44eda14cbcSMatt Macy  * Gd$<id>    identifies permission sets granted on descendent datasets for
45eda14cbcSMatt Macy  *            this groupid.
46eda14cbcSMatt Macy  * el$        identifies permissions granted locally for everyone.
47eda14cbcSMatt Macy  * ed$        identifies permissions granted on descendent datasets
48eda14cbcSMatt Macy  *            for everyone.
49eda14cbcSMatt Macy  * El$        identifies permission sets granted locally for everyone.
50eda14cbcSMatt Macy  * Ed$        identifies permission sets granted to descendent datasets for
51eda14cbcSMatt Macy  *            everyone.
52eda14cbcSMatt Macy  * c-$        identifies permission to create at dataset creation time.
53eda14cbcSMatt Macy  * C-$        identifies permission sets to grant locally at dataset creation
54eda14cbcSMatt Macy  *            time.
55eda14cbcSMatt Macy  * s-$@<name> permissions defined in specified set @<name>
56eda14cbcSMatt Macy  * S-$@<name> Sets defined in named set @<name>
57eda14cbcSMatt Macy  *
58eda14cbcSMatt Macy  * Each of the above entities points to another zap attribute that contains one
59eda14cbcSMatt Macy  * attribute for each allowed permission, such as create, destroy,...
60eda14cbcSMatt Macy  * All of the "upper" case class types will specify permission set names
61eda14cbcSMatt Macy  * rather than permissions.
62eda14cbcSMatt Macy  *
63eda14cbcSMatt Macy  * Basically it looks something like this:
64eda14cbcSMatt Macy  * ul$12 -> ZAP OBJ -> permissions...
65eda14cbcSMatt Macy  *
66eda14cbcSMatt Macy  * The ZAP OBJ is referred to as the jump object.
67eda14cbcSMatt Macy  */
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy #include <sys/dmu.h>
70eda14cbcSMatt Macy #include <sys/dmu_objset.h>
71eda14cbcSMatt Macy #include <sys/dmu_tx.h>
72eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
73eda14cbcSMatt Macy #include <sys/dsl_dir.h>
74eda14cbcSMatt Macy #include <sys/dsl_prop.h>
75eda14cbcSMatt Macy #include <sys/dsl_synctask.h>
76eda14cbcSMatt Macy #include <sys/dsl_deleg.h>
77eda14cbcSMatt Macy #include <sys/spa.h>
78eda14cbcSMatt Macy #include <sys/zap.h>
79eda14cbcSMatt Macy #include <sys/fs/zfs.h>
80eda14cbcSMatt Macy #include <sys/cred.h>
81eda14cbcSMatt Macy #include <sys/sunddi.h>
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy #include "zfs_deleg.h"
84eda14cbcSMatt Macy 
85eda14cbcSMatt Macy /*
86eda14cbcSMatt Macy  * Validate that user is allowed to delegate specified permissions.
87eda14cbcSMatt Macy  *
88eda14cbcSMatt Macy  * In order to delegate "create" you must have "create"
89eda14cbcSMatt Macy  * and "allow".
90eda14cbcSMatt Macy  */
91eda14cbcSMatt Macy int
dsl_deleg_can_allow(char * ddname,nvlist_t * nvp,cred_t * cr)92eda14cbcSMatt Macy dsl_deleg_can_allow(char *ddname, nvlist_t *nvp, cred_t *cr)
93eda14cbcSMatt Macy {
94eda14cbcSMatt Macy 	nvpair_t *whopair = NULL;
95eda14cbcSMatt Macy 	int error;
96eda14cbcSMatt Macy 
97eda14cbcSMatt Macy 	if ((error = dsl_deleg_access(ddname, ZFS_DELEG_PERM_ALLOW, cr)) != 0)
98eda14cbcSMatt Macy 		return (error);
99eda14cbcSMatt Macy 
100eda14cbcSMatt Macy 	while ((whopair = nvlist_next_nvpair(nvp, whopair))) {
101eda14cbcSMatt Macy 		nvlist_t *perms;
102eda14cbcSMatt Macy 		nvpair_t *permpair = NULL;
103eda14cbcSMatt Macy 
104eda14cbcSMatt Macy 		VERIFY(nvpair_value_nvlist(whopair, &perms) == 0);
105eda14cbcSMatt Macy 
106eda14cbcSMatt Macy 		while ((permpair = nvlist_next_nvpair(perms, permpair))) {
107eda14cbcSMatt Macy 			const char *perm = nvpair_name(permpair);
108eda14cbcSMatt Macy 
109eda14cbcSMatt Macy 			if (strcmp(perm, ZFS_DELEG_PERM_ALLOW) == 0)
110eda14cbcSMatt Macy 				return (SET_ERROR(EPERM));
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy 			if ((error = dsl_deleg_access(ddname, perm, cr)) != 0)
113eda14cbcSMatt Macy 				return (error);
114eda14cbcSMatt Macy 		}
115eda14cbcSMatt Macy 	}
116eda14cbcSMatt Macy 	return (0);
117eda14cbcSMatt Macy }
118eda14cbcSMatt Macy 
119eda14cbcSMatt Macy /*
120eda14cbcSMatt Macy  * Validate that user is allowed to unallow specified permissions.  They
121eda14cbcSMatt Macy  * must have the 'allow' permission, and even then can only unallow
122eda14cbcSMatt Macy  * perms for their uid.
123eda14cbcSMatt Macy  */
124eda14cbcSMatt Macy int
dsl_deleg_can_unallow(char * ddname,nvlist_t * nvp,cred_t * cr)125eda14cbcSMatt Macy dsl_deleg_can_unallow(char *ddname, nvlist_t *nvp, cred_t *cr)
126eda14cbcSMatt Macy {
127eda14cbcSMatt Macy 	nvpair_t *whopair = NULL;
128eda14cbcSMatt Macy 	int error;
129eda14cbcSMatt Macy 	char idstr[32];
130eda14cbcSMatt Macy 
131eda14cbcSMatt Macy 	if ((error = dsl_deleg_access(ddname, ZFS_DELEG_PERM_ALLOW, cr)) != 0)
132eda14cbcSMatt Macy 		return (error);
133eda14cbcSMatt Macy 
134eda14cbcSMatt Macy 	(void) snprintf(idstr, sizeof (idstr), "%lld",
135eda14cbcSMatt Macy 	    (longlong_t)crgetuid(cr));
136eda14cbcSMatt Macy 
137eda14cbcSMatt Macy 	while ((whopair = nvlist_next_nvpair(nvp, whopair))) {
138eda14cbcSMatt Macy 		zfs_deleg_who_type_t type = nvpair_name(whopair)[0];
139eda14cbcSMatt Macy 
140eda14cbcSMatt Macy 		if (type != ZFS_DELEG_USER &&
141eda14cbcSMatt Macy 		    type != ZFS_DELEG_USER_SETS)
142eda14cbcSMatt Macy 			return (SET_ERROR(EPERM));
143eda14cbcSMatt Macy 
144eda14cbcSMatt Macy 		if (strcmp(idstr, &nvpair_name(whopair)[3]) != 0)
145eda14cbcSMatt Macy 			return (SET_ERROR(EPERM));
146eda14cbcSMatt Macy 	}
147eda14cbcSMatt Macy 	return (0);
148eda14cbcSMatt Macy }
149eda14cbcSMatt Macy 
150eda14cbcSMatt Macy typedef struct dsl_deleg_arg {
151eda14cbcSMatt Macy 	const char *dda_name;
152eda14cbcSMatt Macy 	nvlist_t *dda_nvlist;
153eda14cbcSMatt Macy } dsl_deleg_arg_t;
154eda14cbcSMatt Macy 
155eda14cbcSMatt Macy static void
dsl_deleg_set_sync(void * arg,dmu_tx_t * tx)156eda14cbcSMatt Macy dsl_deleg_set_sync(void *arg, dmu_tx_t *tx)
157eda14cbcSMatt Macy {
158eda14cbcSMatt Macy 	dsl_deleg_arg_t *dda = arg;
159eda14cbcSMatt Macy 	dsl_dir_t *dd;
160eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
161eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
162eda14cbcSMatt Macy 	nvpair_t *whopair = NULL;
163eda14cbcSMatt Macy 	uint64_t zapobj;
164eda14cbcSMatt Macy 
165eda14cbcSMatt Macy 	VERIFY0(dsl_dir_hold(dp, dda->dda_name, FTAG, &dd, NULL));
166eda14cbcSMatt Macy 
167eda14cbcSMatt Macy 	zapobj = dsl_dir_phys(dd)->dd_deleg_zapobj;
168eda14cbcSMatt Macy 	if (zapobj == 0) {
169eda14cbcSMatt Macy 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
170eda14cbcSMatt Macy 		zapobj = dsl_dir_phys(dd)->dd_deleg_zapobj = zap_create(mos,
171eda14cbcSMatt Macy 		    DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
172eda14cbcSMatt Macy 	}
173eda14cbcSMatt Macy 
174eda14cbcSMatt Macy 	while ((whopair = nvlist_next_nvpair(dda->dda_nvlist, whopair))) {
175eda14cbcSMatt Macy 		const char *whokey = nvpair_name(whopair);
176eda14cbcSMatt Macy 		nvlist_t *perms;
177eda14cbcSMatt Macy 		nvpair_t *permpair = NULL;
178eda14cbcSMatt Macy 		uint64_t jumpobj;
179eda14cbcSMatt Macy 
180eda14cbcSMatt Macy 		perms = fnvpair_value_nvlist(whopair);
181eda14cbcSMatt Macy 
182eda14cbcSMatt Macy 		if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) != 0) {
183eda14cbcSMatt Macy 			jumpobj = zap_create_link(mos, DMU_OT_DSL_PERMS,
184eda14cbcSMatt Macy 			    zapobj, whokey, tx);
185eda14cbcSMatt Macy 		}
186eda14cbcSMatt Macy 
187eda14cbcSMatt Macy 		while ((permpair = nvlist_next_nvpair(perms, permpair))) {
188eda14cbcSMatt Macy 			const char *perm = nvpair_name(permpair);
189eda14cbcSMatt Macy 			uint64_t n = 0;
190eda14cbcSMatt Macy 
191eda14cbcSMatt Macy 			VERIFY(zap_update(mos, jumpobj,
192eda14cbcSMatt Macy 			    perm, 8, 1, &n, tx) == 0);
193eda14cbcSMatt Macy 			spa_history_log_internal_dd(dd, "permission update", tx,
194eda14cbcSMatt Macy 			    "%s %s", whokey, perm);
195eda14cbcSMatt Macy 		}
196eda14cbcSMatt Macy 	}
197eda14cbcSMatt Macy 	dsl_dir_rele(dd, FTAG);
198eda14cbcSMatt Macy }
199eda14cbcSMatt Macy 
200eda14cbcSMatt Macy static void
dsl_deleg_unset_sync(void * arg,dmu_tx_t * tx)201eda14cbcSMatt Macy dsl_deleg_unset_sync(void *arg, dmu_tx_t *tx)
202eda14cbcSMatt Macy {
203eda14cbcSMatt Macy 	dsl_deleg_arg_t *dda = arg;
204eda14cbcSMatt Macy 	dsl_dir_t *dd;
205eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
206eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
207eda14cbcSMatt Macy 	nvpair_t *whopair = NULL;
208eda14cbcSMatt Macy 	uint64_t zapobj;
209eda14cbcSMatt Macy 
210eda14cbcSMatt Macy 	VERIFY0(dsl_dir_hold(dp, dda->dda_name, FTAG, &dd, NULL));
211eda14cbcSMatt Macy 	zapobj = dsl_dir_phys(dd)->dd_deleg_zapobj;
212eda14cbcSMatt Macy 	if (zapobj == 0) {
213eda14cbcSMatt Macy 		dsl_dir_rele(dd, FTAG);
214eda14cbcSMatt Macy 		return;
215eda14cbcSMatt Macy 	}
216eda14cbcSMatt Macy 
217eda14cbcSMatt Macy 	while ((whopair = nvlist_next_nvpair(dda->dda_nvlist, whopair))) {
218eda14cbcSMatt Macy 		const char *whokey = nvpair_name(whopair);
219eda14cbcSMatt Macy 		nvlist_t *perms;
220eda14cbcSMatt Macy 		nvpair_t *permpair = NULL;
221eda14cbcSMatt Macy 		uint64_t jumpobj;
222eda14cbcSMatt Macy 
223eda14cbcSMatt Macy 		if (nvpair_value_nvlist(whopair, &perms) != 0) {
224eda14cbcSMatt Macy 			if (zap_lookup(mos, zapobj, whokey, 8,
225eda14cbcSMatt Macy 			    1, &jumpobj) == 0) {
226eda14cbcSMatt Macy 				(void) zap_remove(mos, zapobj, whokey, tx);
227eda14cbcSMatt Macy 				VERIFY(0 == zap_destroy(mos, jumpobj, tx));
228eda14cbcSMatt Macy 			}
229eda14cbcSMatt Macy 			spa_history_log_internal_dd(dd, "permission who remove",
230eda14cbcSMatt Macy 			    tx, "%s", whokey);
231eda14cbcSMatt Macy 			continue;
232eda14cbcSMatt Macy 		}
233eda14cbcSMatt Macy 
234eda14cbcSMatt Macy 		if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) != 0)
235eda14cbcSMatt Macy 			continue;
236eda14cbcSMatt Macy 
237eda14cbcSMatt Macy 		while ((permpair = nvlist_next_nvpair(perms, permpair))) {
238eda14cbcSMatt Macy 			const char *perm = nvpair_name(permpair);
239eda14cbcSMatt Macy 			uint64_t n = 0;
240eda14cbcSMatt Macy 
241eda14cbcSMatt Macy 			(void) zap_remove(mos, jumpobj, perm, tx);
242eda14cbcSMatt Macy 			if (zap_count(mos, jumpobj, &n) == 0 && n == 0) {
243eda14cbcSMatt Macy 				(void) zap_remove(mos, zapobj,
244eda14cbcSMatt Macy 				    whokey, tx);
245eda14cbcSMatt Macy 				VERIFY(0 == zap_destroy(mos,
246eda14cbcSMatt Macy 				    jumpobj, tx));
247eda14cbcSMatt Macy 			}
248eda14cbcSMatt Macy 			spa_history_log_internal_dd(dd, "permission remove", tx,
249eda14cbcSMatt Macy 			    "%s %s", whokey, perm);
250eda14cbcSMatt Macy 		}
251eda14cbcSMatt Macy 	}
252eda14cbcSMatt Macy 	dsl_dir_rele(dd, FTAG);
253eda14cbcSMatt Macy }
254eda14cbcSMatt Macy 
255eda14cbcSMatt Macy static int
dsl_deleg_check(void * arg,dmu_tx_t * tx)256eda14cbcSMatt Macy dsl_deleg_check(void *arg, dmu_tx_t *tx)
257eda14cbcSMatt Macy {
258eda14cbcSMatt Macy 	dsl_deleg_arg_t *dda = arg;
259eda14cbcSMatt Macy 	dsl_dir_t *dd;
260eda14cbcSMatt Macy 	int error;
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy 	if (spa_version(dmu_tx_pool(tx)->dp_spa) <
263eda14cbcSMatt Macy 	    SPA_VERSION_DELEGATED_PERMS) {
264eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
265eda14cbcSMatt Macy 	}
266eda14cbcSMatt Macy 
267eda14cbcSMatt Macy 	error = dsl_dir_hold(dmu_tx_pool(tx), dda->dda_name, FTAG, &dd, NULL);
268eda14cbcSMatt Macy 	if (error == 0)
269eda14cbcSMatt Macy 		dsl_dir_rele(dd, FTAG);
270eda14cbcSMatt Macy 	return (error);
271eda14cbcSMatt Macy }
272eda14cbcSMatt Macy 
273eda14cbcSMatt Macy int
dsl_deleg_set(const char * ddname,nvlist_t * nvp,boolean_t unset)274eda14cbcSMatt Macy dsl_deleg_set(const char *ddname, nvlist_t *nvp, boolean_t unset)
275eda14cbcSMatt Macy {
276eda14cbcSMatt Macy 	dsl_deleg_arg_t dda;
277eda14cbcSMatt Macy 
278eda14cbcSMatt Macy 	/* nvp must already have been verified to be valid */
279eda14cbcSMatt Macy 
280eda14cbcSMatt Macy 	dda.dda_name = ddname;
281eda14cbcSMatt Macy 	dda.dda_nvlist = nvp;
282eda14cbcSMatt Macy 
283eda14cbcSMatt Macy 	return (dsl_sync_task(ddname, dsl_deleg_check,
284eda14cbcSMatt Macy 	    unset ? dsl_deleg_unset_sync : dsl_deleg_set_sync,
285eda14cbcSMatt Macy 	    &dda, fnvlist_num_pairs(nvp), ZFS_SPACE_CHECK_RESERVED));
286eda14cbcSMatt Macy }
287eda14cbcSMatt Macy 
288eda14cbcSMatt Macy /*
289eda14cbcSMatt Macy  * Find all 'allow' permissions from a given point and then continue
290eda14cbcSMatt Macy  * traversing up to the root.
291eda14cbcSMatt Macy  *
292eda14cbcSMatt Macy  * This function constructs an nvlist of nvlists.
293eda14cbcSMatt Macy  * each setpoint is an nvlist composed of an nvlist of an nvlist
294eda14cbcSMatt Macy  * of the individual * users/groups/everyone/create
295eda14cbcSMatt Macy  * permissions.
296eda14cbcSMatt Macy  *
297eda14cbcSMatt Macy  * The nvlist will look like this.
298eda14cbcSMatt Macy  *
299eda14cbcSMatt Macy  * { source fsname -> { whokeys { permissions,...}, ...}}
300eda14cbcSMatt Macy  *
301eda14cbcSMatt Macy  * The fsname nvpairs will be arranged in a bottom up order.  For example,
302eda14cbcSMatt Macy  * if we have the following structure a/b/c then the nvpairs for the fsnames
303eda14cbcSMatt Macy  * will be ordered a/b/c, a/b, a.
304eda14cbcSMatt Macy  */
305eda14cbcSMatt Macy int
dsl_deleg_get(const char * ddname,nvlist_t ** nvp)306eda14cbcSMatt Macy dsl_deleg_get(const char *ddname, nvlist_t **nvp)
307eda14cbcSMatt Macy {
308eda14cbcSMatt Macy 	dsl_dir_t *dd, *startdd;
309eda14cbcSMatt Macy 	dsl_pool_t *dp;
310eda14cbcSMatt Macy 	int error;
311eda14cbcSMatt Macy 	objset_t *mos;
312eda14cbcSMatt Macy 	zap_cursor_t *basezc, *zc;
313eda14cbcSMatt Macy 	zap_attribute_t *baseza, *za;
314eda14cbcSMatt Macy 	char *source;
315eda14cbcSMatt Macy 
316eda14cbcSMatt Macy 	error = dsl_pool_hold(ddname, FTAG, &dp);
317eda14cbcSMatt Macy 	if (error != 0)
318eda14cbcSMatt Macy 		return (error);
319eda14cbcSMatt Macy 
320eda14cbcSMatt Macy 	error = dsl_dir_hold(dp, ddname, FTAG, &startdd, NULL);
321eda14cbcSMatt Macy 	if (error != 0) {
322eda14cbcSMatt Macy 		dsl_pool_rele(dp, FTAG);
323eda14cbcSMatt Macy 		return (error);
324eda14cbcSMatt Macy 	}
325eda14cbcSMatt Macy 
326eda14cbcSMatt Macy 	dp = startdd->dd_pool;
327eda14cbcSMatt Macy 	mos = dp->dp_meta_objset;
328eda14cbcSMatt Macy 
329eda14cbcSMatt Macy 	zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
330*7a7741afSMartin Matuska 	za = zap_attribute_alloc();
331eda14cbcSMatt Macy 	basezc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
332*7a7741afSMartin Matuska 	baseza = zap_attribute_alloc();
333eda14cbcSMatt Macy 	source = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
334eda14cbcSMatt Macy 	VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
335eda14cbcSMatt Macy 
336eda14cbcSMatt Macy 	for (dd = startdd; dd != NULL; dd = dd->dd_parent) {
337eda14cbcSMatt Macy 		nvlist_t *sp_nvp;
338eda14cbcSMatt Macy 		uint64_t n;
339eda14cbcSMatt Macy 
340eda14cbcSMatt Macy 		if (dsl_dir_phys(dd)->dd_deleg_zapobj == 0 ||
341eda14cbcSMatt Macy 		    zap_count(mos,
342eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_deleg_zapobj, &n) != 0 || n == 0)
343eda14cbcSMatt Macy 			continue;
344eda14cbcSMatt Macy 
345eda14cbcSMatt Macy 		sp_nvp = fnvlist_alloc();
346eda14cbcSMatt Macy 		for (zap_cursor_init(basezc, mos,
347eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_deleg_zapobj);
348eda14cbcSMatt Macy 		    zap_cursor_retrieve(basezc, baseza) == 0;
349eda14cbcSMatt Macy 		    zap_cursor_advance(basezc)) {
350eda14cbcSMatt Macy 			nvlist_t *perms_nvp;
351eda14cbcSMatt Macy 
352eda14cbcSMatt Macy 			ASSERT(baseza->za_integer_length == 8);
353eda14cbcSMatt Macy 			ASSERT(baseza->za_num_integers == 1);
354eda14cbcSMatt Macy 
355eda14cbcSMatt Macy 			perms_nvp = fnvlist_alloc();
356eda14cbcSMatt Macy 			for (zap_cursor_init(zc, mos, baseza->za_first_integer);
357eda14cbcSMatt Macy 			    zap_cursor_retrieve(zc, za) == 0;
358eda14cbcSMatt Macy 			    zap_cursor_advance(zc)) {
359eda14cbcSMatt Macy 				fnvlist_add_boolean(perms_nvp, za->za_name);
360eda14cbcSMatt Macy 			}
361eda14cbcSMatt Macy 			zap_cursor_fini(zc);
362eda14cbcSMatt Macy 			fnvlist_add_nvlist(sp_nvp, baseza->za_name, perms_nvp);
363eda14cbcSMatt Macy 			fnvlist_free(perms_nvp);
364eda14cbcSMatt Macy 		}
365eda14cbcSMatt Macy 
366eda14cbcSMatt Macy 		zap_cursor_fini(basezc);
367eda14cbcSMatt Macy 
368eda14cbcSMatt Macy 		dsl_dir_name(dd, source);
369eda14cbcSMatt Macy 		fnvlist_add_nvlist(*nvp, source, sp_nvp);
370eda14cbcSMatt Macy 		nvlist_free(sp_nvp);
371eda14cbcSMatt Macy 	}
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy 	kmem_free(source, ZFS_MAX_DATASET_NAME_LEN);
374*7a7741afSMartin Matuska 	zap_attribute_free(baseza);
375eda14cbcSMatt Macy 	kmem_free(basezc, sizeof (zap_cursor_t));
376*7a7741afSMartin Matuska 	zap_attribute_free(za);
377eda14cbcSMatt Macy 	kmem_free(zc, sizeof (zap_cursor_t));
378eda14cbcSMatt Macy 
379eda14cbcSMatt Macy 	dsl_dir_rele(startdd, FTAG);
380eda14cbcSMatt Macy 	dsl_pool_rele(dp, FTAG);
381eda14cbcSMatt Macy 	return (0);
382eda14cbcSMatt Macy }
383eda14cbcSMatt Macy 
384eda14cbcSMatt Macy /*
385eda14cbcSMatt Macy  * Routines for dsl_deleg_access() -- access checking.
386eda14cbcSMatt Macy  */
387eda14cbcSMatt Macy typedef struct perm_set {
388eda14cbcSMatt Macy 	avl_node_t	p_node;
389eda14cbcSMatt Macy 	boolean_t	p_matched;
390eda14cbcSMatt Macy 	char		p_setname[ZFS_MAX_DELEG_NAME];
391eda14cbcSMatt Macy } perm_set_t;
392eda14cbcSMatt Macy 
393eda14cbcSMatt Macy static int
perm_set_compare(const void * arg1,const void * arg2)394eda14cbcSMatt Macy perm_set_compare(const void *arg1, const void *arg2)
395eda14cbcSMatt Macy {
396eda14cbcSMatt Macy 	const perm_set_t *node1 = (const perm_set_t *)arg1;
397eda14cbcSMatt Macy 	const perm_set_t *node2 = (const perm_set_t *)arg2;
398eda14cbcSMatt Macy 	int val;
399eda14cbcSMatt Macy 
400eda14cbcSMatt Macy 	val = strcmp(node1->p_setname, node2->p_setname);
401eda14cbcSMatt Macy 
402eda14cbcSMatt Macy 	return (TREE_ISIGN(val));
403eda14cbcSMatt Macy }
404eda14cbcSMatt Macy 
405eda14cbcSMatt Macy /*
406eda14cbcSMatt Macy  * Determine whether a specified permission exists.
407eda14cbcSMatt Macy  *
408eda14cbcSMatt Macy  * First the base attribute has to be retrieved.  i.e. ul$12
409eda14cbcSMatt Macy  * Once the base object has been retrieved the actual permission
410eda14cbcSMatt Macy  * is lookup up in the zap object the base object points to.
411eda14cbcSMatt Macy  *
412eda14cbcSMatt Macy  * Return 0 if permission exists, ENOENT if there is no whokey, EPERM if
413eda14cbcSMatt Macy  * there is no perm in that jumpobj.
414eda14cbcSMatt Macy  */
415eda14cbcSMatt Macy static int
dsl_check_access(objset_t * mos,uint64_t zapobj,char type,char checkflag,void * valp,const char * perm)416eda14cbcSMatt Macy dsl_check_access(objset_t *mos, uint64_t zapobj,
417eda14cbcSMatt Macy     char type, char checkflag, void *valp, const char *perm)
418eda14cbcSMatt Macy {
419eda14cbcSMatt Macy 	int error;
420eda14cbcSMatt Macy 	uint64_t jumpobj, zero;
421eda14cbcSMatt Macy 	char whokey[ZFS_MAX_DELEG_NAME];
422eda14cbcSMatt Macy 
423eda14cbcSMatt Macy 	zfs_deleg_whokey(whokey, type, checkflag, valp);
424eda14cbcSMatt Macy 	error = zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj);
425eda14cbcSMatt Macy 	if (error == 0) {
426eda14cbcSMatt Macy 		error = zap_lookup(mos, jumpobj, perm, 8, 1, &zero);
427eda14cbcSMatt Macy 		if (error == ENOENT)
428eda14cbcSMatt Macy 			error = SET_ERROR(EPERM);
429eda14cbcSMatt Macy 	}
430eda14cbcSMatt Macy 	return (error);
431eda14cbcSMatt Macy }
432eda14cbcSMatt Macy 
433eda14cbcSMatt Macy /*
434eda14cbcSMatt Macy  * check a specified user/group for a requested permission
435eda14cbcSMatt Macy  */
436eda14cbcSMatt Macy static int
dsl_check_user_access(objset_t * mos,uint64_t zapobj,const char * perm,int checkflag,cred_t * cr)437eda14cbcSMatt Macy dsl_check_user_access(objset_t *mos, uint64_t zapobj, const char *perm,
438eda14cbcSMatt Macy     int checkflag, cred_t *cr)
439eda14cbcSMatt Macy {
440eda14cbcSMatt Macy 	const	gid_t *gids;
441eda14cbcSMatt Macy 	int	ngids;
442eda14cbcSMatt Macy 	int	i;
443eda14cbcSMatt Macy 	uint64_t id;
444eda14cbcSMatt Macy 
445eda14cbcSMatt Macy 	/* check for user */
446eda14cbcSMatt Macy 	id = crgetuid(cr);
447eda14cbcSMatt Macy 	if (dsl_check_access(mos, zapobj,
448eda14cbcSMatt Macy 	    ZFS_DELEG_USER, checkflag, &id, perm) == 0)
449eda14cbcSMatt Macy 		return (0);
450eda14cbcSMatt Macy 
451eda14cbcSMatt Macy 	/* check for users primary group */
452eda14cbcSMatt Macy 	id = crgetgid(cr);
453eda14cbcSMatt Macy 	if (dsl_check_access(mos, zapobj,
454eda14cbcSMatt Macy 	    ZFS_DELEG_GROUP, checkflag, &id, perm) == 0)
455eda14cbcSMatt Macy 		return (0);
456eda14cbcSMatt Macy 
457eda14cbcSMatt Macy 	/* check for everyone entry */
458eda14cbcSMatt Macy 	id = -1;
459eda14cbcSMatt Macy 	if (dsl_check_access(mos, zapobj,
460eda14cbcSMatt Macy 	    ZFS_DELEG_EVERYONE, checkflag, &id, perm) == 0)
461eda14cbcSMatt Macy 		return (0);
462eda14cbcSMatt Macy 
463eda14cbcSMatt Macy 	/* check each supplemental group user is a member of */
464eda14cbcSMatt Macy 	ngids = crgetngroups(cr);
465eda14cbcSMatt Macy 	gids = crgetgroups(cr);
466eda14cbcSMatt Macy 	for (i = 0; i != ngids; i++) {
467eda14cbcSMatt Macy 		id = gids[i];
468eda14cbcSMatt Macy 		if (dsl_check_access(mos, zapobj,
469eda14cbcSMatt Macy 		    ZFS_DELEG_GROUP, checkflag, &id, perm) == 0)
470eda14cbcSMatt Macy 			return (0);
471eda14cbcSMatt Macy 	}
472eda14cbcSMatt Macy 
473eda14cbcSMatt Macy 	return (SET_ERROR(EPERM));
474eda14cbcSMatt Macy }
475eda14cbcSMatt Macy 
476eda14cbcSMatt Macy /*
477eda14cbcSMatt Macy  * Iterate over the sets specified in the specified zapobj
478eda14cbcSMatt Macy  * and load them into the permsets avl tree.
479eda14cbcSMatt Macy  */
480eda14cbcSMatt Macy static int
dsl_load_sets(objset_t * mos,uint64_t zapobj,char type,char checkflag,void * valp,avl_tree_t * avl)481eda14cbcSMatt Macy dsl_load_sets(objset_t *mos, uint64_t zapobj,
482eda14cbcSMatt Macy     char type, char checkflag, void *valp, avl_tree_t *avl)
483eda14cbcSMatt Macy {
484eda14cbcSMatt Macy 	zap_cursor_t zc;
485*7a7741afSMartin Matuska 	zap_attribute_t *za;
486eda14cbcSMatt Macy 	perm_set_t *permnode;
487eda14cbcSMatt Macy 	avl_index_t idx;
488eda14cbcSMatt Macy 	uint64_t jumpobj;
489eda14cbcSMatt Macy 	int error;
490eda14cbcSMatt Macy 	char whokey[ZFS_MAX_DELEG_NAME];
491eda14cbcSMatt Macy 
492eda14cbcSMatt Macy 	zfs_deleg_whokey(whokey, type, checkflag, valp);
493eda14cbcSMatt Macy 
494eda14cbcSMatt Macy 	error = zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj);
495eda14cbcSMatt Macy 	if (error != 0)
496eda14cbcSMatt Macy 		return (error);
497eda14cbcSMatt Macy 
498*7a7741afSMartin Matuska 	za = zap_attribute_alloc();
499eda14cbcSMatt Macy 	for (zap_cursor_init(&zc, mos, jumpobj);
500*7a7741afSMartin Matuska 	    zap_cursor_retrieve(&zc, za) == 0;
501eda14cbcSMatt Macy 	    zap_cursor_advance(&zc)) {
502eda14cbcSMatt Macy 		permnode = kmem_alloc(sizeof (perm_set_t), KM_SLEEP);
503*7a7741afSMartin Matuska 		(void) strlcpy(permnode->p_setname, za->za_name,
504eda14cbcSMatt Macy 		    sizeof (permnode->p_setname));
505eda14cbcSMatt Macy 		permnode->p_matched = B_FALSE;
506eda14cbcSMatt Macy 
507eda14cbcSMatt Macy 		if (avl_find(avl, permnode, &idx) == NULL) {
508eda14cbcSMatt Macy 			avl_insert(avl, permnode, idx);
509eda14cbcSMatt Macy 		} else {
510eda14cbcSMatt Macy 			kmem_free(permnode, sizeof (perm_set_t));
511eda14cbcSMatt Macy 		}
512eda14cbcSMatt Macy 	}
513eda14cbcSMatt Macy 	zap_cursor_fini(&zc);
514*7a7741afSMartin Matuska 	zap_attribute_free(za);
515eda14cbcSMatt Macy 	return (0);
516eda14cbcSMatt Macy }
517eda14cbcSMatt Macy 
518eda14cbcSMatt Macy /*
519eda14cbcSMatt Macy  * Load all permissions user based on cred belongs to.
520eda14cbcSMatt Macy  */
521eda14cbcSMatt Macy static void
dsl_load_user_sets(objset_t * mos,uint64_t zapobj,avl_tree_t * avl,char checkflag,cred_t * cr)522eda14cbcSMatt Macy dsl_load_user_sets(objset_t *mos, uint64_t zapobj, avl_tree_t *avl,
523eda14cbcSMatt Macy     char checkflag, cred_t *cr)
524eda14cbcSMatt Macy {
525eda14cbcSMatt Macy 	const	gid_t *gids;
526eda14cbcSMatt Macy 	int	ngids, i;
527eda14cbcSMatt Macy 	uint64_t id;
528eda14cbcSMatt Macy 
529eda14cbcSMatt Macy 	id = crgetuid(cr);
530eda14cbcSMatt Macy 	(void) dsl_load_sets(mos, zapobj,
531eda14cbcSMatt Macy 	    ZFS_DELEG_USER_SETS, checkflag, &id, avl);
532eda14cbcSMatt Macy 
533eda14cbcSMatt Macy 	id = crgetgid(cr);
534eda14cbcSMatt Macy 	(void) dsl_load_sets(mos, zapobj,
535eda14cbcSMatt Macy 	    ZFS_DELEG_GROUP_SETS, checkflag, &id, avl);
536eda14cbcSMatt Macy 
537eda14cbcSMatt Macy 	(void) dsl_load_sets(mos, zapobj,
538eda14cbcSMatt Macy 	    ZFS_DELEG_EVERYONE_SETS, checkflag, NULL, avl);
539eda14cbcSMatt Macy 
540eda14cbcSMatt Macy 	ngids = crgetngroups(cr);
541eda14cbcSMatt Macy 	gids = crgetgroups(cr);
542eda14cbcSMatt Macy 	for (i = 0; i != ngids; i++) {
543eda14cbcSMatt Macy 		id = gids[i];
544eda14cbcSMatt Macy 		(void) dsl_load_sets(mos, zapobj,
545eda14cbcSMatt Macy 		    ZFS_DELEG_GROUP_SETS, checkflag, &id, avl);
546eda14cbcSMatt Macy 	}
547eda14cbcSMatt Macy }
548eda14cbcSMatt Macy 
549eda14cbcSMatt Macy /*
550eda14cbcSMatt Macy  * Check if user has requested permission.
551eda14cbcSMatt Macy  */
552eda14cbcSMatt Macy int
dsl_deleg_access_impl(dsl_dataset_t * ds,const char * perm,cred_t * cr)553eda14cbcSMatt Macy dsl_deleg_access_impl(dsl_dataset_t *ds, const char *perm, cred_t *cr)
554eda14cbcSMatt Macy {
555eda14cbcSMatt Macy 	dsl_dir_t *dd;
556eda14cbcSMatt Macy 	dsl_pool_t *dp;
557eda14cbcSMatt Macy 	void *cookie;
558eda14cbcSMatt Macy 	int	error;
559eda14cbcSMatt Macy 	char	checkflag;
560eda14cbcSMatt Macy 	objset_t *mos;
561eda14cbcSMatt Macy 	avl_tree_t permsets;
562eda14cbcSMatt Macy 	perm_set_t *setnode;
563eda14cbcSMatt Macy 
564eda14cbcSMatt Macy 	dp = ds->ds_dir->dd_pool;
565eda14cbcSMatt Macy 	mos = dp->dp_meta_objset;
566eda14cbcSMatt Macy 
567eda14cbcSMatt Macy 	if (dsl_delegation_on(mos) == B_FALSE)
568eda14cbcSMatt Macy 		return (SET_ERROR(ECANCELED));
569eda14cbcSMatt Macy 
570eda14cbcSMatt Macy 	if (spa_version(dmu_objset_spa(dp->dp_meta_objset)) <
571eda14cbcSMatt Macy 	    SPA_VERSION_DELEGATED_PERMS)
572eda14cbcSMatt Macy 		return (SET_ERROR(EPERM));
573eda14cbcSMatt Macy 
574eda14cbcSMatt Macy 	if (ds->ds_is_snapshot) {
575eda14cbcSMatt Macy 		/*
576eda14cbcSMatt Macy 		 * Snapshots are treated as descendents only,
577eda14cbcSMatt Macy 		 * local permissions do not apply.
578eda14cbcSMatt Macy 		 */
579eda14cbcSMatt Macy 		checkflag = ZFS_DELEG_DESCENDENT;
580eda14cbcSMatt Macy 	} else {
581eda14cbcSMatt Macy 		checkflag = ZFS_DELEG_LOCAL;
582eda14cbcSMatt Macy 	}
583eda14cbcSMatt Macy 
584eda14cbcSMatt Macy 	avl_create(&permsets, perm_set_compare, sizeof (perm_set_t),
585eda14cbcSMatt Macy 	    offsetof(perm_set_t, p_node));
586eda14cbcSMatt Macy 
587eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
588eda14cbcSMatt Macy 	for (dd = ds->ds_dir; dd != NULL; dd = dd->dd_parent,
589eda14cbcSMatt Macy 	    checkflag = ZFS_DELEG_DESCENDENT) {
590eda14cbcSMatt Macy 		uint64_t zapobj;
591eda14cbcSMatt Macy 		boolean_t expanded;
592eda14cbcSMatt Macy 
593eda14cbcSMatt Macy 		/*
594eda14cbcSMatt Macy 		 * If not in global zone then make sure
595eda14cbcSMatt Macy 		 * the zoned property is set
596eda14cbcSMatt Macy 		 */
597eda14cbcSMatt Macy 		if (!INGLOBALZONE(curproc)) {
598eda14cbcSMatt Macy 			uint64_t zoned;
599eda14cbcSMatt Macy 
600eda14cbcSMatt Macy 			if (dsl_prop_get_dd(dd,
601eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_ZONED),
602eda14cbcSMatt Macy 			    8, 1, &zoned, NULL, B_FALSE) != 0)
603eda14cbcSMatt Macy 				break;
604eda14cbcSMatt Macy 			if (!zoned)
605eda14cbcSMatt Macy 				break;
606eda14cbcSMatt Macy 		}
607eda14cbcSMatt Macy 		zapobj = dsl_dir_phys(dd)->dd_deleg_zapobj;
608eda14cbcSMatt Macy 
609eda14cbcSMatt Macy 		if (zapobj == 0)
610eda14cbcSMatt Macy 			continue;
611eda14cbcSMatt Macy 
612eda14cbcSMatt Macy 		dsl_load_user_sets(mos, zapobj, &permsets, checkflag, cr);
613eda14cbcSMatt Macy again:
614eda14cbcSMatt Macy 		expanded = B_FALSE;
615eda14cbcSMatt Macy 		for (setnode = avl_first(&permsets); setnode;
616eda14cbcSMatt Macy 		    setnode = AVL_NEXT(&permsets, setnode)) {
617eda14cbcSMatt Macy 			if (setnode->p_matched == B_TRUE)
618eda14cbcSMatt Macy 				continue;
619eda14cbcSMatt Macy 
620eda14cbcSMatt Macy 			/* See if this set directly grants this permission */
621eda14cbcSMatt Macy 			error = dsl_check_access(mos, zapobj,
622eda14cbcSMatt Macy 			    ZFS_DELEG_NAMED_SET, 0, setnode->p_setname, perm);
623eda14cbcSMatt Macy 			if (error == 0)
624eda14cbcSMatt Macy 				goto success;
625eda14cbcSMatt Macy 			if (error == EPERM)
626eda14cbcSMatt Macy 				setnode->p_matched = B_TRUE;
627eda14cbcSMatt Macy 
628eda14cbcSMatt Macy 			/* See if this set includes other sets */
629eda14cbcSMatt Macy 			error = dsl_load_sets(mos, zapobj,
630eda14cbcSMatt Macy 			    ZFS_DELEG_NAMED_SET_SETS, 0,
631eda14cbcSMatt Macy 			    setnode->p_setname, &permsets);
632eda14cbcSMatt Macy 			if (error == 0)
633eda14cbcSMatt Macy 				setnode->p_matched = expanded = B_TRUE;
634eda14cbcSMatt Macy 		}
635eda14cbcSMatt Macy 		/*
636eda14cbcSMatt Macy 		 * If we expanded any sets, that will define more sets,
637eda14cbcSMatt Macy 		 * which we need to check.
638eda14cbcSMatt Macy 		 */
639eda14cbcSMatt Macy 		if (expanded)
640eda14cbcSMatt Macy 			goto again;
641eda14cbcSMatt Macy 
642eda14cbcSMatt Macy 		error = dsl_check_user_access(mos, zapobj, perm, checkflag, cr);
643eda14cbcSMatt Macy 		if (error == 0)
644eda14cbcSMatt Macy 			goto success;
645eda14cbcSMatt Macy 	}
646eda14cbcSMatt Macy 	error = SET_ERROR(EPERM);
647eda14cbcSMatt Macy success:
648eda14cbcSMatt Macy 
649eda14cbcSMatt Macy 	cookie = NULL;
650eda14cbcSMatt Macy 	while ((setnode = avl_destroy_nodes(&permsets, &cookie)) != NULL)
651eda14cbcSMatt Macy 		kmem_free(setnode, sizeof (perm_set_t));
652eda14cbcSMatt Macy 
653eda14cbcSMatt Macy 	return (error);
654eda14cbcSMatt Macy }
655eda14cbcSMatt Macy 
656eda14cbcSMatt Macy int
dsl_deleg_access(const char * dsname,const char * perm,cred_t * cr)657eda14cbcSMatt Macy dsl_deleg_access(const char *dsname, const char *perm, cred_t *cr)
658eda14cbcSMatt Macy {
659eda14cbcSMatt Macy 	dsl_pool_t *dp;
660eda14cbcSMatt Macy 	dsl_dataset_t *ds;
661eda14cbcSMatt Macy 	int error;
662eda14cbcSMatt Macy 
663eda14cbcSMatt Macy 	error = dsl_pool_hold(dsname, FTAG, &dp);
664eda14cbcSMatt Macy 	if (error != 0)
665eda14cbcSMatt Macy 		return (error);
666eda14cbcSMatt Macy 	error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
667eda14cbcSMatt Macy 	if (error == 0) {
668eda14cbcSMatt Macy 		error = dsl_deleg_access_impl(ds, perm, cr);
669eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
670eda14cbcSMatt Macy 	}
671eda14cbcSMatt Macy 	dsl_pool_rele(dp, FTAG);
672eda14cbcSMatt Macy 
673eda14cbcSMatt Macy 	return (error);
674eda14cbcSMatt Macy }
675eda14cbcSMatt Macy 
676eda14cbcSMatt Macy /*
677eda14cbcSMatt Macy  * Other routines.
678eda14cbcSMatt Macy  */
679eda14cbcSMatt Macy 
680eda14cbcSMatt Macy static void
copy_create_perms(dsl_dir_t * dd,uint64_t pzapobj,boolean_t dosets,uint64_t uid,dmu_tx_t * tx)681eda14cbcSMatt Macy copy_create_perms(dsl_dir_t *dd, uint64_t pzapobj,
682eda14cbcSMatt Macy     boolean_t dosets, uint64_t uid, dmu_tx_t *tx)
683eda14cbcSMatt Macy {
684eda14cbcSMatt Macy 	objset_t *mos = dd->dd_pool->dp_meta_objset;
685eda14cbcSMatt Macy 	uint64_t jumpobj, pjumpobj;
686eda14cbcSMatt Macy 	uint64_t zapobj = dsl_dir_phys(dd)->dd_deleg_zapobj;
687eda14cbcSMatt Macy 	zap_cursor_t zc;
688*7a7741afSMartin Matuska 	zap_attribute_t *za;
689eda14cbcSMatt Macy 	char whokey[ZFS_MAX_DELEG_NAME];
690eda14cbcSMatt Macy 
691eda14cbcSMatt Macy 	zfs_deleg_whokey(whokey,
692eda14cbcSMatt Macy 	    dosets ? ZFS_DELEG_CREATE_SETS : ZFS_DELEG_CREATE,
693eda14cbcSMatt Macy 	    ZFS_DELEG_LOCAL, NULL);
694eda14cbcSMatt Macy 	if (zap_lookup(mos, pzapobj, whokey, 8, 1, &pjumpobj) != 0)
695eda14cbcSMatt Macy 		return;
696eda14cbcSMatt Macy 
697eda14cbcSMatt Macy 	if (zapobj == 0) {
698eda14cbcSMatt Macy 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
699eda14cbcSMatt Macy 		zapobj = dsl_dir_phys(dd)->dd_deleg_zapobj = zap_create(mos,
700eda14cbcSMatt Macy 		    DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
701eda14cbcSMatt Macy 	}
702eda14cbcSMatt Macy 
703eda14cbcSMatt Macy 	zfs_deleg_whokey(whokey,
704eda14cbcSMatt Macy 	    dosets ? ZFS_DELEG_USER_SETS : ZFS_DELEG_USER,
705eda14cbcSMatt Macy 	    ZFS_DELEG_LOCAL, &uid);
706eda14cbcSMatt Macy 	if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) == ENOENT) {
707eda14cbcSMatt Macy 		jumpobj = zap_create(mos, DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
708eda14cbcSMatt Macy 		VERIFY(zap_add(mos, zapobj, whokey, 8, 1, &jumpobj, tx) == 0);
709eda14cbcSMatt Macy 	}
710eda14cbcSMatt Macy 
711*7a7741afSMartin Matuska 	za = zap_attribute_alloc();
712eda14cbcSMatt Macy 	for (zap_cursor_init(&zc, mos, pjumpobj);
713*7a7741afSMartin Matuska 	    zap_cursor_retrieve(&zc, za) == 0;
714eda14cbcSMatt Macy 	    zap_cursor_advance(&zc)) {
715eda14cbcSMatt Macy 		uint64_t zero = 0;
716*7a7741afSMartin Matuska 		ASSERT(za->za_integer_length == 8 && za->za_num_integers == 1);
717eda14cbcSMatt Macy 
718*7a7741afSMartin Matuska 		VERIFY(zap_update(mos, jumpobj, za->za_name,
719eda14cbcSMatt Macy 		    8, 1, &zero, tx) == 0);
720eda14cbcSMatt Macy 	}
721eda14cbcSMatt Macy 	zap_cursor_fini(&zc);
722*7a7741afSMartin Matuska 	zap_attribute_free(za);
723eda14cbcSMatt Macy }
724eda14cbcSMatt Macy 
725eda14cbcSMatt Macy /*
726eda14cbcSMatt Macy  * set all create time permission on new dataset.
727eda14cbcSMatt Macy  */
728eda14cbcSMatt Macy void
dsl_deleg_set_create_perms(dsl_dir_t * sdd,dmu_tx_t * tx,cred_t * cr)729eda14cbcSMatt Macy dsl_deleg_set_create_perms(dsl_dir_t *sdd, dmu_tx_t *tx, cred_t *cr)
730eda14cbcSMatt Macy {
731eda14cbcSMatt Macy 	dsl_dir_t *dd;
732eda14cbcSMatt Macy 	uint64_t uid = crgetuid(cr);
733eda14cbcSMatt Macy 
734eda14cbcSMatt Macy 	if (spa_version(dmu_objset_spa(sdd->dd_pool->dp_meta_objset)) <
735eda14cbcSMatt Macy 	    SPA_VERSION_DELEGATED_PERMS)
736eda14cbcSMatt Macy 		return;
737eda14cbcSMatt Macy 
738eda14cbcSMatt Macy 	for (dd = sdd->dd_parent; dd != NULL; dd = dd->dd_parent) {
739eda14cbcSMatt Macy 		uint64_t pzapobj = dsl_dir_phys(dd)->dd_deleg_zapobj;
740eda14cbcSMatt Macy 
741eda14cbcSMatt Macy 		if (pzapobj == 0)
742eda14cbcSMatt Macy 			continue;
743eda14cbcSMatt Macy 
744eda14cbcSMatt Macy 		copy_create_perms(sdd, pzapobj, B_FALSE, uid, tx);
745eda14cbcSMatt Macy 		copy_create_perms(sdd, pzapobj, B_TRUE, uid, tx);
746eda14cbcSMatt Macy 	}
747eda14cbcSMatt Macy }
748eda14cbcSMatt Macy 
749eda14cbcSMatt Macy int
dsl_deleg_destroy(objset_t * mos,uint64_t zapobj,dmu_tx_t * tx)750eda14cbcSMatt Macy dsl_deleg_destroy(objset_t *mos, uint64_t zapobj, dmu_tx_t *tx)
751eda14cbcSMatt Macy {
752eda14cbcSMatt Macy 	zap_cursor_t zc;
753*7a7741afSMartin Matuska 	zap_attribute_t *za;
754eda14cbcSMatt Macy 
755eda14cbcSMatt Macy 	if (zapobj == 0)
756eda14cbcSMatt Macy 		return (0);
757eda14cbcSMatt Macy 
758*7a7741afSMartin Matuska 	za = zap_attribute_alloc();
759eda14cbcSMatt Macy 	for (zap_cursor_init(&zc, mos, zapobj);
760*7a7741afSMartin Matuska 	    zap_cursor_retrieve(&zc, za) == 0;
761eda14cbcSMatt Macy 	    zap_cursor_advance(&zc)) {
762*7a7741afSMartin Matuska 		ASSERT(za->za_integer_length == 8 && za->za_num_integers == 1);
763*7a7741afSMartin Matuska 		VERIFY(0 == zap_destroy(mos, za->za_first_integer, tx));
764eda14cbcSMatt Macy 	}
765eda14cbcSMatt Macy 	zap_cursor_fini(&zc);
766eda14cbcSMatt Macy 	VERIFY(0 == zap_destroy(mos, zapobj, tx));
767*7a7741afSMartin Matuska 	zap_attribute_free(za);
768eda14cbcSMatt Macy 	return (0);
769eda14cbcSMatt Macy }
770eda14cbcSMatt Macy 
771eda14cbcSMatt Macy boolean_t
dsl_delegation_on(objset_t * os)772eda14cbcSMatt Macy dsl_delegation_on(objset_t *os)
773eda14cbcSMatt Macy {
774eda14cbcSMatt Macy 	return (!!spa_delegation(os->os_spa));
775eda14cbcSMatt Macy }
776eda14cbcSMatt Macy 
777eda14cbcSMatt Macy #if defined(_KERNEL)
778eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_deleg_get);
779eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_deleg_set);
780eda14cbcSMatt Macy #endif
781