kernel: add missing patch
[oweals/openwrt.git] / target / linux / generic / pending-4.9 / 532-jffs2_eofdetect.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Subject: fs: jffs2: EOF marker
3
4 Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 ---
6  fs/jffs2/build.c | 10 ++++++++++
7  fs/jffs2/scan.c  | 21 +++++++++++++++++++--
8  2 files changed, 29 insertions(+), 2 deletions(-)
9
10 diff --git a/fs/jffs2/build.c b/fs/jffs2/build.c
11 index b288c8ae1236..e768f9d41661 100644
12 --- a/fs/jffs2/build.c
13 +++ b/fs/jffs2/build.c
14 @@ -117,6 +117,16 @@ static int jffs2_build_filesystem(struct jffs2_sb_info *c)
15         dbg_fsbuild("scanned flash completely\n");
16         jffs2_dbg_dump_block_lists_nolock(c);
17  
18 +       if (c->flags & (1 << 7)) {
19 +               printk("%s(): unlocking the mtd device... ", __func__);
20 +               mtd_unlock(c->mtd, 0, c->mtd->size);
21 +               printk("done.\n");
22 +
23 +               printk("%s(): erasing all blocks after the end marker... ", __func__);
24 +               jffs2_erase_pending_blocks(c, -1);
25 +               printk("done.\n");
26 +       }
27 +
28         dbg_fsbuild("pass 1 starting\n");
29         c->flags |= JFFS2_SB_FLAG_BUILDING;
30         /* Now scan the directory tree, increasing nlink according to every dirent found. */
31 diff --git a/fs/jffs2/scan.c b/fs/jffs2/scan.c
32 index 90431dd613b8..8c78ab19cdab 100644
33 --- a/fs/jffs2/scan.c
34 +++ b/fs/jffs2/scan.c
35 @@ -148,8 +148,14 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
36                 /* reset summary info for next eraseblock scan */
37                 jffs2_sum_reset_collected(s);
38  
39 -               ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset),
40 -                                               buf_size, s);
41 +               if (c->flags & (1 << 7)) {
42 +                       if (mtd_block_isbad(c->mtd, jeb->offset))
43 +                               ret = BLK_STATE_BADBLOCK;
44 +                       else
45 +                               ret = BLK_STATE_ALLFF;
46 +               } else
47 +                       ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset),
48 +                                                       buf_size, s);
49  
50                 if (ret < 0)
51                         goto out;
52 @@ -561,6 +567,17 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
53                         return err;
54         }
55  
56 +       if ((buf[0] == 0xde) &&
57 +               (buf[1] == 0xad) &&
58 +               (buf[2] == 0xc0) &&
59 +               (buf[3] == 0xde)) {
60 +               /* end of filesystem. erase everything after this point */
61 +               printk("%s(): End of filesystem marker found at 0x%x\n", __func__, jeb->offset);
62 +               c->flags |= (1 << 7);
63 +
64 +               return BLK_STATE_ALLFF;
65 +       }
66 +
67         /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
68         ofs = 0;
69         max_ofs = EMPTY_SCAN_SIZE(c->sector_size);
70 -- 
71 2.11.0
72