2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/rbtree.h>
17 #include <linux/crc32.h>
18 #include <linux/slab.h>
19 #include <linux/pagemap.h>
22 static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
23 struct jffs2_node_frag *this);
25 void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
27 struct jffs2_full_dirent **prev = list;
29 dbg_dentlist("add dirent \"%s\", ino #%u\n", new->name, new->ino);
31 while ((*prev) && (*prev)->nhash <= new->nhash) {
32 if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
33 /* Duplicate. Free one */
34 if (new->version < (*prev)->version) {
35 dbg_dentlist("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n",
36 (*prev)->name, (*prev)->ino);
37 jffs2_mark_node_obsolete(c, new->raw);
38 jffs2_free_full_dirent(new);
40 dbg_dentlist("marking old dirent \"%s\", ino #%u bsolete\n",
41 (*prev)->name, (*prev)->ino);
42 new->next = (*prev)->next;
43 jffs2_mark_node_obsolete(c, ((*prev)->raw));
44 jffs2_free_full_dirent(*prev);
49 prev = &((*prev)->next);
55 void jffs2_truncate_fragtree(struct jffs2_sb_info *c, struct rb_root *list, uint32_t size)
57 struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size);
59 dbg_fragtree("truncating fragtree to 0x%08x bytes\n", size);
61 /* We know frag->ofs <= size. That's what lookup does for us */
62 if (frag && frag->ofs != size) {
63 if (frag->ofs+frag->size > size) {
64 frag->size = size - frag->ofs;
66 frag = frag_next(frag);
68 while (frag && frag->ofs >= size) {
69 struct jffs2_node_frag *next = frag_next(frag);
71 frag_erase(frag, list);
72 jffs2_obsolete_node_frag(c, frag);
80 * If the last fragment starts at the RAM page boundary, it is
81 * REF_PRISTINE irrespective of its size.
83 frag = frag_last(list);
84 if (frag->node && (frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
85 dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
86 frag->ofs, frag->ofs + frag->size);
87 frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
91 static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
92 struct jffs2_node_frag *this)
96 if (!this->node->frags) {
97 /* The node has no valid frags left. It's totally obsoleted */
98 dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
99 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size);
100 jffs2_mark_node_obsolete(c, this->node->raw);
101 jffs2_free_full_dnode(this->node);
103 dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
104 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags);
105 mark_ref_normal(this->node->raw);
109 jffs2_free_node_frag(this);
112 static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
114 struct rb_node *parent = &base->rb;
115 struct rb_node **link = &parent;
117 dbg_fragtree2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
121 base = rb_entry(parent, struct jffs2_node_frag, rb);
123 if (newfrag->ofs > base->ofs)
124 link = &base->rb.rb_right;
125 else if (newfrag->ofs < base->ofs)
126 link = &base->rb.rb_left;
128 JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
133 rb_link_node(&newfrag->rb, &base->rb, link);
137 * Allocate and initializes a new fragment.
139 static struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size)
141 struct jffs2_node_frag *newfrag;
143 newfrag = jffs2_alloc_node_frag();
144 if (likely(newfrag)) {
146 newfrag->size = size;
149 JFFS2_ERROR("cannot allocate a jffs2_node_frag object\n");
156 * Called when there is no overlapping fragment exist. Inserts a hole before the new
157 * fragment and inserts the new fragment to the fragtree.
159 static int no_overlapping_node(struct jffs2_sb_info *c, struct rb_root *root,
160 struct jffs2_node_frag *newfrag,
161 struct jffs2_node_frag *this, uint32_t lastend)
163 if (lastend < newfrag->node->ofs) {
164 /* put a hole in before the new fragment */
165 struct jffs2_node_frag *holefrag;
167 holefrag= new_fragment(NULL, lastend, newfrag->node->ofs - lastend);
168 if (unlikely(!holefrag)) {
169 jffs2_free_node_frag(newfrag);
174 /* By definition, the 'this' node has no right-hand child,
175 because there are no frags with offset greater than it.
176 So that's where we want to put the hole */
177 dbg_fragtree2("add hole frag %#04x-%#04x on the right of the new frag.\n",
178 holefrag->ofs, holefrag->ofs + holefrag->size);
179 rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
181 dbg_fragtree2("Add hole frag %#04x-%#04x to the root of the tree.\n",
182 holefrag->ofs, holefrag->ofs + holefrag->size);
183 rb_link_node(&holefrag->rb, NULL, &root->rb_node);
185 rb_insert_color(&holefrag->rb, root);
190 /* By definition, the 'this' node has no right-hand child,
191 because there are no frags with offset greater than it.
192 So that's where we want to put new fragment */
193 dbg_fragtree2("add the new node at the right\n");
194 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
196 dbg_fragtree2("insert the new node at the root of the tree\n");
197 rb_link_node(&newfrag->rb, NULL, &root->rb_node);
199 rb_insert_color(&newfrag->rb, root);
204 /* Doesn't set inode->i_size */
205 static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *root, struct jffs2_node_frag *newfrag)
207 struct jffs2_node_frag *this;
210 /* Skip all the nodes which are completed before this one starts */
211 this = jffs2_lookup_node_frag(root, newfrag->node->ofs);
214 dbg_fragtree2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
215 this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
216 lastend = this->ofs + this->size;
218 dbg_fragtree2("lookup gave no frag\n");
222 /* See if we ran off the end of the fragtree */
223 if (lastend <= newfrag->ofs) {
226 /* Check if 'this' node was on the same page as the new node.
227 If so, both 'this' and the new node get marked REF_NORMAL so
228 the GC can take a look.
230 if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
232 mark_ref_normal(this->node->raw);
233 mark_ref_normal(newfrag->node->raw);
236 return no_overlapping_node(c, root, newfrag, this, lastend);
240 dbg_fragtree2("dealing with frag %u-%u, phys %#08x(%d).\n",
241 this->ofs, this->ofs + this->size,
242 ref_offset(this->node->raw), ref_flags(this->node->raw));
244 dbg_fragtree2("dealing with hole frag %u-%u.\n",
245 this->ofs, this->ofs + this->size);
247 /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
248 * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
250 if (newfrag->ofs > this->ofs) {
251 /* This node isn't completely obsoleted. The start of it remains valid */
253 /* Mark the new node and the partially covered node REF_NORMAL -- let
254 the GC take a look at them */
255 mark_ref_normal(newfrag->node->raw);
257 mark_ref_normal(this->node->raw);
259 if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
260 /* The new node splits 'this' frag into two */
261 struct jffs2_node_frag *newfrag2;
264 dbg_fragtree2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
265 this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
267 dbg_fragtree2("split old hole frag 0x%04x-0x%04x\n",
268 this->ofs, this->ofs+this->size);
270 /* New second frag pointing to this's node */
271 newfrag2 = new_fragment(this->node, newfrag->ofs + newfrag->size,
272 this->ofs + this->size - newfrag->ofs - newfrag->size);
273 if (unlikely(!newfrag2))
278 /* Adjust size of original 'this' */
279 this->size = newfrag->ofs - this->ofs;
281 /* Now, we know there's no node with offset
282 greater than this->ofs but smaller than
283 newfrag2->ofs or newfrag->ofs, for obvious
284 reasons. So we can do a tree insert from
285 'this' to insert newfrag, and a tree insert
286 from newfrag to insert newfrag2. */
287 jffs2_fragtree_insert(newfrag, this);
288 rb_insert_color(&newfrag->rb, root);
290 jffs2_fragtree_insert(newfrag2, newfrag);
291 rb_insert_color(&newfrag2->rb, root);
295 /* New node just reduces 'this' frag in size, doesn't split it */
296 this->size = newfrag->ofs - this->ofs;
298 /* Again, we know it lives down here in the tree */
299 jffs2_fragtree_insert(newfrag, this);
300 rb_insert_color(&newfrag->rb, root);
302 /* New frag starts at the same point as 'this' used to. Replace
303 it in the tree without doing a delete and insertion */
304 dbg_fragtree2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
305 newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
307 rb_replace_node(&this->rb, &newfrag->rb, root);
309 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
310 dbg_fragtree2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
311 jffs2_obsolete_node_frag(c, this);
313 this->ofs += newfrag->size;
314 this->size -= newfrag->size;
316 jffs2_fragtree_insert(this, newfrag);
317 rb_insert_color(&this->rb, root);
321 /* OK, now we have newfrag added in the correct place in the tree, but
322 frag_next(newfrag) may be a fragment which is overlapped by it
324 while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
325 /* 'this' frag is obsoleted completely. */
326 dbg_fragtree2("obsoleting node frag %p (%x-%x) and removing from tree\n",
327 this, this->ofs, this->ofs+this->size);
328 rb_erase(&this->rb, root);
329 jffs2_obsolete_node_frag(c, this);
331 /* Now we're pointing at the first frag which isn't totally obsoleted by
334 if (!this || newfrag->ofs + newfrag->size == this->ofs)
337 /* Still some overlap but we don't need to move it in the tree */
338 this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
339 this->ofs = newfrag->ofs + newfrag->size;
341 /* And mark them REF_NORMAL so the GC takes a look at them */
343 mark_ref_normal(this->node->raw);
344 mark_ref_normal(newfrag->node->raw);
350 * Given an inode, probably with existing tree of fragments, add the new node
351 * to the fragment tree.
353 int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
356 struct jffs2_node_frag *newfrag;
358 if (unlikely(!fn->size))
361 newfrag = new_fragment(fn, fn->ofs, fn->size);
362 if (unlikely(!newfrag))
364 newfrag->node->frags = 1;
366 dbg_fragtree("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
367 fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
369 ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
373 /* If we now share a page with other nodes, mark either previous
374 or next node REF_NORMAL, as appropriate. */
375 if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
376 struct jffs2_node_frag *prev = frag_prev(newfrag);
378 mark_ref_normal(fn->raw);
379 /* If we don't start at zero there's _always_ a previous */
381 mark_ref_normal(prev->node->raw);
384 if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
385 struct jffs2_node_frag *next = frag_next(newfrag);
388 mark_ref_normal(fn->raw);
390 mark_ref_normal(next->node->raw);
393 jffs2_dbg_fragtree_paranoia_check_nolock(f);
398 void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
400 spin_lock(&c->inocache_lock);
402 wake_up(&c->inocache_wq);
403 spin_unlock(&c->inocache_lock);
406 /* During mount, this needs no locking. During normal operation, its
407 callers want to do other stuff while still holding the inocache_lock.
408 Rather than introducing special case get_ino_cache functions or
409 callbacks, we just let the caller do the locking itself. */
411 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
413 struct jffs2_inode_cache *ret;
415 ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
416 while (ret && ret->ino < ino) {
420 if (ret && ret->ino != ino)
426 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
428 struct jffs2_inode_cache **prev;
430 spin_lock(&c->inocache_lock);
432 new->ino = ++c->highest_ino;
434 dbg_inocache("add %p (ino #%u)\n", new, new->ino);
436 prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
438 while ((*prev) && (*prev)->ino < new->ino) {
439 prev = &(*prev)->next;
444 spin_unlock(&c->inocache_lock);
447 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
449 struct jffs2_inode_cache **prev;
451 #ifdef CONFIG_JFFS2_FS_XATTR
454 dbg_inocache("del %p (ino #%u)\n", old, old->ino);
455 spin_lock(&c->inocache_lock);
457 prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
459 while ((*prev) && (*prev)->ino < old->ino) {
460 prev = &(*prev)->next;
462 if ((*prev) == old) {
466 /* Free it now unless it's in READING or CLEARING state, which
467 are the transitions upon read_inode() and clear_inode(). The
468 rest of the time we know nobody else is looking at it, and
469 if it's held by read_inode() or clear_inode() they'll free it
471 if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
472 jffs2_free_inode_cache(old);
474 spin_unlock(&c->inocache_lock);
477 void jffs2_free_ino_caches(struct jffs2_sb_info *c)
480 struct jffs2_inode_cache *this, *next;
482 for (i=0; i<INOCACHE_HASHSIZE; i++) {
483 this = c->inocache_list[i];
486 jffs2_xattr_free_inode(c, this);
487 jffs2_free_inode_cache(this);
490 c->inocache_list[i] = NULL;
494 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
497 struct jffs2_raw_node_ref *this, *next;
499 for (i=0; i<c->nr_blocks; i++) {
500 this = c->blocks[i].first_node;
502 if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE)
503 next = this[REFS_PER_BLOCK].next_in_ino;
507 jffs2_free_refblock(this);
510 c->blocks[i].first_node = c->blocks[i].last_node = NULL;
514 struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
516 /* The common case in lookup is that there will be a node
517 which precisely matches. So we go looking for that first */
518 struct rb_node *next;
519 struct jffs2_node_frag *prev = NULL;
520 struct jffs2_node_frag *frag = NULL;
522 dbg_fragtree2("root %p, offset %d\n", fragtree, offset);
524 next = fragtree->rb_node;
527 frag = rb_entry(next, struct jffs2_node_frag, rb);
529 if (frag->ofs + frag->size <= offset) {
530 /* Remember the closest smaller match on the way down */
531 if (!prev || frag->ofs > prev->ofs)
533 next = frag->rb.rb_right;
534 } else if (frag->ofs > offset) {
535 next = frag->rb.rb_left;
541 /* Exact match not found. Go back up looking at each parent,
542 and return the closest smaller one */
545 dbg_fragtree2("no match. Returning frag %#04x-%#04x, closest previous\n",
546 prev->ofs, prev->ofs+prev->size);
548 dbg_fragtree2("returning NULL, empty fragtree\n");
553 /* Pass 'c' argument to indicate that nodes should be marked obsolete as
555 void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
557 struct jffs2_node_frag *frag;
558 struct jffs2_node_frag *parent;
563 dbg_fragtree("killing\n");
565 frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
567 if (frag->rb.rb_left) {
568 frag = frag_left(frag);
571 if (frag->rb.rb_right) {
572 frag = frag_right(frag);
576 if (frag->node && !(--frag->node->frags)) {
577 /* Not a hole, and it's the final remaining frag
578 of this node. Free the node */
580 jffs2_mark_node_obsolete(c, frag->node->raw);
582 jffs2_free_full_dnode(frag->node);
584 parent = frag_parent(frag);
586 if (frag_left(parent) == frag)
587 parent->rb.rb_left = NULL;
589 parent->rb.rb_right = NULL;
592 jffs2_free_node_frag(frag);
599 struct jffs2_raw_node_ref *jffs2_link_node_ref(struct jffs2_sb_info *c,
600 struct jffs2_eraseblock *jeb,
601 uint32_t ofs, uint32_t len,
602 struct jffs2_inode_cache *ic)
604 struct jffs2_raw_node_ref *ref;
606 BUG_ON(!jeb->allocated_refs);
607 jeb->allocated_refs--;
609 ref = jeb->last_node;
611 dbg_noderef("Last node at %p is (%08x,%p)\n", ref, ref->flash_offset,
614 while (ref->flash_offset != REF_EMPTY_NODE) {
615 if (ref->flash_offset == REF_LINK_NODE)
616 ref = ref->next_in_ino;
621 dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
622 ref->flash_offset, ofs, ref->next_in_ino, len);
624 ref->flash_offset = ofs;
626 if (!jeb->first_node) {
627 jeb->first_node = ref;
628 BUG_ON(ref_offset(ref) != jeb->offset);
629 } else if (unlikely(ref_offset(ref) != jeb->offset + c->sector_size - jeb->free_size)) {
630 uint32_t last_len = ref_totlen(c, jeb, jeb->last_node);
632 JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
633 ref, ref_offset(ref), ref_offset(ref)+len,
634 ref_offset(jeb->last_node),
635 ref_offset(jeb->last_node)+last_len);
638 jeb->last_node = ref;
641 ref->next_in_ino = ic->nodes;
644 ref->next_in_ino = NULL;
647 switch(ref_flags(ref)) {
649 c->unchecked_size += len;
650 jeb->unchecked_size += len;
656 jeb->used_size += len;
660 c->dirty_size += len;
661 jeb->dirty_size += len;
665 jeb->free_size -= len;
668 /* Set (and test) __totlen field... for now */
670 ref_totlen(c, jeb, ref);
675 /* No locking, no reservation of 'ref'. Do not use on a live file system */
676 int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
681 if (unlikely(size > jeb->free_size)) {
682 printk(KERN_CRIT "Dirty space 0x%x larger then free_size 0x%x (wasted 0x%x)\n",
683 size, jeb->free_size, jeb->wasted_size);
686 /* REF_EMPTY_NODE is !obsolete, so that works OK */
687 if (jeb->last_node && ref_obsolete(jeb->last_node)) {
689 jeb->last_node->__totlen += size;
691 c->dirty_size += size;
692 c->free_size -= size;
693 jeb->dirty_size += size;
694 jeb->free_size -= size;
696 uint32_t ofs = jeb->offset + c->sector_size - jeb->free_size;
699 jffs2_link_node_ref(c, jeb, ofs, size, NULL);
705 /* Calculate totlen from surrounding nodes or eraseblock */
706 static inline uint32_t __ref_totlen(struct jffs2_sb_info *c,
707 struct jffs2_eraseblock *jeb,
708 struct jffs2_raw_node_ref *ref)
711 struct jffs2_raw_node_ref *next_ref = ref_next(ref);
714 ref_end = ref_offset(next_ref);
717 jeb = &c->blocks[ref->flash_offset / c->sector_size];
719 /* Last node in block. Use free_space */
720 if (unlikely(ref != jeb->last_node)) {
721 printk(KERN_CRIT "ref %p @0x%08x is not jeb->last_node (%p @0x%08x)\n",
722 ref, ref_offset(ref), jeb->last_node, jeb->last_node?ref_offset(jeb->last_node):0);
725 ref_end = jeb->offset + c->sector_size - jeb->free_size;
727 return ref_end - ref_offset(ref);
730 uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
731 struct jffs2_raw_node_ref *ref)
735 ret = __ref_totlen(c, jeb, ref);
738 if (unlikely(ret != ref->__totlen)) {
740 jeb = &c->blocks[ref->flash_offset / c->sector_size];
742 printk(KERN_CRIT "Totlen for ref at %p (0x%08x-0x%08x) miscalculated as 0x%x instead of %x\n",
743 ref, ref_offset(ref), ref_offset(ref)+ref->__totlen,
746 printk(KERN_CRIT "next %p (0x%08x-0x%08x)\n", ref_next(ref), ref_offset(ref_next(ref)),
747 ref_offset(ref_next(ref))+ref->__totlen);
749 printk(KERN_CRIT "No next ref. jeb->last_node is %p\n", jeb->last_node);
751 printk(KERN_CRIT "jeb->wasted_size %x, dirty_size %x, used_size %x, free_size %x\n", jeb->wasted_size, jeb->dirty_size, jeb->used_size, jeb->free_size);
753 #if defined(JFFS2_DBG_DUMPS) || defined(JFFS2_DBG_PARANOIA_CHECKS)
754 __jffs2_dbg_dump_node_refs_nolock(c, jeb);
761 #endif /* TEST_TOTLEN */