Commit cbfe93e9 authored by Ben Hutchings's avatar Ben Hutchings Committed by David Woodhouse

mtd: mtdblock: Dynamically allocate cache info structures

Since we allocate struct mtd_blktrans_dev for each block device, we
can add our own structure members to the end.  Therefore embed
struct mtd_blktrans_dev in struct mtdblk_dev and remove the static
array of struct mtdblk_dev.  Also remove the redundant pointer to
struct mtd_info.

This is preparation for removing the static limit on the number of MTD
devices.
Signed-off-by: default avatarBen Hutchings <bhutchings@solarflare.com>
Signed-off-by: default avatarArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
parent 24c15496
...@@ -19,15 +19,15 @@ ...@@ -19,15 +19,15 @@
#include <linux/mutex.h> #include <linux/mutex.h>
static struct mtdblk_dev { struct mtdblk_dev {
struct mtd_info *mtd; struct mtd_blktrans_dev mbd;
int count; int count;
struct mutex cache_mutex; struct mutex cache_mutex;
unsigned char *cache_data; unsigned char *cache_data;
unsigned long cache_offset; unsigned long cache_offset;
unsigned int cache_size; unsigned int cache_size;
enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state; enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
} *mtdblks[MAX_MTD_DEVICES]; };
static struct mutex mtdblks_lock; static struct mutex mtdblks_lock;
...@@ -98,7 +98,7 @@ static int erase_write (struct mtd_info *mtd, unsigned long pos, ...@@ -98,7 +98,7 @@ static int erase_write (struct mtd_info *mtd, unsigned long pos,
static int write_cached_data (struct mtdblk_dev *mtdblk) static int write_cached_data (struct mtdblk_dev *mtdblk)
{ {
struct mtd_info *mtd = mtdblk->mtd; struct mtd_info *mtd = mtdblk->mbd.mtd;
int ret; int ret;
if (mtdblk->cache_state != STATE_DIRTY) if (mtdblk->cache_state != STATE_DIRTY)
...@@ -128,7 +128,7 @@ static int write_cached_data (struct mtdblk_dev *mtdblk) ...@@ -128,7 +128,7 @@ static int write_cached_data (struct mtdblk_dev *mtdblk)
static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos, static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
int len, const char *buf) int len, const char *buf)
{ {
struct mtd_info *mtd = mtdblk->mtd; struct mtd_info *mtd = mtdblk->mbd.mtd;
unsigned int sect_size = mtdblk->cache_size; unsigned int sect_size = mtdblk->cache_size;
size_t retlen; size_t retlen;
int ret; int ret;
...@@ -198,7 +198,7 @@ static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos, ...@@ -198,7 +198,7 @@ static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos, static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
int len, char *buf) int len, char *buf)
{ {
struct mtd_info *mtd = mtdblk->mtd; struct mtd_info *mtd = mtdblk->mbd.mtd;
unsigned int sect_size = mtdblk->cache_size; unsigned int sect_size = mtdblk->cache_size;
size_t retlen; size_t retlen;
int ret; int ret;
...@@ -244,16 +244,16 @@ static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos, ...@@ -244,16 +244,16 @@ static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
static int mtdblock_readsect(struct mtd_blktrans_dev *dev, static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
unsigned long block, char *buf) unsigned long block, char *buf)
{ {
struct mtdblk_dev *mtdblk = mtdblks[dev->devnum]; struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
return do_cached_read(mtdblk, block<<9, 512, buf); return do_cached_read(mtdblk, block<<9, 512, buf);
} }
static int mtdblock_writesect(struct mtd_blktrans_dev *dev, static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
unsigned long block, char *buf) unsigned long block, char *buf)
{ {
struct mtdblk_dev *mtdblk = mtdblks[dev->devnum]; struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) { if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize); mtdblk->cache_data = vmalloc(mtdblk->mbd.mtd->erasesize);
if (!mtdblk->cache_data) if (!mtdblk->cache_data)
return -EINTR; return -EINTR;
/* -EINTR is not really correct, but it is the best match /* -EINTR is not really correct, but it is the best match
...@@ -266,37 +266,26 @@ static int mtdblock_writesect(struct mtd_blktrans_dev *dev, ...@@ -266,37 +266,26 @@ static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
static int mtdblock_open(struct mtd_blktrans_dev *mbd) static int mtdblock_open(struct mtd_blktrans_dev *mbd)
{ {
struct mtdblk_dev *mtdblk; struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
struct mtd_info *mtd = mbd->mtd;
int dev = mbd->devnum;
DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n"); DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
mutex_lock(&mtdblks_lock); mutex_lock(&mtdblks_lock);
if (mtdblks[dev]) { if (mtdblk->count) {
mtdblks[dev]->count++; mtdblk->count++;
mutex_unlock(&mtdblks_lock); mutex_unlock(&mtdblks_lock);
return 0; return 0;
} }
/* OK, it's not open. Create cache info for it */ /* OK, it's not open. Create cache info for it */
mtdblk = kzalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
if (!mtdblk) {
mutex_unlock(&mtdblks_lock);
return -ENOMEM;
}
mtdblk->count = 1; mtdblk->count = 1;
mtdblk->mtd = mtd;
mutex_init(&mtdblk->cache_mutex); mutex_init(&mtdblk->cache_mutex);
mtdblk->cache_state = STATE_EMPTY; mtdblk->cache_state = STATE_EMPTY;
if ( !(mtdblk->mtd->flags & MTD_NO_ERASE) && mtdblk->mtd->erasesize) { if (!(mbd->mtd->flags & MTD_NO_ERASE) && mbd->mtd->erasesize) {
mtdblk->cache_size = mtdblk->mtd->erasesize; mtdblk->cache_size = mbd->mtd->erasesize;
mtdblk->cache_data = NULL; mtdblk->cache_data = NULL;
} }
mtdblks[dev] = mtdblk;
mutex_unlock(&mtdblks_lock); mutex_unlock(&mtdblks_lock);
DEBUG(MTD_DEBUG_LEVEL1, "ok\n"); DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
...@@ -306,8 +295,7 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd) ...@@ -306,8 +295,7 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
static int mtdblock_release(struct mtd_blktrans_dev *mbd) static int mtdblock_release(struct mtd_blktrans_dev *mbd)
{ {
int dev = mbd->devnum; struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
struct mtdblk_dev *mtdblk = mtdblks[dev];
DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n"); DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
...@@ -318,12 +306,10 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd) ...@@ -318,12 +306,10 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd)
mutex_unlock(&mtdblk->cache_mutex); mutex_unlock(&mtdblk->cache_mutex);
if (!--mtdblk->count) { if (!--mtdblk->count) {
/* It was the last usage. Free the device */ /* It was the last usage. Free the cache */
mtdblks[dev] = NULL; if (mbd->mtd->sync)
if (mtdblk->mtd->sync) mbd->mtd->sync(mbd->mtd);
mtdblk->mtd->sync(mtdblk->mtd);
vfree(mtdblk->cache_data); vfree(mtdblk->cache_data);
kfree(mtdblk);
} }
mutex_unlock(&mtdblks_lock); mutex_unlock(&mtdblks_lock);
...@@ -335,40 +321,42 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd) ...@@ -335,40 +321,42 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd)
static int mtdblock_flush(struct mtd_blktrans_dev *dev) static int mtdblock_flush(struct mtd_blktrans_dev *dev)
{ {
struct mtdblk_dev *mtdblk = mtdblks[dev->devnum]; struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
mutex_lock(&mtdblk->cache_mutex); mutex_lock(&mtdblk->cache_mutex);
write_cached_data(mtdblk); write_cached_data(mtdblk);
mutex_unlock(&mtdblk->cache_mutex); mutex_unlock(&mtdblk->cache_mutex);
if (mtdblk->mtd->sync) if (dev->mtd->sync)
mtdblk->mtd->sync(mtdblk->mtd); dev->mtd->sync(dev->mtd);
return 0; return 0;
} }
static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
{ {
struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL); struct mtdblk_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev) if (!dev)
return; return;
dev->mtd = mtd; dev->mbd.mtd = mtd;
dev->devnum = mtd->index; dev->mbd.devnum = mtd->index;
dev->size = mtd->size >> 9; dev->mbd.size = mtd->size >> 9;
dev->tr = tr; dev->mbd.tr = tr;
if (!(mtd->flags & MTD_WRITEABLE)) if (!(mtd->flags & MTD_WRITEABLE))
dev->readonly = 1; dev->mbd.readonly = 1;
add_mtd_blktrans_dev(dev); add_mtd_blktrans_dev(&dev->mbd);
} }
static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev) static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
{ {
struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
del_mtd_blktrans_dev(dev); del_mtd_blktrans_dev(dev);
kfree(dev); kfree(mtdblk);
} }
static struct mtd_blktrans_ops mtdblock_tr = { static struct mtd_blktrans_ops mtdblock_tr = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment