blob: a06d47a688c717e2158d0962a5f7a54663018b57 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
David Woodhousec00c3102007-04-25 14:16:47 +010011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/sched.h>
14#include <linux/slab.h>
15#include <linux/mtd/mtd.h>
16#include <linux/pagemap.h>
17#include <linux/crc32.h>
18#include <linux/compiler.h>
19#include "nodelist.h"
Ferenc Havasie631ddb2005-09-07 09:35:26 +010020#include "summary.h"
21#include "debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Joakim Tjernlund41bdc602010-10-07 19:09:34 +020023#define DEFAULT_EMPTY_SCAN_SIZE 256
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define noisy_printk(noise, args...) do { \
26 if (*(noise)) { \
27 printk(KERN_NOTICE args); \
28 (*(noise))--; \
29 if (!(*(noise))) { \
30 printk(KERN_NOTICE "Further such events for this erase block will not be printed\n"); \
31 } \
32 } \
33} while(0)
34
35static uint32_t pseudo_random;
36
37static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasie631ddb2005-09-07 09:35:26 +010038 unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000040/* These helper functions _must_ increase ofs and also do the dirty/used space accounting.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * Returning an error will abort the mount - bad checksums etc. should just mark the space
42 * as dirty.
43 */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000044static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasie631ddb2005-09-07 09:35:26 +010045 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasie631ddb2005-09-07 09:35:26 +010047 struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static inline int min_free(struct jffs2_sb_info *c)
50{
51 uint32_t min = 2 * sizeof(struct jffs2_raw_inode);
Andrew Victor2f82ce12005-02-09 09:24:26 +000052#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (!jffs2_can_mark_obsolete(c) && min < c->wbuf_pagesize)
54 return c->wbuf_pagesize;
55#endif
56 return min;
57
58}
Andrew Victor3be36672005-02-09 09:09:05 +000059
60static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) {
61 if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
62 return sector_size;
63 else
64 return DEFAULT_EMPTY_SCAN_SIZE;
65}
66
David Woodhouse25090a62006-05-21 03:57:56 +010067static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
68{
David Woodhousea6a8bef2006-05-29 00:41:11 +010069 int ret;
70
71 if ((ret = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
72 return ret;
73 if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
David Woodhouse25090a62006-05-21 03:57:56 +010074 return ret;
75 /* Turned wasted size into dirty, since we apparently
76 think it's recoverable now. */
77 jeb->dirty_size += jeb->wasted_size;
78 c->dirty_size += jeb->wasted_size;
79 c->wasted_size -= jeb->wasted_size;
80 jeb->wasted_size = 0;
81 if (VERYDIRTY(c, jeb->dirty_size)) {
82 list_add(&jeb->list, &c->very_dirty_list);
83 } else {
84 list_add(&jeb->list, &c->dirty_list);
85 }
86 return 0;
87}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089int jffs2_scan_medium(struct jffs2_sb_info *c)
90{
91 int i, ret;
92 uint32_t empty_blocks = 0, bad_blocks = 0;
93 unsigned char *flashbuf = NULL;
94 uint32_t buf_size = 0;
Ferenc Havasie631ddb2005-09-07 09:35:26 +010095 struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#ifndef __ECOS
Grant Erickson1ddd0d92011-04-08 08:51:34 -070097 size_t pointlen, try_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Artem Bityutskiyf0265452012-01-30 15:08:26 +020099 ret = mtd_point(c->mtd, 0, c->mtd->size, &pointlen,
100 (void **)&flashbuf, NULL);
101 if (!ret && pointlen < c->mtd->size) {
102 /* Don't muck about if it won't let us point to the whole flash */
103 D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", pointlen));
104 mtd_unpoint(c->mtd, 0, pointlen);
105 flashbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Artem Bityutskiyf0265452012-01-30 15:08:26 +0200107 if (ret && ret != -EOPNOTSUPP)
108 D1(printk(KERN_DEBUG "MTD point failed %d\n", ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#endif
110 if (!flashbuf) {
111 /* For NAND it's quicker to read a whole eraseblock at a time,
112 apparently */
113 if (jffs2_cleanmarker_oob(c))
Grant Erickson1ddd0d92011-04-08 08:51:34 -0700114 try_size = c->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 else
Grant Erickson1ddd0d92011-04-08 08:51:34 -0700116 try_size = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Grant Erickson1ddd0d92011-04-08 08:51:34 -0700118 D1(printk(KERN_DEBUG "Trying to allocate readbuf of %zu "
119 "bytes\n", try_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Grant Erickson1ddd0d92011-04-08 08:51:34 -0700121 flashbuf = mtd_kmalloc_up_to(c->mtd, &try_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (!flashbuf)
123 return -ENOMEM;
Grant Erickson1ddd0d92011-04-08 08:51:34 -0700124
125 D1(printk(KERN_DEBUG "Allocated readbuf of %zu bytes\n",
126 try_size));
127
128 buf_size = (uint32_t)try_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100131 if (jffs2_sum_active()) {
Yan Burman3d375d92006-12-04 15:03:01 -0800132 s = kzalloc(sizeof(struct jffs2_summary), GFP_KERNEL);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100133 if (!s) {
134 JFFS2_WARNING("Can't allocate memory for summary\n");
David Woodhouse48396412009-06-23 01:34:19 +0100135 ret = -ENOMEM;
136 goto out;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100137 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100138 }
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 for (i=0; i<c->nr_blocks; i++) {
141 struct jffs2_eraseblock *jeb = &c->blocks[i];
142
Artem Bityutskiya2166b92006-12-28 12:01:41 +0200143 cond_resched();
144
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100145 /* reset summary info for next eraseblock scan */
146 jffs2_sum_reset_collected(s);
147
148 ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset),
149 buf_size, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if (ret < 0)
152 goto out;
153
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100154 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /* Now decide which list to put it on */
157 switch(ret) {
158 case BLK_STATE_ALLFF:
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000159 /*
160 * Empty block. Since we can't be sure it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * was entirely erased, we just queue it for erase
162 * again. It will be marked as such when the erase
163 * is complete. Meanwhile we still count it as empty
164 * for later checks.
165 */
166 empty_blocks++;
167 list_add(&jeb->list, &c->erase_pending_list);
168 c->nr_erasing_blocks++;
169 break;
170
171 case BLK_STATE_CLEANMARKER:
172 /* Only a CLEANMARKER node is valid */
173 if (!jeb->dirty_size) {
174 /* It's actually free */
175 list_add(&jeb->list, &c->free_list);
176 c->nr_free_blocks++;
177 } else {
178 /* Dirt */
179 D1(printk(KERN_DEBUG "Adding all-dirty block at 0x%08x to erase_pending_list\n", jeb->offset));
180 list_add(&jeb->list, &c->erase_pending_list);
181 c->nr_erasing_blocks++;
182 }
183 break;
184
185 case BLK_STATE_CLEAN:
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100186 /* Full (or almost full) of clean data. Clean list */
187 list_add(&jeb->list, &c->clean_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 break;
189
190 case BLK_STATE_PARTDIRTY:
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100191 /* Some data, but not full. Dirty list. */
192 /* We want to remember the block with most free space
193 and stick it in the 'nextblock' position to start writing to it. */
194 if (jeb->free_size > min_free(c) &&
195 (!c->nextblock || c->nextblock->free_size < jeb->free_size)) {
196 /* Better candidate for the next writes to go to */
197 if (c->nextblock) {
David Woodhouse25090a62006-05-21 03:57:56 +0100198 ret = file_dirty(c, c->nextblock);
199 if (ret)
Christian Engelmayera2ab0ce2009-06-13 23:06:29 +0200200 goto out;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100201 /* deleting summary information of the old nextblock */
202 jffs2_sum_reset_collected(c->summary);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
David Woodhouse25090a62006-05-21 03:57:56 +0100204 /* update collected summary information for the current nextblock */
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100205 jffs2_sum_move_collected(c, s);
206 D1(printk(KERN_DEBUG "jffs2_scan_medium(): new nextblock = 0x%08x\n", jeb->offset));
207 c->nextblock = jeb;
208 } else {
David Woodhouse25090a62006-05-21 03:57:56 +0100209 ret = file_dirty(c, jeb);
210 if (ret)
Christian Engelmayera2ab0ce2009-06-13 23:06:29 +0200211 goto out;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 break;
214
215 case BLK_STATE_ALLDIRTY:
216 /* Nothing valid - not even a clean marker. Needs erasing. */
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100217 /* For now we just put it on the erasing list. We'll start the erases later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 D1(printk(KERN_NOTICE "JFFS2: Erase block at 0x%08x is not formatted. It will be erased\n", jeb->offset));
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100219 list_add(&jeb->list, &c->erase_pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 c->nr_erasing_blocks++;
221 break;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 case BLK_STATE_BADBLOCK:
224 D1(printk(KERN_NOTICE "JFFS2: Block at 0x%08x is bad\n", jeb->offset));
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100225 list_add(&jeb->list, &c->bad_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 c->bad_size += c->sector_size;
227 c->free_size -= c->sector_size;
228 bad_blocks++;
229 break;
230 default:
231 printk(KERN_WARNING "jffs2_scan_medium(): unknown block state\n");
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100232 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* Nextblock dirty is always seen as wasted, because we cannot recycle it now */
237 if (c->nextblock && (c->nextblock->dirty_size)) {
238 c->nextblock->wasted_size += c->nextblock->dirty_size;
239 c->wasted_size += c->nextblock->dirty_size;
240 c->dirty_size -= c->nextblock->dirty_size;
241 c->nextblock->dirty_size = 0;
242 }
Andrew Victor2f82ce12005-02-09 09:24:26 +0000243#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
David Woodhousee96fb232006-03-07 21:55:36 -0800244 if (!jffs2_can_mark_obsolete(c) && c->wbuf_pagesize && c->nextblock && (c->nextblock->free_size % c->wbuf_pagesize)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000245 /* If we're going to start writing into a block which already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 contains data, and the end of the data isn't page-aligned,
247 skip a little and align it. */
248
Artem B. Bityutskiydaba5cc2005-09-30 14:59:17 +0100249 uint32_t skip = c->nextblock->free_size % c->wbuf_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 D1(printk(KERN_DEBUG "jffs2_scan_medium(): Skipping %d bytes in nextblock to ensure page alignment\n",
252 skip));
David Woodhouse046b8b92006-05-25 01:50:35 +0100253 jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
David Woodhousef5609282006-05-25 01:37:28 +0100254 jffs2_scan_dirty_space(c, c->nextblock, skip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256#endif
257 if (c->nr_erasing_blocks) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000258 if ( !c->used_size && ((c->nr_free_blocks+empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 printk(KERN_NOTICE "Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
260 printk(KERN_NOTICE "empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",empty_blocks,bad_blocks,c->nr_blocks);
261 ret = -EIO;
262 goto out;
263 }
David Woodhouseae3b6ba2010-05-19 17:05:14 +0100264 spin_lock(&c->erase_completion_lock);
265 jffs2_garbage_collect_trigger(c);
266 spin_unlock(&c->erase_completion_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 ret = 0;
269 out:
270 if (buf_size)
271 kfree(flashbuf);
272#ifndef __ECOS
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000273 else
Artem Bityutskiy72197782011-12-23 17:05:52 +0200274 mtd_unpoint(c->mtd, 0, c->mtd->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
Jesper Juhle8a0e412011-06-13 22:16:44 +0200276 kfree(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return ret;
278}
279
Adrian Bunkc05d52c2006-06-22 12:03:35 +0200280static int jffs2_fill_scan_buf(struct jffs2_sb_info *c, void *buf,
281 uint32_t ofs, uint32_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 int ret;
284 size_t retlen;
285
286 ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
287 if (ret) {
288 D1(printk(KERN_WARNING "mtd->read(0x%x bytes from 0x%x) returned %d\n", len, ofs, ret));
289 return ret;
290 }
291 if (retlen < len) {
292 D1(printk(KERN_WARNING "Read at 0x%x gave only 0x%zx bytes\n", ofs, retlen));
293 return -EIO;
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296}
297
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100298int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
299{
300 if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size
David Woodhouse99988f72006-05-24 09:04:17 +0100301 && (!jeb->first_node || !ref_next(jeb->first_node)) )
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100302 return BLK_STATE_CLEANMARKER;
303
304 /* move blocks with max 4 byte dirty space to cleanlist */
305 else if (!ISDIRTY(c->sector_size - (jeb->used_size + jeb->unchecked_size))) {
306 c->dirty_size -= jeb->dirty_size;
307 c->wasted_size += jeb->dirty_size;
308 jeb->wasted_size += jeb->dirty_size;
309 jeb->dirty_size = 0;
310 return BLK_STATE_CLEAN;
311 } else if (jeb->used_size || jeb->unchecked_size)
312 return BLK_STATE_PARTDIRTY;
313 else
314 return BLK_STATE_ALLDIRTY;
315}
316
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900317#ifdef CONFIG_JFFS2_FS_XATTR
318static int jffs2_scan_xattr_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
319 struct jffs2_raw_xattr *rx, uint32_t ofs,
320 struct jffs2_summary *s)
321{
322 struct jffs2_xattr_datum *xd;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900323 uint32_t xid, version, totlen, crc;
David Woodhouse68270992006-05-21 03:46:05 +0100324 int err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900325
326 crc = crc32(0, rx, sizeof(struct jffs2_raw_xattr) - 4);
327 if (crc != je32_to_cpu(rx->node_crc)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900328 JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
329 ofs, je32_to_cpu(rx->node_crc), crc);
David Woodhouse68270992006-05-21 03:46:05 +0100330 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
331 return err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900332 return 0;
333 }
334
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900335 xid = je32_to_cpu(rx->xid);
336 version = je32_to_cpu(rx->version);
337
KaiGai Kohei8a136952006-06-24 09:14:13 +0900338 totlen = PAD(sizeof(struct jffs2_raw_xattr)
339 + rx->name_len + 1 + je16_to_cpu(rx->value_len));
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900340 if (totlen != je32_to_cpu(rx->totlen)) {
341 JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%u\n",
342 ofs, je32_to_cpu(rx->totlen), totlen);
David Woodhouse68270992006-05-21 03:46:05 +0100343 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
344 return err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900345 return 0;
346 }
347
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900348 xd = jffs2_setup_xattr_datum(c, xid, version);
349 if (IS_ERR(xd))
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900350 return PTR_ERR(xd);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900351
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900352 if (xd->version > version) {
353 struct jffs2_raw_node_ref *raw
354 = jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, NULL);
355 raw->next_in_ino = xd->node->next_in_ino;
356 xd->node->next_in_ino = raw;
357 } else {
358 xd->version = version;
359 xd->xprefix = rx->xprefix;
360 xd->name_len = rx->name_len;
361 xd->value_len = je16_to_cpu(rx->value_len);
362 xd->data_crc = je32_to_cpu(rx->data_crc);
363
364 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, (void *)xd);
365 }
David Woodhousef1f96712006-05-20 19:45:26 +0100366
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900367 if (jffs2_sum_active())
368 jffs2_sum_add_xattr_mem(s, rx, ofs - jeb->offset);
Masanari Iida3e341742012-01-28 00:17:28 +0900369 dbg_xattr("scanning xdatum at %#08x (xid=%u, version=%u)\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900370 ofs, xd->xid, xd->version);
371 return 0;
372}
373
374static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
375 struct jffs2_raw_xref *rr, uint32_t ofs,
376 struct jffs2_summary *s)
377{
378 struct jffs2_xattr_ref *ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900379 uint32_t crc;
David Woodhouse68270992006-05-21 03:46:05 +0100380 int err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900381
382 crc = crc32(0, rr, sizeof(*rr) - 4);
383 if (crc != je32_to_cpu(rr->node_crc)) {
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900384 JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
385 ofs, je32_to_cpu(rr->node_crc), crc);
David Woodhouse68270992006-05-21 03:46:05 +0100386 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rr->totlen)))))
387 return err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900388 return 0;
389 }
390
391 if (PAD(sizeof(struct jffs2_raw_xref)) != je32_to_cpu(rr->totlen)) {
David Woodhouse89291a92006-05-25 13:30:24 +0100392 JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%zd\n",
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900393 ofs, je32_to_cpu(rr->totlen),
394 PAD(sizeof(struct jffs2_raw_xref)));
David Woodhouse68270992006-05-21 03:46:05 +0100395 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rr->totlen))))
396 return err;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900397 return 0;
398 }
399
400 ref = jffs2_alloc_xattr_ref();
401 if (!ref)
402 return -ENOMEM;
403
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900404 /* BEFORE jffs2_build_xattr_subsystem() called,
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900405 * and AFTER xattr_ref is marked as a dead xref,
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900406 * ref->xid is used to store 32bit xid, xd is not used
407 * ref->ino is used to store 32bit inode-number, ic is not used
408 * Thoes variables are declared as union, thus using those
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900409 * are exclusive. In a similar way, ref->next is temporarily
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900410 * used to chain all xattr_ref object. It's re-chained to
411 * jffs2_inode_cache in jffs2_build_xattr_subsystem() correctly.
412 */
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900413 ref->ino = je32_to_cpu(rr->ino);
414 ref->xid = je32_to_cpu(rr->xid);
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900415 ref->xseqno = je32_to_cpu(rr->xseqno);
416 if (ref->xseqno > c->highest_xseqno)
417 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
KaiGai Kohei8f2b6f42006-05-13 15:15:07 +0900418 ref->next = c->xref_temp;
419 c->xref_temp = ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900420
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900421 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(rr->totlen)), (void *)ref);
David Woodhousef1f96712006-05-20 19:45:26 +0100422
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900423 if (jffs2_sum_active())
424 jffs2_sum_add_xref_mem(s, rr, ofs - jeb->offset);
425 dbg_xattr("scan xref at %#08x (xid=%u, ino=%u)\n",
426 ofs, ref->xid, ref->ino);
427 return 0;
428}
429#endif
430
David Woodhouse9641b782006-05-20 16:13:34 +0100431/* Called with 'buf_size == 0' if buf is in fact a pointer _directly_ into
432 the flash, XIP-style */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
David Woodhouse9641b782006-05-20 16:13:34 +0100434 unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct jffs2_unknown_node *node;
436 struct jffs2_unknown_node crcnode;
Joakim Tjernlund41bdc602010-10-07 19:09:34 +0200437 uint32_t ofs, prevofs, max_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 uint32_t hdr_crc, buf_ofs, buf_len;
439 int err;
440 int noise = 0;
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100441
442
Andrew Victor2f82ce12005-02-09 09:24:26 +0000443#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 int cleanmarkerfound = 0;
445#endif
446
447 ofs = jeb->offset;
448 prevofs = jeb->offset - 1;
449
450 D1(printk(KERN_DEBUG "jffs2_scan_eraseblock(): Scanning block at 0x%x\n", ofs));
451
Andrew Victor2f82ce12005-02-09 09:24:26 +0000452#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (jffs2_cleanmarker_oob(c)) {
Artem Bityutskiya7a6ace12007-01-31 11:38:53 +0200454 int ret;
455
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200456 if (mtd_block_isbad(c->mtd, jeb->offset))
Artem Bityutskiya7a6ace12007-01-31 11:38:53 +0200457 return BLK_STATE_BADBLOCK;
458
459 ret = jffs2_check_nand_cleanmarker(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 D2(printk(KERN_NOTICE "jffs_check_nand_cleanmarker returned %d\n",ret));
Artem Bityutskiya7a6ace12007-01-31 11:38:53 +0200461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* Even if it's not found, we still scan to see
463 if the block is empty. We use this information
464 to decide whether to erase it or not. */
465 switch (ret) {
466 case 0: cleanmarkerfound = 1; break;
467 case 1: break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 default: return ret;
469 }
470 }
471#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100472
473 if (jffs2_sum_active()) {
David Woodhouse9641b782006-05-20 16:13:34 +0100474 struct jffs2_sum_marker *sm;
475 void *sumptr = NULL;
476 uint32_t sumlen;
477
478 if (!buf_size) {
479 /* XIP case. Just look, point at the summary if it's there */
David Woodhouse13ba42d2006-05-30 08:59:34 +0100480 sm = (void *)buf + c->sector_size - sizeof(*sm);
David Woodhouse9641b782006-05-20 16:13:34 +0100481 if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
482 sumptr = buf + je32_to_cpu(sm->offset);
483 sumlen = c->sector_size - je32_to_cpu(sm->offset);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100484 }
David Woodhouse9641b782006-05-20 16:13:34 +0100485 } else {
486 /* If NAND flash, read a whole page of it. Else just the end */
487 if (c->wbuf_pagesize)
488 buf_len = c->wbuf_pagesize;
489 else
490 buf_len = sizeof(*sm);
491
492 /* Read as much as we want into the _end_ of the preallocated buffer */
493 err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
494 jeb->offset + c->sector_size - buf_len,
495 buf_len);
496 if (err)
497 return err;
498
499 sm = (void *)buf + buf_size - sizeof(*sm);
500 if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
501 sumlen = c->sector_size - je32_to_cpu(sm->offset);
502 sumptr = buf + buf_size - sumlen;
503
504 /* Now, make sure the summary itself is available */
505 if (sumlen > buf_size) {
506 /* Need to kmalloc for this. */
507 sumptr = kmalloc(sumlen, GFP_KERNEL);
508 if (!sumptr)
509 return -ENOMEM;
510 memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
511 }
512 if (buf_len < sumlen) {
513 /* Need to read more so that the entire summary node is present */
514 err = jffs2_fill_scan_buf(c, sumptr,
515 jeb->offset + c->sector_size - sumlen,
516 sumlen - buf_len);
517 if (err)
518 return err;
519 }
520 }
521
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100522 }
523
David Woodhouse9641b782006-05-20 16:13:34 +0100524 if (sumptr) {
525 err = jffs2_sum_scan_sumnode(c, jeb, sumptr, sumlen, &pseudo_random);
David Woodhouse35601602006-05-21 01:28:05 +0100526
David Woodhouse9641b782006-05-20 16:13:34 +0100527 if (buf_size && sumlen > buf_size)
528 kfree(sumptr);
David Woodhouse35601602006-05-21 01:28:05 +0100529 /* If it returns with a real error, bail.
530 If it returns positive, that's a block classification
531 (i.e. BLK_STATE_xxx) so return that too.
532 If it returns zero, fall through to full scan. */
533 if (err)
534 return err;
David Woodhouse9641b782006-05-20 16:13:34 +0100535 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100536 }
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 buf_ofs = jeb->offset;
539
540 if (!buf_size) {
David Woodhouse9641b782006-05-20 16:13:34 +0100541 /* This is the XIP case -- we're reading _directly_ from the flash chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 buf_len = c->sector_size;
543 } else {
Andrew Victor3be36672005-02-09 09:09:05 +0000544 buf_len = EMPTY_SCAN_SIZE(c->sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
546 if (err)
547 return err;
548 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
551 ofs = 0;
Joakim Tjernlund41bdc602010-10-07 19:09:34 +0200552 max_ofs = EMPTY_SCAN_SIZE(c->sector_size);
553 /* Scan only EMPTY_SCAN_SIZE of 0xFF before declaring it's empty */
554 while(ofs < max_ofs && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ofs += 4;
556
Joakim Tjernlund41bdc602010-10-07 19:09:34 +0200557 if (ofs == max_ofs) {
Andrew Victor2f82ce12005-02-09 09:24:26 +0000558#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (jffs2_cleanmarker_oob(c)) {
560 /* scan oob, take care of cleanmarker */
561 int ret = jffs2_check_oob_empty(c, jeb, cleanmarkerfound);
562 D2(printk(KERN_NOTICE "jffs2_check_oob_empty returned %d\n",ret));
563 switch (ret) {
564 case 0: return cleanmarkerfound ? BLK_STATE_CLEANMARKER : BLK_STATE_ALLFF;
565 case 1: return BLK_STATE_ALLDIRTY;
566 default: return ret;
567 }
568 }
569#endif
570 D1(printk(KERN_DEBUG "Block at 0x%08x is empty (erased)\n", jeb->offset));
Andrew Victor8f15fd52005-02-09 09:17:45 +0000571 if (c->cleanmarker_size == 0)
572 return BLK_STATE_CLEANMARKER; /* don't bother with re-erase */
573 else
574 return BLK_STATE_ALLFF; /* OK to erase if all blocks are like this */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576 if (ofs) {
577 D1(printk(KERN_DEBUG "Free space at %08x ends at %08x\n", jeb->offset,
578 jeb->offset + ofs));
David Woodhousea6a8bef2006-05-29 00:41:11 +0100579 if ((err = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
580 return err;
David Woodhouse68270992006-05-21 03:46:05 +0100581 if ((err = jffs2_scan_dirty_space(c, jeb, ofs)))
582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
585 /* Now ofs is a complete physical flash offset as it always was... */
586 ofs += jeb->offset;
587
588 noise = 10;
589
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100590 dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100591
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000592scan_more:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 while(ofs < jeb->offset + c->sector_size) {
594
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100595 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
David Woodhouse2f785402006-05-24 02:04:45 +0100597 /* Make sure there are node refs available for use */
David Woodhouse046b8b92006-05-25 01:50:35 +0100598 err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
David Woodhouse2f785402006-05-24 02:04:45 +0100599 if (err)
600 return err;
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 cond_resched();
603
604 if (ofs & 3) {
605 printk(KERN_WARNING "Eep. ofs 0x%08x not word-aligned!\n", ofs);
606 ofs = PAD(ofs);
607 continue;
608 }
609 if (ofs == prevofs) {
610 printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
David Woodhouse68270992006-05-21 03:46:05 +0100611 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
612 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 ofs += 4;
614 continue;
615 }
616 prevofs = ofs;
617
618 if (jeb->offset + c->sector_size < ofs + sizeof(*node)) {
619 D1(printk(KERN_DEBUG "Fewer than %zd bytes left to end of block. (%x+%x<%x+%zx) Not reading\n", sizeof(struct jffs2_unknown_node),
620 jeb->offset, c->sector_size, ofs, sizeof(*node)));
David Woodhouse68270992006-05-21 03:46:05 +0100621 if ((err = jffs2_scan_dirty_space(c, jeb, (jeb->offset + c->sector_size)-ofs)))
622 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
624 }
625
626 if (buf_ofs + buf_len < ofs + sizeof(*node)) {
627 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
628 D1(printk(KERN_DEBUG "Fewer than %zd bytes (node header) left to end of buf. Reading 0x%x at 0x%08x\n",
629 sizeof(struct jffs2_unknown_node), buf_len, ofs));
630 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
631 if (err)
632 return err;
633 buf_ofs = ofs;
634 }
635
636 node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
637
638 if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
639 uint32_t inbuf_ofs;
Joakim Tjernlundc2aecda2007-03-27 13:32:09 +0200640 uint32_t empty_start, scan_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 empty_start = ofs;
643 ofs += 4;
Joakim Tjernlundc2aecda2007-03-27 13:32:09 +0200644 scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(c->sector_size)/8, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 D1(printk(KERN_DEBUG "Found empty flash at 0x%08x\n", ofs));
647 more_empty:
648 inbuf_ofs = ofs - buf_ofs;
Joakim Tjernlundc2aecda2007-03-27 13:32:09 +0200649 while (inbuf_ofs < scan_end) {
650 if (unlikely(*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 printk(KERN_WARNING "Empty flash at 0x%08x ends at 0x%08x\n",
652 empty_start, ofs);
David Woodhouse68270992006-05-21 03:46:05 +0100653 if ((err = jffs2_scan_dirty_space(c, jeb, ofs-empty_start)))
654 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 goto scan_more;
656 }
657
658 inbuf_ofs+=4;
659 ofs += 4;
660 }
661 /* Ran off end. */
662 D1(printk(KERN_DEBUG "Empty flash to end of buffer at 0x%08x\n", ofs));
663
664 /* If we're only checking the beginning of a block with a cleanmarker,
665 bail now */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000666 if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) &&
David Woodhouse99988f72006-05-24 09:04:17 +0100667 c->cleanmarker_size && !jeb->dirty_size && !ref_next(jeb->first_node)) {
Andrew Victor3be36672005-02-09 09:09:05 +0000668 D1(printk(KERN_DEBUG "%d bytes at start of block seems clean... assuming all clean\n", EMPTY_SCAN_SIZE(c->sector_size)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return BLK_STATE_CLEANMARKER;
670 }
Joakim Tjernlundc2aecda2007-03-27 13:32:09 +0200671 if (!buf_size && (scan_end != buf_len)) {/* XIP/point case */
672 scan_end = buf_len;
673 goto more_empty;
674 }
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* See how much more there is to read in this eraseblock... */
677 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
678 if (!buf_len) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000679 /* No more to read. Break out of main loop without marking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 this range of empty space as dirty (because it's not) */
681 D1(printk(KERN_DEBUG "Empty flash at %08x runs to end of block. Treating as free_space\n",
682 empty_start));
683 break;
684 }
Joakim Tjernlundc2aecda2007-03-27 13:32:09 +0200685 /* point never reaches here */
686 scan_end = buf_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 D1(printk(KERN_DEBUG "Reading another 0x%x at 0x%08x\n", buf_len, ofs));
688 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
689 if (err)
690 return err;
691 buf_ofs = ofs;
692 goto more_empty;
693 }
694
695 if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) {
696 printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs);
David Woodhouse68270992006-05-21 03:46:05 +0100697 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
698 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 ofs += 4;
700 continue;
701 }
702 if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
703 D1(printk(KERN_DEBUG "Dirty bitmask at 0x%08x\n", ofs));
David Woodhouse68270992006-05-21 03:46:05 +0100704 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
705 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 ofs += 4;
707 continue;
708 }
709 if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) {
710 printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs);
711 printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n");
David Woodhouse68270992006-05-21 03:46:05 +0100712 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
713 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ofs += 4;
715 continue;
716 }
717 if (je16_to_cpu(node->magic) != JFFS2_MAGIC_BITMASK) {
718 /* OK. We're out of possibilities. Whinge and move on */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000719 noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
720 JFFS2_MAGIC_BITMASK, ofs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 je16_to_cpu(node->magic));
David Woodhouse68270992006-05-21 03:46:05 +0100722 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
723 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 ofs += 4;
725 continue;
726 }
727 /* We seem to have a node of sorts. Check the CRC */
728 crcnode.magic = node->magic;
729 crcnode.nodetype = cpu_to_je16( je16_to_cpu(node->nodetype) | JFFS2_NODE_ACCURATE);
730 crcnode.totlen = node->totlen;
731 hdr_crc = crc32(0, &crcnode, sizeof(crcnode)-4);
732
733 if (hdr_crc != je32_to_cpu(node->hdr_crc)) {
734 noisy_printk(&noise, "jffs2_scan_eraseblock(): Node at 0x%08x {0x%04x, 0x%04x, 0x%08x) has invalid CRC 0x%08x (calculated 0x%08x)\n",
735 ofs, je16_to_cpu(node->magic),
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000736 je16_to_cpu(node->nodetype),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 je32_to_cpu(node->totlen),
738 je32_to_cpu(node->hdr_crc),
739 hdr_crc);
David Woodhouse68270992006-05-21 03:46:05 +0100740 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
741 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 ofs += 4;
743 continue;
744 }
745
Joakim Tjernlund0dec4c82007-03-10 17:08:44 +0100746 if (ofs + je32_to_cpu(node->totlen) > jeb->offset + c->sector_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* Eep. Node goes over the end of the erase block. */
748 printk(KERN_WARNING "Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
749 ofs, je32_to_cpu(node->totlen));
750 printk(KERN_WARNING "Perhaps the file system was created with the wrong erase size?\n");
David Woodhouse68270992006-05-21 03:46:05 +0100751 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 ofs += 4;
754 continue;
755 }
756
757 if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) {
758 /* Wheee. This is an obsoleted node */
759 D2(printk(KERN_DEBUG "Node at 0x%08x is obsolete. Skipping\n", ofs));
David Woodhouse68270992006-05-21 03:46:05 +0100760 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
761 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ofs += PAD(je32_to_cpu(node->totlen));
763 continue;
764 }
765
766 switch(je16_to_cpu(node->nodetype)) {
767 case JFFS2_NODETYPE_INODE:
768 if (buf_ofs + buf_len < ofs + sizeof(struct jffs2_raw_inode)) {
769 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
770 D1(printk(KERN_DEBUG "Fewer than %zd bytes (inode node) left to end of buf. Reading 0x%x at 0x%08x\n",
771 sizeof(struct jffs2_raw_inode), buf_len, ofs));
772 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
773 if (err)
774 return err;
775 buf_ofs = ofs;
776 node = (void *)buf;
777 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100778 err = jffs2_scan_inode_node(c, jeb, (void *)node, ofs, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (err) return err;
780 ofs += PAD(je32_to_cpu(node->totlen));
781 break;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 case JFFS2_NODETYPE_DIRENT:
784 if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
785 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
786 D1(printk(KERN_DEBUG "Fewer than %d bytes (dirent node) left to end of buf. Reading 0x%x at 0x%08x\n",
787 je32_to_cpu(node->totlen), buf_len, ofs));
788 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
789 if (err)
790 return err;
791 buf_ofs = ofs;
792 node = (void *)buf;
793 }
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100794 err = jffs2_scan_dirent_node(c, jeb, (void *)node, ofs, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (err) return err;
796 ofs += PAD(je32_to_cpu(node->totlen));
797 break;
798
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900799#ifdef CONFIG_JFFS2_FS_XATTR
800 case JFFS2_NODETYPE_XATTR:
801 if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
802 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
803 D1(printk(KERN_DEBUG "Fewer than %d bytes (xattr node)"
804 " left to end of buf. Reading 0x%x at 0x%08x\n",
805 je32_to_cpu(node->totlen), buf_len, ofs));
806 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
807 if (err)
808 return err;
809 buf_ofs = ofs;
810 node = (void *)buf;
811 }
812 err = jffs2_scan_xattr_node(c, jeb, (void *)node, ofs, s);
813 if (err)
814 return err;
815 ofs += PAD(je32_to_cpu(node->totlen));
816 break;
817 case JFFS2_NODETYPE_XREF:
818 if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
819 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
820 D1(printk(KERN_DEBUG "Fewer than %d bytes (xref node)"
821 " left to end of buf. Reading 0x%x at 0x%08x\n",
822 je32_to_cpu(node->totlen), buf_len, ofs));
823 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
824 if (err)
825 return err;
826 buf_ofs = ofs;
827 node = (void *)buf;
828 }
829 err = jffs2_scan_xref_node(c, jeb, (void *)node, ofs, s);
830 if (err)
831 return err;
832 ofs += PAD(je32_to_cpu(node->totlen));
833 break;
834#endif /* CONFIG_JFFS2_FS_XATTR */
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 case JFFS2_NODETYPE_CLEANMARKER:
837 D1(printk(KERN_DEBUG "CLEANMARKER node found at 0x%08x\n", ofs));
838 if (je32_to_cpu(node->totlen) != c->cleanmarker_size) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000839 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ofs, je32_to_cpu(node->totlen), c->cleanmarker_size);
David Woodhouse68270992006-05-21 03:46:05 +0100841 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
842 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 ofs += PAD(sizeof(struct jffs2_unknown_node));
844 } else if (jeb->first_node) {
845 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset);
David Woodhouse68270992006-05-21 03:46:05 +0100846 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
847 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 ofs += PAD(sizeof(struct jffs2_unknown_node));
849 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100850 jffs2_link_node_ref(c, jeb, ofs | REF_NORMAL, c->cleanmarker_size, NULL);
David Woodhousef1f96712006-05-20 19:45:26 +0100851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ofs += PAD(c->cleanmarker_size);
853 }
854 break;
855
856 case JFFS2_NODETYPE_PADDING:
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100857 if (jffs2_sum_active())
858 jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen));
David Woodhouse68270992006-05-21 03:46:05 +0100859 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
860 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ofs += PAD(je32_to_cpu(node->totlen));
862 break;
863
864 default:
865 switch (je16_to_cpu(node->nodetype) & JFFS2_COMPAT_MASK) {
866 case JFFS2_FEATURE_ROCOMPAT:
867 printk(KERN_NOTICE "Read-only compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
David Woodhouseef53cb02007-07-10 10:01:22 +0100868 c->flags |= JFFS2_SB_FLAG_RO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (!(jffs2_is_readonly(c)))
870 return -EROFS;
David Woodhouse68270992006-05-21 03:46:05 +0100871 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
872 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 ofs += PAD(je32_to_cpu(node->totlen));
874 break;
875
876 case JFFS2_FEATURE_INCOMPAT:
877 printk(KERN_NOTICE "Incompatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs);
878 return -EINVAL;
879
880 case JFFS2_FEATURE_RWCOMPAT_DELETE:
881 D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
David Woodhouse68270992006-05-21 03:46:05 +0100882 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
883 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 ofs += PAD(je32_to_cpu(node->totlen));
885 break;
886
David Woodhouse61715862006-05-21 00:02:06 +0100887 case JFFS2_FEATURE_RWCOMPAT_COPY: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
David Woodhouse61715862006-05-21 00:02:06 +0100889
David Woodhouse2f785402006-05-24 02:04:45 +0100890 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(node->totlen)), NULL);
David Woodhouse61715862006-05-21 00:02:06 +0100891
892 /* We can't summarise nodes we don't grok */
893 jffs2_sum_disable_collecting(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 ofs += PAD(je32_to_cpu(node->totlen));
895 break;
David Woodhouse61715862006-05-21 00:02:06 +0100896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 }
899 }
900
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100901 if (jffs2_sum_active()) {
902 if (PAD(s->sum_size + JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size) {
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100903 dbg_summary("There is not enough space for "
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100904 "summary information, disabling for this jeb!\n");
905 jffs2_sum_disable_collecting(s);
906 }
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
David Woodhouse8b9e9fe2006-05-25 01:53:09 +0100909 D1(printk(KERN_DEBUG "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
910 jeb->offset,jeb->free_size, jeb->dirty_size, jeb->unchecked_size, jeb->used_size, jeb->wasted_size));
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /* mark_node_obsolete can add to wasted !! */
913 if (jeb->wasted_size) {
914 jeb->dirty_size += jeb->wasted_size;
915 c->dirty_size += jeb->wasted_size;
916 c->wasted_size -= jeb->wasted_size;
917 jeb->wasted_size = 0;
918 }
919
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100920 return jffs2_scan_classify_jeb(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100923struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 struct jffs2_inode_cache *ic;
926
927 ic = jffs2_get_ino_cache(c, ino);
928 if (ic)
929 return ic;
930
931 if (ino > c->highest_ino)
932 c->highest_ino = ino;
933
934 ic = jffs2_alloc_inode_cache();
935 if (!ic) {
936 printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of inode cache failed\n");
937 return NULL;
938 }
939 memset(ic, 0, sizeof(*ic));
940
941 ic->ino = ino;
942 ic->nodes = (void *)ic;
943 jffs2_add_ino_cache(c, ic);
944 if (ino == 1)
David Woodhouse27c72b02008-05-01 18:47:17 +0100945 ic->pino_nlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return ic;
947}
948
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000949static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100950 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct jffs2_inode_cache *ic;
Thomas Gleixner53043002007-04-05 11:09:01 +0200953 uint32_t crc, ino = je32_to_cpu(ri->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 D1(printk(KERN_DEBUG "jffs2_scan_inode_node(): Node at 0x%08x\n", ofs));
956
957 /* We do very little here now. Just check the ino# to which we should attribute
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000958 this node; we can do all the CRC checking etc. later. There's a tradeoff here --
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 we used to scan the flash once only, reading everything we want from it into
960 memory, then building all our in-core data structures and freeing the extra
961 information. Now we allow the first part of the mount to complete a lot quicker,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000962 but we have to go _back_ to the flash in order to finish the CRC checking, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 Which means that the _full_ amount of time to get to proper write mode with GC
964 operational may actually be _longer_ than before. Sucks to be me. */
965
Thomas Gleixner53043002007-04-05 11:09:01 +0200966 /* Check the node CRC in any case. */
967 crc = crc32(0, ri, sizeof(*ri)-8);
968 if (crc != je32_to_cpu(ri->node_crc)) {
969 printk(KERN_NOTICE "jffs2_scan_inode_node(): CRC failed on "
970 "node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
971 ofs, je32_to_cpu(ri->node_crc), crc);
972 /*
973 * We believe totlen because the CRC on the node
974 * _header_ was OK, just the node itself failed.
975 */
976 return jffs2_scan_dirty_space(c, jeb,
977 PAD(je32_to_cpu(ri->totlen)));
978 }
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 ic = jffs2_get_ino_cache(c, ino);
981 if (!ic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ic = jffs2_scan_make_ino_cache(c, ino);
David Woodhouse2f785402006-05-24 02:04:45 +0100983 if (!ic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
987 /* Wheee. It worked */
David Woodhouse2f785402006-05-24 02:04:45 +0100988 jffs2_link_node_ref(c, jeb, ofs | REF_UNCHECKED, PAD(je32_to_cpu(ri->totlen)), ic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000990 D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
992 je32_to_cpu(ri->offset),
993 je32_to_cpu(ri->offset)+je32_to_cpu(ri->dsize)));
994
995 pseudo_random += je32_to_cpu(ri->version);
996
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100997 if (jffs2_sum_active()) {
998 jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
999 }
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return 0;
1002}
1003
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001004static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
Ferenc Havasie631ddb2005-09-07 09:35:26 +01001005 struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 struct jffs2_full_dirent *fd;
1008 struct jffs2_inode_cache *ic;
David Woodhouseb534e702007-10-13 11:35:58 +01001009 uint32_t checkedlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 uint32_t crc;
David Woodhouse68270992006-05-21 03:46:05 +01001011 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 D1(printk(KERN_DEBUG "jffs2_scan_dirent_node(): Node at 0x%08x\n", ofs));
1014
1015 /* We don't get here unless the node is still valid, so we don't have to
1016 mask in the ACCURATE bit any more. */
1017 crc = crc32(0, rd, sizeof(*rd)-8);
1018
1019 if (crc != je32_to_cpu(rd->node_crc)) {
1020 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
1021 ofs, je32_to_cpu(rd->node_crc), crc);
1022 /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
David Woodhouse68270992006-05-21 03:46:05 +01001023 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1024 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return 0;
1026 }
1027
1028 pseudo_random += je32_to_cpu(rd->version);
1029
David Woodhouseb534e702007-10-13 11:35:58 +01001030 /* Should never happen. Did. (OLPC trac #4184)*/
1031 checkedlen = strnlen(rd->name, rd->nsize);
1032 if (checkedlen < rd->nsize) {
1033 printk(KERN_ERR "Dirent at %08x has zeroes in name. Truncating to %d chars\n",
1034 ofs, checkedlen);
1035 }
1036 fd = jffs2_alloc_full_dirent(checkedlen+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!fd) {
1038 return -ENOMEM;
1039 }
David Woodhouseb534e702007-10-13 11:35:58 +01001040 memcpy(&fd->name, rd->name, checkedlen);
1041 fd->name[checkedlen] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 crc = crc32(0, fd->name, rd->nsize);
1044 if (crc != je32_to_cpu(rd->name_crc)) {
1045 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001046 ofs, je32_to_cpu(rd->name_crc), crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 D1(printk(KERN_NOTICE "Name for which CRC failed is (now) '%s', ino #%d\n", fd->name, je32_to_cpu(rd->ino)));
1048 jffs2_free_full_dirent(fd);
1049 /* FIXME: Why do we believe totlen? */
1050 /* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */
David Woodhouse68270992006-05-21 03:46:05 +01001051 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1052 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 0;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
1056 if (!ic) {
1057 jffs2_free_full_dirent(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -ENOMEM;
1059 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +00001060
David Woodhouse43dfa072007-06-29 13:39:57 +01001061 fd->raw = jffs2_link_node_ref(c, jeb, ofs | dirent_node_state(rd),
1062 PAD(je32_to_cpu(rd->totlen)), ic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 fd->next = NULL;
1065 fd->version = je32_to_cpu(rd->version);
1066 fd->ino = je32_to_cpu(rd->ino);
David Woodhouseb534e702007-10-13 11:35:58 +01001067 fd->nhash = full_name_hash(fd->name, checkedlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 fd->type = rd->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
1070
Ferenc Havasie631ddb2005-09-07 09:35:26 +01001071 if (jffs2_sum_active()) {
1072 jffs2_sum_add_dirent_mem(s, rd, ofs - jeb->offset);
1073 }
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return 0;
1076}
1077
1078static int count_list(struct list_head *l)
1079{
1080 uint32_t count = 0;
1081 struct list_head *tmp;
1082
1083 list_for_each(tmp, l) {
1084 count++;
1085 }
1086 return count;
1087}
1088
1089/* Note: This breaks if list_empty(head). I don't care. You
1090 might, if you copy this code and use it elsewhere :) */
1091static void rotate_list(struct list_head *head, uint32_t count)
1092{
1093 struct list_head *n = head->next;
1094
1095 list_del(head);
1096 while(count--) {
1097 n = n->next;
1098 }
1099 list_add(head, n);
1100}
1101
1102void jffs2_rotate_lists(struct jffs2_sb_info *c)
1103{
1104 uint32_t x;
1105 uint32_t rotateby;
1106
1107 x = count_list(&c->clean_list);
1108 if (x) {
1109 rotateby = pseudo_random % x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 rotate_list((&c->clean_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112
1113 x = count_list(&c->very_dirty_list);
1114 if (x) {
1115 rotateby = pseudo_random % x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 rotate_list((&c->very_dirty_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
1119 x = count_list(&c->dirty_list);
1120 if (x) {
1121 rotateby = pseudo_random % x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 rotate_list((&c->dirty_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
1125 x = count_list(&c->erasable_list);
1126 if (x) {
1127 rotateby = pseudo_random % x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 rotate_list((&c->erasable_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130
1131 if (c->nr_erasing_blocks) {
1132 rotateby = pseudo_random % c->nr_erasing_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 rotate_list((&c->erase_pending_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
1136 if (c->nr_free_blocks) {
1137 rotateby = pseudo_random % c->nr_free_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 rotate_list((&c->free_list), rotateby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140}