upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / 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@redhat.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: nodelist.c,v 1.87 2004/11/14 17:07:07 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 /* Put a new tmp_dnode_info into the list, keeping the list in 
59    order of increasing version
60 */
61 static void jffs2_add_tn_to_list(struct jffs2_tmp_dnode_info *tn, struct jffs2_tmp_dnode_info **list)
62 {
63         struct jffs2_tmp_dnode_info **prev = list;
64         
65         while ((*prev) && (*prev)->version < tn->version) {
66                 prev = &((*prev)->next);
67         }
68         tn->next = (*prev);
69         *prev = tn;
70 }
71
72 static void jffs2_free_tmp_dnode_info_list(struct jffs2_tmp_dnode_info *tn)
73 {
74         struct jffs2_tmp_dnode_info *next;
75
76         while (tn) {
77                 next = tn;
78                 tn = tn->next;
79                 jffs2_free_full_dnode(next->fn);
80                 jffs2_free_tmp_dnode_info(next);
81         }
82 }
83
84 static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
85 {
86         struct jffs2_full_dirent *next;
87
88         while (fd) {
89                 next = fd->next;
90                 jffs2_free_full_dirent(fd);
91                 fd = next;
92         }
93 }
94
95
96 /* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
97    with this ino, returning the former in order of version */
98
99 int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
100                           struct jffs2_tmp_dnode_info **tnp, struct jffs2_full_dirent **fdp,
101                           uint32_t *highest_version, uint32_t *latest_mctime,
102                           uint32_t *mctime_ver)
103 {
104         struct jffs2_raw_node_ref *ref = f->inocache->nodes;
105         struct jffs2_tmp_dnode_info *tn, *ret_tn = NULL;
106         struct jffs2_full_dirent *fd, *ret_fd = NULL;
107         union jffs2_node_union node;
108         size_t retlen;
109         int err;
110
111         *mctime_ver = 0;
112         
113         D1(printk(KERN_DEBUG "jffs2_get_inode_nodes(): ino #%u\n", f->inocache->ino));
114         if (!f->inocache->nodes) {
115                 printk(KERN_WARNING "Eep. no nodes for ino #%u\n", f->inocache->ino);
116         }
117
118         spin_lock(&c->erase_completion_lock);
119
120         for (ref = f->inocache->nodes; ref && ref->next_in_ino; ref = ref->next_in_ino) {
121                 /* Work out whether it's a data node or a dirent node */
122                 if (ref_obsolete(ref)) {
123                         /* FIXME: On NAND flash we may need to read these */
124                         D1(printk(KERN_DEBUG "node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref)));
125                         continue;
126                 }
127                 /* We can hold a pointer to a non-obsolete node without the spinlock,
128                    but _obsolete_ nodes may disappear at any time, if the block
129                    they're in gets erased */
130                 spin_unlock(&c->erase_completion_lock);
131
132                 cond_resched();
133
134                 /* FIXME: point() */
135                 err = jffs2_flash_read(c, (ref_offset(ref)), 
136                                        min_t(uint32_t, ref_totlen(c, NULL, ref), sizeof(node)),
137                                        &retlen, (void *)&node);
138                 if (err) {
139                         printk(KERN_WARNING "error %d reading node at 0x%08x in get_inode_nodes()\n", err, ref_offset(ref));
140                         goto free_out;
141                 }
142                         
143
144                         /* Check we've managed to read at least the common node header */
145                 if (retlen < min_t(uint32_t, ref_totlen(c, NULL, ref), sizeof(node.u))) {
146                         printk(KERN_WARNING "short read in get_inode_nodes()\n");
147                         err = -EIO;
148                         goto free_out;
149                 }
150                         
151                 switch (je16_to_cpu(node.u.nodetype)) {
152                 case JFFS2_NODETYPE_DIRENT:
153                         D1(printk(KERN_DEBUG "Node at %08x (%d) is a dirent node\n", ref_offset(ref), ref_flags(ref)));
154                         if (ref_flags(ref) == REF_UNCHECKED) {
155                                 printk(KERN_WARNING "BUG: Dirent node at 0x%08x never got checked? How?\n", ref_offset(ref));
156                                 BUG();
157                         }
158                         if (retlen < sizeof(node.d)) {
159                                 printk(KERN_WARNING "short read in get_inode_nodes()\n");
160                                 err = -EIO;
161                                 goto free_out;
162                         }
163                         /* sanity check */
164                         if (PAD((node.d.nsize + sizeof (node.d))) != PAD(je32_to_cpu (node.d.totlen))) {
165                                 printk(KERN_NOTICE "jffs2_get_inode_nodes(): Illegal nsize in node at 0x%08x: nsize 0x%02x, totlen %04x\n",
166                                        ref_offset(ref), node.d.nsize, je32_to_cpu(node.d.totlen));
167                                 jffs2_mark_node_obsolete(c, ref);
168                                 spin_lock(&c->erase_completion_lock);
169                                 continue;
170                         }
171                         if (je32_to_cpu(node.d.version) > *highest_version)
172                                 *highest_version = je32_to_cpu(node.d.version);
173                         if (ref_obsolete(ref)) {
174                                 /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
175                                 printk(KERN_ERR "Dirent node at 0x%08x became obsolete while we weren't looking\n",
176                                        ref_offset(ref));
177                                 BUG();
178                         }
179                         
180                         fd = jffs2_alloc_full_dirent(node.d.nsize+1);
181                         if (!fd) {
182                                 err = -ENOMEM;
183                                 goto free_out;
184                         }
185                         memset(fd,0,sizeof(struct jffs2_full_dirent) + node.d.nsize+1);
186                         fd->raw = ref;
187                         fd->version = je32_to_cpu(node.d.version);
188                         fd->ino = je32_to_cpu(node.d.ino);
189                         fd->type = node.d.type;
190
191                         /* Pick out the mctime of the latest dirent */
192                         if(fd->version > *mctime_ver) {
193                                 *mctime_ver = fd->version;
194                                 *latest_mctime = je32_to_cpu(node.d.mctime);
195                         }
196
197                         /* memcpy as much of the name as possible from the raw
198                            dirent we've already read from the flash
199                         */
200                         if (retlen > sizeof(struct jffs2_raw_dirent))
201                                 memcpy(&fd->name[0], &node.d.name[0], min_t(uint32_t, node.d.nsize, (retlen-sizeof(struct jffs2_raw_dirent))));
202                                 
203                         /* Do we need to copy any more of the name directly
204                            from the flash?
205                         */
206                         if (node.d.nsize + sizeof(struct jffs2_raw_dirent) > retlen) {
207                                 /* FIXME: point() */
208                                 int already = retlen - sizeof(struct jffs2_raw_dirent);
209                                         
210                                 err = jffs2_flash_read(c, (ref_offset(ref)) + retlen, 
211                                                    node.d.nsize - already, &retlen, &fd->name[already]);
212                                 if (!err && retlen != node.d.nsize - already)
213                                         err = -EIO;
214                                         
215                                 if (err) {
216                                         printk(KERN_WARNING "Read remainder of name in jffs2_get_inode_nodes(): error %d\n", err);
217                                         jffs2_free_full_dirent(fd);
218                                         goto free_out;
219                                 }
220                         }
221                         fd->nhash = full_name_hash(fd->name, node.d.nsize);
222                         fd->next = NULL;
223                                 /* Wheee. We now have a complete jffs2_full_dirent structure, with
224                                    the name in it and everything. Link it into the list 
225                                 */
226                         D1(printk(KERN_DEBUG "Adding fd \"%s\", ino #%u\n", fd->name, fd->ino));
227                         jffs2_add_fd_to_list(c, fd, &ret_fd);
228                         break;
229
230                 case JFFS2_NODETYPE_INODE:
231                         D1(printk(KERN_DEBUG "Node at %08x (%d) is a data node\n", ref_offset(ref), ref_flags(ref)));
232                         if (retlen < sizeof(node.i)) {
233                                 printk(KERN_WARNING "read too short for dnode\n");
234                                 err = -EIO;
235                                 goto free_out;
236                         }
237                         if (je32_to_cpu(node.i.version) > *highest_version)
238                                 *highest_version = je32_to_cpu(node.i.version);
239                         D1(printk(KERN_DEBUG "version %d, highest_version now %d\n", je32_to_cpu(node.i.version), *highest_version));
240
241                         if (ref_obsolete(ref)) {
242                                 /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
243                                 printk(KERN_ERR "Inode node at 0x%08x became obsolete while we weren't looking\n",
244                                        ref_offset(ref));
245                                 BUG();
246                         }
247
248                         /* If we've never checked the CRCs on this node, check them now. */
249                         if (ref_flags(ref) == REF_UNCHECKED) {
250                                 uint32_t crc, len;
251                                 struct jffs2_eraseblock *jeb;
252
253                                 crc = crc32(0, &node, sizeof(node.i)-8);
254                                 if (crc != je32_to_cpu(node.i.node_crc)) {
255                                         printk(KERN_NOTICE "jffs2_get_inode_nodes(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
256                                                ref_offset(ref), je32_to_cpu(node.i.node_crc), crc);
257                                         jffs2_mark_node_obsolete(c, ref);
258                                         spin_lock(&c->erase_completion_lock);
259                                         continue;
260                                 }
261                                 
262                                 /* sanity checks */
263                                 if ( je32_to_cpu(node.i.offset) > je32_to_cpu(node.i.isize) ||
264                                      PAD(je32_to_cpu(node.i.csize) + sizeof (node.i)) != PAD(je32_to_cpu(node.i.totlen))) {
265                                         printk(KERN_NOTICE "jffs2_get_inode_nodes(): Inode corrupted at 0x%08x, totlen %d, #ino  %d, version %d, isize %d, csize %d, dsize %d \n",
266                                                 ref_offset(ref),  je32_to_cpu(node.i.totlen),  je32_to_cpu(node.i.ino),
267                                                 je32_to_cpu(node.i.version),  je32_to_cpu(node.i.isize), 
268                                                 je32_to_cpu(node.i.csize), je32_to_cpu(node.i.dsize));
269                                         jffs2_mark_node_obsolete(c, ref);
270                                         spin_lock(&c->erase_completion_lock);
271                                         continue;
272                                 }
273
274                                 if (node.i.compr != JFFS2_COMPR_ZERO && je32_to_cpu(node.i.csize)) {
275                                         unsigned char *buf=NULL;
276                                         uint32_t pointed = 0;
277 #ifndef __ECOS
278                                         if (c->mtd->point) {
279                                                 err = c->mtd->point (c->mtd, ref_offset(ref) + sizeof(node.i), je32_to_cpu(node.i.csize),
280                                                                      &retlen, &buf);
281                                                 if (!err && retlen < je32_to_cpu(node.i.csize)) {
282                                                         D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", retlen));
283                                                         c->mtd->unpoint(c->mtd, buf, ref_offset(ref) + sizeof(node.i), je32_to_cpu(node.i.csize));
284                                                 } else if (err){
285                                                         D1(printk(KERN_DEBUG "MTD point failed %d\n", err));
286                                                 } else
287                                                         pointed = 1; /* succefully pointed to device */
288                                         }
289 #endif                                  
290                                         if(!pointed){
291                                                 buf = kmalloc(je32_to_cpu(node.i.csize), GFP_KERNEL);
292                                                 if (!buf)
293                                                         return -ENOMEM;
294                                                 
295                                                 err = jffs2_flash_read(c, ref_offset(ref) + sizeof(node.i), je32_to_cpu(node.i.csize),
296                                                                        &retlen, buf);
297                                                 if (!err && retlen != je32_to_cpu(node.i.csize))
298                                                         err = -EIO;
299                                                 if (err) {
300                                                         kfree(buf);
301                                                         return err;
302                                                 }
303                                         }
304                                         crc = crc32(0, buf, je32_to_cpu(node.i.csize));
305                                         if(!pointed)
306                                                 kfree(buf);
307 #ifndef __ECOS
308                                         else
309                                                 c->mtd->unpoint(c->mtd, buf, ref_offset(ref) + sizeof(node.i), je32_to_cpu(node.i.csize));
310 #endif
311
312                                         if (crc != je32_to_cpu(node.i.data_crc)) {
313                                                 printk(KERN_NOTICE "jffs2_get_inode_nodes(): Data CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
314                                                        ref_offset(ref), je32_to_cpu(node.i.data_crc), crc);
315                                                 jffs2_mark_node_obsolete(c, ref);
316                                                 spin_lock(&c->erase_completion_lock);
317                                                 continue;
318                                         }
319                                         
320                                 }
321
322                                 /* Mark the node as having been checked and fix the accounting accordingly */
323                                 spin_lock(&c->erase_completion_lock);
324                                 jeb = &c->blocks[ref->flash_offset / c->sector_size];
325                                 len = ref_totlen(c, jeb, ref);
326
327                                 jeb->used_size += len;
328                                 jeb->unchecked_size -= len;
329                                 c->used_size += len;
330                                 c->unchecked_size -= len;
331
332                                 /* If node covers at least a whole page, or if it starts at the 
333                                    beginning of a page and runs to the end of the file, or if 
334                                    it's a hole node, mark it REF_PRISTINE, else REF_NORMAL. 
335
336                                    If it's actually overlapped, it'll get made NORMAL (or OBSOLETE) 
337                                    when the overlapping node(s) get added to the tree anyway. 
338                                 */
339                                 if ((je32_to_cpu(node.i.dsize) >= PAGE_CACHE_SIZE) ||
340                                     ( ((je32_to_cpu(node.i.offset)&(PAGE_CACHE_SIZE-1))==0) &&
341                                       (je32_to_cpu(node.i.dsize)+je32_to_cpu(node.i.offset) ==  je32_to_cpu(node.i.isize)))) {
342                                         D1(printk(KERN_DEBUG "Marking node at 0x%08x REF_PRISTINE\n", ref_offset(ref)));
343                                         ref->flash_offset = ref_offset(ref) | REF_PRISTINE;
344                                 } else {
345                                         D1(printk(KERN_DEBUG "Marking node at 0x%08x REF_NORMAL\n", ref_offset(ref)));
346                                         ref->flash_offset = ref_offset(ref) | REF_NORMAL;
347                                 }
348                                 spin_unlock(&c->erase_completion_lock);
349                         }
350
351                         tn = jffs2_alloc_tmp_dnode_info();
352                         if (!tn) {
353                                 D1(printk(KERN_DEBUG "alloc tn failed\n"));
354                                 err = -ENOMEM;
355                                 goto free_out;
356                         }
357
358                         tn->fn = jffs2_alloc_full_dnode();
359                         if (!tn->fn) {
360                                 D1(printk(KERN_DEBUG "alloc fn failed\n"));
361                                 err = -ENOMEM;
362                                 jffs2_free_tmp_dnode_info(tn);
363                                 goto free_out;
364                         }
365                         tn->version = je32_to_cpu(node.i.version);
366                         tn->fn->ofs = je32_to_cpu(node.i.offset);
367                         /* There was a bug where we wrote hole nodes out with
368                            csize/dsize swapped. Deal with it */
369                         if (node.i.compr == JFFS2_COMPR_ZERO && !je32_to_cpu(node.i.dsize) && je32_to_cpu(node.i.csize))
370                                 tn->fn->size = je32_to_cpu(node.i.csize);
371                         else // normal case...
372                                 tn->fn->size = je32_to_cpu(node.i.dsize);
373                         tn->fn->raw = ref;
374                         D1(printk(KERN_DEBUG "dnode @%08x: ver %u, offset %04x, dsize %04x\n",
375                                   ref_offset(ref), je32_to_cpu(node.i.version),
376                                   je32_to_cpu(node.i.offset), je32_to_cpu(node.i.dsize)));
377                         jffs2_add_tn_to_list(tn, &ret_tn);
378                         break;
379
380                 default:
381                         if (ref_flags(ref) == REF_UNCHECKED) {
382                                 struct jffs2_eraseblock *jeb;
383                                 uint32_t len;
384
385                                 printk(KERN_ERR "Eep. Unknown node type %04x at %08x was marked REF_UNCHECKED\n",
386                                        je16_to_cpu(node.u.nodetype), ref_offset(ref));
387
388                                 /* Mark the node as having been checked and fix the accounting accordingly */
389                                 spin_lock(&c->erase_completion_lock);
390                                 jeb = &c->blocks[ref->flash_offset / c->sector_size];
391                                 len = ref_totlen(c, jeb, ref);
392
393                                 jeb->used_size += len;
394                                 jeb->unchecked_size -= len;
395                                 c->used_size += len;
396                                 c->unchecked_size -= len;
397
398                                 mark_ref_normal(ref);
399                                 spin_unlock(&c->erase_completion_lock);
400                         }
401                         node.u.nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(node.u.nodetype));
402                         if (crc32(0, &node, sizeof(struct jffs2_unknown_node)-4) != je32_to_cpu(node.u.hdr_crc)) {
403                                 /* Hmmm. This should have been caught at scan time. */
404                                 printk(KERN_ERR "Node header CRC failed at %08x. But it must have been OK earlier.\n",
405                                        ref_offset(ref));
406                                 printk(KERN_ERR "Node was: { %04x, %04x, %08x, %08x }\n", 
407                                        je16_to_cpu(node.u.magic), je16_to_cpu(node.u.nodetype), je32_to_cpu(node.u.totlen),
408                                        je32_to_cpu(node.u.hdr_crc));
409                                 jffs2_mark_node_obsolete(c, ref);
410                         } else switch(je16_to_cpu(node.u.nodetype) & JFFS2_COMPAT_MASK) {
411                         case JFFS2_FEATURE_INCOMPAT:
412                                 printk(KERN_NOTICE "Unknown INCOMPAT nodetype %04X at %08x\n", je16_to_cpu(node.u.nodetype), ref_offset(ref));
413                                 /* EEP */
414                                 BUG();
415                                 break;
416                         case JFFS2_FEATURE_ROCOMPAT:
417                                 printk(KERN_NOTICE "Unknown ROCOMPAT nodetype %04X at %08x\n", je16_to_cpu(node.u.nodetype), ref_offset(ref));
418                                 if (!(c->flags & JFFS2_SB_FLAG_RO))
419                                         BUG();
420                                 break;
421                         case JFFS2_FEATURE_RWCOMPAT_COPY:
422                                 printk(KERN_NOTICE "Unknown RWCOMPAT_COPY nodetype %04X at %08x\n", je16_to_cpu(node.u.nodetype), ref_offset(ref));
423                                 break;
424                         case JFFS2_FEATURE_RWCOMPAT_DELETE:
425                                 printk(KERN_NOTICE "Unknown RWCOMPAT_DELETE nodetype %04X at %08x\n", je16_to_cpu(node.u.nodetype), ref_offset(ref));
426                                 jffs2_mark_node_obsolete(c, ref);
427                                 break;
428                         }
429
430                 }
431                 spin_lock(&c->erase_completion_lock);
432
433         }
434         spin_unlock(&c->erase_completion_lock);
435         *tnp = ret_tn;
436         *fdp = ret_fd;
437
438         return 0;
439
440  free_out:
441         jffs2_free_tmp_dnode_info_list(ret_tn);
442         jffs2_free_full_dirent_list(ret_fd);
443         return err;
444 }
445
446 void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
447 {
448         spin_lock(&c->inocache_lock);
449         ic->state = state;
450         wake_up(&c->inocache_wq);
451         spin_unlock(&c->inocache_lock);
452 }
453
454 /* During mount, this needs no locking. During normal operation, its
455    callers want to do other stuff while still holding the inocache_lock.
456    Rather than introducing special case get_ino_cache functions or 
457    callbacks, we just let the caller do the locking itself. */
458    
459 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
460 {
461         struct jffs2_inode_cache *ret;
462
463         D2(printk(KERN_DEBUG "jffs2_get_ino_cache(): ino %u\n", ino));
464
465         ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
466         while (ret && ret->ino < ino) {
467                 ret = ret->next;
468         }
469         
470         if (ret && ret->ino != ino)
471                 ret = NULL;
472
473         D2(printk(KERN_DEBUG "jffs2_get_ino_cache found %p for ino %u\n", ret, ino));
474         return ret;
475 }
476
477 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
478 {
479         struct jffs2_inode_cache **prev;
480         D2(printk(KERN_DEBUG "jffs2_add_ino_cache: Add %p (ino #%u)\n", new, new->ino));
481         spin_lock(&c->inocache_lock);
482         
483         prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
484
485         while ((*prev) && (*prev)->ino < new->ino) {
486                 prev = &(*prev)->next;
487         }
488         new->next = *prev;
489         *prev = new;
490
491         spin_unlock(&c->inocache_lock);
492 }
493
494 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
495 {
496         struct jffs2_inode_cache **prev;
497         D2(printk(KERN_DEBUG "jffs2_del_ino_cache: Del %p (ino #%u)\n", old, old->ino));
498         spin_lock(&c->inocache_lock);
499         
500         prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
501         
502         while ((*prev) && (*prev)->ino < old->ino) {
503                 prev = &(*prev)->next;
504         }
505         if ((*prev) == old) {
506                 *prev = old->next;
507         }
508
509         spin_unlock(&c->inocache_lock);
510 }
511
512 void jffs2_free_ino_caches(struct jffs2_sb_info *c)
513 {
514         int i;
515         struct jffs2_inode_cache *this, *next;
516         
517         for (i=0; i<INOCACHE_HASHSIZE; i++) {
518                 this = c->inocache_list[i];
519                 while (this) {
520                         next = this->next;
521                         D2(printk(KERN_DEBUG "jffs2_free_ino_caches: Freeing ino #%u at %p\n", this->ino, this));
522                         jffs2_free_inode_cache(this);
523                         this = next;
524                 }
525                 c->inocache_list[i] = NULL;
526         }
527 }
528
529 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
530 {
531         int i;
532         struct jffs2_raw_node_ref *this, *next;
533
534         for (i=0; i<c->nr_blocks; i++) {
535                 this = c->blocks[i].first_node;
536                 while(this) {
537                         next = this->next_phys;
538                         jffs2_free_raw_node_ref(this);
539                         this = next;
540                 }
541                 c->blocks[i].first_node = c->blocks[i].last_node = NULL;
542         }
543 }
544         
545 struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
546 {
547         /* The common case in lookup is that there will be a node 
548            which precisely matches. So we go looking for that first */
549         struct rb_node *next;
550         struct jffs2_node_frag *prev = NULL;
551         struct jffs2_node_frag *frag = NULL;
552
553         D2(printk(KERN_DEBUG "jffs2_lookup_node_frag(%p, %d)\n", fragtree, offset));
554
555         next = fragtree->rb_node;
556
557         while(next) {
558                 frag = rb_entry(next, struct jffs2_node_frag, rb);
559
560                 D2(printk(KERN_DEBUG "Considering frag %d-%d (%p). left %p, right %p\n",
561                           frag->ofs, frag->ofs+frag->size, frag, frag->rb.rb_left, frag->rb.rb_right));
562                 if (frag->ofs + frag->size <= offset) {
563                         D2(printk(KERN_DEBUG "Going right from frag %d-%d, before the region we care about\n",
564                                   frag->ofs, frag->ofs+frag->size));
565                         /* Remember the closest smaller match on the way down */
566                         if (!prev || frag->ofs > prev->ofs)
567                                 prev = frag;
568                         next = frag->rb.rb_right;
569                 } else if (frag->ofs > offset) {
570                         D2(printk(KERN_DEBUG "Going left from frag %d-%d, after the region we care about\n",
571                                   frag->ofs, frag->ofs+frag->size));
572                         next = frag->rb.rb_left;
573                 } else {
574                         D2(printk(KERN_DEBUG "Returning frag %d,%d, matched\n",
575                                   frag->ofs, frag->ofs+frag->size));
576                         return frag;
577                 }
578         }
579
580         /* Exact match not found. Go back up looking at each parent,
581            and return the closest smaller one */
582
583         if (prev)
584                 D2(printk(KERN_DEBUG "No match. Returning frag %d,%d, closest previous\n",
585                           prev->ofs, prev->ofs+prev->size));
586         else 
587                 D2(printk(KERN_DEBUG "Returning NULL, empty fragtree\n"));
588         
589         return prev;
590 }
591
592 /* Pass 'c' argument to indicate that nodes should be marked obsolete as
593    they're killed. */
594 void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
595 {
596         struct jffs2_node_frag *frag;
597         struct jffs2_node_frag *parent;
598
599         if (!root->rb_node)
600                 return;
601
602         frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
603
604         while(frag) {
605                 if (frag->rb.rb_left) {
606                         D2(printk(KERN_DEBUG "Going left from frag (%p) %d-%d\n", 
607                                   frag, frag->ofs, frag->ofs+frag->size));
608                         frag = frag_left(frag);
609                         continue;
610                 }
611                 if (frag->rb.rb_right) {
612                         D2(printk(KERN_DEBUG "Going right from frag (%p) %d-%d\n", 
613                                   frag, frag->ofs, frag->ofs+frag->size));
614                         frag = frag_right(frag);
615                         continue;
616                 }
617
618                 D2(printk(KERN_DEBUG "jffs2_kill_fragtree: frag at 0x%x-0x%x: node %p, frags %d--\n",
619                           frag->ofs, frag->ofs+frag->size, frag->node,
620                           frag->node?frag->node->frags:0));
621                         
622                 if (frag->node && !(--frag->node->frags)) {
623                         /* Not a hole, and it's the final remaining frag 
624                            of this node. Free the node */
625                         if (c)
626                                 jffs2_mark_node_obsolete(c, frag->node->raw);
627                         
628                         jffs2_free_full_dnode(frag->node);
629                 }
630                 parent = frag_parent(frag);
631                 if (parent) {
632                         if (frag_left(parent) == frag)
633                                 parent->rb.rb_left = NULL;
634                         else 
635                                 parent->rb.rb_right = NULL;
636                 }
637
638                 jffs2_free_node_frag(frag);
639                 frag = parent;
640
641                 cond_resched();
642         }
643 }
644
645 void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
646 {
647         struct rb_node *parent = &base->rb;
648         struct rb_node **link = &parent;
649
650         D2(printk(KERN_DEBUG "jffs2_fragtree_insert(%p; %d-%d, %p)\n", newfrag, 
651                   newfrag->ofs, newfrag->ofs+newfrag->size, base));
652
653         while (*link) {
654                 parent = *link;
655                 base = rb_entry(parent, struct jffs2_node_frag, rb);
656         
657                 D2(printk(KERN_DEBUG "fragtree_insert considering frag at 0x%x\n", base->ofs));
658                 if (newfrag->ofs > base->ofs)
659                         link = &base->rb.rb_right;
660                 else if (newfrag->ofs < base->ofs)
661                         link = &base->rb.rb_left;
662                 else {
663                         printk(KERN_CRIT "Duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
664                         BUG();
665                 }
666         }
667
668         rb_link_node(&newfrag->rb, &base->rb, link);
669 }