Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * JFFS2 -- Journalling Flash File System, Version 2.
0003  *
0004  * Copyright © 2001-2007 Red Hat, Inc.
0005  *
0006  * Created by David Woodhouse <dwmw2@infradead.org>
0007  *
0008  * For licensing information, see the file 'LICENCE' in this directory.
0009  *
0010  */
0011 
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/sched.h>
0016 #include <linux/fs.h>
0017 #include <linux/mtd/mtd.h>
0018 #include <linux/rbtree.h>
0019 #include <linux/crc32.h>
0020 #include <linux/pagemap.h>
0021 #include "nodelist.h"
0022 
0023 static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
0024                      struct jffs2_node_frag *this);
0025 
0026 void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
0027 {
0028     struct jffs2_full_dirent **prev = list;
0029 
0030     dbg_dentlist("add dirent \"%s\", ino #%u\n", new->name, new->ino);
0031 
0032     while ((*prev) && (*prev)->nhash <= new->nhash) {
0033         if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
0034             /* Duplicate. Free one */
0035             if (new->version < (*prev)->version) {
0036                 dbg_dentlist("Eep! Marking new dirent node obsolete, old is \"%s\", ino #%u\n",
0037                     (*prev)->name, (*prev)->ino);
0038                 jffs2_mark_node_obsolete(c, new->raw);
0039                 jffs2_free_full_dirent(new);
0040             } else {
0041                 dbg_dentlist("marking old dirent \"%s\", ino #%u obsolete\n",
0042                     (*prev)->name, (*prev)->ino);
0043                 new->next = (*prev)->next;
0044                 /* It may have been a 'placeholder' deletion dirent, 
0045                    if jffs2_can_mark_obsolete() (see jffs2_do_unlink()) */
0046                 if ((*prev)->raw)
0047                     jffs2_mark_node_obsolete(c, ((*prev)->raw));
0048                 jffs2_free_full_dirent(*prev);
0049                 *prev = new;
0050             }
0051             return;
0052         }
0053         prev = &((*prev)->next);
0054     }
0055     new->next = *prev;
0056     *prev = new;
0057 }
0058 
0059 uint32_t jffs2_truncate_fragtree(struct jffs2_sb_info *c, struct rb_root *list, uint32_t size)
0060 {
0061     struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size);
0062 
0063     dbg_fragtree("truncating fragtree to 0x%08x bytes\n", size);
0064 
0065     /* We know frag->ofs <= size. That's what lookup does for us */
0066     if (frag && frag->ofs != size) {
0067         if (frag->ofs+frag->size > size) {
0068             frag->size = size - frag->ofs;
0069         }
0070         frag = frag_next(frag);
0071     }
0072     while (frag && frag->ofs >= size) {
0073         struct jffs2_node_frag *next = frag_next(frag);
0074 
0075         frag_erase(frag, list);
0076         jffs2_obsolete_node_frag(c, frag);
0077         frag = next;
0078     }
0079 
0080     if (size == 0)
0081         return 0;
0082 
0083     frag = frag_last(list);
0084 
0085     /* Sanity check for truncation to longer than we started with... */
0086     if (!frag)
0087         return 0;
0088     if (frag->ofs + frag->size < size)
0089         return frag->ofs + frag->size;
0090 
0091     /* If the last fragment starts at the RAM page boundary, it is
0092      * REF_PRISTINE irrespective of its size. */
0093     if (frag->node && (frag->ofs & (PAGE_SIZE - 1)) == 0) {
0094         dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
0095             frag->ofs, frag->ofs + frag->size);
0096         frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
0097     }
0098     return size;
0099 }
0100 
0101 static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
0102                      struct jffs2_node_frag *this)
0103 {
0104     if (this->node) {
0105         this->node->frags--;
0106         if (!this->node->frags) {
0107             /* The node has no valid frags left. It's totally obsoleted */
0108             dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
0109                 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size);
0110             jffs2_mark_node_obsolete(c, this->node->raw);
0111             jffs2_free_full_dnode(this->node);
0112         } else {
0113             dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
0114                 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags);
0115             mark_ref_normal(this->node->raw);
0116         }
0117 
0118     }
0119     jffs2_free_node_frag(this);
0120 }
0121 
0122 static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
0123 {
0124     struct rb_node *parent = &base->rb;
0125     struct rb_node **link = &parent;
0126 
0127     dbg_fragtree2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
0128 
0129     while (*link) {
0130         parent = *link;
0131         base = rb_entry(parent, struct jffs2_node_frag, rb);
0132 
0133         if (newfrag->ofs > base->ofs)
0134             link = &base->rb.rb_right;
0135         else if (newfrag->ofs < base->ofs)
0136             link = &base->rb.rb_left;
0137         else {
0138             JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
0139             BUG();
0140         }
0141     }
0142 
0143     rb_link_node(&newfrag->rb, &base->rb, link);
0144 }
0145 
0146 /*
0147  * Allocate and initializes a new fragment.
0148  */
0149 static struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size)
0150 {
0151     struct jffs2_node_frag *newfrag;
0152 
0153     newfrag = jffs2_alloc_node_frag();
0154     if (likely(newfrag)) {
0155         newfrag->ofs = ofs;
0156         newfrag->size = size;
0157         newfrag->node = fn;
0158     } else {
0159         JFFS2_ERROR("cannot allocate a jffs2_node_frag object\n");
0160     }
0161 
0162     return newfrag;
0163 }
0164 
0165 /*
0166  * Called when there is no overlapping fragment exist. Inserts a hole before the new
0167  * fragment and inserts the new fragment to the fragtree.
0168  */
0169 static int no_overlapping_node(struct jffs2_sb_info *c, struct rb_root *root,
0170                    struct jffs2_node_frag *newfrag,
0171                    struct jffs2_node_frag *this, uint32_t lastend)
0172 {
0173     if (lastend < newfrag->node->ofs) {
0174         /* put a hole in before the new fragment */
0175         struct jffs2_node_frag *holefrag;
0176 
0177         holefrag= new_fragment(NULL, lastend, newfrag->node->ofs - lastend);
0178         if (unlikely(!holefrag)) {
0179             jffs2_free_node_frag(newfrag);
0180             return -ENOMEM;
0181         }
0182 
0183         if (this) {
0184             /* By definition, the 'this' node has no right-hand child,
0185                because there are no frags with offset greater than it.
0186                So that's where we want to put the hole */
0187             dbg_fragtree2("add hole frag %#04x-%#04x on the right of the new frag.\n",
0188                 holefrag->ofs, holefrag->ofs + holefrag->size);
0189             rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
0190         } else {
0191             dbg_fragtree2("Add hole frag %#04x-%#04x to the root of the tree.\n",
0192                 holefrag->ofs, holefrag->ofs + holefrag->size);
0193             rb_link_node(&holefrag->rb, NULL, &root->rb_node);
0194         }
0195         rb_insert_color(&holefrag->rb, root);
0196         this = holefrag;
0197     }
0198 
0199     if (this) {
0200         /* By definition, the 'this' node has no right-hand child,
0201            because there are no frags with offset greater than it.
0202            So that's where we want to put new fragment */
0203         dbg_fragtree2("add the new node at the right\n");
0204         rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
0205     } else {
0206         dbg_fragtree2("insert the new node at the root of the tree\n");
0207         rb_link_node(&newfrag->rb, NULL, &root->rb_node);
0208     }
0209     rb_insert_color(&newfrag->rb, root);
0210 
0211     return 0;
0212 }
0213 
0214 /* Doesn't set inode->i_size */
0215 static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *root, struct jffs2_node_frag *newfrag)
0216 {
0217     struct jffs2_node_frag *this;
0218     uint32_t lastend;
0219 
0220     /* Skip all the nodes which are completed before this one starts */
0221     this = jffs2_lookup_node_frag(root, newfrag->node->ofs);
0222 
0223     if (this) {
0224         dbg_fragtree2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
0225               this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
0226         lastend = this->ofs + this->size;
0227     } else {
0228         dbg_fragtree2("lookup gave no frag\n");
0229         lastend = 0;
0230     }
0231 
0232     /* See if we ran off the end of the fragtree */
0233     if (lastend <= newfrag->ofs) {
0234         /* We did */
0235 
0236         /* Check if 'this' node was on the same page as the new node.
0237            If so, both 'this' and the new node get marked REF_NORMAL so
0238            the GC can take a look.
0239         */
0240         if (lastend && (lastend-1) >> PAGE_SHIFT == newfrag->ofs >> PAGE_SHIFT) {
0241             if (this->node)
0242                 mark_ref_normal(this->node->raw);
0243             mark_ref_normal(newfrag->node->raw);
0244         }
0245 
0246         return no_overlapping_node(c, root, newfrag, this, lastend);
0247     }
0248 
0249     if (this->node)
0250         dbg_fragtree2("dealing with frag %u-%u, phys %#08x(%d).\n",
0251         this->ofs, this->ofs + this->size,
0252         ref_offset(this->node->raw), ref_flags(this->node->raw));
0253     else
0254         dbg_fragtree2("dealing with hole frag %u-%u.\n",
0255         this->ofs, this->ofs + this->size);
0256 
0257     /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
0258      * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
0259      */
0260     if (newfrag->ofs > this->ofs) {
0261         /* This node isn't completely obsoleted. The start of it remains valid */
0262 
0263         /* Mark the new node and the partially covered node REF_NORMAL -- let
0264            the GC take a look at them */
0265         mark_ref_normal(newfrag->node->raw);
0266         if (this->node)
0267             mark_ref_normal(this->node->raw);
0268 
0269         if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
0270             /* The new node splits 'this' frag into two */
0271             struct jffs2_node_frag *newfrag2;
0272 
0273             if (this->node)
0274                 dbg_fragtree2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
0275                     this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
0276             else
0277                 dbg_fragtree2("split old hole frag 0x%04x-0x%04x\n",
0278                     this->ofs, this->ofs+this->size);
0279 
0280             /* New second frag pointing to this's node */
0281             newfrag2 = new_fragment(this->node, newfrag->ofs + newfrag->size,
0282                         this->ofs + this->size - newfrag->ofs - newfrag->size);
0283             if (unlikely(!newfrag2))
0284                 return -ENOMEM;
0285             if (this->node)
0286                 this->node->frags++;
0287 
0288             /* Adjust size of original 'this' */
0289             this->size = newfrag->ofs - this->ofs;
0290 
0291             /* Now, we know there's no node with offset
0292                greater than this->ofs but smaller than
0293                newfrag2->ofs or newfrag->ofs, for obvious
0294                reasons. So we can do a tree insert from
0295                'this' to insert newfrag, and a tree insert
0296                from newfrag to insert newfrag2. */
0297             jffs2_fragtree_insert(newfrag, this);
0298             rb_insert_color(&newfrag->rb, root);
0299 
0300             jffs2_fragtree_insert(newfrag2, newfrag);
0301             rb_insert_color(&newfrag2->rb, root);
0302 
0303             return 0;
0304         }
0305         /* New node just reduces 'this' frag in size, doesn't split it */
0306         this->size = newfrag->ofs - this->ofs;
0307 
0308         /* Again, we know it lives down here in the tree */
0309         jffs2_fragtree_insert(newfrag, this);
0310         rb_insert_color(&newfrag->rb, root);
0311     } else {
0312         /* New frag starts at the same point as 'this' used to. Replace
0313            it in the tree without doing a delete and insertion */
0314         dbg_fragtree2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
0315               newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
0316 
0317         rb_replace_node(&this->rb, &newfrag->rb, root);
0318 
0319         if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
0320             dbg_fragtree2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
0321             jffs2_obsolete_node_frag(c, this);
0322         } else {
0323             this->ofs += newfrag->size;
0324             this->size -= newfrag->size;
0325 
0326             jffs2_fragtree_insert(this, newfrag);
0327             rb_insert_color(&this->rb, root);
0328             return 0;
0329         }
0330     }
0331     /* OK, now we have newfrag added in the correct place in the tree, but
0332        frag_next(newfrag) may be a fragment which is overlapped by it
0333     */
0334     while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
0335         /* 'this' frag is obsoleted completely. */
0336         dbg_fragtree2("obsoleting node frag %p (%x-%x) and removing from tree\n",
0337             this, this->ofs, this->ofs+this->size);
0338         rb_erase(&this->rb, root);
0339         jffs2_obsolete_node_frag(c, this);
0340     }
0341     /* Now we're pointing at the first frag which isn't totally obsoleted by
0342        the new frag */
0343 
0344     if (!this || newfrag->ofs + newfrag->size == this->ofs)
0345         return 0;
0346 
0347     /* Still some overlap but we don't need to move it in the tree */
0348     this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
0349     this->ofs = newfrag->ofs + newfrag->size;
0350 
0351     /* And mark them REF_NORMAL so the GC takes a look at them */
0352     if (this->node)
0353         mark_ref_normal(this->node->raw);
0354     mark_ref_normal(newfrag->node->raw);
0355 
0356     return 0;
0357 }
0358 
0359 /*
0360  * Given an inode, probably with existing tree of fragments, add the new node
0361  * to the fragment tree.
0362  */
0363 int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
0364 {
0365     int ret;
0366     struct jffs2_node_frag *newfrag;
0367 
0368     if (unlikely(!fn->size))
0369         return 0;
0370 
0371     newfrag = new_fragment(fn, fn->ofs, fn->size);
0372     if (unlikely(!newfrag))
0373         return -ENOMEM;
0374     newfrag->node->frags = 1;
0375 
0376     dbg_fragtree("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
0377           fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
0378 
0379     ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
0380     if (unlikely(ret))
0381         return ret;
0382 
0383     /* If we now share a page with other nodes, mark either previous
0384        or next node REF_NORMAL, as appropriate.  */
0385     if (newfrag->ofs & (PAGE_SIZE-1)) {
0386         struct jffs2_node_frag *prev = frag_prev(newfrag);
0387 
0388         mark_ref_normal(fn->raw);
0389         /* If we don't start at zero there's _always_ a previous */
0390         if (prev->node)
0391             mark_ref_normal(prev->node->raw);
0392     }
0393 
0394     if ((newfrag->ofs+newfrag->size) & (PAGE_SIZE-1)) {
0395         struct jffs2_node_frag *next = frag_next(newfrag);
0396 
0397         if (next) {
0398             mark_ref_normal(fn->raw);
0399             if (next->node)
0400                 mark_ref_normal(next->node->raw);
0401         }
0402     }
0403     jffs2_dbg_fragtree_paranoia_check_nolock(f);
0404 
0405     return 0;
0406 }
0407 
0408 void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
0409 {
0410     spin_lock(&c->inocache_lock);
0411     ic->state = state;
0412     wake_up(&c->inocache_wq);
0413     spin_unlock(&c->inocache_lock);
0414 }
0415 
0416 /* During mount, this needs no locking. During normal operation, its
0417    callers want to do other stuff while still holding the inocache_lock.
0418    Rather than introducing special case get_ino_cache functions or
0419    callbacks, we just let the caller do the locking itself. */
0420 
0421 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
0422 {
0423     struct jffs2_inode_cache *ret;
0424 
0425     ret = c->inocache_list[ino % c->inocache_hashsize];
0426     while (ret && ret->ino < ino) {
0427         ret = ret->next;
0428     }
0429 
0430     if (ret && ret->ino != ino)
0431         ret = NULL;
0432 
0433     return ret;
0434 }
0435 
0436 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
0437 {
0438     struct jffs2_inode_cache **prev;
0439 
0440     spin_lock(&c->inocache_lock);
0441     if (!new->ino)
0442         new->ino = ++c->highest_ino;
0443 
0444     dbg_inocache("add %p (ino #%u)\n", new, new->ino);
0445 
0446     prev = &c->inocache_list[new->ino % c->inocache_hashsize];
0447 
0448     while ((*prev) && (*prev)->ino < new->ino) {
0449         prev = &(*prev)->next;
0450     }
0451     new->next = *prev;
0452     *prev = new;
0453 
0454     spin_unlock(&c->inocache_lock);
0455 }
0456 
0457 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
0458 {
0459     struct jffs2_inode_cache **prev;
0460 
0461 #ifdef CONFIG_JFFS2_FS_XATTR
0462     BUG_ON(old->xref);
0463 #endif
0464     dbg_inocache("del %p (ino #%u)\n", old, old->ino);
0465     spin_lock(&c->inocache_lock);
0466 
0467     prev = &c->inocache_list[old->ino % c->inocache_hashsize];
0468 
0469     while ((*prev) && (*prev)->ino < old->ino) {
0470         prev = &(*prev)->next;
0471     }
0472     if ((*prev) == old) {
0473         *prev = old->next;
0474     }
0475 
0476     /* Free it now unless it's in READING or CLEARING state, which
0477        are the transitions upon read_inode() and clear_inode(). The
0478        rest of the time we know nobody else is looking at it, and
0479        if it's held by read_inode() or clear_inode() they'll free it
0480        for themselves. */
0481     if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
0482         jffs2_free_inode_cache(old);
0483 
0484     spin_unlock(&c->inocache_lock);
0485 }
0486 
0487 void jffs2_free_ino_caches(struct jffs2_sb_info *c)
0488 {
0489     int i;
0490     struct jffs2_inode_cache *this, *next;
0491 
0492     for (i=0; i < c->inocache_hashsize; i++) {
0493         this = c->inocache_list[i];
0494         while (this) {
0495             next = this->next;
0496             jffs2_xattr_free_inode(c, this);
0497             jffs2_free_inode_cache(this);
0498             this = next;
0499         }
0500         c->inocache_list[i] = NULL;
0501     }
0502 }
0503 
0504 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
0505 {
0506     int i;
0507     struct jffs2_raw_node_ref *this, *next;
0508 
0509     for (i=0; i<c->nr_blocks; i++) {
0510         this = c->blocks[i].first_node;
0511         while (this) {
0512             if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE)
0513                 next = this[REFS_PER_BLOCK].next_in_ino;
0514             else
0515                 next = NULL;
0516 
0517             jffs2_free_refblock(this);
0518             this = next;
0519         }
0520         c->blocks[i].first_node = c->blocks[i].last_node = NULL;
0521     }
0522 }
0523 
0524 struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
0525 {
0526     /* The common case in lookup is that there will be a node
0527        which precisely matches. So we go looking for that first */
0528     struct rb_node *next;
0529     struct jffs2_node_frag *prev = NULL;
0530     struct jffs2_node_frag *frag = NULL;
0531 
0532     dbg_fragtree2("root %p, offset %d\n", fragtree, offset);
0533 
0534     next = fragtree->rb_node;
0535 
0536     while(next) {
0537         frag = rb_entry(next, struct jffs2_node_frag, rb);
0538 
0539         if (frag->ofs + frag->size <= offset) {
0540             /* Remember the closest smaller match on the way down */
0541             if (!prev || frag->ofs > prev->ofs)
0542                 prev = frag;
0543             next = frag->rb.rb_right;
0544         } else if (frag->ofs > offset) {
0545             next = frag->rb.rb_left;
0546         } else {
0547             return frag;
0548         }
0549     }
0550 
0551     /* Exact match not found. Go back up looking at each parent,
0552        and return the closest smaller one */
0553 
0554     if (prev)
0555         dbg_fragtree2("no match. Returning frag %#04x-%#04x, closest previous\n",
0556               prev->ofs, prev->ofs+prev->size);
0557     else
0558         dbg_fragtree2("returning NULL, empty fragtree\n");
0559 
0560     return prev;
0561 }
0562 
0563 /* Pass 'c' argument to indicate that nodes should be marked obsolete as
0564    they're killed. */
0565 void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
0566 {
0567     struct jffs2_node_frag *frag, *next;
0568 
0569     dbg_fragtree("killing\n");
0570     rbtree_postorder_for_each_entry_safe(frag, next, root, rb) {
0571         if (frag->node && !(--frag->node->frags)) {
0572             /* Not a hole, and it's the final remaining frag
0573                of this node. Free the node */
0574             if (c)
0575                 jffs2_mark_node_obsolete(c, frag->node->raw);
0576 
0577             jffs2_free_full_dnode(frag->node);
0578         }
0579 
0580         jffs2_free_node_frag(frag);
0581         cond_resched();
0582     }
0583 }
0584 
0585 struct jffs2_raw_node_ref *jffs2_link_node_ref(struct jffs2_sb_info *c,
0586                            struct jffs2_eraseblock *jeb,
0587                            uint32_t ofs, uint32_t len,
0588                            struct jffs2_inode_cache *ic)
0589 {
0590     struct jffs2_raw_node_ref *ref;
0591 
0592     BUG_ON(!jeb->allocated_refs);
0593     jeb->allocated_refs--;
0594 
0595     ref = jeb->last_node;
0596 
0597     dbg_noderef("Last node at %p is (%08x,%p)\n", ref, ref->flash_offset,
0598             ref->next_in_ino);
0599 
0600     while (ref->flash_offset != REF_EMPTY_NODE) {
0601         if (ref->flash_offset == REF_LINK_NODE)
0602             ref = ref->next_in_ino;
0603         else
0604             ref++;
0605     }
0606 
0607     dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref, 
0608             ref->flash_offset, ofs, ref->next_in_ino, len);
0609 
0610     ref->flash_offset = ofs;
0611 
0612     if (!jeb->first_node) {
0613         jeb->first_node = ref;
0614         BUG_ON(ref_offset(ref) != jeb->offset);
0615     } else if (unlikely(ref_offset(ref) != jeb->offset + c->sector_size - jeb->free_size)) {
0616         uint32_t last_len = ref_totlen(c, jeb, jeb->last_node);
0617 
0618         JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
0619                 ref, ref_offset(ref), ref_offset(ref)+len,
0620                 ref_offset(jeb->last_node), 
0621                 ref_offset(jeb->last_node)+last_len);
0622         BUG();
0623     }
0624     jeb->last_node = ref;
0625 
0626     if (ic) {
0627         ref->next_in_ino = ic->nodes;
0628         ic->nodes = ref;
0629     } else {
0630         ref->next_in_ino = NULL;
0631     }
0632 
0633     switch(ref_flags(ref)) {
0634     case REF_UNCHECKED:
0635         c->unchecked_size += len;
0636         jeb->unchecked_size += len;
0637         break;
0638 
0639     case REF_NORMAL:
0640     case REF_PRISTINE:
0641         c->used_size += len;
0642         jeb->used_size += len;
0643         break;
0644 
0645     case REF_OBSOLETE:
0646         c->dirty_size += len;
0647         jeb->dirty_size += len;
0648         break;
0649     }
0650     c->free_size -= len;
0651     jeb->free_size -= len;
0652 
0653 #ifdef TEST_TOTLEN
0654     /* Set (and test) __totlen field... for now */
0655     ref->__totlen = len;
0656     ref_totlen(c, jeb, ref);
0657 #endif
0658     return ref;
0659 }
0660 
0661 /* No locking, no reservation of 'ref'. Do not use on a live file system */
0662 int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0663                uint32_t size)
0664 {
0665     if (!size)
0666         return 0;
0667     if (unlikely(size > jeb->free_size)) {
0668         pr_crit("Dirty space 0x%x larger then free_size 0x%x (wasted 0x%x)\n",
0669             size, jeb->free_size, jeb->wasted_size);
0670         BUG();
0671     }
0672     /* REF_EMPTY_NODE is !obsolete, so that works OK */
0673     if (jeb->last_node && ref_obsolete(jeb->last_node)) {
0674 #ifdef TEST_TOTLEN
0675         jeb->last_node->__totlen += size;
0676 #endif
0677         c->dirty_size += size;
0678         c->free_size -= size;
0679         jeb->dirty_size += size;
0680         jeb->free_size -= size;
0681     } else {
0682         uint32_t ofs = jeb->offset + c->sector_size - jeb->free_size;
0683         ofs |= REF_OBSOLETE;
0684 
0685         jffs2_link_node_ref(c, jeb, ofs, size, NULL);
0686     }
0687 
0688     return 0;
0689 }
0690 
0691 /* Calculate totlen from surrounding nodes or eraseblock */
0692 static inline uint32_t __ref_totlen(struct jffs2_sb_info *c,
0693                     struct jffs2_eraseblock *jeb,
0694                     struct jffs2_raw_node_ref *ref)
0695 {
0696     uint32_t ref_end;
0697     struct jffs2_raw_node_ref *next_ref = ref_next(ref);
0698 
0699     if (next_ref)
0700         ref_end = ref_offset(next_ref);
0701     else {
0702         if (!jeb)
0703             jeb = &c->blocks[ref->flash_offset / c->sector_size];
0704 
0705         /* Last node in block. Use free_space */
0706         if (unlikely(ref != jeb->last_node)) {
0707             pr_crit("ref %p @0x%08x is not jeb->last_node (%p @0x%08x)\n",
0708                 ref, ref_offset(ref), jeb->last_node,
0709                 jeb->last_node ?
0710                 ref_offset(jeb->last_node) : 0);
0711             BUG();
0712         }
0713         ref_end = jeb->offset + c->sector_size - jeb->free_size;
0714     }
0715     return ref_end - ref_offset(ref);
0716 }
0717 
0718 uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
0719                 struct jffs2_raw_node_ref *ref)
0720 {
0721     uint32_t ret;
0722 
0723     ret = __ref_totlen(c, jeb, ref);
0724 
0725 #ifdef TEST_TOTLEN
0726     if (unlikely(ret != ref->__totlen)) {
0727         if (!jeb)
0728             jeb = &c->blocks[ref->flash_offset / c->sector_size];
0729 
0730         pr_crit("Totlen for ref at %p (0x%08x-0x%08x) miscalculated as 0x%x instead of %x\n",
0731             ref, ref_offset(ref), ref_offset(ref) + ref->__totlen,
0732             ret, ref->__totlen);
0733         if (ref_next(ref)) {
0734             pr_crit("next %p (0x%08x-0x%08x)\n",
0735                 ref_next(ref), ref_offset(ref_next(ref)),
0736                 ref_offset(ref_next(ref)) + ref->__totlen);
0737         } else 
0738             pr_crit("No next ref. jeb->last_node is %p\n",
0739                 jeb->last_node);
0740 
0741         pr_crit("jeb->wasted_size %x, dirty_size %x, used_size %x, free_size %x\n",
0742             jeb->wasted_size, jeb->dirty_size, jeb->used_size,
0743             jeb->free_size);
0744 
0745 #if defined(JFFS2_DBG_DUMPS) || defined(JFFS2_DBG_PARANOIA_CHECKS)
0746         __jffs2_dbg_dump_node_refs_nolock(c, jeb);
0747 #endif
0748 
0749         WARN_ON(1);
0750 
0751         ret = ref->__totlen;
0752     }
0753 #endif /* TEST_TOTLEN */
0754     return ret;
0755 }