block2mtd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * block2mtd.c - create an mtd from a block device
  3. *
  4. * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
  5. * Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de>
  6. *
  7. * Licence: GPL
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. /*
  11. * When the first attempt at device initialization fails, we may need to
  12. * wait a little bit and retry. This timeout, by default 3 seconds, gives
  13. * device time to start up. Required on BCM2708 and a few other chipsets.
  14. */
  15. #define MTD_DEFAULT_TIMEOUT 3
  16. #include <linux/module.h>
  17. #include <linux/delay.h>
  18. #include <linux/fs.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/backing-dev.h>
  21. #include <linux/bio.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/list.h>
  24. #include <linux/init.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mutex.h>
  27. #include <linux/mount.h>
  28. #include <linux/slab.h>
  29. #include <linux/major.h>
  30. /* Info for the block device */
  31. struct block2mtd_dev {
  32. struct list_head list;
  33. struct block_device *blkdev;
  34. struct mtd_info mtd;
  35. struct mutex write_mutex;
  36. };
  37. /* Static info about the MTD, used in cleanup_module */
  38. static LIST_HEAD(blkmtd_device_list);
  39. static struct page *page_read(struct address_space *mapping, int index)
  40. {
  41. return read_mapping_page(mapping, index, NULL);
  42. }
  43. /* erase a specified part of the device */
  44. static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
  45. {
  46. struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
  47. struct page *page;
  48. int index = to >> PAGE_SHIFT; // page index
  49. int pages = len >> PAGE_SHIFT;
  50. u_long *p;
  51. u_long *max;
  52. while (pages) {
  53. page = page_read(mapping, index);
  54. if (IS_ERR(page))
  55. return PTR_ERR(page);
  56. max = page_address(page) + PAGE_SIZE;
  57. for (p=page_address(page); p<max; p++)
  58. if (*p != -1UL) {
  59. lock_page(page);
  60. memset(page_address(page), 0xff, PAGE_SIZE);
  61. set_page_dirty(page);
  62. unlock_page(page);
  63. balance_dirty_pages_ratelimited(mapping);
  64. break;
  65. }
  66. page_cache_release(page);
  67. pages--;
  68. index++;
  69. }
  70. return 0;
  71. }
  72. static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  73. {
  74. struct block2mtd_dev *dev = mtd->priv;
  75. size_t from = instr->addr;
  76. size_t len = instr->len;
  77. int err;
  78. instr->state = MTD_ERASING;
  79. mutex_lock(&dev->write_mutex);
  80. err = _block2mtd_erase(dev, from, len);
  81. mutex_unlock(&dev->write_mutex);
  82. if (err) {
  83. pr_err("erase failed err = %d\n", err);
  84. instr->state = MTD_ERASE_FAILED;
  85. } else
  86. instr->state = MTD_ERASE_DONE;
  87. mtd_erase_callback(instr);
  88. return err;
  89. }
  90. static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  91. size_t *retlen, u_char *buf)
  92. {
  93. struct block2mtd_dev *dev = mtd->priv;
  94. struct page *page;
  95. int index = from >> PAGE_SHIFT;
  96. int offset = from & (PAGE_SIZE-1);
  97. int cpylen;
  98. while (len) {
  99. if ((offset + len) > PAGE_SIZE)
  100. cpylen = PAGE_SIZE - offset; // multiple pages
  101. else
  102. cpylen = len; // this page
  103. len = len - cpylen;
  104. page = page_read(dev->blkdev->bd_inode->i_mapping, index);
  105. if (IS_ERR(page))
  106. return PTR_ERR(page);
  107. memcpy(buf, page_address(page) + offset, cpylen);
  108. page_cache_release(page);
  109. if (retlen)
  110. *retlen += cpylen;
  111. buf += cpylen;
  112. offset = 0;
  113. index++;
  114. }
  115. return 0;
  116. }
  117. /* write data to the underlying device */
  118. static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
  119. loff_t to, size_t len, size_t *retlen)
  120. {
  121. struct page *page;
  122. struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
  123. int index = to >> PAGE_SHIFT; // page index
  124. int offset = to & ~PAGE_MASK; // page offset
  125. int cpylen;
  126. while (len) {
  127. if ((offset+len) > PAGE_SIZE)
  128. cpylen = PAGE_SIZE - offset; // multiple pages
  129. else
  130. cpylen = len; // this page
  131. len = len - cpylen;
  132. page = page_read(mapping, index);
  133. if (IS_ERR(page))
  134. return PTR_ERR(page);
  135. if (memcmp(page_address(page)+offset, buf, cpylen)) {
  136. lock_page(page);
  137. memcpy(page_address(page) + offset, buf, cpylen);
  138. set_page_dirty(page);
  139. unlock_page(page);
  140. balance_dirty_pages_ratelimited(mapping);
  141. }
  142. page_cache_release(page);
  143. if (retlen)
  144. *retlen += cpylen;
  145. buf += cpylen;
  146. offset = 0;
  147. index++;
  148. }
  149. return 0;
  150. }
  151. static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  152. size_t *retlen, const u_char *buf)
  153. {
  154. struct block2mtd_dev *dev = mtd->priv;
  155. int err;
  156. mutex_lock(&dev->write_mutex);
  157. err = _block2mtd_write(dev, buf, to, len, retlen);
  158. mutex_unlock(&dev->write_mutex);
  159. if (err > 0)
  160. err = 0;
  161. return err;
  162. }
  163. /* sync the device - wait until the write queue is empty */
  164. static void block2mtd_sync(struct mtd_info *mtd)
  165. {
  166. struct block2mtd_dev *dev = mtd->priv;
  167. sync_blockdev(dev->blkdev);
  168. return;
  169. }
  170. static void block2mtd_free_device(struct block2mtd_dev *dev)
  171. {
  172. if (!dev)
  173. return;
  174. kfree(dev->mtd.name);
  175. if (dev->blkdev) {
  176. invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
  177. 0, -1);
  178. blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  179. }
  180. kfree(dev);
  181. }
  182. static struct block2mtd_dev *add_device(char *devname, int erase_size,
  183. int timeout)
  184. {
  185. #ifndef MODULE
  186. int i;
  187. #endif
  188. const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
  189. struct block_device *bdev = ERR_PTR(-ENODEV);
  190. struct block2mtd_dev *dev;
  191. char *name;
  192. if (!devname)
  193. return NULL;
  194. dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
  195. if (!dev)
  196. return NULL;
  197. /* Get a handle on the device */
  198. bdev = blkdev_get_by_path(devname, mode, dev);
  199. #ifndef MODULE
  200. /*
  201. * We might not have the root device mounted at this point.
  202. * Try to resolve the device name by other means.
  203. */
  204. for (i = 0; IS_ERR(bdev) && i <= timeout; i++) {
  205. dev_t devt;
  206. if (i)
  207. /*
  208. * Calling wait_for_device_probe in the first loop
  209. * was not enough, sleep for a bit in subsequent
  210. * go-arounds.
  211. */
  212. msleep(1000);
  213. wait_for_device_probe();
  214. devt = name_to_dev_t(devname);
  215. if (!devt)
  216. continue;
  217. bdev = blkdev_get_by_dev(devt, mode, dev);
  218. }
  219. #endif
  220. if (IS_ERR(bdev)) {
  221. pr_err("error: cannot open device %s\n", devname);
  222. goto err_free_block2mtd;
  223. }
  224. dev->blkdev = bdev;
  225. if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
  226. pr_err("attempting to use an MTD device as a block device\n");
  227. goto err_free_block2mtd;
  228. }
  229. if ((long)dev->blkdev->bd_inode->i_size % erase_size) {
  230. pr_err("erasesize must be a divisor of device size\n");
  231. goto err_free_block2mtd;
  232. }
  233. mutex_init(&dev->write_mutex);
  234. /* Setup the MTD structure */
  235. /* make the name contain the block device in */
  236. name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
  237. if (!name)
  238. goto err_destroy_mutex;
  239. dev->mtd.name = name;
  240. dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
  241. dev->mtd.erasesize = erase_size;
  242. dev->mtd.writesize = 1;
  243. dev->mtd.writebufsize = PAGE_SIZE;
  244. dev->mtd.type = MTD_RAM;
  245. dev->mtd.flags = MTD_CAP_RAM;
  246. dev->mtd._erase = block2mtd_erase;
  247. dev->mtd._write = block2mtd_write;
  248. dev->mtd._sync = block2mtd_sync;
  249. dev->mtd._read = block2mtd_read;
  250. dev->mtd.priv = dev;
  251. dev->mtd.owner = THIS_MODULE;
  252. if (mtd_device_register(&dev->mtd, NULL, 0)) {
  253. /* Device didn't get added, so free the entry */
  254. goto err_destroy_mutex;
  255. }
  256. list_add(&dev->list, &blkmtd_device_list);
  257. pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
  258. dev->mtd.index,
  259. dev->mtd.name + strlen("block2mtd: "),
  260. dev->mtd.erasesize >> 10, dev->mtd.erasesize);
  261. return dev;
  262. err_destroy_mutex:
  263. mutex_destroy(&dev->write_mutex);
  264. err_free_block2mtd:
  265. block2mtd_free_device(dev);
  266. return NULL;
  267. }
  268. /* This function works similar to reguler strtoul. In addition, it
  269. * allows some suffixes for a more human-readable number format:
  270. * ki, Ki, kiB, KiB - multiply result with 1024
  271. * Mi, MiB - multiply result with 1024^2
  272. * Gi, GiB - multiply result with 1024^3
  273. */
  274. static int ustrtoul(const char *cp, char **endp, unsigned int base)
  275. {
  276. unsigned long result = simple_strtoul(cp, endp, base);
  277. switch (**endp) {
  278. case 'G' :
  279. result *= 1024;
  280. case 'M':
  281. result *= 1024;
  282. case 'K':
  283. case 'k':
  284. result *= 1024;
  285. /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
  286. if ((*endp)[1] == 'i') {
  287. if ((*endp)[2] == 'B')
  288. (*endp) += 3;
  289. else
  290. (*endp) += 2;
  291. }
  292. }
  293. return result;
  294. }
  295. static int parse_num(size_t *num, const char *token)
  296. {
  297. char *endp;
  298. size_t n;
  299. n = (size_t) ustrtoul(token, &endp, 0);
  300. if (*endp)
  301. return -EINVAL;
  302. *num = n;
  303. return 0;
  304. }
  305. static inline void kill_final_newline(char *str)
  306. {
  307. char *newline = strrchr(str, '\n');
  308. if (newline && !newline[1])
  309. *newline = 0;
  310. }
  311. #ifndef MODULE
  312. static int block2mtd_init_called = 0;
  313. /* 80 for device, 12 for erase size */
  314. static char block2mtd_paramline[80 + 12];
  315. #endif
  316. static int block2mtd_setup2(const char *val)
  317. {
  318. /* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
  319. char buf[80 + 12 + 80 + 8];
  320. char *str = buf;
  321. char *token[2];
  322. char *name;
  323. size_t erase_size = PAGE_SIZE;
  324. unsigned long timeout = MTD_DEFAULT_TIMEOUT;
  325. int i, ret;
  326. if (strnlen(val, sizeof(buf)) >= sizeof(buf)) {
  327. pr_err("parameter too long\n");
  328. return 0;
  329. }
  330. strcpy(str, val);
  331. kill_final_newline(str);
  332. for (i = 0; i < 2; i++)
  333. token[i] = strsep(&str, ",");
  334. if (str) {
  335. pr_err("too many arguments\n");
  336. return 0;
  337. }
  338. if (!token[0]) {
  339. pr_err("no argument\n");
  340. return 0;
  341. }
  342. name = token[0];
  343. if (strlen(name) + 1 > 80) {
  344. pr_err("device name too long\n");
  345. return 0;
  346. }
  347. if (token[1]) {
  348. ret = parse_num(&erase_size, token[1]);
  349. if (ret) {
  350. pr_err("illegal erase size\n");
  351. return 0;
  352. }
  353. }
  354. add_device(name, erase_size, timeout);
  355. return 0;
  356. }
  357. static int block2mtd_setup(const char *val, struct kernel_param *kp)
  358. {
  359. #ifdef MODULE
  360. return block2mtd_setup2(val);
  361. #else
  362. /* If more parameters are later passed in via
  363. /sys/module/block2mtd/parameters/block2mtd
  364. and block2mtd_init() has already been called,
  365. we can parse the argument now. */
  366. if (block2mtd_init_called)
  367. return block2mtd_setup2(val);
  368. /* During early boot stage, we only save the parameters
  369. here. We must parse them later: if the param passed
  370. from kernel boot command line, block2mtd_setup() is
  371. called so early that it is not possible to resolve
  372. the device (even kmalloc() fails). Deter that work to
  373. block2mtd_setup2(). */
  374. strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
  375. return 0;
  376. #endif
  377. }
  378. module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
  379. MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
  380. static int __init block2mtd_init(void)
  381. {
  382. int ret = 0;
  383. #ifndef MODULE
  384. if (strlen(block2mtd_paramline))
  385. ret = block2mtd_setup2(block2mtd_paramline);
  386. block2mtd_init_called = 1;
  387. #endif
  388. return ret;
  389. }
  390. static void block2mtd_exit(void)
  391. {
  392. struct list_head *pos, *next;
  393. /* Remove the MTD devices */
  394. list_for_each_safe(pos, next, &blkmtd_device_list) {
  395. struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
  396. block2mtd_sync(&dev->mtd);
  397. mtd_device_unregister(&dev->mtd);
  398. mutex_destroy(&dev->write_mutex);
  399. pr_info("mtd%d: [%s] removed\n",
  400. dev->mtd.index,
  401. dev->mtd.name + strlen("block2mtd: "));
  402. list_del(&dev->list);
  403. block2mtd_free_device(dev);
  404. }
  405. }
  406. late_initcall(block2mtd_init);
  407. module_exit(block2mtd_exit);
  408. MODULE_LICENSE("GPL");
  409. MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
  410. MODULE_DESCRIPTION("Emulate an MTD using a block device");