aboutsummaryrefslogtreecommitdiffstats
path: root/fs/jffs2/readinode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/jffs2/readinode.c')
-rw-r--r--fs/jffs2/readinode.c1153
1 files changed, 681 insertions, 472 deletions
diff --git a/fs/jffs2/readinode.c b/fs/jffs2/readinode.c
index 5b2a83599d73..5f0652df5d47 100644
--- a/fs/jffs2/readinode.c
+++ b/fs/jffs2/readinode.c
@@ -7,11 +7,12 @@
7 * 7 *
8 * For licensing information, see the file 'LICENCE' in this directory. 8 * For licensing information, see the file 'LICENCE' in this directory.
9 * 9 *
10 * $Id: readinode.c,v 1.125 2005/07/10 13:13:55 dedekind Exp $ 10 * $Id: readinode.c,v 1.143 2005/11/07 11:14:41 gleixner Exp $
11 * 11 *
12 */ 12 */
13 13
14#include <linux/kernel.h> 14#include <linux/kernel.h>
15#include <linux/sched.h>
15#include <linux/slab.h> 16#include <linux/slab.h>
16#include <linux/fs.h> 17#include <linux/fs.h>
17#include <linux/crc32.h> 18#include <linux/crc32.h>
@@ -20,502 +21,631 @@
20#include <linux/compiler.h> 21#include <linux/compiler.h>
21#include "nodelist.h" 22#include "nodelist.h"
22 23
23static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *list, struct jffs2_node_frag *newfrag); 24/*
24 25 * Put a new tmp_dnode_info into the temporaty RB-tree, keeping the list in
25#if CONFIG_JFFS2_FS_DEBUG >= 2 26 * order of increasing version.
26static void jffs2_print_fragtree(struct rb_root *list, int permitbug) 27 */
28static void jffs2_add_tn_to_tree(struct jffs2_tmp_dnode_info *tn, struct rb_root *list)
27{ 29{
28 struct jffs2_node_frag *this = frag_first(list); 30 struct rb_node **p = &list->rb_node;
29 uint32_t lastofs = 0; 31 struct rb_node * parent = NULL;
30 int buggy = 0; 32 struct jffs2_tmp_dnode_info *this;
31 33
32 while(this) { 34 while (*p) {
33 if (this->node) 35 parent = *p;
34 printk(KERN_DEBUG "frag %04x-%04x: 0x%08x(%d) on flash (*%p). left (%p), right (%p), parent (%p)\n", 36 this = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
35 this->ofs, this->ofs+this->size, ref_offset(this->node->raw), ref_flags(this->node->raw), 37
36 this, frag_left(this), frag_right(this), frag_parent(this)); 38 /* There may actually be a collision here, but it doesn't
37 else 39 actually matter. As long as the two nodes with the same
38 printk(KERN_DEBUG "frag %04x-%04x: hole (*%p). left (%p} right (%p), parent (%p)\n", this->ofs, 40 version are together, it's all fine. */
39 this->ofs+this->size, this, frag_left(this), frag_right(this), frag_parent(this)); 41 if (tn->version > this->version)
40 if (this->ofs != lastofs) 42 p = &(*p)->rb_left;
41 buggy = 1; 43 else
42 lastofs = this->ofs+this->size; 44 p = &(*p)->rb_right;
43 this = frag_next(this);
44 } 45 }
45 if (buggy && !permitbug) { 46
46 printk(KERN_CRIT "Frag tree got a hole in it\n"); 47 rb_link_node(&tn->rb, parent, p);
47 BUG(); 48 rb_insert_color(&tn->rb, list);
49}
50
51static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
52{
53 struct rb_node *this;
54 struct jffs2_tmp_dnode_info *tn;
55
56 this = list->rb_node;
57
58 /* Now at bottom of tree */
59 while (this) {
60 if (this->rb_left)
61 this = this->rb_left;
62 else if (this->rb_right)
63 this = this->rb_right;
64 else {
65 tn = rb_entry(this, struct jffs2_tmp_dnode_info, rb);
66 jffs2_free_full_dnode(tn->fn);
67 jffs2_free_tmp_dnode_info(tn);
68
69 this = this->rb_parent;
70 if (!this)
71 break;
72
73 if (this->rb_left == &tn->rb)
74 this->rb_left = NULL;
75 else if (this->rb_right == &tn->rb)
76 this->rb_right = NULL;
77 else BUG();
78 }
48 } 79 }
80 list->rb_node = NULL;
49} 81}
50 82
51void jffs2_print_frag_list(struct jffs2_inode_info *f) 83static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
52{ 84{
53 jffs2_print_fragtree(&f->fragtree, 0); 85 struct jffs2_full_dirent *next;
54 86
55 if (f->metadata) { 87 while (fd) {
56 printk(KERN_DEBUG "metadata at 0x%08x\n", ref_offset(f->metadata->raw)); 88 next = fd->next;
89 jffs2_free_full_dirent(fd);
90 fd = next;
57 } 91 }
58} 92}
59#endif
60 93
61#if CONFIG_JFFS2_FS_DEBUG >= 1 94/* Returns first valid node after 'ref'. May return 'ref' */
62static int jffs2_sanitycheck_fragtree(struct jffs2_inode_info *f) 95static struct jffs2_raw_node_ref *jffs2_first_valid_node(struct jffs2_raw_node_ref *ref)
63{ 96{
64 struct jffs2_node_frag *frag; 97 while (ref && ref->next_in_ino) {
65 int bitched = 0; 98 if (!ref_obsolete(ref))
66 99 return ref;
67 for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) { 100 dbg_noderef("node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref));
101 ref = ref->next_in_ino;
102 }
103 return NULL;
104}
68 105
69 struct jffs2_full_dnode *fn = frag->node; 106/*
70 if (!fn || !fn->raw) 107 * Helper function for jffs2_get_inode_nodes().
71 continue; 108 * It is called every time an directory entry node is found.
109 *
110 * Returns: 0 on succes;
111 * 1 if the node should be marked obsolete;
112 * negative error code on failure.
113 */
114static inline int read_direntry(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
115 struct jffs2_raw_dirent *rd, uint32_t read, struct jffs2_full_dirent **fdp,
116 uint32_t *latest_mctime, uint32_t *mctime_ver)
117{
118 struct jffs2_full_dirent *fd;
119
120 /* The direntry nodes are checked during the flash scanning */
121 BUG_ON(ref_flags(ref) == REF_UNCHECKED);
122 /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
123 BUG_ON(ref_obsolete(ref));
124
125 /* Sanity check */
126 if (unlikely(PAD((rd->nsize + sizeof(*rd))) != PAD(je32_to_cpu(rd->totlen)))) {
127 JFFS2_ERROR("illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n",
128 ref_offset(ref), rd->nsize, je32_to_cpu(rd->totlen));
129 return 1;
130 }
72 131
73 if (ref_flags(fn->raw) == REF_PRISTINE) { 132 fd = jffs2_alloc_full_dirent(rd->nsize + 1);
133 if (unlikely(!fd))
134 return -ENOMEM;
74 135
75 if (fn->frags > 1) { 136 fd->raw = ref;
76 printk(KERN_WARNING "REF_PRISTINE node at 0x%08x had %d frags. Tell dwmw2\n", ref_offset(fn->raw), fn->frags); 137 fd->version = je32_to_cpu(rd->version);
77 bitched = 1; 138 fd->ino = je32_to_cpu(rd->ino);
78 } 139 fd->type = rd->type;
79 /* A hole node which isn't multi-page should be garbage-collected
80 and merged anyway, so we just check for the frag size here,
81 rather than mucking around with actually reading the node
82 and checking the compression type, which is the real way
83 to tell a hole node. */
84 if (frag->ofs & (PAGE_CACHE_SIZE-1) && frag_prev(frag) && frag_prev(frag)->size < PAGE_CACHE_SIZE && frag_prev(frag)->node) {
85 printk(KERN_WARNING "REF_PRISTINE node at 0x%08x had a previous non-hole frag in the same page. Tell dwmw2\n",
86 ref_offset(fn->raw));
87 bitched = 1;
88 }
89 140
90 if ((frag->ofs+frag->size) & (PAGE_CACHE_SIZE-1) && frag_next(frag) && frag_next(frag)->size < PAGE_CACHE_SIZE && frag_next(frag)->node) { 141 /* Pick out the mctime of the latest dirent */
91 printk(KERN_WARNING "REF_PRISTINE node at 0x%08x (%08x-%08x) had a following non-hole frag in the same page. Tell dwmw2\n", 142 if(fd->version > *mctime_ver && je32_to_cpu(rd->mctime)) {
92 ref_offset(fn->raw), frag->ofs, frag->ofs+frag->size); 143 *mctime_ver = fd->version;
93 bitched = 1; 144 *latest_mctime = je32_to_cpu(rd->mctime);
94 }
95 }
96 } 145 }
97
98 if (bitched) {
99 struct jffs2_node_frag *thisfrag;
100
101 printk(KERN_WARNING "Inode is #%u\n", f->inocache->ino);
102 thisfrag = frag_first(&f->fragtree);
103 while (thisfrag) {
104 if (!thisfrag->node) {
105 printk("Frag @0x%x-0x%x; node-less hole\n",
106 thisfrag->ofs, thisfrag->size + thisfrag->ofs);
107 } else if (!thisfrag->node->raw) {
108 printk("Frag @0x%x-0x%x; raw-less hole\n",
109 thisfrag->ofs, thisfrag->size + thisfrag->ofs);
110 } else {
111 printk("Frag @0x%x-0x%x; raw at 0x%08x(%d) (0x%x-0x%x)\n",
112 thisfrag->ofs, thisfrag->size + thisfrag->ofs,
113 ref_offset(thisfrag->node->raw), ref_flags(thisfrag->node->raw),
114 thisfrag->node->ofs, thisfrag->node->ofs+thisfrag->node->size);
115 }
116 thisfrag = frag_next(thisfrag);
117 }
118 }
119 return bitched;
120}
121#endif /* D1 */
122 146
123static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this) 147 /*
124{ 148 * Copy as much of the name as possible from the raw
125 if (this->node) { 149 * dirent we've already read from the flash.
126 this->node->frags--; 150 */
127 if (!this->node->frags) { 151 if (read > sizeof(*rd))
128 /* The node has no valid frags left. It's totally obsoleted */ 152 memcpy(&fd->name[0], &rd->name[0],
129 D2(printk(KERN_DEBUG "Marking old node @0x%08x (0x%04x-0x%04x) obsolete\n", 153 min_t(uint32_t, rd->nsize, (read - sizeof(*rd)) ));
130 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size)); 154
131 jffs2_mark_node_obsolete(c, this->node->raw); 155 /* Do we need to copy any more of the name directly from the flash? */
132 jffs2_free_full_dnode(this->node); 156 if (rd->nsize + sizeof(*rd) > read) {
133 } else { 157 /* FIXME: point() */
134 D2(printk(KERN_DEBUG "Marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n", 158 int err;
135 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size, 159 int already = read - sizeof(*rd);
136 this->node->frags)); 160
137 mark_ref_normal(this->node->raw); 161 err = jffs2_flash_read(c, (ref_offset(ref)) + read,
162 rd->nsize - already, &read, &fd->name[already]);
163 if (unlikely(read != rd->nsize - already) && likely(!err))
164 return -EIO;
165
166 if (unlikely(err)) {
167 JFFS2_ERROR("read remainder of name: error %d\n", err);
168 jffs2_free_full_dirent(fd);
169 return -EIO;
138 } 170 }
139
140 } 171 }
141 jffs2_free_node_frag(this); 172
173 fd->nhash = full_name_hash(fd->name, rd->nsize);
174 fd->next = NULL;
175 fd->name[rd->nsize] = '\0';
176
177 /*
178 * Wheee. We now have a complete jffs2_full_dirent structure, with
179 * the name in it and everything. Link it into the list
180 */
181 jffs2_add_fd_to_list(c, fd, fdp);
182
183 return 0;
142} 184}
143 185
144/* Given an inode, probably with existing list of fragments, add the new node 186/*
145 * to the fragment list. 187 * Helper function for jffs2_get_inode_nodes().
188 * It is called every time an inode node is found.
189 *
190 * Returns: 0 on succes;
191 * 1 if the node should be marked obsolete;
192 * negative error code on failure.
146 */ 193 */
147int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn) 194static inline int read_dnode(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
195 struct jffs2_raw_inode *rd, struct rb_root *tnp, int rdlen,
196 uint32_t *latest_mctime, uint32_t *mctime_ver)
148{ 197{
149 int ret; 198 struct jffs2_tmp_dnode_info *tn;
150 struct jffs2_node_frag *newfrag; 199 uint32_t len, csize;
151 200 int ret = 1;
152 D1(printk(KERN_DEBUG "jffs2_add_full_dnode_to_inode(ino #%u, f %p, fn %p)\n", f->inocache->ino, f, fn));
153 201
154 if (unlikely(!fn->size)) 202 /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
155 return 0; 203 BUG_ON(ref_obsolete(ref));
156 204
157 newfrag = jffs2_alloc_node_frag(); 205 tn = jffs2_alloc_tmp_dnode_info();
158 if (unlikely(!newfrag)) 206 if (!tn) {
207 JFFS2_ERROR("failed to allocate tn (%d bytes).\n", sizeof(*tn));
159 return -ENOMEM; 208 return -ENOMEM;
209 }
160 210
161 D2(printk(KERN_DEBUG "adding node %04x-%04x @0x%08x on flash, newfrag *%p\n", 211 tn->partial_crc = 0;
162 fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag)); 212 csize = je32_to_cpu(rd->csize);
163
164 newfrag->ofs = fn->ofs;
165 newfrag->size = fn->size;
166 newfrag->node = fn;
167 newfrag->node->frags = 1;
168 213
169 ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag); 214 /* If we've never checked the CRCs on this node, check them now */
170 if (ret) 215 if (ref_flags(ref) == REF_UNCHECKED) {
171 return ret; 216 uint32_t crc;
172 217
173 /* If we now share a page with other nodes, mark either previous 218 crc = crc32(0, rd, sizeof(*rd) - 8);
174 or next node REF_NORMAL, as appropriate. */ 219 if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
175 if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) { 220 JFFS2_NOTICE("header CRC failed on node at %#08x: read %#08x, calculated %#08x\n",
176 struct jffs2_node_frag *prev = frag_prev(newfrag); 221 ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
222 goto free_out;
223 }
177 224
178 mark_ref_normal(fn->raw); 225 /* Sanity checks */
179 /* If we don't start at zero there's _always_ a previous */ 226 if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
180 if (prev->node) 227 unlikely(PAD(je32_to_cpu(rd->csize) + sizeof(*rd)) != PAD(je32_to_cpu(rd->totlen)))) {
181 mark_ref_normal(prev->node->raw); 228 JFFS2_WARNING("inode node header CRC is corrupted at %#08x\n", ref_offset(ref));
182 } 229 jffs2_dbg_dump_node(c, ref_offset(ref));
230 goto free_out;
231 }
183 232
184 if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) { 233 if (jffs2_is_writebuffered(c) && csize != 0) {
185 struct jffs2_node_frag *next = frag_next(newfrag); 234 /* At this point we are supposed to check the data CRC
186 235 * of our unchecked node. But thus far, we do not
187 if (next) { 236 * know whether the node is valid or obsolete. To
188 mark_ref_normal(fn->raw); 237 * figure this out, we need to walk all the nodes of
189 if (next->node) 238 * the inode and build the inode fragtree. We don't
190 mark_ref_normal(next->node->raw); 239 * want to spend time checking data of nodes which may
240 * later be found to be obsolete. So we put off the full
241 * data CRC checking until we have read all the inode
242 * nodes and have started building the fragtree.
243 *
244 * The fragtree is being built starting with nodes
245 * having the highest version number, so we'll be able
246 * to detect whether a node is valid (i.e., it is not
247 * overlapped by a node with higher version) or not.
248 * And we'll be able to check only those nodes, which
249 * are not obsolete.
250 *
251 * Of course, this optimization only makes sense in case
252 * of NAND flashes (or other flashes whith
253 * !jffs2_can_mark_obsolete()), since on NOR flashes
254 * nodes are marked obsolete physically.
255 *
256 * Since NAND flashes (or other flashes with
257 * jffs2_is_writebuffered(c)) are anyway read by
258 * fractions of c->wbuf_pagesize, and we have just read
259 * the node header, it is likely that the starting part
260 * of the node data is also read when we read the
261 * header. So we don't mind to check the CRC of the
262 * starting part of the data of the node now, and check
263 * the second part later (in jffs2_check_node_data()).
264 * Of course, we will not need to re-read and re-check
265 * the NAND page which we have just read. This is why we
266 * read the whole NAND page at jffs2_get_inode_nodes(),
267 * while we needed only the node header.
268 */
269 unsigned char *buf;
270
271 /* 'buf' will point to the start of data */
272 buf = (unsigned char *)rd + sizeof(*rd);
273 /* len will be the read data length */
274 len = min_t(uint32_t, rdlen - sizeof(*rd), csize);
275 tn->partial_crc = crc32(0, buf, len);
276
277 dbg_readinode("Calculates CRC (%#08x) for %d bytes, csize %d\n", tn->partial_crc, len, csize);
278
279 /* If we actually calculated the whole data CRC
280 * and it is wrong, drop the node. */
281 if (len >= csize && unlikely(tn->partial_crc != je32_to_cpu(rd->data_crc))) {
282 JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
283 ref_offset(ref), tn->partial_crc, je32_to_cpu(rd->data_crc));
284 goto free_out;
285 }
286
287 } else if (csize == 0) {
288 /*
289 * We checked the header CRC. If the node has no data, adjust
290 * the space accounting now. For other nodes this will be done
291 * later either when the node is marked obsolete or when its
292 * data is checked.
293 */
294 struct jffs2_eraseblock *jeb;
295
296 dbg_readinode("the node has no data.\n");
297 jeb = &c->blocks[ref->flash_offset / c->sector_size];
298 len = ref_totlen(c, jeb, ref);
299
300 spin_lock(&c->erase_completion_lock);
301 jeb->used_size += len;
302 jeb->unchecked_size -= len;
303 c->used_size += len;
304 c->unchecked_size -= len;
305 ref->flash_offset = ref_offset(ref) | REF_NORMAL;
306 spin_unlock(&c->erase_completion_lock);
191 } 307 }
192 } 308 }
193 D2(if (jffs2_sanitycheck_fragtree(f)) { 309
194 printk(KERN_WARNING "Just added node %04x-%04x @0x%08x on flash, newfrag *%p\n", 310 tn->fn = jffs2_alloc_full_dnode();
195 fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag); 311 if (!tn->fn) {
196 return 0; 312 JFFS2_ERROR("alloc fn failed\n");
197 }) 313 ret = -ENOMEM;
198 D2(jffs2_print_frag_list(f)); 314 goto free_out;
315 }
316
317 tn->version = je32_to_cpu(rd->version);
318 tn->fn->ofs = je32_to_cpu(rd->offset);
319 tn->data_crc = je32_to_cpu(rd->data_crc);
320 tn->csize = csize;
321 tn->fn->raw = ref;
322
323 /* There was a bug where we wrote hole nodes out with
324 csize/dsize swapped. Deal with it */
325 if (rd->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(rd->dsize) && csize)
326 tn->fn->size = csize;
327 else // normal case...
328 tn->fn->size = je32_to_cpu(rd->dsize);
329
330 dbg_readinode("dnode @%08x: ver %u, offset %#04x, dsize %#04x, csize %#04x\n",
331 ref_offset(ref), je32_to_cpu(rd->version), je32_to_cpu(rd->offset), je32_to_cpu(rd->dsize), csize);
332
333 jffs2_add_tn_to_tree(tn, tnp);
334
199 return 0; 335 return 0;
336
337free_out:
338 jffs2_free_tmp_dnode_info(tn);
339 return ret;
200} 340}
201 341
202/* Doesn't set inode->i_size */ 342/*
203static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *list, struct jffs2_node_frag *newfrag) 343 * Helper function for jffs2_get_inode_nodes().
344 * It is called every time an unknown node is found.
345 *
346 * Returns: 0 on succes;
347 * 1 if the node should be marked obsolete;
348 * negative error code on failure.
349 */
350static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref, struct jffs2_unknown_node *un)
204{ 351{
205 struct jffs2_node_frag *this; 352 /* We don't mark unknown nodes as REF_UNCHECKED */
206 uint32_t lastend; 353 BUG_ON(ref_flags(ref) == REF_UNCHECKED);
207 354
208 /* Skip all the nodes which are completed before this one starts */ 355 un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype));
209 this = jffs2_lookup_node_frag(list, newfrag->node->ofs);
210 356
211 if (this) { 357 if (crc32(0, un, sizeof(struct jffs2_unknown_node) - 4) != je32_to_cpu(un->hdr_crc)) {
212 D2(printk(KERN_DEBUG "j_a_f_d_t_f: Lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n", 358 /* Hmmm. This should have been caught at scan time. */
213 this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this)); 359 JFFS2_NOTICE("node header CRC failed at %#08x. But it must have been OK earlier.\n", ref_offset(ref));
214 lastend = this->ofs + this->size; 360 jffs2_dbg_dump_node(c, ref_offset(ref));
361 return 1;
215 } else { 362 } else {
216 D2(printk(KERN_DEBUG "j_a_f_d_t_f: Lookup gave no frag\n")); 363 switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
217 lastend = 0;
218 }
219
220 /* See if we ran off the end of the list */
221 if (lastend <= newfrag->ofs) {
222 /* We did */
223
224 /* Check if 'this' node was on the same page as the new node.
225 If so, both 'this' and the new node get marked REF_NORMAL so
226 the GC can take a look.
227 */
228 if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
229 if (this->node)
230 mark_ref_normal(this->node->raw);
231 mark_ref_normal(newfrag->node->raw);
232 }
233 364
234 if (lastend < newfrag->node->ofs) { 365 case JFFS2_FEATURE_INCOMPAT:
235 /* ... and we need to put a hole in before the new node */ 366 JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
236 struct jffs2_node_frag *holefrag = jffs2_alloc_node_frag(); 367 je16_to_cpu(un->nodetype), ref_offset(ref));
237 if (!holefrag) { 368 /* EEP */
238 jffs2_free_node_frag(newfrag); 369 BUG();
239 return -ENOMEM; 370 break;
240 } 371
241 holefrag->ofs = lastend; 372 case JFFS2_FEATURE_ROCOMPAT:
242 holefrag->size = newfrag->node->ofs - lastend; 373 JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
243 holefrag->node = NULL; 374 je16_to_cpu(un->nodetype), ref_offset(ref));
244 if (this) { 375 BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
245 /* By definition, the 'this' node has no right-hand child, 376 break;
246 because there are no frags with offset greater than it. 377
247 So that's where we want to put the hole */ 378 case JFFS2_FEATURE_RWCOMPAT_COPY:
248 D2(printk(KERN_DEBUG "Adding hole frag (%p) on right of node at (%p)\n", holefrag, this)); 379 JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
249 rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right); 380 je16_to_cpu(un->nodetype), ref_offset(ref));
250 } else { 381 break;
251 D2(printk(KERN_DEBUG "Adding hole frag (%p) at root of tree\n", holefrag)); 382
252 rb_link_node(&holefrag->rb, NULL, &list->rb_node); 383 case JFFS2_FEATURE_RWCOMPAT_DELETE:
253 } 384 JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
254 rb_insert_color(&holefrag->rb, list); 385 je16_to_cpu(un->nodetype), ref_offset(ref));
255 this = holefrag; 386 return 1;
256 }
257 if (this) {
258 /* By definition, the 'this' node has no right-hand child,
259 because there are no frags with offset greater than it.
260 So that's where we want to put the hole */
261 D2(printk(KERN_DEBUG "Adding new frag (%p) on right of node at (%p)\n", newfrag, this));
262 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
263 } else {
264 D2(printk(KERN_DEBUG "Adding new frag (%p) at root of tree\n", newfrag));
265 rb_link_node(&newfrag->rb, NULL, &list->rb_node);
266 } 387 }
267 rb_insert_color(&newfrag->rb, list);
268 return 0;
269 } 388 }
270 389
271 D2(printk(KERN_DEBUG "j_a_f_d_t_f: dealing with frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n", 390 return 0;
272 this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this)); 391}
273 392
274 /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes, 393/*
275 * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs 394 * Helper function for jffs2_get_inode_nodes().
276 */ 395 * The function detects whether more data should be read and reads it if yes.
277 if (newfrag->ofs > this->ofs) { 396 *
278 /* This node isn't completely obsoleted. The start of it remains valid */ 397 * Returns: 0 on succes;
279 398 * negative error code on failure.
280 /* Mark the new node and the partially covered node REF_NORMAL -- let 399 */
281 the GC take a look at them */ 400static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
282 mark_ref_normal(newfrag->node->raw); 401 int right_size, int *rdlen, unsigned char *buf, unsigned char *bufstart)
283 if (this->node) 402{
284 mark_ref_normal(this->node->raw); 403 int right_len, err, len;
285 404 size_t retlen;
286 if (this->ofs + this->size > newfrag->ofs + newfrag->size) { 405 uint32_t offs;
287 /* The new node splits 'this' frag into two */
288 struct jffs2_node_frag *newfrag2 = jffs2_alloc_node_frag();
289 if (!newfrag2) {
290 jffs2_free_node_frag(newfrag);
291 return -ENOMEM;
292 }
293 D2(printk(KERN_DEBUG "split old frag 0x%04x-0x%04x -->", this->ofs, this->ofs+this->size);
294 if (this->node)
295 printk("phys 0x%08x\n", ref_offset(this->node->raw));
296 else
297 printk("hole\n");
298 )
299
300 /* New second frag pointing to this's node */
301 newfrag2->ofs = newfrag->ofs + newfrag->size;
302 newfrag2->size = (this->ofs+this->size) - newfrag2->ofs;
303 newfrag2->node = this->node;
304 if (this->node)
305 this->node->frags++;
306
307 /* Adjust size of original 'this' */
308 this->size = newfrag->ofs - this->ofs;
309
310 /* Now, we know there's no node with offset
311 greater than this->ofs but smaller than
312 newfrag2->ofs or newfrag->ofs, for obvious
313 reasons. So we can do a tree insert from
314 'this' to insert newfrag, and a tree insert
315 from newfrag to insert newfrag2. */
316 jffs2_fragtree_insert(newfrag, this);
317 rb_insert_color(&newfrag->rb, list);
318
319 jffs2_fragtree_insert(newfrag2, newfrag);
320 rb_insert_color(&newfrag2->rb, list);
321
322 return 0;
323 }
324 /* New node just reduces 'this' frag in size, doesn't split it */
325 this->size = newfrag->ofs - this->ofs;
326 406
327 /* Again, we know it lives down here in the tree */ 407 if (jffs2_is_writebuffered(c)) {
328 jffs2_fragtree_insert(newfrag, this); 408 right_len = c->wbuf_pagesize - (bufstart - buf);
329 rb_insert_color(&newfrag->rb, list); 409 if (right_size + (int)(bufstart - buf) > c->wbuf_pagesize)
330 } else { 410 right_len += c->wbuf_pagesize;
331 /* New frag starts at the same point as 'this' used to. Replace 411 } else
332 it in the tree without doing a delete and insertion */ 412 right_len = right_size;
333 D2(printk(KERN_DEBUG "Inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
334 newfrag, newfrag->ofs, newfrag->ofs+newfrag->size,
335 this, this->ofs, this->ofs+this->size));
336
337 rb_replace_node(&this->rb, &newfrag->rb, list);
338
339 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
340 D2(printk(KERN_DEBUG "Obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size));
341 jffs2_obsolete_node_frag(c, this);
342 } else {
343 this->ofs += newfrag->size;
344 this->size -= newfrag->size;
345 413
346 jffs2_fragtree_insert(this, newfrag); 414 if (*rdlen == right_len)
347 rb_insert_color(&this->rb, list); 415 return 0;
348 return 0; 416
349 } 417 /* We need to read more data */
418 offs = ref_offset(ref) + *rdlen;
419 if (jffs2_is_writebuffered(c)) {
420 bufstart = buf + c->wbuf_pagesize;
421 len = c->wbuf_pagesize;
422 } else {
423 bufstart = buf + *rdlen;
424 len = right_size - *rdlen;
350 } 425 }
351 /* OK, now we have newfrag added in the correct place in the tree, but 426
352 frag_next(newfrag) may be a fragment which is overlapped by it 427 dbg_readinode("read more %d bytes\n", len);
353 */ 428
354 while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) { 429 err = jffs2_flash_read(c, offs, len, &retlen, bufstart);
355 /* 'this' frag is obsoleted completely. */ 430 if (err) {
356 D2(printk(KERN_DEBUG "Obsoleting node frag %p (%x-%x) and removing from tree\n", this, this->ofs, this->ofs+this->size)); 431 JFFS2_ERROR("can not read %d bytes from 0x%08x, "
357 rb_erase(&this->rb, list); 432 "error code: %d.\n", len, offs, err);
358 jffs2_obsolete_node_frag(c, this); 433 return err;
359 } 434 }
360 /* Now we're pointing at the first frag which isn't totally obsoleted by
361 the new frag */
362 435
363 if (!this || newfrag->ofs + newfrag->size == this->ofs) { 436 if (retlen < len) {
364 return 0; 437 JFFS2_ERROR("short read at %#08x: %d instead of %d.\n",
438 offs, retlen, len);
439 return -EIO;
365 } 440 }
366 /* Still some overlap but we don't need to move it in the tree */
367 this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
368 this->ofs = newfrag->ofs + newfrag->size;
369 441
370 /* And mark them REF_NORMAL so the GC takes a look at them */ 442 *rdlen = right_len;
371 if (this->node)
372 mark_ref_normal(this->node->raw);
373 mark_ref_normal(newfrag->node->raw);
374 443
375 return 0; 444 return 0;
376} 445}
377 446
378void jffs2_truncate_fraglist (struct jffs2_sb_info *c, struct rb_root *list, uint32_t size) 447/* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
448 with this ino, returning the former in order of version */
449static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
450 struct rb_root *tnp, struct jffs2_full_dirent **fdp,
451 uint32_t *highest_version, uint32_t *latest_mctime,
452 uint32_t *mctime_ver)
379{ 453{
380 struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size); 454 struct jffs2_raw_node_ref *ref, *valid_ref;
455 struct rb_root ret_tn = RB_ROOT;
456 struct jffs2_full_dirent *ret_fd = NULL;
457 unsigned char *buf = NULL;
458 union jffs2_node_union *node;
459 size_t retlen;
460 int len, err;
461
462 *mctime_ver = 0;
463
464 dbg_readinode("ino #%u\n", f->inocache->ino);
465
466 if (jffs2_is_writebuffered(c)) {
467 /*
468 * If we have the write buffer, we assume the minimal I/O unit
469 * is c->wbuf_pagesize. We implement some optimizations which in
470 * this case and we need a temporary buffer of size =
471 * 2*c->wbuf_pagesize bytes (see comments in read_dnode()).
472 * Basically, we want to read not only the node header, but the
473 * whole wbuf (NAND page in case of NAND) or 2, if the node
474 * header overlaps the border between the 2 wbufs.
475 */
476 len = 2*c->wbuf_pagesize;
477 } else {
478 /*
479 * When there is no write buffer, the size of the temporary
480 * buffer is the size of the larges node header.
481 */
482 len = sizeof(union jffs2_node_union);
483 }
381 484
382 D1(printk(KERN_DEBUG "Truncating fraglist to 0x%08x bytes\n", size)); 485 /* FIXME: in case of NOR and available ->point() this
486 * needs to be fixed. */
487 buf = kmalloc(len, GFP_KERNEL);
488 if (!buf)
489 return -ENOMEM;
383 490
384 /* We know frag->ofs <= size. That's what lookup does for us */ 491 spin_lock(&c->erase_completion_lock);
385 if (frag && frag->ofs != size) { 492 valid_ref = jffs2_first_valid_node(f->inocache->nodes);
386 if (frag->ofs+frag->size >= size) { 493 if (!valid_ref && f->inocache->ino != 1)
387 D1(printk(KERN_DEBUG "Truncating frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size)); 494 JFFS2_WARNING("Eep. No valid nodes for ino #%u.\n", f->inocache->ino);
388 frag->size = size - frag->ofs; 495 while (valid_ref) {
496 unsigned char *bufstart;
497
498 /* We can hold a pointer to a non-obsolete node without the spinlock,
499 but _obsolete_ nodes may disappear at any time, if the block
500 they're in gets erased. So if we mark 'ref' obsolete while we're
501 not holding the lock, it can go away immediately. For that reason,
502 we find the next valid node first, before processing 'ref'.
503 */
504 ref = valid_ref;
505 valid_ref = jffs2_first_valid_node(ref->next_in_ino);
506 spin_unlock(&c->erase_completion_lock);
507
508 cond_resched();
509
510 /*
511 * At this point we don't know the type of the node we're going
512 * to read, so we do not know the size of its header. In order
513 * to minimize the amount of flash IO we assume the node has
514 * size = JFFS2_MIN_NODE_HEADER.
515 */
516 if (jffs2_is_writebuffered(c)) {
517 /*
518 * We treat 'buf' as 2 adjacent wbufs. We want to
519 * adjust bufstart such as it points to the
520 * beginning of the node within this wbuf.
521 */
522 bufstart = buf + (ref_offset(ref) % c->wbuf_pagesize);
523 /* We will read either one wbuf or 2 wbufs. */
524 len = c->wbuf_pagesize - (bufstart - buf);
525 if (JFFS2_MIN_NODE_HEADER + (int)(bufstart - buf) > c->wbuf_pagesize) {
526 /* The header spans the border of the first wbuf */
527 len += c->wbuf_pagesize;
528 }
529 } else {
530 bufstart = buf;
531 len = JFFS2_MIN_NODE_HEADER;
389 } 532 }
390 frag = frag_next(frag);
391 }
392 while (frag && frag->ofs >= size) {
393 struct jffs2_node_frag *next = frag_next(frag);
394 533
395 D1(printk(KERN_DEBUG "Removing frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size)); 534 dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
396 frag_erase(frag, list);
397 jffs2_obsolete_node_frag(c, frag);
398 frag = next;
399 }
400}
401 535
402/* Scan the list of all nodes present for this ino, build map of versions, etc. */ 536 /* FIXME: point() */
537 err = jffs2_flash_read(c, ref_offset(ref), len,
538 &retlen, bufstart);
539 if (err) {
540 JFFS2_ERROR("can not read %d bytes from 0x%08x, " "error code: %d.\n", len, ref_offset(ref), err);
541 goto free_out;
542 }
403 543
404static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c, 544 if (retlen < len) {
405 struct jffs2_inode_info *f, 545 JFFS2_ERROR("short read at %#08x: %d instead of %d.\n", ref_offset(ref), retlen, len);
406 struct jffs2_raw_inode *latest_node); 546 err = -EIO;
547 goto free_out;
548 }
407 549
408int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, 550 node = (union jffs2_node_union *)bufstart;
409 uint32_t ino, struct jffs2_raw_inode *latest_node)
410{
411 D2(printk(KERN_DEBUG "jffs2_do_read_inode(): getting inocache\n"));
412 551
413 retry_inocache: 552 switch (je16_to_cpu(node->u.nodetype)) {
414 spin_lock(&c->inocache_lock);
415 f->inocache = jffs2_get_ino_cache(c, ino);
416 553
417 D2(printk(KERN_DEBUG "jffs2_do_read_inode(): Got inocache at %p\n", f->inocache)); 554 case JFFS2_NODETYPE_DIRENT:
555
556 if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent)) {
557 err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf, bufstart);
558 if (unlikely(err))
559 goto free_out;
560 }
561
562 err = read_direntry(c, ref, &node->d, retlen, &ret_fd, latest_mctime, mctime_ver);
563 if (err == 1) {
564 jffs2_mark_node_obsolete(c, ref);
565 break;
566 } else if (unlikely(err))
567 goto free_out;
568
569 if (je32_to_cpu(node->d.version) > *highest_version)
570 *highest_version = je32_to_cpu(node->d.version);
418 571
419 if (f->inocache) {
420 /* Check its state. We may need to wait before we can use it */
421 switch(f->inocache->state) {
422 case INO_STATE_UNCHECKED:
423 case INO_STATE_CHECKEDABSENT:
424 f->inocache->state = INO_STATE_READING;
425 break; 572 break;
426
427 case INO_STATE_CHECKING:
428 case INO_STATE_GC:
429 /* If it's in either of these states, we need
430 to wait for whoever's got it to finish and
431 put it back. */
432 D1(printk(KERN_DEBUG "jffs2_get_ino_cache_read waiting for ino #%u in state %d\n",
433 ino, f->inocache->state));
434 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
435 goto retry_inocache;
436 573
437 case INO_STATE_READING: 574 case JFFS2_NODETYPE_INODE:
438 case INO_STATE_PRESENT: 575
439 /* Eep. This should never happen. It can 576 if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode)) {
440 happen if Linux calls read_inode() again 577 err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf, bufstart);
441 before clear_inode() has finished though. */ 578 if (unlikely(err))
442 printk(KERN_WARNING "Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state); 579 goto free_out;
443 /* Fail. That's probably better than allowing it to succeed */ 580 }
444 f->inocache = NULL; 581
582 err = read_dnode(c, ref, &node->i, &ret_tn, len, latest_mctime, mctime_ver);
583 if (err == 1) {
584 jffs2_mark_node_obsolete(c, ref);
585 break;
586 } else if (unlikely(err))
587 goto free_out;
588
589 if (je32_to_cpu(node->i.version) > *highest_version)
590 *highest_version = je32_to_cpu(node->i.version);
591
445 break; 592 break;
446 593
447 default: 594 default:
448 BUG(); 595 if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_unknown_node)) {
449 } 596 err = read_more(c, ref, sizeof(struct jffs2_unknown_node), &len, buf, bufstart);
450 } 597 if (unlikely(err))
451 spin_unlock(&c->inocache_lock); 598 goto free_out;
599 }
600
601 err = read_unknown(c, ref, &node->u);
602 if (err == 1) {
603 jffs2_mark_node_obsolete(c, ref);
604 break;
605 } else if (unlikely(err))
606 goto free_out;
452 607
453 if (!f->inocache && ino == 1) {
454 /* Special case - no root inode on medium */
455 f->inocache = jffs2_alloc_inode_cache();
456 if (!f->inocache) {
457 printk(KERN_CRIT "jffs2_do_read_inode(): Cannot allocate inocache for root inode\n");
458 return -ENOMEM;
459 } 608 }
460 D1(printk(KERN_DEBUG "jffs2_do_read_inode(): Creating inocache for root inode\n")); 609 spin_lock(&c->erase_completion_lock);
461 memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
462 f->inocache->ino = f->inocache->nlink = 1;
463 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
464 f->inocache->state = INO_STATE_READING;
465 jffs2_add_ino_cache(c, f->inocache);
466 }
467 if (!f->inocache) {
468 printk(KERN_WARNING "jffs2_do_read_inode() on nonexistent ino %u\n", ino);
469 return -ENOENT;
470 } 610 }
471 611
472 return jffs2_do_read_inode_internal(c, f, latest_node); 612 spin_unlock(&c->erase_completion_lock);
473} 613 *tnp = ret_tn;
474 614 *fdp = ret_fd;
475int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic) 615 kfree(buf);
476{
477 struct jffs2_raw_inode n;
478 struct jffs2_inode_info *f = kmalloc(sizeof(*f), GFP_KERNEL);
479 int ret;
480
481 if (!f)
482 return -ENOMEM;
483 616
484 memset(f, 0, sizeof(*f)); 617 dbg_readinode("nodes of inode #%u were read, the highest version is %u, latest_mctime %u, mctime_ver %u.\n",
485 init_MUTEX_LOCKED(&f->sem); 618 f->inocache->ino, *highest_version, *latest_mctime, *mctime_ver);
486 f->inocache = ic; 619 return 0;
487 620
488 ret = jffs2_do_read_inode_internal(c, f, &n); 621 free_out:
489 if (!ret) { 622 jffs2_free_tmp_dnode_info_list(&ret_tn);
490 up(&f->sem); 623 jffs2_free_full_dirent_list(ret_fd);
491 jffs2_do_clear_inode(c, f); 624 kfree(buf);
492 } 625 return err;
493 kfree (f);
494 return ret;
495} 626}
496 627
497static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c, 628static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
498 struct jffs2_inode_info *f, 629 struct jffs2_inode_info *f,
499 struct jffs2_raw_inode *latest_node) 630 struct jffs2_raw_inode *latest_node)
500{ 631{
501 struct jffs2_tmp_dnode_info *tn = NULL; 632 struct jffs2_tmp_dnode_info *tn;
502 struct rb_root tn_list; 633 struct rb_root tn_list;
503 struct rb_node *rb, *repl_rb; 634 struct rb_node *rb, *repl_rb;
504 struct jffs2_full_dirent *fd_list; 635 struct jffs2_full_dirent *fd_list;
505 struct jffs2_full_dnode *fn = NULL; 636 struct jffs2_full_dnode *fn, *first_fn = NULL;
506 uint32_t crc; 637 uint32_t crc;
507 uint32_t latest_mctime, mctime_ver; 638 uint32_t latest_mctime, mctime_ver;
508 uint32_t mdata_ver = 0;
509 size_t retlen; 639 size_t retlen;
510 int ret; 640 int ret;
511 641
512 D1(printk(KERN_DEBUG "jffs2_do_read_inode_internal(): ino #%u nlink is %d\n", f->inocache->ino, f->inocache->nlink)); 642 dbg_readinode("ino #%u nlink is %d\n", f->inocache->ino, f->inocache->nlink);
513 643
514 /* Grab all nodes relevant to this ino */ 644 /* Grab all nodes relevant to this ino */
515 ret = jffs2_get_inode_nodes(c, f, &tn_list, &fd_list, &f->highest_version, &latest_mctime, &mctime_ver); 645 ret = jffs2_get_inode_nodes(c, f, &tn_list, &fd_list, &f->highest_version, &latest_mctime, &mctime_ver);
516 646
517 if (ret) { 647 if (ret) {
518 printk(KERN_CRIT "jffs2_get_inode_nodes() for ino %u returned %d\n", f->inocache->ino, ret); 648 JFFS2_ERROR("cannot read nodes for ino %u, returned error is %d\n", f->inocache->ino, ret);
519 if (f->inocache->state == INO_STATE_READING) 649 if (f->inocache->state == INO_STATE_READING)
520 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT); 650 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
521 return ret; 651 return ret;
@@ -525,42 +655,33 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
525 rb = rb_first(&tn_list); 655 rb = rb_first(&tn_list);
526 656
527 while (rb) { 657 while (rb) {
658 cond_resched();
528 tn = rb_entry(rb, struct jffs2_tmp_dnode_info, rb); 659 tn = rb_entry(rb, struct jffs2_tmp_dnode_info, rb);
529 fn = tn->fn; 660 fn = tn->fn;
530 661 ret = 1;
531 if (f->metadata) { 662 dbg_readinode("consider node ver %u, phys offset "
532 if (likely(tn->version >= mdata_ver)) { 663 "%#08x(%d), range %u-%u.\n", tn->version,
533 D1(printk(KERN_DEBUG "Obsoleting old metadata at 0x%08x\n", ref_offset(f->metadata->raw))); 664 ref_offset(fn->raw), ref_flags(fn->raw),
534 jffs2_mark_node_obsolete(c, f->metadata->raw); 665 fn->ofs, fn->ofs + fn->size);
535 jffs2_free_full_dnode(f->metadata);
536 f->metadata = NULL;
537
538 mdata_ver = 0;
539 } else {
540 /* This should never happen. */
541 printk(KERN_WARNING "Er. New metadata at 0x%08x with ver %d is actually older than previous ver %d at 0x%08x\n",
542 ref_offset(fn->raw), tn->version, mdata_ver, ref_offset(f->metadata->raw));
543 jffs2_mark_node_obsolete(c, fn->raw);
544 jffs2_free_full_dnode(fn);
545 /* Fill in latest_node from the metadata, not this one we're about to free... */
546 fn = f->metadata;
547 goto next_tn;
548 }
549 }
550 666
551 if (fn->size) { 667 if (fn->size) {
552 jffs2_add_full_dnode_to_inode(c, f, fn); 668 ret = jffs2_add_older_frag_to_fragtree(c, f, tn);
553 } else { 669 /* TODO: the error code isn't checked, check it */
554 /* Zero-sized node at end of version list. Just a metadata update */ 670 jffs2_dbg_fragtree_paranoia_check_nolock(f);
555 D1(printk(KERN_DEBUG "metadata @%08x: ver %d\n", ref_offset(fn->raw), tn->version)); 671 BUG_ON(ret < 0);
672 if (!first_fn && ret == 0)
673 first_fn = fn;
674 } else if (!first_fn) {
675 first_fn = fn;
556 f->metadata = fn; 676 f->metadata = fn;
557 mdata_ver = tn->version; 677 ret = 0; /* Prevent freeing the metadata update node */
558 } 678 } else
559 next_tn: 679 jffs2_mark_node_obsolete(c, fn->raw);
680
560 BUG_ON(rb->rb_left); 681 BUG_ON(rb->rb_left);
561 if (rb->rb_parent && rb->rb_parent->rb_left == rb) { 682 if (rb->rb_parent && rb->rb_parent->rb_left == rb) {
562 /* We were then left-hand child of our parent. We need 683 /* We were then left-hand child of our parent. We need
563 to move our own right-hand child into our place. */ 684 * to move our own right-hand child into our place. */
564 repl_rb = rb->rb_right; 685 repl_rb = rb->rb_right;
565 if (repl_rb) 686 if (repl_rb)
566 repl_rb->rb_parent = rb->rb_parent; 687 repl_rb->rb_parent = rb->rb_parent;
@@ -570,7 +691,7 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
570 rb = rb_next(rb); 691 rb = rb_next(rb);
571 692
572 /* Remove the spent tn from the tree; don't bother rebalancing 693 /* Remove the spent tn from the tree; don't bother rebalancing
573 but put our right-hand child in our own place. */ 694 * but put our right-hand child in our own place. */
574 if (tn->rb.rb_parent) { 695 if (tn->rb.rb_parent) {
575 if (tn->rb.rb_parent->rb_left == &tn->rb) 696 if (tn->rb.rb_parent->rb_left == &tn->rb)
576 tn->rb.rb_parent->rb_left = repl_rb; 697 tn->rb.rb_parent->rb_left = repl_rb;
@@ -581,19 +702,27 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
581 tn->rb.rb_right->rb_parent = NULL; 702 tn->rb.rb_right->rb_parent = NULL;
582 703
583 jffs2_free_tmp_dnode_info(tn); 704 jffs2_free_tmp_dnode_info(tn);
705 if (ret) {
706 dbg_readinode("delete dnode %u-%u.\n",
707 fn->ofs, fn->ofs + fn->size);
708 jffs2_free_full_dnode(fn);
709 }
584 } 710 }
585 D1(jffs2_sanitycheck_fragtree(f)); 711 jffs2_dbg_fragtree_paranoia_check_nolock(f);
586 712
587 if (!fn) { 713 BUG_ON(first_fn && ref_obsolete(first_fn->raw));
714
715 fn = first_fn;
716 if (unlikely(!first_fn)) {
588 /* No data nodes for this inode. */ 717 /* No data nodes for this inode. */
589 if (f->inocache->ino != 1) { 718 if (f->inocache->ino != 1) {
590 printk(KERN_WARNING "jffs2_do_read_inode(): No data nodes found for ino #%u\n", f->inocache->ino); 719 JFFS2_WARNING("no data nodes found for ino #%u\n", f->inocache->ino);
591 if (!fd_list) { 720 if (!fd_list) {
592 if (f->inocache->state == INO_STATE_READING) 721 if (f->inocache->state == INO_STATE_READING)
593 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT); 722 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
594 return -EIO; 723 return -EIO;
595 } 724 }
596 printk(KERN_WARNING "jffs2_do_read_inode(): But it has children so we fake some modes for it\n"); 725 JFFS2_NOTICE("but it has children so we fake some modes for it\n");
597 } 726 }
598 latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO); 727 latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
599 latest_node->version = cpu_to_je32(0); 728 latest_node->version = cpu_to_je32(0);
@@ -608,8 +737,8 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
608 737
609 ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(*latest_node), &retlen, (void *)latest_node); 738 ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(*latest_node), &retlen, (void *)latest_node);
610 if (ret || retlen != sizeof(*latest_node)) { 739 if (ret || retlen != sizeof(*latest_node)) {
611 printk(KERN_NOTICE "MTD read in jffs2_do_read_inode() failed: Returned %d, %zd of %zd bytes read\n", 740 JFFS2_ERROR("failed to read from flash: error %d, %zd of %zd bytes read\n",
612 ret, retlen, sizeof(*latest_node)); 741 ret, retlen, sizeof(*latest_node));
613 /* FIXME: If this fails, there seems to be a memory leak. Find it. */ 742 /* FIXME: If this fails, there seems to be a memory leak. Find it. */
614 up(&f->sem); 743 up(&f->sem);
615 jffs2_do_clear_inode(c, f); 744 jffs2_do_clear_inode(c, f);
@@ -618,7 +747,8 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
618 747
619 crc = crc32(0, latest_node, sizeof(*latest_node)-8); 748 crc = crc32(0, latest_node, sizeof(*latest_node)-8);
620 if (crc != je32_to_cpu(latest_node->node_crc)) { 749 if (crc != je32_to_cpu(latest_node->node_crc)) {
621 printk(KERN_NOTICE "CRC failed for read_inode of inode %u at physical location 0x%x\n", f->inocache->ino, ref_offset(fn->raw)); 750 JFFS2_ERROR("CRC failed for read_inode of inode %u at physical location 0x%x\n",
751 f->inocache->ino, ref_offset(fn->raw));
622 up(&f->sem); 752 up(&f->sem);
623 jffs2_do_clear_inode(c, f); 753 jffs2_do_clear_inode(c, f);
624 return -EIO; 754 return -EIO;
@@ -633,10 +763,10 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
633 } 763 }
634 break; 764 break;
635 765
636 766
637 case S_IFREG: 767 case S_IFREG:
638 /* If it was a regular file, truncate it to the latest node's isize */ 768 /* If it was a regular file, truncate it to the latest node's isize */
639 jffs2_truncate_fraglist(c, &f->fragtree, je32_to_cpu(latest_node->isize)); 769 jffs2_truncate_fragtree(c, &f->fragtree, je32_to_cpu(latest_node->isize));
640 break; 770 break;
641 771
642 case S_IFLNK: 772 case S_IFLNK:
@@ -649,37 +779,33 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
649 779
650 if (f->inocache->state != INO_STATE_CHECKING) { 780 if (f->inocache->state != INO_STATE_CHECKING) {
651 /* Symlink's inode data is the target path. Read it and 781 /* Symlink's inode data is the target path. Read it and
652 * keep in RAM to facilitate quick follow symlink operation. 782 * keep in RAM to facilitate quick follow symlink
653 * We use f->dents field to store the target path, which 783 * operation. */
654 * is somewhat ugly. */ 784 f->target = kmalloc(je32_to_cpu(latest_node->csize) + 1, GFP_KERNEL);
655 f->dents = kmalloc(je32_to_cpu(latest_node->csize) + 1, GFP_KERNEL); 785 if (!f->target) {
656 if (!f->dents) { 786 JFFS2_ERROR("can't allocate %d bytes of memory for the symlink target path cache\n", je32_to_cpu(latest_node->csize));
657 printk(KERN_WARNING "Can't allocate %d bytes of memory "
658 "for the symlink target path cache\n",
659 je32_to_cpu(latest_node->csize));
660 up(&f->sem); 787 up(&f->sem);
661 jffs2_do_clear_inode(c, f); 788 jffs2_do_clear_inode(c, f);
662 return -ENOMEM; 789 return -ENOMEM;
663 } 790 }
664 791
665 ret = jffs2_flash_read(c, ref_offset(fn->raw) + sizeof(*latest_node), 792 ret = jffs2_flash_read(c, ref_offset(fn->raw) + sizeof(*latest_node),
666 je32_to_cpu(latest_node->csize), &retlen, (char *)f->dents); 793 je32_to_cpu(latest_node->csize), &retlen, (char *)f->target);
667 794
668 if (ret || retlen != je32_to_cpu(latest_node->csize)) { 795 if (ret || retlen != je32_to_cpu(latest_node->csize)) {
669 if (retlen != je32_to_cpu(latest_node->csize)) 796 if (retlen != je32_to_cpu(latest_node->csize))
670 ret = -EIO; 797 ret = -EIO;
671 kfree(f->dents); 798 kfree(f->target);
672 f->dents = NULL; 799 f->target = NULL;
673 up(&f->sem); 800 up(&f->sem);
674 jffs2_do_clear_inode(c, f); 801 jffs2_do_clear_inode(c, f);
675 return -ret; 802 return -ret;
676 } 803 }
677 804
678 ((char *)f->dents)[je32_to_cpu(latest_node->csize)] = '\0'; 805 f->target[je32_to_cpu(latest_node->csize)] = '\0';
679 D1(printk(KERN_DEBUG "jffs2_do_read_inode(): symlink's target '%s' cached\n", 806 dbg_readinode("symlink's target '%s' cached\n", f->target);
680 (char *)f->dents));
681 } 807 }
682 808
683 /* fall through... */ 809 /* fall through... */
684 810
685 case S_IFBLK: 811 case S_IFBLK:
@@ -687,14 +813,14 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
687 /* Certain inode types should have only one data node, and it's 813 /* Certain inode types should have only one data node, and it's
688 kept as the metadata node */ 814 kept as the metadata node */
689 if (f->metadata) { 815 if (f->metadata) {
690 printk(KERN_WARNING "Argh. Special inode #%u with mode 0%o had metadata node\n", 816 JFFS2_ERROR("Argh. Special inode #%u with mode 0%o had metadata node\n",
691 f->inocache->ino, jemode_to_cpu(latest_node->mode)); 817 f->inocache->ino, jemode_to_cpu(latest_node->mode));
692 up(&f->sem); 818 up(&f->sem);
693 jffs2_do_clear_inode(c, f); 819 jffs2_do_clear_inode(c, f);
694 return -EIO; 820 return -EIO;
695 } 821 }
696 if (!frag_first(&f->fragtree)) { 822 if (!frag_first(&f->fragtree)) {
697 printk(KERN_WARNING "Argh. Special inode #%u with mode 0%o has no fragments\n", 823 JFFS2_ERROR("Argh. Special inode #%u with mode 0%o has no fragments\n",
698 f->inocache->ino, jemode_to_cpu(latest_node->mode)); 824 f->inocache->ino, jemode_to_cpu(latest_node->mode));
699 up(&f->sem); 825 up(&f->sem);
700 jffs2_do_clear_inode(c, f); 826 jffs2_do_clear_inode(c, f);
@@ -702,7 +828,7 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
702 } 828 }
703 /* ASSERT: f->fraglist != NULL */ 829 /* ASSERT: f->fraglist != NULL */
704 if (frag_next(frag_first(&f->fragtree))) { 830 if (frag_next(frag_first(&f->fragtree))) {
705 printk(KERN_WARNING "Argh. Special inode #%u with mode 0x%x had more than one node\n", 831 JFFS2_ERROR("Argh. Special inode #%u with mode 0x%x had more than one node\n",
706 f->inocache->ino, jemode_to_cpu(latest_node->mode)); 832 f->inocache->ino, jemode_to_cpu(latest_node->mode));
707 /* FIXME: Deal with it - check crc32, check for duplicate node, check times and discard the older one */ 833 /* FIXME: Deal with it - check crc32, check for duplicate node, check times and discard the older one */
708 up(&f->sem); 834 up(&f->sem);
@@ -721,6 +847,93 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
721 return 0; 847 return 0;
722} 848}
723 849
850/* Scan the list of all nodes present for this ino, build map of versions, etc. */
851int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
852 uint32_t ino, struct jffs2_raw_inode *latest_node)
853{
854 dbg_readinode("read inode #%u\n", ino);
855
856 retry_inocache:
857 spin_lock(&c->inocache_lock);
858 f->inocache = jffs2_get_ino_cache(c, ino);
859
860 if (f->inocache) {
861 /* Check its state. We may need to wait before we can use it */
862 switch(f->inocache->state) {
863 case INO_STATE_UNCHECKED:
864 case INO_STATE_CHECKEDABSENT:
865 f->inocache->state = INO_STATE_READING;
866 break;
867
868 case INO_STATE_CHECKING:
869 case INO_STATE_GC:
870 /* If it's in either of these states, we need
871 to wait for whoever's got it to finish and
872 put it back. */
873 dbg_readinode("waiting for ino #%u in state %d\n", ino, f->inocache->state);
874 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
875 goto retry_inocache;
876
877 case INO_STATE_READING:
878 case INO_STATE_PRESENT:
879 /* Eep. This should never happen. It can
880 happen if Linux calls read_inode() again
881 before clear_inode() has finished though. */
882 JFFS2_ERROR("Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
883 /* Fail. That's probably better than allowing it to succeed */
884 f->inocache = NULL;
885 break;
886
887 default:
888 BUG();
889 }
890 }
891 spin_unlock(&c->inocache_lock);
892
893 if (!f->inocache && ino == 1) {
894 /* Special case - no root inode on medium */
895 f->inocache = jffs2_alloc_inode_cache();
896 if (!f->inocache) {
897 JFFS2_ERROR("cannot allocate inocache for root inode\n");
898 return -ENOMEM;
899 }
900 dbg_readinode("creating inocache for root inode\n");
901 memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
902 f->inocache->ino = f->inocache->nlink = 1;
903 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
904 f->inocache->state = INO_STATE_READING;
905 jffs2_add_ino_cache(c, f->inocache);
906 }
907 if (!f->inocache) {
908 JFFS2_ERROR("requestied to read an nonexistent ino %u\n", ino);
909 return -ENOENT;
910 }
911
912 return jffs2_do_read_inode_internal(c, f, latest_node);
913}
914
915int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
916{
917 struct jffs2_raw_inode n;
918 struct jffs2_inode_info *f = kmalloc(sizeof(*f), GFP_KERNEL);
919 int ret;
920
921 if (!f)
922 return -ENOMEM;
923
924 memset(f, 0, sizeof(*f));
925 init_MUTEX_LOCKED(&f->sem);
926 f->inocache = ic;
927
928 ret = jffs2_do_read_inode_internal(c, f, &n);
929 if (!ret) {
930 up(&f->sem);
931 jffs2_do_clear_inode(c, f);
932 }
933 kfree (f);
934 return ret;
935}
936
724void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f) 937void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
725{ 938{
726 struct jffs2_full_dirent *fd, *fds; 939 struct jffs2_full_dirent *fd, *fds;
@@ -740,20 +953,16 @@ void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
740 953
741 jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL); 954 jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
742 955
743 /* For symlink inodes we us f->dents to store the target path name */ 956 if (f->target) {
744 if (S_ISLNK(OFNI_EDONI_2SFFJ(f)->i_mode)) { 957 kfree(f->target);
745 if (f->dents) { 958 f->target = NULL;
746 kfree(f->dents); 959 }
747 f->dents = NULL;
748 }
749 } else {
750 fds = f->dents;
751 960
752 while(fds) { 961 fds = f->dents;
753 fd = fds; 962 while(fds) {
754 fds = fd->next; 963 fd = fds;
755 jffs2_free_full_dirent(fd); 964 fds = fd->next;
756 } 965 jffs2_free_full_dirent(fd);
757 } 966 }
758 967
759 if (f->inocache && f->inocache->state != INO_STATE_CHECKING) { 968 if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {