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