mtdblock.c 9.2 KB

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