blob: 0cf5e6f1198915c9a89a1bbb0e8b6752a70735da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Artem B. Bityutskiy1e900972005-07-31 09:20:48 +010010 * $Id: nodelist.c,v 1.103 2005/07/31 08:20:44 dedekind Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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
24void 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;
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010027
28 JFFS2_DBG_DENTLIST("add dirent \"%s\", ino #%u\n", new->name, new->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
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) {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010034 JFFS2_DBG_DENTLIST("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n",
35 (*prev)->name, (*prev)->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 jffs2_mark_node_obsolete(c, new->raw);
37 jffs2_free_full_dirent(new);
38 } else {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010039 JFFS2_DBG_DENTLIST("marking old dirent \"%s\", ino #%u bsolete\n",
40 (*prev)->name, (*prev)->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 new->next = (*prev)->next;
42 jffs2_mark_node_obsolete(c, ((*prev)->raw));
43 jffs2_free_full_dirent(*prev);
44 *prev = new;
45 }
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010046 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48 prev = &((*prev)->next);
49 }
50 new->next = *prev;
51 *prev = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Artem B. Bityutskiy1e900972005-07-31 09:20:48 +010054void 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
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +010078void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +010080 if (this->node) {
81 this->node->frags--;
82 if (!this->node->frags) {
83 /* The node has no valid frags left. It's totally obsoleted */
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010084 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);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +010086 jffs2_mark_node_obsolete(c, this->node->raw);
87 jffs2_free_full_dnode(this->node);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +010088 } else {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +010089 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);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +010091 mark_ref_normal(this->node->raw);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +010092 }
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +010093
Artem B. Bityutskiydae62272005-07-15 11:13:57 +010094 }
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +010095 jffs2_free_node_frag(this);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +010096}
97
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +010098static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
Artem B. Bityutskiydae62272005-07-15 11:13:57 +010099{
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100100 struct rb_node *parent = &base->rb;
101 struct rb_node **link = &parent;
102
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100103 JFFS2_DBG_FRAGTREE2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100104
105 while (*link) {
106 parent = *link;
107 base = rb_entry(parent, struct jffs2_node_frag, rb);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100108
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100109 JFFS2_DBG_FRAGTREE2("considering frag at 0x%08x\n", base->ofs);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100110 if (newfrag->ofs > base->ofs)
111 link = &base->rb.rb_right;
112 else if (newfrag->ofs < base->ofs)
113 link = &base->rb.rb_left;
114 else {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100115 JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100116 BUG();
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100117 }
118 }
119
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100120 rb_link_node(&newfrag->rb, &base->rb, link);
Artem B. Bityutskiydae62272005-07-15 11:13:57 +0100121}
122
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100123/* Doesn't set inode->i_size */
124static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *list, struct jffs2_node_frag *newfrag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100126 struct jffs2_node_frag *this;
127 uint32_t lastend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100129 /* Skip all the nodes which are completed before this one starts */
130 this = jffs2_lookup_node_frag(list, newfrag->node->ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100132 if (this) {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100133 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);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100135 lastend = this->ofs + this->size;
136 } else {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100137 JFFS2_DBG_FRAGTREE2("lookup gave no frag\n");
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100138 lastend = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100140
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 }
154
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 */
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100169 JFFS2_DBG_FRAGTREE2("adding hole frag (%p) on right of node at (%p)\n", holefrag, this);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100170 rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
171 } else {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100172 JFFS2_DBG_FRAGTREE2("adding hole frag (%p) at root of tree\n", holefrag);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100173 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 */
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100182 JFFS2_DBG_FRAGTREE2("adding new frag (%p) on right of node at (%p)\n", newfrag, this);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100183 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
184 } else {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100185 JFFS2_DBG_FRAGTREE2("adding new frag (%p) at root of tree\n", newfrag);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100186 rb_link_node(&newfrag->rb, NULL, &list->rb_node);
187 }
188 rb_insert_color(&newfrag->rb, list);
189 return 0;
190 }
191
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100192 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);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100194
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
197 */
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 }
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100214 if (this->node)
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100215 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));
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100217 else
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100218 JFFS2_DBG_FRAGTREE2("split old hole frag 0x%04x-0x%04x\n",
219 this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100220
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);
239
240 jffs2_fragtree_insert(newfrag2, newfrag);
241 rb_insert_color(&newfrag2->rb, list);
242
243 return 0;
244 }
245 /* New node just reduces 'this' frag in size, doesn't split it */
246 this->size = newfrag->ofs - this->ofs;
247
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 */
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100254 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);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100256
257 rb_replace_node(&this->rb, &newfrag->rb, list);
258
259 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100260 JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100261 jffs2_obsolete_node_frag(c, this);
262 } else {
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;
269 }
270 }
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. */
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100276 JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x) and removing from tree\n",
277 this, this->ofs, this->ofs+this->size);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100278 rb_erase(&this->rb, list);
279 jffs2_obsolete_node_frag(c, this);
280 }
281 /* Now we're pointing at the first frag which isn't totally obsoleted by
282 the new frag */
283
284 if (!this || newfrag->ofs + newfrag->size == this->ofs) {
285 return 0;
286 }
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100299/* Given an inode, probably with existing list of fragments, add the new node
300 * to the fragment list.
301 */
302int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
303{
304 int ret;
305 struct jffs2_node_frag *newfrag;
306
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100307 if (unlikely(!fn->size))
308 return 0;
309
310 newfrag = jffs2_alloc_node_frag();
311 if (unlikely(!newfrag))
312 return -ENOMEM;
313
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100314 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);
Artem B. Bityutskiyf97117d2005-07-27 15:46:14 +0100316
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 }
336
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);
344 }
345 }
346 jffs2_dbg_fragtree_paranoia_check_nolock(f);
347 jffs2_dbg_dump_fragtree_nolock(f);
348 return 0;
349}
350
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352void 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
365struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
366{
367 struct jffs2_inode_cache *ret;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return ret;
378}
379
380void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
381{
382 struct jffs2_inode_cache **prev;
Thomas Gleixner7d27c812005-05-22 21:47:19 +0200383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 spin_lock(&c->inocache_lock);
Thomas Gleixner7d27c812005-05-22 21:47:19 +0200385 if (!new->ino)
386 new->ino = ++c->highest_ino;
387
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100388 JFFS2_DBG_INOCACHE("add %p (ino #%u)\n", new, new->ino);
Thomas Gleixner7d27c812005-05-22 21:47:19 +0200389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 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
401void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
402{
403 struct jffs2_inode_cache **prev;
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100404
405 JFFS2_DBG_INOCACHE("del %p (ino #%u)\n", old, old->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 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
David Woodhouse67e345d2005-02-27 23:01:36 +0000417 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_unlock(&c->inocache_lock);
426}
427
428void 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 jffs2_free_inode_cache(this);
438 this = next;
439 }
440 c->inocache_list[i] = NULL;
441 }
442}
443
444void 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
460struct 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
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100468 JFFS2_DBG_FRAGTREE2("root %p, offset %d\n", fragtree, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 next = fragtree->rb_node;
471
472 while(next) {
473 frag = rb_entry(next, struct jffs2_node_frag, rb);
474
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100475 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (frag->ofs + frag->size <= offset) {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100478 JFFS2_DBG_FRAGTREE2("going right from frag %#04x-%#04x, before the region we care about\n",
479 frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* 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) {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100485 JFFS2_DBG_FRAGTREE2("going left from frag %#04x-%#04x, after the region we care about\n",
486 frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 next = frag->rb.rb_left;
488 } else {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100489 JFFS2_DBG_FRAGTREE2("returning frag %#04x-%#04x, matched\n",
490 frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 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)
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100499 JFFS2_DBG_FRAGTREE2("no match. Returning frag %#04x-%#04x, closest previous\n",
500 prev->ofs, prev->ofs+prev->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 else
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100502 JFFS2_DBG_FRAGTREE2("returning NULL, empty fragtree\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 return prev;
505}
506
507/* Pass 'c' argument to indicate that nodes should be marked obsolete as
508 they're killed. */
509void 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
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100517 JFFS2_DBG_FRAGTREE("killing\n");
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 while(frag) {
521 if (frag->rb.rb_left) {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100522 JFFS2_DBG_FRAGTREE2("going left from frag (%p) %#04x-%#04x\n",
523 frag, frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 frag = frag_left(frag);
525 continue;
526 }
527 if (frag->rb.rb_right) {
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100528 JFFS2_DBG_FRAGTREE2("going right from frag (%p) %#04x-%#04x\n",
529 frag, frag->ofs, frag->ofs+frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 frag = frag_right(frag);
531 continue;
532 }
533
Artem B. Bityutskiye0d60132005-07-28 15:46:43 +0100534 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
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}