OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [fs/] [jffs2/] [v2_0/] [src/] [erase.c] - Blame information for rev 565

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
/*
2
 * JFFS2 -- Journalling Flash File System, Version 2.
3
 *
4
 * Copyright (C) 2001, 2002 Red Hat, Inc.
5
 *
6
 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
7
 *
8
 * For licensing information, see the file 'LICENCE' in this directory.
9
 *
10
 * $Id: erase.c,v 1.1.1.1 2004-02-14 13:29:19 phoenix Exp $
11
 *
12
 */
13
 
14
#include <linux/kernel.h>
15
#include <linux/slab.h>
16
#include <linux/mtd/mtd.h>
17
#include <linux/compiler.h>
18
#include <linux/crc32.h>
19
#include <linux/sched.h>
20
#include <linux/pagemap.h>
21
#include "nodelist.h"
22
 
23
struct erase_priv_struct {
24
        struct jffs2_eraseblock *jeb;
25
        struct jffs2_sb_info *c;
26
};
27
 
28
#ifndef __ECOS
29
static void jffs2_erase_callback(struct erase_info *);
30
#endif
31
static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
32
static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
33
static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
34
static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
35
 
36
void jffs2_erase_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
37
{
38
        int ret;
39
#ifdef __ECOS
40
       ret = jffs2_flash_erase(c, jeb);
41
       if (!ret) {
42
               jffs2_erase_succeeded(c, jeb);
43
               return;
44
       }
45
#else /* Linux */
46
        struct erase_info *instr;
47
 
48
        instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
49
        if (!instr) {
50
                printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
51
                spin_lock(&c->erase_completion_lock);
52
                list_del(&jeb->list);
53
                list_add(&jeb->list, &c->erase_pending_list);
54
                c->erasing_size -= c->sector_size;
55
                spin_unlock(&c->erase_completion_lock);
56
                return;
57
        }
58
 
59
        memset(instr, 0, sizeof(*instr));
60
 
61
        instr->mtd = c->mtd;
62
        instr->addr = jeb->offset;
63
        instr->len = c->sector_size;
64
        instr->callback = jffs2_erase_callback;
65
        instr->priv = (unsigned long)(&instr[1]);
66
 
67
        ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
68
        ((struct erase_priv_struct *)instr->priv)->c = c;
69
 
70
        /* NAND , read out the fail counter, if possible */
71
        if (!jffs2_can_mark_obsolete(c))
72
                jffs2_nand_read_failcnt(c,jeb);
73
 
74
        ret = c->mtd->erase(c->mtd, instr);
75
        if (!ret)
76
                return;
77
 
78
        kfree(instr);
79
#endif /* __ECOS */
80
 
81
        if (ret == -ENOMEM || ret == -EAGAIN) {
82
                /* Erase failed immediately. Refile it on the list */
83
                D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret));
84
                spin_lock(&c->erase_completion_lock);
85
                list_del(&jeb->list);
86
                list_add(&jeb->list, &c->erase_pending_list);
87
                c->erasing_size -= c->sector_size;
88
                spin_unlock(&c->erase_completion_lock);
89
                return;
90
        }
91
 
92
        if (ret == -EROFS)
93
                printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset);
94
        else
95
                printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret);
96
 
97
        /* Note: This is almost identical to jffs2_erase_failed() except
98
           for the fact that we used spin_lock() not spin_lock(). If
99
           we could use spin_lock() from a BH, we could merge them.
100
           Or if we abandon the idea that MTD drivers may call the erase
101
           callback from a BH, I suppose :)
102
        */
103
        jffs2_erase_failed(c, jeb);
104
}
105
 
106
void jffs2_erase_pending_blocks(struct jffs2_sb_info *c)
107
{
108
        struct jffs2_eraseblock *jeb;
109
 
110
        down(&c->erase_free_sem);
111
 
112
        spin_lock(&c->erase_completion_lock);
113
 
114
        while (!list_empty(&c->erase_complete_list) ||
115
               !list_empty(&c->erase_pending_list)) {
116
 
117
                if (!list_empty(&c->erase_complete_list)) {
118
                        jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
119
                        list_del(&jeb->list);
120
                        spin_unlock(&c->erase_completion_lock);
121
                        jffs2_mark_erased_block(c, jeb);
122
 
123
                } else if (!list_empty(&c->erase_pending_list)) {
124
                        jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
125
                        D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
126
                        list_del(&jeb->list);
127
                        c->erasing_size += c->sector_size;
128
                        c->free_size -= jeb->free_size;
129
                        c->used_size -= jeb->used_size;
130
                        c->dirty_size -= jeb->dirty_size;
131
                        jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
132
                        jffs2_free_all_node_refs(c, jeb);
133
                        list_add(&jeb->list, &c->erasing_list);
134
                        spin_unlock(&c->erase_completion_lock);
135
 
136
                        jffs2_erase_block(c, jeb);
137
 
138
                } else {
139
                        BUG();
140
                }
141
 
142
                /* Be nice */
143
                cond_resched();
144
                spin_lock(&c->erase_completion_lock);
145
        }
146
 
147
        spin_unlock(&c->erase_completion_lock);
148
        D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
149
 
150
        up(&c->erase_free_sem);
151
}
152
 
153
static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
154
{
155
        D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset));
156
        spin_lock(&c->erase_completion_lock);
157
        list_del(&jeb->list);
158
        list_add_tail(&jeb->list, &c->erase_complete_list);
159
        spin_unlock(&c->erase_completion_lock);
160
        /* Ensure that kupdated calls us again to mark them clean */
161
        jffs2_erase_pending_trigger(c);
162
}
163
 
164
static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
165
{
166
         spin_lock(&c->erase_completion_lock);
167
         c->erasing_size -= c->sector_size;
168
         c->bad_size += c->sector_size;
169
         list_del(&jeb->list);
170
         list_add(&jeb->list, &c->bad_list);
171
         c->nr_erasing_blocks--;
172
         spin_unlock(&c->erase_completion_lock);
173
         wake_up(&c->erase_wait);
174
}
175
 
176
#ifndef __ECOS
177
static void jffs2_erase_callback(struct erase_info *instr)
178
{
179
        struct erase_priv_struct *priv = (void *)instr->priv;
180
 
181
        if(instr->state != MTD_ERASE_DONE) {
182
                printk(KERN_WARNING "Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr->addr, instr->state);
183
                jffs2_erase_failed(priv->c, priv->jeb);
184
        } else {
185
                jffs2_erase_succeeded(priv->c, priv->jeb);
186
        }
187
        kfree(instr);
188
}
189
#endif /* !__ECOS */
190
 
191
/* Hmmm. Maybe we should accept the extra space it takes and make
192
   this a standard doubly-linked list? */
193
static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
194
                        struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
195
{
196
        struct jffs2_inode_cache *ic = NULL;
197
        struct jffs2_raw_node_ref **prev;
198
 
199
        prev = &ref->next_in_ino;
200
 
201
        /* Walk the inode's list once, removing any nodes from this eraseblock */
202
        while (1) {
203
                if (!(*prev)->next_in_ino) {
204
                        /* We're looking at the jffs2_inode_cache, which is
205
                           at the end of the linked list. Stash it and continue
206
                           from the beginning of the list */
207
                        ic = (struct jffs2_inode_cache *)(*prev);
208
                        prev = &ic->nodes;
209
                        continue;
210
                }
211
 
212
                if (((*prev)->flash_offset & ~(c->sector_size -1)) == jeb->offset) {
213
                        /* It's in the block we're erasing */
214
                        struct jffs2_raw_node_ref *this;
215
 
216
                        this = *prev;
217
                        *prev = this->next_in_ino;
218
                        this->next_in_ino = NULL;
219
 
220
                        if (this == ref)
221
                                break;
222
 
223
                        continue;
224
                }
225
                /* Not to be deleted. Skip */
226
                prev = &((*prev)->next_in_ino);
227
        }
228
 
229
        /* PARANOIA */
230
        if (!ic) {
231
                printk(KERN_WARNING "inode_cache not found in remove_node_refs()!!\n");
232
                return;
233
        }
234
 
235
        D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
236
                  jeb->offset, jeb->offset + c->sector_size, ic->ino));
237
 
238
        D2({
239
                int i=0;
240
                struct jffs2_raw_node_ref *this;
241
                printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n" KERN_DEBUG);
242
 
243
                this = ic->nodes;
244
 
245
                while(this) {
246
                        printk( "0x%08x(%d)->", ref_offset(this), ref_flags(this));
247
                        if (++i == 5) {
248
                                printk("\n" KERN_DEBUG);
249
                                i=0;
250
                        }
251
                        this = this->next_in_ino;
252
                }
253
                printk("\n");
254
        });
255
 
256
        if (ic->nodes == (void *)ic) {
257
                D1(printk(KERN_DEBUG "inocache for ino #%u is all gone now. Freeing\n", ic->ino));
258
                jffs2_del_ino_cache(c, ic);
259
                jffs2_free_inode_cache(ic);
260
        }
261
}
262
 
263
static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
264
{
265
        struct jffs2_raw_node_ref *ref;
266
        D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
267
        while(jeb->first_node) {
268
                ref = jeb->first_node;
269
                jeb->first_node = ref->next_phys;
270
 
271
                /* Remove from the inode-list */
272
                if (ref->next_in_ino)
273
                        jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
274
                /* else it was a non-inode node or already removed, so don't bother */
275
 
276
                jffs2_free_raw_node_ref(ref);
277
        }
278
        jeb->last_node = NULL;
279
}
280
 
281
void jffs2_erase_pending_trigger(struct jffs2_sb_info *c)
282
{
283
        OFNI_BS_2SFFJ(c)->s_dirt = 1;
284
}
285
 
286
static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
287
{
288
        struct jffs2_raw_node_ref *marker_ref = NULL;
289
        unsigned char *ebuf;
290
        size_t retlen;
291
        int ret;
292
 
293
        if (!jffs2_cleanmarker_oob(c)) {
294
                marker_ref = jffs2_alloc_raw_node_ref();
295
                if (!marker_ref) {
296
                        printk(KERN_WARNING "Failed to allocate raw node ref for clean marker\n");
297
                        /* Stick it back on the list from whence it came and come back later */
298
                        jffs2_erase_pending_trigger(c);
299
                        spin_lock(&c->erase_completion_lock);
300
                        list_add(&jeb->list, &c->erase_complete_list);
301
                        spin_unlock(&c->erase_completion_lock);
302
                        return;
303
                }
304
        }
305
        ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
306
        if (!ebuf) {
307
                printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Assuming it worked\n", jeb->offset);
308
        } else {
309
                uint32_t ofs = jeb->offset;
310
 
311
                D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
312
                while(ofs < jeb->offset + c->sector_size) {
313
                        uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
314
                        int i;
315
 
316
                        ret = jffs2_flash_read(c, ofs, readlen, &retlen, ebuf);
317
                        if (ret) {
318
                                printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
319
                                goto bad;
320
                        }
321
                        if (retlen != readlen) {
322
                                printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n", ofs, readlen, retlen);
323
                                goto bad;
324
                        }
325
                        for (i=0; i<readlen; i += sizeof(unsigned long)) {
326
                                /* It's OK. We know it's properly aligned */
327
                                unsigned long datum = *(unsigned long *)(&ebuf[i]);
328
                                if (datum + 1) {
329
                                        printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", datum, ofs + i);
330
                                bad:
331
                                        if (!jffs2_cleanmarker_oob(c))
332
                                                jffs2_free_raw_node_ref(marker_ref);
333
                                        else
334
                                                jffs2_write_nand_badblock( c ,jeb );
335
                                        kfree(ebuf);
336
                                bad2:
337
                                        spin_lock(&c->erase_completion_lock);
338
                                        c->erasing_size -= c->sector_size;
339
                                        c->bad_size += c->sector_size;
340
 
341
                                        list_add_tail(&jeb->list, &c->bad_list);
342
                                        c->nr_erasing_blocks--;
343
                                        spin_unlock(&c->erase_completion_lock);
344
                                        wake_up(&c->erase_wait);
345
                                        return;
346
                                }
347
                        }
348
                        ofs += readlen;
349
                        cond_resched();
350
                }
351
                kfree(ebuf);
352
        }
353
 
354
        /* Write the erase complete marker */
355
        D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
356
        if (jffs2_cleanmarker_oob(c)) {
357
 
358
                if (jffs2_write_nand_cleanmarker(c, jeb))
359
                        goto bad2;
360
 
361
                jeb->first_node = jeb->last_node = NULL;
362
 
363
                jeb->free_size = c->sector_size;
364
                jeb->used_size = 0;
365
                jeb->dirty_size = 0;
366
                jeb->wasted_size = 0;
367
        } else {
368
                struct jffs2_unknown_node marker = {
369
                        .magic =        cpu_to_je16(JFFS2_MAGIC_BITMASK),
370
                        .nodetype =     cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
371
                        .totlen =       cpu_to_je32(c->cleanmarker_size)
372
                };
373
 
374
                marker.hdr_crc = cpu_to_je32(crc32(0, &marker, je32_to_cpu(marker.totlen) - 4));
375
 
376
                ret = jffs2_flash_write(c, jeb->offset, je32_to_cpu(marker.totlen), &retlen, (char *)&marker);
377
                if (ret) {
378
                        printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
379
                               jeb->offset, ret);
380
                        goto bad2;
381
                }
382
                if (retlen != je32_to_cpu(marker.totlen)) {
383
                        printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %d, got %zd\n",
384
                               jeb->offset, je32_to_cpu(marker.totlen), retlen);
385
                        goto bad2;
386
                }
387
 
388
                marker_ref->next_in_ino = NULL;
389
                marker_ref->next_phys = NULL;
390
                marker_ref->flash_offset = jeb->offset | REF_NORMAL;
391
                marker_ref->totlen = PAD(je32_to_cpu(marker.totlen));
392
 
393
                jeb->first_node = jeb->last_node = marker_ref;
394
 
395
                jeb->free_size = c->sector_size - marker_ref->totlen;
396
                jeb->used_size = marker_ref->totlen;
397
                jeb->dirty_size = 0;
398
                jeb->wasted_size = 0;
399
        }
400
 
401
        spin_lock(&c->erase_completion_lock);
402
        c->erasing_size -= c->sector_size;
403
        c->free_size += jeb->free_size;
404
        c->used_size += jeb->used_size;
405
 
406
        ACCT_SANITY_CHECK(c,jeb);
407
        D1(ACCT_PARANOIA_CHECK(jeb));
408
 
409
        list_add_tail(&jeb->list, &c->free_list);
410
        c->nr_erasing_blocks--;
411
        c->nr_free_blocks++;
412
        spin_unlock(&c->erase_completion_lock);
413
        wake_up(&c->erase_wait);
414
}
415
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.