10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*13093SRoger.Faulkner@Oracle.COM
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <fmd_alloc.h>
270Sstevel@tonic-gate #include <fmd_subr.h>
280Sstevel@tonic-gate #include <fmd_conf.h>
290Sstevel@tonic-gate #include <fmd_error.h>
300Sstevel@tonic-gate #include <fmd_string.h>
310Sstevel@tonic-gate #include <fmd_idspace.h>
320Sstevel@tonic-gate #include <fmd.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate fmd_idspace_t *
fmd_idspace_create(const char * name,id_t min,id_t max)350Sstevel@tonic-gate fmd_idspace_create(const char *name, id_t min, id_t max)
360Sstevel@tonic-gate {
370Sstevel@tonic-gate fmd_idspace_t *ids = fmd_alloc(sizeof (fmd_idspace_t), FMD_SLEEP);
380Sstevel@tonic-gate uint_t ids_avg, ids_max, hashlen, hashmax;
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate * Dynamically size the hash table bucket array based on the desired
420Sstevel@tonic-gate * chain length. We hash by indexing on the low-order bits.
430Sstevel@tonic-gate * Do not permit the hash bucket array to exceed a reasonable size.
440Sstevel@tonic-gate */
450Sstevel@tonic-gate ASSERT(min >= 0 && max >= 0);
460Sstevel@tonic-gate ASSERT(max >= min);
470Sstevel@tonic-gate
480Sstevel@tonic-gate (void) fmd_conf_getprop(fmd.d_conf, "ids.avg", &ids_avg);
490Sstevel@tonic-gate (void) fmd_conf_getprop(fmd.d_conf, "ids.max", &ids_max);
500Sstevel@tonic-gate
510Sstevel@tonic-gate hashmax = max - min + 1;
52*13093SRoger.Faulkner@Oracle.COM hashlen = 1 << fls(hashmax / ids_avg);
530Sstevel@tonic-gate if (hashlen > ids_max)
540Sstevel@tonic-gate hashlen = ids_max;
550Sstevel@tonic-gate
560Sstevel@tonic-gate (void) strlcpy(ids->ids_name, name, sizeof (ids->ids_name));
570Sstevel@tonic-gate (void) pthread_mutex_init(&ids->ids_lock, NULL);
581193Smws (void) pthread_cond_init(&ids->ids_cv, NULL);
590Sstevel@tonic-gate
600Sstevel@tonic-gate ids->ids_hash = fmd_zalloc(sizeof (void *) * hashlen, FMD_SLEEP);
610Sstevel@tonic-gate ids->ids_hashlen = hashlen;
621193Smws ids->ids_refs = 0;
630Sstevel@tonic-gate ids->ids_nextid = min - 1;
640Sstevel@tonic-gate ids->ids_minid = min;
650Sstevel@tonic-gate ids->ids_maxid = max;
660Sstevel@tonic-gate ids->ids_count = 0;
670Sstevel@tonic-gate
680Sstevel@tonic-gate return (ids);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
710Sstevel@tonic-gate void
fmd_idspace_destroy(fmd_idspace_t * ids)720Sstevel@tonic-gate fmd_idspace_destroy(fmd_idspace_t *ids)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate fmd_idelem_t *ide, *nde;
750Sstevel@tonic-gate uint_t i;
760Sstevel@tonic-gate
770Sstevel@tonic-gate (void) pthread_mutex_lock(&ids->ids_lock);
780Sstevel@tonic-gate
791193Smws while (ids->ids_refs != 0)
801193Smws (void) pthread_cond_wait(&ids->ids_cv, &ids->ids_lock);
811193Smws
820Sstevel@tonic-gate for (i = 0; i < ids->ids_hashlen; i++) {
830Sstevel@tonic-gate for (ide = ids->ids_hash[i]; ide != NULL; ide = nde) {
840Sstevel@tonic-gate nde = ide->ide_next;
850Sstevel@tonic-gate fmd_free(ide, sizeof (fmd_idelem_t));
860Sstevel@tonic-gate }
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
890Sstevel@tonic-gate fmd_free(ids->ids_hash, sizeof (void *) * ids->ids_hashlen);
900Sstevel@tonic-gate fmd_free(ids, sizeof (fmd_idspace_t));
910Sstevel@tonic-gate }
920Sstevel@tonic-gate
930Sstevel@tonic-gate void
fmd_idspace_apply(fmd_idspace_t * ids,void (* func)(fmd_idspace_t *,id_t,void *),void * arg)941193Smws fmd_idspace_apply(fmd_idspace_t *ids,
951193Smws void (*func)(fmd_idspace_t *, id_t, void *), void *arg)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate fmd_idelem_t *ide;
980Sstevel@tonic-gate id_t *ida, *idp;
990Sstevel@tonic-gate uint_t i, count;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate (void) pthread_mutex_lock(&ids->ids_lock);
1020Sstevel@tonic-gate count = ids->ids_count;
1030Sstevel@tonic-gate ida = idp = fmd_alloc(sizeof (id_t) * count, FMD_SLEEP);
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate for (i = 0; i < ids->ids_hashlen; i++) {
1060Sstevel@tonic-gate for (ide = ids->ids_hash[i]; ide != NULL; ide = ide->ide_next)
1070Sstevel@tonic-gate *idp++ = ide->ide_id;
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate ASSERT(idp == ida + count);
1110Sstevel@tonic-gate (void) pthread_mutex_unlock(&ids->ids_lock);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate for (i = 0; i < count; i++)
1141193Smws func(ids, ida[i], arg);
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate fmd_free(ida, sizeof (id_t) * count);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate static fmd_idelem_t *
fmd_idspace_lookup(fmd_idspace_t * ids,id_t id)1200Sstevel@tonic-gate fmd_idspace_lookup(fmd_idspace_t *ids, id_t id)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate fmd_idelem_t *ide;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate ASSERT(MUTEX_HELD(&ids->ids_lock));
1250Sstevel@tonic-gate ide = ids->ids_hash[id & (ids->ids_hashlen - 1)];
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate for (; ide != NULL; ide = ide->ide_next) {
1280Sstevel@tonic-gate if (ide->ide_id == id)
1290Sstevel@tonic-gate break;
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate return (ide);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate void *
fmd_idspace_getspecific(fmd_idspace_t * ids,id_t id)1360Sstevel@tonic-gate fmd_idspace_getspecific(fmd_idspace_t *ids, id_t id)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate fmd_idelem_t *ide;
1390Sstevel@tonic-gate void *data;
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate (void) pthread_mutex_lock(&ids->ids_lock);
1420Sstevel@tonic-gate ide = fmd_idspace_lookup(ids, id);
1430Sstevel@tonic-gate data = ide ? ide->ide_data : NULL;
1440Sstevel@tonic-gate (void) pthread_mutex_unlock(&ids->ids_lock);
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate return (data);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate void
fmd_idspace_setspecific(fmd_idspace_t * ids,id_t id,void * data)1500Sstevel@tonic-gate fmd_idspace_setspecific(fmd_idspace_t *ids, id_t id, void *data)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate fmd_idelem_t *ide;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate (void) pthread_mutex_lock(&ids->ids_lock);
1550Sstevel@tonic-gate
1561193Smws while (ids->ids_refs != 0)
1571193Smws (void) pthread_cond_wait(&ids->ids_cv, &ids->ids_lock);
1581193Smws
1590Sstevel@tonic-gate if ((ide = fmd_idspace_lookup(ids, id)) == NULL) {
1600Sstevel@tonic-gate fmd_panic("idspace %p (%s) does not contain id %ld",
1610Sstevel@tonic-gate (void *)ids, ids->ids_name, id);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate ide->ide_data = data;
1650Sstevel@tonic-gate (void) pthread_mutex_unlock(&ids->ids_lock);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate int
fmd_idspace_contains(fmd_idspace_t * ids,id_t id)1690Sstevel@tonic-gate fmd_idspace_contains(fmd_idspace_t *ids, id_t id)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate fmd_idelem_t *ide;
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate (void) pthread_mutex_lock(&ids->ids_lock);
1740Sstevel@tonic-gate ide = fmd_idspace_lookup(ids, id);
1750Sstevel@tonic-gate (void) pthread_mutex_unlock(&ids->ids_lock);
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate return (ide != NULL);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate int
fmd_idspace_valid(fmd_idspace_t * ids,id_t id)1810Sstevel@tonic-gate fmd_idspace_valid(fmd_idspace_t *ids, id_t id)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate return (id >= ids->ids_minid && id <= ids->ids_maxid);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate static id_t
fmd_idspace_xalloc_locked(fmd_idspace_t * ids,id_t id,void * data)1870Sstevel@tonic-gate fmd_idspace_xalloc_locked(fmd_idspace_t *ids, id_t id, void *data)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate fmd_idelem_t *ide;
1900Sstevel@tonic-gate uint_t h;
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate if (id < ids->ids_minid || id > ids->ids_maxid) {
1930Sstevel@tonic-gate fmd_panic("%ld out of range [%ld .. %ld] for idspace %p (%s)\n",
1940Sstevel@tonic-gate id, ids->ids_minid, ids->ids_maxid,
1950Sstevel@tonic-gate (void *)ids, ids->ids_name);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate if (fmd_idspace_lookup(ids, id) != NULL)
1990Sstevel@tonic-gate return (fmd_set_errno(EALREADY));
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate ide = fmd_alloc(sizeof (fmd_idelem_t), FMD_SLEEP);
2020Sstevel@tonic-gate h = id & (ids->ids_hashlen - 1);
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate ide->ide_next = ids->ids_hash[h];
2050Sstevel@tonic-gate ide->ide_data = data;
2060Sstevel@tonic-gate ide->ide_id = id;
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate ids->ids_hash[h] = ide;
2090Sstevel@tonic-gate ids->ids_count++;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate return (id);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate id_t
fmd_idspace_xalloc(fmd_idspace_t * ids,id_t id,void * data)2150Sstevel@tonic-gate fmd_idspace_xalloc(fmd_idspace_t *ids, id_t id, void *data)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate (void) pthread_mutex_lock(&ids->ids_lock);
2180Sstevel@tonic-gate id = fmd_idspace_xalloc_locked(ids, id, data);
2190Sstevel@tonic-gate (void) pthread_mutex_unlock(&ids->ids_lock);
2200Sstevel@tonic-gate return (id);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2231193Smws static id_t
fmd_idspace_alloc_locked(fmd_idspace_t * ids,void * data)2241193Smws fmd_idspace_alloc_locked(fmd_idspace_t *ids, void *data)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate id_t id;
2270Sstevel@tonic-gate
2281193Smws ASSERT(MUTEX_HELD(&ids->ids_lock));
2290Sstevel@tonic-gate
2301193Smws if (ids->ids_count == ids->ids_maxid - ids->ids_minid + 1)
2310Sstevel@tonic-gate return (fmd_set_errno(ENOSPC));
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate do {
2340Sstevel@tonic-gate if (++ids->ids_nextid > ids->ids_maxid)
2350Sstevel@tonic-gate ids->ids_nextid = ids->ids_minid;
2360Sstevel@tonic-gate id = ids->ids_nextid;
2370Sstevel@tonic-gate } while (fmd_idspace_xalloc_locked(ids, id, data) != id);
2380Sstevel@tonic-gate
2391193Smws return (id);
2401193Smws }
2411193Smws
2421193Smws id_t
fmd_idspace_alloc(fmd_idspace_t * ids,void * data)2431193Smws fmd_idspace_alloc(fmd_idspace_t *ids, void *data)
2441193Smws {
2451193Smws id_t id;
2461193Smws
2471193Smws (void) pthread_mutex_lock(&ids->ids_lock);
2481193Smws id = fmd_idspace_alloc_locked(ids, data);
2490Sstevel@tonic-gate (void) pthread_mutex_unlock(&ids->ids_lock);
2501193Smws
2511193Smws return (id);
2521193Smws }
2531193Smws
2541193Smws /*
2551193Smws * For the moment, we use a simple but slow implementation: reset ids_nextid to
2561193Smws * the minimum id and search in order from there. If this becomes performance
2571193Smws * sensitive we can maintain a freelist of the unallocated identifiers, etc.
2581193Smws */
2591193Smws id_t
fmd_idspace_alloc_min(fmd_idspace_t * ids,void * data)2601193Smws fmd_idspace_alloc_min(fmd_idspace_t *ids, void *data)
2611193Smws {
2621193Smws id_t id;
2631193Smws
2641193Smws (void) pthread_mutex_lock(&ids->ids_lock);
2651193Smws ids->ids_nextid = ids->ids_minid - 1;
2661193Smws id = fmd_idspace_alloc_locked(ids, data);
2671193Smws (void) pthread_mutex_unlock(&ids->ids_lock);
2681193Smws
2690Sstevel@tonic-gate return (id);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate void *
fmd_idspace_free(fmd_idspace_t * ids,id_t id)2730Sstevel@tonic-gate fmd_idspace_free(fmd_idspace_t *ids, id_t id)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate fmd_idelem_t *ide, **pp;
2760Sstevel@tonic-gate void *data;
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate (void) pthread_mutex_lock(&ids->ids_lock);
2790Sstevel@tonic-gate pp = &ids->ids_hash[id & (ids->ids_hashlen - 1)];
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate for (ide = *pp; ide != NULL; ide = ide->ide_next) {
2820Sstevel@tonic-gate if (ide->ide_id != id)
2830Sstevel@tonic-gate pp = &ide->ide_next;
2840Sstevel@tonic-gate else
2850Sstevel@tonic-gate break;
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate if (ide == NULL) {
2890Sstevel@tonic-gate (void) pthread_mutex_unlock(&ids->ids_lock);
2900Sstevel@tonic-gate return (NULL);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate data = ide->ide_data;
2940Sstevel@tonic-gate *pp = ide->ide_next;
2950Sstevel@tonic-gate fmd_free(ide, sizeof (fmd_idelem_t));
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate ASSERT(ids->ids_count != 0);
2980Sstevel@tonic-gate ids->ids_count--;
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate (void) pthread_mutex_unlock(&ids->ids_lock);
3010Sstevel@tonic-gate return (data);
3020Sstevel@tonic-gate }
3031193Smws
3041193Smws /*
3051193Smws * Retrieve the id-specific data for the specified id and place a hold on the
3061193Smws * id so that it cannot be or deleted until fmd_idspace_rele(ids, id) is
3071193Smws * called. For simplicity, we now use a single global reference count for all
3081193Smws * holds. If this feature needs to be used in a place where there is high
3091193Smws * contention between holders and deleters, the implementation can be modified
3101193Smws * to use either a per-hash-bucket or a per-id-element condition variable.
3111193Smws */
3121193Smws void *
fmd_idspace_hold(fmd_idspace_t * ids,id_t id)3131193Smws fmd_idspace_hold(fmd_idspace_t *ids, id_t id)
3141193Smws {
3151193Smws fmd_idelem_t *ide;
3161193Smws void *data = NULL;
3171193Smws
3181193Smws (void) pthread_mutex_lock(&ids->ids_lock);
3191193Smws
3201193Smws if ((ide = fmd_idspace_lookup(ids, id)) != NULL) {
3211193Smws ids->ids_refs++;
3221193Smws ASSERT(ids->ids_refs != 0);
3231193Smws data = ide->ide_data;
3241193Smws ASSERT(data != NULL);
3251193Smws }
3261193Smws
3271193Smws (void) pthread_mutex_unlock(&ids->ids_lock);
3281193Smws return (data);
3291193Smws }
3301193Smws
3311193Smws void
fmd_idspace_rele(fmd_idspace_t * ids,id_t id)3321193Smws fmd_idspace_rele(fmd_idspace_t *ids, id_t id)
3331193Smws {
3341193Smws (void) pthread_mutex_lock(&ids->ids_lock);
3351193Smws
3361193Smws ASSERT(fmd_idspace_lookup(ids, id) != NULL);
3371193Smws ASSERT(ids->ids_refs != 0);
3381193Smws ids->ids_refs--;
3391193Smws
3401193Smws (void) pthread_cond_broadcast(&ids->ids_cv);
3411193Smws (void) pthread_mutex_unlock(&ids->ids_lock);
3421193Smws }
343