blob: c19bd476e8ec785abbdcaf9edc9c9ff2e0446678 [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 Kohei332a6b92006-06-24 09:17:42 +09008 * 2006 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
Andreas Mohrd6e05ed2006-06-26 18:35:02 +020046 dbg_summary("returned successfully\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;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900313 temp = kmalloc(sizeof(struct jffs2_sum_xattr_mem), GFP_KERNEL);
314 if (!temp)
315 goto no_mem;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100316
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900317 temp->nodetype = node->x.nodetype;
318 temp->xid = node->x.xid;
319 temp->version = node->x.version;
320 temp->totlen = node->x.totlen;
321 temp->offset = cpu_to_je32(ofs);
322 temp->next = NULL;
323
324 return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp);
325 }
326 case JFFS2_NODETYPE_XREF: {
327 struct jffs2_sum_xref_mem *temp;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900328 temp = kmalloc(sizeof(struct jffs2_sum_xref_mem), GFP_KERNEL);
329 if (!temp)
330 goto no_mem;
331 temp->nodetype = node->r.nodetype;
332 temp->offset = cpu_to_je32(ofs);
333 temp->next = NULL;
334
335 return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp);
336 }
337#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100338 case JFFS2_NODETYPE_PADDING:
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100339 dbg_summary("node PADDING\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100340 c->summary->sum_padded += je32_to_cpu(node->u.totlen);
341 break;
342
343 case JFFS2_NODETYPE_CLEANMARKER:
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100344 dbg_summary("node CLEANMARKER\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100345 break;
346
347 case JFFS2_NODETYPE_SUMMARY:
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100348 dbg_summary("node SUMMARY\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100349 break;
350
351 default:
352 /* If you implement a new node type you should also implement
353 summary support for it or disable summary.
354 */
355 BUG();
356 break;
357 }
358
359 return 0;
360
361no_mem:
362 JFFS2_WARNING("MEMORY ALLOCATION ERROR!");
363 return -ENOMEM;
364}
365
David Woodhouse2f785402006-05-24 02:04:45 +0100366static struct jffs2_raw_node_ref *sum_link_node_ref(struct jffs2_sb_info *c,
367 struct jffs2_eraseblock *jeb,
368 uint32_t ofs, uint32_t len,
369 struct jffs2_inode_cache *ic)
David Woodhouse49f11d42006-05-21 04:00:01 +0100370{
David Woodhouse49f11d42006-05-21 04:00:01 +0100371 /* If there was a gap, mark it dirty */
David Woodhouse2f785402006-05-24 02:04:45 +0100372 if ((ofs & ~3) > c->sector_size - jeb->free_size) {
373 /* Ew. Summary doesn't actually tell us explicitly about dirty space */
374 jffs2_scan_dirty_space(c, jeb, (ofs & ~3) - (c->sector_size - jeb->free_size));
David Woodhouse49f11d42006-05-21 04:00:01 +0100375 }
David Woodhouse49f11d42006-05-21 04:00:01 +0100376
David Woodhouse2f785402006-05-24 02:04:45 +0100377 return jffs2_link_node_ref(c, jeb, jeb->offset + ofs, len, ic);
David Woodhouse49f11d42006-05-21 04:00:01 +0100378}
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100379
380/* Process the stored summary information - helper function for jffs2_sum_scan_sumnode() */
381
382static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasi2bc97642005-09-26 12:37:25 +0100383 struct jffs2_raw_summary *summary, uint32_t *pseudo_random)
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100384{
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100385 struct jffs2_inode_cache *ic;
386 struct jffs2_full_dirent *fd;
387 void *sp;
388 int i, ino;
David Woodhouse68270992006-05-21 03:46:05 +0100389 int err;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100390
391 sp = summary->sum;
392
393 for (i=0; i<je32_to_cpu(summary->sum_num); i++) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100394 dbg_summary("processing summary index %d\n", i);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100395
David Woodhouse2f785402006-05-24 02:04:45 +0100396 /* Make sure there's a spare ref for dirty space */
David Woodhouse046b8b92006-05-25 01:50:35 +0100397 err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
David Woodhouse2f785402006-05-24 02:04:45 +0100398 if (err)
399 return err;
400
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100401 switch (je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype)) {
402 case JFFS2_NODETYPE_INODE: {
403 struct jffs2_sum_inode_flash *spi;
404 spi = sp;
405
406 ino = je32_to_cpu(spi->inode);
407
David Woodhouse9167e0f2006-05-21 13:13:45 +0100408 dbg_summary("Inode at 0x%08x-0x%08x\n",
409 jeb->offset + je32_to_cpu(spi->offset),
David Woodhouse8b9e9fe2006-05-25 01:53:09 +0100410 jeb->offset + je32_to_cpu(spi->offset) + je32_to_cpu(spi->totlen));
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100411
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100412 ic = jffs2_scan_make_ino_cache(c, ino);
413 if (!ic) {
414 JFFS2_NOTICE("scan_make_ino_cache failed\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100415 return -ENOMEM;
416 }
417
David Woodhouse2f785402006-05-24 02:04:45 +0100418 sum_link_node_ref(c, jeb, je32_to_cpu(spi->offset) | REF_UNCHECKED,
419 PAD(je32_to_cpu(spi->totlen)), ic);
David Woodhousef1f96712006-05-20 19:45:26 +0100420
421 *pseudo_random += je32_to_cpu(spi->version);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100422
423 sp += JFFS2_SUMMARY_INODE_SIZE;
424
425 break;
426 }
427
428 case JFFS2_NODETYPE_DIRENT: {
429 struct jffs2_sum_dirent_flash *spd;
430 spd = sp;
431
David Woodhouse8b9e9fe2006-05-25 01:53:09 +0100432 dbg_summary("Dirent at 0x%08x-0x%08x\n",
David Woodhouse9167e0f2006-05-21 13:13:45 +0100433 jeb->offset + je32_to_cpu(spd->offset),
434 jeb->offset + je32_to_cpu(spd->offset) + je32_to_cpu(spd->totlen));
435
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100436
437 fd = jffs2_alloc_full_dirent(spd->nsize+1);
David Woodhouse9641b782006-05-20 16:13:34 +0100438 if (!fd)
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100439 return -ENOMEM;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100440
441 memcpy(&fd->name, spd->name, spd->nsize);
442 fd->name[spd->nsize] = 0;
443
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100444 ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(spd->pino));
445 if (!ic) {
446 jffs2_free_full_dirent(fd);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100447 return -ENOMEM;
448 }
449
David Woodhouse1046d882006-06-18 22:44:21 +0100450 fd->raw = sum_link_node_ref(c, jeb, je32_to_cpu(spd->offset) | REF_UNCHECKED,
David Woodhouse2f785402006-05-24 02:04:45 +0100451 PAD(je32_to_cpu(spd->totlen)), ic);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100452
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100453 fd->next = NULL;
454 fd->version = je32_to_cpu(spd->version);
455 fd->ino = je32_to_cpu(spd->ino);
456 fd->nhash = full_name_hash(fd->name, spd->nsize);
457 fd->type = spd->type;
David Woodhousef1f96712006-05-20 19:45:26 +0100458
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100459 jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
460
461 *pseudo_random += je32_to_cpu(spd->version);
462
463 sp += JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize);
464
465 break;
466 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900467#ifdef CONFIG_JFFS2_FS_XATTR
468 case JFFS2_NODETYPE_XATTR: {
469 struct jffs2_xattr_datum *xd;
470 struct jffs2_sum_xattr_flash *spx;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100471
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900472 spx = (struct jffs2_sum_xattr_flash *)sp;
David Woodhouse9167e0f2006-05-21 13:13:45 +0100473 dbg_summary("xattr at %#08x-%#08x (xid=%u, version=%u)\n",
David Woodhouse49f11d42006-05-21 04:00:01 +0100474 jeb->offset + je32_to_cpu(spx->offset),
David Woodhouse9167e0f2006-05-21 13:13:45 +0100475 jeb->offset + je32_to_cpu(spx->offset) + je32_to_cpu(spx->totlen),
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900476 je32_to_cpu(spx->xid), je32_to_cpu(spx->version));
David Woodhouse2f785402006-05-24 02:04:45 +0100477
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900478 xd = jffs2_setup_xattr_datum(c, je32_to_cpu(spx->xid),
479 je32_to_cpu(spx->version));
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900480 if (IS_ERR(xd))
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900481 return PTR_ERR(xd);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900482 if (xd->version > je32_to_cpu(spx->version)) {
483 /* node is not the newest one */
484 struct jffs2_raw_node_ref *raw
485 = sum_link_node_ref(c, jeb, je32_to_cpu(spx->offset) | REF_UNCHECKED,
486 PAD(je32_to_cpu(spx->totlen)), NULL);
487 raw->next_in_ino = xd->node->next_in_ino;
488 xd->node->next_in_ino = raw;
489 } else {
490 xd->version = je32_to_cpu(spx->version);
491 sum_link_node_ref(c, jeb, je32_to_cpu(spx->offset) | REF_UNCHECKED,
492 PAD(je32_to_cpu(spx->totlen)), (void *)xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900493 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900494 *pseudo_random += je32_to_cpu(spx->xid);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900495 sp += JFFS2_SUMMARY_XATTR_SIZE;
496
497 break;
498 }
499 case JFFS2_NODETYPE_XREF: {
500 struct jffs2_xattr_ref *ref;
501 struct jffs2_sum_xref_flash *spr;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900502
503 spr = (struct jffs2_sum_xref_flash *)sp;
David Woodhouse9167e0f2006-05-21 13:13:45 +0100504 dbg_summary("xref at %#08x-%#08x\n",
David Woodhouse49f11d42006-05-21 04:00:01 +0100505 jeb->offset + je32_to_cpu(spr->offset),
David Woodhouse9bfeb692006-05-26 21:19:05 +0100506 jeb->offset + je32_to_cpu(spr->offset) +
507 (uint32_t)PAD(sizeof(struct jffs2_raw_xref)));
David Woodhouse9167e0f2006-05-21 13:13:45 +0100508
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900509 ref = jffs2_alloc_xattr_ref();
510 if (!ref) {
511 JFFS2_NOTICE("allocation of xattr_datum failed\n");
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900512 return -ENOMEM;
513 }
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900514 ref->next = c->xref_temp;
515 c->xref_temp = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900516
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900517 sum_link_node_ref(c, jeb, je32_to_cpu(spr->offset) | REF_UNCHECKED,
518 PAD(sizeof(struct jffs2_raw_xref)), (void *)ref);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900519
David Woodhouse2f785402006-05-24 02:04:45 +0100520 *pseudo_random += ref->node->flash_offset;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900521 sp += JFFS2_SUMMARY_XREF_SIZE;
522
523 break;
524 }
525#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100526 default : {
David Woodhouse7807ef72006-05-21 03:45:27 +0100527 uint16_t nodetype = je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype);
528 JFFS2_WARNING("Unsupported node type %x found in summary! Exiting...\n", nodetype);
529 if ((nodetype & JFFS2_COMPAT_MASK) == JFFS2_FEATURE_INCOMPAT)
530 return -EIO;
531
532 /* For compatible node types, just fall back to the full scan */
533 c->wasted_size -= jeb->wasted_size;
534 c->free_size += c->sector_size - jeb->free_size;
535 c->used_size -= jeb->used_size;
536 c->dirty_size -= jeb->dirty_size;
537 jeb->wasted_size = jeb->used_size = jeb->dirty_size = 0;
538 jeb->free_size = c->sector_size;
539
David Woodhousec38c1b62006-05-25 01:38:27 +0100540 jffs2_free_jeb_node_refs(c, jeb);
David Woodhouse7807ef72006-05-21 03:45:27 +0100541 return -ENOTRECOVERABLE;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100542 }
543 }
544 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100545 return 0;
546}
547
548/* Process the summary node - called from jffs2_scan_eraseblock() */
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100549int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
David Woodhouse9641b782006-05-20 16:13:34 +0100550 struct jffs2_raw_summary *summary, uint32_t sumsize,
551 uint32_t *pseudo_random)
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100552{
553 struct jffs2_unknown_node crcnode;
David Woodhouse9641b782006-05-20 16:13:34 +0100554 int ret, ofs;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100555 uint32_t crc;
556
David Woodhouse49f11d42006-05-21 04:00:01 +0100557 ofs = c->sector_size - sumsize;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100558
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100559 dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
David Woodhouse49f11d42006-05-21 04:00:01 +0100560 jeb->offset, jeb->offset + ofs, sumsize);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100561
562 /* OK, now check for node validity and CRC */
563 crcnode.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
564 crcnode.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY);
565 crcnode.totlen = summary->totlen;
566 crc = crc32(0, &crcnode, sizeof(crcnode)-4);
567
568 if (je32_to_cpu(summary->hdr_crc) != crc) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100569 dbg_summary("Summary node header is corrupt (bad CRC or "
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100570 "no summary at all)\n");
571 goto crc_err;
572 }
573
574 if (je32_to_cpu(summary->totlen) != sumsize) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100575 dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100576 goto crc_err;
577 }
578
Ferenc Havasi2bc97642005-09-26 12:37:25 +0100579 crc = crc32(0, summary, sizeof(struct jffs2_raw_summary)-8);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100580
581 if (je32_to_cpu(summary->node_crc) != crc) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100582 dbg_summary("Summary node is corrupt (bad CRC)\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100583 goto crc_err;
584 }
585
Ferenc Havasi2bc97642005-09-26 12:37:25 +0100586 crc = crc32(0, summary->sum, sumsize - sizeof(struct jffs2_raw_summary));
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100587
588 if (je32_to_cpu(summary->sum_crc) != crc) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100589 dbg_summary("Summary node data is corrupt (bad CRC)\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100590 goto crc_err;
591 }
592
593 if ( je32_to_cpu(summary->cln_mkr) ) {
594
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100595 dbg_summary("Summary : CLEANMARKER node \n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100596
David Woodhouse098a1982006-05-30 09:00:14 +0100597 ret = jffs2_prealloc_raw_node_refs(c, jeb, 1);
598 if (ret)
599 return ret;
600
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100601 if (je32_to_cpu(summary->cln_mkr) != c->cleanmarker_size) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100602 dbg_summary("CLEANMARKER node has totlen 0x%x != normal 0x%x\n",
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100603 je32_to_cpu(summary->cln_mkr), c->cleanmarker_size);
David Woodhouse098a1982006-05-30 09:00:14 +0100604 if ((ret = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(summary->cln_mkr)))))
605 return ret;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100606 } else if (jeb->first_node) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100607 dbg_summary("CLEANMARKER node not first node in block "
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100608 "(0x%08x)\n", jeb->offset);
David Woodhouse098a1982006-05-30 09:00:14 +0100609 if ((ret = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(summary->cln_mkr)))))
610 return ret;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100611 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100612 jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL,
613 je32_to_cpu(summary->cln_mkr), NULL);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100614 }
615 }
616
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100617 ret = jffs2_sum_process_sum_data(c, jeb, summary, pseudo_random);
David Woodhouse7807ef72006-05-21 03:45:27 +0100618 /* -ENOTRECOVERABLE isn't a fatal error -- it means we should do a full
619 scan of this eraseblock. So return zero */
620 if (ret == -ENOTRECOVERABLE)
621 return 0;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100622 if (ret)
David Woodhouse7807ef72006-05-21 03:45:27 +0100623 return ret; /* real error */
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100624
625 /* for PARANOIA_CHECK */
David Woodhouse046b8b92006-05-25 01:50:35 +0100626 ret = jffs2_prealloc_raw_node_refs(c, jeb, 2);
David Woodhouse2f785402006-05-24 02:04:45 +0100627 if (ret)
628 return ret;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100629
David Woodhousef61579c32006-05-25 01:42:40 +0100630 sum_link_node_ref(c, jeb, ofs | REF_NORMAL, sumsize, NULL);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100631
David Woodhouse49f11d42006-05-21 04:00:01 +0100632 if (unlikely(jeb->free_size)) {
633 JFFS2_WARNING("Free size 0x%x bytes in eraseblock @0x%08x with summary?\n",
634 jeb->free_size, jeb->offset);
635 jeb->wasted_size += jeb->free_size;
636 c->wasted_size += jeb->free_size;
637 c->free_size -= jeb->free_size;
638 jeb->free_size = 0;
639 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100640
641 return jffs2_scan_classify_jeb(c, jeb);
642
643crc_err:
644 JFFS2_WARNING("Summary node crc error, skipping summary information.\n");
645
646 return 0;
647}
648
649/* Write summary data to flash - helper function for jffs2_sum_write_sumnode() */
650
651static int jffs2_sum_write_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
652 uint32_t infosize, uint32_t datasize, int padsize)
653{
Ferenc Havasi2bc97642005-09-26 12:37:25 +0100654 struct jffs2_raw_summary isum;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100655 union jffs2_sum_mem *temp;
656 struct jffs2_sum_marker *sm;
657 struct kvec vecs[2];
David Woodhouse2f785402006-05-24 02:04:45 +0100658 uint32_t sum_ofs;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100659 void *wpage;
660 int ret;
661 size_t retlen;
662
663 memset(c->summary->sum_buf, 0xff, datasize);
664 memset(&isum, 0, sizeof(isum));
665
666 isum.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
667 isum.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY);
668 isum.totlen = cpu_to_je32(infosize);
669 isum.hdr_crc = cpu_to_je32(crc32(0, &isum, sizeof(struct jffs2_unknown_node) - 4));
670 isum.padded = cpu_to_je32(c->summary->sum_padded);
671 isum.cln_mkr = cpu_to_je32(c->cleanmarker_size);
672 isum.sum_num = cpu_to_je32(c->summary->sum_num);
673 wpage = c->summary->sum_buf;
674
675 while (c->summary->sum_num) {
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100676 temp = c->summary->sum_list_head;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100677
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100678 switch (je16_to_cpu(temp->u.nodetype)) {
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100679 case JFFS2_NODETYPE_INODE: {
680 struct jffs2_sum_inode_flash *sino_ptr = wpage;
681
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100682 sino_ptr->nodetype = temp->i.nodetype;
683 sino_ptr->inode = temp->i.inode;
684 sino_ptr->version = temp->i.version;
685 sino_ptr->offset = temp->i.offset;
686 sino_ptr->totlen = temp->i.totlen;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100687
688 wpage += JFFS2_SUMMARY_INODE_SIZE;
689
690 break;
691 }
692
693 case JFFS2_NODETYPE_DIRENT: {
694 struct jffs2_sum_dirent_flash *sdrnt_ptr = wpage;
695
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100696 sdrnt_ptr->nodetype = temp->d.nodetype;
697 sdrnt_ptr->totlen = temp->d.totlen;
698 sdrnt_ptr->offset = temp->d.offset;
699 sdrnt_ptr->pino = temp->d.pino;
700 sdrnt_ptr->version = temp->d.version;
701 sdrnt_ptr->ino = temp->d.ino;
702 sdrnt_ptr->nsize = temp->d.nsize;
703 sdrnt_ptr->type = temp->d.type;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100704
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100705 memcpy(sdrnt_ptr->name, temp->d.name,
706 temp->d.nsize);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100707
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100708 wpage += JFFS2_SUMMARY_DIRENT_SIZE(temp->d.nsize);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100709
710 break;
711 }
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900712#ifdef CONFIG_JFFS2_FS_XATTR
713 case JFFS2_NODETYPE_XATTR: {
714 struct jffs2_sum_xattr_flash *sxattr_ptr = wpage;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100715
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900716 temp = c->summary->sum_list_head;
717 sxattr_ptr->nodetype = temp->x.nodetype;
718 sxattr_ptr->xid = temp->x.xid;
719 sxattr_ptr->version = temp->x.version;
720 sxattr_ptr->offset = temp->x.offset;
721 sxattr_ptr->totlen = temp->x.totlen;
722
723 wpage += JFFS2_SUMMARY_XATTR_SIZE;
724 break;
725 }
726 case JFFS2_NODETYPE_XREF: {
727 struct jffs2_sum_xref_flash *sxref_ptr = wpage;
728
729 temp = c->summary->sum_list_head;
730 sxref_ptr->nodetype = temp->r.nodetype;
731 sxref_ptr->offset = temp->r.offset;
732
733 wpage += JFFS2_SUMMARY_XREF_SIZE;
734 break;
735 }
736#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100737 default : {
David Woodhouse61715862006-05-21 00:02:06 +0100738 if ((je16_to_cpu(temp->u.nodetype) & JFFS2_COMPAT_MASK)
739 == JFFS2_FEATURE_RWCOMPAT_COPY) {
740 dbg_summary("Writing unknown RWCOMPAT_COPY node type %x\n",
741 je16_to_cpu(temp->u.nodetype));
742 jffs2_sum_disable_collecting(c->summary);
743 } else {
744 BUG(); /* unknown node in summary information */
745 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100746 }
747 }
748
Jesper Juhl20ffdcb2006-05-12 11:55:51 +0100749 c->summary->sum_list_head = temp->u.next;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100750 kfree(temp);
751
752 c->summary->sum_num--;
753 }
754
755 jffs2_sum_reset_collected(c->summary);
756
757 wpage += padsize;
758
759 sm = wpage;
760 sm->offset = cpu_to_je32(c->sector_size - jeb->free_size);
761 sm->magic = cpu_to_je32(JFFS2_SUM_MAGIC);
762
763 isum.sum_crc = cpu_to_je32(crc32(0, c->summary->sum_buf, datasize));
764 isum.node_crc = cpu_to_je32(crc32(0, &isum, sizeof(isum) - 8));
765
766 vecs[0].iov_base = &isum;
767 vecs[0].iov_len = sizeof(isum);
768 vecs[1].iov_base = c->summary->sum_buf;
769 vecs[1].iov_len = datasize;
770
David Woodhouse2f785402006-05-24 02:04:45 +0100771 sum_ofs = jeb->offset + c->sector_size - jeb->free_size;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100772
David Woodhouse2f785402006-05-24 02:04:45 +0100773 dbg_summary("JFFS2: writing out data to flash to pos : 0x%08x\n",
774 sum_ofs);
775
776 ret = jffs2_flash_writev(c, vecs, 2, sum_ofs, &retlen, 0);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100777
778 if (ret || (retlen != infosize)) {
David Woodhouse010b06d2006-05-21 13:15:59 +0100779
David Woodhousec41ff6e2006-05-16 17:05:33 +0100780 JFFS2_WARNING("Write of %u bytes at 0x%08x failed. returned %d, retlen %zd\n",
David Woodhouse2f785402006-05-24 02:04:45 +0100781 infosize, sum_ofs, ret, retlen);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100782
David Woodhouse9bfeb692006-05-26 21:19:05 +0100783 if (retlen) {
784 /* Waste remaining space */
785 spin_lock(&c->erase_completion_lock);
786 jffs2_link_node_ref(c, jeb, sum_ofs | REF_OBSOLETE, infosize, NULL);
787 spin_unlock(&c->erase_completion_lock);
788 }
David Woodhouse010b06d2006-05-21 13:15:59 +0100789
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100790 c->summary->sum_size = JFFS2_SUMMARY_NOSUM_SIZE;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100791
David Woodhouse2f785402006-05-24 02:04:45 +0100792 return 0;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100793 }
794
David Woodhouse010b06d2006-05-21 13:15:59 +0100795 spin_lock(&c->erase_completion_lock);
David Woodhouse2f785402006-05-24 02:04:45 +0100796 jffs2_link_node_ref(c, jeb, sum_ofs | REF_NORMAL, infosize, NULL);
797 spin_unlock(&c->erase_completion_lock);
David Woodhouse010b06d2006-05-21 13:15:59 +0100798
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100799 return 0;
800}
801
802/* Write out summary information - called from jffs2_do_reserve_space */
803
804int jffs2_sum_write_sumnode(struct jffs2_sb_info *c)
805{
David Woodhouse2f785402006-05-24 02:04:45 +0100806 int datasize, infosize, padsize;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100807 struct jffs2_eraseblock *jeb;
David Woodhouse2f785402006-05-24 02:04:45 +0100808 int ret;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100809
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100810 dbg_summary("called\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100811
David Woodhouse2f785402006-05-24 02:04:45 +0100812 spin_unlock(&c->erase_completion_lock);
David Woodhouse2f785402006-05-24 02:04:45 +0100813
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100814 jeb = c->nextblock;
David Woodhouse046b8b92006-05-25 01:50:35 +0100815 jffs2_prealloc_raw_node_refs(c, jeb, 1);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100816
817 if (!c->summary->sum_num || !c->summary->sum_list_head) {
818 JFFS2_WARNING("Empty summary info!!!\n");
819 BUG();
820 }
821
822 datasize = c->summary->sum_size + sizeof(struct jffs2_sum_marker);
Ferenc Havasi2bc97642005-09-26 12:37:25 +0100823 infosize = sizeof(struct jffs2_raw_summary) + datasize;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100824 padsize = jeb->free_size - infosize;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000825 infosize += padsize;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100826 datasize += padsize;
827
828 /* Is there enough space for summary? */
829 if (padsize < 0) {
830 /* don't try to write out summary for this jeb */
831 jffs2_sum_disable_collecting(c->summary);
832
833 JFFS2_WARNING("Not enough space for summary, padsize = %d\n", padsize);
David Woodhouse9bfeb692006-05-26 21:19:05 +0100834 spin_lock(&c->erase_completion_lock);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100835 return 0;
836 }
837
838 ret = jffs2_sum_write_data(c, jeb, infosize, datasize, padsize);
David Woodhouse010b06d2006-05-21 13:15:59 +0100839 spin_lock(&c->erase_completion_lock);
David Woodhouse2f785402006-05-24 02:04:45 +0100840 return ret;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100841}