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