mtdblock.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Direct MTD block device access
  3. *
  4. * (C) 2000-2003 Nicolas Pitre <nico@fluxnic.net>
  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. static struct mutex mtdblks_lock;
  28. /*
  29. * Cache stuff...
  30. *
  31. * Since typical flash erasable sectors are much larger than what Linux's
  32. * buffer cache can handle, we must implement read-modify-write on flash
  33. * sectors for each block write requests. To avoid over-erasing flash sectors
  34. * and to speed things up, we locally cache a whole flash sector while it is
  35. * being written to until a different sector is required.
  36. */
  37. static void erase_callback(struct erase_info *done)
  38. {
  39. wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
  40. wake_up(wait_q);
  41. }
  42. static int erase_write (struct mtd_info *mtd, unsigned long pos,
  43. int len, const char *buf)
  44. {
  45. struct erase_info erase;
  46. DECLARE_WAITQUEUE(wait, current);
  47. wait_queue_head_t wait_q;
  48. size_t retlen;
  49. int ret;
  50. /*
  51. * First, let's erase the flash block.
  52. */
  53. init_waitqueue_head(&wait_q);
  54. erase.mtd = mtd;
  55. erase.callback = erase_callback;
  56. erase.addr = pos;
  57. erase.len = len;
  58. erase.priv = (u_long)&wait_q;
  59. set_current_state(TASK_INTERRUPTIBLE);
  60. add_wait_queue(&wait_q, &wait);
  61. ret = mtd->erase(mtd, &erase);
  62. if (ret) {
  63. set_current_state(TASK_RUNNING);
  64. remove_wait_queue(&wait_q, &wait);
  65. printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
  66. "on \"%s\" failed\n",
  67. pos, len, mtd->name);
  68. return ret;
  69. }
  70. schedule(); /* Wait for erase to finish. */
  71. remove_wait_queue(&wait_q, &wait);
  72. /*
  73. * Next, write the data to flash.
  74. */
  75. ret = mtd->write(mtd, pos, len, &retlen, buf);
  76. if (ret)
  77. return ret;
  78. if (retlen != len)
  79. return -EIO;
  80. return 0;
  81. }
  82. static int write_cached_data (struct mtdblk_dev *mtdblk)
  83. {
  84. struct mtd_info *mtd = mtdblk->mtd;
  85. int ret;
  86. if (mtdblk->cache_state != STATE_DIRTY)
  87. return 0;
  88. DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: writing cached data for \"%s\" "
  89. "at 0x%lx, size 0x%x\n", mtd->name,
  90. mtdblk->cache_offset, mtdblk->cache_size);
  91. ret = erase_write (mtd, mtdblk->cache_offset,
  92. mtdblk->cache_size, mtdblk->cache_data);
  93. if (ret)
  94. return ret;
  95. /*
  96. * Here we could argubly set the cache state to STATE_CLEAN.
  97. * However this could lead to inconsistency since we will not
  98. * be notified if this content is altered on the flash by other
  99. * means. Let's declare it empty and leave buffering tasks to
  100. * the buffer cache instead.
  101. */
  102. mtdblk->cache_state = STATE_EMPTY;
  103. return 0;
  104. }
  105. static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
  106. int len, const char *buf)
  107. {
  108. struct mtd_info *mtd = mtdblk->mtd;
  109. unsigned int sect_size = mtdblk->cache_size;
  110. size_t retlen;
  111. int ret;
  112. DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
  113. mtd->name, pos, len);
  114. if (!sect_size)
  115. return mtd->write(mtd, pos, len, &retlen, buf);
  116. while (len > 0) {
  117. unsigned long sect_start = (pos/sect_size)*sect_size;
  118. unsigned int offset = pos - sect_start;
  119. unsigned int size = sect_size - offset;
  120. if( size > len )
  121. size = len;
  122. if (size == sect_size) {
  123. /*
  124. * We are covering a whole sector. Thus there is no
  125. * need to bother with the cache while it may still be
  126. * useful for other partial writes.
  127. */
  128. ret = erase_write (mtd, pos, size, buf);
  129. if (ret)
  130. return ret;
  131. } else {
  132. /* Partial sector: need to use the cache */
  133. if (mtdblk->cache_state == STATE_DIRTY &&
  134. mtdblk->cache_offset != sect_start) {
  135. ret = write_cached_data(mtdblk);
  136. if (ret)
  137. return ret;
  138. }
  139. if (mtdblk->cache_state == STATE_EMPTY ||
  140. mtdblk->cache_offset != sect_start) {
  141. /* fill the cache with the current sector */
  142. mtdblk->cache_state = STATE_EMPTY;
  143. ret = mtd->read(mtd, sect_start, sect_size,
  144. &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. mutex_lock(&mtdblks_lock);
  230. if (mtdblks[dev]) {
  231. mtdblks[dev]->count++;
  232. mutex_unlock(&mtdblks_lock);
  233. return 0;
  234. }
  235. /* OK, it's not open. Create cache info for it */
  236. mtdblk = kzalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
  237. if (!mtdblk) {
  238. mutex_unlock(&mtdblks_lock);
  239. return -ENOMEM;
  240. }
  241. mtdblk->count = 1;
  242. mtdblk->mtd = mtd;
  243. mutex_init(&mtdblk->cache_mutex);
  244. mtdblk->cache_state = STATE_EMPTY;
  245. if ( !(mtdblk->mtd->flags & MTD_NO_ERASE) && mtdblk->mtd->erasesize) {
  246. mtdblk->cache_size = mtdblk->mtd->erasesize;
  247. mtdblk->cache_data = NULL;
  248. }
  249. mtdblks[dev] = mtdblk;
  250. mutex_unlock(&mtdblks_lock);
  251. DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
  252. return 0;
  253. }
  254. static int mtdblock_release(struct mtd_blktrans_dev *mbd)
  255. {
  256. int dev = mbd->devnum;
  257. struct mtdblk_dev *mtdblk = mtdblks[dev];
  258. DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
  259. mutex_lock(&mtdblks_lock);
  260. mutex_lock(&mtdblk->cache_mutex);
  261. write_cached_data(mtdblk);
  262. mutex_unlock(&mtdblk->cache_mutex);
  263. if (!--mtdblk->count) {
  264. /* It was the last usage. Free the device */
  265. mtdblks[dev] = NULL;
  266. if (mtdblk->mtd->sync)
  267. mtdblk->mtd->sync(mtdblk->mtd);
  268. vfree(mtdblk->cache_data);
  269. kfree(mtdblk);
  270. }
  271. mutex_unlock(&mtdblks_lock);
  272. DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
  273. return 0;
  274. }
  275. static int mtdblock_flush(struct mtd_blktrans_dev *dev)
  276. {
  277. struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
  278. mutex_lock(&mtdblk->cache_mutex);
  279. write_cached_data(mtdblk);
  280. mutex_unlock(&mtdblk->cache_mutex);
  281. if (mtdblk->mtd->sync)
  282. mtdblk->mtd->sync(mtdblk->mtd);
  283. return 0;
  284. }
  285. static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  286. {
  287. struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  288. if (!dev)
  289. return;
  290. dev->mtd = mtd;
  291. dev->devnum = mtd->index;
  292. dev->size = mtd->size >> 9;
  293. dev->tr = tr;
  294. if (!(mtd->flags & MTD_WRITEABLE))
  295. dev->readonly = 1;
  296. add_mtd_blktrans_dev(dev);
  297. }
  298. static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
  299. {
  300. del_mtd_blktrans_dev(dev);
  301. kfree(dev);
  302. }
  303. static struct mtd_blktrans_ops mtdblock_tr = {
  304. .name = "mtdblock",
  305. .major = 31,
  306. .part_bits = 0,
  307. .blksize = 512,
  308. .open = mtdblock_open,
  309. .flush = mtdblock_flush,
  310. .release = mtdblock_release,
  311. .readsect = mtdblock_readsect,
  312. .writesect = mtdblock_writesect,
  313. .add_mtd = mtdblock_add_mtd,
  314. .remove_dev = mtdblock_remove_dev,
  315. .owner = THIS_MODULE,
  316. };
  317. static int __init init_mtdblock(void)
  318. {
  319. mutex_init(&mtdblks_lock);
  320. return register_mtd_blktrans(&mtdblock_tr);
  321. }
  322. static void __exit cleanup_mtdblock(void)
  323. {
  324. deregister_mtd_blktrans(&mtdblock_tr);
  325. }
  326. module_init(init_mtdblock);
  327. module_exit(cleanup_mtdblock);
  328. MODULE_LICENSE("GPL");
  329. MODULE_AUTHOR("Nicolas Pitre <nico@fluxnic.net> et al.");
  330. MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");