block2mtd.c 12 KB

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