Skip to content

Commit cbfe93e

Browse files
Ben HutchingsDavid Woodhouse
authored andcommitted
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: Ben Hutchings <bhutchings@solarflare.com> Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
1 parent 24c1549 commit cbfe93e

1 file changed

Lines changed: 31 additions & 43 deletions

File tree

‎drivers/mtd/mtdblock.c‎

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
#include <linux/mutex.h>
2020

2121

22-
static struct mtdblk_dev {
23-
struct mtd_info *mtd;
22+
struct mtdblk_dev {
23+
struct mtd_blktrans_dev mbd;
2424
int count;
2525
struct mutex cache_mutex;
2626
unsigned char *cache_data;
2727
unsigned long cache_offset;
2828
unsigned int cache_size;
2929
enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
30-
} *mtdblks[MAX_MTD_DEVICES];
30+
};
3131

3232
static struct mutex mtdblks_lock;
3333

@@ -98,7 +98,7 @@ static int erase_write (struct mtd_info *mtd, unsigned long pos,
9898

9999
static int write_cached_data (struct mtdblk_dev *mtdblk)
100100
{
101-
struct mtd_info *mtd = mtdblk->mtd;
101+
struct mtd_info *mtd = mtdblk->mbd.mtd;
102102
int ret;
103103

104104
if (mtdblk->cache_state != STATE_DIRTY)
@@ -128,7 +128,7 @@ static int write_cached_data (struct mtdblk_dev *mtdblk)
128128
static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
129129
int len, const char *buf)
130130
{
131-
struct mtd_info *mtd = mtdblk->mtd;
131+
struct mtd_info *mtd = mtdblk->mbd.mtd;
132132
unsigned int sect_size = mtdblk->cache_size;
133133
size_t retlen;
134134
int ret;
@@ -198,7 +198,7 @@ static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
198198
static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
199199
int len, char *buf)
200200
{
201-
struct mtd_info *mtd = mtdblk->mtd;
201+
struct mtd_info *mtd = mtdblk->mbd.mtd;
202202
unsigned int sect_size = mtdblk->cache_size;
203203
size_t retlen;
204204
int ret;
@@ -244,16 +244,16 @@ static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
244244
static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
245245
unsigned long block, char *buf)
246246
{
247-
struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
247+
struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
248248
return do_cached_read(mtdblk, block<<9, 512, buf);
249249
}
250250

251251
static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
252252
unsigned long block, char *buf)
253253
{
254-
struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
254+
struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
255255
if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
256-
mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize);
256+
mtdblk->cache_data = vmalloc(mtdblk->mbd.mtd->erasesize);
257257
if (!mtdblk->cache_data)
258258
return -EINTR;
259259
/* -EINTR is not really correct, but it is the best match
@@ -266,37 +266,26 @@ static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
266266

267267
static int mtdblock_open(struct mtd_blktrans_dev *mbd)
268268
{
269-
struct mtdblk_dev *mtdblk;
270-
struct mtd_info *mtd = mbd->mtd;
271-
int dev = mbd->devnum;
269+
struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
272270

273271
DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
274272

275273
mutex_lock(&mtdblks_lock);
276-
if (mtdblks[dev]) {
277-
mtdblks[dev]->count++;
274+
if (mtdblk->count) {
275+
mtdblk->count++;
278276
mutex_unlock(&mtdblks_lock);
279277
return 0;
280278
}
281279

282280
/* OK, it's not open. Create cache info for it */
283-
mtdblk = kzalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
284-
if (!mtdblk) {
285-
mutex_unlock(&mtdblks_lock);
286-
return -ENOMEM;
287-
}
288-
289281
mtdblk->count = 1;
290-
mtdblk->mtd = mtd;
291-
292282
mutex_init(&mtdblk->cache_mutex);
293283
mtdblk->cache_state = STATE_EMPTY;
294-
if ( !(mtdblk->mtd->flags & MTD_NO_ERASE) && mtdblk->mtd->erasesize) {
295-
mtdblk->cache_size = mtdblk->mtd->erasesize;
284+
if (!(mbd->mtd->flags & MTD_NO_ERASE) && mbd->mtd->erasesize) {
285+
mtdblk->cache_size = mbd->mtd->erasesize;
296286
mtdblk->cache_data = NULL;
297287
}
298288

299-
mtdblks[dev] = mtdblk;
300289
mutex_unlock(&mtdblks_lock);
301290

302291
DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
@@ -306,8 +295,7 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
306295

307296
static int mtdblock_release(struct mtd_blktrans_dev *mbd)
308297
{
309-
int dev = mbd->devnum;
310-
struct mtdblk_dev *mtdblk = mtdblks[dev];
298+
struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
311299

312300
DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
313301

@@ -318,12 +306,10 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd)
318306
mutex_unlock(&mtdblk->cache_mutex);
319307

320308
if (!--mtdblk->count) {
321-
/* It was the last usage. Free the device */
322-
mtdblks[dev] = NULL;
323-
if (mtdblk->mtd->sync)
324-
mtdblk->mtd->sync(mtdblk->mtd);
309+
/* It was the last usage. Free the cache */
310+
if (mbd->mtd->sync)
311+
mbd->mtd->sync(mbd->mtd);
325312
vfree(mtdblk->cache_data);
326-
kfree(mtdblk);
327313
}
328314

329315
mutex_unlock(&mtdblks_lock);
@@ -335,40 +321,42 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd)
335321

336322
static int mtdblock_flush(struct mtd_blktrans_dev *dev)
337323
{
338-
struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
324+
struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
339325

340326
mutex_lock(&mtdblk->cache_mutex);
341327
write_cached_data(mtdblk);
342328
mutex_unlock(&mtdblk->cache_mutex);
343329

344-
if (mtdblk->mtd->sync)
345-
mtdblk->mtd->sync(mtdblk->mtd);
330+
if (dev->mtd->sync)
331+
dev->mtd->sync(dev->mtd);
346332
return 0;
347333
}
348334

349335
static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
350336
{
351-
struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
337+
struct mtdblk_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
352338

353339
if (!dev)
354340
return;
355341

356-
dev->mtd = mtd;
357-
dev->devnum = mtd->index;
342+
dev->mbd.mtd = mtd;
343+
dev->mbd.devnum = mtd->index;
358344

359-
dev->size = mtd->size >> 9;
360-
dev->tr = tr;
345+
dev->mbd.size = mtd->size >> 9;
346+
dev->mbd.tr = tr;
361347

362348
if (!(mtd->flags & MTD_WRITEABLE))
363-
dev->readonly = 1;
349+
dev->mbd.readonly = 1;
364350

365-
add_mtd_blktrans_dev(dev);
351+
add_mtd_blktrans_dev(&dev->mbd);
366352
}
367353

368354
static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
369355
{
356+
struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
357+
370358
del_mtd_blktrans_dev(dev);
371-
kfree(dev);
359+
kfree(mtdblk);
372360
}
373361

374362
static struct mtd_blktrans_ops mtdblock_tr = {

0 commit comments

Comments
 (0)