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