Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
block2mtd.c
Go to the documentation of this file.
1 /*
2  * block2mtd.c - create an mtd from a block device
3  *
4  * Copyright (C) 2001,2002 Simon Evans <[email protected]>
5  * Copyright (C) 2004-2006 Joern Engel <[email protected]>
6  *
7  * Licence: GPL
8  */
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/mutex.h>
18 #include <linux/mount.h>
19 #include <linux/slab.h>
20 
21 #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
22 #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
23 
24 
25 /* Info for the block device */
26 struct block2mtd_dev {
27  struct list_head list;
29  struct mtd_info mtd;
31 };
32 
33 
34 /* Static info about the MTD, used in cleanup_module */
35 static LIST_HEAD(blkmtd_device_list);
36 
37 
38 static struct page *page_read(struct address_space *mapping, int index)
39 {
40  return read_mapping_page(mapping, index, NULL);
41 }
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 
53  while (pages) {
54  page = page_read(mapping, index);
55  if (IS_ERR(page))
56  return PTR_ERR(page);
57 
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  break;
66  }
67 
68  page_cache_release(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 
81  instr->state = MTD_ERASING;
82  mutex_lock(&dev->write_mutex);
83  err = _block2mtd_erase(dev, from, len);
85  if (err) {
86  ERROR("erase failed err = %d", err);
87  instr->state = MTD_ERASE_FAILED;
88  } else
89  instr->state = MTD_ERASE_DONE;
90 
91  mtd_erase_callback(instr);
92  return err;
93 }
94 
95 
96 static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
97  size_t *retlen, u_char *buf)
98 {
99  struct block2mtd_dev *dev = mtd->priv;
100  struct page *page;
101  int index = from >> PAGE_SHIFT;
102  int offset = from & (PAGE_SIZE-1);
103  int cpylen;
104 
105  while (len) {
106  if ((offset + len) > PAGE_SIZE)
107  cpylen = PAGE_SIZE - offset; // multiple pages
108  else
109  cpylen = len; // this page
110  len = len - cpylen;
111 
112  page = page_read(dev->blkdev->bd_inode->i_mapping, index);
113  if (IS_ERR(page))
114  return PTR_ERR(page);
115 
116  memcpy(buf, page_address(page) + offset, cpylen);
117  page_cache_release(page);
118 
119  if (retlen)
120  *retlen += cpylen;
121  buf += cpylen;
122  offset = 0;
123  index++;
124  }
125  return 0;
126 }
127 
128 
129 /* write data to the underlying device */
130 static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
131  loff_t to, size_t len, size_t *retlen)
132 {
133  struct page *page;
134  struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
135  int index = to >> PAGE_SHIFT; // page index
136  int offset = to & ~PAGE_MASK; // page offset
137  int cpylen;
138 
139  while (len) {
140  if ((offset+len) > PAGE_SIZE)
141  cpylen = PAGE_SIZE - offset; // multiple pages
142  else
143  cpylen = len; // this page
144  len = len - cpylen;
145 
146  page = page_read(mapping, index);
147  if (IS_ERR(page))
148  return PTR_ERR(page);
149 
150  if (memcmp(page_address(page)+offset, buf, cpylen)) {
151  lock_page(page);
152  memcpy(page_address(page) + offset, buf, cpylen);
153  set_page_dirty(page);
154  unlock_page(page);
155  }
156  page_cache_release(page);
157 
158  if (retlen)
159  *retlen += cpylen;
160 
161  buf += cpylen;
162  offset = 0;
163  index++;
164  }
165  return 0;
166 }
167 
168 
169 static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
170  size_t *retlen, const u_char *buf)
171 {
172  struct block2mtd_dev *dev = mtd->priv;
173  int err;
174 
175  mutex_lock(&dev->write_mutex);
176  err = _block2mtd_write(dev, buf, to, len, retlen);
177  mutex_unlock(&dev->write_mutex);
178  if (err > 0)
179  err = 0;
180  return err;
181 }
182 
183 
184 /* sync the device - wait until the write queue is empty */
185 static void block2mtd_sync(struct mtd_info *mtd)
186 {
187  struct block2mtd_dev *dev = mtd->priv;
188  sync_blockdev(dev->blkdev);
189  return;
190 }
191 
192 
193 static void block2mtd_free_device(struct block2mtd_dev *dev)
194 {
195  if (!dev)
196  return;
197 
198  kfree(dev->mtd.name);
199 
200  if (dev->blkdev) {
201  invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
202  0, -1);
204  }
205 
206  kfree(dev);
207 }
208 
209 
210 /* FIXME: ensure that mtd->size % erase_size == 0 */
211 static struct block2mtd_dev *add_device(char *devname, int erase_size)
212 {
214  struct block_device *bdev;
215  struct block2mtd_dev *dev;
216  char *name;
217 
218  if (!devname)
219  return NULL;
220 
221  dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
222  if (!dev)
223  return NULL;
224 
225  /* Get a handle on the device */
226  bdev = blkdev_get_by_path(devname, mode, dev);
227 #ifndef MODULE
228  if (IS_ERR(bdev)) {
229 
230  /* We might not have rootfs mounted at this point. Try
231  to resolve the device name by other means. */
232 
233  dev_t devt = name_to_dev_t(devname);
234  if (devt)
235  bdev = blkdev_get_by_dev(devt, mode, dev);
236  }
237 #endif
238 
239  if (IS_ERR(bdev)) {
240  ERROR("error: cannot open device %s", devname);
241  goto devinit_err;
242  }
243  dev->blkdev = bdev;
244 
245  if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
246  ERROR("attempting to use an MTD device as a block device");
247  goto devinit_err;
248  }
249 
250  mutex_init(&dev->write_mutex);
251 
252  /* Setup the MTD structure */
253  /* make the name contain the block device in */
254  name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
255  if (!name)
256  goto devinit_err;
257 
258  dev->mtd.name = name;
259 
260  dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
261  dev->mtd.erasesize = erase_size;
262  dev->mtd.writesize = 1;
263  dev->mtd.writebufsize = PAGE_SIZE;
264  dev->mtd.type = MTD_RAM;
265  dev->mtd.flags = MTD_CAP_RAM;
266  dev->mtd._erase = block2mtd_erase;
267  dev->mtd._write = block2mtd_write;
268  dev->mtd._sync = block2mtd_sync;
269  dev->mtd._read = block2mtd_read;
270  dev->mtd.priv = dev;
271  dev->mtd.owner = THIS_MODULE;
272 
273  if (mtd_device_register(&dev->mtd, NULL, 0)) {
274  /* Device didn't get added, so free the entry */
275  goto devinit_err;
276  }
277  list_add(&dev->list, &blkmtd_device_list);
278  INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
279  dev->mtd.name + strlen("block2mtd: "),
280  dev->mtd.erasesize >> 10, dev->mtd.erasesize);
281  return dev;
282 
283 devinit_err:
284  block2mtd_free_device(dev);
285  return NULL;
286 }
287 
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  case 'M':
302  result *= 1024;
303  case 'K':
304  case 'k':
305  result *= 1024;
306  /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
307  if ((*endp)[1] == 'i') {
308  if ((*endp)[2] == 'B')
309  (*endp) += 3;
310  else
311  (*endp) += 2;
312  }
313  }
314  return result;
315 }
316 
317 
318 static int parse_num(size_t *num, const char *token)
319 {
320  char *endp;
321  size_t n;
322 
323  n = (size_t) ustrtoul(token, &endp, 0);
324  if (*endp)
325  return -EINVAL;
326 
327  *num = n;
328  return 0;
329 }
330 
331 
332 static inline void kill_final_newline(char *str)
333 {
334  char *newline = strrchr(str, '\n');
335  if (newline && !newline[1])
336  *newline = 0;
337 }
338 
339 
340 #define parse_err(fmt, args...) do { \
341  ERROR(fmt, ## args); \
342  return 0; \
343 } while (0)
344 
345 #ifndef MODULE
346 static int block2mtd_init_called = 0;
347 static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
348 #endif
349 
350 
351 static int block2mtd_setup2(const char *val)
352 {
353  char buf[80 + 12]; /* 80 for device, 12 for erase size */
354  char *str = buf;
355  char *token[2];
356  char *name;
357  size_t erase_size = PAGE_SIZE;
358  int i, ret;
359 
360  if (strnlen(val, sizeof(buf)) >= sizeof(buf))
361  parse_err("parameter too long");
362 
363  strcpy(str, val);
364  kill_final_newline(str);
365 
366  for (i = 0; i < 2; i++)
367  token[i] = strsep(&str, ",");
368 
369  if (str)
370  parse_err("too many arguments");
371 
372  if (!token[0])
373  parse_err("no argument");
374 
375  name = token[0];
376  if (strlen(name) + 1 > 80)
377  parse_err("device name too long");
378 
379  if (token[1]) {
380  ret = parse_num(&erase_size, token[1]);
381  if (ret) {
382  parse_err("illegal erase size");
383  }
384  }
385 
386  add_device(name, erase_size);
387 
388  return 0;
389 }
390 
391 
392 static int block2mtd_setup(const char *val, struct kernel_param *kp)
393 {
394 #ifdef MODULE
395  return block2mtd_setup2(val);
396 #else
397  /* If more parameters are later passed in via
398  /sys/module/block2mtd/parameters/block2mtd
399  and block2mtd_init() has already been called,
400  we can parse the argument now. */
401 
402  if (block2mtd_init_called)
403  return block2mtd_setup2(val);
404 
405  /* During early boot stage, we only save the parameters
406  here. We must parse them later: if the param passed
407  from kernel boot command line, block2mtd_setup() is
408  called so early that it is not possible to resolve
409  the device (even kmalloc() fails). Deter that work to
410  block2mtd_setup2(). */
411 
412  strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
413 
414  return 0;
415 #endif
416 }
417 
418 
419 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
420 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
421 
422 static int __init block2mtd_init(void)
423 {
424  int ret = 0;
425 
426 #ifndef MODULE
427  if (strlen(block2mtd_paramline))
428  ret = block2mtd_setup2(block2mtd_paramline);
429  block2mtd_init_called = 1;
430 #endif
431 
432  return ret;
433 }
434 
435 
436 static void __devexit block2mtd_exit(void)
437 {
438  struct list_head *pos, *next;
439 
440  /* Remove the MTD devices */
441  list_for_each_safe(pos, next, &blkmtd_device_list) {
442  struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
443  block2mtd_sync(&dev->mtd);
444  mtd_device_unregister(&dev->mtd);
445  INFO("mtd%d: [%s] removed", dev->mtd.index,
446  dev->mtd.name + strlen("block2mtd: "));
447  list_del(&dev->list);
448  block2mtd_free_device(dev);
449  }
450 }
451 
452 
453 module_init(block2mtd_init);
454 module_exit(block2mtd_exit);
455 
456 MODULE_LICENSE("GPL");
457 MODULE_AUTHOR("Joern Engel <[email protected]>");
458 MODULE_DESCRIPTION("Emulate an MTD using a block device");