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