ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / jffs2 / wbuf.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001-2003 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@redhat.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: wbuf.c,v 1.53 2003/10/11 11:46:09 dwmw2 Exp $
11  *
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/crc32.h>
18 #include <linux/mtd/nand.h>
19 #include "nodelist.h"
20
21 /* For testing write failures */
22 #undef BREAKME
23 #undef BREAKMEHEADER
24
25 #ifdef BREAKME
26 static unsigned char *brokenbuf;
27 #endif
28
29 /* max. erase failures before we mark a block bad */
30 #define MAX_ERASE_FAILURES      5
31
32 /* two seconds timeout for timed wbuf-flushing */
33 #define WBUF_FLUSH_TIMEOUT      2 * HZ
34
35 struct jffs2_inodirty {
36         uint32_t ino;
37         struct jffs2_inodirty *next;
38 };
39
40 static struct jffs2_inodirty inodirty_nomem;
41
42 static int jffs2_wbuf_pending_for_ino(struct jffs2_sb_info *c, uint32_t ino)
43 {
44         struct jffs2_inodirty *this = c->wbuf_inodes;
45
46         /* If a malloc failed, consider _everything_ dirty */
47         if (this == &inodirty_nomem)
48                 return 1;
49
50         /* If ino == 0, _any_ non-GC writes mean 'yes' */
51         if (this && !ino)
52                 return 1;
53
54         /* Look to see if the inode in question is pending in the wbuf */
55         while (this) {
56                 if (this->ino == ino)
57                         return 1;
58                 this = this->next;
59         }
60         return 0;
61 }
62
63 static void jffs2_clear_wbuf_ino_list(struct jffs2_sb_info *c)
64 {
65         struct jffs2_inodirty *this;
66
67         this = c->wbuf_inodes;
68
69         if (this != &inodirty_nomem) {
70                 while (this) {
71                         struct jffs2_inodirty *next = this->next;
72                         kfree(this);
73                         this = next;
74                 }
75         }
76         c->wbuf_inodes = NULL;
77 }
78
79 static void jffs2_wbuf_dirties_inode(struct jffs2_sb_info *c, uint32_t ino)
80 {
81         struct jffs2_inodirty *new;
82
83         /* Mark the superblock dirty so that kupdated will flush... */
84         OFNI_BS_2SFFJ(c)->s_dirt = 1;
85
86         if (jffs2_wbuf_pending_for_ino(c, ino))
87                 return;
88
89         new = kmalloc(sizeof(*new), GFP_KERNEL);
90         if (!new) {
91                 D1(printk(KERN_DEBUG "No memory to allocate inodirty. Fallback to all considered dirty\n"));
92                 jffs2_clear_wbuf_ino_list(c);
93                 c->wbuf_inodes = &inodirty_nomem;
94                 return;
95         }
96         new->ino = ino;
97         new->next = c->wbuf_inodes;
98         c->wbuf_inodes = new;
99         return;
100 }
101
102 static inline void jffs2_refile_wbuf_blocks(struct jffs2_sb_info *c)
103 {
104         struct list_head *this, *next;
105         static int n;
106
107         if (list_empty(&c->erasable_pending_wbuf_list))
108                 return;
109
110         list_for_each_safe(this, next, &c->erasable_pending_wbuf_list) {
111                 struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
112
113                 D1(printk(KERN_DEBUG "Removing eraseblock at 0x%08x from erasable_pending_wbuf_list...\n", jeb->offset));
114                 list_del(this);
115                 if ((jiffies + (n++)) & 127) {
116                         /* Most of the time, we just erase it immediately. Otherwise we
117                            spend ages scanning it on mount, etc. */
118                         D1(printk(KERN_DEBUG "...and adding to erase_pending_list\n"));
119                         list_add_tail(&jeb->list, &c->erase_pending_list);
120                         c->nr_erasing_blocks++;
121                         jffs2_erase_pending_trigger(c);
122                 } else {
123                         /* Sometimes, however, we leave it elsewhere so it doesn't get
124                            immediately reused, and we spread the load a bit. */
125                         D1(printk(KERN_DEBUG "...and adding to erasable_list\n"));
126                         list_add_tail(&jeb->list, &c->erasable_list);
127                 }
128         }
129 }
130
131 /* Recover from failure to write wbuf. Recover the nodes up to the
132  * wbuf, not the one which we were starting to try to write. */
133
134 static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
135 {
136         struct jffs2_eraseblock *jeb, *new_jeb;
137         struct jffs2_raw_node_ref **first_raw, **raw;
138         size_t retlen;
139         int ret;
140         unsigned char *buf;
141         uint32_t start, end, ofs, len;
142
143         spin_lock(&c->erase_completion_lock);
144
145         jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
146
147         D1(printk("About to refile bad block at %08x\n", jeb->offset));
148
149         D2(jffs2_dump_block_lists(c));
150         /* File the existing block on the bad_used_list.... */
151         if (c->nextblock == jeb)
152                 c->nextblock = NULL;
153         else /* Not sure this should ever happen... need more coffee */
154                 list_del(&jeb->list);
155         if (jeb->first_node) {
156                 D1(printk("Refiling block at %08x to bad_used_list\n", jeb->offset));
157                 list_add(&jeb->list, &c->bad_used_list);
158         } else {
159                 BUG();
160                 /* It has to have had some nodes or we couldn't be here */
161                 D1(printk("Refiling block at %08x to erase_pending_list\n", jeb->offset));
162                 list_add(&jeb->list, &c->erase_pending_list);
163                 c->nr_erasing_blocks++;
164                 jffs2_erase_pending_trigger(c);
165         }
166         D2(jffs2_dump_block_lists(c));
167
168         /* Adjust its size counts accordingly */
169         c->wasted_size += jeb->free_size;
170         c->free_size -= jeb->free_size;
171         jeb->wasted_size += jeb->free_size;
172         jeb->free_size = 0;
173
174         ACCT_SANITY_CHECK(c,jeb);
175         D1(ACCT_PARANOIA_CHECK(jeb));
176
177         /* Find the first node to be recovered, by skipping over every
178            node which ends before the wbuf starts, or which is obsolete. */
179         first_raw = &jeb->first_node;
180         while (*first_raw && 
181                (ref_obsolete(*first_raw) ||
182                 (ref_offset(*first_raw) + (*first_raw)->totlen) < c->wbuf_ofs)) {
183                 D1(printk(KERN_DEBUG "Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n",
184                           ref_offset(*first_raw), ref_flags(*first_raw),
185                           (ref_offset(*first_raw) + (*first_raw)->totlen),
186                           c->wbuf_ofs));
187                 first_raw = &(*first_raw)->next_phys;
188         }
189
190         if (!*first_raw) {
191                 /* All nodes were obsolete. Nothing to recover. */
192                 D1(printk(KERN_DEBUG "No non-obsolete nodes to be recovered. Just filing block bad\n"));
193                 spin_unlock(&c->erase_completion_lock);
194                 return;
195         }
196
197         start = ref_offset(*first_raw);
198         end = ref_offset(*first_raw) + (*first_raw)->totlen;
199
200         /* Find the last node to be recovered */
201         raw = first_raw;
202         while ((*raw)) {
203                 if (!ref_obsolete(*raw))
204                         end = ref_offset(*raw) + (*raw)->totlen;
205
206                 raw = &(*raw)->next_phys;
207         }
208         spin_unlock(&c->erase_completion_lock);
209
210         D1(printk(KERN_DEBUG "wbuf recover %08x-%08x\n", start, end));
211
212         buf = NULL;
213         if (start < c->wbuf_ofs) {
214                 /* First affected node was already partially written.
215                  * Attempt to reread the old data into our buffer. */
216
217                 buf = kmalloc(end - start, GFP_KERNEL);
218                 if (!buf) {
219                         printk(KERN_CRIT "Malloc failure in wbuf recovery. Data loss ensues.\n");
220
221                         goto read_failed;
222                 }
223
224                 /* Do the read... */
225                 ret = c->mtd->read_ecc(c->mtd, start, c->wbuf_ofs - start, &retlen, buf, NULL, c->oobinfo);
226                 if (ret == -EIO && retlen == c->wbuf_ofs - start) {
227                         /* ECC recovered */
228                         ret = 0;
229                 }
230                 if (ret || retlen != c->wbuf_ofs - start) {
231                         printk(KERN_CRIT "Old data are already lost in wbuf recovery. Data loss ensues.\n");
232
233                         kfree(buf);
234                         buf = NULL;
235                 read_failed:
236                         first_raw = &(*first_raw)->next_phys;
237                         /* If this was the only node to be recovered, give up */
238                         if (!(*first_raw))
239                                 return;
240
241                         /* It wasn't. Go on and try to recover nodes complete in the wbuf */
242                         start = ref_offset(*first_raw);
243                 } else {
244                         /* Read succeeded. Copy the remaining data from the wbuf */
245                         memcpy(buf + (c->wbuf_ofs - start), c->wbuf, end - c->wbuf_ofs);
246                 }
247         }
248         /* OK... we're to rewrite (end-start) bytes of data from first_raw onwards.
249            Either 'buf' contains the data, or we find it in the wbuf */
250
251
252         /* ... and get an allocation of space from a shiny new block instead */
253         ret = jffs2_reserve_space_gc(c, end-start, &ofs, &len);
254         if (ret) {
255                 printk(KERN_WARNING "Failed to allocate space for wbuf recovery. Data loss ensues.\n");
256                 if (buf)
257                         kfree(buf);
258                 return;
259         }
260         if (end-start >= c->wbuf_pagesize) {
261                 /* Need to do another write immediately. This, btw,
262                  means that we'll be writing from 'buf' and not from
263                  the wbuf. Since if we're writing from the wbuf there
264                  won't be more than a wbuf full of data, now will
265                  there? :) */
266
267                 uint32_t towrite = (end-start) - ((end-start)%c->wbuf_pagesize);
268
269                 D1(printk(KERN_DEBUG "Write 0x%x bytes at 0x%08x in wbuf recover\n",
270                           towrite, ofs));
271           
272 #ifdef BREAKMEHEADER
273                 static int breakme;
274                 if (breakme++ == 20) {
275                         printk(KERN_NOTICE "Faking write error at 0x%08x\n", ofs);
276                         breakme = 0;
277                         c->mtd->write_ecc(c->mtd, ofs, towrite, &retlen,
278                                           brokenbuf, NULL, c->oobinfo);
279                         ret = -EIO;
280                 } else
281 #endif
282                         ret = c->mtd->write_ecc(c->mtd, ofs, towrite, &retlen,
283                                                 buf, NULL, c->oobinfo);
284
285                 if (ret || retlen != towrite) {
286                         /* Argh. We tried. Really we did. */
287                         printk(KERN_CRIT "Recovery of wbuf failed due to a second write error\n");
288                         kfree(buf);
289
290                         if (retlen) {
291                                 struct jffs2_raw_node_ref *raw2;
292
293                                 raw2 = jffs2_alloc_raw_node_ref();
294                                 if (!raw2)
295                                         return;
296
297                                 raw2->flash_offset = ofs | REF_OBSOLETE;
298                                 raw2->totlen = (*first_raw)->totlen;
299                                 raw2->next_phys = NULL;
300                                 raw2->next_in_ino = NULL;
301
302                                 jffs2_add_physical_node_ref(c, raw2);
303                         }
304                         return;
305                 }
306                 printk(KERN_NOTICE "Recovery of wbuf succeeded to %08x\n", ofs);
307
308                 c->wbuf_len = (end - start) - towrite;
309                 c->wbuf_ofs = ofs + towrite;
310                 memcpy(c->wbuf, buf + towrite, c->wbuf_len);
311                 /* Don't muck about with c->wbuf_inodes. False positives are harmless. */
312
313                 kfree(buf);
314         } else {
315                 /* OK, now we're left with the dregs in whichever buffer we're using */
316                 if (buf) {
317                         memcpy(c->wbuf, buf, end-start);
318                         kfree(buf);
319                 } else {
320                         memmove(c->wbuf, c->wbuf + (start - c->wbuf_ofs), end - start);
321                 }
322                 c->wbuf_ofs = ofs;
323                 c->wbuf_len = end - start;
324         }
325
326         /* Now sort out the jffs2_raw_node_refs, moving them from the old to the next block */
327         new_jeb = &c->blocks[ofs / c->sector_size];
328
329         spin_lock(&c->erase_completion_lock);
330         if (new_jeb->first_node) {
331                 /* Odd, but possible with ST flash later maybe */
332                 new_jeb->last_node->next_phys = *first_raw;
333         } else {
334                 new_jeb->first_node = *first_raw;
335         }
336
337         raw = first_raw;
338         while (*raw) {
339                 D1(printk(KERN_DEBUG "Refiling block of %08x at %08x(%d) to %08x\n",
340                           (*raw)->totlen, ref_offset(*raw), ref_flags(*raw), ofs));
341
342                 if (ref_obsolete(*raw)) {
343                         /* Shouldn't really happen much */
344                         new_jeb->dirty_size += (*raw)->totlen;
345                         new_jeb->free_size -= (*raw)->totlen;
346                         c->dirty_size += (*raw)->totlen;
347                 } else {
348                         new_jeb->used_size += (*raw)->totlen;
349                         new_jeb->free_size -= (*raw)->totlen;
350                         jeb->dirty_size += (*raw)->totlen;
351                         jeb->used_size  -= (*raw)->totlen;
352                         c->dirty_size += (*raw)->totlen;
353                 }
354                 c->free_size -= (*raw)->totlen;
355                 (*raw)->flash_offset = ofs | ref_flags(*raw);
356                 ofs += (*raw)->totlen;
357                 new_jeb->last_node = *raw;
358
359                 raw = &(*raw)->next_phys;
360         }
361
362         /* Fix up the original jeb now it's on the bad_list */
363         *first_raw = NULL;
364         if (first_raw == &jeb->first_node) {
365                 jeb->last_node = NULL;
366                 D1(printk(KERN_DEBUG "Failing block at %08x is now empty. Moving to erase_pending_list\n", jeb->offset));
367                 list_del(&jeb->list);
368                 list_add(&jeb->list, &c->erase_pending_list);
369                 c->nr_erasing_blocks++;
370                 jffs2_erase_pending_trigger(c);
371         }
372         else
373                 jeb->last_node = container_of(first_raw, struct jffs2_raw_node_ref, next_phys);
374
375         ACCT_SANITY_CHECK(c,jeb);
376         D1(ACCT_PARANOIA_CHECK(jeb));
377
378         ACCT_SANITY_CHECK(c,new_jeb);
379         D1(ACCT_PARANOIA_CHECK(new_jeb));
380
381         spin_unlock(&c->erase_completion_lock);
382
383         D1(printk(KERN_DEBUG "wbuf recovery completed OK\n"));
384 }
385
386 /* Meaning of pad argument:
387    0: Do not pad. Probably pointless - we only ever use this when we can't pad anyway.
388    1: Pad, do not adjust nextblock free_size
389    2: Pad, adjust nextblock free_size
390 */
391 static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
392 {
393         int ret;
394         size_t retlen;
395
396         /* Nothing to do if not NAND flash. In particular, we shouldn't
397            del_timer() the timer we never initialised. */
398         if (jffs2_can_mark_obsolete(c))
399                 return 0;
400
401         if (!down_trylock(&c->alloc_sem)) {
402                 up(&c->alloc_sem);
403                 printk(KERN_CRIT "jffs2_flush_wbuf() called with alloc_sem not locked!\n");
404                 BUG();
405         }
406
407         if(!c->wbuf || !c->wbuf_len)
408                 return 0;
409
410         /* claim remaining space on the page
411            this happens, if we have a change to a new block,
412            or if fsync forces us to flush the writebuffer.
413            if we have a switch to next page, we will not have
414            enough remaining space for this. 
415         */
416         if (pad) {
417                 c->wbuf_len = PAD(c->wbuf_len);
418                 
419                 if ( c->wbuf_len + sizeof(struct jffs2_unknown_node) < c->wbuf_pagesize) {
420                         struct jffs2_unknown_node *padnode = (void *)(c->wbuf + c->wbuf_len);
421                         padnode->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
422                         padnode->nodetype = cpu_to_je16(JFFS2_NODETYPE_PADDING);
423                         padnode->totlen = cpu_to_je32(c->wbuf_pagesize - c->wbuf_len);
424                         padnode->hdr_crc = cpu_to_je32(crc32(0, padnode, sizeof(*padnode)-4));
425                 }
426         }
427         /* else jffs2_flash_writev has actually filled in the rest of the
428            buffer for us, and will deal with the node refs etc. later. */
429         
430 #ifdef BREAKME
431         static int breakme;
432         if (breakme++ == 20) {
433                 printk(KERN_NOTICE "Faking write error at 0x%08x\n", c->wbuf_ofs);
434                 breakme = 0;
435                 c->mtd->write_ecc(c->mtd, c->wbuf_ofs, c->wbuf_pagesize,
436                                         &retlen, brokenbuf, NULL, c->oobinfo);
437                 ret = -EIO;
438         } else 
439 #endif
440         ret = c->mtd->write_ecc(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen, c->wbuf, NULL, c->oobinfo);
441
442
443         if (ret || retlen != c->wbuf_pagesize) {
444                 if (ret)
445                         printk(KERN_WARNING "jffs2_flush_wbuf(): Write failed with %d\n",ret);
446                 else {
447                         printk(KERN_WARNING "jffs2_flush_wbuf(): Write was short: %zd instead of %d\n",
448                                 retlen, c->wbuf_pagesize);
449                         ret = -EIO;
450                 }
451
452                 jffs2_wbuf_recover(c);
453
454                 return ret; 
455         }
456
457         /* Adjusting free size of next block only, if it's called from fsync ! */
458         if (pad == 2) {
459                 D1(printk(KERN_DEBUG "jffs2_flush_wbuf() adjusting free_size of c->nextblock\n"));
460                 spin_lock(&c->erase_completion_lock);
461                 if (!c->nextblock)
462                         BUG();
463                 /* wbuf_pagesize - wbuf_len is the amount of space that's to be 
464                    padded. If there is less free space in the block than that,
465                    something screwed up */
466                 if (c->nextblock->free_size < (c->wbuf_pagesize - c->wbuf_len)) {
467                         printk(KERN_CRIT "jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n",
468                                c->wbuf_ofs, c->wbuf_len, c->wbuf_pagesize-c->wbuf_len);
469                         printk(KERN_CRIT "jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n",
470                                c->nextblock->offset, c->nextblock->free_size);
471                         BUG();
472                 }
473                 c->nextblock->free_size -= (c->wbuf_pagesize - c->wbuf_len);
474                 c->free_size -= (c->wbuf_pagesize - c->wbuf_len);
475                 c->nextblock->wasted_size += (c->wbuf_pagesize - c->wbuf_len);
476                 c->wasted_size += (c->wbuf_pagesize - c->wbuf_len);
477                 spin_unlock(&c->erase_completion_lock);
478         }
479
480         /* Stick any now-obsoleted blocks on the erase_pending_list */
481         spin_lock(&c->erase_completion_lock);
482         jffs2_refile_wbuf_blocks(c);
483         jffs2_clear_wbuf_ino_list(c);
484         spin_unlock(&c->erase_completion_lock);
485
486         memset(c->wbuf,0xff,c->wbuf_pagesize);
487         /* adjust write buffer offset, else we get a non contiguous write bug */
488         c->wbuf_ofs += c->wbuf_pagesize;
489         c->wbuf_len = 0;
490         return 0;
491 }
492
493 /* Trigger garbage collection to flush the write-buffer. 
494    If ino arg is zero, do it if _any_ real (i.e. not GC) writes are
495    outstanding. If ino arg non-zero, do it only if a write for the 
496    given inode is outstanding. */
497 int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino)
498 {
499         uint32_t old_wbuf_ofs;
500         uint32_t old_wbuf_len;
501         int ret = 0;
502
503         D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() called for ino #%u...\n", ino));
504
505         down(&c->alloc_sem);
506         if (!jffs2_wbuf_pending_for_ino(c, ino)) {
507                 D1(printk(KERN_DEBUG "Ino #%d not pending in wbuf. Returning\n", ino));
508                 up(&c->alloc_sem);
509                 return 0;
510         }
511
512         old_wbuf_ofs = c->wbuf_ofs;
513         old_wbuf_len = c->wbuf_len;
514
515         while (old_wbuf_len &&
516                old_wbuf_ofs == c->wbuf_ofs) {
517
518                 up(&c->alloc_sem);
519
520                 D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() calls gc pass\n"));
521
522                 ret = jffs2_garbage_collect_pass(c);
523                 if (ret) {
524                         /* GC failed. Flush it with padding instead */
525                         down(&c->alloc_sem);
526                         ret = __jffs2_flush_wbuf(c, 2);
527                         break;
528                 }
529                 down(&c->alloc_sem);
530         }
531
532         D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() ends...\n"));
533
534         up(&c->alloc_sem);
535         return ret;
536 }
537
538 /* Pad write-buffer to end and write it, wasting space. */
539 int jffs2_flush_wbuf_pad(struct jffs2_sb_info *c)
540 {
541         return __jffs2_flush_wbuf(c, 1);
542 }
543
544
545 #define PAGE_DIV(x) ( (x) & (~(c->wbuf_pagesize - 1)) )
546 #define PAGE_MOD(x) ( (x) & (c->wbuf_pagesize - 1) )
547 int jffs2_flash_writev(struct jffs2_sb_info *c, const struct iovec *invecs, unsigned long count, loff_t to, size_t *retlen, uint32_t ino)
548 {
549         struct iovec outvecs[3];
550         uint32_t totlen = 0;
551         uint32_t split_ofs = 0;
552         uint32_t old_totlen;
553         int ret, splitvec = -1;
554         int invec, outvec;
555         size_t wbuf_retlen;
556         unsigned char *wbuf_ptr;
557         size_t donelen = 0;
558         uint32_t outvec_to = to;
559
560         /* If not NAND flash, don't bother */
561         if (!c->wbuf)
562                 return jffs2_flash_direct_writev(c, invecs, count, to, retlen);
563         
564         /* If wbuf_ofs is not initialized, set it to target address */
565         if (c->wbuf_ofs == 0xFFFFFFFF) {
566                 c->wbuf_ofs = PAGE_DIV(to);
567                 c->wbuf_len = PAGE_MOD(to);                     
568                 memset(c->wbuf,0xff,c->wbuf_pagesize);
569         }
570
571         /* Sanity checks on target address. 
572            It's permitted to write at PAD(c->wbuf_len+c->wbuf_ofs), 
573            and it's permitted to write at the beginning of a new 
574            erase block. Anything else, and you die.
575            New block starts at xxx000c (0-b = block header)
576         */
577         if ( (to & ~(c->sector_size-1)) != (c->wbuf_ofs & ~(c->sector_size-1)) ) {
578                 /* It's a write to a new block */
579                 if (c->wbuf_len) {
580                         D1(printk(KERN_DEBUG "jffs2_flash_writev() to 0x%lx causes flush of wbuf at 0x%08x\n", (unsigned long)to, c->wbuf_ofs));
581                         ret = jffs2_flush_wbuf_pad(c);
582                         if (ret) {
583                                 /* the underlying layer has to check wbuf_len to do the cleanup */
584                                 D1(printk(KERN_WARNING "jffs2_flush_wbuf() called from jffs2_flash_writev() failed %d\n", ret));
585                                 *retlen = 0;
586                                 return ret;
587                         }
588                 }
589                 /* set pointer to new block */
590                 c->wbuf_ofs = PAGE_DIV(to);
591                 c->wbuf_len = PAGE_MOD(to);                     
592         } 
593
594         if (to != PAD(c->wbuf_ofs + c->wbuf_len)) {
595                 /* We're not writing immediately after the writebuffer. Bad. */
596                 printk(KERN_CRIT "jffs2_flash_writev(): Non-contiguous write to %08lx\n", (unsigned long)to);
597                 if (c->wbuf_len)
598                         printk(KERN_CRIT "wbuf was previously %08x-%08x\n",
599                                           c->wbuf_ofs, c->wbuf_ofs+c->wbuf_len);
600                 BUG();
601         }
602
603         /* Note outvecs[3] above. We know count is never greater than 2 */
604         if (count > 2) {
605                 printk(KERN_CRIT "jffs2_flash_writev(): count is %ld\n", count);
606                 BUG();
607         }
608
609         invec = 0;
610         outvec = 0;
611
612
613         /* Fill writebuffer first, if already in use */ 
614         if (c->wbuf_len) {
615                 uint32_t invec_ofs = 0;
616
617                 /* adjust alignment offset */ 
618                 if (c->wbuf_len != PAGE_MOD(to)) {
619                         c->wbuf_len = PAGE_MOD(to);
620                         /* take care of alignment to next page */
621                         if (!c->wbuf_len)
622                                 c->wbuf_len = c->wbuf_pagesize;
623                 }
624                 
625                 while(c->wbuf_len < c->wbuf_pagesize) {
626                         uint32_t thislen;
627                         
628                         if (invec == count)
629                                 goto alldone;
630
631                         thislen = c->wbuf_pagesize - c->wbuf_len;
632
633                         if (thislen >= invecs[invec].iov_len)
634                                 thislen = invecs[invec].iov_len;
635         
636                         invec_ofs = thislen;
637
638                         memcpy(c->wbuf + c->wbuf_len, invecs[invec].iov_base, thislen);
639                         c->wbuf_len += thislen;
640                         donelen += thislen;
641                         /* Get next invec, if actual did not fill the buffer */
642                         if (c->wbuf_len < c->wbuf_pagesize) 
643                                 invec++;
644                 }                       
645                 
646                 /* write buffer is full, flush buffer */
647                 ret = __jffs2_flush_wbuf(c, 0);
648                 if (ret) {
649                         /* the underlying layer has to check wbuf_len to do the cleanup */
650                         D1(printk(KERN_WARNING "jffs2_flush_wbuf() called from jffs2_flash_writev() failed %d\n", ret));
651                         /* Retlen zero to make sure our caller doesn't mark the space dirty.
652                            We've already done everything that's necessary */
653                         *retlen = 0;
654                         return ret;
655                 }
656                 outvec_to += donelen;
657                 c->wbuf_ofs = outvec_to;
658
659                 /* All invecs done ? */
660                 if (invec == count)
661                         goto alldone;
662
663                 /* Set up the first outvec, containing the remainder of the
664                    invec we partially used */
665                 if (invecs[invec].iov_len > invec_ofs) {
666                         outvecs[0].iov_base = invecs[invec].iov_base+invec_ofs;
667                         totlen = outvecs[0].iov_len = invecs[invec].iov_len-invec_ofs;
668                         if (totlen > c->wbuf_pagesize) {
669                                 splitvec = outvec;
670                                 split_ofs = outvecs[0].iov_len - PAGE_MOD(totlen);
671                         }
672                         outvec++;
673                 }
674                 invec++;
675         }
676
677         /* OK, now we've flushed the wbuf and the start of the bits
678            we have been asked to write, now to write the rest.... */
679
680         /* totlen holds the amount of data still to be written */
681         old_totlen = totlen;
682         for ( ; invec < count; invec++,outvec++ ) {
683                 outvecs[outvec].iov_base = invecs[invec].iov_base;
684                 totlen += outvecs[outvec].iov_len = invecs[invec].iov_len;
685                 if (PAGE_DIV(totlen) != PAGE_DIV(old_totlen)) {
686                         splitvec = outvec;
687                         split_ofs = outvecs[outvec].iov_len - PAGE_MOD(totlen);
688                         old_totlen = totlen;
689                 }
690         }
691
692         /* Now the outvecs array holds all the remaining data to write */
693         /* Up to splitvec,split_ofs is to be written immediately. The rest
694            goes into the (now-empty) wbuf */
695
696         if (splitvec != -1) {
697                 uint32_t remainder;
698                 int ret;
699
700                 remainder = outvecs[splitvec].iov_len - split_ofs;
701                 outvecs[splitvec].iov_len = split_ofs;
702
703                 /* We did cross a page boundary, so we write some now */
704                 ret = c->mtd->writev_ecc(c->mtd, outvecs, splitvec+1, outvec_to, &wbuf_retlen, NULL, c->oobinfo); 
705                 if (ret < 0 || wbuf_retlen != PAGE_DIV(totlen)) {
706                         /* At this point we have no problem,
707                            c->wbuf is empty. 
708                         */
709                         *retlen = donelen;
710                         return ret;
711                 }
712                 
713                 donelen += wbuf_retlen;
714                 c->wbuf_ofs = PAGE_DIV(outvec_to) + PAGE_DIV(totlen);
715
716                 if (remainder) {
717                         outvecs[splitvec].iov_base += split_ofs;
718                         outvecs[splitvec].iov_len = remainder;
719                 } else {
720                         splitvec++;
721                 }
722
723         } else {
724                 splitvec = 0;
725         }
726
727         /* Now splitvec points to the start of the bits we have to copy
728            into the wbuf */
729         wbuf_ptr = c->wbuf;
730
731         for ( ; splitvec < outvec; splitvec++) {
732                 /* Don't copy the wbuf into itself */
733                 if (outvecs[splitvec].iov_base == c->wbuf)
734                         continue;
735                 memcpy(wbuf_ptr, outvecs[splitvec].iov_base, outvecs[splitvec].iov_len);
736                 wbuf_ptr += outvecs[splitvec].iov_len;
737                 donelen += outvecs[splitvec].iov_len;
738         }
739         c->wbuf_len = wbuf_ptr - c->wbuf;
740
741         /* If there's a remainder in the wbuf and it's a non-GC write,
742            remember that the wbuf affects this ino */
743 alldone:
744         *retlen = donelen;
745
746         if (c->wbuf_len && ino)
747                 jffs2_wbuf_dirties_inode(c, ino);
748
749         return 0;
750 }
751
752 /*
753  *      This is the entry for flash write.
754  *      Check, if we work on NAND FLASH, if so build an iovec and write it via vritev
755 */
756 int jffs2_flash_write(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *retlen, const u_char *buf)
757 {
758         struct iovec vecs[1];
759
760         if (jffs2_can_mark_obsolete(c))
761                 return c->mtd->write(c->mtd, ofs, len, retlen, buf);
762
763         vecs[0].iov_base = (unsigned char *) buf;
764         vecs[0].iov_len = len;
765         return jffs2_flash_writev(c, vecs, 1, ofs, retlen, 0);
766 }
767
768 /*
769         Handle readback from writebuffer and ECC failure return
770 */
771 int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *retlen, u_char *buf)
772 {
773         loff_t  orbf = 0, owbf = 0, lwbf = 0;
774         int     ret;
775
776         /* Read flash */
777         if (!jffs2_can_mark_obsolete(c)) {
778                 ret = c->mtd->read_ecc(c->mtd, ofs, len, retlen, buf, NULL, c->oobinfo);
779
780                 if ( (ret == -EIO) && (*retlen == len) ) {
781                         printk(KERN_WARNING "mtd->read(0x%zx bytes from 0x%llx) returned ECC error\n",
782                                len, ofs);
783                         /* 
784                          * We have the raw data without ECC correction in the buffer, maybe 
785                          * we are lucky and all data or parts are correct. We check the node.
786                          * If data are corrupted node check will sort it out.
787                          * We keep this block, it will fail on write or erase and the we
788                          * mark it bad. Or should we do that now? But we should give him a chance.
789                          * Maybe we had a system crash or power loss before the ecc write or  
790                          * a erase was completed.
791                          * So we return success. :)
792                          */
793                         ret = 0;
794                  }      
795         } else
796                 return c->mtd->read(c->mtd, ofs, len, retlen, buf);
797
798         /* if no writebuffer available or write buffer empty, return */
799         if (!c->wbuf_pagesize || !c->wbuf_len)
800                 return ret;
801
802         /* if we read in a different block, return */
803         if ( (ofs & ~(c->sector_size-1)) != (c->wbuf_ofs & ~(c->sector_size-1)) ) 
804                 return ret;     
805
806         if (ofs >= c->wbuf_ofs) {
807                 owbf = (ofs - c->wbuf_ofs);     /* offset in write buffer */
808                 if (owbf > c->wbuf_len)         /* is read beyond write buffer ? */
809                         return ret;
810                 lwbf = c->wbuf_len - owbf;      /* number of bytes to copy */
811                 if (lwbf > len) 
812                         lwbf = len;
813         } else {        
814                 orbf = (c->wbuf_ofs - ofs);     /* offset in read buffer */
815                 if (orbf > len)                 /* is write beyond write buffer ? */
816                         return ret;
817                 lwbf = len - orbf;              /* number of bytes to copy */
818                 if (lwbf > c->wbuf_len) 
819                         lwbf = c->wbuf_len;
820         }       
821         if (lwbf > 0)
822                 memcpy(buf+orbf,c->wbuf+owbf,lwbf);
823
824         return ret;
825 }
826
827 /*
828  *      Check, if the out of band area is empty
829  */
830 int jffs2_check_oob_empty( struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int mode)
831 {
832         unsigned char *buf;
833         int     ret = 0;
834         int     i,len,page;
835         size_t  retlen;
836         int     oob_size;
837
838         oob_size = c->mtd->oobsize;
839
840         /* allocate a buffer for all oob data in this sector */
841         len = 4 * oob_size;
842         buf = kmalloc(len, GFP_KERNEL);
843         if (!buf) {
844                 printk(KERN_NOTICE "jffs2_check_oob_empty(): allocation of temporary data buffer for oob check failed\n");
845                 return -ENOMEM;
846         }
847         /* 
848          * if mode = 0, we scan for a total empty oob area, else we have
849          * to take care of the cleanmarker in the first page of the block
850         */
851         ret = jffs2_flash_read_oob(c, jeb->offset, len , &retlen, buf);
852         if (ret) {
853                 D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB failed %d for block at %08x\n", ret, jeb->offset));
854                 goto out;
855         }
856         
857         if (retlen < len) {
858                 D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB return short read "
859                           "(%zd bytes not %d) for block at %08x\n", retlen, len, jeb->offset));
860                 ret = -EIO;
861                 goto out;
862         }
863         
864         /* Special check for first two pages */
865         for (page = 0; page < 2 * oob_size; page += oob_size) {
866                 /* Check for bad block marker */
867                 if (buf[page+c->badblock_pos] != 0xff) {
868                         D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Bad or failed block at %08x\n",jeb->offset));
869                         /* Return 2 for bad and 3 for failed block 
870                            bad goes to list_bad and failed to list_erase */
871                         ret = (!page) ? 2 : 3;
872                         goto out;
873                 }
874                 for(i = 0; i < oob_size ; i++) {
875                         /* Yeah, we know about the cleanmarker. */
876                         if (mode && i >= c->fsdata_pos && 
877                             i < c->fsdata_pos+c->fsdata_len)
878                                 continue;
879
880                         if (buf[page+i] != 0xFF) {
881                                 D2(printk(KERN_DEBUG "Found %02x at %x in OOB for %08x\n",
882                                           buf[page+i], page+i, jeb->offset));
883                                 ret = 1; 
884                                 goto out;
885                         }
886                 }
887                 /* only the first page can contain a cleanmarker !*/
888                 mode = 0;
889         }       
890
891         /* we know, we are aligned :) */        
892         for (; page < len; page += sizeof(long)) {
893                 unsigned long dat = *(unsigned long *)(&buf[page]);
894                 if(dat != -1) {
895                         ret = 1; 
896                         goto out;
897                 }
898         }
899
900 out:
901         kfree(buf);     
902         
903         return ret;
904 }
905
906 /*
907 *       Scan for a valid cleanmarker and for bad blocks
908 *       For virtual blocks (concatenated physical blocks) check the cleanmarker
909 *       only in the first page of the first physical block, but scan for bad blocks in all
910 *       physical blocks
911 */
912 int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
913 {
914         struct jffs2_unknown_node n;
915         unsigned char buf[32];
916         unsigned char *p;
917         int ret, i, cnt, retval = 0;
918         size_t retlen, offset;
919         int oob_size;
920
921         offset = jeb->offset;
922         oob_size = c->mtd->oobsize;
923
924         /* Loop through the physical blocks */
925         for (cnt = 0; cnt < (c->sector_size / c->mtd->erasesize); cnt++) {
926                 /*
927                    *    We read oob data from page 0 and 1 of the block.
928                    *    page 0 contains cleanmarker and badblock info
929                    *    page 1 contains failure count of this block
930                  */
931                 ret = c->mtd->read_oob (c->mtd, offset, oob_size << 1, &retlen, buf);
932
933                 if (ret) {
934                         D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Read OOB failed %d for block at %08x\n", ret, jeb->offset));
935                         return ret;
936                 }
937                 if (retlen < (oob_size << 1)) {
938                         D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Read OOB return short read (%zd bytes not %d) for block at %08x\n", retlen, oob_size << 1, jeb->offset));
939                         return -EIO;
940                 }
941
942                 /* Check for bad block marker */
943                 if (buf[c->badblock_pos] != 0xff) {
944                         D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Bad block at %08x (has %02x %02x in badblock_pos %d\n",
945                                     jeb->offset, buf[c->badblock_pos],  buf[c->badblock_pos + oob_size], c->badblock_pos));
946                         return 2;
947                 }
948
949                 /* Check for failure counter in the second page */
950                 if (buf[c->badblock_pos + oob_size] != 0xff) {
951                         D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Block marked as failed at %08x, fail count:%d\n", jeb->offset, buf[c->badblock_pos + oob_size]));
952                         return 3;
953                 }
954
955                 /* Check cleanmarker only on the first physical block */
956                 if (!cnt) {
957                         n.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
958                         n.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
959                         n.totlen = cpu_to_je32 (8);
960                         p = (unsigned char *) &n;
961
962                         for (i = 0; i < c->fsdata_len; i++) {
963                                 if (buf[c->fsdata_pos + i] != p[i]) {
964                                         retval = 1;
965                                 }
966                         }
967                         D1(if (retval == 1) {
968                                 printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): Cleanmarker node not detected in block at %08x\n", jeb->offset);
969                                 printk(KERN_WARNING "OOB at %08x was ", offset);
970                                 for (i=0; i < oob_size; i++) {
971                                         printk("%02x ", buf[i]);
972                                 }
973                                 printk("\n");
974                         })
975                 }
976                 offset += c->mtd->erasesize;
977         }
978         return retval;
979 }
980
981 int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
982 {
983         struct  jffs2_unknown_node n;
984         int     ret;
985         size_t  retlen;
986
987         n.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
988         n.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
989         n.totlen = cpu_to_je32(8);
990
991         ret = jffs2_flash_write_oob(c, jeb->offset + c->fsdata_pos, c->fsdata_len, &retlen, (unsigned char *)&n);
992         
993         if (ret) {
994                 D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): Write failed for block at %08x: error %d\n", jeb->offset, ret));
995                 return ret;
996         }
997         if (retlen != c->fsdata_len) {
998                 D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): Short write for block at %08x: %zd not %d\n", jeb->offset, retlen, c->fsdata_len));
999                 return ret;
1000         }
1001         return 0;
1002 }
1003
1004 /* 
1005  * We try to get the failure count of this block.
1006  */
1007 int jffs2_nand_read_failcnt(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) {
1008
1009         unsigned char buf[16];
1010         int     ret;
1011         size_t  retlen;
1012         int     oob_size;
1013
1014         oob_size = c->mtd->oobsize;
1015         
1016         ret = c->mtd->read_oob(c->mtd, jeb->offset + c->mtd->oobblock, oob_size , &retlen, buf);
1017         
1018         if (ret) {
1019                 D1(printk(KERN_WARNING "jffs2_nand_read_failcnt(): Read OOB failed %d for block at %08x\n", ret, jeb->offset));
1020                 return ret;
1021         }
1022
1023         if (retlen < oob_size) {
1024                 D1(printk(KERN_WARNING "jffs2_nand_read_failcnt(): Read OOB return short read (%zd bytes not %d) for block at %08x\n", retlen, oob_size, jeb->offset));
1025                 return -EIO;
1026         }
1027
1028         jeb->bad_count =  buf[c->badblock_pos]; 
1029         return 0;
1030 }
1031
1032 /* 
1033  * On NAND we try to mark this block bad. We try to write how often
1034  * the block was erased and mark it finaly bad, if the count
1035  * is > MAX_ERASE_FAILURES. We read this information on mount !
1036  * jeb->bad_count contains the count before this erase.
1037  * Don't care about failures. This block remains on the erase-pending
1038  * or badblock list as long as nobody manipulates the flash with
1039  * a bootloader or something like that.
1040  */
1041
1042 int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
1043 {
1044         unsigned char buf = 0x0;
1045         int     ret;
1046         size_t  retlen;
1047
1048         /* if the count is < max, we try to write the counter to the 2nd page oob area */
1049         if( ++jeb->bad_count < MAX_ERASE_FAILURES) {
1050                 buf = (unsigned char)jeb->bad_count;
1051                 c->badblock_pos += c->mtd->oobblock;
1052         }
1053         
1054         ret = jffs2_flash_write_oob(c, jeb->offset + c->badblock_pos, 1, &retlen, &buf);
1055         
1056         if (ret) {
1057                 D1(printk(KERN_WARNING "jffs2_write_nand_badblock(): Write failed for block at %08x: error %d\n", jeb->offset, ret));
1058                 return ret;
1059         }
1060         if (retlen != 1) {
1061                 D1(printk(KERN_WARNING "jffs2_write_nand_badblock(): Short write for block at %08x: %zd not 1\n", jeb->offset, retlen));
1062                 return ret;
1063         }
1064         return 0;
1065 }
1066
1067 #define JFFS2_OOB_ECCPOS0               0
1068 #define JFFS2_OOB_ECCPOS1               1
1069 #define JFFS2_OOB_ECCPOS2               2
1070 #define JFFS2_OOB_ECCPOS3               3
1071 #define JFFS2_OOB_ECCPOS4               6
1072 #define JFFS2_OOB_ECCPOS5               7
1073
1074 #define NAND_JFFS2_OOB8_FSDAPOS         6
1075 #define NAND_JFFS2_OOB16_FSDAPOS        8
1076 #define NAND_JFFS2_OOB8_FSDALEN         2
1077 #define NAND_JFFS2_OOB16_FSDALEN        8
1078
1079 static struct nand_oobinfo jffs2_oobinfo_swecc = {
1080         .useecc = 1,
1081         .eccpos = {JFFS2_OOB_ECCPOS0, JFFS2_OOB_ECCPOS1, JFFS2_OOB_ECCPOS2,
1082                    JFFS2_OOB_ECCPOS3, JFFS2_OOB_ECCPOS4, JFFS2_OOB_ECCPOS5}
1083 };
1084
1085 static struct nand_oobinfo jffs2_oobinfo_docecc = {
1086         .useecc = 1,
1087         .eccpos = {0,1,2,3,4,5}
1088 };
1089
1090
1091
1092 int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
1093 {
1094         /* Cleanmarker is out-of-band, so inline size zero */
1095         c->cleanmarker_size = 0;
1096
1097         /* Initialise write buffer */
1098         c->wbuf_pagesize = c->mtd->oobblock;
1099         c->wbuf_ofs = 0xFFFFFFFF;
1100
1101         /* FIXME: If we had a generic way of describing the hardware's
1102            use of OOB area, we could perhaps make this generic too. */
1103         switch(c->mtd->ecctype) {
1104         case MTD_ECC_SW:
1105                 D1(printk(KERN_DEBUG "JFFS2 using software ECC\n"));
1106                 c->oobinfo = &jffs2_oobinfo_swecc;
1107                 if (c->mtd->oobsize == 8) {
1108                         c->fsdata_pos = NAND_JFFS2_OOB8_FSDAPOS;
1109                         c->fsdata_len = NAND_JFFS2_OOB8_FSDALEN;
1110                 } else {
1111                         c->fsdata_pos = NAND_JFFS2_OOB16_FSDAPOS;
1112                         c->fsdata_len = NAND_JFFS2_OOB16_FSDALEN;
1113                 }
1114                 c->badblock_pos = NAND_BADBLOCK_POS;
1115                 break;
1116
1117         case MTD_ECC_RS_DiskOnChip:
1118                 D1(printk(KERN_DEBUG "JFFS2 using DiskOnChip hardware ECC\n"));
1119                 c->oobinfo = &jffs2_oobinfo_docecc;
1120                 c->fsdata_pos = 6;
1121                 c->fsdata_len = NAND_JFFS2_OOB16_FSDALEN;
1122                 c->badblock_pos = 15;
1123                 break;
1124
1125         default:
1126                 printk("JFFS2 doesn't yet know how to handle ECC type %d\n",
1127                        c->mtd->ecctype);
1128                 return -EINVAL;
1129         }
1130
1131         c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1132         if (!c->wbuf)
1133                 return -ENOMEM;
1134
1135 #ifdef BREAKME
1136         if (!brokenbuf)
1137                 brokenbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1138         if (!brokenbuf) {
1139                 kfree(c->wbuf);
1140                 return -ENOMEM;
1141         }
1142         memset(brokenbuf, 0xdb, c->wbuf_pagesize);
1143 #endif
1144         return 0;
1145 }
1146
1147 void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)
1148 {
1149         kfree(c->wbuf);
1150 }