Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * JFFS2 -- Journalling Flash File System, Version 2. | |
3 | * | |
4 | * Copyright (C) 2001-2003 Red Hat, Inc. | |
5 | * | |
6 | * Created by David Woodhouse <dwmw2@infradead.org> | |
7 | * | |
8 | * For licensing information, see the file 'LICENCE' in this directory. | |
9 | * | |
1e900979 | 10 | * $Id: nodelist.c,v 1.103 2005/07/31 08:20:44 dedekind Exp $ |
1da177e4 LT |
11 | * |
12 | */ | |
13 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/sched.h> | |
16 | #include <linux/fs.h> | |
17 | #include <linux/mtd/mtd.h> | |
18 | #include <linux/rbtree.h> | |
19 | #include <linux/crc32.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/pagemap.h> | |
22 | #include "nodelist.h" | |
23 | ||
24 | void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list) | |
25 | { | |
26 | struct jffs2_full_dirent **prev = list; | |
e0d60137 AB |
27 | |
28 | JFFS2_DBG_DENTLIST("add dirent \"%s\", ino #%u\n", new->name, new->ino); | |
1da177e4 LT |
29 | |
30 | while ((*prev) && (*prev)->nhash <= new->nhash) { | |
31 | if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) { | |
32 | /* Duplicate. Free one */ | |
33 | if (new->version < (*prev)->version) { | |
e0d60137 AB |
34 | JFFS2_DBG_DENTLIST("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n", |
35 | (*prev)->name, (*prev)->ino); | |
1da177e4 LT |
36 | jffs2_mark_node_obsolete(c, new->raw); |
37 | jffs2_free_full_dirent(new); | |
38 | } else { | |
e0d60137 AB |
39 | JFFS2_DBG_DENTLIST("marking old dirent \"%s\", ino #%u bsolete\n", |
40 | (*prev)->name, (*prev)->ino); | |
1da177e4 LT |
41 | new->next = (*prev)->next; |
42 | jffs2_mark_node_obsolete(c, ((*prev)->raw)); | |
43 | jffs2_free_full_dirent(*prev); | |
44 | *prev = new; | |
45 | } | |
e0d60137 | 46 | return; |
1da177e4 LT |
47 | } |
48 | prev = &((*prev)->next); | |
49 | } | |
50 | new->next = *prev; | |
51 | *prev = new; | |
1da177e4 LT |
52 | } |
53 | ||
1e900979 AB |
54 | void jffs2_truncate_fragtree(struct jffs2_sb_info *c, struct rb_root *list, uint32_t size) |
55 | { | |
56 | struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size); | |
57 | ||
58 | JFFS2_DBG_FRAGTREE("truncating fragtree to 0x%08x bytes\n", size); | |
59 | ||
60 | /* We know frag->ofs <= size. That's what lookup does for us */ | |
61 | if (frag && frag->ofs != size) { | |
62 | if (frag->ofs+frag->size >= size) { | |
63 | JFFS2_DBG_FRAGTREE2("truncating frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size); | |
64 | frag->size = size - frag->ofs; | |
65 | } | |
66 | frag = frag_next(frag); | |
67 | } | |
68 | while (frag && frag->ofs >= size) { | |
69 | struct jffs2_node_frag *next = frag_next(frag); | |
70 | ||
71 | JFFS2_DBG_FRAGTREE("removing frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size); | |
72 | frag_erase(frag, list); | |
73 | jffs2_obsolete_node_frag(c, frag); | |
74 | frag = next; | |
75 | } | |
76 | } | |
77 | ||
f97117d1 | 78 | void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this) |
1da177e4 | 79 | { |
f97117d1 AB |
80 | if (this->node) { |
81 | this->node->frags--; | |
82 | if (!this->node->frags) { | |
83 | /* The node has no valid frags left. It's totally obsoleted */ | |
e0d60137 AB |
84 | JFFS2_DBG_FRAGTREE2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n", |
85 | ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size); | |
f97117d1 AB |
86 | jffs2_mark_node_obsolete(c, this->node->raw); |
87 | jffs2_free_full_dnode(this->node); | |
88 | } else { | |
e0d60137 AB |
89 | JFFS2_DBG_FRAGTREE2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n", |
90 | ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags); | |
f97117d1 AB |
91 | mark_ref_normal(this->node->raw); |
92 | } | |
93 | ||
94 | } | |
95 | jffs2_free_node_frag(this); | |
1da177e4 LT |
96 | } |
97 | ||
f97117d1 | 98 | static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base) |
1da177e4 | 99 | { |
f97117d1 AB |
100 | struct rb_node *parent = &base->rb; |
101 | struct rb_node **link = &parent; | |
9dee7503 | 102 | |
e0d60137 | 103 | JFFS2_DBG_FRAGTREE2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size); |
9dee7503 | 104 | |
f97117d1 AB |
105 | while (*link) { |
106 | parent = *link; | |
107 | base = rb_entry(parent, struct jffs2_node_frag, rb); | |
108 | ||
e0d60137 | 109 | JFFS2_DBG_FRAGTREE2("considering frag at 0x%08x\n", base->ofs); |
f97117d1 AB |
110 | if (newfrag->ofs > base->ofs) |
111 | link = &base->rb.rb_right; | |
112 | else if (newfrag->ofs < base->ofs) | |
113 | link = &base->rb.rb_left; | |
9dee7503 | 114 | else { |
e0d60137 | 115 | JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base); |
f97117d1 | 116 | BUG(); |
9dee7503 | 117 | } |
1da177e4 | 118 | } |
f97117d1 AB |
119 | |
120 | rb_link_node(&newfrag->rb, &base->rb, link); | |
1da177e4 LT |
121 | } |
122 | ||
f97117d1 AB |
123 | /* Doesn't set inode->i_size */ |
124 | static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *list, struct jffs2_node_frag *newfrag) | |
1da177e4 | 125 | { |
f97117d1 AB |
126 | struct jffs2_node_frag *this; |
127 | uint32_t lastend; | |
1da177e4 | 128 | |
f97117d1 AB |
129 | /* Skip all the nodes which are completed before this one starts */ |
130 | this = jffs2_lookup_node_frag(list, newfrag->node->ofs); | |
1da177e4 | 131 | |
f97117d1 | 132 | if (this) { |
e0d60137 AB |
133 | JFFS2_DBG_FRAGTREE2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n", |
134 | this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this); | |
f97117d1 AB |
135 | lastend = this->ofs + this->size; |
136 | } else { | |
e0d60137 | 137 | JFFS2_DBG_FRAGTREE2("lookup gave no frag\n"); |
f97117d1 | 138 | lastend = 0; |
1da177e4 | 139 | } |
f97117d1 AB |
140 | |
141 | /* See if we ran off the end of the list */ | |
142 | if (lastend <= newfrag->ofs) { | |
143 | /* We did */ | |
144 | ||
145 | /* Check if 'this' node was on the same page as the new node. | |
146 | If so, both 'this' and the new node get marked REF_NORMAL so | |
147 | the GC can take a look. | |
148 | */ | |
149 | if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) { | |
150 | if (this->node) | |
151 | mark_ref_normal(this->node->raw); | |
152 | mark_ref_normal(newfrag->node->raw); | |
153 | } | |
1da177e4 | 154 | |
f97117d1 AB |
155 | if (lastend < newfrag->node->ofs) { |
156 | /* ... and we need to put a hole in before the new node */ | |
157 | struct jffs2_node_frag *holefrag = jffs2_alloc_node_frag(); | |
158 | if (!holefrag) { | |
159 | jffs2_free_node_frag(newfrag); | |
160 | return -ENOMEM; | |
161 | } | |
162 | holefrag->ofs = lastend; | |
163 | holefrag->size = newfrag->node->ofs - lastend; | |
164 | holefrag->node = NULL; | |
165 | if (this) { | |
166 | /* By definition, the 'this' node has no right-hand child, | |
167 | because there are no frags with offset greater than it. | |
168 | So that's where we want to put the hole */ | |
e0d60137 | 169 | JFFS2_DBG_FRAGTREE2("adding hole frag (%p) on right of node at (%p)\n", holefrag, this); |
f97117d1 AB |
170 | rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right); |
171 | } else { | |
e0d60137 | 172 | JFFS2_DBG_FRAGTREE2("adding hole frag (%p) at root of tree\n", holefrag); |
f97117d1 AB |
173 | rb_link_node(&holefrag->rb, NULL, &list->rb_node); |
174 | } | |
175 | rb_insert_color(&holefrag->rb, list); | |
176 | this = holefrag; | |
177 | } | |
178 | if (this) { | |
179 | /* By definition, the 'this' node has no right-hand child, | |
180 | because there are no frags with offset greater than it. | |
181 | So that's where we want to put new fragment */ | |
e0d60137 | 182 | JFFS2_DBG_FRAGTREE2("adding new frag (%p) on right of node at (%p)\n", newfrag, this); |
f97117d1 AB |
183 | rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right); |
184 | } else { | |
e0d60137 | 185 | JFFS2_DBG_FRAGTREE2("adding new frag (%p) at root of tree\n", newfrag); |
f97117d1 AB |
186 | rb_link_node(&newfrag->rb, NULL, &list->rb_node); |
187 | } | |
188 | rb_insert_color(&newfrag->rb, list); | |
189 | return 0; | |
dae6227f | 190 | } |
dae6227f | 191 | |
e0d60137 AB |
192 | JFFS2_DBG_FRAGTREE2("dealing with frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n", |
193 | this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this); | |
dae6227f | 194 | |
f97117d1 AB |
195 | /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes, |
196 | * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs | |
dae6227f | 197 | */ |
f97117d1 AB |
198 | if (newfrag->ofs > this->ofs) { |
199 | /* This node isn't completely obsoleted. The start of it remains valid */ | |
200 | ||
201 | /* Mark the new node and the partially covered node REF_NORMAL -- let | |
202 | the GC take a look at them */ | |
203 | mark_ref_normal(newfrag->node->raw); | |
204 | if (this->node) | |
205 | mark_ref_normal(this->node->raw); | |
206 | ||
207 | if (this->ofs + this->size > newfrag->ofs + newfrag->size) { | |
208 | /* The new node splits 'this' frag into two */ | |
209 | struct jffs2_node_frag *newfrag2 = jffs2_alloc_node_frag(); | |
210 | if (!newfrag2) { | |
211 | jffs2_free_node_frag(newfrag); | |
212 | return -ENOMEM; | |
213 | } | |
f97117d1 | 214 | if (this->node) |
e0d60137 AB |
215 | JFFS2_DBG_FRAGTREE2("split old frag 0x%04x-0x%04x, phys 0x%08x\n", |
216 | this->ofs, this->ofs+this->size, ref_offset(this->node->raw)); | |
f97117d1 | 217 | else |
e0d60137 AB |
218 | JFFS2_DBG_FRAGTREE2("split old hole frag 0x%04x-0x%04x\n", |
219 | this->ofs, this->ofs+this->size, ref_offset(this->node->raw)); | |
f97117d1 AB |
220 | |
221 | /* New second frag pointing to this's node */ | |
222 | newfrag2->ofs = newfrag->ofs + newfrag->size; | |
223 | newfrag2->size = (this->ofs+this->size) - newfrag2->ofs; | |
224 | newfrag2->node = this->node; | |
225 | if (this->node) | |
226 | this->node->frags++; | |
227 | ||
228 | /* Adjust size of original 'this' */ | |
229 | this->size = newfrag->ofs - this->ofs; | |
230 | ||
231 | /* Now, we know there's no node with offset | |
232 | greater than this->ofs but smaller than | |
233 | newfrag2->ofs or newfrag->ofs, for obvious | |
234 | reasons. So we can do a tree insert from | |
235 | 'this' to insert newfrag, and a tree insert | |
236 | from newfrag to insert newfrag2. */ | |
237 | jffs2_fragtree_insert(newfrag, this); | |
238 | rb_insert_color(&newfrag->rb, list); | |
dae6227f | 239 | |
f97117d1 AB |
240 | jffs2_fragtree_insert(newfrag2, newfrag); |
241 | rb_insert_color(&newfrag2->rb, list); | |
dae6227f | 242 | |
f97117d1 | 243 | return 0; |
dae6227f | 244 | } |
f97117d1 AB |
245 | /* New node just reduces 'this' frag in size, doesn't split it */ |
246 | this->size = newfrag->ofs - this->ofs; | |
dae6227f | 247 | |
f97117d1 AB |
248 | /* Again, we know it lives down here in the tree */ |
249 | jffs2_fragtree_insert(newfrag, this); | |
250 | rb_insert_color(&newfrag->rb, list); | |
251 | } else { | |
252 | /* New frag starts at the same point as 'this' used to. Replace | |
253 | it in the tree without doing a delete and insertion */ | |
e0d60137 AB |
254 | JFFS2_DBG_FRAGTREE2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n", |
255 | newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size); | |
dae6227f | 256 | |
f97117d1 | 257 | rb_replace_node(&this->rb, &newfrag->rb, list); |
dae6227f | 258 | |
f97117d1 | 259 | if (newfrag->ofs + newfrag->size >= this->ofs+this->size) { |
e0d60137 | 260 | JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size); |
f97117d1 | 261 | jffs2_obsolete_node_frag(c, this); |
dae6227f | 262 | } else { |
f97117d1 AB |
263 | this->ofs += newfrag->size; |
264 | this->size -= newfrag->size; | |
265 | ||
266 | jffs2_fragtree_insert(this, newfrag); | |
267 | rb_insert_color(&this->rb, list); | |
268 | return 0; | |
dae6227f | 269 | } |
dae6227f | 270 | } |
f97117d1 AB |
271 | /* OK, now we have newfrag added in the correct place in the tree, but |
272 | frag_next(newfrag) may be a fragment which is overlapped by it | |
273 | */ | |
274 | while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) { | |
275 | /* 'this' frag is obsoleted completely. */ | |
e0d60137 AB |
276 | JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x) and removing from tree\n", |
277 | this, this->ofs, this->ofs+this->size); | |
f97117d1 AB |
278 | rb_erase(&this->rb, list); |
279 | jffs2_obsolete_node_frag(c, this); | |
dae6227f | 280 | } |
f97117d1 AB |
281 | /* Now we're pointing at the first frag which isn't totally obsoleted by |
282 | the new frag */ | |
dae6227f | 283 | |
f97117d1 AB |
284 | if (!this || newfrag->ofs + newfrag->size == this->ofs) { |
285 | return 0; | |
dae6227f | 286 | } |
f97117d1 AB |
287 | /* Still some overlap but we don't need to move it in the tree */ |
288 | this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size); | |
289 | this->ofs = newfrag->ofs + newfrag->size; | |
290 | ||
291 | /* And mark them REF_NORMAL so the GC takes a look at them */ | |
292 | if (this->node) | |
293 | mark_ref_normal(this->node->raw); | |
294 | mark_ref_normal(newfrag->node->raw); | |
dae6227f AB |
295 | |
296 | return 0; | |
297 | } | |
298 | ||
f97117d1 AB |
299 | /* Given an inode, probably with existing list of fragments, add the new node |
300 | * to the fragment list. | |
dae6227f | 301 | */ |
f97117d1 | 302 | int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn) |
dae6227f | 303 | { |
f97117d1 AB |
304 | int ret; |
305 | struct jffs2_node_frag *newfrag; | |
dae6227f | 306 | |
f97117d1 AB |
307 | if (unlikely(!fn->size)) |
308 | return 0; | |
dae6227f | 309 | |
f97117d1 AB |
310 | newfrag = jffs2_alloc_node_frag(); |
311 | if (unlikely(!newfrag)) | |
312 | return -ENOMEM; | |
1da177e4 | 313 | |
e0d60137 AB |
314 | JFFS2_DBG_FRAGTREE("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n", |
315 | fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag); | |
1da177e4 | 316 | |
f97117d1 AB |
317 | newfrag->ofs = fn->ofs; |
318 | newfrag->size = fn->size; | |
319 | newfrag->node = fn; | |
320 | newfrag->node->frags = 1; | |
321 | ||
322 | ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag); | |
323 | if (unlikely(ret)) | |
324 | return ret; | |
325 | ||
326 | /* If we now share a page with other nodes, mark either previous | |
327 | or next node REF_NORMAL, as appropriate. */ | |
328 | if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) { | |
329 | struct jffs2_node_frag *prev = frag_prev(newfrag); | |
330 | ||
331 | mark_ref_normal(fn->raw); | |
332 | /* If we don't start at zero there's _always_ a previous */ | |
333 | if (prev->node) | |
334 | mark_ref_normal(prev->node->raw); | |
335 | } | |
1da177e4 | 336 | |
f97117d1 AB |
337 | if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) { |
338 | struct jffs2_node_frag *next = frag_next(newfrag); | |
339 | ||
340 | if (next) { | |
341 | mark_ref_normal(fn->raw); | |
342 | if (next->node) | |
343 | mark_ref_normal(next->node->raw); | |
1da177e4 | 344 | } |
1da177e4 | 345 | } |
f97117d1 AB |
346 | jffs2_dbg_fragtree_paranoia_check_nolock(f); |
347 | jffs2_dbg_dump_fragtree_nolock(f); | |
1da177e4 | 348 | return 0; |
1da177e4 LT |
349 | } |
350 | ||
f97117d1 | 351 | |
1da177e4 LT |
352 | void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state) |
353 | { | |
354 | spin_lock(&c->inocache_lock); | |
355 | ic->state = state; | |
356 | wake_up(&c->inocache_wq); | |
357 | spin_unlock(&c->inocache_lock); | |
358 | } | |
359 | ||
360 | /* During mount, this needs no locking. During normal operation, its | |
361 | callers want to do other stuff while still holding the inocache_lock. | |
362 | Rather than introducing special case get_ino_cache functions or | |
363 | callbacks, we just let the caller do the locking itself. */ | |
364 | ||
365 | struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino) | |
366 | { | |
367 | struct jffs2_inode_cache *ret; | |
368 | ||
1da177e4 LT |
369 | ret = c->inocache_list[ino % INOCACHE_HASHSIZE]; |
370 | while (ret && ret->ino < ino) { | |
371 | ret = ret->next; | |
372 | } | |
373 | ||
374 | if (ret && ret->ino != ino) | |
375 | ret = NULL; | |
376 | ||
1da177e4 LT |
377 | return ret; |
378 | } | |
379 | ||
380 | void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new) | |
381 | { | |
382 | struct jffs2_inode_cache **prev; | |
7d27c814 | 383 | |
1da177e4 | 384 | spin_lock(&c->inocache_lock); |
7d27c814 TG |
385 | if (!new->ino) |
386 | new->ino = ++c->highest_ino; | |
387 | ||
e0d60137 | 388 | JFFS2_DBG_INOCACHE("add %p (ino #%u)\n", new, new->ino); |
7d27c814 | 389 | |
1da177e4 LT |
390 | prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE]; |
391 | ||
392 | while ((*prev) && (*prev)->ino < new->ino) { | |
393 | prev = &(*prev)->next; | |
394 | } | |
395 | new->next = *prev; | |
396 | *prev = new; | |
397 | ||
398 | spin_unlock(&c->inocache_lock); | |
399 | } | |
400 | ||
401 | void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old) | |
402 | { | |
403 | struct jffs2_inode_cache **prev; | |
e0d60137 AB |
404 | |
405 | JFFS2_DBG_INOCACHE("del %p (ino #%u)\n", old, old->ino); | |
1da177e4 LT |
406 | spin_lock(&c->inocache_lock); |
407 | ||
408 | prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE]; | |
409 | ||
410 | while ((*prev) && (*prev)->ino < old->ino) { | |
411 | prev = &(*prev)->next; | |
412 | } | |
413 | if ((*prev) == old) { | |
414 | *prev = old->next; | |
415 | } | |
416 | ||
67e345d1 DW |
417 | /* Free it now unless it's in READING or CLEARING state, which |
418 | are the transitions upon read_inode() and clear_inode(). The | |
419 | rest of the time we know nobody else is looking at it, and | |
420 | if it's held by read_inode() or clear_inode() they'll free it | |
421 | for themselves. */ | |
422 | if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING) | |
423 | jffs2_free_inode_cache(old); | |
424 | ||
1da177e4 LT |
425 | spin_unlock(&c->inocache_lock); |
426 | } | |
427 | ||
428 | void jffs2_free_ino_caches(struct jffs2_sb_info *c) | |
429 | { | |
430 | int i; | |
431 | struct jffs2_inode_cache *this, *next; | |
432 | ||
433 | for (i=0; i<INOCACHE_HASHSIZE; i++) { | |
434 | this = c->inocache_list[i]; | |
435 | while (this) { | |
436 | next = this->next; | |
1da177e4 LT |
437 | jffs2_free_inode_cache(this); |
438 | this = next; | |
439 | } | |
440 | c->inocache_list[i] = NULL; | |
441 | } | |
442 | } | |
443 | ||
444 | void jffs2_free_raw_node_refs(struct jffs2_sb_info *c) | |
445 | { | |
446 | int i; | |
447 | struct jffs2_raw_node_ref *this, *next; | |
448 | ||
449 | for (i=0; i<c->nr_blocks; i++) { | |
450 | this = c->blocks[i].first_node; | |
451 | while(this) { | |
452 | next = this->next_phys; | |
453 | jffs2_free_raw_node_ref(this); | |
454 | this = next; | |
455 | } | |
456 | c->blocks[i].first_node = c->blocks[i].last_node = NULL; | |
457 | } | |
458 | } | |
459 | ||
460 | struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset) | |
461 | { | |
462 | /* The common case in lookup is that there will be a node | |
463 | which precisely matches. So we go looking for that first */ | |
464 | struct rb_node *next; | |
465 | struct jffs2_node_frag *prev = NULL; | |
466 | struct jffs2_node_frag *frag = NULL; | |
467 | ||
e0d60137 | 468 | JFFS2_DBG_FRAGTREE2("root %p, offset %d\n", fragtree, offset); |
1da177e4 LT |
469 | |
470 | next = fragtree->rb_node; | |
471 | ||
472 | while(next) { | |
473 | frag = rb_entry(next, struct jffs2_node_frag, rb); | |
474 | ||
e0d60137 AB |
475 | JFFS2_DBG_FRAGTREE2("considering frag %#04x-%#04x (%p). left %p, right %p\n", |
476 | frag->ofs, frag->ofs+frag->size, frag, frag->rb.rb_left, frag->rb.rb_right); | |
1da177e4 | 477 | if (frag->ofs + frag->size <= offset) { |
e0d60137 AB |
478 | JFFS2_DBG_FRAGTREE2("going right from frag %#04x-%#04x, before the region we care about\n", |
479 | frag->ofs, frag->ofs+frag->size); | |
1da177e4 LT |
480 | /* Remember the closest smaller match on the way down */ |
481 | if (!prev || frag->ofs > prev->ofs) | |
482 | prev = frag; | |
483 | next = frag->rb.rb_right; | |
484 | } else if (frag->ofs > offset) { | |
e0d60137 AB |
485 | JFFS2_DBG_FRAGTREE2("going left from frag %#04x-%#04x, after the region we care about\n", |
486 | frag->ofs, frag->ofs+frag->size); | |
1da177e4 LT |
487 | next = frag->rb.rb_left; |
488 | } else { | |
e0d60137 AB |
489 | JFFS2_DBG_FRAGTREE2("returning frag %#04x-%#04x, matched\n", |
490 | frag->ofs, frag->ofs+frag->size); | |
1da177e4 LT |
491 | return frag; |
492 | } | |
493 | } | |
494 | ||
495 | /* Exact match not found. Go back up looking at each parent, | |
496 | and return the closest smaller one */ | |
497 | ||
498 | if (prev) | |
e0d60137 AB |
499 | JFFS2_DBG_FRAGTREE2("no match. Returning frag %#04x-%#04x, closest previous\n", |
500 | prev->ofs, prev->ofs+prev->size); | |
1da177e4 | 501 | else |
e0d60137 | 502 | JFFS2_DBG_FRAGTREE2("returning NULL, empty fragtree\n"); |
1da177e4 LT |
503 | |
504 | return prev; | |
505 | } | |
506 | ||
507 | /* Pass 'c' argument to indicate that nodes should be marked obsolete as | |
508 | they're killed. */ | |
509 | void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c) | |
510 | { | |
511 | struct jffs2_node_frag *frag; | |
512 | struct jffs2_node_frag *parent; | |
513 | ||
514 | if (!root->rb_node) | |
515 | return; | |
516 | ||
e0d60137 AB |
517 | JFFS2_DBG_FRAGTREE("killing\n"); |
518 | ||
1da177e4 | 519 | frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb)); |
1da177e4 LT |
520 | while(frag) { |
521 | if (frag->rb.rb_left) { | |
e0d60137 AB |
522 | JFFS2_DBG_FRAGTREE2("going left from frag (%p) %#04x-%#04x\n", |
523 | frag, frag->ofs, frag->ofs+frag->size); | |
1da177e4 LT |
524 | frag = frag_left(frag); |
525 | continue; | |
526 | } | |
527 | if (frag->rb.rb_right) { | |
e0d60137 AB |
528 | JFFS2_DBG_FRAGTREE2("going right from frag (%p) %#04x-%#04x\n", |
529 | frag, frag->ofs, frag->ofs+frag->size); | |
1da177e4 LT |
530 | frag = frag_right(frag); |
531 | continue; | |
532 | } | |
533 | ||
e0d60137 AB |
534 | JFFS2_DBG_FRAGTREE2("frag %#04x-%#04x: node %p, frags %d\n", |
535 | frag->ofs, frag->ofs+frag->size, frag->node, frag->node?frag->node->frags:0); | |
1da177e4 LT |
536 | |
537 | if (frag->node && !(--frag->node->frags)) { | |
538 | /* Not a hole, and it's the final remaining frag | |
539 | of this node. Free the node */ | |
540 | if (c) | |
541 | jffs2_mark_node_obsolete(c, frag->node->raw); | |
542 | ||
543 | jffs2_free_full_dnode(frag->node); | |
544 | } | |
545 | parent = frag_parent(frag); | |
546 | if (parent) { | |
547 | if (frag_left(parent) == frag) | |
548 | parent->rb.rb_left = NULL; | |
549 | else | |
550 | parent->rb.rb_right = NULL; | |
551 | } | |
552 | ||
553 | jffs2_free_node_frag(frag); | |
554 | frag = parent; | |
555 | ||
556 | cond_resched(); | |
557 | } | |
558 | } |