Skip to content

Commit be95745

Browse files
Simon KagstromDavid Woodhouse
authored andcommitted
mtd: mtdoops: keep track of used/unused pages in an array
This patch makes mtdoops keep track of used/unused pages in an array instead of scanning the flash after a write. The advantage with this approach is that it avoids calling mtd->read on a panic, which is not possible for all mtd drivers. Signed-off-by: Simon Kagstrom <simon.kagstrom@netinsight.net> Reviewed-by: Anders Grafstrom <anders.grafstrom@netinsight.net> Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
1 parent a15b124 commit be95745

1 file changed

Lines changed: 44 additions & 18 deletions

File tree

‎drivers/mtd/mtdoops.c‎

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static struct mtdoops_context {
4444
int oops_pages;
4545
int nextpage;
4646
int nextcount;
47+
unsigned long *oops_page_used;
4748
char *name;
4849

4950
void *oops_buf;
@@ -54,18 +55,38 @@ static struct mtdoops_context {
5455
int writecount;
5556
} oops_cxt;
5657

58+
static void mark_page_used(struct mtdoops_context *cxt, int page)
59+
{
60+
set_bit(page, cxt->oops_page_used);
61+
}
62+
63+
static void mark_page_unused(struct mtdoops_context *cxt, int page)
64+
{
65+
clear_bit(page, cxt->oops_page_used);
66+
}
67+
68+
static int page_is_used(struct mtdoops_context *cxt, int page)
69+
{
70+
return test_bit(page, cxt->oops_page_used);
71+
}
72+
5773
static void mtdoops_erase_callback(struct erase_info *done)
5874
{
5975
wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
6076
wake_up(wait_q);
6177
}
6278

63-
static int mtdoops_erase_block(struct mtd_info *mtd, int offset)
79+
static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset)
6480
{
81+
struct mtd_info *mtd = cxt->mtd;
82+
u32 start_page_offset = mtd_div_by_eb(offset, mtd) * mtd->erasesize;
83+
u32 start_page = start_page_offset / OOPS_PAGE_SIZE;
84+
u32 erase_pages = mtd->erasesize / OOPS_PAGE_SIZE;
6585
struct erase_info erase;
6686
DECLARE_WAITQUEUE(wait, current);
6787
wait_queue_head_t wait_q;
6888
int ret;
89+
int page;
6990

7091
init_waitqueue_head(&wait_q);
7192
erase.mtd = mtd;
@@ -90,34 +111,23 @@ static int mtdoops_erase_block(struct mtd_info *mtd, int offset)
90111
schedule(); /* Wait for erase to finish. */
91112
remove_wait_queue(&wait_q, &wait);
92113

114+
/* Mark pages as unused */
115+
for (page = start_page; page < start_page + erase_pages; page++)
116+
mark_page_unused(cxt, page);
117+
93118
return 0;
94119
}
95120

96121
static void mtdoops_inc_counter(struct mtdoops_context *cxt)
97122
{
98-
struct mtd_info *mtd = cxt->mtd;
99-
size_t retlen;
100-
u32 count;
101-
int ret;
102-
103123
cxt->nextpage++;
104124
if (cxt->nextpage >= cxt->oops_pages)
105125
cxt->nextpage = 0;
106126
cxt->nextcount++;
107127
if (cxt->nextcount == 0xffffffff)
108128
cxt->nextcount = 0;
109129

110-
ret = mtd->read(mtd, cxt->nextpage * OOPS_PAGE_SIZE, 4,
111-
&retlen, (u_char *) &count);
112-
if (retlen != 4 || (ret < 0 && ret != -EUCLEAN)) {
113-
printk(KERN_ERR "mtdoops: read failure at %d (%td of 4 read), err %d\n",
114-
cxt->nextpage * OOPS_PAGE_SIZE, retlen, ret);
115-
schedule_work(&cxt->work_erase);
116-
return;
117-
}
118-
119-
/* See if we need to erase the next block */
120-
if (count != 0xffffffff) {
130+
if (page_is_used(cxt, cxt->nextpage)) {
121131
schedule_work(&cxt->work_erase);
122132
return;
123133
}
@@ -168,7 +178,7 @@ static void mtdoops_workfunc_erase(struct work_struct *work)
168178
}
169179

170180
for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
171-
ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE);
181+
ret = mtdoops_erase_block(cxt, cxt->nextpage * OOPS_PAGE_SIZE);
172182

173183
if (ret >= 0) {
174184
printk(KERN_DEBUG "mtdoops: ready %d, %d\n",
@@ -209,6 +219,7 @@ static void mtdoops_write(struct mtdoops_context *cxt, int panic)
209219
if (retlen != OOPS_PAGE_SIZE || ret < 0)
210220
printk(KERN_ERR "mtdoops: write failure at %d (%td of %d written), error %d\n",
211221
cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret);
222+
mark_page_used(cxt, cxt->nextpage);
212223

213224
mtdoops_inc_counter(cxt);
214225
}
@@ -230,13 +241,17 @@ static void find_next_position(struct mtdoops_context *cxt)
230241
size_t retlen;
231242

232243
for (page = 0; page < cxt->oops_pages; page++) {
244+
/* Assume the page is used */
245+
mark_page_used(cxt, page);
233246
ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 8, &retlen, (u_char *) &count[0]);
234247
if (retlen != 8 || (ret < 0 && ret != -EUCLEAN)) {
235248
printk(KERN_ERR "mtdoops: read failure at %d (%td of 8 read), err %d\n",
236249
page * OOPS_PAGE_SIZE, retlen, ret);
237250
continue;
238251
}
239252

253+
if (count[0] == 0xffffffff && count[1] == 0xffffffff)
254+
mark_page_unused(cxt, page);
240255
if (count[1] != MTDOOPS_KERNMSG_MAGIC)
241256
continue;
242257
if (count[0] == 0xffffffff)
@@ -273,6 +288,9 @@ static void find_next_position(struct mtdoops_context *cxt)
273288
static void mtdoops_notify_add(struct mtd_info *mtd)
274289
{
275290
struct mtdoops_context *cxt = &oops_cxt;
291+
u64 mtdoops_pages = mtd->size;
292+
293+
do_div(mtdoops_pages, OOPS_PAGE_SIZE);
276294

277295
if (cxt->name && !strcmp(mtd->name, cxt->name))
278296
cxt->mtd_index = mtd->index;
@@ -292,6 +310,13 @@ static void mtdoops_notify_add(struct mtd_info *mtd)
292310
return;
293311
}
294312

313+
/* oops_page_used is a bit field */
314+
cxt->oops_page_used = vmalloc(DIV_ROUND_UP(mtdoops_pages,
315+
BITS_PER_LONG));
316+
if (!cxt->oops_page_used) {
317+
printk(KERN_ERR "Could not allocate page array\n");
318+
return;
319+
}
295320
cxt->mtd = mtd;
296321
if (mtd->size > INT_MAX)
297322
cxt->oops_pages = INT_MAX / OOPS_PAGE_SIZE;
@@ -444,6 +469,7 @@ static void __exit mtdoops_console_exit(void)
444469
unregister_console(&mtdoops_console);
445470
kfree(cxt->name);
446471
vfree(cxt->oops_buf);
472+
vfree(cxt->oops_page_used);
447473
}
448474

449475

0 commit comments

Comments
 (0)