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