2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: wbuf.c,v 1.53 2003/10/11 11:46:09 dwmw2 Exp $
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>
21 /* For testing write failures */
26 static unsigned char *brokenbuf
;
29 /* max. erase failures before we mark a block bad */
30 #define MAX_ERASE_FAILURES 5
32 /* two seconds timeout for timed wbuf-flushing */
33 #define WBUF_FLUSH_TIMEOUT 2 * HZ
35 struct jffs2_inodirty
{
37 struct jffs2_inodirty
*next
;
40 static struct jffs2_inodirty inodirty_nomem
;
42 static int jffs2_wbuf_pending_for_ino(struct jffs2_sb_info
*c
, uint32_t ino
)
44 struct jffs2_inodirty
*this = c
->wbuf_inodes
;
46 /* If a malloc failed, consider _everything_ dirty */
47 if (this == &inodirty_nomem
)
50 /* If ino == 0, _any_ non-GC writes mean 'yes' */
54 /* Look to see if the inode in question is pending in the wbuf */
63 static void jffs2_clear_wbuf_ino_list(struct jffs2_sb_info
*c
)
65 struct jffs2_inodirty
*this;
67 this = c
->wbuf_inodes
;
69 if (this != &inodirty_nomem
) {
71 struct jffs2_inodirty
*next
= this->next
;
76 c
->wbuf_inodes
= NULL
;
79 static void jffs2_wbuf_dirties_inode(struct jffs2_sb_info
*c
, uint32_t ino
)
81 struct jffs2_inodirty
*new;
83 /* Mark the superblock dirty so that kupdated will flush... */
84 OFNI_BS_2SFFJ(c
)->s_dirt
= 1;
86 if (jffs2_wbuf_pending_for_ino(c
, ino
))
89 new = kmalloc(sizeof(*new), GFP_KERNEL
);
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
;
97 new->next
= c
->wbuf_inodes
;
102 static inline void jffs2_refile_wbuf_blocks(struct jffs2_sb_info
*c
)
104 struct list_head
*this, *next
;
107 if (list_empty(&c
->erasable_pending_wbuf_list
))
110 list_for_each_safe(this, next
, &c
->erasable_pending_wbuf_list
) {
111 struct jffs2_eraseblock
*jeb
= list_entry(this, struct jffs2_eraseblock
, list
);
113 D1(printk(KERN_DEBUG
"Removing eraseblock at 0x%08x from erasable_pending_wbuf_list...\n", jeb
->offset
));
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
);
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
);
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. */
134 static void jffs2_wbuf_recover(struct jffs2_sb_info
*c
)
136 struct jffs2_eraseblock
*jeb
, *new_jeb
;
137 struct jffs2_raw_node_ref
**first_raw
, **raw
;
141 uint32_t start
, end
, ofs
, len
;
143 spin_lock(&c
->erase_completion_lock
);
145 jeb
= &c
->blocks
[c
->wbuf_ofs
/ c
->sector_size
];
147 D1(printk("About to refile bad block at %08x\n", jeb
->offset
));
149 D2(jffs2_dump_block_lists(c
));
150 /* File the existing block on the bad_used_list.... */
151 if (c
->nextblock
== jeb
)
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
);
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
);
166 D2(jffs2_dump_block_lists(c
));
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
;
174 ACCT_SANITY_CHECK(c
,jeb
);
175 D1(ACCT_PARANOIA_CHECK(jeb
));
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
;
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
),
187 first_raw
= &(*first_raw
)->next_phys
;
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
);
197 start
= ref_offset(*first_raw
);
198 end
= ref_offset(*first_raw
) + (*first_raw
)->totlen
;
200 /* Find the last node to be recovered */
203 if (!ref_obsolete(*raw
))
204 end
= ref_offset(*raw
) + (*raw
)->totlen
;
206 raw
= &(*raw
)->next_phys
;
208 spin_unlock(&c
->erase_completion_lock
);
210 D1(printk(KERN_DEBUG
"wbuf recover %08x-%08x\n", start
, end
));
213 if (start
< c
->wbuf_ofs
) {
214 /* First affected node was already partially written.
215 * Attempt to reread the old data into our buffer. */
217 buf
= kmalloc(end
- start
, GFP_KERNEL
);
219 printk(KERN_CRIT
"Malloc failure in wbuf recovery. Data loss ensues.\n");
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
) {
230 if (ret
|| retlen
!= c
->wbuf_ofs
- start
) {
231 printk(KERN_CRIT
"Old data are already lost in wbuf recovery. Data loss ensues.\n");
236 first_raw
= &(*first_raw
)->next_phys
;
237 /* If this was the only node to be recovered, give up */
241 /* It wasn't. Go on and try to recover nodes complete in the wbuf */
242 start
= ref_offset(*first_raw
);
244 /* Read succeeded. Copy the remaining data from the wbuf */
245 memcpy(buf
+ (c
->wbuf_ofs
- start
), c
->wbuf
, end
- c
->wbuf_ofs
);
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 */
252 /* ... and get an allocation of space from a shiny new block instead */
253 ret
= jffs2_reserve_space_gc(c
, end
-start
, &ofs
, &len
);
255 printk(KERN_WARNING
"Failed to allocate space for wbuf recovery. Data loss ensues.\n");
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
267 uint32_t towrite
= (end
-start
) - ((end
-start
)%c
->wbuf_pagesize
);
269 D1(printk(KERN_DEBUG
"Write 0x%x bytes at 0x%08x in wbuf recover\n",
274 if (breakme
++ == 20) {
275 printk(KERN_NOTICE
"Faking write error at 0x%08x\n", ofs
);
277 c
->mtd
->write_ecc(c
->mtd
, ofs
, towrite
, &retlen
,
278 brokenbuf
, NULL
, c
->oobinfo
);
282 ret
= c
->mtd
->write_ecc(c
->mtd
, ofs
, towrite
, &retlen
,
283 buf
, NULL
, c
->oobinfo
);
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");
291 struct jffs2_raw_node_ref
*raw2
;
293 raw2
= jffs2_alloc_raw_node_ref();
297 raw2
->flash_offset
= ofs
| REF_OBSOLETE
;
298 raw2
->totlen
= (*first_raw
)->totlen
;
299 raw2
->next_phys
= NULL
;
300 raw2
->next_in_ino
= NULL
;
302 jffs2_add_physical_node_ref(c
, raw2
);
306 printk(KERN_NOTICE
"Recovery of wbuf succeeded to %08x\n", ofs
);
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. */
315 /* OK, now we're left with the dregs in whichever buffer we're using */
317 memcpy(c
->wbuf
, buf
, end
-start
);
320 memmove(c
->wbuf
, c
->wbuf
+ (start
- c
->wbuf_ofs
), end
- start
);
323 c
->wbuf_len
= end
- start
;
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
];
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
;
334 new_jeb
->first_node
= *first_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
));
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
;
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
;
354 c
->free_size
-= (*raw
)->totlen
;
355 (*raw
)->flash_offset
= ofs
| ref_flags(*raw
);
356 ofs
+= (*raw
)->totlen
;
357 new_jeb
->last_node
= *raw
;
359 raw
= &(*raw
)->next_phys
;
362 /* Fix up the original jeb now it's on the bad_list */
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
);
373 jeb
->last_node
= container_of(first_raw
, struct jffs2_raw_node_ref
, next_phys
);
375 ACCT_SANITY_CHECK(c
,jeb
);
376 D1(ACCT_PARANOIA_CHECK(jeb
));
378 ACCT_SANITY_CHECK(c
,new_jeb
);
379 D1(ACCT_PARANOIA_CHECK(new_jeb
));
381 spin_unlock(&c
->erase_completion_lock
);
383 D1(printk(KERN_DEBUG
"wbuf recovery completed OK\n"));
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
391 static int __jffs2_flush_wbuf(struct jffs2_sb_info
*c
, int pad
)
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
))
401 if (!down_trylock(&c
->alloc_sem
)) {
403 printk(KERN_CRIT
"jffs2_flush_wbuf() called with alloc_sem not locked!\n");
407 if(!c
->wbuf
|| !c
->wbuf_len
)
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.
417 c
->wbuf_len
= PAD(c
->wbuf_len
);
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));
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. */
432 if (breakme
++ == 20) {
433 printk(KERN_NOTICE
"Faking write error at 0x%08x\n", c
->wbuf_ofs
);
435 c
->mtd
->write_ecc(c
->mtd
, c
->wbuf_ofs
, c
->wbuf_pagesize
,
436 &retlen
, brokenbuf
, NULL
, c
->oobinfo
);
440 ret
= c
->mtd
->write_ecc(c
->mtd
, c
->wbuf_ofs
, c
->wbuf_pagesize
, &retlen
, c
->wbuf
, NULL
, c
->oobinfo
);
443 if (ret
|| retlen
!= c
->wbuf_pagesize
) {
445 printk(KERN_WARNING
"jffs2_flush_wbuf(): Write failed with %d\n",ret
);
447 printk(KERN_WARNING
"jffs2_flush_wbuf(): Write was short: %zd instead of %d\n",
448 retlen
, c
->wbuf_pagesize
);
452 jffs2_wbuf_recover(c
);
457 /* Adjusting free size of next block only, if it's called from fsync ! */
459 D1(printk(KERN_DEBUG
"jffs2_flush_wbuf() adjusting free_size of c->nextblock\n"));
460 spin_lock(&c
->erase_completion_lock
);
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
);
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
);
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
);
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
;
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
)
499 uint32_t old_wbuf_ofs
;
500 uint32_t old_wbuf_len
;
503 D1(printk(KERN_DEBUG
"jffs2_flush_wbuf_gc() called for ino #%u...\n", ino
));
506 if (!jffs2_wbuf_pending_for_ino(c
, ino
)) {
507 D1(printk(KERN_DEBUG
"Ino #%d not pending in wbuf. Returning\n", ino
));
512 old_wbuf_ofs
= c
->wbuf_ofs
;
513 old_wbuf_len
= c
->wbuf_len
;
515 while (old_wbuf_len
&&
516 old_wbuf_ofs
== c
->wbuf_ofs
) {
520 D1(printk(KERN_DEBUG
"jffs2_flush_wbuf_gc() calls gc pass\n"));
522 ret
= jffs2_garbage_collect_pass(c
);
524 /* GC failed. Flush it with padding instead */
526 ret
= __jffs2_flush_wbuf(c
, 2);
532 D1(printk(KERN_DEBUG
"jffs2_flush_wbuf_gc() ends...\n"));
538 /* Pad write-buffer to end and write it, wasting space. */
539 int jffs2_flush_wbuf_pad(struct jffs2_sb_info
*c
)
541 return __jffs2_flush_wbuf(c
, 1);
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
)
549 struct iovec outvecs
[3];
551 uint32_t split_ofs
= 0;
553 int ret
, splitvec
= -1;
556 unsigned char *wbuf_ptr
;
558 uint32_t outvec_to
= to
;
560 /* If not NAND flash, don't bother */
562 return jffs2_flash_direct_writev(c
, invecs
, count
, to
, retlen
);
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
);
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)
577 if ( (to
& ~(c
->sector_size
-1)) != (c
->wbuf_ofs
& ~(c
->sector_size
-1)) ) {
578 /* It's a write to a new block */
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
);
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
));
589 /* set pointer to new block */
590 c
->wbuf_ofs
= PAGE_DIV(to
);
591 c
->wbuf_len
= PAGE_MOD(to
);
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
);
598 printk(KERN_CRIT
"wbuf was previously %08x-%08x\n",
599 c
->wbuf_ofs
, c
->wbuf_ofs
+c
->wbuf_len
);
603 /* Note outvecs[3] above. We know count is never greater than 2 */
605 printk(KERN_CRIT
"jffs2_flash_writev(): count is %ld\n", count
);
613 /* Fill writebuffer first, if already in use */
615 uint32_t invec_ofs
= 0;
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 */
622 c
->wbuf_len
= c
->wbuf_pagesize
;
625 while(c
->wbuf_len
< c
->wbuf_pagesize
) {
631 thislen
= c
->wbuf_pagesize
- c
->wbuf_len
;
633 if (thislen
>= invecs
[invec
].iov_len
)
634 thislen
= invecs
[invec
].iov_len
;
638 memcpy(c
->wbuf
+ c
->wbuf_len
, invecs
[invec
].iov_base
, thislen
);
639 c
->wbuf_len
+= thislen
;
641 /* Get next invec, if actual did not fill the buffer */
642 if (c
->wbuf_len
< c
->wbuf_pagesize
)
646 /* write buffer is full, flush buffer */
647 ret
= __jffs2_flush_wbuf(c
, 0);
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 */
656 outvec_to
+= donelen
;
657 c
->wbuf_ofs
= outvec_to
;
659 /* All invecs done ? */
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
) {
670 split_ofs
= outvecs
[0].iov_len
- PAGE_MOD(totlen
);
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.... */
680 /* totlen holds the amount of data still to be written */
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
)) {
687 split_ofs
= outvecs
[outvec
].iov_len
- PAGE_MOD(totlen
);
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 */
696 if (splitvec
!= -1) {
700 remainder
= outvecs
[splitvec
].iov_len
- split_ofs
;
701 outvecs
[splitvec
].iov_len
= split_ofs
;
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,
713 donelen
+= wbuf_retlen
;
714 c
->wbuf_ofs
= PAGE_DIV(outvec_to
) + PAGE_DIV(totlen
);
717 outvecs
[splitvec
].iov_base
+= split_ofs
;
718 outvecs
[splitvec
].iov_len
= remainder
;
727 /* Now splitvec points to the start of the bits we have to copy
731 for ( ; splitvec
< outvec
; splitvec
++) {
732 /* Don't copy the wbuf into itself */
733 if (outvecs
[splitvec
].iov_base
== c
->wbuf
)
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
;
739 c
->wbuf_len
= wbuf_ptr
- c
->wbuf
;
741 /* If there's a remainder in the wbuf and it's a non-GC write,
742 remember that the wbuf affects this ino */
746 if (c
->wbuf_len
&& ino
)
747 jffs2_wbuf_dirties_inode(c
, ino
);
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
756 int jffs2_flash_write(struct jffs2_sb_info
*c
, loff_t ofs
, size_t len
, size_t *retlen
, const u_char
*buf
)
758 struct iovec vecs
[1];
760 if (jffs2_can_mark_obsolete(c
))
761 return c
->mtd
->write(c
->mtd
, ofs
, len
, retlen
, buf
);
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);
769 Handle readback from writebuffer and ECC failure return
771 int jffs2_flash_read(struct jffs2_sb_info
*c
, loff_t ofs
, size_t len
, size_t *retlen
, u_char
*buf
)
773 loff_t orbf
= 0, owbf
= 0, lwbf
= 0;
777 if (!jffs2_can_mark_obsolete(c
)) {
778 ret
= c
->mtd
->read_ecc(c
->mtd
, ofs
, len
, retlen
, buf
, NULL
, c
->oobinfo
);
780 if ( (ret
== -EIO
) && (*retlen
== len
) ) {
781 printk(KERN_WARNING
"mtd->read(0x%zx bytes from 0x%llx) returned ECC error\n",
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. :)
796 return c
->mtd
->read(c
->mtd
, ofs
, len
, retlen
, buf
);
798 /* if no writebuffer available or write buffer empty, return */
799 if (!c
->wbuf_pagesize
|| !c
->wbuf_len
)
802 /* if we read in a different block, return */
803 if ( (ofs
& ~(c
->sector_size
-1)) != (c
->wbuf_ofs
& ~(c
->sector_size
-1)) )
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 ? */
810 lwbf
= c
->wbuf_len
- owbf
; /* number of bytes to copy */
814 orbf
= (c
->wbuf_ofs
- ofs
); /* offset in read buffer */
815 if (orbf
> len
) /* is write beyond write buffer ? */
817 lwbf
= len
- orbf
; /* number of bytes to copy */
818 if (lwbf
> c
->wbuf_len
)
822 memcpy(buf
+orbf
,c
->wbuf
+owbf
,lwbf
);
828 * Check, if the out of band area is empty
830 int jffs2_check_oob_empty( struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
, int mode
)
838 oob_size
= c
->mtd
->oobsize
;
840 /* allocate a buffer for all oob data in this sector */
842 buf
= kmalloc(len
, GFP_KERNEL
);
844 printk(KERN_NOTICE
"jffs2_check_oob_empty(): allocation of temporary data buffer for oob check failed\n");
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
851 ret
= jffs2_flash_read_oob(c
, jeb
->offset
, len
, &retlen
, buf
);
853 D1(printk(KERN_WARNING
"jffs2_check_oob_empty(): Read OOB failed %d for block at %08x\n", ret
, jeb
->offset
));
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
));
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;
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
)
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
));
887 /* only the first page can contain a cleanmarker !*/
891 /* we know, we are aligned :) */
892 for (; page
< len
; page
+= sizeof(long)) {
893 unsigned long dat
= *(unsigned long *)(&buf
[page
]);
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
912 int jffs2_check_nand_cleanmarker (struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
914 struct jffs2_unknown_node n
;
915 unsigned char buf
[32];
917 int ret
, i
, cnt
, retval
= 0;
918 size_t retlen
, offset
;
921 offset
= jeb
->offset
;
922 oob_size
= c
->mtd
->oobsize
;
924 /* Loop through the physical blocks */
925 for (cnt
= 0; cnt
< (c
->sector_size
/ c
->mtd
->erasesize
); cnt
++) {
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
931 ret
= c
->mtd
->read_oob (c
->mtd
, offset
, oob_size
<< 1, &retlen
, buf
);
934 D1 (printk (KERN_WARNING
"jffs2_check_nand_cleanmarker(): Read OOB failed %d for block at %08x\n", ret
, jeb
->offset
));
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
));
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
));
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
]));
955 /* Check cleanmarker only on the first physical block */
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
;
962 for (i
= 0; i
< c
->fsdata_len
; i
++) {
963 if (buf
[c
->fsdata_pos
+ i
] != p
[i
]) {
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
]);
976 offset
+= c
->mtd
->erasesize
;
981 int jffs2_write_nand_cleanmarker(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
983 struct jffs2_unknown_node n
;
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);
991 ret
= jffs2_flash_write_oob(c
, jeb
->offset
+ c
->fsdata_pos
, c
->fsdata_len
, &retlen
, (unsigned char *)&n
);
994 D1(printk(KERN_WARNING
"jffs2_write_nand_cleanmarker(): Write failed for block at %08x: error %d\n", jeb
->offset
, ret
));
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
));
1005 * We try to get the failure count of this block.
1007 int jffs2_nand_read_failcnt(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
) {
1009 unsigned char buf
[16];
1014 oob_size
= c
->mtd
->oobsize
;
1016 ret
= c
->mtd
->read_oob(c
->mtd
, jeb
->offset
+ c
->mtd
->oobblock
, oob_size
, &retlen
, buf
);
1019 D1(printk(KERN_WARNING
"jffs2_nand_read_failcnt(): Read OOB failed %d for block at %08x\n", ret
, jeb
->offset
));
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
));
1028 jeb
->bad_count
= buf
[c
->badblock_pos
];
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.
1042 int jffs2_write_nand_badblock(struct jffs2_sb_info
*c
, struct jffs2_eraseblock
*jeb
)
1044 unsigned char buf
= 0x0;
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
;
1054 ret
= jffs2_flash_write_oob(c
, jeb
->offset
+ c
->badblock_pos
, 1, &retlen
, &buf
);
1057 D1(printk(KERN_WARNING
"jffs2_write_nand_badblock(): Write failed for block at %08x: error %d\n", jeb
->offset
, ret
));
1061 D1(printk(KERN_WARNING
"jffs2_write_nand_badblock(): Short write for block at %08x: %zd not 1\n", jeb
->offset
, retlen
));
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
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
1079 static struct nand_oobinfo jffs2_oobinfo_swecc
= {
1081 .eccpos
= {JFFS2_OOB_ECCPOS0
, JFFS2_OOB_ECCPOS1
, JFFS2_OOB_ECCPOS2
,
1082 JFFS2_OOB_ECCPOS3
, JFFS2_OOB_ECCPOS4
, JFFS2_OOB_ECCPOS5
}
1085 static struct nand_oobinfo jffs2_oobinfo_docecc
= {
1087 .eccpos
= {0,1,2,3,4,5}
1092 int jffs2_nand_flash_setup(struct jffs2_sb_info
*c
)
1094 /* Cleanmarker is out-of-band, so inline size zero */
1095 c
->cleanmarker_size
= 0;
1097 /* Initialise write buffer */
1098 c
->wbuf_pagesize
= c
->mtd
->oobblock
;
1099 c
->wbuf_ofs
= 0xFFFFFFFF;
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
) {
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
;
1111 c
->fsdata_pos
= NAND_JFFS2_OOB16_FSDAPOS
;
1112 c
->fsdata_len
= NAND_JFFS2_OOB16_FSDALEN
;
1114 c
->badblock_pos
= NAND_BADBLOCK_POS
;
1117 case MTD_ECC_RS_DiskOnChip
:
1118 D1(printk(KERN_DEBUG
"JFFS2 using DiskOnChip hardware ECC\n"));
1119 c
->oobinfo
= &jffs2_oobinfo_docecc
;
1121 c
->fsdata_len
= NAND_JFFS2_OOB16_FSDALEN
;
1122 c
->badblock_pos
= 15;
1126 printk("JFFS2 doesn't yet know how to handle ECC type %d\n",
1131 c
->wbuf
= kmalloc(c
->wbuf_pagesize
, GFP_KERNEL
);
1137 brokenbuf
= kmalloc(c
->wbuf_pagesize
, GFP_KERNEL
);
1142 memset(brokenbuf
, 0xdb, c
->wbuf_pagesize
);
1147 void jffs2_nand_flash_cleanup(struct jffs2_sb_info
*c
)