v2.4.9.9 -> v2.4.9.10
[opensuse:kernel.git] / fs / jffs2 / scan.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
7  *
8  * The original JFFS, from which the design for JFFS2 was derived,
9  * was designed and implemented by Axis Communications AB.
10  *
11  * The contents of this file are subject to the Red Hat eCos Public
12  * License Version 1.1 (the "Licence"); you may not use this file
13  * except in compliance with the Licence.  You may obtain a copy of
14  * the Licence at http://www.redhat.com/
15  *
16  * Software distributed under the Licence is distributed on an "AS IS"
17  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18  * See the Licence for the specific language governing rights and
19  * limitations under the Licence.
20  *
21  * The Original Code is JFFS2 - Journalling Flash File System, version 2
22  *
23  * Alternatively, the contents of this file may be used under the
24  * terms of the GNU General Public License version 2 (the "GPL"), in
25  * which case the provisions of the GPL are applicable instead of the
26  * above.  If you wish to allow the use of your version of this file
27  * only under the terms of the GPL and not to allow others to use your
28  * version of this file under the RHEPL, indicate your decision by
29  * deleting the provisions above and replace them with the notice and
30  * other provisions required by the GPL.  If you do not delete the
31  * provisions above, a recipient may use your version of this file
32  * under either the RHEPL or the GPL.
33  *
34  * $Id: scan.c,v 1.44 2001/04/13 00:46:41 nico Exp $
35  *
36  */
37 #include <linux/kernel.h>
38 #include <linux/slab.h>
39 #include <linux/jffs2.h>
40 #include <linux/mtd/mtd.h>
41 #include <linux/pagemap.h>
42 #include "nodelist.h"
43 #include "crc32.h"
44
45
46 #define DIRTY_SPACE(x) do { typeof(x) _x = (x); \
47                 c->free_size -= _x; c->dirty_size += _x; \
48                 jeb->free_size -= _x ; jeb->dirty_size += _x; \
49                 }while(0)
50 #define USED_SPACE(x) do { typeof(x) _x = (x); \
51                 c->free_size -= _x; c->used_size += _x; \
52                 jeb->free_size -= _x ; jeb->used_size += _x; \
53                 }while(0)
54
55 #define noisy_printk(noise, args...) do { \
56         if (*(noise)) { \
57                 printk(KERN_NOTICE args); \
58                  (*(noise))--; \
59                  if (!(*(noise))) { \
60                          printk(KERN_NOTICE "Further such events for this erase block will not be printed\n"); \
61                  } \
62         } \
63 } while(0)
64
65 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
66
67 /* These helper functions _must_ increase ofs and also do the dirty/used space accounting. 
68  * Returning an error will abort the mount - bad checksums etc. should just mark the space
69  * as dirty.
70  */
71 static int jffs2_scan_empty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *ofs, int *noise);
72 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *ofs);
73 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *ofs);
74
75
76 int jffs2_scan_medium(struct jffs2_sb_info *c)
77 {
78         int i, ret;
79         if (!c->blocks) {
80                 printk(KERN_WARNING "EEEK! c->blocks is NULL!\n");
81                 return -EINVAL;
82         }
83         for (i=0; i<c->nr_blocks; i++) {
84                 struct jffs2_eraseblock *jeb = &c->blocks[i];
85
86                 ret = jffs2_scan_eraseblock(c, jeb);
87                 if (ret)
88                         return ret;
89
90                 ACCT_PARANOIA_CHECK(jeb);
91
92                    /* Now decide which list to put it on */
93                 if (jeb->used_size == PAD(sizeof(struct jffs2_unknown_node)) && !jeb->first_node->next_in_ino) {
94                         /* Only a CLEANMARKER node is valid */
95                         if (!jeb->dirty_size) {
96                                 /* It's actually free */
97                                 list_add(&jeb->list, &c->free_list);
98                                 c->nr_free_blocks++;
99                         } else {
100                                 /* Dirt */
101                                 D1(printk(KERN_DEBUG "Adding all-dirty block at 0x%08x to erase_pending_list\n", jeb->offset));
102                                 list_add(&jeb->list, &c->erase_pending_list);
103                                 c->nr_erasing_blocks++;
104                         }
105                 } else if (jeb->used_size > c->sector_size - (2*sizeof(struct jffs2_raw_inode))) {
106                         /* Full (or almost full) of clean data. Clean list */
107                         list_add(&jeb->list, &c->clean_list);
108                 } else if (jeb->used_size) {
109                         /* Some data, but not full. Dirty list. */
110                         /* Except that we want to remember the block with most free space,
111                            and stick it in the 'nextblock' position to start writing to it.
112                            Later when we do snapshots, this must be the most recent block,
113                            not the one with most free space.
114                         */
115                         if (jeb->free_size > 2*sizeof(struct jffs2_raw_inode) && 
116                                 (!c->nextblock || c->nextblock->free_size < jeb->free_size)) {
117                                 /* Better candidate for the next writes to go to */
118                                 if (c->nextblock)
119                                         list_add(&c->nextblock->list, &c->dirty_list);
120                                 c->nextblock = jeb;
121                         } else {
122                                 list_add(&jeb->list, &c->dirty_list);
123                         }
124                 } else {
125                         /* Nothing valid - not even a clean marker. Needs erasing. */
126                         /* For now we just put it on the erasing list. We'll start the erases later */
127                         printk(KERN_NOTICE "JFFS2: Erase block at 0x%08x is not formatted. It will be erased\n", jeb->offset);
128                         list_add(&jeb->list, &c->erase_pending_list);
129                         c->nr_erasing_blocks++;
130                 }
131         }
132         if (c->nr_erasing_blocks)
133                 jffs2_erase_pending_trigger(c);
134
135         return 0;
136 }
137
138 static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) {
139         struct jffs2_unknown_node node;
140         __u32 ofs, prevofs;
141         __u32 hdr_crc, nodetype;
142         int err;
143         int noise = 10;
144
145         ofs = jeb->offset;
146         prevofs = jeb->offset - 1;
147
148         D1(printk(KERN_DEBUG "jffs2_scan_eraseblock(): Scanning block at 0x%x\n", ofs));
149
150         while(ofs < jeb->offset + c->sector_size) {
151                 ssize_t retlen;
152                 ACCT_PARANOIA_CHECK(jeb);
153                 
154                 if (ofs & 3) {
155                         printk(KERN_WARNING "Eep. ofs 0x%08x not word-aligned!\n", ofs);
156                         ofs = (ofs+3)&~3;
157                         continue;
158                 }
159                 if (ofs == prevofs) {
160                         printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
161                         DIRTY_SPACE(4);
162                         ofs += 4;
163                         continue;
164                 }
165                 prevofs = ofs;
166                 
167                 if (jeb->offset + c->sector_size < ofs + sizeof(node)) {
168                         D1(printk(KERN_DEBUG "Fewer than %d bytes left to end of block. Not reading\n", sizeof(struct jffs2_unknown_node)));
169                         DIRTY_SPACE((jeb->offset + c->sector_size)-ofs);
170                         break;
171                 }
172
173                 err = c->mtd->read(c->mtd, ofs, sizeof(node), &retlen, (char *)&node);
174                 
175                 if (err) {
176                         D1(printk(KERN_WARNING "mtd->read(0x%x bytes from 0x%x) returned %d\n", sizeof(node), ofs, err));
177                         return err;
178                 }
179                 if (retlen < sizeof(node)) {
180                         D1(printk(KERN_WARNING "Read at 0x%x gave only 0x%x bytes\n", ofs, retlen));
181                         DIRTY_SPACE(retlen);
182                         ofs += retlen;
183                         continue;
184                 }
185
186                 if (node.magic == JFFS2_EMPTY_BITMASK && node.nodetype == JFFS2_EMPTY_BITMASK) {
187                         D1(printk(KERN_DEBUG "Found empty flash at 0x%x\n", ofs));
188                         err = jffs2_scan_empty(c, jeb, &ofs, &noise);
189                         if (err) return err;
190                         continue;
191                 }
192
193                 if (ofs == jeb->offset && node.magic == KSAMTIB_CIGAM_2SFFJ) {
194                         printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs);
195                         DIRTY_SPACE(4);
196                         ofs += 4;
197                         continue;
198                 }
199                 if (node.magic == JFFS2_DIRTY_BITMASK) {
200                         D1(printk(KERN_DEBUG "Empty bitmask at 0x%08x\n", ofs));
201                         DIRTY_SPACE(4);
202                         ofs += 4;
203                         continue;
204                 }
205                 if (node.magic == JFFS2_OLD_MAGIC_BITMASK) {
206                         printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs);
207                         printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n");
208                         DIRTY_SPACE(4);
209                         ofs += 4;
210                         continue;
211                 }
212                 if (node.magic != JFFS2_MAGIC_BITMASK) {
213                         /* OK. We're out of possibilities. Whinge and move on */
214                         noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n", JFFS2_MAGIC_BITMASK, ofs, node.magic);
215                         DIRTY_SPACE(4);
216                         ofs += 4;
217                         continue;
218                 }
219                 /* We seem to have a node of sorts. Check the CRC */
220                 nodetype = node.nodetype;
221                 node.nodetype |= JFFS2_NODE_ACCURATE;
222                 hdr_crc = crc32(0, &node, sizeof(node)-4);
223                 node.nodetype = nodetype;
224                 if (hdr_crc != node.hdr_crc) {
225                         noisy_printk(&noise, "jffs2_scan_eraseblock(): Node at 0x%08x {0x%04x, 0x%04x, 0x%08x) has invalid CRC 0x%08x (calculated 0x%08x)\n",
226                                      ofs, node.magic, node.nodetype, node.totlen, node.hdr_crc, hdr_crc);
227                         DIRTY_SPACE(4);
228                         ofs += 4;
229                         continue;
230                 }
231
232                 switch(node.nodetype | JFFS2_NODE_ACCURATE) {
233                 case JFFS2_NODETYPE_INODE:
234                         err = jffs2_scan_inode_node(c, jeb, &ofs);
235                         if (err) return err;
236                         break;
237                         
238                 case JFFS2_NODETYPE_DIRENT:
239                         err = jffs2_scan_dirent_node(c, jeb, &ofs);
240                         if (err) return err;
241                         break;
242
243                 case JFFS2_NODETYPE_CLEANMARKER:
244                         if (node.totlen != sizeof(struct jffs2_unknown_node)) {
245                                 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n", 
246                                        ofs, node.totlen, sizeof(struct jffs2_unknown_node));
247                                 DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
248                         } else if (jeb->first_node) {
249                                 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset);
250                                 DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
251                                 ofs += PAD(sizeof(struct jffs2_unknown_node));
252                                 continue;
253                         } else {
254                                 struct jffs2_raw_node_ref *marker_ref = jffs2_alloc_raw_node_ref();
255                                 if (!marker_ref) {
256                                         printk(KERN_NOTICE "Failed to allocate node ref for clean marker\n");
257                                         return -ENOMEM;
258                                 }
259                                 marker_ref->next_in_ino = NULL;
260                                 marker_ref->next_phys = NULL;
261                                 marker_ref->flash_offset = ofs;
262                                 marker_ref->totlen = sizeof(struct jffs2_unknown_node);
263                                 jeb->first_node = jeb->last_node = marker_ref;
264                              
265                                 USED_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
266                         }
267                         ofs += PAD(sizeof(struct jffs2_unknown_node));
268                         break;
269
270                 default:
271                         switch (node.nodetype & JFFS2_COMPAT_MASK) {
272                         case JFFS2_FEATURE_ROCOMPAT:
273                                 printk(KERN_NOTICE "Read-only compatible feature node (0x%04x) found at offset 0x%08x\n", node.nodetype, ofs);
274                                 c->flags |= JFFS2_SB_FLAG_RO;
275                                 DIRTY_SPACE(PAD(node.totlen));
276                                 ofs += PAD(node.totlen);
277                                 continue;
278
279                         case JFFS2_FEATURE_INCOMPAT:
280                                 printk(KERN_NOTICE "Incompatible feature node (0x%04x) found at offset 0x%08x\n", node.nodetype, ofs);
281                                 return -EINVAL;
282
283                         case JFFS2_FEATURE_RWCOMPAT_DELETE:
284                                 printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", node.nodetype, ofs);
285                                 DIRTY_SPACE(PAD(node.totlen));
286                                 ofs += PAD(node.totlen);
287                                 break;
288
289                         case JFFS2_FEATURE_RWCOMPAT_COPY:
290                                 printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", node.nodetype, ofs);
291                                 USED_SPACE(PAD(node.totlen));
292                                 ofs += PAD(node.totlen);
293                                 break;
294                         }
295                 }
296         }
297         D1(printk(KERN_DEBUG "Block at 0x%08x: free 0x%08x, dirty 0x%08x, used 0x%08x\n", jeb->offset, 
298                   jeb->free_size, jeb->dirty_size, jeb->used_size));
299         return 0;
300 }
301
302 /* We're pointing at the first empty word on the flash. Scan and account for the whole dirty region */
303 static int jffs2_scan_empty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *startofs, int *noise)
304 {
305         __u32 *buf;
306         __u32 scanlen = (jeb->offset + c->sector_size) - *startofs;
307         __u32 curofs = *startofs;
308         
309         buf = kmalloc(min(PAGE_SIZE, scanlen), GFP_KERNEL);
310         if (!buf) {
311                 printk(KERN_WARNING "Scan buffer allocation failed\n");
312                 return -ENOMEM;
313         }
314         while(scanlen) {
315                 ssize_t retlen;
316                 int ret, i;
317                 
318                 ret = c->mtd->read(c->mtd, curofs, min(PAGE_SIZE, scanlen), &retlen, (char *)buf);
319                 if(ret) {
320                         D1(printk(KERN_WARNING "jffs2_scan_empty(): Read 0x%lx bytes at 0x%08x returned %d\n", min(PAGE_SIZE, scanlen), curofs, ret));
321                         kfree(buf);
322                         return ret;
323                 }
324                 if (retlen < 4) {
325                         D1(printk(KERN_WARNING "Eep. too few bytes read in scan_empty()\n"));
326                         kfree(buf);
327                         return -EIO;
328                 }
329                 for (i=0; i<(retlen / 4); i++) {
330                         if (buf[i] != 0xffffffff) {
331                                 curofs += i*4;
332
333                                 noisy_printk(noise, "jffs2_scan_empty(): Empty block at 0x%08x ends at 0x%08x (with 0x%08x)! Marking dirty\n", *startofs, curofs, buf[i]);
334                                 DIRTY_SPACE(curofs - (*startofs));
335                                 *startofs = curofs;
336                                 kfree(buf);
337                                 return 0;
338                         }
339                 }
340                 scanlen -= retlen&~3;
341                 curofs += retlen&~3;
342         }
343
344         D1(printk(KERN_DEBUG "Empty flash detected from 0x%08x to 0x%08x\n", *startofs, curofs));
345         kfree(buf);
346         *startofs = curofs;
347         return 0;
348 }
349
350 static struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, __u32 ino)
351 {
352         struct jffs2_inode_cache *ic;
353
354         ic = jffs2_get_ino_cache(c, ino);
355         if (ic)
356                 return ic;
357
358         ic = jffs2_alloc_inode_cache();
359         if (!ic) {
360                 printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of inode cache failed\n");
361                 return NULL;
362         }
363         memset(ic, 0, sizeof(*ic));
364         ic->scan = kmalloc(sizeof(struct jffs2_scan_info), GFP_KERNEL);
365         if (!ic->scan) {
366                 printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of scan info for inode cache failed\n");
367                 jffs2_free_inode_cache(ic);
368                 return NULL;
369         }
370         memset(ic->scan, 0, sizeof(*ic->scan));
371         ic->ino = ino;
372         ic->nodes = (void *)ic;
373         jffs2_add_ino_cache(c, ic);
374         if (ino == 1)
375                 ic->nlink=1;
376         return ic;
377 }
378
379 static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *ofs)
380 {
381         struct jffs2_raw_node_ref *raw;
382         struct jffs2_full_dnode *fn;
383         struct jffs2_tmp_dnode_info *tn, **tn_list;
384         struct jffs2_inode_cache *ic;
385         struct jffs2_raw_inode ri;
386         __u32 crc;
387         __u16 oldnodetype;
388         int ret;
389         ssize_t retlen;
390
391         D1(printk(KERN_DEBUG "jffs2_scan_inode_node(): Node at 0x%08x\n", *ofs));
392
393         ret = c->mtd->read(c->mtd, *ofs, sizeof(ri), &retlen, (char *)&ri);
394         if (ret) {
395                 printk(KERN_NOTICE "jffs2_scan_inode_node(): Read error at 0x%08x: %d\n", *ofs, ret);
396                 return ret;
397         }
398         if (retlen != sizeof(ri)) {
399                 printk(KERN_NOTICE "Short read: 0x%x bytes at 0x%08x instead of requested %x\n", 
400                        retlen, *ofs, sizeof(ri));
401                 return -EIO;
402         }
403
404         /* We sort of assume that the node was accurate when it was 
405            first written to the medium :) */
406         oldnodetype = ri.nodetype;
407         ri.nodetype |= JFFS2_NODE_ACCURATE;
408         crc = crc32(0, &ri, sizeof(ri)-8);
409         ri.nodetype = oldnodetype;
410
411         if(crc != ri.node_crc) {
412                 printk(KERN_NOTICE "jffs2_scan_inode_node(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
413                        *ofs, ri.node_crc, crc);
414                 /* FIXME: Why do we believe totlen? */
415                 DIRTY_SPACE(4);
416                 *ofs += 4;
417                 return 0;
418         }
419
420         if (ri.csize) {
421                 /* Check data CRC too */
422                 unsigned char *dbuf;
423                 __u32 crc;
424
425                 dbuf = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
426                 if (!dbuf) {
427                         printk(KERN_NOTICE "jffs2_scan_inode_node(): allocation of temporary data buffer for CRC check failed\n");
428                         return -ENOMEM;
429                 }
430                 ret = c->mtd->read(c->mtd, *ofs+sizeof(ri), ri.csize, &retlen, dbuf);
431                 if (ret) {
432                         printk(KERN_NOTICE "jffs2_scan_inode_node(): Read error at 0x%08x: %d\n", *ofs+sizeof(ri), ret);
433                         kfree(dbuf);
434                         return ret;
435                 }
436                 if (retlen != ri.csize) {
437                         printk(KERN_NOTICE "Short read: 0x%x bytes at 0x%08x instead of requested %x\n", 
438                                retlen, *ofs+ sizeof(ri), ri.csize);
439                         kfree(dbuf);
440                         return -EIO;
441                 }
442                 crc = crc32(0, dbuf, ri.csize);
443                 kfree(dbuf);
444                 if (crc != ri.data_crc) {
445                         printk(KERN_NOTICE "jffs2_scan_inode_node(): Data CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
446                                *ofs, ri.data_crc, crc);
447                         DIRTY_SPACE(PAD(ri.totlen));
448                         *ofs += PAD(ri.totlen);
449                         return -0;
450                 }
451         }
452
453         /* Wheee. It worked */
454         raw = jffs2_alloc_raw_node_ref();
455         if (!raw) {
456                 printk(KERN_NOTICE "jffs2_scan_inode_node(): allocation of node reference failed\n");
457                 return -ENOMEM;
458         }
459         tn = jffs2_alloc_tmp_dnode_info();
460         if (!tn) {
461                 jffs2_free_raw_node_ref(raw);
462                 return -ENOMEM;
463         }
464         fn = jffs2_alloc_full_dnode();
465         if (!fn) {
466                 jffs2_free_tmp_dnode_info(tn);
467                 jffs2_free_raw_node_ref(raw);
468                 return -ENOMEM;
469         }
470         ic = jffs2_scan_make_ino_cache(c, ri.ino);
471         if (!ic) {
472                 jffs2_free_full_dnode(fn);
473                 jffs2_free_tmp_dnode_info(tn);
474                 jffs2_free_raw_node_ref(raw);
475                 return -ENOMEM;
476         }
477
478         /* Build the data structures and file them for later */
479         raw->flash_offset = *ofs;
480         raw->totlen = PAD(ri.totlen);
481         raw->next_phys = NULL;
482         raw->next_in_ino = ic->nodes;
483         ic->nodes = raw;
484         if (!jeb->first_node)
485                 jeb->first_node = raw;
486         if (jeb->last_node)
487                 jeb->last_node->next_phys = raw;
488         jeb->last_node = raw;
489
490         D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n", 
491                   ri.ino, ri.version, ri.offset, ri.offset+ri.dsize));
492
493         for (tn_list = &ic->scan->tmpnodes; *tn_list; tn_list = &((*tn_list)->next)) {
494                 if ((*tn_list)->version < ri.version)
495                         continue;
496                 if ((*tn_list)->version > ri.version) 
497                         break;
498                 /* Wheee. We've found another instance of the same version number.
499                    We should obsolete one of them. 
500                 */
501                 D1(printk(KERN_DEBUG "Duplicate version %d found in ino #%u. Previous one is at 0x%08x\n", ri.version, ic->ino, (*tn_list)->fn->raw->flash_offset &~3));
502                 if (!jeb->used_size) {
503                         D1(printk(KERN_DEBUG "No valid nodes yet found in this eraseblock 0x%08x, so obsoleting the new instance at 0x%08x\n", 
504                                   jeb->offset, raw->flash_offset & ~3));
505                         ri.nodetype &= ~JFFS2_NODE_ACCURATE;
506                         /* Perhaps we could also mark it as such on the medium. Maybe later */
507                 }
508                 break;
509         }
510
511         if (ri.nodetype & JFFS2_NODE_ACCURATE) {
512                 memset(fn,0,sizeof(*fn));
513
514                 fn->ofs = ri.offset;
515                 fn->size = ri.dsize;
516                 fn->frags = 0;
517                 fn->raw = raw;
518
519                 tn->next = NULL;
520                 tn->fn = fn;
521                 tn->version = ri.version;
522
523                 USED_SPACE(PAD(ri.totlen));
524                 jffs2_add_tn_to_list(tn, &ic->scan->tmpnodes);
525                 /* Make sure the one we just added is the _last_ in the list
526                    with this version number, so the older ones get obsoleted */
527                 while (tn->next && tn->next->version == tn->version) {
528
529                         D1(printk(KERN_DEBUG "Shifting new node at 0x%08x after other node at 0x%08x for version %d in list\n",
530                                   fn->raw->flash_offset&~3, tn->next->fn->raw->flash_offset &~3, ri.version));
531
532                         if(tn->fn != fn)
533                                 BUG();
534                         tn->fn = tn->next->fn;
535                         tn->next->fn = fn;
536                         tn = tn->next;
537                 }
538         } else {
539                 jffs2_free_full_dnode(fn);
540                 jffs2_free_tmp_dnode_info(tn);
541                 raw->flash_offset |= 1;
542                 DIRTY_SPACE(PAD(ri.totlen));
543         }               
544         *ofs += PAD(ri.totlen);
545         return 0;
546 }
547
548 static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *ofs)
549 {
550         struct jffs2_raw_node_ref *raw;
551         struct jffs2_full_dirent *fd;
552         struct jffs2_inode_cache *ic;
553         struct jffs2_raw_dirent rd;
554         __u16 oldnodetype;
555         int ret;
556         __u32 crc;
557         ssize_t retlen;
558
559         D1(printk(KERN_DEBUG "jffs2_scan_dirent_node(): Node at 0x%08x\n", *ofs));
560
561         ret = c->mtd->read(c->mtd, *ofs, sizeof(rd), &retlen, (char *)&rd);
562         if (ret) {
563                 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Read error at 0x%08x: %d\n", *ofs, ret);
564                 return ret;
565         }
566         if (retlen != sizeof(rd)) {
567                 printk(KERN_NOTICE "Short read: 0x%x bytes at 0x%08x instead of requested %x\n", 
568                        retlen, *ofs, sizeof(rd));
569                 return -EIO;
570         }
571
572         /* We sort of assume that the node was accurate when it was 
573            first written to the medium :) */
574         oldnodetype = rd.nodetype;
575         rd.nodetype |= JFFS2_NODE_ACCURATE;
576         crc = crc32(0, &rd, sizeof(rd)-8);
577         rd.nodetype = oldnodetype;
578
579         if (crc != rd.node_crc) {
580                 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
581                        *ofs, rd.node_crc, crc);
582                 /* FIXME: Why do we believe totlen? */
583                 DIRTY_SPACE(4);
584                 *ofs += 4;
585                 return 0;
586         }
587
588         fd = jffs2_alloc_full_dirent(rd.nsize+1);
589         if (!fd) {
590                 return -ENOMEM;
591 }
592         ret = c->mtd->read(c->mtd, *ofs + sizeof(rd), rd.nsize, &retlen, &fd->name[0]);
593         if (ret) {
594                 jffs2_free_full_dirent(fd);
595                 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Read error at 0x%08x: %d\n", 
596                        *ofs + sizeof(rd), ret);
597                 return ret;
598         }
599         if (retlen != rd.nsize) {
600                 jffs2_free_full_dirent(fd);
601                 printk(KERN_NOTICE "Short read: 0x%x bytes at 0x%08x instead of requested %x\n", 
602                        retlen, *ofs + sizeof(rd), rd.nsize);
603                 return -EIO;
604         }
605         crc = crc32(0, fd->name, rd.nsize);
606         if (crc != rd.name_crc) {
607                 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
608                        *ofs, rd.name_crc, crc); 
609                 jffs2_free_full_dirent(fd);
610                 /* FIXME: Why do we believe totlen? */
611                 DIRTY_SPACE(PAD(rd.totlen));
612                 *ofs += PAD(rd.totlen);
613                 return 0;
614         }
615         raw = jffs2_alloc_raw_node_ref();
616         if (!raw) {
617                 jffs2_free_full_dirent(fd);
618                 printk(KERN_NOTICE "jffs2_scan_dirent_node(): allocation of node reference failed\n");
619                 return -ENOMEM;
620         }
621         ic = jffs2_scan_make_ino_cache(c, rd.pino);
622         if (!ic) {
623                 jffs2_free_full_dirent(fd);
624                 jffs2_free_raw_node_ref(raw);
625                 return -ENOMEM;
626         }
627         
628         raw->totlen = PAD(rd.totlen);
629         raw->flash_offset = *ofs;
630         raw->next_phys = NULL;
631         raw->next_in_ino = ic->nodes;
632         ic->nodes = raw;
633         if (!jeb->first_node)
634                 jeb->first_node = raw;
635         if (jeb->last_node)
636                 jeb->last_node->next_phys = raw;
637         jeb->last_node = raw;
638
639         if (rd.nodetype & JFFS2_NODE_ACCURATE) {
640                 fd->raw = raw;
641                 fd->next = NULL;
642                 fd->version = rd.version;
643                 fd->ino = rd.ino;
644                 fd->name[rd.nsize]=0;
645                 fd->nhash = full_name_hash(fd->name, rd.nsize);
646                 fd->type = rd.type;
647
648                 USED_SPACE(PAD(rd.totlen));
649                 jffs2_add_fd_to_list(c, fd, &ic->scan->dents);
650         } else {
651                 raw->flash_offset |= 1;
652                 jffs2_free_full_dirent(fd);
653
654                 DIRTY_SPACE(PAD(rd.totlen));
655         } 
656         *ofs += PAD(rd.totlen);
657         return 0;
658 }