mtdblock.c 9.3 KB

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