chfs_nodeops.c revision 1.1 1 1.1 ahoka /* $NetBSD: chfs_nodeops.c,v 1.1 2011/11/24 15:51:31 ahoka 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 David Tengeri <dtengeri (at) inf.u-szeged.hu>
7 1.1 ahoka * Copyright (C) 2010 Tamas Toth <ttoth (at) inf.u-szeged.hu>
8 1.1 ahoka * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
9 1.1 ahoka * All rights reserved.
10 1.1 ahoka *
11 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation
12 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary
13 1.1 ahoka *
14 1.1 ahoka * Redistribution and use in source and binary forms, with or without
15 1.1 ahoka * modification, are permitted provided that the following conditions
16 1.1 ahoka * are met:
17 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
18 1.1 ahoka * notice, this list of conditions and the following disclaimer.
19 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
21 1.1 ahoka * documentation and/or other materials provided with the distribution.
22 1.1 ahoka *
23 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 ahoka * SUCH DAMAGE.
34 1.1 ahoka */
35 1.1 ahoka
36 1.1 ahoka #include "chfs.h"
37 1.1 ahoka
38 1.1 ahoka /**
39 1.1 ahoka * chfs_update_eb_dirty - updates dirty and free space, first and
40 1.1 ahoka * last node references
41 1.1 ahoka * @sbi: CHFS main descriptor structure
42 1.1 ahoka * @cheb: eraseblock to update
43 1.1 ahoka * @size: increase dirty space size with this
44 1.1 ahoka * Returns zero in case of success, %1 in case of fail.
45 1.1 ahoka */
46 1.1 ahoka int
47 1.1 ahoka chfs_update_eb_dirty(struct chfs_mount *chmp,
48 1.1 ahoka struct chfs_eraseblock *cheb, uint32_t size)
49 1.1 ahoka {
50 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
51 1.1 ahoka KASSERT(!mutex_owned(&chmp->chm_lock_sizes));
52 1.1 ahoka
53 1.1 ahoka if (!size)
54 1.1 ahoka return 0;
55 1.1 ahoka
56 1.1 ahoka if (size > cheb->free_size) {
57 1.1 ahoka chfs_err("free_size (%d) is less then dirty space (%d) "
58 1.1 ahoka "on block (%d)\n", cheb->free_size, size, cheb->lnr);
59 1.1 ahoka return 1;
60 1.1 ahoka }
61 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
62 1.1 ahoka //dbg("BEFORE: free_size: %d\n", cheb->free_size);
63 1.1 ahoka chfs_change_size_free(chmp, cheb, -size);
64 1.1 ahoka chfs_change_size_dirty(chmp, cheb, size);
65 1.1 ahoka //dbg(" AFTER: free_size: %d\n", cheb->free_size);
66 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
67 1.1 ahoka return 0;
68 1.1 ahoka }
69 1.1 ahoka
70 1.1 ahoka /**
71 1.1 ahoka * chfs_add_node_to_list - adds a data node ref to vnode cache's dnode list
72 1.1 ahoka * @sbi: super block informations
73 1.1 ahoka * @new: node ref to insert
74 1.1 ahoka * @list: head of the list
75 1.1 ahoka * This function inserts a data node ref to the list of vnode cache.
76 1.1 ahoka * The list is sorted by data node's lnr and offset.
77 1.1 ahoka */
78 1.1 ahoka void
79 1.1 ahoka chfs_add_node_to_list(struct chfs_mount *chmp,
80 1.1 ahoka struct chfs_vnode_cache *vc,
81 1.1 ahoka struct chfs_node_ref *new, struct chfs_node_ref **list)
82 1.1 ahoka {
83 1.1 ahoka struct chfs_node_ref *nextref = *list;
84 1.1 ahoka struct chfs_node_ref *prevref = NULL;
85 1.1 ahoka
86 1.1 ahoka while (nextref && nextref != (struct chfs_node_ref *)vc &&
87 1.1 ahoka (nextref->nref_lnr <= new->nref_lnr)) {
88 1.1 ahoka if (nextref->nref_lnr == new->nref_lnr) {
89 1.1 ahoka while (nextref && nextref !=
90 1.1 ahoka (struct chfs_node_ref *)vc &&
91 1.1 ahoka (CHFS_GET_OFS(nextref->nref_offset) <
92 1.1 ahoka CHFS_GET_OFS(new->nref_offset))) {
93 1.1 ahoka prevref = nextref;
94 1.1 ahoka nextref = nextref->nref_next;
95 1.1 ahoka }
96 1.1 ahoka break;
97 1.1 ahoka }
98 1.1 ahoka prevref = nextref;
99 1.1 ahoka nextref = nextref->nref_next;
100 1.1 ahoka }
101 1.1 ahoka
102 1.1 ahoka if (nextref && nextref != (struct chfs_node_ref *)vc &&
103 1.1 ahoka nextref->nref_lnr == new->nref_lnr &&
104 1.1 ahoka CHFS_GET_OFS(nextref->nref_offset) ==
105 1.1 ahoka CHFS_GET_OFS(new->nref_offset)) {
106 1.1 ahoka new->nref_next = nextref->nref_next;
107 1.1 ahoka } else {
108 1.1 ahoka new->nref_next = nextref;
109 1.1 ahoka }
110 1.1 ahoka
111 1.1 ahoka if (prevref) {
112 1.1 ahoka prevref->nref_next = new;
113 1.1 ahoka } else {
114 1.1 ahoka *list = new;
115 1.1 ahoka }
116 1.1 ahoka }
117 1.1 ahoka
118 1.1 ahoka void
119 1.1 ahoka chfs_add_fd_to_inode(struct chfs_mount *chmp,
120 1.1 ahoka struct chfs_inode *parent, struct chfs_dirent *new)
121 1.1 ahoka {
122 1.1 ahoka // struct chfs_dirent **prev = &parent->dents;
123 1.1 ahoka struct chfs_dirent *fd, *tmpfd;
124 1.1 ahoka
125 1.1 ahoka if (new->version > parent->chvc->highest_version) {
126 1.1 ahoka parent->chvc->highest_version = new->version;
127 1.1 ahoka }
128 1.1 ahoka
129 1.1 ahoka //mutex_enter(&parent->inode_lock);
130 1.1 ahoka TAILQ_FOREACH_SAFE(fd, &parent->dents, fds, tmpfd) {
131 1.1 ahoka if (fd->nhash > new->nhash) {
132 1.1 ahoka /* insert new before fd */
133 1.1 ahoka TAILQ_INSERT_BEFORE(fd, new, fds);
134 1.1 ahoka return;
135 1.1 ahoka } else if (fd->nhash == new->nhash &&
136 1.1 ahoka !strcmp(fd->name, new->name)) {
137 1.1 ahoka if (new->version > fd->version) {
138 1.1 ahoka // new->next = fd->next;
139 1.1 ahoka /* replace fd with new */
140 1.1 ahoka TAILQ_INSERT_BEFORE(fd, new, fds);
141 1.1 ahoka TAILQ_REMOVE(&parent->dents, fd, fds);
142 1.1 ahoka if (fd->nref) {
143 1.1 ahoka chfs_mark_node_obsolete(chmp,
144 1.1 ahoka fd->nref);
145 1.1 ahoka }
146 1.1 ahoka chfs_free_dirent(fd);
147 1.1 ahoka // *prev = new;//XXX
148 1.1 ahoka } else {
149 1.1 ahoka chfs_mark_node_obsolete(chmp, new->nref);
150 1.1 ahoka chfs_free_dirent(new);
151 1.1 ahoka }
152 1.1 ahoka return;
153 1.1 ahoka }
154 1.1 ahoka }
155 1.1 ahoka /* if we couldnt fit it elsewhere, lets add to the end */
156 1.1 ahoka /* FIXME insert tail or insert head? */
157 1.1 ahoka TAILQ_INSERT_HEAD(&parent->dents, new, fds);
158 1.1 ahoka //mutex_exit(&parent->inode_lock);
159 1.1 ahoka #if 0
160 1.1 ahoka while ((*prev) && (*prev)->nhash <= new->nhash) {
161 1.1 ahoka if ((*prev)->nhash == new->nhash &&
162 1.1 ahoka !strcmp((*prev)->name, new->name)) {
163 1.1 ahoka if (new->version > (*prev)->version) {
164 1.1 ahoka new->next = (*prev)->next;
165 1.1 ahoka if ((*prev)->nref) {
166 1.1 ahoka chfs_mark_node_obsolete(chmp,
167 1.1 ahoka (*prev)->nref);
168 1.1 ahoka }
169 1.1 ahoka chfs_free_dirent(*prev);
170 1.1 ahoka *prev = new;
171 1.1 ahoka } else {
172 1.1 ahoka chfs_mark_node_obsolete(chmp, new->nref);
173 1.1 ahoka chfs_free_dirent(new);
174 1.1 ahoka }
175 1.1 ahoka return;
176 1.1 ahoka }
177 1.1 ahoka prev = &((*prev)->next);
178 1.1 ahoka }
179 1.1 ahoka
180 1.1 ahoka new->next = *prev;
181 1.1 ahoka *prev = new;
182 1.1 ahoka #endif
183 1.1 ahoka }
184 1.1 ahoka
185 1.1 ahoka void
186 1.1 ahoka chfs_add_vnode_ref_to_vc(struct chfs_mount *chmp,
187 1.1 ahoka struct chfs_vnode_cache *vc, struct chfs_node_ref *new)
188 1.1 ahoka {
189 1.1 ahoka if ((struct chfs_vnode_cache*)(vc->v) != vc) {
190 1.1 ahoka chfs_mark_node_obsolete(chmp, vc->v);
191 1.1 ahoka new->nref_next = vc->v->nref_next;
192 1.1 ahoka } else {
193 1.1 ahoka new->nref_next = vc->v;
194 1.1 ahoka }
195 1.1 ahoka vc->v = new;
196 1.1 ahoka }
197 1.1 ahoka
198 1.1 ahoka struct chfs_node_ref *
199 1.1 ahoka chfs_nref_next(struct chfs_node_ref *nref)
200 1.1 ahoka {
201 1.1 ahoka // dbg("check nref: %u - %u\n", nref->nref_lnr, nref->nref_offset);
202 1.1 ahoka nref++;
203 1.1 ahoka // dbg("next nref: %u - %u\n", nref->nref_lnr, nref->nref_offset);
204 1.1 ahoka if (nref->nref_lnr == REF_LINK_TO_NEXT) {
205 1.1 ahoka //End of chain
206 1.1 ahoka if (!nref->nref_next)
207 1.1 ahoka return NULL;
208 1.1 ahoka
209 1.1 ahoka nref = nref->nref_next;
210 1.1 ahoka }
211 1.1 ahoka //end of chain
212 1.1 ahoka if (nref->nref_lnr == REF_EMPTY_NODE)
213 1.1 ahoka return NULL;
214 1.1 ahoka
215 1.1 ahoka return nref;
216 1.1 ahoka }
217 1.1 ahoka
218 1.1 ahoka int
219 1.1 ahoka chfs_nref_len(struct chfs_mount *chmp,
220 1.1 ahoka struct chfs_eraseblock *cheb, struct chfs_node_ref *nref)
221 1.1 ahoka {
222 1.1 ahoka struct chfs_node_ref *next;
223 1.1 ahoka
224 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
225 1.1 ahoka
226 1.1 ahoka if (!cheb)
227 1.1 ahoka cheb = &chmp->chm_blocks[nref->nref_lnr];
228 1.1 ahoka
229 1.1 ahoka next = chfs_nref_next(nref);
230 1.1 ahoka
231 1.1 ahoka if (!next) {
232 1.1 ahoka //dbg("next null\n");
233 1.1 ahoka return chmp->chm_ebh->eb_size - cheb->free_size -
234 1.1 ahoka CHFS_GET_OFS(nref->nref_offset);
235 1.1 ahoka }
236 1.1 ahoka //dbg("size: %d\n", CHFS_GET_OFS(next->nref_offset) - CHFS_GET_OFS(nref->nref_offset));
237 1.1 ahoka return CHFS_GET_OFS(next->nref_offset) -
238 1.1 ahoka CHFS_GET_OFS(nref->nref_offset);
239 1.1 ahoka }
240 1.1 ahoka
241 1.1 ahoka /**
242 1.1 ahoka * chfs_mark_node_obsolete - marks a node obsolete
243 1.1 ahoka */
244 1.1 ahoka void
245 1.1 ahoka chfs_mark_node_obsolete(struct chfs_mount *chmp,
246 1.1 ahoka struct chfs_node_ref *nref)
247 1.1 ahoka {
248 1.1 ahoka int len;
249 1.1 ahoka struct chfs_eraseblock *cheb;
250 1.1 ahoka
251 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
252 1.1 ahoka
253 1.1 ahoka KASSERT(!CHFS_REF_OBSOLETE(nref));
254 1.1 ahoka
255 1.1 ahoka KASSERT(nref->nref_lnr <= chmp->chm_ebh->peb_nr);
256 1.1 ahoka cheb = &chmp->chm_blocks[nref->nref_lnr];
257 1.1 ahoka
258 1.1 ahoka #ifdef DIAGNOSTIC
259 1.1 ahoka if (cheb->used_size + cheb->free_size + cheb->dirty_size +
260 1.1 ahoka cheb->unchecked_size + cheb->wasted_size != chmp->chm_ebh->eb_size) {
261 1.1 ahoka dbg("eraseblock leak detected!\nused: %u\nfree: %u\n"
262 1.1 ahoka "dirty: %u\nunchecked: %u\nwasted: %u\ntotal: %u\nshould be: %zu\n",
263 1.1 ahoka cheb->used_size, cheb->free_size, cheb->dirty_size,
264 1.1 ahoka cheb->unchecked_size, cheb->wasted_size, cheb->used_size + cheb->free_size +
265 1.1 ahoka cheb->dirty_size + cheb->unchecked_size + cheb->wasted_size,
266 1.1 ahoka chmp->chm_ebh->eb_size);
267 1.1 ahoka }
268 1.1 ahoka #endif
269 1.1 ahoka
270 1.1 ahoka len = chfs_nref_len(chmp, cheb, nref);
271 1.1 ahoka //dbg("len: %u\n", len);
272 1.1 ahoka //dbg("1. used: %u\n", cheb->used_size);
273 1.1 ahoka
274 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
275 1.1 ahoka
276 1.1 ahoka if (CHFS_REF_FLAGS(nref) == CHFS_UNCHECKED_NODE_MASK) {
277 1.1 ahoka //dbg("UNCHECKED mark an unchecked node\n");
278 1.1 ahoka chfs_change_size_unchecked(chmp, cheb, -len);
279 1.1 ahoka //dbg("unchecked: %u\n", chmp->chm_unchecked_size);
280 1.1 ahoka } else {
281 1.1 ahoka chfs_change_size_used(chmp, cheb, -len);
282 1.1 ahoka
283 1.1 ahoka //dbg("2. used: %u\n", cheb->used_size);
284 1.1 ahoka KASSERT(cheb->used_size <= chmp->chm_ebh->eb_size);
285 1.1 ahoka }
286 1.1 ahoka chfs_change_size_dirty(chmp, cheb, len);
287 1.1 ahoka
288 1.1 ahoka #ifdef DIAGNOSTIC
289 1.1 ahoka if (cheb->used_size + cheb->free_size + cheb->dirty_size +
290 1.1 ahoka cheb->unchecked_size + cheb->wasted_size != chmp->chm_ebh->eb_size) {
291 1.1 ahoka panic("eraseblock leak detected!\nused: %u\nfree: %u\n"
292 1.1 ahoka "dirty: %u\nunchecked: %u\nwasted: %u\ntotal: %u\nshould be: %zu\n",
293 1.1 ahoka cheb->used_size, cheb->free_size, cheb->dirty_size,
294 1.1 ahoka cheb->unchecked_size, cheb->wasted_size, cheb->used_size + cheb->free_size +
295 1.1 ahoka cheb->dirty_size + cheb->unchecked_size + cheb->wasted_size,
296 1.1 ahoka chmp->chm_ebh->eb_size);
297 1.1 ahoka }
298 1.1 ahoka #endif
299 1.1 ahoka nref->nref_offset = CHFS_GET_OFS(nref->nref_offset) |
300 1.1 ahoka CHFS_OBSOLETE_NODE_MASK;
301 1.1 ahoka
302 1.1 ahoka if (chmp->chm_flags & CHFS_MP_FLAG_SCANNING) {
303 1.1 ahoka /*Scan is in progress, do nothing now*/
304 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
305 1.1 ahoka return;
306 1.1 ahoka }
307 1.1 ahoka
308 1.1 ahoka if (cheb == chmp->chm_nextblock) {
309 1.1 ahoka dbg("Not moving nextblock to dirty/erase_pending list\n");
310 1.1 ahoka } else if (!cheb->used_size && !cheb->unchecked_size) {
311 1.1 ahoka if (cheb == chmp->chm_gcblock) {
312 1.1 ahoka dbg("gcblock is completely dirtied\n");
313 1.1 ahoka chmp->chm_gcblock = NULL;
314 1.1 ahoka } else {
315 1.1 ahoka //remove from a tailq, but we don't know which tailq contains this cheb
316 1.1 ahoka //so we remove it from the dirty list now
317 1.1 ahoka //TAILQ_REMOVE(&chmp->chm_dirty_queue, cheb, queue);
318 1.1 ahoka int removed = 0;
319 1.1 ahoka struct chfs_eraseblock *eb, *tmpeb;
320 1.1 ahoka //XXX ugly code
321 1.1 ahoka TAILQ_FOREACH_SAFE(eb, &chmp->chm_free_queue, queue, tmpeb) {
322 1.1 ahoka if (eb == cheb) {
323 1.1 ahoka TAILQ_REMOVE(&chmp->chm_free_queue, cheb, queue);
324 1.1 ahoka removed = 1;
325 1.1 ahoka break;
326 1.1 ahoka }
327 1.1 ahoka }
328 1.1 ahoka if (removed == 0) {
329 1.1 ahoka TAILQ_FOREACH_SAFE(eb, &chmp->chm_dirty_queue, queue, tmpeb) {
330 1.1 ahoka if (eb == cheb) {
331 1.1 ahoka TAILQ_REMOVE(&chmp->chm_dirty_queue, cheb, queue);
332 1.1 ahoka removed = 1;
333 1.1 ahoka break;
334 1.1 ahoka }
335 1.1 ahoka }
336 1.1 ahoka }
337 1.1 ahoka if (removed == 0) {
338 1.1 ahoka TAILQ_FOREACH_SAFE(eb, &chmp->chm_very_dirty_queue, queue, tmpeb) {
339 1.1 ahoka if (eb == cheb) {
340 1.1 ahoka TAILQ_REMOVE(&chmp->chm_very_dirty_queue, cheb, queue);
341 1.1 ahoka removed = 1;
342 1.1 ahoka break;
343 1.1 ahoka }
344 1.1 ahoka }
345 1.1 ahoka }
346 1.1 ahoka if (removed == 0) {
347 1.1 ahoka TAILQ_FOREACH_SAFE(eb, &chmp->chm_clean_queue, queue, tmpeb) {
348 1.1 ahoka if (eb == cheb) {
349 1.1 ahoka TAILQ_REMOVE(&chmp->chm_clean_queue, cheb, queue);
350 1.1 ahoka removed = 1;
351 1.1 ahoka break;
352 1.1 ahoka }
353 1.1 ahoka }
354 1.1 ahoka }
355 1.1 ahoka }
356 1.1 ahoka if (chmp->chm_wbuf_len) {
357 1.1 ahoka dbg("Adding block to erasable pending wbuf queue\n");
358 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_erasable_pending_wbuf_queue,
359 1.1 ahoka cheb, queue);
360 1.1 ahoka } else {
361 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_erase_pending_queue,
362 1.1 ahoka cheb, queue);
363 1.1 ahoka chmp->chm_nr_erasable_blocks++;
364 1.1 ahoka }
365 1.1 ahoka chfs_remap_leb(chmp);
366 1.1 ahoka } else if (cheb == chmp->chm_gcblock) {
367 1.1 ahoka dbg("Not moving gcblock to dirty list\n");
368 1.1 ahoka } else if (cheb->dirty_size > MAX_DIRTY_TO_CLEAN &&
369 1.1 ahoka cheb->dirty_size - len <= MAX_DIRTY_TO_CLEAN) {
370 1.1 ahoka dbg("Freshly dirtied, remove it from clean queue and "
371 1.1 ahoka "add it to dirty\n");
372 1.1 ahoka TAILQ_REMOVE(&chmp->chm_clean_queue, cheb, queue);
373 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_dirty_queue, cheb, queue);
374 1.1 ahoka } else if (VERY_DIRTY(chmp, cheb->dirty_size) &&
375 1.1 ahoka !VERY_DIRTY(chmp, cheb->dirty_size - len)) {
376 1.1 ahoka dbg("Becomes now very dirty, remove it from dirty "
377 1.1 ahoka "queue and add it to very dirty\n");
378 1.1 ahoka TAILQ_REMOVE(&chmp->chm_dirty_queue, cheb, queue);
379 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_very_dirty_queue, cheb, queue);
380 1.1 ahoka } else {
381 1.1 ahoka dbg("Leave cheb where it is\n");
382 1.1 ahoka }
383 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
384 1.1 ahoka return;
385 1.1 ahoka }
386 1.1 ahoka
387 1.1 ahoka /**
388 1.1 ahoka * chfs_close_eraseblock - close an eraseblock
389 1.1 ahoka * @chmp: chfs mount structure
390 1.1 ahoka * @cheb: eraseblock informations
391 1.1 ahoka *
392 1.1 ahoka * This function close the physical chain of the nodes on the eraseblock,
393 1.1 ahoka * convert its free size to dirty and add it to clean, dirty or very dirty list.
394 1.1 ahoka */
395 1.1 ahoka int
396 1.1 ahoka chfs_close_eraseblock(struct chfs_mount *chmp,
397 1.1 ahoka struct chfs_eraseblock *cheb)
398 1.1 ahoka {
399 1.1 ahoka uint32_t offset;
400 1.1 ahoka struct chfs_node_ref *nref;
401 1.1 ahoka
402 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
403 1.1 ahoka
404 1.1 ahoka offset = chmp->chm_ebh->eb_size - cheb->free_size;
405 1.1 ahoka
406 1.1 ahoka // Close the chain
407 1.1 ahoka nref = chfs_alloc_node_ref(cheb);
408 1.1 ahoka if (!nref)
409 1.1 ahoka return ENOMEM;
410 1.1 ahoka
411 1.1 ahoka nref->nref_next = NULL;
412 1.1 ahoka nref->nref_offset = offset;
413 1.1 ahoka
414 1.1 ahoka // Mark space as dirty
415 1.1 ahoka chfs_update_eb_dirty(chmp, cheb, cheb->free_size);
416 1.1 ahoka
417 1.1 ahoka if (cheb->dirty_size < MAX_DIRTY_TO_CLEAN) {
418 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_clean_queue, cheb, queue);
419 1.1 ahoka } else if (VERY_DIRTY(chmp, cheb->dirty_size)) {
420 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_very_dirty_queue, cheb, queue);
421 1.1 ahoka } else {
422 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_dirty_queue, cheb, queue);
423 1.1 ahoka }
424 1.1 ahoka return 0;
425 1.1 ahoka }
426 1.1 ahoka
427 1.1 ahoka int
428 1.1 ahoka chfs_reserve_space_normal(struct chfs_mount *chmp, uint32_t size, int prio)
429 1.1 ahoka {
430 1.1 ahoka int ret;
431 1.1 ahoka
432 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
433 1.1 ahoka
434 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
435 1.1 ahoka while (chmp->chm_nr_free_blocks + chmp->chm_nr_erasable_blocks < chmp->chm_resv_blocks_write) {
436 1.1 ahoka dbg("free: %d, erasable: %d, resv: %d\n", chmp->chm_nr_free_blocks, chmp->chm_nr_erasable_blocks, chmp->chm_resv_blocks_write);
437 1.1 ahoka uint32_t avail, dirty;
438 1.1 ahoka if (prio == ALLOC_DELETION && chmp->chm_nr_free_blocks + chmp->chm_nr_erasable_blocks >= chmp->chm_resv_blocks_deletion)
439 1.1 ahoka break;
440 1.1 ahoka
441 1.1 ahoka dirty = chmp->chm_dirty_size - chmp->chm_nr_erasable_blocks * chmp->chm_ebh->eb_size + chmp->chm_unchecked_size;
442 1.1 ahoka if (dirty < chmp->chm_nospc_dirty) {
443 1.1 ahoka dbg("dirty: %u < nospc_dirty: %u\n", dirty, chmp->chm_nospc_dirty);
444 1.1 ahoka ret = ENOSPC;
445 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
446 1.1 ahoka goto out;
447 1.1 ahoka }
448 1.1 ahoka
449 1.1 ahoka avail = chmp->chm_free_size - (chmp->chm_resv_blocks_write * chmp->chm_ebh->eb_size);
450 1.1 ahoka if (size > avail) {
451 1.1 ahoka dbg("size: %u > avail: %u\n", size, avail);
452 1.1 ahoka ret = ENOSPC;
453 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
454 1.1 ahoka goto out;
455 1.1 ahoka }
456 1.1 ahoka
457 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
458 1.1 ahoka ret = chfs_gcollect_pass(chmp);
459 1.1 ahoka /* gcollect_pass exits chm_lock_mountfields */
460 1.1 ahoka mutex_enter(&chmp->chm_lock_mountfields);
461 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
462 1.1 ahoka
463 1.1 ahoka if (chmp->chm_nr_erasable_blocks ||
464 1.1 ahoka !TAILQ_EMPTY(&chmp->chm_erasable_pending_wbuf_queue) ||
465 1.1 ahoka ret == EAGAIN) {
466 1.1 ahoka ret = chfs_remap_leb(chmp);
467 1.1 ahoka }
468 1.1 ahoka
469 1.1 ahoka if (ret) {
470 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
471 1.1 ahoka goto out;
472 1.1 ahoka }
473 1.1 ahoka }
474 1.1 ahoka
475 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
476 1.1 ahoka ret = chfs_reserve_space(chmp, size);
477 1.1 ahoka out:
478 1.1 ahoka return ret;
479 1.1 ahoka }
480 1.1 ahoka
481 1.1 ahoka
482 1.1 ahoka int
483 1.1 ahoka chfs_reserve_space_gc(struct chfs_mount *chmp, uint32_t size)
484 1.1 ahoka {
485 1.1 ahoka int ret;
486 1.1 ahoka
487 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
488 1.1 ahoka
489 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
490 1.1 ahoka chfs_remap_leb(chmp);
491 1.1 ahoka
492 1.1 ahoka if (size > chmp->chm_free_size) {
493 1.1 ahoka dbg("size: %u\n", size);
494 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
495 1.1 ahoka return ENOSPC;
496 1.1 ahoka }
497 1.1 ahoka
498 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
499 1.1 ahoka ret = chfs_reserve_space(chmp, size);
500 1.1 ahoka return ret;
501 1.1 ahoka }
502 1.1 ahoka
503 1.1 ahoka /**
504 1.1 ahoka * chfs_reserve_space - finds a block which free size is >= requested size
505 1.1 ahoka * @chmp: chfs mount point
506 1.1 ahoka * @size: requested size
507 1.1 ahoka * @len: reserved spaced will be returned in this variable;
508 1.1 ahoka * Returns zero in case of success, error code in case of fail.
509 1.1 ahoka */
510 1.1 ahoka int
511 1.1 ahoka chfs_reserve_space(struct chfs_mount *chmp, uint32_t size)
512 1.1 ahoka {
513 1.1 ahoka //TODO define minimum reserved blocks, which is needed for writing
514 1.1 ahoka //TODO check we have enough free blocks to write
515 1.1 ahoka //TODO if no: need erase and GC
516 1.1 ahoka
517 1.1 ahoka int err;
518 1.1 ahoka struct chfs_eraseblock *cheb;
519 1.1 ahoka
520 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
521 1.1 ahoka KASSERT(!mutex_owned(&chmp->chm_lock_sizes));
522 1.1 ahoka
523 1.1 ahoka cheb = chmp->chm_nextblock;
524 1.1 ahoka //if (cheb)
525 1.1 ahoka //dbg("cheb->free_size %u\n", cheb->free_size);
526 1.1 ahoka if (cheb && size > cheb->free_size) {
527 1.1 ahoka dbg("size: %u > free_size: %u\n", size, cheb->free_size);
528 1.1 ahoka /*
529 1.1 ahoka * There isn't enough space on this eraseblock, we mark this as
530 1.1 ahoka * dirty and close the physical chain of the node refs.
531 1.1 ahoka */
532 1.1 ahoka //Write out pending data if any
533 1.1 ahoka if (chmp->chm_wbuf_len) {
534 1.1 ahoka chfs_flush_pending_wbuf(chmp);
535 1.1 ahoka //FIXME need goto restart here?
536 1.1 ahoka }
537 1.1 ahoka
538 1.1 ahoka while (chmp->chm_wbuf_ofs < chmp->chm_ebh->eb_size) {
539 1.1 ahoka dbg("wbuf ofs: %zu - eb_size: %zu\n",
540 1.1 ahoka chmp->chm_wbuf_ofs, chmp->chm_ebh->eb_size);
541 1.1 ahoka chfs_flush_pending_wbuf(chmp);
542 1.1 ahoka }
543 1.1 ahoka
544 1.1 ahoka if (!(chmp->chm_wbuf_ofs % chmp->chm_ebh->eb_size) && !chmp->chm_wbuf_len)
545 1.1 ahoka chmp->chm_wbuf_ofs = 0xffffffff;
546 1.1 ahoka
547 1.1 ahoka err = chfs_close_eraseblock(chmp, cheb);
548 1.1 ahoka if (err)
549 1.1 ahoka return err;
550 1.1 ahoka
551 1.1 ahoka cheb = NULL;
552 1.1 ahoka }
553 1.1 ahoka if (!cheb) {
554 1.1 ahoka //get a block for nextblock
555 1.1 ahoka if (TAILQ_EMPTY(&chmp->chm_free_queue)) {
556 1.1 ahoka // If this succeeds there will be a block on free_queue
557 1.1 ahoka dbg("cheb remap (free: %d)\n", chmp->chm_nr_free_blocks);
558 1.1 ahoka err = chfs_remap_leb(chmp);
559 1.1 ahoka if (err)
560 1.1 ahoka return err;
561 1.1 ahoka }
562 1.1 ahoka cheb = TAILQ_FIRST(&chmp->chm_free_queue);
563 1.1 ahoka TAILQ_REMOVE(&chmp->chm_free_queue, cheb, queue);
564 1.1 ahoka chmp->chm_nextblock = cheb;
565 1.1 ahoka chmp->chm_nr_free_blocks--;
566 1.1 ahoka }
567 1.1 ahoka
568 1.1 ahoka return 0;
569 1.1 ahoka }
570 1.1 ahoka
571