]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * JFFS2 -- Journalling Flash File System, Version 2. | |
3 | * | |
4 | * Copyright (C) 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>, | |
5 | * Zoltan Sogor <weth@inf.u-szeged.hu>, | |
6 | * Patrik Kluba <pajko@halom.u-szeged.hu>, | |
7 | * University of Szeged, Hungary | |
8 | * 2005 KaiGai Kohei <kaigai@ak.jp.nec.com> | |
9 | * | |
10 | * For licensing information, see the file 'LICENCE' in this directory. | |
11 | * | |
12 | * $Id: summary.c,v 1.4 2005/09/26 11:37:21 havasi Exp $ | |
13 | * | |
14 | */ | |
15 | ||
16 | #include <linux/kernel.h> | |
17 | #include <linux/sched.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/mtd/mtd.h> | |
20 | #include <linux/pagemap.h> | |
21 | #include <linux/crc32.h> | |
22 | #include <linux/compiler.h> | |
23 | #include <linux/vmalloc.h> | |
24 | #include "nodelist.h" | |
25 | #include "debug.h" | |
26 | ||
27 | int jffs2_sum_init(struct jffs2_sb_info *c) | |
28 | { | |
29 | c->summary = kmalloc(sizeof(struct jffs2_summary), GFP_KERNEL); | |
30 | ||
31 | if (!c->summary) { | |
32 | JFFS2_WARNING("Can't allocate memory for summary information!\n"); | |
33 | return -ENOMEM; | |
34 | } | |
35 | ||
36 | memset(c->summary, 0, sizeof(struct jffs2_summary)); | |
37 | ||
38 | c->summary->sum_buf = vmalloc(c->sector_size); | |
39 | ||
40 | if (!c->summary->sum_buf) { | |
41 | JFFS2_WARNING("Can't allocate buffer for writing out summary information!\n"); | |
42 | kfree(c->summary); | |
43 | return -ENOMEM; | |
44 | } | |
45 | ||
46 | dbg_summary("returned succesfully\n"); | |
47 | ||
48 | return 0; | |
49 | } | |
50 | ||
51 | void jffs2_sum_exit(struct jffs2_sb_info *c) | |
52 | { | |
53 | dbg_summary("called\n"); | |
54 | ||
55 | jffs2_sum_disable_collecting(c->summary); | |
56 | ||
57 | vfree(c->summary->sum_buf); | |
58 | c->summary->sum_buf = NULL; | |
59 | ||
60 | kfree(c->summary); | |
61 | c->summary = NULL; | |
62 | } | |
63 | ||
64 | static int jffs2_sum_add_mem(struct jffs2_summary *s, union jffs2_sum_mem *item) | |
65 | { | |
66 | if (!s->sum_list_head) | |
67 | s->sum_list_head = (union jffs2_sum_mem *) item; | |
68 | if (s->sum_list_tail) | |
69 | s->sum_list_tail->u.next = (union jffs2_sum_mem *) item; | |
70 | s->sum_list_tail = (union jffs2_sum_mem *) item; | |
71 | ||
72 | switch (je16_to_cpu(item->u.nodetype)) { | |
73 | case JFFS2_NODETYPE_INODE: | |
74 | s->sum_size += JFFS2_SUMMARY_INODE_SIZE; | |
75 | s->sum_num++; | |
76 | dbg_summary("inode (%u) added to summary\n", | |
77 | je32_to_cpu(item->i.inode)); | |
78 | break; | |
79 | case JFFS2_NODETYPE_DIRENT: | |
80 | s->sum_size += JFFS2_SUMMARY_DIRENT_SIZE(item->d.nsize); | |
81 | s->sum_num++; | |
82 | dbg_summary("dirent (%u) added to summary\n", | |
83 | je32_to_cpu(item->d.ino)); | |
84 | break; | |
85 | #ifdef CONFIG_JFFS2_FS_XATTR | |
86 | case JFFS2_NODETYPE_XATTR: | |
87 | s->sum_size += JFFS2_SUMMARY_XATTR_SIZE; | |
88 | s->sum_num++; | |
89 | dbg_summary("xattr (xid=%u, version=%u) added to summary\n", | |
90 | je32_to_cpu(item->x.xid), je32_to_cpu(item->x.version)); | |
91 | break; | |
92 | case JFFS2_NODETYPE_XREF: | |
93 | s->sum_size += JFFS2_SUMMARY_XREF_SIZE; | |
94 | s->sum_num++; | |
95 | dbg_summary("xref added to summary\n"); | |
96 | break; | |
97 | #endif | |
98 | default: | |
99 | JFFS2_WARNING("UNKNOWN node type %u\n", | |
100 | je16_to_cpu(item->u.nodetype)); | |
101 | return 1; | |
102 | } | |
103 | return 0; | |
104 | } | |
105 | ||
106 | ||
107 | /* The following 3 functions are called from scan.c to collect summary info for not closed jeb */ | |
108 | ||
109 | int jffs2_sum_add_padding_mem(struct jffs2_summary *s, uint32_t size) | |
110 | { | |
111 | dbg_summary("called with %u\n", size); | |
112 | s->sum_padded += size; | |
113 | return 0; | |
114 | } | |
115 | ||
116 | int jffs2_sum_add_inode_mem(struct jffs2_summary *s, struct jffs2_raw_inode *ri, | |
117 | uint32_t ofs) | |
118 | { | |
119 | struct jffs2_sum_inode_mem *temp = kmalloc(sizeof(struct jffs2_sum_inode_mem), GFP_KERNEL); | |
120 | ||
121 | if (!temp) | |
122 | return -ENOMEM; | |
123 | ||
124 | temp->nodetype = ri->nodetype; | |
125 | temp->inode = ri->ino; | |
126 | temp->version = ri->version; | |
127 | temp->offset = cpu_to_je32(ofs); /* relative offset from the begining of the jeb */ | |
128 | temp->totlen = ri->totlen; | |
129 | temp->next = NULL; | |
130 | ||
131 | return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp); | |
132 | } | |
133 | ||
134 | int jffs2_sum_add_dirent_mem(struct jffs2_summary *s, struct jffs2_raw_dirent *rd, | |
135 | uint32_t ofs) | |
136 | { | |
137 | struct jffs2_sum_dirent_mem *temp = | |
138 | kmalloc(sizeof(struct jffs2_sum_dirent_mem) + rd->nsize, GFP_KERNEL); | |
139 | ||
140 | if (!temp) | |
141 | return -ENOMEM; | |
142 | ||
143 | temp->nodetype = rd->nodetype; | |
144 | temp->totlen = rd->totlen; | |
145 | temp->offset = cpu_to_je32(ofs); /* relative from the begining of the jeb */ | |
146 | temp->pino = rd->pino; | |
147 | temp->version = rd->version; | |
148 | temp->ino = rd->ino; | |
149 | temp->nsize = rd->nsize; | |
150 | temp->type = rd->type; | |
151 | temp->next = NULL; | |
152 | ||
153 | memcpy(temp->name, rd->name, rd->nsize); | |
154 | ||
155 | return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp); | |
156 | } | |
157 | ||
158 | #ifdef CONFIG_JFFS2_FS_XATTR | |
159 | int jffs2_sum_add_xattr_mem(struct jffs2_summary *s, struct jffs2_raw_xattr *rx, uint32_t ofs) | |
160 | { | |
161 | struct jffs2_sum_xattr_mem *temp; | |
162 | ||
163 | temp = kmalloc(sizeof(struct jffs2_sum_xattr_mem), GFP_KERNEL); | |
164 | if (!temp) | |
165 | return -ENOMEM; | |
166 | ||
167 | temp->nodetype = rx->nodetype; | |
168 | temp->xid = rx->xid; | |
169 | temp->version = rx->version; | |
170 | temp->offset = cpu_to_je32(ofs); | |
171 | temp->totlen = rx->totlen; | |
172 | temp->next = NULL; | |
173 | ||
174 | return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp); | |
175 | } | |
176 | ||
177 | int jffs2_sum_add_xref_mem(struct jffs2_summary *s, struct jffs2_raw_xref *rr, uint32_t ofs) | |
178 | { | |
179 | struct jffs2_sum_xref_mem *temp; | |
180 | ||
181 | temp = kmalloc(sizeof(struct jffs2_sum_xref_mem), GFP_KERNEL); | |
182 | if (!temp) | |
183 | return -ENOMEM; | |
184 | ||
185 | temp->nodetype = rr->nodetype; | |
186 | temp->offset = cpu_to_je32(ofs); | |
187 | temp->next = NULL; | |
188 | ||
189 | return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp); | |
190 | } | |
191 | #endif | |
192 | /* Cleanup every collected summary information */ | |
193 | ||
194 | static void jffs2_sum_clean_collected(struct jffs2_summary *s) | |
195 | { | |
196 | union jffs2_sum_mem *temp; | |
197 | ||
198 | if (!s->sum_list_head) { | |
199 | dbg_summary("already empty\n"); | |
200 | } | |
201 | while (s->sum_list_head) { | |
202 | temp = s->sum_list_head; | |
203 | s->sum_list_head = s->sum_list_head->u.next; | |
204 | kfree(temp); | |
205 | } | |
206 | s->sum_list_tail = NULL; | |
207 | s->sum_padded = 0; | |
208 | s->sum_num = 0; | |
209 | } | |
210 | ||
211 | void jffs2_sum_reset_collected(struct jffs2_summary *s) | |
212 | { | |
213 | dbg_summary("called\n"); | |
214 | jffs2_sum_clean_collected(s); | |
215 | s->sum_size = 0; | |
216 | } | |
217 | ||
218 | void jffs2_sum_disable_collecting(struct jffs2_summary *s) | |
219 | { | |
220 | dbg_summary("called\n"); | |
221 | jffs2_sum_clean_collected(s); | |
222 | s->sum_size = JFFS2_SUMMARY_NOSUM_SIZE; | |
223 | } | |
224 | ||
225 | int jffs2_sum_is_disabled(struct jffs2_summary *s) | |
226 | { | |
227 | return (s->sum_size == JFFS2_SUMMARY_NOSUM_SIZE); | |
228 | } | |
229 | ||
230 | /* Move the collected summary information into sb (called from scan.c) */ | |
231 | ||
232 | void jffs2_sum_move_collected(struct jffs2_sb_info *c, struct jffs2_summary *s) | |
233 | { | |
234 | dbg_summary("oldsize=0x%x oldnum=%u => newsize=0x%x newnum=%u\n", | |
235 | c->summary->sum_size, c->summary->sum_num, | |
236 | s->sum_size, s->sum_num); | |
237 | ||
238 | c->summary->sum_size = s->sum_size; | |
239 | c->summary->sum_num = s->sum_num; | |
240 | c->summary->sum_padded = s->sum_padded; | |
241 | c->summary->sum_list_head = s->sum_list_head; | |
242 | c->summary->sum_list_tail = s->sum_list_tail; | |
243 | ||
244 | s->sum_list_head = s->sum_list_tail = NULL; | |
245 | } | |
246 | ||
247 | /* Called from wbuf.c to collect writed node info */ | |
248 | ||
249 | int jffs2_sum_add_kvec(struct jffs2_sb_info *c, const struct kvec *invecs, | |
250 | unsigned long count, uint32_t ofs) | |
251 | { | |
252 | union jffs2_node_union *node; | |
253 | struct jffs2_eraseblock *jeb; | |
254 | ||
255 | node = invecs[0].iov_base; | |
256 | jeb = &c->blocks[ofs / c->sector_size]; | |
257 | ofs -= jeb->offset; | |
258 | ||
259 | switch (je16_to_cpu(node->u.nodetype)) { | |
260 | case JFFS2_NODETYPE_INODE: { | |
261 | struct jffs2_sum_inode_mem *temp = | |
262 | kmalloc(sizeof(struct jffs2_sum_inode_mem), GFP_KERNEL); | |
263 | ||
264 | if (!temp) | |
265 | goto no_mem; | |
266 | ||
267 | temp->nodetype = node->i.nodetype; | |
268 | temp->inode = node->i.ino; | |
269 | temp->version = node->i.version; | |
270 | temp->offset = cpu_to_je32(ofs); | |
271 | temp->totlen = node->i.totlen; | |
272 | temp->next = NULL; | |
273 | ||
274 | return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp); | |
275 | } | |
276 | ||
277 | case JFFS2_NODETYPE_DIRENT: { | |
278 | struct jffs2_sum_dirent_mem *temp = | |
279 | kmalloc(sizeof(struct jffs2_sum_dirent_mem) + node->d.nsize, GFP_KERNEL); | |
280 | ||
281 | if (!temp) | |
282 | goto no_mem; | |
283 | ||
284 | temp->nodetype = node->d.nodetype; | |
285 | temp->totlen = node->d.totlen; | |
286 | temp->offset = cpu_to_je32(ofs); | |
287 | temp->pino = node->d.pino; | |
288 | temp->version = node->d.version; | |
289 | temp->ino = node->d.ino; | |
290 | temp->nsize = node->d.nsize; | |
291 | temp->type = node->d.type; | |
292 | temp->next = NULL; | |
293 | ||
294 | switch (count) { | |
295 | case 1: | |
296 | memcpy(temp->name,node->d.name,node->d.nsize); | |
297 | break; | |
298 | ||
299 | case 2: | |
300 | memcpy(temp->name,invecs[1].iov_base,node->d.nsize); | |
301 | break; | |
302 | ||
303 | default: | |
304 | BUG(); /* impossible count value */ | |
305 | break; | |
306 | } | |
307 | ||
308 | return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp); | |
309 | } | |
310 | #ifdef CONFIG_JFFS2_FS_XATTR | |
311 | case JFFS2_NODETYPE_XATTR: { | |
312 | struct jffs2_sum_xattr_mem *temp; | |
313 | if (je32_to_cpu(node->x.version) == 0xffffffff) | |
314 | return 0; | |
315 | temp = kmalloc(sizeof(struct jffs2_sum_xattr_mem), GFP_KERNEL); | |
316 | if (!temp) | |
317 | goto no_mem; | |
318 | ||
319 | temp->nodetype = node->x.nodetype; | |
320 | temp->xid = node->x.xid; | |
321 | temp->version = node->x.version; | |
322 | temp->totlen = node->x.totlen; | |
323 | temp->offset = cpu_to_je32(ofs); | |
324 | temp->next = NULL; | |
325 | ||
326 | return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp); | |
327 | } | |
328 | case JFFS2_NODETYPE_XREF: { | |
329 | struct jffs2_sum_xref_mem *temp; | |
330 | ||
331 | if (je32_to_cpu(node->r.ino) == 0xffffffff | |
332 | && je32_to_cpu(node->r.xid) == 0xffffffff) | |
333 | return 0; | |
334 | temp = kmalloc(sizeof(struct jffs2_sum_xref_mem), GFP_KERNEL); | |
335 | if (!temp) | |
336 | goto no_mem; | |
337 | temp->nodetype = node->r.nodetype; | |
338 | temp->offset = cpu_to_je32(ofs); | |
339 | temp->next = NULL; | |
340 | ||
341 | return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp); | |
342 | } | |
343 | #endif | |
344 | case JFFS2_NODETYPE_PADDING: | |
345 | dbg_summary("node PADDING\n"); | |
346 | c->summary->sum_padded += je32_to_cpu(node->u.totlen); | |
347 | break; | |
348 | ||
349 | case JFFS2_NODETYPE_CLEANMARKER: | |
350 | dbg_summary("node CLEANMARKER\n"); | |
351 | break; | |
352 | ||
353 | case JFFS2_NODETYPE_SUMMARY: | |
354 | dbg_summary("node SUMMARY\n"); | |
355 | break; | |
356 | ||
357 | default: | |
358 | /* If you implement a new node type you should also implement | |
359 | summary support for it or disable summary. | |
360 | */ | |
361 | BUG(); | |
362 | break; | |
363 | } | |
364 | ||
365 | return 0; | |
366 | ||
367 | no_mem: | |
368 | JFFS2_WARNING("MEMORY ALLOCATION ERROR!"); | |
369 | return -ENOMEM; | |
370 | } | |
371 | ||
372 | static struct jffs2_raw_node_ref *sum_link_node_ref(struct jffs2_sb_info *c, | |
373 | struct jffs2_eraseblock *jeb, | |
374 | uint32_t ofs, uint32_t len, | |
375 | struct jffs2_inode_cache *ic) | |
376 | { | |
377 | /* If there was a gap, mark it dirty */ | |
378 | if ((ofs & ~3) > c->sector_size - jeb->free_size) { | |
379 | /* Ew. Summary doesn't actually tell us explicitly about dirty space */ | |
380 | jffs2_scan_dirty_space(c, jeb, (ofs & ~3) - (c->sector_size - jeb->free_size)); | |
381 | } | |
382 | ||
383 | return jffs2_link_node_ref(c, jeb, jeb->offset + ofs, len, ic); | |
384 | } | |
385 | ||
386 | /* Process the stored summary information - helper function for jffs2_sum_scan_sumnode() */ | |
387 | ||
388 | static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, | |
389 | struct jffs2_raw_summary *summary, uint32_t *pseudo_random) | |
390 | { | |
391 | struct jffs2_inode_cache *ic; | |
392 | struct jffs2_full_dirent *fd; | |
393 | void *sp; | |
394 | int i, ino; | |
395 | int err; | |
396 | ||
397 | sp = summary->sum; | |
398 | ||
399 | for (i=0; i<je32_to_cpu(summary->sum_num); i++) { | |
400 | dbg_summary("processing summary index %d\n", i); | |
401 | ||
402 | /* Make sure there's a spare ref for dirty space */ | |
403 | err = jffs2_prealloc_raw_node_refs(c, 2); | |
404 | if (err) | |
405 | return err; | |
406 | ||
407 | switch (je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype)) { | |
408 | case JFFS2_NODETYPE_INODE: { | |
409 | struct jffs2_sum_inode_flash *spi; | |
410 | spi = sp; | |
411 | ||
412 | ino = je32_to_cpu(spi->inode); | |
413 | ||
414 | dbg_summary("Inode at 0x%08x-0x%08x\n", | |
415 | jeb->offset + je32_to_cpu(spi->offset), | |
416 | jeb->offset + je32_to_cpu(spi->offset) + je32_to_cpu(spu->totlen)); | |
417 | ||
418 | ic = jffs2_scan_make_ino_cache(c, ino); | |
419 | if (!ic) { | |
420 | JFFS2_NOTICE("scan_make_ino_cache failed\n"); | |
421 | return -ENOMEM; | |
422 | } | |
423 | ||
424 | sum_link_node_ref(c, jeb, je32_to_cpu(spi->offset) | REF_UNCHECKED, | |
425 | PAD(je32_to_cpu(spi->totlen)), ic); | |
426 | ||
427 | *pseudo_random += je32_to_cpu(spi->version); | |
428 | ||
429 | sp += JFFS2_SUMMARY_INODE_SIZE; | |
430 | ||
431 | break; | |
432 | } | |
433 | ||
434 | case JFFS2_NODETYPE_DIRENT: { | |
435 | struct jffs2_sum_dirent_flash *spd; | |
436 | spd = sp; | |
437 | ||
438 | dbg_summary("Dirent at 0x%08x\n", | |
439 | jeb->offset + je32_to_cpu(spd->offset), | |
440 | jeb->offset + je32_to_cpu(spd->offset) + je32_to_cpu(spd->totlen)); | |
441 | ||
442 | ||
443 | fd = jffs2_alloc_full_dirent(spd->nsize+1); | |
444 | if (!fd) | |
445 | return -ENOMEM; | |
446 | ||
447 | memcpy(&fd->name, spd->name, spd->nsize); | |
448 | fd->name[spd->nsize] = 0; | |
449 | ||
450 | ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(spd->pino)); | |
451 | if (!ic) { | |
452 | jffs2_free_full_dirent(fd); | |
453 | return -ENOMEM; | |
454 | } | |
455 | ||
456 | fd->raw = sum_link_node_ref(c, jeb, je32_to_cpu(spd->offset) | REF_PRISTINE, | |
457 | PAD(je32_to_cpu(spd->totlen)), ic); | |
458 | ||
459 | fd->next = NULL; | |
460 | fd->version = je32_to_cpu(spd->version); | |
461 | fd->ino = je32_to_cpu(spd->ino); | |
462 | fd->nhash = full_name_hash(fd->name, spd->nsize); | |
463 | fd->type = spd->type; | |
464 | ||
465 | jffs2_add_fd_to_list(c, fd, &ic->scan_dents); | |
466 | ||
467 | *pseudo_random += je32_to_cpu(spd->version); | |
468 | ||
469 | sp += JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize); | |
470 | ||
471 | break; | |
472 | } | |
473 | #ifdef CONFIG_JFFS2_FS_XATTR | |
474 | case JFFS2_NODETYPE_XATTR: { | |
475 | struct jffs2_xattr_datum *xd; | |
476 | struct jffs2_sum_xattr_flash *spx; | |
477 | ||
478 | spx = (struct jffs2_sum_xattr_flash *)sp; | |
479 | dbg_summary("xattr at %#08x-%#08x (xid=%u, version=%u)\n", | |
480 | jeb->offset + je32_to_cpu(spx->offset), | |
481 | jeb->offset + je32_to_cpu(spx->offset) + je32_to_cpu(spx->totlen), | |
482 | je32_to_cpu(spx->xid), je32_to_cpu(spx->version)); | |
483 | ||
484 | xd = jffs2_setup_xattr_datum(c, je32_to_cpu(spx->xid), | |
485 | je32_to_cpu(spx->version)); | |
486 | if (IS_ERR(xd)) { | |
487 | if (PTR_ERR(xd) == -EEXIST) { | |
488 | /* a newer version of xd exists */ | |
489 | if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(spx->totlen)))) | |
490 | return err; | |
491 | sp += JFFS2_SUMMARY_XATTR_SIZE; | |
492 | break; | |
493 | } | |
494 | JFFS2_NOTICE("allocation of xattr_datum failed\n"); | |
495 | return PTR_ERR(xd); | |
496 | } | |
497 | ||
498 | xd->node = sum_link_node_ref(c, jeb, je32_to_cpu(spx->offset) | REF_UNCHECKED, | |
499 | PAD(je32_to_cpu(spx->totlen)), NULL); | |
500 | /* FIXME */ xd->node->next_in_ino = (void *)xd; | |
501 | ||
502 | *pseudo_random += je32_to_cpu(spx->xid); | |
503 | sp += JFFS2_SUMMARY_XATTR_SIZE; | |
504 | ||
505 | break; | |
506 | } | |
507 | case JFFS2_NODETYPE_XREF: { | |
508 | struct jffs2_xattr_ref *ref; | |
509 | struct jffs2_sum_xref_flash *spr; | |
510 | ||
511 | spr = (struct jffs2_sum_xref_flash *)sp; | |
512 | dbg_summary("xref at %#08x-%#08x\n", | |
513 | jeb->offset + je32_to_cpu(spr->offset), | |
514 | jeb->offset + je32_to_cpu(spr->offset) + PAD(sizeof(struct jffs2_raw_xref))); | |
515 | ||
516 | ref = jffs2_alloc_xattr_ref(); | |
517 | if (!ref) { | |
518 | JFFS2_NOTICE("allocation of xattr_datum failed\n"); | |
519 | return -ENOMEM; | |
520 | } | |
521 | ref->ino = 0xfffffffe; | |
522 | ref->xid = 0xfffffffd; | |
523 | ref->next = c->xref_temp; | |
524 | c->xref_temp = ref; | |
525 | ||
526 | ref->node = sum_link_node_ref(c, jeb, je32_to_cpu(spr->offset) | REF_UNCHECKED, | |
527 | PAD(sizeof(struct jffs2_raw_xref)), NULL); | |
528 | /* FIXME */ ref->node->next_in_ino = (void *)ref; | |
529 | ||
530 | *pseudo_random += ref->node->flash_offset; | |
531 | sp += JFFS2_SUMMARY_XREF_SIZE; | |
532 | ||
533 | break; | |
534 | } | |
535 | #endif | |
536 | default : { | |
537 | uint16_t nodetype = je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype); | |
538 | JFFS2_WARNING("Unsupported node type %x found in summary! Exiting...\n", nodetype); | |
539 | if ((nodetype & JFFS2_COMPAT_MASK) == JFFS2_FEATURE_INCOMPAT) | |
540 | return -EIO; | |
541 | ||
542 | /* For compatible node types, just fall back to the full scan */ | |
543 | c->wasted_size -= jeb->wasted_size; | |
544 | c->free_size += c->sector_size - jeb->free_size; | |
545 | c->used_size -= jeb->used_size; | |
546 | c->dirty_size -= jeb->dirty_size; | |
547 | jeb->wasted_size = jeb->used_size = jeb->dirty_size = 0; | |
548 | jeb->free_size = c->sector_size; | |
549 | ||
550 | jffs2_free_all_node_refs(c, jeb); | |
551 | return -ENOTRECOVERABLE; | |
552 | } | |
553 | } | |
554 | } | |
555 | return 0; | |
556 | } | |
557 | ||
558 | /* Process the summary node - called from jffs2_scan_eraseblock() */ | |
559 | int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, | |
560 | struct jffs2_raw_summary *summary, uint32_t sumsize, | |
561 | uint32_t *pseudo_random) | |
562 | { | |
563 | struct jffs2_unknown_node crcnode; | |
564 | int ret, ofs; | |
565 | uint32_t crc; | |
566 | int err; | |
567 | ||
568 | ofs = c->sector_size - sumsize; | |
569 | ||
570 | dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n", | |
571 | jeb->offset, jeb->offset + ofs, sumsize); | |
572 | ||
573 | /* OK, now check for node validity and CRC */ | |
574 | crcnode.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); | |
575 | crcnode.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY); | |
576 | crcnode.totlen = summary->totlen; | |
577 | crc = crc32(0, &crcnode, sizeof(crcnode)-4); | |
578 | ||
579 | if (je32_to_cpu(summary->hdr_crc) != crc) { | |
580 | dbg_summary("Summary node header is corrupt (bad CRC or " | |
581 | "no summary at all)\n"); | |
582 | goto crc_err; | |
583 | } | |
584 | ||
585 | if (je32_to_cpu(summary->totlen) != sumsize) { | |
586 | dbg_summary("Summary node is corrupt (wrong erasesize?)\n"); | |
587 | goto crc_err; | |
588 | } | |
589 | ||
590 | crc = crc32(0, summary, sizeof(struct jffs2_raw_summary)-8); | |
591 | ||
592 | if (je32_to_cpu(summary->node_crc) != crc) { | |
593 | dbg_summary("Summary node is corrupt (bad CRC)\n"); | |
594 | goto crc_err; | |
595 | } | |
596 | ||
597 | crc = crc32(0, summary->sum, sumsize - sizeof(struct jffs2_raw_summary)); | |
598 | ||
599 | if (je32_to_cpu(summary->sum_crc) != crc) { | |
600 | dbg_summary("Summary node data is corrupt (bad CRC)\n"); | |
601 | goto crc_err; | |
602 | } | |
603 | ||
604 | if ( je32_to_cpu(summary->cln_mkr) ) { | |
605 | ||
606 | dbg_summary("Summary : CLEANMARKER node \n"); | |
607 | ||
608 | if (je32_to_cpu(summary->cln_mkr) != c->cleanmarker_size) { | |
609 | dbg_summary("CLEANMARKER node has totlen 0x%x != normal 0x%x\n", | |
610 | je32_to_cpu(summary->cln_mkr), c->cleanmarker_size); | |
611 | if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(summary->cln_mkr))))) | |
612 | return err; | |
613 | } else if (jeb->first_node) { | |
614 | dbg_summary("CLEANMARKER node not first node in block " | |
615 | "(0x%08x)\n", jeb->offset); | |
616 | if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(summary->cln_mkr))))) | |
617 | return err; | |
618 | } else { | |
619 | jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, | |
620 | je32_to_cpu(summary->cln_mkr), NULL); | |
621 | } | |
622 | } | |
623 | ||
624 | ret = jffs2_sum_process_sum_data(c, jeb, summary, pseudo_random); | |
625 | /* -ENOTRECOVERABLE isn't a fatal error -- it means we should do a full | |
626 | scan of this eraseblock. So return zero */ | |
627 | if (ret == -ENOTRECOVERABLE) | |
628 | return 0; | |
629 | if (ret) | |
630 | return ret; /* real error */ | |
631 | ||
632 | /* for PARANOIA_CHECK */ | |
633 | ret = jffs2_prealloc_raw_node_refs(c, 1); | |
634 | if (ret) | |
635 | return ret; | |
636 | ||
637 | jffs2_link_node_ref(c, jeb, (jeb->offset + ofs) | REF_NORMAL, sumsize, NULL); | |
638 | ||
639 | if (unlikely(jeb->free_size)) { | |
640 | JFFS2_WARNING("Free size 0x%x bytes in eraseblock @0x%08x with summary?\n", | |
641 | jeb->free_size, jeb->offset); | |
642 | jeb->wasted_size += jeb->free_size; | |
643 | c->wasted_size += jeb->free_size; | |
644 | c->free_size -= jeb->free_size; | |
645 | jeb->free_size = 0; | |
646 | } | |
647 | ||
648 | return jffs2_scan_classify_jeb(c, jeb); | |
649 | ||
650 | crc_err: | |
651 | JFFS2_WARNING("Summary node crc error, skipping summary information.\n"); | |
652 | ||
653 | return 0; | |
654 | } | |
655 | ||
656 | /* Write summary data to flash - helper function for jffs2_sum_write_sumnode() */ | |
657 | ||
658 | static int jffs2_sum_write_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, | |
659 | uint32_t infosize, uint32_t datasize, int padsize) | |
660 | { | |
661 | struct jffs2_raw_summary isum; | |
662 | union jffs2_sum_mem *temp; | |
663 | struct jffs2_sum_marker *sm; | |
664 | struct kvec vecs[2]; | |
665 | uint32_t sum_ofs; | |
666 | void *wpage; | |
667 | int ret; | |
668 | size_t retlen; | |
669 | ||
670 | memset(c->summary->sum_buf, 0xff, datasize); | |
671 | memset(&isum, 0, sizeof(isum)); | |
672 | ||
673 | isum.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); | |
674 | isum.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY); | |
675 | isum.totlen = cpu_to_je32(infosize); | |
676 | isum.hdr_crc = cpu_to_je32(crc32(0, &isum, sizeof(struct jffs2_unknown_node) - 4)); | |
677 | isum.padded = cpu_to_je32(c->summary->sum_padded); | |
678 | isum.cln_mkr = cpu_to_je32(c->cleanmarker_size); | |
679 | isum.sum_num = cpu_to_je32(c->summary->sum_num); | |
680 | wpage = c->summary->sum_buf; | |
681 | ||
682 | while (c->summary->sum_num) { | |
683 | temp = c->summary->sum_list_head; | |
684 | ||
685 | switch (je16_to_cpu(temp->u.nodetype)) { | |
686 | case JFFS2_NODETYPE_INODE: { | |
687 | struct jffs2_sum_inode_flash *sino_ptr = wpage; | |
688 | ||
689 | sino_ptr->nodetype = temp->i.nodetype; | |
690 | sino_ptr->inode = temp->i.inode; | |
691 | sino_ptr->version = temp->i.version; | |
692 | sino_ptr->offset = temp->i.offset; | |
693 | sino_ptr->totlen = temp->i.totlen; | |
694 | ||
695 | wpage += JFFS2_SUMMARY_INODE_SIZE; | |
696 | ||
697 | break; | |
698 | } | |
699 | ||
700 | case JFFS2_NODETYPE_DIRENT: { | |
701 | struct jffs2_sum_dirent_flash *sdrnt_ptr = wpage; | |
702 | ||
703 | sdrnt_ptr->nodetype = temp->d.nodetype; | |
704 | sdrnt_ptr->totlen = temp->d.totlen; | |
705 | sdrnt_ptr->offset = temp->d.offset; | |
706 | sdrnt_ptr->pino = temp->d.pino; | |
707 | sdrnt_ptr->version = temp->d.version; | |
708 | sdrnt_ptr->ino = temp->d.ino; | |
709 | sdrnt_ptr->nsize = temp->d.nsize; | |
710 | sdrnt_ptr->type = temp->d.type; | |
711 | ||
712 | memcpy(sdrnt_ptr->name, temp->d.name, | |
713 | temp->d.nsize); | |
714 | ||
715 | wpage += JFFS2_SUMMARY_DIRENT_SIZE(temp->d.nsize); | |
716 | ||
717 | break; | |
718 | } | |
719 | #ifdef CONFIG_JFFS2_FS_XATTR | |
720 | case JFFS2_NODETYPE_XATTR: { | |
721 | struct jffs2_sum_xattr_flash *sxattr_ptr = wpage; | |
722 | ||
723 | temp = c->summary->sum_list_head; | |
724 | sxattr_ptr->nodetype = temp->x.nodetype; | |
725 | sxattr_ptr->xid = temp->x.xid; | |
726 | sxattr_ptr->version = temp->x.version; | |
727 | sxattr_ptr->offset = temp->x.offset; | |
728 | sxattr_ptr->totlen = temp->x.totlen; | |
729 | ||
730 | wpage += JFFS2_SUMMARY_XATTR_SIZE; | |
731 | break; | |
732 | } | |
733 | case JFFS2_NODETYPE_XREF: { | |
734 | struct jffs2_sum_xref_flash *sxref_ptr = wpage; | |
735 | ||
736 | temp = c->summary->sum_list_head; | |
737 | sxref_ptr->nodetype = temp->r.nodetype; | |
738 | sxref_ptr->offset = temp->r.offset; | |
739 | ||
740 | wpage += JFFS2_SUMMARY_XREF_SIZE; | |
741 | break; | |
742 | } | |
743 | #endif | |
744 | default : { | |
745 | if ((je16_to_cpu(temp->u.nodetype) & JFFS2_COMPAT_MASK) | |
746 | == JFFS2_FEATURE_RWCOMPAT_COPY) { | |
747 | dbg_summary("Writing unknown RWCOMPAT_COPY node type %x\n", | |
748 | je16_to_cpu(temp->u.nodetype)); | |
749 | jffs2_sum_disable_collecting(c->summary); | |
750 | } else { | |
751 | BUG(); /* unknown node in summary information */ | |
752 | } | |
753 | } | |
754 | } | |
755 | ||
756 | c->summary->sum_list_head = temp->u.next; | |
757 | kfree(temp); | |
758 | ||
759 | c->summary->sum_num--; | |
760 | } | |
761 | ||
762 | jffs2_sum_reset_collected(c->summary); | |
763 | ||
764 | wpage += padsize; | |
765 | ||
766 | sm = wpage; | |
767 | sm->offset = cpu_to_je32(c->sector_size - jeb->free_size); | |
768 | sm->magic = cpu_to_je32(JFFS2_SUM_MAGIC); | |
769 | ||
770 | isum.sum_crc = cpu_to_je32(crc32(0, c->summary->sum_buf, datasize)); | |
771 | isum.node_crc = cpu_to_je32(crc32(0, &isum, sizeof(isum) - 8)); | |
772 | ||
773 | vecs[0].iov_base = &isum; | |
774 | vecs[0].iov_len = sizeof(isum); | |
775 | vecs[1].iov_base = c->summary->sum_buf; | |
776 | vecs[1].iov_len = datasize; | |
777 | ||
778 | sum_ofs = jeb->offset + c->sector_size - jeb->free_size; | |
779 | ||
780 | dbg_summary("JFFS2: writing out data to flash to pos : 0x%08x\n", | |
781 | sum_ofs); | |
782 | ||
783 | ret = jffs2_flash_writev(c, vecs, 2, sum_ofs, &retlen, 0); | |
784 | ||
785 | if (ret || (retlen != infosize)) { | |
786 | ||
787 | JFFS2_WARNING("Write of %u bytes at 0x%08x failed. returned %d, retlen %zd\n", | |
788 | infosize, sum_ofs, ret, retlen); | |
789 | ||
790 | /* Waste remaining space */ | |
791 | spin_lock(&c->erase_completion_lock); | |
792 | jffs2_link_node_ref(c, jeb, sum_ofs | REF_OBSOLETE, infosize, NULL); | |
793 | spin_unlock(&c->erase_completion_lock); | |
794 | ||
795 | c->summary->sum_size = JFFS2_SUMMARY_NOSUM_SIZE; | |
796 | ||
797 | return 0; | |
798 | } | |
799 | ||
800 | spin_lock(&c->erase_completion_lock); | |
801 | jffs2_link_node_ref(c, jeb, sum_ofs | REF_NORMAL, infosize, NULL); | |
802 | spin_unlock(&c->erase_completion_lock); | |
803 | ||
804 | return 0; | |
805 | } | |
806 | ||
807 | /* Write out summary information - called from jffs2_do_reserve_space */ | |
808 | ||
809 | int jffs2_sum_write_sumnode(struct jffs2_sb_info *c) | |
810 | { | |
811 | int datasize, infosize, padsize; | |
812 | struct jffs2_eraseblock *jeb; | |
813 | int ret; | |
814 | ||
815 | dbg_summary("called\n"); | |
816 | ||
817 | spin_unlock(&c->erase_completion_lock); | |
818 | jffs2_prealloc_raw_node_refs(c, 1); | |
819 | ||
820 | jeb = c->nextblock; | |
821 | ||
822 | if (!c->summary->sum_num || !c->summary->sum_list_head) { | |
823 | JFFS2_WARNING("Empty summary info!!!\n"); | |
824 | BUG(); | |
825 | } | |
826 | ||
827 | datasize = c->summary->sum_size + sizeof(struct jffs2_sum_marker); | |
828 | infosize = sizeof(struct jffs2_raw_summary) + datasize; | |
829 | padsize = jeb->free_size - infosize; | |
830 | infosize += padsize; | |
831 | datasize += padsize; | |
832 | ||
833 | /* Is there enough space for summary? */ | |
834 | if (padsize < 0) { | |
835 | /* don't try to write out summary for this jeb */ | |
836 | jffs2_sum_disable_collecting(c->summary); | |
837 | ||
838 | JFFS2_WARNING("Not enough space for summary, padsize = %d\n", padsize); | |
839 | return 0; | |
840 | } | |
841 | ||
842 | ret = jffs2_sum_write_data(c, jeb, infosize, datasize, padsize); | |
843 | spin_lock(&c->erase_completion_lock); | |
844 | return ret; | |
845 | } |