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,
25
struct jffs2_full_dirent *new, struct jffs2_full_dirent **list) {
26
struct jffs2_full_dirent **prev = list;
28
JFFS2_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 &&
32
!strcmp((const char *) (*prev)->name,
33
(const char *) new->name)) {
35
if (new->version < (*prev)->version) {
36
JFFS2_DBG_DENTLIST("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n",
37
(*prev)->name, (*prev)->ino);
38
jffs2_mark_node_obsolete(c, new->raw);
39
jffs2_free_full_dirent(new);
41
JFFS2_DBG_DENTLIST("marking old dirent \"%s\", ino #%u bsolete\n",
42
(*prev)->name, (*prev)->ino);
43
new->next = (*prev)->next;
44
jffs2_mark_node_obsolete(c, ((*prev)->raw));
45
jffs2_free_full_dirent(*prev);
50
prev = &((*prev)->next);
56
void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
57
struct jffs2_node_frag *this) {
60
if (!this->node->frags) {
62
JFFS2_DBG_FRAGTREE2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
63
ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size);
64
jffs2_mark_node_obsolete(c, this->node->raw);
65
jffs2_free_full_dnode(this->node);
67
JFFS2_DBG_FRAGTREE2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
68
ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags);
69
mark_ref_normal(this->node->raw);
73
jffs2_free_node_frag(this);
76
static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag,
77
struct jffs2_node_frag *base) {
78
struct rb_node *parent = &base->rb;
79
struct rb_node **link = &parent;
81
JFFS2_DBG_FRAGTREE2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
85
base = rb_entry(parent, struct jffs2_node_frag, rb);
87
JFFS2_DBG_FRAGTREE2("considering frag at 0x%08x\n", base->ofs);
88
if (newfrag->ofs > base->ofs) {
89
link = &base->rb.rb_right;
90
} else if (newfrag->ofs < base->ofs) {
91
link = &base->rb.rb_left;
93
JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
98
rb_link_node(&newfrag->rb, &base->rb, link);
102
static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c,
103
struct rb_root *list, struct jffs2_node_frag *newfrag) {
104
struct jffs2_node_frag *this;
108
this = jffs2_lookup_node_frag(list, newfrag->node->ofs);
111
JFFS2_DBG_FRAGTREE2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
112
this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
113
lastend = this->ofs + this->size;
115
JFFS2_DBG_FRAGTREE2("lookup gave no frag\n");
120
if (lastend <= newfrag->ofs) {
127
if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT ==
128
newfrag->ofs >> PAGE_CACHE_SHIFT) {
130
mark_ref_normal(this->node->raw);
132
mark_ref_normal(newfrag->node->raw);
135
if (lastend < newfrag->node->ofs) {
137
struct jffs2_node_frag *holefrag = jffs2_alloc_node_frag();
139
jffs2_free_node_frag(newfrag);
142
holefrag->ofs = lastend;
143
holefrag->size = newfrag->node->ofs - lastend;
144
holefrag->node = NULL;
150
JFFS2_DBG_FRAGTREE2("adding hole frag (%p) on right of node at (%p)\n", holefrag, this);
151
rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
153
JFFS2_DBG_FRAGTREE2("adding hole frag (%p) at root of tree\n", holefrag);
154
rb_link_node(&holefrag->rb, NULL, &list->rb_node);
156
rb_insert_color(&holefrag->rb, list);
164
JFFS2_DBG_FRAGTREE2("adding new frag (%p) on right of node at (%p)\n", newfrag, this);
165
rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
167
JFFS2_DBG_FRAGTREE2("adding new frag (%p) at root of tree\n", newfrag);
168
rb_link_node(&newfrag->rb, NULL, &list->rb_node);
170
rb_insert_color(&newfrag->rb, list);
174
JFFS2_DBG_FRAGTREE2("dealing with frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
175
this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
182
if (newfrag->ofs > this->ofs) {
189
mark_ref_normal(newfrag->node->raw);
191
mark_ref_normal(this->node->raw);
194
if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
196
struct jffs2_node_frag *newfrag2 = jffs2_alloc_node_frag();
198
jffs2_free_node_frag(newfrag);
202
newfrag2->ofs = newfrag->ofs + newfrag->size;
203
newfrag2->size = (this->ofs+this->size) - newfrag2->ofs;
204
newfrag2->node = this->node;
210
this->size = newfrag->ofs - this->ofs;
219
jffs2_fragtree_insert(newfrag, this);
220
rb_insert_color(&newfrag->rb, list);
222
jffs2_fragtree_insert(newfrag2, newfrag);
223
rb_insert_color(&newfrag2->rb, list);
228
this->size = newfrag->ofs - this->ofs;
231
jffs2_fragtree_insert(newfrag, this);
232
rb_insert_color(&newfrag->rb, list);
237
JFFS2_DBG_FRAGTREE2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
238
newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
240
rb_replace_node(&this->rb, &newfrag->rb, list);
242
if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
243
JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
244
jffs2_obsolete_node_frag(c, this);
246
this->ofs += newfrag->size;
247
this->size -= newfrag->size;
249
jffs2_fragtree_insert(this, newfrag);
250
rb_insert_color(&this->rb, list);
257
while ((this = frag_next(newfrag)) &&
258
newfrag->ofs + newfrag->size >= this->ofs + this->size) {
260
JFFS2_DBG_FRAGTREE2("obsoleting node frag %p (%x-%x) and removing from tree\n",
261
this, this->ofs, this->ofs+this->size);
262
rb_erase(&this->rb, list);
263
jffs2_obsolete_node_frag(c, this);
269
if (!this || newfrag->ofs + newfrag->size == this->ofs) {
273
this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
274
this->ofs = newfrag->ofs + newfrag->size;
278
mark_ref_normal(this->node->raw);
280
mark_ref_normal(newfrag->node->raw);
289
int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c,
290
struct jffs2_inode_info *f, struct jffs2_full_dnode *fn) {
292
struct jffs2_node_frag *newfrag;
294
if (unlikely(!fn->size)) {
298
newfrag = jffs2_alloc_node_frag();
299
if (unlikely(!newfrag)) {
303
JFFS2_DBG_FRAGTREE("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
304
fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
306
newfrag->ofs = fn->ofs;
307
newfrag->size = fn->size;
309
newfrag->node->frags = 1;
311
ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
319
if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
320
struct jffs2_node_frag *prev = frag_prev(newfrag);
322
mark_ref_normal(fn->raw);
325
mark_ref_normal(prev->node->raw);
329
if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
330
struct jffs2_node_frag *next = frag_next(newfrag);
333
mark_ref_normal(fn->raw);
335
mark_ref_normal(next->node->raw);
339
jffs2_dbg_fragtree_paranoia_check_nolock(f);
340
jffs2_dbg_dump_fragtree_nolock(f);
345
void jffs2_set_inocache_state(struct jffs2_sb_info *c,
346
struct jffs2_inode_cache *ic, int state) {
347
spin_lock(&c->inocache_lock);
349
wake_up(&c->inocache_wq);
350
spin_unlock(&c->inocache_lock);
360
struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c,
362
struct jffs2_inode_cache *ret;
364
ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
365
while (ret && ret->ino < ino) {
369
if (ret && ret->ino != ino) {
376
void jffs2_add_ino_cache (struct jffs2_sb_info *c,
377
struct jffs2_inode_cache *new) {
378
struct jffs2_inode_cache **prev;
380
spin_lock(&c->inocache_lock);
382
new->ino = ++c->highest_ino;
385
JFFS2_DBG_INOCACHE("add %p (ino #%u)\n", new, new->ino);
387
prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
389
while ((*prev) && (*prev)->ino < new->ino) {
390
prev = &(*prev)->next;
395
spin_unlock(&c->inocache_lock);
398
void jffs2_del_ino_cache(struct jffs2_sb_info *c,
399
struct jffs2_inode_cache *old) {
400
struct jffs2_inode_cache **prev;
402
JFFS2_DBG_INOCACHE("del %p (ino #%u)\n", old, old->ino);
403
spin_lock(&c->inocache_lock);
405
prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
407
while ((*prev) && (*prev)->ino < old->ino) {
408
prev = &(*prev)->next;
410
if ((*prev) == old) {
420
if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING) {
421
jffs2_free_inode_cache(old);
424
spin_unlock(&c->inocache_lock);
427
void jffs2_free_ino_caches(struct jffs2_sb_info *c) {
429
struct jffs2_inode_cache *this, *next;
431
for (i=0; i<INOCACHE_HASHSIZE; i++) {
432
this = c->inocache_list[i];
435
jffs2_free_inode_cache(this);
438
c->inocache_list[i] = NULL;
442
void jffs2_free_raw_node_refs(struct jffs2_sb_info *c) {
444
struct jffs2_raw_node_ref *this, *next;
446
for (i=0; i<c->nr_blocks; i++) {
447
this = c->blocks[i].first_node;
449
next = this->next_phys;
450
jffs2_free_raw_node_ref(this);
453
c->blocks[i].first_node = c->blocks[i].last_node = NULL;
457
struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree,
462
struct rb_node *next;
463
struct jffs2_node_frag *prev = NULL;
464
struct jffs2_node_frag *frag = NULL;
466
JFFS2_DBG_FRAGTREE2("root %p, offset %d\n", fragtree, offset);
468
next = fragtree->rb_node;
471
frag = rb_entry(next, struct jffs2_node_frag, rb);
473
JFFS2_DBG_FRAGTREE2("considering frag %#04x-%#04x (%p). left %p, right %p\n",
474
frag->ofs, frag->ofs+frag->size, frag, frag->rb.rb_left, frag->rb.rb_right);
475
if (frag->ofs + frag->size <= offset) {
476
JFFS2_DBG_FRAGTREE2("going right from frag %#04x-%#04x, before the region we care about\n",
477
frag->ofs, frag->ofs+frag->size);
479
if (!prev || frag->ofs > prev->ofs) {
482
next = frag->rb.rb_right;
483
} else if (frag->ofs > offset) {
484
JFFS2_DBG_FRAGTREE2("going left from frag %#04x-%#04x, after the region we care about\n",
485
frag->ofs, frag->ofs+frag->size);
486
next = frag->rb.rb_left;
488
JFFS2_DBG_FRAGTREE2("returning frag %#04x-%#04x, matched\n",
489
frag->ofs, frag->ofs+frag->size);
499
JFFS2_DBG_FRAGTREE2("no match. Returning frag %#04x-%#04x, closest previous\n",
500
prev->ofs, prev->ofs+prev->size);
502
JFFS2_DBG_FRAGTREE2("returning NULL, empty fragtree\n");
512
void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c) {
513
struct jffs2_node_frag *frag;
514
struct jffs2_node_frag *parent;
516
if (!root->rb_node) {
520
JFFS2_DBG_FRAGTREE("killing\n");
522
frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
524
if (frag->rb.rb_left) {
525
JFFS2_DBG_FRAGTREE2("going left from frag (%p) %#04x-%#04x\n",
526
frag, frag->ofs, frag->ofs+frag->size);
527
frag = frag_left(frag);
530
if (frag->rb.rb_right) {
531
JFFS2_DBG_FRAGTREE2("going right from frag (%p) %#04x-%#04x\n",
532
frag, frag->ofs, frag->ofs+frag->size);
533
frag = frag_right(frag);
537
JFFS2_DBG_FRAGTREE2("frag %#04x-%#04x: node %p, frags %d\n",
538
frag->ofs, frag->ofs+frag->size, frag->node, frag->node?frag->node->frags:0);
540
if (frag->node && !(--frag->node->frags)) {
545
jffs2_mark_node_obsolete(c, frag->node->raw);
548
jffs2_free_full_dnode(frag->node);
550
parent = frag_parent(frag);
552
if (frag_left(parent) == frag) {
553
parent->rb.rb_left = NULL;
555
parent->rb.rb_right = NULL;
559
jffs2_free_node_frag(frag);