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