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 | */ |
Joe Perches | a1c0660 | 2013-04-19 10:59:35 -0700 | [diff] [blame] | 9 | |
| 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/blkdev.h> |
| 15 | #include <linux/bio.h> |
| 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/mtd/mtd.h> |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 20 | #include <linux/mutex.h> |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 21 | #include <linux/mount.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Ezequiel Garcia | f83c383 | 2013-10-13 18:05:23 -0300 | [diff] [blame] | 23 | #include <linux/major.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 (IS_ERR(page)) |
| 56 | return PTR_ERR(page); |
| 57 | |
Joern Engel | 0ffb74c | 2007-02-20 20:20:58 +0100 | [diff] [blame] | 58 | max = page_address(page) + PAGE_SIZE; |
| 59 | for (p=page_address(page); p<max; p++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
NeilBrown | 031da73 | 2012-12-12 19:32:15 +1100 | [diff] [blame] | 65 | balance_dirty_pages_ratelimited(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | break; |
| 67 | } |
| 68 | |
| 69 | page_cache_release(page); |
| 70 | pages--; |
| 71 | index++; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 76 | { |
| 77 | struct block2mtd_dev *dev = mtd->priv; |
| 78 | size_t from = instr->addr; |
| 79 | size_t len = instr->len; |
| 80 | int err; |
| 81 | |
| 82 | instr->state = MTD_ERASING; |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 83 | mutex_lock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | err = _block2mtd_erase(dev, from, len); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 85 | mutex_unlock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | if (err) { |
Joe Perches | a1c0660 | 2013-04-19 10:59:35 -0700 | [diff] [blame] | 87 | pr_err("erase failed err = %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | instr->state = MTD_ERASE_FAILED; |
| 89 | } else |
| 90 | instr->state = MTD_ERASE_DONE; |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | mtd_erase_callback(instr); |
| 93 | return err; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 98 | size_t *retlen, u_char *buf) |
| 99 | { |
| 100 | struct block2mtd_dev *dev = mtd->priv; |
| 101 | struct page *page; |
| 102 | int index = from >> PAGE_SHIFT; |
Joern Engel | 711c11b | 2005-03-07 20:29:09 +0000 | [diff] [blame] | 103 | int offset = from & (PAGE_SIZE-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | int cpylen; |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | while (len) { |
| 107 | if ((offset + len) > PAGE_SIZE) |
| 108 | cpylen = PAGE_SIZE - offset; // multiple pages |
| 109 | else |
| 110 | cpylen = len; // this page |
| 111 | len = len - cpylen; |
| 112 | |
Joern Engel | 21d31f1 | 2007-02-20 20:22:22 +0100 | [diff] [blame] | 113 | page = page_read(dev->blkdev->bd_inode->i_mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | if (IS_ERR(page)) |
| 115 | return PTR_ERR(page); |
| 116 | |
| 117 | memcpy(buf, page_address(page) + offset, cpylen); |
| 118 | page_cache_release(page); |
| 119 | |
| 120 | if (retlen) |
| 121 | *retlen += cpylen; |
| 122 | buf += cpylen; |
| 123 | offset = 0; |
| 124 | index++; |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* write data to the underlying device */ |
| 131 | static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf, |
| 132 | loff_t to, size_t len, size_t *retlen) |
| 133 | { |
| 134 | struct page *page; |
| 135 | struct address_space *mapping = dev->blkdev->bd_inode->i_mapping; |
| 136 | int index = to >> PAGE_SHIFT; // page index |
| 137 | int offset = to & ~PAGE_MASK; // page offset |
| 138 | int cpylen; |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | while (len) { |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 141 | if ((offset+len) > PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | cpylen = PAGE_SIZE - offset; // multiple pages |
| 143 | else |
| 144 | cpylen = len; // this page |
| 145 | len = len - cpylen; |
| 146 | |
Joern Engel | 21d31f1 | 2007-02-20 20:22:22 +0100 | [diff] [blame] | 147 | page = page_read(mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | if (IS_ERR(page)) |
| 149 | return PTR_ERR(page); |
| 150 | |
| 151 | if (memcmp(page_address(page)+offset, buf, cpylen)) { |
| 152 | lock_page(page); |
| 153 | memcpy(page_address(page) + offset, buf, cpylen); |
| 154 | set_page_dirty(page); |
| 155 | unlock_page(page); |
NeilBrown | 031da73 | 2012-12-12 19:32:15 +1100 | [diff] [blame] | 156 | balance_dirty_pages_ratelimited(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | page_cache_release(page); |
| 159 | |
| 160 | if (retlen) |
| 161 | *retlen += cpylen; |
| 162 | |
| 163 | buf += cpylen; |
| 164 | offset = 0; |
| 165 | index++; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 169 | |
| 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 172 | size_t *retlen, const u_char *buf) |
| 173 | { |
| 174 | struct block2mtd_dev *dev = mtd->priv; |
| 175 | int err; |
| 176 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 177 | mutex_lock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | err = _block2mtd_write(dev, buf, to, len, retlen); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 179 | mutex_unlock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (err > 0) |
| 181 | err = 0; |
| 182 | return err; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /* sync the device - wait until the write queue is empty */ |
| 187 | static void block2mtd_sync(struct mtd_info *mtd) |
| 188 | { |
| 189 | struct block2mtd_dev *dev = mtd->priv; |
| 190 | sync_blockdev(dev->blkdev); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | static void block2mtd_free_device(struct block2mtd_dev *dev) |
| 196 | { |
| 197 | if (!dev) |
| 198 | return; |
| 199 | |
| 200 | kfree(dev->mtd.name); |
| 201 | |
| 202 | if (dev->blkdev) { |
Andrew Morton | fc0ecff | 2007-02-10 01:45:39 -0800 | [diff] [blame] | 203 | invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, |
| 204 | 0, -1); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 205 | blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | kfree(dev); |
| 209 | } |
| 210 | |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | static struct block2mtd_dev *add_device(char *devname, int erase_size) |
| 213 | { |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 214 | const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | struct block_device *bdev; |
| 216 | struct block2mtd_dev *dev; |
Greg Kroah-Hartman | eadcf0d | 2008-07-02 12:46:22 -0700 | [diff] [blame] | 217 | char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | if (!devname) |
| 220 | return NULL; |
| 221 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 222 | dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if (!dev) |
| 224 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | /* Get a handle on the device */ |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 227 | bdev = blkdev_get_by_path(devname, mode, dev); |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 228 | #ifndef MODULE |
| 229 | if (IS_ERR(bdev)) { |
| 230 | |
| 231 | /* We might not have rootfs mounted at this point. Try |
| 232 | to resolve the device name by other means. */ |
| 233 | |
Joern Engel | 8870530 | 2007-02-20 20:21:41 +0100 | [diff] [blame] | 234 | dev_t devt = name_to_dev_t(devname); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 235 | if (devt) |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 236 | bdev = blkdev_get_by_dev(devt, mode, dev); |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 237 | } |
| 238 | #endif |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | if (IS_ERR(bdev)) { |
Joe Perches | a1c0660 | 2013-04-19 10:59:35 -0700 | [diff] [blame] | 241 | pr_err("error: cannot open device %s\n", devname); |
Fabian Frederick | 4bed207 | 2014-01-25 10:45:08 +0800 | [diff] [blame] | 242 | goto err_free_block2mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | dev->blkdev = bdev; |
| 245 | |
| 246 | if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) { |
Joe Perches | a1c0660 | 2013-04-19 10:59:35 -0700 | [diff] [blame] | 247 | pr_err("attempting to use an MTD device as a block device\n"); |
Fabian Frederick | 4bed207 | 2014-01-25 10:45:08 +0800 | [diff] [blame] | 248 | goto err_free_block2mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Fabian Frederick | ea6d833 | 2014-03-06 18:04:22 +0800 | [diff] [blame] | 251 | if ((long)dev->blkdev->bd_inode->i_size % erase_size) { |
| 252 | pr_err("erasesize must be a divisor of device size\n"); |
| 253 | goto err_free_block2mtd; |
| 254 | } |
| 255 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 256 | mutex_init(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | /* Setup the MTD structure */ |
| 259 | /* make the name contain the block device in */ |
Julia Lawall | 4d68242 | 2010-03-10 22:15:19 +0100 | [diff] [blame] | 260 | name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname); |
Greg Kroah-Hartman | eadcf0d | 2008-07-02 12:46:22 -0700 | [diff] [blame] | 261 | if (!name) |
Fabian Frederick | 4bed207 | 2014-01-25 10:45:08 +0800 | [diff] [blame] | 262 | goto err_destroy_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Greg Kroah-Hartman | eadcf0d | 2008-07-02 12:46:22 -0700 | [diff] [blame] | 264 | dev->mtd.name = name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK; |
| 267 | dev->mtd.erasesize = erase_size; |
Artem B. Bityutskiy | 17ffc7b | 2006-06-22 18:15:48 +0400 | [diff] [blame] | 268 | dev->mtd.writesize = 1; |
Artem Bityutskiy | b604387 | 2012-02-03 09:32:44 +0200 | [diff] [blame] | 269 | dev->mtd.writebufsize = PAGE_SIZE; |
David Woodhouse | 21c8db9 | 2006-06-14 21:39:48 +0100 | [diff] [blame] | 270 | dev->mtd.type = MTD_RAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | dev->mtd.flags = MTD_CAP_RAM; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 272 | dev->mtd._erase = block2mtd_erase; |
| 273 | dev->mtd._write = block2mtd_write; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 274 | dev->mtd._sync = block2mtd_sync; |
| 275 | dev->mtd._read = block2mtd_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | dev->mtd.priv = dev; |
| 277 | dev->mtd.owner = THIS_MODULE; |
| 278 | |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 279 | if (mtd_device_register(&dev->mtd, NULL, 0)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 280 | /* Device didn't get added, so free the entry */ |
Fabian Frederick | 4bed207 | 2014-01-25 10:45:08 +0800 | [diff] [blame] | 281 | goto err_destroy_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | list_add(&dev->list, &blkmtd_device_list); |
Joe Perches | a1c0660 | 2013-04-19 10:59:35 -0700 | [diff] [blame] | 284 | pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n", |
| 285 | dev->mtd.index, |
| 286 | dev->mtd.name + strlen("block2mtd: "), |
| 287 | dev->mtd.erasesize >> 10, dev->mtd.erasesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return dev; |
| 289 | |
Fabian Frederick | 4bed207 | 2014-01-25 10:45:08 +0800 | [diff] [blame] | 290 | err_destroy_mutex: |
| 291 | mutex_destroy(&dev->write_mutex); |
| 292 | err_free_block2mtd: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | block2mtd_free_device(dev); |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | |
Joern Engel | 954c242 | 2006-04-18 21:03:08 -0700 | [diff] [blame] | 298 | /* This function works similar to reguler strtoul. In addition, it |
| 299 | * allows some suffixes for a more human-readable number format: |
| 300 | * ki, Ki, kiB, KiB - multiply result with 1024 |
| 301 | * Mi, MiB - multiply result with 1024^2 |
| 302 | * Gi, GiB - multiply result with 1024^3 |
| 303 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | static int ustrtoul(const char *cp, char **endp, unsigned int base) |
| 305 | { |
| 306 | unsigned long result = simple_strtoul(cp, endp, base); |
| 307 | switch (**endp) { |
| 308 | case 'G' : |
| 309 | result *= 1024; |
| 310 | case 'M': |
| 311 | result *= 1024; |
Joern Engel | 954c242 | 2006-04-18 21:03:08 -0700 | [diff] [blame] | 312 | case 'K': |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | case 'k': |
| 314 | result *= 1024; |
| 315 | /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */ |
Joern Engel | 954c242 | 2006-04-18 21:03:08 -0700 | [diff] [blame] | 316 | if ((*endp)[1] == 'i') { |
| 317 | if ((*endp)[2] == 'B') |
| 318 | (*endp) += 3; |
| 319 | else |
| 320 | (*endp) += 2; |
| 321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | return result; |
| 324 | } |
| 325 | |
| 326 | |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 327 | static int parse_num(size_t *num, const char *token) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
| 329 | char *endp; |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 330 | size_t n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 332 | n = (size_t) ustrtoul(token, &endp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | if (*endp) |
| 334 | return -EINVAL; |
| 335 | |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 336 | *num = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | static inline void kill_final_newline(char *str) |
| 342 | { |
| 343 | char *newline = strrchr(str, '\n'); |
| 344 | if (newline && !newline[1]) |
| 345 | *newline = 0; |
| 346 | } |
| 347 | |
| 348 | |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 349 | #ifndef MODULE |
| 350 | static int block2mtd_init_called = 0; |
Adrian Bunk | 4839f04 | 2007-05-02 12:33:17 +0100 | [diff] [blame] | 351 | 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] | 352 | #endif |
| 353 | |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 354 | static int block2mtd_setup2(const char *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 356 | char buf[80 + 12]; /* 80 for device, 12 for erase size */ |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 357 | char *str = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | char *token[2]; |
| 359 | char *name; |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 360 | size_t erase_size = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | int i, ret; |
| 362 | |
Joe Perches | a1c0660 | 2013-04-19 10:59:35 -0700 | [diff] [blame] | 363 | if (strnlen(val, sizeof(buf)) >= sizeof(buf)) { |
| 364 | pr_err("parameter too long\n"); |
| 365 | return 0; |
| 366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | strcpy(str, val); |
| 369 | kill_final_newline(str); |
| 370 | |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 371 | for (i = 0; i < 2; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | token[i] = strsep(&str, ","); |
| 373 | |
Joe Perches | a1c0660 | 2013-04-19 10:59:35 -0700 | [diff] [blame] | 374 | if (str) { |
| 375 | pr_err("too many arguments\n"); |
| 376 | return 0; |
| 377 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Joe Perches | a1c0660 | 2013-04-19 10:59:35 -0700 | [diff] [blame] | 379 | if (!token[0]) { |
| 380 | pr_err("no argument\n"); |
| 381 | return 0; |
| 382 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 384 | name = token[0]; |
Joe Perches | a1c0660 | 2013-04-19 10:59:35 -0700 | [diff] [blame] | 385 | if (strlen(name) + 1 > 80) { |
| 386 | pr_err("device name too long\n"); |
| 387 | return 0; |
| 388 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | if (token[1]) { |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 391 | ret = parse_num(&erase_size, token[1]); |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 392 | if (ret) { |
Joe Perches | a1c0660 | 2013-04-19 10:59:35 -0700 | [diff] [blame] | 393 | pr_err("illegal erase size\n"); |
| 394 | return 0; |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | add_device(name, erase_size); |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 404 | static int block2mtd_setup(const char *val, struct kernel_param *kp) |
| 405 | { |
| 406 | #ifdef MODULE |
| 407 | return block2mtd_setup2(val); |
| 408 | #else |
| 409 | /* If more parameters are later passed in via |
| 410 | /sys/module/block2mtd/parameters/block2mtd |
| 411 | and block2mtd_init() has already been called, |
| 412 | we can parse the argument now. */ |
| 413 | |
| 414 | if (block2mtd_init_called) |
| 415 | return block2mtd_setup2(val); |
| 416 | |
| 417 | /* During early boot stage, we only save the parameters |
| 418 | here. We must parse them later: if the param passed |
| 419 | from kernel boot command line, block2mtd_setup() is |
| 420 | called so early that it is not possible to resolve |
| 421 | the device (even kmalloc() fails). Deter that work to |
| 422 | block2mtd_setup2(). */ |
| 423 | |
| 424 | strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline)); |
| 425 | |
| 426 | return 0; |
| 427 | #endif |
| 428 | } |
| 429 | |
| 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200); |
| 432 | MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\""); |
| 433 | |
| 434 | static int __init block2mtd_init(void) |
| 435 | { |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 436 | int ret = 0; |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 437 | |
| 438 | #ifndef MODULE |
| 439 | if (strlen(block2mtd_paramline)) |
| 440 | ret = block2mtd_setup2(block2mtd_paramline); |
| 441 | block2mtd_init_called = 1; |
| 442 | #endif |
| 443 | |
| 444 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 448 | static void block2mtd_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
| 450 | struct list_head *pos, *next; |
| 451 | |
| 452 | /* Remove the MTD devices */ |
| 453 | list_for_each_safe(pos, next, &blkmtd_device_list) { |
| 454 | struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list); |
| 455 | block2mtd_sync(&dev->mtd); |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 456 | mtd_device_unregister(&dev->mtd); |
Fabian Frederick | 4bed207 | 2014-01-25 10:45:08 +0800 | [diff] [blame] | 457 | mutex_destroy(&dev->write_mutex); |
Joe Perches | a1c0660 | 2013-04-19 10:59:35 -0700 | [diff] [blame] | 458 | pr_info("mtd%d: [%s] removed\n", |
| 459 | dev->mtd.index, |
| 460 | dev->mtd.name + strlen("block2mtd: ")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | list_del(&dev->list); |
| 462 | block2mtd_free_device(dev); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | |
| 467 | module_init(block2mtd_init); |
| 468 | module_exit(block2mtd_exit); |
| 469 | |
| 470 | MODULE_LICENSE("GPL"); |
Joern Engel | 2b54aae | 2008-02-06 01:38:02 -0800 | [diff] [blame] | 471 | MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | MODULE_DESCRIPTION("Emulate an MTD using a block device"); |