erase.c 13 KB

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