source: rtems/cpukit/libfs/src/jffs2/src/nodelist.c @ 1465e70c

5
Last change on this file since 1465e70c was 1465e70c, checked in by Cody P Schafer <cody@…>, on 01/23/14 at 23:56:11

fs/jffs2: use rbtree postorder iteration helper instead of opencoding

Use rbtree_postorder_for_each_entry_safe() to destroy the rbtree instead
of opencoding an alternate postorder iteration that modifies the tree

Signed-off-by: Cody P Schafer <cody@…>
Cc: Michel Lespinasse <walken@…>
Cc: Jan Kara <jack@…>
Cc: David Woodhouse <dwmw2@…>
Signed-off-by: Andrew Morton <akpm@…>
Signed-off-by: Linus Torvalds <torvalds@…>

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