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