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