Home | History | Annotate | Line # | Download | only in kern
subr_specificdata.c revision 1.3
      1  1.3  thorpej /*	$NetBSD: subr_specificdata.c,v 1.3 2006/10/11 05:37:32 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe.
      9  1.1  thorpej  *
     10  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1  thorpej  * modification, are permitted provided that the following conditions
     12  1.1  thorpej  * are met:
     13  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.1  thorpej  *    must display the following acknowledgement:
     20  1.1  thorpej  *	This product includes software developed by the NetBSD
     21  1.1  thorpej  *	Foundation, Inc. and its contributors.
     22  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.1  thorpej  *    from this software without specific prior written permission.
     25  1.1  thorpej  *
     26  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  thorpej  */
     38  1.1  thorpej 
     39  1.1  thorpej /*-
     40  1.1  thorpej  * Copyright (c) 2006 YAMAMOTO Takashi.
     41  1.1  thorpej  * All rights reserved.
     42  1.1  thorpej  *
     43  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     44  1.1  thorpej  * modification, are permitted provided that the following conditions
     45  1.1  thorpej  * are met:
     46  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     47  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     48  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     50  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     51  1.1  thorpej  *
     52  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  1.1  thorpej  * SUCH DAMAGE.
     63  1.1  thorpej  */
     64  1.1  thorpej 
     65  1.1  thorpej #include <sys/cdefs.h>
     66  1.3  thorpej __KERNEL_RCSID(0, "$NetBSD: subr_specificdata.c,v 1.3 2006/10/11 05:37:32 thorpej Exp $");
     67  1.1  thorpej 
     68  1.1  thorpej #include <sys/param.h>
     69  1.1  thorpej #include <sys/kmem.h>
     70  1.1  thorpej #include <sys/proc.h>
     71  1.1  thorpej #include <sys/specificdata.h>
     72  1.1  thorpej #include <sys/queue.h>
     73  1.1  thorpej 
     74  1.1  thorpej /*
     75  1.1  thorpej  * Locking notes:
     76  1.1  thorpej  *
     77  1.1  thorpej  * The specdataref_container pointer in the specificdata_reference
     78  1.1  thorpej  * is volatile.  To read it, you must hold EITHER the domain lock
     79  1.1  thorpej  * or the ref lock.  To write it, you must hold BOTH the domain lock
     80  1.1  thorpej  * and the ref lock.  The locks must be acquired in the following
     81  1.1  thorpej  * order:
     82  1.1  thorpej  *	domain -> ref
     83  1.1  thorpej  */
     84  1.1  thorpej 
     85  1.1  thorpej typedef struct {
     86  1.1  thorpej 	specificdata_dtor_t	ski_dtor;
     87  1.1  thorpej } specificdata_key_impl;
     88  1.1  thorpej 
     89  1.1  thorpej struct specificdata_container {
     90  1.1  thorpej 	size_t		sc_nkey;
     91  1.1  thorpej 	LIST_ENTRY(specificdata_container) sc_list;
     92  1.1  thorpej 	void *		sc_data[];	/* variable length */
     93  1.1  thorpej };
     94  1.1  thorpej 
     95  1.1  thorpej #define	SPECIFICDATA_CONTAINER_BYTESIZE(n)		\
     96  1.1  thorpej 	(sizeof(struct specificdata_container) + ((n) * sizeof(void *)))
     97  1.1  thorpej 
     98  1.1  thorpej struct specificdata_domain {
     99  1.1  thorpej 	struct lock	sd_lock;
    100  1.1  thorpej 	unsigned int	sd_nkey;
    101  1.1  thorpej 	LIST_HEAD(, specificdata_container) sd_list;
    102  1.1  thorpej 	specificdata_key_impl *sd_keys;
    103  1.1  thorpej };
    104  1.1  thorpej 
    105  1.1  thorpej #define	specdataref_lock_init(ref)			\
    106  1.1  thorpej 				simple_lock_init(&(ref)->specdataref_slock)
    107  1.1  thorpej #define	specdataref_lock(ref)	simple_lock(&(ref)->specdataref_slock)
    108  1.1  thorpej #define	specdataref_unlock(ref)	simple_unlock(&(ref)->specdataref_slock)
    109  1.1  thorpej 
    110  1.1  thorpej static void
    111  1.1  thorpej specificdata_domain_lock(specificdata_domain_t sd)
    112  1.1  thorpej {
    113  1.1  thorpej 
    114  1.1  thorpej 	ASSERT_SLEEPABLE(NULL, __func__);
    115  1.1  thorpej 	lockmgr(&sd->sd_lock, LK_EXCLUSIVE, 0);
    116  1.1  thorpej }
    117  1.1  thorpej 
    118  1.3  thorpej static int
    119  1.3  thorpej specificdata_domain_trylock(specificdata_domain_t sd)
    120  1.3  thorpej {
    121  1.3  thorpej 
    122  1.3  thorpej 	return (lockmgr(&sd->sd_lock, LK_EXCLUSIVE|LK_NOWAIT, 0));
    123  1.3  thorpej }
    124  1.3  thorpej 
    125  1.1  thorpej static void
    126  1.1  thorpej specificdata_domain_unlock(specificdata_domain_t sd)
    127  1.1  thorpej {
    128  1.1  thorpej 
    129  1.1  thorpej 	lockmgr(&sd->sd_lock, LK_RELEASE, 0);
    130  1.1  thorpej }
    131  1.1  thorpej 
    132  1.1  thorpej static void
    133  1.1  thorpej specificdata_container_link(specificdata_domain_t sd,
    134  1.1  thorpej 			    specificdata_container_t sc)
    135  1.1  thorpej {
    136  1.1  thorpej 
    137  1.1  thorpej 	LIST_INSERT_HEAD(&sd->sd_list, sc, sc_list);
    138  1.1  thorpej }
    139  1.1  thorpej 
    140  1.1  thorpej static void
    141  1.1  thorpej specificdata_container_unlink(specificdata_domain_t sd,
    142  1.1  thorpej 			      specificdata_container_t sc)
    143  1.1  thorpej {
    144  1.1  thorpej 
    145  1.1  thorpej 	LIST_REMOVE(sc, sc_list);
    146  1.1  thorpej }
    147  1.1  thorpej 
    148  1.1  thorpej static void
    149  1.1  thorpej specificdata_destroy_datum(specificdata_domain_t sd,
    150  1.1  thorpej 			   specificdata_container_t sc, specificdata_key_t key)
    151  1.1  thorpej {
    152  1.1  thorpej 	specificdata_dtor_t dtor;
    153  1.1  thorpej 	void *data;
    154  1.1  thorpej 
    155  1.1  thorpej 	if (key >= sc->sc_nkey)
    156  1.1  thorpej 		return;
    157  1.1  thorpej 
    158  1.1  thorpej 	KASSERT(key < sd->sd_nkey);
    159  1.1  thorpej 
    160  1.1  thorpej 	data = sc->sc_data[key];
    161  1.1  thorpej 	dtor = sd->sd_keys[key].ski_dtor;
    162  1.1  thorpej 
    163  1.1  thorpej 	if (dtor != NULL) {
    164  1.1  thorpej 		if (data != NULL) {
    165  1.1  thorpej 			sc->sc_data[key] = NULL;
    166  1.1  thorpej 			(*dtor)(data);
    167  1.1  thorpej 		}
    168  1.1  thorpej 	} else {
    169  1.1  thorpej 		KASSERT(data == NULL);
    170  1.1  thorpej 	}
    171  1.1  thorpej }
    172  1.1  thorpej 
    173  1.1  thorpej static void
    174  1.1  thorpej specificdata_noop_dtor(void *data)
    175  1.1  thorpej {
    176  1.1  thorpej 
    177  1.1  thorpej 	/* nothing */
    178  1.1  thorpej }
    179  1.1  thorpej 
    180  1.1  thorpej /*
    181  1.1  thorpej  * specificdata_domain_create --
    182  1.1  thorpej  *	Create a specifidata domain.
    183  1.1  thorpej  */
    184  1.1  thorpej specificdata_domain_t
    185  1.1  thorpej specificdata_domain_create(void)
    186  1.1  thorpej {
    187  1.1  thorpej 	specificdata_domain_t sd;
    188  1.1  thorpej 
    189  1.1  thorpej 	sd = kmem_zalloc(sizeof(*sd), KM_SLEEP);
    190  1.1  thorpej 	KASSERT(sd != NULL);
    191  1.1  thorpej 	lockinit(&sd->sd_lock, PLOCK, "specdata", 0, 0);
    192  1.1  thorpej 	LIST_INIT(&sd->sd_list);
    193  1.1  thorpej 
    194  1.1  thorpej 	return (sd);
    195  1.1  thorpej }
    196  1.1  thorpej 
    197  1.1  thorpej /*
    198  1.1  thorpej  * specificdata_domain_delete --
    199  1.1  thorpej  *	Destroy a specifidata domain.
    200  1.1  thorpej  */
    201  1.1  thorpej void
    202  1.1  thorpej specificdata_domain_delete(specificdata_domain_t sd)
    203  1.1  thorpej {
    204  1.1  thorpej 
    205  1.1  thorpej 	panic("specificdata_domain_delete: not implemented");
    206  1.1  thorpej }
    207  1.1  thorpej 
    208  1.1  thorpej /*
    209  1.1  thorpej  * specificdata_key_create --
    210  1.1  thorpej  *	Create a specificdata key for a domain.
    211  1.1  thorpej  *
    212  1.1  thorpej  *	Note: This is a rare operation.
    213  1.1  thorpej  */
    214  1.1  thorpej int
    215  1.1  thorpej specificdata_key_create(specificdata_domain_t sd, specificdata_key_t *keyp,
    216  1.1  thorpej 			specificdata_dtor_t dtor)
    217  1.1  thorpej {
    218  1.1  thorpej 	specificdata_key_impl *newkeys;
    219  1.1  thorpej 	specificdata_key_t key = 0;
    220  1.1  thorpej 	size_t nsz;
    221  1.1  thorpej 
    222  1.1  thorpej 	ASSERT_SLEEPABLE(NULL, __func__);
    223  1.1  thorpej 
    224  1.1  thorpej 	if (dtor == NULL)
    225  1.1  thorpej 		dtor = specificdata_noop_dtor;
    226  1.1  thorpej 
    227  1.1  thorpej 	specificdata_domain_lock(sd);
    228  1.1  thorpej 
    229  1.1  thorpej 	if (sd->sd_keys == NULL)
    230  1.1  thorpej 		goto needalloc;
    231  1.1  thorpej 
    232  1.1  thorpej 	for (; key < sd->sd_nkey; key++) {
    233  1.1  thorpej 		if (sd->sd_keys[key].ski_dtor == NULL)
    234  1.1  thorpej 			goto gotit;
    235  1.1  thorpej 	}
    236  1.1  thorpej 
    237  1.1  thorpej  needalloc:
    238  1.1  thorpej 	nsz = (sd->sd_nkey + 1) * sizeof(*newkeys);
    239  1.1  thorpej 	newkeys = kmem_zalloc(nsz, KM_SLEEP);
    240  1.1  thorpej 	KASSERT(newkeys != NULL);
    241  1.1  thorpej 	if (sd->sd_keys != NULL) {
    242  1.1  thorpej 		size_t osz = sd->sd_nkey * sizeof(*newkeys);
    243  1.1  thorpej 		memcpy(newkeys, sd->sd_keys, osz);
    244  1.1  thorpej 		kmem_free(sd->sd_keys, osz);
    245  1.1  thorpej 	}
    246  1.1  thorpej 	sd->sd_keys = newkeys;
    247  1.1  thorpej 	sd->sd_nkey++;
    248  1.1  thorpej  gotit:
    249  1.1  thorpej 	sd->sd_keys[key].ski_dtor = dtor;
    250  1.1  thorpej 
    251  1.1  thorpej 	specificdata_domain_unlock(sd);
    252  1.1  thorpej 
    253  1.1  thorpej 	*keyp = key;
    254  1.1  thorpej 	return (0);
    255  1.1  thorpej }
    256  1.1  thorpej 
    257  1.1  thorpej /*
    258  1.1  thorpej  * specificdata_key_delete --
    259  1.1  thorpej  *	Destroy a specificdata key for a domain.
    260  1.1  thorpej  *
    261  1.1  thorpej  *	Note: This is a rare operation.
    262  1.1  thorpej  */
    263  1.1  thorpej void
    264  1.1  thorpej specificdata_key_delete(specificdata_domain_t sd, specificdata_key_t key)
    265  1.1  thorpej {
    266  1.1  thorpej 	specificdata_container_t sc;
    267  1.1  thorpej 
    268  1.1  thorpej 	specificdata_domain_lock(sd);
    269  1.1  thorpej 
    270  1.1  thorpej 	if (key >= sd->sd_nkey)
    271  1.1  thorpej 		goto out;
    272  1.1  thorpej 
    273  1.1  thorpej 	/*
    274  1.1  thorpej 	 * Traverse all of the specificdata containers in the domain
    275  1.1  thorpej 	 * and the destroy the datum for the dying key.
    276  1.1  thorpej 	 */
    277  1.1  thorpej 	LIST_FOREACH(sc, &sd->sd_list, sc_list) {
    278  1.1  thorpej 		specificdata_destroy_datum(sd, sc, key);
    279  1.1  thorpej 	}
    280  1.1  thorpej 
    281  1.1  thorpej 	sd->sd_keys[key].ski_dtor = NULL;
    282  1.1  thorpej 
    283  1.1  thorpej  out:
    284  1.1  thorpej 	specificdata_domain_unlock(sd);
    285  1.1  thorpej }
    286  1.1  thorpej 
    287  1.1  thorpej /*
    288  1.1  thorpej  * specificdata_init --
    289  1.1  thorpej  *	Initialize a specificdata container for operation in the
    290  1.1  thorpej  *	specified domain.
    291  1.1  thorpej  */
    292  1.1  thorpej int
    293  1.1  thorpej specificdata_init(specificdata_domain_t sd, specificdata_reference *ref)
    294  1.1  thorpej {
    295  1.1  thorpej 
    296  1.1  thorpej 	/*
    297  1.1  thorpej 	 * Just NULL-out the container pointer; we'll allocate the
    298  1.1  thorpej 	 * container the first time specificdata is put into it.
    299  1.1  thorpej 	 */
    300  1.1  thorpej 	ref->specdataref_container = NULL;
    301  1.1  thorpej 	specdataref_lock_init(ref);
    302  1.1  thorpej 
    303  1.1  thorpej 	return (0);
    304  1.1  thorpej }
    305  1.1  thorpej 
    306  1.1  thorpej /*
    307  1.1  thorpej  * specificdata_fini --
    308  1.1  thorpej  *	Destroy a specificdata container.  We destroy all of the datums
    309  1.1  thorpej  *	stuffed into the container just as if the key were destroyed.
    310  1.1  thorpej  */
    311  1.1  thorpej void
    312  1.1  thorpej specificdata_fini(specificdata_domain_t sd, specificdata_reference *ref)
    313  1.1  thorpej {
    314  1.1  thorpej 	specificdata_container_t sc;
    315  1.1  thorpej 	specificdata_key_t key;
    316  1.1  thorpej 
    317  1.1  thorpej 	ASSERT_SLEEPABLE(NULL, __func__);
    318  1.1  thorpej 
    319  1.1  thorpej 	sc = ref->specdataref_container;
    320  1.1  thorpej 	if (sc == NULL)
    321  1.1  thorpej 		return;
    322  1.1  thorpej 	ref->specdataref_container = NULL;
    323  1.1  thorpej 
    324  1.1  thorpej 	specificdata_domain_lock(sd);
    325  1.1  thorpej 
    326  1.1  thorpej 	specificdata_container_unlink(sd, sc);
    327  1.1  thorpej 	for (key = 0; key < sc->sc_nkey; key++) {
    328  1.1  thorpej 		specificdata_destroy_datum(sd, sc, key);
    329  1.1  thorpej 	}
    330  1.1  thorpej 
    331  1.1  thorpej 	specificdata_domain_unlock(sd);
    332  1.1  thorpej 
    333  1.1  thorpej 	kmem_free(sc, SPECIFICDATA_CONTAINER_BYTESIZE(sc->sc_nkey));
    334  1.1  thorpej }
    335  1.1  thorpej 
    336  1.1  thorpej /*
    337  1.1  thorpej  * specificdata_getspecific --
    338  1.1  thorpej  *	Get a datum from a container.
    339  1.1  thorpej  *
    340  1.1  thorpej  *	Note: This routine is guaranteed not to sleep.
    341  1.1  thorpej  */
    342  1.1  thorpej void *
    343  1.1  thorpej specificdata_getspecific(specificdata_domain_t sd, specificdata_reference *ref,
    344  1.1  thorpej 			 specificdata_key_t key)
    345  1.1  thorpej {
    346  1.1  thorpej 	specificdata_container_t sc;
    347  1.1  thorpej 	void *data = NULL;
    348  1.1  thorpej 
    349  1.1  thorpej 	specdataref_lock(ref);
    350  1.1  thorpej 
    351  1.1  thorpej 	sc = ref->specdataref_container;
    352  1.1  thorpej 	if (sc != NULL && key < sc->sc_nkey)
    353  1.1  thorpej 		data = sc->sc_data[key];
    354  1.1  thorpej 
    355  1.1  thorpej 	specdataref_unlock(ref);
    356  1.1  thorpej 
    357  1.1  thorpej 	return (data);
    358  1.1  thorpej }
    359  1.1  thorpej 
    360  1.1  thorpej /*
    361  1.1  thorpej  * specificdata_getspecific_unlocked --
    362  1.1  thorpej  *	Get a datum from a container in a lockless fashion.
    363  1.1  thorpej  *
    364  1.1  thorpej  *	Note: When using this routine, care must be taken to ensure
    365  1.1  thorpej  *	that no other thread could cause the specificdata_reference
    366  1.1  thorpej  *	to become invalid (i.e. point at the wrong container) by
    367  1.1  thorpej  *	issuing a setspecific call or destroying the container.
    368  1.1  thorpej  *
    369  1.1  thorpej  *	Note #2: This routine is guaranteed not to sleep.
    370  1.1  thorpej  */
    371  1.1  thorpej void *
    372  1.1  thorpej specificdata_getspecific_unlocked(specificdata_domain_t sd,
    373  1.1  thorpej 				  specificdata_reference *ref,
    374  1.1  thorpej 				  specificdata_key_t key)
    375  1.1  thorpej {
    376  1.1  thorpej 	specificdata_container_t sc;
    377  1.1  thorpej 
    378  1.1  thorpej 	sc = ref->specdataref_container;
    379  1.1  thorpej 	if (sc != NULL && key < sc->sc_nkey)
    380  1.1  thorpej 		return (sc->sc_data[key]);
    381  1.1  thorpej 
    382  1.1  thorpej 	return (NULL);
    383  1.1  thorpej }
    384  1.1  thorpej 
    385  1.3  thorpej static int
    386  1.3  thorpej specificdata_setspecific_internal(specificdata_domain_t sd,
    387  1.3  thorpej 				  specificdata_reference *ref,
    388  1.3  thorpej 				  specificdata_key_t key, void *data,
    389  1.3  thorpej 				  boolean_t waitok)
    390  1.1  thorpej {
    391  1.1  thorpej 	specificdata_container_t sc, newsc;
    392  1.1  thorpej 	size_t newnkey, sz;
    393  1.3  thorpej 	int error;
    394  1.1  thorpej 
    395  1.1  thorpej 	ASSERT_SLEEPABLE(NULL, __func__);
    396  1.1  thorpej 
    397  1.1  thorpej 	specdataref_lock(ref);
    398  1.1  thorpej 
    399  1.1  thorpej 	sc = ref->specdataref_container;
    400  1.1  thorpej 	if (__predict_true(sc != NULL && key < sc->sc_nkey)) {
    401  1.1  thorpej 		sc->sc_data[key] = data;
    402  1.1  thorpej 		specdataref_unlock(ref);
    403  1.3  thorpej 		return (0);
    404  1.1  thorpej 	}
    405  1.1  thorpej 
    406  1.1  thorpej 	specdataref_unlock(ref);
    407  1.1  thorpej 
    408  1.1  thorpej 	/*
    409  1.1  thorpej 	 * Slow path: need to resize.
    410  1.1  thorpej 	 */
    411  1.1  thorpej 
    412  1.3  thorpej 	if (waitok)
    413  1.3  thorpej 		specificdata_domain_lock(sd);
    414  1.3  thorpej 	else {
    415  1.3  thorpej 		error = specificdata_domain_trylock(sd);
    416  1.3  thorpej 		if (error)
    417  1.3  thorpej 			return (error);
    418  1.3  thorpej 	}
    419  1.1  thorpej 	newnkey = sd->sd_nkey;
    420  1.1  thorpej 	if (key >= newnkey) {
    421  1.1  thorpej 		specificdata_domain_unlock(sd);
    422  1.1  thorpej 		panic("specificdata_setspecific");
    423  1.1  thorpej 	}
    424  1.1  thorpej 	sz = SPECIFICDATA_CONTAINER_BYTESIZE(newnkey);
    425  1.3  thorpej 	newsc = kmem_zalloc(sz, waitok ? KM_SLEEP : KM_NOSLEEP);
    426  1.3  thorpej 	if (waitok) {
    427  1.3  thorpej 		KASSERT(newsc != NULL);
    428  1.3  thorpej 	} else if (newsc == NULL) {
    429  1.3  thorpej 		specificdata_domain_unlock(sd);
    430  1.3  thorpej 		return (EWOULDBLOCK);
    431  1.3  thorpej 	}
    432  1.1  thorpej 	newsc->sc_nkey = newnkey;
    433  1.1  thorpej 
    434  1.1  thorpej 	specdataref_lock(ref);
    435  1.1  thorpej 
    436  1.1  thorpej 	sc = ref->specdataref_container;
    437  1.1  thorpej 	if (sc != NULL) {
    438  1.1  thorpej 		if (key < sc->sc_nkey) {
    439  1.1  thorpej 			/*
    440  1.1  thorpej 			 * Someone beat us to the punch.  Unwind and put
    441  1.1  thorpej 			 * the object into the now large enough container.
    442  1.1  thorpej 			 */
    443  1.1  thorpej 			sc->sc_data[key] = data;
    444  1.1  thorpej 			specdataref_unlock(ref);
    445  1.1  thorpej 			specificdata_domain_unlock(sd);
    446  1.1  thorpej 			kmem_free(newsc, sz);
    447  1.3  thorpej 			return (0);
    448  1.1  thorpej 		}
    449  1.1  thorpej 		specificdata_container_unlink(sd, sc);
    450  1.1  thorpej 		memcpy(newsc->sc_data, sc->sc_data,
    451  1.1  thorpej 		       sc->sc_nkey * sizeof(void *));
    452  1.1  thorpej 	}
    453  1.1  thorpej 	newsc->sc_data[key] = data;
    454  1.1  thorpej 	specificdata_container_link(sd, newsc);
    455  1.1  thorpej 	ref->specdataref_container = newsc;
    456  1.1  thorpej 
    457  1.1  thorpej 	specdataref_unlock(ref);
    458  1.1  thorpej 	specificdata_domain_unlock(sd);
    459  1.1  thorpej 
    460  1.1  thorpej 	if (sc != NULL)
    461  1.1  thorpej 		kmem_free(sc, SPECIFICDATA_CONTAINER_BYTESIZE(sc->sc_nkey));
    462  1.3  thorpej 
    463  1.3  thorpej 	return (0);
    464  1.3  thorpej }
    465  1.3  thorpej 
    466  1.3  thorpej /*
    467  1.3  thorpej  * specificdata_setspecific --
    468  1.3  thorpej  *	Put a datum into a container.
    469  1.3  thorpej  */
    470  1.3  thorpej void
    471  1.3  thorpej specificdata_setspecific(specificdata_domain_t sd, specificdata_reference *ref,
    472  1.3  thorpej 			 specificdata_key_t key, void *data)
    473  1.3  thorpej {
    474  1.3  thorpej 	int rv;
    475  1.3  thorpej 
    476  1.3  thorpej 	rv = specificdata_setspecific_internal(sd, ref, key, data, TRUE);
    477  1.3  thorpej 	KASSERT(rv == 0);
    478  1.3  thorpej }
    479  1.3  thorpej 
    480  1.3  thorpej /*
    481  1.3  thorpej  * specificdata_setspecific_nowait --
    482  1.3  thorpej  *	Put a datum into a container.  Caller has indicated that it does
    483  1.3  thorpej  *	not want to sleep.
    484  1.3  thorpej  */
    485  1.3  thorpej int
    486  1.3  thorpej specificdata_setspecific_nowait(specificdata_domain_t sd,
    487  1.3  thorpej 				specificdata_reference *ref,
    488  1.3  thorpej 				specificdata_key_t key, void *data)
    489  1.3  thorpej {
    490  1.3  thorpej 	int rv;
    491  1.3  thorpej 
    492  1.3  thorpej 	return (specificdata_setspecific_internal(sd, ref, key, data, FALSE));
    493  1.1  thorpej }
    494