[JFFS2] Move scattered function into related files
[linux-2.6] / fs / jffs2 / nodelist.c
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  *
10  * $Id: nodelist.c,v 1.101 2005/07/27 14:46:11 dedekind Exp $
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
24 void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
25 {
26         struct jffs2_full_dirent **prev = list;
27         D1(printk(KERN_DEBUG "jffs2_add_fd_to_list( %p, %p (->%p))\n", new, list, *list));
28
29         while ((*prev) && (*prev)->nhash <= new->nhash) {
30                 if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
31                         /* Duplicate. Free one */
32                         if (new->version < (*prev)->version) {
33                                 D1(printk(KERN_DEBUG "Eep! Marking new dirent node obsolete\n"));
34                                 D1(printk(KERN_DEBUG "New dirent is \"%s\"->ino #%u. Old is \"%s\"->ino #%u\n", new->name, new->ino, (*prev)->name, (*prev)->ino));
35                                 jffs2_mark_node_obsolete(c, new->raw);
36                                 jffs2_free_full_dirent(new);
37                         } else {
38                                 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) obsolete\n", (*prev)->ino));
39                                 new->next = (*prev)->next;
40                                 jffs2_mark_node_obsolete(c, ((*prev)->raw));
41                                 jffs2_free_full_dirent(*prev);
42                                 *prev = new;
43                         }
44                         goto out;
45                 }
46                 prev = &((*prev)->next);
47         }
48         new->next = *prev;
49         *prev = new;
50
51  out:
52         D2(while(*list) {
53                 printk(KERN_DEBUG "Dirent \"%s\" (hash 0x%08x, ino #%u\n", (*list)->name, (*list)->nhash, (*list)->ino);
54                 list = &(*list)->next;
55         });
56 }
57
58 void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this)
59 {
60         if (this->node) {
61                 this->node->frags--;
62                 if (!this->node->frags) {
63                         /* The node has no valid frags left. It's totally obsoleted */
64                         D2(printk(KERN_DEBUG "Marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
65                                   ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size));
66                         jffs2_mark_node_obsolete(c, this->node->raw);
67                         jffs2_free_full_dnode(this->node);
68                 } else {
69                         D2(printk(KERN_DEBUG "Marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
70                                   ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size,
71                                   this->node->frags));
72                         mark_ref_normal(this->node->raw);
73                 }
74                 
75         }
76         jffs2_free_node_frag(this);
77 }
78
79 static void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
80 {
81         struct rb_node *parent = &base->rb;
82         struct rb_node **link = &parent;
83
84         D2(printk(KERN_DEBUG "jffs2_fragtree_insert(%p; %d-%d, %p)\n", newfrag, 
85                   newfrag->ofs, newfrag->ofs+newfrag->size, base));
86
87         while (*link) {
88                 parent = *link;
89                 base = rb_entry(parent, struct jffs2_node_frag, rb);
90         
91                 D2(printk(KERN_DEBUG "fragtree_insert considering frag at 0x%x\n", base->ofs));
92                 if (newfrag->ofs > base->ofs)
93                         link = &base->rb.rb_right;
94                 else if (newfrag->ofs < base->ofs)
95                         link = &base->rb.rb_left;
96                 else {
97                         printk(KERN_CRIT "Duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
98                         BUG();
99                 }
100         }
101
102         rb_link_node(&newfrag->rb, &base->rb, link);
103 }
104
105 /* Doesn't set inode->i_size */
106 static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *list, struct jffs2_node_frag *newfrag)
107 {
108         struct jffs2_node_frag *this;
109         uint32_t lastend;
110
111         /* Skip all the nodes which are completed before this one starts */
112         this = jffs2_lookup_node_frag(list, newfrag->node->ofs);
113
114         if (this) {
115                 D2(printk(KERN_DEBUG "j_a_f_d_t_f: Lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
116                           this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this));
117                 lastend = this->ofs + this->size;
118         } else {
119                 D2(printk(KERN_DEBUG "j_a_f_d_t_f: Lookup gave no frag\n"));
120                 lastend = 0;
121         }
122                           
123         /* See if we ran off the end of the list */
124         if (lastend <= newfrag->ofs) {
125                 /* We did */
126
127                 /* Check if 'this' node was on the same page as the new node.
128                    If so, both 'this' and the new node get marked REF_NORMAL so
129                    the GC can take a look.
130                 */
131                 if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
132                         if (this->node)
133                                 mark_ref_normal(this->node->raw);
134                         mark_ref_normal(newfrag->node->raw);
135                 }
136
137                 if (lastend < newfrag->node->ofs) {
138                         /* ... and we need to put a hole in before the new node */
139                         struct jffs2_node_frag *holefrag = jffs2_alloc_node_frag();
140                         if (!holefrag) {
141                                 jffs2_free_node_frag(newfrag);
142                                 return -ENOMEM;
143                         }
144                         holefrag->ofs = lastend;
145                         holefrag->size = newfrag->node->ofs - lastend;
146                         holefrag->node = NULL;
147                         if (this) {
148                                 /* By definition, the 'this' node has no right-hand child, 
149                                    because there are no frags with offset greater than it.
150                                    So that's where we want to put the hole */
151                                 D2(printk(KERN_DEBUG "Adding hole frag (%p) on right of node at (%p)\n", holefrag, this));
152                                 rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
153                         } else {
154                                 D2(printk(KERN_DEBUG "Adding hole frag (%p) at root of tree\n", holefrag));
155                                 rb_link_node(&holefrag->rb, NULL, &list->rb_node);
156                         }
157                         rb_insert_color(&holefrag->rb, list);
158                         this = holefrag;
159                 }
160                 if (this) {
161                         /* By definition, the 'this' node has no right-hand child, 
162                            because there are no frags with offset greater than it.
163                            So that's where we want to put new fragment */
164                         D2(printk(KERN_DEBUG "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);                      
166                 } else {
167                         D2(printk(KERN_DEBUG "Adding new frag (%p) at root of tree\n", newfrag));
168                         rb_link_node(&newfrag->rb, NULL, &list->rb_node);
169                 }
170                 rb_insert_color(&newfrag->rb, list);
171                 return 0;
172         }
173
174         D2(printk(KERN_DEBUG "j_a_f_d_t_f: 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));
176
177         /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
178          * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs  
179          */
180         if (newfrag->ofs > this->ofs) {
181                 /* This node isn't completely obsoleted. The start of it remains valid */
182
183                 /* Mark the new node and the partially covered node REF_NORMAL -- let
184                    the GC take a look at them */
185                 mark_ref_normal(newfrag->node->raw);
186                 if (this->node)
187                         mark_ref_normal(this->node->raw);
188
189                 if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
190                         /* The new node splits 'this' frag into two */
191                         struct jffs2_node_frag *newfrag2 = jffs2_alloc_node_frag();
192                         if (!newfrag2) {
193                                 jffs2_free_node_frag(newfrag);
194                                 return -ENOMEM;
195                         }
196                         D2(printk(KERN_DEBUG "split old frag 0x%04x-0x%04x -->", this->ofs, this->ofs+this->size);
197                         if (this->node)
198                                 printk("phys 0x%08x\n", ref_offset(this->node->raw));
199                         else 
200                                 printk("hole\n");
201                            )
202                         
203                         /* New second frag pointing to this's node */
204                         newfrag2->ofs = newfrag->ofs + newfrag->size;
205                         newfrag2->size = (this->ofs+this->size) - newfrag2->ofs;
206                         newfrag2->node = this->node;
207                         if (this->node)
208                                 this->node->frags++;
209
210                         /* Adjust size of original 'this' */
211                         this->size = newfrag->ofs - this->ofs;
212
213                         /* Now, we know there's no node with offset
214                            greater than this->ofs but smaller than
215                            newfrag2->ofs or newfrag->ofs, for obvious
216                            reasons. So we can do a tree insert from
217                            'this' to insert newfrag, and a tree insert
218                            from newfrag to insert newfrag2. */
219                         jffs2_fragtree_insert(newfrag, this);
220                         rb_insert_color(&newfrag->rb, list);
221                         
222                         jffs2_fragtree_insert(newfrag2, newfrag);
223                         rb_insert_color(&newfrag2->rb, list);
224                         
225                         return 0;
226                 }
227                 /* New node just reduces 'this' frag in size, doesn't split it */
228                 this->size = newfrag->ofs - this->ofs;
229
230                 /* Again, we know it lives down here in the tree */
231                 jffs2_fragtree_insert(newfrag, this);
232                 rb_insert_color(&newfrag->rb, list);
233         } else {
234                 /* New frag starts at the same point as 'this' used to. Replace 
235                    it in the tree without doing a delete and insertion */
236                 D2(printk(KERN_DEBUG "Inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
237                           newfrag, newfrag->ofs, newfrag->ofs+newfrag->size,
238                           this, this->ofs, this->ofs+this->size));
239         
240                 rb_replace_node(&this->rb, &newfrag->rb, list);
241                 
242                 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
243                         D2(printk(KERN_DEBUG "Obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size));
244                         jffs2_obsolete_node_frag(c, this);
245                 } else {
246                         this->ofs += newfrag->size;
247                         this->size -= newfrag->size;
248
249                         jffs2_fragtree_insert(this, newfrag);
250                         rb_insert_color(&this->rb, list);
251                         return 0;
252                 }
253         }
254         /* OK, now we have newfrag added in the correct place in the tree, but
255            frag_next(newfrag) may be a fragment which is overlapped by it 
256         */
257         while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
258                 /* 'this' frag is obsoleted completely. */
259                 D2(printk(KERN_DEBUG "Obsoleting node frag %p (%x-%x) and removing from tree\n", this, this->ofs, this->ofs+this->size));
260                 rb_erase(&this->rb, list);
261                 jffs2_obsolete_node_frag(c, this);
262         }
263         /* Now we're pointing at the first frag which isn't totally obsoleted by 
264            the new frag */
265
266         if (!this || newfrag->ofs + newfrag->size == this->ofs) {
267                 return 0;
268         }
269         /* Still some overlap but we don't need to move it in the tree */
270         this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
271         this->ofs = newfrag->ofs + newfrag->size;
272
273         /* And mark them REF_NORMAL so the GC takes a look at them */
274         if (this->node)
275                 mark_ref_normal(this->node->raw);
276         mark_ref_normal(newfrag->node->raw);
277
278         return 0;
279 }
280
281 /* Given an inode, probably with existing list of fragments, add the new node
282  * to the fragment list.
283  */
284 int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
285 {
286         int ret;
287         struct jffs2_node_frag *newfrag;
288
289         D1(printk(KERN_DEBUG "jffs2_add_full_dnode_to_inode(ino #%u, f %p, fn %p)\n", f->inocache->ino, f, fn));
290
291         if (unlikely(!fn->size))
292                 return 0;
293
294         newfrag = jffs2_alloc_node_frag();
295         if (unlikely(!newfrag))
296                 return -ENOMEM;
297
298         D2(printk(KERN_DEBUG "adding node %04x-%04x @0x%08x on flash, newfrag *%p\n",
299                   fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag));
300         
301         newfrag->ofs = fn->ofs;
302         newfrag->size = fn->size;
303         newfrag->node = fn;
304         newfrag->node->frags = 1;
305
306         ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
307         if (unlikely(ret))
308                 return ret;
309
310         /* If we now share a page with other nodes, mark either previous
311            or next node REF_NORMAL, as appropriate.  */
312         if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
313                 struct jffs2_node_frag *prev = frag_prev(newfrag);
314
315                 mark_ref_normal(fn->raw);
316                 /* If we don't start at zero there's _always_ a previous */     
317                 if (prev->node)
318                         mark_ref_normal(prev->node->raw);
319         }
320
321         if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
322                 struct jffs2_node_frag *next = frag_next(newfrag);
323                 
324                 if (next) {
325                         mark_ref_normal(fn->raw);
326                         if (next->node)
327                                 mark_ref_normal(next->node->raw);
328                 }
329         }
330         jffs2_dbg_fragtree_paranoia_check_nolock(f);
331         jffs2_dbg_dump_fragtree_nolock(f);
332         return 0;
333 }
334
335
336 void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
337 {
338         spin_lock(&c->inocache_lock);
339         ic->state = state;
340         wake_up(&c->inocache_wq);
341         spin_unlock(&c->inocache_lock);
342 }
343
344 /* During mount, this needs no locking. During normal operation, its
345    callers want to do other stuff while still holding the inocache_lock.
346    Rather than introducing special case get_ino_cache functions or 
347    callbacks, we just let the caller do the locking itself. */
348    
349 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
350 {
351         struct jffs2_inode_cache *ret;
352
353         D2(printk(KERN_DEBUG "jffs2_get_ino_cache(): ino %u\n", ino));
354
355         ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
356         while (ret && ret->ino < ino) {
357                 ret = ret->next;
358         }
359         
360         if (ret && ret->ino != ino)
361                 ret = NULL;
362
363         D2(printk(KERN_DEBUG "jffs2_get_ino_cache found %p for ino %u\n", ret, ino));
364         return ret;
365 }
366
367 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
368 {
369         struct jffs2_inode_cache **prev;
370
371         spin_lock(&c->inocache_lock);
372         if (!new->ino)
373                 new->ino = ++c->highest_ino;
374
375         D2(printk(KERN_DEBUG "jffs2_add_ino_cache: Add %p (ino #%u)\n", new, new->ino));
376
377         prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
378
379         while ((*prev) && (*prev)->ino < new->ino) {
380                 prev = &(*prev)->next;
381         }
382         new->next = *prev;
383         *prev = new;
384
385         spin_unlock(&c->inocache_lock);
386 }
387
388 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
389 {
390         struct jffs2_inode_cache **prev;
391         D1(printk(KERN_DEBUG "jffs2_del_ino_cache: Del %p (ino #%u)\n", old, old->ino));
392         spin_lock(&c->inocache_lock);
393         
394         prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
395         
396         while ((*prev) && (*prev)->ino < old->ino) {
397                 prev = &(*prev)->next;
398         }
399         if ((*prev) == old) {
400                 *prev = old->next;
401         }
402
403         /* Free it now unless it's in READING or CLEARING state, which
404            are the transitions upon read_inode() and clear_inode(). The
405            rest of the time we know nobody else is looking at it, and 
406            if it's held by read_inode() or clear_inode() they'll free it
407            for themselves. */
408         if (old->state != INO_STATE_READING && old->state != INO_STATE_CLEARING)
409                 jffs2_free_inode_cache(old);
410
411         spin_unlock(&c->inocache_lock);
412 }
413
414 void jffs2_free_ino_caches(struct jffs2_sb_info *c)
415 {
416         int i;
417         struct jffs2_inode_cache *this, *next;
418         
419         for (i=0; i<INOCACHE_HASHSIZE; i++) {
420                 this = c->inocache_list[i];
421                 while (this) {
422                         next = this->next;
423                         jffs2_free_inode_cache(this);
424                         this = next;
425                 }
426                 c->inocache_list[i] = NULL;
427         }
428 }
429
430 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
431 {
432         int i;
433         struct jffs2_raw_node_ref *this, *next;
434
435         for (i=0; i<c->nr_blocks; i++) {
436                 this = c->blocks[i].first_node;
437                 while(this) {
438                         next = this->next_phys;
439                         jffs2_free_raw_node_ref(this);
440                         this = next;
441                 }
442                 c->blocks[i].first_node = c->blocks[i].last_node = NULL;
443         }
444 }
445         
446 struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
447 {
448         /* The common case in lookup is that there will be a node 
449            which precisely matches. So we go looking for that first */
450         struct rb_node *next;
451         struct jffs2_node_frag *prev = NULL;
452         struct jffs2_node_frag *frag = NULL;
453
454         D2(printk(KERN_DEBUG "jffs2_lookup_node_frag(%p, %d)\n", fragtree, offset));
455
456         next = fragtree->rb_node;
457
458         while(next) {
459                 frag = rb_entry(next, struct jffs2_node_frag, rb);
460
461                 D2(printk(KERN_DEBUG "Considering frag %d-%d (%p). left %p, right %p\n",
462                           frag->ofs, frag->ofs+frag->size, frag, frag->rb.rb_left, frag->rb.rb_right));
463                 if (frag->ofs + frag->size <= offset) {
464                         D2(printk(KERN_DEBUG "Going right from frag %d-%d, before the region we care about\n",
465                                   frag->ofs, frag->ofs+frag->size));
466                         /* Remember the closest smaller match on the way down */
467                         if (!prev || frag->ofs > prev->ofs)
468                                 prev = frag;
469                         next = frag->rb.rb_right;
470                 } else if (frag->ofs > offset) {
471                         D2(printk(KERN_DEBUG "Going left from frag %d-%d, after the region we care about\n",
472                                   frag->ofs, frag->ofs+frag->size));
473                         next = frag->rb.rb_left;
474                 } else {
475                         D2(printk(KERN_DEBUG "Returning frag %d,%d, matched\n",
476                                   frag->ofs, frag->ofs+frag->size));
477                         return frag;
478                 }
479         }
480
481         /* Exact match not found. Go back up looking at each parent,
482            and return the closest smaller one */
483
484         if (prev)
485                 D2(printk(KERN_DEBUG "No match. Returning frag %d,%d, closest previous\n",
486                           prev->ofs, prev->ofs+prev->size));
487         else 
488                 D2(printk(KERN_DEBUG "Returning NULL, empty fragtree\n"));
489         
490         return prev;
491 }
492
493 /* Pass 'c' argument to indicate that nodes should be marked obsolete as
494    they're killed. */
495 void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
496 {
497         struct jffs2_node_frag *frag;
498         struct jffs2_node_frag *parent;
499
500         if (!root->rb_node)
501                 return;
502
503         frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
504
505         while(frag) {
506                 if (frag->rb.rb_left) {
507                         D2(printk(KERN_DEBUG "Going left from frag (%p) %d-%d\n", 
508                                   frag, frag->ofs, frag->ofs+frag->size));
509                         frag = frag_left(frag);
510                         continue;
511                 }
512                 if (frag->rb.rb_right) {
513                         D2(printk(KERN_DEBUG "Going right from frag (%p) %d-%d\n", 
514                                   frag, frag->ofs, frag->ofs+frag->size));
515                         frag = frag_right(frag);
516                         continue;
517                 }
518
519                 D2(printk(KERN_DEBUG "jffs2_kill_fragtree: frag at 0x%x-0x%x: node %p, frags %d--\n",
520                           frag->ofs, frag->ofs+frag->size, frag->node,
521                           frag->node?frag->node->frags:0));
522                         
523                 if (frag->node && !(--frag->node->frags)) {
524                         /* Not a hole, and it's the final remaining frag 
525                            of this node. Free the node */
526                         if (c)
527                                 jffs2_mark_node_obsolete(c, frag->node->raw);
528                         
529                         jffs2_free_full_dnode(frag->node);
530                 }
531                 parent = frag_parent(frag);
532                 if (parent) {
533                         if (frag_left(parent) == frag)
534                                 parent->rb.rb_left = NULL;
535                         else 
536                                 parent->rb.rb_right = NULL;
537                 }
538
539                 jffs2_free_node_frag(frag);
540                 frag = parent;
541
542                 cond_resched();
543         }
544 }