mtdblock.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Direct MTD block device access
  4. *
  5. * Copyright © 1999-2010 David Woodhouse <[email protected]>
  6. * Copyright © 2000-2003 Nicolas Pitre <[email protected]>
  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 && !mtd_is_bitflip(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. ret = mtd_read(mtd, pos, len, &retlen, buf);
  161. if (ret && !mtd_is_bitflip(ret))
  162. return ret;
  163. return 0;
  164. }
  165. while (len > 0) {
  166. unsigned long sect_start = (pos/sect_size)*sect_size;
  167. unsigned int offset = pos - sect_start;
  168. unsigned int size = sect_size - offset;
  169. if (size > len)
  170. size = len;
  171. /*
  172. * Check if the requested data is already cached
  173. * Read the requested amount of data from our internal cache if it
  174. * contains what we want, otherwise we read the data directly
  175. * from flash.
  176. */
  177. if (mtdblk->cache_state != STATE_EMPTY &&
  178. mtdblk->cache_offset == sect_start) {
  179. memcpy (buf, mtdblk->cache_data + offset, size);
  180. } else {
  181. ret = mtd_read(mtd, pos, size, &retlen, buf);
  182. if (ret && !mtd_is_bitflip(ret))
  183. return ret;
  184. if (retlen != size)
  185. return -EIO;
  186. }
  187. buf += size;
  188. pos += size;
  189. len -= size;
  190. }
  191. return 0;
  192. }
  193. static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
  194. unsigned long block, char *buf)
  195. {
  196. struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
  197. return do_cached_read(mtdblk, block<<9, 512, buf);
  198. }
  199. static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
  200. unsigned long block, char *buf)
  201. {
  202. struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
  203. if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
  204. mtdblk->cache_data = vmalloc(mtdblk->mbd.mtd->erasesize);
  205. if (!mtdblk->cache_data)
  206. return -EINTR;
  207. /* -EINTR is not really correct, but it is the best match
  208. * documented in man 2 write for all cases. We could also
  209. * return -EAGAIN sometimes, but why bother?
  210. */
  211. }
  212. return do_cached_write(mtdblk, block<<9, 512, buf);
  213. }
  214. static int mtdblock_open(struct mtd_blktrans_dev *mbd)
  215. {
  216. struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
  217. pr_debug("mtdblock_open\n");
  218. if (mtdblk->count) {
  219. mtdblk->count++;
  220. return 0;
  221. }
  222. if (mtd_type_is_nand(mbd->mtd))
  223. pr_warn("%s: MTD device '%s' is NAND, please consider using UBI block devices instead.\n",
  224. mbd->tr->name, mbd->mtd->name);
  225. /* OK, it's not open. Create cache info for it */
  226. mtdblk->count = 1;
  227. mutex_init(&mtdblk->cache_mutex);
  228. mtdblk->cache_state = STATE_EMPTY;
  229. if (!(mbd->mtd->flags & MTD_NO_ERASE) && mbd->mtd->erasesize) {
  230. mtdblk->cache_size = mbd->mtd->erasesize;
  231. mtdblk->cache_data = NULL;
  232. }
  233. pr_debug("ok\n");
  234. return 0;
  235. }
  236. static void mtdblock_release(struct mtd_blktrans_dev *mbd)
  237. {
  238. struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
  239. pr_debug("mtdblock_release\n");
  240. mutex_lock(&mtdblk->cache_mutex);
  241. write_cached_data(mtdblk);
  242. mutex_unlock(&mtdblk->cache_mutex);
  243. if (!--mtdblk->count) {
  244. /*
  245. * It was the last usage. Free the cache, but only sync if
  246. * opened for writing.
  247. */
  248. if (mbd->file_mode & FMODE_WRITE)
  249. mtd_sync(mbd->mtd);
  250. vfree(mtdblk->cache_data);
  251. }
  252. pr_debug("ok\n");
  253. }
  254. static int mtdblock_flush(struct mtd_blktrans_dev *dev)
  255. {
  256. struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
  257. int ret;
  258. mutex_lock(&mtdblk->cache_mutex);
  259. ret = write_cached_data(mtdblk);
  260. mutex_unlock(&mtdblk->cache_mutex);
  261. mtd_sync(dev->mtd);
  262. return ret;
  263. }
  264. static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  265. {
  266. struct mtdblk_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  267. if (!dev)
  268. return;
  269. dev->mbd.mtd = mtd;
  270. dev->mbd.devnum = mtd->index;
  271. dev->mbd.size = mtd->size >> 9;
  272. dev->mbd.tr = tr;
  273. if (!(mtd->flags & MTD_WRITEABLE))
  274. dev->mbd.readonly = 1;
  275. if (add_mtd_blktrans_dev(&dev->mbd))
  276. kfree(dev);
  277. }
  278. static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
  279. {
  280. del_mtd_blktrans_dev(dev);
  281. }
  282. static struct mtd_blktrans_ops mtdblock_tr = {
  283. .name = "mtdblock",
  284. .major = MTD_BLOCK_MAJOR,
  285. .part_bits = 0,
  286. .blksize = 512,
  287. .open = mtdblock_open,
  288. .flush = mtdblock_flush,
  289. .release = mtdblock_release,
  290. .readsect = mtdblock_readsect,
  291. .writesect = mtdblock_writesect,
  292. .add_mtd = mtdblock_add_mtd,
  293. .remove_dev = mtdblock_remove_dev,
  294. .owner = THIS_MODULE,
  295. };
  296. module_mtd_blktrans(mtdblock_tr);
  297. MODULE_LICENSE("GPL");
  298. MODULE_AUTHOR("Nicolas Pitre <[email protected]> et al.");
  299. MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");