Skip to content

Commit 664addc

Browse files
dedekindDavid Woodhouse
authored andcommitted
mtd: remove R/O checking duplication
Many drivers check whether the partition is R/O and return -EROFS if yes. Let's stop having duplicated checks and move them to the API functions instead. And again a bit of noise - deleted few too sparse newlines, sorry. Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
1 parent 5def489 commit 664addc

8 files changed

Lines changed: 17 additions & 54 deletions

File tree

‎drivers/mtd/chips/map_ram.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,10 @@ static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
122122
unsigned long i;
123123

124124
allff = map_word_ff(map);
125-
126125
for (i=0; i<instr->len; i += map_bankwidth(map))
127126
map_write(map, allff, instr->addr + i);
128-
129127
instr->state = MTD_ERASE_DONE;
130-
131128
mtd_erase_callback(instr);
132-
133129
return 0;
134130
}
135131

‎drivers/mtd/chips/map_rom.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ static void maprom_nop(struct mtd_info *mtd)
8585

8686
static int maprom_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
8787
{
88-
printk(KERN_NOTICE "maprom_write called\n");
89-
return -EIO;
88+
return -EROFS;
9089
}
9190

9291
static int maprom_erase (struct mtd_info *mtd, struct erase_info *info)

‎drivers/mtd/devices/phram.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ struct phram_mtd_list {
3333

3434
static LIST_HEAD(phram_list);
3535

36-
3736
static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
3837
{
3938
u_char *start = mtd->priv;

‎drivers/mtd/mtdconcat.c‎

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ concat_write(struct mtd_info *mtd, loff_t to, size_t len,
126126
int err = -EINVAL;
127127
int i;
128128

129-
if (!(mtd->flags & MTD_WRITEABLE))
130-
return -EROFS;
131-
132129
*retlen = 0;
133130

134131
for (i = 0; i < concat->num_subdev; i++) {
@@ -145,11 +142,7 @@ concat_write(struct mtd_info *mtd, loff_t to, size_t len,
145142
else
146143
size = len;
147144

148-
if (!(subdev->flags & MTD_WRITEABLE))
149-
err = -EROFS;
150-
else
151-
err = mtd_write(subdev, to, size, &retsize, buf);
152-
145+
err = mtd_write(subdev, to, size, &retsize, buf);
153146
if (err)
154147
break;
155148

@@ -176,9 +169,6 @@ concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
176169
int i;
177170
int err = -EINVAL;
178171

179-
if (!(mtd->flags & MTD_WRITEABLE))
180-
return -EROFS;
181-
182172
*retlen = 0;
183173

184174
/* Calculate total length of data */
@@ -220,12 +210,8 @@ concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
220210
old_iov_len = vecs_copy[entry_high].iov_len;
221211
vecs_copy[entry_high].iov_len = size;
222212

223-
if (!(subdev->flags & MTD_WRITEABLE))
224-
err = -EROFS;
225-
else
226-
err = mtd_writev(subdev, &vecs_copy[entry_low],
227-
entry_high - entry_low + 1, to,
228-
&retsize);
213+
err = mtd_writev(subdev, &vecs_copy[entry_low],
214+
entry_high - entry_low + 1, to, &retsize);
229215

230216
vecs_copy[entry_high].iov_len = old_iov_len - size;
231217
vecs_copy[entry_high].iov_base += size;
@@ -399,9 +385,6 @@ static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
399385
uint64_t length, offset = 0;
400386
struct erase_info *erase;
401387

402-
if (!(mtd->flags & MTD_WRITEABLE))
403-
return -EROFS;
404-
405388
/*
406389
* Check for proper erase block alignment of the to-be-erased area.
407390
* It is easier to do this based on the super device's erase
@@ -489,10 +472,6 @@ static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
489472
else
490473
erase->len = length;
491474

492-
if (!(subdev->flags & MTD_WRITEABLE)) {
493-
err = -EROFS;
494-
break;
495-
}
496475
length -= erase->len;
497476
if ((err = concat_dev_erase(subdev, erase))) {
498477
/* sanity check: should never happen since

‎drivers/mtd/mtdcore.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,8 @@ int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
693693
{
694694
if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
695695
return -EINVAL;
696+
if (!(mtd->flags & MTD_WRITEABLE))
697+
return -EROFS;
696698
return mtd->_erase(mtd, instr);
697699
}
698700
EXPORT_SYMBOL_GPL(mtd_erase);
@@ -752,10 +754,10 @@ int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
752754
const u_char *buf)
753755
{
754756
*retlen = 0;
755-
if (!mtd->_write)
756-
return -EROFS;
757757
if (to < 0 || to > mtd->size || len > mtd->size - to)
758758
return -EINVAL;
759+
if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
760+
return -EROFS;
759761
return mtd->_write(mtd, to, len, retlen, buf);
760762
}
761763
EXPORT_SYMBOL_GPL(mtd_write);
@@ -775,6 +777,8 @@ int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
775777
return -EOPNOTSUPP;
776778
if (to < 0 || to > mtd->size || len > mtd->size - to)
777779
return -EINVAL;
780+
if (!(mtd->flags & MTD_WRITEABLE))
781+
return -EROFS;
778782
return mtd->_panic_write(mtd, to, len, retlen, buf);
779783
}
780784
EXPORT_SYMBOL_GPL(mtd_panic_write);
@@ -826,6 +830,8 @@ int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
826830
return -EOPNOTSUPP;
827831
if (ofs < 0 || ofs > mtd->size)
828832
return -EINVAL;
833+
if (!(mtd->flags & MTD_WRITEABLE))
834+
return -EROFS;
829835
return mtd->_block_markbad(mtd, ofs);
830836
}
831837
EXPORT_SYMBOL_GPL(mtd_block_markbad);
@@ -877,6 +883,8 @@ int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
877883
unsigned long count, loff_t to, size_t *retlen)
878884
{
879885
*retlen = 0;
886+
if (!(mtd->flags & MTD_WRITEABLE))
887+
return -EROFS;
880888
if (!mtd->_writev)
881889
return default_mtd_writev(mtd, vecs, count, to, retlen);
882890
return mtd->_writev(mtd, vecs, count, to, retlen);

‎drivers/mtd/mtdpart.c‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,13 @@ static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
172172
size_t *retlen, const u_char *buf)
173173
{
174174
struct mtd_part *part = PART(mtd);
175-
if (!(mtd->flags & MTD_WRITEABLE))
176-
return -EROFS;
177175
return mtd_write(part->master, to + part->offset, len, retlen, buf);
178176
}
179177

180178
static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
181179
size_t *retlen, const u_char *buf)
182180
{
183181
struct mtd_part *part = PART(mtd);
184-
if (!(mtd->flags & MTD_WRITEABLE))
185-
return -EROFS;
186182
return mtd_panic_write(part->master, to + part->offset, len, retlen,
187183
buf);
188184
}
@@ -192,9 +188,6 @@ static int part_write_oob(struct mtd_info *mtd, loff_t to,
192188
{
193189
struct mtd_part *part = PART(mtd);
194190

195-
if (!(mtd->flags & MTD_WRITEABLE))
196-
return -EROFS;
197-
198191
if (to >= mtd->size)
199192
return -EINVAL;
200193
if (ops->datbuf && to + ops->len > mtd->size)
@@ -220,8 +213,6 @@ static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
220213
unsigned long count, loff_t to, size_t *retlen)
221214
{
222215
struct mtd_part *part = PART(mtd);
223-
if (!(mtd->flags & MTD_WRITEABLE))
224-
return -EROFS;
225216
return mtd_writev(part->master, vecs, count, to + part->offset,
226217
retlen);
227218
}
@@ -230,8 +221,7 @@ static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
230221
{
231222
struct mtd_part *part = PART(mtd);
232223
int ret;
233-
if (!(mtd->flags & MTD_WRITEABLE))
234-
return -EROFS;
224+
235225
instr->addr += part->offset;
236226
ret = mtd_erase(part->master, instr);
237227
if (ret) {
@@ -304,8 +294,6 @@ static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
304294
struct mtd_part *part = PART(mtd);
305295
int res;
306296

307-
if (!(mtd->flags & MTD_WRITEABLE))
308-
return -EROFS;
309297
ofs += part->offset;
310298
res = mtd_block_markbad(part->master, ofs);
311299
if (!res)

‎drivers/mtd/ubi/gluebi.c‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,6 @@ static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
215215
struct gluebi_device *gluebi;
216216

217217
gluebi = container_of(mtd, struct gluebi_device, mtd);
218-
219-
if (!(mtd->flags & MTD_WRITEABLE))
220-
return -EROFS;
221-
222218
lnum = div_u64_rem(to, mtd->erasesize, &offs);
223219

224220
if (len % mtd->writesize || offs % mtd->writesize)
@@ -263,12 +259,8 @@ static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
263259

264260
lnum = mtd_div_by_eb(instr->addr, mtd);
265261
count = mtd_div_by_eb(instr->len, mtd);
266-
267262
gluebi = container_of(mtd, struct gluebi_device, mtd);
268263

269-
if (!(mtd->flags & MTD_WRITEABLE))
270-
return -EROFS;
271-
272264
for (i = 0; i < count - 1; i++) {
273265
err = ubi_leb_unmap(gluebi->desc, lnum + i);
274266
if (err)

‎include/linux/mtd/mtd.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ static inline int mtd_write_oob(struct mtd_info *mtd, loff_t to,
268268
ops->retlen = ops->oobretlen = 0;
269269
if (!mtd->_write_oob)
270270
return -EOPNOTSUPP;
271+
if (!(mtd->flags & MTD_WRITEABLE))
272+
return -EROFS;
271273
return mtd->_write_oob(mtd, to, ops);
272274
}
273275

0 commit comments

Comments
 (0)