erase.c 13 KB

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