mtdblock.c 9.3 KB

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