mtdblock.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Direct MTD block device access
  3. *
  4. * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
  5. * Copyright © 2000-2003 Nicolas Pitre <nico@fluxnic.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/types.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/mtd/blktrans.h>
  32. #include <linux/mutex.h>
  33. #include <linux/major.h>
  34. struct mtdblk_dev {
  35. struct mtd_blktrans_dev mbd;
  36. int count;
  37. struct mutex cache_mutex;
  38. unsigned char *cache_data;
  39. unsigned long cache_offset;
  40. unsigned int cache_size;
  41. enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
  42. };
  43. /*
  44. * Cache stuff...
  45. *
  46. * Since typical flash erasable sectors are much larger than what Linux's
  47. * buffer cache can handle, we must implement read-modify-write on flash
  48. * sectors for each block write requests. To avoid over-erasing flash sectors
  49. * and to speed things up, we locally cache a whole flash sector while it is
  50. * being written to until a different sector is required.
  51. */
  52. static void erase_callback(struct erase_info *done)
  53. {
  54. wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
  55. wake_up(wait_q);
  56. }
  57. static int erase_write (struct mtd_info *mtd, unsigned long pos,
  58. int len, const char *buf)
  59. {
  60. struct erase_info erase;
  61. DECLARE_WAITQUEUE(wait, current);
  62. wait_queue_head_t wait_q;
  63. size_t retlen;
  64. int ret;
  65. /*
  66. * First, let's erase the flash block.
  67. */
  68. init_waitqueue_head(&wait_q);
  69. erase.mtd = mtd;
  70. erase.callback = erase_callback;
  71. erase.addr = pos;
  72. erase.len = len;
  73. erase.priv = (u_long)&wait_q;
  74. set_current_state(TASK_INTERRUPTIBLE);
  75. add_wait_queue(&wait_q, &wait);
  76. ret = mtd_erase(mtd, &erase);
  77. if (ret) {
  78. set_current_state(TASK_RUNNING);
  79. remove_wait_queue(&wait_q, &wait);
  80. printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
  81. "on \"%s\" failed\n",
  82. pos, len, mtd->name);
  83. return ret;
  84. }
  85. schedule(); /* Wait for erase to finish. */
  86. remove_wait_queue(&wait_q, &wait);
  87. /*
  88. * Next, write the data to flash.
  89. */
  90. ret = mtd_write(mtd, pos, len, &retlen, buf);
  91. if (ret)
  92. return ret;
  93. if (retlen != len)
  94. return -EIO;
  95. return 0;
  96. }
  97. static int write_cached_data (struct mtdblk_dev *mtdblk)
  98. {
  99. struct mtd_info *mtd = mtdblk->mbd.mtd;
  100. int ret;
  101. if (mtdblk->cache_state != STATE_DIRTY)
  102. return 0;
  103. pr_debug("mtdblock: writing cached data for \"%s\" "
  104. "at 0x%lx, size 0x%x\n", mtd->name,
  105. mtdblk->cache_offset, mtdblk->cache_size);
  106. ret = erase_write (mtd, mtdblk->cache_offset,
  107. mtdblk->cache_size, mtdblk->cache_data);
  108. if (ret)
  109. return ret;
  110. /*
  111. * Here we could arguably set the cache state to STATE_CLEAN.
  112. * However this could lead to inconsistency since we will not
  113. * be notified if this content is altered on the flash by other
  114. * means. Let's declare it empty and leave buffering tasks to
  115. * the buffer cache instead.
  116. */
  117. mtdblk->cache_state = STATE_EMPTY;
  118. return 0;
  119. }
  120. static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
  121. int len, const char *buf)
  122. {
  123. struct mtd_info *mtd = mtdblk->mbd.mtd;
  124. unsigned int sect_size = mtdblk->cache_size;
  125. size_t retlen;
  126. int ret;
  127. pr_debug("mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
  128. mtd->name, pos, len);
  129. if (!sect_size)
  130. return mtd_write(mtd, pos, len, &retlen, buf);
  131. while (len > 0) {
  132. unsigned long sect_start = (pos/sect_size)*sect_size;
  133. unsigned int offset = pos - sect_start;
  134. unsigned int size = sect_size - offset;
  135. if( size > len )
  136. size = len;
  137. if (size == sect_size) {
  138. /*
  139. * We are covering a whole sector. Thus there is no
  140. * need to bother with the cache while it may still be
  141. * useful for other partial writes.
  142. */
  143. ret = erase_write (mtd, pos, size, buf);
  144. if (ret)
  145. return ret;
  146. } else {
  147. /* Partial sector: need to use the cache */
  148. if (mtdblk->cache_state == STATE_DIRTY &&
  149. mtdblk->cache_offset != sect_start) {
  150. ret = write_cached_data(mtdblk);
  151. if (ret)
  152. return ret;
  153. }
  154. if (mtdblk->cache_state == STATE_EMPTY ||
  155. mtdblk->cache_offset != sect_start) {
  156. /* fill the cache with the current sector */
  157. mtdblk->cache_state = STATE_EMPTY;
  158. ret = mtd_read(mtd, sect_start, sect_size,
  159. &retlen, mtdblk->cache_data);
  160. if (ret)
  161. return ret;
  162. if (retlen != sect_size)
  163. return -EIO;
  164. mtdblk->cache_offset = sect_start;
  165. mtdblk->cache_size = sect_size;
  166. mtdblk->cache_state = STATE_CLEAN;
  167. }
  168. /* write data to our local cache */
  169. memcpy (mtdblk->cache_data + offset, buf, size);
  170. mtdblk->cache_state = STATE_DIRTY;
  171. }
  172. buf += size;
  173. pos += size;
  174. len -= size;
  175. }
  176. return 0;
  177. }
  178. static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
  179. int len, char *buf)
  180. {
  181. struct mtd_info *mtd = mtdblk->mbd.mtd;
  182. unsigned int sect_size = mtdblk->cache_size;
  183. size_t retlen;
  184. int ret;
  185. pr_debug("mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
  186. mtd->name, pos, len);
  187. if (!sect_size)
  188. return mtd_read(mtd, pos, len, &retlen, buf);
  189. while (len > 0) {
  190. unsigned long sect_start = (pos/sect_size)*sect_size;
  191. unsigned int offset = pos - sect_start;
  192. unsigned int size = sect_size - offset;
  193. if (size > len)
  194. size = len;
  195. /*
  196. * Check if the requested data is already cached
  197. * Read the requested amount of data from our internal cache if it
  198. * contains what we want, otherwise we read the data directly
  199. * from flash.
  200. */
  201. if (mtdblk->cache_state != STATE_EMPTY &&
  202. mtdblk->cache_offset == sect_start) {
  203. memcpy (buf, mtdblk->cache_data + offset, size);
  204. } else {
  205. ret = mtd_read(mtd, pos, size, &retlen, buf);
  206. if (ret)
  207. return ret;
  208. if (retlen != size)
  209. return -EIO;
  210. }
  211. buf += size;
  212. pos += size;
  213. len -= size;
  214. }
  215. return 0;
  216. }
  217. static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
  218. unsigned long block, char *buf)
  219. {
  220. struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
  221. return do_cached_read(mtdblk, block<<9, 512, buf);
  222. }
  223. static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
  224. unsigned long block, char *buf)
  225. {
  226. struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
  227. if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
  228. mtdblk->cache_data = vmalloc(mtdblk->mbd.mtd->erasesize);
  229. if (!mtdblk->cache_data)
  230. return -EINTR;
  231. /* -EINTR is not really correct, but it is the best match
  232. * documented in man 2 write for all cases. We could also
  233. * return -EAGAIN sometimes, but why bother?
  234. */
  235. }
  236. return do_cached_write(mtdblk, block<<9, 512, buf);
  237. }
  238. static int mtdblock_open(struct mtd_blktrans_dev *mbd)
  239. {
  240. struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
  241. pr_debug("mtdblock_open\n");
  242. if (mtdblk->count) {
  243. mtdblk->count++;
  244. return 0;
  245. }
  246. /* OK, it's not open. Create cache info for it */
  247. mtdblk->count = 1;
  248. mutex_init(&mtdblk->cache_mutex);
  249. mtdblk->cache_state = STATE_EMPTY;
  250. if (!(mbd->mtd->flags & MTD_NO_ERASE) && mbd->mtd->erasesize) {
  251. mtdblk->cache_size = mbd->mtd->erasesize;
  252. mtdblk->cache_data = NULL;
  253. }
  254. pr_debug("ok\n");
  255. return 0;
  256. }
  257. static void mtdblock_release(struct mtd_blktrans_dev *mbd)
  258. {
  259. struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
  260. pr_debug("mtdblock_release\n");
  261. mutex_lock(&mtdblk->cache_mutex);
  262. write_cached_data(mtdblk);
  263. mutex_unlock(&mtdblk->cache_mutex);
  264. if (!--mtdblk->count) {
  265. /*
  266. * It was the last usage. Free the cache, but only sync if
  267. * opened for writing.
  268. */
  269. if (mbd->file_mode & FMODE_WRITE)
  270. mtd_sync(mbd->mtd);
  271. vfree(mtdblk->cache_data);
  272. }
  273. pr_debug("ok\n");
  274. }
  275. static int mtdblock_flush(struct mtd_blktrans_dev *dev)
  276. {
  277. struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
  278. mutex_lock(&mtdblk->cache_mutex);
  279. write_cached_data(mtdblk);
  280. mutex_unlock(&mtdblk->cache_mutex);
  281. mtd_sync(dev->mtd);
  282. return 0;
  283. }
  284. static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  285. {
  286. struct mtdblk_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  287. if (!dev)
  288. return;
  289. dev->mbd.mtd = mtd;
  290. dev->mbd.devnum = mtd->index;
  291. dev->mbd.size = mtd->size >> 9;
  292. dev->mbd.tr = tr;
  293. if (!(mtd->flags & MTD_WRITEABLE))
  294. dev->mbd.readonly = 1;
  295. if (add_mtd_blktrans_dev(&dev->mbd))
  296. kfree(dev);
  297. }
  298. static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
  299. {
  300. del_mtd_blktrans_dev(dev);
  301. }
  302. static struct mtd_blktrans_ops mtdblock_tr = {
  303. .name = "mtdblock",
  304. .major = MTD_BLOCK_MAJOR,
  305. .part_bits = 0,
  306. .blksize = 512,
  307. .open = mtdblock_open,
  308. .flush = mtdblock_flush,
  309. .release = mtdblock_release,
  310. .readsect = mtdblock_readsect,
  311. .writesect = mtdblock_writesect,
  312. .add_mtd = mtdblock_add_mtd,
  313. .remove_dev = mtdblock_remove_dev,
  314. .owner = THIS_MODULE,
  315. };
  316. static int __init init_mtdblock(void)
  317. {
  318. return register_mtd_blktrans(&mtdblock_tr);
  319. }
  320. static void __exit cleanup_mtdblock(void)
  321. {
  322. deregister_mtd_blktrans(&mtdblock_tr);
  323. }
  324. module_init(init_mtdblock);
  325. module_exit(cleanup_mtdblock);
  326. MODULE_LICENSE("GPL");
  327. MODULE_AUTHOR("Nicolas Pitre <nico@fluxnic.net> et al.");
  328. MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");