erase.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001-2003 Red Hat, Inc.
  5. *
  6. * Created by David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. * $Id: erase.c,v 1.80 2005/07/14 19:46:24 joern Exp $
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/compiler.h>
  17. #include <linux/crc32.h>
  18. #include <linux/sched.h>
  19. #include <linux/pagemap.h>
  20. #include "nodelist.h"
  21. struct erase_priv_struct {
  22. struct jffs2_eraseblock *jeb;
  23. struct jffs2_sb_info *c;
  24. };
  25. #ifndef __ECOS
  26. static void jffs2_erase_callback(struct erase_info *);
  27. #endif
  28. static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
  29. static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
  30. static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
  31. static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
  32. static void jffs2_erase_block(struct jffs2_sb_info *c,
  33. struct jffs2_eraseblock *jeb)
  34. {
  35. int ret;
  36. uint32_t bad_offset;
  37. #ifdef __ECOS
  38. ret = jffs2_flash_erase(c, jeb);
  39. if (!ret) {
  40. jffs2_erase_succeeded(c, jeb);
  41. return;
  42. }
  43. bad_offset = jeb->offset;
  44. #else /* Linux */
  45. struct erase_info *instr;
  46. D1(printk(KERN_DEBUG "jffs2_erase_block(): erase block %#x (range %#x-%#x)\n", jeb->offset, jeb->offset, jeb->offset + c->sector_size));
  47. instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
  48. if (!instr) {
  49. printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
  50. spin_lock(&c->erase_completion_lock);
  51. list_del(&jeb->list);
  52. list_add(&jeb->list, &c->erase_pending_list);
  53. c->erasing_size -= c->sector_size;
  54. c->dirty_size += c->sector_size;
  55. jeb->dirty_size = c->sector_size;
  56. spin_unlock(&c->erase_completion_lock);
  57. return;
  58. }
  59. memset(instr, 0, sizeof(*instr));
  60. instr->mtd = c->mtd;
  61. instr->addr = jeb->offset;
  62. instr->len = c->sector_size;
  63. instr->callback = jffs2_erase_callback;
  64. instr->priv = (unsigned long)(&instr[1]);
  65. instr->fail_addr = 0xffffffff;
  66. ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
  67. ((struct erase_priv_struct *)instr->priv)->c = c;
  68. ret = c->mtd->erase(c->mtd, instr);
  69. if (!ret)
  70. return;
  71. bad_offset = instr->fail_addr;
  72. kfree(instr);
  73. #endif /* __ECOS */
  74. if (ret == -ENOMEM || ret == -EAGAIN) {
  75. /* Erase failed immediately. Refile it on the list */
  76. D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret));
  77. spin_lock(&c->erase_completion_lock);
  78. list_del(&jeb->list);
  79. list_add(&jeb->list, &c->erase_pending_list);
  80. c->erasing_size -= c->sector_size;
  81. c->dirty_size += c->sector_size;
  82. jeb->dirty_size = c->sector_size;
  83. spin_unlock(&c->erase_completion_lock);
  84. return;
  85. }
  86. if (ret == -EROFS)
  87. printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset);
  88. else
  89. printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret);
  90. jffs2_erase_failed(c, jeb, bad_offset);
  91. }
  92. void jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
  93. {
  94. struct jffs2_eraseblock *jeb;
  95. down(&c->erase_free_sem);
  96. spin_lock(&c->erase_completion_lock);
  97. while (!list_empty(&c->erase_complete_list) ||
  98. !list_empty(&c->erase_pending_list)) {
  99. if (!list_empty(&c->erase_complete_list)) {
  100. jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
  101. list_del(&jeb->list);
  102. spin_unlock(&c->erase_completion_lock);
  103. jffs2_mark_erased_block(c, jeb);
  104. if (!--count) {
  105. D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving\n"));
  106. goto done;
  107. }
  108. } else if (!list_empty(&c->erase_pending_list)) {
  109. jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
  110. D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
  111. list_del(&jeb->list);
  112. c->erasing_size += c->sector_size;
  113. c->wasted_size -= jeb->wasted_size;
  114. c->free_size -= jeb->free_size;
  115. c->used_size -= jeb->used_size;
  116. c->dirty_size -= jeb->dirty_size;
  117. jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
  118. jffs2_free_all_node_refs(c, jeb);
  119. list_add(&jeb->list, &c->erasing_list);
  120. spin_unlock(&c->erase_completion_lock);
  121. jffs2_erase_block(c, jeb);
  122. } else {
  123. BUG();
  124. }
  125. /* Be nice */
  126. cond_resched();
  127. spin_lock(&c->erase_completion_lock);
  128. }
  129. spin_unlock(&c->erase_completion_lock);
  130. done:
  131. D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
  132. up(&c->erase_free_sem);
  133. }
  134. static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  135. {
  136. D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset));
  137. spin_lock(&c->erase_completion_lock);
  138. list_del(&jeb->list);
  139. list_add_tail(&jeb->list, &c->erase_complete_list);
  140. spin_unlock(&c->erase_completion_lock);
  141. /* Ensure that kupdated calls us again to mark them clean */
  142. jffs2_erase_pending_trigger(c);
  143. }
  144. static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
  145. {
  146. /* For NAND, if the failure did not occur at the device level for a
  147. specific physical page, don't bother updating the bad block table. */
  148. if (jffs2_cleanmarker_oob(c) && (bad_offset != 0xffffffff)) {
  149. /* We had a device-level failure to erase. Let's see if we've
  150. failed too many times. */
  151. if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
  152. /* We'd like to give this block another try. */
  153. spin_lock(&c->erase_completion_lock);
  154. list_del(&jeb->list);
  155. list_add(&jeb->list, &c->erase_pending_list);
  156. c->erasing_size -= c->sector_size;
  157. c->dirty_size += c->sector_size;
  158. jeb->dirty_size = c->sector_size;
  159. spin_unlock(&c->erase_completion_lock);
  160. return;
  161. }
  162. }
  163. spin_lock(&c->erase_completion_lock);
  164. c->erasing_size -= c->sector_size;
  165. c->bad_size += c->sector_size;
  166. list_del(&jeb->list);
  167. list_add(&jeb->list, &c->bad_list);
  168. c->nr_erasing_blocks--;
  169. spin_unlock(&c->erase_completion_lock);
  170. wake_up(&c->erase_wait);
  171. }
  172. #ifndef __ECOS
  173. static void jffs2_erase_callback(struct erase_info *instr)
  174. {
  175. struct erase_priv_struct *priv = (void *)instr->priv;
  176. if(instr->state != MTD_ERASE_DONE) {
  177. printk(KERN_WARNING "Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr->addr, instr->state);
  178. jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
  179. } else {
  180. jffs2_erase_succeeded(priv->c, priv->jeb);
  181. }
  182. kfree(instr);
  183. }
  184. #endif /* !__ECOS */
  185. /* Hmmm. Maybe we should accept the extra space it takes and make
  186. this a standard doubly-linked list? */
  187. static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
  188. struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
  189. {
  190. struct jffs2_inode_cache *ic = NULL;
  191. struct jffs2_raw_node_ref **prev;
  192. prev = &ref->next_in_ino;
  193. /* Walk the inode's list once, removing any nodes from this eraseblock */
  194. while (1) {
  195. if (!(*prev)->next_in_ino) {
  196. /* We're looking at the jffs2_inode_cache, which is
  197. at the end of the linked list. Stash it and continue
  198. from the beginning of the list */
  199. ic = (struct jffs2_inode_cache *)(*prev);
  200. prev = &ic->nodes;
  201. continue;
  202. }
  203. if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
  204. /* It's in the block we're erasing */
  205. struct jffs2_raw_node_ref *this;
  206. this = *prev;
  207. *prev = this->next_in_ino;
  208. this->next_in_ino = NULL;
  209. if (this == ref)
  210. break;
  211. continue;
  212. }
  213. /* Not to be deleted. Skip */
  214. prev = &((*prev)->next_in_ino);
  215. }
  216. /* PARANOIA */
  217. if (!ic) {
  218. printk(KERN_WARNING "inode_cache not found in remove_node_refs()!!\n");
  219. return;
  220. }
  221. D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
  222. jeb->offset, jeb->offset + c->sector_size, ic->ino));
  223. D2({
  224. int i=0;
  225. struct jffs2_raw_node_ref *this;
  226. printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n" KERN_DEBUG);
  227. this = ic->nodes;
  228. while(this) {
  229. printk( "0x%08x(%d)->", ref_offset(this), ref_flags(this));
  230. if (++i == 5) {
  231. printk("\n" KERN_DEBUG);
  232. i=0;
  233. }
  234. this = this->next_in_ino;
  235. }
  236. printk("\n");
  237. });
  238. if (ic->nodes == (void *)ic && ic->nlink == 0)
  239. jffs2_del_ino_cache(c, ic);
  240. }
  241. static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  242. {
  243. struct jffs2_raw_node_ref *ref;
  244. D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
  245. while(jeb->first_node) {
  246. ref = jeb->first_node;
  247. jeb->first_node = ref->next_phys;
  248. /* Remove from the inode-list */
  249. if (ref->next_in_ino)
  250. jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
  251. /* else it was a non-inode node or already removed, so don't bother */
  252. jffs2_free_raw_node_ref(ref);
  253. }
  254. jeb->last_node = NULL;
  255. }
  256. static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
  257. {
  258. void *ebuf;
  259. uint32_t ofs;
  260. size_t retlen;
  261. int ret = -EIO;
  262. ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  263. if (!ebuf) {
  264. printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset);
  265. return -EAGAIN;
  266. }
  267. D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
  268. for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
  269. uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
  270. int i;
  271. *bad_offset = ofs;
  272. ret = jffs2_flash_read(c, ofs, readlen, &retlen, ebuf);
  273. if (ret) {
  274. printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
  275. goto fail;
  276. }
  277. if (retlen != readlen) {
  278. printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen);
  279. goto fail;
  280. }
  281. for (i=0; i<readlen; i += sizeof(unsigned long)) {
  282. /* It's OK. We know it's properly aligned */
  283. unsigned long *datum = ebuf + i;
  284. if (*datum + 1) {
  285. *bad_offset += i;
  286. printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset);
  287. goto fail;
  288. }
  289. }
  290. ofs += readlen;
  291. cond_resched();
  292. }
  293. ret = 0;
  294. fail:
  295. kfree(ebuf);
  296. return ret;
  297. }
  298. static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  299. {
  300. struct jffs2_raw_node_ref *marker_ref = NULL;
  301. size_t retlen;
  302. int ret;
  303. uint32_t bad_offset;
  304. switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
  305. case -EAGAIN: goto refile;
  306. case -EIO: goto filebad;
  307. }
  308. /* Write the erase complete marker */
  309. D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
  310. bad_offset = jeb->offset;
  311. /* Cleanmarker in oob area or no cleanmarker at all ? */
  312. if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
  313. if (jffs2_cleanmarker_oob(c)) {
  314. if (jffs2_write_nand_cleanmarker(c, jeb))
  315. goto filebad;
  316. }
  317. jeb->first_node = jeb->last_node = NULL;
  318. jeb->free_size = c->sector_size;
  319. jeb->used_size = 0;
  320. jeb->dirty_size = 0;
  321. jeb->wasted_size = 0;
  322. } else {
  323. struct kvec vecs[1];
  324. struct jffs2_unknown_node marker = {
  325. .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
  326. .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
  327. .totlen = cpu_to_je32(c->cleanmarker_size)
  328. };
  329. marker_ref = jffs2_alloc_raw_node_ref();
  330. if (!marker_ref) {
  331. printk(KERN_WARNING "Failed to allocate raw node ref for clean marker. Refiling\n");
  332. goto refile;
  333. }
  334. marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
  335. vecs[0].iov_base = (unsigned char *) &marker;
  336. vecs[0].iov_len = sizeof(marker);
  337. ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
  338. if (ret || retlen != sizeof(marker)) {
  339. if (ret)
  340. printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
  341. jeb->offset, ret);
  342. else
  343. printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
  344. jeb->offset, sizeof(marker), retlen);
  345. jffs2_free_raw_node_ref(marker_ref);
  346. goto filebad;
  347. }
  348. marker_ref->next_in_ino = NULL;
  349. marker_ref->next_phys = NULL;
  350. marker_ref->flash_offset = jeb->offset | REF_NORMAL;
  351. marker_ref->__totlen = c->cleanmarker_size;
  352. jeb->first_node = jeb->last_node = marker_ref;
  353. jeb->free_size = c->sector_size - c->cleanmarker_size;
  354. jeb->used_size = c->cleanmarker_size;
  355. jeb->dirty_size = 0;
  356. jeb->wasted_size = 0;
  357. }
  358. spin_lock(&c->erase_completion_lock);
  359. c->erasing_size -= c->sector_size;
  360. c->free_size += jeb->free_size;
  361. c->used_size += jeb->used_size;
  362. ACCT_SANITY_CHECK(c,jeb);
  363. D1(ACCT_PARANOIA_CHECK(jeb));
  364. list_add_tail(&jeb->list, &c->free_list);
  365. c->nr_erasing_blocks--;
  366. c->nr_free_blocks++;
  367. spin_unlock(&c->erase_completion_lock);
  368. wake_up(&c->erase_wait);
  369. return;
  370. filebad:
  371. spin_lock(&c->erase_completion_lock);
  372. /* Stick it on a list (any list) so erase_failed can take it
  373. right off again. Silly, but shouldn't happen often. */
  374. list_add(&jeb->list, &c->erasing_list);
  375. spin_unlock(&c->erase_completion_lock);
  376. jffs2_erase_failed(c, jeb, bad_offset);
  377. return;
  378. refile:
  379. /* Stick it back on the list from whence it came and come back later */
  380. jffs2_erase_pending_trigger(c);
  381. spin_lock(&c->erase_completion_lock);
  382. list_add(&jeb->list, &c->erase_complete_list);
  383. spin_unlock(&c->erase_completion_lock);
  384. return;
  385. }