xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_fuid.c (revision 02bc52be7430b2f7fafe1a2c981bff49ef11d6fa)
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 	searchnode.f_ksid = ksid_lookupdomain(domain);
221 	if (retdomain) {
222 		*retdomain = searchnode.f_ksid->kd_name;
223 	}
224 	if (zfsvfs->z_fuid_loaded == B_FALSE)
225 		zfs_fuid_init(zfsvfs, tx);
226 
227 	rw_enter(&zfsvfs->z_fuid_lock, RW_READER);
228 	findnode = avl_find(&zfsvfs->z_fuid_domain, &searchnode, &loc);
229 	rw_exit(&zfsvfs->z_fuid_lock);
230 
231 	if (findnode) {
232 		ksiddomain_rele(searchnode.f_ksid);
233 		return (findnode->f_idx);
234 	} else {
235 		fuid_domain_t *domnode;
236 		fuid_idx_t *newidxnode;
237 		nvlist_t *nvp;
238 		nvlist_t **fuids;
239 		uint64_t retidx;
240 		size_t nvsize = 0;
241 		char *packed;
242 		dmu_buf_t *db;
243 		int i = 0;
244 
245 		domnode = kmem_alloc(sizeof (fuid_domain_t), KM_SLEEP);
246 		domnode->f_ksid = searchnode.f_ksid;
247 		domnode->f_offset = 0;
248 
249 		newidxnode = kmem_alloc(sizeof (fuid_idx_t), KM_SLEEP);
250 		newidxnode->f_domain = domnode;
251 
252 		rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
253 		retidx = domnode->f_idx = newidxnode->f_idx =
254 		    avl_numnodes(&zfsvfs->z_fuid_idx) + 1;
255 
256 		avl_add(&zfsvfs->z_fuid_domain, domnode);
257 		avl_add(&zfsvfs->z_fuid_idx, newidxnode);
258 		/*
259 		 * Now resync the on-disk nvlist.
260 		 */
261 		VERIFY(nvlist_alloc(&nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
262 
263 		domnode = avl_first(&zfsvfs->z_fuid_domain);
264 		fuids = kmem_alloc(retidx * sizeof (void *), KM_SLEEP);
265 		while (domnode) {
266 			VERIFY(nvlist_alloc(&fuids[i],
267 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
268 			VERIFY(nvlist_add_uint64(fuids[i], FUID_IDX,
269 			    domnode->f_idx) == 0);
270 			VERIFY(nvlist_add_uint64(fuids[i], FUID_OFFSET,
271 			    domnode->f_offset) == 0);
272 			VERIFY(nvlist_add_string(fuids[i++], FUID_DOMAIN,
273 			    domnode->f_ksid->kd_name) == 0);
274 			domnode = AVL_NEXT(&zfsvfs->z_fuid_domain, domnode);
275 		}
276 		VERIFY(nvlist_add_nvlist_array(nvp, FUID_NVP_ARRAY,
277 		    fuids, retidx) == 0);
278 		for (i = 0; i != retidx; i++)
279 			nvlist_free(fuids[i]);
280 		kmem_free(fuids, retidx * sizeof (void *));
281 		VERIFY(nvlist_size(nvp, &nvsize, NV_ENCODE_XDR) == 0);
282 		packed = kmem_alloc(nvsize, KM_SLEEP);
283 		VERIFY(nvlist_pack(nvp, &packed, &nvsize,
284 		    NV_ENCODE_XDR, KM_SLEEP) == 0);
285 		nvlist_free(nvp);
286 		dmu_write(zfsvfs->z_os, zfsvfs->z_fuid_obj, 0, nvsize,
287 		    packed, tx);
288 		kmem_free(packed, nvsize);
289 		VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, zfsvfs->z_fuid_obj,
290 		    FTAG, &db));
291 		dmu_buf_will_dirty(db, tx);
292 		*(uint64_t *)db->db_data = nvsize;
293 		dmu_buf_rele(db, FTAG);
294 
295 		rw_exit(&zfsvfs->z_fuid_lock);
296 		return (retidx);
297 	}
298 }
299 
300 /*
301  * Query domain table by index, returning domain string
302  *
303  * Returns a pointer from an avl node of the domain string.
304  *
305  */
306 char *
307 zfs_fuid_find_by_idx(zfsvfs_t *zfsvfs, uint64_t idx)
308 {
309 	fuid_idx_t searchnode, *findnode;
310 	avl_index_t loc;
311 
312 	if (idx == 0 || zfsvfs->z_use_fuids == B_FALSE)
313 		return (NULL);
314 
315 	if (zfsvfs->z_fuid_loaded == B_FALSE)
316 		zfs_fuid_init(zfsvfs, NULL);
317 
318 	searchnode.f_idx = idx;
319 
320 	rw_enter(&zfsvfs->z_fuid_lock, RW_READER);
321 	findnode = avl_find(&zfsvfs->z_fuid_idx, &searchnode, &loc);
322 	rw_exit(&zfsvfs->z_fuid_lock);
323 
324 	ASSERT(findnode);
325 	return (findnode->f_domain->f_ksid->kd_name);
326 }
327 
328 void
329 zfs_fuid_get_mappings(zfs_fuid_hdl_t *hdl)
330 {
331 	VERIFY(hdl != NULL);
332 	if (hdl->z_map_needed == B_FALSE)
333 		return;
334 
335 	(void) kidmap_get_mappings(hdl->z_hdl);
336 
337 	kidmap_get_destroy(hdl->z_hdl);
338 	hdl->z_hdl = NULL;
339 	hdl->z_map_needed = B_FALSE;
340 }
341 
342 void
343 zfs_fuid_queue_map_id(zfsvfs_t *zfsvfs, zfs_fuid_hdl_t *hdl,
344     uint64_t fuid, zfs_fuid_type_t type, uid_t *id)
345 {
346 	uint32_t index = FUID_INDEX(fuid);
347 	char *domain;
348 	int status;
349 
350 	VERIFY(hdl);
351 
352 	if (index == 0 || zfsvfs->z_use_fuids == B_FALSE) {
353 		*id = (uid_t)fuid;
354 		return;
355 	}
356 
357 	if (hdl->z_hdl == NULL) {
358 		hdl->z_hdl = kidmap_get_create();
359 		hdl->z_map_needed = B_TRUE;
360 	}
361 
362 	domain = zfs_fuid_find_by_idx(zfsvfs, index);
363 	ASSERT(domain != NULL);
364 
365 	if (type == ZFS_OWNER || type == ZFS_ACE_USER)
366 		status = kidmap_batch_getuidbysid(hdl->z_hdl, domain,
367 		    FUID_RID(fuid), id, &hdl->z_status);
368 	else
369 		status = kidmap_batch_getgidbysid(hdl->z_hdl, domain,
370 		    FUID_RID(fuid), id, &hdl->z_status);
371 	ASSERT(status == 0);
372 }
373 
374 void
375 zfs_fuid_map_ids(znode_t *zp, uid_t *uid, uid_t *gid)
376 {
377 	uint32_t uid_index = FUID_INDEX(zp->z_phys->zp_uid);
378 	uint32_t gid_index = FUID_INDEX(zp->z_phys->zp_gid);
379 
380 	/* Favor the common case, neither will be ephemeral */
381 	if (uid_index == 0 && gid_index == 0) {
382 		*uid = zp->z_phys->zp_uid;
383 		*gid = zp->z_phys->zp_gid;
384 		return;
385 	} else {
386 		zfs_fuid_hdl_t hdl = { 0 };
387 
388 		zfs_fuid_queue_map_id(zp->z_zfsvfs, &hdl,
389 		    zp->z_phys->zp_uid, ZFS_OWNER, uid);
390 
391 		zfs_fuid_queue_map_id(zp->z_zfsvfs, &hdl,
392 		    zp->z_phys->zp_gid, ZFS_GROUP, gid);
393 
394 		zfs_fuid_get_mappings(&hdl);
395 	}
396 }
397 
398 void
399 zfs_fuid_map_id(zfsvfs_t *zfsvfs, uint64_t fuid,
400     zfs_fuid_type_t type, uid_t *id)
401 {
402 	uint32_t index = FUID_INDEX(fuid);
403 	char *domain;
404 
405 	if (index == 0) {
406 		*id = (uid_t)fuid;
407 		return;
408 	}
409 
410 	domain = zfs_fuid_find_by_idx(zfsvfs, index);
411 	ASSERT(domain != NULL);
412 
413 	if (type == ZFS_OWNER || type == ZFS_ACE_USER)
414 		(void) kidmap_getuidbysid(domain, FUID_RID(fuid), id);
415 	else
416 		(void) kidmap_getgidbysid(domain, FUID_RID(fuid), id);
417 }
418 
419 /*
420  * Add a FUID node to the list of fuid's being created for this
421  * ACL
422  *
423  * If ACL has multiple domains, then keep only one copy of each unique
424  * domain.
425  */
426 static void
427 zfs_fuid_node_add(zfs_fuid_info_t **fuidpp, const char *domain, uint32_t rid,
428     uint64_t idx, uint64_t id, zfs_fuid_type_t type)
429 {
430 	zfs_fuid_t *fuid;
431 	zfs_fuid_domain_t *fuid_domain;
432 	zfs_fuid_info_t *fuidp;
433 	uint64_t fuididx;
434 	boolean_t found = B_FALSE;
435 
436 	if (*fuidpp == NULL)
437 		*fuidpp = zfs_fuid_info_alloc();
438 
439 	fuidp = *fuidpp;
440 	/*
441 	 * First find fuid domain index in linked list
442 	 *
443 	 * If one isn't found then create an entry.
444 	 */
445 
446 	for (fuididx = 1, fuid_domain = list_head(&fuidp->z_domains);
447 	    fuid_domain; fuid_domain = list_next(&fuidp->z_domains,
448 	    fuid_domain), fuididx++) {
449 		if (idx == fuid_domain->z_domidx) {
450 			found = B_TRUE;
451 			break;
452 		}
453 	}
454 
455 	if (found == B_FALSE) {
456 		fuid_domain = kmem_alloc(sizeof (zfs_fuid_domain_t), KM_SLEEP);
457 		fuid_domain->z_domain = domain;
458 		fuid_domain->z_domidx = idx;
459 		list_insert_tail(&fuidp->z_domains, fuid_domain);
460 		fuidp->z_domain_str_sz += strlen(domain) + 1;
461 		fuidp->z_domain_cnt++;
462 	}
463 
464 	if (type == ZFS_ACE_USER || type == ZFS_ACE_GROUP) {
465 		/*
466 		 * Now allocate fuid entry and add it on the end of the list
467 		 */
468 
469 		fuid = kmem_alloc(sizeof (zfs_fuid_t), KM_SLEEP);
470 		fuid->z_id = id;
471 		fuid->z_domidx = idx;
472 		fuid->z_logfuid = FUID_ENCODE(fuididx, rid);
473 
474 		list_insert_tail(&fuidp->z_fuids, fuid);
475 		fuidp->z_fuid_cnt++;
476 	} else {
477 		if (type == ZFS_OWNER)
478 			fuidp->z_fuid_owner = FUID_ENCODE(fuididx, rid);
479 		else
480 			fuidp->z_fuid_group = FUID_ENCODE(fuididx, rid);
481 	}
482 }
483 
484 /*
485  * Create a file system FUID
486  *
487  * During a replay operation the id will be incorrect and
488  * will be ignored.  In this case replay must be true and the
489  * cred will have a ksid_t attached to it.
490  *
491  * A mapped uid/gid would have a ksid_t attached to the cred.
492  */
493 uint64_t
494 zfs_fuid_create_cred(zfsvfs_t *zfsvfs, uint64_t id,
495     zfs_fuid_type_t type, dmu_tx_t *tx, cred_t *cr, zfs_fuid_info_t **fuidp)
496 {
497 	uint64_t	idx;
498 	ksid_t		*ksid;
499 	uint32_t	rid;
500 	char 		*kdomain;
501 	const char	*domain;
502 
503 	VERIFY(type == ZFS_OWNER || type == ZFS_GROUP);
504 
505 	if (zfsvfs->z_use_fuids == B_FALSE || !IS_EPHEMERAL(id))
506 		return ((uint64_t)id);
507 
508 	ksid = crgetsid(cr, (type == ZFS_OWNER) ? KSID_OWNER : KSID_GROUP);
509 
510 	VERIFY(ksid != NULL);
511 	rid = ksid_getrid(ksid);
512 	domain = ksid_getdomain(ksid);
513 
514 	idx = zfs_fuid_find_by_domain(zfsvfs, domain, &kdomain, tx);
515 
516 	zfs_fuid_node_add(fuidp, kdomain, rid, idx, id, type);
517 
518 	return (FUID_ENCODE(idx, rid));
519 }
520 
521 /*
522  * Create a file system FUID for an ACL ace
523  * or a chown/chgrp of the file.
524  * This is similar to zfs_fuid_create_cred, except that
525  * we can't find the domain + rid information in the
526  * cred.  Instead we have to query Winchester for the
527  * domain and rid.
528  */
529 uint64_t
530 zfs_fuid_create(zfsvfs_t *zfsvfs, uint64_t id,
531     zfs_fuid_type_t type, dmu_tx_t *tx, zfs_fuid_info_t **fuidpp)
532 {
533 	const char *domain;
534 	char *kdomain;
535 	uint32_t fuid_idx = FUID_INDEX(id);
536 	uint32_t rid;
537 	idmap_stat status;
538 	uint64_t idx;
539 	boolean_t is_replay = (zfsvfs->z_assign >= TXG_INITIAL);
540 	zfs_fuid_t *zfuid = NULL;
541 	zfs_fuid_info_t *fuidp;
542 
543 	/*
544 	 * If POSIX ID, or entry is already a FUID then
545 	 * just return the id
546 	 */
547 	if (!IS_EPHEMERAL(id) || fuid_idx != 0)
548 		return (id);
549 
550 	if (is_replay) {
551 		fuidp = zfsvfs->z_fuid_replay;
552 
553 		/*
554 		 * If we are passed an ephemeral id, but no
555 		 * fuid_info was logged then return NOBODY.
556 		 * This is most likely a result of idmap service
557 		 * not being available.
558 		 */
559 		if (fuidp == NULL)
560 			return (UID_NOBODY);
561 
562 		switch (type) {
563 		case ZFS_ACE_USER:
564 		case ZFS_ACE_GROUP:
565 			zfuid = list_head(&fuidp->z_fuids);
566 			rid = FUID_RID(zfuid->z_logfuid);
567 			idx = FUID_INDEX(zfuid->z_logfuid);
568 			break;
569 		case ZFS_OWNER:
570 			rid = FUID_RID(fuidp->z_fuid_owner);
571 			idx = FUID_INDEX(fuidp->z_fuid_owner);
572 			break;
573 		case ZFS_GROUP:
574 			rid = FUID_RID(fuidp->z_fuid_group);
575 			idx = FUID_INDEX(fuidp->z_fuid_group);
576 			break;
577 		};
578 		domain = fuidp->z_domain_table[idx -1];
579 	} else {
580 		if (type == ZFS_OWNER || type == ZFS_ACE_USER)
581 			status = kidmap_getsidbyuid(id, &domain, &rid);
582 		else
583 			status = kidmap_getsidbygid(id, &domain, &rid);
584 
585 		if (status != 0)
586 			return (UID_NOBODY);
587 
588 	}
589 
590 	idx = zfs_fuid_find_by_domain(zfsvfs, domain, &kdomain, tx);
591 
592 	if (is_replay == B_FALSE)
593 		zfs_fuid_node_add(fuidpp, kdomain, rid, idx, id, type);
594 	else if (zfuid != NULL) {
595 		list_remove(&fuidp->z_fuids, zfuid);
596 		kmem_free(zfuid, sizeof (zfs_fuid_t));
597 	}
598 	return (FUID_ENCODE(idx, rid));
599 }
600 
601 void
602 zfs_fuid_destroy(zfsvfs_t *zfsvfs)
603 {
604 	fuid_domain_t *domnode;
605 	fuid_idx_t *idxnode;
606 	void *cookie;
607 
608 	rw_enter(&zfsvfs->z_fuid_lock, RW_WRITER);
609 	if (zfsvfs->z_fuid_loaded == B_FALSE) {
610 		rw_exit(&zfsvfs->z_fuid_lock);
611 		return;
612 	}
613 	cookie = NULL;
614 	while (domnode = avl_destroy_nodes(&zfsvfs->z_fuid_domain, &cookie)) {
615 		ksiddomain_rele(domnode->f_ksid);
616 		kmem_free(domnode, sizeof (fuid_domain_t));
617 	}
618 	avl_destroy(&zfsvfs->z_fuid_domain);
619 	cookie = NULL;
620 	while (idxnode = avl_destroy_nodes(&zfsvfs->z_fuid_idx, &cookie))
621 		kmem_free(idxnode, sizeof (fuid_idx_t));
622 	avl_destroy(&zfsvfs->z_fuid_idx);
623 	rw_exit(&zfsvfs->z_fuid_lock);
624 }
625 
626 /*
627  * Allocate zfs_fuid_info for tracking FUIDs created during
628  * zfs_mknode, VOP_SETATTR() or VOP_SETSECATTR()
629  */
630 zfs_fuid_info_t *
631 zfs_fuid_info_alloc(void)
632 {
633 	zfs_fuid_info_t *fuidp;
634 
635 	fuidp = kmem_zalloc(sizeof (zfs_fuid_info_t), KM_SLEEP);
636 	list_create(&fuidp->z_domains, sizeof (zfs_fuid_domain_t),
637 	    offsetof(zfs_fuid_domain_t, z_next));
638 	list_create(&fuidp->z_fuids, sizeof (zfs_fuid_t),
639 	    offsetof(zfs_fuid_t, z_next));
640 	return (fuidp);
641 }
642 
643 /*
644  * Release all memory associated with zfs_fuid_info_t
645  */
646 void
647 zfs_fuid_info_free(zfs_fuid_info_t *fuidp)
648 {
649 	zfs_fuid_t *zfuid;
650 	zfs_fuid_domain_t *zdomain;
651 
652 	while ((zfuid = list_head(&fuidp->z_fuids)) != NULL) {
653 		list_remove(&fuidp->z_fuids, zfuid);
654 		kmem_free(zfuid, sizeof (zfs_fuid_t));
655 	}
656 
657 	if (fuidp->z_domain_table != NULL)
658 		kmem_free(fuidp->z_domain_table,
659 		    (sizeof (char **)) * fuidp->z_domain_cnt);
660 
661 	while ((zdomain = list_head(&fuidp->z_domains)) != NULL) {
662 		list_remove(&fuidp->z_domains, zdomain);
663 		kmem_free(zdomain, sizeof (zfs_fuid_domain_t));
664 	}
665 
666 	kmem_free(fuidp, sizeof (zfs_fuid_info_t));
667 }
668 
669 /*
670  * Check to see if id is a groupmember.  If cred
671  * has ksid info then sidlist is checked first
672  * and if still not found then POSIX groups are checked
673  *
674  * Will use a straight FUID compare when possible.
675  */
676 boolean_t
677 zfs_groupmember(zfsvfs_t *zfsvfs, uint64_t id, cred_t *cr)
678 {
679 	ksid_t		*ksid = crgetsid(cr, KSID_GROUP);
680 	uid_t		gid;
681 
682 	if (ksid) {
683 		int 		i;
684 		ksid_t		*ksid_groups;
685 		ksidlist_t	*ksidlist = crgetsidlist(cr);
686 		uint32_t	idx = FUID_INDEX(id);
687 		uint32_t	rid = FUID_RID(id);
688 
689 		ASSERT(ksidlist);
690 		ksid_groups = ksidlist->ksl_sids;
691 
692 		for (i = 0; i != ksidlist->ksl_nsid; i++) {
693 			if (idx == 0) {
694 				if (id != IDMAP_WK_CREATOR_GROUP_GID &&
695 				    id == ksid_groups[i].ks_id) {
696 					return (B_TRUE);
697 				}
698 			} else {
699 				char *domain;
700 
701 				domain = zfs_fuid_find_by_idx(zfsvfs, idx);
702 				ASSERT(domain != NULL);
703 
704 				if (strcmp(domain,
705 				    IDMAP_WK_CREATOR_SID_AUTHORITY) == 0) {
706 					return (B_FALSE);
707 				}
708 
709 				if ((strcmp(domain,
710 				    ksid_groups[i].ks_domain->kd_name) == 0) &&
711 				    rid == ksid_groups[i].ks_rid) {
712 					return (B_TRUE);
713 				}
714 			}
715 		}
716 	}
717 
718 	/*
719 	 * Not found in ksidlist, check posix groups
720 	 */
721 	zfs_fuid_map_id(zfsvfs, id, ZFS_GROUP, &gid);
722 
723 	return (groupmember(gid, cr));
724 }
725