[JFFS2] Better fix for all-zero node headers
[safe/jmp/linux-2.6] / fs / jffs2 / readinode.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: readinode.c,v 1.143 2005/11/07 11:14:41 gleixner Exp $
11  *
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18 #include <linux/crc32.h>
19 #include <linux/pagemap.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/compiler.h>
22 #include "nodelist.h"
23
24 /*
25  * Check the data CRC of the node.
26  *
27  * Returns: 0 if the data CRC is correct;
28  *          1 - if incorrect;
29  *          error code if an error occured.
30  */
31 static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
32 {
33         struct jffs2_raw_node_ref *ref = tn->fn->raw;
34         int err = 0, pointed = 0;
35         struct jffs2_eraseblock *jeb;
36         unsigned char *buffer;
37         uint32_t crc, ofs, len;
38         size_t retlen;
39
40         BUG_ON(tn->csize == 0);
41
42         if (!jffs2_is_writebuffered(c))
43                 goto adj_acc;
44
45         /* Calculate how many bytes were already checked */
46         ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
47         len = ofs % c->wbuf_pagesize;
48         if (likely(len))
49                 len = c->wbuf_pagesize - len;
50
51         if (len >= tn->csize) {
52                 dbg_readinode("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
53                         ref_offset(ref), tn->csize, ofs);
54                 goto adj_acc;
55         }
56
57         ofs += len;
58         len = tn->csize - len;
59
60         dbg_readinode("check node at %#08x, data length %u, partial CRC %#08x, correct CRC %#08x, data starts at %#08x, start checking from %#08x - %u bytes.\n",
61                 ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
62
63 #ifndef __ECOS
64         /* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
65          * adding and jffs2_flash_read_end() interface. */
66         if (c->mtd->point) {
67                 err = c->mtd->point(c->mtd, ofs, len, &retlen, &buffer);
68                 if (!err && retlen < tn->csize) {
69                         JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize);
70                         c->mtd->unpoint(c->mtd, buffer, ofs, len);
71                 } else if (err)
72                         JFFS2_WARNING("MTD point failed: error code %d.\n", err);
73                 else
74                         pointed = 1; /* succefully pointed to device */
75         }
76 #endif
77
78         if (!pointed) {
79                 buffer = kmalloc(len, GFP_KERNEL);
80                 if (unlikely(!buffer))
81                         return -ENOMEM;
82
83                 /* TODO: this is very frequent pattern, make it a separate
84                  * routine */
85                 err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
86                 if (err) {
87                         JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
88                         goto free_out;
89                 }
90
91                 if (retlen != len) {
92                         JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
93                         err = -EIO;
94                         goto free_out;
95                 }
96         }
97
98         /* Continue calculating CRC */
99         crc = crc32(tn->partial_crc, buffer, len);
100         if(!pointed)
101                 kfree(buffer);
102 #ifndef __ECOS
103         else
104                 c->mtd->unpoint(c->mtd, buffer, ofs, len);
105 #endif
106
107         if (crc != tn->data_crc) {
108                 JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
109                         ofs, tn->data_crc, crc);
110                 return 1;
111         }
112
113 adj_acc:
114         jeb = &c->blocks[ref->flash_offset / c->sector_size];
115         len = ref_totlen(c, jeb, ref);
116         /* If it should be REF_NORMAL, it'll get marked as such when
117            we build the fragtree, shortly. No need to worry about GC
118            moving it while it's marked REF_PRISTINE -- GC won't happen
119            till we've finished checking every inode anyway. */
120         ref->flash_offset |= REF_PRISTINE;
121         /*
122          * Mark the node as having been checked and fix the
123          * accounting accordingly.
124          */
125         spin_lock(&c->erase_completion_lock);
126         jeb->used_size += len;
127         jeb->unchecked_size -= len;
128         c->used_size += len;
129         c->unchecked_size -= len;
130         jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
131         spin_unlock(&c->erase_completion_lock);
132
133         return 0;
134
135 free_out:
136         if(!pointed)
137                 kfree(buffer);
138 #ifndef __ECOS
139         else
140                 c->mtd->unpoint(c->mtd, buffer, ofs, len);
141 #endif
142         return err;
143 }
144
145 /*
146  * Helper function for jffs2_add_older_frag_to_fragtree().
147  *
148  * Checks the node if we are in the checking stage.
149  */
150 static int check_tn_node(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
151 {
152         int ret;
153
154         BUG_ON(ref_obsolete(tn->fn->raw));
155
156         /* We only check the data CRC of unchecked nodes */
157         if (ref_flags(tn->fn->raw) != REF_UNCHECKED)
158                 return 0;
159
160         dbg_readinode("check node %#04x-%#04x, phys offs %#08x\n",
161                       tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw));
162
163         ret = check_node_data(c, tn);
164         if (unlikely(ret < 0)) {
165                 JFFS2_ERROR("check_node_data() returned error: %d.\n",
166                         ret);
167         } else if (unlikely(ret > 0)) {
168                 dbg_readinode("CRC error, mark it obsolete.\n");
169                 jffs2_mark_node_obsolete(c, tn->fn->raw);
170         }
171
172         return ret;
173 }
174
175 static struct jffs2_tmp_dnode_info *jffs2_lookup_tn(struct rb_root *tn_root, uint32_t offset)
176 {
177         struct rb_node *next;
178         struct jffs2_tmp_dnode_info *tn = NULL;
179
180         dbg_readinode("root %p, offset %d\n", tn_root, offset);
181
182         next = tn_root->rb_node;
183
184         while (next) {
185                 tn = rb_entry(next, struct jffs2_tmp_dnode_info, rb);
186
187                 if (tn->fn->ofs < offset)
188                         next = tn->rb.rb_right;
189                 else if (tn->fn->ofs >= offset)
190                         next = tn->rb.rb_left;
191                 else
192                         break;
193         }
194
195         return tn;
196 }
197
198
199 static void jffs2_kill_tn(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
200 {
201         jffs2_mark_node_obsolete(c, tn->fn->raw);
202         jffs2_free_full_dnode(tn->fn);
203         jffs2_free_tmp_dnode_info(tn);
204 }
205 /*
206  * This function is used when we read an inode. Data nodes arrive in
207  * arbitrary order -- they may be older or newer than the nodes which
208  * are already in the tree. Where overlaps occur, the older node can
209  * be discarded as long as the newer passes the CRC check. We don't
210  * bother to keep track of holes in this rbtree, and neither do we deal
211  * with frags -- we can have multiple entries starting at the same
212  * offset, and the one with the smallest length will come first in the
213  * ordering.
214  *
215  * Returns 0 if the node was inserted
216  *         1 if the node is obsolete (because we can't mark it so yet)
217  *         < 0 an if error occurred
218  */
219 static int jffs2_add_tn_to_tree(struct jffs2_sb_info *c,
220                                 struct jffs2_readinode_info *rii,
221                                 struct jffs2_tmp_dnode_info *tn)
222 {
223         uint32_t fn_end = tn->fn->ofs + tn->fn->size;
224         struct jffs2_tmp_dnode_info *insert_point = NULL, *this;
225
226         dbg_readinode("insert fragment %#04x-%#04x, ver %u\n", tn->fn->ofs, fn_end, tn->version);
227
228         /* If a node has zero dsize, we only have to keep if it if it might be the
229            node with highest version -- i.e. the one which will end up as f->metadata.
230            Note that such nodes won't be REF_UNCHECKED since there are no data to
231            check anyway. */
232         if (!tn->fn->size) {
233                 if (rii->mdata_tn) {
234                         /* We had a candidate mdata node already */
235                         dbg_readinode("kill old mdata with ver %d\n", rii->mdata_tn->version);
236                         jffs2_kill_tn(c, rii->mdata_tn);
237                 }
238                 rii->mdata_tn = tn;
239                 dbg_readinode("keep new mdata with ver %d\n", tn->version);
240                 return 0;
241         }
242
243         /* Find the earliest node which _may_ be relevant to this one */
244         this = jffs2_lookup_tn(&rii->tn_root, tn->fn->ofs);
245         if (!this) {
246                 /* First addition to empty tree. $DEITY how I love the easy cases */
247                 rb_link_node(&tn->rb, NULL, &rii->tn_root.rb_node);
248                 rb_insert_color(&tn->rb, &rii->tn_root);
249                 dbg_readinode("keep new frag\n");
250                 return 0;
251         }
252
253         /* If we add a new node it'll be somewhere under here. */
254         insert_point = this;
255
256         /* If the node is coincident with another at a lower address,
257            back up until the other node is found. It may be relevant */
258         while (tn->overlapped)
259                 tn = tn_prev(tn);
260
261         dbg_readinode("'this' found %#04x-%#04x (%s)\n", this->fn->ofs, this->fn->ofs + this->fn->size, this->fn ? "data" : "hole");
262
263         while (this) {
264                 if (this->fn->ofs > fn_end)
265                         break;
266                 dbg_readinode("Ponder this ver %d, 0x%x-0x%x\n",
267                               this->version, this->fn->ofs, this->fn->size);
268
269                 if (this->version == tn->version) {
270                         /* Version number collision means REF_PRISTINE GC. Accept either of them
271                            as long as the CRC is correct. Check the one we have already...  */
272                         if (!check_tn_node(c, this)) {
273                                 /* The one we already had was OK. Keep it and throw away the new one */
274                                 dbg_readinode("Like old node. Throw away new\n");
275                                 jffs2_kill_tn(c, tn);
276                                 return 0;
277                         } else {
278                                 /* Who cares if the new one is good; keep it for now anyway. */
279                                 rb_replace_node(&this->rb, &tn->rb, &rii->tn_root);
280                                 /* Same overlapping from in front and behind */
281                                 tn->overlapped = this->overlapped;
282                                 jffs2_kill_tn(c, this);
283                                 dbg_readinode("Like new node. Throw away old\n");
284                                 return 0;
285                         }
286                 }
287                 if (this->version < tn->version &&
288                     this->fn->ofs >= tn->fn->ofs &&
289                     this->fn->ofs + this->fn->size <= fn_end) {
290                         /* New node entirely overlaps 'this' */
291                         if (check_tn_node(c, tn)) {
292                                 dbg_readinode("new node bad CRC\n");
293                                 jffs2_kill_tn(c, tn);
294                                 return 0;
295                         }
296                         /* ... and is good. Kill 'this'... */
297                         rb_replace_node(&this->rb, &tn->rb, &rii->tn_root);
298                         tn->overlapped = this->overlapped;
299                         jffs2_kill_tn(c, this);
300                         /* ... and any subsequent nodes which are also overlapped */
301                         this = tn_next(tn);
302                         while (this && this->fn->ofs + this->fn->size < fn_end) {
303                                 struct jffs2_tmp_dnode_info *next = tn_next(this);
304                                 if (this->version < tn->version) {
305                                         tn_erase(this, &rii->tn_root);
306                                         dbg_readinode("Kill overlapped ver %d, 0x%x-0x%x\n",
307                                                       this->version, this->fn->ofs,
308                                                       this->fn->ofs+this->fn->size);
309                                         jffs2_kill_tn(c, this);
310                                 }
311                                 this = next;
312                         }
313                         dbg_readinode("Done inserting new\n");
314                         return 0;
315                 }
316                 if (this->version > tn->version &&
317                     this->fn->ofs <= tn->fn->ofs &&
318                     this->fn->ofs+this->fn->size >= fn_end) {
319                         /* New node entirely overlapped by 'this' */
320                         if (!check_tn_node(c, this)) {
321                                 dbg_readinode("Good CRC on old node. Kill new\n");
322                                 jffs2_kill_tn(c, tn);
323                                 return 0;
324                         }
325                         /* ... but 'this' was bad. Replace it... */
326                         rb_replace_node(&this->rb, &tn->rb, &rii->tn_root);
327                         dbg_readinode("Bad CRC on old overlapping node. Kill it\n");
328                         jffs2_kill_tn(c, this);
329                         return 0;
330                 }
331                 /* We want to be inserted under the last node which is
332                    either at a lower offset _or_ has a smaller range */
333                 if (this->fn->ofs < tn->fn->ofs ||
334                     (this->fn->ofs == tn->fn->ofs &&
335                      this->fn->size <= tn->fn->size))
336                         insert_point = this;
337
338                 this = tn_next(this);
339         }
340         dbg_readinode("insert_point %p, ver %d, 0x%x-0x%x, ov %d\n",
341                       insert_point, insert_point->version, insert_point->fn->ofs,
342                       insert_point->fn->ofs+insert_point->fn->size,
343                       insert_point->overlapped);
344         /* We neither completely obsoleted nor were completely
345            obsoleted by an earlier node. Insert under insert_point */
346         {
347                 struct rb_node *parent = &insert_point->rb;
348                 struct rb_node **link = &parent;
349
350                 while (*link) {
351                         parent = *link;
352                         insert_point = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
353                         if (tn->fn->ofs > insert_point->fn->ofs)
354                                 link = &insert_point->rb.rb_right;
355                         else if (tn->fn->ofs < insert_point->fn->ofs ||
356                                  tn->fn->size < insert_point->fn->size)
357                                 link = &insert_point->rb.rb_left;
358                         else
359                                 link = &insert_point->rb.rb_right;
360                 }
361                 rb_link_node(&tn->rb, &insert_point->rb, link);
362                 rb_insert_color(&tn->rb, &rii->tn_root);
363         }
364         /* If there's anything behind that overlaps us, note it */
365         this = tn_prev(tn);
366         if (this) {
367                 while (1) {
368                         if (this->fn->ofs + this->fn->size > tn->fn->ofs) {
369                                 dbg_readinode("Node is overlapped by %p (v %d, 0x%x-0x%x)\n",
370                                               this, this->version, this->fn->ofs,
371                                               this->fn->ofs+this->fn->size);
372                                 tn->overlapped = 1;
373                                 break;
374                         }
375                         if (!this->overlapped)
376                                 break;
377                         this = tn_prev(this);
378                 }
379         }
380
381         /* If the new node overlaps anything ahead, note it */
382         this = tn_next(tn);
383         while (this && this->fn->ofs < fn_end) {
384                 this->overlapped = 1;
385                 dbg_readinode("Node ver %d, 0x%x-0x%x is overlapped\n",
386                               this->version, this->fn->ofs,
387                               this->fn->ofs+this->fn->size);
388                 this = tn_next(this);
389         }
390         return 0;
391 }
392
393 /* Trivial function to remove the last node in the tree. Which by definition
394    has no right-hand -- so can be removed just by making its only child (if
395    any) take its place under its parent. */
396 static void eat_last(struct rb_root *root, struct rb_node *node)
397 {
398         struct rb_node *parent = rb_parent(node);
399         struct rb_node **link;
400
401         /* LAST! */
402         BUG_ON(node->rb_right);
403
404         if (!parent)
405                 link = &root->rb_node;
406         else if (node == parent->rb_left)
407                 link = &parent->rb_left;
408         else
409                 link = &parent->rb_right;
410
411         *link = node->rb_left;
412         /* Colour doesn't matter now. Only the parent pointer. */
413         if (node->rb_left)
414                 node->rb_left->rb_parent_color = node->rb_parent_color;
415 }
416
417 /* We put this in reverse order, so we can just use eat_last */
418 static void ver_insert(struct rb_root *ver_root, struct jffs2_tmp_dnode_info *tn)
419 {
420         struct rb_node **link = &ver_root->rb_node;
421         struct rb_node *parent = NULL;
422         struct jffs2_tmp_dnode_info *this_tn;
423
424         while (*link) {
425                 parent = *link;
426                 this_tn = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
427
428                 if (tn->version > this_tn->version)
429                         link = &parent->rb_left;
430                 else
431                         link = &parent->rb_right;
432         }
433         dbg_readinode("Link new node at %p (root is %p)\n", link, ver_root);
434         rb_link_node(&tn->rb, parent, link);
435         rb_insert_color(&tn->rb, ver_root);
436 }
437
438 /* Build final, normal fragtree from tn tree. It doesn't matter which order
439    we add nodes to the real fragtree, as long as they don't overlap. And
440    having thrown away the majority of overlapped nodes as we went, there
441    really shouldn't be many sets of nodes which do overlap. If we start at
442    the end, we can use the overlap markers -- we can just eat nodes which
443    aren't overlapped, and when we encounter nodes which _do_ overlap we
444    sort them all into a temporary tree in version order before replaying them. */
445 static int jffs2_build_inode_fragtree(struct jffs2_sb_info *c,
446                                       struct jffs2_inode_info *f,
447                                       struct jffs2_readinode_info *rii)
448 {
449         struct jffs2_tmp_dnode_info *pen, *last, *this;
450         struct rb_root ver_root = RB_ROOT;
451         uint32_t high_ver = 0;
452
453         if (rii->mdata_tn) {
454                 dbg_readinode("potential mdata is ver %d at %p\n", rii->mdata_tn->version, rii->mdata_tn);
455                 high_ver = rii->mdata_tn->version;
456                 rii->latest_ref = rii->mdata_tn->fn->raw;
457         }
458 #ifdef JFFS2_DBG_READINODE_MESSAGES
459         this = tn_last(&rii->tn_root);
460         while (this) {
461                 dbg_readinode("tn %p ver %d range 0x%x-0x%x ov %d\n", this, this->version, this->fn->ofs,
462                              this->fn->ofs+this->fn->size, this->overlapped);
463                 this = tn_prev(this);
464         }
465 #endif
466         pen = tn_last(&rii->tn_root);
467         while ((last = pen)) {
468                 pen = tn_prev(last);
469
470                 eat_last(&rii->tn_root, &last->rb);
471                 ver_insert(&ver_root, last);
472
473                 if (unlikely(last->overlapped))
474                         continue;
475
476                 /* Now we have a bunch of nodes in reverse version
477                    order, in the tree at ver_root. Most of the time,
478                    there'll actually be only one node in the 'tree',
479                    in fact. */
480                 this = tn_last(&ver_root);
481
482                 while (this) {
483                         struct jffs2_tmp_dnode_info *vers_next;
484                         int ret;
485                         vers_next = tn_prev(this);
486                         eat_last(&ver_root, &this->rb);
487                         if (check_tn_node(c, this)) {
488                                 dbg_readinode("node ver %x, 0x%x-0x%x failed CRC\n",
489                                              this->version, this->fn->ofs,
490                                              this->fn->ofs+this->fn->size);
491                                 jffs2_kill_tn(c, this);
492                         } else {
493                                 if (this->version > high_ver) {
494                                         /* Note that this is different from the other
495                                            highest_version, because this one is only
496                                            counting _valid_ nodes which could give the
497                                            latest inode metadata */
498                                         high_ver = this->version;
499                                         rii->latest_ref = this->fn->raw;
500                                 }
501                                 dbg_readinode("Add %p (v %x, 0x%x-0x%x, ov %d) to fragtree\n",
502                                              this, this->version, this->fn->ofs,
503                                              this->fn->ofs+this->fn->size, this->overlapped);
504
505                                 ret = jffs2_add_full_dnode_to_inode(c, f, this->fn);
506                                 if (ret) {
507                                         /* Free the nodes in vers_root; let the caller
508                                            deal with the rest */
509                                         JFFS2_ERROR("Add node to tree failed %d\n", ret);
510                                         while (1) {
511                                                 vers_next = tn_prev(this);
512                                                 if (check_tn_node(c, this))
513                                                         jffs2_mark_node_obsolete(c, this->fn->raw);
514                                                 jffs2_free_full_dnode(this->fn);
515                                                 jffs2_free_tmp_dnode_info(this);
516                                                 this = vers_next;
517                                                 if (!this)
518                                                         break;
519                                                 eat_last(&ver_root, &vers_next->rb);
520                                         }
521                                         return ret;
522                                 }
523                                 jffs2_free_tmp_dnode_info(this);
524                         }
525                         this = vers_next;
526                 }
527         }
528         return 0;
529 }
530
531 static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
532 {
533         struct rb_node *this;
534         struct jffs2_tmp_dnode_info *tn;
535
536         this = list->rb_node;
537
538         /* Now at bottom of tree */
539         while (this) {
540                 if (this->rb_left)
541                         this = this->rb_left;
542                 else if (this->rb_right)
543                         this = this->rb_right;
544                 else {
545                         tn = rb_entry(this, struct jffs2_tmp_dnode_info, rb);
546                         jffs2_free_full_dnode(tn->fn);
547                         jffs2_free_tmp_dnode_info(tn);
548
549                         this = rb_parent(this);
550                         if (!this)
551                                 break;
552
553                         if (this->rb_left == &tn->rb)
554                                 this->rb_left = NULL;
555                         else if (this->rb_right == &tn->rb)
556                                 this->rb_right = NULL;
557                         else BUG();
558                 }
559         }
560         list->rb_node = NULL;
561 }
562
563 static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
564 {
565         struct jffs2_full_dirent *next;
566
567         while (fd) {
568                 next = fd->next;
569                 jffs2_free_full_dirent(fd);
570                 fd = next;
571         }
572 }
573
574 /* Returns first valid node after 'ref'. May return 'ref' */
575 static struct jffs2_raw_node_ref *jffs2_first_valid_node(struct jffs2_raw_node_ref *ref)
576 {
577         while (ref && ref->next_in_ino) {
578                 if (!ref_obsolete(ref))
579                         return ref;
580                 dbg_noderef("node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref));
581                 ref = ref->next_in_ino;
582         }
583         return NULL;
584 }
585
586 /*
587  * Helper function for jffs2_get_inode_nodes().
588  * It is called every time an directory entry node is found.
589  *
590  * Returns: 0 on succes;
591  *          1 if the node should be marked obsolete;
592  *          negative error code on failure.
593  */
594 static inline int read_direntry(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
595                                 struct jffs2_raw_dirent *rd, size_t read,
596                                 struct jffs2_readinode_info *rii)
597 {
598         struct jffs2_full_dirent *fd;
599         uint32_t crc;
600
601         /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
602         BUG_ON(ref_obsolete(ref));
603
604         crc = crc32(0, rd, sizeof(*rd) - 8);
605         if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
606                 JFFS2_NOTICE("header CRC failed on dirent node at %#08x: read %#08x, calculated %#08x\n",
607                              ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
608                 jffs2_mark_node_obsolete(c, ref);
609                 return 0;
610         }
611
612         /* If we've never checked the CRCs on this node, check them now */
613         if (ref_flags(ref) == REF_UNCHECKED) {
614                 struct jffs2_eraseblock *jeb;
615                 int len;
616
617                 /* Sanity check */
618                 if (unlikely(PAD((rd->nsize + sizeof(*rd))) != PAD(je32_to_cpu(rd->totlen)))) {
619                         JFFS2_ERROR("illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n",
620                                     ref_offset(ref), rd->nsize, je32_to_cpu(rd->totlen));
621                         jffs2_mark_node_obsolete(c, ref);
622                         return 0;
623                 }
624
625                 jeb = &c->blocks[ref->flash_offset / c->sector_size];
626                 len = ref_totlen(c, jeb, ref);
627
628                 spin_lock(&c->erase_completion_lock);
629                 jeb->used_size += len;
630                 jeb->unchecked_size -= len;
631                 c->used_size += len;
632                 c->unchecked_size -= len;
633                 ref->flash_offset = ref_offset(ref) | REF_PRISTINE;
634                 spin_unlock(&c->erase_completion_lock);
635         }
636
637         fd = jffs2_alloc_full_dirent(rd->nsize + 1);
638         if (unlikely(!fd))
639                 return -ENOMEM;
640
641         fd->raw = ref;
642         fd->version = je32_to_cpu(rd->version);
643         fd->ino = je32_to_cpu(rd->ino);
644         fd->type = rd->type;
645
646         if (fd->version > rii->highest_version)
647                 rii->highest_version = fd->version;
648
649         /* Pick out the mctime of the latest dirent */
650         if(fd->version > rii->mctime_ver && je32_to_cpu(rd->mctime)) {
651                 rii->mctime_ver = fd->version;
652                 rii->latest_mctime = je32_to_cpu(rd->mctime);
653         }
654
655         /*
656          * Copy as much of the name as possible from the raw
657          * dirent we've already read from the flash.
658          */
659         if (read > sizeof(*rd))
660                 memcpy(&fd->name[0], &rd->name[0],
661                        min_t(uint32_t, rd->nsize, (read - sizeof(*rd)) ));
662
663         /* Do we need to copy any more of the name directly from the flash? */
664         if (rd->nsize + sizeof(*rd) > read) {
665                 /* FIXME: point() */
666                 int err;
667                 int already = read - sizeof(*rd);
668
669                 err = jffs2_flash_read(c, (ref_offset(ref)) + read,
670                                 rd->nsize - already, &read, &fd->name[already]);
671                 if (unlikely(read != rd->nsize - already) && likely(!err))
672                         return -EIO;
673
674                 if (unlikely(err)) {
675                         JFFS2_ERROR("read remainder of name: error %d\n", err);
676                         jffs2_free_full_dirent(fd);
677                         return -EIO;
678                 }
679         }
680
681         fd->nhash = full_name_hash(fd->name, rd->nsize);
682         fd->next = NULL;
683         fd->name[rd->nsize] = '\0';
684
685         /*
686          * Wheee. We now have a complete jffs2_full_dirent structure, with
687          * the name in it and everything. Link it into the list
688          */
689         jffs2_add_fd_to_list(c, fd, &rii->fds);
690
691         return 0;
692 }
693
694 /*
695  * Helper function for jffs2_get_inode_nodes().
696  * It is called every time an inode node is found.
697  *
698  * Returns: 0 on success;
699  *          1 if the node should be marked obsolete;
700  *          negative error code on failure.
701  */
702 static inline int read_dnode(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
703                              struct jffs2_raw_inode *rd, int rdlen,
704                              struct jffs2_readinode_info *rii)
705 {
706         struct jffs2_tmp_dnode_info *tn;
707         uint32_t len, csize;
708         int ret = 1;
709         uint32_t crc;
710
711         /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
712         BUG_ON(ref_obsolete(ref));
713
714         crc = crc32(0, rd, sizeof(*rd) - 8);
715         if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
716                 JFFS2_NOTICE("node CRC failed on dnode at %#08x: read %#08x, calculated %#08x\n",
717                              ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
718                 jffs2_mark_node_obsolete(c, ref);
719                 return 0;
720         }
721
722         tn = jffs2_alloc_tmp_dnode_info();
723         if (!tn) {
724                 JFFS2_ERROR("failed to allocate tn (%zu bytes).\n", sizeof(*tn));
725                 return -ENOMEM;
726         }
727
728         tn->partial_crc = 0;
729         csize = je32_to_cpu(rd->csize);
730
731         /* If we've never checked the CRCs on this node, check them now */
732         if (ref_flags(ref) == REF_UNCHECKED) {
733
734                 /* Sanity checks */
735                 if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
736                     unlikely(PAD(je32_to_cpu(rd->csize) + sizeof(*rd)) != PAD(je32_to_cpu(rd->totlen)))) {
737                                 JFFS2_WARNING("inode node header CRC is corrupted at %#08x\n", ref_offset(ref));
738                                 jffs2_dbg_dump_node(c, ref_offset(ref));
739                         goto free_out;
740                 }
741
742                 if (jffs2_is_writebuffered(c) && csize != 0) {
743                         /* At this point we are supposed to check the data CRC
744                          * of our unchecked node. But thus far, we do not
745                          * know whether the node is valid or obsolete. To
746                          * figure this out, we need to walk all the nodes of
747                          * the inode and build the inode fragtree. We don't
748                          * want to spend time checking data of nodes which may
749                          * later be found to be obsolete. So we put off the full
750                          * data CRC checking until we have read all the inode
751                          * nodes and have started building the fragtree.
752                          *
753                          * The fragtree is being built starting with nodes
754                          * having the highest version number, so we'll be able
755                          * to detect whether a node is valid (i.e., it is not
756                          * overlapped by a node with higher version) or not.
757                          * And we'll be able to check only those nodes, which
758                          * are not obsolete.
759                          *
760                          * Of course, this optimization only makes sense in case
761                          * of NAND flashes (or other flashes whith
762                          * !jffs2_can_mark_obsolete()), since on NOR flashes
763                          * nodes are marked obsolete physically.
764                          *
765                          * Since NAND flashes (or other flashes with
766                          * jffs2_is_writebuffered(c)) are anyway read by
767                          * fractions of c->wbuf_pagesize, and we have just read
768                          * the node header, it is likely that the starting part
769                          * of the node data is also read when we read the
770                          * header. So we don't mind to check the CRC of the
771                          * starting part of the data of the node now, and check
772                          * the second part later (in jffs2_check_node_data()).
773                          * Of course, we will not need to re-read and re-check
774                          * the NAND page which we have just read. This is why we
775                          * read the whole NAND page at jffs2_get_inode_nodes(),
776                          * while we needed only the node header.
777                          */
778                         unsigned char *buf;
779
780                         /* 'buf' will point to the start of data */
781                         buf = (unsigned char *)rd + sizeof(*rd);
782                         /* len will be the read data length */
783                         len = min_t(uint32_t, rdlen - sizeof(*rd), csize);
784                         tn->partial_crc = crc32(0, buf, len);
785
786                         dbg_readinode("Calculates CRC (%#08x) for %d bytes, csize %d\n", tn->partial_crc, len, csize);
787
788                         /* If we actually calculated the whole data CRC
789                          * and it is wrong, drop the node. */
790                         if (len >= csize && unlikely(tn->partial_crc != je32_to_cpu(rd->data_crc))) {
791                                 JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
792                                         ref_offset(ref), tn->partial_crc, je32_to_cpu(rd->data_crc));
793                                 goto free_out;
794                         }
795
796                 } else if (csize == 0) {
797                         /*
798                          * We checked the header CRC. If the node has no data, adjust
799                          * the space accounting now. For other nodes this will be done
800                          * later either when the node is marked obsolete or when its
801                          * data is checked.
802                          */
803                         struct jffs2_eraseblock *jeb;
804
805                         dbg_readinode("the node has no data.\n");
806                         jeb = &c->blocks[ref->flash_offset / c->sector_size];
807                         len = ref_totlen(c, jeb, ref);
808
809                         spin_lock(&c->erase_completion_lock);
810                         jeb->used_size += len;
811                         jeb->unchecked_size -= len;
812                         c->used_size += len;
813                         c->unchecked_size -= len;
814                         ref->flash_offset = ref_offset(ref) | REF_NORMAL;
815                         spin_unlock(&c->erase_completion_lock);
816                 }
817         }
818
819         tn->fn = jffs2_alloc_full_dnode();
820         if (!tn->fn) {
821                 JFFS2_ERROR("alloc fn failed\n");
822                 ret = -ENOMEM;
823                 goto free_out;
824         }
825
826         tn->version = je32_to_cpu(rd->version);
827         tn->fn->ofs = je32_to_cpu(rd->offset);
828         tn->data_crc = je32_to_cpu(rd->data_crc);
829         tn->csize = csize;
830         tn->fn->raw = ref;
831         tn->overlapped = 0;
832
833         if (tn->version > rii->highest_version)
834                 rii->highest_version = tn->version;
835
836         /* There was a bug where we wrote hole nodes out with
837            csize/dsize swapped. Deal with it */
838         if (rd->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(rd->dsize) && csize)
839                 tn->fn->size = csize;
840         else // normal case...
841                 tn->fn->size = je32_to_cpu(rd->dsize);
842
843         dbg_readinode("dnode @%08x: ver %u, offset %#04x, dsize %#04x, csize %#04x\n",
844                   ref_offset(ref), je32_to_cpu(rd->version), je32_to_cpu(rd->offset), je32_to_cpu(rd->dsize), csize);
845
846         ret = jffs2_add_tn_to_tree(c, rii, tn);
847
848         if (ret) {
849                 jffs2_free_full_dnode(tn->fn);
850         free_out:
851                 jffs2_free_tmp_dnode_info(tn);
852                 return ret;
853         }
854 #ifdef JFFS2_DBG_READINODE_MESSAGES
855         dbg_readinode("After adding ver %d:\n", tn->version);
856         tn = tn_first(&rii->tn_root);
857         while (tn) {
858                 dbg_readinode("%p: v %d r 0x%x-0x%x ov %d\n",
859                              tn, tn->version, tn->fn->ofs,
860                              tn->fn->ofs+tn->fn->size, tn->overlapped);
861                 tn = tn_next(tn);
862         }
863 #endif
864         return 0;
865 }
866
867 /*
868  * Helper function for jffs2_get_inode_nodes().
869  * It is called every time an unknown node is found.
870  *
871  * Returns: 0 on success;
872  *          1 if the node should be marked obsolete;
873  *          negative error code on failure.
874  */
875 static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref, struct jffs2_unknown_node *un)
876 {
877         /* We don't mark unknown nodes as REF_UNCHECKED */
878         if (ref_flags(ref) == REF_UNCHECKED) {
879                 JFFS2_ERROR("REF_UNCHECKED but unknown node at %#08x\n",
880                             ref_offset(ref));
881                 JFFS2_ERROR("Node is {%04x,%04x,%08x,%08x}. Please report this error.\n",
882                             je16_to_cpu(un->magic), je16_to_cpu(un->nodetype),
883                             je32_to_cpu(un->totlen), je32_to_cpu(un->hdr_crc));
884                 jffs2_mark_node_obsolete(c, ref);
885                 return 0;
886         }
887
888         un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype));
889
890         switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
891
892         case JFFS2_FEATURE_INCOMPAT:
893                 JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
894                             je16_to_cpu(un->nodetype), ref_offset(ref));
895                 /* EEP */
896                 BUG();
897                 break;
898
899         case JFFS2_FEATURE_ROCOMPAT:
900                 JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
901                             je16_to_cpu(un->nodetype), ref_offset(ref));
902                 BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
903                 break;
904
905         case JFFS2_FEATURE_RWCOMPAT_COPY:
906                 JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
907                              je16_to_cpu(un->nodetype), ref_offset(ref));
908                 break;
909
910         case JFFS2_FEATURE_RWCOMPAT_DELETE:
911                 JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
912                              je16_to_cpu(un->nodetype), ref_offset(ref));
913                 jffs2_mark_node_obsolete(c, ref);
914                 return 0;
915         }
916
917         return 0;
918 }
919
920 /*
921  * Helper function for jffs2_get_inode_nodes().
922  * The function detects whether more data should be read and reads it if yes.
923  *
924  * Returns: 0 on succes;
925  *          negative error code on failure.
926  */
927 static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
928                      int needed_len, int *rdlen, unsigned char *buf)
929 {
930         int err, to_read = needed_len - *rdlen;
931         size_t retlen;
932         uint32_t offs;
933
934         if (jffs2_is_writebuffered(c)) {
935                 int rem = to_read % c->wbuf_pagesize;
936
937                 if (rem)
938                         to_read += c->wbuf_pagesize - rem;
939         }
940
941         /* We need to read more data */
942         offs = ref_offset(ref) + *rdlen;
943
944         dbg_readinode("read more %d bytes\n", to_read);
945
946         err = jffs2_flash_read(c, offs, to_read, &retlen, buf + *rdlen);
947         if (err) {
948                 JFFS2_ERROR("can not read %d bytes from 0x%08x, "
949                         "error code: %d.\n", to_read, offs, err);
950                 return err;
951         }
952
953         if (retlen < to_read) {
954                 JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n",
955                                 offs, retlen, to_read);
956                 return -EIO;
957         }
958
959         *rdlen += to_read;
960         return 0;
961 }
962
963 /* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
964    with this ino. Perform a preliminary ordering on data nodes, throwing away
965    those which are completely obsoleted by newer ones. The naïve approach we
966    use to take of just returning them _all_ in version order will cause us to
967    run out of memory in certain degenerate cases. */
968 static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
969                                  struct jffs2_readinode_info *rii)
970 {
971         struct jffs2_raw_node_ref *ref, *valid_ref;
972         unsigned char *buf = NULL;
973         union jffs2_node_union *node;
974         size_t retlen;
975         int len, err;
976
977         rii->mctime_ver = 0;
978
979         dbg_readinode("ino #%u\n", f->inocache->ino);
980
981         /* FIXME: in case of NOR and available ->point() this
982          * needs to be fixed. */
983         len = sizeof(union jffs2_node_union) + c->wbuf_pagesize;
984         buf = kmalloc(len, GFP_KERNEL);
985         if (!buf)
986                 return -ENOMEM;
987
988         spin_lock(&c->erase_completion_lock);
989         valid_ref = jffs2_first_valid_node(f->inocache->nodes);
990         if (!valid_ref && f->inocache->ino != 1)
991                 JFFS2_WARNING("Eep. No valid nodes for ino #%u.\n", f->inocache->ino);
992         while (valid_ref) {
993                 /* We can hold a pointer to a non-obsolete node without the spinlock,
994                    but _obsolete_ nodes may disappear at any time, if the block
995                    they're in gets erased. So if we mark 'ref' obsolete while we're
996                    not holding the lock, it can go away immediately. For that reason,
997                    we find the next valid node first, before processing 'ref'.
998                 */
999                 ref = valid_ref;
1000                 valid_ref = jffs2_first_valid_node(ref->next_in_ino);
1001                 spin_unlock(&c->erase_completion_lock);
1002
1003                 cond_resched();
1004
1005                 /*
1006                  * At this point we don't know the type of the node we're going
1007                  * to read, so we do not know the size of its header. In order
1008                  * to minimize the amount of flash IO we assume the header is
1009                  * of size = JFFS2_MIN_NODE_HEADER.
1010                  */
1011                 len = JFFS2_MIN_NODE_HEADER;
1012                 if (jffs2_is_writebuffered(c)) {
1013                         int end, rem;
1014
1015                         /*
1016                          * We are about to read JFFS2_MIN_NODE_HEADER bytes,
1017                          * but this flash has some minimal I/O unit. It is
1018                          * possible that we'll need to read more soon, so read
1019                          * up to the next min. I/O unit, in order not to
1020                          * re-read the same min. I/O unit twice.
1021                          */
1022                         end = ref_offset(ref) + len;
1023                         rem = end % c->wbuf_pagesize;
1024                         if (rem)
1025                                 end += c->wbuf_pagesize - rem;
1026                         len = end - ref_offset(ref);
1027                 }
1028
1029                 dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
1030
1031                 /* FIXME: point() */
1032                 err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, buf);
1033                 if (err) {
1034                         JFFS2_ERROR("can not read %d bytes from 0x%08x, " "error code: %d.\n", len, ref_offset(ref), err);
1035                         goto free_out;
1036                 }
1037
1038                 if (retlen < len) {
1039                         JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n", ref_offset(ref), retlen, len);
1040                         err = -EIO;
1041                         goto free_out;
1042                 }
1043
1044                 node = (union jffs2_node_union *)buf;
1045
1046                 /* No need to mask in the valid bit; it shouldn't be invalid */
1047                 if (je32_to_cpu(node->u.hdr_crc) != crc32(0, node, sizeof(node->u)-4)) {
1048                         JFFS2_NOTICE("Node header CRC failed at %#08x. {%04x,%04x,%08x,%08x}\n",
1049                                      ref_offset(ref), je16_to_cpu(node->u.magic),
1050                                      je16_to_cpu(node->u.nodetype),
1051                                      je32_to_cpu(node->u.totlen),
1052                                      je32_to_cpu(node->u.hdr_crc));
1053                         jffs2_dbg_dump_node(c, ref_offset(ref));
1054                         jffs2_mark_node_obsolete(c, ref);
1055                         goto cont;
1056                 }
1057                 if (je16_to_cpu(node->u.magic) != JFFS2_MAGIC_BITMASK) {
1058                         /* Not a JFFS2 node, whinge and move on */
1059                         JFFS2_NOTICE("Wrong magic bitmask 0x%04x in node header at %#08x.\n",
1060                                      je16_to_cpu(node->u.magic), ref_offset(ref));
1061                         jffs2_mark_node_obsolete(c, ref);
1062                         goto cont;
1063                 }
1064
1065                 switch (je16_to_cpu(node->u.nodetype)) {
1066
1067                 case JFFS2_NODETYPE_DIRENT:
1068
1069                         if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent)) {
1070                                 err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf);
1071                                 if (unlikely(err))
1072                                         goto free_out;
1073                         }
1074
1075                         err = read_direntry(c, ref, &node->d, retlen, rii);
1076                         if (unlikely(err))
1077                                 goto free_out;
1078
1079                         break;
1080
1081                 case JFFS2_NODETYPE_INODE:
1082
1083                         if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode)) {
1084                                 err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf);
1085                                 if (unlikely(err))
1086                                         goto free_out;
1087                         }
1088
1089                         err = read_dnode(c, ref, &node->i, len, rii);
1090                         if (unlikely(err))
1091                                 goto free_out;
1092
1093                         break;
1094
1095                 default:
1096                         if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_unknown_node)) {
1097                                 err = read_more(c, ref, sizeof(struct jffs2_unknown_node), &len, buf);
1098                                 if (unlikely(err))
1099                                         goto free_out;
1100                         }
1101
1102                         err = read_unknown(c, ref, &node->u);
1103                         if (err == 1) {
1104                                 jffs2_mark_node_obsolete(c, ref);
1105                                 break;
1106                         } else if (unlikely(err))
1107                                 goto free_out;
1108
1109                 }
1110         cont:
1111                 spin_lock(&c->erase_completion_lock);
1112         }
1113
1114         spin_unlock(&c->erase_completion_lock);
1115         kfree(buf);
1116
1117         f->highest_version = rii->highest_version;
1118
1119         dbg_readinode("nodes of inode #%u were read, the highest version is %u, latest_mctime %u, mctime_ver %u.\n",
1120                       f->inocache->ino, rii->highest_version, rii->latest_mctime,
1121                       rii->mctime_ver);
1122         return 0;
1123
1124  free_out:
1125         jffs2_free_tmp_dnode_info_list(&rii->tn_root);
1126         jffs2_free_full_dirent_list(rii->fds);
1127         rii->fds = NULL;
1128         kfree(buf);
1129         return err;
1130 }
1131
1132 static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
1133                                         struct jffs2_inode_info *f,
1134                                         struct jffs2_raw_inode *latest_node)
1135 {
1136         struct jffs2_readinode_info rii;
1137         uint32_t crc;
1138         size_t retlen;
1139         int ret;
1140
1141         dbg_readinode("ino #%u nlink is %d\n", f->inocache->ino, f->inocache->nlink);
1142
1143         memset(&rii, 0, sizeof(rii));
1144
1145         /* Grab all nodes relevant to this ino */
1146         ret = jffs2_get_inode_nodes(c, f, &rii);
1147
1148         if (ret) {
1149                 JFFS2_ERROR("cannot read nodes for ino %u, returned error is %d\n", f->inocache->ino, ret);
1150                 if (f->inocache->state == INO_STATE_READING)
1151                         jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1152                 return ret;
1153         }
1154
1155         ret = jffs2_build_inode_fragtree(c, f, &rii);
1156         if (ret) {
1157                 JFFS2_ERROR("Failed to build final fragtree for inode #%u: error %d\n",
1158                             f->inocache->ino, ret);
1159                 if (f->inocache->state == INO_STATE_READING)
1160                         jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1161                 jffs2_free_tmp_dnode_info_list(&rii.tn_root);
1162                 /* FIXME: We could at least crc-check them all */
1163                 if (rii.mdata_tn) {
1164                         jffs2_free_full_dnode(rii.mdata_tn->fn);
1165                         jffs2_free_tmp_dnode_info(rii.mdata_tn);
1166                         rii.mdata_tn = NULL;
1167                 }
1168                 return ret;
1169         }
1170
1171         if (rii.mdata_tn) {
1172                 if (rii.mdata_tn->fn->raw == rii.latest_ref) {
1173                         f->metadata = rii.mdata_tn->fn;
1174                         jffs2_free_tmp_dnode_info(rii.mdata_tn);
1175                 } else {
1176                         jffs2_kill_tn(c, rii.mdata_tn);
1177                 }
1178                 rii.mdata_tn = NULL;
1179         }
1180
1181         f->dents = rii.fds;
1182
1183         jffs2_dbg_fragtree_paranoia_check_nolock(f);
1184
1185         if (unlikely(!rii.latest_ref)) {
1186                 /* No data nodes for this inode. */
1187                 if (f->inocache->ino != 1) {
1188                         JFFS2_WARNING("no data nodes found for ino #%u\n", f->inocache->ino);
1189                         if (!rii.fds) {
1190                                 if (f->inocache->state == INO_STATE_READING)
1191                                         jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1192                                 return -EIO;
1193                         }
1194                         JFFS2_NOTICE("but it has children so we fake some modes for it\n");
1195                 }
1196                 latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
1197                 latest_node->version = cpu_to_je32(0);
1198                 latest_node->atime = latest_node->ctime = latest_node->mtime = cpu_to_je32(0);
1199                 latest_node->isize = cpu_to_je32(0);
1200                 latest_node->gid = cpu_to_je16(0);
1201                 latest_node->uid = cpu_to_je16(0);
1202                 if (f->inocache->state == INO_STATE_READING)
1203                         jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
1204                 return 0;
1205         }
1206
1207         ret = jffs2_flash_read(c, ref_offset(rii.latest_ref), sizeof(*latest_node), &retlen, (void *)latest_node);
1208         if (ret || retlen != sizeof(*latest_node)) {
1209                 JFFS2_ERROR("failed to read from flash: error %d, %zd of %zd bytes read\n",
1210                         ret, retlen, sizeof(*latest_node));
1211                 /* FIXME: If this fails, there seems to be a memory leak. Find it. */
1212                 up(&f->sem);
1213                 jffs2_do_clear_inode(c, f);
1214                 return ret?ret:-EIO;
1215         }
1216
1217         crc = crc32(0, latest_node, sizeof(*latest_node)-8);
1218         if (crc != je32_to_cpu(latest_node->node_crc)) {
1219                 JFFS2_ERROR("CRC failed for read_inode of inode %u at physical location 0x%x\n",
1220                         f->inocache->ino, ref_offset(rii.latest_ref));
1221                 up(&f->sem);
1222                 jffs2_do_clear_inode(c, f);
1223                 return -EIO;
1224         }
1225
1226         switch(jemode_to_cpu(latest_node->mode) & S_IFMT) {
1227         case S_IFDIR:
1228                 if (rii.mctime_ver > je32_to_cpu(latest_node->version)) {
1229                         /* The times in the latest_node are actually older than
1230                            mctime in the latest dirent. Cheat. */
1231                         latest_node->ctime = latest_node->mtime = cpu_to_je32(rii.latest_mctime);
1232                 }
1233                 break;
1234
1235
1236         case S_IFREG:
1237                 /* If it was a regular file, truncate it to the latest node's isize */
1238                 jffs2_truncate_fragtree(c, &f->fragtree, je32_to_cpu(latest_node->isize));
1239                 break;
1240
1241         case S_IFLNK:
1242                 /* Hack to work around broken isize in old symlink code.
1243                    Remove this when dwmw2 comes to his senses and stops
1244                    symlinks from being an entirely gratuitous special
1245                    case. */
1246                 if (!je32_to_cpu(latest_node->isize))
1247                         latest_node->isize = latest_node->dsize;
1248
1249                 if (f->inocache->state != INO_STATE_CHECKING) {
1250                         /* Symlink's inode data is the target path. Read it and
1251                          * keep in RAM to facilitate quick follow symlink
1252                          * operation. */
1253                         f->target = kmalloc(je32_to_cpu(latest_node->csize) + 1, GFP_KERNEL);
1254                         if (!f->target) {
1255                                 JFFS2_ERROR("can't allocate %d bytes of memory for the symlink target path cache\n", je32_to_cpu(latest_node->csize));
1256                                 up(&f->sem);
1257                                 jffs2_do_clear_inode(c, f);
1258                                 return -ENOMEM;
1259                         }
1260
1261                         ret = jffs2_flash_read(c, ref_offset(rii.latest_ref) + sizeof(*latest_node),
1262                                                 je32_to_cpu(latest_node->csize), &retlen, (char *)f->target);
1263
1264                         if (ret  || retlen != je32_to_cpu(latest_node->csize)) {
1265                                 if (retlen != je32_to_cpu(latest_node->csize))
1266                                         ret = -EIO;
1267                                 kfree(f->target);
1268                                 f->target = NULL;
1269                                 up(&f->sem);
1270                                 jffs2_do_clear_inode(c, f);
1271                                 return -ret;
1272                         }
1273
1274                         f->target[je32_to_cpu(latest_node->csize)] = '\0';
1275                         dbg_readinode("symlink's target '%s' cached\n", f->target);
1276                 }
1277
1278                 /* fall through... */
1279
1280         case S_IFBLK:
1281         case S_IFCHR:
1282                 /* Certain inode types should have only one data node, and it's
1283                    kept as the metadata node */
1284                 if (f->metadata) {
1285                         JFFS2_ERROR("Argh. Special inode #%u with mode 0%o had metadata node\n",
1286                                f->inocache->ino, jemode_to_cpu(latest_node->mode));
1287                         up(&f->sem);
1288                         jffs2_do_clear_inode(c, f);
1289                         return -EIO;
1290                 }
1291                 if (!frag_first(&f->fragtree)) {
1292                         JFFS2_ERROR("Argh. Special inode #%u with mode 0%o has no fragments\n",
1293                                f->inocache->ino, jemode_to_cpu(latest_node->mode));
1294                         up(&f->sem);
1295                         jffs2_do_clear_inode(c, f);
1296                         return -EIO;
1297                 }
1298                 /* ASSERT: f->fraglist != NULL */
1299                 if (frag_next(frag_first(&f->fragtree))) {
1300                         JFFS2_ERROR("Argh. Special inode #%u with mode 0x%x had more than one node\n",
1301                                f->inocache->ino, jemode_to_cpu(latest_node->mode));
1302                         /* FIXME: Deal with it - check crc32, check for duplicate node, check times and discard the older one */
1303                         up(&f->sem);
1304                         jffs2_do_clear_inode(c, f);
1305                         return -EIO;
1306                 }
1307                 /* OK. We're happy */
1308                 f->metadata = frag_first(&f->fragtree)->node;
1309                 jffs2_free_node_frag(frag_first(&f->fragtree));
1310                 f->fragtree = RB_ROOT;
1311                 break;
1312         }
1313         if (f->inocache->state == INO_STATE_READING)
1314                 jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
1315
1316         return 0;
1317 }
1318
1319 /* Scan the list of all nodes present for this ino, build map of versions, etc. */
1320 int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
1321                         uint32_t ino, struct jffs2_raw_inode *latest_node)
1322 {
1323         dbg_readinode("read inode #%u\n", ino);
1324
1325  retry_inocache:
1326         spin_lock(&c->inocache_lock);
1327         f->inocache = jffs2_get_ino_cache(c, ino);
1328
1329         if (f->inocache) {
1330                 /* Check its state. We may need to wait before we can use it */
1331                 switch(f->inocache->state) {
1332                 case INO_STATE_UNCHECKED:
1333                 case INO_STATE_CHECKEDABSENT:
1334                         f->inocache->state = INO_STATE_READING;
1335                         break;
1336
1337                 case INO_STATE_CHECKING:
1338                 case INO_STATE_GC:
1339                         /* If it's in either of these states, we need
1340                            to wait for whoever's got it to finish and
1341                            put it back. */
1342                         dbg_readinode("waiting for ino #%u in state %d\n", ino, f->inocache->state);
1343                         sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
1344                         goto retry_inocache;
1345
1346                 case INO_STATE_READING:
1347                 case INO_STATE_PRESENT:
1348                         /* Eep. This should never happen. It can
1349                         happen if Linux calls read_inode() again
1350                         before clear_inode() has finished though. */
1351                         JFFS2_ERROR("Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
1352                         /* Fail. That's probably better than allowing it to succeed */
1353                         f->inocache = NULL;
1354                         break;
1355
1356                 default:
1357                         BUG();
1358                 }
1359         }
1360         spin_unlock(&c->inocache_lock);
1361
1362         if (!f->inocache && ino == 1) {
1363                 /* Special case - no root inode on medium */
1364                 f->inocache = jffs2_alloc_inode_cache();
1365                 if (!f->inocache) {
1366                         JFFS2_ERROR("cannot allocate inocache for root inode\n");
1367                         return -ENOMEM;
1368                 }
1369                 dbg_readinode("creating inocache for root inode\n");
1370                 memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
1371                 f->inocache->ino = f->inocache->nlink = 1;
1372                 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
1373                 f->inocache->state = INO_STATE_READING;
1374                 jffs2_add_ino_cache(c, f->inocache);
1375         }
1376         if (!f->inocache) {
1377                 JFFS2_ERROR("requestied to read an nonexistent ino %u\n", ino);
1378                 return -ENOENT;
1379         }
1380
1381         return jffs2_do_read_inode_internal(c, f, latest_node);
1382 }
1383
1384 int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
1385 {
1386         struct jffs2_raw_inode n;
1387         struct jffs2_inode_info *f = kzalloc(sizeof(*f), GFP_KERNEL);
1388         int ret;
1389
1390         if (!f)
1391                 return -ENOMEM;
1392
1393         init_MUTEX_LOCKED(&f->sem);
1394         f->inocache = ic;
1395
1396         ret = jffs2_do_read_inode_internal(c, f, &n);
1397         if (!ret) {
1398                 up(&f->sem);
1399                 jffs2_do_clear_inode(c, f);
1400         }
1401         kfree (f);
1402         return ret;
1403 }
1404
1405 void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
1406 {
1407         struct jffs2_full_dirent *fd, *fds;
1408         int deleted;
1409
1410         jffs2_clear_acl(f);
1411         jffs2_xattr_delete_inode(c, f->inocache);
1412         down(&f->sem);
1413         deleted = f->inocache && !f->inocache->nlink;
1414
1415         if (f->inocache && f->inocache->state != INO_STATE_CHECKING)
1416                 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CLEARING);
1417
1418         if (f->metadata) {
1419                 if (deleted)
1420                         jffs2_mark_node_obsolete(c, f->metadata->raw);
1421                 jffs2_free_full_dnode(f->metadata);
1422         }
1423
1424         jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
1425
1426         if (f->target) {
1427                 kfree(f->target);
1428                 f->target = NULL;
1429         }
1430
1431         fds = f->dents;
1432         while(fds) {
1433                 fd = fds;
1434                 fds = fd->next;
1435                 jffs2_free_full_dirent(fd);
1436         }
1437
1438         if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
1439                 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1440                 if (f->inocache->nodes == (void *)f->inocache)
1441                         jffs2_del_ino_cache(c, f->inocache);
1442         }
1443
1444         up(&f->sem);
1445 }