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