提交 d676c117 编写于 作者: M Matthias Kaehlcke 提交者: David Woodhouse

mtd: mtdblock: introduce mtdblks_lock

The mtdblks array and its content are prone to race conditions. Introduce
the mutex mtdblks_lock in order to solve this.

[Amended by Artem Bityutskiy]
Signed-off-by: NMatthias Kaehlcke <matthias@kaehlcke.net>
Signed-off-by: NArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: NDavid Woodhouse <David.Woodhouse@intel.com>
上级 2bf961b7
...@@ -29,6 +29,8 @@ static struct mtdblk_dev { ...@@ -29,6 +29,8 @@ static struct mtdblk_dev {
enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state; enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
} *mtdblks[MAX_MTD_DEVICES]; } *mtdblks[MAX_MTD_DEVICES];
static struct mutex mtdblks_lock;
/* /*
* Cache stuff... * Cache stuff...
* *
...@@ -270,15 +272,19 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd) ...@@ -270,15 +272,19 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n"); DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
mutex_lock(&mtdblks_lock);
if (mtdblks[dev]) { if (mtdblks[dev]) {
mtdblks[dev]->count++; mtdblks[dev]->count++;
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); mtdblk = kzalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
if (!mtdblk) if (!mtdblk) {
mutex_unlock(&mtdblks_lock);
return -ENOMEM; return -ENOMEM;
}
mtdblk->count = 1; mtdblk->count = 1;
mtdblk->mtd = mtd; mtdblk->mtd = mtd;
...@@ -291,6 +297,7 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd) ...@@ -291,6 +297,7 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
} }
mtdblks[dev] = mtdblk; mtdblks[dev] = mtdblk;
mutex_unlock(&mtdblks_lock);
DEBUG(MTD_DEBUG_LEVEL1, "ok\n"); DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
...@@ -304,6 +311,8 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd) ...@@ -304,6 +311,8 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd)
DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n"); DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
mutex_lock(&mtdblks_lock);
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);
...@@ -316,6 +325,9 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd) ...@@ -316,6 +325,9 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd)
vfree(mtdblk->cache_data); vfree(mtdblk->cache_data);
kfree(mtdblk); kfree(mtdblk);
} }
mutex_unlock(&mtdblks_lock);
DEBUG(MTD_DEBUG_LEVEL1, "ok\n"); DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
return 0; return 0;
...@@ -376,6 +388,8 @@ static struct mtd_blktrans_ops mtdblock_tr = { ...@@ -376,6 +388,8 @@ static struct mtd_blktrans_ops mtdblock_tr = {
static int __init init_mtdblock(void) static int __init init_mtdblock(void)
{ {
mutex_init(&mtdblks_lock);
return register_mtd_blktrans(&mtdblock_tr); return register_mtd_blktrans(&mtdblock_tr);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册