1 /* 2 * block2mtd.c - create an mtd from a block device 3 * 4 * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk> 5 * Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de> 6 * 7 * Licence: GPL 8 */ 9 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 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> 20 #include <linux/mutex.h> 21 #include <linux/mount.h> 22 #include <linux/slab.h> 23 #include <linux/major.h> 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; 30 struct mutex write_mutex; 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 balance_dirty_pages_ratelimited(mapping); 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; 83 mutex_lock(&dev->write_mutex); 84 err = _block2mtd_erase(dev, from, len); 85 mutex_unlock(&dev->write_mutex); 86 if (err) { 87 pr_err("erase failed err = %d\n", err); 88 instr->state = MTD_ERASE_FAILED; 89 } else 90 instr->state = MTD_ERASE_DONE; 91 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; 103 int offset = from & (PAGE_SIZE-1); 104 int cpylen; 105 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 113 page = page_read(dev->blkdev->bd_inode->i_mapping, index); 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 140 while (len) { 141 if ((offset+len) > PAGE_SIZE) 142 cpylen = PAGE_SIZE - offset; // multiple pages 143 else 144 cpylen = len; // this page 145 len = len - cpylen; 146 147 page = page_read(mapping, index); 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); 156 balance_dirty_pages_ratelimited(mapping); 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 } 169 170 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 177 mutex_lock(&dev->write_mutex); 178 err = _block2mtd_write(dev, buf, to, len, retlen); 179 mutex_unlock(&dev->write_mutex); 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) { 203 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 204 0, -1); 205 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 206 } 207 208 kfree(dev); 209 } 210 211 212 /* FIXME: ensure that mtd->size % erase_size == 0 */ 213 static struct block2mtd_dev *add_device(char *devname, int erase_size) 214 { 215 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL; 216 struct block_device *bdev; 217 struct block2mtd_dev *dev; 218 char *name; 219 220 if (!devname) 221 return NULL; 222 223 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL); 224 if (!dev) 225 return NULL; 226 227 /* Get a handle on the device */ 228 bdev = blkdev_get_by_path(devname, mode, dev); 229 #ifndef MODULE 230 if (IS_ERR(bdev)) { 231 232 /* We might not have rootfs mounted at this point. Try 233 to resolve the device name by other means. */ 234 235 dev_t devt = name_to_dev_t(devname); 236 if (devt) 237 bdev = blkdev_get_by_dev(devt, mode, dev); 238 } 239 #endif 240 241 if (IS_ERR(bdev)) { 242 pr_err("error: cannot open device %s\n", devname); 243 goto devinit_err; 244 } 245 dev->blkdev = bdev; 246 247 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) { 248 pr_err("attempting to use an MTD device as a block device\n"); 249 goto devinit_err; 250 } 251 252 mutex_init(&dev->write_mutex); 253 254 /* Setup the MTD structure */ 255 /* make the name contain the block device in */ 256 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname); 257 if (!name) 258 goto devinit_err; 259 260 dev->mtd.name = name; 261 262 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK; 263 dev->mtd.erasesize = erase_size; 264 dev->mtd.writesize = 1; 265 dev->mtd.writebufsize = PAGE_SIZE; 266 dev->mtd.type = MTD_RAM; 267 dev->mtd.flags = MTD_CAP_RAM; 268 dev->mtd._erase = block2mtd_erase; 269 dev->mtd._write = block2mtd_write; 270 dev->mtd._sync = block2mtd_sync; 271 dev->mtd._read = block2mtd_read; 272 dev->mtd.priv = dev; 273 dev->mtd.owner = THIS_MODULE; 274 275 if (mtd_device_register(&dev->mtd, NULL, 0)) { 276 /* Device didn't get added, so free the entry */ 277 goto devinit_err; 278 } 279 list_add(&dev->list, &blkmtd_device_list); 280 pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n", 281 dev->mtd.index, 282 dev->mtd.name + strlen("block2mtd: "), 283 dev->mtd.erasesize >> 10, dev->mtd.erasesize); 284 return dev; 285 286 devinit_err: 287 block2mtd_free_device(dev); 288 return NULL; 289 } 290 291 292 /* This function works similar to reguler strtoul. In addition, it 293 * allows some suffixes for a more human-readable number format: 294 * ki, Ki, kiB, KiB - multiply result with 1024 295 * Mi, MiB - multiply result with 1024^2 296 * Gi, GiB - multiply result with 1024^3 297 */ 298 static int ustrtoul(const char *cp, char **endp, unsigned int base) 299 { 300 unsigned long result = simple_strtoul(cp, endp, base); 301 switch (**endp) { 302 case 'G' : 303 result *= 1024; 304 case 'M': 305 result *= 1024; 306 case 'K': 307 case 'k': 308 result *= 1024; 309 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */ 310 if ((*endp)[1] == 'i') { 311 if ((*endp)[2] == 'B') 312 (*endp) += 3; 313 else 314 (*endp) += 2; 315 } 316 } 317 return result; 318 } 319 320 321 static int parse_num(size_t *num, const char *token) 322 { 323 char *endp; 324 size_t n; 325 326 n = (size_t) ustrtoul(token, &endp, 0); 327 if (*endp) 328 return -EINVAL; 329 330 *num = n; 331 return 0; 332 } 333 334 335 static inline void kill_final_newline(char *str) 336 { 337 char *newline = strrchr(str, '\n'); 338 if (newline && !newline[1]) 339 *newline = 0; 340 } 341 342 343 #ifndef MODULE 344 static int block2mtd_init_called = 0; 345 static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */ 346 #endif 347 348 static int block2mtd_setup2(const char *val) 349 { 350 char buf[80 + 12]; /* 80 for device, 12 for erase size */ 351 char *str = buf; 352 char *token[2]; 353 char *name; 354 size_t erase_size = PAGE_SIZE; 355 int i, ret; 356 357 if (strnlen(val, sizeof(buf)) >= sizeof(buf)) { 358 pr_err("parameter too long\n"); 359 return 0; 360 } 361 362 strcpy(str, val); 363 kill_final_newline(str); 364 365 for (i = 0; i < 2; i++) 366 token[i] = strsep(&str, ","); 367 368 if (str) { 369 pr_err("too many arguments\n"); 370 return 0; 371 } 372 373 if (!token[0]) { 374 pr_err("no argument\n"); 375 return 0; 376 } 377 378 name = token[0]; 379 if (strlen(name) + 1 > 80) { 380 pr_err("device name too long\n"); 381 return 0; 382 } 383 384 if (token[1]) { 385 ret = parse_num(&erase_size, token[1]); 386 if (ret) { 387 pr_err("illegal erase size\n"); 388 return 0; 389 } 390 } 391 392 add_device(name, erase_size); 393 394 return 0; 395 } 396 397 398 static int block2mtd_setup(const char *val, struct kernel_param *kp) 399 { 400 #ifdef MODULE 401 return block2mtd_setup2(val); 402 #else 403 /* If more parameters are later passed in via 404 /sys/module/block2mtd/parameters/block2mtd 405 and block2mtd_init() has already been called, 406 we can parse the argument now. */ 407 408 if (block2mtd_init_called) 409 return block2mtd_setup2(val); 410 411 /* During early boot stage, we only save the parameters 412 here. We must parse them later: if the param passed 413 from kernel boot command line, block2mtd_setup() is 414 called so early that it is not possible to resolve 415 the device (even kmalloc() fails). Deter that work to 416 block2mtd_setup2(). */ 417 418 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline)); 419 420 return 0; 421 #endif 422 } 423 424 425 module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200); 426 MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\""); 427 428 static int __init block2mtd_init(void) 429 { 430 int ret = 0; 431 432 #ifndef MODULE 433 if (strlen(block2mtd_paramline)) 434 ret = block2mtd_setup2(block2mtd_paramline); 435 block2mtd_init_called = 1; 436 #endif 437 438 return ret; 439 } 440 441 442 static void block2mtd_exit(void) 443 { 444 struct list_head *pos, *next; 445 446 /* Remove the MTD devices */ 447 list_for_each_safe(pos, next, &blkmtd_device_list) { 448 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list); 449 block2mtd_sync(&dev->mtd); 450 mtd_device_unregister(&dev->mtd); 451 pr_info("mtd%d: [%s] removed\n", 452 dev->mtd.index, 453 dev->mtd.name + strlen("block2mtd: ")); 454 list_del(&dev->list); 455 block2mtd_free_device(dev); 456 } 457 } 458 459 460 module_init(block2mtd_init); 461 module_exit(block2mtd_exit); 462 463 MODULE_LICENSE("GPL"); 464 MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>"); 465 MODULE_DESCRIPTION("Emulate an MTD using a block device"); 466