11da177e4SLinus Torvalds /* 2*711c11b7SJoern Engel * $Id: block2mtd.c,v 1.25 2005/03/07 20:29:05 joern Exp $ 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * block2mtd.c - create an mtd from a block device 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk> 71da177e4SLinus Torvalds * Copyright (C) 2004,2005 J�rn Engel <joern@wh.fh-wedel.de> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Licence: GPL 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds #include <linux/config.h> 121da177e4SLinus Torvalds #include <linux/module.h> 131da177e4SLinus Torvalds #include <linux/fs.h> 141da177e4SLinus Torvalds #include <linux/blkdev.h> 151da177e4SLinus Torvalds #include <linux/bio.h> 161da177e4SLinus Torvalds #include <linux/pagemap.h> 171da177e4SLinus Torvalds #include <linux/list.h> 181da177e4SLinus Torvalds #include <linux/init.h> 191da177e4SLinus Torvalds #include <linux/mtd/mtd.h> 201da177e4SLinus Torvalds #include <linux/buffer_head.h> 211da177e4SLinus Torvalds 22*711c11b7SJoern Engel #define VERSION "$Revision: 1.24 $" 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args) 261da177e4SLinus Torvalds #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args) 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds /* Info for the block device */ 301da177e4SLinus Torvalds struct block2mtd_dev { 311da177e4SLinus Torvalds struct list_head list; 321da177e4SLinus Torvalds struct block_device *blkdev; 331da177e4SLinus Torvalds struct mtd_info mtd; 341da177e4SLinus Torvalds struct semaphore write_mutex; 351da177e4SLinus Torvalds }; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds /* Static info about the MTD, used in cleanup_module */ 391da177e4SLinus Torvalds static LIST_HEAD(blkmtd_device_list); 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #define PAGE_READAHEAD 64 431da177e4SLinus Torvalds void cache_readahead(struct address_space *mapping, int index) 441da177e4SLinus Torvalds { 451da177e4SLinus Torvalds filler_t *filler = (filler_t*)mapping->a_ops->readpage; 461da177e4SLinus Torvalds int i, pagei; 471da177e4SLinus Torvalds unsigned ret = 0; 481da177e4SLinus Torvalds unsigned long end_index; 491da177e4SLinus Torvalds struct page *page; 501da177e4SLinus Torvalds LIST_HEAD(page_pool); 511da177e4SLinus Torvalds struct inode *inode = mapping->host; 521da177e4SLinus Torvalds loff_t isize = i_size_read(inode); 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds if (!isize) { 551da177e4SLinus Torvalds INFO("iSize=0 in cache_readahead\n"); 561da177e4SLinus Torvalds return; 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds end_index = ((isize - 1) >> PAGE_CACHE_SHIFT); 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds read_lock_irq(&mapping->tree_lock); 621da177e4SLinus Torvalds for (i = 0; i < PAGE_READAHEAD; i++) { 631da177e4SLinus Torvalds pagei = index + i; 641da177e4SLinus Torvalds if (pagei > end_index) { 651da177e4SLinus Torvalds INFO("Overrun end of disk in cache readahead\n"); 661da177e4SLinus Torvalds break; 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds page = radix_tree_lookup(&mapping->page_tree, pagei); 691da177e4SLinus Torvalds if (page && (!i)) 701da177e4SLinus Torvalds break; 711da177e4SLinus Torvalds if (page) 721da177e4SLinus Torvalds continue; 731da177e4SLinus Torvalds read_unlock_irq(&mapping->tree_lock); 741da177e4SLinus Torvalds page = page_cache_alloc_cold(mapping); 751da177e4SLinus Torvalds read_lock_irq(&mapping->tree_lock); 761da177e4SLinus Torvalds if (!page) 771da177e4SLinus Torvalds break; 781da177e4SLinus Torvalds page->index = pagei; 791da177e4SLinus Torvalds list_add(&page->lru, &page_pool); 801da177e4SLinus Torvalds ret++; 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds read_unlock_irq(&mapping->tree_lock); 831da177e4SLinus Torvalds if (ret) 841da177e4SLinus Torvalds read_cache_pages(mapping, &page_pool, filler, NULL); 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds static struct page* page_readahead(struct address_space *mapping, int index) 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds filler_t *filler = (filler_t*)mapping->a_ops->readpage; 911da177e4SLinus Torvalds //do_page_cache_readahead(mapping, index, XXX, 64); 921da177e4SLinus Torvalds cache_readahead(mapping, index); 931da177e4SLinus Torvalds return read_cache_page(mapping, index, filler, NULL); 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* erase a specified part of the device */ 981da177e4SLinus Torvalds static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len) 991da177e4SLinus Torvalds { 1001da177e4SLinus Torvalds struct address_space *mapping = dev->blkdev->bd_inode->i_mapping; 1011da177e4SLinus Torvalds struct page *page; 1021da177e4SLinus Torvalds int index = to >> PAGE_SHIFT; // page index 1031da177e4SLinus Torvalds int pages = len >> PAGE_SHIFT; 1041da177e4SLinus Torvalds u_long *p; 1051da177e4SLinus Torvalds u_long *max; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds while (pages) { 1081da177e4SLinus Torvalds page = page_readahead(mapping, index); 1091da177e4SLinus Torvalds if (!page) 1101da177e4SLinus Torvalds return -ENOMEM; 1111da177e4SLinus Torvalds if (IS_ERR(page)) 1121da177e4SLinus Torvalds return PTR_ERR(page); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds max = (u_long*)page_address(page) + PAGE_SIZE; 1151da177e4SLinus Torvalds for (p=(u_long*)page_address(page); p<max; p++) 1161da177e4SLinus Torvalds if (*p != -1UL) { 1171da177e4SLinus Torvalds lock_page(page); 1181da177e4SLinus Torvalds memset(page_address(page), 0xff, PAGE_SIZE); 1191da177e4SLinus Torvalds set_page_dirty(page); 1201da177e4SLinus Torvalds unlock_page(page); 1211da177e4SLinus Torvalds break; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds page_cache_release(page); 1251da177e4SLinus Torvalds pages--; 1261da177e4SLinus Torvalds index++; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds return 0; 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds struct block2mtd_dev *dev = mtd->priv; 1331da177e4SLinus Torvalds size_t from = instr->addr; 1341da177e4SLinus Torvalds size_t len = instr->len; 1351da177e4SLinus Torvalds int err; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds instr->state = MTD_ERASING; 1381da177e4SLinus Torvalds down(&dev->write_mutex); 1391da177e4SLinus Torvalds err = _block2mtd_erase(dev, from, len); 1401da177e4SLinus Torvalds up(&dev->write_mutex); 1411da177e4SLinus Torvalds if (err) { 1421da177e4SLinus Torvalds ERROR("erase failed err = %d", err); 1431da177e4SLinus Torvalds instr->state = MTD_ERASE_FAILED; 1441da177e4SLinus Torvalds } else 1451da177e4SLinus Torvalds instr->state = MTD_ERASE_DONE; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds instr->state = MTD_ERASE_DONE; 1481da177e4SLinus Torvalds mtd_erase_callback(instr); 1491da177e4SLinus Torvalds return err; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len, 1541da177e4SLinus Torvalds size_t *retlen, u_char *buf) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds struct block2mtd_dev *dev = mtd->priv; 1571da177e4SLinus Torvalds struct page *page; 1581da177e4SLinus Torvalds int index = from >> PAGE_SHIFT; 159*711c11b7SJoern Engel int offset = from & (PAGE_SIZE-1); 1601da177e4SLinus Torvalds int cpylen; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds if (from > mtd->size) 1631da177e4SLinus Torvalds return -EINVAL; 1641da177e4SLinus Torvalds if (from + len > mtd->size) 1651da177e4SLinus Torvalds len = mtd->size - from; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds if (retlen) 1681da177e4SLinus Torvalds *retlen = 0; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds while (len) { 1711da177e4SLinus Torvalds if ((offset + len) > PAGE_SIZE) 1721da177e4SLinus Torvalds cpylen = PAGE_SIZE - offset; // multiple pages 1731da177e4SLinus Torvalds else 1741da177e4SLinus Torvalds cpylen = len; // this page 1751da177e4SLinus Torvalds len = len - cpylen; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds // Get page 1781da177e4SLinus Torvalds page = page_readahead(dev->blkdev->bd_inode->i_mapping, index); 1791da177e4SLinus Torvalds if (!page) 1801da177e4SLinus Torvalds return -ENOMEM; 1811da177e4SLinus Torvalds if (IS_ERR(page)) 1821da177e4SLinus Torvalds return PTR_ERR(page); 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds memcpy(buf, page_address(page) + offset, cpylen); 1851da177e4SLinus Torvalds page_cache_release(page); 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds if (retlen) 1881da177e4SLinus Torvalds *retlen += cpylen; 1891da177e4SLinus Torvalds buf += cpylen; 1901da177e4SLinus Torvalds offset = 0; 1911da177e4SLinus Torvalds index++; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds return 0; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds /* write data to the underlying device */ 1981da177e4SLinus Torvalds static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf, 1991da177e4SLinus Torvalds loff_t to, size_t len, size_t *retlen) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds struct page *page; 2021da177e4SLinus Torvalds struct address_space *mapping = dev->blkdev->bd_inode->i_mapping; 2031da177e4SLinus Torvalds int index = to >> PAGE_SHIFT; // page index 2041da177e4SLinus Torvalds int offset = to & ~PAGE_MASK; // page offset 2051da177e4SLinus Torvalds int cpylen; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds if (retlen) 2081da177e4SLinus Torvalds *retlen = 0; 2091da177e4SLinus Torvalds while (len) { 2101da177e4SLinus Torvalds if ((offset+len) > PAGE_SIZE) 2111da177e4SLinus Torvalds cpylen = PAGE_SIZE - offset; // multiple pages 2121da177e4SLinus Torvalds else 2131da177e4SLinus Torvalds cpylen = len; // this page 2141da177e4SLinus Torvalds len = len - cpylen; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds // Get page 2171da177e4SLinus Torvalds page = page_readahead(mapping, index); 2181da177e4SLinus Torvalds if (!page) 2191da177e4SLinus Torvalds return -ENOMEM; 2201da177e4SLinus Torvalds if (IS_ERR(page)) 2211da177e4SLinus Torvalds return PTR_ERR(page); 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds if (memcmp(page_address(page)+offset, buf, cpylen)) { 2241da177e4SLinus Torvalds lock_page(page); 2251da177e4SLinus Torvalds memcpy(page_address(page) + offset, buf, cpylen); 2261da177e4SLinus Torvalds set_page_dirty(page); 2271da177e4SLinus Torvalds unlock_page(page); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds page_cache_release(page); 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds if (retlen) 2321da177e4SLinus Torvalds *retlen += cpylen; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds buf += cpylen; 2351da177e4SLinus Torvalds offset = 0; 2361da177e4SLinus Torvalds index++; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds return 0; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len, 2411da177e4SLinus Torvalds size_t *retlen, const u_char *buf) 2421da177e4SLinus Torvalds { 2431da177e4SLinus Torvalds struct block2mtd_dev *dev = mtd->priv; 2441da177e4SLinus Torvalds int err; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds if (!len) 2471da177e4SLinus Torvalds return 0; 2481da177e4SLinus Torvalds if (to >= mtd->size) 2491da177e4SLinus Torvalds return -ENOSPC; 2501da177e4SLinus Torvalds if (to + len > mtd->size) 2511da177e4SLinus Torvalds len = mtd->size - to; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds down(&dev->write_mutex); 2541da177e4SLinus Torvalds err = _block2mtd_write(dev, buf, to, len, retlen); 2551da177e4SLinus Torvalds up(&dev->write_mutex); 2561da177e4SLinus Torvalds if (err > 0) 2571da177e4SLinus Torvalds err = 0; 2581da177e4SLinus Torvalds return err; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds /* sync the device - wait until the write queue is empty */ 2631da177e4SLinus Torvalds static void block2mtd_sync(struct mtd_info *mtd) 2641da177e4SLinus Torvalds { 2651da177e4SLinus Torvalds struct block2mtd_dev *dev = mtd->priv; 2661da177e4SLinus Torvalds sync_blockdev(dev->blkdev); 2671da177e4SLinus Torvalds return; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds static void block2mtd_free_device(struct block2mtd_dev *dev) 2721da177e4SLinus Torvalds { 2731da177e4SLinus Torvalds if (!dev) 2741da177e4SLinus Torvalds return; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds kfree(dev->mtd.name); 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds if (dev->blkdev) { 2791da177e4SLinus Torvalds invalidate_inode_pages(dev->blkdev->bd_inode->i_mapping); 2801da177e4SLinus Torvalds close_bdev_excl(dev->blkdev); 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds kfree(dev); 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds /* FIXME: ensure that mtd->size % erase_size == 0 */ 2881da177e4SLinus Torvalds static struct block2mtd_dev *add_device(char *devname, int erase_size) 2891da177e4SLinus Torvalds { 2901da177e4SLinus Torvalds struct block_device *bdev; 2911da177e4SLinus Torvalds struct block2mtd_dev *dev; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds if (!devname) 2941da177e4SLinus Torvalds return NULL; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds dev = kmalloc(sizeof(struct block2mtd_dev), GFP_KERNEL); 2971da177e4SLinus Torvalds if (!dev) 2981da177e4SLinus Torvalds return NULL; 2991da177e4SLinus Torvalds memset(dev, 0, sizeof(*dev)); 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds /* Get a handle on the device */ 3021da177e4SLinus Torvalds bdev = open_bdev_excl(devname, O_RDWR, NULL); 3031da177e4SLinus Torvalds if (IS_ERR(bdev)) { 3041da177e4SLinus Torvalds ERROR("error: cannot open device %s", devname); 3051da177e4SLinus Torvalds goto devinit_err; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds dev->blkdev = bdev; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) { 3101da177e4SLinus Torvalds ERROR("attempting to use an MTD device as a block device"); 3111da177e4SLinus Torvalds goto devinit_err; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds init_MUTEX(&dev->write_mutex); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds /* Setup the MTD structure */ 3171da177e4SLinus Torvalds /* make the name contain the block device in */ 3181da177e4SLinus Torvalds dev->mtd.name = kmalloc(sizeof("block2mtd: ") + strlen(devname), 3191da177e4SLinus Torvalds GFP_KERNEL); 3201da177e4SLinus Torvalds if (!dev->mtd.name) 3211da177e4SLinus Torvalds goto devinit_err; 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds sprintf(dev->mtd.name, "block2mtd: %s", devname); 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK; 3261da177e4SLinus Torvalds dev->mtd.erasesize = erase_size; 3271da177e4SLinus Torvalds dev->mtd.type = MTD_RAM; 3281da177e4SLinus Torvalds dev->mtd.flags = MTD_CAP_RAM; 3291da177e4SLinus Torvalds dev->mtd.erase = block2mtd_erase; 3301da177e4SLinus Torvalds dev->mtd.write = block2mtd_write; 3311da177e4SLinus Torvalds dev->mtd.writev = default_mtd_writev; 3321da177e4SLinus Torvalds dev->mtd.sync = block2mtd_sync; 3331da177e4SLinus Torvalds dev->mtd.read = block2mtd_read; 3341da177e4SLinus Torvalds dev->mtd.readv = default_mtd_readv; 3351da177e4SLinus Torvalds dev->mtd.priv = dev; 3361da177e4SLinus Torvalds dev->mtd.owner = THIS_MODULE; 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds if (add_mtd_device(&dev->mtd)) { 3391da177e4SLinus Torvalds /* Device didnt get added, so free the entry */ 3401da177e4SLinus Torvalds goto devinit_err; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds list_add(&dev->list, &blkmtd_device_list); 3431da177e4SLinus Torvalds INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index, 3441da177e4SLinus Torvalds dev->mtd.name + strlen("blkmtd: "), 3451da177e4SLinus Torvalds dev->mtd.erasesize >> 10, dev->mtd.erasesize); 3461da177e4SLinus Torvalds return dev; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds devinit_err: 3491da177e4SLinus Torvalds block2mtd_free_device(dev); 3501da177e4SLinus Torvalds return NULL; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds static int ustrtoul(const char *cp, char **endp, unsigned int base) 3551da177e4SLinus Torvalds { 3561da177e4SLinus Torvalds unsigned long result = simple_strtoul(cp, endp, base); 3571da177e4SLinus Torvalds switch (**endp) { 3581da177e4SLinus Torvalds case 'G' : 3591da177e4SLinus Torvalds result *= 1024; 3601da177e4SLinus Torvalds case 'M': 3611da177e4SLinus Torvalds result *= 1024; 3621da177e4SLinus Torvalds case 'k': 3631da177e4SLinus Torvalds result *= 1024; 3641da177e4SLinus Torvalds /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */ 3651da177e4SLinus Torvalds if ((*endp)[1] == 'i') 3661da177e4SLinus Torvalds (*endp) += 2; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds return result; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds static int parse_num32(u32 *num32, const char *token) 3731da177e4SLinus Torvalds { 3741da177e4SLinus Torvalds char *endp; 3751da177e4SLinus Torvalds unsigned long n; 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds n = ustrtoul(token, &endp, 0); 3781da177e4SLinus Torvalds if (*endp) 3791da177e4SLinus Torvalds return -EINVAL; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds *num32 = n; 3821da177e4SLinus Torvalds return 0; 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds static int parse_name(char **pname, const char *token, size_t limit) 3871da177e4SLinus Torvalds { 3881da177e4SLinus Torvalds size_t len; 3891da177e4SLinus Torvalds char *name; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds len = strlen(token) + 1; 3921da177e4SLinus Torvalds if (len > limit) 3931da177e4SLinus Torvalds return -ENOSPC; 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds name = kmalloc(len, GFP_KERNEL); 3961da177e4SLinus Torvalds if (!name) 3971da177e4SLinus Torvalds return -ENOMEM; 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds strcpy(name, token); 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds *pname = name; 4021da177e4SLinus Torvalds return 0; 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds static inline void kill_final_newline(char *str) 4071da177e4SLinus Torvalds { 4081da177e4SLinus Torvalds char *newline = strrchr(str, '\n'); 4091da177e4SLinus Torvalds if (newline && !newline[1]) 4101da177e4SLinus Torvalds *newline = 0; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds #define parse_err(fmt, args...) do { \ 4151da177e4SLinus Torvalds ERROR("block2mtd: " fmt "\n", ## args); \ 4161da177e4SLinus Torvalds return 0; \ 4171da177e4SLinus Torvalds } while (0) 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds static int block2mtd_setup(const char *val, struct kernel_param *kp) 4201da177e4SLinus Torvalds { 4211da177e4SLinus Torvalds char buf[80+12], *str=buf; /* 80 for device, 12 for erase size */ 4221da177e4SLinus Torvalds char *token[2]; 4231da177e4SLinus Torvalds char *name; 4241da177e4SLinus Torvalds u32 erase_size = PAGE_SIZE; 4251da177e4SLinus Torvalds int i, ret; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds if (strnlen(val, sizeof(buf)) >= sizeof(buf)) 4281da177e4SLinus Torvalds parse_err("parameter too long"); 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds strcpy(str, val); 4311da177e4SLinus Torvalds kill_final_newline(str); 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds for (i=0; i<2; i++) 4341da177e4SLinus Torvalds token[i] = strsep(&str, ","); 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds if (str) 4371da177e4SLinus Torvalds parse_err("too many arguments"); 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds if (!token[0]) 4401da177e4SLinus Torvalds parse_err("no argument"); 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds ret = parse_name(&name, token[0], 80); 4431da177e4SLinus Torvalds if (ret == -ENOMEM) 4441da177e4SLinus Torvalds parse_err("out of memory"); 4451da177e4SLinus Torvalds if (ret == -ENOSPC) 4461da177e4SLinus Torvalds parse_err("name too long"); 4471da177e4SLinus Torvalds if (ret) 4481da177e4SLinus Torvalds return 0; 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds if (token[1]) { 4511da177e4SLinus Torvalds ret = parse_num32(&erase_size, token[1]); 4521da177e4SLinus Torvalds if (ret) 4531da177e4SLinus Torvalds parse_err("illegal erase size"); 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds add_device(name, erase_size); 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds return 0; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200); 4631da177e4SLinus Torvalds MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\""); 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds static int __init block2mtd_init(void) 4661da177e4SLinus Torvalds { 4671da177e4SLinus Torvalds INFO("version " VERSION); 4681da177e4SLinus Torvalds return 0; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds static void __devexit block2mtd_exit(void) 4731da177e4SLinus Torvalds { 4741da177e4SLinus Torvalds struct list_head *pos, *next; 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds /* Remove the MTD devices */ 4771da177e4SLinus Torvalds list_for_each_safe(pos, next, &blkmtd_device_list) { 4781da177e4SLinus Torvalds struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list); 4791da177e4SLinus Torvalds block2mtd_sync(&dev->mtd); 4801da177e4SLinus Torvalds del_mtd_device(&dev->mtd); 4811da177e4SLinus Torvalds INFO("mtd%d: [%s] removed", dev->mtd.index, 4821da177e4SLinus Torvalds dev->mtd.name + strlen("blkmtd: ")); 4831da177e4SLinus Torvalds list_del(&dev->list); 4841da177e4SLinus Torvalds block2mtd_free_device(dev); 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds module_init(block2mtd_init); 4901da177e4SLinus Torvalds module_exit(block2mtd_exit); 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 4931da177e4SLinus Torvalds MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others"); 4941da177e4SLinus Torvalds MODULE_DESCRIPTION("Emulate an MTD using a block device"); 495