blob: 831a42c130590517f504121a1cc82082d7097cf1 [file] [log] [blame]
Ferenc Havasie631ddb2005-09-07 09:35:26 +01001/*
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
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +09008 * 2005 KaiGai Kohei <kaigai@ak.jp.nec.com>
Ferenc Havasie631ddb2005-09-07 09:35:26 +01009 *
10 * For licensing information, see the file 'LICENCE' in this directory.
11 *
Ferenc Havasi2bc97642005-09-26 12:37:25 +010012 * $Id: summary.c,v 1.4 2005/09/26 11:37:21 havasi Exp $
Ferenc Havasie631ddb2005-09-07 09:35:26 +010013 *
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{
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");
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +010042 kfree(c->summary);
Ferenc Havasie631ddb2005-09-07 09:35:26 +010043 return -ENOMEM;
44 }
45
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +010046 dbg_summary("returned succesfully\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +010047
48 return 0;
49}
50
51void jffs2_sum_exit(struct jffs2_sb_info *c)
52{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +010053 dbg_summary("called\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +010054
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
64static 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++;
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +010076 dbg_summary("inode (%u) added to summary\n",
Ferenc Havasie631ddb2005-09-07 09:35:26 +010077 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++;
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +010082 dbg_summary("dirent (%u) added to summary\n",
Ferenc Havasie631ddb2005-09-07 09:35:26 +010083 je32_to_cpu(item->d.ino));
84 break;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090085#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
Ferenc Havasie631ddb2005-09-07 09:35:26 +010098 default:
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000099 JFFS2_WARNING("UNKNOWN node type %u\n",
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100100 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
109int jffs2_sum_add_padding_mem(struct jffs2_summary *s, uint32_t size)
110{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100111 dbg_summary("called with %u\n", size);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100112 s->sum_padded += size;
113 return 0;
114}
115
116int 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
134int 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
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900158#ifdef CONFIG_JFFS2_FS_XATTR
159int 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
177int 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
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100192/* Cleanup every collected summary information */
193
194static void jffs2_sum_clean_collected(struct jffs2_summary *s)
195{
196 union jffs2_sum_mem *temp;
197
198 if (!s->sum_list_head) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100199 dbg_summary("already empty\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100200 }
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
211void jffs2_sum_reset_collected(struct jffs2_summary *s)
212{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100213 dbg_summary("called\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100214 jffs2_sum_clean_collected(s);
215 s->sum_size = 0;
216}
217
218void jffs2_sum_disable_collecting(struct jffs2_summary *s)
219{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100220 dbg_summary("called\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100221 jffs2_sum_clean_collected(s);
222 s->sum_size = JFFS2_SUMMARY_NOSUM_SIZE;
223}
224
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000225int jffs2_sum_is_disabled(struct jffs2_summary *s)
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100226{
227 return (s->sum_size == JFFS2_SUMMARY_NOSUM_SIZE);
228}
229
230/* Move the collected summary information into sb (called from scan.c) */
231
232void jffs2_sum_move_collected(struct jffs2_sb_info *c, struct jffs2_summary *s)
233{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100234 dbg_summary("oldsize=0x%x oldnum=%u => newsize=0x%x newnum=%u\n",
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100235 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
249int 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 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900310#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;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100318
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900319 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
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100344 case JFFS2_NODETYPE_PADDING:
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100345 dbg_summary("node PADDING\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100346 c->summary->sum_padded += je32_to_cpu(node->u.totlen);
347 break;
348
349 case JFFS2_NODETYPE_CLEANMARKER:
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100350 dbg_summary("node CLEANMARKER\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100351 break;
352
353 case JFFS2_NODETYPE_SUMMARY:
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100354 dbg_summary("node SUMMARY\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100355 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
367no_mem:
368 JFFS2_WARNING("MEMORY ALLOCATION ERROR!");
369 return -ENOMEM;
370}
371
372
373/* Process the stored summary information - helper function for jffs2_sum_scan_sumnode() */
374
375static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasi2bc97642005-09-26 12:37:25 +0100376 struct jffs2_raw_summary *summary, uint32_t *pseudo_random)
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100377{
378 struct jffs2_raw_node_ref *raw;
379 struct jffs2_inode_cache *ic;
380 struct jffs2_full_dirent *fd;
381 void *sp;
382 int i, ino;
383
384 sp = summary->sum;
385
386 for (i=0; i<je32_to_cpu(summary->sum_num); i++) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100387 dbg_summary("processing summary index %d\n", i);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100388
389 switch (je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype)) {
390 case JFFS2_NODETYPE_INODE: {
391 struct jffs2_sum_inode_flash *spi;
392 spi = sp;
393
394 ino = je32_to_cpu(spi->inode);
395
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100396 dbg_summary("Inode at 0x%08x\n",
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100397 jeb->offset + je32_to_cpu(spi->offset));
398
399 raw = jffs2_alloc_raw_node_ref();
400 if (!raw) {
401 JFFS2_NOTICE("allocation of node reference failed\n");
402 kfree(summary);
403 return -ENOMEM;
404 }
405
406 ic = jffs2_scan_make_ino_cache(c, ino);
407 if (!ic) {
408 JFFS2_NOTICE("scan_make_ino_cache failed\n");
409 jffs2_free_raw_node_ref(raw);
410 kfree(summary);
411 return -ENOMEM;
412 }
413
414 raw->flash_offset = (jeb->offset + je32_to_cpu(spi->offset)) | REF_UNCHECKED;
415 raw->__totlen = PAD(je32_to_cpu(spi->totlen));
416 raw->next_phys = NULL;
417 raw->next_in_ino = ic->nodes;
418
419 ic->nodes = raw;
420 if (!jeb->first_node)
421 jeb->first_node = raw;
422 if (jeb->last_node)
423 jeb->last_node->next_phys = raw;
424 jeb->last_node = raw;
425 *pseudo_random += je32_to_cpu(spi->version);
426
427 UNCHECKED_SPACE(PAD(je32_to_cpu(spi->totlen)));
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
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100438 dbg_summary("Dirent at 0x%08x\n",
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100439 jeb->offset + je32_to_cpu(spd->offset));
440
441 fd = jffs2_alloc_full_dirent(spd->nsize+1);
442 if (!fd) {
443 kfree(summary);
444 return -ENOMEM;
445 }
446
447 memcpy(&fd->name, spd->name, spd->nsize);
448 fd->name[spd->nsize] = 0;
449
450 raw = jffs2_alloc_raw_node_ref();
451 if (!raw) {
452 jffs2_free_full_dirent(fd);
453 JFFS2_NOTICE("allocation of node reference failed\n");
454 kfree(summary);
455 return -ENOMEM;
456 }
457
458 ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(spd->pino));
459 if (!ic) {
460 jffs2_free_full_dirent(fd);
461 jffs2_free_raw_node_ref(raw);
462 kfree(summary);
463 return -ENOMEM;
464 }
465
466 raw->__totlen = PAD(je32_to_cpu(spd->totlen));
467 raw->flash_offset = (jeb->offset + je32_to_cpu(spd->offset)) | REF_PRISTINE;
468 raw->next_phys = NULL;
469 raw->next_in_ino = ic->nodes;
470 ic->nodes = raw;
471 if (!jeb->first_node)
472 jeb->first_node = raw;
473 if (jeb->last_node)
474 jeb->last_node->next_phys = raw;
475 jeb->last_node = raw;
476
477 fd->raw = raw;
478 fd->next = NULL;
479 fd->version = je32_to_cpu(spd->version);
480 fd->ino = je32_to_cpu(spd->ino);
481 fd->nhash = full_name_hash(fd->name, spd->nsize);
482 fd->type = spd->type;
483 USED_SPACE(PAD(je32_to_cpu(spd->totlen)));
484 jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
485
486 *pseudo_random += je32_to_cpu(spd->version);
487
488 sp += JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize);
489
490 break;
491 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900492#ifdef CONFIG_JFFS2_FS_XATTR
493 case JFFS2_NODETYPE_XATTR: {
494 struct jffs2_xattr_datum *xd;
495 struct jffs2_sum_xattr_flash *spx;
496 uint32_t ofs;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100497
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900498 spx = (struct jffs2_sum_xattr_flash *)sp;
499 ofs = jeb->offset + je32_to_cpu(spx->offset);
500 dbg_summary("xattr at %#08x (xid=%u, version=%u)\n", ofs,
501 je32_to_cpu(spx->xid), je32_to_cpu(spx->version));
502 raw = jffs2_alloc_raw_node_ref();
503 if (!raw) {
504 JFFS2_NOTICE("allocation of node reference failed\n");
505 kfree(summary);
506 return -ENOMEM;
507 }
508 xd = jffs2_setup_xattr_datum(c, je32_to_cpu(spx->xid),
509 je32_to_cpu(spx->version));
510 if (IS_ERR(xd)) {
511 JFFS2_NOTICE("allocation of xattr_datum failed\n");
512 jffs2_free_raw_node_ref(raw);
513 kfree(summary);
514 return PTR_ERR(xd);
515 }
516 xd->node = raw;
517
518 raw->flash_offset = ofs | REF_UNCHECKED;
519 raw->__totlen = PAD(je32_to_cpu(spx->totlen));
520 raw->next_phys = NULL;
521 raw->next_in_ino = (void *)xd;
522 if (!jeb->first_node)
523 jeb->first_node = raw;
524 if (jeb->last_node)
525 jeb->last_node->next_phys = raw;
526 jeb->last_node = raw;
527
528 *pseudo_random += je32_to_cpu(spx->xid);
529 UNCHECKED_SPACE(je32_to_cpu(spx->totlen));
530 sp += JFFS2_SUMMARY_XATTR_SIZE;
531
532 break;
533 }
534 case JFFS2_NODETYPE_XREF: {
535 struct jffs2_xattr_ref *ref;
536 struct jffs2_sum_xref_flash *spr;
537 uint32_t ofs;
538
539 spr = (struct jffs2_sum_xref_flash *)sp;
540 ofs = jeb->offset + je32_to_cpu(spr->offset);
541 dbg_summary("xref at %#08x (xid=%u, ino=%u)\n", ofs,
542 je32_to_cpu(spr->xid), je32_to_cpu(spr->ino));
543 raw = jffs2_alloc_raw_node_ref();
544 if (!raw) {
545 JFFS2_NOTICE("allocation of node reference failed\n");
546 kfree(summary);
547 return -ENOMEM;
548 }
549 ref = jffs2_alloc_xattr_ref();
550 if (!ref) {
551 JFFS2_NOTICE("allocation of xattr_datum failed\n");
552 jffs2_free_raw_node_ref(raw);
553 kfree(summary);
554 return -ENOMEM;
555 }
556 ref->ino = 0xfffffffe;
557 ref->xid = 0xfffffffd;
558 ref->node = raw;
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900559 ref->next = c->xref_temp;
560 c->xref_temp = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900561
562 raw->__totlen = PAD(sizeof(struct jffs2_raw_xref));
563 raw->flash_offset = ofs | REF_UNCHECKED;
564 raw->next_phys = NULL;
565 raw->next_in_ino = (void *)ref;
566 if (!jeb->first_node)
567 jeb->first_node = raw;
568 if (jeb->last_node)
569 jeb->last_node->next_phys = raw;
570 jeb->last_node = raw;
571
572 UNCHECKED_SPACE(PAD(sizeof(struct jffs2_raw_xref)));
573 *pseudo_random += ofs;
574 sp += JFFS2_SUMMARY_XREF_SIZE;
575
576 break;
577 }
578#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100579 default : {
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900580printk("nodetype = %#04x\n",je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype));
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100581 JFFS2_WARNING("Unsupported node type found in summary! Exiting...");
582 kfree(summary);
583 return -EIO;
584 }
585 }
586 }
587
588 kfree(summary);
589 return 0;
590}
591
592/* Process the summary node - called from jffs2_scan_eraseblock() */
593
594int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
595 uint32_t ofs, uint32_t *pseudo_random)
596{
597 struct jffs2_unknown_node crcnode;
598 struct jffs2_raw_node_ref *cache_ref;
Ferenc Havasi2bc97642005-09-26 12:37:25 +0100599 struct jffs2_raw_summary *summary;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100600 int ret, sumsize;
601 uint32_t crc;
602
603 sumsize = c->sector_size - ofs;
604 ofs += jeb->offset;
605
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100606 dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100607 jeb->offset, ofs, sumsize);
608
609 summary = kmalloc(sumsize, GFP_KERNEL);
610
611 if (!summary) {
612 return -ENOMEM;
613 }
614
615 ret = jffs2_fill_scan_buf(c, (unsigned char *)summary, ofs, sumsize);
616
617 if (ret) {
618 kfree(summary);
619 return ret;
620 }
621
622 /* OK, now check for node validity and CRC */
623 crcnode.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
624 crcnode.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY);
625 crcnode.totlen = summary->totlen;
626 crc = crc32(0, &crcnode, sizeof(crcnode)-4);
627
628 if (je32_to_cpu(summary->hdr_crc) != crc) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100629 dbg_summary("Summary node header is corrupt (bad CRC or "
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100630 "no summary at all)\n");
631 goto crc_err;
632 }
633
634 if (je32_to_cpu(summary->totlen) != sumsize) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100635 dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100636 goto crc_err;
637 }
638
Ferenc Havasi2bc97642005-09-26 12:37:25 +0100639 crc = crc32(0, summary, sizeof(struct jffs2_raw_summary)-8);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100640
641 if (je32_to_cpu(summary->node_crc) != crc) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100642 dbg_summary("Summary node is corrupt (bad CRC)\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100643 goto crc_err;
644 }
645
Ferenc Havasi2bc97642005-09-26 12:37:25 +0100646 crc = crc32(0, summary->sum, sumsize - sizeof(struct jffs2_raw_summary));
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100647
648 if (je32_to_cpu(summary->sum_crc) != crc) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100649 dbg_summary("Summary node data is corrupt (bad CRC)\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100650 goto crc_err;
651 }
652
653 if ( je32_to_cpu(summary->cln_mkr) ) {
654
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100655 dbg_summary("Summary : CLEANMARKER node \n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100656
657 if (je32_to_cpu(summary->cln_mkr) != c->cleanmarker_size) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100658 dbg_summary("CLEANMARKER node has totlen 0x%x != normal 0x%x\n",
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100659 je32_to_cpu(summary->cln_mkr), c->cleanmarker_size);
660 UNCHECKED_SPACE(PAD(je32_to_cpu(summary->cln_mkr)));
661 } else if (jeb->first_node) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100662 dbg_summary("CLEANMARKER node not first node in block "
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100663 "(0x%08x)\n", jeb->offset);
664 UNCHECKED_SPACE(PAD(je32_to_cpu(summary->cln_mkr)));
665 } else {
666 struct jffs2_raw_node_ref *marker_ref = jffs2_alloc_raw_node_ref();
667
668 if (!marker_ref) {
669 JFFS2_NOTICE("Failed to allocate node ref for clean marker\n");
670 kfree(summary);
671 return -ENOMEM;
672 }
673
674 marker_ref->next_in_ino = NULL;
675 marker_ref->next_phys = NULL;
676 marker_ref->flash_offset = jeb->offset | REF_NORMAL;
677 marker_ref->__totlen = je32_to_cpu(summary->cln_mkr);
678 jeb->first_node = jeb->last_node = marker_ref;
679
680 USED_SPACE( PAD(je32_to_cpu(summary->cln_mkr)) );
681 }
682 }
683
684 if (je32_to_cpu(summary->padded)) {
685 DIRTY_SPACE(je32_to_cpu(summary->padded));
686 }
687
688 ret = jffs2_sum_process_sum_data(c, jeb, summary, pseudo_random);
689 if (ret)
690 return ret;
691
692 /* for PARANOIA_CHECK */
693 cache_ref = jffs2_alloc_raw_node_ref();
694
695 if (!cache_ref) {
696 JFFS2_NOTICE("Failed to allocate node ref for cache\n");
697 return -ENOMEM;
698 }
699
700 cache_ref->next_in_ino = NULL;
701 cache_ref->next_phys = NULL;
702 cache_ref->flash_offset = ofs | REF_NORMAL;
703 cache_ref->__totlen = sumsize;
704
705 if (!jeb->first_node)
706 jeb->first_node = cache_ref;
707 if (jeb->last_node)
708 jeb->last_node->next_phys = cache_ref;
709 jeb->last_node = cache_ref;
710
711 USED_SPACE(sumsize);
712
713 jeb->wasted_size += jeb->free_size;
714 c->wasted_size += jeb->free_size;
715 c->free_size -= jeb->free_size;
716 jeb->free_size = 0;
717
718 return jffs2_scan_classify_jeb(c, jeb);
719
720crc_err:
721 JFFS2_WARNING("Summary node crc error, skipping summary information.\n");
722
723 return 0;
724}
725
726/* Write summary data to flash - helper function for jffs2_sum_write_sumnode() */
727
728static int jffs2_sum_write_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
729 uint32_t infosize, uint32_t datasize, int padsize)
730{
Ferenc Havasi2bc97642005-09-26 12:37:25 +0100731 struct jffs2_raw_summary isum;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100732 union jffs2_sum_mem *temp;
733 struct jffs2_sum_marker *sm;
734 struct kvec vecs[2];
735 void *wpage;
736 int ret;
737 size_t retlen;
738
739 memset(c->summary->sum_buf, 0xff, datasize);
740 memset(&isum, 0, sizeof(isum));
741
742 isum.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
743 isum.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY);
744 isum.totlen = cpu_to_je32(infosize);
745 isum.hdr_crc = cpu_to_je32(crc32(0, &isum, sizeof(struct jffs2_unknown_node) - 4));
746 isum.padded = cpu_to_je32(c->summary->sum_padded);
747 isum.cln_mkr = cpu_to_je32(c->cleanmarker_size);
748 isum.sum_num = cpu_to_je32(c->summary->sum_num);
749 wpage = c->summary->sum_buf;
750
751 while (c->summary->sum_num) {
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100752 temp = c->summary->sum_list_head;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100753
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100754 switch (je16_to_cpu(temp->u.nodetype)) {
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100755 case JFFS2_NODETYPE_INODE: {
756 struct jffs2_sum_inode_flash *sino_ptr = wpage;
757
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100758 sino_ptr->nodetype = temp->i.nodetype;
759 sino_ptr->inode = temp->i.inode;
760 sino_ptr->version = temp->i.version;
761 sino_ptr->offset = temp->i.offset;
762 sino_ptr->totlen = temp->i.totlen;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100763
764 wpage += JFFS2_SUMMARY_INODE_SIZE;
765
766 break;
767 }
768
769 case JFFS2_NODETYPE_DIRENT: {
770 struct jffs2_sum_dirent_flash *sdrnt_ptr = wpage;
771
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100772 sdrnt_ptr->nodetype = temp->d.nodetype;
773 sdrnt_ptr->totlen = temp->d.totlen;
774 sdrnt_ptr->offset = temp->d.offset;
775 sdrnt_ptr->pino = temp->d.pino;
776 sdrnt_ptr->version = temp->d.version;
777 sdrnt_ptr->ino = temp->d.ino;
778 sdrnt_ptr->nsize = temp->d.nsize;
779 sdrnt_ptr->type = temp->d.type;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100780
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100781 memcpy(sdrnt_ptr->name, temp->d.name,
782 temp->d.nsize);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100783
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100784 wpage += JFFS2_SUMMARY_DIRENT_SIZE(temp->d.nsize);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100785
786 break;
787 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900788#ifdef CONFIG_JFFS2_FS_XATTR
789 case JFFS2_NODETYPE_XATTR: {
790 struct jffs2_sum_xattr_flash *sxattr_ptr = wpage;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100791
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900792 temp = c->summary->sum_list_head;
793 sxattr_ptr->nodetype = temp->x.nodetype;
794 sxattr_ptr->xid = temp->x.xid;
795 sxattr_ptr->version = temp->x.version;
796 sxattr_ptr->offset = temp->x.offset;
797 sxattr_ptr->totlen = temp->x.totlen;
798
799 wpage += JFFS2_SUMMARY_XATTR_SIZE;
800 break;
801 }
802 case JFFS2_NODETYPE_XREF: {
803 struct jffs2_sum_xref_flash *sxref_ptr = wpage;
804
805 temp = c->summary->sum_list_head;
806 sxref_ptr->nodetype = temp->r.nodetype;
807 sxref_ptr->offset = temp->r.offset;
808
809 wpage += JFFS2_SUMMARY_XREF_SIZE;
810 break;
811 }
812#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100813 default : {
814 BUG(); /* unknown node in summary information */
815 }
816 }
817
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100818 c->summary->sum_list_head = temp->u.next;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100819 kfree(temp);
820
821 c->summary->sum_num--;
822 }
823
824 jffs2_sum_reset_collected(c->summary);
825
826 wpage += padsize;
827
828 sm = wpage;
829 sm->offset = cpu_to_je32(c->sector_size - jeb->free_size);
830 sm->magic = cpu_to_je32(JFFS2_SUM_MAGIC);
831
832 isum.sum_crc = cpu_to_je32(crc32(0, c->summary->sum_buf, datasize));
833 isum.node_crc = cpu_to_je32(crc32(0, &isum, sizeof(isum) - 8));
834
835 vecs[0].iov_base = &isum;
836 vecs[0].iov_len = sizeof(isum);
837 vecs[1].iov_base = c->summary->sum_buf;
838 vecs[1].iov_len = datasize;
839
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100840 dbg_summary("JFFS2: writing out data to flash to pos : 0x%08x\n",
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100841 jeb->offset + c->sector_size - jeb->free_size);
842
843 spin_unlock(&c->erase_completion_lock);
844 ret = jffs2_flash_writev(c, vecs, 2, jeb->offset + c->sector_size -
845 jeb->free_size, &retlen, 0);
846 spin_lock(&c->erase_completion_lock);
847
848
849 if (ret || (retlen != infosize)) {
Randy Dunlapfb6a82c2006-04-11 20:12:10 -0400850 JFFS2_WARNING("Write of %d bytes at 0x%08x failed. returned %d, retlen %zu\n",
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100851 infosize, jeb->offset + c->sector_size - jeb->free_size, ret, retlen);
852
853 c->summary->sum_size = JFFS2_SUMMARY_NOSUM_SIZE;
854 WASTED_SPACE(infosize);
855
856 return 1;
857 }
858
859 return 0;
860}
861
862/* Write out summary information - called from jffs2_do_reserve_space */
863
864int jffs2_sum_write_sumnode(struct jffs2_sb_info *c)
865{
866 struct jffs2_raw_node_ref *summary_ref;
867 int datasize, infosize, padsize, ret;
868 struct jffs2_eraseblock *jeb;
869
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100870 dbg_summary("called\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100871
872 jeb = c->nextblock;
873
874 if (!c->summary->sum_num || !c->summary->sum_list_head) {
875 JFFS2_WARNING("Empty summary info!!!\n");
876 BUG();
877 }
878
879 datasize = c->summary->sum_size + sizeof(struct jffs2_sum_marker);
Ferenc Havasi2bc97642005-09-26 12:37:25 +0100880 infosize = sizeof(struct jffs2_raw_summary) + datasize;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100881 padsize = jeb->free_size - infosize;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000882 infosize += padsize;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100883 datasize += padsize;
884
885 /* Is there enough space for summary? */
886 if (padsize < 0) {
887 /* don't try to write out summary for this jeb */
888 jffs2_sum_disable_collecting(c->summary);
889
890 JFFS2_WARNING("Not enough space for summary, padsize = %d\n", padsize);
891 return 0;
892 }
893
894 ret = jffs2_sum_write_data(c, jeb, infosize, datasize, padsize);
895 if (ret)
896 return 0; /* can't write out summary, block is marked as NOSUM_SIZE */
897
898 /* for ACCT_PARANOIA_CHECK */
899 spin_unlock(&c->erase_completion_lock);
900 summary_ref = jffs2_alloc_raw_node_ref();
901 spin_lock(&c->erase_completion_lock);
902
903 if (!summary_ref) {
904 JFFS2_NOTICE("Failed to allocate node ref for summary\n");
905 return -ENOMEM;
906 }
907
908 summary_ref->next_in_ino = NULL;
909 summary_ref->next_phys = NULL;
910 summary_ref->flash_offset = (jeb->offset + c->sector_size - jeb->free_size) | REF_NORMAL;
911 summary_ref->__totlen = infosize;
912
913 if (!jeb->first_node)
914 jeb->first_node = summary_ref;
915 if (jeb->last_node)
916 jeb->last_node->next_phys = summary_ref;
917 jeb->last_node = summary_ref;
918
919 USED_SPACE(infosize);
920
921 return 0;
922}