mtdblock.c 9.9 KB

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