xref: /titanic_50/usr/src/uts/common/fs/zfs/zfs_fuid.c (revision dc20a3024900c47dd2ee44b9707e6df38f7d62a5)
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 2008 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, cred_t *cr, 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(crgetzone(cr));
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, cred_t *cr, 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, cr, ZFS_OWNER, uid);
400 
401 		zfs_fuid_queue_map_id(zp->z_zfsvfs, &hdl,
402 		    zp->z_phys->zp_gid, cr, 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     cred_t *cr, 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(crgetzone(cr), domain,
425 		    FUID_RID(fuid), id);
426 	else
427 		(void) kidmap_getgidbysid(crgetzone(cr), domain,
428 		    FUID_RID(fuid), id);
429 }
430 
431 /*
432  * Add a FUID node to the list of fuid's being created for this
433  * ACL
434  *
435  * If ACL has multiple domains, then keep only one copy of each unique
436  * domain.
437  */
438 static void
439 zfs_fuid_node_add(zfs_fuid_info_t **fuidpp, const char *domain, uint32_t rid,
440     uint64_t idx, uint64_t id, zfs_fuid_type_t type)
441 {
442 	zfs_fuid_t *fuid;
443 	zfs_fuid_domain_t *fuid_domain;
444 	zfs_fuid_info_t *fuidp;
445 	uint64_t fuididx;
446 	boolean_t found = B_FALSE;
447 
448 	if (*fuidpp == NULL)
449 		*fuidpp = zfs_fuid_info_alloc();
450 
451 	fuidp = *fuidpp;
452 	/*
453 	 * First find fuid domain index in linked list
454 	 *
455 	 * If one isn't found then create an entry.
456 	 */
457 
458 	for (fuididx = 1, fuid_domain = list_head(&fuidp->z_domains);
459 	    fuid_domain; fuid_domain = list_next(&fuidp->z_domains,
460 	    fuid_domain), fuididx++) {
461 		if (idx == fuid_domain->z_domidx) {
462 			found = B_TRUE;
463 			break;
464 		}
465 	}
466 
467 	if (found == B_FALSE) {
468 		fuid_domain = kmem_alloc(sizeof (zfs_fuid_domain_t), KM_SLEEP);
469 		fuid_domain->z_domain = domain;
470 		fuid_domain->z_domidx = idx;
471 		list_insert_tail(&fuidp->z_domains, fuid_domain);
472 		fuidp->z_domain_str_sz += strlen(domain) + 1;
473 		fuidp->z_domain_cnt++;
474 	}
475 
476 	if (type == ZFS_ACE_USER || type == ZFS_ACE_GROUP) {
477 		/*
478 		 * Now allocate fuid entry and add it on the end of the list
479 		 */
480 
481 		fuid = kmem_alloc(sizeof (zfs_fuid_t), KM_SLEEP);
482 		fuid->z_id = id;
483 		fuid->z_domidx = idx;
484 		fuid->z_logfuid = FUID_ENCODE(fuididx, rid);
485 
486 		list_insert_tail(&fuidp->z_fuids, fuid);
487 		fuidp->z_fuid_cnt++;
488 	} else {
489 		if (type == ZFS_OWNER)
490 			fuidp->z_fuid_owner = FUID_ENCODE(fuididx, rid);
491 		else
492 			fuidp->z_fuid_group = FUID_ENCODE(fuididx, rid);
493 	}
494 }
495 
496 /*
497  * Create a file system FUID, based on information in the users cred
498  */
499 uint64_t
500 zfs_fuid_create_cred(zfsvfs_t *zfsvfs, uint64_t id,
501     zfs_fuid_type_t type, dmu_tx_t *tx, cred_t *cr, zfs_fuid_info_t **fuidp)
502 {
503 	uint64_t	idx;
504 	ksid_t		*ksid;
505 	uint32_t	rid;
506 	char 		*kdomain;
507 	const char	*domain;
508 
509 	VERIFY(type == ZFS_OWNER || type == ZFS_GROUP);
510 
511 	if (zfsvfs->z_use_fuids == B_FALSE || !IS_EPHEMERAL(id))
512 		return ((uint64_t)id);
513 
514 	ksid = crgetsid(cr, (type == ZFS_OWNER) ? KSID_OWNER : KSID_GROUP);
515 
516 	VERIFY(ksid != NULL);
517 	rid = ksid_getrid(ksid);
518 	domain = ksid_getdomain(ksid);
519 
520 	idx = zfs_fuid_find_by_domain(zfsvfs, domain, &kdomain, tx);
521 
522 	zfs_fuid_node_add(fuidp, kdomain, rid, idx, id, type);
523 
524 	return (FUID_ENCODE(idx, rid));
525 }
526 
527 /*
528  * Create a file system FUID for an ACL ace
529  * or a chown/chgrp of the file.
530  * This is similar to zfs_fuid_create_cred, except that
531  * we can't find the domain + rid information in the
532  * cred.  Instead we have to query Winchester for the
533  * domain and rid.
534  *
535  * During replay operations the domain+rid information is
536  * found in the zfs_fuid_info_t that the replay code has
537  * attached to the zfsvfs of the file system.
538  */
539 uint64_t
540 zfs_fuid_create(zfsvfs_t *zfsvfs, uint64_t id, cred_t *cr,
541     zfs_fuid_type_t type, dmu_tx_t *tx, zfs_fuid_info_t **fuidpp)
542 {
543 	const char *domain;
544 	char *kdomain;
545 	uint32_t fuid_idx = FUID_INDEX(id);
546 	uint32_t rid;
547 	idmap_stat status;
548 	uint64_t idx;
549 	boolean_t is_replay = (zfsvfs->z_assign >= TXG_INITIAL);
550 	zfs_fuid_t *zfuid = NULL;
551 	zfs_fuid_info_t *fuidp;
552 
553 	/*
554 	 * If POSIX ID, or entry is already a FUID then
555 	 * just return the id
556 	 */
557 	if (!IS_EPHEMERAL(id) || fuid_idx != 0)
558 		return (id);
559 
560 	if (is_replay) {
561 		fuidp = zfsvfs->z_fuid_replay;
562 
563 		/*
564 		 * If we are passed an ephemeral id, but no
565 		 * fuid_info was logged then return NOBODY.
566 		 * This is most likely a result of idmap service
567 		 * not being available.
568 		 */
569 		if (fuidp == NULL)
570 			return (UID_NOBODY);
571 
572 		switch (type) {
573 		case ZFS_ACE_USER:
574 		case ZFS_ACE_GROUP:
575 			zfuid = list_head(&fuidp->z_fuids);
576 			rid = FUID_RID(zfuid->z_logfuid);
577 			idx = FUID_INDEX(zfuid->z_logfuid);
578 			break;
579 		case ZFS_OWNER:
580 			rid = FUID_RID(fuidp->z_fuid_owner);
581 			idx = FUID_INDEX(fuidp->z_fuid_owner);
582 			break;
583 		case ZFS_GROUP:
584 			rid = FUID_RID(fuidp->z_fuid_group);
585 			idx = FUID_INDEX(fuidp->z_fuid_group);
586 			break;
587 		};
588 		domain = fuidp->z_domain_table[idx -1];
589 	} else {
590 		if (type == ZFS_OWNER || type == ZFS_ACE_USER)
591 			status = kidmap_getsidbyuid(crgetzone(cr), id,
592 			    &domain, &rid);
593 		else
594 			status = kidmap_getsidbygid(crgetzone(cr), id,
595 			    &domain, &rid);
596 
597 		if (status != 0) {
598 			/*
599 			 * When returning nobody we will need to
600 			 * make a dummy fuid table entry for logging
601 			 * purposes.
602 			 */
603 			rid = UID_NOBODY;
604 			domain = "";
605 		}
606 
607 	}
608 
609 	idx = zfs_fuid_find_by_domain(zfsvfs, domain, &kdomain, tx);
610 
611 	if (is_replay == B_FALSE)
612 		zfs_fuid_node_add(fuidpp, kdomain, rid, idx, id, type);
613 	else if (zfuid != NULL) {
614 		list_remove(&fuidp->z_fuids, zfuid);
615 		kmem_free(zfuid, sizeof (zfs_fuid_t));
616 	}
617 	return (FUID_ENCODE(idx, rid));
618 }
619 
620 void
621 zfs_fuid_destroy(zfsvfs_t *zfsvfs)
622 {
623 	fuid_domain_t *domnode;
624 	fuid_idx_t *idxnode;
625 	void *cookie;
626 
627 	rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
628 	if (zfsvfs->z_fuid_loaded == B_FALSE) {
629 		rw_exit(&zfsvfs->z_fuid_lock);
630 		return;
631 	}
632 	cookie = NULL;
633 	while (domnode = avl_destroy_nodes(&zfsvfs->z_fuid_domain, &cookie)) {
634 		ksiddomain_rele(domnode->f_ksid);
635 		kmem_free(domnode, sizeof (fuid_domain_t));
636 	}
637 	avl_destroy(&zfsvfs->z_fuid_domain);
638 	cookie = NULL;
639 	while (idxnode = avl_destroy_nodes(&zfsvfs->z_fuid_idx, &cookie))
640 		kmem_free(idxnode, sizeof (fuid_idx_t));
641 	avl_destroy(&zfsvfs->z_fuid_idx);
642 	rw_exit(&zfsvfs->z_fuid_lock);
643 }
644 
645 /*
646  * Allocate zfs_fuid_info for tracking FUIDs created during
647  * zfs_mknode, VOP_SETATTR() or VOP_SETSECATTR()
648  */
649 zfs_fuid_info_t *
650 zfs_fuid_info_alloc(void)
651 {
652 	zfs_fuid_info_t *fuidp;
653 
654 	fuidp = kmem_zalloc(sizeof (zfs_fuid_info_t), KM_SLEEP);
655 	list_create(&fuidp->z_domains, sizeof (zfs_fuid_domain_t),
656 	    offsetof(zfs_fuid_domain_t, z_next));
657 	list_create(&fuidp->z_fuids, sizeof (zfs_fuid_t),
658 	    offsetof(zfs_fuid_t, z_next));
659 	return (fuidp);
660 }
661 
662 /*
663  * Release all memory associated with zfs_fuid_info_t
664  */
665 void
666 zfs_fuid_info_free(zfs_fuid_info_t *fuidp)
667 {
668 	zfs_fuid_t *zfuid;
669 	zfs_fuid_domain_t *zdomain;
670 
671 	while ((zfuid = list_head(&fuidp->z_fuids)) != NULL) {
672 		list_remove(&fuidp->z_fuids, zfuid);
673 		kmem_free(zfuid, sizeof (zfs_fuid_t));
674 	}
675 
676 	if (fuidp->z_domain_table != NULL)
677 		kmem_free(fuidp->z_domain_table,
678 		    (sizeof (char **)) * fuidp->z_domain_cnt);
679 
680 	while ((zdomain = list_head(&fuidp->z_domains)) != NULL) {
681 		list_remove(&fuidp->z_domains, zdomain);
682 		kmem_free(zdomain, sizeof (zfs_fuid_domain_t));
683 	}
684 
685 	kmem_free(fuidp, sizeof (zfs_fuid_info_t));
686 }
687 
688 /*
689  * Check to see if id is a groupmember.  If cred
690  * has ksid info then sidlist is checked first
691  * and if still not found then POSIX groups are checked
692  *
693  * Will use a straight FUID compare when possible.
694  */
695 boolean_t
696 zfs_groupmember(zfsvfs_t *zfsvfs, uint64_t id, cred_t *cr)
697 {
698 	ksid_t		*ksid = crgetsid(cr, KSID_GROUP);
699 	uid_t		gid;
700 
701 	if (ksid) {
702 		int 		i;
703 		ksid_t		*ksid_groups;
704 		ksidlist_t	*ksidlist = crgetsidlist(cr);
705 		uint32_t	idx = FUID_INDEX(id);
706 		uint32_t	rid = FUID_RID(id);
707 
708 		ASSERT(ksidlist);
709 		ksid_groups = ksidlist->ksl_sids;
710 
711 		for (i = 0; i != ksidlist->ksl_nsid; i++) {
712 			if (idx == 0) {
713 				if (id != IDMAP_WK_CREATOR_GROUP_GID &&
714 				    id == ksid_groups[i].ks_id) {
715 					return (B_TRUE);
716 				}
717 			} else {
718 				char *domain;
719 
720 				domain = zfs_fuid_find_by_idx(zfsvfs, idx);
721 				ASSERT(domain != NULL);
722 
723 				if (strcmp(domain,
724 				    IDMAP_WK_CREATOR_SID_AUTHORITY) == 0) {
725 					return (B_FALSE);
726 				}
727 
728 				if ((strcmp(domain,
729 				    ksid_groups[i].ks_domain->kd_name) == 0) &&
730 				    rid == ksid_groups[i].ks_rid) {
731 					return (B_TRUE);
732 				}
733 			}
734 		}
735 	}
736 
737 	/*
738 	 * Not found in ksidlist, check posix groups
739 	 */
740 	zfs_fuid_map_id(zfsvfs, id, cr, ZFS_GROUP, &gid);
741 	return (groupmember(gid, cr));
742 }
743