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