blob: 5df40d8bc719a7984f823b40bd8098486745ef0a [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Copyright © 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 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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 <linux/jiffies.h>
22#include <linux/sched.h>
23
24#include "nodelist.h"
25#include "pub_debug_info.h"
26
27/* For testing write failures */
28#undef BREAKME
29#undef BREAKMEHEADER
30
31#ifdef BREAKME
32static unsigned char *brokenbuf;
33#endif
34
35#define PAGE_DIV(x) ( ((unsigned long)(x) / (unsigned long)(c->wbuf_pagesize)) * (unsigned long)(c->wbuf_pagesize) )
36#define PAGE_MOD(x) ( (unsigned long)(x) % (unsigned long)(c->wbuf_pagesize) )
37
38/* max. erase failures before we mark a block bad */
39#define MAX_ERASE_FAILURES 2
40
41struct jffs2_inodirty {
42 uint32_t ino;
43 struct jffs2_inodirty *next;
44};
45
46static struct jffs2_inodirty inodirty_nomem;
47
48static int jffs2_wbuf_pending_for_ino(struct jffs2_sb_info *c, uint32_t ino)
49{
50 struct jffs2_inodirty *this = c->wbuf_inodes;
51
52 /* If a malloc failed, consider _everything_ dirty */
53 if (this == &inodirty_nomem)
54 return 1;
55
56 /* If ino == 0, _any_ non-GC writes mean 'yes' */
57 if (this && !ino)
58 return 1;
59
60 /* Look to see if the inode in question is pending in the wbuf */
61 while (this) {
62 if (this->ino == ino)
63 return 1;
64 this = this->next;
65 }
66 return 0;
67}
68
69static void jffs2_clear_wbuf_ino_list(struct jffs2_sb_info *c)
70{
71 struct jffs2_inodirty *this;
72
73 this = c->wbuf_inodes;
74
75 if (this != &inodirty_nomem) {
76 while (this) {
77 struct jffs2_inodirty *next = this->next;
78 kfree(this);
79 this = next;
80 }
81 }
82 c->wbuf_inodes = NULL;
83}
84
85static void jffs2_wbuf_dirties_inode(struct jffs2_sb_info *c, uint32_t ino)
86{
87 struct jffs2_inodirty *new;
88
89 /* Mark the superblock dirty so that kupdated will flush... */
90 jffs2_dirty_trigger(c);
91
92 if (jffs2_wbuf_pending_for_ino(c, ino))
93 return;
94
95 new = kmalloc(sizeof(*new), GFP_KERNEL);
96 if (!new) {
97 jffs2_dbg(1, "No memory to allocate inodirty. Fallback to all considered dirty\n");
98 jffs2_clear_wbuf_ino_list(c);
99 c->wbuf_inodes = &inodirty_nomem;
100 return;
101 }
102 new->ino = ino;
103 new->next = c->wbuf_inodes;
104 c->wbuf_inodes = new;
105 return;
106}
107
108static inline void jffs2_refile_wbuf_blocks(struct jffs2_sb_info *c)
109{
110 struct list_head *this, *next;
111 static int n;
112
113 if (list_empty(&c->erasable_pending_wbuf_list))
114 return;
115
116 list_for_each_safe(this, next, &c->erasable_pending_wbuf_list) {
117 struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
118
119 jffs2_dbg(1, "Removing eraseblock at 0x%08x from erasable_pending_wbuf_list...\n",
120 jeb->offset);
121 list_del(this);
122 if ((jiffies + (n++)) & 127) {
123 /* Most of the time, we just erase it immediately. Otherwise we
124 spend ages scanning it on mount, etc. */
125 jffs2_dbg(1, "...and adding to erase_pending_list\n");
126 list_add_tail(&jeb->list, &c->erase_pending_list);
127 c->nr_erasing_blocks++;
128 jffs2_garbage_collect_trigger(c);
129 } else {
130 /* Sometimes, however, we leave it elsewhere so it doesn't get
131 immediately reused, and we spread the load a bit. */
132 jffs2_dbg(1, "...and adding to erasable_list\n");
133 list_add_tail(&jeb->list, &c->erasable_list);
134 }
135 }
136}
137
138#define REFILE_NOTEMPTY 0
139#define REFILE_ANYWAY 1
140
141static void jffs2_block_refile(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int allow_empty)
142{
143 jffs2_dbg(1, "About to refile bad block at %08x\n", jeb->offset);
144
145 /* File the existing block on the bad_used_list.... */
146 if (c->nextblock == jeb)
147 c->nextblock = NULL;
148 else /* Not sure this should ever happen... need more coffee */
149 list_del(&jeb->list);
150 if (jeb->first_node) {
151 jffs2_dbg(1, "Refiling block at %08x to bad_used_list\n",
152 jeb->offset);
153 list_add(&jeb->list, &c->bad_used_list);
154 } else {
155 BUG_ON(allow_empty == REFILE_NOTEMPTY);
156 /* It has to have had some nodes or we couldn't be here */
157 jffs2_dbg(1, "Refiling block at %08x to erase_pending_list\n",
158 jeb->offset);
159 list_add(&jeb->list, &c->erase_pending_list);
160 c->nr_erasing_blocks++;
161 jffs2_garbage_collect_trigger(c);
162 }
163
164 if (!jffs2_prealloc_raw_node_refs(c, jeb, 1)) {
165 uint32_t oldfree = jeb->free_size;
166
167 jffs2_link_node_ref(c, jeb,
168 (jeb->offset+c->sector_size-oldfree) | REF_OBSOLETE,
169 oldfree, NULL);
170 /* convert to wasted */
171 c->wasted_size += oldfree;
172 jeb->wasted_size += oldfree;
173 c->dirty_size -= oldfree;
174 jeb->dirty_size -= oldfree;
175 }
176
177 jffs2_dbg_dump_block_lists_nolock(c);
178 jffs2_dbg_acct_sanity_check_nolock(c,jeb);
179 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
180}
181
182static struct jffs2_raw_node_ref **jffs2_incore_replace_raw(struct jffs2_sb_info *c,
183 struct jffs2_inode_info *f,
184 struct jffs2_raw_node_ref *raw,
185 union jffs2_node_union *node)
186{
187 struct jffs2_node_frag *frag;
188 struct jffs2_full_dirent *fd;
189
190 dbg_noderef("incore_replace_raw: node at %p is {%04x,%04x}\n",
191 node, je16_to_cpu(node->u.magic), je16_to_cpu(node->u.nodetype));
192
193 BUG_ON(je16_to_cpu(node->u.magic) != 0x1985 &&
194 je16_to_cpu(node->u.magic) != 0);
195
196 switch (je16_to_cpu(node->u.nodetype)) {
197 case JFFS2_NODETYPE_INODE:
198 if (f->metadata && f->metadata->raw == raw) {
199 dbg_noderef("Will replace ->raw in f->metadata at %p\n", f->metadata);
200 return &f->metadata->raw;
201 }
202 frag = jffs2_lookup_node_frag(&f->fragtree, je32_to_cpu(node->i.offset));
203 BUG_ON(!frag);
204 /* Find a frag which refers to the full_dnode we want to modify */
205 while (!frag->node || frag->node->raw != raw) {
206 frag = frag_next(frag);
207 BUG_ON(!frag);
208 }
209 dbg_noderef("Will replace ->raw in full_dnode at %p\n", frag->node);
210 return &frag->node->raw;
211
212 case JFFS2_NODETYPE_DIRENT:
213 for (fd = f->dents; fd; fd = fd->next) {
214 if (fd->raw == raw) {
215 dbg_noderef("Will replace ->raw in full_dirent at %p\n", fd);
216 return &fd->raw;
217 }
218 }
219 BUG();
220
221 default:
222 dbg_noderef("Don't care about replacing raw for nodetype %x\n",
223 je16_to_cpu(node->u.nodetype));
224 break;
225 }
226 return NULL;
227}
228
229#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
230static int jffs2_verify_write(struct jffs2_sb_info *c, unsigned char *buf,
231 uint32_t ofs)
232{
233 int ret;
234 size_t retlen;
235 char *eccstr;
236
237 ret = mtd_read(c->mtd, ofs, c->wbuf_pagesize, &retlen, c->wbuf_verify);
238 if (ret && ret != -EUCLEAN && ret != -EBADMSG) {
239 pr_warn("%s(): Read back of page at %08x failed: %d\n",
240 __func__, c->wbuf_ofs, ret);
241 return ret;
242 } else if (retlen != c->wbuf_pagesize) {
243 pr_warn("%s(): Read back of page at %08x gave short read: %zd not %d\n",
244 __func__, ofs, retlen, c->wbuf_pagesize);
245 return -EIO;
246 }
247 if (!memcmp(buf, c->wbuf_verify, c->wbuf_pagesize))
248 return 0;
249
250 if (ret == -EUCLEAN)
251 eccstr = "corrected";
252 else if (ret == -EBADMSG)
253 eccstr = "correction failed";
254 else
255 eccstr = "OK or unused";
256
257 pr_warn("Write verify error (ECC %s) at %08x. Wrote:\n",
258 eccstr, c->wbuf_ofs);
259 sc_debug_info_record(MODULE_ID_AP_JFFS2, "jffs2 %s ECC error at %08x Wrote", c->mtd->name, c->wbuf_ofs);
260 print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET, 16, 1,
261 c->wbuf, c->wbuf_pagesize, 0);
262
263 pr_warn("Read back:\n");
264 print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET, 16, 1,
265 c->wbuf_verify, c->wbuf_pagesize, 0);
266
267 return -EIO;
268}
269#else
270#define jffs2_verify_write(c,b,o) (0)
271#endif
272
273/* Recover from failure to write wbuf. Recover the nodes up to the
274 * wbuf, not the one which we were starting to try to write. */
275
276static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
277{
278 struct jffs2_eraseblock *jeb, *new_jeb;
279 struct jffs2_raw_node_ref *raw, *next, *first_raw = NULL;
280 size_t retlen;
281 int ret;
282 int nr_refile = 0;
283 unsigned char *buf;
284 uint32_t start, end, ofs, len;
285
286 jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
287
288 spin_lock(&c->erase_completion_lock);
289 if (c->wbuf_ofs % c->mtd->erasesize)
290 jffs2_block_refile(c, jeb, REFILE_NOTEMPTY);
291 else
292 jffs2_block_refile(c, jeb, REFILE_ANYWAY);
293 spin_unlock(&c->erase_completion_lock);
294
295 BUG_ON(!ref_obsolete(jeb->last_node));
296
297 /* Find the first node to be recovered, by skipping over every
298 node which ends before the wbuf starts, or which is obsolete. */
299 for (next = raw = jeb->first_node; next; raw = next) {
300 next = ref_next(raw);
301
302 if (ref_obsolete(raw) ||
303 (next && ref_offset(next) <= c->wbuf_ofs)) {
304 dbg_noderef("Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n",
305 ref_offset(raw), ref_flags(raw),
306 (ref_offset(raw) + ref_totlen(c, jeb, raw)),
307 c->wbuf_ofs);
308 continue;
309 }
310 dbg_noderef("First node to be recovered is at 0x%08x(%d)-0x%08x\n",
311 ref_offset(raw), ref_flags(raw),
312 (ref_offset(raw) + ref_totlen(c, jeb, raw)));
313
314 first_raw = raw;
315 break;
316 }
317
318 if (!first_raw) {
319 /* All nodes were obsolete. Nothing to recover. */
320 jffs2_dbg(1, "No non-obsolete nodes to be recovered. Just filing block bad\n");
321 c->wbuf_len = 0;
322 return;
323 }
324
325 start = ref_offset(first_raw);
326 end = ref_offset(jeb->last_node);
327 nr_refile = 1;
328
329 /* Count the number of refs which need to be copied */
330 while ((raw = ref_next(raw)) != jeb->last_node)
331 nr_refile++;
332
333 dbg_noderef("wbuf recover %08x-%08x (%d bytes in %d nodes)\n",
334 start, end, end - start, nr_refile);
335
336 buf = NULL;
337 if (start < c->wbuf_ofs) {
338 /* First affected node was already partially written.
339 * Attempt to reread the old data into our buffer. */
340
341 buf = kmalloc(end - start, GFP_KERNEL);
342 if (!buf) {
343 pr_crit("Malloc failure in wbuf recovery. Data loss ensues.\n");
344
345 goto read_failed;
346 }
347
348 /* Do the read... */
349 ret = mtd_read(c->mtd, start, c->wbuf_ofs - start, &retlen,
350 buf);
351
352 /* ECC recovered ? */
353 if ((ret == -EUCLEAN || ret == -EBADMSG) &&
354 (retlen == c->wbuf_ofs - start))
355 ret = 0;
356
357 if (ret || retlen != c->wbuf_ofs - start) {
358 pr_crit("Old data are already lost in wbuf recovery. Data loss ensues.\n");
359
360 kfree(buf);
361 buf = NULL;
362 read_failed:
363 first_raw = ref_next(first_raw);
364 nr_refile--;
365 while (first_raw && ref_obsolete(first_raw)) {
366 first_raw = ref_next(first_raw);
367 nr_refile--;
368 }
369
370 /* If this was the only node to be recovered, give up */
371 if (!first_raw) {
372 c->wbuf_len = 0;
373 return;
374 }
375
376 /* It wasn't. Go on and try to recover nodes complete in the wbuf */
377 start = ref_offset(first_raw);
378 dbg_noderef("wbuf now recover %08x-%08x (%d bytes in %d nodes)\n",
379 start, end, end - start, nr_refile);
380
381 } else {
382 /* Read succeeded. Copy the remaining data from the wbuf */
383 memcpy(buf + (c->wbuf_ofs - start), c->wbuf, end - c->wbuf_ofs);
384 }
385 }
386 /* OK... we're to rewrite (end-start) bytes of data from first_raw onwards.
387 Either 'buf' contains the data, or we find it in the wbuf */
388
389 /* ... and get an allocation of space from a shiny new block instead */
390 ret = jffs2_reserve_space_gc(c, end-start, &len, JFFS2_SUMMARY_NOSUM_SIZE);
391 if (ret) {
392 pr_warn("Failed to allocate space for wbuf recovery. Data loss ensues.\n");
393 kfree(buf);
394 return;
395 }
396
397 /* The summary is not recovered, so it must be disabled for this erase block */
398 jffs2_sum_disable_collecting(c->summary);
399
400 ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, nr_refile);
401 if (ret) {
402 pr_warn("Failed to allocate node refs for wbuf recovery. Data loss ensues.\n");
403 kfree(buf);
404 return;
405 }
406
407 ofs = write_ofs(c);
408
409 if (end-start >= c->wbuf_pagesize) {
410 /* Need to do another write immediately, but it's possible
411 that this is just because the wbuf itself is completely
412 full, and there's nothing earlier read back from the
413 flash. Hence 'buf' isn't necessarily what we're writing
414 from. */
415 unsigned char *rewrite_buf = buf?:c->wbuf;
416 uint32_t towrite = (end-start) - ((end-start)%c->wbuf_pagesize);
417
418 jffs2_dbg(1, "Write 0x%x bytes at 0x%08x in wbuf recover\n",
419 towrite, ofs);
420
421#ifdef BREAKMEHEADER
422 static int breakme;
423 if (breakme++ == 20) {
424 pr_notice("Faking write error at 0x%08x\n", ofs);
425 breakme = 0;
426 mtd_write(c->mtd, ofs, towrite, &retlen, brokenbuf);
427 ret = -EIO;
428 } else
429#endif
430 ret = mtd_write(c->mtd, ofs, towrite, &retlen,
431 rewrite_buf);
432
433 if (ret || retlen != towrite || jffs2_verify_write(c, rewrite_buf, ofs)) {
434 /* Argh. We tried. Really we did. */
435 pr_crit("Recovery of wbuf failed due to a second write error\n");
436 kfree(buf);
437
438 if (retlen)
439 jffs2_add_physical_node_ref(c, ofs | REF_OBSOLETE, ref_totlen(c, jeb, first_raw), NULL);
440
441 return;
442 }
443 pr_notice("Recovery of wbuf succeeded to %08x\n", ofs);
444
445 c->wbuf_len = (end - start) - towrite;
446 c->wbuf_ofs = ofs + towrite;
447 memmove(c->wbuf, rewrite_buf + towrite, c->wbuf_len);
448 /* Don't muck about with c->wbuf_inodes. False positives are harmless. */
449 } else {
450 /* OK, now we're left with the dregs in whichever buffer we're using */
451 if (buf) {
452 memcpy(c->wbuf, buf, end-start);
453 } else {
454 memmove(c->wbuf, c->wbuf + (start - c->wbuf_ofs), end - start);
455 }
456 c->wbuf_ofs = ofs;
457 c->wbuf_len = end - start;
458 }
459
460 /* Now sort out the jffs2_raw_node_refs, moving them from the old to the next block */
461 new_jeb = &c->blocks[ofs / c->sector_size];
462
463 spin_lock(&c->erase_completion_lock);
464 for (raw = first_raw; raw != jeb->last_node; raw = ref_next(raw)) {
465 uint32_t rawlen = ref_totlen(c, jeb, raw);
466 struct jffs2_inode_cache *ic;
467 struct jffs2_raw_node_ref *new_ref;
468 struct jffs2_raw_node_ref **adjust_ref = NULL;
469 struct jffs2_inode_info *f = NULL;
470
471 jffs2_dbg(1, "Refiling block of %08x at %08x(%d) to %08x\n",
472 rawlen, ref_offset(raw), ref_flags(raw), ofs);
473
474 ic = jffs2_raw_ref_to_ic(raw);
475
476 /* Ick. This XATTR mess should be fixed shortly... */
477 if (ic && ic->class == RAWNODE_CLASS_XATTR_DATUM) {
478 struct jffs2_xattr_datum *xd = (void *)ic;
479 BUG_ON(xd->node != raw);
480 adjust_ref = &xd->node;
481 raw->next_in_ino = NULL;
482 ic = NULL;
483 } else if (ic && ic->class == RAWNODE_CLASS_XATTR_REF) {
484 struct jffs2_xattr_datum *xr = (void *)ic;
485 BUG_ON(xr->node != raw);
486 adjust_ref = &xr->node;
487 raw->next_in_ino = NULL;
488 ic = NULL;
489 } else if (ic && ic->class == RAWNODE_CLASS_INODE_CACHE) {
490 struct jffs2_raw_node_ref **p = &ic->nodes;
491
492 /* Remove the old node from the per-inode list */
493 while (*p && *p != (void *)ic) {
494 if (*p == raw) {
495 (*p) = (raw->next_in_ino);
496 raw->next_in_ino = NULL;
497 break;
498 }
499 p = &((*p)->next_in_ino);
500 }
501
502 if (ic->state == INO_STATE_PRESENT && !ref_obsolete(raw)) {
503 /* If it's an in-core inode, then we have to adjust any
504 full_dirent or full_dnode structure to point to the
505 new version instead of the old */
506 f = jffs2_gc_fetch_inode(c, ic->ino, !ic->pino_nlink);
507 if (IS_ERR(f)) {
508 /* Should never happen; it _must_ be present */
509 JFFS2_ERROR("Failed to iget() ino #%u, err %ld\n",
510 ic->ino, PTR_ERR(f));
511 BUG();
512 }
513 /* We don't lock f->sem. There's a number of ways we could
514 end up in here with it already being locked, and nobody's
515 going to modify it on us anyway because we hold the
516 alloc_sem. We're only changing one ->raw pointer too,
517 which we can get away with without upsetting readers. */
518 adjust_ref = jffs2_incore_replace_raw(c, f, raw,
519 (void *)(buf?:c->wbuf) + (ref_offset(raw) - start));
520 } else if (unlikely(ic->state != INO_STATE_PRESENT &&
521 ic->state != INO_STATE_CHECKEDABSENT &&
522 ic->state != INO_STATE_GC)) {
523 JFFS2_ERROR("Inode #%u is in strange state %d!\n", ic->ino, ic->state);
524 BUG();
525 }
526 }
527
528 new_ref = jffs2_link_node_ref(c, new_jeb, ofs | ref_flags(raw), rawlen, ic);
529
530 if (adjust_ref) {
531 BUG_ON(*adjust_ref != raw);
532 *adjust_ref = new_ref;
533 }
534 if (f)
535 jffs2_gc_release_inode(c, f);
536
537 if (!ref_obsolete(raw)) {
538 jeb->dirty_size += rawlen;
539 jeb->used_size -= rawlen;
540 c->dirty_size += rawlen;
541 c->used_size -= rawlen;
542 raw->flash_offset = ref_offset(raw) | REF_OBSOLETE;
543 BUG_ON(raw->next_in_ino);
544 }
545 ofs += rawlen;
546 }
547
548 kfree(buf);
549
550 /* Fix up the original jeb now it's on the bad_list */
551 if (first_raw == jeb->first_node) {
552 jffs2_dbg(1, "Failing block at %08x is now empty. Moving to erase_pending_list\n",
553 jeb->offset);
554 list_move(&jeb->list, &c->erase_pending_list);
555 c->nr_erasing_blocks++;
556 jffs2_garbage_collect_trigger(c);
557 }
558
559 jffs2_dbg_acct_sanity_check_nolock(c, jeb);
560 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
561
562 jffs2_dbg_acct_sanity_check_nolock(c, new_jeb);
563 jffs2_dbg_acct_paranoia_check_nolock(c, new_jeb);
564
565 spin_unlock(&c->erase_completion_lock);
566
567 jffs2_dbg(1, "wbuf recovery completed OK. wbuf_ofs 0x%08x, len 0x%x\n",
568 c->wbuf_ofs, c->wbuf_len);
569
570}
571
572/* Meaning of pad argument:
573 0: Do not pad. Probably pointless - we only ever use this when we can't pad anyway.
574 1: Pad, do not adjust nextblock free_size
575 2: Pad, adjust nextblock free_size
576*/
577#define NOPAD 0
578#define PAD_NOACCOUNT 1
579#define PAD_ACCOUNTING 2
580
581static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
582{
583 struct jffs2_eraseblock *wbuf_jeb;
584 int ret;
585 size_t retlen;
586
587 /* Nothing to do if not write-buffering the flash. In particular, we shouldn't
588 del_timer() the timer we never initialised. */
589 if (!jffs2_is_writebuffered(c))
590 return 0;
591
592 if (!mutex_is_locked(&c->alloc_sem)) {
593 pr_crit("jffs2_flush_wbuf() called with alloc_sem not locked!\n");
594 BUG();
595 }
596
597 if (!c->wbuf_len) /* already checked c->wbuf above */
598 return 0;
599
600 wbuf_jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
601 if (jffs2_prealloc_raw_node_refs(c, wbuf_jeb, c->nextblock->allocated_refs + 1))
602 return -ENOMEM;
603
604 /* claim remaining space on the page
605 this happens, if we have a change to a new block,
606 or if fsync forces us to flush the writebuffer.
607 if we have a switch to next page, we will not have
608 enough remaining space for this.
609 */
610 if (pad ) {
611 c->wbuf_len = PAD(c->wbuf_len);
612
613 /* Pad with JFFS2_DIRTY_BITMASK initially. this helps out ECC'd NOR
614 with 8 byte page size */
615 memset(c->wbuf + c->wbuf_len, 0, c->wbuf_pagesize - c->wbuf_len);
616
617 if ( c->wbuf_len + sizeof(struct jffs2_unknown_node) < c->wbuf_pagesize) {
618 struct jffs2_unknown_node *padnode = (void *)(c->wbuf + c->wbuf_len);
619 padnode->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
620 padnode->nodetype = cpu_to_je16(JFFS2_NODETYPE_PADDING);
621 padnode->totlen = cpu_to_je32(c->wbuf_pagesize - c->wbuf_len);
622 padnode->hdr_crc = cpu_to_je32(crc32(0, padnode, sizeof(*padnode)-4));
623 }
624 }
625 /* else jffs2_flash_writev has actually filled in the rest of the
626 buffer for us, and will deal with the node refs etc. later. */
627
628#ifdef BREAKME
629 static int breakme;
630 if (breakme++ == 20) {
631 pr_notice("Faking write error at 0x%08x\n", c->wbuf_ofs);
632 breakme = 0;
633 mtd_write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen,
634 brokenbuf);
635 ret = -EIO;
636 } else
637#endif
638
639 ret = mtd_write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize,
640 &retlen, c->wbuf);
641
642 if (ret) {
643 pr_warn("jffs2_flush_wbuf(): Write failed with %d\n", ret);
644 goto wfail;
645 } else if (retlen != c->wbuf_pagesize) {
646 pr_warn("jffs2_flush_wbuf(): Write was short: %zd instead of %d\n",
647 retlen, c->wbuf_pagesize);
648 ret = -EIO;
649 goto wfail;
650 } else if ((ret = jffs2_verify_write(c, c->wbuf, c->wbuf_ofs))) {
651 wfail:
652 jffs2_wbuf_recover(c);
653
654 return ret;
655 }
656
657 /* Adjust free size of the block if we padded. */
658 if (pad) {
659 uint32_t waste = c->wbuf_pagesize - c->wbuf_len;
660
661 jffs2_dbg(1, "jffs2_flush_wbuf() adjusting free_size of %sblock at %08x\n",
662 (wbuf_jeb == c->nextblock) ? "next" : "",
663 wbuf_jeb->offset);
664
665 /* wbuf_pagesize - wbuf_len is the amount of space that's to be
666 padded. If there is less free space in the block than that,
667 something screwed up */
668 if (wbuf_jeb->free_size < waste) {
669 pr_crit("jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n",
670 c->wbuf_ofs, c->wbuf_len, waste);
671 pr_crit("jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n",
672 wbuf_jeb->offset, wbuf_jeb->free_size);
673 BUG();
674 }
675
676 spin_lock(&c->erase_completion_lock);
677
678 jffs2_link_node_ref(c, wbuf_jeb, (c->wbuf_ofs + c->wbuf_len) | REF_OBSOLETE, waste, NULL);
679 /* FIXME: that made it count as dirty. Convert to wasted */
680 wbuf_jeb->dirty_size -= waste;
681 c->dirty_size -= waste;
682 wbuf_jeb->wasted_size += waste;
683 c->wasted_size += waste;
684 } else
685 spin_lock(&c->erase_completion_lock);
686
687 /* Stick any now-obsoleted blocks on the erase_pending_list */
688 jffs2_refile_wbuf_blocks(c);
689 jffs2_clear_wbuf_ino_list(c);
690 spin_unlock(&c->erase_completion_lock);
691
692 memset(c->wbuf,0xff,c->wbuf_pagesize);
693 /* adjust write buffer offset, else we get a non contiguous write bug */
694 c->wbuf_ofs += c->wbuf_pagesize;
695 c->wbuf_len = 0;
696 return 0;
697}
698
699/* Trigger garbage collection to flush the write-buffer.
700 If ino arg is zero, do it if _any_ real (i.e. not GC) writes are
701 outstanding. If ino arg non-zero, do it only if a write for the
702 given inode is outstanding. */
703int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino)
704{
705 uint32_t old_wbuf_ofs;
706 uint32_t old_wbuf_len;
707 int ret = 0;
708
709 jffs2_dbg(1, "jffs2_flush_wbuf_gc() called for ino #%u...\n", ino);
710
711 if (!c->wbuf)
712 return 0;
713
714 mutex_lock(&c->alloc_sem);
715 if (!jffs2_wbuf_pending_for_ino(c, ino)) {
716 jffs2_dbg(1, "Ino #%d not pending in wbuf. Returning\n", ino);
717 mutex_unlock(&c->alloc_sem);
718 return 0;
719 }
720
721 old_wbuf_ofs = c->wbuf_ofs;
722 old_wbuf_len = c->wbuf_len;
723
724 if (c->unchecked_size) {
725 /* GC won't make any progress for a while */
726 jffs2_dbg(1, "%s(): padding. Not finished checking\n",
727 __func__);
728 down_write(&c->wbuf_sem);
729 ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
730 /* retry flushing wbuf in case jffs2_wbuf_recover
731 left some data in the wbuf */
732 if (ret)
733 ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
734 up_write(&c->wbuf_sem);
735 } else while (old_wbuf_len &&
736 old_wbuf_ofs == c->wbuf_ofs) {
737
738 mutex_unlock(&c->alloc_sem);
739
740 jffs2_dbg(1, "%s(): calls gc pass\n", __func__);
741
742 ret = jffs2_garbage_collect_pass(c);
743 if (ret) {
744 /* GC failed. Flush it with padding instead */
745 mutex_lock(&c->alloc_sem);
746 down_write(&c->wbuf_sem);
747 ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
748 /* retry flushing wbuf in case jffs2_wbuf_recover
749 left some data in the wbuf */
750 if (ret)
751 ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
752 up_write(&c->wbuf_sem);
753 break;
754 }
755 mutex_lock(&c->alloc_sem);
756 }
757
758 jffs2_dbg(1, "%s(): ends...\n", __func__);
759
760 mutex_unlock(&c->alloc_sem);
761 return ret;
762}
763
764/* Pad write-buffer to end and write it, wasting space. */
765int jffs2_flush_wbuf_pad(struct jffs2_sb_info *c)
766{
767 int ret;
768
769 if (!c->wbuf)
770 return 0;
771
772 down_write(&c->wbuf_sem);
773 ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
774 /* retry - maybe wbuf recover left some data in wbuf. */
775 if (ret)
776 ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
777 up_write(&c->wbuf_sem);
778
779 return ret;
780}
781
782static size_t jffs2_fill_wbuf(struct jffs2_sb_info *c, const uint8_t *buf,
783 size_t len)
784{
785 if (len && !c->wbuf_len && (len >= c->wbuf_pagesize))
786 return 0;
787
788 if (len > (c->wbuf_pagesize - c->wbuf_len))
789 len = c->wbuf_pagesize - c->wbuf_len;
790 memcpy(c->wbuf + c->wbuf_len, buf, len);
791 c->wbuf_len += (uint32_t) len;
792 return len;
793}
794
795int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs,
796 unsigned long count, loff_t to, size_t *retlen,
797 uint32_t ino)
798{
799 struct jffs2_eraseblock *jeb;
800 size_t wbuf_retlen, donelen = 0;
801 uint32_t outvec_to = to;
802 int ret, invec;
803
804 /* If not writebuffered flash, don't bother */
805 if (!jffs2_is_writebuffered(c))
806 return jffs2_flash_direct_writev(c, invecs, count, to, retlen);
807
808 down_write(&c->wbuf_sem);
809
810 /* If wbuf_ofs is not initialized, set it to target address */
811 if (c->wbuf_ofs == 0xFFFFFFFF) {
812 c->wbuf_ofs = PAGE_DIV(to);
813 c->wbuf_len = PAGE_MOD(to);
814 memset(c->wbuf,0xff,c->wbuf_pagesize);
815 }
816
817 /*
818 * Sanity checks on target address. It's permitted to write
819 * at PAD(c->wbuf_len+c->wbuf_ofs), and it's permitted to
820 * write at the beginning of a new erase block. Anything else,
821 * and you die. New block starts at xxx000c (0-b = block
822 * header)
823 */
824 if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs)) {
825 /* It's a write to a new block */
826 if (c->wbuf_len) {
827 jffs2_dbg(1, "%s(): to 0x%lx causes flush of wbuf at 0x%08x\n",
828 __func__, (unsigned long)to, c->wbuf_ofs);
829 ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
830 if (ret)
831 goto outerr;
832 }
833 /* set pointer to new block */
834 c->wbuf_ofs = PAGE_DIV(to);
835 c->wbuf_len = PAGE_MOD(to);
836 }
837
838 if (to != PAD(c->wbuf_ofs + c->wbuf_len)) {
839 /* We're not writing immediately after the writebuffer. Bad. */
840 pr_crit("%s(): Non-contiguous write to %08lx\n",
841 __func__, (unsigned long)to);
842 if (c->wbuf_len)
843 pr_crit("wbuf was previously %08x-%08x\n",
844 c->wbuf_ofs, c->wbuf_ofs + c->wbuf_len);
845 BUG();
846 }
847
848 /* adjust alignment offset */
849 if (c->wbuf_len != PAGE_MOD(to)) {
850 c->wbuf_len = PAGE_MOD(to);
851 /* take care of alignment to next page */
852 if (!c->wbuf_len) {
853 c->wbuf_len = c->wbuf_pagesize;
854 ret = __jffs2_flush_wbuf(c, NOPAD);
855 if (ret)
856 goto outerr;
857 }
858 }
859
860 for (invec = 0; invec < count; invec++) {
861 int vlen = invecs[invec].iov_len;
862 uint8_t *v = invecs[invec].iov_base;
863
864 wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
865
866 if (c->wbuf_len == c->wbuf_pagesize) {
867 ret = __jffs2_flush_wbuf(c, NOPAD);
868 if (ret)
869 goto outerr;
870 }
871 vlen -= wbuf_retlen;
872 outvec_to += wbuf_retlen;
873 donelen += wbuf_retlen;
874 v += wbuf_retlen;
875
876 if (vlen >= c->wbuf_pagesize) {
877 ret = mtd_write(c->mtd, outvec_to, PAGE_DIV(vlen),
878 &wbuf_retlen, v);
879 if (ret < 0 || wbuf_retlen != PAGE_DIV(vlen))
880 goto outfile;
881
882 vlen -= wbuf_retlen;
883 outvec_to += wbuf_retlen;
884 c->wbuf_ofs = outvec_to;
885 donelen += wbuf_retlen;
886 v += wbuf_retlen;
887 }
888
889 wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
890 if (c->wbuf_len == c->wbuf_pagesize) {
891 ret = __jffs2_flush_wbuf(c, NOPAD);
892 if (ret)
893 goto outerr;
894 }
895
896 outvec_to += wbuf_retlen;
897 donelen += wbuf_retlen;
898 }
899
900 /*
901 * If there's a remainder in the wbuf and it's a non-GC write,
902 * remember that the wbuf affects this ino
903 */
904 *retlen = donelen;
905
906 if (jffs2_sum_active()) {
907 int res = jffs2_sum_add_kvec(c, invecs, count, (uint32_t) to);
908 if (res)
909 return res;
910 }
911
912 if (c->wbuf_len && ino)
913 jffs2_wbuf_dirties_inode(c, ino);
914
915 ret = 0;
916 up_write(&c->wbuf_sem);
917 return ret;
918
919outfile:
920 /*
921 * At this point we have no problem, c->wbuf is empty. However
922 * refile nextblock to avoid writing again to same address.
923 */
924
925 spin_lock(&c->erase_completion_lock);
926
927 jeb = &c->blocks[outvec_to / c->sector_size];
928 jffs2_block_refile(c, jeb, REFILE_ANYWAY);
929
930 spin_unlock(&c->erase_completion_lock);
931
932outerr:
933 *retlen = 0;
934 up_write(&c->wbuf_sem);
935 return ret;
936}
937
938/*
939 * This is the entry for flash write.
940 * Check, if we work on NAND FLASH, if so build an kvec and write it via vritev
941*/
942int jffs2_flash_write(struct jffs2_sb_info *c, loff_t ofs, size_t len,
943 size_t *retlen, const u_char *buf)
944{
945 struct kvec vecs[1];
946
947 if (!jffs2_is_writebuffered(c))
948 return jffs2_flash_direct_write(c, ofs, len, retlen, buf);
949
950 vecs[0].iov_base = (unsigned char *) buf;
951 vecs[0].iov_len = len;
952 return jffs2_flash_writev(c, vecs, 1, ofs, retlen, 0);
953}
954
955/*
956 Handle readback from writebuffer and ECC failure return
957*/
958int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *retlen, u_char *buf)
959{
960 loff_t orbf = 0, owbf = 0, lwbf = 0;
961 int ret;
962
963 if (!jffs2_is_writebuffered(c))
964 return mtd_read(c->mtd, ofs, len, retlen, buf);
965
966 /* Read flash */
967 down_read(&c->wbuf_sem);
968 ret = mtd_read(c->mtd, ofs, len, retlen, buf);
969
970 if ( (ret == -EBADMSG || ret == -EUCLEAN) && (*retlen == len) ) {
971 if (ret == -EBADMSG)
972 pr_warn("mtd->read(0x%zx bytes from 0x%llx) returned ECC error\n",
973 len, ofs);
974 /*
975 * We have the raw data without ECC correction in the buffer,
976 * maybe we are lucky and all data or parts are correct. We
977 * check the node. If data are corrupted node check will sort
978 * it out. We keep this block, it will fail on write or erase
979 * and the we mark it bad. Or should we do that now? But we
980 * should give him a chance. Maybe we had a system crash or
981 * power loss before the ecc write or a erase was completed.
982 * So we return success. :)
983 */
984 ret = 0;
985 }
986
987 /* if no writebuffer available or write buffer empty, return */
988 if (!c->wbuf_pagesize || !c->wbuf_len)
989 goto exit;
990
991 /* if we read in a different block, return */
992 if (SECTOR_ADDR(ofs) != SECTOR_ADDR(c->wbuf_ofs))
993 goto exit;
994
995 if (ofs >= c->wbuf_ofs) {
996 owbf = (ofs - c->wbuf_ofs); /* offset in write buffer */
997 if (owbf > c->wbuf_len) /* is read beyond write buffer ? */
998 goto exit;
999 lwbf = c->wbuf_len - owbf; /* number of bytes to copy */
1000 if (lwbf > len)
1001 lwbf = len;
1002 } else {
1003 orbf = (c->wbuf_ofs - ofs); /* offset in read buffer */
1004 if (orbf > len) /* is write beyond write buffer ? */
1005 goto exit;
1006 lwbf = len - orbf; /* number of bytes to copy */
1007 if (lwbf > c->wbuf_len)
1008 lwbf = c->wbuf_len;
1009 }
1010 if (lwbf > 0)
1011 memcpy(buf+orbf,c->wbuf+owbf,lwbf);
1012
1013exit:
1014 up_read(&c->wbuf_sem);
1015 return ret;
1016}
1017
1018#define NR_OOB_SCAN_PAGES 4
1019
1020/* For historical reasons we use only 8 bytes for OOB clean marker */
1021#define OOB_CM_SIZE 8
1022
1023static const struct jffs2_unknown_node oob_cleanmarker =
1024{
1025 .magic = constant_cpu_to_je16(JFFS2_MAGIC_BITMASK),
1026 .nodetype = constant_cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
1027 .totlen = constant_cpu_to_je32(8)
1028};
1029
1030/*
1031 * Check, if the out of band area is empty. This function knows about the clean
1032 * marker and if it is present in OOB, treats the OOB as empty anyway.
1033 */
1034int jffs2_check_oob_empty(struct jffs2_sb_info *c,
1035 struct jffs2_eraseblock *jeb, int mode)
1036{
1037 int i, ret;
1038 int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
1039 struct mtd_oob_ops ops;
1040
1041 ops.mode = MTD_OPS_AUTO_OOB;
1042 ops.ooblen = NR_OOB_SCAN_PAGES * c->oobavail;
1043 ops.oobbuf = c->oobbuf;
1044 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
1045 ops.datbuf = NULL;
1046
1047 ret = mtd_read_oob(c->mtd, jeb->offset, &ops);
1048 if ((ret && !mtd_is_bitflip(ret)) || ops.oobretlen != ops.ooblen) {
1049 pr_err("cannot read OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
1050 jeb->offset, ops.ooblen, ops.oobretlen, ret);
1051 if (!ret || mtd_is_bitflip(ret))
1052 ret = -EIO;
1053 return ret;
1054 }
1055
1056 for(i = 0; i < ops.ooblen; i++) {
1057 if (mode && i < cmlen)
1058 /* Yeah, we know about the cleanmarker */
1059 continue;
1060
1061 if (ops.oobbuf[i] != 0xFF) {
1062 jffs2_dbg(2, "Found %02x at %x in OOB for "
1063 "%08x\n", ops.oobbuf[i], i, jeb->offset);
1064 return 1;
1065 }
1066 }
1067
1068 return 0;
1069}
1070
1071/*
1072 * Check for a valid cleanmarker.
1073 * Returns: 0 if a valid cleanmarker was found
1074 * 1 if no cleanmarker was found
1075 * negative error code if an error occurred
1076 */
1077int jffs2_check_nand_cleanmarker(struct jffs2_sb_info *c,
1078 struct jffs2_eraseblock *jeb)
1079{
1080 struct mtd_oob_ops ops;
1081 int i, ret, cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
1082
1083 ops.mode = MTD_OPS_AUTO_OOB;
1084 ops.ooblen = cmlen;
1085 ops.oobbuf = c->oobbuf;
1086 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
1087 ops.datbuf = NULL;
1088
1089 ret = mtd_read_oob(c->mtd, jeb->offset, &ops);
1090 if ((ret && !mtd_is_bitflip(ret)) || ops.oobretlen != ops.ooblen) {
1091 pr_err("cannot read OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
1092 jeb->offset, ops.ooblen, ops.oobretlen, ret);
1093 if (!ret || mtd_is_bitflip(ret))
1094 ret = -EIO;
1095 return ret;
1096 }
1097 if(jffs2_cleanmarker_oob(c))
1098 return !!memcmp(&oob_cleanmarker, c->oobbuf, cmlen);
1099 else {
1100 for(i = 0; i < cmlen; i++){
1101 if(ops.oobbuf[i] != 0xFF){
1102 jffs2_dbg(2,"Found dirty data %02x at %x in OOB for "
1103 "%08xYn", ops.oobbuf[i], i, jeb->offset);
1104 return 1;
1105 }
1106 }
1107 return 0;
1108 }
1109
1110}
1111
1112int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c,
1113 struct jffs2_eraseblock *jeb)
1114{
1115 int ret;
1116 struct mtd_oob_ops ops;
1117 int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
1118
1119 ops.mode = MTD_OPS_AUTO_OOB;
1120 ops.ooblen = cmlen;
1121 ops.oobbuf = (uint8_t *)&oob_cleanmarker;
1122 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
1123 ops.datbuf = NULL;
1124
1125 ret = mtd_write_oob(c->mtd, jeb->offset, &ops);
1126 if (ret || ops.oobretlen != ops.ooblen) {
1127 pr_err("cannot write OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
1128 jeb->offset, ops.ooblen, ops.oobretlen, ret);
1129 if (!ret)
1130 ret = -EIO;
1131 return ret;
1132 }
1133
1134 return 0;
1135}
1136
1137/*
1138 * On NAND we try to mark this block bad. If the block was erased more
1139 * than MAX_ERASE_FAILURES we mark it finally bad.
1140 * Don't care about failures. This block remains on the erase-pending
1141 * or badblock list as long as nobody manipulates the flash with
1142 * a bootloader or something like that.
1143 */
1144
1145int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
1146{
1147 int ret;
1148
1149 /* if the count is < max, we try to write the counter to the 2nd page oob area */
1150 if( ++jeb->bad_count < MAX_ERASE_FAILURES)
1151 return 0;
1152
1153 pr_warn("marking eraseblock at %08x as bad\n", bad_offset);
1154 ret = mtd_block_markbad(c->mtd, bad_offset);
1155
1156 if (ret) {
1157 jffs2_dbg(1, "%s(): Write failed for block at %08x: error %d\n",
1158 __func__, jeb->offset, ret);
1159 return ret;
1160 }
1161 return 1;
1162}
1163
1164int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
1165{
1166 struct nand_ecclayout *oinfo = c->mtd->ecclayout;
1167
1168 if (!c->mtd->oobsize)
1169 return 0;
1170
1171 /* Cleanmarker is out-of-band, so inline size zero */
1172 c->cleanmarker_size = 0;
1173
1174 if (!oinfo || oinfo->oobavail == 0) {
1175 pr_err("inconsistent device description\n");
1176 return -EINVAL;
1177 }
1178
1179 jffs2_dbg(1, "using OOB on NAND\n");
1180
1181 c->oobavail = oinfo->oobavail;
1182
1183 /* Initialise write buffer */
1184 init_rwsem(&c->wbuf_sem);
1185 c->wbuf_pagesize = c->mtd->writesize;
1186 c->wbuf_ofs = 0xFFFFFFFF;
1187
1188 c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1189 if (!c->wbuf)
1190 return -ENOMEM;
1191
1192 c->oobbuf = kmalloc(NR_OOB_SCAN_PAGES * c->oobavail, GFP_KERNEL);
1193 if (!c->oobbuf) {
1194 kfree(c->wbuf);
1195 return -ENOMEM;
1196 }
1197
1198#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1199 c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1200 if (!c->wbuf_verify) {
1201 kfree(c->oobbuf);
1202 kfree(c->wbuf);
1203 return -ENOMEM;
1204 }
1205#endif
1206 return 0;
1207}
1208
1209void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)
1210{
1211#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1212 kfree(c->wbuf_verify);
1213#endif
1214 kfree(c->wbuf);
1215 kfree(c->oobbuf);
1216}
1217
1218int jffs2_dataflash_setup(struct jffs2_sb_info *c) {
1219 c->cleanmarker_size = 0; /* No cleanmarkers needed */
1220
1221 /* Initialize write buffer */
1222 init_rwsem(&c->wbuf_sem);
1223
1224
1225 c->wbuf_pagesize = c->mtd->erasesize;
1226
1227 /* Find a suitable c->sector_size
1228 * - Not too much sectors
1229 * - Sectors have to be at least 4 K + some bytes
1230 * - All known dataflashes have erase sizes of 528 or 1056
1231 * - we take at least 8 eraseblocks and want to have at least 8K size
1232 * - The concatenation should be a power of 2
1233 */
1234
1235 c->sector_size = 8 * c->mtd->erasesize;
1236
1237 while (c->sector_size < 8192) {
1238 c->sector_size *= 2;
1239 }
1240
1241 /* It may be necessary to adjust the flash size */
1242 c->flash_size = c->mtd->size;
1243
1244 if ((c->flash_size % c->sector_size) != 0) {
1245 c->flash_size = (c->flash_size / c->sector_size) * c->sector_size;
1246 pr_warn("flash size adjusted to %dKiB\n", c->flash_size);
1247 };
1248
1249 c->wbuf_ofs = 0xFFFFFFFF;
1250 c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1251 if (!c->wbuf)
1252 return -ENOMEM;
1253
1254#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1255 c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1256 if (!c->wbuf_verify) {
1257 kfree(c->oobbuf);
1258 kfree(c->wbuf);
1259 return -ENOMEM;
1260 }
1261#endif
1262
1263 pr_info("write-buffering enabled buffer (%d) erasesize (%d)\n",
1264 c->wbuf_pagesize, c->sector_size);
1265
1266 return 0;
1267}
1268
1269void jffs2_dataflash_cleanup(struct jffs2_sb_info *c) {
1270#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1271 kfree(c->wbuf_verify);
1272#endif
1273 kfree(c->wbuf);
1274}
1275
1276int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c) {
1277 /* Cleanmarker currently occupies whole programming regions,
1278 * either one or 2 for 8Byte STMicro flashes. */
1279 c->cleanmarker_size = max(16u, c->mtd->writesize);
1280
1281 /* Initialize write buffer */
1282 init_rwsem(&c->wbuf_sem);
1283 c->wbuf_pagesize = c->mtd->writesize;
1284 c->wbuf_ofs = 0xFFFFFFFF;
1285
1286 c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1287 if (!c->wbuf)
1288 return -ENOMEM;
1289
1290#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1291 c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1292 if (!c->wbuf_verify) {
1293 kfree(c->wbuf);
1294 return -ENOMEM;
1295 }
1296#endif
1297 return 0;
1298}
1299
1300void jffs2_nor_wbuf_flash_cleanup(struct jffs2_sb_info *c) {
1301#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
1302 kfree(c->wbuf_verify);
1303#endif
1304 kfree(c->wbuf);
1305}
1306
1307int jffs2_ubivol_setup(struct jffs2_sb_info *c) {
1308 c->cleanmarker_size = 0;
1309
1310 if (c->mtd->writesize == 1)
1311 /* We do not need write-buffer */
1312 return 0;
1313
1314 init_rwsem(&c->wbuf_sem);
1315
1316 c->wbuf_pagesize = c->mtd->writesize;
1317 c->wbuf_ofs = 0xFFFFFFFF;
1318 c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1319 if (!c->wbuf)
1320 return -ENOMEM;
1321
1322 pr_info("write-buffering enabled buffer (%d) erasesize (%d)\n",
1323 c->wbuf_pagesize, c->sector_size);
1324
1325 return 0;
1326}
1327
1328void jffs2_ubivol_cleanup(struct jffs2_sb_info *c) {
1329 kfree(c->wbuf);
1330}