12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
18 #include <linux/rbtree.h>
30 dbg_dentlist(
"add dirent \"%s\", ino #%u\n", new->name, new->ino);
32 while ((*prev) && (*prev)->nhash <= new->nhash) {
33 if ((*prev)->nhash == new->nhash && !
strcmp((*prev)->name, new->name)) {
35 if (new->version < (*prev)->version) {
36 dbg_dentlist(
"Eep! Marking new dirent node obsolete, old is \"%s\", ino #%u\n",
37 (*prev)->name, (*prev)->ino);
41 dbg_dentlist(
"marking old dirent \"%s\", ino #%u obsolete\n",
42 (*prev)->name, (*prev)->ino);
43 new->next = (*prev)->next;
53 prev = &((*prev)->next);
63 dbg_fragtree(
"truncating fragtree to 0x%08x bytes\n", size);
66 if (frag && frag->
ofs != size) {
67 if (frag->
ofs+frag->
size > size) {
72 while (frag && frag->
ofs >= size) {
76 jffs2_obsolete_node_frag(c, frag);
83 frag = frag_last(list);
88 if (frag->
ofs + frag->
size < size)
94 dbg_fragtree2(
"marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
106 if (!this->
node->frags) {
108 dbg_fragtree2(
"marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
109 ref_offset(this->
node->raw), this->node->ofs, this->node->ofs+this->node->size);
113 dbg_fragtree2(
"marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
114 ref_offset(this->
node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags);
133 if (newfrag->
ofs > base->
ofs)
134 link = &base->
rb.rb_right;
135 else if (newfrag->
ofs < base->
ofs)
136 link = &base->
rb.rb_left;
138 JFFS2_ERROR(
"duplicate frag at %08x (%p,%p)\n", newfrag->
ofs, newfrag, base);
143 rb_link_node(&newfrag->
rb, &base->
rb, link);
159 JFFS2_ERROR(
"cannot allocate a jffs2_node_frag object\n");
173 if (lastend < newfrag->
node->ofs) {
177 holefrag= new_fragment(
NULL, lastend, newfrag->
node->ofs - lastend);
187 dbg_fragtree2(
"add hole frag %#04x-%#04x on the right of the new frag.\n",
188 holefrag->
ofs, holefrag->
ofs + holefrag->
size);
189 rb_link_node(&holefrag->
rb, &this->rb, &this->rb.rb_right);
191 dbg_fragtree2(
"Add hole frag %#04x-%#04x to the root of the tree.\n",
192 holefrag->
ofs, holefrag->
ofs + holefrag->
size);
204 rb_link_node(&newfrag->
rb, &this->rb, &this->rb.rb_right);
206 dbg_fragtree2(
"insert the new node at the root of the tree\n");
224 dbg_fragtree2(
"lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
225 this->ofs, this->ofs+this->size, this->
node?(
ref_offset(this->
node->raw)):0xffffffff,
this);
226 lastend = this->ofs + this->
size;
233 if (lastend <= newfrag->ofs) {
246 return no_overlapping_node(c, root, newfrag,
this, lastend);
251 this->ofs, this->ofs + this->size,
255 this->ofs, this->ofs + this->size);
260 if (newfrag->
ofs > this->ofs) {
269 if (this->ofs + this->size > newfrag->
ofs + newfrag->
size) {
278 this->ofs, this->ofs+this->size);
281 newfrag2 = new_fragment(this->
node, newfrag->
ofs + newfrag->
size,
282 this->ofs + this->size - newfrag->
ofs - newfrag->
size);
289 this->size = newfrag->
ofs - this->
ofs;
297 jffs2_fragtree_insert(newfrag,
this);
300 jffs2_fragtree_insert(newfrag2, newfrag);
306 this->size = newfrag->
ofs - this->
ofs;
309 jffs2_fragtree_insert(newfrag,
this);
314 dbg_fragtree2(
"inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
315 newfrag, newfrag->
ofs, newfrag->
ofs+newfrag->
size,
this, this->ofs, this->ofs+this->size);
319 if (newfrag->
ofs + newfrag->
size >= this->ofs+this->size) {
320 dbg_fragtree2(
"obsoleting node frag %p (%x-%x)\n",
this, this->ofs, this->ofs+this->size);
321 jffs2_obsolete_node_frag(c,
this);
323 this->ofs += newfrag->
size;
324 this->size -= newfrag->
size;
326 jffs2_fragtree_insert(
this, newfrag);
334 while ((
this =
frag_next(newfrag)) && newfrag->
ofs + newfrag->
size >= this->ofs + this->size) {
336 dbg_fragtree2(
"obsoleting node frag %p (%x-%x) and removing from tree\n",
337 this, this->ofs, this->ofs+this->size);
339 jffs2_obsolete_node_frag(c,
this);
344 if (!
this || newfrag->
ofs + newfrag->
size == this->ofs)
348 this->size = (this->ofs + this->
size) - (newfrag->
ofs + newfrag->
size);
349 this->ofs = newfrag->
ofs + newfrag->
size;
371 newfrag = new_fragment(fn, fn->
ofs, fn->
size);
374 newfrag->
node->frags = 1;
376 dbg_fragtree(
"adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
379 ret = jffs2_add_frag_to_fragtree(c, &f->
fragtree, newfrag);
426 while (ret && ret->
ino < ino) {
430 if (ret && ret->
ino != ino)
448 while ((*prev) && (*prev)->ino < new->ino) {
449 prev = &(*prev)->
next;
461 #ifdef CONFIG_JFFS2_FS_XATTR
469 while ((*prev) && (*prev)->ino < old->
ino) {
470 prev = &(*prev)->
next;
472 if ((*prev) == old) {
510 this = c->
blocks[
i].first_node;
539 if (frag->
ofs + frag->
size <= offset) {
541 if (!prev || frag->
ofs > prev->
ofs)
543 next = frag->
rb.rb_right;
544 }
else if (frag->
ofs > offset) {
545 next = frag->
rb.rb_left;
555 dbg_fragtree2(
"no match. Returning frag %#04x-%#04x, closest previous\n",
577 if (frag->
rb.rb_left) {
581 if (frag->
rb.rb_right) {
586 if (frag->
node && !(--frag->
node->frags)) {
597 parent->
rb.rb_left =
NULL;
599 parent->
rb.rb_right =
NULL;
631 dbg_noderef(
"New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
642 JFFS2_ERROR(
"Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
692 pr_crit(
"Dirty space 0x%x larger then free_size 0x%x (wasted 0x%x)\n",
731 pr_crit(
"ref %p @0x%08x is not jeb->last_node (%p @0x%08x)\n",
747 ret = __ref_totlen(c, jeb, ref);
750 if (
unlikely(ret != ref->__totlen)) {
754 pr_crit(
"Totlen for ref at %p (0x%08x-0x%08x) miscalculated as 0x%x instead of %x\n",
758 pr_crit(
"next %p (0x%08x-0x%08x)\n",
762 pr_crit(
"No next ref. jeb->last_node is %p\n",
765 pr_crit(
"jeb->wasted_size %x, dirty_size %x, used_size %x, free_size %x\n",
769 #if defined(JFFS2_DBG_DUMPS) || defined(JFFS2_DBG_PARANOIA_CHECKS)