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