block2mtd.c 11 KB

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