2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
8 * The original JFFS, from which the design for JFFS2 was derived,
9 * was designed and implemented by Axis Communications AB.
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/
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.
21 * The Original Code is JFFS2 - Journalling Flash File System, version 2
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.
34 * $Id: erase.c,v 1.24.2.1 2003/11/02 13:51:17 dwmw2 Exp $
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>
43 #include <linux/crc32.h>
45 struct erase_priv_struct {
46 struct jffs2_eraseblock *jeb;
47 struct jffs2_sb_info *c;
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);
53 void jffs2_erase_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
55 struct erase_info *instr;
58 instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
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);
63 list_add(&jeb->list, &c->erase_pending_list);
64 c->erasing_size -= c->sector_size;
65 spin_unlock_bh(&c->erase_completion_lock);
69 memset(instr, 0, sizeof(*instr));
72 instr->addr = jeb->offset;
73 instr->len = c->sector_size;
74 instr->callback = jffs2_erase_callback;
75 instr->priv = (unsigned long)(&instr[1]);
77 ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
78 ((struct erase_priv_struct *)instr->priv)->c = c;
80 ret = c->mtd->erase(c->mtd, instr);
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);
89 list_add(&jeb->list, &c->erase_pending_list);
90 c->erasing_size -= c->sector_size;
91 spin_unlock_bh(&c->erase_completion_lock);
97 printk(KERN_WARNING "Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n", jeb->offset);
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);
111 void jffs2_erase_pending_blocks(struct jffs2_sb_info *c)
113 struct jffs2_eraseblock *jeb;
115 spin_lock_bh(&c->erase_completion_lock);
116 while (!list_empty(&c->erase_pending_list)) {
118 jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
120 D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
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);
132 jffs2_erase_block(c, jeb);
134 if (current->need_resched)
136 spin_lock_bh(&c->erase_completion_lock);
138 spin_unlock_bh(&c->erase_completion_lock);
139 D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
143 static void jffs2_erase_callback(struct erase_info *instr)
145 struct erase_priv_struct *priv = (void *)instr->priv;
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);
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);
164 /* Make sure someone picks up the block off the erase_complete list */
165 OFNI_BS_2SFFJ(priv->c)->s_dirt = 1;
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)
174 struct jffs2_inode_cache *ic = NULL;
175 struct jffs2_raw_node_ref **prev;
177 prev = &ref->next_in_ino;
179 /* Walk the inode's list once, removing any nodes from this eraseblock */
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);
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;
195 *prev = this->next_in_ino;
196 this->next_in_ino = NULL;
203 /* Not to be deleted. Skip */
204 prev = &((*prev)->next_in_ino);
209 printk(KERN_WARNING "inode_cache not found in remove_node_refs()!!\n");
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));
218 struct jffs2_raw_node_ref *this;
219 printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n" KERN_DEBUG);
224 printk( "0x%08x(%d)->", this->flash_offset & ~3, this->flash_offset &3);
226 printk("\n" KERN_DEBUG);
229 this = this->next_in_ino;
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);
241 static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
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;
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 */
254 jffs2_free_raw_node_ref(ref);
256 jeb->last_node = NULL;
259 void jffs2_erase_pending_trigger(struct jffs2_sb_info *c)
261 OFNI_BS_2SFFJ(c)->s_dirt = 1;
264 void jffs2_mark_erased_blocks(struct jffs2_sb_info *c)
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;
273 marker.hdr_crc = crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4);
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);
281 marker_ref = jffs2_alloc_raw_node_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);
289 ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
291 printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Assuming it worked\n", jeb->offset);
293 __u32 ofs = jeb->offset;
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);
300 ret = c->mtd->read(c->mtd, ofs, readlen, &retlen, ebuf);
302 printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
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);
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]);
313 printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", datum, ofs + i);
315 jffs2_free_raw_node_ref(marker_ref);
318 spin_lock_bh(&c->erase_completion_lock);
319 c->erasing_size -= c->sector_size;
320 c->bad_size += c->sector_size;
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);
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);
338 printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
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);
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));
353 jeb->first_node = jeb->last_node = marker_ref;
355 jeb->free_size = c->sector_size - marker_ref->totlen;
356 jeb->used_size = marker_ref->totlen;
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;
364 ACCT_SANITY_CHECK(c,jeb);
365 ACCT_PARANOIA_CHECK(jeb);
367 list_add_tail(&jeb->list, &c->free_list);
368 c->nr_erasing_blocks--;
370 wake_up(&c->erase_wait);
372 spin_unlock_bh(&c->erase_completion_lock);