erase.c 13 KB

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