erase.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. mutex_lock(&c->erase_free_sem);
  49. spin_lock(&c->erase_completion_lock);
  50. list_move(&jeb->list, &c->erase_pending_list);
  51. c->erasing_size -= c->sector_size;
  52. c->dirty_size += c->sector_size;
  53. jeb->dirty_size = c->sector_size;
  54. spin_unlock(&c->erase_completion_lock);
  55. mutex_unlock(&c->erase_free_sem);
  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 = MTD_FAIL_ADDR_UNKNOWN;
  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. mutex_lock(&c->erase_free_sem);
  77. spin_lock(&c->erase_completion_lock);
  78. list_move(&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. mutex_unlock(&c->erase_free_sem);
  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. int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
  93. {
  94. struct jffs2_eraseblock *jeb;
  95. int work_done = 0;
  96. mutex_lock(&c->erase_free_sem);
  97. spin_lock(&c->erase_completion_lock);
  98. while (!list_empty(&c->erase_complete_list) ||
  99. !list_empty(&c->erase_pending_list)) {
  100. if (!list_empty(&c->erase_complete_list)) {
  101. jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
  102. list_move(&jeb->list, &c->erase_checking_list);
  103. spin_unlock(&c->erase_completion_lock);
  104. mutex_unlock(&c->erase_free_sem);
  105. jffs2_mark_erased_block(c, jeb);
  106. work_done++;
  107. if (!--count) {
  108. D1(printk(KERN_DEBUG "Count reached. jffs2_erase_pending_blocks leaving\n"));
  109. goto done;
  110. }
  111. } else if (!list_empty(&c->erase_pending_list)) {
  112. jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
  113. D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
  114. list_del(&jeb->list);
  115. c->erasing_size += c->sector_size;
  116. c->wasted_size -= jeb->wasted_size;
  117. c->free_size -= jeb->free_size;
  118. c->used_size -= jeb->used_size;
  119. c->dirty_size -= jeb->dirty_size;
  120. jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
  121. jffs2_free_jeb_node_refs(c, jeb);
  122. list_add(&jeb->list, &c->erasing_list);
  123. spin_unlock(&c->erase_completion_lock);
  124. mutex_unlock(&c->erase_free_sem);
  125. jffs2_erase_block(c, jeb);
  126. } else {
  127. BUG();
  128. }
  129. /* Be nice */
  130. yield();
  131. mutex_lock(&c->erase_free_sem);
  132. spin_lock(&c->erase_completion_lock);
  133. }
  134. spin_unlock(&c->erase_completion_lock);
  135. mutex_unlock(&c->erase_free_sem);
  136. done:
  137. D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
  138. return work_done;
  139. }
  140. static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  141. {
  142. D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset));
  143. mutex_lock(&c->erase_free_sem);
  144. spin_lock(&c->erase_completion_lock);
  145. list_move_tail(&jeb->list, &c->erase_complete_list);
  146. /* Wake the GC thread to mark them clean */
  147. jffs2_garbage_collect_trigger(c);
  148. spin_unlock(&c->erase_completion_lock);
  149. mutex_unlock(&c->erase_free_sem);
  150. wake_up(&c->erase_wait);
  151. }
  152. static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
  153. {
  154. /* For NAND, if the failure did not occur at the device level for a
  155. specific physical page, don't bother updating the bad block table. */
  156. if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
  157. /* We had a device-level failure to erase. Let's see if we've
  158. failed too many times. */
  159. if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
  160. /* We'd like to give this block another try. */
  161. mutex_lock(&c->erase_free_sem);
  162. spin_lock(&c->erase_completion_lock);
  163. list_move(&jeb->list, &c->erase_pending_list);
  164. c->erasing_size -= c->sector_size;
  165. c->dirty_size += c->sector_size;
  166. jeb->dirty_size = c->sector_size;
  167. spin_unlock(&c->erase_completion_lock);
  168. mutex_unlock(&c->erase_free_sem);
  169. return;
  170. }
  171. }
  172. mutex_lock(&c->erase_free_sem);
  173. spin_lock(&c->erase_completion_lock);
  174. c->erasing_size -= c->sector_size;
  175. c->bad_size += c->sector_size;
  176. list_move(&jeb->list, &c->bad_list);
  177. c->nr_erasing_blocks--;
  178. spin_unlock(&c->erase_completion_lock);
  179. mutex_unlock(&c->erase_free_sem);
  180. wake_up(&c->erase_wait);
  181. }
  182. #ifndef __ECOS
  183. static void jffs2_erase_callback(struct erase_info *instr)
  184. {
  185. struct erase_priv_struct *priv = (void *)instr->priv;
  186. if(instr->state != MTD_ERASE_DONE) {
  187. printk(KERN_WARNING "Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n",
  188. (unsigned long long)instr->addr, instr->state);
  189. jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
  190. } else {
  191. jffs2_erase_succeeded(priv->c, priv->jeb);
  192. }
  193. kfree(instr);
  194. }
  195. #endif /* !__ECOS */
  196. /* Hmmm. Maybe we should accept the extra space it takes and make
  197. this a standard doubly-linked list? */
  198. static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
  199. struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
  200. {
  201. struct jffs2_inode_cache *ic = NULL;
  202. struct jffs2_raw_node_ref **prev;
  203. prev = &ref->next_in_ino;
  204. /* Walk the inode's list once, removing any nodes from this eraseblock */
  205. while (1) {
  206. if (!(*prev)->next_in_ino) {
  207. /* We're looking at the jffs2_inode_cache, which is
  208. at the end of the linked list. Stash it and continue
  209. from the beginning of the list */
  210. ic = (struct jffs2_inode_cache *)(*prev);
  211. prev = &ic->nodes;
  212. continue;
  213. }
  214. if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
  215. /* It's in the block we're erasing */
  216. struct jffs2_raw_node_ref *this;
  217. this = *prev;
  218. *prev = this->next_in_ino;
  219. this->next_in_ino = NULL;
  220. if (this == ref)
  221. break;
  222. continue;
  223. }
  224. /* Not to be deleted. Skip */
  225. prev = &((*prev)->next_in_ino);
  226. }
  227. /* PARANOIA */
  228. if (!ic) {
  229. JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
  230. " not found in remove_node_refs()!!\n");
  231. return;
  232. }
  233. D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
  234. jeb->offset, jeb->offset + c->sector_size, ic->ino));
  235. D2({
  236. int i=0;
  237. struct jffs2_raw_node_ref *this;
  238. printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n");
  239. this = ic->nodes;
  240. printk(KERN_DEBUG);
  241. while(this) {
  242. printk(KERN_CONT "0x%08x(%d)->",
  243. ref_offset(this), ref_flags(this));
  244. if (++i == 5) {
  245. printk(KERN_DEBUG);
  246. i=0;
  247. }
  248. this = this->next_in_ino;
  249. }
  250. printk(KERN_CONT "\n");
  251. });
  252. switch (ic->class) {
  253. #ifdef CONFIG_JFFS2_FS_XATTR
  254. case RAWNODE_CLASS_XATTR_DATUM:
  255. jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
  256. break;
  257. case RAWNODE_CLASS_XATTR_REF:
  258. jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
  259. break;
  260. #endif
  261. default:
  262. if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
  263. jffs2_del_ino_cache(c, ic);
  264. }
  265. }
  266. void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  267. {
  268. struct jffs2_raw_node_ref *block, *ref;
  269. D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
  270. block = ref = jeb->first_node;
  271. while (ref) {
  272. if (ref->flash_offset == REF_LINK_NODE) {
  273. ref = ref->next_in_ino;
  274. jffs2_free_refblock(block);
  275. block = ref;
  276. continue;
  277. }
  278. if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
  279. jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
  280. /* else it was a non-inode node or already removed, so don't bother */
  281. ref++;
  282. }
  283. jeb->first_node = jeb->last_node = NULL;
  284. }
  285. static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
  286. {
  287. void *ebuf;
  288. uint32_t ofs;
  289. size_t retlen;
  290. int ret = -EIO;
  291. if (c->mtd->point) {
  292. unsigned long *wordebuf;
  293. ret = c->mtd->point(c->mtd, jeb->offset, c->sector_size,
  294. &retlen, &ebuf, NULL);
  295. if (ret) {
  296. D1(printk(KERN_DEBUG "MTD point failed %d\n", ret));
  297. goto do_flash_read;
  298. }
  299. if (retlen < c->sector_size) {
  300. /* Don't muck about if it won't let us point to the whole erase sector */
  301. D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", retlen));
  302. c->mtd->unpoint(c->mtd, jeb->offset, retlen);
  303. goto do_flash_read;
  304. }
  305. wordebuf = ebuf-sizeof(*wordebuf);
  306. retlen /= sizeof(*wordebuf);
  307. do {
  308. if (*++wordebuf != ~0)
  309. break;
  310. } while(--retlen);
  311. c->mtd->unpoint(c->mtd, jeb->offset, c->sector_size);
  312. if (retlen) {
  313. printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08tx\n",
  314. *wordebuf, jeb->offset + c->sector_size-retlen*sizeof(*wordebuf));
  315. return -EIO;
  316. }
  317. return 0;
  318. }
  319. do_flash_read:
  320. ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  321. if (!ebuf) {
  322. printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n", jeb->offset);
  323. return -EAGAIN;
  324. }
  325. D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
  326. for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
  327. uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
  328. int i;
  329. *bad_offset = ofs;
  330. ret = c->mtd->read(c->mtd, ofs, readlen, &retlen, ebuf);
  331. if (ret) {
  332. printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
  333. ret = -EIO;
  334. goto fail;
  335. }
  336. if (retlen != readlen) {
  337. printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen);
  338. ret = -EIO;
  339. goto fail;
  340. }
  341. for (i=0; i<readlen; i += sizeof(unsigned long)) {
  342. /* It's OK. We know it's properly aligned */
  343. unsigned long *datum = ebuf + i;
  344. if (*datum + 1) {
  345. *bad_offset += i;
  346. printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", *datum, *bad_offset);
  347. ret = -EIO;
  348. goto fail;
  349. }
  350. }
  351. ofs += readlen;
  352. cond_resched();
  353. }
  354. ret = 0;
  355. fail:
  356. kfree(ebuf);
  357. return ret;
  358. }
  359. static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  360. {
  361. size_t retlen;
  362. int ret;
  363. uint32_t uninitialized_var(bad_offset);
  364. switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
  365. case -EAGAIN: goto refile;
  366. case -EIO: goto filebad;
  367. }
  368. /* Write the erase complete marker */
  369. D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
  370. bad_offset = jeb->offset;
  371. /* Cleanmarker in oob area or no cleanmarker at all ? */
  372. if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
  373. if (jffs2_cleanmarker_oob(c)) {
  374. if (jffs2_write_nand_cleanmarker(c, jeb))
  375. goto filebad;
  376. }
  377. } else {
  378. struct kvec vecs[1];
  379. struct jffs2_unknown_node marker = {
  380. .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
  381. .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
  382. .totlen = cpu_to_je32(c->cleanmarker_size)
  383. };
  384. jffs2_prealloc_raw_node_refs(c, jeb, 1);
  385. marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
  386. vecs[0].iov_base = (unsigned char *) &marker;
  387. vecs[0].iov_len = sizeof(marker);
  388. ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
  389. if (ret || retlen != sizeof(marker)) {
  390. if (ret)
  391. printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
  392. jeb->offset, ret);
  393. else
  394. printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
  395. jeb->offset, sizeof(marker), retlen);
  396. goto filebad;
  397. }
  398. }
  399. /* Everything else got zeroed before the erase */
  400. jeb->free_size = c->sector_size;
  401. mutex_lock(&c->erase_free_sem);
  402. spin_lock(&c->erase_completion_lock);
  403. c->erasing_size -= c->sector_size;
  404. c->free_size += c->sector_size;
  405. /* Account for cleanmarker now, if it's in-band */
  406. if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c))
  407. jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
  408. list_move_tail(&jeb->list, &c->free_list);
  409. c->nr_erasing_blocks--;
  410. c->nr_free_blocks++;
  411. jffs2_dbg_acct_sanity_check_nolock(c, jeb);
  412. jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
  413. spin_unlock(&c->erase_completion_lock);
  414. mutex_unlock(&c->erase_free_sem);
  415. wake_up(&c->erase_wait);
  416. return;
  417. filebad:
  418. jffs2_erase_failed(c, jeb, bad_offset);
  419. return;
  420. refile:
  421. /* Stick it back on the list from whence it came and come back later */
  422. mutex_lock(&c->erase_free_sem);
  423. spin_lock(&c->erase_completion_lock);
  424. jffs2_garbage_collect_trigger(c);
  425. list_move(&jeb->list, &c->erase_complete_list);
  426. spin_unlock(&c->erase_completion_lock);
  427. mutex_unlock(&c->erase_free_sem);
  428. return;
  429. }