erase.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 = kzalloc_obj(struct erase_info);
  42. if (!instr) {
  43. pr_warn("kzalloc 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. instr->addr = jeb->offset;
  55. instr->len = c->sector_size;
  56. ret = mtd_erase(c->mtd, instr);
  57. if (!ret) {
  58. jffs2_erase_succeeded(c, jeb);
  59. kfree(instr);
  60. return;
  61. }
  62. bad_offset = instr->fail_addr;
  63. kfree(instr);
  64. #endif /* __ECOS */
  65. if (ret == -ENOMEM || ret == -EAGAIN) {
  66. /* Erase failed immediately. Refile it on the list */
  67. jffs2_dbg(1, "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n",
  68. jeb->offset, ret);
  69. mutex_lock(&c->erase_free_sem);
  70. spin_lock(&c->erase_completion_lock);
  71. list_move(&jeb->list, &c->erase_pending_list);
  72. c->erasing_size -= c->sector_size;
  73. c->dirty_size += c->sector_size;
  74. jeb->dirty_size = c->sector_size;
  75. spin_unlock(&c->erase_completion_lock);
  76. mutex_unlock(&c->erase_free_sem);
  77. return;
  78. }
  79. if (ret == -EROFS)
  80. pr_warn("Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n",
  81. jeb->offset);
  82. else
  83. pr_warn("Erase at 0x%08x failed immediately: errno %d\n",
  84. jeb->offset, ret);
  85. jffs2_erase_failed(c, jeb, bad_offset);
  86. }
  87. int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
  88. {
  89. struct jffs2_eraseblock *jeb;
  90. int work_done = 0;
  91. mutex_lock(&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_move(&jeb->list, &c->erase_checking_list);
  98. spin_unlock(&c->erase_completion_lock);
  99. mutex_unlock(&c->erase_free_sem);
  100. jffs2_mark_erased_block(c, jeb);
  101. work_done++;
  102. if (!--count) {
  103. jffs2_dbg(1, "Count reached. jffs2_erase_pending_blocks leaving\n");
  104. goto done;
  105. }
  106. } else if (!list_empty(&c->erase_pending_list)) {
  107. jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
  108. jffs2_dbg(1, "Starting erase of pending block 0x%08x\n",
  109. 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_jeb_node_refs(c, jeb);
  118. list_add(&jeb->list, &c->erasing_list);
  119. spin_unlock(&c->erase_completion_lock);
  120. mutex_unlock(&c->erase_free_sem);
  121. jffs2_erase_block(c, jeb);
  122. } else {
  123. BUG();
  124. }
  125. /* Be nice */
  126. cond_resched();
  127. mutex_lock(&c->erase_free_sem);
  128. spin_lock(&c->erase_completion_lock);
  129. }
  130. spin_unlock(&c->erase_completion_lock);
  131. mutex_unlock(&c->erase_free_sem);
  132. done:
  133. jffs2_dbg(1, "jffs2_erase_pending_blocks completed\n");
  134. return work_done;
  135. }
  136. static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  137. {
  138. jffs2_dbg(1, "Erase completed successfully at 0x%08x\n", jeb->offset);
  139. mutex_lock(&c->erase_free_sem);
  140. spin_lock(&c->erase_completion_lock);
  141. list_move_tail(&jeb->list, &c->erase_complete_list);
  142. /* Wake the GC thread to mark them clean */
  143. jffs2_garbage_collect_trigger(c);
  144. spin_unlock(&c->erase_completion_lock);
  145. mutex_unlock(&c->erase_free_sem);
  146. wake_up(&c->erase_wait);
  147. }
  148. static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
  149. {
  150. /* For NAND, if the failure did not occur at the device level for a
  151. specific physical page, don't bother updating the bad block table. */
  152. if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
  153. /* We had a device-level failure to erase. Let's see if we've
  154. failed too many times. */
  155. if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
  156. /* We'd like to give this block another try. */
  157. mutex_lock(&c->erase_free_sem);
  158. spin_lock(&c->erase_completion_lock);
  159. list_move(&jeb->list, &c->erase_pending_list);
  160. c->erasing_size -= c->sector_size;
  161. c->dirty_size += c->sector_size;
  162. jeb->dirty_size = c->sector_size;
  163. spin_unlock(&c->erase_completion_lock);
  164. mutex_unlock(&c->erase_free_sem);
  165. return;
  166. }
  167. }
  168. mutex_lock(&c->erase_free_sem);
  169. spin_lock(&c->erase_completion_lock);
  170. c->erasing_size -= c->sector_size;
  171. c->bad_size += c->sector_size;
  172. list_move(&jeb->list, &c->bad_list);
  173. c->nr_erasing_blocks--;
  174. spin_unlock(&c->erase_completion_lock);
  175. mutex_unlock(&c->erase_free_sem);
  176. wake_up(&c->erase_wait);
  177. }
  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. jffs2_dbg(1, "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");
  221. this = ic->nodes;
  222. printk(KERN_DEBUG);
  223. while(this) {
  224. pr_cont("0x%08x(%d)->",
  225. ref_offset(this), ref_flags(this));
  226. if (++i == 5) {
  227. printk(KERN_DEBUG);
  228. i=0;
  229. }
  230. this = this->next_in_ino;
  231. }
  232. pr_cont("\n");
  233. });
  234. switch (ic->class) {
  235. #ifdef CONFIG_JFFS2_FS_XATTR
  236. case RAWNODE_CLASS_XATTR_DATUM:
  237. jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
  238. break;
  239. case RAWNODE_CLASS_XATTR_REF:
  240. jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
  241. break;
  242. #endif
  243. default:
  244. if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
  245. jffs2_del_ino_cache(c, ic);
  246. }
  247. }
  248. void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  249. {
  250. struct jffs2_raw_node_ref *block, *ref;
  251. jffs2_dbg(1, "Freeing all node refs for eraseblock offset 0x%08x\n",
  252. jeb->offset);
  253. block = ref = jeb->first_node;
  254. while (ref) {
  255. if (ref->flash_offset == REF_LINK_NODE) {
  256. ref = ref->next_in_ino;
  257. jffs2_free_refblock(block);
  258. block = ref;
  259. continue;
  260. }
  261. if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
  262. jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
  263. /* else it was a non-inode node or already removed, so don't bother */
  264. ref++;
  265. }
  266. jeb->first_node = jeb->last_node = NULL;
  267. }
  268. static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
  269. {
  270. void *ebuf;
  271. uint32_t ofs;
  272. size_t retlen;
  273. int ret;
  274. unsigned long *wordebuf;
  275. ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen,
  276. &ebuf, NULL);
  277. if (ret != -EOPNOTSUPP) {
  278. if (ret) {
  279. jffs2_dbg(1, "MTD point failed %d\n", ret);
  280. goto do_flash_read;
  281. }
  282. if (retlen < c->sector_size) {
  283. /* Don't muck about if it won't let us point to the whole erase sector */
  284. jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
  285. retlen);
  286. mtd_unpoint(c->mtd, jeb->offset, retlen);
  287. goto do_flash_read;
  288. }
  289. wordebuf = ebuf-sizeof(*wordebuf);
  290. retlen /= sizeof(*wordebuf);
  291. do {
  292. if (*++wordebuf != ~0)
  293. break;
  294. } while(--retlen);
  295. mtd_unpoint(c->mtd, jeb->offset, c->sector_size);
  296. if (retlen) {
  297. *bad_offset = jeb->offset + c->sector_size - retlen * sizeof(*wordebuf);
  298. pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08x\n",
  299. *wordebuf, *bad_offset);
  300. return -EIO;
  301. }
  302. return 0;
  303. }
  304. do_flash_read:
  305. ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  306. if (!ebuf) {
  307. pr_warn("Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n",
  308. jeb->offset);
  309. return -EAGAIN;
  310. }
  311. jffs2_dbg(1, "Verifying erase at 0x%08x\n", jeb->offset);
  312. for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
  313. uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
  314. int i;
  315. *bad_offset = ofs;
  316. ret = mtd_read(c->mtd, ofs, readlen, &retlen, ebuf);
  317. if (ret) {
  318. pr_warn("Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n",
  319. ofs, ret);
  320. ret = -EIO;
  321. goto fail;
  322. }
  323. if (retlen != readlen) {
  324. pr_warn("Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n",
  325. ofs, readlen, retlen);
  326. ret = -EIO;
  327. goto fail;
  328. }
  329. for (i=0; i<readlen; i += sizeof(unsigned long)) {
  330. /* It's OK. We know it's properly aligned */
  331. unsigned long *datum = ebuf + i;
  332. if (*datum + 1) {
  333. *bad_offset += i;
  334. pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08x\n",
  335. *datum, *bad_offset);
  336. ret = -EIO;
  337. goto fail;
  338. }
  339. }
  340. ofs += readlen;
  341. cond_resched();
  342. }
  343. ret = 0;
  344. fail:
  345. kfree(ebuf);
  346. return ret;
  347. }
  348. static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  349. {
  350. size_t retlen;
  351. int ret;
  352. uint32_t bad_offset;
  353. switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
  354. case -EAGAIN: goto refile;
  355. case -EIO: goto filebad;
  356. }
  357. /* Write the erase complete marker */
  358. jffs2_dbg(1, "Writing erased marker to block at 0x%08x\n", jeb->offset);
  359. bad_offset = jeb->offset;
  360. /* Cleanmarker in oob area or no cleanmarker at all ? */
  361. if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
  362. if (jffs2_cleanmarker_oob(c)) {
  363. if (jffs2_write_nand_cleanmarker(c, jeb))
  364. goto filebad;
  365. }
  366. } else {
  367. struct kvec vecs[1];
  368. struct jffs2_unknown_node marker = {
  369. .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
  370. .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
  371. .totlen = cpu_to_je32(c->cleanmarker_size)
  372. };
  373. ret = jffs2_prealloc_raw_node_refs(c, jeb, 1);
  374. if (ret)
  375. goto filebad;
  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. }