mtdblock.c 8.7 KB

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