chfs_malloc.c revision 1.4.30.1 1 1.4.30.1 martin /* $NetBSD: chfs_malloc.c,v 1.4.30.1 2018/02/27 09:07:33 martin 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.4 ttoth /* chfs_alloc_pool_caches - allocating pool caches */
48 1.1 ahoka int
49 1.2 christos chfs_alloc_pool_caches(void)
50 1.1 ahoka {
51 1.1 ahoka chfs_vnode_cache = pool_cache_init(
52 1.1 ahoka sizeof(struct chfs_vnode_cache),
53 1.1 ahoka 0, 0, 0, "chfs_vnode_cache", NULL, IPL_NONE, NULL, NULL,
54 1.1 ahoka NULL);
55 1.1 ahoka if (!chfs_vnode_cache)
56 1.1 ahoka goto err_vnode;
57 1.1 ahoka
58 1.1 ahoka chfs_nrefs_cache = pool_cache_init(
59 1.1 ahoka (REFS_BLOCK_LEN + 1) * sizeof(struct chfs_node_ref), 0, 0,
60 1.1 ahoka 0, "chfs_nrefs_pool", NULL, IPL_NONE, NULL, NULL, NULL);
61 1.1 ahoka if (!chfs_nrefs_cache)
62 1.1 ahoka goto err_nrefs;
63 1.1 ahoka
64 1.1 ahoka chfs_flash_vnode_cache = pool_cache_init(
65 1.1 ahoka sizeof(struct chfs_flash_vnode), 0, 0, 0,
66 1.1 ahoka "chfs_flash_vnode_pool", NULL, IPL_NONE, NULL, NULL, NULL);
67 1.1 ahoka if (!chfs_flash_vnode_cache)
68 1.1 ahoka goto err_flash_vnode;
69 1.1 ahoka
70 1.1 ahoka chfs_flash_dirent_cache = pool_cache_init(
71 1.1 ahoka sizeof(struct chfs_flash_dirent_node), 0, 0, 0,
72 1.1 ahoka "chfs_flash_dirent_pool", NULL, IPL_NONE, NULL, NULL, NULL);
73 1.1 ahoka if (!chfs_flash_dirent_cache)
74 1.1 ahoka goto err_flash_dirent;
75 1.1 ahoka
76 1.1 ahoka chfs_flash_dnode_cache = pool_cache_init(
77 1.1 ahoka sizeof(struct chfs_flash_data_node), 0, 0, 0,
78 1.1 ahoka "chfs_flash_dnode_pool", NULL, IPL_NONE, NULL, NULL, NULL);
79 1.1 ahoka if (!chfs_flash_dnode_cache)
80 1.1 ahoka goto err_flash_dnode;
81 1.1 ahoka
82 1.1 ahoka chfs_node_frag_cache = pool_cache_init(
83 1.1 ahoka sizeof(struct chfs_node_frag), 0, 0, 0,
84 1.1 ahoka "chfs_node_frag_pool", NULL, IPL_NONE, NULL, NULL, NULL);
85 1.1 ahoka if (!chfs_node_frag_cache)
86 1.1 ahoka goto err_node_frag;
87 1.1 ahoka
88 1.1 ahoka chfs_tmp_dnode_cache = pool_cache_init(
89 1.1 ahoka sizeof(struct chfs_tmp_dnode), 0, 0, 0,
90 1.1 ahoka "chfs_tmp_dnode_pool", NULL, IPL_NONE, NULL, NULL, NULL);
91 1.1 ahoka if (!chfs_tmp_dnode_cache)
92 1.1 ahoka goto err_tmp_dnode;
93 1.1 ahoka
94 1.1 ahoka chfs_tmp_dnode_info_cache = pool_cache_init(
95 1.1 ahoka sizeof(struct chfs_tmp_dnode_info), 0, 0, 0,
96 1.1 ahoka "chfs_tmp_dnode_info_pool", NULL, IPL_NONE, NULL, NULL, NULL);
97 1.1 ahoka if (!chfs_tmp_dnode_info_cache)
98 1.1 ahoka goto err_tmp_dnode_info;
99 1.1 ahoka
100 1.1 ahoka return 0;
101 1.1 ahoka
102 1.1 ahoka err_tmp_dnode_info:
103 1.1 ahoka pool_cache_destroy(chfs_tmp_dnode_cache);
104 1.1 ahoka err_tmp_dnode:
105 1.1 ahoka pool_cache_destroy(chfs_node_frag_cache);
106 1.1 ahoka err_node_frag:
107 1.1 ahoka pool_cache_destroy(chfs_flash_dnode_cache);
108 1.1 ahoka err_flash_dnode:
109 1.1 ahoka pool_cache_destroy(chfs_flash_dirent_cache);
110 1.1 ahoka err_flash_dirent:
111 1.1 ahoka pool_cache_destroy(chfs_flash_vnode_cache);
112 1.1 ahoka err_flash_vnode:
113 1.1 ahoka pool_cache_destroy(chfs_nrefs_cache);
114 1.1 ahoka err_nrefs:
115 1.1 ahoka pool_cache_destroy(chfs_vnode_cache);
116 1.1 ahoka err_vnode:
117 1.1 ahoka
118 1.1 ahoka return ENOMEM;
119 1.1 ahoka }
120 1.1 ahoka
121 1.4 ttoth /* chfs_destroy_pool_caches - destroying pool caches */
122 1.1 ahoka void
123 1.2 christos chfs_destroy_pool_caches(void)
124 1.1 ahoka {
125 1.1 ahoka if (chfs_vnode_cache)
126 1.1 ahoka pool_cache_destroy(chfs_vnode_cache);
127 1.1 ahoka
128 1.1 ahoka if (chfs_nrefs_cache)
129 1.1 ahoka pool_cache_destroy(chfs_nrefs_cache);
130 1.1 ahoka
131 1.1 ahoka if (chfs_flash_vnode_cache)
132 1.1 ahoka pool_cache_destroy(chfs_flash_vnode_cache);
133 1.1 ahoka
134 1.1 ahoka if (chfs_flash_dirent_cache)
135 1.1 ahoka pool_cache_destroy(chfs_flash_dirent_cache);
136 1.1 ahoka
137 1.1 ahoka if (chfs_flash_dnode_cache)
138 1.1 ahoka pool_cache_destroy(chfs_flash_dnode_cache);
139 1.1 ahoka
140 1.1 ahoka if (chfs_node_frag_cache)
141 1.1 ahoka pool_cache_destroy(chfs_node_frag_cache);
142 1.1 ahoka
143 1.1 ahoka if (chfs_tmp_dnode_cache)
144 1.1 ahoka pool_cache_destroy(chfs_tmp_dnode_cache);
145 1.1 ahoka
146 1.1 ahoka if (chfs_tmp_dnode_info_cache)
147 1.1 ahoka pool_cache_destroy(chfs_tmp_dnode_info_cache);
148 1.1 ahoka }
149 1.1 ahoka
150 1.4 ttoth /* chfs_vnode_cache_alloc - allocating and initializing a vnode cache */
151 1.1 ahoka struct chfs_vnode_cache *
152 1.1 ahoka chfs_vnode_cache_alloc(ino_t vno)
153 1.1 ahoka {
154 1.1 ahoka struct chfs_vnode_cache* vc;
155 1.1 ahoka vc = pool_cache_get(chfs_vnode_cache, PR_WAITOK);
156 1.1 ahoka
157 1.1 ahoka memset(vc, 0, sizeof(*vc));
158 1.1 ahoka vc->vno = vno;
159 1.4 ttoth /* vnode cache is the last element of all chain */
160 1.1 ahoka vc->v = (void *)vc;
161 1.1 ahoka vc->dirents = (void *)vc;
162 1.1 ahoka vc->dnode = (void *)vc;
163 1.1 ahoka TAILQ_INIT(&vc->scan_dirents);
164 1.1 ahoka vc->highest_version = 0;
165 1.1 ahoka
166 1.1 ahoka return vc;
167 1.1 ahoka }
168 1.1 ahoka
169 1.4 ttoth /* chfs_vnode_cache_free - freeing a vnode cache */
170 1.1 ahoka void
171 1.1 ahoka chfs_vnode_cache_free(struct chfs_vnode_cache *vc)
172 1.1 ahoka {
173 1.1 ahoka pool_cache_put(chfs_vnode_cache, vc);
174 1.1 ahoka }
175 1.1 ahoka
176 1.4 ttoth /*
177 1.1 ahoka * chfs_alloc_refblock - allocating a refblock
178 1.1 ahoka *
179 1.1 ahoka * Returns a pointer of the first element in the block.
180 1.1 ahoka *
181 1.1 ahoka * We are not allocating just one node ref, instead we allocating REFS_BLOCK_LEN
182 1.1 ahoka * number of node refs, the last element will be a pointer to the next block.
183 1.1 ahoka * We do this, because we need a chain of nodes which have been ordered by the
184 1.1 ahoka * physical address of them.
185 1.1 ahoka *
186 1.1 ahoka */
187 1.1 ahoka struct chfs_node_ref*
188 1.1 ahoka chfs_alloc_refblock(void)
189 1.1 ahoka {
190 1.1 ahoka int i;
191 1.1 ahoka struct chfs_node_ref *nref;
192 1.1 ahoka nref = pool_cache_get(chfs_nrefs_cache, PR_WAITOK);
193 1.1 ahoka
194 1.1 ahoka for (i = 0; i < REFS_BLOCK_LEN; i++) {
195 1.1 ahoka nref[i].nref_lnr = REF_EMPTY_NODE;
196 1.1 ahoka nref[i].nref_next = NULL;
197 1.1 ahoka }
198 1.1 ahoka i = REFS_BLOCK_LEN;
199 1.1 ahoka nref[i].nref_lnr = REF_LINK_TO_NEXT;
200 1.1 ahoka nref[i].nref_next = NULL;
201 1.1 ahoka
202 1.1 ahoka return nref;
203 1.1 ahoka }
204 1.1 ahoka
205 1.4 ttoth /* chfs_free_refblock - freeing a refblock */
206 1.1 ahoka void
207 1.1 ahoka chfs_free_refblock(struct chfs_node_ref *nref)
208 1.1 ahoka {
209 1.1 ahoka pool_cache_put(chfs_nrefs_cache, nref);
210 1.1 ahoka }
211 1.1 ahoka
212 1.4 ttoth /*
213 1.1 ahoka * chfs_alloc_node_ref - allocating a node ref from a refblock
214 1.1 ahoka *
215 1.1 ahoka * Allocating a node ref from a refblock, it there isn't any free element in the
216 1.1 ahoka * block, a new block will be allocated and be linked to the current block.
217 1.1 ahoka */
218 1.1 ahoka struct chfs_node_ref*
219 1.1 ahoka chfs_alloc_node_ref(struct chfs_eraseblock *cheb)
220 1.1 ahoka {
221 1.1 ahoka struct chfs_node_ref *nref, *new, *old;
222 1.1 ahoka old = cheb->last_node;
223 1.1 ahoka nref = cheb->last_node;
224 1.1 ahoka
225 1.1 ahoka if (!nref) {
226 1.4 ttoth /* There haven't been any nref allocated for this block yet */
227 1.1 ahoka nref = chfs_alloc_refblock();
228 1.1 ahoka
229 1.1 ahoka cheb->first_node = nref;
230 1.1 ahoka cheb->last_node = nref;
231 1.1 ahoka nref->nref_lnr = cheb->lnr;
232 1.1 ahoka KASSERT(cheb->lnr == nref->nref_lnr);
233 1.1 ahoka
234 1.1 ahoka return nref;
235 1.1 ahoka }
236 1.1 ahoka
237 1.1 ahoka nref++;
238 1.1 ahoka if (nref->nref_lnr == REF_LINK_TO_NEXT) {
239 1.4 ttoth /* this was the last element, allocate a new block */
240 1.1 ahoka new = chfs_alloc_refblock();
241 1.1 ahoka nref->nref_next = new;
242 1.1 ahoka nref = new;
243 1.1 ahoka }
244 1.1 ahoka
245 1.1 ahoka cheb->last_node = nref;
246 1.1 ahoka nref->nref_lnr = cheb->lnr;
247 1.1 ahoka
248 1.1 ahoka KASSERT(old->nref_lnr == nref->nref_lnr &&
249 1.1 ahoka nref->nref_lnr == cheb->lnr);
250 1.1 ahoka
251 1.1 ahoka return nref;
252 1.1 ahoka }
253 1.1 ahoka
254 1.4 ttoth /* chfs_free_node_refs - freeing an eraseblock's node refs */
255 1.1 ahoka void
256 1.1 ahoka chfs_free_node_refs(struct chfs_eraseblock *cheb)
257 1.1 ahoka {
258 1.1 ahoka struct chfs_node_ref *nref, *block;
259 1.1 ahoka
260 1.1 ahoka block = nref = cheb->first_node;
261 1.1 ahoka
262 1.1 ahoka while (nref) {
263 1.1 ahoka if (nref->nref_lnr == REF_LINK_TO_NEXT) {
264 1.1 ahoka nref = nref->nref_next;
265 1.1 ahoka chfs_free_refblock(block);
266 1.1 ahoka block = nref;
267 1.1 ahoka continue;
268 1.1 ahoka }
269 1.1 ahoka nref++;
270 1.1 ahoka }
271 1.1 ahoka }
272 1.1 ahoka
273 1.4 ttoth /* chfs_alloc_dirent - allocating a directory entry */
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
282 1.1 ahoka return ret;
283 1.1 ahoka }
284 1.1 ahoka
285 1.4 ttoth /* chfs_free_dirent - freeing a directory entry */
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 = sizeof(struct chfs_dirent) + dirent->nsize + 1;
290 1.1 ahoka
291 1.1 ahoka kmem_free(dirent, size);
292 1.1 ahoka }
293 1.1 ahoka
294 1.4 ttoth /* chfs_alloc_full_dnode - allocating a full data node */
295 1.1 ahoka struct chfs_full_dnode*
296 1.2 christos 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.3 ttoth ret->nref = NULL;
301 1.3 ttoth ret->frags = 0;
302 1.1 ahoka return ret;
303 1.1 ahoka }
304 1.1 ahoka
305 1.4 ttoth /* chfs_free_full_dnode - freeing a full data node */
306 1.1 ahoka void
307 1.1 ahoka chfs_free_full_dnode(struct chfs_full_dnode *fd)
308 1.1 ahoka {
309 1.1 ahoka kmem_free(fd,(sizeof(struct chfs_full_dnode)));
310 1.1 ahoka }
311 1.1 ahoka
312 1.4 ttoth /* chfs_alloc_flash_vnode - allocating vnode info (used on flash) */
313 1.1 ahoka struct chfs_flash_vnode*
314 1.2 christos chfs_alloc_flash_vnode(void)
315 1.1 ahoka {
316 1.1 ahoka struct chfs_flash_vnode *ret;
317 1.4.30.1 martin ret = pool_cache_get(chfs_flash_vnode_cache, PR_WAITOK);
318 1.1 ahoka return ret;
319 1.1 ahoka }
320 1.1 ahoka
321 1.4 ttoth /* chfs_free_flash_vnode - freeing vnode info */
322 1.1 ahoka void
323 1.1 ahoka chfs_free_flash_vnode(struct chfs_flash_vnode *fvnode)
324 1.1 ahoka {
325 1.1 ahoka pool_cache_put(chfs_flash_vnode_cache, fvnode);
326 1.1 ahoka }
327 1.1 ahoka
328 1.4 ttoth /* chfs_alloc_flash_dirent - allocating a directory entry (used on flash) */
329 1.1 ahoka struct chfs_flash_dirent_node*
330 1.2 christos chfs_alloc_flash_dirent(void)
331 1.1 ahoka {
332 1.1 ahoka struct chfs_flash_dirent_node *ret;
333 1.4.30.1 martin ret = pool_cache_get(chfs_flash_dirent_cache, PR_WAITOK);
334 1.1 ahoka return ret;
335 1.1 ahoka }
336 1.1 ahoka
337 1.4 ttoth /* chfs_free_flash_dirent - freeing a (flash) directory entry */
338 1.1 ahoka void
339 1.1 ahoka chfs_free_flash_dirent(struct chfs_flash_dirent_node *fdnode)
340 1.1 ahoka {
341 1.1 ahoka pool_cache_put(chfs_flash_dirent_cache, fdnode);
342 1.1 ahoka }
343 1.1 ahoka
344 1.4 ttoth /* chfs_alloc_flash_dnode - allocating a data node (used on flash) */
345 1.1 ahoka struct chfs_flash_data_node*
346 1.2 christos chfs_alloc_flash_dnode(void)
347 1.1 ahoka {
348 1.1 ahoka struct chfs_flash_data_node *ret;
349 1.4.30.1 martin ret = pool_cache_get(chfs_flash_dnode_cache, PR_WAITOK);
350 1.1 ahoka return ret;
351 1.1 ahoka }
352 1.1 ahoka
353 1.4 ttoth /* chfs_free_flash_dnode - freeing a (flash) data node */
354 1.1 ahoka void
355 1.1 ahoka chfs_free_flash_dnode(struct chfs_flash_data_node *fdnode)
356 1.1 ahoka {
357 1.1 ahoka pool_cache_put(chfs_flash_dnode_cache, fdnode);
358 1.1 ahoka }
359 1.1 ahoka
360 1.4 ttoth /* chfs_alloc_node_frag - allocating a fragment of a node */
361 1.1 ahoka struct chfs_node_frag*
362 1.2 christos chfs_alloc_node_frag(void)
363 1.1 ahoka {
364 1.1 ahoka struct chfs_node_frag *ret;
365 1.4.30.1 martin ret = pool_cache_get(chfs_node_frag_cache, PR_WAITOK);
366 1.1 ahoka return ret;
367 1.1 ahoka }
368 1.1 ahoka
369 1.4 ttoth /* chfs_free_node_frag - freeing a fragment of a node */
370 1.1 ahoka void
371 1.1 ahoka chfs_free_node_frag(struct chfs_node_frag *frag)
372 1.1 ahoka {
373 1.1 ahoka pool_cache_put(chfs_node_frag_cache, frag);
374 1.1 ahoka }
375 1.1 ahoka
376 1.4 ttoth /* chfs_alloc_tmp_dnode - allocating a temporarly used dnode */
377 1.1 ahoka struct chfs_tmp_dnode *
378 1.2 christos chfs_alloc_tmp_dnode(void)
379 1.1 ahoka {
380 1.1 ahoka struct chfs_tmp_dnode *ret;
381 1.4.30.1 martin ret = pool_cache_get(chfs_tmp_dnode_cache, PR_WAITOK);
382 1.1 ahoka ret->next = NULL;
383 1.1 ahoka return ret;
384 1.1 ahoka }
385 1.1 ahoka
386 1.4 ttoth /* chfs_free_tmp_dnode - freeing a temporarly used dnode */
387 1.1 ahoka void
388 1.1 ahoka chfs_free_tmp_dnode(struct chfs_tmp_dnode *td)
389 1.1 ahoka {
390 1.1 ahoka pool_cache_put(chfs_tmp_dnode_cache, td);
391 1.1 ahoka }
392 1.1 ahoka
393 1.4 ttoth /* chfs_alloc_tmp_dnode_info - allocating a temporarly used dnode descriptor */
394 1.1 ahoka struct chfs_tmp_dnode_info *
395 1.2 christos chfs_alloc_tmp_dnode_info(void)
396 1.1 ahoka {
397 1.1 ahoka struct chfs_tmp_dnode_info *ret;
398 1.4.30.1 martin ret = pool_cache_get(chfs_tmp_dnode_info_cache, PR_WAITOK);
399 1.1 ahoka ret->tmpnode = NULL;
400 1.1 ahoka return ret;
401 1.1 ahoka }
402 1.1 ahoka
403 1.4 ttoth /* chfs_free_tmp_dnode_info - freeing a temporarly used dnode descriptor */
404 1.1 ahoka void
405 1.1 ahoka chfs_free_tmp_dnode_info(struct chfs_tmp_dnode_info *di)
406 1.1 ahoka {
407 1.1 ahoka pool_cache_put(chfs_tmp_dnode_info_cache, di);
408 1.1 ahoka }
409 1.1 ahoka
410