[JFFS2] Build fragtree in reverse order
[safe/jmp/linux-2.6] / fs / jffs2 / nodelist.c
1 /*
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  *
10  * $Id: nodelist.c,v 1.104 2005/08/01 12:05:19 dedekind Exp $
11  *
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
24 void 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;
27         
28         JFFS2_DBG_DENTLIST("add dirent \"%s\", ino #%u\n", new->name, new->ino);
29
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);
38                         } else {
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);
44                                 *prev = new;
45                         }
46                         return;
47                 }
48                 prev = &((*prev)->next);
49         }
50         new->next = *prev;
51         *prev = new;
52 }
53
54 void 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         if (size == 0)
78                 return;
79
80         /* 
81          * If the last fragment starts at the RAM page boundary, it is
82          * REF_PRISTINE irrespective of its size.
83          */
84         frag = frag_last(list);
85         if ((frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
86                 JFFS2_DBG_FRAGTREE2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
87                         frag->ofs, frag->ofs + frag->size); 
88                 frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
89         }
90 }
91
92 void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this)
93 {
94         if (this->node) {
95                 this->node->frags--;
96                 if (!this->node->frags) {
97                         /* The node has no valid frags left. It's totally obsoleted */
98                         JFFS2_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);
102                 } else {
103                         JFFS2_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);
106                 }
107                 
108         }
109         jffs2_free_node_frag(this);
110 }
111
112 static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
113 {
114         struct rb_node *parent = &base->rb;
115         struct rb_node **link = &parent;
116
117         JFFS2_DBG_FRAGTREE2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
118
119         while (*link) {
120                 parent = *link;
121                 base = rb_entry(parent, struct jffs2_node_frag, rb);
122         
123                 JFFS2_DBG_FRAGTREE2("considering frag at 0x%08x\n", base->ofs);
124                 if (newfrag->ofs > base->ofs)
125                         link = &base->rb.rb_right;
126                 else if (newfrag->ofs < base->ofs)
127                         link = &base->rb.rb_left;
128                 else {
129                         JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
130                         BUG();
131                 }
132         }
133
134         rb_link_node(&newfrag->rb, &base->rb, link);
135 }
136
137 /*
138  * Allocate and initializes a new fragment.
139  */
140 static inline struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size)
141 {
142         struct jffs2_node_frag *newfrag;
143         
144         newfrag = jffs2_alloc_node_frag();
145         if (likely(newfrag)) {
146                 newfrag->ofs = ofs;
147                 newfrag->size = size;
148                 newfrag->node = fn;
149         } else {
150                 JFFS2_ERROR("cannot allocate a jffs2_node_frag object\n");
151         }
152
153         return newfrag;
154 }
155
156 /*
157  * Called when there is no overlapping fragment exist. Inserts a hole before the new
158  * fragment and inserts the new fragment to the fragtree.
159  */
160 static int no_overlapping_node(struct jffs2_sb_info *c, struct rb_root *root,
161                                struct jffs2_node_frag *newfrag,
162                                struct jffs2_node_frag *this, uint32_t lastend)
163 {
164         if (lastend < newfrag->node->ofs) {
165                 /* put a hole in before the new fragment */
166                 struct jffs2_node_frag *holefrag;
167
168                 holefrag= new_fragment(NULL, lastend, newfrag->node->ofs - lastend);
169                 if (unlikely(!holefrag)) {
170                         jffs2_free_node_frag(newfrag);
171                         return -ENOMEM;
172                 }
173
174                 if (this) {
175                         /* By definition, the 'this' node has no right-hand child, 
176                            because there are no frags with offset greater than it.
177                            So that's where we want to put the hole */
178                         JFFS2_DBG_FRAGTREE2("add hole frag %u-%u on the right of the new frag.\n",
179                                 holefrag->ofs, holefrag->ofs + holefrag->size);
180                         rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
181                 } else {
182                         JFFS2_DBG_FRAGTREE2("Add hole frag %u-%u to the root of the tree.\n",
183                                 holefrag->ofs, holefrag->ofs + holefrag->size);
184                         rb_link_node(&holefrag->rb, NULL, &root->rb_node);
185                 }
186                 rb_insert_color(&holefrag->rb, root);
187                 this = holefrag;
188         }
189         
190         if (this) {
191                 /* By definition, the 'this' node has no right-hand child, 
192                    because there are no frags with offset greater than it.
193                    So that's where we want to put new fragment */
194                 JFFS2_DBG_FRAGTREE2("add the new node at the right\n");
195                 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);                      
196         } else {
197                 JFFS2_DBG_FRAGTREE2("insert the new node at the root of the tree\n");
198                 rb_link_node(&newfrag->rb, NULL, &root->rb_node);
199         }
200         rb_insert_color(&newfrag->rb, root);
201
202         return 0;
203 }
204
205 /* Doesn't set inode->i_size */
206 static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *root, struct jffs2_node_frag *newfrag)
207 {
208         struct jffs2_node_frag *this;
209         uint32_t lastend;
210
211         /* Skip all the nodes which are completed before this one starts */
212         this = jffs2_lookup_node_frag(root, newfrag->node->ofs);
213
214         if (this) {
215                 JFFS2_DBG_FRAGTREE2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
216                           this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
217                 lastend = this->ofs + this->size;
218         } else {
219                 JFFS2_DBG_FRAGTREE2("lookup gave no frag\n");
220                 lastend = 0;
221         }
222                           
223         /* See if we ran off the end of the fragtree */
224         if (lastend <= newfrag->ofs) {
225                 /* We did */
226
227                 /* Check if 'this' node was on the same page as the new node.
228                    If so, both 'this' and the new node get marked REF_NORMAL so
229                    the GC can take a look.
230                 */
231                 if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
232                         if (this->node)
233                                 mark_ref_normal(this->node->raw);
234                         mark_ref_normal(newfrag->node->raw);
235                 }
236
237                 return no_overlapping_node(c, root, newfrag, this, lastend);
238         }
239
240         if (this->node)
241                 JFFS2_DBG_FRAGTREE2("dealing with frag %u-%u, phys %#08x(%d).\n",
242                 this->ofs, this->ofs + this->size,
243                 ref_offset(this->node->raw), ref_flags(this->node->raw));
244         else
245                 JFFS2_DBG_FRAGTREE2("dealing with hole frag %u-%u.\n",
246                 this->ofs, this->ofs + this->size);
247
248         /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
249          * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs  
250          */
251         if (newfrag->ofs > this->ofs) {
252                 /* This node isn't completely obsoleted. The start of it remains valid */
253
254                 /* Mark the new node and the partially covered node REF_NORMAL -- let
255                    the GC take a look at them */
256                 mark_ref_normal(newfrag->node->raw);
257                 if (this->node)
258                         mark_ref_normal(this->node->raw);
259
260                 if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
261                         /* The new node splits 'this' frag into two */
262                         struct jffs2_node_frag *newfrag2;
263
264                         if (this->node)
265                                 JFFS2_DBG_FRAGTREE2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
266                                         this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
267                         else 
268                                 JFFS2_DBG_FRAGTREE2("split old hole frag 0x%04x-0x%04x\n",
269                                         this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
270                         
271                         /* New second frag pointing to this's node */
272                         newfrag2 = new_fragment(this->node, newfrag->ofs + newfrag->size,
273                                                 this->ofs + this->size - newfrag->ofs - newfrag->size);
274                         if (unlikely(!newfrag2))
275                                 return -ENOMEM;
276                         if (this->node)
277                                 this->node->frags++;
278
279                         /* Adjust size of original 'this' */
280                         this->size = newfrag->ofs - this->ofs;
281
282                         /* Now, we know there's no node with offset
283                            greater than this->ofs but smaller than
284                            newfrag2->ofs or newfrag->ofs, for obvious
285                            reasons. So we can do a tree insert from
286                            'this' to insert newfrag, and a tree insert
287                            from newfrag to insert newfrag2. */
288                         jffs2_fragtree_insert(newfrag, this);
289                         rb_insert_color(&newfrag->rb, root);
290                         
291                         jffs2_fragtree_insert(newfrag2, newfrag);
292                         rb_insert_color(&newfrag2->rb, root);
293                         
294                         return 0;
295                 }
296                 /* New node just reduces 'this' frag in size, doesn't split it */
297                 this->size = newfrag->ofs - this->ofs;
298
299                 /* Again, we know it lives down here in the tree */
300                 jffs2_fragtree_insert(newfrag, this);
301                 rb_insert_color(&newfrag->rb, root);
302         } else {
303                 /* New frag starts at the same point as 'this' used to. Replace 
304                    it in the tree without doing a delete and insertion */
305                 JFFS2_DBG_FRAGTREE2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
306                           newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
307         
308                 rb_replace_node(&this->rb, &newfrag->rb, root);
309                 
310                 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
311                         JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
312                         jffs2_obsolete_node_frag(c, this);
313                 } else {
314                         this->ofs += newfrag->size;
315                         this->size -= newfrag->size;
316
317                         jffs2_fragtree_insert(this, newfrag);
318                         rb_insert_color(&this->rb, root);
319                         return 0;
320                 }
321         }
322         /* OK, now we have newfrag added in the correct place in the tree, but
323            frag_next(newfrag) may be a fragment which is overlapped by it 
324         */
325         while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
326                 /* 'this' frag is obsoleted completely. */
327                 JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x) and removing from tree\n",
328                         this, this->ofs, this->ofs+this->size);
329                 rb_erase(&this->rb, root);
330                 jffs2_obsolete_node_frag(c, this);
331         }
332         /* Now we're pointing at the first frag which isn't totally obsoleted by 
333            the new frag */
334
335         if (!this || newfrag->ofs + newfrag->size == this->ofs)
336                 return 0;
337
338         /* Still some overlap but we don't need to move it in the tree */
339         this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
340         this->ofs = newfrag->ofs + newfrag->size;
341
342         /* And mark them REF_NORMAL so the GC takes a look at them */
343         if (this->node)
344                 mark_ref_normal(this->node->raw);
345         mark_ref_normal(newfrag->node->raw);
346
347         return 0;
348 }
349
350 /* 
351  * Given an inode, probably with existing tree of fragments, add the new node
352  * to the fragment tree.
353  */
354 int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
355 {
356         int ret;
357         struct jffs2_node_frag *newfrag;
358
359         if (unlikely(!fn->size))
360                 return 0;
361
362         newfrag = new_fragment(fn, fn->ofs, fn->size);
363         if (unlikely(!newfrag))
364                 return -ENOMEM;
365         newfrag->node->frags = 1;
366
367         JFFS2_DBG_FRAGTREE("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
368                   fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
369         
370         ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
371         if (unlikely(ret))
372                 return ret;
373
374         /* If we now share a page with other nodes, mark either previous
375            or next node REF_NORMAL, as appropriate.  */
376         if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
377                 struct jffs2_node_frag *prev = frag_prev(newfrag);
378
379                 mark_ref_normal(fn->raw);
380                 /* If we don't start at zero there's _always_ a previous */     
381                 if (prev->node)
382                         mark_ref_normal(prev->node->raw);
383         }
384
385         if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
386                 struct jffs2_node_frag *next = frag_next(newfrag);
387                 
388                 if (next) {
389                         mark_ref_normal(fn->raw);
390                         if (next->node)
391                                 mark_ref_normal(next->node->raw);
392                 }
393         }
394         jffs2_dbg_fragtree_paranoia_check_nolock(f);
395
396         return 0;
397 }
398
399 /*
400  * Check the data CRC of the node.
401  *
402  * Returns: 0 if the data CRC is correct;
403  *          1 - if incorrect;
404  *          error code if an error occured.
405  */
406 static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
407 {
408         struct jffs2_raw_node_ref *ref = tn->fn->raw;
409         int err = 0, pointed = 0;
410         struct jffs2_eraseblock *jeb;
411         unsigned char *buffer;
412         uint32_t crc, ofs, retlen, len;
413
414         BUG_ON(tn->csize == 0);
415
416         /* Calculate how many bytes were already checked */
417         ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
418         len = ofs - (ofs & (PAGE_CACHE_SIZE - 1));
419         len = c->wbuf_pagesize - len;
420
421         if (len >= tn->csize) {
422                 JFFS2_DBG_READINODE("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
423                         ref_offset(ref), tn->csize, ofs);
424                 goto adj_acc;
425         }
426         
427         ofs += len;
428         len = tn->csize - len;
429         
430         JFFS2_DBG_READINODE("check node at %#08x, data length %u, partial CRC %#08x, correct CRC %#08x, data starts at %#08x, start checking from %#08x - %u bytes.\n",
431                 ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
432         
433 #ifndef __ECOS
434         /* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
435          * adding and jffs2_flash_read_end() interface. */
436         if (c->mtd->point) {
437                 err = c->mtd->point(c->mtd, ofs, len, &retlen, &buffer);
438                 if (!err && retlen < tn->csize) {
439                         JFFS2_WARNING("MTD point returned len too short: %u instead of %u.\n", retlen, tn->csize);
440                         c->mtd->unpoint(c->mtd, buffer, ofs, len);
441                 } else if (err)
442                         JFFS2_WARNING("MTD point failed: error code %d.\n", err);
443                 else
444                         pointed = 1; /* succefully pointed to device */
445         }
446 #endif
447         
448         if (!pointed) {
449                 buffer = kmalloc(len, GFP_KERNEL);
450                 if (unlikely(!buffer))
451                         return -ENOMEM;
452                         
453                 /* TODO: this is very frequent pattern, make it a separate
454                  * routine */
455                 err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
456                 if (err) {
457                         JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
458                         goto free_out;
459                 }
460                         
461                 if (retlen != len) {
462                         JFFS2_ERROR("short read at %#08x: %d instead of %d.\n", ofs, retlen, len);
463                         err = -EIO;
464                         goto free_out;
465                 }
466         }
467
468         /* Continue calculating CRC */
469         crc = crc32(tn->partial_crc, buffer, len);
470         if(!pointed)
471                 kfree(buffer);
472 #ifndef __ECOS
473         else
474                 c->mtd->unpoint(c->mtd, buffer, ofs, len);
475 #endif
476
477         if (crc != tn->data_crc) {
478                 JFFS2_NOTICE("drong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
479                         ofs, tn->data_crc, crc);
480                 return 1;
481         }
482
483 adj_acc:
484         jeb = &c->blocks[ref->flash_offset / c->sector_size];
485         len = ref_totlen(c, jeb, ref);
486
487         /* 
488          * Mark the node as having been checked and fix the
489          * accounting accordingly.
490          */
491         spin_lock(&c->erase_completion_lock);
492         jeb->used_size += len;
493         jeb->unchecked_size -= len;
494         c->used_size += len;
495         c->unchecked_size -= len;
496         spin_unlock(&c->erase_completion_lock);
497
498         return 0;
499
500 free_out:
501         if(!pointed)
502                 kfree(buffer);
503 #ifndef __ECOS
504         else
505                 c->mtd->unpoint(c->mtd, buffer, ofs, len);
506 #endif
507         return err;
508 }
509
510 /*
511  * Helper function for jffs2_add_older_frag_to_fragtree().
512  *
513  * Checks the node if we are in the checking stage.
514  */
515 static inline int check_node(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_tmp_dnode_info *tn)
516 {
517         int ret;
518         
519         BUG_ON(ref_obsolete(tn->fn->raw));
520
521         /* We only check the data CRC of unchecked nodes */
522         if (ref_flags(tn->fn->raw) != REF_UNCHECKED)
523                 return 0;
524         
525         JFFS2_DBG_FRAGTREE2("check node %u-%u, phys offs %#08x.\n",
526                 tn->fn->ofs, tn->fn->ofs + tn->fn->size,
527                 ref_offset(tn->fn->raw));
528
529         ret = check_node_data(c, tn);
530         if (unlikely(ret < 0)) {
531                 JFFS2_ERROR("check_node_data() returned error: %d.\n",
532                         ret);
533         } else if (unlikely(ret > 0)) {
534                 JFFS2_DBG_FRAGTREE2("CRC error, mark it obsolete.\n");
535                 jffs2_mark_node_obsolete(c, tn->fn->raw);
536         }
537
538         return ret;
539 }
540
541 /* 
542  * Helper function for jffs2_add_older_frag_to_fragtree().
543  *
544  * Called when the new fragment that is being inserted
545  * splits a hole fragment.
546  */
547 static int split_hole(struct jffs2_sb_info *c, struct rb_root *root,
548                       struct jffs2_node_frag *newfrag, struct jffs2_node_frag *hole)
549 {
550         JFFS2_DBG_FRAGTREE2("fragment %#04x-%#04x splits the hole %#04x-%#04x\n",
551                 newfrag->ofs, newfrag->ofs + newfrag->size, hole->ofs, hole->ofs + hole->size);
552
553         if (hole->ofs == newfrag->ofs) {
554                 /* 
555                  * Well, the new fragment actually starts at the same offset as
556                  * the hole.
557                  */
558                 if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
559                         /* 
560                          * We replace the overlapped left part of the hole by
561                          * the new node.
562                          */
563                         
564                         JFFS2_DBG_FRAGTREE2("insert fragment %#04x-%#04x and cut the left part of the hole\n",
565                                 newfrag->ofs, newfrag->ofs + newfrag->size);
566                         rb_replace_node(&hole->rb, &newfrag->rb, root);
567                         
568                         hole->ofs += newfrag->size;
569                         hole->size -= newfrag->size;
570                         
571                         /* 
572                          * We know that 'hole' should be the right hand
573                          * fragment.
574                          */
575                         jffs2_fragtree_insert(hole, newfrag);
576                         rb_insert_color(&hole->rb, root);
577                 } else {
578                         /* 
579                          * Ah, the new fragment is of the same size as the hole.
580                          * Relace the hole by it.
581                          */
582                         JFFS2_DBG_FRAGTREE2("insert fragment %#04x-%#04x and overwrite hole\n",
583                                 newfrag->ofs, newfrag->ofs + newfrag->size);
584                         rb_replace_node(&hole->rb, &newfrag->rb, root);
585                         jffs2_free_node_frag(hole);
586                 }
587         } else {
588                 /* The new fragment lefts some hole space at the left */
589                 
590                 struct jffs2_node_frag * newfrag2 = NULL;
591
592                 if (hole->ofs + hole->size > newfrag->ofs + newfrag->size) {
593                         /* The new frag also lefts some space at the right */
594                         newfrag2 = new_fragment(NULL, newfrag->ofs +
595                                 newfrag->size, hole->ofs + hole->size
596                                 - newfrag->ofs - newfrag->size);
597                         if (unlikely(!newfrag2)) {
598                                 jffs2_free_node_frag(newfrag);
599                                 return -ENOMEM;
600                         }
601                 }
602
603                 hole->size = newfrag->ofs - hole->ofs;
604                 JFFS2_DBG_FRAGTREE2("left the hole %#04x-%#04x at the left and inserd fragment %#04x-%#04x\n",
605                         hole->ofs, hole->ofs + hole->size, newfrag->ofs, newfrag->ofs + newfrag->size);
606
607                 jffs2_fragtree_insert(newfrag, hole);
608                 rb_insert_color(&newfrag->rb, root);
609                 
610                 if (newfrag2) {
611                         JFFS2_DBG_FRAGTREE2("left the hole %#04x-%#04x at the right\n",
612                                 newfrag2->ofs, newfrag2->ofs + newfrag2->size);
613                         jffs2_fragtree_insert(newfrag2, newfrag);
614                         rb_insert_color(&newfrag2->rb, root);
615                 }
616         }
617
618         return 0;
619 }
620
621 /*
622  * This function is used when we build inode. It expects the nodes are passed
623  * in the decreasing version order. The whole point of this is to improve the
624  * inodes checking on NAND: we check the nodes' data CRC only when they are not
625  * obsoleted. Previously, add_frag_to_fragtree() function was used and
626  * nodes were passed to it in the increasing version ordes and CRCs of all
627  * nodes were checked.
628  *
629  * Note: tn->fn->size shouldn't be zero.
630  *
631  * Returns 0 if the node was inserted
632  *         1 if it wasn't inserted (since it is obsolete)
633  *         < 0 an if error occured
634  */
635 int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
636                                      struct jffs2_tmp_dnode_info *tn)
637 {
638         struct jffs2_node_frag *this, *newfrag;
639         uint32_t lastend;
640         struct jffs2_full_dnode *fn = tn->fn;
641         struct rb_root *root = &f->fragtree;
642         uint32_t fn_size = fn->size, fn_ofs = fn->ofs;
643         int err, checked = 0;
644         int ref_flag;
645
646         JFFS2_DBG_FRAGTREE("insert fragment %#04x-%#04x\n", fn_ofs, fn_ofs + fn_size);
647
648         /* Skip all the nodes which are completed before this one starts */
649         this = jffs2_lookup_node_frag(root, fn_ofs);
650         if (this)
651                 JFFS2_DBG_FRAGTREE2("'this' found %#04x-%#04x (%s)\n", this->ofs, this->ofs + this->size, this->node ? "data" : "hole");
652
653         if (this)
654                 lastend = this->ofs + this->size;
655         else
656                 lastend = 0;
657         
658         /* Detect the preliminary type of node */
659         if (fn->size >= PAGE_CACHE_SIZE)
660                 ref_flag = REF_PRISTINE;
661         else
662                 ref_flag = REF_NORMAL;
663         
664         /* See if we ran off the end of the root */
665         if (lastend <= fn_ofs) {
666                 /* We did */
667                 
668                 /* 
669                  * We are going to insert the new node into the
670                  * fragment tree, so check it.
671                  */
672                 err = check_node(c, f, tn);
673                 if (err != 0)
674                         return err;
675
676                 fn->frags = 1;
677
678                 newfrag = new_fragment(fn, fn_ofs, fn_size);
679                 if (unlikely(!newfrag))
680                         return -ENOMEM;
681
682                 err = no_overlapping_node(c, root, newfrag, this, lastend);
683                 if (unlikely(err != 0)) {
684                         jffs2_free_node_frag(newfrag);
685                         return err;
686                 }
687
688                 goto out_ok;
689         }
690
691         fn->frags = 0;
692
693         while (1) {
694                 /* 
695                  * Here we have:
696                  * fn_ofs < this->ofs + this->size && fn_ofs >= this->ofs.
697                  * 
698                  * Remember, 'this' has higher version, any non-hole node
699                  * which is already in the fragtree is newer then the newly
700                  * inserted.
701                  */
702                 if (!this->node) {
703                         /* 
704                          * 'this' is the hole fragment, so at least the
705                          * beginning of the new fragment is valid.
706                          */
707                         
708                         /* 
709                          * We are going to insert the new node into the
710                          * fragment tree, so check it.
711                          */
712                         if (!checked) {
713                                 err = check_node(c, f, tn);
714                                 if (unlikely(err != 0))
715                                         return err;
716                                 checked = 1;
717                         }
718                         
719                         if (this->ofs + this->size >= fn_ofs + fn_size) {
720                                 /* We split the hole on two parts */
721
722                                 fn->frags += 1;
723                                 newfrag = new_fragment(fn, fn_ofs, fn_size);
724                                 if (unlikely(!newfrag))
725                                         return -ENOMEM;
726
727                                 err = split_hole(c, root, newfrag, this);
728                                 if (unlikely(err))
729                                         return err;
730                                 goto out_ok;
731                         }
732
733                         /* 
734                          * The beginning of the new fragment is valid since it
735                          * overlaps the hole node.
736                          */
737
738                         ref_flag = REF_NORMAL;
739
740                         fn->frags += 1;
741                         newfrag = new_fragment(fn, fn_ofs,
742                                         this->ofs + this->size - fn_ofs);
743                         if (unlikely(!newfrag))
744                                 return -ENOMEM;
745                         
746                         if (fn_ofs == this->ofs) {
747                                 /* 
748                                  * The new node starts at the same offset as
749                                  * the hole and supersieds the hole.
750                                  */
751                                 JFFS2_DBG_FRAGTREE2("add the new fragment instead of hole %#04x-%#04x, refcnt %d\n",
752                                         fn_ofs, fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);
753
754                                 rb_replace_node(&this->rb, &newfrag->rb, root);
755                                 jffs2_free_node_frag(this);
756                         } else {
757                                 /* 
758                                  * The hole becomes shorter as its right part
759                                  * is supersieded by the new fragment.
760                                  */
761                                 JFFS2_DBG_FRAGTREE2("reduce size of hole %#04x-%#04x to %#04x-%#04x\n",
762                                         this->ofs, this->ofs + this->size, this->ofs, this->ofs + this->size - newfrag->size);
763                                 
764                                 JFFS2_DBG_FRAGTREE2("add new fragment %#04x-%#04x, refcnt %d\n", fn_ofs,
765                                         fn_ofs + this->ofs + this->size - fn_ofs, fn->frags);
766         
767                                 this->size -= newfrag->size;
768                                 jffs2_fragtree_insert(newfrag, this);
769                                 rb_insert_color(&newfrag->rb, root);
770                         }
771                         
772                         fn_ofs += newfrag->size;
773                         fn_size -= newfrag->size;
774                         this = rb_entry(rb_next(&newfrag->rb),
775                                         struct jffs2_node_frag, rb);
776
777                         JFFS2_DBG_FRAGTREE2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
778                                 this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
779                 }
780
781                 /* 
782                  * 'This' node is not the hole so it obsoletes the new fragment
783                  * either fully or partially.
784                  */
785                 if (this->ofs + this->size >= fn_ofs + fn_size) {
786                         /* The new node is obsolete, drop it */
787                         if (fn->frags == 0) {
788                                 JFFS2_DBG_FRAGTREE2("%#04x-%#04x is obsolete, mark it obsolete\n", fn_ofs, fn_ofs + fn_size);
789                                 ref_flag = REF_OBSOLETE;
790                         }
791                         goto out_ok;
792                 } else {
793                         struct jffs2_node_frag *new_this;
794                         
795                         /* 'This' node obsoletes the beginning of the new node */
796                         JFFS2_DBG_FRAGTREE2("the beginning %#04x-%#04x is obsolete\n", fn_ofs, this->ofs + this->size);
797
798                         ref_flag = REF_NORMAL;
799                         
800                         fn_size -= this->ofs + this->size - fn_ofs;
801                         fn_ofs = this->ofs + this->size;
802                         JFFS2_DBG_FRAGTREE2("now considering %#04x-%#04x\n", fn_ofs, fn_ofs + fn_size);
803                         
804                         new_this = rb_entry(rb_next(&this->rb), struct jffs2_node_frag, rb);
805                         if (!new_this) {
806                                 /* 
807                                  * There is no next fragment. Add the rest of
808                                  * the new node as the right-hand child.
809                                  */
810                                 if (!checked) {
811                                         err = check_node(c, f, tn);
812                                         if (unlikely(err != 0))
813                                                 return err;
814                                         checked = 1;
815                                 }
816                                 
817                                 fn->frags += 1;
818                                 newfrag = new_fragment(fn, fn_ofs, fn_size);
819                                 if (unlikely(!newfrag))
820                                         return -ENOMEM;
821
822                                 JFFS2_DBG_FRAGTREE2("there are no more fragments, insert %#04x-%#04x\n",
823                                         newfrag->ofs, newfrag->ofs + newfrag->size);
824                                 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);                      
825                                 rb_insert_color(&newfrag->rb, root);
826                                 goto out_ok;
827                         } else {
828                                 this = new_this;
829                                 JFFS2_DBG_FRAGTREE2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
830                                         this->ofs, this->ofs + this->size, this->node ? "(data)" : "(hole)");
831                         }
832                 }
833         }
834
835 out_ok:
836         BUG_ON(fn->size < PAGE_CACHE_SIZE && ref_flag == REF_PRISTINE);
837
838         if (ref_flag == REF_OBSOLETE) {
839                 JFFS2_DBG_FRAGTREE2("the node is obsolete now\n");
840                 /* jffs2_mark_node_obsolete() will adjust space accounting */
841                 jffs2_mark_node_obsolete(c, fn->raw);
842                 return 1;
843         }
844
845         JFFS2_DBG_FRAGTREE2("the node is \"%s\" now\n", ref_flag == REF_NORMAL ? "REF_NORMAL" : "REF_PRISTINE");
846
847         /* Space accounting was adjusted at check_node_data() */
848         spin_lock(&c->erase_completion_lock);
849         fn->raw->flash_offset = ref_offset(fn->raw) | ref_flag;
850         spin_unlock(&c->erase_completion_lock);
851
852         return 0;
853 }
854
855 void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
856 {
857         spin_lock(&c->inocache_lock);
858         ic->state = state;
859         wake_up(&c->inocache_wq);
860         spin_unlock(&c->inocache_lock);
861 }
862
863 /* During mount, this needs no locking. During normal operation, its
864    callers want to do other stuff while still holding the inocache_lock.
865    Rather than introducing special case get_ino_cache functions or 
866    callbacks, we just let the caller do the locking itself. */
867    
868 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
869 {
870         struct jffs2_inode_cache *ret;
871
872         ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
873         while (ret && ret->ino < ino) {
874                 ret = ret->next;
875         }
876         
877         if (ret && ret->ino != ino)
878                 ret = NULL;
879
880         return ret;
881 }
882
883 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
884 {
885         struct jffs2_inode_cache **prev;
886
887         spin_lock(&c->inocache_lock);
888         if (!new->ino)
889                 new->ino = ++c->highest_ino;
890
891         JFFS2_DBG_INOCACHE("add %p (ino #%u)\n", new, new->ino);
892
893         prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
894
895         while ((*prev) && (*prev)->ino < new->ino) {
896                 prev = &(*prev)->next;
897         }
898         new->next = *prev;
899         *prev = new;
900
901         spin_unlock(&c->inocache_lock);
902 }
903
904 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
905 {
906         struct jffs2_inode_cache **prev;
907
908         JFFS2_DBG_INOCACHE("del %p (ino #%u)\n", old, old->ino);
909         spin_lock(&c->inocache_lock);
910         
911         prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
912         
913         while ((*prev) && (*prev)->ino < old->ino) {
914                 prev = &(*prev)->next;
915         }
916         if ((*prev) == old) {
917                 *prev = old->next;
918         }
919
920         /* Free it now unless it's in READING or CLEARING state, which
921            are the transitions upon read_inode() and clear_inode(). The
922            rest of the time we know nobody else is looking at it, and 
923            if it's held by read_inode() or clear_inode() they'll free it
924            for themselves. */
925         if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
926                 jffs2_free_inode_cache(old);
927
928         spin_unlock(&c->inocache_lock);
929 }
930
931 void jffs2_free_ino_caches(struct jffs2_sb_info *c)
932 {
933         int i;
934         struct jffs2_inode_cache *this, *next;
935         
936         for (i=0; i<INOCACHE_HASHSIZE; i++) {
937                 this = c->inocache_list[i];
938                 while (this) {
939                         next = this->next;
940                         jffs2_free_inode_cache(this);
941                         this = next;
942                 }
943                 c->inocache_list[i] = NULL;
944         }
945 }
946
947 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
948 {
949         int i;
950         struct jffs2_raw_node_ref *this, *next;
951
952         for (i=0; i<c->nr_blocks; i++) {
953                 this = c->blocks[i].first_node;
954                 while(this) {
955                         next = this->next_phys;
956                         jffs2_free_raw_node_ref(this);
957                         this = next;
958                 }
959                 c->blocks[i].first_node = c->blocks[i].last_node = NULL;
960         }
961 }
962         
963 struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
964 {
965         /* The common case in lookup is that there will be a node 
966            which precisely matches. So we go looking for that first */
967         struct rb_node *next;
968         struct jffs2_node_frag *prev = NULL;
969         struct jffs2_node_frag *frag = NULL;
970
971         JFFS2_DBG_FRAGTREE2("root %p, offset %d\n", fragtree, offset);
972
973         next = fragtree->rb_node;
974
975         while(next) {
976                 frag = rb_entry(next, struct jffs2_node_frag, rb);
977
978                 JFFS2_DBG_FRAGTREE2("considering frag %#04x-%#04x (%p). left %p, right %p\n",
979                           frag->ofs, frag->ofs+frag->size, frag, frag->rb.rb_left, frag->rb.rb_right);
980                 if (frag->ofs + frag->size <= offset) {
981                         JFFS2_DBG_FRAGTREE2("going right from frag %#04x-%#04x, before the region we care about\n",
982                                   frag->ofs, frag->ofs+frag->size);
983                         /* Remember the closest smaller match on the way down */
984                         if (!prev || frag->ofs > prev->ofs)
985                                 prev = frag;
986                         next = frag->rb.rb_right;
987                 } else if (frag->ofs > offset) {
988                         JFFS2_DBG_FRAGTREE2("going left from frag %#04x-%#04x, after the region we care about\n",
989                                   frag->ofs, frag->ofs+frag->size);
990                         next = frag->rb.rb_left;
991                 } else {
992                         JFFS2_DBG_FRAGTREE2("returning frag %#04x-%#04x, matched\n",
993                                   frag->ofs, frag->ofs+frag->size);
994                         return frag;
995                 }
996         }
997
998         /* Exact match not found. Go back up looking at each parent,
999            and return the closest smaller one */
1000
1001         if (prev)
1002                 JFFS2_DBG_FRAGTREE2("no match. Returning frag %#04x-%#04x, closest previous\n",
1003                           prev->ofs, prev->ofs+prev->size);
1004         else 
1005                 JFFS2_DBG_FRAGTREE2("returning NULL, empty fragtree\n");
1006         
1007         return prev;
1008 }
1009
1010 /* Pass 'c' argument to indicate that nodes should be marked obsolete as
1011    they're killed. */
1012 void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
1013 {
1014         struct jffs2_node_frag *frag;
1015         struct jffs2_node_frag *parent;
1016
1017         if (!root->rb_node)
1018                 return;
1019
1020         JFFS2_DBG_FRAGTREE("killing\n");
1021         
1022         frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
1023         while(frag) {
1024                 if (frag->rb.rb_left) {
1025                         JFFS2_DBG_FRAGTREE2("going left from frag (%p) %#04x-%#04x\n",
1026                                 frag, frag->ofs, frag->ofs+frag->size);
1027                         frag = frag_left(frag);
1028                         continue;
1029                 }
1030                 if (frag->rb.rb_right) {
1031                         JFFS2_DBG_FRAGTREE2("going right from frag (%p) %#04x-%#04x\n", 
1032                                   frag, frag->ofs, frag->ofs+frag->size);
1033                         frag = frag_right(frag);
1034                         continue;
1035                 }
1036
1037                 JFFS2_DBG_FRAGTREE2("frag %#04x-%#04x: node %p, frags %d\n",
1038                           frag->ofs, frag->ofs+frag->size, frag->node, frag->node?frag->node->frags:0);
1039                         
1040                 if (frag->node && !(--frag->node->frags)) {
1041                         /* Not a hole, and it's the final remaining frag 
1042                            of this node. Free the node */
1043                         if (c)
1044                                 jffs2_mark_node_obsolete(c, frag->node->raw);
1045                         
1046                         jffs2_free_full_dnode(frag->node);
1047                 }
1048                 parent = frag_parent(frag);
1049                 if (parent) {
1050                         if (frag_left(parent) == frag)
1051                                 parent->rb.rb_left = NULL;
1052                         else 
1053                                 parent->rb.rb_right = NULL;
1054                 }
1055
1056                 jffs2_free_node_frag(frag);
1057                 frag = parent;
1058
1059                 cond_resched();
1060         }
1061 }