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