2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
14 #include <linux/kernel.h>
15 #include <linux/sched.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>
24 void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
26 struct jffs2_full_dirent **prev = list;
28 JFFS2_DBG_DENTLIST("add dirent \"%s\", ino #%u\n", new->name, new->ino);
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) {
34 JFFS2_DBG_DENTLIST("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n",
35 (*prev)->name, (*prev)->ino);
36 jffs2_mark_node_obsolete(c, new->raw);
37 jffs2_free_full_dirent(new);
39 JFFS2_DBG_DENTLIST("marking old dirent \"%s\", ino #%u bsolete\n",
40 (*prev)->name, (*prev)->ino);
41 new->next = (*prev)->next;
42 jffs2_mark_node_obsolete(c, ((*prev)->raw));
43 jffs2_free_full_dirent(*prev);
48 prev = &((*prev)->next);
54 void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this)
58 if (!this->node->frags) {
59 /* The node has no valid frags left. It's totally obsoleted */
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);
62 jffs2_mark_node_obsolete(c, this->node->raw);
63 jffs2_free_full_dnode(this->node);
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);
67 mark_ref_normal(this->node->raw);
71 jffs2_free_node_frag(this);
74 static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
76 struct rb_node *parent = &base->rb;
77 struct rb_node **link = &parent;
79 JFFS2_DBG_FRAGTREE2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
83 base = rb_entry(parent, struct jffs2_node_frag, rb);
85 JFFS2_DBG_FRAGTREE2("considering frag at 0x%08x\n", base->ofs);
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;
91 JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
96 rb_link_node(&newfrag->rb, &base->rb, link);
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)
102 struct jffs2_node_frag *this;
105 /* Skip all the nodes which are completed before this one starts */
106 this = jffs2_lookup_node_frag(list, newfrag->node->ofs);
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);
111 lastend = this->ofs + this->size;
113 JFFS2_DBG_FRAGTREE2("lookup gave no frag\n");
117 /* See if we ran off the end of the list */
118 if (lastend <= newfrag->ofs) {
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.
125 if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
127 mark_ref_normal(this->node->raw);
128 mark_ref_normal(newfrag->node->raw);
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();
135 jffs2_free_node_frag(newfrag);
138 holefrag->ofs = lastend;
139 holefrag->size = newfrag->node->ofs - lastend;
140 holefrag->node = NULL;
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 */
145 JFFS2_DBG_FRAGTREE2("adding hole frag (%p) on right of node at (%p)\n", holefrag, this);
146 rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
148 JFFS2_DBG_FRAGTREE2("adding hole frag (%p) at root of tree\n", holefrag);
149 rb_link_node(&holefrag->rb, NULL, &list->rb_node);
151 rb_insert_color(&holefrag->rb, list);
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 */
158 JFFS2_DBG_FRAGTREE2("adding new frag (%p) on right of node at (%p)\n", newfrag, this);
159 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
161 JFFS2_DBG_FRAGTREE2("adding new frag (%p) at root of tree\n", newfrag);
162 rb_link_node(&newfrag->rb, NULL, &list->rb_node);
164 rb_insert_color(&newfrag->rb, list);
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);
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
174 if (newfrag->ofs > this->ofs) {
175 /* This node isn't completely obsoleted. The start of it remains valid */
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);
181 mark_ref_normal(this->node->raw);
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();
187 jffs2_free_node_frag(newfrag);
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));
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));
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;
204 /* Adjust size of original 'this' */
205 this->size = newfrag->ofs - this->ofs;
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);
216 jffs2_fragtree_insert(newfrag2, newfrag);
217 rb_insert_color(&newfrag2->rb, list);
221 /* New node just reduces 'this' frag in size, doesn't split it */
222 this->size = newfrag->ofs - this->ofs;
224 /* Again, we know it lives down here in the tree */
225 jffs2_fragtree_insert(newfrag, this);
226 rb_insert_color(&newfrag->rb, list);
228 /* New frag starts at the same point as 'this' used to. Replace
229 it in the tree without doing a delete and insertion */
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);
233 rb_replace_node(&this->rb, &newfrag->rb, list);
235 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
236 JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
237 jffs2_obsolete_node_frag(c, this);
239 this->ofs += newfrag->size;
240 this->size -= newfrag->size;
242 jffs2_fragtree_insert(this, newfrag);
243 rb_insert_color(&this->rb, list);
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
250 while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
251 /* 'this' frag is obsoleted completely. */
252 JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x) and removing from tree\n",
253 this, this->ofs, this->ofs+this->size);
254 rb_erase(&this->rb, list);
255 jffs2_obsolete_node_frag(c, this);
257 /* Now we're pointing at the first frag which isn't totally obsoleted by
260 if (!this || newfrag->ofs + newfrag->size == this->ofs) {
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;
267 /* And mark them REF_NORMAL so the GC takes a look at them */
269 mark_ref_normal(this->node->raw);
270 mark_ref_normal(newfrag->node->raw);
275 /* Given an inode, probably with existing list of fragments, add the new node
276 * to the fragment list.
278 int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
281 struct jffs2_node_frag *newfrag;
283 if (unlikely(!fn->size))
286 newfrag = jffs2_alloc_node_frag();
287 if (unlikely(!newfrag))
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);
293 newfrag->ofs = fn->ofs;
294 newfrag->size = fn->size;
296 newfrag->node->frags = 1;
298 ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
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);
307 mark_ref_normal(fn->raw);
308 /* If we don't start at zero there's _always_ a previous */
310 mark_ref_normal(prev->node->raw);
313 if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
314 struct jffs2_node_frag *next = frag_next(newfrag);
317 mark_ref_normal(fn->raw);
319 mark_ref_normal(next->node->raw);
322 jffs2_dbg_fragtree_paranoia_check_nolock(f);
323 jffs2_dbg_dump_fragtree_nolock(f);
328 void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
330 spin_lock(&c->inocache_lock);
332 wake_up(&c->inocache_wq);
333 spin_unlock(&c->inocache_lock);
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. */
341 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
343 struct jffs2_inode_cache *ret;
345 ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
346 while (ret && ret->ino < ino) {
350 if (ret && ret->ino != ino)
356 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
358 struct jffs2_inode_cache **prev;
360 spin_lock(&c->inocache_lock);
362 new->ino = ++c->highest_ino;
364 JFFS2_DBG_INOCACHE("add %p (ino #%u)\n", new, new->ino);
366 prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
368 while ((*prev) && (*prev)->ino < new->ino) {
369 prev = &(*prev)->next;
374 spin_unlock(&c->inocache_lock);
377 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
379 struct jffs2_inode_cache **prev;
381 JFFS2_DBG_INOCACHE("del %p (ino #%u)\n", old, old->ino);
382 spin_lock(&c->inocache_lock);
384 prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
386 while ((*prev) && (*prev)->ino < old->ino) {
387 prev = &(*prev)->next;
389 if ((*prev) == old) {
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
398 if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
399 jffs2_free_inode_cache(old);
401 spin_unlock(&c->inocache_lock);
404 void jffs2_free_ino_caches(struct jffs2_sb_info *c)
407 struct jffs2_inode_cache *this, *next;
409 for (i=0; i<INOCACHE_HASHSIZE; i++) {
410 this = c->inocache_list[i];
413 jffs2_free_inode_cache(this);
416 c->inocache_list[i] = NULL;
420 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
423 struct jffs2_raw_node_ref *this, *next;
425 for (i=0; i<c->nr_blocks; i++) {
426 this = c->blocks[i].first_node;
428 next = this->next_phys;
429 jffs2_free_raw_node_ref(this);
432 c->blocks[i].first_node = c->blocks[i].last_node = NULL;
436 struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
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;
444 JFFS2_DBG_FRAGTREE2("root %p, offset %d\n", fragtree, offset);
446 next = fragtree->rb_node;
449 frag = rb_entry(next, struct jffs2_node_frag, rb);
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);
453 if (frag->ofs + frag->size <= offset) {
454 JFFS2_DBG_FRAGTREE2("going right from frag %#04x-%#04x, before the region we care about\n",
455 frag->ofs, frag->ofs+frag->size);
456 /* Remember the closest smaller match on the way down */
457 if (!prev || frag->ofs > prev->ofs)
459 next = frag->rb.rb_right;
460 } else if (frag->ofs > offset) {
461 JFFS2_DBG_FRAGTREE2("going left from frag %#04x-%#04x, after the region we care about\n",
462 frag->ofs, frag->ofs+frag->size);
463 next = frag->rb.rb_left;
465 JFFS2_DBG_FRAGTREE2("returning frag %#04x-%#04x, matched\n",
466 frag->ofs, frag->ofs+frag->size);
471 /* Exact match not found. Go back up looking at each parent,
472 and return the closest smaller one */
475 JFFS2_DBG_FRAGTREE2("no match. Returning frag %#04x-%#04x, closest previous\n",
476 prev->ofs, prev->ofs+prev->size);
478 JFFS2_DBG_FRAGTREE2("returning NULL, empty fragtree\n");
483 /* Pass 'c' argument to indicate that nodes should be marked obsolete as
485 void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
487 struct jffs2_node_frag *frag;
488 struct jffs2_node_frag *parent;
493 JFFS2_DBG_FRAGTREE("killing\n");
495 frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
497 if (frag->rb.rb_left) {
498 JFFS2_DBG_FRAGTREE2("going left from frag (%p) %#04x-%#04x\n",
499 frag, frag->ofs, frag->ofs+frag->size);
500 frag = frag_left(frag);
503 if (frag->rb.rb_right) {
504 JFFS2_DBG_FRAGTREE2("going right from frag (%p) %#04x-%#04x\n",
505 frag, frag->ofs, frag->ofs+frag->size);
506 frag = frag_right(frag);
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);
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 */
517 jffs2_mark_node_obsolete(c, frag->node->raw);
519 jffs2_free_full_dnode(frag->node);
521 parent = frag_parent(frag);
523 if (frag_left(parent) == frag)
524 parent->rb.rb_left = NULL;
526 parent->rb.rb_right = NULL;
529 jffs2_free_node_frag(frag);