Release 4.11 drivers/mtd/devices/block2mtd.c
/*
* block2mtd.c - create an mtd from a block device
*
* Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
* Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de>
*
* Licence: GPL
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
/*
* When the first attempt at device initialization fails, we may need to
* wait a little bit and retry. This timeout, by default 3 seconds, gives
* device time to start up. Required on BCM2708 and a few other chipsets.
*/
#define MTD_DEFAULT_TIMEOUT 3
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/backing-dev.h>
#include <linux/bio.h>
#include <linux/pagemap.h>
#include <linux/list.h>
#include <linux/init.h>
#include <linux/mtd/mtd.h>
#include <linux/mutex.h>
#include <linux/mount.h>
#include <linux/slab.h>
#include <linux/major.h>
/* Info for the block device */
struct block2mtd_dev {
struct list_head list;
struct block_device *blkdev;
struct mtd_info mtd;
struct mutex write_mutex;
};
/* Static info about the MTD, used in cleanup_module */
static LIST_HEAD(blkmtd_device_list);
static struct page *page_read(struct address_space *mapping, int index)
{
return read_mapping_page(mapping, index, NULL);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 25 | 96.15% | 2 | 66.67% |
Nicholas Piggin | 1 | 3.85% | 1 | 33.33% |
Total | 26 | 100.00% | 3 | 100.00% |
/* 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) {
page = page_read(mapping, index);
if (IS_ERR(page))
return PTR_ERR(page);
max = page_address(page) + PAGE_SIZE;
for (p=page_address(page); p<max; p++)
if (*p != -1UL) {
lock_page(page);
memset(page_address(page), 0xff, PAGE_SIZE);
set_page_dirty(page);
unlock_page(page);
balance_dirty_pages_ratelimited(mapping);
break;
}
put_page(page);
pages--;
index++;
}
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 162 | 96.43% | 3 | 60.00% |
Neil Brown | 5 | 2.98% | 1 | 20.00% |
Kirill A. Shutemov | 1 | 0.60% | 1 | 20.00% |
Total | 168 | 100.00% | 5 | 100.00% |
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;
mutex_lock(&dev->write_mutex);
err = _block2mtd_erase(dev, from, len);
mutex_unlock(&dev->write_mutex);
if (err) {
pr_err("erase failed err = %d\n", err);
instr->state = MTD_ERASE_FAILED;
} else
instr->state = MTD_ERASE_DONE;
mtd_erase_callback(instr);
return err;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 105 | 96.33% | 2 | 50.00% |
Joe Perches | 2 | 1.83% | 1 | 25.00% |
Ingo Molnar | 2 | 1.83% | 1 | 25.00% |
Total | 109 | 100.00% | 4 | 100.00% |
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;
int offset = from & (PAGE_SIZE-1);
int cpylen;
while (len) {
if ((offset + len) > PAGE_SIZE)
cpylen = PAGE_SIZE - offset; // multiple pages
else
cpylen = len; // this page
len = len - cpylen;
page = page_read(dev->blkdev->bd_inode->i_mapping, index);
if (IS_ERR(page))
return PTR_ERR(page);
memcpy(buf, page_address(page) + offset, cpylen);
put_page(page);
if (retlen)
*retlen += cpylen;
buf += cpylen;
offset = 0;
index++;
}
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 164 | 99.39% | 4 | 80.00% |
Kirill A. Shutemov | 1 | 0.61% | 1 | 20.00% |
Total | 165 | 100.00% | 5 | 100.00% |
/* 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) {
if ((offset+len) > PAGE_SIZE)
cpylen = PAGE_SIZE - offset; // multiple pages
else
cpylen = len; // this page
len = len - cpylen;
page = page_read(mapping, index);
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);
balance_dirty_pages_ratelimited(mapping);
}
put_page(page);
if (retlen)
*retlen += cpylen;
buf += cpylen;
offset = 0;
index++;
}
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 195 | 97.01% | 3 | 60.00% |
Neil Brown | 5 | 2.49% | 1 | 20.00% |
Kirill A. Shutemov | 1 | 0.50% | 1 | 20.00% |
Total | 201 | 100.00% | 5 | 100.00% |
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;
mutex_lock(&dev->write_mutex);
err = _block2mtd_write(dev, buf, to, len, retlen);
mutex_unlock(&dev->write_mutex);
if (err > 0)
err = 0;
return err;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 80 | 97.56% | 2 | 66.67% |
Ingo Molnar | 2 | 2.44% | 1 | 33.33% |
Total | 82 | 100.00% | 3 | 100.00% |
/* 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;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 28 | 100.00% | 2 | 100.00% |
Total | 28 | 100.00% | 2 | 100.00% |
static void block2mtd_free_device(struct block2mtd_dev *dev)
{
if (!dev)
return;
kfree(dev->mtd.name);
if (dev->blkdev) {
invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
0, -1);
blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
}
kfree(dev);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 55 | 80.88% | 2 | 40.00% |
Andrew Morton | 6 | 8.82% | 1 | 20.00% |
Al Viro | 4 | 5.88% | 1 | 20.00% |
Tejun Heo | 3 | 4.41% | 1 | 20.00% |
Total | 68 | 100.00% | 5 | 100.00% |
static struct block2mtd_dev *add_device(char *devname, int erase_size,
int timeout)
{
#ifndef MODULE
int i;
#endif
const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
struct block_device *bdev = ERR_PTR(-ENODEV);
struct block2mtd_dev *dev;
char *name;
if (!devname)
return NULL;
dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
if (!dev)
return NULL;
/* Get a handle on the device */
bdev = blkdev_get_by_path(devname, mode, dev);
#ifndef MODULE
/*
* We might not have the root device mounted at this point.
* Try to resolve the device name by other means.
*/
for (i = 0; IS_ERR(bdev) && i <= timeout; i++) {
dev_t devt;
if (i)
/*
* Calling wait_for_device_probe in the first loop
* was not enough, sleep for a bit in subsequent
* go-arounds.
*/
msleep(1000);
wait_for_device_probe();
devt = name_to_dev_t(devname);
if (!devt)
continue;
bdev = blkdev_get_by_dev(devt, mode, dev);
}
#endif
if (IS_ERR(bdev)) {
pr_err("error: cannot open device %s\n", devname);
goto err_free_block2mtd;
}
dev->blkdev = bdev;
if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
pr_err("attempting to use an MTD device as a block device\n");
goto err_free_block2mtd;
}
if ((long)dev->blkdev->bd_inode->i_size % erase_size) {
pr_err("erasesize must be a divisor of device size\n");
goto err_free_block2mtd;
}
mutex_init(&dev->write_mutex);
/* Setup the MTD structure */
/* make the name contain the block device in */
name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
if (!name)
goto err_destroy_mutex;
dev->mtd.name = name;
dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
dev->mtd.erasesize = erase_size;
dev->mtd.writesize = 1;
dev->mtd.writebufsize = PAGE_SIZE;
dev->mtd.type = MTD_RAM;
dev->mtd.flags = MTD_CAP_RAM;
dev->mtd._erase = block2mtd_erase;
dev->mtd._write = block2mtd_write;
dev->mtd._sync = block2mtd_sync;
dev->mtd._read = block2mtd_read;
dev->mtd.priv = dev;
dev->mtd.owner = THIS_MODULE;
if (mtd_device_register(&dev->mtd, NULL, 0)) {
/* Device didn't get added, so free the entry */
goto err_destroy_mutex;
}
list_add(&dev->list, &blkmtd_device_list);
pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
dev->mtd.index,
dev->mtd.name + strlen("block2mtd: "),
dev->mtd.erasesize >> 10, dev->mtd.erasesize);
return dev;
err_destroy_mutex:
mutex_destroy(&dev->write_mutex);
err_free_block2mtd:
block2mtd_free_device(dev);
return NULL;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 279 | 60.00% | 3 | 13.64% |
Felix Fietkau | 51 | 10.97% | 1 | 4.55% |
Fabian Frederick | 40 | 8.60% | 2 | 9.09% |
Ville Herva | 22 | 4.73% | 1 | 4.55% |
Artem B. Bityutskiy | 20 | 4.30% | 3 | 13.64% |
Tejun Heo | 20 | 4.30% | 3 | 13.64% |
Greg Kroah-Hartman | 12 | 2.58% | 1 | 4.55% |
Joe Perches | 6 | 1.29% | 1 | 4.55% |
Jamie Iles | 5 | 1.08% | 1 | 4.55% |
Julia Lawall | 5 | 1.08% | 1 | 4.55% |
David Woodhouse | 1 | 0.22% | 1 | 4.55% |
Lucas De Marchi | 1 | 0.22% | 1 | 4.55% |
Burman Yan | 1 | 0.22% | 1 | 4.55% |
Stephane Chazelas | 1 | 0.22% | 1 | 4.55% |
Ingo Molnar | 1 | 0.22% | 1 | 4.55% |
Total | 465 | 100.00% | 22 | 100.00% |
/* 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
*/
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;
case 'K':
case 'k':
result *= 1024;
/* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
if ((*endp)[1] == 'i') {
if ((*endp)[2] == 'B')
(*endp) += 3;
else
(*endp) += 2;
}
}
return result;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 110 | 100.00% | 2 | 100.00% |
Total | 110 | 100.00% | 2 | 100.00% |
static int parse_num(size_t *num, const char *token)
{
char *endp;
size_t n;
n = (size_t) ustrtoul(token, &endp, 0);
if (*endp)
return -EINVAL;
*num = n;
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 46 | 85.19% | 1 | 50.00% |
Thomas Gleixner | 8 | 14.81% | 1 | 50.00% |
Total | 54 | 100.00% | 2 | 100.00% |
static inline void kill_final_newline(char *str)
{
char *newline = strrchr(str, '\n');
if (newline && !newline[1])
*newline = 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 37 | 100.00% | 1 | 100.00% |
Total | 37 | 100.00% | 1 | 100.00% |
#ifndef MODULE
static int block2mtd_init_called = 0;
/* 80 for device, 12 for erase size */
static char block2mtd_paramline[80 + 12];
#endif
static int block2mtd_setup2(const char *val)
{
/* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
char buf[80 + 12 + 80 + 8];
char *str = buf;
char *token[2];
char *name;
size_t erase_size = PAGE_SIZE;
unsigned long timeout = MTD_DEFAULT_TIMEOUT;
int i, ret;
if (strnlen(val, sizeof(buf)) >= sizeof(buf)) {
pr_err("parameter too long\n");
return 0;
}
strcpy(str, val);
kill_final_newline(str);
for (i = 0; i < 2; i++)
token[i] = strsep(&str, ",");
if (str) {
pr_err("too many arguments\n");
return 0;
}
if (!token[0]) {
pr_err("no argument\n");
return 0;
}
name = token[0];
if (strlen(name) + 1 > 80) {
pr_err("device name too long\n");
return 0;
}
if (token[1]) {
ret = parse_num(&erase_size, token[1]);
if (ret) {
pr_err("illegal erase size\n");
return 0;
}
}
add_device(name, erase_size, timeout);
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 170 | 73.91% | 2 | 28.57% |
Joe Perches | 33 | 14.35% | 1 | 14.29% |
Felix Fietkau | 13 | 5.65% | 1 | 14.29% |
Ville Herva | 8 | 3.48% | 1 | 14.29% |
Jesper Juhl | 4 | 1.74% | 1 | 14.29% |
Thomas Gleixner | 2 | 0.87% | 1 | 14.29% |
Total | 230 | 100.00% | 7 | 100.00% |
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
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Ville Herva | 56 | 100.00% | 1 | 100.00% |
Total | 56 | 100.00% | 1 | 100.00% |
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)
{
int ret = 0;
#ifndef MODULE
if (strlen(block2mtd_paramline))
ret = block2mtd_setup2(block2mtd_paramline);
block2mtd_init_called = 1;
#endif
return ret;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Ville Herva | 29 | 72.50% | 1 | 33.33% |
Jörn Engel | 11 | 27.50% | 2 | 66.67% |
Total | 40 | 100.00% | 3 | 100.00% |
static void 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);
mtd_device_unregister(&dev->mtd);
mutex_destroy(&dev->write_mutex);
pr_info("mtd%d: [%s] removed\n",
dev->mtd.index,
dev->mtd.name + strlen("block2mtd: "));
list_del(&dev->list);
block2mtd_free_device(dev);
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 92 | 88.46% | 2 | 33.33% |
Fabian Frederick | 8 | 7.69% | 1 | 16.67% |
Joe Perches | 2 | 1.92% | 1 | 16.67% |
Stephane Chazelas | 1 | 0.96% | 1 | 16.67% |
Jamie Iles | 1 | 0.96% | 1 | 16.67% |
Total | 104 | 100.00% | 6 | 100.00% |
late_initcall(block2mtd_init);
module_exit(block2mtd_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
MODULE_DESCRIPTION("Emulate an MTD using a block device");
Overall Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jörn Engel | 1662 | 79.22% | 7 | 18.92% |
Ville Herva | 138 | 6.58% | 1 | 2.70% |
Felix Fietkau | 74 | 3.53% | 1 | 2.70% |
Joe Perches | 50 | 2.38% | 1 | 2.70% |
Fabian Frederick | 48 | 2.29% | 2 | 5.41% |
Tejun Heo | 29 | 1.38% | 5 | 13.51% |
Artem B. Bityutskiy | 20 | 0.95% | 3 | 8.11% |
Greg Kroah-Hartman | 12 | 0.57% | 1 | 2.70% |
Thomas Gleixner | 10 | 0.48% | 1 | 2.70% |
Neil Brown | 10 | 0.48% | 1 | 2.70% |
Ingo Molnar | 7 | 0.33% | 1 | 2.70% |
Jamie Iles | 6 | 0.29% | 1 | 2.70% |
Andrew Morton | 6 | 0.29% | 1 | 2.70% |
Julia Lawall | 5 | 0.24% | 1 | 2.70% |
Al Viro | 4 | 0.19% | 1 | 2.70% |
Jesper Juhl | 4 | 0.19% | 1 | 2.70% |
Kirill A. Shutemov | 3 | 0.14% | 1 | 2.70% |
Ezequiel García | 3 | 0.14% | 1 | 2.70% |
Stephane Chazelas | 2 | 0.10% | 1 | 2.70% |
David Woodhouse | 1 | 0.05% | 1 | 2.70% |
Lucas De Marchi | 1 | 0.05% | 1 | 2.70% |
Adrian Bunk | 1 | 0.05% | 1 | 2.70% |
Burman Yan | 1 | 0.05% | 1 | 2.70% |
Nicholas Piggin | 1 | 0.05% | 1 | 2.70% |
Total | 2098 | 100.00% | 37 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.