source: rtems/cpukit/libfs/src/jffs2/src/erase.c @ 6ac6a5c8

5
Last change on this file since 6ac6a5c8 was 6ac6a5c8, checked in by Sebastian Huber <sebastian.huber@…>, on 03/27/18 at 11:01:56

jffs2: Do not use command line defines

Update #3375.

  • Property mode set to 100644
File size: 14.3 KB
Line 
1#include "rtems-jffs2-config.h"
2
3/*
4 * JFFS2 -- Journalling Flash File System, Version 2.
5 *
6 * Copyright © 2001-2007 Red Hat, Inc.
7 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
8 *
9 * Created by David Woodhouse <dwmw2@infradead.org>
10 *
11 * For licensing information, see the file 'LICENCE' in this directory.
12 *
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/kernel.h>
18#include <linux/slab.h>
19#include <linux/mtd/mtd.h>
20#include <linux/compiler.h>
21#include <linux/crc32.h>
22#include <linux/sched.h>
23#include <linux/pagemap.h>
24#include "nodelist.h"
25
26struct erase_priv_struct {
27        struct jffs2_eraseblock *jeb;
28        struct jffs2_sb_info *c;
29};
30
31#ifndef __ECOS
32static void jffs2_erase_callback(struct erase_info *);
33#endif
34static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
35static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
36static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
37
38static void jffs2_erase_block(struct jffs2_sb_info *c,
39                              struct jffs2_eraseblock *jeb)
40{
41        int ret;
42        uint32_t bad_offset;
43#ifdef __ECOS
44       ret = jffs2_flash_erase(c, jeb);
45       if (!ret) {
46               jffs2_erase_succeeded(c, jeb);
47               return;
48       }
49       bad_offset = jeb->offset;
50#else /* Linux */
51        struct erase_info *instr;
52
53        jffs2_dbg(1, "%s(): erase block %#08x (range %#08x-%#08x)\n",
54                  __func__,
55                  jeb->offset, jeb->offset, jeb->offset + c->sector_size);
56        instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
57        if (!instr) {
58                pr_warn("kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
59                mutex_lock(&c->erase_free_sem);
60                spin_lock(&c->erase_completion_lock);
61                list_move(&jeb->list, &c->erase_pending_list);
62                c->erasing_size -= c->sector_size;
63                c->dirty_size += c->sector_size;
64                jeb->dirty_size = c->sector_size;
65                spin_unlock(&c->erase_completion_lock);
66                mutex_unlock(&c->erase_free_sem);
67                return;
68        }
69
70        memset(instr, 0, sizeof(*instr));
71
72        instr->mtd = c->mtd;
73        instr->addr = jeb->offset;
74        instr->len = c->sector_size;
75        instr->callback = jffs2_erase_callback;
76        instr->priv = (unsigned long)(&instr[1]);
77
78        ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
79        ((struct erase_priv_struct *)instr->priv)->c = c;
80
81        ret = mtd_erase(c->mtd, instr);
82        if (!ret)
83                return;
84
85        bad_offset = instr->fail_addr;
86        kfree(instr);
87#endif /* __ECOS */
88
89        if (ret == -ENOMEM || ret == -EAGAIN) {
90                /* Erase failed immediately. Refile it on the list */
91                jffs2_dbg(1, "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n",
92                          jeb->offset, ret);
93                mutex_lock(&c->erase_free_sem);
94                spin_lock(&c->erase_completion_lock);
95                list_move(&jeb->list, &c->erase_pending_list);
96                c->erasing_size -= c->sector_size;
97                c->dirty_size += c->sector_size;
98                jeb->dirty_size = c->sector_size;
99                spin_unlock(&c->erase_completion_lock);
100                mutex_unlock(&c->erase_free_sem);
101                return;
102        }
103
104        if (ret == -EROFS)
105                pr_warn("Erase at 0x%08x failed immediately: -EROFS. Is the sector locked?\n",
106                        jeb->offset);
107        else
108                pr_warn("Erase at 0x%08x failed immediately: errno %d\n",
109                        jeb->offset, ret);
110
111        jffs2_erase_failed(c, jeb, bad_offset);
112}
113
114int jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
115{
116        struct jffs2_eraseblock *jeb;
117        int work_done = 0;
118
119        mutex_lock(&c->erase_free_sem);
120
121        spin_lock(&c->erase_completion_lock);
122
123        while (!list_empty(&c->erase_complete_list) ||
124               !list_empty(&c->erase_pending_list)) {
125
126                if (!list_empty(&c->erase_complete_list)) {
127                        jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
128                        list_move(&jeb->list, &c->erase_checking_list);
129                        spin_unlock(&c->erase_completion_lock);
130                        mutex_unlock(&c->erase_free_sem);
131                        jffs2_mark_erased_block(c, jeb);
132
133                        work_done++;
134                        if (!--count) {
135                                jffs2_dbg(1, "Count reached. jffs2_erase_pending_blocks leaving\n");
136                                goto done;
137                        }
138
139                } else if (!list_empty(&c->erase_pending_list)) {
140                        jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
141                        jffs2_dbg(1, "Starting erase of pending block 0x%08x\n",
142                                  jeb->offset);
143                        list_del(&jeb->list);
144                        c->erasing_size += c->sector_size;
145                        c->wasted_size -= jeb->wasted_size;
146                        c->free_size -= jeb->free_size;
147                        c->used_size -= jeb->used_size;
148                        c->dirty_size -= jeb->dirty_size;
149                        jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
150                        jffs2_free_jeb_node_refs(c, jeb);
151                        list_add(&jeb->list, &c->erasing_list);
152                        spin_unlock(&c->erase_completion_lock);
153                        mutex_unlock(&c->erase_free_sem);
154
155                        jffs2_erase_block(c, jeb);
156
157                } else {
158                        BUG();
159                }
160
161                /* Be nice */
162                cond_resched();
163                mutex_lock(&c->erase_free_sem);
164                spin_lock(&c->erase_completion_lock);
165        }
166
167        spin_unlock(&c->erase_completion_lock);
168        mutex_unlock(&c->erase_free_sem);
169 done:
170        jffs2_dbg(1, "jffs2_erase_pending_blocks completed\n");
171        return work_done;
172}
173
174static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
175{
176        jffs2_dbg(1, "Erase completed successfully at 0x%08x\n", jeb->offset);
177        mutex_lock(&c->erase_free_sem);
178        spin_lock(&c->erase_completion_lock);
179        list_move_tail(&jeb->list, &c->erase_complete_list);
180        /* Wake the GC thread to mark them clean */
181        jffs2_garbage_collect_trigger(c);
182        spin_unlock(&c->erase_completion_lock);
183        mutex_unlock(&c->erase_free_sem);
184        wake_up(&c->erase_wait);
185}
186
187static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
188{
189        /* For NAND, if the failure did not occur at the device level for a
190           specific physical page, don't bother updating the bad block table. */
191        if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
192                /* We had a device-level failure to erase.  Let's see if we've
193                   failed too many times. */
194                if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
195                        /* We'd like to give this block another try. */
196                        mutex_lock(&c->erase_free_sem);
197                        spin_lock(&c->erase_completion_lock);
198                        list_move(&jeb->list, &c->erase_pending_list);
199                        c->erasing_size -= c->sector_size;
200                        c->dirty_size += c->sector_size;
201                        jeb->dirty_size = c->sector_size;
202                        spin_unlock(&c->erase_completion_lock);
203                        mutex_unlock(&c->erase_free_sem);
204                        return;
205                }
206        }
207
208        mutex_lock(&c->erase_free_sem);
209        spin_lock(&c->erase_completion_lock);
210        c->erasing_size -= c->sector_size;
211        c->bad_size += c->sector_size;
212        list_move(&jeb->list, &c->bad_list);
213        c->nr_erasing_blocks--;
214        spin_unlock(&c->erase_completion_lock);
215        mutex_unlock(&c->erase_free_sem);
216        wake_up(&c->erase_wait);
217}
218
219#ifndef __ECOS
220static void jffs2_erase_callback(struct erase_info *instr)
221{
222        struct erase_priv_struct *priv = (void *)instr->priv;
223
224        if(instr->state != MTD_ERASE_DONE) {
225                pr_warn("Erase at 0x%08llx finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n",
226                        (unsigned long long)instr->addr, instr->state);
227                jffs2_erase_failed(priv->c, priv->jeb, instr->fail_addr);
228        } else {
229                jffs2_erase_succeeded(priv->c, priv->jeb);
230        }
231        kfree(instr);
232}
233#endif /* !__ECOS */
234
235/* Hmmm. Maybe we should accept the extra space it takes and make
236   this a standard doubly-linked list? */
237static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
238                        struct jffs2_raw_node_ref *ref, struct jffs2_eraseblock *jeb)
239{
240        struct jffs2_inode_cache *ic = NULL;
241        struct jffs2_raw_node_ref **prev;
242
243        prev = &ref->next_in_ino;
244
245        /* Walk the inode's list once, removing any nodes from this eraseblock */
246        while (1) {
247                if (!(*prev)->next_in_ino) {
248                        /* We're looking at the jffs2_inode_cache, which is
249                           at the end of the linked list. Stash it and continue
250                           from the beginning of the list */
251                        ic = (struct jffs2_inode_cache *)(*prev);
252                        prev = &ic->nodes;
253                        continue;
254                }
255
256                if (SECTOR_ADDR((*prev)->flash_offset) == jeb->offset) {
257                        /* It's in the block we're erasing */
258                        struct jffs2_raw_node_ref *this;
259
260                        this = *prev;
261                        *prev = this->next_in_ino;
262                        this->next_in_ino = NULL;
263
264                        if (this == ref)
265                                break;
266
267                        continue;
268                }
269                /* Not to be deleted. Skip */
270                prev = &((*prev)->next_in_ino);
271        }
272
273        /* PARANOIA */
274        if (!ic) {
275                JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
276                              " not found in remove_node_refs()!!\n");
277                return;
278        }
279
280        jffs2_dbg(1, "Removed nodes in range 0x%08x-0x%08x from ino #%u\n",
281                  jeb->offset, jeb->offset + c->sector_size, ic->ino);
282
283        D2({
284                int i=0;
285                struct jffs2_raw_node_ref *this;
286                printk(KERN_DEBUG "After remove_node_refs_from_ino_list: \n");
287
288                this = ic->nodes;
289
290                printk(KERN_DEBUG);
291                while(this) {
292                        pr_cont("0x%08x(%d)->",
293                               ref_offset(this), ref_flags(this));
294                        if (++i == 5) {
295                                printk(KERN_DEBUG);
296                                i=0;
297                        }
298                        this = this->next_in_ino;
299                }
300                pr_cont("\n");
301        });
302
303        switch (ic->class) {
304#ifdef CONFIG_JFFS2_FS_XATTR
305                case RAWNODE_CLASS_XATTR_DATUM:
306                        jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
307                        break;
308                case RAWNODE_CLASS_XATTR_REF:
309                        jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
310                        break;
311#endif
312                default:
313                        if (ic->nodes == (void *)ic && ic->pino_nlink == 0)
314                                jffs2_del_ino_cache(c, ic);
315        }
316}
317
318void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
319{
320        struct jffs2_raw_node_ref *block, *ref;
321        jffs2_dbg(1, "Freeing all node refs for eraseblock offset 0x%08x\n",
322                  jeb->offset);
323
324        block = ref = jeb->first_node;
325
326        while (ref) {
327                if (ref->flash_offset == REF_LINK_NODE) {
328                        ref = ref->next_in_ino;
329                        jffs2_free_refblock(block);
330                        block = ref;
331                        continue;
332                }
333                if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
334                        jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
335                /* else it was a non-inode node or already removed, so don't bother */
336
337                ref++;
338        }
339        jeb->first_node = jeb->last_node = NULL;
340}
341
342static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
343{
344        void *ebuf;
345        uint32_t ofs;
346        size_t retlen;
347        int ret;
348        unsigned long *wordebuf;
349
350        ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen,
351                        &ebuf, NULL);
352        if (ret != -EOPNOTSUPP) {
353                if (ret) {
354                        jffs2_dbg(1, "MTD point failed %d\n", ret);
355                        goto do_flash_read;
356                }
357                if (retlen < c->sector_size) {
358                        /* Don't muck about if it won't let us point to the whole erase sector */
359                        jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
360                                  retlen);
361                        mtd_unpoint(c->mtd, jeb->offset, retlen);
362                        goto do_flash_read;
363                }
364                wordebuf = ebuf-sizeof(*wordebuf);
365                retlen /= sizeof(*wordebuf);
366                do {
367                   if (*++wordebuf != ~0)
368                           break;
369                } while(--retlen);
370                mtd_unpoint(c->mtd, jeb->offset, c->sector_size);
371                if (retlen) {
372                        pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08tx\n",
373                                *wordebuf,
374                                jeb->offset +
375                                c->sector_size-retlen * sizeof(*wordebuf));
376                        return -EIO;
377                }
378                return 0;
379        }
380 do_flash_read:
381        ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
382        if (!ebuf) {
383                pr_warn("Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n",
384                        jeb->offset);
385                return -EAGAIN;
386        }
387
388        jffs2_dbg(1, "Verifying erase at 0x%08x\n", jeb->offset);
389
390        for (ofs = jeb->offset; ofs < jeb->offset + c->sector_size; ) {
391                uint32_t readlen = min((uint32_t)PAGE_SIZE, jeb->offset + c->sector_size - ofs);
392                int i;
393
394                *bad_offset = ofs;
395
396                ret = jffs2_flash_read(c, ofs, readlen, &retlen, ebuf);
397                if (ret) {
398                        pr_warn("Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n",
399                                ofs, ret);
400                        ret = -EIO;
401                        goto fail;
402                }
403                if (retlen != readlen) {
404                        pr_warn("Short read from newly-erased block at 0x%08x. Wanted %d, got %zd\n",
405                                ofs, readlen, retlen);
406                        ret = -EIO;
407                        goto fail;
408                }
409                for (i=0; i<readlen; i += sizeof(unsigned long)) {
410                        /* It's OK. We know it's properly aligned */
411                        unsigned long *datum = ebuf + i;
412                        if (*datum + 1) {
413                                *bad_offset += i;
414                                pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08x\n",
415                                        *datum, *bad_offset);
416                                ret = -EIO;
417                                goto fail;
418                        }
419                }
420                ofs += readlen;
421                cond_resched();
422        }
423        ret = 0;
424fail:
425        kfree(ebuf);
426        return ret;
427}
428
429static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
430{
431        size_t retlen;
432        int ret;
433        uint32_t uninitialized_var(bad_offset);
434
435        switch (jffs2_block_check_erase(c, jeb, &bad_offset)) {
436        case -EAGAIN:   goto refile;
437        case -EIO:      goto filebad;
438        }
439
440        /* Write the erase complete marker */
441        jffs2_dbg(1, "Writing erased marker to block at 0x%08x\n", jeb->offset);
442        bad_offset = jeb->offset;
443
444        /* Cleanmarker in oob area or no cleanmarker at all ? */
445        if (jffs2_cleanmarker_oob(c) || c->cleanmarker_size == 0) {
446
447                if (jffs2_cleanmarker_oob(c)) {
448                        if (jffs2_write_nand_cleanmarker(c, jeb))
449                                goto filebad;
450                }
451        } else {
452
453                struct kvec vecs[1];
454                struct jffs2_unknown_node marker = {
455                        .magic =        cpu_to_je16(JFFS2_MAGIC_BITMASK),
456                        .nodetype =     cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
457                        .totlen =       cpu_to_je32(c->cleanmarker_size)
458                };
459
460                jffs2_prealloc_raw_node_refs(c, jeb, 1);
461
462                marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
463
464                vecs[0].iov_base = (unsigned char *) &marker;
465                vecs[0].iov_len = sizeof(marker);
466                ret = jffs2_flash_direct_writev(c, vecs, 1, jeb->offset, &retlen);
467
468                if (ret || retlen != sizeof(marker)) {
469                        if (ret)
470                                pr_warn("Write clean marker to block at 0x%08x failed: %d\n",
471                                       jeb->offset, ret);
472                        else
473                                pr_warn("Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
474                                       jeb->offset, sizeof(marker), retlen);
475
476                        goto filebad;
477                }
478        }
479        /* Everything else got zeroed before the erase */
480        jeb->free_size = c->sector_size;
481
482        mutex_lock(&c->erase_free_sem);
483        spin_lock(&c->erase_completion_lock);
484
485        c->erasing_size -= c->sector_size;
486        c->free_size += c->sector_size;
487
488        /* Account for cleanmarker now, if it's in-band */
489        if (c->cleanmarker_size && !jffs2_cleanmarker_oob(c))
490                jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
491
492        list_move_tail(&jeb->list, &c->free_list);
493        c->nr_erasing_blocks--;
494        c->nr_free_blocks++;
495
496        jffs2_dbg_acct_sanity_check_nolock(c, jeb);
497        jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
498
499        spin_unlock(&c->erase_completion_lock);
500        mutex_unlock(&c->erase_free_sem);
501        wake_up(&c->erase_wait);
502        return;
503
504filebad:
505        jffs2_erase_failed(c, jeb, bad_offset);
506        return;
507
508refile:
509        /* Stick it back on the list from whence it came and come back later */
510        mutex_lock(&c->erase_free_sem);
511        spin_lock(&c->erase_completion_lock);
512        jffs2_garbage_collect_trigger(c);
513        list_move(&jeb->list, &c->erase_complete_list);
514        spin_unlock(&c->erase_completion_lock);
515        mutex_unlock(&c->erase_free_sem);
516        return;
517}
Note: See TracBrowser for help on using the repository browser.