2 * inftlmount.c -- INFTL mount code with extensive checks.
4 * Author: Greg Ungerer (gerg@snapgear.com)
5 * (C) 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 (C) 2000 Netgem S.A.
11 * $Id: inftlmount.c,v 1.15 2004/11/05 21:55:55 kalev Exp $
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <asm/errno.h>
32 #include <asm/uaccess.h>
33 #include <linux/miscdevice.h>
34 #include <linux/pci.h>
35 #include <linux/delay.h>
36 #include <linux/slab.h>
37 #include <linux/sched.h>
38 #include <linux/init.h>
39 #include <linux/mtd/mtd.h>
40 #include <linux/mtd/nftl.h>
41 #include <linux/mtd/inftl.h>
42 #include <linux/mtd/compatmac.h>
44 char inftlmountrev[]="$Revision: 1.15 $";
47 * find_boot_record: Find the INFTL Media Header and its Spare copy which
48 * contains the various device information of the INFTL partition and
49 * Bad Unit Table. Update the PUtable[] table according to the Bad
50 * Unit Table. PUtable[] is used for management of Erase Unit in
51 * other routines in inftlcore.c and inftlmount.c.
53 static int find_boot_record(struct INFTLrecord *inftl)
55 struct inftl_unittail h1;
56 //struct inftl_oob oob;
57 unsigned int i, block;
59 struct INFTLMediaHeader *mh = &inftl->MediaHdr;
60 struct INFTLPartition *ip;
63 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: find_boot_record(inftl=%p)\n", inftl);
66 * Assume logical EraseSize == physical erasesize for starting the
67 * scan. We'll sort it out later if we find a MediaHeader which says
70 inftl->EraseSize = inftl->mbd.mtd->erasesize;
71 inftl->nb_blocks = inftl->mbd.mtd->size / inftl->EraseSize;
73 inftl->MediaUnit = BLOCK_NIL;
75 /* Search for a valid boot record */
76 for (block = 0; block < inftl->nb_blocks; block++) {
80 * Check for BNAND header first. Then whinge if it's found
81 * but later checks fail.
83 ret = MTD_READ(inftl->mbd.mtd, block * inftl->EraseSize,
84 SECTORSIZE, &retlen, buf);
85 /* We ignore ret in case the ECC of the MediaHeader is invalid
86 (which is apparently acceptable) */
87 if (retlen != SECTORSIZE) {
88 static int warncount = 5;
91 printk(KERN_WARNING "INFTL: block read at 0x%x "
92 "of mtd%d failed: %d\n",
93 block * inftl->EraseSize,
94 inftl->mbd.mtd->index, ret);
96 printk(KERN_WARNING "INFTL: further "
97 "failures for this block will "
103 if (retlen < 6 || memcmp(buf, "BNAND", 6)) {
104 /* BNAND\0 not found. Continue */
108 /* To be safer with BIOS, also use erase mark as discriminant */
109 if ((ret = MTD_READOOB(inftl->mbd.mtd, block * inftl->EraseSize +
110 SECTORSIZE + 8, 8, &retlen, (char *)&h1) < 0)) {
111 printk(KERN_WARNING "INFTL: ANAND header found at "
112 "0x%x in mtd%d, but OOB data read failed "
113 "(err %d)\n", block * inftl->EraseSize,
114 inftl->mbd.mtd->index, ret);
120 * This is the first we've seen.
121 * Copy the media header structure into place.
123 memcpy(mh, buf, sizeof(struct INFTLMediaHeader));
125 /* Read the spare media header at offset 4096 */
126 MTD_READ(inftl->mbd.mtd, block * inftl->EraseSize + 4096,
127 SECTORSIZE, &retlen, buf);
128 if (retlen != SECTORSIZE) {
129 printk(KERN_WARNING "INFTL: Unable to read spare "
133 /* Check if this one is the same as the first one we found. */
134 if (memcmp(mh, buf, sizeof(struct INFTLMediaHeader))) {
135 printk(KERN_WARNING "INFTL: Primary and spare Media "
136 "Headers disagree.\n");
140 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
141 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
142 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
143 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
144 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
145 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
147 #ifdef CONFIG_MTD_DEBUG_VERBOSE
148 if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
149 printk("INFTL: Media Header ->\n"
150 " bootRecordID = %s\n"
151 " NoOfBootImageBlocks = %d\n"
152 " NoOfBinaryPartitions = %d\n"
153 " NoOfBDTLPartitions = %d\n"
154 " BlockMultiplerBits = %d\n"
156 " OsakVersion = 0x%x\n"
157 " PercentUsed = %d\n",
158 mh->bootRecordID, mh->NoOfBootImageBlocks,
159 mh->NoOfBinaryPartitions,
160 mh->NoOfBDTLPartitions,
161 mh->BlockMultiplierBits, mh->FormatFlags,
162 mh->OsakVersion, mh->PercentUsed);
166 if (mh->NoOfBDTLPartitions == 0) {
167 printk(KERN_WARNING "INFTL: Media Header sanity check "
168 "failed: NoOfBDTLPartitions (%d) == 0, "
169 "must be at least 1\n", mh->NoOfBDTLPartitions);
173 if ((mh->NoOfBDTLPartitions + mh->NoOfBinaryPartitions) > 4) {
174 printk(KERN_WARNING "INFTL: Media Header sanity check "
175 "failed: Total Partitions (%d) > 4, "
176 "BDTL=%d Binary=%d\n", mh->NoOfBDTLPartitions +
177 mh->NoOfBinaryPartitions,
178 mh->NoOfBDTLPartitions,
179 mh->NoOfBinaryPartitions);
183 if (mh->BlockMultiplierBits > 1) {
184 printk(KERN_WARNING "INFTL: sorry, we don't support "
185 "UnitSizeFactor 0x%02x\n",
186 mh->BlockMultiplierBits);
188 } else if (mh->BlockMultiplierBits == 1) {
189 printk(KERN_WARNING "INFTL: support for INFTL with "
190 "UnitSizeFactor 0x%02x is experimental\n",
191 mh->BlockMultiplierBits);
192 inftl->EraseSize = inftl->mbd.mtd->erasesize <<
193 mh->BlockMultiplierBits;
194 inftl->nb_blocks = inftl->mbd.mtd->size / inftl->EraseSize;
195 block >>= mh->BlockMultiplierBits;
198 /* Scan the partitions */
199 for (i = 0; (i < 4); i++) {
200 ip = &mh->Partitions[i];
201 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
202 ip->firstUnit = le32_to_cpu(ip->firstUnit);
203 ip->lastUnit = le32_to_cpu(ip->lastUnit);
204 ip->flags = le32_to_cpu(ip->flags);
205 ip->spareUnits = le32_to_cpu(ip->spareUnits);
206 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
208 #ifdef CONFIG_MTD_DEBUG_VERBOSE
209 if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
210 printk(" PARTITION[%d] ->\n"
211 " virtualUnits = %d\n"
215 " spareUnits = %d\n",
216 i, ip->virtualUnits, ip->firstUnit,
217 ip->lastUnit, ip->flags,
222 if (ip->Reserved0 != ip->firstUnit) {
223 struct erase_info *instr = &inftl->instr;
225 instr->mtd = inftl->mbd.mtd;
228 * Most likely this is using the
229 * undocumented qiuck mount feature.
230 * We don't support that, we will need
231 * to erase the hidden block for full
234 instr->addr = ip->Reserved0 * inftl->EraseSize;
235 instr->len = inftl->EraseSize;
236 MTD_ERASE(inftl->mbd.mtd, instr);
238 if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
239 printk(KERN_WARNING "INFTL: Media Header "
240 "Partition %d sanity check failed\n"
241 " firstUnit %d : lastUnit %d > "
242 "virtualUnits %d\n", i, ip->lastUnit,
243 ip->firstUnit, ip->Reserved0);
246 if (ip->Reserved1 != 0) {
247 printk(KERN_WARNING "INFTL: Media Header "
248 "Partition %d sanity check failed: "
249 "Reserved1 %d != 0\n",
254 if (ip->flags & INFTL_BDTL)
259 printk(KERN_WARNING "INFTL: Media Header Partition "
260 "sanity check failed:\n No partition "
261 "marked as Disk Partition\n");
265 inftl->nb_boot_blocks = ip->firstUnit;
266 inftl->numvunits = ip->virtualUnits;
267 if (inftl->numvunits > (inftl->nb_blocks -
268 inftl->nb_boot_blocks - 2)) {
269 printk(KERN_WARNING "INFTL: Media Header sanity check "
270 "failed:\n numvunits (%d) > nb_blocks "
271 "(%d) - nb_boot_blocks(%d) - 2\n",
272 inftl->numvunits, inftl->nb_blocks,
273 inftl->nb_boot_blocks);
277 inftl->mbd.size = inftl->numvunits *
278 (inftl->EraseSize / SECTORSIZE);
281 * Block count is set to last used EUN (we won't need to keep
282 * any meta-data past that point).
284 inftl->firstEUN = ip->firstUnit;
285 inftl->lastEUN = ip->lastUnit;
286 inftl->nb_blocks = ip->lastUnit + 1;
289 inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
290 if (!inftl->PUtable) {
291 printk(KERN_WARNING "INFTL: allocation of PUtable "
292 "failed (%zd bytes)\n",
293 inftl->nb_blocks * sizeof(u16));
297 inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
298 if (!inftl->VUtable) {
299 kfree(inftl->PUtable);
300 printk(KERN_WARNING "INFTL: allocation of VUtable "
301 "failed (%zd bytes)\n",
302 inftl->nb_blocks * sizeof(u16));
306 /* Mark the blocks before INFTL MediaHeader as reserved */
307 for (i = 0; i < inftl->nb_boot_blocks; i++)
308 inftl->PUtable[i] = BLOCK_RESERVED;
309 /* Mark all remaining blocks as potentially containing data */
310 for (; i < inftl->nb_blocks; i++)
311 inftl->PUtable[i] = BLOCK_NOTEXPLORED;
313 /* Mark this boot record (NFTL MediaHeader) block as reserved */
314 inftl->PUtable[block] = BLOCK_RESERVED;
316 /* Read Bad Erase Unit Table and modify PUtable[] accordingly */
317 for (i = 0; i < inftl->nb_blocks; i++) {
319 /* If any of the physical eraseblocks are bad, don't
321 for (physblock = 0; physblock < inftl->EraseSize; physblock += inftl->mbd.mtd->erasesize) {
322 if (inftl->mbd.mtd->block_isbad(inftl->mbd.mtd, i * inftl->EraseSize + physblock))
323 inftl->PUtable[i] = BLOCK_RESERVED;
327 inftl->MediaUnit = block;
335 static int memcmpb(void *a, int c, int n)
338 for (i = 0; i < n; i++) {
339 if (c != ((unsigned char *)a)[i])
346 * check_free_sector: check if a free sector is actually FREE,
347 * i.e. All 0xff in data and oob area.
349 static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
350 int len, int check_oob)
352 u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
356 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: check_free_sectors(inftl=%p,"
357 "address=0x%x,len=%d,check_oob=%d)\n", inftl,
358 address, len, check_oob);
360 for (i = 0; i < len; i += SECTORSIZE) {
361 if (MTD_READECC(inftl->mbd.mtd, address, SECTORSIZE, &retlen, buf, &buf[SECTORSIZE], &inftl->oobinfo) < 0)
363 if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
367 if (memcmpb(buf + SECTORSIZE, 0xff, inftl->mbd.mtd->oobsize) != 0)
370 address += SECTORSIZE;
377 * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
378 * Unit and Update INFTL metadata. Each erase operation is
379 * checked with check_free_sectors.
381 * Return: 0 when succeed, -1 on error.
383 * ToDo: 1. Is it neceressary to check_free_sector after erasing ??
385 int INFTL_formatblock(struct INFTLrecord *inftl, int block)
388 struct inftl_unittail uci;
389 struct erase_info *instr = &inftl->instr;
392 instr->mtd = inftl->mbd.mtd;
394 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_formatblock(inftl=%p,"
395 "block=%d)\n", inftl, block);
397 memset(instr, 0, sizeof(struct erase_info));
399 /* FIXME: Shouldn't we be setting the 'discarded' flag to zero
402 /* Use async erase interface, test return code */
403 instr->addr = block * inftl->EraseSize;
404 instr->len = inftl->mbd.mtd->erasesize;
405 /* Erase one physical eraseblock at a time, even though the NAND api
406 allows us to group them. This way we if we have a failure, we can
407 mark only the failed block in the bbt. */
408 for (physblock = 0; physblock < inftl->EraseSize; physblock += instr->len, instr->addr += instr->len) {
409 MTD_ERASE(inftl->mbd.mtd, instr);
411 if (instr->state == MTD_ERASE_FAILED) {
412 printk(KERN_WARNING "INFTL: error while formatting block %d\n",
418 * Check the "freeness" of Erase Unit before updating metadata.
419 * FixMe: is this check really necessary? Since we have check the
420 * return code after the erase operation.
422 if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
426 uci.EraseMark = cpu_to_le16(ERASE_MARK);
427 uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
432 instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
433 if (MTD_WRITEOOB(inftl->mbd.mtd, instr->addr +
434 8, 8, &retlen, (char *)&uci) < 0)
438 /* could not format, update the bad block table (caller is responsible
439 for setting the PUtable to BLOCK_RESERVED on failure) */
440 inftl->mbd.mtd->block_markbad(inftl->mbd.mtd, instr->addr);
445 * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
446 * Units in a Virtual Unit Chain, i.e. all the units are disconnected.
448 * Since the chain is invalid then we will have to erase it from its
449 * head (normally for INFTL we go from the oldest). But if it has a
450 * loop then there is no oldest...
452 static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
454 unsigned int block = first_block, block1;
456 printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
460 block1 = inftl->PUtable[block];
462 printk(KERN_WARNING "INFTL: formatting block %d\n", block);
463 if (INFTL_formatblock(inftl, block) < 0) {
465 * Cannot format !!!! Mark it as Bad Unit,
467 inftl->PUtable[block] = BLOCK_RESERVED;
469 inftl->PUtable[block] = BLOCK_FREE;
472 /* Goto next block on the chain */
475 if (block == BLOCK_NIL || block >= inftl->lastEUN)
480 void INFTL_dumptables(struct INFTLrecord *s)
484 printk("-------------------------------------------"
485 "----------------------------------\n");
487 printk("VUtable[%d] ->", s->nb_blocks);
488 for (i = 0; i < s->nb_blocks; i++) {
490 printk("\n%04x: ", i);
491 printk("%04x ", s->VUtable[i]);
494 printk("\n-------------------------------------------"
495 "----------------------------------\n");
497 printk("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
498 for (i = 0; i <= s->lastEUN; i++) {
500 printk("\n%04x: ", i);
501 printk("%04x ", s->PUtable[i]);
504 printk("\n-------------------------------------------"
505 "----------------------------------\n");
509 " h/s/c = %d/%d/%d\n"
513 " numfreeEUNs = %d\n"
514 " LastFreeEUN = %d\n"
516 " nb_boot_blocks = %d",
517 s->EraseSize, s->heads, s->sectors, s->cylinders,
518 s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
519 s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
521 printk("\n-------------------------------------------"
522 "----------------------------------\n");
525 void INFTL_dumpVUchains(struct INFTLrecord *s)
527 int logical, block, i;
529 printk("-------------------------------------------"
530 "----------------------------------\n");
532 printk("INFTL Virtual Unit Chains:\n");
533 for (logical = 0; logical < s->nb_blocks; logical++) {
534 block = s->VUtable[logical];
535 if (block > s->nb_blocks)
537 printk(" LOGICAL %d --> %d ", logical, block);
538 for (i = 0; i < s->nb_blocks; i++) {
539 if (s->PUtable[block] == BLOCK_NIL)
541 block = s->PUtable[block];
542 printk("%d ", block);
547 printk("-------------------------------------------"
548 "----------------------------------\n");
551 int INFTL_mount(struct INFTLrecord *s)
553 unsigned int block, first_block, prev_block, last_block;
554 unsigned int first_logical_block, logical_block, erase_mark;
555 int chain_length, do_format_chain;
556 struct inftl_unithead1 h0;
557 struct inftl_unittail h1;
562 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_mount(inftl=%p)\n", s);
564 /* Search for INFTL MediaHeader and Spare INFTL Media Header */
565 if (find_boot_record(s) < 0) {
566 printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
570 /* Init the logical to physical table */
571 for (i = 0; i < s->nb_blocks; i++)
572 s->VUtable[i] = BLOCK_NIL;
574 logical_block = block = BLOCK_NIL;
576 /* Temporary buffer to store ANAC numbers. */
577 ANACtable = kmalloc(s->nb_blocks * sizeof(u8), GFP_KERNEL);
578 memset(ANACtable, 0, s->nb_blocks);
581 * First pass is to explore each physical unit, and construct the
582 * virtual chains that exist (newest physical unit goes into VUtable).
583 * Any block that is in any way invalid will be left in the
584 * NOTEXPLORED state. Then at the end we will try to format it and
587 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 1, explore each unit\n");
588 for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
589 if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
593 first_logical_block = BLOCK_NIL;
594 last_block = BLOCK_NIL;
597 for (chain_length = 0; ; chain_length++) {
599 if ((chain_length == 0) &&
600 (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
601 /* Nothing to do here, onto next block */
605 if (MTD_READOOB(s->mbd.mtd, block * s->EraseSize + 8,
606 8, &retlen, (char *)&h0) < 0 ||
607 MTD_READOOB(s->mbd.mtd, block * s->EraseSize +
608 2 * SECTORSIZE + 8, 8, &retlen, (char *)&h1) < 0) {
609 /* Should never happen? */
614 logical_block = le16_to_cpu(h0.virtualUnitNo);
615 prev_block = le16_to_cpu(h0.prevUnitNo);
616 erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
617 ANACtable[block] = h0.ANAC;
619 /* Previous block is relative to start of Partition */
620 if (prev_block < s->nb_blocks)
621 prev_block += s->firstEUN;
623 /* Already explored partial chain? */
624 if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
625 /* Check if chain for this logical */
626 if (logical_block == first_logical_block) {
627 if (last_block != BLOCK_NIL)
628 s->PUtable[last_block] = block;
633 /* Check for invalid block */
634 if (erase_mark != ERASE_MARK) {
635 printk(KERN_WARNING "INFTL: corrupt block %d "
636 "in chain %d, chain length %d, erase "
637 "mark 0x%x?\n", block, first_block,
638 chain_length, erase_mark);
640 * Assume end of chain, probably incomplete
643 if (chain_length == 0)
648 /* Check for it being free already then... */
649 if ((logical_block == BLOCK_FREE) ||
650 (logical_block == BLOCK_NIL)) {
651 s->PUtable[block] = BLOCK_FREE;
655 /* Sanity checks on block numbers */
656 if ((logical_block >= s->nb_blocks) ||
657 ((prev_block >= s->nb_blocks) &&
658 (prev_block != BLOCK_NIL))) {
659 if (chain_length > 0) {
660 printk(KERN_WARNING "INFTL: corrupt "
661 "block %d in chain %d?\n",
668 if (first_logical_block == BLOCK_NIL) {
669 first_logical_block = logical_block;
671 if (first_logical_block != logical_block) {
672 /* Normal for folded chain... */
678 * Current block is valid, so if we followed a virtual
679 * chain to get here then we can set the previous
680 * block pointer in our PUtable now. Then move onto
681 * the previous block in the chain.
683 s->PUtable[block] = BLOCK_NIL;
684 if (last_block != BLOCK_NIL)
685 s->PUtable[last_block] = block;
689 /* Check for end of chain */
690 if (block == BLOCK_NIL)
693 /* Validate next block before following it... */
694 if (block > s->lastEUN) {
695 printk(KERN_WARNING "INFTL: invalid previous "
696 "block %d in chain %d?\n", block,
703 if (do_format_chain) {
704 format_chain(s, first_block);
709 * Looks like a valid chain then. It may not really be the
710 * newest block in the chain, but it is the newest we have
711 * found so far. We might update it in later iterations of
712 * this loop if we find something newer.
714 s->VUtable[first_logical_block] = first_block;
715 logical_block = BLOCK_NIL;
718 #ifdef CONFIG_MTD_DEBUG_VERBOSE
719 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
724 * Second pass, check for infinite loops in chains. These are
725 * possible because we don't update the previous pointers when
726 * we fold chains. No big deal, just fix them up in PUtable.
728 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 2, validate virtual chains\n");
729 for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
730 block = s->VUtable[logical_block];
731 last_block = BLOCK_NIL;
733 /* Check for free/reserved/nil */
734 if (block >= BLOCK_RESERVED)
737 ANAC = ANACtable[block];
738 for (i = 0; i < s->numvunits; i++) {
739 if (s->PUtable[block] == BLOCK_NIL)
741 if (s->PUtable[block] > s->lastEUN) {
742 printk(KERN_WARNING "INFTL: invalid prev %d, "
743 "in virtual chain %d\n",
744 s->PUtable[block], logical_block);
745 s->PUtable[block] = BLOCK_NIL;
748 if (ANACtable[block] != ANAC) {
750 * Chain must point back to itself. This is ok,
751 * but we will need adjust the tables with this
752 * newest block and oldest block.
754 s->VUtable[logical_block] = block;
755 s->PUtable[last_block] = BLOCK_NIL;
761 block = s->PUtable[block];
764 if (i >= s->nb_blocks) {
766 * Uhoo, infinite chain with valid ANACS!
767 * Format whole chain...
769 format_chain(s, first_block);
773 #ifdef CONFIG_MTD_DEBUG_VERBOSE
774 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
776 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
777 INFTL_dumpVUchains(s);
781 * Third pass, format unreferenced blocks and init free block count.
784 s->LastFreeEUN = BLOCK_NIL;
786 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 3, format unused blocks\n");
787 for (block = s->firstEUN; block <= s->lastEUN; block++) {
788 if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
789 printk("INFTL: unreferenced block %d, formatting it\n",
791 if (INFTL_formatblock(s, block) < 0)
792 s->PUtable[block] = BLOCK_RESERVED;
794 s->PUtable[block] = BLOCK_FREE;
796 if (s->PUtable[block] == BLOCK_FREE) {
798 if (s->LastFreeEUN == BLOCK_NIL)
799 s->LastFreeEUN = block;