block2mtd.c 10.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * block2mtd.c - create an mtd from a block device
 *
 * Copyright (C) 2001,2002	Simon Evans <spse@secret.org.uk>
5
 * Copyright (C) 2004-2006	Joern Engel <joern@wh.fh-wedel.de>
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16
 *
 * Licence: GPL
 */
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/bio.h>
#include <linux/pagemap.h>
#include <linux/list.h>
#include <linux/init.h>
#include <linux/mtd/mtd.h>
I
Ingo Molnar 已提交
17
#include <linux/mutex.h>
18
#include <linux/mount.h>
19
#include <linux/slab.h>
L
Linus Torvalds 已提交
20 21 22 23 24 25 26 27 28 29

#define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
#define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)


/* Info for the block device */
struct block2mtd_dev {
	struct list_head list;
	struct block_device *blkdev;
	struct mtd_info mtd;
I
Ingo Molnar 已提交
30
	struct mutex write_mutex;
L
Linus Torvalds 已提交
31 32 33 34 35 36 37
};


/* Static info about the MTD, used in cleanup_module */
static LIST_HEAD(blkmtd_device_list);


38
static struct page *page_read(struct address_space *mapping, int index)
L
Linus Torvalds 已提交
39
{
40
	return read_mapping_page(mapping, index, NULL);
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
}

/* erase a specified part of the device */
static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
{
	struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
	struct page *page;
	int index = to >> PAGE_SHIFT;	// page index
	int pages = len >> PAGE_SHIFT;
	u_long *p;
	u_long *max;

	while (pages) {
54
		page = page_read(mapping, index);
L
Linus Torvalds 已提交
55 56 57
		if (IS_ERR(page))
			return PTR_ERR(page);

58 59
		max = page_address(page) + PAGE_SIZE;
		for (p=page_address(page); p<max; p++)
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
			if (*p != -1UL) {
				lock_page(page);
				memset(page_address(page), 0xff, PAGE_SIZE);
				set_page_dirty(page);
				unlock_page(page);
				break;
			}

		page_cache_release(page);
		pages--;
		index++;
	}
	return 0;
}
static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
{
	struct block2mtd_dev *dev = mtd->priv;
	size_t from = instr->addr;
	size_t len = instr->len;
	int err;

	instr->state = MTD_ERASING;
I
Ingo Molnar 已提交
82
	mutex_lock(&dev->write_mutex);
L
Linus Torvalds 已提交
83
	err = _block2mtd_erase(dev, from, len);
I
Ingo Molnar 已提交
84
	mutex_unlock(&dev->write_mutex);
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	if (err) {
		ERROR("erase failed err = %d", err);
		instr->state = MTD_ERASE_FAILED;
	} else
		instr->state = MTD_ERASE_DONE;

	mtd_erase_callback(instr);
	return err;
}


static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
		size_t *retlen, u_char *buf)
{
	struct block2mtd_dev *dev = mtd->priv;
	struct page *page;
	int index = from >> PAGE_SHIFT;
102
	int offset = from & (PAGE_SIZE-1);
L
Linus Torvalds 已提交
103 104 105 106 107 108 109 110 111
	int cpylen;

	while (len) {
		if ((offset + len) > PAGE_SIZE)
			cpylen = PAGE_SIZE - offset;	// multiple pages
		else
			cpylen = len;	// this page
		len = len - cpylen;

112
		page = page_read(dev->blkdev->bd_inode->i_mapping, index);
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		if (IS_ERR(page))
			return PTR_ERR(page);

		memcpy(buf, page_address(page) + offset, cpylen);
		page_cache_release(page);

		if (retlen)
			*retlen += cpylen;
		buf += cpylen;
		offset = 0;
		index++;
	}
	return 0;
}


/* write data to the underlying device */
static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
		loff_t to, size_t len, size_t *retlen)
{
	struct page *page;
	struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
	int index = to >> PAGE_SHIFT;	// page index
	int offset = to & ~PAGE_MASK;	// page offset
	int cpylen;

	while (len) {
140
		if ((offset+len) > PAGE_SIZE)
L
Linus Torvalds 已提交
141 142 143 144 145
			cpylen = PAGE_SIZE - offset;	// multiple pages
		else
			cpylen = len;			// this page
		len = len - cpylen;

146
		page = page_read(mapping, index);
L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		if (IS_ERR(page))
			return PTR_ERR(page);

		if (memcmp(page_address(page)+offset, buf, cpylen)) {
			lock_page(page);
			memcpy(page_address(page) + offset, buf, cpylen);
			set_page_dirty(page);
			unlock_page(page);
		}
		page_cache_release(page);

		if (retlen)
			*retlen += cpylen;

		buf += cpylen;
		offset = 0;
		index++;
	}
	return 0;
}
167 168


L
Linus Torvalds 已提交
169 170 171 172 173 174
static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
		size_t *retlen, const u_char *buf)
{
	struct block2mtd_dev *dev = mtd->priv;
	int err;

I
Ingo Molnar 已提交
175
	mutex_lock(&dev->write_mutex);
L
Linus Torvalds 已提交
176
	err = _block2mtd_write(dev, buf, to, len, retlen);
I
Ingo Molnar 已提交
177
	mutex_unlock(&dev->write_mutex);
L
Linus Torvalds 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	if (err > 0)
		err = 0;
	return err;
}


/* sync the device - wait until the write queue is empty */
static void block2mtd_sync(struct mtd_info *mtd)
{
	struct block2mtd_dev *dev = mtd->priv;
	sync_blockdev(dev->blkdev);
	return;
}


static void block2mtd_free_device(struct block2mtd_dev *dev)
{
	if (!dev)
		return;

	kfree(dev->mtd.name);

	if (dev->blkdev) {
201 202
		invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
					0, -1);
203
		blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212
	}

	kfree(dev);
}


/* FIXME: ensure that mtd->size % erase_size == 0 */
static struct block2mtd_dev *add_device(char *devname, int erase_size)
{
213
	const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
L
Linus Torvalds 已提交
214 215
	struct block_device *bdev;
	struct block2mtd_dev *dev;
216
	char *name;
L
Linus Torvalds 已提交
217 218 219 220

	if (!devname)
		return NULL;

221
	dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
L
Linus Torvalds 已提交
222 223 224 225
	if (!dev)
		return NULL;

	/* Get a handle on the device */
226
	bdev = blkdev_get_by_path(devname, mode, dev);
227 228 229 230 231 232
#ifndef MODULE
	if (IS_ERR(bdev)) {

		/* We might not have rootfs mounted at this point. Try
		   to resolve the device name by other means. */

233
		dev_t devt = name_to_dev_t(devname);
234
		if (devt)
235
			bdev = blkdev_get_by_dev(devt, mode, dev);
236 237 238
	}
#endif

L
Linus Torvalds 已提交
239 240 241 242 243 244 245 246 247 248 249
	if (IS_ERR(bdev)) {
		ERROR("error: cannot open device %s", devname);
		goto devinit_err;
	}
	dev->blkdev = bdev;

	if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
		ERROR("attempting to use an MTD device as a block device");
		goto devinit_err;
	}

I
Ingo Molnar 已提交
250
	mutex_init(&dev->write_mutex);
L
Linus Torvalds 已提交
251 252 253

	/* Setup the MTD structure */
	/* make the name contain the block device in */
J
Julia Lawall 已提交
254
	name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
255
	if (!name)
L
Linus Torvalds 已提交
256 257
		goto devinit_err;

258
	dev->mtd.name = name;
L
Linus Torvalds 已提交
259 260 261

	dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
	dev->mtd.erasesize = erase_size;
262
	dev->mtd.writesize = 1;
263
	dev->mtd.writebufsize = PAGE_SIZE;
264
	dev->mtd.type = MTD_RAM;
L
Linus Torvalds 已提交
265
	dev->mtd.flags = MTD_CAP_RAM;
266 267 268 269
	dev->mtd._erase = block2mtd_erase;
	dev->mtd._write = block2mtd_write;
	dev->mtd._sync = block2mtd_sync;
	dev->mtd._read = block2mtd_read;
L
Linus Torvalds 已提交
270 271 272
	dev->mtd.priv = dev;
	dev->mtd.owner = THIS_MODULE;

273
	if (mtd_device_register(&dev->mtd, NULL, 0)) {
L
Lucas De Marchi 已提交
274
		/* Device didn't get added, so free the entry */
L
Linus Torvalds 已提交
275 276 277 278
		goto devinit_err;
	}
	list_add(&dev->list, &blkmtd_device_list);
	INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
279
			dev->mtd.name + strlen("block2mtd: "),
L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287 288
			dev->mtd.erasesize >> 10, dev->mtd.erasesize);
	return dev;

devinit_err:
	block2mtd_free_device(dev);
	return NULL;
}


289 290 291 292 293 294
/* This function works similar to reguler strtoul.  In addition, it
 * allows some suffixes for a more human-readable number format:
 * ki, Ki, kiB, KiB	- multiply result with 1024
 * Mi, MiB		- multiply result with 1024^2
 * Gi, GiB		- multiply result with 1024^3
 */
L
Linus Torvalds 已提交
295 296 297 298 299 300 301 302
static int ustrtoul(const char *cp, char **endp, unsigned int base)
{
	unsigned long result = simple_strtoul(cp, endp, base);
	switch (**endp) {
	case 'G' :
		result *= 1024;
	case 'M':
		result *= 1024;
303
	case 'K':
L
Linus Torvalds 已提交
304 305 306
	case 'k':
		result *= 1024;
	/* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
307 308 309 310 311 312
		if ((*endp)[1] == 'i') {
			if ((*endp)[2] == 'B')
				(*endp) += 3;
			else
				(*endp) += 2;
		}
L
Linus Torvalds 已提交
313 314 315 316 317
	}
	return result;
}


318
static int parse_num(size_t *num, const char *token)
L
Linus Torvalds 已提交
319 320
{
	char *endp;
321
	size_t n;
L
Linus Torvalds 已提交
322

323
	n = (size_t) ustrtoul(token, &endp, 0);
L
Linus Torvalds 已提交
324 325 326
	if (*endp)
		return -EINVAL;

327
	*num = n;
L
Linus Torvalds 已提交
328 329 330 331 332 333 334 335 336 337 338 339
	return 0;
}


static inline void kill_final_newline(char *str)
{
	char *newline = strrchr(str, '\n');
	if (newline && !newline[1])
		*newline = 0;
}


340 341 342
#define parse_err(fmt, args...) do {	\
	ERROR(fmt, ## args);		\
	return 0;			\
L
Linus Torvalds 已提交
343 344
} while (0)

345 346
#ifndef MODULE
static int block2mtd_init_called = 0;
347
static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
348 349 350 351
#endif


static int block2mtd_setup2(const char *val)
L
Linus Torvalds 已提交
352
{
353
	char buf[80 + 12]; /* 80 for device, 12 for erase size */
354
	char *str = buf;
L
Linus Torvalds 已提交
355 356
	char *token[2];
	char *name;
357
	size_t erase_size = PAGE_SIZE;
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365
	int i, ret;

	if (strnlen(val, sizeof(buf)) >= sizeof(buf))
		parse_err("parameter too long");

	strcpy(str, val);
	kill_final_newline(str);

366
	for (i = 0; i < 2; i++)
L
Linus Torvalds 已提交
367 368 369 370 371 372 373 374
		token[i] = strsep(&str, ",");

	if (str)
		parse_err("too many arguments");

	if (!token[0])
		parse_err("no argument");

375 376 377
	name = token[0];
	if (strlen(name) + 1 > 80)
		parse_err("device name too long");
L
Linus Torvalds 已提交
378 379

	if (token[1]) {
380
		ret = parse_num(&erase_size, token[1]);
381
		if (ret) {
L
Linus Torvalds 已提交
382
			parse_err("illegal erase size");
383
		}
L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391
	}

	add_device(name, erase_size);

	return 0;
}


392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
static int block2mtd_setup(const char *val, struct kernel_param *kp)
{
#ifdef MODULE
	return block2mtd_setup2(val);
#else
	/* If more parameters are later passed in via
	   /sys/module/block2mtd/parameters/block2mtd
	   and block2mtd_init() has already been called,
	   we can parse the argument now. */

	if (block2mtd_init_called)
		return block2mtd_setup2(val);

	/* During early boot stage, we only save the parameters
	   here. We must parse them later: if the param passed
	   from kernel boot command line, block2mtd_setup() is
	   called so early that it is not possible to resolve
	   the device (even kmalloc() fails). Deter that work to
	   block2mtd_setup2(). */

	strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));

	return 0;
#endif
}


L
Linus Torvalds 已提交
419 420 421 422 423
module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");

static int __init block2mtd_init(void)
{
424 425 426 427 428 429 430 431 432
	int ret = 0;

#ifndef MODULE
	if (strlen(block2mtd_paramline))
		ret = block2mtd_setup2(block2mtd_paramline);
	block2mtd_init_called = 1;
#endif

	return ret;
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440 441 442 443
}


static void __devexit block2mtd_exit(void)
{
	struct list_head *pos, *next;

	/* Remove the MTD devices */
	list_for_each_safe(pos, next, &blkmtd_device_list) {
		struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
		block2mtd_sync(&dev->mtd);
444
		mtd_device_unregister(&dev->mtd);
L
Linus Torvalds 已提交
445
		INFO("mtd%d: [%s] removed", dev->mtd.index,
446
				dev->mtd.name + strlen("block2mtd: "));
L
Linus Torvalds 已提交
447 448 449 450 451 452 453 454 455 456
		list_del(&dev->list);
		block2mtd_free_device(dev);
	}
}


module_init(block2mtd_init);
module_exit(block2mtd_exit);

MODULE_LICENSE("GPL");
457
MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
L
Linus Torvalds 已提交
458
MODULE_DESCRIPTION("Emulate an MTD using a block device");