erase.c 14 KB

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