dev_mtd.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * fs/logfs/dev_mtd.c - Device access methods for MTD
  3. *
  4. * As should be obvious for Linux kernel code, license is GPLv2
  5. *
  6. * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
  7. */
  8. #include "logfs.h"
  9. #include <linux/completion.h>
  10. #include <linux/mount.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))
  14. static int logfs_mtd_read(struct super_block *sb, loff_t ofs, size_t len,
  15. void *buf)
  16. {
  17. struct mtd_info *mtd = logfs_super(sb)->s_mtd;
  18. size_t retlen;
  19. int ret;
  20. ret = mtd_read(mtd, ofs, len, &retlen, buf);
  21. BUG_ON(ret == -EINVAL);
  22. if (ret)
  23. return ret;
  24. /* Not sure if we should loop instead. */
  25. if (retlen != len)
  26. return -EIO;
  27. return 0;
  28. }
  29. static int loffs_mtd_write(struct super_block *sb, loff_t ofs, size_t len,
  30. void *buf)
  31. {
  32. struct logfs_super *super = logfs_super(sb);
  33. struct mtd_info *mtd = super->s_mtd;
  34. size_t retlen;
  35. loff_t page_start, page_end;
  36. int ret;
  37. if (super->s_flags & LOGFS_SB_FLAG_RO)
  38. return -EROFS;
  39. BUG_ON((ofs >= mtd->size) || (len > mtd->size - ofs));
  40. BUG_ON(ofs != (ofs >> super->s_writeshift) << super->s_writeshift);
  41. BUG_ON(len > PAGE_CACHE_SIZE);
  42. page_start = ofs & PAGE_CACHE_MASK;
  43. page_end = PAGE_CACHE_ALIGN(ofs + len) - 1;
  44. ret = mtd_write(mtd, ofs, len, &retlen, buf);
  45. if (ret || (retlen != len))
  46. return -EIO;
  47. return 0;
  48. }
  49. /*
  50. * For as long as I can remember (since about 2001) mtd->erase has been an
  51. * asynchronous interface lacking the first driver to actually use the
  52. * asynchronous properties. So just to prevent the first implementor of such
  53. * a thing from breaking logfs in 2350, we do the usual pointless dance to
  54. * declare a completion variable and wait for completion before returning
  55. * from logfs_mtd_erase(). What an exercise in futility!
  56. */
  57. static void logfs_erase_callback(struct erase_info *ei)
  58. {
  59. complete((struct completion *)ei->priv);
  60. }
  61. static int logfs_mtd_erase_mapping(struct super_block *sb, loff_t ofs,
  62. size_t len)
  63. {
  64. struct logfs_super *super = logfs_super(sb);
  65. struct address_space *mapping = super->s_mapping_inode->i_mapping;
  66. struct page *page;
  67. pgoff_t index = ofs >> PAGE_SHIFT;
  68. for (index = ofs >> PAGE_SHIFT; index < (ofs + len) >> PAGE_SHIFT; index++) {
  69. page = find_get_page(mapping, index);
  70. if (!page)
  71. continue;
  72. memset(page_address(page), 0xFF, PAGE_SIZE);
  73. page_cache_release(page);
  74. }
  75. return 0;
  76. }
  77. static int logfs_mtd_erase(struct super_block *sb, loff_t ofs, size_t len,
  78. int ensure_write)
  79. {
  80. struct mtd_info *mtd = logfs_super(sb)->s_mtd;
  81. struct erase_info ei;
  82. DECLARE_COMPLETION_ONSTACK(complete);
  83. int ret;
  84. BUG_ON(len % mtd->erasesize);
  85. if (logfs_super(sb)->s_flags & LOGFS_SB_FLAG_RO)
  86. return -EROFS;
  87. memset(&ei, 0, sizeof(ei));
  88. ei.mtd = mtd;
  89. ei.addr = ofs;
  90. ei.len = len;
  91. ei.callback = logfs_erase_callback;
  92. ei.priv = (long)&complete;
  93. ret = mtd_erase(mtd, &ei);
  94. if (ret)
  95. return -EIO;
  96. wait_for_completion(&complete);
  97. if (ei.state != MTD_ERASE_DONE)
  98. return -EIO;
  99. return logfs_mtd_erase_mapping(sb, ofs, len);
  100. }
  101. static void logfs_mtd_sync(struct super_block *sb)
  102. {
  103. struct mtd_info *mtd = logfs_super(sb)->s_mtd;
  104. mtd_sync(mtd);
  105. }
  106. static int logfs_mtd_readpage(void *_sb, struct page *page)
  107. {
  108. struct super_block *sb = _sb;
  109. int err;
  110. err = logfs_mtd_read(sb, page->index << PAGE_SHIFT, PAGE_SIZE,
  111. page_address(page));
  112. if (err == -EUCLEAN || err == -EBADMSG) {
  113. /* -EBADMSG happens regularly on power failures */
  114. err = 0;
  115. /* FIXME: force GC this segment */
  116. }
  117. if (err) {
  118. ClearPageUptodate(page);
  119. SetPageError(page);
  120. } else {
  121. SetPageUptodate(page);
  122. ClearPageError(page);
  123. }
  124. unlock_page(page);
  125. return err;
  126. }
  127. static struct page *logfs_mtd_find_first_sb(struct super_block *sb, u64 *ofs)
  128. {
  129. struct logfs_super *super = logfs_super(sb);
  130. struct address_space *mapping = super->s_mapping_inode->i_mapping;
  131. filler_t *filler = logfs_mtd_readpage;
  132. struct mtd_info *mtd = super->s_mtd;
  133. *ofs = 0;
  134. while (mtd_block_isbad(mtd, *ofs)) {
  135. *ofs += mtd->erasesize;
  136. if (*ofs >= mtd->size)
  137. return NULL;
  138. }
  139. BUG_ON(*ofs & ~PAGE_MASK);
  140. return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb);
  141. }
  142. static struct page *logfs_mtd_find_last_sb(struct super_block *sb, u64 *ofs)
  143. {
  144. struct logfs_super *super = logfs_super(sb);
  145. struct address_space *mapping = super->s_mapping_inode->i_mapping;
  146. filler_t *filler = logfs_mtd_readpage;
  147. struct mtd_info *mtd = super->s_mtd;
  148. *ofs = mtd->size - mtd->erasesize;
  149. while (mtd_block_isbad(mtd, *ofs)) {
  150. *ofs -= mtd->erasesize;
  151. if (*ofs <= 0)
  152. return NULL;
  153. }
  154. *ofs = *ofs + mtd->erasesize - 0x1000;
  155. BUG_ON(*ofs & ~PAGE_MASK);
  156. return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb);
  157. }
  158. static int __logfs_mtd_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
  159. size_t nr_pages)
  160. {
  161. struct logfs_super *super = logfs_super(sb);
  162. struct address_space *mapping = super->s_mapping_inode->i_mapping;
  163. struct page *page;
  164. int i, err;
  165. for (i = 0; i < nr_pages; i++) {
  166. page = find_lock_page(mapping, index + i);
  167. BUG_ON(!page);
  168. err = loffs_mtd_write(sb, page->index << PAGE_SHIFT, PAGE_SIZE,
  169. page_address(page));
  170. unlock_page(page);
  171. page_cache_release(page);
  172. if (err)
  173. return err;
  174. }
  175. return 0;
  176. }
  177. static void logfs_mtd_writeseg(struct super_block *sb, u64 ofs, size_t len)
  178. {
  179. struct logfs_super *super = logfs_super(sb);
  180. int head;
  181. if (super->s_flags & LOGFS_SB_FLAG_RO)
  182. return;
  183. if (len == 0) {
  184. /* This can happen when the object fit perfectly into a
  185. * segment, the segment gets written per sync and subsequently
  186. * closed.
  187. */
  188. return;
  189. }
  190. head = ofs & (PAGE_SIZE - 1);
  191. if (head) {
  192. ofs -= head;
  193. len += head;
  194. }
  195. len = PAGE_ALIGN(len);
  196. __logfs_mtd_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
  197. }
  198. static void logfs_mtd_put_device(struct logfs_super *s)
  199. {
  200. put_mtd_device(s->s_mtd);
  201. }
  202. static int logfs_mtd_can_write_buf(struct super_block *sb, u64 ofs)
  203. {
  204. struct logfs_super *super = logfs_super(sb);
  205. void *buf;
  206. int err;
  207. buf = kmalloc(super->s_writesize, GFP_KERNEL);
  208. if (!buf)
  209. return -ENOMEM;
  210. err = logfs_mtd_read(sb, ofs, super->s_writesize, buf);
  211. if (err)
  212. goto out;
  213. if (memchr_inv(buf, 0xff, super->s_writesize))
  214. err = -EIO;
  215. kfree(buf);
  216. out:
  217. return err;
  218. }
  219. static const struct logfs_device_ops mtd_devops = {
  220. .find_first_sb = logfs_mtd_find_first_sb,
  221. .find_last_sb = logfs_mtd_find_last_sb,
  222. .readpage = logfs_mtd_readpage,
  223. .writeseg = logfs_mtd_writeseg,
  224. .erase = logfs_mtd_erase,
  225. .can_write_buf = logfs_mtd_can_write_buf,
  226. .sync = logfs_mtd_sync,
  227. .put_device = logfs_mtd_put_device,
  228. };
  229. int logfs_get_sb_mtd(struct logfs_super *s, int mtdnr)
  230. {
  231. struct mtd_info *mtd = get_mtd_device(NULL, mtdnr);
  232. if (IS_ERR(mtd))
  233. return PTR_ERR(mtd);
  234. s->s_bdev = NULL;
  235. s->s_mtd = mtd;
  236. s->s_devops = &mtd_devops;
  237. return 0;
  238. }