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

Subversion Repositories or1k

[/] [or1k/] [tags/] [LINUX_2_4_26_OR32/] [linux/] [linux-2.4/] [fs/] [jffs2/] [erase.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * JFFS2 -- Journalling Flash File System, Version 2.
3
 *
4
 * Copyright (C) 2001 Red Hat, Inc.
5
 *
6
 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
7
 *
8
 * The original JFFS, from which the design for JFFS2 was derived,
9
 * was designed and implemented by Axis Communications AB.
10
 *
11
 * The contents of this file are subject to the Red Hat eCos Public
12
 * License Version 1.1 (the "Licence"); you may not use this file
13
 * except in compliance with the Licence.  You may obtain a copy of
14
 * the Licence at http://www.redhat.com/
15
 *
16
 * Software distributed under the Licence is distributed on an "AS IS"
17
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18
 * See the Licence for the specific language governing rights and
19
 * limitations under the Licence.
20
 *
21
 * The Original Code is JFFS2 - Journalling Flash File System, version 2
22
 *
23
 * Alternatively, the contents of this file may be used under the
24
 * terms of the GNU General Public License version 2 (the "GPL"), in
25
 * which case the provisions of the GPL are applicable instead of the
26
 * above.  If you wish to allow the use of your version of this file
27
 * only under the terms of the GPL and not to allow others to use your
28
 * version of this file under the RHEPL, indicate your decision by
29
 * deleting the provisions above and replace them with the notice and
30
 * other provisions required by the GPL.  If you do not delete the
31
 * provisions above, a recipient may use your version of this file
32
 * under either the RHEPL or the GPL.
33
 *
34
 * $Id: erase.c,v 1.1.1.1 2004-04-15 01:11:04 phoenix Exp $
35
 *
36
 */
37
#include <linux/kernel.h>
38
#include <linux/slab.h>
39
#include <linux/mtd/mtd.h>
40
#include <linux/jffs2.h>
41
#include <linux/interrupt.h>
42
#include "nodelist.h"
43
#include <linux/crc32.h>
44
 
45
struct erase_priv_struct {
46
        struct jffs2_eraseblock *jeb;
47
        struct jffs2_sb_info *c;
48
};
49
 
50
static void jffs2_erase_callback(struct erase_info *);
51
static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
52
 
53
void jffs2_erase_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
54
{
55
        struct erase_info *instr;
56
        int ret;
57
 
58
        instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
59
        if (!instr) {
60
                printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
61
                spin_lock_bh(&c->erase_completion_lock);
62
                list_del(&jeb->list);
63
                list_add(&jeb->list, &c->erase_pending_list);
64
                c->erasing_size -= c->sector_size;
65
                spin_unlock_bh(&c->erase_completion_lock);
66
                return;
67
        }
68
 
69
        memset(instr, 0, sizeof(*instr));
70
 
71
        instr->mtd = c->mtd;
72
        instr->addr = jeb->offset;
73
        instr->len = c->sector_size;
74
        instr->callback = jffs2_erase_callback;
75
        instr->priv = (unsigned long)(&instr[1]);
76
 
77
        ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
78
        ((struct erase_priv_struct *)instr->priv)->c = c;
79
 
80
        ret = c->mtd->erase(c->mtd, instr);
81
        if (!ret) {
82
                return;
83
        }
84
        if (ret == -ENOMEM || ret == -EAGAIN) {
85
                /* Erase failed immediately. Refile it on the list */
86
                D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret));
87
                spin_lock_bh(&c->erase_completion_lock);
88
                list_del(&jeb->list);
89
                list_add(&jeb->list, &c->erase_pending_list);
90
                c->erasing_size -= c->sector_size;
91
                spin_unlock_bh(&c->erase_completion_lock);
92
                kfree(instr);
93
                return;
94
        }
95
 
96
        if (ret == -EROFS)
97
                printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset);
98
        else
99
                printk(KERN_WARNING "Erase at 0x%08x failed immediately: errno %d\n", jeb->offset, ret);
100
        spin_lock_bh(&c->erase_completion_lock);
101
        list_del(&jeb->list);
102
        list_add(&jeb->list, &c->bad_list);
103
        c->nr_erasing_blocks--;
104
        c->bad_size += c->sector_size;
105
        c->erasing_size -= c->sector_size;
106
        spin_unlock_bh(&c->erase_completion_lock);
107
        wake_up(&c->erase_wait);
108
        kfree(instr);
109
}
110
 
111
void jffs2_erase_pending_blocks(struct jffs2_sb_info *c)
112
{
113
        struct jffs2_eraseblock *jeb;
114
 
115
        spin_lock_bh(&c->erase_completion_lock);
116
        while (!list_empty(&c->erase_pending_list)) {
117
 
118
                jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
119
 
120
                D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
121
 
122
                list_del(&jeb->list);
123
                c->erasing_size += c->sector_size;
124
                c->free_size -= jeb->free_size;
125
                c->used_size -= jeb->used_size;
126
                c->dirty_size -= jeb->dirty_size;
127
                jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
128
                jffs2_free_all_node_refs(c, jeb);
129
                list_add(&jeb->list, &c->erasing_list);
130
                spin_unlock_bh(&c->erase_completion_lock);
131
 
132
                jffs2_erase_block(c, jeb);
133
                /* Be nice */
134
                if (current->need_resched)
135
                        schedule();
136
                spin_lock_bh(&c->erase_completion_lock);
137
        }
138
        spin_unlock_bh(&c->erase_completion_lock);
139
        D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
140
}
141
 
142
 
143
static void jffs2_erase_callback(struct erase_info *instr)
144
{
145
        struct erase_priv_struct *priv = (void *)instr->priv;
146
 
147
        if(instr->state != MTD_ERASE_DONE) {
148
                printk(KERN_WARNING "Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr->addr, instr->state);
149
                spin_lock(&priv->c->erase_completion_lock);
150
                priv->c->erasing_size -= priv->c->sector_size;
151
                priv->c->bad_size += priv->c->sector_size;
152
                list_del(&priv->jeb->list);
153
                list_add(&priv->jeb->list, &priv->c->bad_list);
154
                priv->c->nr_erasing_blocks--;
155
                spin_unlock(&priv->c->erase_completion_lock);
156
                wake_up(&priv->c->erase_wait);
157
        } else {
158
                D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", instr->addr));
159
                spin_lock(&priv->c->erase_completion_lock);
160
                list_del(&priv->jeb->list);
161
                list_add_tail(&priv->jeb->list, &priv->c->erase_complete_list);
162
                spin_unlock(&priv->c->erase_completion_lock);
163
        }
164
        /* Make sure someone picks up the block off the erase_complete list */
165
        OFNI_BS_2SFFJ(priv->c)->s_dirt = 1;
166
        kfree(instr);
167
}
168
 
169
/* Hmmm. Maybe we should accept the extra space it takes and make
170
   this a standard doubly-linked list? */
171
static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
172
                        struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
173
{
174
        struct jffs2_inode_cache *ic = NULL;
175
        struct jffs2_raw_node_ref **prev;
176
 
177
        prev = &ref->next_in_ino;
178
 
179
        /* Walk the inode's list once, removing any nodes from this eraseblock */
180
        while (1) {
181
                if (!(*prev)->next_in_ino) {
182
                        /* We're looking at the jffs2_inode_cache, which is
183
                           at the end of the linked list. Stash it and continue
184
                           from the beginning of the list */
185
                        ic = (struct jffs2_inode_cache *)(*prev);
186
                        prev = &ic->nodes;
187
                        continue;
188
                }
189
 
190
                if (((*prev)->flash_offset & ~(c->sector_size -1)) == jeb->offset) {
191
                        /* It's in the block we're erasing */
192
                        struct jffs2_raw_node_ref *this;
193
 
194
                        this = *prev;
195
                        *prev = this->next_in_ino;
196
                        this->next_in_ino = NULL;
197
 
198
                        if (this == ref)
199
                                break;
200
 
201
                        continue;
202
                }
203
                /* Not to be deleted. Skip */
204
                prev = &((*prev)->next_in_ino);
205
        }
206
 
207
        /* PARANOIA */
208
        if (!ic) {
209
                printk(KERN_WARNING "inode_cache not found in remove_node_refs()!!\n");
210
                return;
211
        }
212
 
213
        D1(printk(KERN_DEBUG "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
214
                  jeb->offset, jeb->offset + c->sector_size, ic->ino));
215
 
216
        D2({
217
                int i=0;
218
                struct jffs2_raw_node_ref *this;
219
                printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n" KERN_DEBUG);
220
 
221
                this = ic->nodes;
222
 
223
                while(this) {
224
                        printk( "0x%08x(%d)->", this->flash_offset & ~3, this->flash_offset &3);
225
                        if (++i == 5) {
226
                                printk("\n" KERN_DEBUG);
227
                                i=0;
228
                        }
229
                        this = this->next_in_ino;
230
                }
231
                printk("\n");
232
        });
233
 
234
        if (ic->nodes == (void *)ic) {
235
                D1(printk(KERN_DEBUG "inocache for ino #%u is all gone now. Freeing\n", ic->ino));
236
                jffs2_del_ino_cache(c, ic);
237
                jffs2_free_inode_cache(ic);
238
        }
239
}
240
 
241
static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
242
{
243
        struct jffs2_raw_node_ref *ref;
244
        D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
245
        while(jeb->first_node) {
246
                ref = jeb->first_node;
247
                jeb->first_node = ref->next_phys;
248
 
249
                /* Remove from the inode-list */
250
                if (ref->next_in_ino)
251
                        jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
252
                /* else it was a non-inode node or already removed, so don't bother */
253
 
254
                jffs2_free_raw_node_ref(ref);
255
        }
256
        jeb->last_node = NULL;
257
}
258
 
259
void jffs2_erase_pending_trigger(struct jffs2_sb_info *c)
260
{
261
        OFNI_BS_2SFFJ(c)->s_dirt = 1;
262
}
263
 
264
void jffs2_mark_erased_blocks(struct jffs2_sb_info *c)
265
{
266
        static struct jffs2_unknown_node marker = {JFFS2_MAGIC_BITMASK, JFFS2_NODETYPE_CLEANMARKER, sizeof(struct jffs2_unknown_node)};
267
        struct jffs2_eraseblock *jeb;
268
        struct jffs2_raw_node_ref *marker_ref;
269
        unsigned char *ebuf;
270
        ssize_t retlen;
271
        int ret;
272
 
273
        marker.hdr_crc = crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4);
274
 
275
        spin_lock_bh(&c->erase_completion_lock);
276
        while (!list_empty(&c->erase_complete_list)) {
277
                jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
278
                list_del(&jeb->list);
279
                spin_unlock_bh(&c->erase_completion_lock);
280
 
281
                marker_ref = jffs2_alloc_raw_node_ref();
282
                if (!marker_ref) {
283
                        printk(KERN_WARNING "Failed to allocate raw node ref for clean marker\n");
284
                        /* Come back later */
285
                        jffs2_erase_pending_trigger(c);
286
                        return;
287
                }
288
 
289
                ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
290
                if (!ebuf) {
291
                        printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Assuming it worked\n", jeb->offset);
292
                } else {
293
                        __u32 ofs = jeb->offset;
294
 
295
                        D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
296
                        while(ofs < jeb->offset + c->sector_size) {
297
                                __u32 readlen = min((__u32)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
298
                                int i;
299
 
300
                                ret = c->mtd->read(c->mtd, ofs, readlen, &retlen, ebuf);
301
                                if (ret < 0) {
302
                                        printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
303
                                        goto bad;
304
                                }
305
                                if (retlen != readlen) {
306
                                        printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %d\n", ofs, readlen, retlen);
307
                                        goto bad;
308
                                }
309
                                for (i=0; i<readlen; i += sizeof(unsigned long)) {
310
                                        /* It's OK. We know it's properly aligned */
311
                                        unsigned long datum = *(unsigned long *)(&ebuf[i]);
312
                                        if (datum + 1) {
313
                                                printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", datum, ofs + i);
314
                                        bad:
315
                                                jffs2_free_raw_node_ref(marker_ref);
316
                                                kfree(ebuf);
317
                                        bad2:
318
                                                spin_lock_bh(&c->erase_completion_lock);
319
                                                c->erasing_size -= c->sector_size;
320
                                                c->bad_size += c->sector_size;
321
 
322
                                                list_add_tail(&jeb->list, &c->bad_list);
323
                                                c->nr_erasing_blocks--;
324
                                                spin_unlock_bh(&c->erase_completion_lock);
325
                                                wake_up(&c->erase_wait);
326
                                                return;
327
                                        }
328
                                }
329
                                ofs += readlen;
330
                        }
331
                        kfree(ebuf);
332
                }
333
 
334
                /* Write the erase complete marker */
335
                D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
336
                ret = c->mtd->write(c->mtd, jeb->offset, sizeof(marker), &retlen, (char *)&marker);
337
                if (ret) {
338
                        printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
339
                               jeb->offset, ret);
340
                        goto bad2;
341
                }
342
                if (retlen != sizeof(marker)) {
343
                        printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %d, got %d\n",
344
                               jeb->offset, sizeof(marker), retlen);
345
                        goto bad2;
346
                }
347
 
348
                marker_ref->next_in_ino = NULL;
349
                marker_ref->next_phys = NULL;
350
                marker_ref->flash_offset = jeb->offset;
351
                marker_ref->totlen = PAD(sizeof(marker));
352
 
353
                jeb->first_node = jeb->last_node = marker_ref;
354
 
355
                jeb->free_size = c->sector_size - marker_ref->totlen;
356
                jeb->used_size = marker_ref->totlen;
357
                jeb->dirty_size = 0;
358
 
359
                spin_lock_bh(&c->erase_completion_lock);
360
                c->erasing_size -= c->sector_size;
361
                c->free_size += jeb->free_size;
362
                c->used_size += jeb->used_size;
363
 
364
                ACCT_SANITY_CHECK(c,jeb);
365
                ACCT_PARANOIA_CHECK(jeb);
366
 
367
                list_add_tail(&jeb->list, &c->free_list);
368
                c->nr_erasing_blocks--;
369
                c->nr_free_blocks++;
370
                wake_up(&c->erase_wait);
371
        }
372
        spin_unlock_bh(&c->erase_completion_lock);
373
}

powered by: WebSVN 2.1.0

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