chfs_nodeops.c revision 1.2 1 1.2 ttoth /* $NetBSD: chfs_nodeops.c,v 1.2 2012/08/10 09:26:58 ttoth 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.2 ttoth KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
84 1.2 ttoth
85 1.1 ahoka struct chfs_node_ref *nextref = *list;
86 1.1 ahoka struct chfs_node_ref *prevref = NULL;
87 1.1 ahoka
88 1.1 ahoka while (nextref && nextref != (struct chfs_node_ref *)vc &&
89 1.1 ahoka (nextref->nref_lnr <= new->nref_lnr)) {
90 1.1 ahoka if (nextref->nref_lnr == new->nref_lnr) {
91 1.1 ahoka while (nextref && nextref !=
92 1.1 ahoka (struct chfs_node_ref *)vc &&
93 1.1 ahoka (CHFS_GET_OFS(nextref->nref_offset) <
94 1.1 ahoka CHFS_GET_OFS(new->nref_offset))) {
95 1.1 ahoka prevref = nextref;
96 1.1 ahoka nextref = nextref->nref_next;
97 1.1 ahoka }
98 1.1 ahoka break;
99 1.1 ahoka }
100 1.1 ahoka prevref = nextref;
101 1.1 ahoka nextref = nextref->nref_next;
102 1.1 ahoka }
103 1.1 ahoka
104 1.1 ahoka if (nextref && nextref != (struct chfs_node_ref *)vc &&
105 1.1 ahoka nextref->nref_lnr == new->nref_lnr &&
106 1.1 ahoka CHFS_GET_OFS(nextref->nref_offset) ==
107 1.1 ahoka CHFS_GET_OFS(new->nref_offset)) {
108 1.1 ahoka new->nref_next = nextref->nref_next;
109 1.2 ttoth chfs_mark_node_obsolete(chmp, nextref);
110 1.1 ahoka } else {
111 1.1 ahoka new->nref_next = nextref;
112 1.1 ahoka }
113 1.1 ahoka
114 1.2 ttoth KASSERT(new->nref_next != NULL);
115 1.2 ttoth
116 1.1 ahoka if (prevref) {
117 1.1 ahoka prevref->nref_next = new;
118 1.1 ahoka } else {
119 1.1 ahoka *list = new;
120 1.1 ahoka }
121 1.1 ahoka }
122 1.1 ahoka
123 1.2 ttoth /*
124 1.2 ttoth * Removes a node from a list. Usually used for removing data nodes.
125 1.2 ttoth */
126 1.2 ttoth void
127 1.2 ttoth chfs_remove_node_from_list(struct chfs_mount *chmp,
128 1.2 ttoth struct chfs_vnode_cache *vc,
129 1.2 ttoth struct chfs_node_ref *old_nref, struct chfs_node_ref **list)
130 1.2 ttoth {
131 1.2 ttoth KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
132 1.2 ttoth KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
133 1.2 ttoth
134 1.2 ttoth struct chfs_node_ref *tmpnref;
135 1.2 ttoth
136 1.2 ttoth if (*list == (struct chfs_node_ref *)vc) {
137 1.2 ttoth return;
138 1.2 ttoth }
139 1.2 ttoth
140 1.2 ttoth KASSERT(old_nref->nref_next != NULL);
141 1.2 ttoth
142 1.2 ttoth if (*list == old_nref) {
143 1.2 ttoth *list = old_nref->nref_next;
144 1.2 ttoth } else {
145 1.2 ttoth tmpnref = *list;
146 1.2 ttoth while (tmpnref->nref_next &&
147 1.2 ttoth tmpnref->nref_next != (struct chfs_node_ref *)vc) {
148 1.2 ttoth if (tmpnref->nref_next == old_nref) {
149 1.2 ttoth tmpnref->nref_next = old_nref->nref_next;
150 1.2 ttoth break;
151 1.2 ttoth }
152 1.2 ttoth tmpnref = tmpnref->nref_next;
153 1.2 ttoth }
154 1.2 ttoth }
155 1.2 ttoth }
156 1.2 ttoth
157 1.2 ttoth /*
158 1.2 ttoth * Removes a node from a list and obsoletes the nref.
159 1.2 ttoth * We should use this function carefully on data nodes,
160 1.2 ttoth * because removing a frag will obsolete the node ref.
161 1.2 ttoth */
162 1.2 ttoth void
163 1.2 ttoth chfs_remove_and_obsolete(struct chfs_mount *chmp,
164 1.2 ttoth struct chfs_vnode_cache *vc,
165 1.2 ttoth struct chfs_node_ref *old_nref, struct chfs_node_ref **list)
166 1.2 ttoth {
167 1.2 ttoth KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
168 1.2 ttoth KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
169 1.2 ttoth
170 1.2 ttoth chfs_remove_node_from_list(chmp, vc, old_nref, list);
171 1.2 ttoth
172 1.2 ttoth dbg("[MARK] vno: %llu lnr: %u ofs: %u\n", vc->vno, old_nref->nref_lnr,
173 1.2 ttoth old_nref->nref_offset);
174 1.2 ttoth chfs_mark_node_obsolete(chmp, old_nref);
175 1.2 ttoth }
176 1.2 ttoth
177 1.1 ahoka void
178 1.1 ahoka chfs_add_fd_to_inode(struct chfs_mount *chmp,
179 1.1 ahoka struct chfs_inode *parent, struct chfs_dirent *new)
180 1.1 ahoka {
181 1.1 ahoka // struct chfs_dirent **prev = &parent->dents;
182 1.1 ahoka struct chfs_dirent *fd, *tmpfd;
183 1.1 ahoka
184 1.1 ahoka if (new->version > parent->chvc->highest_version) {
185 1.1 ahoka parent->chvc->highest_version = new->version;
186 1.1 ahoka }
187 1.1 ahoka
188 1.1 ahoka //mutex_enter(&parent->inode_lock);
189 1.1 ahoka TAILQ_FOREACH_SAFE(fd, &parent->dents, fds, tmpfd) {
190 1.1 ahoka if (fd->nhash > new->nhash) {
191 1.1 ahoka /* insert new before fd */
192 1.1 ahoka TAILQ_INSERT_BEFORE(fd, new, fds);
193 1.1 ahoka return;
194 1.1 ahoka } else if (fd->nhash == new->nhash &&
195 1.1 ahoka !strcmp(fd->name, new->name)) {
196 1.1 ahoka if (new->version > fd->version) {
197 1.1 ahoka // new->next = fd->next;
198 1.1 ahoka /* replace fd with new */
199 1.1 ahoka TAILQ_INSERT_BEFORE(fd, new, fds);
200 1.1 ahoka TAILQ_REMOVE(&parent->dents, fd, fds);
201 1.1 ahoka if (fd->nref) {
202 1.2 ttoth mutex_enter(&chmp->chm_lock_vnocache);
203 1.2 ttoth chfs_remove_and_obsolete(chmp, parent->chvc, fd->nref,
204 1.2 ttoth &parent->chvc->dirents);
205 1.2 ttoth mutex_exit(&chmp->chm_lock_vnocache);
206 1.1 ahoka }
207 1.1 ahoka chfs_free_dirent(fd);
208 1.1 ahoka // *prev = new;//XXX
209 1.1 ahoka } else {
210 1.1 ahoka chfs_mark_node_obsolete(chmp, new->nref);
211 1.1 ahoka chfs_free_dirent(new);
212 1.1 ahoka }
213 1.1 ahoka return;
214 1.1 ahoka }
215 1.1 ahoka }
216 1.1 ahoka /* if we couldnt fit it elsewhere, lets add to the end */
217 1.1 ahoka /* FIXME insert tail or insert head? */
218 1.1 ahoka TAILQ_INSERT_HEAD(&parent->dents, new, fds);
219 1.1 ahoka //mutex_exit(&parent->inode_lock);
220 1.1 ahoka #if 0
221 1.1 ahoka while ((*prev) && (*prev)->nhash <= new->nhash) {
222 1.1 ahoka if ((*prev)->nhash == new->nhash &&
223 1.1 ahoka !strcmp((*prev)->name, new->name)) {
224 1.1 ahoka if (new->version > (*prev)->version) {
225 1.1 ahoka new->next = (*prev)->next;
226 1.1 ahoka if ((*prev)->nref) {
227 1.1 ahoka chfs_mark_node_obsolete(chmp,
228 1.1 ahoka (*prev)->nref);
229 1.1 ahoka }
230 1.1 ahoka chfs_free_dirent(*prev);
231 1.1 ahoka *prev = new;
232 1.1 ahoka } else {
233 1.1 ahoka chfs_mark_node_obsolete(chmp, new->nref);
234 1.1 ahoka chfs_free_dirent(new);
235 1.1 ahoka }
236 1.1 ahoka return;
237 1.1 ahoka }
238 1.1 ahoka prev = &((*prev)->next);
239 1.1 ahoka }
240 1.1 ahoka
241 1.1 ahoka new->next = *prev;
242 1.1 ahoka *prev = new;
243 1.1 ahoka #endif
244 1.1 ahoka }
245 1.1 ahoka
246 1.1 ahoka void
247 1.1 ahoka chfs_add_vnode_ref_to_vc(struct chfs_mount *chmp,
248 1.1 ahoka struct chfs_vnode_cache *vc, struct chfs_node_ref *new)
249 1.1 ahoka {
250 1.2 ttoth KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
251 1.2 ttoth struct chfs_node_ref *nref;
252 1.2 ttoth
253 1.2 ttoth while (vc->v != (struct chfs_node_ref *)vc) {
254 1.2 ttoth nref = vc->v;
255 1.2 ttoth chfs_remove_and_obsolete(chmp, vc, nref, &vc->v);
256 1.1 ahoka }
257 1.2 ttoth
258 1.2 ttoth new->nref_next = (struct chfs_node_ref *)vc;
259 1.1 ahoka vc->v = new;
260 1.1 ahoka }
261 1.1 ahoka
262 1.1 ahoka struct chfs_node_ref *
263 1.1 ahoka chfs_nref_next(struct chfs_node_ref *nref)
264 1.1 ahoka {
265 1.1 ahoka // dbg("check nref: %u - %u\n", nref->nref_lnr, nref->nref_offset);
266 1.1 ahoka nref++;
267 1.1 ahoka // dbg("next nref: %u - %u\n", nref->nref_lnr, nref->nref_offset);
268 1.1 ahoka if (nref->nref_lnr == REF_LINK_TO_NEXT) {
269 1.1 ahoka //End of chain
270 1.1 ahoka if (!nref->nref_next)
271 1.1 ahoka return NULL;
272 1.1 ahoka
273 1.1 ahoka nref = nref->nref_next;
274 1.1 ahoka }
275 1.1 ahoka //end of chain
276 1.1 ahoka if (nref->nref_lnr == REF_EMPTY_NODE)
277 1.1 ahoka return NULL;
278 1.1 ahoka
279 1.1 ahoka return nref;
280 1.1 ahoka }
281 1.1 ahoka
282 1.1 ahoka int
283 1.1 ahoka chfs_nref_len(struct chfs_mount *chmp,
284 1.1 ahoka struct chfs_eraseblock *cheb, struct chfs_node_ref *nref)
285 1.1 ahoka {
286 1.1 ahoka struct chfs_node_ref *next;
287 1.1 ahoka
288 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
289 1.1 ahoka
290 1.1 ahoka if (!cheb)
291 1.1 ahoka cheb = &chmp->chm_blocks[nref->nref_lnr];
292 1.1 ahoka
293 1.1 ahoka next = chfs_nref_next(nref);
294 1.1 ahoka
295 1.1 ahoka if (!next) {
296 1.1 ahoka //dbg("next null\n");
297 1.1 ahoka return chmp->chm_ebh->eb_size - cheb->free_size -
298 1.1 ahoka CHFS_GET_OFS(nref->nref_offset);
299 1.1 ahoka }
300 1.1 ahoka //dbg("size: %d\n", CHFS_GET_OFS(next->nref_offset) - CHFS_GET_OFS(nref->nref_offset));
301 1.1 ahoka return CHFS_GET_OFS(next->nref_offset) -
302 1.1 ahoka CHFS_GET_OFS(nref->nref_offset);
303 1.1 ahoka }
304 1.1 ahoka
305 1.1 ahoka /**
306 1.1 ahoka * chfs_mark_node_obsolete - marks a node obsolete
307 1.1 ahoka */
308 1.1 ahoka void
309 1.1 ahoka chfs_mark_node_obsolete(struct chfs_mount *chmp,
310 1.1 ahoka struct chfs_node_ref *nref)
311 1.1 ahoka {
312 1.1 ahoka int len;
313 1.1 ahoka struct chfs_eraseblock *cheb;
314 1.1 ahoka
315 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
316 1.1 ahoka
317 1.1 ahoka KASSERT(!CHFS_REF_OBSOLETE(nref));
318 1.1 ahoka
319 1.1 ahoka KASSERT(nref->nref_lnr <= chmp->chm_ebh->peb_nr);
320 1.1 ahoka cheb = &chmp->chm_blocks[nref->nref_lnr];
321 1.1 ahoka
322 1.1 ahoka #ifdef DIAGNOSTIC
323 1.1 ahoka if (cheb->used_size + cheb->free_size + cheb->dirty_size +
324 1.1 ahoka cheb->unchecked_size + cheb->wasted_size != chmp->chm_ebh->eb_size) {
325 1.1 ahoka dbg("eraseblock leak detected!\nused: %u\nfree: %u\n"
326 1.1 ahoka "dirty: %u\nunchecked: %u\nwasted: %u\ntotal: %u\nshould be: %zu\n",
327 1.1 ahoka cheb->used_size, cheb->free_size, cheb->dirty_size,
328 1.1 ahoka cheb->unchecked_size, cheb->wasted_size, cheb->used_size + cheb->free_size +
329 1.1 ahoka cheb->dirty_size + cheb->unchecked_size + cheb->wasted_size,
330 1.1 ahoka chmp->chm_ebh->eb_size);
331 1.1 ahoka }
332 1.1 ahoka #endif
333 1.1 ahoka
334 1.1 ahoka len = chfs_nref_len(chmp, cheb, nref);
335 1.1 ahoka //dbg("len: %u\n", len);
336 1.1 ahoka //dbg("1. used: %u\n", cheb->used_size);
337 1.1 ahoka
338 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
339 1.1 ahoka
340 1.1 ahoka if (CHFS_REF_FLAGS(nref) == CHFS_UNCHECKED_NODE_MASK) {
341 1.1 ahoka //dbg("UNCHECKED mark an unchecked node\n");
342 1.1 ahoka chfs_change_size_unchecked(chmp, cheb, -len);
343 1.1 ahoka //dbg("unchecked: %u\n", chmp->chm_unchecked_size);
344 1.1 ahoka } else {
345 1.1 ahoka chfs_change_size_used(chmp, cheb, -len);
346 1.1 ahoka
347 1.1 ahoka //dbg("2. used: %u\n", cheb->used_size);
348 1.1 ahoka KASSERT(cheb->used_size <= chmp->chm_ebh->eb_size);
349 1.1 ahoka }
350 1.1 ahoka chfs_change_size_dirty(chmp, cheb, len);
351 1.1 ahoka
352 1.1 ahoka #ifdef DIAGNOSTIC
353 1.1 ahoka if (cheb->used_size + cheb->free_size + cheb->dirty_size +
354 1.1 ahoka cheb->unchecked_size + cheb->wasted_size != chmp->chm_ebh->eb_size) {
355 1.1 ahoka panic("eraseblock leak detected!\nused: %u\nfree: %u\n"
356 1.1 ahoka "dirty: %u\nunchecked: %u\nwasted: %u\ntotal: %u\nshould be: %zu\n",
357 1.1 ahoka cheb->used_size, cheb->free_size, cheb->dirty_size,
358 1.1 ahoka cheb->unchecked_size, cheb->wasted_size, cheb->used_size + cheb->free_size +
359 1.1 ahoka cheb->dirty_size + cheb->unchecked_size + cheb->wasted_size,
360 1.1 ahoka chmp->chm_ebh->eb_size);
361 1.1 ahoka }
362 1.1 ahoka #endif
363 1.1 ahoka nref->nref_offset = CHFS_GET_OFS(nref->nref_offset) |
364 1.1 ahoka CHFS_OBSOLETE_NODE_MASK;
365 1.1 ahoka
366 1.1 ahoka if (chmp->chm_flags & CHFS_MP_FLAG_SCANNING) {
367 1.1 ahoka /*Scan is in progress, do nothing now*/
368 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
369 1.1 ahoka return;
370 1.1 ahoka }
371 1.1 ahoka
372 1.1 ahoka if (cheb == chmp->chm_nextblock) {
373 1.1 ahoka dbg("Not moving nextblock to dirty/erase_pending list\n");
374 1.1 ahoka } else if (!cheb->used_size && !cheb->unchecked_size) {
375 1.1 ahoka if (cheb == chmp->chm_gcblock) {
376 1.1 ahoka dbg("gcblock is completely dirtied\n");
377 1.1 ahoka chmp->chm_gcblock = NULL;
378 1.1 ahoka } else {
379 1.1 ahoka //remove from a tailq, but we don't know which tailq contains this cheb
380 1.1 ahoka //so we remove it from the dirty list now
381 1.1 ahoka //TAILQ_REMOVE(&chmp->chm_dirty_queue, cheb, queue);
382 1.1 ahoka int removed = 0;
383 1.1 ahoka struct chfs_eraseblock *eb, *tmpeb;
384 1.1 ahoka //XXX ugly code
385 1.1 ahoka TAILQ_FOREACH_SAFE(eb, &chmp->chm_free_queue, queue, tmpeb) {
386 1.1 ahoka if (eb == cheb) {
387 1.1 ahoka TAILQ_REMOVE(&chmp->chm_free_queue, cheb, queue);
388 1.1 ahoka removed = 1;
389 1.1 ahoka break;
390 1.1 ahoka }
391 1.1 ahoka }
392 1.1 ahoka if (removed == 0) {
393 1.1 ahoka TAILQ_FOREACH_SAFE(eb, &chmp->chm_dirty_queue, queue, tmpeb) {
394 1.1 ahoka if (eb == cheb) {
395 1.1 ahoka TAILQ_REMOVE(&chmp->chm_dirty_queue, cheb, queue);
396 1.1 ahoka removed = 1;
397 1.1 ahoka break;
398 1.1 ahoka }
399 1.1 ahoka }
400 1.1 ahoka }
401 1.1 ahoka if (removed == 0) {
402 1.1 ahoka TAILQ_FOREACH_SAFE(eb, &chmp->chm_very_dirty_queue, queue, tmpeb) {
403 1.1 ahoka if (eb == cheb) {
404 1.1 ahoka TAILQ_REMOVE(&chmp->chm_very_dirty_queue, cheb, queue);
405 1.1 ahoka removed = 1;
406 1.1 ahoka break;
407 1.1 ahoka }
408 1.1 ahoka }
409 1.1 ahoka }
410 1.1 ahoka if (removed == 0) {
411 1.1 ahoka TAILQ_FOREACH_SAFE(eb, &chmp->chm_clean_queue, queue, tmpeb) {
412 1.1 ahoka if (eb == cheb) {
413 1.1 ahoka TAILQ_REMOVE(&chmp->chm_clean_queue, cheb, queue);
414 1.1 ahoka removed = 1;
415 1.1 ahoka break;
416 1.1 ahoka }
417 1.1 ahoka }
418 1.1 ahoka }
419 1.1 ahoka }
420 1.1 ahoka if (chmp->chm_wbuf_len) {
421 1.1 ahoka dbg("Adding block to erasable pending wbuf queue\n");
422 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_erasable_pending_wbuf_queue,
423 1.1 ahoka cheb, queue);
424 1.1 ahoka } else {
425 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_erase_pending_queue,
426 1.1 ahoka cheb, queue);
427 1.1 ahoka chmp->chm_nr_erasable_blocks++;
428 1.1 ahoka }
429 1.1 ahoka chfs_remap_leb(chmp);
430 1.1 ahoka } else if (cheb == chmp->chm_gcblock) {
431 1.1 ahoka dbg("Not moving gcblock to dirty list\n");
432 1.1 ahoka } else if (cheb->dirty_size > MAX_DIRTY_TO_CLEAN &&
433 1.1 ahoka cheb->dirty_size - len <= MAX_DIRTY_TO_CLEAN) {
434 1.1 ahoka dbg("Freshly dirtied, remove it from clean queue and "
435 1.1 ahoka "add it to dirty\n");
436 1.1 ahoka TAILQ_REMOVE(&chmp->chm_clean_queue, cheb, queue);
437 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_dirty_queue, cheb, queue);
438 1.1 ahoka } else if (VERY_DIRTY(chmp, cheb->dirty_size) &&
439 1.1 ahoka !VERY_DIRTY(chmp, cheb->dirty_size - len)) {
440 1.1 ahoka dbg("Becomes now very dirty, remove it from dirty "
441 1.1 ahoka "queue and add it to very dirty\n");
442 1.1 ahoka TAILQ_REMOVE(&chmp->chm_dirty_queue, cheb, queue);
443 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_very_dirty_queue, cheb, queue);
444 1.1 ahoka } else {
445 1.1 ahoka dbg("Leave cheb where it is\n");
446 1.1 ahoka }
447 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
448 1.1 ahoka return;
449 1.1 ahoka }
450 1.1 ahoka
451 1.1 ahoka /**
452 1.1 ahoka * chfs_close_eraseblock - close an eraseblock
453 1.1 ahoka * @chmp: chfs mount structure
454 1.1 ahoka * @cheb: eraseblock informations
455 1.1 ahoka *
456 1.1 ahoka * This function close the physical chain of the nodes on the eraseblock,
457 1.1 ahoka * convert its free size to dirty and add it to clean, dirty or very dirty list.
458 1.1 ahoka */
459 1.1 ahoka int
460 1.1 ahoka chfs_close_eraseblock(struct chfs_mount *chmp,
461 1.1 ahoka struct chfs_eraseblock *cheb)
462 1.1 ahoka {
463 1.1 ahoka uint32_t offset;
464 1.1 ahoka struct chfs_node_ref *nref;
465 1.1 ahoka
466 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
467 1.1 ahoka
468 1.1 ahoka offset = chmp->chm_ebh->eb_size - cheb->free_size;
469 1.1 ahoka
470 1.1 ahoka // Close the chain
471 1.1 ahoka nref = chfs_alloc_node_ref(cheb);
472 1.1 ahoka if (!nref)
473 1.1 ahoka return ENOMEM;
474 1.1 ahoka
475 1.1 ahoka nref->nref_next = NULL;
476 1.1 ahoka nref->nref_offset = offset;
477 1.1 ahoka
478 1.1 ahoka // Mark space as dirty
479 1.1 ahoka chfs_update_eb_dirty(chmp, cheb, cheb->free_size);
480 1.1 ahoka
481 1.1 ahoka if (cheb->dirty_size < MAX_DIRTY_TO_CLEAN) {
482 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_clean_queue, cheb, queue);
483 1.1 ahoka } else if (VERY_DIRTY(chmp, cheb->dirty_size)) {
484 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_very_dirty_queue, cheb, queue);
485 1.1 ahoka } else {
486 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_dirty_queue, cheb, queue);
487 1.1 ahoka }
488 1.1 ahoka return 0;
489 1.1 ahoka }
490 1.1 ahoka
491 1.1 ahoka int
492 1.1 ahoka chfs_reserve_space_normal(struct chfs_mount *chmp, uint32_t size, int prio)
493 1.1 ahoka {
494 1.1 ahoka int ret;
495 1.1 ahoka
496 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
497 1.1 ahoka
498 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
499 1.1 ahoka while (chmp->chm_nr_free_blocks + chmp->chm_nr_erasable_blocks < chmp->chm_resv_blocks_write) {
500 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);
501 1.1 ahoka uint32_t avail, dirty;
502 1.1 ahoka if (prio == ALLOC_DELETION && chmp->chm_nr_free_blocks + chmp->chm_nr_erasable_blocks >= chmp->chm_resv_blocks_deletion)
503 1.1 ahoka break;
504 1.1 ahoka
505 1.1 ahoka dirty = chmp->chm_dirty_size - chmp->chm_nr_erasable_blocks * chmp->chm_ebh->eb_size + chmp->chm_unchecked_size;
506 1.1 ahoka if (dirty < chmp->chm_nospc_dirty) {
507 1.1 ahoka dbg("dirty: %u < nospc_dirty: %u\n", dirty, chmp->chm_nospc_dirty);
508 1.1 ahoka ret = ENOSPC;
509 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
510 1.1 ahoka goto out;
511 1.1 ahoka }
512 1.1 ahoka
513 1.1 ahoka avail = chmp->chm_free_size - (chmp->chm_resv_blocks_write * chmp->chm_ebh->eb_size);
514 1.1 ahoka if (size > avail) {
515 1.1 ahoka dbg("size: %u > avail: %u\n", size, avail);
516 1.1 ahoka ret = ENOSPC;
517 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
518 1.1 ahoka goto out;
519 1.1 ahoka }
520 1.1 ahoka
521 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
522 1.1 ahoka ret = chfs_gcollect_pass(chmp);
523 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
524 1.1 ahoka
525 1.1 ahoka if (chmp->chm_nr_erasable_blocks ||
526 1.1 ahoka !TAILQ_EMPTY(&chmp->chm_erasable_pending_wbuf_queue) ||
527 1.1 ahoka ret == EAGAIN) {
528 1.1 ahoka ret = chfs_remap_leb(chmp);
529 1.1 ahoka }
530 1.1 ahoka
531 1.1 ahoka if (ret) {
532 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
533 1.1 ahoka goto out;
534 1.1 ahoka }
535 1.1 ahoka }
536 1.1 ahoka
537 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
538 1.1 ahoka ret = chfs_reserve_space(chmp, size);
539 1.1 ahoka out:
540 1.1 ahoka return ret;
541 1.1 ahoka }
542 1.1 ahoka
543 1.1 ahoka
544 1.1 ahoka int
545 1.1 ahoka chfs_reserve_space_gc(struct chfs_mount *chmp, uint32_t size)
546 1.1 ahoka {
547 1.1 ahoka int ret;
548 1.1 ahoka
549 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
550 1.1 ahoka
551 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
552 1.1 ahoka chfs_remap_leb(chmp);
553 1.1 ahoka
554 1.1 ahoka if (size > chmp->chm_free_size) {
555 1.1 ahoka dbg("size: %u\n", size);
556 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
557 1.1 ahoka return ENOSPC;
558 1.1 ahoka }
559 1.1 ahoka
560 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
561 1.1 ahoka ret = chfs_reserve_space(chmp, size);
562 1.1 ahoka return ret;
563 1.1 ahoka }
564 1.1 ahoka
565 1.1 ahoka /**
566 1.1 ahoka * chfs_reserve_space - finds a block which free size is >= requested size
567 1.1 ahoka * @chmp: chfs mount point
568 1.1 ahoka * @size: requested size
569 1.1 ahoka * @len: reserved spaced will be returned in this variable;
570 1.1 ahoka * Returns zero in case of success, error code in case of fail.
571 1.1 ahoka */
572 1.1 ahoka int
573 1.1 ahoka chfs_reserve_space(struct chfs_mount *chmp, uint32_t size)
574 1.1 ahoka {
575 1.1 ahoka //TODO define minimum reserved blocks, which is needed for writing
576 1.1 ahoka //TODO check we have enough free blocks to write
577 1.1 ahoka //TODO if no: need erase and GC
578 1.1 ahoka
579 1.1 ahoka int err;
580 1.1 ahoka struct chfs_eraseblock *cheb;
581 1.1 ahoka
582 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
583 1.1 ahoka KASSERT(!mutex_owned(&chmp->chm_lock_sizes));
584 1.1 ahoka
585 1.1 ahoka cheb = chmp->chm_nextblock;
586 1.1 ahoka //if (cheb)
587 1.1 ahoka //dbg("cheb->free_size %u\n", cheb->free_size);
588 1.1 ahoka if (cheb && size > cheb->free_size) {
589 1.1 ahoka dbg("size: %u > free_size: %u\n", size, cheb->free_size);
590 1.1 ahoka /*
591 1.1 ahoka * There isn't enough space on this eraseblock, we mark this as
592 1.1 ahoka * dirty and close the physical chain of the node refs.
593 1.1 ahoka */
594 1.1 ahoka //Write out pending data if any
595 1.1 ahoka if (chmp->chm_wbuf_len) {
596 1.1 ahoka chfs_flush_pending_wbuf(chmp);
597 1.1 ahoka //FIXME need goto restart here?
598 1.1 ahoka }
599 1.1 ahoka
600 1.1 ahoka while (chmp->chm_wbuf_ofs < chmp->chm_ebh->eb_size) {
601 1.1 ahoka dbg("wbuf ofs: %zu - eb_size: %zu\n",
602 1.1 ahoka chmp->chm_wbuf_ofs, chmp->chm_ebh->eb_size);
603 1.1 ahoka chfs_flush_pending_wbuf(chmp);
604 1.1 ahoka }
605 1.1 ahoka
606 1.1 ahoka if (!(chmp->chm_wbuf_ofs % chmp->chm_ebh->eb_size) && !chmp->chm_wbuf_len)
607 1.1 ahoka chmp->chm_wbuf_ofs = 0xffffffff;
608 1.1 ahoka
609 1.1 ahoka err = chfs_close_eraseblock(chmp, cheb);
610 1.1 ahoka if (err)
611 1.1 ahoka return err;
612 1.1 ahoka
613 1.1 ahoka cheb = NULL;
614 1.1 ahoka }
615 1.1 ahoka if (!cheb) {
616 1.1 ahoka //get a block for nextblock
617 1.1 ahoka if (TAILQ_EMPTY(&chmp->chm_free_queue)) {
618 1.1 ahoka // If this succeeds there will be a block on free_queue
619 1.1 ahoka dbg("cheb remap (free: %d)\n", chmp->chm_nr_free_blocks);
620 1.1 ahoka err = chfs_remap_leb(chmp);
621 1.1 ahoka if (err)
622 1.1 ahoka return err;
623 1.1 ahoka }
624 1.1 ahoka cheb = TAILQ_FIRST(&chmp->chm_free_queue);
625 1.1 ahoka TAILQ_REMOVE(&chmp->chm_free_queue, cheb, queue);
626 1.1 ahoka chmp->chm_nextblock = cheb;
627 1.1 ahoka chmp->chm_nr_free_blocks--;
628 1.1 ahoka }
629 1.1 ahoka
630 1.1 ahoka return 0;
631 1.1 ahoka }
632 1.1 ahoka
633