xref: /titanic_52/usr/src/uts/common/fs/zfs/zfs_fuid.c (revision 36e5aa2ab5c51d4747a2470e41ccb782056c90e7)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/unistd.h>
30 #include <sys/sysmacros.h>
31 #include <sys/sunddi.h>
32 #include <sys/zfs_vfsops.h>
33 #include <sys/zfs_znode.h>
34 #include <sys/zfs_fuid.h>
35 #include <sys/dmu.h>
36 #include <sys/refcount.h>
37 #include <sys/avl.h>
38 #include <sys/zap.h>
39 #include <sys/nvpair.h>
40 #include <sys/kidmap.h>
41 #include <sys/sid.h>
42 
43 /*
44  * FUID Domain table(s).
45  *
46  * The FUID table is stored as a packed nvlist of an array
47  * of nvlists which contain an index, domain string and offset
48  *
49  * During file system initialization the nvlist(s) are read and
50  * two AVL trees are created.  One tree is keyed by the index number
51  * and the other by the domain string.  Nodes are never removed from
52  * trees, but new entries may be added.  If a new entry is added then the
53  * on-disk packed nvlist will also be updated.
54  */
55 
56 #define	FUID_IDX	"fuid_idx"
57 #define	FUID_DOMAIN	"fuid_domain"
58 #define	FUID_OFFSET	"fuid_offset"
59 #define	FUID_NVP_ARRAY	"fuid_nvlist"
60 
61 typedef struct fuid_domain {
62 	avl_node_t	f_node;
63 	ksiddomain_t	*f_ksid;
64 	int		f_idx;
65 	uint32_t	f_offset;
66 } fuid_domain_t;
67 
68 typedef struct fuid_idx {
69 	avl_node_t	f_node;
70 	int		f_idx;
71 	fuid_domain_t	*f_domain;
72 } fuid_idx_t;
73 
74 /*
75  * Compare two indexes.
76  */
77 static int
78 idx_compare(const void *arg1, const void *arg2)
79 {
80 	const fuid_idx_t *node1 = arg1;
81 	const fuid_idx_t *node2 = arg2;
82 
83 	if (node1->f_idx < node2->f_idx)
84 		return (-1);
85 	else if (node1->f_idx > node2->f_idx)
86 		return (1);
87 	return (0);
88 }
89 
90 /*
91  * Compare two domain strings.
92  */
93 static int
94 domain_compare(const void *arg1, const void *arg2)
95 {
96 	const fuid_domain_t *node1 = arg1;
97 	const fuid_domain_t *node2 = arg2;
98 	int val;
99 
100 	val = strcmp(node1->f_ksid->kd_name, node2->f_ksid->kd_name);
101 	if (val == 0)
102 		return (0);
103 	return (val > 0 ? 1 : -1);
104 }
105 
106 /*
107  * Load the fuid table(s) into memory.
108  */
109 static void
110 zfs_fuid_init(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
111 {
112 	dmu_buf_t *db;
113 	char *packed;
114 	size_t nvsize = 0;
115 	int error = 0;
116 	int i;
117 
118 	rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
119 
120 	if (zfsvfs->z_fuid_loaded) {
121 		rw_exit(&zfsvfs->z_fuid_lock);
122 		return;
123 	}
124 
125 	if (zfsvfs->z_fuid_obj == 0) {
126 
127 		/* first make sure we need to allocate object */
128 
129 		error = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
130 		    ZFS_FUID_TABLES, 8, 1, &zfsvfs->z_fuid_obj);
131 		if (error == ENOENT && tx != NULL) {
132 			zfsvfs->z_fuid_obj = dmu_object_alloc(zfsvfs->z_os,
133 			    DMU_OT_FUID, 1 << 14, DMU_OT_FUID_SIZE,
134 			    sizeof (uint64_t), tx);
135 			VERIFY(zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
136 			    ZFS_FUID_TABLES, sizeof (uint64_t), 1,
137 			    &zfsvfs->z_fuid_obj, tx) == 0);
138 		}
139 	}
140 
141 	avl_create(&zfsvfs->z_fuid_idx, idx_compare,
142 	    sizeof (fuid_idx_t), offsetof(fuid_idx_t, f_node));
143 	avl_create(&zfsvfs->z_fuid_domain, domain_compare,
144 	    sizeof (fuid_domain_t), offsetof(fuid_domain_t, f_node));
145 
146 	if (zfsvfs->z_fuid_obj) {
147 		VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, zfsvfs->z_fuid_obj,
148 		    FTAG, &db));
149 		nvsize = *(uint64_t *)db->db_data;
150 		dmu_buf_rele(db, FTAG);
151 	}
152 
153 	if (nvsize == 0)
154 		goto initialized;
155 
156 	packed = kmem_alloc(nvsize, KM_SLEEP);
157 	error = dmu_read(zfsvfs->z_os, zfsvfs->z_fuid_obj, 0, nvsize, packed);
158 	if (error == 0)  {
159 		nvlist_t **fuidnvp;
160 		nvlist_t *nvp = NULL;
161 		uint_t count;
162 
163 		VERIFY(nvlist_unpack(packed, nvsize, &nvp, 0) == 0);
164 		VERIFY((error = nvlist_lookup_nvlist_array(nvp, FUID_NVP_ARRAY,
165 		    &fuidnvp, &count)) == 0);
166 
167 		for (i = 0; i != count; i++) {
168 			fuid_idx_t *idxnode;
169 			fuid_domain_t *domnode;
170 			char *domain;
171 			avl_index_t loc;
172 			uint64_t idx, offset;
173 
174 			VERIFY(nvlist_lookup_string(fuidnvp[i], FUID_DOMAIN,
175 			    &domain) == 0);
176 			VERIFY(nvlist_lookup_uint64(fuidnvp[i], FUID_IDX,
177 			    &idx) == 0);
178 			VERIFY(nvlist_lookup_uint64(fuidnvp[i], FUID_OFFSET,
179 			    &offset) == 0);
180 
181 			idxnode = kmem_alloc(sizeof (fuid_idx_t), KM_SLEEP);
182 			domnode = kmem_alloc(sizeof (fuid_domain_t), KM_SLEEP);
183 
184 			domnode->f_idx = idxnode->f_idx = idx;
185 			domnode->f_ksid = ksid_lookupdomain(domain);
186 			idxnode->f_domain = domnode;
187 			domnode->f_offset = offset;
188 			if (avl_find(&zfsvfs->z_fuid_idx,
189 			    idxnode, &loc) == NULL) {
190 				avl_insert(&zfsvfs->z_fuid_idx, idxnode, loc);
191 			}
192 			if (avl_find(&zfsvfs->z_fuid_domain,
193 			    domnode, &loc) == NULL) {
194 				avl_insert(&zfsvfs->z_fuid_domain,
195 				    domnode, loc);
196 			}
197 		}
198 		nvlist_free(nvp);
199 	}
200 	kmem_free(packed, nvsize);
201 
202 initialized:
203 	zfsvfs->z_fuid_loaded = B_TRUE;
204 	rw_exit(&zfsvfs->z_fuid_lock);
205 }
206 
207 /*
208  * Query domain table for a given domain.
209  *
210  * If domain isn't found it is added to AVL trees and
211  * the results are pushed out to disk.
212  */
213 int
214 zfs_fuid_find_by_domain(zfsvfs_t *zfsvfs, const char *domain, char **retdomain,
215     dmu_tx_t *tx)
216 {
217 	fuid_domain_t searchnode, *findnode;
218 	avl_index_t loc;
219 
220 	/*
221 	 * If the dummy "nobody" domain then return an index of 0
222 	 * to cause the created FUID to be a standard POSIX id
223 	 * for the user nobody.
224 	 */
225 	if (domain[0] == '\0') {
226 		*retdomain = "";
227 		return (0);
228 	}
229 
230 	searchnode.f_ksid = ksid_lookupdomain(domain);
231 	if (retdomain) {
232 		*retdomain = searchnode.f_ksid->kd_name;
233 	}
234 	if (zfsvfs->z_fuid_loaded == B_FALSE)
235 		zfs_fuid_init(zfsvfs, tx);
236 
237 	rw_enter(&zfsvfs->z_fuid_lock, RW_READER);
238 	findnode = avl_find(&zfsvfs->z_fuid_domain, &searchnode, &loc);
239 	rw_exit(&zfsvfs->z_fuid_lock);
240 
241 	if (findnode) {
242 		ksiddomain_rele(searchnode.f_ksid);
243 		return (findnode->f_idx);
244 	} else {
245 		fuid_domain_t *domnode;
246 		fuid_idx_t *newidxnode;
247 		nvlist_t *nvp;
248 		nvlist_t **fuids;
249 		uint64_t retidx;
250 		size_t nvsize = 0;
251 		char *packed;
252 		dmu_buf_t *db;
253 		int i = 0;
254 
255 		domnode = kmem_alloc(sizeof (fuid_domain_t), KM_SLEEP);
256 		domnode->f_ksid = searchnode.f_ksid;
257 		domnode->f_offset = 0;
258 
259 		newidxnode = kmem_alloc(sizeof (fuid_idx_t), KM_SLEEP);
260 		newidxnode->f_domain = domnode;
261 
262 		rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
263 		retidx = domnode->f_idx = newidxnode->f_idx =
264 		    avl_numnodes(&zfsvfs->z_fuid_idx) + 1;
265 
266 		avl_add(&zfsvfs->z_fuid_domain, domnode);
267 		avl_add(&zfsvfs->z_fuid_idx, newidxnode);
268 		/*
269 		 * Now resync the on-disk nvlist.
270 		 */
271 		VERIFY(nvlist_alloc(&nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
272 
273 		domnode = avl_first(&zfsvfs->z_fuid_domain);
274 		fuids = kmem_alloc(retidx * sizeof (void *), KM_SLEEP);
275 		while (domnode) {
276 			VERIFY(nvlist_alloc(&fuids[i],
277 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
278 			VERIFY(nvlist_add_uint64(fuids[i], FUID_IDX,
279 			    domnode->f_idx) == 0);
280 			VERIFY(nvlist_add_uint64(fuids[i], FUID_OFFSET,
281 			    domnode->f_offset) == 0);
282 			VERIFY(nvlist_add_string(fuids[i++], FUID_DOMAIN,
283 			    domnode->f_ksid->kd_name) == 0);
284 			domnode = AVL_NEXT(&zfsvfs->z_fuid_domain, domnode);
285 		}
286 		VERIFY(nvlist_add_nvlist_array(nvp, FUID_NVP_ARRAY,
287 		    fuids, retidx) == 0);
288 		for (i = 0; i != retidx; i++)
289 			nvlist_free(fuids[i]);
290 		kmem_free(fuids, retidx * sizeof (void *));
291 		VERIFY(nvlist_size(nvp, &nvsize, NV_ENCODE_XDR) == 0);
292 		packed = kmem_alloc(nvsize, KM_SLEEP);
293 		VERIFY(nvlist_pack(nvp, &packed, &nvsize,
294 		    NV_ENCODE_XDR, KM_SLEEP) == 0);
295 		nvlist_free(nvp);
296 		dmu_write(zfsvfs->z_os, zfsvfs->z_fuid_obj, 0, nvsize,
297 		    packed, tx);
298 		kmem_free(packed, nvsize);
299 		VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, zfsvfs->z_fuid_obj,
300 		    FTAG, &db));
301 		dmu_buf_will_dirty(db, tx);
302 		*(uint64_t *)db->db_data = nvsize;
303 		dmu_buf_rele(db, FTAG);
304 
305 		rw_exit(&zfsvfs->z_fuid_lock);
306 		return (retidx);
307 	}
308 }
309 
310 /*
311  * Query domain table by index, returning domain string
312  *
313  * Returns a pointer from an avl node of the domain string.
314  *
315  */
316 char *
317 zfs_fuid_find_by_idx(zfsvfs_t *zfsvfs, uint64_t idx)
318 {
319 	fuid_idx_t searchnode, *findnode;
320 	avl_index_t loc;
321 
322 	if (idx == 0 || zfsvfs->z_use_fuids == B_FALSE)
323 		return (NULL);
324 
325 	if (zfsvfs->z_fuid_loaded == B_FALSE)
326 		zfs_fuid_init(zfsvfs, NULL);
327 
328 	searchnode.f_idx = idx;
329 
330 	rw_enter(&zfsvfs->z_fuid_lock, RW_READER);
331 	findnode = avl_find(&zfsvfs->z_fuid_idx, &searchnode, &loc);
332 	rw_exit(&zfsvfs->z_fuid_lock);
333 
334 	ASSERT(findnode);
335 	return (findnode->f_domain->f_ksid->kd_name);
336 }
337 
338 void
339 zfs_fuid_get_mappings(zfs_fuid_hdl_t *hdl)
340 {
341 	VERIFY(hdl != NULL);
342 	if (hdl->z_map_needed == B_FALSE)
343 		return;
344 
345 	(void) kidmap_get_mappings(hdl->z_hdl);
346 
347 	kidmap_get_destroy(hdl->z_hdl);
348 	hdl->z_hdl = NULL;
349 	hdl->z_map_needed = B_FALSE;
350 }
351 
352 void
353 zfs_fuid_queue_map_id(zfsvfs_t *zfsvfs, zfs_fuid_hdl_t *hdl,
354     uint64_t fuid, zfs_fuid_type_t type, uid_t *id)
355 {
356 	uint32_t index = FUID_INDEX(fuid);
357 	char *domain;
358 	int status;
359 
360 	VERIFY(hdl);
361 
362 	if (index == 0 || zfsvfs->z_use_fuids == B_FALSE) {
363 		*id = (uid_t)fuid;
364 		return;
365 	}
366 
367 	if (hdl->z_hdl == NULL) {
368 		hdl->z_hdl = kidmap_get_create();
369 		hdl->z_map_needed = B_TRUE;
370 	}
371 
372 	domain = zfs_fuid_find_by_idx(zfsvfs, index);
373 	ASSERT(domain != NULL);
374 
375 	if (type == ZFS_OWNER || type == ZFS_ACE_USER)
376 		status = kidmap_batch_getuidbysid(hdl->z_hdl, domain,
377 		    FUID_RID(fuid), id, &hdl->z_status);
378 	else
379 		status = kidmap_batch_getgidbysid(hdl->z_hdl, domain,
380 		    FUID_RID(fuid), id, &hdl->z_status);
381 	ASSERT(status == 0);
382 }
383 
384 void
385 zfs_fuid_map_ids(znode_t *zp, uid_t *uid, uid_t *gid)
386 {
387 	uint32_t uid_index = FUID_INDEX(zp->z_phys->zp_uid);
388 	uint32_t gid_index = FUID_INDEX(zp->z_phys->zp_gid);
389 
390 	/* Favor the common case, neither will be ephemeral */
391 	if (uid_index == 0 && gid_index == 0) {
392 		*uid = zp->z_phys->zp_uid;
393 		*gid = zp->z_phys->zp_gid;
394 		return;
395 	} else {
396 		zfs_fuid_hdl_t hdl = { 0 };
397 
398 		zfs_fuid_queue_map_id(zp->z_zfsvfs, &hdl,
399 		    zp->z_phys->zp_uid, ZFS_OWNER, uid);
400 
401 		zfs_fuid_queue_map_id(zp->z_zfsvfs, &hdl,
402 		    zp->z_phys->zp_gid, ZFS_GROUP, gid);
403 
404 		zfs_fuid_get_mappings(&hdl);
405 	}
406 }
407 
408 void
409 zfs_fuid_map_id(zfsvfs_t *zfsvfs, uint64_t fuid,
410     zfs_fuid_type_t type, uid_t *id)
411 {
412 	uint32_t index = FUID_INDEX(fuid);
413 	char *domain;
414 
415 	if (index == 0) {
416 		*id = (uid_t)fuid;
417 		return;
418 	}
419 
420 	domain = zfs_fuid_find_by_idx(zfsvfs, index);
421 	ASSERT(domain != NULL);
422 
423 	if (type == ZFS_OWNER || type == ZFS_ACE_USER)
424 		(void) kidmap_getuidbysid(domain, FUID_RID(fuid), id);
425 	else
426 		(void) kidmap_getgidbysid(domain, FUID_RID(fuid), id);
427 }
428 
429 /*
430  * Add a FUID node to the list of fuid's being created for this
431  * ACL
432  *
433  * If ACL has multiple domains, then keep only one copy of each unique
434  * domain.
435  */
436 static void
437 zfs_fuid_node_add(zfs_fuid_info_t **fuidpp, const char *domain, uint32_t rid,
438     uint64_t idx, uint64_t id, zfs_fuid_type_t type)
439 {
440 	zfs_fuid_t *fuid;
441 	zfs_fuid_domain_t *fuid_domain;
442 	zfs_fuid_info_t *fuidp;
443 	uint64_t fuididx;
444 	boolean_t found = B_FALSE;
445 
446 	if (*fuidpp == NULL)
447 		*fuidpp = zfs_fuid_info_alloc();
448 
449 	fuidp = *fuidpp;
450 	/*
451 	 * First find fuid domain index in linked list
452 	 *
453 	 * If one isn't found then create an entry.
454 	 */
455 
456 	for (fuididx = 1, fuid_domain = list_head(&fuidp->z_domains);
457 	    fuid_domain; fuid_domain = list_next(&fuidp->z_domains,
458 	    fuid_domain), fuididx++) {
459 		if (idx == fuid_domain->z_domidx) {
460 			found = B_TRUE;
461 			break;
462 		}
463 	}
464 
465 	if (found == B_FALSE) {
466 		fuid_domain = kmem_alloc(sizeof (zfs_fuid_domain_t), KM_SLEEP);
467 		fuid_domain->z_domain = domain;
468 		fuid_domain->z_domidx = idx;
469 		list_insert_tail(&fuidp->z_domains, fuid_domain);
470 		fuidp->z_domain_str_sz += strlen(domain) + 1;
471 		fuidp->z_domain_cnt++;
472 	}
473 
474 	if (type == ZFS_ACE_USER || type == ZFS_ACE_GROUP) {
475 		/*
476 		 * Now allocate fuid entry and add it on the end of the list
477 		 */
478 
479 		fuid = kmem_alloc(sizeof (zfs_fuid_t), KM_SLEEP);
480 		fuid->z_id = id;
481 		fuid->z_domidx = idx;
482 		fuid->z_logfuid = FUID_ENCODE(fuididx, rid);
483 
484 		list_insert_tail(&fuidp->z_fuids, fuid);
485 		fuidp->z_fuid_cnt++;
486 	} else {
487 		if (type == ZFS_OWNER)
488 			fuidp->z_fuid_owner = FUID_ENCODE(fuididx, rid);
489 		else
490 			fuidp->z_fuid_group = FUID_ENCODE(fuididx, rid);
491 	}
492 }
493 
494 /*
495  * Create a file system FUID, based on information in the users cred
496  */
497 uint64_t
498 zfs_fuid_create_cred(zfsvfs_t *zfsvfs, uint64_t id,
499     zfs_fuid_type_t type, dmu_tx_t *tx, cred_t *cr, zfs_fuid_info_t **fuidp)
500 {
501 	uint64_t	idx;
502 	ksid_t		*ksid;
503 	uint32_t	rid;
504 	char 		*kdomain;
505 	const char	*domain;
506 
507 	VERIFY(type == ZFS_OWNER || type == ZFS_GROUP);
508 
509 	if (zfsvfs->z_use_fuids == B_FALSE || !IS_EPHEMERAL(id))
510 		return ((uint64_t)id);
511 
512 	ksid = crgetsid(cr, (type == ZFS_OWNER) ? KSID_OWNER : KSID_GROUP);
513 
514 	VERIFY(ksid != NULL);
515 	rid = ksid_getrid(ksid);
516 	domain = ksid_getdomain(ksid);
517 
518 	idx = zfs_fuid_find_by_domain(zfsvfs, domain, &kdomain, tx);
519 
520 	zfs_fuid_node_add(fuidp, kdomain, rid, idx, id, type);
521 
522 	return (FUID_ENCODE(idx, rid));
523 }
524 
525 /*
526  * Create a file system FUID for an ACL ace
527  * or a chown/chgrp of the file.
528  * This is similar to zfs_fuid_create_cred, except that
529  * we can't find the domain + rid information in the
530  * cred.  Instead we have to query Winchester for the
531  * domain and rid.
532  *
533  * During replay operations the domain+rid information is
534  * found in the zfs_fuid_info_t that the replay code has
535  * attached to the zfsvfs of the file system.
536  */
537 uint64_t
538 zfs_fuid_create(zfsvfs_t *zfsvfs, uint64_t id,
539     zfs_fuid_type_t type, dmu_tx_t *tx, zfs_fuid_info_t **fuidpp)
540 {
541 	const char *domain;
542 	char *kdomain;
543 	uint32_t fuid_idx = FUID_INDEX(id);
544 	uint32_t rid;
545 	idmap_stat status;
546 	uint64_t idx;
547 	boolean_t is_replay = (zfsvfs->z_assign >= TXG_INITIAL);
548 	zfs_fuid_t *zfuid = NULL;
549 	zfs_fuid_info_t *fuidp;
550 
551 	/*
552 	 * If POSIX ID, or entry is already a FUID then
553 	 * just return the id
554 	 */
555 	if (!IS_EPHEMERAL(id) || fuid_idx != 0)
556 		return (id);
557 
558 	if (is_replay) {
559 		fuidp = zfsvfs->z_fuid_replay;
560 
561 		/*
562 		 * If we are passed an ephemeral id, but no
563 		 * fuid_info was logged then return NOBODY.
564 		 * This is most likely a result of idmap service
565 		 * not being available.
566 		 */
567 		if (fuidp == NULL)
568 			return (UID_NOBODY);
569 
570 		switch (type) {
571 		case ZFS_ACE_USER:
572 		case ZFS_ACE_GROUP:
573 			zfuid = list_head(&fuidp->z_fuids);
574 			rid = FUID_RID(zfuid->z_logfuid);
575 			idx = FUID_INDEX(zfuid->z_logfuid);
576 			break;
577 		case ZFS_OWNER:
578 			rid = FUID_RID(fuidp->z_fuid_owner);
579 			idx = FUID_INDEX(fuidp->z_fuid_owner);
580 			break;
581 		case ZFS_GROUP:
582 			rid = FUID_RID(fuidp->z_fuid_group);
583 			idx = FUID_INDEX(fuidp->z_fuid_group);
584 			break;
585 		};
586 		domain = fuidp->z_domain_table[idx -1];
587 	} else {
588 		if (type == ZFS_OWNER || type == ZFS_ACE_USER)
589 			status = kidmap_getsidbyuid(id, &domain, &rid);
590 		else
591 			status = kidmap_getsidbygid(id, &domain, &rid);
592 
593 		if (status != 0) {
594 			/*
595 			 * When returning nobody we will need to
596 			 * make a dummy fuid table entry for logging
597 			 * purposes.
598 			 */
599 			rid = UID_NOBODY;
600 			domain = "";
601 		}
602 
603 	}
604 
605 	idx = zfs_fuid_find_by_domain(zfsvfs, domain, &kdomain, tx);
606 
607 	if (is_replay == B_FALSE)
608 		zfs_fuid_node_add(fuidpp, kdomain, rid, idx, id, type);
609 	else if (zfuid != NULL) {
610 		list_remove(&fuidp->z_fuids, zfuid);
611 		kmem_free(zfuid, sizeof (zfs_fuid_t));
612 	}
613 	return (FUID_ENCODE(idx, rid));
614 }
615 
616 void
617 zfs_fuid_destroy(zfsvfs_t *zfsvfs)
618 {
619 	fuid_domain_t *domnode;
620 	fuid_idx_t *idxnode;
621 	void *cookie;
622 
623 	rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
624 	if (zfsvfs->z_fuid_loaded == B_FALSE) {
625 		rw_exit(&zfsvfs->z_fuid_lock);
626 		return;
627 	}
628 	cookie = NULL;
629 	while (domnode = avl_destroy_nodes(&zfsvfs->z_fuid_domain, &cookie)) {
630 		ksiddomain_rele(domnode->f_ksid);
631 		kmem_free(domnode, sizeof (fuid_domain_t));
632 	}
633 	avl_destroy(&zfsvfs->z_fuid_domain);
634 	cookie = NULL;
635 	while (idxnode = avl_destroy_nodes(&zfsvfs->z_fuid_idx, &cookie))
636 		kmem_free(idxnode, sizeof (fuid_idx_t));
637 	avl_destroy(&zfsvfs->z_fuid_idx);
638 	rw_exit(&zfsvfs->z_fuid_lock);
639 }
640 
641 /*
642  * Allocate zfs_fuid_info for tracking FUIDs created during
643  * zfs_mknode, VOP_SETATTR() or VOP_SETSECATTR()
644  */
645 zfs_fuid_info_t *
646 zfs_fuid_info_alloc(void)
647 {
648 	zfs_fuid_info_t *fuidp;
649 
650 	fuidp = kmem_zalloc(sizeof (zfs_fuid_info_t), KM_SLEEP);
651 	list_create(&fuidp->z_domains, sizeof (zfs_fuid_domain_t),
652 	    offsetof(zfs_fuid_domain_t, z_next));
653 	list_create(&fuidp->z_fuids, sizeof (zfs_fuid_t),
654 	    offsetof(zfs_fuid_t, z_next));
655 	return (fuidp);
656 }
657 
658 /*
659  * Release all memory associated with zfs_fuid_info_t
660  */
661 void
662 zfs_fuid_info_free(zfs_fuid_info_t *fuidp)
663 {
664 	zfs_fuid_t *zfuid;
665 	zfs_fuid_domain_t *zdomain;
666 
667 	while ((zfuid = list_head(&fuidp->z_fuids)) != NULL) {
668 		list_remove(&fuidp->z_fuids, zfuid);
669 		kmem_free(zfuid, sizeof (zfs_fuid_t));
670 	}
671 
672 	if (fuidp->z_domain_table != NULL)
673 		kmem_free(fuidp->z_domain_table,
674 		    (sizeof (char **)) * fuidp->z_domain_cnt);
675 
676 	while ((zdomain = list_head(&fuidp->z_domains)) != NULL) {
677 		list_remove(&fuidp->z_domains, zdomain);
678 		kmem_free(zdomain, sizeof (zfs_fuid_domain_t));
679 	}
680 
681 	kmem_free(fuidp, sizeof (zfs_fuid_info_t));
682 }
683 
684 /*
685  * Check to see if id is a groupmember.  If cred
686  * has ksid info then sidlist is checked first
687  * and if still not found then POSIX groups are checked
688  *
689  * Will use a straight FUID compare when possible.
690  */
691 boolean_t
692 zfs_groupmember(zfsvfs_t *zfsvfs, uint64_t id, cred_t *cr)
693 {
694 	ksid_t		*ksid = crgetsid(cr, KSID_GROUP);
695 	uid_t		gid;
696 
697 	if (ksid) {
698 		int 		i;
699 		ksid_t		*ksid_groups;
700 		ksidlist_t	*ksidlist = crgetsidlist(cr);
701 		uint32_t	idx = FUID_INDEX(id);
702 		uint32_t	rid = FUID_RID(id);
703 
704 		ASSERT(ksidlist);
705 		ksid_groups = ksidlist->ksl_sids;
706 
707 		for (i = 0; i != ksidlist->ksl_nsid; i++) {
708 			if (idx == 0) {
709 				if (id != IDMAP_WK_CREATOR_GROUP_GID &&
710 				    id == ksid_groups[i].ks_id) {
711 					return (B_TRUE);
712 				}
713 			} else {
714 				char *domain;
715 
716 				domain = zfs_fuid_find_by_idx(zfsvfs, idx);
717 				ASSERT(domain != NULL);
718 
719 				if (strcmp(domain,
720 				    IDMAP_WK_CREATOR_SID_AUTHORITY) == 0) {
721 					return (B_FALSE);
722 				}
723 
724 				if ((strcmp(domain,
725 				    ksid_groups[i].ks_domain->kd_name) == 0) &&
726 				    rid == ksid_groups[i].ks_rid) {
727 					return (B_TRUE);
728 				}
729 			}
730 		}
731 	}
732 
733 	/*
734 	 * Not found in ksidlist, check posix groups
735 	 */
736 	zfs_fuid_map_id(zfsvfs, id, ZFS_GROUP, &gid);
737 	return (groupmember(gid, cr));
738 }
739