Illumos #2619 and #2747
[zfs.git] / module / zfs / dsl_deleg.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012 by Delphix. All rights reserved.
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
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>
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  */
91 int
92 dsl_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
100         while ((whopair = nvlist_next_nvpair(nvp, whopair))) {
101                 nvlist_t *perms;
102                 nvpair_t *permpair = NULL;
103
104                 VERIFY(nvpair_value_nvlist(whopair, &perms) == 0);
105
106                 while ((permpair = nvlist_next_nvpair(perms, permpair))) {
107                         const char *perm = nvpair_name(permpair);
108
109                         if (strcmp(perm, ZFS_DELEG_PERM_ALLOW) == 0)
110                                 return (EPERM);
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  */
124 int
125 dsl_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
137         while ((whopair = nvlist_next_nvpair(nvp, whopair))) {
138                 zfs_deleg_who_type_t type = nvpair_name(whopair)[0];
139
140                 if (type != ZFS_DELEG_USER &&
141                     type != ZFS_DELEG_USER_SETS)
142                         return (EPERM);
143
144                 if (strcmp(idstr, &nvpair_name(whopair)[3]) != 0)
145                         return (EPERM);
146         }
147         return (0);
148 }
149
150 static void
151 dsl_deleg_set_sync(void *arg1, void *arg2, dmu_tx_t *tx)
152 {
153         dsl_dir_t *dd = arg1;
154         nvlist_t *nvp = arg2;
155         objset_t *mos = dd->dd_pool->dp_meta_objset;
156         nvpair_t *whopair = NULL;
157         uint64_t zapobj = dd->dd_phys->dd_deleg_zapobj;
158
159         if (zapobj == 0) {
160                 dmu_buf_will_dirty(dd->dd_dbuf, tx);
161                 zapobj = dd->dd_phys->dd_deleg_zapobj = zap_create(mos,
162                     DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
163         }
164
165         while ((whopair = nvlist_next_nvpair(nvp, whopair))) {
166                 const char *whokey = nvpair_name(whopair);
167                 nvlist_t *perms;
168                 nvpair_t *permpair = NULL;
169                 uint64_t jumpobj;
170
171                 VERIFY(nvpair_value_nvlist(whopair, &perms) == 0);
172
173                 if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) != 0) {
174                         jumpobj = zap_create_link(mos, DMU_OT_DSL_PERMS,
175                             zapobj, whokey, tx);
176                 }
177
178                 while ((permpair = nvlist_next_nvpair(perms, permpair))) {
179                         const char *perm = nvpair_name(permpair);
180                         uint64_t n = 0;
181
182                         VERIFY(zap_update(mos, jumpobj,
183                             perm, 8, 1, &n, tx) == 0);
184                         spa_history_log_internal(LOG_DS_PERM_UPDATE,
185                             dd->dd_pool->dp_spa, tx,
186                             "%s %s dataset = %llu", whokey, perm,
187                             dd->dd_phys->dd_head_dataset_obj);
188                 }
189         }
190 }
191
192 static void
193 dsl_deleg_unset_sync(void *arg1, void *arg2, dmu_tx_t *tx)
194 {
195         dsl_dir_t *dd = arg1;
196         nvlist_t *nvp = arg2;
197         objset_t *mos = dd->dd_pool->dp_meta_objset;
198         nvpair_t *whopair = NULL;
199         uint64_t zapobj = dd->dd_phys->dd_deleg_zapobj;
200
201         if (zapobj == 0)
202                 return;
203
204         while ((whopair = nvlist_next_nvpair(nvp, whopair))) {
205                 const char *whokey = nvpair_name(whopair);
206                 nvlist_t *perms;
207                 nvpair_t *permpair = NULL;
208                 uint64_t jumpobj;
209
210                 if (nvpair_value_nvlist(whopair, &perms) != 0) {
211                         if (zap_lookup(mos, zapobj, whokey, 8,
212                             1, &jumpobj) == 0) {
213                                 (void) zap_remove(mos, zapobj, whokey, tx);
214                                 VERIFY(0 == zap_destroy(mos, jumpobj, tx));
215                         }
216                         spa_history_log_internal(LOG_DS_PERM_WHO_REMOVE,
217                             dd->dd_pool->dp_spa, tx,
218                             "%s dataset = %llu", whokey,
219                             dd->dd_phys->dd_head_dataset_obj);
220                         continue;
221                 }
222
223                 if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) != 0)
224                         continue;
225
226                 while ((permpair = nvlist_next_nvpair(perms, permpair))) {
227                         const char *perm = nvpair_name(permpair);
228                         uint64_t n = 0;
229
230                         (void) zap_remove(mos, jumpobj, perm, tx);
231                         if (zap_count(mos, jumpobj, &n) == 0 && n == 0) {
232                                 (void) zap_remove(mos, zapobj,
233                                     whokey, tx);
234                                 VERIFY(0 == zap_destroy(mos,
235                                     jumpobj, tx));
236                         }
237                         spa_history_log_internal(LOG_DS_PERM_REMOVE,
238                             dd->dd_pool->dp_spa, tx,
239                             "%s %s dataset = %llu", whokey, perm,
240                             dd->dd_phys->dd_head_dataset_obj);
241                 }
242         }
243 }
244
245 int
246 dsl_deleg_set(const char *ddname, nvlist_t *nvp, boolean_t unset)
247 {
248         dsl_dir_t *dd;
249         int error;
250         nvpair_t *whopair = NULL;
251         int blocks_modified = 0;
252
253         error = dsl_dir_open(ddname, FTAG, &dd, NULL);
254         if (error)
255                 return (error);
256
257         if (spa_version(dmu_objset_spa(dd->dd_pool->dp_meta_objset)) <
258             SPA_VERSION_DELEGATED_PERMS) {
259                 dsl_dir_close(dd, FTAG);
260                 return (ENOTSUP);
261         }
262
263         while ((whopair = nvlist_next_nvpair(nvp, whopair)))
264                 blocks_modified++;
265
266         error = dsl_sync_task_do(dd->dd_pool, NULL,
267             unset ? dsl_deleg_unset_sync : dsl_deleg_set_sync,
268             dd, nvp, blocks_modified);
269         dsl_dir_close(dd, FTAG);
270
271         return (error);
272 }
273
274 /*
275  * Find all 'allow' permissions from a given point and then continue
276  * traversing up to the root.
277  *
278  * This function constructs an nvlist of nvlists.
279  * each setpoint is an nvlist composed of an nvlist of an nvlist
280  * of the individual * users/groups/everyone/create
281  * permissions.
282  *
283  * The nvlist will look like this.
284  *
285  * { source fsname -> { whokeys { permissions,...}, ...}}
286  *
287  * The fsname nvpairs will be arranged in a bottom up order.  For example,
288  * if we have the following structure a/b/c then the nvpairs for the fsnames
289  * will be ordered a/b/c, a/b, a.
290  */
291 int
292 dsl_deleg_get(const char *ddname, nvlist_t **nvp)
293 {
294         dsl_dir_t *dd, *startdd;
295         dsl_pool_t *dp;
296         int error;
297         objset_t *mos;
298         zap_cursor_t *basezc, *zc;
299         zap_attribute_t *baseza, *za;
300         char *source;
301
302         error = dsl_dir_open(ddname, FTAG, &startdd, NULL);
303         if (error)
304                 return (error);
305
306         dp = startdd->dd_pool;
307         mos = dp->dp_meta_objset;
308
309         zc = kmem_alloc(sizeof(zap_cursor_t), KM_SLEEP);
310         za = kmem_alloc(sizeof(zap_attribute_t), KM_SLEEP);
311         basezc = kmem_alloc(sizeof(zap_cursor_t), KM_SLEEP);
312         baseza = kmem_alloc(sizeof(zap_attribute_t), KM_SLEEP);
313         source = kmem_alloc(MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, KM_SLEEP);
314         VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
315
316         rw_enter(&dp->dp_config_rwlock, RW_READER);
317         for (dd = startdd; dd != NULL; dd = dd->dd_parent) {
318                 nvlist_t *sp_nvp;
319                 uint64_t n;
320
321                 if (dd->dd_phys->dd_deleg_zapobj &&
322                     (zap_count(mos, dd->dd_phys->dd_deleg_zapobj,
323                     &n) == 0) && n) {
324                         VERIFY(nvlist_alloc(&sp_nvp,
325                             NV_UNIQUE_NAME, KM_SLEEP) == 0);
326                 } else {
327                         continue;
328                 }
329
330                 for (zap_cursor_init(basezc, mos,
331                     dd->dd_phys->dd_deleg_zapobj);
332                     zap_cursor_retrieve(basezc, baseza) == 0;
333                     zap_cursor_advance(basezc)) {
334                         nvlist_t *perms_nvp;
335
336                         ASSERT(baseza->za_integer_length == 8);
337                         ASSERT(baseza->za_num_integers == 1);
338
339                         VERIFY(nvlist_alloc(&perms_nvp,
340                             NV_UNIQUE_NAME, KM_SLEEP) == 0);
341                         for (zap_cursor_init(zc, mos, baseza->za_first_integer);
342                             zap_cursor_retrieve(zc, za) == 0;
343                             zap_cursor_advance(zc)) {
344                                 VERIFY(nvlist_add_boolean(perms_nvp,
345                                     za->za_name) == 0);
346                         }
347                         zap_cursor_fini(zc);
348                         VERIFY(nvlist_add_nvlist(sp_nvp, baseza->za_name,
349                             perms_nvp) == 0);
350                         nvlist_free(perms_nvp);
351                 }
352
353                 zap_cursor_fini(basezc);
354
355                 dsl_dir_name(dd, source);
356                 VERIFY(nvlist_add_nvlist(*nvp, source, sp_nvp) == 0);
357                 nvlist_free(sp_nvp);
358         }
359         rw_exit(&dp->dp_config_rwlock);
360
361         kmem_free(source, MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
362         kmem_free(baseza, sizeof(zap_attribute_t));
363         kmem_free(basezc, sizeof(zap_cursor_t));
364         kmem_free(za, sizeof(zap_attribute_t));
365         kmem_free(zc, sizeof(zap_cursor_t));
366
367         dsl_dir_close(startdd, FTAG);
368         return (0);
369 }
370
371 /*
372  * Routines for dsl_deleg_access() -- access checking.
373  */
374 typedef struct perm_set {
375         avl_node_t      p_node;
376         boolean_t       p_matched;
377         char            p_setname[ZFS_MAX_DELEG_NAME];
378 } perm_set_t;
379
380 static int
381 perm_set_compare(const void *arg1, const void *arg2)
382 {
383         const perm_set_t *node1 = arg1;
384         const perm_set_t *node2 = arg2;
385         int val;
386
387         val = strcmp(node1->p_setname, node2->p_setname);
388         if (val == 0)
389                 return (0);
390         return (val > 0 ? 1 : -1);
391 }
392
393 /*
394  * Determine whether a specified permission exists.
395  *
396  * First the base attribute has to be retrieved.  i.e. ul$12
397  * Once the base object has been retrieved the actual permission
398  * is lookup up in the zap object the base object points to.
399  *
400  * Return 0 if permission exists, ENOENT if there is no whokey, EPERM if
401  * there is no perm in that jumpobj.
402  */
403 static int
404 dsl_check_access(objset_t *mos, uint64_t zapobj,
405     char type, char checkflag, void *valp, const char *perm)
406 {
407         int error;
408         uint64_t jumpobj, zero;
409         char whokey[ZFS_MAX_DELEG_NAME];
410
411         zfs_deleg_whokey(whokey, type, checkflag, valp);
412         error = zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj);
413         if (error == 0) {
414                 error = zap_lookup(mos, jumpobj, perm, 8, 1, &zero);
415                 if (error == ENOENT)
416                         error = EPERM;
417         }
418         return (error);
419 }
420
421 /*
422  * check a specified user/group for a requested permission
423  */
424 static int
425 dsl_check_user_access(objset_t *mos, uint64_t zapobj, const char *perm,
426     int checkflag, cred_t *cr)
427 {
428         const   gid_t *gids;
429         int     ngids;
430         int     i;
431         uint64_t id;
432
433         /* check for user */
434         id = crgetuid(cr);
435         if (dsl_check_access(mos, zapobj,
436             ZFS_DELEG_USER, checkflag, &id, perm) == 0)
437                 return (0);
438
439         /* check for users primary group */
440         id = crgetgid(cr);
441         if (dsl_check_access(mos, zapobj,
442             ZFS_DELEG_GROUP, checkflag, &id, perm) == 0)
443                 return (0);
444
445         /* check for everyone entry */
446         id = -1;
447         if (dsl_check_access(mos, zapobj,
448             ZFS_DELEG_EVERYONE, checkflag, &id, perm) == 0)
449                 return (0);
450
451         /* check each supplemental group user is a member of */
452         ngids = crgetngroups(cr);
453         gids = crgetgroups(cr);
454         for (i = 0; i != ngids; i++) {
455                 id = gids[i];
456                 if (dsl_check_access(mos, zapobj,
457                     ZFS_DELEG_GROUP, checkflag, &id, perm) == 0)
458                         return (0);
459         }
460
461         return (EPERM);
462 }
463
464 /*
465  * Iterate over the sets specified in the specified zapobj
466  * and load them into the permsets avl tree.
467  */
468 static int
469 dsl_load_sets(objset_t *mos, uint64_t zapobj,
470     char type, char checkflag, void *valp, avl_tree_t *avl)
471 {
472         zap_cursor_t zc;
473         zap_attribute_t za;
474         perm_set_t *permnode;
475         avl_index_t idx;
476         uint64_t jumpobj;
477         int error;
478         char whokey[ZFS_MAX_DELEG_NAME];
479
480         zfs_deleg_whokey(whokey, type, checkflag, valp);
481
482         error = zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj);
483         if (error != 0)
484                 return (error);
485
486         for (zap_cursor_init(&zc, mos, jumpobj);
487             zap_cursor_retrieve(&zc, &za) == 0;
488             zap_cursor_advance(&zc)) {
489                 permnode = kmem_alloc(sizeof (perm_set_t), KM_SLEEP);
490                 (void) strlcpy(permnode->p_setname, za.za_name,
491                     sizeof (permnode->p_setname));
492                 permnode->p_matched = B_FALSE;
493
494                 if (avl_find(avl, permnode, &idx) == NULL) {
495                         avl_insert(avl, permnode, idx);
496                 } else {
497                         kmem_free(permnode, sizeof (perm_set_t));
498                 }
499         }
500         zap_cursor_fini(&zc);
501         return (0);
502 }
503
504 /*
505  * Load all permissions user based on cred belongs to.
506  */
507 static void
508 dsl_load_user_sets(objset_t *mos, uint64_t zapobj, avl_tree_t *avl,
509     char checkflag, cred_t *cr)
510 {
511         const   gid_t *gids;
512         int     ngids, i;
513         uint64_t id;
514
515         id = crgetuid(cr);
516         (void) dsl_load_sets(mos, zapobj,
517             ZFS_DELEG_USER_SETS, checkflag, &id, avl);
518
519         id = crgetgid(cr);
520         (void) dsl_load_sets(mos, zapobj,
521             ZFS_DELEG_GROUP_SETS, checkflag, &id, avl);
522
523         (void) dsl_load_sets(mos, zapobj,
524             ZFS_DELEG_EVERYONE_SETS, checkflag, NULL, avl);
525
526         ngids = crgetngroups(cr);
527         gids = crgetgroups(cr);
528         for (i = 0; i != ngids; i++) {
529                 id = gids[i];
530                 (void) dsl_load_sets(mos, zapobj,
531                     ZFS_DELEG_GROUP_SETS, checkflag, &id, avl);
532         }
533 }
534
535 /*
536  * Check if user has requested permission.  If descendent is set, must have
537  * descendent perms.
538  */
539 int
540 dsl_deleg_access_impl(dsl_dataset_t *ds, boolean_t descendent, const char *perm,
541     cred_t *cr)
542 {
543         dsl_dir_t *dd;
544         dsl_pool_t *dp;
545         void *cookie;
546         int     error;
547         char    checkflag;
548         objset_t *mos;
549         avl_tree_t permsets;
550         perm_set_t *setnode;
551
552         dp = ds->ds_dir->dd_pool;
553         mos = dp->dp_meta_objset;
554
555         if (dsl_delegation_on(mos) == B_FALSE)
556                 return (ECANCELED);
557
558         if (spa_version(dmu_objset_spa(dp->dp_meta_objset)) <
559             SPA_VERSION_DELEGATED_PERMS)
560                 return (EPERM);
561
562         if (dsl_dataset_is_snapshot(ds) || descendent) {
563                 /*
564                  * Snapshots are treated as descendents only,
565                  * local permissions do not apply.
566                  */
567                 checkflag = ZFS_DELEG_DESCENDENT;
568         } else {
569                 checkflag = ZFS_DELEG_LOCAL;
570         }
571
572         avl_create(&permsets, perm_set_compare, sizeof (perm_set_t),
573             offsetof(perm_set_t, p_node));
574
575         rw_enter(&dp->dp_config_rwlock, RW_READER);
576         for (dd = ds->ds_dir; dd != NULL; dd = dd->dd_parent,
577             checkflag = ZFS_DELEG_DESCENDENT) {
578                 uint64_t zapobj;
579                 boolean_t expanded;
580
581                 /*
582                  * If not in global zone then make sure
583                  * the zoned property is set
584                  */
585                 if (!INGLOBALZONE(curproc)) {
586                         uint64_t zoned;
587
588                         if (dsl_prop_get_dd(dd,
589                             zfs_prop_to_name(ZFS_PROP_ZONED),
590                             8, 1, &zoned, NULL, B_FALSE) != 0)
591                                 break;
592                         if (!zoned)
593                                 break;
594                 }
595                 zapobj = dd->dd_phys->dd_deleg_zapobj;
596
597                 if (zapobj == 0)
598                         continue;
599
600                 dsl_load_user_sets(mos, zapobj, &permsets, checkflag, cr);
601 again:
602                 expanded = B_FALSE;
603                 for (setnode = avl_first(&permsets); setnode;
604                     setnode = AVL_NEXT(&permsets, setnode)) {
605                         if (setnode->p_matched == B_TRUE)
606                                 continue;
607
608                         /* See if this set directly grants this permission */
609                         error = dsl_check_access(mos, zapobj,
610                             ZFS_DELEG_NAMED_SET, 0, setnode->p_setname, perm);
611                         if (error == 0)
612                                 goto success;
613                         if (error == EPERM)
614                                 setnode->p_matched = B_TRUE;
615
616                         /* See if this set includes other sets */
617                         error = dsl_load_sets(mos, zapobj,
618                             ZFS_DELEG_NAMED_SET_SETS, 0,
619                             setnode->p_setname, &permsets);
620                         if (error == 0)
621                                 setnode->p_matched = expanded = B_TRUE;
622                 }
623                 /*
624                  * If we expanded any sets, that will define more sets,
625                  * which we need to check.
626                  */
627                 if (expanded)
628                         goto again;
629
630                 error = dsl_check_user_access(mos, zapobj, perm, checkflag, cr);
631                 if (error == 0)
632                         goto success;
633         }
634         error = EPERM;
635 success:
636         rw_exit(&dp->dp_config_rwlock);
637
638         cookie = NULL;
639         while ((setnode = avl_destroy_nodes(&permsets, &cookie)) != NULL)
640                 kmem_free(setnode, sizeof (perm_set_t));
641
642         return (error);
643 }
644
645 int
646 dsl_deleg_access(const char *dsname, const char *perm, cred_t *cr)
647 {
648         dsl_dataset_t *ds;
649         int error;
650
651         error = dsl_dataset_hold(dsname, FTAG, &ds);
652         if (error)
653                 return (error);
654
655         error = dsl_deleg_access_impl(ds, B_FALSE, perm, cr);
656         dsl_dataset_rele(ds, FTAG);
657
658         return (error);
659 }
660
661 /*
662  * Other routines.
663  */
664
665 static void
666 copy_create_perms(dsl_dir_t *dd, uint64_t pzapobj,
667     boolean_t dosets, uint64_t uid, dmu_tx_t *tx)
668 {
669         objset_t *mos = dd->dd_pool->dp_meta_objset;
670         uint64_t jumpobj, pjumpobj;
671         uint64_t zapobj = dd->dd_phys->dd_deleg_zapobj;
672         zap_cursor_t zc;
673         zap_attribute_t za;
674         char whokey[ZFS_MAX_DELEG_NAME];
675
676         zfs_deleg_whokey(whokey,
677             dosets ? ZFS_DELEG_CREATE_SETS : ZFS_DELEG_CREATE,
678             ZFS_DELEG_LOCAL, NULL);
679         if (zap_lookup(mos, pzapobj, whokey, 8, 1, &pjumpobj) != 0)
680                 return;
681
682         if (zapobj == 0) {
683                 dmu_buf_will_dirty(dd->dd_dbuf, tx);
684                 zapobj = dd->dd_phys->dd_deleg_zapobj = zap_create(mos,
685                     DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
686         }
687
688         zfs_deleg_whokey(whokey,
689             dosets ? ZFS_DELEG_USER_SETS : ZFS_DELEG_USER,
690             ZFS_DELEG_LOCAL, &uid);
691         if (zap_lookup(mos, zapobj, whokey, 8, 1, &jumpobj) == ENOENT) {
692                 jumpobj = zap_create(mos, DMU_OT_DSL_PERMS, DMU_OT_NONE, 0, tx);
693                 VERIFY(zap_add(mos, zapobj, whokey, 8, 1, &jumpobj, tx) == 0);
694         }
695
696         for (zap_cursor_init(&zc, mos, pjumpobj);
697             zap_cursor_retrieve(&zc, &za) == 0;
698             zap_cursor_advance(&zc)) {
699                 uint64_t zero = 0;
700                 ASSERT(za.za_integer_length == 8 && za.za_num_integers == 1);
701
702                 VERIFY(zap_update(mos, jumpobj, za.za_name,
703                     8, 1, &zero, tx) == 0);
704         }
705         zap_cursor_fini(&zc);
706 }
707
708 /*
709  * set all create time permission on new dataset.
710  */
711 void
712 dsl_deleg_set_create_perms(dsl_dir_t *sdd, dmu_tx_t *tx, cred_t *cr)
713 {
714         dsl_dir_t *dd;
715         uint64_t uid = crgetuid(cr);
716
717         if (spa_version(dmu_objset_spa(sdd->dd_pool->dp_meta_objset)) <
718             SPA_VERSION_DELEGATED_PERMS)
719                 return;
720
721         for (dd = sdd->dd_parent; dd != NULL; dd = dd->dd_parent) {
722                 uint64_t pzapobj = dd->dd_phys->dd_deleg_zapobj;
723
724                 if (pzapobj == 0)
725                         continue;
726
727                 copy_create_perms(sdd, pzapobj, B_FALSE, uid, tx);
728                 copy_create_perms(sdd, pzapobj, B_TRUE, uid, tx);
729         }
730 }
731
732 int
733 dsl_deleg_destroy(objset_t *mos, uint64_t zapobj, dmu_tx_t *tx)
734 {
735         zap_cursor_t zc;
736         zap_attribute_t za;
737
738         if (zapobj == 0)
739                 return (0);
740
741         for (zap_cursor_init(&zc, mos, zapobj);
742             zap_cursor_retrieve(&zc, &za) == 0;
743             zap_cursor_advance(&zc)) {
744                 ASSERT(za.za_integer_length == 8 && za.za_num_integers == 1);
745                 VERIFY(0 == zap_destroy(mos, za.za_first_integer, tx));
746         }
747         zap_cursor_fini(&zc);
748         VERIFY(0 == zap_destroy(mos, zapobj, tx));
749         return (0);
750 }
751
752 boolean_t
753 dsl_delegation_on(objset_t *os)
754 {
755         return (!!spa_delegation(os->os_spa));
756 }
757
758 #if defined(_KERNEL) && defined(HAVE_SPL)
759 EXPORT_SYMBOL(dsl_deleg_get);
760 EXPORT_SYMBOL(dsl_deleg_set);
761 #endif