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