GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / mtd / inftlmount.c
blob75c5408dd1d026a4053fc7a5af8d978008a8a4bd
1 /*
2 * inftlmount.c -- INFTL mount code with extensive checks.
4 * Author: Greg Ungerer (gerg@snapgear.com)
5 * Copyright © 2002-2003, Greg Ungerer (gerg@snapgear.com)
7 * Based heavily on the nftlmount.c code which is:
8 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
9 * Copyright © 2000 Netgem S.A.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <asm/errno.h>
29 #include <asm/io.h>
30 #include <asm/uaccess.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/nftl.h>
36 #include <linux/mtd/inftl.h>
39 * find_boot_record: Find the INFTL Media Header and its Spare copy which
40 * contains the various device information of the INFTL partition and
41 * Bad Unit Table. Update the PUtable[] table according to the Bad
42 * Unit Table. PUtable[] is used for management of Erase Unit in
43 * other routines in inftlcore.c and inftlmount.c.
45 static int find_boot_record(struct INFTLrecord *inftl)
47 struct inftl_unittail h1;
48 //struct inftl_oob oob;
49 unsigned int i, block;
50 u8 buf[SECTORSIZE];
51 struct INFTLMediaHeader *mh = &inftl->MediaHdr;
52 struct mtd_info *mtd = inftl->mbd.mtd;
53 struct INFTLPartition *ip;
54 size_t retlen;
56 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: find_boot_record(inftl=%p)\n", inftl);
59 * Assume logical EraseSize == physical erasesize for starting the
60 * scan. We'll sort it out later if we find a MediaHeader which says
61 * otherwise.
63 inftl->EraseSize = inftl->mbd.mtd->erasesize;
64 inftl->nb_blocks = (u32)inftl->mbd.mtd->size / inftl->EraseSize;
66 inftl->MediaUnit = BLOCK_NIL;
68 /* Search for a valid boot record */
69 for (block = 0; block < inftl->nb_blocks; block++) {
70 int ret;
73 * Check for BNAND header first. Then whinge if it's found
74 * but later checks fail.
76 ret = mtd->read(mtd, block * inftl->EraseSize,
77 SECTORSIZE, &retlen, buf);
78 /* We ignore ret in case the ECC of the MediaHeader is invalid
79 (which is apparently acceptable) */
80 if (retlen != SECTORSIZE) {
81 static int warncount = 5;
83 if (warncount) {
84 printk(KERN_WARNING "INFTL: block read at 0x%x "
85 "of mtd%d failed: %d\n",
86 block * inftl->EraseSize,
87 inftl->mbd.mtd->index, ret);
88 if (!--warncount)
89 printk(KERN_WARNING "INFTL: further "
90 "failures for this block will "
91 "not be printed\n");
93 continue;
96 if (retlen < 6 || memcmp(buf, "BNAND", 6)) {
97 /* BNAND\0 not found. Continue */
98 continue;
101 /* To be safer with BIOS, also use erase mark as discriminant */
102 ret = inftl_read_oob(mtd,
103 block * inftl->EraseSize + SECTORSIZE + 8,
104 8, &retlen,(char *)&h1);
105 if (ret < 0) {
106 printk(KERN_WARNING "INFTL: ANAND header found at "
107 "0x%x in mtd%d, but OOB data read failed "
108 "(err %d)\n", block * inftl->EraseSize,
109 inftl->mbd.mtd->index, ret);
110 continue;
115 * This is the first we've seen.
116 * Copy the media header structure into place.
118 memcpy(mh, buf, sizeof(struct INFTLMediaHeader));
120 /* Read the spare media header at offset 4096 */
121 mtd->read(mtd, block * inftl->EraseSize + 4096,
122 SECTORSIZE, &retlen, buf);
123 if (retlen != SECTORSIZE) {
124 printk(KERN_WARNING "INFTL: Unable to read spare "
125 "Media Header\n");
126 return -1;
128 /* Check if this one is the same as the first one we found. */
129 if (memcmp(mh, buf, sizeof(struct INFTLMediaHeader))) {
130 printk(KERN_WARNING "INFTL: Primary and spare Media "
131 "Headers disagree.\n");
132 return -1;
135 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
136 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
137 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
138 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
139 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
140 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
142 #ifdef CONFIG_MTD_DEBUG_VERBOSE
143 if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
144 printk("INFTL: Media Header ->\n"
145 " bootRecordID = %s\n"
146 " NoOfBootImageBlocks = %d\n"
147 " NoOfBinaryPartitions = %d\n"
148 " NoOfBDTLPartitions = %d\n"
149 " BlockMultiplerBits = %d\n"
150 " FormatFlgs = %d\n"
151 " OsakVersion = 0x%x\n"
152 " PercentUsed = %d\n",
153 mh->bootRecordID, mh->NoOfBootImageBlocks,
154 mh->NoOfBinaryPartitions,
155 mh->NoOfBDTLPartitions,
156 mh->BlockMultiplierBits, mh->FormatFlags,
157 mh->OsakVersion, mh->PercentUsed);
159 #endif
161 if (mh->NoOfBDTLPartitions == 0) {
162 printk(KERN_WARNING "INFTL: Media Header sanity check "
163 "failed: NoOfBDTLPartitions (%d) == 0, "
164 "must be at least 1\n", mh->NoOfBDTLPartitions);
165 return -1;
168 if ((mh->NoOfBDTLPartitions + mh->NoOfBinaryPartitions) > 4) {
169 printk(KERN_WARNING "INFTL: Media Header sanity check "
170 "failed: Total Partitions (%d) > 4, "
171 "BDTL=%d Binary=%d\n", mh->NoOfBDTLPartitions +
172 mh->NoOfBinaryPartitions,
173 mh->NoOfBDTLPartitions,
174 mh->NoOfBinaryPartitions);
175 return -1;
178 if (mh->BlockMultiplierBits > 1) {
179 printk(KERN_WARNING "INFTL: sorry, we don't support "
180 "UnitSizeFactor 0x%02x\n",
181 mh->BlockMultiplierBits);
182 return -1;
183 } else if (mh->BlockMultiplierBits == 1) {
184 printk(KERN_WARNING "INFTL: support for INFTL with "
185 "UnitSizeFactor 0x%02x is experimental\n",
186 mh->BlockMultiplierBits);
187 inftl->EraseSize = inftl->mbd.mtd->erasesize <<
188 mh->BlockMultiplierBits;
189 inftl->nb_blocks = (u32)inftl->mbd.mtd->size / inftl->EraseSize;
190 block >>= mh->BlockMultiplierBits;
193 /* Scan the partitions */
194 for (i = 0; (i < 4); i++) {
195 ip = &mh->Partitions[i];
196 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
197 ip->firstUnit = le32_to_cpu(ip->firstUnit);
198 ip->lastUnit = le32_to_cpu(ip->lastUnit);
199 ip->flags = le32_to_cpu(ip->flags);
200 ip->spareUnits = le32_to_cpu(ip->spareUnits);
201 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
203 #ifdef CONFIG_MTD_DEBUG_VERBOSE
204 if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
205 printk(" PARTITION[%d] ->\n"
206 " virtualUnits = %d\n"
207 " firstUnit = %d\n"
208 " lastUnit = %d\n"
209 " flags = 0x%x\n"
210 " spareUnits = %d\n",
211 i, ip->virtualUnits, ip->firstUnit,
212 ip->lastUnit, ip->flags,
213 ip->spareUnits);
215 #endif
217 if (ip->Reserved0 != ip->firstUnit) {
218 struct erase_info *instr = &inftl->instr;
220 instr->mtd = inftl->mbd.mtd;
223 * Most likely this is using the
224 * undocumented qiuck mount feature.
225 * We don't support that, we will need
226 * to erase the hidden block for full
227 * compatibility.
229 instr->addr = ip->Reserved0 * inftl->EraseSize;
230 instr->len = inftl->EraseSize;
231 mtd->erase(mtd, instr);
233 if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
234 printk(KERN_WARNING "INFTL: Media Header "
235 "Partition %d sanity check failed\n"
236 " firstUnit %d : lastUnit %d > "
237 "virtualUnits %d\n", i, ip->lastUnit,
238 ip->firstUnit, ip->Reserved0);
239 return -1;
241 if (ip->Reserved1 != 0) {
242 printk(KERN_WARNING "INFTL: Media Header "
243 "Partition %d sanity check failed: "
244 "Reserved1 %d != 0\n",
245 i, ip->Reserved1);
246 return -1;
249 if (ip->flags & INFTL_BDTL)
250 break;
253 if (i >= 4) {
254 printk(KERN_WARNING "INFTL: Media Header Partition "
255 "sanity check failed:\n No partition "
256 "marked as Disk Partition\n");
257 return -1;
260 inftl->nb_boot_blocks = ip->firstUnit;
261 inftl->numvunits = ip->virtualUnits;
262 if (inftl->numvunits > (inftl->nb_blocks -
263 inftl->nb_boot_blocks - 2)) {
264 printk(KERN_WARNING "INFTL: Media Header sanity check "
265 "failed:\n numvunits (%d) > nb_blocks "
266 "(%d) - nb_boot_blocks(%d) - 2\n",
267 inftl->numvunits, inftl->nb_blocks,
268 inftl->nb_boot_blocks);
269 return -1;
272 inftl->mbd.size = inftl->numvunits *
273 (inftl->EraseSize / SECTORSIZE);
276 * Block count is set to last used EUN (we won't need to keep
277 * any meta-data past that point).
279 inftl->firstEUN = ip->firstUnit;
280 inftl->lastEUN = ip->lastUnit;
281 inftl->nb_blocks = ip->lastUnit + 1;
283 /* Memory alloc */
284 inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
285 if (!inftl->PUtable) {
286 printk(KERN_WARNING "INFTL: allocation of PUtable "
287 "failed (%zd bytes)\n",
288 inftl->nb_blocks * sizeof(u16));
289 return -ENOMEM;
292 inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
293 if (!inftl->VUtable) {
294 kfree(inftl->PUtable);
295 printk(KERN_WARNING "INFTL: allocation of VUtable "
296 "failed (%zd bytes)\n",
297 inftl->nb_blocks * sizeof(u16));
298 return -ENOMEM;
301 /* Mark the blocks before INFTL MediaHeader as reserved */
302 for (i = 0; i < inftl->nb_boot_blocks; i++)
303 inftl->PUtable[i] = BLOCK_RESERVED;
304 /* Mark all remaining blocks as potentially containing data */
305 for (; i < inftl->nb_blocks; i++)
306 inftl->PUtable[i] = BLOCK_NOTEXPLORED;
308 /* Mark this boot record (NFTL MediaHeader) block as reserved */
309 inftl->PUtable[block] = BLOCK_RESERVED;
311 /* Read Bad Erase Unit Table and modify PUtable[] accordingly */
312 for (i = 0; i < inftl->nb_blocks; i++) {
313 int physblock;
314 /* If any of the physical eraseblocks are bad, don't
315 use the unit. */
316 for (physblock = 0; physblock < inftl->EraseSize; physblock += inftl->mbd.mtd->erasesize) {
317 if (inftl->mbd.mtd->block_isbad(inftl->mbd.mtd, i * inftl->EraseSize + physblock))
318 inftl->PUtable[i] = BLOCK_RESERVED;
322 inftl->MediaUnit = block;
323 return 0;
326 /* Not found. */
327 return -1;
330 static int memcmpb(void *a, int c, int n)
332 int i;
333 for (i = 0; i < n; i++) {
334 if (c != ((unsigned char *)a)[i])
335 return 1;
337 return 0;
341 * check_free_sector: check if a free sector is actually FREE,
342 * i.e. All 0xff in data and oob area.
344 static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
345 int len, int check_oob)
347 u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
348 struct mtd_info *mtd = inftl->mbd.mtd;
349 size_t retlen;
350 int i;
352 for (i = 0; i < len; i += SECTORSIZE) {
353 if (mtd->read(mtd, address, SECTORSIZE, &retlen, buf))
354 return -1;
355 if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
356 return -1;
358 if (check_oob) {
359 if(inftl_read_oob(mtd, address, mtd->oobsize,
360 &retlen, &buf[SECTORSIZE]) < 0)
361 return -1;
362 if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
363 return -1;
365 address += SECTORSIZE;
368 return 0;
372 * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
373 * Unit and Update INFTL metadata. Each erase operation is
374 * checked with check_free_sectors.
376 * Return: 0 when succeed, -1 on error.
378 * ToDo: 1. Is it neceressary to check_free_sector after erasing ??
380 int INFTL_formatblock(struct INFTLrecord *inftl, int block)
382 size_t retlen;
383 struct inftl_unittail uci;
384 struct erase_info *instr = &inftl->instr;
385 struct mtd_info *mtd = inftl->mbd.mtd;
386 int physblock;
388 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_formatblock(inftl=%p,"
389 "block=%d)\n", inftl, block);
391 memset(instr, 0, sizeof(struct erase_info));
394 /* Use async erase interface, test return code */
395 instr->mtd = inftl->mbd.mtd;
396 instr->addr = block * inftl->EraseSize;
397 instr->len = inftl->mbd.mtd->erasesize;
398 /* Erase one physical eraseblock at a time, even though the NAND api
399 allows us to group them. This way we if we have a failure, we can
400 mark only the failed block in the bbt. */
401 for (physblock = 0; physblock < inftl->EraseSize;
402 physblock += instr->len, instr->addr += instr->len) {
403 mtd->erase(inftl->mbd.mtd, instr);
405 if (instr->state == MTD_ERASE_FAILED) {
406 printk(KERN_WARNING "INFTL: error while formatting block %d\n",
407 block);
408 goto fail;
411 if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
412 goto fail;
415 uci.EraseMark = cpu_to_le16(ERASE_MARK);
416 uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
417 uci.Reserved[0] = 0;
418 uci.Reserved[1] = 0;
419 uci.Reserved[2] = 0;
420 uci.Reserved[3] = 0;
421 instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
422 if (inftl_write_oob(mtd, instr->addr + 8, 8, &retlen, (char *)&uci) < 0)
423 goto fail;
424 return 0;
425 fail:
426 /* could not format, update the bad block table (caller is responsible
427 for setting the PUtable to BLOCK_RESERVED on failure) */
428 inftl->mbd.mtd->block_markbad(inftl->mbd.mtd, instr->addr);
429 return -1;
433 * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
434 * Units in a Virtual Unit Chain, i.e. all the units are disconnected.
436 * Since the chain is invalid then we will have to erase it from its
437 * head (normally for INFTL we go from the oldest). But if it has a
438 * loop then there is no oldest...
440 static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
442 unsigned int block = first_block, block1;
444 printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
445 first_block);
447 for (;;) {
448 block1 = inftl->PUtable[block];
450 printk(KERN_WARNING "INFTL: formatting block %d\n", block);
451 if (INFTL_formatblock(inftl, block) < 0) {
453 * Cannot format !!!! Mark it as Bad Unit,
455 inftl->PUtable[block] = BLOCK_RESERVED;
456 } else {
457 inftl->PUtable[block] = BLOCK_FREE;
460 /* Goto next block on the chain */
461 block = block1;
463 if (block == BLOCK_NIL || block >= inftl->lastEUN)
464 break;
468 void INFTL_dumptables(struct INFTLrecord *s)
470 int i;
472 printk("-------------------------------------------"
473 "----------------------------------\n");
475 printk("VUtable[%d] ->", s->nb_blocks);
476 for (i = 0; i < s->nb_blocks; i++) {
477 if ((i % 8) == 0)
478 printk("\n%04x: ", i);
479 printk("%04x ", s->VUtable[i]);
482 printk("\n-------------------------------------------"
483 "----------------------------------\n");
485 printk("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
486 for (i = 0; i <= s->lastEUN; i++) {
487 if ((i % 8) == 0)
488 printk("\n%04x: ", i);
489 printk("%04x ", s->PUtable[i]);
492 printk("\n-------------------------------------------"
493 "----------------------------------\n");
495 printk("INFTL ->\n"
496 " EraseSize = %d\n"
497 " h/s/c = %d/%d/%d\n"
498 " numvunits = %d\n"
499 " firstEUN = %d\n"
500 " lastEUN = %d\n"
501 " numfreeEUNs = %d\n"
502 " LastFreeEUN = %d\n"
503 " nb_blocks = %d\n"
504 " nb_boot_blocks = %d",
505 s->EraseSize, s->heads, s->sectors, s->cylinders,
506 s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
507 s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
509 printk("\n-------------------------------------------"
510 "----------------------------------\n");
513 void INFTL_dumpVUchains(struct INFTLrecord *s)
515 int logical, block, i;
517 printk("-------------------------------------------"
518 "----------------------------------\n");
520 printk("INFTL Virtual Unit Chains:\n");
521 for (logical = 0; logical < s->nb_blocks; logical++) {
522 block = s->VUtable[logical];
523 if (block > s->nb_blocks)
524 continue;
525 printk(" LOGICAL %d --> %d ", logical, block);
526 for (i = 0; i < s->nb_blocks; i++) {
527 if (s->PUtable[block] == BLOCK_NIL)
528 break;
529 block = s->PUtable[block];
530 printk("%d ", block);
532 printk("\n");
535 printk("-------------------------------------------"
536 "----------------------------------\n");
539 int INFTL_mount(struct INFTLrecord *s)
541 struct mtd_info *mtd = s->mbd.mtd;
542 unsigned int block, first_block, prev_block, last_block;
543 unsigned int first_logical_block, logical_block, erase_mark;
544 int chain_length, do_format_chain;
545 struct inftl_unithead1 h0;
546 struct inftl_unittail h1;
547 size_t retlen;
548 int i;
549 u8 *ANACtable, ANAC;
551 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_mount(inftl=%p)\n", s);
553 /* Search for INFTL MediaHeader and Spare INFTL Media Header */
554 if (find_boot_record(s) < 0) {
555 printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
556 return -ENXIO;
559 /* Init the logical to physical table */
560 for (i = 0; i < s->nb_blocks; i++)
561 s->VUtable[i] = BLOCK_NIL;
563 logical_block = block = BLOCK_NIL;
565 /* Temporary buffer to store ANAC numbers. */
566 ANACtable = kcalloc(s->nb_blocks, sizeof(u8), GFP_KERNEL);
567 if (!ANACtable) {
568 printk(KERN_WARNING "INFTL: allocation of ANACtable "
569 "failed (%zd bytes)\n",
570 s->nb_blocks * sizeof(u8));
571 return -ENOMEM;
575 * First pass is to explore each physical unit, and construct the
576 * virtual chains that exist (newest physical unit goes into VUtable).
577 * Any block that is in any way invalid will be left in the
578 * NOTEXPLORED state. Then at the end we will try to format it and
579 * mark it as free.
581 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 1, explore each unit\n");
582 for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
583 if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
584 continue;
586 do_format_chain = 0;
587 first_logical_block = BLOCK_NIL;
588 last_block = BLOCK_NIL;
589 block = first_block;
591 for (chain_length = 0; ; chain_length++) {
593 if ((chain_length == 0) &&
594 (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
595 /* Nothing to do here, onto next block */
596 break;
599 if (inftl_read_oob(mtd, block * s->EraseSize + 8,
600 8, &retlen, (char *)&h0) < 0 ||
601 inftl_read_oob(mtd, block * s->EraseSize +
602 2 * SECTORSIZE + 8, 8, &retlen,
603 (char *)&h1) < 0) {
604 /* Should never happen? */
605 do_format_chain++;
606 break;
609 logical_block = le16_to_cpu(h0.virtualUnitNo);
610 prev_block = le16_to_cpu(h0.prevUnitNo);
611 erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
612 ANACtable[block] = h0.ANAC;
614 /* Previous block is relative to start of Partition */
615 if (prev_block < s->nb_blocks)
616 prev_block += s->firstEUN;
618 /* Already explored partial chain? */
619 if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
620 /* Check if chain for this logical */
621 if (logical_block == first_logical_block) {
622 if (last_block != BLOCK_NIL)
623 s->PUtable[last_block] = block;
625 break;
628 /* Check for invalid block */
629 if (erase_mark != ERASE_MARK) {
630 printk(KERN_WARNING "INFTL: corrupt block %d "
631 "in chain %d, chain length %d, erase "
632 "mark 0x%x?\n", block, first_block,
633 chain_length, erase_mark);
635 * Assume end of chain, probably incomplete
636 * fold/erase...
638 if (chain_length == 0)
639 do_format_chain++;
640 break;
643 /* Check for it being free already then... */
644 if ((logical_block == BLOCK_FREE) ||
645 (logical_block == BLOCK_NIL)) {
646 s->PUtable[block] = BLOCK_FREE;
647 break;
650 /* Sanity checks on block numbers */
651 if ((logical_block >= s->nb_blocks) ||
652 ((prev_block >= s->nb_blocks) &&
653 (prev_block != BLOCK_NIL))) {
654 if (chain_length > 0) {
655 printk(KERN_WARNING "INFTL: corrupt "
656 "block %d in chain %d?\n",
657 block, first_block);
658 do_format_chain++;
660 break;
663 if (first_logical_block == BLOCK_NIL) {
664 first_logical_block = logical_block;
665 } else {
666 if (first_logical_block != logical_block) {
667 /* Normal for folded chain... */
668 break;
673 * Current block is valid, so if we followed a virtual
674 * chain to get here then we can set the previous
675 * block pointer in our PUtable now. Then move onto
676 * the previous block in the chain.
678 s->PUtable[block] = BLOCK_NIL;
679 if (last_block != BLOCK_NIL)
680 s->PUtable[last_block] = block;
681 last_block = block;
682 block = prev_block;
684 /* Check for end of chain */
685 if (block == BLOCK_NIL)
686 break;
688 /* Validate next block before following it... */
689 if (block > s->lastEUN) {
690 printk(KERN_WARNING "INFTL: invalid previous "
691 "block %d in chain %d?\n", block,
692 first_block);
693 do_format_chain++;
694 break;
698 if (do_format_chain) {
699 format_chain(s, first_block);
700 continue;
704 * Looks like a valid chain then. It may not really be the
705 * newest block in the chain, but it is the newest we have
706 * found so far. We might update it in later iterations of
707 * this loop if we find something newer.
709 s->VUtable[first_logical_block] = first_block;
710 logical_block = BLOCK_NIL;
713 #ifdef CONFIG_MTD_DEBUG_VERBOSE
714 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
715 INFTL_dumptables(s);
716 #endif
719 * Second pass, check for infinite loops in chains. These are
720 * possible because we don't update the previous pointers when
721 * we fold chains. No big deal, just fix them up in PUtable.
723 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 2, validate virtual chains\n");
724 for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
725 block = s->VUtable[logical_block];
726 last_block = BLOCK_NIL;
728 /* Check for free/reserved/nil */
729 if (block >= BLOCK_RESERVED)
730 continue;
732 ANAC = ANACtable[block];
733 for (i = 0; i < s->numvunits; i++) {
734 if (s->PUtable[block] == BLOCK_NIL)
735 break;
736 if (s->PUtable[block] > s->lastEUN) {
737 printk(KERN_WARNING "INFTL: invalid prev %d, "
738 "in virtual chain %d\n",
739 s->PUtable[block], logical_block);
740 s->PUtable[block] = BLOCK_NIL;
743 if (ANACtable[block] != ANAC) {
745 * Chain must point back to itself. This is ok,
746 * but we will need adjust the tables with this
747 * newest block and oldest block.
749 s->VUtable[logical_block] = block;
750 s->PUtable[last_block] = BLOCK_NIL;
751 break;
754 ANAC--;
755 last_block = block;
756 block = s->PUtable[block];
759 if (i >= s->nb_blocks) {
761 * Uhoo, infinite chain with valid ANACS!
762 * Format whole chain...
764 format_chain(s, first_block);
768 #ifdef CONFIG_MTD_DEBUG_VERBOSE
769 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
770 INFTL_dumptables(s);
771 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
772 INFTL_dumpVUchains(s);
773 #endif
776 * Third pass, format unreferenced blocks and init free block count.
778 s->numfreeEUNs = 0;
779 s->LastFreeEUN = BLOCK_NIL;
781 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 3, format unused blocks\n");
782 for (block = s->firstEUN; block <= s->lastEUN; block++) {
783 if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
784 printk("INFTL: unreferenced block %d, formatting it\n",
785 block);
786 if (INFTL_formatblock(s, block) < 0)
787 s->PUtable[block] = BLOCK_RESERVED;
788 else
789 s->PUtable[block] = BLOCK_FREE;
791 if (s->PUtable[block] == BLOCK_FREE) {
792 s->numfreeEUNs++;
793 if (s->LastFreeEUN == BLOCK_NIL)
794 s->LastFreeEUN = block;
798 kfree(ANACtable);
799 return 0;