Commit 7788ba71 authored by Atsushi Nemoto's avatar Atsushi Nemoto Committed by David Woodhouse

[MTD][MTDPART] Seperate main loop from per-partition code in add_mtd_partition

add_mtd_partition was a 150+ line monster consisting mostly of a single
loop.  Seperate the loop from most of the body.  Now it should be
obvious which variables are carried around from iteration to iteration.
Signed-off-by: default avatarJörn Engel <joern@logfs.org>
Signed-off-by: default avatarAtsushi Nemoto <anemo@mba.ocn.ne.jp>
Signed-off-by: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
parent 4b5e33a7
...@@ -313,45 +313,32 @@ int del_mtd_partitions(struct mtd_info *master) ...@@ -313,45 +313,32 @@ int del_mtd_partitions(struct mtd_info *master)
return 0; return 0;
} }
/* static struct mtd_part *add_one_partition(struct mtd_info *master,
* This function, given a master MTD object and a partition table, creates const struct mtd_partition *part, int partno,
* and registers slave MTD objects which are bound to the master according to u_int32_t cur_offset)
* the partition definitions.
* (Q: should we register the master MTD object as well?)
*/
int add_mtd_partitions(struct mtd_info *master,
const struct mtd_partition *parts,
int nbparts)
{ {
struct mtd_part *slave; struct mtd_part *slave;
u_int32_t cur_offset = 0;
int i;
printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
for (i = 0; i < nbparts; i++) {
/* allocate the partition structure */ /* allocate the partition structure */
slave = kzalloc (sizeof(*slave), GFP_KERNEL); slave = kzalloc (sizeof(*slave), GFP_KERNEL);
if (!slave) { if (!slave) {
printk ("memory allocation error while creating partitions for \"%s\"\n", printk("memory allocation error while creating partitions for \"%s\"\n",
master->name); master->name);
del_mtd_partitions(master); del_mtd_partitions(master);
return -ENOMEM; return NULL;
} }
list_add(&slave->list, &mtd_partitions); list_add(&slave->list, &mtd_partitions);
/* set up the MTD object for this partition */ /* set up the MTD object for this partition */
slave->mtd.type = master->type; slave->mtd.type = master->type;
slave->mtd.flags = master->flags & ~parts[i].mask_flags; slave->mtd.flags = master->flags & ~part->mask_flags;
slave->mtd.size = parts[i].size; slave->mtd.size = part->size;
slave->mtd.writesize = master->writesize; slave->mtd.writesize = master->writesize;
slave->mtd.oobsize = master->oobsize; slave->mtd.oobsize = master->oobsize;
slave->mtd.oobavail = master->oobavail; slave->mtd.oobavail = master->oobavail;
slave->mtd.subpage_sft = master->subpage_sft; slave->mtd.subpage_sft = master->subpage_sft;
slave->mtd.name = parts[i].name; slave->mtd.name = part->name;
slave->mtd.owner = master->owner; slave->mtd.owner = master->owner;
slave->mtd.read = part_read; slave->mtd.read = part_read;
...@@ -383,7 +370,7 @@ int add_mtd_partitions(struct mtd_info *master, ...@@ -383,7 +370,7 @@ int add_mtd_partitions(struct mtd_info *master,
slave->mtd.get_fact_prot_info = part_get_fact_prot_info; slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
if (master->sync) if (master->sync)
slave->mtd.sync = part_sync; slave->mtd.sync = part_sync;
if (!i && master->suspend && master->resume) { if (!partno && master->suspend && master->resume) {
slave->mtd.suspend = part_suspend; slave->mtd.suspend = part_suspend;
slave->mtd.resume = part_resume; slave->mtd.resume = part_resume;
} }
...@@ -399,8 +386,8 @@ int add_mtd_partitions(struct mtd_info *master, ...@@ -399,8 +386,8 @@ int add_mtd_partitions(struct mtd_info *master,
slave->mtd.block_markbad = part_block_markbad; slave->mtd.block_markbad = part_block_markbad;
slave->mtd.erase = part_erase; slave->mtd.erase = part_erase;
slave->master = master; slave->master = master;
slave->offset = parts[i].offset; slave->offset = part->offset;
slave->index = i; slave->index = partno;
if (slave->offset == MTDPART_OFS_APPEND) if (slave->offset == MTDPART_OFS_APPEND)
slave->offset = cur_offset; slave->offset = cur_offset;
...@@ -410,13 +397,12 @@ int add_mtd_partitions(struct mtd_info *master, ...@@ -410,13 +397,12 @@ int add_mtd_partitions(struct mtd_info *master,
/* Round up to next erasesize */ /* Round up to next erasesize */
slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize; slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
printk(KERN_NOTICE "Moving partition %d: " printk(KERN_NOTICE "Moving partition %d: "
"0x%08x -> 0x%08x\n", i, "0x%08x -> 0x%08x\n", partno,
cur_offset, slave->offset); cur_offset, slave->offset);
} }
} }
if (slave->mtd.size == MTDPART_SIZ_FULL) if (slave->mtd.size == MTDPART_SIZ_FULL)
slave->mtd.size = master->size - slave->offset; slave->mtd.size = master->size - slave->offset;
cur_offset = slave->offset + slave->mtd.size;
printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset, printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
slave->offset + slave->mtd.size, slave->mtd.name); slave->offset + slave->mtd.size, slave->mtd.name);
...@@ -427,12 +413,12 @@ int add_mtd_partitions(struct mtd_info *master, ...@@ -427,12 +413,12 @@ int add_mtd_partitions(struct mtd_info *master,
slave->offset = 0; slave->offset = 0;
slave->mtd.size = 0; slave->mtd.size = 0;
printk ("mtd: partition \"%s\" is out of reach -- disabled\n", printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
parts[i].name); part->name);
} }
if (slave->offset + slave->mtd.size > master->size) { if (slave->offset + slave->mtd.size > master->size) {
slave->mtd.size = master->size - slave->offset; slave->mtd.size = master->size - slave->offset;
printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n", printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
parts[i].name, master->name, slave->mtd.size); part->name, master->name, slave->mtd.size);
} }
if (master->numeraseregions>1) { if (master->numeraseregions>1) {
/* Deal with variable erase size stuff */ /* Deal with variable erase size stuff */
...@@ -440,10 +426,10 @@ int add_mtd_partitions(struct mtd_info *master, ...@@ -440,10 +426,10 @@ int add_mtd_partitions(struct mtd_info *master,
struct mtd_erase_region_info *regions = master->eraseregions; struct mtd_erase_region_info *regions = master->eraseregions;
/* Find the first erase regions which is part of this partition. */ /* Find the first erase regions which is part of this partition. */
for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++) for (i=0; i < master->numeraseregions && regions[i].offset <= slave->offset; i++)
; ;
for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) { for (i--; i < master->numeraseregions && regions[i].offset < slave->offset + slave->mtd.size; i++) {
if (slave->mtd.erasesize < regions[i].erasesize) { if (slave->mtd.erasesize < regions[i].erasesize) {
slave->mtd.erasesize = regions[i].erasesize; slave->mtd.erasesize = regions[i].erasesize;
} }
...@@ -459,13 +445,13 @@ int add_mtd_partitions(struct mtd_info *master, ...@@ -459,13 +445,13 @@ int add_mtd_partitions(struct mtd_info *master,
/* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */ /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
slave->mtd.flags &= ~MTD_WRITEABLE; slave->mtd.flags &= ~MTD_WRITEABLE;
printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n", printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
parts[i].name); part->name);
} }
if ((slave->mtd.flags & MTD_WRITEABLE) && if ((slave->mtd.flags & MTD_WRITEABLE) &&
(slave->mtd.size % slave->mtd.erasesize)) { (slave->mtd.size % slave->mtd.erasesize)) {
slave->mtd.flags &= ~MTD_WRITEABLE; slave->mtd.flags &= ~MTD_WRITEABLE;
printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n", printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
parts[i].name); part->name);
} }
slave->mtd.ecclayout = master->ecclayout; slave->mtd.ecclayout = master->ecclayout;
...@@ -480,17 +466,39 @@ int add_mtd_partitions(struct mtd_info *master, ...@@ -480,17 +466,39 @@ int add_mtd_partitions(struct mtd_info *master,
} }
} }
if(parts[i].mtdp) if(part->mtdp) { /* store the object pointer (caller may or may not register it */
{ /* store the object pointer (caller may or may not register it */ *part->mtdp = &slave->mtd;
*parts[i].mtdp = &slave->mtd;
slave->registered = 0; slave->registered = 0;
} } else {
else
{
/* register our partition */ /* register our partition */
add_mtd_device(&slave->mtd); add_mtd_device(&slave->mtd);
slave->registered = 1; slave->registered = 1;
} }
return slave;
}
/*
* This function, given a master MTD object and a partition table, creates
* and registers slave MTD objects which are bound to the master according to
* the partition definitions.
* (Q: should we register the master MTD object as well?)
*/
int add_mtd_partitions(struct mtd_info *master,
const struct mtd_partition *parts,
int nbparts)
{
struct mtd_part *slave;
u_int32_t cur_offset = 0;
int i;
printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
for (i = 0; i < nbparts; i++) {
slave = add_one_partition(master, parts + i, i, cur_offset);
if (!slave)
return -ENOMEM;
cur_offset = slave->offset + slave->mtd.size;
} }
return 0; return 0;
......
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