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