Merge ../linux-2.6
[linux-2.6] / fs / jffs2 / summary.c
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
8  *
9  * For licensing information, see the file 'LICENCE' in this directory.
10  *
11  * $Id: summary.c,v 1.4 2005/09/26 11:37:21 havasi Exp $
12  *
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/pagemap.h>
20 #include <linux/crc32.h>
21 #include <linux/compiler.h>
22 #include <linux/vmalloc.h>
23 #include "nodelist.h"
24 #include "debug.h"
25
26 int jffs2_sum_init(struct jffs2_sb_info *c)
27 {
28         c->summary = kmalloc(sizeof(struct jffs2_summary), GFP_KERNEL);
29
30         if (!c->summary) {
31                 JFFS2_WARNING("Can't allocate memory for summary information!\n");
32                 return -ENOMEM;
33         }
34
35         memset(c->summary, 0, sizeof(struct jffs2_summary));
36
37         c->summary->sum_buf = vmalloc(c->sector_size);
38
39         if (!c->summary->sum_buf) {
40                 JFFS2_WARNING("Can't allocate buffer for writing out summary information!\n");
41                 kfree(c->summary);
42                 return -ENOMEM;
43         }
44
45         dbg_summary("returned succesfully\n");
46
47         return 0;
48 }
49
50 void jffs2_sum_exit(struct jffs2_sb_info *c)
51 {
52         dbg_summary("called\n");
53
54         jffs2_sum_disable_collecting(c->summary);
55
56         vfree(c->summary->sum_buf);
57         c->summary->sum_buf = NULL;
58
59         kfree(c->summary);
60         c->summary = NULL;
61 }
62
63 static int jffs2_sum_add_mem(struct jffs2_summary *s, union jffs2_sum_mem *item)
64 {
65         if (!s->sum_list_head)
66                 s->sum_list_head = (union jffs2_sum_mem *) item;
67         if (s->sum_list_tail)
68                 s->sum_list_tail->u.next = (union jffs2_sum_mem *) item;
69         s->sum_list_tail = (union jffs2_sum_mem *) item;
70
71         switch (je16_to_cpu(item->u.nodetype)) {
72                 case JFFS2_NODETYPE_INODE:
73                         s->sum_size += JFFS2_SUMMARY_INODE_SIZE;
74                         s->sum_num++;
75                         dbg_summary("inode (%u) added to summary\n",
76                                                 je32_to_cpu(item->i.inode));
77                         break;
78                 case JFFS2_NODETYPE_DIRENT:
79                         s->sum_size += JFFS2_SUMMARY_DIRENT_SIZE(item->d.nsize);
80                         s->sum_num++;
81                         dbg_summary("dirent (%u) added to summary\n",
82                                                 je32_to_cpu(item->d.ino));
83                         break;
84                 default:
85                         JFFS2_WARNING("UNKNOWN node type %u\n",
86                                             je16_to_cpu(item->u.nodetype));
87                         return 1;
88         }
89         return 0;
90 }
91
92
93 /* The following 3 functions are called from scan.c to collect summary info for not closed jeb */
94
95 int jffs2_sum_add_padding_mem(struct jffs2_summary *s, uint32_t size)
96 {
97         dbg_summary("called with %u\n", size);
98         s->sum_padded += size;
99         return 0;
100 }
101
102 int jffs2_sum_add_inode_mem(struct jffs2_summary *s, struct jffs2_raw_inode *ri,
103                                 uint32_t ofs)
104 {
105         struct jffs2_sum_inode_mem *temp = kmalloc(sizeof(struct jffs2_sum_inode_mem), GFP_KERNEL);
106
107         if (!temp)
108                 return -ENOMEM;
109
110         temp->nodetype = ri->nodetype;
111         temp->inode = ri->ino;
112         temp->version = ri->version;
113         temp->offset = cpu_to_je32(ofs); /* relative offset from the begining of the jeb */
114         temp->totlen = ri->totlen;
115         temp->next = NULL;
116
117         return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp);
118 }
119
120 int jffs2_sum_add_dirent_mem(struct jffs2_summary *s, struct jffs2_raw_dirent *rd,
121                                 uint32_t ofs)
122 {
123         struct jffs2_sum_dirent_mem *temp =
124                 kmalloc(sizeof(struct jffs2_sum_dirent_mem) + rd->nsize, GFP_KERNEL);
125
126         if (!temp)
127                 return -ENOMEM;
128
129         temp->nodetype = rd->nodetype;
130         temp->totlen = rd->totlen;
131         temp->offset = cpu_to_je32(ofs);        /* relative from the begining of the jeb */
132         temp->pino = rd->pino;
133         temp->version = rd->version;
134         temp->ino = rd->ino;
135         temp->nsize = rd->nsize;
136         temp->type = rd->type;
137         temp->next = NULL;
138
139         memcpy(temp->name, rd->name, rd->nsize);
140
141         return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp);
142 }
143
144 /* Cleanup every collected summary information */
145
146 static void jffs2_sum_clean_collected(struct jffs2_summary *s)
147 {
148         union jffs2_sum_mem *temp;
149
150         if (!s->sum_list_head) {
151                 dbg_summary("already empty\n");
152         }
153         while (s->sum_list_head) {
154                 temp = s->sum_list_head;
155                 s->sum_list_head = s->sum_list_head->u.next;
156                 kfree(temp);
157         }
158         s->sum_list_tail = NULL;
159         s->sum_padded = 0;
160         s->sum_num = 0;
161 }
162
163 void jffs2_sum_reset_collected(struct jffs2_summary *s)
164 {
165         dbg_summary("called\n");
166         jffs2_sum_clean_collected(s);
167         s->sum_size = 0;
168 }
169
170 void jffs2_sum_disable_collecting(struct jffs2_summary *s)
171 {
172         dbg_summary("called\n");
173         jffs2_sum_clean_collected(s);
174         s->sum_size = JFFS2_SUMMARY_NOSUM_SIZE;
175 }
176
177 int jffs2_sum_is_disabled(struct jffs2_summary *s)
178 {
179         return (s->sum_size == JFFS2_SUMMARY_NOSUM_SIZE);
180 }
181
182 /* Move the collected summary information into sb (called from scan.c) */
183
184 void jffs2_sum_move_collected(struct jffs2_sb_info *c, struct jffs2_summary *s)
185 {
186         dbg_summary("oldsize=0x%x oldnum=%u => newsize=0x%x newnum=%u\n",
187                                 c->summary->sum_size, c->summary->sum_num,
188                                 s->sum_size, s->sum_num);
189
190         c->summary->sum_size = s->sum_size;
191         c->summary->sum_num = s->sum_num;
192         c->summary->sum_padded = s->sum_padded;
193         c->summary->sum_list_head = s->sum_list_head;
194         c->summary->sum_list_tail = s->sum_list_tail;
195
196         s->sum_list_head = s->sum_list_tail = NULL;
197 }
198
199 /* Called from wbuf.c to collect writed node info */
200
201 int jffs2_sum_add_kvec(struct jffs2_sb_info *c, const struct kvec *invecs,
202                                 unsigned long count, uint32_t ofs)
203 {
204         union jffs2_node_union *node;
205         struct jffs2_eraseblock *jeb;
206
207         node = invecs[0].iov_base;
208         jeb = &c->blocks[ofs / c->sector_size];
209         ofs -= jeb->offset;
210
211         switch (je16_to_cpu(node->u.nodetype)) {
212                 case JFFS2_NODETYPE_INODE: {
213                         struct jffs2_sum_inode_mem *temp =
214                                 kmalloc(sizeof(struct jffs2_sum_inode_mem), GFP_KERNEL);
215
216                         if (!temp)
217                                 goto no_mem;
218
219                         temp->nodetype = node->i.nodetype;
220                         temp->inode = node->i.ino;
221                         temp->version = node->i.version;
222                         temp->offset = cpu_to_je32(ofs);
223                         temp->totlen = node->i.totlen;
224                         temp->next = NULL;
225
226                         return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp);
227                 }
228
229                 case JFFS2_NODETYPE_DIRENT: {
230                         struct jffs2_sum_dirent_mem *temp =
231                                 kmalloc(sizeof(struct jffs2_sum_dirent_mem) + node->d.nsize, GFP_KERNEL);
232
233                         if (!temp)
234                                 goto no_mem;
235
236                         temp->nodetype = node->d.nodetype;
237                         temp->totlen = node->d.totlen;
238                         temp->offset = cpu_to_je32(ofs);
239                         temp->pino = node->d.pino;
240                         temp->version = node->d.version;
241                         temp->ino = node->d.ino;
242                         temp->nsize = node->d.nsize;
243                         temp->type = node->d.type;
244                         temp->next = NULL;
245
246                         switch (count) {
247                                 case 1:
248                                         memcpy(temp->name,node->d.name,node->d.nsize);
249                                         break;
250
251                                 case 2:
252                                         memcpy(temp->name,invecs[1].iov_base,node->d.nsize);
253                                         break;
254
255                                 default:
256                                         BUG();  /* impossible count value */
257                                         break;
258                         }
259
260                         return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp);
261                 }
262
263                 case JFFS2_NODETYPE_PADDING:
264                         dbg_summary("node PADDING\n");
265                         c->summary->sum_padded += je32_to_cpu(node->u.totlen);
266                         break;
267
268                 case JFFS2_NODETYPE_CLEANMARKER:
269                         dbg_summary("node CLEANMARKER\n");
270                         break;
271
272                 case JFFS2_NODETYPE_SUMMARY:
273                         dbg_summary("node SUMMARY\n");
274                         break;
275
276                 default:
277                         /* If you implement a new node type you should also implement
278                            summary support for it or disable summary.
279                         */
280                         BUG();
281                         break;
282         }
283
284         return 0;
285
286 no_mem:
287         JFFS2_WARNING("MEMORY ALLOCATION ERROR!");
288         return -ENOMEM;
289 }
290
291
292 /* Process the stored summary information - helper function for jffs2_sum_scan_sumnode() */
293
294 static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
295                                 struct jffs2_raw_summary *summary, uint32_t *pseudo_random)
296 {
297         struct jffs2_raw_node_ref *raw;
298         struct jffs2_inode_cache *ic;
299         struct jffs2_full_dirent *fd;
300         void *sp;
301         int i, ino;
302
303         sp = summary->sum;
304
305         for (i=0; i<je32_to_cpu(summary->sum_num); i++) {
306                 dbg_summary("processing summary index %d\n", i);
307
308                 switch (je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype)) {
309                         case JFFS2_NODETYPE_INODE: {
310                                 struct jffs2_sum_inode_flash *spi;
311                                 spi = sp;
312
313                                 ino = je32_to_cpu(spi->inode);
314
315                                 dbg_summary("Inode at 0x%08x\n",
316                                                         jeb->offset + je32_to_cpu(spi->offset));
317
318                                 raw = jffs2_alloc_raw_node_ref();
319                                 if (!raw) {
320                                         JFFS2_NOTICE("allocation of node reference failed\n");
321                                         kfree(summary);
322                                         return -ENOMEM;
323                                 }
324
325                                 ic = jffs2_scan_make_ino_cache(c, ino);
326                                 if (!ic) {
327                                         JFFS2_NOTICE("scan_make_ino_cache failed\n");
328                                         jffs2_free_raw_node_ref(raw);
329                                         kfree(summary);
330                                         return -ENOMEM;
331                                 }
332
333                                 raw->flash_offset = (jeb->offset + je32_to_cpu(spi->offset)) | REF_UNCHECKED;
334                                 raw->__totlen = PAD(je32_to_cpu(spi->totlen));
335                                 raw->next_phys = NULL;
336                                 raw->next_in_ino = ic->nodes;
337
338                                 ic->nodes = raw;
339                                 if (!jeb->first_node)
340                                         jeb->first_node = raw;
341                                 if (jeb->last_node)
342                                         jeb->last_node->next_phys = raw;
343                                 jeb->last_node = raw;
344                                 *pseudo_random += je32_to_cpu(spi->version);
345
346                                 UNCHECKED_SPACE(PAD(je32_to_cpu(spi->totlen)));
347
348                                 sp += JFFS2_SUMMARY_INODE_SIZE;
349
350                                 break;
351                         }
352
353                         case JFFS2_NODETYPE_DIRENT: {
354                                 struct jffs2_sum_dirent_flash *spd;
355                                 spd = sp;
356
357                                 dbg_summary("Dirent at 0x%08x\n",
358                                                         jeb->offset + je32_to_cpu(spd->offset));
359
360                                 fd = jffs2_alloc_full_dirent(spd->nsize+1);
361                                 if (!fd) {
362                                         kfree(summary);
363                                         return -ENOMEM;
364                                 }
365
366                                 memcpy(&fd->name, spd->name, spd->nsize);
367                                 fd->name[spd->nsize] = 0;
368
369                                 raw = jffs2_alloc_raw_node_ref();
370                                 if (!raw) {
371                                         jffs2_free_full_dirent(fd);
372                                         JFFS2_NOTICE("allocation of node reference failed\n");
373                                         kfree(summary);
374                                         return -ENOMEM;
375                                 }
376
377                                 ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(spd->pino));
378                                 if (!ic) {
379                                         jffs2_free_full_dirent(fd);
380                                         jffs2_free_raw_node_ref(raw);
381                                         kfree(summary);
382                                         return -ENOMEM;
383                                 }
384
385                                 raw->__totlen = PAD(je32_to_cpu(spd->totlen));
386                                 raw->flash_offset = (jeb->offset + je32_to_cpu(spd->offset)) | REF_PRISTINE;
387                                 raw->next_phys = NULL;
388                                 raw->next_in_ino = ic->nodes;
389                                 ic->nodes = raw;
390                                 if (!jeb->first_node)
391                                         jeb->first_node = raw;
392                                 if (jeb->last_node)
393                                         jeb->last_node->next_phys = raw;
394                                 jeb->last_node = raw;
395
396                                 fd->raw = raw;
397                                 fd->next = NULL;
398                                 fd->version = je32_to_cpu(spd->version);
399                                 fd->ino = je32_to_cpu(spd->ino);
400                                 fd->nhash = full_name_hash(fd->name, spd->nsize);
401                                 fd->type = spd->type;
402                                 USED_SPACE(PAD(je32_to_cpu(spd->totlen)));
403                                 jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
404
405                                 *pseudo_random += je32_to_cpu(spd->version);
406
407                                 sp += JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize);
408
409                                 break;
410                         }
411
412                         default : {
413                                 JFFS2_WARNING("Unsupported node type found in summary! Exiting...");
414                                 kfree(summary);
415                                 return -EIO;
416                         }
417                 }
418         }
419
420         kfree(summary);
421         return 0;
422 }
423
424 /* Process the summary node - called from jffs2_scan_eraseblock() */
425
426 int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
427                                 uint32_t ofs, uint32_t *pseudo_random)
428 {
429         struct jffs2_unknown_node crcnode;
430         struct jffs2_raw_node_ref *cache_ref;
431         struct jffs2_raw_summary *summary;
432         int ret, sumsize;
433         uint32_t crc;
434
435         sumsize = c->sector_size - ofs;
436         ofs += jeb->offset;
437
438         dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
439                                 jeb->offset, ofs, sumsize);
440
441         summary = kmalloc(sumsize, GFP_KERNEL);
442
443         if (!summary) {
444                 return -ENOMEM;
445         }
446
447         ret = jffs2_fill_scan_buf(c, (unsigned char *)summary, ofs, sumsize);
448
449         if (ret) {
450                 kfree(summary);
451                 return ret;
452         }
453
454         /* OK, now check for node validity and CRC */
455         crcnode.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
456         crcnode.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY);
457         crcnode.totlen = summary->totlen;
458         crc = crc32(0, &crcnode, sizeof(crcnode)-4);
459
460         if (je32_to_cpu(summary->hdr_crc) != crc) {
461                 dbg_summary("Summary node header is corrupt (bad CRC or "
462                                 "no summary at all)\n");
463                 goto crc_err;
464         }
465
466         if (je32_to_cpu(summary->totlen) != sumsize) {
467                 dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
468                 goto crc_err;
469         }
470
471         crc = crc32(0, summary, sizeof(struct jffs2_raw_summary)-8);
472
473         if (je32_to_cpu(summary->node_crc) != crc) {
474                 dbg_summary("Summary node is corrupt (bad CRC)\n");
475                 goto crc_err;
476         }
477
478         crc = crc32(0, summary->sum, sumsize - sizeof(struct jffs2_raw_summary));
479
480         if (je32_to_cpu(summary->sum_crc) != crc) {
481                 dbg_summary("Summary node data is corrupt (bad CRC)\n");
482                 goto crc_err;
483         }
484
485         if ( je32_to_cpu(summary->cln_mkr) ) {
486
487                 dbg_summary("Summary : CLEANMARKER node \n");
488
489                 if (je32_to_cpu(summary->cln_mkr) != c->cleanmarker_size) {
490                         dbg_summary("CLEANMARKER node has totlen 0x%x != normal 0x%x\n",
491                                 je32_to_cpu(summary->cln_mkr), c->cleanmarker_size);
492                         UNCHECKED_SPACE(PAD(je32_to_cpu(summary->cln_mkr)));
493                 } else if (jeb->first_node) {
494                         dbg_summary("CLEANMARKER node not first node in block "
495                                         "(0x%08x)\n", jeb->offset);
496                         UNCHECKED_SPACE(PAD(je32_to_cpu(summary->cln_mkr)));
497                 } else {
498                         struct jffs2_raw_node_ref *marker_ref = jffs2_alloc_raw_node_ref();
499
500                         if (!marker_ref) {
501                                 JFFS2_NOTICE("Failed to allocate node ref for clean marker\n");
502                                 kfree(summary);
503                                 return -ENOMEM;
504                         }
505
506                         marker_ref->next_in_ino = NULL;
507                         marker_ref->next_phys = NULL;
508                         marker_ref->flash_offset = jeb->offset | REF_NORMAL;
509                         marker_ref->__totlen = je32_to_cpu(summary->cln_mkr);
510                         jeb->first_node = jeb->last_node = marker_ref;
511
512                         USED_SPACE( PAD(je32_to_cpu(summary->cln_mkr)) );
513                 }
514         }
515
516         if (je32_to_cpu(summary->padded)) {
517                 DIRTY_SPACE(je32_to_cpu(summary->padded));
518         }
519
520         ret = jffs2_sum_process_sum_data(c, jeb, summary, pseudo_random);
521         if (ret)
522                 return ret;
523
524         /* for PARANOIA_CHECK */
525         cache_ref = jffs2_alloc_raw_node_ref();
526
527         if (!cache_ref) {
528                 JFFS2_NOTICE("Failed to allocate node ref for cache\n");
529                 return -ENOMEM;
530         }
531
532         cache_ref->next_in_ino = NULL;
533         cache_ref->next_phys = NULL;
534         cache_ref->flash_offset = ofs | REF_NORMAL;
535         cache_ref->__totlen = sumsize;
536
537         if (!jeb->first_node)
538                 jeb->first_node = cache_ref;
539         if (jeb->last_node)
540                 jeb->last_node->next_phys = cache_ref;
541         jeb->last_node = cache_ref;
542
543         USED_SPACE(sumsize);
544
545         jeb->wasted_size += jeb->free_size;
546         c->wasted_size += jeb->free_size;
547         c->free_size -= jeb->free_size;
548         jeb->free_size = 0;
549
550         return jffs2_scan_classify_jeb(c, jeb);
551
552 crc_err:
553         JFFS2_WARNING("Summary node crc error, skipping summary information.\n");
554
555         return 0;
556 }
557
558 /* Write summary data to flash - helper function for jffs2_sum_write_sumnode() */
559
560 static int jffs2_sum_write_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
561                                         uint32_t infosize, uint32_t datasize, int padsize)
562 {
563         struct jffs2_raw_summary isum;
564         union jffs2_sum_mem *temp;
565         struct jffs2_sum_marker *sm;
566         struct kvec vecs[2];
567         void *wpage;
568         int ret;
569         size_t retlen;
570
571         memset(c->summary->sum_buf, 0xff, datasize);
572         memset(&isum, 0, sizeof(isum));
573
574         isum.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
575         isum.nodetype = cpu_to_je16(JFFS2_NODETYPE_SUMMARY);
576         isum.totlen = cpu_to_je32(infosize);
577         isum.hdr_crc = cpu_to_je32(crc32(0, &isum, sizeof(struct jffs2_unknown_node) - 4));
578         isum.padded = cpu_to_je32(c->summary->sum_padded);
579         isum.cln_mkr = cpu_to_je32(c->cleanmarker_size);
580         isum.sum_num = cpu_to_je32(c->summary->sum_num);
581         wpage = c->summary->sum_buf;
582
583         while (c->summary->sum_num) {
584
585                 switch (je16_to_cpu(c->summary->sum_list_head->u.nodetype)) {
586                         case JFFS2_NODETYPE_INODE: {
587                                 struct jffs2_sum_inode_flash *sino_ptr = wpage;
588
589                                 sino_ptr->nodetype = c->summary->sum_list_head->i.nodetype;
590                                 sino_ptr->inode = c->summary->sum_list_head->i.inode;
591                                 sino_ptr->version = c->summary->sum_list_head->i.version;
592                                 sino_ptr->offset = c->summary->sum_list_head->i.offset;
593                                 sino_ptr->totlen = c->summary->sum_list_head->i.totlen;
594
595                                 wpage += JFFS2_SUMMARY_INODE_SIZE;
596
597                                 break;
598                         }
599
600                         case JFFS2_NODETYPE_DIRENT: {
601                                 struct jffs2_sum_dirent_flash *sdrnt_ptr = wpage;
602
603                                 sdrnt_ptr->nodetype = c->summary->sum_list_head->d.nodetype;
604                                 sdrnt_ptr->totlen = c->summary->sum_list_head->d.totlen;
605                                 sdrnt_ptr->offset = c->summary->sum_list_head->d.offset;
606                                 sdrnt_ptr->pino = c->summary->sum_list_head->d.pino;
607                                 sdrnt_ptr->version = c->summary->sum_list_head->d.version;
608                                 sdrnt_ptr->ino = c->summary->sum_list_head->d.ino;
609                                 sdrnt_ptr->nsize = c->summary->sum_list_head->d.nsize;
610                                 sdrnt_ptr->type = c->summary->sum_list_head->d.type;
611
612                                 memcpy(sdrnt_ptr->name, c->summary->sum_list_head->d.name,
613                                                         c->summary->sum_list_head->d.nsize);
614
615                                 wpage += JFFS2_SUMMARY_DIRENT_SIZE(c->summary->sum_list_head->d.nsize);
616
617                                 break;
618                         }
619
620                         default : {
621                                 BUG();  /* unknown node in summary information */
622                         }
623                 }
624
625                 temp = c->summary->sum_list_head;
626                 c->summary->sum_list_head = c->summary->sum_list_head->u.next;
627                 kfree(temp);
628
629                 c->summary->sum_num--;
630         }
631
632         jffs2_sum_reset_collected(c->summary);
633
634         wpage += padsize;
635
636         sm = wpage;
637         sm->offset = cpu_to_je32(c->sector_size - jeb->free_size);
638         sm->magic = cpu_to_je32(JFFS2_SUM_MAGIC);
639
640         isum.sum_crc = cpu_to_je32(crc32(0, c->summary->sum_buf, datasize));
641         isum.node_crc = cpu_to_je32(crc32(0, &isum, sizeof(isum) - 8));
642
643         vecs[0].iov_base = &isum;
644         vecs[0].iov_len = sizeof(isum);
645         vecs[1].iov_base = c->summary->sum_buf;
646         vecs[1].iov_len = datasize;
647
648         dbg_summary("JFFS2: writing out data to flash to pos : 0x%08x\n",
649                         jeb->offset + c->sector_size - jeb->free_size);
650
651         spin_unlock(&c->erase_completion_lock);
652         ret = jffs2_flash_writev(c, vecs, 2, jeb->offset + c->sector_size -
653                                 jeb->free_size, &retlen, 0);
654         spin_lock(&c->erase_completion_lock);
655
656
657         if (ret || (retlen != infosize)) {
658                 JFFS2_WARNING("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
659                         infosize, jeb->offset + c->sector_size - jeb->free_size, ret, retlen);
660
661                 c->summary->sum_size = JFFS2_SUMMARY_NOSUM_SIZE;
662                 WASTED_SPACE(infosize);
663
664                 return 1;
665         }
666
667         return 0;
668 }
669
670 /* Write out summary information - called from jffs2_do_reserve_space */
671
672 int jffs2_sum_write_sumnode(struct jffs2_sb_info *c)
673 {
674         struct jffs2_raw_node_ref *summary_ref;
675         int datasize, infosize, padsize, ret;
676         struct jffs2_eraseblock *jeb;
677
678         dbg_summary("called\n");
679
680         jeb = c->nextblock;
681
682         if (!c->summary->sum_num || !c->summary->sum_list_head) {
683                 JFFS2_WARNING("Empty summary info!!!\n");
684                 BUG();
685         }
686
687         datasize = c->summary->sum_size + sizeof(struct jffs2_sum_marker);
688         infosize = sizeof(struct jffs2_raw_summary) + datasize;
689         padsize = jeb->free_size - infosize;
690         infosize += padsize;
691         datasize += padsize;
692
693         /* Is there enough space for summary? */
694         if (padsize < 0) {
695                 /* don't try to write out summary for this jeb */
696                 jffs2_sum_disable_collecting(c->summary);
697
698                 JFFS2_WARNING("Not enough space for summary, padsize = %d\n", padsize);
699                 return 0;
700         }
701
702         ret = jffs2_sum_write_data(c, jeb, infosize, datasize, padsize);
703         if (ret)
704                 return 0; /* can't write out summary, block is marked as NOSUM_SIZE */
705
706         /* for ACCT_PARANOIA_CHECK */
707         spin_unlock(&c->erase_completion_lock);
708         summary_ref = jffs2_alloc_raw_node_ref();
709         spin_lock(&c->erase_completion_lock);
710
711         if (!summary_ref) {
712                 JFFS2_NOTICE("Failed to allocate node ref for summary\n");
713                 return -ENOMEM;
714         }
715
716         summary_ref->next_in_ino = NULL;
717         summary_ref->next_phys = NULL;
718         summary_ref->flash_offset = (jeb->offset + c->sector_size - jeb->free_size) | REF_NORMAL;
719         summary_ref->__totlen = infosize;
720
721         if (!jeb->first_node)
722                 jeb->first_node = summary_ref;
723         if (jeb->last_node)
724                 jeb->last_node->next_phys = summary_ref;
725         jeb->last_node = summary_ref;
726
727         USED_SPACE(infosize);
728
729         return 0;
730 }