Skip to content

Commit

Permalink
mtd: do not duplicate length and offset checks in drivers
Browse files Browse the repository at this point in the history
We already verify that offset and length are within the MTD device size
in the MTD API functions. Let's remove the duplicated checks in drivers.
This patch only affects the following API's:

'mtd_erase()'
'mtd_point()'
'mtd_unpoint()'
'mtd_get_unmapped_area()'
'mtd_read()'
'mtd_write()'
'mtd_panic_write()'
'mtd_lock()'
'mtd_unlock()'
'mtd_is_locked()'
'mtd_block_isbad()'
'mtd_block_markbad()'

This patch adds a bit of noise by removing too sparse empty lines, but this is
not too bad.

Signed-off-by: Artem Bityutskiy <[email protected]>
Signed-off-by: David Woodhouse <[email protected]>
  • Loading branch information
dedekind authored and David Woodhouse committed Mar 26, 2012
1 parent 8273a0c commit 5def489
Show file tree
Hide file tree
Showing 23 changed files with 3 additions and 321 deletions.
2 changes: 1 addition & 1 deletion drivers/mtd/chips/cfi_cmdset_0001.c
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ static int cfi_intelext_point(struct mtd_info *mtd, loff_t from, size_t len,
int chipnum;
int ret = 0;

if (!map->virt || (from + len > mtd->size))
if (!map->virt)
return -EINVAL;

/* Now lock the chip(s) to POINT state */
Expand Down
9 changes: 0 additions & 9 deletions drivers/mtd/chips/cfi_cmdset_0020.c
Original file line number Diff line number Diff line change
Expand Up @@ -904,12 +904,6 @@ static int cfi_staa_erase_varsize(struct mtd_info *mtd,
int i, first;
struct mtd_erase_region_info *regions = mtd->eraseregions;

if (instr->addr > mtd->size)
return -EINVAL;

if ((instr->len + instr->addr) > mtd->size)
return -EINVAL;

/* Check that both start and end of the requested erase are
* aligned with the erasesize at the appropriate addresses.
*/
Expand Down Expand Up @@ -1155,9 +1149,6 @@ static int cfi_staa_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
if (len & (mtd->erasesize -1))
return -EINVAL;

if ((len + ofs) > mtd->size)
return -EINVAL;

chipnum = ofs >> cfi->chipshift;
adr = ofs - (chipnum << cfi->chipshift);

Expand Down
6 changes: 0 additions & 6 deletions drivers/mtd/chips/cfi_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,6 @@ int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
int i, first;
struct mtd_erase_region_info *regions = mtd->eraseregions;

if (ofs > mtd->size)
return -EINVAL;

if ((len + ofs) > mtd->size)
return -EINVAL;

/* Check that both start and end of the requested erase are
* aligned with the erasesize at the appropriate addresses.
*/
Expand Down
9 changes: 0 additions & 9 deletions drivers/mtd/devices/block2mtd.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,6 @@ static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
int offset = from & (PAGE_SIZE-1);
int cpylen;

if (from > mtd->size)
return -EINVAL;
if (from + len > mtd->size)
len = mtd->size - from;

if (retlen)
*retlen = 0;

Expand Down Expand Up @@ -190,10 +185,6 @@ static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,

if (!len)
return 0;
if (to >= mtd->size)
return -ENOSPC;
if (to + len > mtd->size)
len = mtd->size - to;

mutex_lock(&dev->write_mutex);
err = _block2mtd_write(dev, buf, to, len, retlen);
Expand Down
8 changes: 0 additions & 8 deletions drivers/mtd/devices/doc2000.c
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,6 @@ static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
int i, len256 = 0, ret=0;
size_t left = len;

/* Don't allow read past end of device */
if (from >= this->totlen)
return -EINVAL;

mutex_lock(&this->lock);

*retlen = 0;
Expand Down Expand Up @@ -748,10 +744,6 @@ static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
size_t left = len;
int status;

/* Don't allow write past end of device */
if (to >= this->totlen)
return -EINVAL;

mutex_lock(&this->lock);

*retlen = 0;
Expand Down
8 changes: 0 additions & 8 deletions drivers/mtd/devices/doc2001.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,6 @@ static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
void __iomem *docptr = this->virtadr;
struct Nand *mychip = &this->chips[from >> (this->chipshift)];

/* Don't allow read past end of device */
if (from >= this->totlen)
return -EINVAL;

/* Don't allow a single read to cross a 512-byte block boundary */
if (from + len > ((from | 0x1ff) + 1))
len = ((from | 0x1ff) + 1) - from;
Expand Down Expand Up @@ -494,10 +490,6 @@ static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
void __iomem *docptr = this->virtadr;
struct Nand *mychip = &this->chips[to >> (this->chipshift)];

/* Don't allow write past end of device */
if (to >= this->totlen)
return -EINVAL;

#if 0
/* Don't allow a single write to cross a 512-byte block boundary */
if (to + len > ( (to | 0x1ff) + 1))
Expand Down
8 changes: 0 additions & 8 deletions drivers/mtd/devices/doc2001plus.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,10 +581,6 @@ static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
void __iomem * docptr = this->virtadr;
struct Nand *mychip = &this->chips[from >> (this->chipshift)];

/* Don't allow read past end of device */
if (from >= this->totlen)
return -EINVAL;

/* Don't allow a single read to cross a 512-byte block boundary */
if (from + len > ((from | 0x1ff) + 1))
len = ((from | 0x1ff) + 1) - from;
Expand Down Expand Up @@ -700,10 +696,6 @@ static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
void __iomem * docptr = this->virtadr;
struct Nand *mychip = &this->chips[to >> (this->chipshift)];

/* Don't allow write past end of device */
if (to >= this->totlen)
return -EINVAL;

/* Don't allow writes which aren't exactly one block (512 bytes) */
if ((to & 0x1ff) || (len != 0x200))
return -EINVAL;
Expand Down
5 changes: 0 additions & 5 deletions drivers/mtd/devices/lart.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,6 @@ static int flash_erase (struct mtd_info *mtd,struct erase_info *instr)
printk (KERN_DEBUG "%s(addr = 0x%.8x, len = %d)\n", __func__, instr->addr, instr->len);
#endif

/* sanity checks */
if (instr->addr + instr->len > mtd->size) return (-EINVAL);

/*
* check that both start and end of the requested erase are
* aligned with the erasesize at the appropriate addresses.
Expand Down Expand Up @@ -442,7 +439,6 @@ static int flash_read (struct mtd_info *mtd,loff_t from,size_t len,size_t *retle

/* sanity checks */
if (!len) return (0);
if (from + len > mtd->size) return (-EINVAL);

/* we always read len bytes */
*retlen = len;
Expand Down Expand Up @@ -526,7 +522,6 @@ static int flash_write (struct mtd_info *mtd,loff_t to,size_t len,size_t *retlen

/* sanity checks */
if (!len) return (0);
if (to + len > mtd->size) return (-EINVAL);

/* first, we write a 0xFF.... padded byte until we reach a dword boundary */
if (to & (BUSWIDTH - 1))
Expand Down
12 changes: 0 additions & 12 deletions drivers/mtd/devices/m25p80.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,6 @@ static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
__func__, (long long)instr->addr,
(long long)instr->len);

/* sanity checks */
if (instr->addr + instr->len > flash->mtd.size)
return -EINVAL;
div_u64_rem(instr->len, mtd->erasesize, &rem);
if (rem)
return -EINVAL;
Expand Down Expand Up @@ -353,9 +350,6 @@ static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
if (!len)
return 0;

if (from + len > flash->mtd.size)
return -EINVAL;

spi_message_init(&m);
memset(t, 0, (sizeof t));

Expand Down Expand Up @@ -423,9 +417,6 @@ static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
if (!len)
return(0);

if (to + len > flash->mtd.size)
return -EINVAL;

spi_message_init(&m);
memset(t, 0, (sizeof t));

Expand Down Expand Up @@ -515,9 +506,6 @@ static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
if (!len)
return 0;

if (to + len > flash->mtd.size)
return -EINVAL;

spi_message_init(&m);
memset(t, 0, (sizeof t));

Expand Down
8 changes: 0 additions & 8 deletions drivers/mtd/devices/ms02-nv.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,8 @@ static int ms02nv_read(struct mtd_info *mtd, loff_t from,
{
struct ms02nv_private *mp = mtd->priv;

if (from + len > mtd->size)
return -EINVAL;

memcpy(buf, mp->uaddr + from, len);
*retlen = len;

return 0;
}

Expand All @@ -73,12 +69,8 @@ static int ms02nv_write(struct mtd_info *mtd, loff_t to,
{
struct ms02nv_private *mp = mtd->priv;

if (to + len > mtd->size)
return -EINVAL;

memcpy(mp->uaddr + to, buf, len);
*retlen = len;

return 0;
}

Expand Down
7 changes: 0 additions & 7 deletions drivers/mtd/devices/mtd_dataflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,6 @@ static int dataflash_erase(struct mtd_info *mtd, struct erase_info *instr)
dev_name(&spi->dev), (long long)instr->addr,
(long long)instr->len);

/* Sanity checks */
if (instr->addr + instr->len > mtd->size)
return -EINVAL;
div_u64_rem(instr->len, priv->page_size, &rem);
if (rem)
return -EINVAL;
Expand Down Expand Up @@ -257,8 +254,6 @@ static int dataflash_read(struct mtd_info *mtd, loff_t from, size_t len,
/* Sanity checks */
if (!len)
return 0;
if (from + len > mtd->size)
return -EINVAL;

/* Calculate flash page/byte address */
addr = (((unsigned)from / priv->page_size) << priv->page_offset)
Expand Down Expand Up @@ -333,8 +328,6 @@ static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len,
/* Sanity checks */
if (!len)
return 0;
if ((to + len) > mtd->size)
return -EINVAL;

spi_message_init(&msg);

Expand Down
17 changes: 0 additions & 17 deletions drivers/mtd/devices/mtdram.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,18 @@ static struct mtd_info *mtd_info;

static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
{
if (instr->addr + instr->len > mtd->size)
return -EINVAL;

memset((char *)mtd->priv + instr->addr, 0xff, instr->len);

instr->state = MTD_ERASE_DONE;
mtd_erase_callback(instr);

return 0;
}

static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,
size_t *retlen, void **virt, resource_size_t *phys)
{
if (from + len > mtd->size)
return -EINVAL;

/* can we return a physical address with this driver? */
if (phys)
return -EINVAL;

*virt = mtd->priv + from;
*retlen = len;
return 0;
Expand All @@ -81,23 +72,15 @@ static unsigned long ram_get_unmapped_area(struct mtd_info *mtd,
static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
size_t *retlen, u_char *buf)
{
if (from + len > mtd->size)
return -EINVAL;

memcpy(buf, mtd->priv + from, len);

*retlen = len;
return 0;
}

static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const u_char *buf)
{
if (to + len > mtd->size)
return -EINVAL;

memcpy((char *)mtd->priv + to, buf, len);

*retlen = len;
return 0;
}
Expand Down
23 changes: 0 additions & 23 deletions drivers/mtd/devices/phram.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,20 @@ static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
{
u_char *start = mtd->priv;

if (instr->addr + instr->len > mtd->size)
return -EINVAL;

memset(start + instr->addr, 0xff, instr->len);

/* This'll catch a few races. Free the thing before returning :)
* I don't feel at all ashamed. This kind of thing is possible anyway
* with flash, but unlikely.
*/

instr->state = MTD_ERASE_DONE;

mtd_erase_callback(instr);

return 0;
}

static int phram_point(struct mtd_info *mtd, loff_t from, size_t len,
size_t *retlen, void **virt, resource_size_t *phys)
{
if (from + len > mtd->size)
return -EINVAL;

/* can we return a physical address with this driver? */
if (phys)
return -EINVAL;
Expand All @@ -80,14 +71,7 @@ static int phram_read(struct mtd_info *mtd, loff_t from, size_t len,
{
u_char *start = mtd->priv;

if (from >= mtd->size)
return -EINVAL;

if (len > mtd->size - from)
len = mtd->size - from;

memcpy(buf, start + from, len);

*retlen = len;
return 0;
}
Expand All @@ -97,14 +81,7 @@ static int phram_write(struct mtd_info *mtd, loff_t to, size_t len,
{
u_char *start = mtd->priv;

if (to >= mtd->size)
return -EINVAL;

if (len > mtd->size - to)
len = mtd->size - to;

memcpy(start + to, buf, len);

*retlen = len;
return 0;
}
Expand Down
Loading

0 comments on commit 5def489

Please sign in to comment.