2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: nodelist.c,v 1.115 2005/11/07 11:14:40 gleixner Exp $
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/rbtree.h>
19 #include <linux/crc32.h>
20 #include <linux/slab.h>
21 #include <linux/pagemap.h>
24 void jffs2_add_fd_to_list(struct jffs2_sb_info
*c
, struct jffs2_full_dirent
*new, struct jffs2_full_dirent
**list
)
26 struct jffs2_full_dirent
**prev
= list
;
28 dbg_dentlist("add dirent \"%s\", ino #%u\n", new->name
, new->ino
);
30 while ((*prev
) && (*prev
)->nhash
<= new->nhash
) {
31 if ((*prev
)->nhash
== new->nhash
&& !strcmp((*prev
)->name
, new->name
)) {
32 /* Duplicate. Free one */
33 if (new->version
< (*prev
)->version
) {
34 dbg_dentlist("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n",
35 (*prev
)->name
, (*prev
)->ino
);
36 jffs2_mark_node_obsolete(c
, new->raw
);
37 jffs2_free_full_dirent(new);
39 dbg_dentlist("marking old dirent \"%s\", ino #%u bsolete\n",
40 (*prev
)->name
, (*prev
)->ino
);
41 new->next
= (*prev
)->next
;
42 jffs2_mark_node_obsolete(c
, ((*prev
)->raw
));
43 jffs2_free_full_dirent(*prev
);
48 prev
= &((*prev
)->next
);
54 void jffs2_truncate_fragtree(struct jffs2_sb_info
*c
, struct rb_root
*list
, uint32_t size
)
56 struct jffs2_node_frag
*frag
= jffs2_lookup_node_frag(list
, size
);
58 dbg_fragtree("truncating fragtree to 0x%08x bytes\n", size
);
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 frag
->size
= size
- frag
->ofs
;
65 frag
= frag_next(frag
);
67 while (frag
&& frag
->ofs
>= size
) {
68 struct jffs2_node_frag
*next
= frag_next(frag
);
70 frag_erase(frag
, list
);
71 jffs2_obsolete_node_frag(c
, frag
);
79 * If the last fragment starts at the RAM page boundary, it is
80 * REF_PRISTINE irrespective of its size.
82 frag
= frag_last(list
);
83 if (frag
->node
&& (frag
->ofs
& (PAGE_CACHE_SIZE
- 1)) == 0) {
84 dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
85 frag
->ofs
, frag
->ofs
+ frag
->size
);
86 frag
->node
->raw
->flash_offset
= ref_offset(frag
->node
->raw
) | REF_PRISTINE
;
90 void jffs2_obsolete_node_frag(struct jffs2_sb_info
*c
, struct jffs2_node_frag
*this)
94 if (!this->node
->frags
) {
95 /* The node has no valid frags left. It's totally obsoleted */
96 dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
97 ref_offset(this->node
->raw
), this->node
->ofs
, this->node
->ofs
+this->node
->size
);
98 jffs2_mark_node_obsolete(c
, this->node
->raw
);
99 jffs2_free_full_dnode(this->node
);
101 dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
102 ref_offset(this->node
->raw
), this->node
->ofs
, this->node
->ofs
+this->node
->size
, this->node
->frags
);
103 mark_ref_normal(this->node
->raw
);
107 jffs2_free_node_frag(this);
110 static void jffs2_fragtree_insert(struct jffs2_node_frag
*newfrag
, struct jffs2_node_frag
*base
)
112 struct rb_node
*parent
= &base
->rb
;
113 struct rb_node
**link
= &parent
;
115 dbg_fragtree2("insert frag (0x%04x-0x%04x)\n", newfrag
->ofs
, newfrag
->ofs
+ newfrag
->size
);
119 base
= rb_entry(parent
, struct jffs2_node_frag
, rb
);
121 if (newfrag
->ofs
> base
->ofs
)
122 link
= &base
->rb
.rb_right
;
123 else if (newfrag
->ofs
< base
->ofs
)
124 link
= &base
->rb
.rb_left
;
126 JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag
->ofs
, newfrag
, base
);
131 rb_link_node(&newfrag
->rb
, &base
->rb
, link
);
135 * Allocate and initializes a new fragment.
137 static struct jffs2_node_frag
* new_fragment(struct jffs2_full_dnode
*fn
, uint32_t ofs
, uint32_t size
)
139 struct jffs2_node_frag
*newfrag
;
141 newfrag
= jffs2_alloc_node_frag();
142 if (likely(newfrag
)) {
144 newfrag
->size
= size
;
147 JFFS2_ERROR("cannot allocate a jffs2_node_frag object\n");
154 * Called when there is no overlapping fragment exist. Inserts a hole before the new
155 * fragment and inserts the new fragment to the fragtree.
157 static int no_overlapping_node(struct jffs2_sb_info
*c
, struct rb_root
*root
,
158 struct jffs2_node_frag
*newfrag
,
159 struct jffs2_node_frag
*this, uint32_t lastend
)
161 if (lastend
< newfrag
->node
->ofs
) {
162 /* put a hole in before the new fragment */
163 struct jffs2_node_frag
*holefrag
;
165 holefrag
= new_fragment(NULL
, lastend
, newfrag
->node
->ofs
- lastend
);
166 if (unlikely(!holefrag
)) {
167 jffs2_free_node_frag(newfrag
);
172 /* By definition, the 'this' node has no right-hand child,
173 because there are no frags with offset greater than it.
174 So that's where we want to put the hole */
175 dbg_fragtree2("add hole frag %#04x-%#04x on the right of the new frag.\n",
176 holefrag
->ofs
, holefrag
->ofs
+ holefrag
->size
);
177 rb_link_node(&holefrag
->rb
, &this->rb
, &this->rb
.rb_right
);
179 dbg_fragtree2("Add hole frag %#04x-%#04x to the root of the tree.\n",
180 holefrag
->ofs
, holefrag
->ofs
+ holefrag
->size
);
181 rb_link_node(&holefrag
->rb
, NULL
, &root
->rb_node
);
183 rb_insert_color(&holefrag
->rb
, root
);
188 /* By definition, the 'this' node has no right-hand child,
189 because there are no frags with offset greater than it.
190 So that's where we want to put new fragment */
191 dbg_fragtree2("add the new node at the right\n");
192 rb_link_node(&newfrag
->rb
, &this->rb
, &this->rb
.rb_right
);
194 dbg_fragtree2("insert the new node at the root of the tree\n");
195 rb_link_node(&newfrag
->rb
, NULL
, &root
->rb_node
);
197 rb_insert_color(&newfrag
->rb
, root
);
202 /* Doesn't set inode->i_size */
203 static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info
*c
, struct rb_root
*root
, struct jffs2_node_frag
*newfrag
)
205 struct jffs2_node_frag
*this;
208 /* Skip all the nodes which are completed before this one starts */
209 this = jffs2_lookup_node_frag(root
, newfrag
->node
->ofs
);
212 dbg_fragtree2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
213 this->ofs
, this->ofs
+this->size
, this->node
?(ref_offset(this->node
->raw
)):0xffffffff, this);
214 lastend
= this->ofs
+ this->size
;
216 dbg_fragtree2("lookup gave no frag\n");
220 /* See if we ran off the end of the fragtree */
221 if (lastend
<= newfrag
->ofs
) {
224 /* Check if 'this' node was on the same page as the new node.
225 If so, both 'this' and the new node get marked REF_NORMAL so
226 the GC can take a look.
228 if (lastend
&& (lastend
-1) >> PAGE_CACHE_SHIFT
== newfrag
->ofs
>> PAGE_CACHE_SHIFT
) {
230 mark_ref_normal(this->node
->raw
);
231 mark_ref_normal(newfrag
->node
->raw
);
234 return no_overlapping_node(c
, root
, newfrag
, this, lastend
);
238 dbg_fragtree2("dealing with frag %u-%u, phys %#08x(%d).\n",
239 this->ofs
, this->ofs
+ this->size
,
240 ref_offset(this->node
->raw
), ref_flags(this->node
->raw
));
242 dbg_fragtree2("dealing with hole frag %u-%u.\n",
243 this->ofs
, this->ofs
+ this->size
);
245 /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
246 * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
248 if (newfrag
->ofs
> this->ofs
) {
249 /* This node isn't completely obsoleted. The start of it remains valid */
251 /* Mark the new node and the partially covered node REF_NORMAL -- let
252 the GC take a look at them */
253 mark_ref_normal(newfrag
->node
->raw
);
255 mark_ref_normal(this->node
->raw
);
257 if (this->ofs
+ this->size
> newfrag
->ofs
+ newfrag
->size
) {
258 /* The new node splits 'this' frag into two */
259 struct jffs2_node_frag
*newfrag2
;
262 dbg_fragtree2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
263 this->ofs
, this->ofs
+this->size
, ref_offset(this->node
->raw
));
265 dbg_fragtree2("split old hole frag 0x%04x-0x%04x\n",
266 this->ofs
, this->ofs
+this->size
);
268 /* New second frag pointing to this's node */
269 newfrag2
= new_fragment(this->node
, newfrag
->ofs
+ newfrag
->size
,
270 this->ofs
+ this->size
- newfrag
->ofs
- newfrag
->size
);
271 if (unlikely(!newfrag2
))
276 /* Adjust size of original 'this' */
277 this->size
= newfrag
->ofs
- this->ofs
;
279 /* Now, we know there's no node with offset
280 greater than this->ofs but smaller than
281 newfrag2->ofs or newfrag->ofs, for obvious
282 reasons. So we can do a tree insert from
283 'this' to insert newfrag, and a tree insert
284 from newfrag to insert newfrag2. */
285 jffs2_fragtree_insert(newfrag
, this);
286 rb_insert_color(&newfrag
->rb
, root
);
288 jffs2_fragtree_insert(newfrag2
, newfrag
);
289 rb_insert_color(&newfrag2
->rb
, root
);
293 /* New node just reduces 'this' frag in size, doesn't split it */
294 this->size
= newfrag
->ofs
- this->ofs
;
296 /* Again, we know it lives down here in the tree */
297 jffs2_fragtree_insert(newfrag
, this);
298 rb_insert_color(&newfrag
->rb
, root
);
300 /* New frag starts at the same point as 'this' used to. Replace
301 it in the tree without doing a delete and insertion */
302 dbg_fragtree2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
303 newfrag
, newfrag
->ofs
, newfrag
->ofs
+newfrag
->size
, this, this->ofs
, this->ofs
+this->size
);
305 rb_replace_node(&this->rb
, &newfrag
->rb
, root
);
307 if (newfrag
->ofs
+ newfrag
->size
>= this->ofs
+this->size
) {
308 dbg_fragtree2("obsoleting node frag %p (%x-%x)\n", this, this->ofs
, this->ofs
+this->size
);
309 jffs2_obsolete_node_frag(c
, this);
311 this->ofs
+= newfrag
->size
;
312 this->size
-= newfrag
->size
;
314 jffs2_fragtree_insert(this, newfrag
);
315 rb_insert_color(&this->rb
, root
);
319 /* OK, now we have newfrag added in the correct place in the tree, but
320 frag_next(newfrag) may be a fragment which is overlapped by it
322 while ((this = frag_next(newfrag
)) && newfrag
->ofs
+ newfrag
->size
>= this->ofs
+ this->size
) {
323 /* 'this' frag is obsoleted completely. */
324 dbg_fragtree2("obsoleting node frag %p (%x-%x) and removing from tree\n",
325 this, this->ofs
, this->ofs
+this->size
);
326 rb_erase(&this->rb
, root
);
327 jffs2_obsolete_node_frag(c
, this);
329 /* Now we're pointing at the first frag which isn't totally obsoleted by
332 if (!this || newfrag
->ofs
+ newfrag
->size
== this->ofs
)
335 /* Still some overlap but we don't need to move it in the tree */
336 this->size
= (this->ofs
+ this->size
) - (newfrag
->ofs
+ newfrag
->size
);
337 this->ofs
= newfrag
->ofs
+ newfrag
->size
;
339 /* And mark them REF_NORMAL so the GC takes a look at them */
341 mark_ref_normal(this->node
->raw
);
342 mark_ref_normal(newfrag
->node
->raw
);
348 * Given an inode, probably with existing tree of fragments, add the new node
349 * to the fragment tree.
351 int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
, struct jffs2_full_dnode
*fn
)
354 struct jffs2_node_frag
*newfrag
;
356 if (unlikely(!fn
->size
))
359 newfrag
= new_fragment(fn
, fn
->ofs
, fn
->size
);
360 if (unlikely(!newfrag
))
362 newfrag
->node
->frags
= 1;
364 dbg_fragtree("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
365 fn
->ofs
, fn
->ofs
+fn
->size
, ref_offset(fn
->raw
), newfrag
);
367 ret
= jffs2_add_frag_to_fragtree(c
, &f
->fragtree
, newfrag
);
371 /* If we now share a page with other nodes, mark either previous
372 or next node REF_NORMAL, as appropriate. */
373 if (newfrag
->ofs
& (PAGE_CACHE_SIZE
-1)) {
374 struct jffs2_node_frag
*prev
= frag_prev(newfrag
);
376 mark_ref_normal(fn
->raw
);
377 /* If we don't start at zero there's _always_ a previous */
379 mark_ref_normal(prev
->node
->raw
);
382 if ((newfrag
->ofs
+newfrag
->size
) & (PAGE_CACHE_SIZE
-1)) {
383 struct jffs2_node_frag
*next
= frag_next(newfrag
);
386 mark_ref_normal(fn
->raw
);
388 mark_ref_normal(next
->node
->raw
);
391 jffs2_dbg_fragtree_paranoia_check_nolock(f
);
397 * Check the data CRC of the node.
399 * Returns: 0 if the data CRC is correct;
401 * error code if an error occured.
403 static int check_node_data(struct jffs2_sb_info
*c
, struct jffs2_tmp_dnode_info
*tn
)
405 struct jffs2_raw_node_ref
*ref
= tn
->fn
->raw
;
406 int err
= 0, pointed
= 0;
407 struct jffs2_eraseblock
*jeb
;
408 unsigned char *buffer
;
409 uint32_t crc
, ofs
, len
;
412 BUG_ON(tn
->csize
== 0);
414 if (!jffs2_is_writebuffered(c
))
417 /* Calculate how many bytes were already checked */
418 ofs
= ref_offset(ref
) + sizeof(struct jffs2_raw_inode
);
419 len
= ofs
% c
->wbuf_pagesize
;
421 len
= c
->wbuf_pagesize
- len
;
423 if (len
>= tn
->csize
) {
424 dbg_readinode("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
425 ref_offset(ref
), tn
->csize
, ofs
);
430 len
= tn
->csize
- len
;
432 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",
433 ref_offset(ref
), tn
->csize
, tn
->partial_crc
, tn
->data_crc
, ofs
- len
, ofs
, len
);
436 /* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
437 * adding and jffs2_flash_read_end() interface. */
439 err
= c
->mtd
->point(c
->mtd
, ofs
, len
, &retlen
, &buffer
);
440 if (!err
&& retlen
< tn
->csize
) {
441 JFFS2_WARNING("MTD point returned len too short: %u instead of %u.\n", retlen
, tn
->csize
);
442 c
->mtd
->unpoint(c
->mtd
, buffer
, ofs
, len
);
444 JFFS2_WARNING("MTD point failed: error code %d.\n", err
);
446 pointed
= 1; /* succefully pointed to device */
451 buffer
= kmalloc(len
, GFP_KERNEL
);
452 if (unlikely(!buffer
))
455 /* TODO: this is very frequent pattern, make it a separate
457 err
= jffs2_flash_read(c
, ofs
, len
, &retlen
, buffer
);
459 JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len
, ofs
, err
);
464 JFFS2_ERROR("short read at %#08x: %d instead of %d.\n", ofs
, retlen
, len
);
470 /* Continue calculating CRC */
471 crc
= crc32(tn
->partial_crc
, buffer
, len
);
476 c
->mtd
->unpoint(c
->mtd
, buffer
, ofs
, len
);
479 if (crc
!= tn
->data_crc
) {
480 JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
481 ofs
, tn
->data_crc
, crc
);
486 jeb
= &c
->blocks
[ref
->flash_offset
/ c
->sector_size
];
487 len
= ref_totlen(c
, jeb
, ref
);
490 * Mark the node as having been checked and fix the
491 * accounting accordingly.
493 spin_lock(&c
->erase_completion_lock
);
494 jeb
->used_size
+= len
;
495 jeb
->unchecked_size
-= len
;
497 c
->unchecked_size
-= len
;
498 spin_unlock(&c
->erase_completion_lock
);
507 c
->mtd
->unpoint(c
->mtd
, buffer
, ofs
, len
);
513 * Helper function for jffs2_add_older_frag_to_fragtree().
515 * Checks the node if we are in the checking stage.
517 static int check_node(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
, struct jffs2_tmp_dnode_info
*tn
)
521 BUG_ON(ref_obsolete(tn
->fn
->raw
));
523 /* We only check the data CRC of unchecked nodes */
524 if (ref_flags(tn
->fn
->raw
) != REF_UNCHECKED
)
527 dbg_fragtree2("check node %#04x-%#04x, phys offs %#08x.\n",
528 tn
->fn
->ofs
, tn
->fn
->ofs
+ tn
->fn
->size
, ref_offset(tn
->fn
->raw
));
530 ret
= check_node_data(c
, tn
);
531 if (unlikely(ret
< 0)) {
532 JFFS2_ERROR("check_node_data() returned error: %d.\n",
534 } else if (unlikely(ret
> 0)) {
535 dbg_fragtree2("CRC error, mark it obsolete.\n");
536 jffs2_mark_node_obsolete(c
, tn
->fn
->raw
);
543 * Helper function for jffs2_add_older_frag_to_fragtree().
545 * Called when the new fragment that is being inserted
546 * splits a hole fragment.
548 static int split_hole(struct jffs2_sb_info
*c
, struct rb_root
*root
,
549 struct jffs2_node_frag
*newfrag
, struct jffs2_node_frag
*hole
)
551 dbg_fragtree2("fragment %#04x-%#04x splits the hole %#04x-%#04x\n",
552 newfrag
->ofs
, newfrag
->ofs
+ newfrag
->size
, hole
->ofs
, hole
->ofs
+ hole
->size
);
554 if (hole
->ofs
== newfrag
->ofs
) {
556 * Well, the new fragment actually starts at the same offset as
559 if (hole
->ofs
+ hole
->size
> newfrag
->ofs
+ newfrag
->size
) {
561 * We replace the overlapped left part of the hole by
565 dbg_fragtree2("insert fragment %#04x-%#04x and cut the left part of the hole\n",
566 newfrag
->ofs
, newfrag
->ofs
+ newfrag
->size
);
567 rb_replace_node(&hole
->rb
, &newfrag
->rb
, root
);
569 hole
->ofs
+= newfrag
->size
;
570 hole
->size
-= newfrag
->size
;
573 * We know that 'hole' should be the right hand
576 jffs2_fragtree_insert(hole
, newfrag
);
577 rb_insert_color(&hole
->rb
, root
);
580 * Ah, the new fragment is of the same size as the hole.
581 * Relace the hole by it.
583 dbg_fragtree2("insert fragment %#04x-%#04x and overwrite hole\n",
584 newfrag
->ofs
, newfrag
->ofs
+ newfrag
->size
);
585 rb_replace_node(&hole
->rb
, &newfrag
->rb
, root
);
586 jffs2_free_node_frag(hole
);
589 /* The new fragment lefts some hole space at the left */
591 struct jffs2_node_frag
* newfrag2
= NULL
;
593 if (hole
->ofs
+ hole
->size
> newfrag
->ofs
+ newfrag
->size
) {
594 /* The new frag also lefts some space at the right */
595 newfrag2
= new_fragment(NULL
, newfrag
->ofs
+
596 newfrag
->size
, hole
->ofs
+ hole
->size
597 - newfrag
->ofs
- newfrag
->size
);
598 if (unlikely(!newfrag2
)) {
599 jffs2_free_node_frag(newfrag
);
604 hole
->size
= newfrag
->ofs
- hole
->ofs
;
605 dbg_fragtree2("left the hole %#04x-%#04x at the left and inserd fragment %#04x-%#04x\n",
606 hole
->ofs
, hole
->ofs
+ hole
->size
, newfrag
->ofs
, newfrag
->ofs
+ newfrag
->size
);
608 jffs2_fragtree_insert(newfrag
, hole
);
609 rb_insert_color(&newfrag
->rb
, root
);
612 dbg_fragtree2("left the hole %#04x-%#04x at the right\n",
613 newfrag2
->ofs
, newfrag2
->ofs
+ newfrag2
->size
);
614 jffs2_fragtree_insert(newfrag2
, newfrag
);
615 rb_insert_color(&newfrag2
->rb
, root
);
623 * This function is used when we build inode. It expects the nodes are passed
624 * in the decreasing version order. The whole point of this is to improve the
625 * inodes checking on NAND: we check the nodes' data CRC only when they are not
626 * obsoleted. Previously, add_frag_to_fragtree() function was used and
627 * nodes were passed to it in the increasing version ordes and CRCs of all
628 * nodes were checked.
630 * Note: tn->fn->size shouldn't be zero.
632 * Returns 0 if the node was inserted
633 * 1 if it wasn't inserted (since it is obsolete)
634 * < 0 an if error occured
636 int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
,
637 struct jffs2_tmp_dnode_info
*tn
)
639 struct jffs2_node_frag
*this, *newfrag
;
641 struct jffs2_full_dnode
*fn
= tn
->fn
;
642 struct rb_root
*root
= &f
->fragtree
;
643 uint32_t fn_size
= fn
->size
, fn_ofs
= fn
->ofs
;
644 int err
, checked
= 0;
647 dbg_fragtree("insert fragment %#04x-%#04x, ver %u\n", fn_ofs
, fn_ofs
+ fn_size
, tn
->version
);
649 /* Skip all the nodes which are completed before this one starts */
650 this = jffs2_lookup_node_frag(root
, fn_ofs
);
652 dbg_fragtree2("'this' found %#04x-%#04x (%s)\n", this->ofs
, this->ofs
+ this->size
, this->node
? "data" : "hole");
655 lastend
= this->ofs
+ this->size
;
659 /* Detect the preliminary type of node */
660 if (fn
->size
>= PAGE_CACHE_SIZE
)
661 ref_flag
= REF_PRISTINE
;
663 ref_flag
= REF_NORMAL
;
665 /* See if we ran off the end of the root */
666 if (lastend
<= fn_ofs
) {
670 * We are going to insert the new node into the
671 * fragment tree, so check it.
673 err
= check_node(c
, f
, tn
);
679 newfrag
= new_fragment(fn
, fn_ofs
, fn_size
);
680 if (unlikely(!newfrag
))
683 err
= no_overlapping_node(c
, root
, newfrag
, this, lastend
);
684 if (unlikely(err
!= 0)) {
685 jffs2_free_node_frag(newfrag
);
697 * fn_ofs < this->ofs + this->size && fn_ofs >= this->ofs.
699 * Remember, 'this' has higher version, any non-hole node
700 * which is already in the fragtree is newer then the newly
705 * 'this' is the hole fragment, so at least the
706 * beginning of the new fragment is valid.
710 * We are going to insert the new node into the
711 * fragment tree, so check it.
714 err
= check_node(c
, f
, tn
);
715 if (unlikely(err
!= 0))
720 if (this->ofs
+ this->size
>= fn_ofs
+ fn_size
) {
721 /* We split the hole on two parts */
724 newfrag
= new_fragment(fn
, fn_ofs
, fn_size
);
725 if (unlikely(!newfrag
))
728 err
= split_hole(c
, root
, newfrag
, this);
735 * The beginning of the new fragment is valid since it
736 * overlaps the hole node.
739 ref_flag
= REF_NORMAL
;
742 newfrag
= new_fragment(fn
, fn_ofs
,
743 this->ofs
+ this->size
- fn_ofs
);
744 if (unlikely(!newfrag
))
747 if (fn_ofs
== this->ofs
) {
749 * The new node starts at the same offset as
750 * the hole and supersieds the hole.
752 dbg_fragtree2("add the new fragment instead of hole %#04x-%#04x, refcnt %d\n",
753 fn_ofs
, fn_ofs
+ this->ofs
+ this->size
- fn_ofs
, fn
->frags
);
755 rb_replace_node(&this->rb
, &newfrag
->rb
, root
);
756 jffs2_free_node_frag(this);
759 * The hole becomes shorter as its right part
760 * is supersieded by the new fragment.
762 dbg_fragtree2("reduce size of hole %#04x-%#04x to %#04x-%#04x\n",
763 this->ofs
, this->ofs
+ this->size
, this->ofs
, this->ofs
+ this->size
- newfrag
->size
);
765 dbg_fragtree2("add new fragment %#04x-%#04x, refcnt %d\n", fn_ofs
,
766 fn_ofs
+ this->ofs
+ this->size
- fn_ofs
, fn
->frags
);
768 this->size
-= newfrag
->size
;
769 jffs2_fragtree_insert(newfrag
, this);
770 rb_insert_color(&newfrag
->rb
, root
);
773 fn_ofs
+= newfrag
->size
;
774 fn_size
-= newfrag
->size
;
775 this = rb_entry(rb_next(&newfrag
->rb
),
776 struct jffs2_node_frag
, rb
);
778 dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
779 this->ofs
, this->ofs
+ this->size
, this->node
? "(data)" : "(hole)");
783 * 'This' node is not the hole so it obsoletes the new fragment
784 * either fully or partially.
786 if (this->ofs
+ this->size
>= fn_ofs
+ fn_size
) {
787 /* The new node is obsolete, drop it */
788 if (fn
->frags
== 0) {
789 dbg_fragtree2("%#04x-%#04x is obsolete, mark it obsolete\n", fn_ofs
, fn_ofs
+ fn_size
);
790 ref_flag
= REF_OBSOLETE
;
794 struct jffs2_node_frag
*new_this
;
796 /* 'This' node obsoletes the beginning of the new node */
797 dbg_fragtree2("the beginning %#04x-%#04x is obsolete\n", fn_ofs
, this->ofs
+ this->size
);
799 ref_flag
= REF_NORMAL
;
801 fn_size
-= this->ofs
+ this->size
- fn_ofs
;
802 fn_ofs
= this->ofs
+ this->size
;
803 dbg_fragtree2("now considering %#04x-%#04x\n", fn_ofs
, fn_ofs
+ fn_size
);
805 new_this
= rb_entry(rb_next(&this->rb
), struct jffs2_node_frag
, rb
);
808 * There is no next fragment. Add the rest of
809 * the new node as the right-hand child.
812 err
= check_node(c
, f
, tn
);
813 if (unlikely(err
!= 0))
819 newfrag
= new_fragment(fn
, fn_ofs
, fn_size
);
820 if (unlikely(!newfrag
))
823 dbg_fragtree2("there are no more fragments, insert %#04x-%#04x\n",
824 newfrag
->ofs
, newfrag
->ofs
+ newfrag
->size
);
825 rb_link_node(&newfrag
->rb
, &this->rb
, &this->rb
.rb_right
);
826 rb_insert_color(&newfrag
->rb
, root
);
830 dbg_fragtree2("switch to the next 'this' fragment: %#04x-%#04x %s\n",
831 this->ofs
, this->ofs
+ this->size
, this->node
? "(data)" : "(hole)");
837 BUG_ON(fn
->size
< PAGE_CACHE_SIZE
&& ref_flag
== REF_PRISTINE
);
839 if (ref_flag
== REF_OBSOLETE
) {
840 dbg_fragtree2("the node is obsolete now\n");
841 /* jffs2_mark_node_obsolete() will adjust space accounting */
842 jffs2_mark_node_obsolete(c
, fn
->raw
);
846 dbg_fragtree2("the node is \"%s\" now\n", ref_flag
== REF_NORMAL
? "REF_NORMAL" : "REF_PRISTINE");
848 /* Space accounting was adjusted at check_node_data() */
849 spin_lock(&c
->erase_completion_lock
);
850 fn
->raw
->flash_offset
= ref_offset(fn
->raw
) | ref_flag
;
851 spin_unlock(&c
->erase_completion_lock
);
856 void jffs2_set_inocache_state(struct jffs2_sb_info
*c
, struct jffs2_inode_cache
*ic
, int state
)
858 spin_lock(&c
->inocache_lock
);
860 wake_up(&c
->inocache_wq
);
861 spin_unlock(&c
->inocache_lock
);
864 /* During mount, this needs no locking. During normal operation, its
865 callers want to do other stuff while still holding the inocache_lock.
866 Rather than introducing special case get_ino_cache functions or
867 callbacks, we just let the caller do the locking itself. */
869 struct jffs2_inode_cache
*jffs2_get_ino_cache(struct jffs2_sb_info
*c
, uint32_t ino
)
871 struct jffs2_inode_cache
*ret
;
873 ret
= c
->inocache_list
[ino
% INOCACHE_HASHSIZE
];
874 while (ret
&& ret
->ino
< ino
) {
878 if (ret
&& ret
->ino
!= ino
)
884 void jffs2_add_ino_cache (struct jffs2_sb_info
*c
, struct jffs2_inode_cache
*new)
886 struct jffs2_inode_cache
**prev
;
888 spin_lock(&c
->inocache_lock
);
890 new->ino
= ++c
->highest_ino
;
892 dbg_inocache("add %p (ino #%u)\n", new, new->ino
);
894 prev
= &c
->inocache_list
[new->ino
% INOCACHE_HASHSIZE
];
896 while ((*prev
) && (*prev
)->ino
< new->ino
) {
897 prev
= &(*prev
)->next
;
902 spin_unlock(&c
->inocache_lock
);
905 void jffs2_del_ino_cache(struct jffs2_sb_info
*c
, struct jffs2_inode_cache
*old
)
907 struct jffs2_inode_cache
**prev
;
909 dbg_inocache("del %p (ino #%u)\n", old
, old
->ino
);
910 spin_lock(&c
->inocache_lock
);
912 prev
= &c
->inocache_list
[old
->ino
% INOCACHE_HASHSIZE
];
914 while ((*prev
) && (*prev
)->ino
< old
->ino
) {
915 prev
= &(*prev
)->next
;
917 if ((*prev
) == old
) {
921 /* Free it now unless it's in READING or CLEARING state, which
922 are the transitions upon read_inode() and clear_inode(). The
923 rest of the time we know nobody else is looking at it, and
924 if it's held by read_inode() or clear_inode() they'll free it
926 if (old
->state
!= INO_STATE_READING
&& old
->state
!= INO_STATE_CLEARING
)
927 jffs2_free_inode_cache(old
);
929 spin_unlock(&c
->inocache_lock
);
932 void jffs2_free_ino_caches(struct jffs2_sb_info
*c
)
935 struct jffs2_inode_cache
*this, *next
;
937 for (i
=0; i
<INOCACHE_HASHSIZE
; i
++) {
938 this = c
->inocache_list
[i
];
941 jffs2_free_inode_cache(this);
944 c
->inocache_list
[i
] = NULL
;
948 void jffs2_free_raw_node_refs(struct jffs2_sb_info
*c
)
951 struct jffs2_raw_node_ref
*this, *next
;
953 for (i
=0; i
<c
->nr_blocks
; i
++) {
954 this = c
->blocks
[i
].first_node
;
956 next
= this->next_phys
;
957 jffs2_free_raw_node_ref(this);
960 c
->blocks
[i
].first_node
= c
->blocks
[i
].last_node
= NULL
;
964 struct jffs2_node_frag
*jffs2_lookup_node_frag(struct rb_root
*fragtree
, uint32_t offset
)
966 /* The common case in lookup is that there will be a node
967 which precisely matches. So we go looking for that first */
968 struct rb_node
*next
;
969 struct jffs2_node_frag
*prev
= NULL
;
970 struct jffs2_node_frag
*frag
= NULL
;
972 dbg_fragtree2("root %p, offset %d\n", fragtree
, offset
);
974 next
= fragtree
->rb_node
;
977 frag
= rb_entry(next
, struct jffs2_node_frag
, rb
);
979 if (frag
->ofs
+ frag
->size
<= offset
) {
980 /* Remember the closest smaller match on the way down */
981 if (!prev
|| frag
->ofs
> prev
->ofs
)
983 next
= frag
->rb
.rb_right
;
984 } else if (frag
->ofs
> offset
) {
985 next
= frag
->rb
.rb_left
;
991 /* Exact match not found. Go back up looking at each parent,
992 and return the closest smaller one */
995 dbg_fragtree2("no match. Returning frag %#04x-%#04x, closest previous\n",
996 prev
->ofs
, prev
->ofs
+prev
->size
);
998 dbg_fragtree2("returning NULL, empty fragtree\n");
1003 /* Pass 'c' argument to indicate that nodes should be marked obsolete as
1005 void jffs2_kill_fragtree(struct rb_root
*root
, struct jffs2_sb_info
*c
)
1007 struct jffs2_node_frag
*frag
;
1008 struct jffs2_node_frag
*parent
;
1013 dbg_fragtree("killing\n");
1015 frag
= (rb_entry(root
->rb_node
, struct jffs2_node_frag
, rb
));
1017 if (frag
->rb
.rb_left
) {
1018 frag
= frag_left(frag
);
1021 if (frag
->rb
.rb_right
) {
1022 frag
= frag_right(frag
);
1026 if (frag
->node
&& !(--frag
->node
->frags
)) {
1027 /* Not a hole, and it's the final remaining frag
1028 of this node. Free the node */
1030 jffs2_mark_node_obsolete(c
, frag
->node
->raw
);
1032 jffs2_free_full_dnode(frag
->node
);
1034 parent
= frag_parent(frag
);
1036 if (frag_left(parent
) == frag
)
1037 parent
->rb
.rb_left
= NULL
;
1039 parent
->rb
.rb_right
= NULL
;
1042 jffs2_free_node_frag(frag
);