hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/mtd/mtdblock.c
....@@ -1,23 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Direct MTD block device access
34 *
45 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
56 * Copyright © 2000-2003 Nicolas Pitre <nico@fluxnic.net>
6
- *
7
- * This program is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation; either version 2 of the License, or
10
- * (at your option) any later version.
11
- *
12
- * This program is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- * GNU General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU General Public License
18
- * along with this program; if not, write to the Free Software
19
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
- *
217 */
228
239 #include <linux/fs.h>
....@@ -56,7 +42,7 @@
5642 */
5743
5844 static int erase_write (struct mtd_info *mtd, unsigned long pos,
59
- int len, const char *buf)
45
+ unsigned int len, const char *buf)
6046 {
6147 struct erase_info erase;
6248 size_t retlen;
....@@ -103,8 +89,6 @@
10389
10490 ret = erase_write (mtd, mtdblk->cache_offset,
10591 mtdblk->cache_size, mtdblk->cache_data);
106
- if (ret)
107
- return ret;
10892
10993 /*
11094 * Here we could arguably set the cache state to STATE_CLEAN.
....@@ -112,9 +96,14 @@
11296 * be notified if this content is altered on the flash by other
11397 * means. Let's declare it empty and leave buffering tasks to
11498 * the buffer cache instead.
99
+ *
100
+ * If this cache_offset points to a bad block, data cannot be
101
+ * written to the device. Clear cache_state to avoid writing to
102
+ * bad blocks repeatedly.
115103 */
116
- mtdblk->cache_state = STATE_EMPTY;
117
- return 0;
104
+ if (ret == 0 || ret == -EIO)
105
+ mtdblk->cache_state = STATE_EMPTY;
106
+ return ret;
118107 }
119108
120109
....@@ -164,7 +153,7 @@
164153 mtdblk->cache_state = STATE_EMPTY;
165154 ret = mtd_read(mtd, sect_start, sect_size,
166155 &retlen, mtdblk->cache_data);
167
- if (ret)
156
+ if (ret && !mtd_is_bitflip(ret))
168157 return ret;
169158 if (retlen != sect_size)
170159 return -EIO;
....@@ -199,8 +188,12 @@
199188 pr_debug("mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
200189 mtd->name, pos, len);
201190
202
- if (!sect_size)
203
- return mtd_read(mtd, pos, len, &retlen, buf);
191
+ if (!sect_size) {
192
+ ret = mtd_read(mtd, pos, len, &retlen, buf);
193
+ if (ret && !mtd_is_bitflip(ret))
194
+ return ret;
195
+ return 0;
196
+ }
204197
205198 while (len > 0) {
206199 unsigned long sect_start = (pos/sect_size)*sect_size;
....@@ -220,7 +213,7 @@
220213 memcpy (buf, mtdblk->cache_data + offset, size);
221214 } else {
222215 ret = mtd_read(mtd, pos, size, &retlen, buf);
223
- if (ret)
216
+ if (ret && !mtd_is_bitflip(ret))
224217 return ret;
225218 if (retlen != size)
226219 return -EIO;
....@@ -308,12 +301,13 @@
308301 static int mtdblock_flush(struct mtd_blktrans_dev *dev)
309302 {
310303 struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
304
+ int ret;
311305
312306 mutex_lock(&mtdblk->cache_mutex);
313
- write_cached_data(mtdblk);
307
+ ret = write_cached_data(mtdblk);
314308 mutex_unlock(&mtdblk->cache_mutex);
315309 mtd_sync(dev->mtd);
316
- return 0;
310
+ return ret;
317311 }
318312
319313 static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)