xref: /linux/drivers/mtd/devices/block2mtd.c (revision a6550e57f9d074511cf420bdb802ab5e56edc3bb)
11da177e4SLinus Torvalds /*
22b9175c1SAdrian Bunk  * $Id: block2mtd.c,v 1.30 2005/11/29 14:48:32 gleixner 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>
7954c2422SJoern Engel  * Copyright (C) 2004-2006	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>
2148b19268SIngo Molnar #include <linux/mutex.h>
221da177e4SLinus Torvalds 
232b9175c1SAdrian Bunk #define VERSION "$Revision: 1.30 $"
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
271da177e4SLinus Torvalds #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /* Info for the block device */
311da177e4SLinus Torvalds struct block2mtd_dev {
321da177e4SLinus Torvalds 	struct list_head list;
331da177e4SLinus Torvalds 	struct block_device *blkdev;
341da177e4SLinus Torvalds 	struct mtd_info mtd;
3548b19268SIngo Molnar 	struct mutex write_mutex;
361da177e4SLinus Torvalds };
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /* Static info about the MTD, used in cleanup_module */
401da177e4SLinus Torvalds static LIST_HEAD(blkmtd_device_list);
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #define PAGE_READAHEAD 64
442b9175c1SAdrian Bunk static void cache_readahead(struct address_space *mapping, int index)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	filler_t *filler = (filler_t*)mapping->a_ops->readpage;
471da177e4SLinus Torvalds 	int i, pagei;
481da177e4SLinus Torvalds 	unsigned ret = 0;
491da177e4SLinus Torvalds 	unsigned long end_index;
501da177e4SLinus Torvalds 	struct page *page;
511da177e4SLinus Torvalds 	LIST_HEAD(page_pool);
521da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
531da177e4SLinus Torvalds 	loff_t isize = i_size_read(inode);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	if (!isize) {
561da177e4SLinus Torvalds 		INFO("iSize=0 in cache_readahead\n");
571da177e4SLinus Torvalds 		return;
581da177e4SLinus Torvalds 	}
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	read_lock_irq(&mapping->tree_lock);
631da177e4SLinus Torvalds 	for (i = 0; i < PAGE_READAHEAD; i++) {
641da177e4SLinus Torvalds 		pagei = index + i;
651da177e4SLinus Torvalds 		if (pagei > end_index) {
661da177e4SLinus Torvalds 			INFO("Overrun end of disk in cache readahead\n");
671da177e4SLinus Torvalds 			break;
681da177e4SLinus Torvalds 		}
691da177e4SLinus Torvalds 		page = radix_tree_lookup(&mapping->page_tree, pagei);
701da177e4SLinus Torvalds 		if (page && (!i))
711da177e4SLinus Torvalds 			break;
721da177e4SLinus Torvalds 		if (page)
731da177e4SLinus Torvalds 			continue;
741da177e4SLinus Torvalds 		read_unlock_irq(&mapping->tree_lock);
751da177e4SLinus Torvalds 		page = page_cache_alloc_cold(mapping);
761da177e4SLinus Torvalds 		read_lock_irq(&mapping->tree_lock);
771da177e4SLinus Torvalds 		if (!page)
781da177e4SLinus Torvalds 			break;
791da177e4SLinus Torvalds 		page->index = pagei;
801da177e4SLinus Torvalds 		list_add(&page->lru, &page_pool);
811da177e4SLinus Torvalds 		ret++;
821da177e4SLinus Torvalds 	}
831da177e4SLinus Torvalds 	read_unlock_irq(&mapping->tree_lock);
841da177e4SLinus Torvalds 	if (ret)
851da177e4SLinus Torvalds 		read_cache_pages(mapping, &page_pool, filler, NULL);
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds static struct page* page_readahead(struct address_space *mapping, int index)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	filler_t *filler = (filler_t*)mapping->a_ops->readpage;
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;
13848b19268SIngo Molnar 	mutex_lock(&dev->write_mutex);
1391da177e4SLinus Torvalds 	err = _block2mtd_erase(dev, from, len);
14048b19268SIngo Molnar 	mutex_unlock(&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;
159711c11b7SJoern 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 
25348b19268SIngo Molnar 	mutex_lock(&dev->write_mutex);
2541da177e4SLinus Torvalds 	err = _block2mtd_write(dev, buf, to, len, retlen);
25548b19268SIngo Molnar 	mutex_unlock(&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 
31448b19268SIngo Molnar 	mutex_init(&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 
354954c2422SJoern Engel /* This function works similar to reguler strtoul.  In addition, it
355954c2422SJoern Engel  * allows some suffixes for a more human-readable number format:
356954c2422SJoern Engel  * ki, Ki, kiB, KiB	- multiply result with 1024
357954c2422SJoern Engel  * Mi, MiB		- multiply result with 1024^2
358954c2422SJoern Engel  * Gi, GiB		- multiply result with 1024^3
359954c2422SJoern Engel  */
3601da177e4SLinus Torvalds static int ustrtoul(const char *cp, char **endp, unsigned int base)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	unsigned long result = simple_strtoul(cp, endp, base);
3631da177e4SLinus Torvalds 	switch (**endp) {
3641da177e4SLinus Torvalds 	case 'G' :
3651da177e4SLinus Torvalds 		result *= 1024;
3661da177e4SLinus Torvalds 	case 'M':
3671da177e4SLinus Torvalds 		result *= 1024;
368954c2422SJoern Engel 	case 'K':
3691da177e4SLinus Torvalds 	case 'k':
3701da177e4SLinus Torvalds 		result *= 1024;
3711da177e4SLinus Torvalds 	/* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
372954c2422SJoern Engel 		if ((*endp)[1] == 'i') {
373954c2422SJoern Engel 			if ((*endp)[2] == 'B')
374954c2422SJoern Engel 				(*endp) += 3;
375954c2422SJoern Engel 			else
3761da177e4SLinus Torvalds 				(*endp) += 2;
3771da177e4SLinus Torvalds 		}
378954c2422SJoern Engel 	}
3791da177e4SLinus Torvalds 	return result;
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 
383cc71229fSThomas Gleixner static int parse_num(size_t *num, const char *token)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	char *endp;
386cc71229fSThomas Gleixner 	size_t n;
3871da177e4SLinus Torvalds 
388cc71229fSThomas Gleixner 	n = (size_t) ustrtoul(token, &endp, 0);
3891da177e4SLinus Torvalds 	if (*endp)
3901da177e4SLinus Torvalds 		return -EINVAL;
3911da177e4SLinus Torvalds 
392cc71229fSThomas Gleixner 	*num = n;
3931da177e4SLinus Torvalds 	return 0;
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds static int parse_name(char **pname, const char *token, size_t limit)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	size_t len;
4001da177e4SLinus Torvalds 	char *name;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	len = strlen(token) + 1;
4031da177e4SLinus Torvalds 	if (len > limit)
4041da177e4SLinus Torvalds 		return -ENOSPC;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	name = kmalloc(len, GFP_KERNEL);
4071da177e4SLinus Torvalds 	if (!name)
4081da177e4SLinus Torvalds 		return -ENOMEM;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	strcpy(name, token);
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	*pname = name;
4131da177e4SLinus Torvalds 	return 0;
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds static inline void kill_final_newline(char *str)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	char *newline = strrchr(str, '\n');
4201da177e4SLinus Torvalds 	if (newline && !newline[1])
4211da177e4SLinus Torvalds 		*newline = 0;
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds #define parse_err(fmt, args...) do {		\
4261da177e4SLinus Torvalds 	ERROR("block2mtd: " fmt "\n", ## args);	\
4271da177e4SLinus Torvalds 	return 0;				\
4281da177e4SLinus Torvalds } while (0)
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds static int block2mtd_setup(const char *val, struct kernel_param *kp)
4311da177e4SLinus Torvalds {
432*a6550e57SJesper Juhl 	char buf[80+12]; /* 80 for device, 12 for erase size */
433*a6550e57SJesper Juhl 	char *str = buf;
4341da177e4SLinus Torvalds 	char *token[2];
4351da177e4SLinus Torvalds 	char *name;
436cc71229fSThomas Gleixner 	size_t erase_size = PAGE_SIZE;
4371da177e4SLinus Torvalds 	int i, ret;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	if (strnlen(val, sizeof(buf)) >= sizeof(buf))
4401da177e4SLinus Torvalds 		parse_err("parameter too long");
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	strcpy(str, val);
4431da177e4SLinus Torvalds 	kill_final_newline(str);
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	for (i = 0; i < 2; i++)
4461da177e4SLinus Torvalds 		token[i] = strsep(&str, ",");
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	if (str)
4491da177e4SLinus Torvalds 		parse_err("too many arguments");
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	if (!token[0])
4521da177e4SLinus Torvalds 		parse_err("no argument");
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	ret = parse_name(&name, token[0], 80);
4551da177e4SLinus Torvalds 	if (ret == -ENOMEM)
4561da177e4SLinus Torvalds 		parse_err("out of memory");
4571da177e4SLinus Torvalds 	if (ret == -ENOSPC)
4581da177e4SLinus Torvalds 		parse_err("name too long");
4591da177e4SLinus Torvalds 	if (ret)
4601da177e4SLinus Torvalds 		return 0;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	if (token[1]) {
463cc71229fSThomas Gleixner 		ret = parse_num(&erase_size, token[1]);
464*a6550e57SJesper Juhl 		if (ret) {
465*a6550e57SJesper Juhl 			kfree(name);
4661da177e4SLinus Torvalds 			parse_err("illegal erase size");
4671da177e4SLinus Torvalds 		}
468*a6550e57SJesper Juhl 	}
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	add_device(name, erase_size);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	return 0;
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
4771da177e4SLinus Torvalds MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds static int __init block2mtd_init(void)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	INFO("version " VERSION);
4821da177e4SLinus Torvalds 	return 0;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds static void __devexit block2mtd_exit(void)
4871da177e4SLinus Torvalds {
4881da177e4SLinus Torvalds 	struct list_head *pos, *next;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	/* Remove the MTD devices */
4911da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &blkmtd_device_list) {
4921da177e4SLinus Torvalds 		struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
4931da177e4SLinus Torvalds 		block2mtd_sync(&dev->mtd);
4941da177e4SLinus Torvalds 		del_mtd_device(&dev->mtd);
4951da177e4SLinus Torvalds 		INFO("mtd%d: [%s] removed", dev->mtd.index,
4961da177e4SLinus Torvalds 				dev->mtd.name + strlen("blkmtd: "));
4971da177e4SLinus Torvalds 		list_del(&dev->list);
4981da177e4SLinus Torvalds 		block2mtd_free_device(dev);
4991da177e4SLinus Torvalds 	}
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds module_init(block2mtd_init);
5041da177e4SLinus Torvalds module_exit(block2mtd_exit);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds MODULE_LICENSE("GPL");
5071da177e4SLinus Torvalds MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others");
5081da177e4SLinus Torvalds MODULE_DESCRIPTION("Emulate an MTD using a block device");
509