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