[MTD] [OneNAND] Exit loop only when column start with 0
[deliverable/linux.git] / fs / jffs2 / nodelist.c
CommitLineData
1da177e4
LT
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 *
182ec4ee 10 * $Id: nodelist.c,v 1.115 2005/11/07 11:14:40 gleixner Exp $
1da177e4
LT
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
90a18fab
AB
24static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
25 struct jffs2_node_frag *this);
26
1da177e4
LT
27void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
28{
29 struct jffs2_full_dirent **prev = list;
182ec4ee 30
733802d9 31 dbg_dentlist("add dirent \"%s\", ino #%u\n", new->name, new->ino);
1da177e4
LT
32
33 while ((*prev) && (*prev)->nhash <= new->nhash) {
34 if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
35 /* Duplicate. Free one */
36 if (new->version < (*prev)->version) {
733802d9 37 dbg_dentlist("Eep! Marking new dirent node is obsolete, old is \"%s\", ino #%u\n",
e0d60137 38 (*prev)->name, (*prev)->ino);
1da177e4
LT
39 jffs2_mark_node_obsolete(c, new->raw);
40 jffs2_free_full_dirent(new);
41 } else {
733802d9 42 dbg_dentlist("marking old dirent \"%s\", ino #%u bsolete\n",
e0d60137 43 (*prev)->name, (*prev)->ino);
1da177e4
LT
44 new->next = (*prev)->next;
45 jffs2_mark_node_obsolete(c, ((*prev)->raw));
46 jffs2_free_full_dirent(*prev);
47 *prev = new;
48 }
e0d60137 49 return;
1da177e4
LT
50 }
51 prev = &((*prev)->next);
52 }
53 new->next = *prev;
54 *prev = new;
1da177e4
LT
55}
56
1e900979
AB
57void jffs2_truncate_fragtree(struct jffs2_sb_info *c, struct rb_root *list, uint32_t size)
58{
59 struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size);
60
733802d9 61 dbg_fragtree("truncating fragtree to 0x%08x bytes\n", size);
1e900979
AB
62
63 /* We know frag->ofs <= size. That's what lookup does for us */
64 if (frag && frag->ofs != size) {
1e0da3cb 65 if (frag->ofs+frag->size > size) {
1e900979
AB
66 frag->size = size - frag->ofs;
67 }
68 frag = frag_next(frag);
69 }
70 while (frag && frag->ofs >= size) {
71 struct jffs2_node_frag *next = frag_next(frag);
72
1e900979
AB
73 frag_erase(frag, list);
74 jffs2_obsolete_node_frag(c, frag);
75 frag = next;
76 }
1e0da3cb
AB
77
78 if (size == 0)
79 return;
80
182ec4ee 81 /*
1e0da3cb
AB
82 * If the last fragment starts at the RAM page boundary, it is
83 * REF_PRISTINE irrespective of its size.
84 */
85 frag = frag_last(list);
f0507530 86 if (frag->node && (frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
733802d9 87 dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
182ec4ee 88 frag->ofs, frag->ofs + frag->size);
1e0da3cb
AB
89 frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
90 }
1e900979
AB
91}
92
90a18fab
AB
93static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
94 struct jffs2_node_frag *this)
1da177e4 95{
f97117d1
AB
96 if (this->node) {
97 this->node->frags--;
98 if (!this->node->frags) {
99 /* The node has no valid frags left. It's totally obsoleted */
733802d9 100 dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
e0d60137 101 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size);
f97117d1
AB
102 jffs2_mark_node_obsolete(c, this->node->raw);
103 jffs2_free_full_dnode(this->node);
104 } else {
733802d9 105 dbg_fragtree2("marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
e0d60137 106 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, this->node->frags);
f97117d1
AB
107 mark_ref_normal(this->node->raw);
108 }
182ec4ee 109
f97117d1
AB
110 }
111 jffs2_free_node_frag(this);
1da177e4
LT
112}
113
f97117d1 114static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
1da177e4 115{
f97117d1
AB
116 struct rb_node *parent = &base->rb;
117 struct rb_node **link = &parent;
9dee7503 118
733802d9 119 dbg_fragtree2("insert frag (0x%04x-0x%04x)\n", newfrag->ofs, newfrag->ofs + newfrag->size);
9dee7503 120
f97117d1
AB
121 while (*link) {
122 parent = *link;
123 base = rb_entry(parent, struct jffs2_node_frag, rb);
182ec4ee 124
f97117d1
AB
125 if (newfrag->ofs > base->ofs)
126 link = &base->rb.rb_right;
127 else if (newfrag->ofs < base->ofs)
128 link = &base->rb.rb_left;
9dee7503 129 else {
e0d60137 130 JFFS2_ERROR("duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
f97117d1 131 BUG();
9dee7503 132 }
1da177e4 133 }
f97117d1
AB
134
135 rb_link_node(&newfrag->rb, &base->rb, link);
1da177e4
LT
136}
137
1e0da3cb
AB
138/*
139 * Allocate and initializes a new fragment.
140 */
858119e1 141static struct jffs2_node_frag * new_fragment(struct jffs2_full_dnode *fn, uint32_t ofs, uint32_t size)
1e0da3cb
AB
142{
143 struct jffs2_node_frag *newfrag;
182ec4ee 144
1e0da3cb
AB
145 newfrag = jffs2_alloc_node_frag();
146 if (likely(newfrag)) {
147 newfrag->ofs = ofs;
148 newfrag->size = size;
149 newfrag->node = fn;
150 } else {
151 JFFS2_ERROR("cannot allocate a jffs2_node_frag object\n");
152 }
153
154 return newfrag;
155}
156
157/*
158 * Called when there is no overlapping fragment exist. Inserts a hole before the new
159 * fragment and inserts the new fragment to the fragtree.
160 */
161static int no_overlapping_node(struct jffs2_sb_info *c, struct rb_root *root,
162 struct jffs2_node_frag *newfrag,
163 struct jffs2_node_frag *this, uint32_t lastend)
164{
165 if (lastend < newfrag->node->ofs) {
166 /* put a hole in before the new fragment */
167 struct jffs2_node_frag *holefrag;
168
169 holefrag= new_fragment(NULL, lastend, newfrag->node->ofs - lastend);
170 if (unlikely(!holefrag)) {
171 jffs2_free_node_frag(newfrag);
172 return -ENOMEM;
173 }
174
175 if (this) {
182ec4ee 176 /* By definition, the 'this' node has no right-hand child,
1e0da3cb
AB
177 because there are no frags with offset greater than it.
178 So that's where we want to put the hole */
733802d9 179 dbg_fragtree2("add hole frag %#04x-%#04x on the right of the new frag.\n",
1e0da3cb
AB
180 holefrag->ofs, holefrag->ofs + holefrag->size);
181 rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
182 } else {
733802d9 183 dbg_fragtree2("Add hole frag %#04x-%#04x to the root of the tree.\n",
1e0da3cb
AB
184 holefrag->ofs, holefrag->ofs + holefrag->size);
185 rb_link_node(&holefrag->rb, NULL, &root->rb_node);
186 }
187 rb_insert_color(&holefrag->rb, root);
188 this = holefrag;
189 }
182ec4ee 190
1e0da3cb 191 if (this) {
182ec4ee 192 /* By definition, the 'this' node has no right-hand child,
1e0da3cb
AB
193 because there are no frags with offset greater than it.
194 So that's where we want to put new fragment */
733802d9 195 dbg_fragtree2("add the new node at the right\n");
182ec4ee 196 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
1e0da3cb 197 } else {
733802d9 198 dbg_fragtree2("insert the new node at the root of the tree\n");
1e0da3cb
AB
199 rb_link_node(&newfrag->rb, NULL, &root->rb_node);
200 }
201 rb_insert_color(&newfrag->rb, root);
202
203 return 0;
204}
205
f97117d1 206/* Doesn't set inode->i_size */
1e0da3cb 207static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *root, struct jffs2_node_frag *newfrag)
1da177e4 208{
f97117d1
AB
209 struct jffs2_node_frag *this;
210 uint32_t lastend;
1da177e4 211
f97117d1 212 /* Skip all the nodes which are completed before this one starts */
1e0da3cb 213 this = jffs2_lookup_node_frag(root, newfrag->node->ofs);
1da177e4 214
f97117d1 215 if (this) {
733802d9 216 dbg_fragtree2("lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
e0d60137 217 this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this);
f97117d1
AB
218 lastend = this->ofs + this->size;
219 } else {
733802d9 220 dbg_fragtree2("lookup gave no frag\n");
f97117d1 221 lastend = 0;
1da177e4 222 }
182ec4ee 223
1e0da3cb 224 /* See if we ran off the end of the fragtree */
f97117d1
AB
225 if (lastend <= newfrag->ofs) {
226 /* We did */
227
228 /* Check if 'this' node was on the same page as the new node.
229 If so, both 'this' and the new node get marked REF_NORMAL so
230 the GC can take a look.
231 */
232 if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
233 if (this->node)
234 mark_ref_normal(this->node->raw);
235 mark_ref_normal(newfrag->node->raw);
236 }
1da177e4 237
1e0da3cb 238 return no_overlapping_node(c, root, newfrag, this, lastend);
dae6227f 239 }
dae6227f 240
1e0da3cb 241 if (this->node)
733802d9 242 dbg_fragtree2("dealing with frag %u-%u, phys %#08x(%d).\n",
1e0da3cb
AB
243 this->ofs, this->ofs + this->size,
244 ref_offset(this->node->raw), ref_flags(this->node->raw));
245 else
733802d9 246 dbg_fragtree2("dealing with hole frag %u-%u.\n",
1e0da3cb 247 this->ofs, this->ofs + this->size);
dae6227f 248
f97117d1 249 /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
182ec4ee 250 * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
dae6227f 251 */
f97117d1
AB
252 if (newfrag->ofs > this->ofs) {
253 /* This node isn't completely obsoleted. The start of it remains valid */
254
255 /* Mark the new node and the partially covered node REF_NORMAL -- let
256 the GC take a look at them */
257 mark_ref_normal(newfrag->node->raw);
258 if (this->node)
259 mark_ref_normal(this->node->raw);
260
261 if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
262 /* The new node splits 'this' frag into two */
1e0da3cb
AB
263 struct jffs2_node_frag *newfrag2;
264
f97117d1 265 if (this->node)
733802d9 266 dbg_fragtree2("split old frag 0x%04x-0x%04x, phys 0x%08x\n",
e0d60137 267 this->ofs, this->ofs+this->size, ref_offset(this->node->raw));
182ec4ee 268 else
733802d9 269 dbg_fragtree2("split old hole frag 0x%04x-0x%04x\n",
8d5df409 270 this->ofs, this->ofs+this->size);
182ec4ee 271
f97117d1 272 /* New second frag pointing to this's node */
1e0da3cb
AB
273 newfrag2 = new_fragment(this->node, newfrag->ofs + newfrag->size,
274 this->ofs + this->size - newfrag->ofs - newfrag->size);
275 if (unlikely(!newfrag2))
276 return -ENOMEM;
f97117d1
AB
277 if (this->node)
278 this->node->frags++;
279
280 /* Adjust size of original 'this' */
281 this->size = newfrag->ofs - this->ofs;
282
283 /* Now, we know there's no node with offset
284 greater than this->ofs but smaller than
285 newfrag2->ofs or newfrag->ofs, for obvious
286 reasons. So we can do a tree insert from
287 'this' to insert newfrag, and a tree insert
288 from newfrag to insert newfrag2. */
289 jffs2_fragtree_insert(newfrag, this);
1e0da3cb 290 rb_insert_color(&newfrag->rb, root);
182ec4ee 291
f97117d1 292 jffs2_fragtree_insert(newfrag2, newfrag);
1e0da3cb 293 rb_insert_color(&newfrag2->rb, root);
182ec4ee 294
f97117d1 295 return 0;
dae6227f 296 }
f97117d1
AB
297 /* New node just reduces 'this' frag in size, doesn't split it */
298 this->size = newfrag->ofs - this->ofs;
dae6227f 299
f97117d1
AB
300 /* Again, we know it lives down here in the tree */
301 jffs2_fragtree_insert(newfrag, this);
1e0da3cb 302 rb_insert_color(&newfrag->rb, root);
f97117d1 303 } else {
182ec4ee 304 /* New frag starts at the same point as 'this' used to. Replace
f97117d1 305 it in the tree without doing a delete and insertion */
733802d9 306 dbg_fragtree2("inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
e0d60137 307 newfrag, newfrag->ofs, newfrag->ofs+newfrag->size, this, this->ofs, this->ofs+this->size);
182ec4ee 308
1e0da3cb 309 rb_replace_node(&this->rb, &newfrag->rb, root);
182ec4ee 310
f97117d1 311 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
733802d9 312 dbg_fragtree2("obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size);
f97117d1 313 jffs2_obsolete_node_frag(c, this);
dae6227f 314 } else {
f97117d1
AB
315 this->ofs += newfrag->size;
316 this->size -= newfrag->size;
317
318 jffs2_fragtree_insert(this, newfrag);
1e0da3cb 319 rb_insert_color(&this->rb, root);
f97117d1 320 return 0;
dae6227f 321 }
dae6227f 322 }
f97117d1 323 /* OK, now we have newfrag added in the correct place in the tree, but
182ec4ee 324 frag_next(newfrag) may be a fragment which is overlapped by it
f97117d1
AB
325 */
326 while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
327 /* 'this' frag is obsoleted completely. */
733802d9 328 dbg_fragtree2("obsoleting node frag %p (%x-%x) and removing from tree\n",
e0d60137 329 this, this->ofs, this->ofs+this->size);
1e0da3cb 330 rb_erase(&this->rb, root);
f97117d1 331 jffs2_obsolete_node_frag(c, this);
dae6227f 332 }
182ec4ee 333 /* Now we're pointing at the first frag which isn't totally obsoleted by
f97117d1 334 the new frag */
dae6227f 335
1e0da3cb 336 if (!this || newfrag->ofs + newfrag->size == this->ofs)
f97117d1 337 return 0;
1e0da3cb 338
f97117d1
AB
339 /* Still some overlap but we don't need to move it in the tree */
340 this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
341 this->ofs = newfrag->ofs + newfrag->size;
342
343 /* And mark them REF_NORMAL so the GC takes a look at them */
344 if (this->node)
345 mark_ref_normal(this->node->raw);
346 mark_ref_normal(newfrag->node->raw);
dae6227f
AB
347
348 return 0;
349}
350
182ec4ee 351/*
1e0da3cb
AB
352 * Given an inode, probably with existing tree of fragments, add the new node
353 * to the fragment tree.
dae6227f 354 */
f97117d1 355int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
dae6227f 356{
f97117d1
AB
357 int ret;
358 struct jffs2_node_frag *newfrag;
dae6227f 359
f97117d1
AB
360 if (unlikely(!fn->size))
361 return 0;
dae6227f 362
1e0da3cb 363 newfrag = new_fragment(fn, fn->ofs, fn->size);
f97117d1
AB
364 if (unlikely(!newfrag))
365 return -ENOMEM;
1e0da3cb 366 newfrag->node->frags = 1;
1da177e4 367
733802d9 368 dbg_fragtree("adding node %#04x-%#04x @0x%08x on flash, newfrag *%p\n",
e0d60137 369 fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
182ec4ee 370
f97117d1
AB
371 ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
372 if (unlikely(ret))
373 return ret;
374
375 /* If we now share a page with other nodes, mark either previous
376 or next node REF_NORMAL, as appropriate. */
377 if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
378 struct jffs2_node_frag *prev = frag_prev(newfrag);
379
380 mark_ref_normal(fn->raw);
182ec4ee 381 /* If we don't start at zero there's _always_ a previous */
f97117d1
AB
382 if (prev->node)
383 mark_ref_normal(prev->node->raw);
384 }
1da177e4 385
f97117d1
AB
386 if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
387 struct jffs2_node_frag *next = frag_next(newfrag);
182ec4ee 388
f97117d1
AB
389 if (next) {
390 mark_ref_normal(fn->raw);
391 if (next->node)
392 mark_ref_normal(next->node->raw);
1da177e4 393 }
1da177e4 394 }
f97117d1 395 jffs2_dbg_fragtree_paranoia_check_nolock(f);
1e0da3cb
AB
396
397 return 0;
398}
399
1da177e4
LT
400void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
401{
402 spin_lock(&c->inocache_lock);
403 ic->state = state;
404 wake_up(&c->inocache_wq);
405 spin_unlock(&c->inocache_lock);
406}
407
408/* During mount, this needs no locking. During normal operation, its
409 callers want to do other stuff while still holding the inocache_lock.
182ec4ee 410 Rather than introducing special case get_ino_cache functions or
1da177e4 411 callbacks, we just let the caller do the locking itself. */
182ec4ee 412
1da177e4
LT
413struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
414{
415 struct jffs2_inode_cache *ret;
416
1da177e4
LT
417 ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
418 while (ret && ret->ino < ino) {
419 ret = ret->next;
420 }
182ec4ee 421
1da177e4
LT
422 if (ret && ret->ino != ino)
423 ret = NULL;
424
1da177e4
LT
425 return ret;
426}
427
428void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
429{
430 struct jffs2_inode_cache **prev;
7d27c814 431
1da177e4 432 spin_lock(&c->inocache_lock);
7d27c814
TG
433 if (!new->ino)
434 new->ino = ++c->highest_ino;
435
733802d9 436 dbg_inocache("add %p (ino #%u)\n", new, new->ino);
7d27c814 437
1da177e4
LT
438 prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
439
440 while ((*prev) && (*prev)->ino < new->ino) {
441 prev = &(*prev)->next;
442 }
443 new->next = *prev;
444 *prev = new;
445
446 spin_unlock(&c->inocache_lock);
447}
448
449void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
450{
451 struct jffs2_inode_cache **prev;
e0d60137 452
355ed4e1
KK
453#ifdef CONFIG_JFFS2_FS_XATTR
454 BUG_ON(old->xref);
455#endif
733802d9 456 dbg_inocache("del %p (ino #%u)\n", old, old->ino);
1da177e4 457 spin_lock(&c->inocache_lock);
182ec4ee 458
1da177e4 459 prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
182ec4ee 460
1da177e4
LT
461 while ((*prev) && (*prev)->ino < old->ino) {
462 prev = &(*prev)->next;
463 }
464 if ((*prev) == old) {
465 *prev = old->next;
466 }
467
67e345d1
DW
468 /* Free it now unless it's in READING or CLEARING state, which
469 are the transitions upon read_inode() and clear_inode(). The
182ec4ee 470 rest of the time we know nobody else is looking at it, and
67e345d1
DW
471 if it's held by read_inode() or clear_inode() they'll free it
472 for themselves. */
473 if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
474 jffs2_free_inode_cache(old);
475
1da177e4
LT
476 spin_unlock(&c->inocache_lock);
477}
478
479void jffs2_free_ino_caches(struct jffs2_sb_info *c)
480{
481 int i;
482 struct jffs2_inode_cache *this, *next;
182ec4ee 483
1da177e4
LT
484 for (i=0; i<INOCACHE_HASHSIZE; i++) {
485 this = c->inocache_list[i];
486 while (this) {
487 next = this->next;
aa98d7cf 488 jffs2_xattr_free_inode(c, this);
1da177e4
LT
489 jffs2_free_inode_cache(this);
490 this = next;
491 }
492 c->inocache_list[i] = NULL;
493 }
494}
495
496void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
497{
498 int i;
499 struct jffs2_raw_node_ref *this, *next;
500
501 for (i=0; i<c->nr_blocks; i++) {
502 this = c->blocks[i].first_node;
2f785402 503 while (this) {
9bfeb691
DW
504 if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE)
505 next = this[REFS_PER_BLOCK].next_in_ino;
506 else
507 next = NULL;
508
509 jffs2_free_refblock(this);
1da177e4
LT
510 this = next;
511 }
512 c->blocks[i].first_node = c->blocks[i].last_node = NULL;
513 }
514}
182ec4ee 515
1da177e4
LT
516struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
517{
182ec4ee 518 /* The common case in lookup is that there will be a node
1da177e4
LT
519 which precisely matches. So we go looking for that first */
520 struct rb_node *next;
521 struct jffs2_node_frag *prev = NULL;
522 struct jffs2_node_frag *frag = NULL;
523
733802d9 524 dbg_fragtree2("root %p, offset %d\n", fragtree, offset);
1da177e4
LT
525
526 next = fragtree->rb_node;
527
528 while(next) {
529 frag = rb_entry(next, struct jffs2_node_frag, rb);
530
1da177e4 531 if (frag->ofs + frag->size <= offset) {
1da177e4
LT
532 /* Remember the closest smaller match on the way down */
533 if (!prev || frag->ofs > prev->ofs)
534 prev = frag;
535 next = frag->rb.rb_right;
536 } else if (frag->ofs > offset) {
1da177e4
LT
537 next = frag->rb.rb_left;
538 } else {
1da177e4
LT
539 return frag;
540 }
541 }
542
543 /* Exact match not found. Go back up looking at each parent,
544 and return the closest smaller one */
545
546 if (prev)
733802d9 547 dbg_fragtree2("no match. Returning frag %#04x-%#04x, closest previous\n",
e0d60137 548 prev->ofs, prev->ofs+prev->size);
182ec4ee 549 else
733802d9 550 dbg_fragtree2("returning NULL, empty fragtree\n");
182ec4ee 551
1da177e4
LT
552 return prev;
553}
554
555/* Pass 'c' argument to indicate that nodes should be marked obsolete as
556 they're killed. */
557void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
558{
559 struct jffs2_node_frag *frag;
560 struct jffs2_node_frag *parent;
561
562 if (!root->rb_node)
563 return;
564
733802d9 565 dbg_fragtree("killing\n");
182ec4ee 566
1da177e4 567 frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
1da177e4
LT
568 while(frag) {
569 if (frag->rb.rb_left) {
1da177e4
LT
570 frag = frag_left(frag);
571 continue;
572 }
573 if (frag->rb.rb_right) {
1da177e4
LT
574 frag = frag_right(frag);
575 continue;
576 }
577
1da177e4 578 if (frag->node && !(--frag->node->frags)) {
182ec4ee 579 /* Not a hole, and it's the final remaining frag
1da177e4
LT
580 of this node. Free the node */
581 if (c)
582 jffs2_mark_node_obsolete(c, frag->node->raw);
182ec4ee 583
1da177e4
LT
584 jffs2_free_full_dnode(frag->node);
585 }
586 parent = frag_parent(frag);
587 if (parent) {
588 if (frag_left(parent) == frag)
589 parent->rb.rb_left = NULL;
182ec4ee 590 else
1da177e4
LT
591 parent->rb.rb_right = NULL;
592 }
593
594 jffs2_free_node_frag(frag);
595 frag = parent;
596
597 cond_resched();
598 }
599}
f1f9671b 600
2f785402
DW
601struct jffs2_raw_node_ref *jffs2_link_node_ref(struct jffs2_sb_info *c,
602 struct jffs2_eraseblock *jeb,
603 uint32_t ofs, uint32_t len,
604 struct jffs2_inode_cache *ic)
f1f9671b 605{
2f785402
DW
606 struct jffs2_raw_node_ref *ref;
607
9bfeb691
DW
608 BUG_ON(!jeb->allocated_refs);
609 jeb->allocated_refs--;
610
611 ref = jeb->last_node;
612
613 dbg_noderef("Last node at %p is (%08x,%p)\n", ref, ref->flash_offset,
614 ref->next_in_ino);
615
616 while (ref->flash_offset != REF_EMPTY_NODE) {
617 if (ref->flash_offset == REF_LINK_NODE)
618 ref = ref->next_in_ino;
619 else
620 ref++;
2f785402
DW
621 }
622
9bfeb691
DW
623 dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
624 ref->flash_offset, ofs, ref->next_in_ino, len);
625
2f785402
DW
626 ref->flash_offset = ofs;
627
9bfeb691 628 if (!jeb->first_node) {
f1f9671b 629 jeb->first_node = ref;
9bfeb691
DW
630 BUG_ON(ref_offset(ref) != jeb->offset);
631 } else if (unlikely(ref_offset(ref) != jeb->offset + c->sector_size - jeb->free_size)) {
632 uint32_t last_len = ref_totlen(c, jeb, jeb->last_node);
633
634 JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
635 ref, ref_offset(ref), ref_offset(ref)+len,
636 ref_offset(jeb->last_node),
637 ref_offset(jeb->last_node)+last_len);
638 BUG();
ca89a517 639 }
f1f9671b
DW
640 jeb->last_node = ref;
641
fcb75787
DW
642 if (ic) {
643 ref->next_in_ino = ic->nodes;
644 ic->nodes = ref;
645 } else {
646 ref->next_in_ino = NULL;
647 }
648
f1f9671b
DW
649 switch(ref_flags(ref)) {
650 case REF_UNCHECKED:
651 c->unchecked_size += len;
652 jeb->unchecked_size += len;
653 break;
654
655 case REF_NORMAL:
656 case REF_PRISTINE:
657 c->used_size += len;
658 jeb->used_size += len;
659 break;
660
661 case REF_OBSOLETE:
662 c->dirty_size += len;
3b79673c 663 jeb->dirty_size += len;
f1f9671b
DW
664 break;
665 }
666 c->free_size -= len;
667 jeb->free_size -= len;
668
ca89a517
DW
669#ifdef TEST_TOTLEN
670 /* Set (and test) __totlen field... for now */
671 ref->__totlen = len;
672 ref_totlen(c, jeb, ref);
673#endif
2f785402 674 return ref;
f1f9671b 675}
68270995 676
2f785402 677/* No locking, no reservation of 'ref'. Do not use on a live file system */
68270995
DW
678int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
679 uint32_t size)
680{
ca89a517
DW
681 if (!size)
682 return 0;
9bfeb691
DW
683 if (unlikely(size > jeb->free_size)) {
684 printk(KERN_CRIT "Dirty space 0x%x larger then free_size 0x%x (wasted 0x%x)\n",
685 size, jeb->free_size, jeb->wasted_size);
ca89a517
DW
686 BUG();
687 }
9bfeb691 688 /* REF_EMPTY_NODE is !obsolete, so that works OK */
2ebf09c2 689 if (jeb->last_node && ref_obsolete(jeb->last_node)) {
ca89a517
DW
690#ifdef TEST_TOTLEN
691 jeb->last_node->__totlen += size;
692#endif
693 c->dirty_size += size;
694 c->free_size -= size;
695 jeb->dirty_size += size;
696 jeb->free_size -= size;
697 } else {
2f785402
DW
698 uint32_t ofs = jeb->offset + c->sector_size - jeb->free_size;
699 ofs |= REF_OBSOLETE;
ca89a517 700
2f785402 701 jffs2_link_node_ref(c, jeb, ofs, size, NULL);
ca89a517 702 }
68270995
DW
703
704 return 0;
705}
ca89a517
DW
706
707/* Calculate totlen from surrounding nodes or eraseblock */
708static inline uint32_t __ref_totlen(struct jffs2_sb_info *c,
709 struct jffs2_eraseblock *jeb,
710 struct jffs2_raw_node_ref *ref)
711{
712 uint32_t ref_end;
99988f7b 713 struct jffs2_raw_node_ref *next_ref = ref_next(ref);
ca89a517 714
99988f7b
DW
715 if (next_ref)
716 ref_end = ref_offset(next_ref);
ca89a517
DW
717 else {
718 if (!jeb)
719 jeb = &c->blocks[ref->flash_offset / c->sector_size];
720
721 /* Last node in block. Use free_space */
9bfeb691 722 if (unlikely(ref != jeb->last_node)) {
ca89a517
DW
723 printk(KERN_CRIT "ref %p @0x%08x is not jeb->last_node (%p @0x%08x)\n",
724 ref, ref_offset(ref), jeb->last_node, jeb->last_node?ref_offset(jeb->last_node):0);
725 BUG();
726 }
727 ref_end = jeb->offset + c->sector_size - jeb->free_size;
728 }
729 return ref_end - ref_offset(ref);
730}
731
732uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
733 struct jffs2_raw_node_ref *ref)
734{
735 uint32_t ret;
736
ca89a517 737 ret = __ref_totlen(c, jeb, ref);
9bfeb691 738
ca89a517 739#ifdef TEST_TOTLEN
9bfeb691
DW
740 if (unlikely(ret != ref->__totlen)) {
741 if (!jeb)
742 jeb = &c->blocks[ref->flash_offset / c->sector_size];
743
ca89a517
DW
744 printk(KERN_CRIT "Totlen for ref at %p (0x%08x-0x%08x) miscalculated as 0x%x instead of %x\n",
745 ref, ref_offset(ref), ref_offset(ref)+ref->__totlen,
746 ret, ref->__totlen);
99988f7b
DW
747 if (ref_next(ref)) {
748 printk(KERN_CRIT "next %p (0x%08x-0x%08x)\n", ref_next(ref), ref_offset(ref_next(ref)),
749 ref_offset(ref_next(ref))+ref->__totlen);
ca89a517 750 } else
99988f7b 751 printk(KERN_CRIT "No next ref. jeb->last_node is %p\n", jeb->last_node);
ca89a517
DW
752
753 printk(KERN_CRIT "jeb->wasted_size %x, dirty_size %x, used_size %x, free_size %x\n", jeb->wasted_size, jeb->dirty_size, jeb->used_size, jeb->free_size);
9bfeb691 754
ca89a517
DW
755#if defined(JFFS2_DBG_DUMPS) || defined(JFFS2_DBG_PARANOIA_CHECKS)
756 __jffs2_dbg_dump_node_refs_nolock(c, jeb);
757#endif
9bfeb691 758
ca89a517 759 WARN_ON(1);
9bfeb691
DW
760
761 ret = ref->__totlen;
ca89a517
DW
762 }
763#endif /* TEST_TOTLEN */
764 return ret;
765}
This page took 0.172143 seconds and 5 git commands to generate.