chfs_malloc.c revision 1.1.4.1 1 1.1 ahoka /* $NetBSD: chfs_malloc.c,v 1.1.4.1 2012/03/02 16:31:44 riz Exp $ */
2 1.1 ahoka
3 1.1 ahoka /*-
4 1.1 ahoka * Copyright (c) 2010 Department of Software Engineering,
5 1.1 ahoka * University of Szeged, Hungary
6 1.1 ahoka * Copyright (C) 2010 Tamas Toth <ttoth (at) inf.u-szeged.hu>
7 1.1 ahoka * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
8 1.1 ahoka * All rights reserved.
9 1.1 ahoka *
10 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation
11 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary
12 1.1 ahoka *
13 1.1 ahoka * Redistribution and use in source and binary forms, with or without
14 1.1 ahoka * modification, are permitted provided that the following conditions
15 1.1 ahoka * are met:
16 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
17 1.1 ahoka * notice, this list of conditions and the following disclaimer.
18 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
20 1.1 ahoka * documentation and/or other materials provided with the distribution.
21 1.1 ahoka *
22 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 ahoka * SUCH DAMAGE.
33 1.1 ahoka */
34 1.1 ahoka
35 1.1 ahoka #include "chfs.h"
36 1.1 ahoka #include <sys/pool.h>
37 1.1 ahoka
38 1.1 ahoka pool_cache_t chfs_vnode_cache;
39 1.1 ahoka pool_cache_t chfs_nrefs_cache;
40 1.1 ahoka pool_cache_t chfs_flash_vnode_cache;
41 1.1 ahoka pool_cache_t chfs_flash_dirent_cache;
42 1.1 ahoka pool_cache_t chfs_flash_dnode_cache;
43 1.1 ahoka pool_cache_t chfs_node_frag_cache;
44 1.1 ahoka pool_cache_t chfs_tmp_dnode_cache;
45 1.1 ahoka pool_cache_t chfs_tmp_dnode_info_cache;
46 1.1 ahoka
47 1.1 ahoka int
48 1.1.4.1 riz chfs_alloc_pool_caches(void)
49 1.1 ahoka {
50 1.1 ahoka chfs_vnode_cache = pool_cache_init(
51 1.1 ahoka sizeof(struct chfs_vnode_cache),
52 1.1 ahoka 0, 0, 0, "chfs_vnode_cache", NULL, IPL_NONE, NULL, NULL,
53 1.1 ahoka NULL);
54 1.1 ahoka if (!chfs_vnode_cache)
55 1.1 ahoka goto err_vnode;
56 1.1 ahoka
57 1.1 ahoka chfs_nrefs_cache = pool_cache_init(
58 1.1 ahoka (REFS_BLOCK_LEN + 1) * sizeof(struct chfs_node_ref), 0, 0,
59 1.1 ahoka 0, "chfs_nrefs_pool", NULL, IPL_NONE, NULL, NULL, NULL);
60 1.1 ahoka if (!chfs_nrefs_cache)
61 1.1 ahoka goto err_nrefs;
62 1.1 ahoka
63 1.1 ahoka chfs_flash_vnode_cache = pool_cache_init(
64 1.1 ahoka sizeof(struct chfs_flash_vnode), 0, 0, 0,
65 1.1 ahoka "chfs_flash_vnode_pool", NULL, IPL_NONE, NULL, NULL, NULL);
66 1.1 ahoka if (!chfs_flash_vnode_cache)
67 1.1 ahoka goto err_flash_vnode;
68 1.1 ahoka
69 1.1 ahoka chfs_flash_dirent_cache = pool_cache_init(
70 1.1 ahoka sizeof(struct chfs_flash_dirent_node), 0, 0, 0,
71 1.1 ahoka "chfs_flash_dirent_pool", NULL, IPL_NONE, NULL, NULL, NULL);
72 1.1 ahoka if (!chfs_flash_dirent_cache)
73 1.1 ahoka goto err_flash_dirent;
74 1.1 ahoka
75 1.1 ahoka chfs_flash_dnode_cache = pool_cache_init(
76 1.1 ahoka sizeof(struct chfs_flash_data_node), 0, 0, 0,
77 1.1 ahoka "chfs_flash_dnode_pool", NULL, IPL_NONE, NULL, NULL, NULL);
78 1.1 ahoka if (!chfs_flash_dnode_cache)
79 1.1 ahoka goto err_flash_dnode;
80 1.1 ahoka
81 1.1 ahoka chfs_node_frag_cache = pool_cache_init(
82 1.1 ahoka sizeof(struct chfs_node_frag), 0, 0, 0,
83 1.1 ahoka "chfs_node_frag_pool", NULL, IPL_NONE, NULL, NULL, NULL);
84 1.1 ahoka if (!chfs_node_frag_cache)
85 1.1 ahoka goto err_node_frag;
86 1.1 ahoka
87 1.1 ahoka chfs_tmp_dnode_cache = pool_cache_init(
88 1.1 ahoka sizeof(struct chfs_tmp_dnode), 0, 0, 0,
89 1.1 ahoka "chfs_tmp_dnode_pool", NULL, IPL_NONE, NULL, NULL, NULL);
90 1.1 ahoka if (!chfs_tmp_dnode_cache)
91 1.1 ahoka goto err_tmp_dnode;
92 1.1 ahoka
93 1.1 ahoka chfs_tmp_dnode_info_cache = pool_cache_init(
94 1.1 ahoka sizeof(struct chfs_tmp_dnode_info), 0, 0, 0,
95 1.1 ahoka "chfs_tmp_dnode_info_pool", NULL, IPL_NONE, NULL, NULL, NULL);
96 1.1 ahoka if (!chfs_tmp_dnode_info_cache)
97 1.1 ahoka goto err_tmp_dnode_info;
98 1.1 ahoka
99 1.1 ahoka return 0;
100 1.1 ahoka
101 1.1 ahoka err_tmp_dnode_info:
102 1.1 ahoka pool_cache_destroy(chfs_tmp_dnode_cache);
103 1.1 ahoka err_tmp_dnode:
104 1.1 ahoka pool_cache_destroy(chfs_node_frag_cache);
105 1.1 ahoka err_node_frag:
106 1.1 ahoka pool_cache_destroy(chfs_flash_dnode_cache);
107 1.1 ahoka err_flash_dnode:
108 1.1 ahoka pool_cache_destroy(chfs_flash_dirent_cache);
109 1.1 ahoka err_flash_dirent:
110 1.1 ahoka pool_cache_destroy(chfs_flash_vnode_cache);
111 1.1 ahoka err_flash_vnode:
112 1.1 ahoka pool_cache_destroy(chfs_nrefs_cache);
113 1.1 ahoka err_nrefs:
114 1.1 ahoka pool_cache_destroy(chfs_vnode_cache);
115 1.1 ahoka err_vnode:
116 1.1 ahoka
117 1.1 ahoka return ENOMEM;
118 1.1 ahoka }
119 1.1 ahoka
120 1.1 ahoka void
121 1.1.4.1 riz chfs_destroy_pool_caches(void)
122 1.1 ahoka {
123 1.1 ahoka if (chfs_vnode_cache)
124 1.1 ahoka pool_cache_destroy(chfs_vnode_cache);
125 1.1 ahoka
126 1.1 ahoka if (chfs_nrefs_cache)
127 1.1 ahoka pool_cache_destroy(chfs_nrefs_cache);
128 1.1 ahoka
129 1.1 ahoka if (chfs_flash_vnode_cache)
130 1.1 ahoka pool_cache_destroy(chfs_flash_vnode_cache);
131 1.1 ahoka
132 1.1 ahoka if (chfs_flash_dirent_cache)
133 1.1 ahoka pool_cache_destroy(chfs_flash_dirent_cache);
134 1.1 ahoka
135 1.1 ahoka if (chfs_flash_dnode_cache)
136 1.1 ahoka pool_cache_destroy(chfs_flash_dnode_cache);
137 1.1 ahoka
138 1.1 ahoka if (chfs_node_frag_cache)
139 1.1 ahoka pool_cache_destroy(chfs_node_frag_cache);
140 1.1 ahoka
141 1.1 ahoka if (chfs_tmp_dnode_cache)
142 1.1 ahoka pool_cache_destroy(chfs_tmp_dnode_cache);
143 1.1 ahoka
144 1.1 ahoka if (chfs_tmp_dnode_info_cache)
145 1.1 ahoka pool_cache_destroy(chfs_tmp_dnode_info_cache);
146 1.1 ahoka }
147 1.1 ahoka
148 1.1 ahoka struct chfs_vnode_cache *
149 1.1 ahoka chfs_vnode_cache_alloc(ino_t vno)
150 1.1 ahoka {
151 1.1 ahoka struct chfs_vnode_cache* vc;
152 1.1 ahoka vc = pool_cache_get(chfs_vnode_cache, PR_WAITOK);
153 1.1 ahoka
154 1.1 ahoka memset(vc, 0, sizeof(*vc));
155 1.1 ahoka vc->vno = vno;
156 1.1 ahoka vc->v = (void *)vc;
157 1.1 ahoka vc->dirents = (void *)vc;
158 1.1 ahoka vc->dnode = (void *)vc;
159 1.1 ahoka TAILQ_INIT(&vc->scan_dirents);
160 1.1 ahoka vc->highest_version = 0;
161 1.1 ahoka
162 1.1 ahoka return vc;
163 1.1 ahoka }
164 1.1 ahoka
165 1.1 ahoka void
166 1.1 ahoka chfs_vnode_cache_free(struct chfs_vnode_cache *vc)
167 1.1 ahoka {
168 1.1 ahoka //kmem_free(vc->vno_version, sizeof(uint64_t));
169 1.1 ahoka pool_cache_put(chfs_vnode_cache, vc);
170 1.1 ahoka }
171 1.1 ahoka
172 1.1 ahoka /**
173 1.1 ahoka * chfs_alloc_refblock - allocating a refblock
174 1.1 ahoka *
175 1.1 ahoka * Returns a pointer of the first element in the block.
176 1.1 ahoka *
177 1.1 ahoka * We are not allocating just one node ref, instead we allocating REFS_BLOCK_LEN
178 1.1 ahoka * number of node refs, the last element will be a pointer to the next block.
179 1.1 ahoka * We do this, because we need a chain of nodes which have been ordered by the
180 1.1 ahoka * physical address of them.
181 1.1 ahoka *
182 1.1 ahoka */
183 1.1 ahoka struct chfs_node_ref*
184 1.1 ahoka chfs_alloc_refblock(void)
185 1.1 ahoka {
186 1.1 ahoka int i;
187 1.1 ahoka struct chfs_node_ref *nref;
188 1.1 ahoka nref = pool_cache_get(chfs_nrefs_cache, PR_WAITOK);
189 1.1 ahoka
190 1.1 ahoka for (i = 0; i < REFS_BLOCK_LEN; i++) {
191 1.1 ahoka nref[i].nref_lnr = REF_EMPTY_NODE;
192 1.1 ahoka nref[i].nref_next = NULL;
193 1.1 ahoka }
194 1.1 ahoka i = REFS_BLOCK_LEN;
195 1.1 ahoka nref[i].nref_lnr = REF_LINK_TO_NEXT;
196 1.1 ahoka nref[i].nref_next = NULL;
197 1.1 ahoka
198 1.1 ahoka return nref;
199 1.1 ahoka }
200 1.1 ahoka
201 1.1 ahoka /**
202 1.1 ahoka * chfs_free_refblock - freeing a refblock
203 1.1 ahoka */
204 1.1 ahoka void
205 1.1 ahoka chfs_free_refblock(struct chfs_node_ref *nref)
206 1.1 ahoka {
207 1.1 ahoka pool_cache_put(chfs_nrefs_cache, nref);
208 1.1 ahoka }
209 1.1 ahoka
210 1.1 ahoka /**
211 1.1 ahoka * chfs_alloc_node_ref - allocating a node ref from a refblock
212 1.1 ahoka * @cheb: eraseblock information structure
213 1.1 ahoka *
214 1.1 ahoka * Allocating a node ref from a refblock, it there isn't any free element in the
215 1.1 ahoka * block, a new block will be allocated and be linked to the current block.
216 1.1 ahoka */
217 1.1 ahoka struct chfs_node_ref*
218 1.1 ahoka chfs_alloc_node_ref(struct chfs_eraseblock *cheb)
219 1.1 ahoka {
220 1.1 ahoka struct chfs_node_ref *nref, *new, *old;
221 1.1 ahoka old = cheb->last_node;
222 1.1 ahoka nref = cheb->last_node;
223 1.1 ahoka
224 1.1 ahoka if (!nref) {
225 1.1 ahoka //There haven't been any nref allocated for this block yet
226 1.1 ahoka nref = chfs_alloc_refblock();
227 1.1 ahoka
228 1.1 ahoka cheb->first_node = nref;
229 1.1 ahoka cheb->last_node = nref;
230 1.1 ahoka nref->nref_lnr = cheb->lnr;
231 1.1 ahoka KASSERT(cheb->lnr == nref->nref_lnr);
232 1.1 ahoka
233 1.1 ahoka return nref;
234 1.1 ahoka }
235 1.1 ahoka
236 1.1 ahoka nref++;
237 1.1 ahoka if (nref->nref_lnr == REF_LINK_TO_NEXT) {
238 1.1 ahoka new = chfs_alloc_refblock();
239 1.1 ahoka nref->nref_next = new;
240 1.1 ahoka nref = new;
241 1.1 ahoka }
242 1.1 ahoka
243 1.1 ahoka cheb->last_node = nref;
244 1.1 ahoka nref->nref_lnr = cheb->lnr;
245 1.1 ahoka
246 1.1 ahoka KASSERT(old->nref_lnr == nref->nref_lnr &&
247 1.1 ahoka nref->nref_lnr == cheb->lnr);
248 1.1 ahoka
249 1.1 ahoka return nref;
250 1.1 ahoka }
251 1.1 ahoka
252 1.1 ahoka /**
253 1.1 ahoka * chfs_free_node_refs - freeing an eraseblock's node refs
254 1.1 ahoka * @cheb: eraseblock information structure
255 1.1 ahoka */
256 1.1 ahoka void
257 1.1 ahoka chfs_free_node_refs(struct chfs_eraseblock *cheb)
258 1.1 ahoka {
259 1.1 ahoka struct chfs_node_ref *nref, *block;
260 1.1 ahoka
261 1.1 ahoka block = nref = cheb->first_node;
262 1.1 ahoka
263 1.1 ahoka while (nref) {
264 1.1 ahoka if (nref->nref_lnr == REF_LINK_TO_NEXT) {
265 1.1 ahoka nref = nref->nref_next;
266 1.1 ahoka chfs_free_refblock(block);
267 1.1 ahoka block = nref;
268 1.1 ahoka continue;
269 1.1 ahoka }
270 1.1 ahoka nref++;
271 1.1 ahoka }
272 1.1 ahoka }
273 1.1 ahoka
274 1.1 ahoka struct chfs_dirent*
275 1.1 ahoka chfs_alloc_dirent(int namesize)
276 1.1 ahoka {
277 1.1 ahoka struct chfs_dirent *ret;
278 1.1 ahoka size_t size = sizeof(struct chfs_dirent) + namesize;
279 1.1 ahoka
280 1.1 ahoka ret = kmem_alloc(size, KM_SLEEP);
281 1.1 ahoka //ret->alloc_size = size;
282 1.1 ahoka
283 1.1 ahoka return ret;
284 1.1 ahoka }
285 1.1 ahoka
286 1.1 ahoka void
287 1.1 ahoka chfs_free_dirent(struct chfs_dirent *dirent)
288 1.1 ahoka {
289 1.1 ahoka //size_t size = dirent->alloc_size;
290 1.1 ahoka size_t size = sizeof(struct chfs_dirent) + dirent->nsize + 1;
291 1.1 ahoka
292 1.1 ahoka kmem_free(dirent, size);
293 1.1 ahoka }
294 1.1 ahoka
295 1.1 ahoka struct chfs_full_dnode*
296 1.1.4.1 riz chfs_alloc_full_dnode(void)
297 1.1 ahoka {
298 1.1 ahoka struct chfs_full_dnode *ret;
299 1.1 ahoka ret = kmem_alloc(sizeof(struct chfs_full_dnode), KM_SLEEP);
300 1.1 ahoka return ret;
301 1.1 ahoka }
302 1.1 ahoka
303 1.1 ahoka void
304 1.1 ahoka chfs_free_full_dnode(struct chfs_full_dnode *fd)
305 1.1 ahoka {
306 1.1 ahoka kmem_free(fd,(sizeof(struct chfs_full_dnode)));
307 1.1 ahoka }
308 1.1 ahoka
309 1.1 ahoka struct chfs_flash_vnode*
310 1.1.4.1 riz chfs_alloc_flash_vnode(void)
311 1.1 ahoka {
312 1.1 ahoka struct chfs_flash_vnode *ret;
313 1.1 ahoka ret = pool_cache_get(chfs_flash_vnode_cache, 0);
314 1.1 ahoka return ret;
315 1.1 ahoka }
316 1.1 ahoka
317 1.1 ahoka void
318 1.1 ahoka chfs_free_flash_vnode(struct chfs_flash_vnode *fvnode)
319 1.1 ahoka {
320 1.1 ahoka pool_cache_put(chfs_flash_vnode_cache, fvnode);
321 1.1 ahoka }
322 1.1 ahoka
323 1.1 ahoka struct chfs_flash_dirent_node*
324 1.1.4.1 riz chfs_alloc_flash_dirent(void)
325 1.1 ahoka {
326 1.1 ahoka struct chfs_flash_dirent_node *ret;
327 1.1 ahoka ret = pool_cache_get(chfs_flash_dirent_cache, 0);
328 1.1 ahoka return ret;
329 1.1 ahoka }
330 1.1 ahoka
331 1.1 ahoka void
332 1.1 ahoka chfs_free_flash_dirent(struct chfs_flash_dirent_node *fdnode)
333 1.1 ahoka {
334 1.1 ahoka pool_cache_put(chfs_flash_dirent_cache, fdnode);
335 1.1 ahoka }
336 1.1 ahoka
337 1.1 ahoka struct chfs_flash_data_node*
338 1.1.4.1 riz chfs_alloc_flash_dnode(void)
339 1.1 ahoka {
340 1.1 ahoka struct chfs_flash_data_node *ret;
341 1.1 ahoka ret = pool_cache_get(chfs_flash_dnode_cache, 0);
342 1.1 ahoka return ret;
343 1.1 ahoka }
344 1.1 ahoka
345 1.1 ahoka void
346 1.1 ahoka chfs_free_flash_dnode(struct chfs_flash_data_node *fdnode)
347 1.1 ahoka {
348 1.1 ahoka pool_cache_put(chfs_flash_dnode_cache, fdnode);
349 1.1 ahoka }
350 1.1 ahoka
351 1.1 ahoka
352 1.1 ahoka struct chfs_node_frag*
353 1.1.4.1 riz chfs_alloc_node_frag(void)
354 1.1 ahoka {
355 1.1 ahoka struct chfs_node_frag *ret;
356 1.1 ahoka ret = pool_cache_get(chfs_node_frag_cache, 0);
357 1.1 ahoka return ret;
358 1.1 ahoka
359 1.1 ahoka }
360 1.1 ahoka
361 1.1 ahoka void
362 1.1 ahoka chfs_free_node_frag(struct chfs_node_frag *frag)
363 1.1 ahoka {
364 1.1 ahoka pool_cache_put(chfs_node_frag_cache, frag);
365 1.1 ahoka }
366 1.1 ahoka
367 1.1 ahoka struct chfs_tmp_dnode *
368 1.1.4.1 riz chfs_alloc_tmp_dnode(void)
369 1.1 ahoka {
370 1.1 ahoka struct chfs_tmp_dnode *ret;
371 1.1 ahoka ret = pool_cache_get(chfs_tmp_dnode_cache, 0);
372 1.1 ahoka ret->next = NULL;
373 1.1 ahoka return ret;
374 1.1 ahoka }
375 1.1 ahoka
376 1.1 ahoka void
377 1.1 ahoka chfs_free_tmp_dnode(struct chfs_tmp_dnode *td)
378 1.1 ahoka {
379 1.1 ahoka pool_cache_put(chfs_tmp_dnode_cache, td);
380 1.1 ahoka }
381 1.1 ahoka
382 1.1 ahoka struct chfs_tmp_dnode_info *
383 1.1.4.1 riz chfs_alloc_tmp_dnode_info(void)
384 1.1 ahoka {
385 1.1 ahoka struct chfs_tmp_dnode_info *ret;
386 1.1 ahoka ret = pool_cache_get(chfs_tmp_dnode_info_cache, 0);
387 1.1 ahoka ret->tmpnode = NULL;
388 1.1 ahoka return ret;
389 1.1 ahoka }
390 1.1 ahoka
391 1.1 ahoka void
392 1.1 ahoka chfs_free_tmp_dnode_info(struct chfs_tmp_dnode_info *di)
393 1.1 ahoka {
394 1.1 ahoka pool_cache_put(chfs_tmp_dnode_info_cache, di);
395 1.1 ahoka }
396 1.1 ahoka
397