source: rtems/cpukit/libfs/src/jffs2/src/readinode.c @ 05b5f9c0

5
Last change on this file since 05b5f9c0 was 05b5f9c0, checked in by Sebastian Huber <sebastian.huber@…>, on 10/05/18 at 11:56:59

jffs2: Avoid use of constant register variable

Avoid the use of a constant register variable which is used in some
conditions. This gets rid of a clang -Wsometimes-uninitialized warning.

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