erase.c 14 KB

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