/src/barebox/fs/jffs2/scan.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 | | #include <common.h> |
12 | | #include <crc.h> |
13 | | #include <linux/kernel.h> |
14 | | #include <linux/sched.h> |
15 | | #include <linux/mtd/mtd.h> |
16 | | #include <linux/pagemap.h> |
17 | | #include "nodelist.h" |
18 | | #include "summary.h" |
19 | | #include "debug.h" |
20 | | |
21 | 0 | #define DEFAULT_EMPTY_SCAN_SIZE 256 |
22 | | |
23 | 0 | #define noisy_printk(noise, fmt, ...) \ |
24 | 0 | do { \ |
25 | 0 | if (*(noise)) { \ |
26 | 0 | pr_notice(fmt, ##__VA_ARGS__); \ |
27 | 0 | (*(noise))--; \ |
28 | 0 | if (!(*(noise))) \ |
29 | 0 | pr_notice("Further such events for this erase block will not be printed\n"); \ |
30 | 0 | } \ |
31 | 0 | } while (0) |
32 | | |
33 | | static uint32_t pseudo_random; |
34 | | |
35 | | static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
36 | | unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s); |
37 | | |
38 | | /* These helper functions _must_ increase ofs and also do the dirty/used space accounting. |
39 | | * Returning an error will abort the mount - bad checksums etc. should just mark the space |
40 | | * as dirty. |
41 | | */ |
42 | | static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
43 | | struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s); |
44 | | static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
45 | | struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s); |
46 | | |
47 | | static inline int min_free(struct jffs2_sb_info *c) |
48 | 0 | { |
49 | 0 | uint32_t min = 2 * sizeof(struct jffs2_raw_inode); |
50 | 0 | return min; |
51 | 0 | } |
52 | | |
53 | 0 | static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) { |
54 | 0 | if (sector_size < DEFAULT_EMPTY_SCAN_SIZE) |
55 | 0 | return sector_size; |
56 | 0 | else |
57 | 0 | return DEFAULT_EMPTY_SCAN_SIZE; |
58 | 0 | } |
59 | | |
60 | | static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) |
61 | 0 | { |
62 | 0 | int ret; |
63 | |
|
64 | 0 | if ((ret = jffs2_prealloc_raw_node_refs(c, jeb, 1))) |
65 | 0 | return ret; |
66 | 0 | if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size))) |
67 | 0 | return ret; |
68 | | /* Turned wasted size into dirty, since we apparently |
69 | | think it's recoverable now. */ |
70 | 0 | jeb->dirty_size += jeb->wasted_size; |
71 | 0 | c->dirty_size += jeb->wasted_size; |
72 | 0 | c->wasted_size -= jeb->wasted_size; |
73 | 0 | jeb->wasted_size = 0; |
74 | 0 | if (VERYDIRTY(c, jeb->dirty_size)) { |
75 | 0 | list_add(&jeb->list, &c->very_dirty_list); |
76 | 0 | } else { |
77 | 0 | list_add(&jeb->list, &c->dirty_list); |
78 | 0 | } |
79 | 0 | return 0; |
80 | 0 | } |
81 | | |
82 | | int jffs2_scan_medium(struct jffs2_sb_info *c) |
83 | 0 | { |
84 | 0 | int i, ret; |
85 | 0 | uint32_t empty_blocks = 0, bad_blocks = 0; |
86 | 0 | unsigned char *flashbuf = NULL; |
87 | 0 | uint32_t buf_size = 0; |
88 | 0 | struct jffs2_summary *s = NULL; /* summary info collected by the scan process */ |
89 | 0 | size_t try_size; |
90 | |
|
91 | 0 | if (!flashbuf) { |
92 | | /* For NAND it's quicker to read a whole eraseblock at a time, |
93 | | apparently */ |
94 | 0 | if (jffs2_cleanmarker_oob(c)) |
95 | 0 | try_size = c->sector_size; |
96 | 0 | else |
97 | 0 | try_size = PAGE_SIZE; |
98 | |
|
99 | 0 | jffs2_dbg(1, "Trying to allocate readbuf of %zu " |
100 | 0 | "bytes\n", try_size); |
101 | |
|
102 | 0 | flashbuf = kmalloc(try_size, GFP_KERNEL); |
103 | 0 | if (!flashbuf) |
104 | 0 | return -ENOMEM; |
105 | | |
106 | 0 | jffs2_dbg(1, "Allocated readbuf of %zu bytes\n", |
107 | 0 | try_size); |
108 | |
|
109 | 0 | buf_size = (uint32_t)try_size; |
110 | 0 | } |
111 | | |
112 | 0 | if (jffs2_sum_active()) { |
113 | 0 | s = kzalloc(sizeof(struct jffs2_summary), GFP_KERNEL); |
114 | 0 | if (!s) { |
115 | 0 | JFFS2_WARNING("Can't allocate memory for summary\n"); |
116 | 0 | ret = -ENOMEM; |
117 | 0 | goto out; |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | 0 | for (i=0; i<c->nr_blocks; i++) { |
122 | 0 | struct jffs2_eraseblock *jeb = &c->blocks[i]; |
123 | |
|
124 | 0 | cond_resched(); |
125 | | |
126 | | /* reset summary info for next eraseblock scan */ |
127 | 0 | jffs2_sum_reset_collected(s); |
128 | |
|
129 | 0 | ret = jffs2_scan_eraseblock(c, jeb, buf_size ? flashbuf : (flashbuf + jeb->offset), |
130 | 0 | buf_size, s); |
131 | |
|
132 | 0 | if (ret < 0) |
133 | 0 | goto out; |
134 | | |
135 | 0 | jffs2_dbg_acct_paranoia_check_nolock(c, jeb); |
136 | | |
137 | | /* Now decide which list to put it on */ |
138 | 0 | switch(ret) { |
139 | 0 | case BLK_STATE_ALLFF: |
140 | | /* |
141 | | * Empty block. Since we can't be sure it |
142 | | * was entirely erased, we just queue it for erase |
143 | | * again. It will be marked as such when the erase |
144 | | * is complete. Meanwhile we still count it as empty |
145 | | * for later checks. |
146 | | */ |
147 | 0 | empty_blocks++; |
148 | 0 | list_add(&jeb->list, &c->erase_pending_list); |
149 | 0 | c->nr_erasing_blocks++; |
150 | 0 | break; |
151 | | |
152 | 0 | case BLK_STATE_CLEANMARKER: |
153 | | /* Only a CLEANMARKER node is valid */ |
154 | 0 | if (!jeb->dirty_size) { |
155 | | /* It's actually free */ |
156 | 0 | list_add(&jeb->list, &c->free_list); |
157 | 0 | c->nr_free_blocks++; |
158 | 0 | } else { |
159 | | /* Dirt */ |
160 | 0 | jffs2_dbg(1, "Adding all-dirty block at 0x%08x to erase_pending_list\n", |
161 | 0 | jeb->offset); |
162 | 0 | list_add(&jeb->list, &c->erase_pending_list); |
163 | 0 | c->nr_erasing_blocks++; |
164 | 0 | } |
165 | 0 | break; |
166 | | |
167 | 0 | case BLK_STATE_CLEAN: |
168 | | /* Full (or almost full) of clean data. Clean list */ |
169 | 0 | list_add(&jeb->list, &c->clean_list); |
170 | 0 | break; |
171 | | |
172 | 0 | case BLK_STATE_PARTDIRTY: |
173 | | /* Some data, but not full. Dirty list. */ |
174 | | /* We want to remember the block with most free space |
175 | | and stick it in the 'nextblock' position to start writing to it. */ |
176 | 0 | if (jeb->free_size > min_free(c) && |
177 | 0 | (!c->nextblock || c->nextblock->free_size < jeb->free_size)) { |
178 | | /* Better candidate for the next writes to go to */ |
179 | 0 | if (c->nextblock) { |
180 | 0 | ret = file_dirty(c, c->nextblock); |
181 | 0 | if (ret) |
182 | 0 | goto out; |
183 | | /* deleting summary information of the old nextblock */ |
184 | 0 | jffs2_sum_reset_collected(c->summary); |
185 | 0 | } |
186 | | /* update collected summary information for the current nextblock */ |
187 | 0 | jffs2_sum_move_collected(c, s); |
188 | 0 | jffs2_dbg(1, "%s(): new nextblock = 0x%08x\n", |
189 | 0 | __func__, jeb->offset); |
190 | 0 | c->nextblock = jeb; |
191 | 0 | } else { |
192 | 0 | ret = file_dirty(c, jeb); |
193 | 0 | if (ret) |
194 | 0 | goto out; |
195 | 0 | } |
196 | 0 | break; |
197 | | |
198 | 0 | case BLK_STATE_ALLDIRTY: |
199 | | /* Nothing valid - not even a clean marker. Needs erasing. */ |
200 | | /* For now we just put it on the erasing list. We'll start the erases later */ |
201 | 0 | jffs2_dbg(1, "Erase block at 0x%08x is not formatted. It will be erased\n", |
202 | 0 | jeb->offset); |
203 | 0 | list_add(&jeb->list, &c->erase_pending_list); |
204 | 0 | c->nr_erasing_blocks++; |
205 | 0 | break; |
206 | | |
207 | 0 | case BLK_STATE_BADBLOCK: |
208 | 0 | jffs2_dbg(1, "Block at 0x%08x is bad\n", jeb->offset); |
209 | 0 | list_add(&jeb->list, &c->bad_list); |
210 | 0 | c->bad_size += c->sector_size; |
211 | 0 | c->free_size -= c->sector_size; |
212 | 0 | bad_blocks++; |
213 | 0 | break; |
214 | 0 | default: |
215 | 0 | pr_warn("%s(): unknown block state\n", __func__); |
216 | 0 | BUG(); |
217 | 0 | } |
218 | 0 | } |
219 | | |
220 | | /* Nextblock dirty is always seen as wasted, because we cannot recycle it now */ |
221 | 0 | if (c->nextblock && (c->nextblock->dirty_size)) { |
222 | 0 | c->nextblock->wasted_size += c->nextblock->dirty_size; |
223 | 0 | c->wasted_size += c->nextblock->dirty_size; |
224 | 0 | c->dirty_size -= c->nextblock->dirty_size; |
225 | 0 | c->nextblock->dirty_size = 0; |
226 | 0 | } |
227 | |
|
228 | 0 | if (c->nr_erasing_blocks) { |
229 | 0 | if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) { |
230 | 0 | pr_notice("Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n"); |
231 | 0 | pr_notice("empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n", |
232 | 0 | empty_blocks, bad_blocks, c->nr_blocks); |
233 | 0 | ret = -EIO; |
234 | 0 | goto out; |
235 | 0 | } |
236 | 0 | } |
237 | 0 | ret = 0; |
238 | 0 | out: |
239 | 0 | if (buf_size) |
240 | 0 | kfree(flashbuf); |
241 | 0 | kfree(s); |
242 | 0 | return ret; |
243 | 0 | } |
244 | | |
245 | | static int jffs2_fill_scan_buf(struct jffs2_sb_info *c, void *buf, |
246 | | uint32_t ofs, uint32_t len) |
247 | 0 | { |
248 | 0 | int ret; |
249 | 0 | size_t retlen; |
250 | |
|
251 | 0 | ret = jffs2_flash_read(c, ofs, len, &retlen, buf); |
252 | 0 | if (ret) { |
253 | 0 | jffs2_dbg(1, "mtd->read(0x%x bytes from 0x%x) returned %d\n", |
254 | 0 | len, ofs, ret); |
255 | 0 | return ret; |
256 | 0 | } |
257 | 0 | if (retlen < len) { |
258 | 0 | jffs2_dbg(1, "Read at 0x%x gave only 0x%zx bytes\n", |
259 | 0 | ofs, retlen); |
260 | 0 | return -EIO; |
261 | 0 | } |
262 | 0 | return 0; |
263 | 0 | } |
264 | | |
265 | | int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) |
266 | 0 | { |
267 | 0 | if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size |
268 | 0 | && (!jeb->first_node || !ref_next(jeb->first_node)) ) |
269 | 0 | return BLK_STATE_CLEANMARKER; |
270 | | |
271 | | /* move blocks with max 4 byte dirty space to cleanlist */ |
272 | 0 | else if (!ISDIRTY(c->sector_size - (jeb->used_size + jeb->unchecked_size))) { |
273 | 0 | c->dirty_size -= jeb->dirty_size; |
274 | 0 | c->wasted_size += jeb->dirty_size; |
275 | 0 | jeb->wasted_size += jeb->dirty_size; |
276 | 0 | jeb->dirty_size = 0; |
277 | 0 | return BLK_STATE_CLEAN; |
278 | 0 | } else if (jeb->used_size || jeb->unchecked_size) |
279 | 0 | return BLK_STATE_PARTDIRTY; |
280 | 0 | else |
281 | 0 | return BLK_STATE_ALLDIRTY; |
282 | 0 | } |
283 | | |
284 | | #ifdef CONFIG_JFFS2_FS_XATTR |
285 | | static int jffs2_scan_xattr_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
286 | | struct jffs2_raw_xattr *rx, uint32_t ofs, |
287 | | struct jffs2_summary *s) |
288 | | { |
289 | | struct jffs2_xattr_datum *xd; |
290 | | uint32_t xid, version, totlen, crc; |
291 | | int err; |
292 | | |
293 | | crc = crc32(0, rx, sizeof(struct jffs2_raw_xattr) - 4); |
294 | | if (crc != je32_to_cpu(rx->node_crc)) { |
295 | | JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n", |
296 | | ofs, je32_to_cpu(rx->node_crc), crc); |
297 | | if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen)))) |
298 | | return err; |
299 | | return 0; |
300 | | } |
301 | | |
302 | | xid = je32_to_cpu(rx->xid); |
303 | | version = je32_to_cpu(rx->version); |
304 | | |
305 | | totlen = PAD(sizeof(struct jffs2_raw_xattr) |
306 | | + rx->name_len + 1 + je16_to_cpu(rx->value_len)); |
307 | | if (totlen != je32_to_cpu(rx->totlen)) { |
308 | | JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%u\n", |
309 | | ofs, je32_to_cpu(rx->totlen), totlen); |
310 | | if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen)))) |
311 | | return err; |
312 | | return 0; |
313 | | } |
314 | | |
315 | | xd = jffs2_setup_xattr_datum(c, xid, version); |
316 | | if (IS_ERR(xd)) |
317 | | return PTR_ERR(xd); |
318 | | |
319 | | if (xd->version > version) { |
320 | | struct jffs2_raw_node_ref *raw |
321 | | = jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, NULL); |
322 | | raw->next_in_ino = xd->node->next_in_ino; |
323 | | xd->node->next_in_ino = raw; |
324 | | } else { |
325 | | xd->version = version; |
326 | | xd->xprefix = rx->xprefix; |
327 | | xd->name_len = rx->name_len; |
328 | | xd->value_len = je16_to_cpu(rx->value_len); |
329 | | xd->data_crc = je32_to_cpu(rx->data_crc); |
330 | | |
331 | | jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, (void *)xd); |
332 | | } |
333 | | |
334 | | if (jffs2_sum_active()) |
335 | | jffs2_sum_add_xattr_mem(s, rx, ofs - jeb->offset); |
336 | | dbg_xattr("scanning xdatum at %#08x (xid=%u, version=%u)\n", |
337 | | ofs, xd->xid, xd->version); |
338 | | return 0; |
339 | | } |
340 | | |
341 | | static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
342 | | struct jffs2_raw_xref *rr, uint32_t ofs, |
343 | | struct jffs2_summary *s) |
344 | | { |
345 | | struct jffs2_xattr_ref *ref; |
346 | | uint32_t crc; |
347 | | int err; |
348 | | |
349 | | crc = crc32(0, rr, sizeof(*rr) - 4); |
350 | | if (crc != je32_to_cpu(rr->node_crc)) { |
351 | | JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n", |
352 | | ofs, je32_to_cpu(rr->node_crc), crc); |
353 | | if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rr->totlen))))) |
354 | | return err; |
355 | | return 0; |
356 | | } |
357 | | |
358 | | if (PAD(sizeof(struct jffs2_raw_xref)) != je32_to_cpu(rr->totlen)) { |
359 | | JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%zd\n", |
360 | | ofs, je32_to_cpu(rr->totlen), |
361 | | PAD(sizeof(struct jffs2_raw_xref))); |
362 | | if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rr->totlen)))) |
363 | | return err; |
364 | | return 0; |
365 | | } |
366 | | |
367 | | ref = jffs2_alloc_xattr_ref(); |
368 | | if (!ref) |
369 | | return -ENOMEM; |
370 | | |
371 | | /* BEFORE jffs2_build_xattr_subsystem() called, |
372 | | * and AFTER xattr_ref is marked as a dead xref, |
373 | | * ref->xid is used to store 32bit xid, xd is not used |
374 | | * ref->ino is used to store 32bit inode-number, ic is not used |
375 | | * Thoes variables are declared as union, thus using those |
376 | | * are exclusive. In a similar way, ref->next is temporarily |
377 | | * used to chain all xattr_ref object. It's re-chained to |
378 | | * jffs2_inode_cache in jffs2_build_xattr_subsystem() correctly. |
379 | | */ |
380 | | ref->ino = je32_to_cpu(rr->ino); |
381 | | ref->xid = je32_to_cpu(rr->xid); |
382 | | ref->xseqno = je32_to_cpu(rr->xseqno); |
383 | | if (ref->xseqno > c->highest_xseqno) |
384 | | c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER); |
385 | | ref->next = c->xref_temp; |
386 | | c->xref_temp = ref; |
387 | | |
388 | | jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(rr->totlen)), (void *)ref); |
389 | | |
390 | | if (jffs2_sum_active()) |
391 | | jffs2_sum_add_xref_mem(s, rr, ofs - jeb->offset); |
392 | | dbg_xattr("scan xref at %#08x (xid=%u, ino=%u)\n", |
393 | | ofs, ref->xid, ref->ino); |
394 | | return 0; |
395 | | } |
396 | | #endif |
397 | | |
398 | | /* Called with 'buf_size == 0' if buf is in fact a pointer _directly_ into |
399 | | the flash, XIP-style */ |
400 | | static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
401 | 0 | unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s) { |
402 | 0 | struct jffs2_unknown_node *node; |
403 | 0 | struct jffs2_unknown_node crcnode; |
404 | 0 | uint32_t ofs, prevofs, max_ofs; |
405 | 0 | uint32_t hdr_crc, buf_ofs, buf_len; |
406 | 0 | int err; |
407 | 0 | int noise = 0; |
408 | |
|
409 | 0 | ofs = jeb->offset; |
410 | 0 | prevofs = jeb->offset - 1; |
411 | |
|
412 | 0 | jffs2_dbg(1, "%s(): Scanning block at 0x%x\n", __func__, ofs); |
413 | |
|
414 | 0 | if (jffs2_sum_active()) { |
415 | 0 | struct jffs2_sum_marker *sm; |
416 | 0 | void *sumptr = NULL; |
417 | 0 | uint32_t sumlen; |
418 | |
|
419 | 0 | if (!buf_size) { |
420 | | /* XIP case. Just look, point at the summary if it's there */ |
421 | 0 | sm = (void *)buf + c->sector_size - sizeof(*sm); |
422 | 0 | if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) { |
423 | 0 | sumptr = buf + je32_to_cpu(sm->offset); |
424 | 0 | sumlen = c->sector_size - je32_to_cpu(sm->offset); |
425 | 0 | } |
426 | 0 | } else { |
427 | | /* If NAND flash, read a whole page of it. Else just the end */ |
428 | 0 | if (c->wbuf_pagesize) |
429 | 0 | buf_len = c->wbuf_pagesize; |
430 | 0 | else |
431 | 0 | buf_len = sizeof(*sm); |
432 | | |
433 | | /* Read as much as we want into the _end_ of the preallocated buffer */ |
434 | 0 | err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len, |
435 | 0 | jeb->offset + c->sector_size - buf_len, |
436 | 0 | buf_len); |
437 | 0 | if (err) |
438 | 0 | return err; |
439 | | |
440 | 0 | sm = (void *)buf + buf_size - sizeof(*sm); |
441 | 0 | if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) { |
442 | 0 | sumlen = c->sector_size - je32_to_cpu(sm->offset); |
443 | 0 | sumptr = buf + buf_size - sumlen; |
444 | | |
445 | | /* sm->offset maybe wrong but MAGIC maybe right */ |
446 | 0 | if (sumlen > c->sector_size) |
447 | 0 | goto full_scan; |
448 | | |
449 | | /* Now, make sure the summary itself is available */ |
450 | 0 | if (sumlen > buf_size) { |
451 | | /* Need to kmalloc for this. */ |
452 | 0 | sumptr = kmalloc(sumlen, GFP_KERNEL); |
453 | 0 | if (!sumptr) |
454 | 0 | return -ENOMEM; |
455 | 0 | memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len); |
456 | 0 | } |
457 | 0 | if (buf_len < sumlen) { |
458 | | /* Need to read more so that the entire summary node is present */ |
459 | 0 | err = jffs2_fill_scan_buf(c, sumptr, |
460 | 0 | jeb->offset + c->sector_size - sumlen, |
461 | 0 | sumlen - buf_len); |
462 | 0 | if (err) { |
463 | 0 | if (sumlen > buf_size) |
464 | 0 | kfree(sumptr); |
465 | 0 | return err; |
466 | 0 | } |
467 | 0 | } |
468 | 0 | } |
469 | |
|
470 | 0 | } |
471 | | |
472 | 0 | if (sumptr) { |
473 | 0 | err = jffs2_sum_scan_sumnode(c, jeb, sumptr, sumlen, &pseudo_random); |
474 | |
|
475 | 0 | if (buf_size && sumlen > buf_size) |
476 | 0 | kfree(sumptr); |
477 | | /* If it returns with a real error, bail. |
478 | | If it returns positive, that's a block classification |
479 | | (i.e. BLK_STATE_xxx) so return that too. |
480 | | If it returns zero, fall through to full scan. */ |
481 | 0 | if (err) |
482 | 0 | return err; |
483 | 0 | } |
484 | 0 | } |
485 | | |
486 | 0 | full_scan: |
487 | 0 | buf_ofs = jeb->offset; |
488 | |
|
489 | 0 | if (!buf_size) { |
490 | | /* This is the XIP case -- we're reading _directly_ from the flash chip */ |
491 | 0 | buf_len = c->sector_size; |
492 | 0 | } else { |
493 | 0 | buf_len = EMPTY_SCAN_SIZE(c->sector_size); |
494 | 0 | err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len); |
495 | 0 | if (err) |
496 | 0 | return err; |
497 | 0 | } |
498 | | |
499 | | /* We temporarily use 'ofs' as a pointer into the buffer/jeb */ |
500 | 0 | ofs = 0; |
501 | 0 | max_ofs = EMPTY_SCAN_SIZE(c->sector_size); |
502 | | /* Scan only EMPTY_SCAN_SIZE of 0xFF before declaring it's empty */ |
503 | 0 | while(ofs < max_ofs && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF) |
504 | 0 | ofs += 4; |
505 | |
|
506 | 0 | if (ofs == max_ofs) { |
507 | 0 | jffs2_dbg(1, "Block at 0x%08x is empty (erased)\n", |
508 | 0 | jeb->offset); |
509 | 0 | if (c->cleanmarker_size == 0) |
510 | 0 | return BLK_STATE_CLEANMARKER; /* don't bother with re-erase */ |
511 | 0 | else |
512 | 0 | return BLK_STATE_ALLFF; /* OK to erase if all blocks are like this */ |
513 | 0 | } |
514 | 0 | if (ofs) { |
515 | 0 | jffs2_dbg(1, "Free space at %08x ends at %08x\n", jeb->offset, |
516 | 0 | jeb->offset + ofs); |
517 | 0 | if ((err = jffs2_prealloc_raw_node_refs(c, jeb, 1))) |
518 | 0 | return err; |
519 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, ofs))) |
520 | 0 | return err; |
521 | 0 | } |
522 | | |
523 | | /* Now ofs is a complete physical flash offset as it always was... */ |
524 | 0 | ofs += jeb->offset; |
525 | |
|
526 | 0 | noise = 10; |
527 | |
|
528 | 0 | dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset); |
529 | |
|
530 | 0 | scan_more: |
531 | 0 | while(ofs < jeb->offset + c->sector_size) { |
532 | |
|
533 | 0 | jffs2_dbg_acct_paranoia_check_nolock(c, jeb); |
534 | | |
535 | | /* Make sure there are node refs available for use */ |
536 | 0 | err = jffs2_prealloc_raw_node_refs(c, jeb, 2); |
537 | 0 | if (err) |
538 | 0 | return err; |
539 | | |
540 | 0 | cond_resched(); |
541 | |
|
542 | 0 | if (ofs & 3) { |
543 | 0 | pr_warn("Eep. ofs 0x%08x not word-aligned!\n", ofs); |
544 | 0 | ofs = PAD(ofs); |
545 | 0 | continue; |
546 | 0 | } |
547 | 0 | if (ofs == prevofs) { |
548 | 0 | pr_warn("ofs 0x%08x has already been seen. Skipping\n", |
549 | 0 | ofs); |
550 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, 4))) |
551 | 0 | return err; |
552 | 0 | ofs += 4; |
553 | 0 | continue; |
554 | 0 | } |
555 | 0 | prevofs = ofs; |
556 | |
|
557 | 0 | if (jeb->offset + c->sector_size < ofs + sizeof(*node)) { |
558 | 0 | jffs2_dbg(1, "Fewer than %zd bytes left to end of block. (%x+%x<%x+%zx) Not reading\n", |
559 | 0 | sizeof(struct jffs2_unknown_node), |
560 | 0 | jeb->offset, c->sector_size, ofs, |
561 | 0 | sizeof(*node)); |
562 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, (jeb->offset + c->sector_size)-ofs))) |
563 | 0 | return err; |
564 | 0 | break; |
565 | 0 | } |
566 | | |
567 | 0 | if (buf_ofs + buf_len < ofs + sizeof(*node)) { |
568 | 0 | buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs); |
569 | 0 | jffs2_dbg(1, "Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n", |
570 | 0 | sizeof(struct jffs2_unknown_node), |
571 | 0 | buf_len, ofs); |
572 | 0 | err = jffs2_fill_scan_buf(c, buf, ofs, buf_len); |
573 | 0 | if (err) |
574 | 0 | return err; |
575 | 0 | buf_ofs = ofs; |
576 | 0 | } |
577 | | |
578 | 0 | node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs]; |
579 | |
|
580 | 0 | if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) { |
581 | 0 | uint32_t inbuf_ofs; |
582 | 0 | uint32_t empty_start, scan_end; |
583 | |
|
584 | 0 | empty_start = ofs; |
585 | 0 | ofs += 4; |
586 | 0 | scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(c->sector_size)/8, buf_len); |
587 | |
|
588 | 0 | jffs2_dbg(1, "Found empty flash at 0x%08x\n", ofs); |
589 | 0 | more_empty: |
590 | 0 | inbuf_ofs = ofs - buf_ofs; |
591 | 0 | while (inbuf_ofs < scan_end) { |
592 | 0 | if (unlikely(*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff)) { |
593 | 0 | jffs2_dbg(1,"Empty flash at 0x%08x ends at 0x%08x\n", |
594 | 0 | empty_start, ofs); |
595 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, ofs-empty_start))) |
596 | 0 | return err; |
597 | 0 | goto scan_more; |
598 | 0 | } |
599 | | |
600 | 0 | inbuf_ofs+=4; |
601 | 0 | ofs += 4; |
602 | 0 | } |
603 | | /* Ran off end. */ |
604 | 0 | jffs2_dbg(1, "Empty flash to end of buffer at 0x%08x\n", |
605 | 0 | ofs); |
606 | | |
607 | | /* If we're only checking the beginning of a block with a cleanmarker, |
608 | | bail now */ |
609 | 0 | if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) && |
610 | 0 | c->cleanmarker_size && !jeb->dirty_size && !ref_next(jeb->first_node)) { |
611 | 0 | jffs2_dbg(1, "%d bytes at start of block seems clean... assuming all clean\n", |
612 | 0 | EMPTY_SCAN_SIZE(c->sector_size)); |
613 | 0 | return BLK_STATE_CLEANMARKER; |
614 | 0 | } |
615 | 0 | if (!buf_size && (scan_end != buf_len)) {/* XIP/point case */ |
616 | 0 | scan_end = buf_len; |
617 | 0 | goto more_empty; |
618 | 0 | } |
619 | | |
620 | | /* See how much more there is to read in this eraseblock... */ |
621 | 0 | buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs); |
622 | 0 | if (!buf_len) { |
623 | | /* No more to read. Break out of main loop without marking |
624 | | this range of empty space as dirty (because it's not) */ |
625 | 0 | jffs2_dbg(1, "Empty flash at %08x runs to end of block. Treating as free_space\n", |
626 | 0 | empty_start); |
627 | 0 | break; |
628 | 0 | } |
629 | | /* point never reaches here */ |
630 | 0 | scan_end = buf_len; |
631 | 0 | jffs2_dbg(1, "Reading another 0x%x at 0x%08x\n", |
632 | 0 | buf_len, ofs); |
633 | 0 | err = jffs2_fill_scan_buf(c, buf, ofs, buf_len); |
634 | 0 | if (err) |
635 | 0 | return err; |
636 | 0 | buf_ofs = ofs; |
637 | 0 | goto more_empty; |
638 | 0 | } |
639 | | |
640 | 0 | if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) { |
641 | 0 | pr_warn("Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", |
642 | 0 | ofs); |
643 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, 4))) |
644 | 0 | return err; |
645 | 0 | ofs += 4; |
646 | 0 | continue; |
647 | 0 | } |
648 | 0 | if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) { |
649 | 0 | jffs2_dbg(1, "Dirty bitmask at 0x%08x\n", ofs); |
650 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, 4))) |
651 | 0 | return err; |
652 | 0 | ofs += 4; |
653 | 0 | continue; |
654 | 0 | } |
655 | 0 | if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) { |
656 | 0 | pr_warn("Old JFFS2 bitmask found at 0x%08x\n", ofs); |
657 | 0 | pr_warn("You cannot use older JFFS2 filesystems with newer kernels\n"); |
658 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, 4))) |
659 | 0 | return err; |
660 | 0 | ofs += 4; |
661 | 0 | continue; |
662 | 0 | } |
663 | 0 | if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) { |
664 | | /* OK. We're out of possibilities. Whinge and move on */ |
665 | 0 | noisy_printk(&noise, "%s(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n", |
666 | 0 | __func__, |
667 | 0 | JFFS2_MAGIC_BITMASK, ofs, |
668 | 0 | je16_to_cpu(node->magic)); |
669 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, 4))) |
670 | 0 | return err; |
671 | 0 | ofs += 4; |
672 | 0 | continue; |
673 | 0 | } |
674 | | /* We seem to have a node of sorts. Check the CRC */ |
675 | 0 | crcnode.magic = node->magic; |
676 | 0 | crcnode.nodetype = cpu_to_je16( je16_to_cpu(node->nodetype) | JFFS2_NODE_ACCURATE); |
677 | 0 | crcnode.totlen = node->totlen; |
678 | 0 | hdr_crc = crc32(0, &crcnode, sizeof(crcnode)-4); |
679 | |
|
680 | 0 | if (hdr_crc != je32_to_cpu(node->hdr_crc)) { |
681 | 0 | noisy_printk(&noise, "%s(): Node at 0x%08x {0x%04x, 0x%04x, 0x%08x) has invalid CRC 0x%08x (calculated 0x%08x)\n", |
682 | 0 | __func__, |
683 | 0 | ofs, je16_to_cpu(node->magic), |
684 | 0 | je16_to_cpu(node->nodetype), |
685 | 0 | je32_to_cpu(node->totlen), |
686 | 0 | je32_to_cpu(node->hdr_crc), |
687 | 0 | hdr_crc); |
688 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, 4))) |
689 | 0 | return err; |
690 | 0 | ofs += 4; |
691 | 0 | continue; |
692 | 0 | } |
693 | | |
694 | 0 | if (ofs + je32_to_cpu(node->totlen) > jeb->offset + c->sector_size) { |
695 | | /* Eep. Node goes over the end of the erase block. */ |
696 | 0 | pr_warn("Node at 0x%08x with length 0x%08x would run over the end of the erase block\n", |
697 | 0 | ofs, je32_to_cpu(node->totlen)); |
698 | 0 | pr_warn("Perhaps the file system was created with the wrong erase size?\n"); |
699 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, 4))) |
700 | 0 | return err; |
701 | 0 | ofs += 4; |
702 | 0 | continue; |
703 | 0 | } |
704 | | |
705 | 0 | if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) { |
706 | | /* Wheee. This is an obsoleted node */ |
707 | 0 | jffs2_dbg(2, "Node at 0x%08x is obsolete. Skipping\n", |
708 | 0 | ofs); |
709 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen))))) |
710 | 0 | return err; |
711 | 0 | ofs += PAD(je32_to_cpu(node->totlen)); |
712 | 0 | continue; |
713 | 0 | } |
714 | | |
715 | 0 | switch(je16_to_cpu(node->nodetype)) { |
716 | 0 | case JFFS2_NODETYPE_INODE: |
717 | 0 | if (buf_ofs + buf_len < ofs + sizeof(struct jffs2_raw_inode)) { |
718 | 0 | buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs); |
719 | 0 | jffs2_dbg(1, "Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n", |
720 | 0 | sizeof(struct jffs2_raw_inode), |
721 | 0 | buf_len, ofs); |
722 | 0 | err = jffs2_fill_scan_buf(c, buf, ofs, buf_len); |
723 | 0 | if (err) |
724 | 0 | return err; |
725 | 0 | buf_ofs = ofs; |
726 | 0 | node = (void *)buf; |
727 | 0 | } |
728 | 0 | err = jffs2_scan_inode_node(c, jeb, (void *)node, ofs, s); |
729 | 0 | if (err) return err; |
730 | 0 | ofs += PAD(je32_to_cpu(node->totlen)); |
731 | 0 | break; |
732 | | |
733 | 0 | case JFFS2_NODETYPE_DIRENT: |
734 | 0 | if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) { |
735 | 0 | buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs); |
736 | 0 | jffs2_dbg(1, "Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n", |
737 | 0 | je32_to_cpu(node->totlen), buf_len, |
738 | 0 | ofs); |
739 | 0 | err = jffs2_fill_scan_buf(c, buf, ofs, buf_len); |
740 | 0 | if (err) |
741 | 0 | return err; |
742 | 0 | buf_ofs = ofs; |
743 | 0 | node = (void *)buf; |
744 | 0 | } |
745 | 0 | err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs, s); |
746 | 0 | if (err) return err; |
747 | 0 | ofs += PAD(je32_to_cpu(node->totlen)); |
748 | 0 | break; |
749 | | |
750 | | #ifdef CONFIG_JFFS2_FS_XATTR |
751 | | case JFFS2_NODETYPE_XATTR: |
752 | | if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) { |
753 | | buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs); |
754 | | jffs2_dbg(1, "Fewer than %d bytes (xattr node) left to end of buf. Reading 0x%x at 0x%08x\n", |
755 | | je32_to_cpu(node->totlen), buf_len, |
756 | | ofs); |
757 | | err = jffs2_fill_scan_buf(c, buf, ofs, buf_len); |
758 | | if (err) |
759 | | return err; |
760 | | buf_ofs = ofs; |
761 | | node = (void *)buf; |
762 | | } |
763 | | err = jffs2_scan_xattr_node(c, jeb, (void *)node, ofs, s); |
764 | | if (err) |
765 | | return err; |
766 | | ofs += PAD(je32_to_cpu(node->totlen)); |
767 | | break; |
768 | | case JFFS2_NODETYPE_XREF: |
769 | | if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) { |
770 | | buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs); |
771 | | jffs2_dbg(1, "Fewer than %d bytes (xref node) left to end of buf. Reading 0x%x at 0x%08x\n", |
772 | | je32_to_cpu(node->totlen), buf_len, |
773 | | ofs); |
774 | | err = jffs2_fill_scan_buf(c, buf, ofs, buf_len); |
775 | | if (err) |
776 | | return err; |
777 | | buf_ofs = ofs; |
778 | | node = (void *)buf; |
779 | | } |
780 | | err = jffs2_scan_xref_node(c, jeb, (void *)node, ofs, s); |
781 | | if (err) |
782 | | return err; |
783 | | ofs += PAD(je32_to_cpu(node->totlen)); |
784 | | break; |
785 | | #endif /* CONFIG_JFFS2_FS_XATTR */ |
786 | | |
787 | 0 | case JFFS2_NODETYPE_CLEANMARKER: |
788 | 0 | jffs2_dbg(1, "CLEANMARKER node found at 0x%08x\n", ofs); |
789 | 0 | if (je32_to_cpu(node->totlen) != c->cleanmarker_size) { |
790 | 0 | jffs2_dbg(1, "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n", |
791 | 0 | ofs, je32_to_cpu(node->totlen), |
792 | 0 | c->cleanmarker_size); |
793 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node))))) |
794 | 0 | return err; |
795 | 0 | ofs += PAD(sizeof(struct jffs2_unknown_node)); |
796 | 0 | } else if (jeb->first_node) { |
797 | 0 | jffs2_dbg(1, "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", |
798 | 0 | ofs, jeb->offset); |
799 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node))))) |
800 | 0 | return err; |
801 | 0 | ofs += PAD(sizeof(struct jffs2_unknown_node)); |
802 | 0 | } else { |
803 | 0 | jffs2_link_node_ref(c, jeb, ofs | REF_NORMAL, c->cleanmarker_size, NULL); |
804 | |
|
805 | 0 | ofs += PAD(c->cleanmarker_size); |
806 | 0 | } |
807 | 0 | break; |
808 | | |
809 | 0 | case JFFS2_NODETYPE_PADDING: |
810 | 0 | if (jffs2_sum_active()) |
811 | 0 | jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen)); |
812 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen))))) |
813 | 0 | return err; |
814 | 0 | ofs += PAD(je32_to_cpu(node->totlen)); |
815 | 0 | break; |
816 | | |
817 | 0 | default: |
818 | 0 | switch (je16_to_cpu(node->nodetype) & JFFS2_COMPAT_MASK) { |
819 | 0 | case JFFS2_FEATURE_ROCOMPAT: |
820 | 0 | pr_notice("Read-only compatible feature node (0x%04x) found at offset 0x%08x\n", |
821 | 0 | je16_to_cpu(node->nodetype), ofs); |
822 | 0 | c->flags |= JFFS2_SB_FLAG_RO; |
823 | 0 | if (!(jffs2_is_readonly(c))) |
824 | 0 | return -EROFS; |
825 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen))))) |
826 | 0 | return err; |
827 | 0 | ofs += PAD(je32_to_cpu(node->totlen)); |
828 | 0 | break; |
829 | | |
830 | 0 | case JFFS2_FEATURE_INCOMPAT: |
831 | 0 | pr_notice("Incompatible feature node (0x%04x) found at offset 0x%08x\n", |
832 | 0 | je16_to_cpu(node->nodetype), ofs); |
833 | 0 | return -EINVAL; |
834 | | |
835 | 0 | case JFFS2_FEATURE_RWCOMPAT_DELETE: |
836 | 0 | jffs2_dbg(1, "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", |
837 | 0 | je16_to_cpu(node->nodetype), ofs); |
838 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen))))) |
839 | 0 | return err; |
840 | 0 | ofs += PAD(je32_to_cpu(node->totlen)); |
841 | 0 | break; |
842 | | |
843 | 0 | case JFFS2_FEATURE_RWCOMPAT_COPY: { |
844 | 0 | jffs2_dbg(1, "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", |
845 | 0 | je16_to_cpu(node->nodetype), ofs); |
846 | |
|
847 | 0 | jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(node->totlen)), NULL); |
848 | | |
849 | | /* We can't summarise nodes we don't grok */ |
850 | 0 | jffs2_sum_disable_collecting(s); |
851 | 0 | ofs += PAD(je32_to_cpu(node->totlen)); |
852 | 0 | break; |
853 | 0 | } |
854 | 0 | } |
855 | 0 | } |
856 | 0 | } |
857 | | |
858 | 0 | if (jffs2_sum_active()) { |
859 | 0 | if (PAD(s->sum_size + JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size) { |
860 | 0 | dbg_summary("There is not enough space for " |
861 | 0 | "summary information, disabling for this jeb!\n"); |
862 | 0 | jffs2_sum_disable_collecting(s); |
863 | 0 | } |
864 | 0 | } |
865 | |
|
866 | 0 | jffs2_dbg(1, "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n", |
867 | 0 | jeb->offset, jeb->free_size, jeb->dirty_size, |
868 | 0 | jeb->unchecked_size, jeb->used_size, jeb->wasted_size); |
869 | | |
870 | | /* mark_node_obsolete can add to wasted !! */ |
871 | 0 | if (jeb->wasted_size) { |
872 | 0 | jeb->dirty_size += jeb->wasted_size; |
873 | 0 | c->dirty_size += jeb->wasted_size; |
874 | 0 | c->wasted_size -= jeb->wasted_size; |
875 | 0 | jeb->wasted_size = 0; |
876 | 0 | } |
877 | |
|
878 | 0 | return jffs2_scan_classify_jeb(c, jeb); |
879 | 0 | } |
880 | | |
881 | | struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino) |
882 | 0 | { |
883 | 0 | struct jffs2_inode_cache *ic; |
884 | |
|
885 | 0 | ic = jffs2_get_ino_cache(c, ino); |
886 | 0 | if (ic) |
887 | 0 | return ic; |
888 | | |
889 | 0 | if (ino > c->highest_ino) |
890 | 0 | c->highest_ino = ino; |
891 | |
|
892 | 0 | ic = jffs2_alloc_inode_cache(); |
893 | 0 | if (!ic) { |
894 | 0 | pr_notice("%s(): allocation of inode cache failed\n", __func__); |
895 | 0 | return NULL; |
896 | 0 | } |
897 | 0 | memset(ic, 0, sizeof(*ic)); |
898 | |
|
899 | 0 | ic->ino = ino; |
900 | 0 | ic->nodes = (void *)ic; |
901 | 0 | jffs2_add_ino_cache(c, ic); |
902 | 0 | if (ino == 1) |
903 | 0 | ic->pino_nlink = 1; |
904 | 0 | return ic; |
905 | 0 | } |
906 | | |
907 | | static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
908 | | struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s) |
909 | 0 | { |
910 | 0 | struct jffs2_inode_cache *ic; |
911 | 0 | uint32_t crc, ino = je32_to_cpu(ri->ino); |
912 | |
|
913 | 0 | jffs2_dbg(1, "%s(): Node at 0x%08x\n", __func__, ofs); |
914 | | |
915 | | /* We do very little here now. Just check the ino# to which we should attribute |
916 | | this node; we can do all the CRC checking etc. later. There's a tradeoff here -- |
917 | | we used to scan the flash once only, reading everything we want from it into |
918 | | memory, then building all our in-core data structures and freeing the extra |
919 | | information. Now we allow the first part of the mount to complete a lot quicker, |
920 | | but we have to go _back_ to the flash in order to finish the CRC checking, etc. |
921 | | Which means that the _full_ amount of time to get to proper write mode with GC |
922 | | operational may actually be _longer_ than before. Sucks to be me. */ |
923 | | |
924 | | /* Check the node CRC in any case. */ |
925 | 0 | crc = crc32(0, ri, sizeof(*ri)-8); |
926 | 0 | if (crc != je32_to_cpu(ri->node_crc)) { |
927 | 0 | pr_notice("%s(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n", |
928 | 0 | __func__, ofs, je32_to_cpu(ri->node_crc), crc); |
929 | | /* |
930 | | * We believe totlen because the CRC on the node |
931 | | * _header_ was OK, just the node itself failed. |
932 | | */ |
933 | 0 | return jffs2_scan_dirty_space(c, jeb, |
934 | 0 | PAD(je32_to_cpu(ri->totlen))); |
935 | 0 | } |
936 | | |
937 | 0 | ic = jffs2_get_ino_cache(c, ino); |
938 | 0 | if (!ic) { |
939 | 0 | ic = jffs2_scan_make_ino_cache(c, ino); |
940 | 0 | if (!ic) |
941 | 0 | return -ENOMEM; |
942 | 0 | } |
943 | | |
944 | | /* Wheee. It worked */ |
945 | 0 | jffs2_link_node_ref(c, jeb, ofs | REF_UNCHECKED, PAD(je32_to_cpu(ri->totlen)), ic); |
946 | |
|
947 | 0 | jffs2_dbg(1, "Node is ino #%u, version %d. Range 0x%x-0x%x\n", |
948 | 0 | je32_to_cpu(ri->ino), je32_to_cpu(ri->version), |
949 | 0 | je32_to_cpu(ri->offset), |
950 | 0 | je32_to_cpu(ri->offset)+je32_to_cpu(ri->dsize)); |
951 | |
|
952 | 0 | pseudo_random += je32_to_cpu(ri->version); |
953 | |
|
954 | 0 | if (jffs2_sum_active()) { |
955 | 0 | jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset); |
956 | 0 | } |
957 | |
|
958 | 0 | return 0; |
959 | 0 | } |
960 | | |
961 | | static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, |
962 | | struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s) |
963 | 0 | { |
964 | 0 | struct jffs2_full_dirent *fd; |
965 | 0 | struct jffs2_inode_cache *ic; |
966 | 0 | uint32_t checkedlen; |
967 | 0 | uint32_t crc; |
968 | 0 | int err; |
969 | |
|
970 | 0 | jffs2_dbg(1, "%s(): Node at 0x%08x\n", __func__, ofs); |
971 | | |
972 | | /* We don't get here unless the node is still valid, so we don't have to |
973 | | mask in the ACCURATE bit any more. */ |
974 | 0 | crc = crc32(0, rd, sizeof(*rd)-8); |
975 | |
|
976 | 0 | if (crc != je32_to_cpu(rd->node_crc)) { |
977 | 0 | pr_notice("%s(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n", |
978 | 0 | __func__, ofs, je32_to_cpu(rd->node_crc), crc); |
979 | | /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */ |
980 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen))))) |
981 | 0 | return err; |
982 | 0 | return 0; |
983 | 0 | } |
984 | | |
985 | 0 | pseudo_random += je32_to_cpu(rd->version); |
986 | | |
987 | | /* Should never happen. Did. (OLPC trac #4184)*/ |
988 | 0 | checkedlen = strnlen(rd->name, rd->nsize); |
989 | 0 | if (checkedlen < rd->nsize) { |
990 | 0 | pr_err("Dirent at %08x has zeroes in name. Truncating to %d chars\n", |
991 | 0 | ofs, checkedlen); |
992 | 0 | } |
993 | 0 | fd = jffs2_alloc_full_dirent(checkedlen+1); |
994 | 0 | if (!fd) { |
995 | 0 | return -ENOMEM; |
996 | 0 | } |
997 | 0 | memcpy(&fd->name, rd->name, checkedlen); |
998 | 0 | fd->name[checkedlen] = 0; |
999 | |
|
1000 | 0 | crc = crc32(0, fd->name, rd->nsize); |
1001 | 0 | if (crc != je32_to_cpu(rd->name_crc)) { |
1002 | 0 | pr_notice("%s(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n", |
1003 | 0 | __func__, ofs, je32_to_cpu(rd->name_crc), crc); |
1004 | 0 | jffs2_dbg(1, "Name for which CRC failed is (now) '%s', ino #%d\n", |
1005 | 0 | fd->name, je32_to_cpu(rd->ino)); |
1006 | 0 | jffs2_free_full_dirent(fd); |
1007 | | /* FIXME: Why do we believe totlen? */ |
1008 | | /* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */ |
1009 | 0 | if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen))))) |
1010 | 0 | return err; |
1011 | 0 | return 0; |
1012 | 0 | } |
1013 | 0 | ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino)); |
1014 | 0 | if (!ic) { |
1015 | 0 | jffs2_free_full_dirent(fd); |
1016 | 0 | return -ENOMEM; |
1017 | 0 | } |
1018 | | |
1019 | 0 | fd->raw = jffs2_link_node_ref(c, jeb, ofs | dirent_node_state(rd), |
1020 | 0 | PAD(je32_to_cpu(rd->totlen)), ic); |
1021 | |
|
1022 | 0 | fd->next = NULL; |
1023 | 0 | fd->version = je32_to_cpu(rd->version); |
1024 | 0 | fd->ino = je32_to_cpu(rd->ino); |
1025 | 0 | fd->nhash = full_name_hash(NULL, fd->name, checkedlen); |
1026 | 0 | fd->type = rd->type; |
1027 | 0 | jffs2_add_fd_to_list(c, fd, &ic->scan_dents); |
1028 | |
|
1029 | 0 | if (jffs2_sum_active()) { |
1030 | 0 | jffs2_sum_add_dirent_mem(s, rd, ofs - jeb->offset); |
1031 | 0 | } |
1032 | |
|
1033 | 0 | return 0; |
1034 | 0 | } |
1035 | | |
1036 | | static int count_list(struct list_head *l) |
1037 | 0 | { |
1038 | 0 | uint32_t count = 0; |
1039 | 0 | struct list_head *tmp; |
1040 | |
|
1041 | 0 | list_for_each(tmp, l) { |
1042 | 0 | count++; |
1043 | 0 | } |
1044 | 0 | return count; |
1045 | 0 | } |
1046 | | |
1047 | | /* Note: This breaks if list_empty(head). I don't care. You |
1048 | | might, if you copy this code and use it elsewhere :) */ |
1049 | | static void rotate_list(struct list_head *head, uint32_t count) |
1050 | 0 | { |
1051 | 0 | struct list_head *n = head->next; |
1052 | |
|
1053 | 0 | list_del(head); |
1054 | 0 | while(count--) { |
1055 | 0 | n = n->next; |
1056 | 0 | } |
1057 | 0 | list_add(head, n); |
1058 | 0 | } |
1059 | | |
1060 | | void jffs2_rotate_lists(struct jffs2_sb_info *c) |
1061 | 0 | { |
1062 | 0 | uint32_t x; |
1063 | 0 | uint32_t rotateby; |
1064 | |
|
1065 | 0 | x = count_list(&c->clean_list); |
1066 | 0 | if (x) { |
1067 | 0 | rotateby = pseudo_random % x; |
1068 | 0 | rotate_list((&c->clean_list), rotateby); |
1069 | 0 | } |
1070 | |
|
1071 | 0 | x = count_list(&c->very_dirty_list); |
1072 | 0 | if (x) { |
1073 | 0 | rotateby = pseudo_random % x; |
1074 | 0 | rotate_list((&c->very_dirty_list), rotateby); |
1075 | 0 | } |
1076 | |
|
1077 | 0 | x = count_list(&c->dirty_list); |
1078 | 0 | if (x) { |
1079 | 0 | rotateby = pseudo_random % x; |
1080 | 0 | rotate_list((&c->dirty_list), rotateby); |
1081 | 0 | } |
1082 | |
|
1083 | 0 | x = count_list(&c->erasable_list); |
1084 | 0 | if (x) { |
1085 | 0 | rotateby = pseudo_random % x; |
1086 | 0 | rotate_list((&c->erasable_list), rotateby); |
1087 | 0 | } |
1088 | |
|
1089 | 0 | if (c->nr_erasing_blocks) { |
1090 | 0 | rotateby = pseudo_random % c->nr_erasing_blocks; |
1091 | 0 | rotate_list((&c->erase_pending_list), rotateby); |
1092 | 0 | } |
1093 | |
|
1094 | 0 | if (c->nr_free_blocks) { |
1095 | 0 | rotateby = pseudo_random % c->nr_free_blocks; |
1096 | 0 | rotate_list((&c->free_list), rotateby); |
1097 | 0 | } |
1098 | 0 | } |