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.
10 * $Id: nodelist.c,v 1.101 2005/07/27 14:46:11 dedekind Exp $
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;
27 D1(printk(KERN_DEBUG "jffs2_add_fd_to_list( %p, %p (->%p))\n", new, list, *list));
29 while ((*prev) && (*prev)->nhash <= new->nhash) {
30 if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
31 /* Duplicate. Free one */
32 if (new->version < (*prev)->version) {
33 D1(printk(KERN_DEBUG "Eep! Marking new dirent node obsolete\n"));
34 D1(printk(KERN_DEBUG "New dirent is \"%s\"->ino #%u. Old is \"%s\"->ino #%u\n", new->name, new->ino, (*prev)->name, (*prev)->ino));
35 jffs2_mark_node_obsolete(c, new->raw);
36 jffs2_free_full_dirent(new);
38 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) obsolete\n", (*prev)->ino));
39 new->next = (*prev)->next;
40 jffs2_mark_node_obsolete(c, ((*prev)->raw));
41 jffs2_free_full_dirent(*prev);
46 prev = &((*prev)->next);
53 printk(KERN_DEBUG "Dirent \"%s\" (hash 0x%08x, ino #%u\n", (*list)->name, (*list)->nhash, (*list)->ino);
54 list = &(*list)->next;
58 void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this)
62 if (!this->node->frags) {
63 /* The node has no valid frags left. It's totally obsoleted */
64 D2(printk(KERN_DEBUG "Marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
65 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size));
66 jffs2_mark_node_obsolete(c, this->node->raw);
67 jffs2_free_full_dnode(this->node);
69 D2(printk(KERN_DEBUG "Marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
70 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size,
72 mark_ref_normal(this->node->raw);
76 jffs2_free_node_frag(this);
79 static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
81 struct rb_node *parent = &base->rb;
82 struct rb_node **link = &parent;
84 D2(printk(KERN_DEBUG "jffs2_fragtree_insert(%p; %d-%d, %p)\n", newfrag,
85 newfrag->ofs, newfrag->ofs+newfrag->size, base));
89 base = rb_entry(parent, struct jffs2_node_frag, rb);
91 D2(printk(KERN_DEBUG "fragtree_insert considering frag at 0x%x\n", base->ofs));
92 if (newfrag->ofs > base->ofs)
93 link = &base->rb.rb_right;
94 else if (newfrag->ofs < base->ofs)
95 link = &base->rb.rb_left;
97 printk(KERN_CRIT "Duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
102 rb_link_node(&newfrag->rb, &base->rb, link);
105 /* Doesn't set inode->i_size */
106 static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *list, struct jffs2_node_frag *newfrag)
108 struct jffs2_node_frag *this;
111 /* Skip all the nodes which are completed before this one starts */
112 this = jffs2_lookup_node_frag(list, newfrag->node->ofs);
115 D2(printk(KERN_DEBUG "j_a_f_d_t_f: Lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
116 this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this));
117 lastend = this->ofs + this->size;
119 D2(printk(KERN_DEBUG "j_a_f_d_t_f: Lookup gave no frag\n"));
123 /* See if we ran off the end of the list */
124 if (lastend <= newfrag->ofs) {
127 /* Check if 'this' node was on the same page as the new node.
128 If so, both 'this' and the new node get marked REF_NORMAL so
129 the GC can take a look.
131 if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
133 mark_ref_normal(this->node->raw);
134 mark_ref_normal(newfrag->node->raw);
137 if (lastend < newfrag->node->ofs) {
138 /* ... and we need to put a hole in before the new node */
139 struct jffs2_node_frag *holefrag = jffs2_alloc_node_frag();
141 jffs2_free_node_frag(newfrag);
144 holefrag->ofs = lastend;
145 holefrag->size = newfrag->node->ofs - lastend;
146 holefrag->node = NULL;
148 /* By definition, the 'this' node has no right-hand child,
149 because there are no frags with offset greater than it.
150 So that's where we want to put the hole */
151 D2(printk(KERN_DEBUG "Adding hole frag (%p) on right of node at (%p)\n", holefrag, this));
152 rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
154 D2(printk(KERN_DEBUG "Adding hole frag (%p) at root of tree\n", holefrag));
155 rb_link_node(&holefrag->rb, NULL, &list->rb_node);
157 rb_insert_color(&holefrag->rb, list);
161 /* By definition, the 'this' node has no right-hand child,
162 because there are no frags with offset greater than it.
163 So that's where we want to put new fragment */
164 D2(printk(KERN_DEBUG "Adding new frag (%p) on right of node at (%p)\n", newfrag, this));
165 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
167 D2(printk(KERN_DEBUG "Adding new frag (%p) at root of tree\n", newfrag));
168 rb_link_node(&newfrag->rb, NULL, &list->rb_node);
170 rb_insert_color(&newfrag->rb, list);
174 D2(printk(KERN_DEBUG "j_a_f_d_t_f: dealing with frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
175 this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this));
177 /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
178 * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
180 if (newfrag->ofs > this->ofs) {
181 /* This node isn't completely obsoleted. The start of it remains valid */
183 /* Mark the new node and the partially covered node REF_NORMAL -- let
184 the GC take a look at them */
185 mark_ref_normal(newfrag->node->raw);
187 mark_ref_normal(this->node->raw);
189 if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
190 /* The new node splits 'this' frag into two */
191 struct jffs2_node_frag *newfrag2 = jffs2_alloc_node_frag();
193 jffs2_free_node_frag(newfrag);
196 D2(printk(KERN_DEBUG "split old frag 0x%04x-0x%04x -->", this->ofs, this->ofs+this->size);
198 printk("phys 0x%08x\n", ref_offset(this->node->raw));
203 /* New second frag pointing to this's node */
204 newfrag2->ofs = newfrag->ofs + newfrag->size;
205 newfrag2->size = (this->ofs+this->size) - newfrag2->ofs;
206 newfrag2->node = this->node;
210 /* Adjust size of original 'this' */
211 this->size = newfrag->ofs - this->ofs;
213 /* Now, we know there's no node with offset
214 greater than this->ofs but smaller than
215 newfrag2->ofs or newfrag->ofs, for obvious
216 reasons. So we can do a tree insert from
217 'this' to insert newfrag, and a tree insert
218 from newfrag to insert newfrag2. */
219 jffs2_fragtree_insert(newfrag, this);
220 rb_insert_color(&newfrag->rb, list);
222 jffs2_fragtree_insert(newfrag2, newfrag);
223 rb_insert_color(&newfrag2->rb, list);
227 /* New node just reduces 'this' frag in size, doesn't split it */
228 this->size = newfrag->ofs - this->ofs;
230 /* Again, we know it lives down here in the tree */
231 jffs2_fragtree_insert(newfrag, this);
232 rb_insert_color(&newfrag->rb, list);
234 /* New frag starts at the same point as 'this' used to. Replace
235 it in the tree without doing a delete and insertion */
236 D2(printk(KERN_DEBUG "Inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
237 newfrag, newfrag->ofs, newfrag->ofs+newfrag->size,
238 this, this->ofs, this->ofs+this->size));
240 rb_replace_node(&this->rb, &newfrag->rb, list);
242 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
243 D2(printk(KERN_DEBUG "Obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size));
244 jffs2_obsolete_node_frag(c, this);
246 this->ofs += newfrag->size;
247 this->size -= newfrag->size;
249 jffs2_fragtree_insert(this, newfrag);
250 rb_insert_color(&this->rb, list);
254 /* OK, now we have newfrag added in the correct place in the tree, but
255 frag_next(newfrag) may be a fragment which is overlapped by it
257 while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
258 /* 'this' frag is obsoleted completely. */
259 D2(printk(KERN_DEBUG "Obsoleting node frag %p (%x-%x) and removing from tree\n", this, this->ofs, this->ofs+this->size));
260 rb_erase(&this->rb, list);
261 jffs2_obsolete_node_frag(c, this);
263 /* Now we're pointing at the first frag which isn't totally obsoleted by
266 if (!this || newfrag->ofs + newfrag->size == this->ofs) {
269 /* Still some overlap but we don't need to move it in the tree */
270 this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
271 this->ofs = newfrag->ofs + newfrag->size;
273 /* And mark them REF_NORMAL so the GC takes a look at them */
275 mark_ref_normal(this->node->raw);
276 mark_ref_normal(newfrag->node->raw);
281 /* Given an inode, probably with existing list of fragments, add the new node
282 * to the fragment list.
284 int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
287 struct jffs2_node_frag *newfrag;
289 D1(printk(KERN_DEBUG "jffs2_add_full_dnode_to_inode(ino #%u, f %p, fn %p)\n", f->inocache->ino, f, fn));
291 if (unlikely(!fn->size))
294 newfrag = jffs2_alloc_node_frag();
295 if (unlikely(!newfrag))
298 D2(printk(KERN_DEBUG "adding node %04x-%04x @0x%08x on flash, newfrag *%p\n",
299 fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag));
301 newfrag->ofs = fn->ofs;
302 newfrag->size = fn->size;
304 newfrag->node->frags = 1;
306 ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
310 /* If we now share a page with other nodes, mark either previous
311 or next node REF_NORMAL, as appropriate. */
312 if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
313 struct jffs2_node_frag *prev = frag_prev(newfrag);
315 mark_ref_normal(fn->raw);
316 /* If we don't start at zero there's _always_ a previous */
318 mark_ref_normal(prev->node->raw);
321 if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
322 struct jffs2_node_frag *next = frag_next(newfrag);
325 mark_ref_normal(fn->raw);
327 mark_ref_normal(next->node->raw);
330 jffs2_dbg_fragtree_paranoia_check_nolock(f);
331 jffs2_dbg_dump_fragtree_nolock(f);
336 void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
338 spin_lock(&c->inocache_lock);
340 wake_up(&c->inocache_wq);
341 spin_unlock(&c->inocache_lock);
344 /* During mount, this needs no locking. During normal operation, its
345 callers want to do other stuff while still holding the inocache_lock.
346 Rather than introducing special case get_ino_cache functions or
347 callbacks, we just let the caller do the locking itself. */
349 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
351 struct jffs2_inode_cache *ret;
353 D2(printk(KERN_DEBUG "jffs2_get_ino_cache(): ino %u\n", ino));
355 ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
356 while (ret && ret->ino < ino) {
360 if (ret && ret->ino != ino)
363 D2(printk(KERN_DEBUG "jffs2_get_ino_cache found %p for ino %u\n", ret, ino));
367 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
369 struct jffs2_inode_cache **prev;
371 spin_lock(&c->inocache_lock);
373 new->ino = ++c->highest_ino;
375 D2(printk(KERN_DEBUG "jffs2_add_ino_cache: Add %p (ino #%u)\n", new, new->ino));
377 prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
379 while ((*prev) && (*prev)->ino < new->ino) {
380 prev = &(*prev)->next;
385 spin_unlock(&c->inocache_lock);
388 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
390 struct jffs2_inode_cache **prev;
391 D1(printk(KERN_DEBUG "jffs2_del_ino_cache: Del %p (ino #%u)\n", old, old->ino));
392 spin_lock(&c->inocache_lock);
394 prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
396 while ((*prev) && (*prev)->ino < old->ino) {
397 prev = &(*prev)->next;
399 if ((*prev) == old) {
403 /* Free it now unless it's in READING or CLEARING state, which
404 are the transitions upon read_inode() and clear_inode(). The
405 rest of the time we know nobody else is looking at it, and
406 if it's held by read_inode() or clear_inode() they'll free it
408 if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
409 jffs2_free_inode_cache(old);
411 spin_unlock(&c->inocache_lock);
414 void jffs2_free_ino_caches(struct jffs2_sb_info *c)
417 struct jffs2_inode_cache *this, *next;
419 for (i=0; i<INOCACHE_HASHSIZE; i++) {
420 this = c->inocache_list[i];
423 jffs2_free_inode_cache(this);
426 c->inocache_list[i] = NULL;
430 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
433 struct jffs2_raw_node_ref *this, *next;
435 for (i=0; i<c->nr_blocks; i++) {
436 this = c->blocks[i].first_node;
438 next = this->next_phys;
439 jffs2_free_raw_node_ref(this);
442 c->blocks[i].first_node = c->blocks[i].last_node = NULL;
446 struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
448 /* The common case in lookup is that there will be a node
449 which precisely matches. So we go looking for that first */
450 struct rb_node *next;
451 struct jffs2_node_frag *prev = NULL;
452 struct jffs2_node_frag *frag = NULL;
454 D2(printk(KERN_DEBUG "jffs2_lookup_node_frag(%p, %d)\n", fragtree, offset));
456 next = fragtree->rb_node;
459 frag = rb_entry(next, struct jffs2_node_frag, rb);
461 D2(printk(KERN_DEBUG "Considering frag %d-%d (%p). left %p, right %p\n",
462 frag->ofs, frag->ofs+frag->size, frag, frag->rb.rb_left, frag->rb.rb_right));
463 if (frag->ofs + frag->size <= offset) {
464 D2(printk(KERN_DEBUG "Going right from frag %d-%d, before the region we care about\n",
465 frag->ofs, frag->ofs+frag->size));
466 /* Remember the closest smaller match on the way down */
467 if (!prev || frag->ofs > prev->ofs)
469 next = frag->rb.rb_right;
470 } else if (frag->ofs > offset) {
471 D2(printk(KERN_DEBUG "Going left from frag %d-%d, after the region we care about\n",
472 frag->ofs, frag->ofs+frag->size));
473 next = frag->rb.rb_left;
475 D2(printk(KERN_DEBUG "Returning frag %d,%d, matched\n",
476 frag->ofs, frag->ofs+frag->size));
481 /* Exact match not found. Go back up looking at each parent,
482 and return the closest smaller one */
485 D2(printk(KERN_DEBUG "No match. Returning frag %d,%d, closest previous\n",
486 prev->ofs, prev->ofs+prev->size));
488 D2(printk(KERN_DEBUG "Returning NULL, empty fragtree\n"));
493 /* Pass 'c' argument to indicate that nodes should be marked obsolete as
495 void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
497 struct jffs2_node_frag *frag;
498 struct jffs2_node_frag *parent;
503 frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
506 if (frag->rb.rb_left) {
507 D2(printk(KERN_DEBUG "Going left from frag (%p) %d-%d\n",
508 frag, frag->ofs, frag->ofs+frag->size));
509 frag = frag_left(frag);
512 if (frag->rb.rb_right) {
513 D2(printk(KERN_DEBUG "Going right from frag (%p) %d-%d\n",
514 frag, frag->ofs, frag->ofs+frag->size));
515 frag = frag_right(frag);
519 D2(printk(KERN_DEBUG "jffs2_kill_fragtree: frag at 0x%x-0x%x: node %p, frags %d--\n",
520 frag->ofs, frag->ofs+frag->size, frag->node,
521 frag->node?frag->node->frags:0));
523 if (frag->node && !(--frag->node->frags)) {
524 /* Not a hole, and it's the final remaining frag
525 of this node. Free the node */
527 jffs2_mark_node_obsolete(c, frag->node->raw);
529 jffs2_free_full_dnode(frag->node);
531 parent = frag_parent(frag);
533 if (frag_left(parent) == frag)
534 parent->rb.rb_left = NULL;
536 parent->rb.rb_right = NULL;
539 jffs2_free_node_frag(frag);