Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * JFFS2 -- Journalling Flash File System, Version 2.
0003  *
0004  * Copyright © 2001-2007 Red Hat, Inc.
0005  *
0006  * Created by David Woodhouse <dwmw2@infradead.org>
0007  *
0008  * For licensing information, see the file 'LICENCE' in this directory.
0009  *
0010  */
0011 
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/sched.h>
0016 #include <linux/slab.h>
0017 #include <linux/fs.h>
0018 #include <linux/crc32.h>
0019 #include <linux/pagemap.h>
0020 #include <linux/mtd/mtd.h>
0021 #include <linux/compiler.h>
0022 #include "nodelist.h"
0023 
0024 /*
0025  * Check the data CRC of the node.
0026  *
0027  * Returns: 0 if the data CRC is correct;
0028  *      1 - if incorrect;
0029  *      error code if an error occurred.
0030  */
0031 static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
0032 {
0033     struct jffs2_raw_node_ref *ref = tn->fn->raw;
0034     int err = 0, pointed = 0;
0035     struct jffs2_eraseblock *jeb;
0036     unsigned char *buffer;
0037     uint32_t crc, ofs, len;
0038     size_t retlen;
0039 
0040     BUG_ON(tn->csize == 0);
0041 
0042     /* Calculate how many bytes were already checked */
0043     ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
0044     len = tn->csize;
0045 
0046     if (jffs2_is_writebuffered(c)) {
0047         int adj = ofs % c->wbuf_pagesize;
0048         if (likely(adj))
0049             adj = c->wbuf_pagesize - adj;
0050 
0051         if (adj >= tn->csize) {
0052             dbg_readinode("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
0053                       ref_offset(ref), tn->csize, ofs);
0054             goto adj_acc;
0055         }
0056 
0057         ofs += adj;
0058         len -= adj;
0059     }
0060 
0061     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",
0062         ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
0063 
0064 #ifndef __ECOS
0065     /* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
0066      * adding and jffs2_flash_read_end() interface. */
0067     err = mtd_point(c->mtd, ofs, len, &retlen, (void **)&buffer, NULL);
0068     if (!err && retlen < len) {
0069         JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize);
0070         mtd_unpoint(c->mtd, ofs, retlen);
0071     } else if (err) {
0072         if (err != -EOPNOTSUPP)
0073             JFFS2_WARNING("MTD point failed: error code %d.\n", err);
0074     } else
0075         pointed = 1; /* succefully pointed to device */
0076 #endif
0077 
0078     if (!pointed) {
0079         buffer = kmalloc(len, GFP_KERNEL);
0080         if (unlikely(!buffer))
0081             return -ENOMEM;
0082 
0083         /* TODO: this is very frequent pattern, make it a separate
0084          * routine */
0085         err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
0086         if (err) {
0087             JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
0088             goto free_out;
0089         }
0090 
0091         if (retlen != len) {
0092             JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
0093             err = -EIO;
0094             goto free_out;
0095         }
0096     }
0097 
0098     /* Continue calculating CRC */
0099     crc = crc32(tn->partial_crc, buffer, len);
0100     if(!pointed)
0101         kfree(buffer);
0102 #ifndef __ECOS
0103     else
0104         mtd_unpoint(c->mtd, ofs, len);
0105 #endif
0106 
0107     if (crc != tn->data_crc) {
0108         JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
0109                  ref_offset(ref), tn->data_crc, crc);
0110         return 1;
0111     }
0112 
0113 adj_acc:
0114     jeb = &c->blocks[ref->flash_offset / c->sector_size];
0115     len = ref_totlen(c, jeb, ref);
0116     /* If it should be REF_NORMAL, it'll get marked as such when
0117        we build the fragtree, shortly. No need to worry about GC
0118        moving it while it's marked REF_PRISTINE -- GC won't happen
0119        till we've finished checking every inode anyway. */
0120     ref->flash_offset |= REF_PRISTINE;
0121     /*
0122      * Mark the node as having been checked and fix the
0123      * accounting accordingly.
0124      */
0125     spin_lock(&c->erase_completion_lock);
0126     jeb->used_size += len;
0127     jeb->unchecked_size -= len;
0128     c->used_size += len;
0129     c->unchecked_size -= len;
0130     jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
0131     spin_unlock(&c->erase_completion_lock);
0132 
0133     return 0;
0134 
0135 free_out:
0136     if(!pointed)
0137         kfree(buffer);
0138 #ifndef __ECOS
0139     else
0140         mtd_unpoint(c->mtd, ofs, len);
0141 #endif
0142     return err;
0143 }
0144 
0145 /*
0146  * Helper function for jffs2_add_older_frag_to_fragtree().
0147  *
0148  * Checks the node if we are in the checking stage.
0149  */
0150 static int check_tn_node(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
0151 {
0152     int ret;
0153 
0154     BUG_ON(ref_obsolete(tn->fn->raw));
0155 
0156     /* We only check the data CRC of unchecked nodes */
0157     if (ref_flags(tn->fn->raw) != REF_UNCHECKED)
0158         return 0;
0159 
0160     dbg_readinode("check node %#04x-%#04x, phys offs %#08x\n",
0161               tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw));
0162 
0163     ret = check_node_data(c, tn);
0164     if (unlikely(ret < 0)) {
0165         JFFS2_ERROR("check_node_data() returned error: %d.\n",
0166             ret);
0167     } else if (unlikely(ret > 0)) {
0168         dbg_readinode("CRC error, mark it obsolete.\n");
0169         jffs2_mark_node_obsolete(c, tn->fn->raw);
0170     }
0171 
0172     return ret;
0173 }
0174 
0175 static struct jffs2_tmp_dnode_info *jffs2_lookup_tn(struct rb_root *tn_root, uint32_t offset)
0176 {
0177     struct rb_node *next;
0178     struct jffs2_tmp_dnode_info *tn = NULL;
0179 
0180     dbg_readinode("root %p, offset %d\n", tn_root, offset);
0181 
0182     next = tn_root->rb_node;
0183 
0184     while (next) {
0185         tn = rb_entry(next, struct jffs2_tmp_dnode_info, rb);
0186 
0187         if (tn->fn->ofs < offset)
0188             next = tn->rb.rb_right;
0189         else if (tn->fn->ofs >= offset)
0190             next = tn->rb.rb_left;
0191         else
0192             break;
0193     }
0194 
0195     return tn;
0196 }
0197 
0198 
0199 static void jffs2_kill_tn(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
0200 {
0201     jffs2_mark_node_obsolete(c, tn->fn->raw);
0202     jffs2_free_full_dnode(tn->fn);
0203     jffs2_free_tmp_dnode_info(tn);
0204 }
0205 /*
0206  * This function is used when we read an inode. Data nodes arrive in
0207  * arbitrary order -- they may be older or newer than the nodes which
0208  * are already in the tree. Where overlaps occur, the older node can
0209  * be discarded as long as the newer passes the CRC check. We don't
0210  * bother to keep track of holes in this rbtree, and neither do we deal
0211  * with frags -- we can have multiple entries starting at the same
0212  * offset, and the one with the smallest length will come first in the
0213  * ordering.
0214  *
0215  * Returns 0 if the node was handled (including marking it obsolete)
0216  *   < 0 an if error occurred
0217  */
0218 static int jffs2_add_tn_to_tree(struct jffs2_sb_info *c,
0219                 struct jffs2_readinode_info *rii,
0220                 struct jffs2_tmp_dnode_info *tn)
0221 {
0222     uint32_t fn_end = tn->fn->ofs + tn->fn->size;
0223     struct jffs2_tmp_dnode_info *this, *ptn;
0224 
0225     dbg_readinode("insert fragment %#04x-%#04x, ver %u at %08x\n", tn->fn->ofs, fn_end, tn->version, ref_offset(tn->fn->raw));
0226 
0227     /* If a node has zero dsize, we only have to keep it if it might be the
0228        node with highest version -- i.e. the one which will end up as f->metadata.
0229        Note that such nodes won't be REF_UNCHECKED since there are no data to
0230        check anyway. */
0231     if (!tn->fn->size) {
0232         if (rii->mdata_tn) {
0233             if (rii->mdata_tn->version < tn->version) {
0234                 /* We had a candidate mdata node already */
0235                 dbg_readinode("kill old mdata with ver %d\n", rii->mdata_tn->version);
0236                 jffs2_kill_tn(c, rii->mdata_tn);
0237             } else {
0238                 dbg_readinode("kill new mdata with ver %d (older than existing %d\n",
0239                           tn->version, rii->mdata_tn->version);
0240                 jffs2_kill_tn(c, tn);
0241                 return 0;
0242             }
0243         }
0244         rii->mdata_tn = tn;
0245         dbg_readinode("keep new mdata with ver %d\n", tn->version);
0246         return 0;
0247     }
0248 
0249     /* Find the earliest node which _may_ be relevant to this one */
0250     this = jffs2_lookup_tn(&rii->tn_root, tn->fn->ofs);
0251     if (this) {
0252         /* If the node is coincident with another at a lower address,
0253            back up until the other node is found. It may be relevant */
0254         while (this->overlapped) {
0255             ptn = tn_prev(this);
0256             if (!ptn) {
0257                 /*
0258                  * We killed a node which set the overlapped
0259                  * flags during the scan. Fix it up.
0260                  */
0261                 this->overlapped = 0;
0262                 break;
0263             }
0264             this = ptn;
0265         }
0266         dbg_readinode("'this' found %#04x-%#04x (%s)\n", this->fn->ofs, this->fn->ofs + this->fn->size, this->fn ? "data" : "hole");
0267     }
0268 
0269     while (this) {
0270         if (this->fn->ofs > fn_end)
0271             break;
0272         dbg_readinode("Ponder this ver %d, 0x%x-0x%x\n",
0273                   this->version, this->fn->ofs, this->fn->size);
0274 
0275         if (this->version == tn->version) {
0276             /* Version number collision means REF_PRISTINE GC. Accept either of them
0277                as long as the CRC is correct. Check the one we have already...  */
0278             if (!check_tn_node(c, this)) {
0279                 /* The one we already had was OK. Keep it and throw away the new one */
0280                 dbg_readinode("Like old node. Throw away new\n");
0281                 jffs2_kill_tn(c, tn);
0282                 return 0;
0283             } else {
0284                 /* Who cares if the new one is good; keep it for now anyway. */
0285                 dbg_readinode("Like new node. Throw away old\n");
0286                 rb_replace_node(&this->rb, &tn->rb, &rii->tn_root);
0287                 jffs2_kill_tn(c, this);
0288                 /* Same overlapping from in front and behind */
0289                 return 0;
0290             }
0291         }
0292         if (this->version < tn->version &&
0293             this->fn->ofs >= tn->fn->ofs &&
0294             this->fn->ofs + this->fn->size <= fn_end) {
0295             /* New node entirely overlaps 'this' */
0296             if (check_tn_node(c, tn)) {
0297                 dbg_readinode("new node bad CRC\n");
0298                 jffs2_kill_tn(c, tn);
0299                 return 0;
0300             }
0301             /* ... and is good. Kill 'this' and any subsequent nodes which are also overlapped */
0302             while (this && this->fn->ofs + this->fn->size <= fn_end) {
0303                 struct jffs2_tmp_dnode_info *next = tn_next(this);
0304                 if (this->version < tn->version) {
0305                     tn_erase(this, &rii->tn_root);
0306                     dbg_readinode("Kill overlapped ver %d, 0x%x-0x%x\n",
0307                               this->version, this->fn->ofs,
0308                               this->fn->ofs+this->fn->size);
0309                     jffs2_kill_tn(c, this);
0310                 }
0311                 this = next;
0312             }
0313             dbg_readinode("Done killing overlapped nodes\n");
0314             continue;
0315         }
0316         if (this->version > tn->version &&
0317             this->fn->ofs <= tn->fn->ofs &&
0318             this->fn->ofs+this->fn->size >= fn_end) {
0319             /* New node entirely overlapped by 'this' */
0320             if (!check_tn_node(c, this)) {
0321                 dbg_readinode("Good CRC on old node. Kill new\n");
0322                 jffs2_kill_tn(c, tn);
0323                 return 0;
0324             }
0325             /* ... but 'this' was bad. Replace it... */
0326             dbg_readinode("Bad CRC on old overlapping node. Kill it\n");
0327             tn_erase(this, &rii->tn_root);
0328             jffs2_kill_tn(c, this);
0329             break;
0330         }
0331 
0332         this = tn_next(this);
0333     }
0334 
0335     /* We neither completely obsoleted nor were completely
0336        obsoleted by an earlier node. Insert into the tree */
0337     {
0338         struct rb_node *parent;
0339         struct rb_node **link = &rii->tn_root.rb_node;
0340         struct jffs2_tmp_dnode_info *insert_point = NULL;
0341 
0342         while (*link) {
0343             parent = *link;
0344             insert_point = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
0345             if (tn->fn->ofs > insert_point->fn->ofs)
0346                 link = &insert_point->rb.rb_right;
0347             else if (tn->fn->ofs < insert_point->fn->ofs ||
0348                  tn->fn->size < insert_point->fn->size)
0349                 link = &insert_point->rb.rb_left;
0350             else
0351                 link = &insert_point->rb.rb_right;
0352         }
0353         rb_link_node(&tn->rb, &insert_point->rb, link);
0354         rb_insert_color(&tn->rb, &rii->tn_root);
0355     }
0356 
0357     /* If there's anything behind that overlaps us, note it */
0358     this = tn_prev(tn);
0359     if (this) {
0360         while (1) {
0361             if (this->fn->ofs + this->fn->size > tn->fn->ofs) {
0362                 dbg_readinode("Node is overlapped by %p (v %d, 0x%x-0x%x)\n",
0363                           this, this->version, this->fn->ofs,
0364                           this->fn->ofs+this->fn->size);
0365                 tn->overlapped = 1;
0366                 break;
0367             }
0368             if (!this->overlapped)
0369                 break;
0370 
0371             ptn = tn_prev(this);
0372             if (!ptn) {
0373                 /*
0374                  * We killed a node which set the overlapped
0375                  * flags during the scan. Fix it up.
0376                  */
0377                 this->overlapped = 0;
0378                 break;
0379             }
0380             this = ptn;
0381         }
0382     }
0383 
0384     /* If the new node overlaps anything ahead, note it */
0385     this = tn_next(tn);
0386     while (this && this->fn->ofs < fn_end) {
0387         this->overlapped = 1;
0388         dbg_readinode("Node ver %d, 0x%x-0x%x is overlapped\n",
0389                   this->version, this->fn->ofs,
0390                   this->fn->ofs+this->fn->size);
0391         this = tn_next(this);
0392     }
0393     return 0;
0394 }
0395 
0396 /* Trivial function to remove the last node in the tree. Which by definition
0397    has no right-hand child — so can be removed just by making its left-hand
0398    child (if any) take its place under its parent. Since this is only done
0399    when we're consuming the whole tree, there's no need to use rb_erase()
0400    and let it worry about adjusting colours and balancing the tree. That
0401    would just be a waste of time. */
0402 static void eat_last(struct rb_root *root, struct rb_node *node)
0403 {
0404     struct rb_node *parent = rb_parent(node);
0405     struct rb_node **link;
0406 
0407     /* LAST! */
0408     BUG_ON(node->rb_right);
0409 
0410     if (!parent)
0411         link = &root->rb_node;
0412     else if (node == parent->rb_left)
0413         link = &parent->rb_left;
0414     else
0415         link = &parent->rb_right;
0416 
0417     *link = node->rb_left;
0418     if (node->rb_left)
0419         node->rb_left->__rb_parent_color = node->__rb_parent_color;
0420 }
0421 
0422 /* We put the version tree in reverse order, so we can use the same eat_last()
0423    function that we use to consume the tmpnode tree (tn_root). */
0424 static void ver_insert(struct rb_root *ver_root, struct jffs2_tmp_dnode_info *tn)
0425 {
0426     struct rb_node **link = &ver_root->rb_node;
0427     struct rb_node *parent = NULL;
0428     struct jffs2_tmp_dnode_info *this_tn;
0429 
0430     while (*link) {
0431         parent = *link;
0432         this_tn = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
0433 
0434         if (tn->version > this_tn->version)
0435             link = &parent->rb_left;
0436         else
0437             link = &parent->rb_right;
0438     }
0439     dbg_readinode("Link new node at %p (root is %p)\n", link, ver_root);
0440     rb_link_node(&tn->rb, parent, link);
0441     rb_insert_color(&tn->rb, ver_root);
0442 }
0443 
0444 /* Build final, normal fragtree from tn tree. It doesn't matter which order
0445    we add nodes to the real fragtree, as long as they don't overlap. And
0446    having thrown away the majority of overlapped nodes as we went, there
0447    really shouldn't be many sets of nodes which do overlap. If we start at
0448    the end, we can use the overlap markers -- we can just eat nodes which
0449    aren't overlapped, and when we encounter nodes which _do_ overlap we
0450    sort them all into a temporary tree in version order before replaying them. */
0451 static int jffs2_build_inode_fragtree(struct jffs2_sb_info *c,
0452                       struct jffs2_inode_info *f,
0453                       struct jffs2_readinode_info *rii)
0454 {
0455     struct jffs2_tmp_dnode_info *pen, *last, *this;
0456     struct rb_root ver_root = RB_ROOT;
0457     uint32_t high_ver = 0;
0458 
0459     if (rii->mdata_tn) {
0460         dbg_readinode("potential mdata is ver %d at %p\n", rii->mdata_tn->version, rii->mdata_tn);
0461         high_ver = rii->mdata_tn->version;
0462         rii->latest_ref = rii->mdata_tn->fn->raw;
0463     }
0464 #ifdef JFFS2_DBG_READINODE_MESSAGES
0465     this = tn_last(&rii->tn_root);
0466     while (this) {
0467         dbg_readinode("tn %p ver %d range 0x%x-0x%x ov %d\n", this, this->version, this->fn->ofs,
0468                   this->fn->ofs+this->fn->size, this->overlapped);
0469         this = tn_prev(this);
0470     }
0471 #endif
0472     pen = tn_last(&rii->tn_root);
0473     while ((last = pen)) {
0474         pen = tn_prev(last);
0475 
0476         eat_last(&rii->tn_root, &last->rb);
0477         ver_insert(&ver_root, last);
0478 
0479         if (unlikely(last->overlapped)) {
0480             if (pen)
0481                 continue;
0482             /*
0483              * We killed a node which set the overlapped
0484              * flags during the scan. Fix it up.
0485              */
0486             last->overlapped = 0;
0487         }
0488 
0489         /* Now we have a bunch of nodes in reverse version
0490            order, in the tree at ver_root. Most of the time,
0491            there'll actually be only one node in the 'tree',
0492            in fact. */
0493         this = tn_last(&ver_root);
0494 
0495         while (this) {
0496             struct jffs2_tmp_dnode_info *vers_next;
0497             int ret;
0498             vers_next = tn_prev(this);
0499             eat_last(&ver_root, &this->rb);
0500             if (check_tn_node(c, this)) {
0501                 dbg_readinode("node ver %d, 0x%x-0x%x failed CRC\n",
0502                          this->version, this->fn->ofs,
0503                          this->fn->ofs+this->fn->size);
0504                 jffs2_kill_tn(c, this);
0505             } else {
0506                 if (this->version > high_ver) {
0507                     /* Note that this is different from the other
0508                        highest_version, because this one is only
0509                        counting _valid_ nodes which could give the
0510                        latest inode metadata */
0511                     high_ver = this->version;
0512                     rii->latest_ref = this->fn->raw;
0513                 }
0514                 dbg_readinode("Add %p (v %d, 0x%x-0x%x, ov %d) to fragtree\n",
0515                          this, this->version, this->fn->ofs,
0516                          this->fn->ofs+this->fn->size, this->overlapped);
0517 
0518                 ret = jffs2_add_full_dnode_to_inode(c, f, this->fn);
0519                 if (ret) {
0520                     /* Free the nodes in vers_root; let the caller
0521                        deal with the rest */
0522                     JFFS2_ERROR("Add node to tree failed %d\n", ret);
0523                     while (1) {
0524                         vers_next = tn_prev(this);
0525                         if (check_tn_node(c, this))
0526                             jffs2_mark_node_obsolete(c, this->fn->raw);
0527                         jffs2_free_full_dnode(this->fn);
0528                         jffs2_free_tmp_dnode_info(this);
0529                         this = vers_next;
0530                         if (!this)
0531                             break;
0532                         eat_last(&ver_root, &vers_next->rb);
0533                     }
0534                     return ret;
0535                 }
0536                 jffs2_free_tmp_dnode_info(this);
0537             }
0538             this = vers_next;
0539         }
0540     }
0541     return 0;
0542 }
0543 
0544 static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
0545 {
0546     struct jffs2_tmp_dnode_info *tn, *next;
0547 
0548     rbtree_postorder_for_each_entry_safe(tn, next, list, rb) {
0549             jffs2_free_full_dnode(tn->fn);
0550             jffs2_free_tmp_dnode_info(tn);
0551     }
0552 
0553     *list = RB_ROOT;
0554 }
0555 
0556 static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
0557 {
0558     struct jffs2_full_dirent *next;
0559 
0560     while (fd) {
0561         next = fd->next;
0562         jffs2_free_full_dirent(fd);
0563         fd = next;
0564     }
0565 }
0566 
0567 /* Returns first valid node after 'ref'. May return 'ref' */
0568 static struct jffs2_raw_node_ref *jffs2_first_valid_node(struct jffs2_raw_node_ref *ref)
0569 {
0570     while (ref && ref->next_in_ino) {
0571         if (!ref_obsolete(ref))
0572             return ref;
0573         dbg_noderef("node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref));
0574         ref = ref->next_in_ino;
0575     }
0576     return NULL;
0577 }
0578 
0579 /*
0580  * Helper function for jffs2_get_inode_nodes().
0581  * It is called every time an directory entry node is found.
0582  *
0583  * Returns: 0 on success;
0584  *      negative error code on failure.
0585  */
0586 static inline int read_direntry(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
0587                 struct jffs2_raw_dirent *rd, size_t read,
0588                 struct jffs2_readinode_info *rii)
0589 {
0590     struct jffs2_full_dirent *fd;
0591     uint32_t crc;
0592 
0593     /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
0594     BUG_ON(ref_obsolete(ref));
0595 
0596     crc = crc32(0, rd, sizeof(*rd) - 8);
0597     if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
0598         JFFS2_NOTICE("header CRC failed on dirent node at %#08x: read %#08x, calculated %#08x\n",
0599                  ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
0600         jffs2_mark_node_obsolete(c, ref);
0601         return 0;
0602     }
0603 
0604     /* If we've never checked the CRCs on this node, check them now */
0605     if (ref_flags(ref) == REF_UNCHECKED) {
0606         struct jffs2_eraseblock *jeb;
0607         int len;
0608 
0609         /* Sanity check */
0610         if (unlikely(PAD((rd->nsize + sizeof(*rd))) != PAD(je32_to_cpu(rd->totlen)))) {
0611             JFFS2_ERROR("illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n",
0612                     ref_offset(ref), rd->nsize, je32_to_cpu(rd->totlen));
0613             jffs2_mark_node_obsolete(c, ref);
0614             return 0;
0615         }
0616 
0617         jeb = &c->blocks[ref->flash_offset / c->sector_size];
0618         len = ref_totlen(c, jeb, ref);
0619 
0620         spin_lock(&c->erase_completion_lock);
0621         jeb->used_size += len;
0622         jeb->unchecked_size -= len;
0623         c->used_size += len;
0624         c->unchecked_size -= len;
0625         ref->flash_offset = ref_offset(ref) | dirent_node_state(rd);
0626         spin_unlock(&c->erase_completion_lock);
0627     }
0628 
0629     fd = jffs2_alloc_full_dirent(rd->nsize + 1);
0630     if (unlikely(!fd))
0631         return -ENOMEM;
0632 
0633     fd->raw = ref;
0634     fd->version = je32_to_cpu(rd->version);
0635     fd->ino = je32_to_cpu(rd->ino);
0636     fd->type = rd->type;
0637 
0638     if (fd->version > rii->highest_version)
0639         rii->highest_version = fd->version;
0640 
0641     /* Pick out the mctime of the latest dirent */
0642     if(fd->version > rii->mctime_ver && je32_to_cpu(rd->mctime)) {
0643         rii->mctime_ver = fd->version;
0644         rii->latest_mctime = je32_to_cpu(rd->mctime);
0645     }
0646 
0647     /*
0648      * Copy as much of the name as possible from the raw
0649      * dirent we've already read from the flash.
0650      */
0651     if (read > sizeof(*rd))
0652         memcpy(&fd->name[0], &rd->name[0],
0653                min_t(uint32_t, rd->nsize, (read - sizeof(*rd)) ));
0654 
0655     /* Do we need to copy any more of the name directly from the flash? */
0656     if (rd->nsize + sizeof(*rd) > read) {
0657         /* FIXME: point() */
0658         int err;
0659         int already = read - sizeof(*rd);
0660 
0661         err = jffs2_flash_read(c, (ref_offset(ref)) + read,
0662                 rd->nsize - already, &read, &fd->name[already]);
0663         if (unlikely(read != rd->nsize - already) && likely(!err)) {
0664             jffs2_free_full_dirent(fd);
0665             JFFS2_ERROR("short read: wanted %d bytes, got %zd\n",
0666                     rd->nsize - already, read);
0667             return -EIO;
0668         }
0669 
0670         if (unlikely(err)) {
0671             JFFS2_ERROR("read remainder of name: error %d\n", err);
0672             jffs2_free_full_dirent(fd);
0673             return -EIO;
0674         }
0675 
0676 #ifdef CONFIG_JFFS2_SUMMARY
0677         /*
0678          * we use CONFIG_JFFS2_SUMMARY because without it, we
0679          * have checked it while mounting
0680          */
0681         crc = crc32(0, fd->name, rd->nsize);
0682         if (unlikely(crc != je32_to_cpu(rd->name_crc))) {
0683             JFFS2_NOTICE("name CRC failed on dirent node at"
0684                "%#08x: read %#08x,calculated %#08x\n",
0685                ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
0686             jffs2_mark_node_obsolete(c, ref);
0687             jffs2_free_full_dirent(fd);
0688             return 0;
0689         }
0690 #endif
0691     }
0692 
0693     fd->nhash = full_name_hash(NULL, fd->name, rd->nsize);
0694     fd->next = NULL;
0695     fd->name[rd->nsize] = '\0';
0696 
0697     /*
0698      * Wheee. We now have a complete jffs2_full_dirent structure, with
0699      * the name in it and everything. Link it into the list
0700      */
0701     jffs2_add_fd_to_list(c, fd, &rii->fds);
0702 
0703     return 0;
0704 }
0705 
0706 /*
0707  * Helper function for jffs2_get_inode_nodes().
0708  * It is called every time an inode node is found.
0709  *
0710  * Returns: 0 on success (possibly after marking a bad node obsolete);
0711  *      negative error code on failure.
0712  */
0713 static inline int read_dnode(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
0714                  struct jffs2_raw_inode *rd, int rdlen,
0715                  struct jffs2_readinode_info *rii)
0716 {
0717     struct jffs2_tmp_dnode_info *tn;
0718     uint32_t len, csize;
0719     int ret = 0;
0720     uint32_t crc;
0721 
0722     /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
0723     BUG_ON(ref_obsolete(ref));
0724 
0725     crc = crc32(0, rd, sizeof(*rd) - 8);
0726     if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
0727         JFFS2_NOTICE("node CRC failed on dnode at %#08x: read %#08x, calculated %#08x\n",
0728                  ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
0729         jffs2_mark_node_obsolete(c, ref);
0730         return 0;
0731     }
0732 
0733     tn = jffs2_alloc_tmp_dnode_info();
0734     if (!tn) {
0735         JFFS2_ERROR("failed to allocate tn (%zu bytes).\n", sizeof(*tn));
0736         return -ENOMEM;
0737     }
0738 
0739     tn->partial_crc = 0;
0740     csize = je32_to_cpu(rd->csize);
0741 
0742     /* If we've never checked the CRCs on this node, check them now */
0743     if (ref_flags(ref) == REF_UNCHECKED) {
0744 
0745         /* Sanity checks */
0746         if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
0747             unlikely(PAD(je32_to_cpu(rd->csize) + sizeof(*rd)) != PAD(je32_to_cpu(rd->totlen)))) {
0748             JFFS2_WARNING("inode node header CRC is corrupted at %#08x\n", ref_offset(ref));
0749             jffs2_dbg_dump_node(c, ref_offset(ref));
0750             jffs2_mark_node_obsolete(c, ref);
0751             goto free_out;
0752         }
0753 
0754         if (jffs2_is_writebuffered(c) && csize != 0) {
0755             /* At this point we are supposed to check the data CRC
0756              * of our unchecked node. But thus far, we do not
0757              * know whether the node is valid or obsolete. To
0758              * figure this out, we need to walk all the nodes of
0759              * the inode and build the inode fragtree. We don't
0760              * want to spend time checking data of nodes which may
0761              * later be found to be obsolete. So we put off the full
0762              * data CRC checking until we have read all the inode
0763              * nodes and have started building the fragtree.
0764              *
0765              * The fragtree is being built starting with nodes
0766              * having the highest version number, so we'll be able
0767              * to detect whether a node is valid (i.e., it is not
0768              * overlapped by a node with higher version) or not.
0769              * And we'll be able to check only those nodes, which
0770              * are not obsolete.
0771              *
0772              * Of course, this optimization only makes sense in case
0773              * of NAND flashes (or other flashes with
0774              * !jffs2_can_mark_obsolete()), since on NOR flashes
0775              * nodes are marked obsolete physically.
0776              *
0777              * Since NAND flashes (or other flashes with
0778              * jffs2_is_writebuffered(c)) are anyway read by
0779              * fractions of c->wbuf_pagesize, and we have just read
0780              * the node header, it is likely that the starting part
0781              * of the node data is also read when we read the
0782              * header. So we don't mind to check the CRC of the
0783              * starting part of the data of the node now, and check
0784              * the second part later (in jffs2_check_node_data()).
0785              * Of course, we will not need to re-read and re-check
0786              * the NAND page which we have just read. This is why we
0787              * read the whole NAND page at jffs2_get_inode_nodes(),
0788              * while we needed only the node header.
0789              */
0790             unsigned char *buf;
0791 
0792             /* 'buf' will point to the start of data */
0793             buf = (unsigned char *)rd + sizeof(*rd);
0794             /* len will be the read data length */
0795             len = min_t(uint32_t, rdlen - sizeof(*rd), csize);
0796             tn->partial_crc = crc32(0, buf, len);
0797 
0798             dbg_readinode("Calculates CRC (%#08x) for %d bytes, csize %d\n", tn->partial_crc, len, csize);
0799 
0800             /* If we actually calculated the whole data CRC
0801              * and it is wrong, drop the node. */
0802             if (len >= csize && unlikely(tn->partial_crc != je32_to_cpu(rd->data_crc))) {
0803                 JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
0804                     ref_offset(ref), tn->partial_crc, je32_to_cpu(rd->data_crc));
0805                 jffs2_mark_node_obsolete(c, ref);
0806                 goto free_out;
0807             }
0808 
0809         } else if (csize == 0) {
0810             /*
0811              * We checked the header CRC. If the node has no data, adjust
0812              * the space accounting now. For other nodes this will be done
0813              * later either when the node is marked obsolete or when its
0814              * data is checked.
0815              */
0816             struct jffs2_eraseblock *jeb;
0817 
0818             dbg_readinode("the node has no data.\n");
0819             jeb = &c->blocks[ref->flash_offset / c->sector_size];
0820             len = ref_totlen(c, jeb, ref);
0821 
0822             spin_lock(&c->erase_completion_lock);
0823             jeb->used_size += len;
0824             jeb->unchecked_size -= len;
0825             c->used_size += len;
0826             c->unchecked_size -= len;
0827             ref->flash_offset = ref_offset(ref) | REF_NORMAL;
0828             spin_unlock(&c->erase_completion_lock);
0829         }
0830     }
0831 
0832     tn->fn = jffs2_alloc_full_dnode();
0833     if (!tn->fn) {
0834         JFFS2_ERROR("alloc fn failed\n");
0835         ret = -ENOMEM;
0836         goto free_out;
0837     }
0838 
0839     tn->version = je32_to_cpu(rd->version);
0840     tn->fn->ofs = je32_to_cpu(rd->offset);
0841     tn->data_crc = je32_to_cpu(rd->data_crc);
0842     tn->csize = csize;
0843     tn->fn->raw = ref;
0844     tn->overlapped = 0;
0845 
0846     if (tn->version > rii->highest_version)
0847         rii->highest_version = tn->version;
0848 
0849     /* There was a bug where we wrote hole nodes out with
0850        csize/dsize swapped. Deal with it */
0851     if (rd->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(rd->dsize) && csize)
0852         tn->fn->size = csize;
0853     else // normal case...
0854         tn->fn->size = je32_to_cpu(rd->dsize);
0855 
0856     dbg_readinode2("dnode @%08x: ver %u, offset %#04x, dsize %#04x, csize %#04x\n",
0857                ref_offset(ref), je32_to_cpu(rd->version),
0858                je32_to_cpu(rd->offset), je32_to_cpu(rd->dsize), csize);
0859 
0860     ret = jffs2_add_tn_to_tree(c, rii, tn);
0861 
0862     if (ret) {
0863         jffs2_free_full_dnode(tn->fn);
0864     free_out:
0865         jffs2_free_tmp_dnode_info(tn);
0866         return ret;
0867     }
0868 #ifdef JFFS2_DBG_READINODE2_MESSAGES
0869     dbg_readinode2("After adding ver %d:\n", je32_to_cpu(rd->version));
0870     tn = tn_first(&rii->tn_root);
0871     while (tn) {
0872         dbg_readinode2("%p: v %d r 0x%x-0x%x ov %d\n",
0873                    tn, tn->version, tn->fn->ofs,
0874                    tn->fn->ofs+tn->fn->size, tn->overlapped);
0875         tn = tn_next(tn);
0876     }
0877 #endif
0878     return 0;
0879 }
0880 
0881 /*
0882  * Helper function for jffs2_get_inode_nodes().
0883  * It is called every time an unknown node is found.
0884  *
0885  * Returns: 0 on success;
0886  *      negative error code on failure.
0887  */
0888 static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref, struct jffs2_unknown_node *un)
0889 {
0890     /* We don't mark unknown nodes as REF_UNCHECKED */
0891     if (ref_flags(ref) == REF_UNCHECKED) {
0892         JFFS2_ERROR("REF_UNCHECKED but unknown node at %#08x\n",
0893                 ref_offset(ref));
0894         JFFS2_ERROR("Node is {%04x,%04x,%08x,%08x}. Please report this error.\n",
0895                 je16_to_cpu(un->magic), je16_to_cpu(un->nodetype),
0896                 je32_to_cpu(un->totlen), je32_to_cpu(un->hdr_crc));
0897         jffs2_mark_node_obsolete(c, ref);
0898         return 0;
0899     }
0900 
0901     un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype));
0902 
0903     switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
0904 
0905     case JFFS2_FEATURE_INCOMPAT:
0906         JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
0907                 je16_to_cpu(un->nodetype), ref_offset(ref));
0908         /* EEP */
0909         BUG();
0910         break;
0911 
0912     case JFFS2_FEATURE_ROCOMPAT:
0913         JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
0914                 je16_to_cpu(un->nodetype), ref_offset(ref));
0915         BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
0916         break;
0917 
0918     case JFFS2_FEATURE_RWCOMPAT_COPY:
0919         JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
0920                  je16_to_cpu(un->nodetype), ref_offset(ref));
0921         break;
0922 
0923     case JFFS2_FEATURE_RWCOMPAT_DELETE:
0924         JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
0925                  je16_to_cpu(un->nodetype), ref_offset(ref));
0926         jffs2_mark_node_obsolete(c, ref);
0927         return 0;
0928     }
0929 
0930     return 0;
0931 }
0932 
0933 /*
0934  * Helper function for jffs2_get_inode_nodes().
0935  * The function detects whether more data should be read and reads it if yes.
0936  *
0937  * Returns: 0 on success;
0938  *      negative error code on failure.
0939  */
0940 static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
0941              int needed_len, int *rdlen, unsigned char *buf)
0942 {
0943     int err, to_read = needed_len - *rdlen;
0944     size_t retlen;
0945     uint32_t offs;
0946 
0947     if (jffs2_is_writebuffered(c)) {
0948         int rem = to_read % c->wbuf_pagesize;
0949 
0950         if (rem)
0951             to_read += c->wbuf_pagesize - rem;
0952     }
0953 
0954     /* We need to read more data */
0955     offs = ref_offset(ref) + *rdlen;
0956 
0957     dbg_readinode("read more %d bytes\n", to_read);
0958 
0959     err = jffs2_flash_read(c, offs, to_read, &retlen, buf + *rdlen);
0960     if (err) {
0961         JFFS2_ERROR("can not read %d bytes from 0x%08x, "
0962             "error code: %d.\n", to_read, offs, err);
0963         return err;
0964     }
0965 
0966     if (retlen < to_read) {
0967         JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n",
0968                 offs, retlen, to_read);
0969         return -EIO;
0970     }
0971 
0972     *rdlen += to_read;
0973     return 0;
0974 }
0975 
0976 /* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
0977    with this ino. Perform a preliminary ordering on data nodes, throwing away
0978    those which are completely obsoleted by newer ones. The naïve approach we
0979    use to take of just returning them _all_ in version order will cause us to
0980    run out of memory in certain degenerate cases. */
0981 static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
0982                  struct jffs2_readinode_info *rii)
0983 {
0984     struct jffs2_raw_node_ref *ref, *valid_ref;
0985     unsigned char *buf = NULL;
0986     union jffs2_node_union *node;
0987     size_t retlen;
0988     int len, err;
0989 
0990     rii->mctime_ver = 0;
0991 
0992     dbg_readinode("ino #%u\n", f->inocache->ino);
0993 
0994     /* FIXME: in case of NOR and available ->point() this
0995      * needs to be fixed. */
0996     len = sizeof(union jffs2_node_union) + c->wbuf_pagesize;
0997     buf = kmalloc(len, GFP_KERNEL);
0998     if (!buf)
0999         return -ENOMEM;
1000 
1001     spin_lock(&c->erase_completion_lock);
1002     valid_ref = jffs2_first_valid_node(f->inocache->nodes);
1003     if (!valid_ref && f->inocache->ino != 1)
1004         JFFS2_WARNING("Eep. No valid nodes for ino #%u.\n", f->inocache->ino);
1005     while (valid_ref) {
1006         /* We can hold a pointer to a non-obsolete node without the spinlock,
1007            but _obsolete_ nodes may disappear at any time, if the block
1008            they're in gets erased. So if we mark 'ref' obsolete while we're
1009            not holding the lock, it can go away immediately. For that reason,
1010            we find the next valid node first, before processing 'ref'.
1011         */
1012         ref = valid_ref;
1013         valid_ref = jffs2_first_valid_node(ref->next_in_ino);
1014         spin_unlock(&c->erase_completion_lock);
1015 
1016         cond_resched();
1017 
1018         /*
1019          * At this point we don't know the type of the node we're going
1020          * to read, so we do not know the size of its header. In order
1021          * to minimize the amount of flash IO we assume the header is
1022          * of size = JFFS2_MIN_NODE_HEADER.
1023          */
1024         len = JFFS2_MIN_NODE_HEADER;
1025         if (jffs2_is_writebuffered(c)) {
1026             int end, rem;
1027 
1028             /*
1029              * We are about to read JFFS2_MIN_NODE_HEADER bytes,
1030              * but this flash has some minimal I/O unit. It is
1031              * possible that we'll need to read more soon, so read
1032              * up to the next min. I/O unit, in order not to
1033              * re-read the same min. I/O unit twice.
1034              */
1035             end = ref_offset(ref) + len;
1036             rem = end % c->wbuf_pagesize;
1037             if (rem)
1038                 end += c->wbuf_pagesize - rem;
1039             len = end - ref_offset(ref);
1040         }
1041 
1042         dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
1043 
1044         /* FIXME: point() */
1045         err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, buf);
1046         if (err) {
1047             JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ref_offset(ref), err);
1048             goto free_out;
1049         }
1050 
1051         if (retlen < len) {
1052             JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n", ref_offset(ref), retlen, len);
1053             err = -EIO;
1054             goto free_out;
1055         }
1056 
1057         node = (union jffs2_node_union *)buf;
1058 
1059         /* No need to mask in the valid bit; it shouldn't be invalid */
1060         if (je32_to_cpu(node->u.hdr_crc) != crc32(0, node, sizeof(node->u)-4)) {
1061             JFFS2_NOTICE("Node header CRC failed at %#08x. {%04x,%04x,%08x,%08x}\n",
1062                      ref_offset(ref), je16_to_cpu(node->u.magic),
1063                      je16_to_cpu(node->u.nodetype),
1064                      je32_to_cpu(node->u.totlen),
1065                      je32_to_cpu(node->u.hdr_crc));
1066             jffs2_dbg_dump_node(c, ref_offset(ref));
1067             jffs2_mark_node_obsolete(c, ref);
1068             goto cont;
1069         }
1070         if (je16_to_cpu(node->u.magic) != JFFS2_MAGIC_BITMASK) {
1071             /* Not a JFFS2 node, whinge and move on */
1072             JFFS2_NOTICE("Wrong magic bitmask 0x%04x in node header at %#08x.\n",
1073                      je16_to_cpu(node->u.magic), ref_offset(ref));
1074             jffs2_mark_node_obsolete(c, ref);
1075             goto cont;
1076         }
1077 
1078         switch (je16_to_cpu(node->u.nodetype)) {
1079 
1080         case JFFS2_NODETYPE_DIRENT:
1081 
1082             if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent) &&
1083                 len < sizeof(struct jffs2_raw_dirent)) {
1084                 err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf);
1085                 if (unlikely(err))
1086                     goto free_out;
1087             }
1088 
1089             err = read_direntry(c, ref, &node->d, retlen, rii);
1090             if (unlikely(err))
1091                 goto free_out;
1092 
1093             break;
1094 
1095         case JFFS2_NODETYPE_INODE:
1096 
1097             if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode) &&
1098                 len < sizeof(struct jffs2_raw_inode)) {
1099                 err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf);
1100                 if (unlikely(err))
1101                     goto free_out;
1102             }
1103 
1104             err = read_dnode(c, ref, &node->i, len, rii);
1105             if (unlikely(err))
1106                 goto free_out;
1107 
1108             break;
1109 
1110         default:
1111             if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_unknown_node) &&
1112                 len < sizeof(struct jffs2_unknown_node)) {
1113                 err = read_more(c, ref, sizeof(struct jffs2_unknown_node), &len, buf);
1114                 if (unlikely(err))
1115                     goto free_out;
1116             }
1117 
1118             err = read_unknown(c, ref, &node->u);
1119             if (unlikely(err))
1120                 goto free_out;
1121 
1122         }
1123     cont:
1124         spin_lock(&c->erase_completion_lock);
1125     }
1126 
1127     spin_unlock(&c->erase_completion_lock);
1128     kfree(buf);
1129 
1130     f->highest_version = rii->highest_version;
1131 
1132     dbg_readinode("nodes of inode #%u were read, the highest version is %u, latest_mctime %u, mctime_ver %u.\n",
1133               f->inocache->ino, rii->highest_version, rii->latest_mctime,
1134               rii->mctime_ver);
1135     return 0;
1136 
1137  free_out:
1138     jffs2_free_tmp_dnode_info_list(&rii->tn_root);
1139     jffs2_free_full_dirent_list(rii->fds);
1140     rii->fds = NULL;
1141     kfree(buf);
1142     return err;
1143 }
1144 
1145 static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
1146                     struct jffs2_inode_info *f,
1147                     struct jffs2_raw_inode *latest_node)
1148 {
1149     struct jffs2_readinode_info rii;
1150     uint32_t crc, new_size;
1151     size_t retlen;
1152     int ret;
1153 
1154     dbg_readinode("ino #%u pino/nlink is %d\n", f->inocache->ino,
1155               f->inocache->pino_nlink);
1156 
1157     memset(&rii, 0, sizeof(rii));
1158 
1159     /* Grab all nodes relevant to this ino */
1160     ret = jffs2_get_inode_nodes(c, f, &rii);
1161 
1162     if (ret) {
1163         JFFS2_ERROR("cannot read nodes for ino %u, returned error is %d\n", f->inocache->ino, ret);
1164         if (f->inocache->state == INO_STATE_READING)
1165             jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1166         return ret;
1167     }
1168 
1169     ret = jffs2_build_inode_fragtree(c, f, &rii);
1170     if (ret) {
1171         JFFS2_ERROR("Failed to build final fragtree for inode #%u: error %d\n",
1172                 f->inocache->ino, ret);
1173         if (f->inocache->state == INO_STATE_READING)
1174             jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1175         jffs2_free_tmp_dnode_info_list(&rii.tn_root);
1176         /* FIXME: We could at least crc-check them all */
1177         if (rii.mdata_tn) {
1178             jffs2_free_full_dnode(rii.mdata_tn->fn);
1179             jffs2_free_tmp_dnode_info(rii.mdata_tn);
1180             rii.mdata_tn = NULL;
1181         }
1182         return ret;
1183     }
1184 
1185     if (rii.mdata_tn) {
1186         if (rii.mdata_tn->fn->raw == rii.latest_ref) {
1187             f->metadata = rii.mdata_tn->fn;
1188             jffs2_free_tmp_dnode_info(rii.mdata_tn);
1189         } else {
1190             jffs2_kill_tn(c, rii.mdata_tn);
1191         }
1192         rii.mdata_tn = NULL;
1193     }
1194 
1195     f->dents = rii.fds;
1196 
1197     jffs2_dbg_fragtree_paranoia_check_nolock(f);
1198 
1199     if (unlikely(!rii.latest_ref)) {
1200         /* No data nodes for this inode. */
1201         if (f->inocache->ino != 1) {
1202             JFFS2_WARNING("no data nodes found for ino #%u\n", f->inocache->ino);
1203             if (!rii.fds) {
1204                 if (f->inocache->state == INO_STATE_READING)
1205                     jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1206                 return -EIO;
1207             }
1208             JFFS2_NOTICE("but it has children so we fake some modes for it\n");
1209         }
1210         latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
1211         latest_node->version = cpu_to_je32(0);
1212         latest_node->atime = latest_node->ctime = latest_node->mtime = cpu_to_je32(0);
1213         latest_node->isize = cpu_to_je32(0);
1214         latest_node->gid = cpu_to_je16(0);
1215         latest_node->uid = cpu_to_je16(0);
1216         if (f->inocache->state == INO_STATE_READING)
1217             jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
1218         return 0;
1219     }
1220 
1221     ret = jffs2_flash_read(c, ref_offset(rii.latest_ref), sizeof(*latest_node), &retlen, (void *)latest_node);
1222     if (ret || retlen != sizeof(*latest_node)) {
1223         JFFS2_ERROR("failed to read from flash: error %d, %zd of %zd bytes read\n",
1224             ret, retlen, sizeof(*latest_node));
1225         /* FIXME: If this fails, there seems to be a memory leak. Find it. */
1226         return ret ? ret : -EIO;
1227     }
1228 
1229     crc = crc32(0, latest_node, sizeof(*latest_node)-8);
1230     if (crc != je32_to_cpu(latest_node->node_crc)) {
1231         JFFS2_ERROR("CRC failed for read_inode of inode %u at physical location 0x%x\n",
1232             f->inocache->ino, ref_offset(rii.latest_ref));
1233         return -EIO;
1234     }
1235 
1236     switch(jemode_to_cpu(latest_node->mode) & S_IFMT) {
1237     case S_IFDIR:
1238         if (rii.mctime_ver > je32_to_cpu(latest_node->version)) {
1239             /* The times in the latest_node are actually older than
1240                mctime in the latest dirent. Cheat. */
1241             latest_node->ctime = latest_node->mtime = cpu_to_je32(rii.latest_mctime);
1242         }
1243         break;
1244 
1245 
1246     case S_IFREG:
1247         /* If it was a regular file, truncate it to the latest node's isize */
1248         new_size = jffs2_truncate_fragtree(c, &f->fragtree, je32_to_cpu(latest_node->isize));
1249         if (new_size != je32_to_cpu(latest_node->isize)) {
1250             JFFS2_WARNING("Truncating ino #%u to %d bytes failed because it only had %d bytes to start with!\n",
1251                       f->inocache->ino, je32_to_cpu(latest_node->isize), new_size);
1252             latest_node->isize = cpu_to_je32(new_size);
1253         }
1254         break;
1255 
1256     case S_IFLNK:
1257         /* Hack to work around broken isize in old symlink code.
1258            Remove this when dwmw2 comes to his senses and stops
1259            symlinks from being an entirely gratuitous special
1260            case. */
1261         if (!je32_to_cpu(latest_node->isize))
1262             latest_node->isize = latest_node->dsize;
1263 
1264         if (f->inocache->state != INO_STATE_CHECKING) {
1265             /* Symlink's inode data is the target path. Read it and
1266              * keep in RAM to facilitate quick follow symlink
1267              * operation. */
1268             uint32_t csize = je32_to_cpu(latest_node->csize);
1269             if (csize > JFFS2_MAX_NAME_LEN)
1270                 return -ENAMETOOLONG;
1271             f->target = kmalloc(csize + 1, GFP_KERNEL);
1272             if (!f->target) {
1273                 JFFS2_ERROR("can't allocate %u bytes of memory for the symlink target path cache\n", csize);
1274                 return -ENOMEM;
1275             }
1276 
1277             ret = jffs2_flash_read(c, ref_offset(rii.latest_ref) + sizeof(*latest_node),
1278                            csize, &retlen, (char *)f->target);
1279 
1280             if (ret || retlen != csize) {
1281                 if (retlen != csize)
1282                     ret = -EIO;
1283                 kfree(f->target);
1284                 f->target = NULL;
1285                 return ret;
1286             }
1287 
1288             f->target[csize] = '\0';
1289             dbg_readinode("symlink's target '%s' cached\n", f->target);
1290         }
1291 
1292         fallthrough;
1293 
1294     case S_IFBLK:
1295     case S_IFCHR:
1296         /* Certain inode types should have only one data node, and it's
1297            kept as the metadata node */
1298         if (f->metadata) {
1299             JFFS2_ERROR("Argh. Special inode #%u with mode 0%o had metadata node\n",
1300                    f->inocache->ino, jemode_to_cpu(latest_node->mode));
1301             return -EIO;
1302         }
1303         if (!frag_first(&f->fragtree)) {
1304             JFFS2_ERROR("Argh. Special inode #%u with mode 0%o has no fragments\n",
1305                    f->inocache->ino, jemode_to_cpu(latest_node->mode));
1306             return -EIO;
1307         }
1308         /* ASSERT: f->fraglist != NULL */
1309         if (frag_next(frag_first(&f->fragtree))) {
1310             JFFS2_ERROR("Argh. Special inode #%u with mode 0x%x had more than one node\n",
1311                    f->inocache->ino, jemode_to_cpu(latest_node->mode));
1312             /* FIXME: Deal with it - check crc32, check for duplicate node, check times and discard the older one */
1313             return -EIO;
1314         }
1315         /* OK. We're happy */
1316         f->metadata = frag_first(&f->fragtree)->node;
1317         jffs2_free_node_frag(frag_first(&f->fragtree));
1318         f->fragtree = RB_ROOT;
1319         break;
1320     }
1321     if (f->inocache->state == INO_STATE_READING)
1322         jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
1323 
1324     return 0;
1325 }
1326 
1327 /* Scan the list of all nodes present for this ino, build map of versions, etc. */
1328 int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
1329             uint32_t ino, struct jffs2_raw_inode *latest_node)
1330 {
1331     dbg_readinode("read inode #%u\n", ino);
1332 
1333  retry_inocache:
1334     spin_lock(&c->inocache_lock);
1335     f->inocache = jffs2_get_ino_cache(c, ino);
1336 
1337     if (f->inocache) {
1338         /* Check its state. We may need to wait before we can use it */
1339         switch(f->inocache->state) {
1340         case INO_STATE_UNCHECKED:
1341         case INO_STATE_CHECKEDABSENT:
1342             f->inocache->state = INO_STATE_READING;
1343             break;
1344 
1345         case INO_STATE_CHECKING:
1346         case INO_STATE_GC:
1347             /* If it's in either of these states, we need
1348                to wait for whoever's got it to finish and
1349                put it back. */
1350             dbg_readinode("waiting for ino #%u in state %d\n", ino, f->inocache->state);
1351             sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
1352             goto retry_inocache;
1353 
1354         case INO_STATE_READING:
1355         case INO_STATE_PRESENT:
1356             /* Eep. This should never happen. It can
1357             happen if Linux calls read_inode() again
1358             before clear_inode() has finished though. */
1359             JFFS2_ERROR("Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
1360             /* Fail. That's probably better than allowing it to succeed */
1361             f->inocache = NULL;
1362             break;
1363 
1364         default:
1365             BUG();
1366         }
1367     }
1368     spin_unlock(&c->inocache_lock);
1369 
1370     if (!f->inocache && ino == 1) {
1371         /* Special case - no root inode on medium */
1372         f->inocache = jffs2_alloc_inode_cache();
1373         if (!f->inocache) {
1374             JFFS2_ERROR("cannot allocate inocache for root inode\n");
1375             return -ENOMEM;
1376         }
1377         dbg_readinode("creating inocache for root inode\n");
1378         memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
1379         f->inocache->ino = f->inocache->pino_nlink = 1;
1380         f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
1381         f->inocache->state = INO_STATE_READING;
1382         jffs2_add_ino_cache(c, f->inocache);
1383     }
1384     if (!f->inocache) {
1385         JFFS2_ERROR("requested to read a nonexistent ino %u\n", ino);
1386         return -ENOENT;
1387     }
1388 
1389     return jffs2_do_read_inode_internal(c, f, latest_node);
1390 }
1391 
1392 int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
1393 {
1394     struct jffs2_raw_inode n;
1395     struct jffs2_inode_info *f = kzalloc(sizeof(*f), GFP_KERNEL);
1396     int ret;
1397 
1398     if (!f)
1399         return -ENOMEM;
1400 
1401     mutex_init(&f->sem);
1402     mutex_lock(&f->sem);
1403     f->inocache = ic;
1404 
1405     ret = jffs2_do_read_inode_internal(c, f, &n);
1406     mutex_unlock(&f->sem);
1407     jffs2_do_clear_inode(c, f);
1408     jffs2_xattr_do_crccheck_inode(c, ic);
1409     kfree (f);
1410     return ret;
1411 }
1412 
1413 void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
1414 {
1415     struct jffs2_full_dirent *fd, *fds;
1416     int deleted;
1417 
1418     jffs2_xattr_delete_inode(c, f->inocache);
1419     mutex_lock(&f->sem);
1420     deleted = f->inocache && !f->inocache->pino_nlink;
1421 
1422     if (f->inocache && f->inocache->state != INO_STATE_CHECKING)
1423         jffs2_set_inocache_state(c, f->inocache, INO_STATE_CLEARING);
1424 
1425     if (f->metadata) {
1426         if (deleted)
1427             jffs2_mark_node_obsolete(c, f->metadata->raw);
1428         jffs2_free_full_dnode(f->metadata);
1429     }
1430 
1431     jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
1432 
1433     fds = f->dents;
1434     while(fds) {
1435         fd = fds;
1436         fds = fd->next;
1437         jffs2_free_full_dirent(fd);
1438     }
1439 
1440     if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
1441         jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1442         if (f->inocache->nodes == (void *)f->inocache)
1443             jffs2_del_ino_cache(c, f->inocache);
1444     }
1445 
1446     mutex_unlock(&f->sem);
1447 }