wbuf.c 34 KB

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