Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / drivers / mtd / inftlmount.c
blob042c4eeadf8ec6100b978a9e1b310fc25702306e
1 /*
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.11 2003/06/23 07:39:21 dwmw2 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>
31 #include <asm/io.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.11 $";
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, boot_record_count = 0;
58 u8 buf[SECTORSIZE];
59 struct INFTLMediaHeader *mh = &inftl->MediaHdr;
60 struct INFTLPartition *ip;
61 int retlen;
63 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: find_boot_record(inftl=0x%x)\n",
64 (int)inftl);
67 * Assume logical EraseSize == physical erasesize for starting the
68 * scan. We'll sort it out later if we find a MediaHeader which says
69 * otherwise.
71 inftl->EraseSize = inftl->mbd.mtd->erasesize;
72 inftl->nb_blocks = inftl->mbd.mtd->size / inftl->EraseSize;
74 inftl->MediaUnit = BLOCK_NIL;
75 inftl->SpareMediaUnit = BLOCK_NIL;
77 /* Search for a valid boot record */
78 for (block = 0; block < inftl->nb_blocks; block++) {
79 int ret;
82 * Check for BNAND header first. Then whinge if it's found
83 * but later checks fail.
85 if ((ret = MTD_READ(inftl->mbd.mtd, block * inftl->EraseSize,
86 SECTORSIZE, &retlen, buf))) {
87 static int warncount = 5;
89 if (warncount) {
90 printk(KERN_WARNING "INFTL: block read at 0x%x "
91 "of mtd%d failed: %d\n",
92 block * inftl->EraseSize,
93 inftl->mbd.mtd->index, ret);
94 if (!--warncount)
95 printk(KERN_WARNING "INFTL: further "
96 "failures for this block will "
97 "not be printed\n");
99 continue;
102 if (retlen < 6 || memcmp(buf, "BNAND", 6)) {
103 /* BNAND\0 not found. Continue */
104 continue;
107 /* To be safer with BIOS, also use erase mark as discriminant */
108 if ((ret = MTD_READOOB(inftl->mbd.mtd, block * inftl->EraseSize +
109 SECTORSIZE + 8, 8, &retlen, (char *)&h1) < 0)) {
110 printk(KERN_WARNING "INFTL: ANAND header found at "
111 "0x%x in mtd%d, but OOB data read failed "
112 "(err %d)\n", block * inftl->EraseSize,
113 inftl->mbd.mtd->index, ret);
114 continue;
117 if (boot_record_count) {
119 * We've already processed one. So we just check if
120 * this one is the same as the first one we found.
122 if (memcmp(mh, buf, sizeof(struct INFTLMediaHeader))) {
123 printk(KERN_WARNING "INFTL: Media Headers at "
124 "0x%x and 0x%x disagree.\n",
125 inftl->MediaUnit * inftl->EraseSize,
126 block * inftl->EraseSize);
127 return -1;
129 if (boot_record_count == 1)
130 inftl->SpareMediaUnit = block;
133 * Mark this boot record (INFTL MediaHeader) block as
134 * reserved.
136 inftl->PUtable[block] = BLOCK_RESERVED;
138 boot_record_count++;
139 continue;
143 * This is the first we've seen.
144 * Copy the media header structure into place.
146 memcpy(mh, buf, sizeof(struct INFTLMediaHeader));
147 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
148 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
149 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
150 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
151 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
152 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
154 #ifdef CONFIG_MTD_DEBUG_VERBOSE
155 if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
156 printk("INFTL: Media Header ->\n"
157 " bootRecordID = %s\n"
158 " NoOfBootImageBlocks = %d\n"
159 " NoOfBinaryPartitions = %d\n"
160 " NoOfBDTLPartitions = %d\n"
161 " BlockMultiplerBits = %d\n"
162 " FormatFlgs = %d\n"
163 " OsakVersion = 0x%x\n"
164 " PercentUsed = %d\n",
165 mh->bootRecordID, mh->NoOfBootImageBlocks,
166 mh->NoOfBinaryPartitions,
167 mh->NoOfBDTLPartitions,
168 mh->BlockMultiplierBits, mh->FormatFlags,
169 mh->OsakVersion, mh->PercentUsed);
171 #endif
173 if (mh->NoOfBDTLPartitions == 0) {
174 printk(KERN_WARNING "INFTL: Media Header sanity check "
175 "failed: NoOfBDTLPartitions (%d) == 0, "
176 "must be at least 1\n", mh->NoOfBDTLPartitions);
177 return -1;
180 if ((mh->NoOfBDTLPartitions + mh->NoOfBinaryPartitions) > 4) {
181 printk(KERN_WARNING "INFTL: Media Header sanity check "
182 "failed: Total Partitions (%d) > 4, "
183 "BDTL=%d Binary=%d\n", mh->NoOfBDTLPartitions +
184 mh->NoOfBinaryPartitions,
185 mh->NoOfBDTLPartitions,
186 mh->NoOfBinaryPartitions);
187 return -1;
190 if (mh->BlockMultiplierBits > 1) {
191 printk(KERN_WARNING "INFTL: sorry, we don't support "
192 "UnitSizeFactor 0x%02x\n",
193 mh->BlockMultiplierBits);
194 return -1;
195 } else if (mh->BlockMultiplierBits == 1) {
196 printk(KERN_WARNING "INFTL: support for INFTL with "
197 "UnitSizeFactor 0x%02x is experimental\n",
198 mh->BlockMultiplierBits);
199 inftl->EraseSize = inftl->mbd.mtd->erasesize <<
200 (0xff - mh->BlockMultiplierBits);
201 inftl->nb_blocks = inftl->mbd.mtd->size / inftl->EraseSize;
204 /* Scan the partitions */
205 for (i = 0; (i < 4); i++) {
206 ip = &mh->Partitions[i];
207 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
208 ip->firstUnit = le32_to_cpu(ip->firstUnit);
209 ip->lastUnit = le32_to_cpu(ip->lastUnit);
210 ip->flags = le32_to_cpu(ip->flags);
211 ip->spareUnits = le32_to_cpu(ip->spareUnits);
212 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
214 #ifdef CONFIG_MTD_DEBUG_VERBOSE
215 if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
216 printk(" PARTITION[%d] ->\n"
217 " virtualUnits = %d\n"
218 " firstUnit = %d\n"
219 " lastUnit = %d\n"
220 " flags = 0x%x\n"
221 " spareUnits = %d\n",
222 i, ip->virtualUnits, ip->firstUnit,
223 ip->lastUnit, ip->flags,
224 ip->spareUnits);
226 #endif
228 if (ip->Reserved0 != ip->firstUnit) {
229 struct erase_info *instr = &inftl->instr;
232 * Most likely this is using the
233 * undocumented qiuck mount feature.
234 * We don't support that, we will need
235 * to erase the hidden block for full
236 * compatibility.
238 instr->addr = ip->Reserved0 * inftl->EraseSize;
239 instr->len = inftl->EraseSize;
240 MTD_ERASE(inftl->mbd.mtd, instr);
242 if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
243 printk(KERN_WARNING "INFTL: Media Header "
244 "Partition %d sanity check failed\n"
245 " firstUnit %d : lastUnit %d > "
246 "virtualUnits %d\n", i, ip->lastUnit,
247 ip->firstUnit, ip->Reserved0);
248 return -1;
250 if (ip->Reserved1 != 0) {
251 printk(KERN_WARNING "INFTL: Media Header "
252 "Partition %d sanity check failed: "
253 "Reserved1 %d != 0\n",
254 i, ip->Reserved1);
255 return -1;
258 if (ip->flags & INFTL_BDTL)
259 break;
262 if (i >= 4) {
263 printk(KERN_WARNING "INFTL: Media Header Partition "
264 "sanity check failed:\n No partition "
265 "marked as Disk Partition\n");
266 return -1;
269 inftl->nb_boot_blocks = ip->firstUnit;
270 inftl->numvunits = ip->virtualUnits;
271 if (inftl->numvunits > (inftl->nb_blocks -
272 inftl->nb_boot_blocks - 2)) {
273 printk(KERN_WARNING "INFTL: Media Header sanity check "
274 "failed:\n numvunits (%d) > nb_blocks "
275 "(%d) - nb_boot_blocks(%d) - 2\n",
276 inftl->numvunits, inftl->nb_blocks,
277 inftl->nb_boot_blocks);
278 return -1;
281 inftl->mbd.size = inftl->numvunits *
282 (inftl->EraseSize / SECTORSIZE);
285 * Block count is set to last used EUN (we won't need to keep
286 * any meta-data past that point).
288 inftl->firstEUN = ip->firstUnit;
289 inftl->lastEUN = ip->lastUnit;
290 inftl->nb_blocks = ip->lastUnit + 1;
292 /* Memory alloc */
293 inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
294 if (!inftl->PUtable) {
295 printk(KERN_WARNING "INFTL: allocation of PUtable "
296 "failed (%d bytes)\n",
297 inftl->nb_blocks * sizeof(u16));
298 return -ENOMEM;
301 inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
302 if (!inftl->VUtable) {
303 kfree(inftl->PUtable);
304 printk(KERN_WARNING "INFTL: allocation of VUtable "
305 "failed (%d bytes)\n",
306 inftl->nb_blocks * sizeof(u16));
307 return -ENOMEM;
310 /* Mark the blocks before INFTL MediaHeader as reserved */
311 for (i = 0; i < inftl->nb_boot_blocks; i++)
312 inftl->PUtable[i] = BLOCK_RESERVED;
313 /* Mark all remaining blocks as potentially containing data */
314 for (; i < inftl->nb_blocks; i++)
315 inftl->PUtable[i] = BLOCK_NOTEXPLORED;
317 /* Mark this boot record (NFTL MediaHeader) block as reserved */
318 inftl->PUtable[block] = BLOCK_RESERVED;
320 #if 0
321 /* Read Bad Erase Unit Table and modify PUtable[] accordingly */
322 for (i = 0; i < inftl->nb_blocks; i++) {
323 if ((i & (SECTORSIZE - 1)) == 0) {
324 /* read one sector for every SECTORSIZE of blocks */
325 if ((ret = MTD_READECC(inftl->mbd.mtd,
326 block * inftl->EraseSize + i + SECTORSIZE,
327 SECTORSIZE, &retlen, buf,
328 (char *)&oob, NULL)) < 0) {
329 printk(KERN_WARNING "INFTL: read of "
330 "bad sector table failed "
331 "(err %d)\n", ret);
332 kfree(inftl->VUtable);
333 kfree(inftl->PUtable);
334 return -1;
337 /* Mark the Bad Erase Unit as RESERVED in PUtable */
338 if (buf[i & (SECTORSIZE - 1)] != 0xff)
339 inftl->PUtable[i] = BLOCK_RESERVED;
341 #endif
343 inftl->MediaUnit = block;
344 boot_record_count++;
347 return boot_record_count ? 0 : -1;
350 static int memcmpb(void *a, int c, int n)
352 int i;
353 for (i = 0; i < n; i++) {
354 if (c != ((unsigned char *)a)[i])
355 return 1;
357 return 0;
361 * check_free_sector: check if a free sector is actually FREE,
362 * i.e. All 0xff in data and oob area.
364 static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
365 int len, int check_oob)
367 int i, retlen;
368 u8 buf[SECTORSIZE];
370 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: check_free_sectors(inftl=0x%x,"
371 "address=0x%x,len=%d,check_oob=%d)\n", (int)inftl,
372 address, len, check_oob);
374 for (i = 0; i < len; i += SECTORSIZE) {
376 * We want to read the sector without ECC check here since a
377 * free sector does not have ECC syndrome on it yet.
379 if (MTD_READ(inftl->mbd.mtd, address, SECTORSIZE, &retlen, buf) < 0)
380 return -1;
381 if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
382 return -1;
384 if (check_oob) {
385 if (MTD_READOOB(inftl->mbd.mtd, address,
386 inftl->mbd.mtd->oobsize, &retlen, buf) < 0)
387 return -1;
388 if (memcmpb(buf, 0xff, inftl->mbd.mtd->oobsize) != 0)
389 return -1;
391 address += SECTORSIZE;
394 return 0;
398 * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
399 * Unit and Update INFTL metadata. Each erase operation is
400 * checked with check_free_sectors.
402 * Return: 0 when succeed, -1 on error.
404 * ToDo: 1. Is it neceressary to check_free_sector after erasing ??
405 * 2. UnitSizeFactor != 0xFF
407 int INFTL_formatblock(struct INFTLrecord *inftl, int block)
409 int retlen;
410 struct inftl_unittail uci;
411 struct erase_info *instr = &inftl->instr;
413 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_formatblock(inftl=0x%x,"
414 "block=%d)\n", (int)inftl, block);
416 memset(instr, 0, sizeof(struct erase_info));
418 /* Use async erase interface, test return code */
419 instr->addr = block * inftl->EraseSize;
420 instr->len = inftl->EraseSize;
421 MTD_ERASE(inftl->mbd.mtd, instr);
423 if (instr->state == MTD_ERASE_FAILED) {
425 * Could not format, FixMe: We should update the BadUnitTable
426 * both in memory and on disk.
428 printk(KERN_WARNING "INFTL: error while formatting block %d\n",
429 block);
430 return -1;
434 * Check the "freeness" of Erase Unit before updating metadata.
435 * FixMe: is this check really necessary? Since we have check the
436 * return code after the erase operation.
438 if (check_free_sectors(inftl, instr->addr, inftl->EraseSize, 1) != 0)
439 return -1;
441 uci.EraseMark = cpu_to_le16(ERASE_MARK);
442 uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
443 uci.Reserved[0] = 0;
444 uci.Reserved[1] = 0;
445 uci.Reserved[2] = 0;
446 uci.Reserved[3] = 0;
447 if (MTD_WRITEOOB(inftl->mbd.mtd, block * inftl->EraseSize + SECTORSIZE * 2 +
448 8, 8, &retlen, (char *)&uci) < 0)
449 return -1;
450 return 0;
454 * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
455 * Units in a Virtual Unit Chain, i.e. all the units are disconnected.
457 * Since the chain is invalid then we will have to erase it from its
458 * head (normally for INFTL we go from the oldest). But if it has a
459 * loop then there is no oldest...
461 static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
463 unsigned int block = first_block, block1;
465 printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
466 first_block);
468 for (;;) {
469 block1 = inftl->PUtable[block];
471 printk(KERN_WARNING "INFTL: formatting block %d\n", block);
472 if (INFTL_formatblock(inftl, block) < 0) {
474 * Cannot format !!!! Mark it as Bad Unit,
475 * FixMe: update the BadUnitTable on disk.
477 inftl->PUtable[block] = BLOCK_RESERVED;
478 } else {
479 inftl->PUtable[block] = BLOCK_FREE;
482 /* Goto next block on the chain */
483 block = block1;
485 if (block == BLOCK_NIL || block >= inftl->lastEUN)
486 break;
490 void INFTL_dumptables(struct INFTLrecord *s)
492 int i;
494 printk("-------------------------------------------"
495 "----------------------------------\n");
497 printk("VUtable[%d] ->", s->nb_blocks);
498 for (i = 0; i < s->nb_blocks; i++) {
499 if ((i % 8) == 0)
500 printk("\n%04x: ", i);
501 printk("%04x ", s->VUtable[i]);
504 printk("\n-------------------------------------------"
505 "----------------------------------\n");
507 printk("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
508 for (i = 0; i <= s->lastEUN; i++) {
509 if ((i % 8) == 0)
510 printk("\n%04x: ", i);
511 printk("%04x ", s->PUtable[i]);
514 printk("\n-------------------------------------------"
515 "----------------------------------\n");
517 printk("INFTL ->\n"
518 " EraseSize = %d\n"
519 " h/s/c = %d/%d/%d\n"
520 " numvunits = %d\n"
521 " firstEUN = %d\n"
522 " lastEUN = %d\n"
523 " numfreeEUNs = %d\n"
524 " LastFreeEUN = %d\n"
525 " nb_blocks = %d\n"
526 " nb_boot_blocks = %d",
527 s->EraseSize, s->heads, s->sectors, s->cylinders,
528 s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
529 s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
531 printk("\n-------------------------------------------"
532 "----------------------------------\n");
535 void INFTL_dumpVUchains(struct INFTLrecord *s)
537 int logical, block, i;
539 printk("-------------------------------------------"
540 "----------------------------------\n");
542 printk("INFTL Virtual Unit Chains:\n");
543 for (logical = 0; logical < s->nb_blocks; logical++) {
544 block = s->VUtable[logical];
545 if (block > s->nb_blocks)
546 continue;
547 printk(" LOGICAL %d --> %d ", logical, block);
548 for (i = 0; i < s->nb_blocks; i++) {
549 if (s->PUtable[block] == BLOCK_NIL)
550 break;
551 block = s->PUtable[block];
552 printk("%d ", block);
554 printk("\n");
557 printk("-------------------------------------------"
558 "----------------------------------\n");
561 int INFTL_mount(struct INFTLrecord *s)
563 unsigned int block, first_block, prev_block, last_block;
564 unsigned int first_logical_block, logical_block, erase_mark;
565 int chain_length, do_format_chain;
566 struct inftl_unithead1 h0;
567 struct inftl_unittail h1;
568 int i, retlen;
569 u8 *ANACtable, ANAC;
571 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_mount(inftl=0x%x)\n", (int)s);
573 /* Search for INFTL MediaHeader and Spare INFTL Media Header */
574 if (find_boot_record(s) < 0) {
575 printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
576 return -1;
579 /* Init the logical to physical table */
580 for (i = 0; i < s->nb_blocks; i++)
581 s->VUtable[i] = BLOCK_NIL;
583 logical_block = block = BLOCK_NIL;
585 /* Temporary buffer to store ANAC numbers. */
586 ANACtable = kmalloc(s->nb_blocks * sizeof(u8), GFP_KERNEL);
587 memset(ANACtable, 0, s->nb_blocks);
590 * First pass is to explore each physical unit, and construct the
591 * virtual chains that exist (newest physical unit goes into VUtable).
592 * Any block that is in any way invalid will be left in the
593 * NOTEXPLORED state. Then at the end we will try to format it and
594 * mark it as free.
596 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 1, explore each unit\n");
597 for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
598 if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
599 continue;
601 do_format_chain = 0;
602 first_logical_block = BLOCK_NIL;
603 last_block = BLOCK_NIL;
604 block = first_block;
606 for (chain_length = 0; ; chain_length++) {
608 if ((chain_length == 0) &&
609 (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
610 /* Nothing to do here, onto next block */
611 break;
614 if (MTD_READOOB(s->mbd.mtd, block * s->EraseSize + 8,
615 8, &retlen, (char *)&h0) < 0 ||
616 MTD_READOOB(s->mbd.mtd, block * s->EraseSize +
617 2 * SECTORSIZE + 8, 8, &retlen, (char *)&h1) < 0) {
618 /* Should never happen? */
619 do_format_chain++;
620 break;
623 logical_block = le16_to_cpu(h0.virtualUnitNo);
624 prev_block = le16_to_cpu(h0.prevUnitNo);
625 erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
626 ANACtable[block] = h0.ANAC;
628 /* Previous block is relative to start of Partition */
629 if (prev_block < s->nb_blocks)
630 prev_block += s->firstEUN;
632 /* Already explored partial chain? */
633 if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
634 /* Check if chain for this logical */
635 if (logical_block == first_logical_block) {
636 if (last_block != BLOCK_NIL)
637 s->PUtable[last_block] = block;
639 break;
642 /* Check for invalid block */
643 if (erase_mark != ERASE_MARK) {
644 printk(KERN_WARNING "INFTL: corrupt block %d "
645 "in chain %d, chain length %d, erase "
646 "mark 0x%x?\n", block, first_block,
647 chain_length, erase_mark);
649 * Assume end of chain, probably incomplete
650 * fold/erase...
652 if (chain_length == 0)
653 do_format_chain++;
654 break;
657 /* Check for it being free already then... */
658 if ((logical_block == BLOCK_FREE) ||
659 (logical_block == BLOCK_NIL)) {
660 s->PUtable[block] = BLOCK_FREE;
661 break;
664 /* Sanity checks on block numbers */
665 if ((logical_block >= s->nb_blocks) ||
666 ((prev_block >= s->nb_blocks) &&
667 (prev_block != BLOCK_NIL))) {
668 if (chain_length > 0) {
669 printk(KERN_WARNING "INFTL: corrupt "
670 "block %d in chain %d?\n",
671 block, first_block);
672 do_format_chain++;
674 break;
677 if (first_logical_block == BLOCK_NIL) {
678 first_logical_block = logical_block;
679 } else {
680 if (first_logical_block != logical_block) {
681 /* Normal for folded chain... */
682 break;
687 * Current block is valid, so if we followed a virtual
688 * chain to get here then we can set the previous
689 * block pointer in our PUtable now. Then move onto
690 * the previous block in the chain.
692 s->PUtable[block] = BLOCK_NIL;
693 if (last_block != BLOCK_NIL)
694 s->PUtable[last_block] = block;
695 last_block = block;
696 block = prev_block;
698 /* Check for end of chain */
699 if (block == BLOCK_NIL)
700 break;
702 /* Validate next block before following it... */
703 if (block > s->lastEUN) {
704 printk(KERN_WARNING "INFTL: invalid previous "
705 "block %d in chain %d?\n", block,
706 first_block);
707 do_format_chain++;
708 break;
712 if (do_format_chain) {
713 format_chain(s, first_block);
714 continue;
718 * Looks like a valid chain then. It may not really be the
719 * newest block in the chain, but it is the newest we have
720 * found so far. We might update it in later iterations of
721 * this loop if we find something newer.
723 s->VUtable[first_logical_block] = first_block;
724 logical_block = BLOCK_NIL;
727 #ifdef CONFIG_MTD_DEBUG_VERBOSE
728 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
729 INFTL_dumptables(s);
730 #endif
733 * Second pass, check for infinite loops in chains. These are
734 * possible because we don't update the previous pointers when
735 * we fold chains. No big deal, just fix them up in PUtable.
737 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 2, validate virtual chains\n");
738 for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
739 block = s->VUtable[logical_block];
740 last_block = BLOCK_NIL;
742 /* Check for free/reserved/nil */
743 if (block >= BLOCK_RESERVED)
744 continue;
746 ANAC = ANACtable[block];
747 for (i = 0; i < s->numvunits; i++) {
748 if (s->PUtable[block] == BLOCK_NIL)
749 break;
750 if (s->PUtable[block] > s->lastEUN) {
751 printk(KERN_WARNING "INFTL: invalid prev %d, "
752 "in virtual chain %d\n",
753 s->PUtable[block], logical_block);
754 s->PUtable[block] = BLOCK_NIL;
757 if (ANACtable[block] != ANAC) {
759 * Chain must point back to itself. This is ok,
760 * but we will need adjust the tables with this
761 * newest block and oldest block.
763 s->VUtable[logical_block] = block;
764 s->PUtable[last_block] = BLOCK_NIL;
765 break;
768 ANAC--;
769 last_block = block;
770 block = s->PUtable[block];
773 if (i >= s->nb_blocks) {
775 * Uhoo, infinite chain with valid ANACS!
776 * Format whole chain...
778 format_chain(s, first_block);
782 #ifdef CONFIG_MTD_DEBUG_VERBOSE
783 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
784 INFTL_dumptables(s);
785 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
786 INFTL_dumpVUchains(s);
787 #endif
790 * Third pass, format unreferenced blocks and init free block count.
792 s->numfreeEUNs = 0;
793 s->LastFreeEUN = BLOCK_NIL;
795 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 3, format unused blocks\n");
796 for (block = s->firstEUN; block <= s->lastEUN; block++) {
797 if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
798 printk("INFTL: unreferenced block %d, formatting it\n",
799 block);
800 if (INFTL_formatblock(s, block) < 0)
801 s->PUtable[block] = BLOCK_RESERVED;
802 else
803 s->PUtable[block] = BLOCK_FREE;
805 if (s->PUtable[block] == BLOCK_FREE) {
806 s->numfreeEUNs++;
807 if (s->LastFreeEUN == BLOCK_NIL)
808 s->LastFreeEUN = block;
812 kfree(ANACtable);
813 return 0;