Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * block2mtd.c - create an mtd from a block device |
| 3 | * |
| 4 | * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk> |
Joern Engel | 2b54aae | 2008-02-06 01:38:02 -0800 | [diff] [blame] | 5 | * Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Licence: GPL |
| 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 19 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 28 | struct block_device *blkdev; |
| 29 | struct mtd_info mtd; |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 30 | struct mutex write_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | |
| 34 | /* Static info about the MTD, used in cleanup_module */ |
| 35 | static LIST_HEAD(blkmtd_device_list); |
| 36 | |
| 37 | |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 38 | static struct page *page_read(struct address_space *mapping, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 40 | return read_mapping_page(mapping, index, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Joern Engel | 21d31f1 | 2007-02-20 20:22:22 +0100 | [diff] [blame] | 54 | page = page_read(mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | if (!page) |
| 56 | return -ENOMEM; |
| 57 | if (IS_ERR(page)) |
| 58 | return PTR_ERR(page); |
| 59 | |
Joern Engel | 0ffb74c | 2007-02-20 20:20:58 +0100 | [diff] [blame] | 60 | max = page_address(page) + PAGE_SIZE; |
| 61 | for (p=page_address(page); p<max; p++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | if (*p != -1UL) { |
| 63 | lock_page(page); |
| 64 | memset(page_address(page), 0xff, PAGE_SIZE); |
| 65 | set_page_dirty(page); |
| 66 | unlock_page(page); |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | page_cache_release(page); |
| 71 | pages--; |
| 72 | index++; |
| 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 77 | { |
| 78 | struct block2mtd_dev *dev = mtd->priv; |
| 79 | size_t from = instr->addr; |
| 80 | size_t len = instr->len; |
| 81 | int err; |
| 82 | |
| 83 | instr->state = MTD_ERASING; |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 84 | mutex_lock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | err = _block2mtd_erase(dev, from, len); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 86 | mutex_unlock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if (err) { |
| 88 | ERROR("erase failed err = %d", err); |
| 89 | instr->state = MTD_ERASE_FAILED; |
| 90 | } else |
| 91 | instr->state = MTD_ERASE_DONE; |
| 92 | |
| 93 | instr->state = MTD_ERASE_DONE; |
| 94 | mtd_erase_callback(instr); |
| 95 | return err; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | static 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; |
Joern Engel | 711c11b | 2005-03-07 20:29:09 +0000 | [diff] [blame] | 105 | int offset = from & (PAGE_SIZE-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Joern Engel | 21d31f1 | 2007-02-20 20:22:22 +0100 | [diff] [blame] | 123 | page = page_read(dev->blkdev->bd_inode->i_mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
| 143 | static 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) { |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 155 | if ((offset+len) > PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | cpylen = PAGE_SIZE - offset; // multiple pages |
| 157 | else |
| 158 | cpylen = len; // this page |
| 159 | len = len - cpylen; |
| 160 | |
Joern Engel | 21d31f1 | 2007-02-20 20:22:22 +0100 | [diff] [blame] | 161 | page = page_read(mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 184 | |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | static 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 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 199 | mutex_lock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | err = _block2mtd_write(dev, buf, to, len, retlen); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 201 | mutex_unlock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | if (err > 0) |
| 203 | err = 0; |
| 204 | return err; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /* sync the device - wait until the write queue is empty */ |
| 209 | static 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 | |
| 217 | static 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) { |
Andrew Morton | fc0ecff | 2007-02-10 01:45:39 -0800 | [diff] [blame] | 225 | invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, |
| 226 | 0, -1); |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 227 | close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | kfree(dev); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /* FIXME: ensure that mtd->size % erase_size == 0 */ |
| 235 | static struct block2mtd_dev *add_device(char *devname, int erase_size) |
| 236 | { |
| 237 | struct block_device *bdev; |
| 238 | struct block2mtd_dev *dev; |
Greg Kroah-Hartman | eadcf0d | 2008-07-02 12:46:22 -0700 | [diff] [blame] | 239 | char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | if (!devname) |
| 242 | return NULL; |
| 243 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 244 | dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | if (!dev) |
| 246 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | /* Get a handle on the device */ |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 249 | bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL); |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 250 | #ifndef MODULE |
| 251 | if (IS_ERR(bdev)) { |
| 252 | |
| 253 | /* We might not have rootfs mounted at this point. Try |
| 254 | to resolve the device name by other means. */ |
| 255 | |
Joern Engel | 8870530 | 2007-02-20 20:21:41 +0100 | [diff] [blame] | 256 | dev_t devt = name_to_dev_t(devname); |
| 257 | if (devt) { |
| 258 | bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ); |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | #endif |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (IS_ERR(bdev)) { |
| 264 | ERROR("error: cannot open device %s", devname); |
| 265 | goto devinit_err; |
| 266 | } |
| 267 | dev->blkdev = bdev; |
| 268 | |
| 269 | if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) { |
| 270 | ERROR("attempting to use an MTD device as a block device"); |
| 271 | goto devinit_err; |
| 272 | } |
| 273 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 274 | mutex_init(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
| 276 | /* Setup the MTD structure */ |
| 277 | /* make the name contain the block device in */ |
Greg Kroah-Hartman | eadcf0d | 2008-07-02 12:46:22 -0700 | [diff] [blame] | 278 | name = kmalloc(sizeof("block2mtd: ") + strlen(devname) + 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | GFP_KERNEL); |
Greg Kroah-Hartman | eadcf0d | 2008-07-02 12:46:22 -0700 | [diff] [blame] | 280 | if (!name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | goto devinit_err; |
| 282 | |
Greg Kroah-Hartman | eadcf0d | 2008-07-02 12:46:22 -0700 | [diff] [blame] | 283 | sprintf(name, "block2mtd: %s", devname); |
| 284 | dev->mtd.name = name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK; |
| 287 | dev->mtd.erasesize = erase_size; |
Artem B. Bityutskiy | 17ffc7b | 2006-06-22 18:15:48 +0400 | [diff] [blame] | 288 | dev->mtd.writesize = 1; |
David Woodhouse | 21c8db9 | 2006-06-14 21:39:48 +0100 | [diff] [blame] | 289 | dev->mtd.type = MTD_RAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | dev->mtd.flags = MTD_CAP_RAM; |
| 291 | dev->mtd.erase = block2mtd_erase; |
| 292 | dev->mtd.write = block2mtd_write; |
| 293 | dev->mtd.writev = default_mtd_writev; |
| 294 | dev->mtd.sync = block2mtd_sync; |
| 295 | dev->mtd.read = block2mtd_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | dev->mtd.priv = dev; |
| 297 | dev->mtd.owner = THIS_MODULE; |
| 298 | |
| 299 | if (add_mtd_device(&dev->mtd)) { |
| 300 | /* Device didnt get added, so free the entry */ |
| 301 | goto devinit_err; |
| 302 | } |
| 303 | list_add(&dev->list, &blkmtd_device_list); |
| 304 | INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index, |
Stephane Chazelas | 0bc88c5 | 2008-04-18 13:44:15 -0700 | [diff] [blame] | 305 | dev->mtd.name + strlen("block2mtd: "), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | dev->mtd.erasesize >> 10, dev->mtd.erasesize); |
| 307 | return dev; |
| 308 | |
| 309 | devinit_err: |
| 310 | block2mtd_free_device(dev); |
| 311 | return NULL; |
| 312 | } |
| 313 | |
| 314 | |
Joern Engel | 954c242 | 2006-04-18 21:03:08 -0700 | [diff] [blame] | 315 | /* This function works similar to reguler strtoul. In addition, it |
| 316 | * allows some suffixes for a more human-readable number format: |
| 317 | * ki, Ki, kiB, KiB - multiply result with 1024 |
| 318 | * Mi, MiB - multiply result with 1024^2 |
| 319 | * Gi, GiB - multiply result with 1024^3 |
| 320 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | static int ustrtoul(const char *cp, char **endp, unsigned int base) |
| 322 | { |
| 323 | unsigned long result = simple_strtoul(cp, endp, base); |
| 324 | switch (**endp) { |
| 325 | case 'G' : |
| 326 | result *= 1024; |
| 327 | case 'M': |
| 328 | result *= 1024; |
Joern Engel | 954c242 | 2006-04-18 21:03:08 -0700 | [diff] [blame] | 329 | case 'K': |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | case 'k': |
| 331 | result *= 1024; |
| 332 | /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */ |
Joern Engel | 954c242 | 2006-04-18 21:03:08 -0700 | [diff] [blame] | 333 | if ((*endp)[1] == 'i') { |
| 334 | if ((*endp)[2] == 'B') |
| 335 | (*endp) += 3; |
| 336 | else |
| 337 | (*endp) += 2; |
| 338 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | } |
| 340 | return result; |
| 341 | } |
| 342 | |
| 343 | |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 344 | static int parse_num(size_t *num, const char *token) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
| 346 | char *endp; |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 347 | size_t n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 349 | n = (size_t) ustrtoul(token, &endp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if (*endp) |
| 351 | return -EINVAL; |
| 352 | |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 353 | *num = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | static inline void kill_final_newline(char *str) |
| 359 | { |
| 360 | char *newline = strrchr(str, '\n'); |
| 361 | if (newline && !newline[1]) |
| 362 | *newline = 0; |
| 363 | } |
| 364 | |
| 365 | |
Stephane Chazelas | 0bc88c5 | 2008-04-18 13:44:15 -0700 | [diff] [blame] | 366 | #define parse_err(fmt, args...) do { \ |
| 367 | ERROR(fmt, ## args); \ |
| 368 | return 0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } while (0) |
| 370 | |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 371 | #ifndef MODULE |
| 372 | static int block2mtd_init_called = 0; |
Adrian Bunk | 4839f04 | 2007-05-02 12:33:17 +0100 | [diff] [blame] | 373 | static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */ |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 374 | #endif |
| 375 | |
| 376 | |
| 377 | static int block2mtd_setup2(const char *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | { |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 379 | char buf[80 + 12]; /* 80 for device, 12 for erase size */ |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 380 | char *str = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | char *token[2]; |
| 382 | char *name; |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 383 | size_t erase_size = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | int i, ret; |
| 385 | |
| 386 | if (strnlen(val, sizeof(buf)) >= sizeof(buf)) |
| 387 | parse_err("parameter too long"); |
| 388 | |
| 389 | strcpy(str, val); |
| 390 | kill_final_newline(str); |
| 391 | |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 392 | for (i = 0; i < 2; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | token[i] = strsep(&str, ","); |
| 394 | |
| 395 | if (str) |
| 396 | parse_err("too many arguments"); |
| 397 | |
| 398 | if (!token[0]) |
| 399 | parse_err("no argument"); |
| 400 | |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 401 | name = token[0]; |
| 402 | if (strlen(name) + 1 > 80) |
| 403 | parse_err("device name too long"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | if (token[1]) { |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 406 | ret = parse_num(&erase_size, token[1]); |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 407 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | parse_err("illegal erase size"); |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 409 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | add_device(name, erase_size); |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 418 | static int block2mtd_setup(const char *val, struct kernel_param *kp) |
| 419 | { |
| 420 | #ifdef MODULE |
| 421 | return block2mtd_setup2(val); |
| 422 | #else |
| 423 | /* If more parameters are later passed in via |
| 424 | /sys/module/block2mtd/parameters/block2mtd |
| 425 | and block2mtd_init() has already been called, |
| 426 | we can parse the argument now. */ |
| 427 | |
| 428 | if (block2mtd_init_called) |
| 429 | return block2mtd_setup2(val); |
| 430 | |
| 431 | /* During early boot stage, we only save the parameters |
| 432 | here. We must parse them later: if the param passed |
| 433 | from kernel boot command line, block2mtd_setup() is |
| 434 | called so early that it is not possible to resolve |
| 435 | the device (even kmalloc() fails). Deter that work to |
| 436 | block2mtd_setup2(). */ |
| 437 | |
| 438 | strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline)); |
| 439 | |
| 440 | return 0; |
| 441 | #endif |
| 442 | } |
| 443 | |
| 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200); |
| 446 | MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\""); |
| 447 | |
| 448 | static int __init block2mtd_init(void) |
| 449 | { |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 450 | int ret = 0; |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 451 | |
| 452 | #ifndef MODULE |
| 453 | if (strlen(block2mtd_paramline)) |
| 454 | ret = block2mtd_setup2(block2mtd_paramline); |
| 455 | block2mtd_init_called = 1; |
| 456 | #endif |
| 457 | |
| 458 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | |
| 462 | static void __devexit block2mtd_exit(void) |
| 463 | { |
| 464 | struct list_head *pos, *next; |
| 465 | |
| 466 | /* Remove the MTD devices */ |
| 467 | list_for_each_safe(pos, next, &blkmtd_device_list) { |
| 468 | struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list); |
| 469 | block2mtd_sync(&dev->mtd); |
| 470 | del_mtd_device(&dev->mtd); |
| 471 | INFO("mtd%d: [%s] removed", dev->mtd.index, |
Stephane Chazelas | 0bc88c5 | 2008-04-18 13:44:15 -0700 | [diff] [blame] | 472 | dev->mtd.name + strlen("block2mtd: ")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | list_del(&dev->list); |
| 474 | block2mtd_free_device(dev); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | |
| 479 | module_init(block2mtd_init); |
| 480 | module_exit(block2mtd_exit); |
| 481 | |
| 482 | MODULE_LICENSE("GPL"); |
Joern Engel | 2b54aae | 2008-02-06 01:38:02 -0800 | [diff] [blame] | 483 | MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | MODULE_DESCRIPTION("Emulate an MTD using a block device"); |