snmp: fix SNMP_ADD_STATS()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / inftlmount.c
blob8f988d7d3c5c9925320eae830b4a44a829b360bc
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 * 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>
37 #include <linux/mtd/compatmac.h>
40 * find_boot_record: Find the INFTL Media Header and its Spare copy which
41 * contains the various device information of the INFTL partition and
42 * Bad Unit Table. Update the PUtable[] table according to the Bad
43 * Unit Table. PUtable[] is used for management of Erase Unit in
44 * other routines in inftlcore.c and inftlmount.c.
46 static int find_boot_record(struct INFTLrecord *inftl)
48 struct inftl_unittail h1;
49 //struct inftl_oob oob;
50 unsigned int i, block;
51 u8 buf[SECTORSIZE];
52 struct INFTLMediaHeader *mh = &inftl->MediaHdr;
53 struct mtd_info *mtd = inftl->mbd.mtd;
54 struct INFTLPartition *ip;
55 size_t retlen;
57 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: find_boot_record(inftl=%p)\n", inftl);
60 * Assume logical EraseSize == physical erasesize for starting the
61 * scan. We'll sort it out later if we find a MediaHeader which says
62 * otherwise.
64 inftl->EraseSize = inftl->mbd.mtd->erasesize;
65 inftl->nb_blocks = (u32)inftl->mbd.mtd->size / inftl->EraseSize;
67 inftl->MediaUnit = BLOCK_NIL;
69 /* Search for a valid boot record */
70 for (block = 0; block < inftl->nb_blocks; block++) {
71 int ret;
74 * Check for BNAND header first. Then whinge if it's found
75 * but later checks fail.
77 ret = mtd->read(mtd, block * inftl->EraseSize,
78 SECTORSIZE, &retlen, buf);
79 /* We ignore ret in case the ECC of the MediaHeader is invalid
80 (which is apparently acceptable) */
81 if (retlen != SECTORSIZE) {
82 static int warncount = 5;
84 if (warncount) {
85 printk(KERN_WARNING "INFTL: block read at 0x%x "
86 "of mtd%d failed: %d\n",
87 block * inftl->EraseSize,
88 inftl->mbd.mtd->index, ret);
89 if (!--warncount)
90 printk(KERN_WARNING "INFTL: further "
91 "failures for this block will "
92 "not be printed\n");
94 continue;
97 if (retlen < 6 || memcmp(buf, "BNAND", 6)) {
98 /* BNAND\0 not found. Continue */
99 continue;
102 /* To be safer with BIOS, also use erase mark as discriminant */
103 ret = inftl_read_oob(mtd,
104 block * inftl->EraseSize + SECTORSIZE + 8,
105 8, &retlen,(char *)&h1);
106 if (ret < 0) {
107 printk(KERN_WARNING "INFTL: ANAND header found at "
108 "0x%x in mtd%d, but OOB data read failed "
109 "(err %d)\n", block * inftl->EraseSize,
110 inftl->mbd.mtd->index, ret);
111 continue;
116 * This is the first we've seen.
117 * Copy the media header structure into place.
119 memcpy(mh, buf, sizeof(struct INFTLMediaHeader));
121 /* Read the spare media header at offset 4096 */
122 mtd->read(mtd, block * inftl->EraseSize + 4096,
123 SECTORSIZE, &retlen, buf);
124 if (retlen != SECTORSIZE) {
125 printk(KERN_WARNING "INFTL: Unable to read spare "
126 "Media Header\n");
127 return -1;
129 /* Check if this one is the same as the first one we found. */
130 if (memcmp(mh, buf, sizeof(struct INFTLMediaHeader))) {
131 printk(KERN_WARNING "INFTL: Primary and spare Media "
132 "Headers disagree.\n");
133 return -1;
136 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
137 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
138 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
139 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
140 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
141 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
143 #ifdef CONFIG_MTD_DEBUG_VERBOSE
144 if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
145 printk("INFTL: Media Header ->\n"
146 " bootRecordID = %s\n"
147 " NoOfBootImageBlocks = %d\n"
148 " NoOfBinaryPartitions = %d\n"
149 " NoOfBDTLPartitions = %d\n"
150 " BlockMultiplerBits = %d\n"
151 " FormatFlgs = %d\n"
152 " OsakVersion = 0x%x\n"
153 " PercentUsed = %d\n",
154 mh->bootRecordID, mh->NoOfBootImageBlocks,
155 mh->NoOfBinaryPartitions,
156 mh->NoOfBDTLPartitions,
157 mh->BlockMultiplierBits, mh->FormatFlags,
158 mh->OsakVersion, mh->PercentUsed);
160 #endif
162 if (mh->NoOfBDTLPartitions == 0) {
163 printk(KERN_WARNING "INFTL: Media Header sanity check "
164 "failed: NoOfBDTLPartitions (%d) == 0, "
165 "must be at least 1\n", mh->NoOfBDTLPartitions);
166 return -1;
169 if ((mh->NoOfBDTLPartitions + mh->NoOfBinaryPartitions) > 4) {
170 printk(KERN_WARNING "INFTL: Media Header sanity check "
171 "failed: Total Partitions (%d) > 4, "
172 "BDTL=%d Binary=%d\n", mh->NoOfBDTLPartitions +
173 mh->NoOfBinaryPartitions,
174 mh->NoOfBDTLPartitions,
175 mh->NoOfBinaryPartitions);
176 return -1;
179 if (mh->BlockMultiplierBits > 1) {
180 printk(KERN_WARNING "INFTL: sorry, we don't support "
181 "UnitSizeFactor 0x%02x\n",
182 mh->BlockMultiplierBits);
183 return -1;
184 } else if (mh->BlockMultiplierBits == 1) {
185 printk(KERN_WARNING "INFTL: support for INFTL with "
186 "UnitSizeFactor 0x%02x is experimental\n",
187 mh->BlockMultiplierBits);
188 inftl->EraseSize = inftl->mbd.mtd->erasesize <<
189 mh->BlockMultiplierBits;
190 inftl->nb_blocks = (u32)inftl->mbd.mtd->size / inftl->EraseSize;
191 block >>= mh->BlockMultiplierBits;
194 /* Scan the partitions */
195 for (i = 0; (i < 4); i++) {
196 ip = &mh->Partitions[i];
197 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
198 ip->firstUnit = le32_to_cpu(ip->firstUnit);
199 ip->lastUnit = le32_to_cpu(ip->lastUnit);
200 ip->flags = le32_to_cpu(ip->flags);
201 ip->spareUnits = le32_to_cpu(ip->spareUnits);
202 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
204 #ifdef CONFIG_MTD_DEBUG_VERBOSE
205 if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
206 printk(" PARTITION[%d] ->\n"
207 " virtualUnits = %d\n"
208 " firstUnit = %d\n"
209 " lastUnit = %d\n"
210 " flags = 0x%x\n"
211 " spareUnits = %d\n",
212 i, ip->virtualUnits, ip->firstUnit,
213 ip->lastUnit, ip->flags,
214 ip->spareUnits);
216 #endif
218 if (ip->Reserved0 != ip->firstUnit) {
219 struct erase_info *instr = &inftl->instr;
221 instr->mtd = inftl->mbd.mtd;
224 * Most likely this is using the
225 * undocumented qiuck mount feature.
226 * We don't support that, we will need
227 * to erase the hidden block for full
228 * compatibility.
230 instr->addr = ip->Reserved0 * inftl->EraseSize;
231 instr->len = inftl->EraseSize;
232 mtd->erase(mtd, instr);
234 if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
235 printk(KERN_WARNING "INFTL: Media Header "
236 "Partition %d sanity check failed\n"
237 " firstUnit %d : lastUnit %d > "
238 "virtualUnits %d\n", i, ip->lastUnit,
239 ip->firstUnit, ip->Reserved0);
240 return -1;
242 if (ip->Reserved1 != 0) {
243 printk(KERN_WARNING "INFTL: Media Header "
244 "Partition %d sanity check failed: "
245 "Reserved1 %d != 0\n",
246 i, ip->Reserved1);
247 return -1;
250 if (ip->flags & INFTL_BDTL)
251 break;
254 if (i >= 4) {
255 printk(KERN_WARNING "INFTL: Media Header Partition "
256 "sanity check failed:\n No partition "
257 "marked as Disk Partition\n");
258 return -1;
261 inftl->nb_boot_blocks = ip->firstUnit;
262 inftl->numvunits = ip->virtualUnits;
263 if (inftl->numvunits > (inftl->nb_blocks -
264 inftl->nb_boot_blocks - 2)) {
265 printk(KERN_WARNING "INFTL: Media Header sanity check "
266 "failed:\n numvunits (%d) > nb_blocks "
267 "(%d) - nb_boot_blocks(%d) - 2\n",
268 inftl->numvunits, inftl->nb_blocks,
269 inftl->nb_boot_blocks);
270 return -1;
273 inftl->mbd.size = inftl->numvunits *
274 (inftl->EraseSize / SECTORSIZE);
277 * Block count is set to last used EUN (we won't need to keep
278 * any meta-data past that point).
280 inftl->firstEUN = ip->firstUnit;
281 inftl->lastEUN = ip->lastUnit;
282 inftl->nb_blocks = ip->lastUnit + 1;
284 /* Memory alloc */
285 inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
286 if (!inftl->PUtable) {
287 printk(KERN_WARNING "INFTL: allocation of PUtable "
288 "failed (%zd bytes)\n",
289 inftl->nb_blocks * sizeof(u16));
290 return -ENOMEM;
293 inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
294 if (!inftl->VUtable) {
295 kfree(inftl->PUtable);
296 printk(KERN_WARNING "INFTL: allocation of VUtable "
297 "failed (%zd bytes)\n",
298 inftl->nb_blocks * sizeof(u16));
299 return -ENOMEM;
302 /* Mark the blocks before INFTL MediaHeader as reserved */
303 for (i = 0; i < inftl->nb_boot_blocks; i++)
304 inftl->PUtable[i] = BLOCK_RESERVED;
305 /* Mark all remaining blocks as potentially containing data */
306 for (; i < inftl->nb_blocks; i++)
307 inftl->PUtable[i] = BLOCK_NOTEXPLORED;
309 /* Mark this boot record (NFTL MediaHeader) block as reserved */
310 inftl->PUtable[block] = BLOCK_RESERVED;
312 /* Read Bad Erase Unit Table and modify PUtable[] accordingly */
313 for (i = 0; i < inftl->nb_blocks; i++) {
314 int physblock;
315 /* If any of the physical eraseblocks are bad, don't
316 use the unit. */
317 for (physblock = 0; physblock < inftl->EraseSize; physblock += inftl->mbd.mtd->erasesize) {
318 if (inftl->mbd.mtd->block_isbad(inftl->mbd.mtd, i * inftl->EraseSize + physblock))
319 inftl->PUtable[i] = BLOCK_RESERVED;
323 inftl->MediaUnit = block;
324 return 0;
327 /* Not found. */
328 return -1;
331 static int memcmpb(void *a, int c, int n)
333 int i;
334 for (i = 0; i < n; i++) {
335 if (c != ((unsigned char *)a)[i])
336 return 1;
338 return 0;
342 * check_free_sector: check if a free sector is actually FREE,
343 * i.e. All 0xff in data and oob area.
345 static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
346 int len, int check_oob)
348 u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
349 struct mtd_info *mtd = inftl->mbd.mtd;
350 size_t retlen;
351 int i;
353 for (i = 0; i < len; i += SECTORSIZE) {
354 if (mtd->read(mtd, address, SECTORSIZE, &retlen, buf))
355 return -1;
356 if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
357 return -1;
359 if (check_oob) {
360 if(inftl_read_oob(mtd, address, mtd->oobsize,
361 &retlen, &buf[SECTORSIZE]) < 0)
362 return -1;
363 if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
364 return -1;
366 address += SECTORSIZE;
369 return 0;
373 * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
374 * Unit and Update INFTL metadata. Each erase operation is
375 * checked with check_free_sectors.
377 * Return: 0 when succeed, -1 on error.
379 * ToDo: 1. Is it neceressary to check_free_sector after erasing ??
381 int INFTL_formatblock(struct INFTLrecord *inftl, int block)
383 size_t retlen;
384 struct inftl_unittail uci;
385 struct erase_info *instr = &inftl->instr;
386 struct mtd_info *mtd = inftl->mbd.mtd;
387 int physblock;
389 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_formatblock(inftl=%p,"
390 "block=%d)\n", inftl, block);
392 memset(instr, 0, sizeof(struct erase_info));
394 /* FIXME: Shouldn't we be setting the 'discarded' flag to zero
395 _first_? */
397 /* Use async erase interface, test return code */
398 instr->mtd = inftl->mbd.mtd;
399 instr->addr = block * inftl->EraseSize;
400 instr->len = inftl->mbd.mtd->erasesize;
401 /* Erase one physical eraseblock at a time, even though the NAND api
402 allows us to group them. This way we if we have a failure, we can
403 mark only the failed block in the bbt. */
404 for (physblock = 0; physblock < inftl->EraseSize;
405 physblock += instr->len, instr->addr += instr->len) {
406 mtd->erase(inftl->mbd.mtd, instr);
408 if (instr->state == MTD_ERASE_FAILED) {
409 printk(KERN_WARNING "INFTL: error while formatting block %d\n",
410 block);
411 goto fail;
415 * Check the "freeness" of Erase Unit before updating metadata.
416 * FixMe: is this check really necessary? Since we have check
417 * the return code after the erase operation.
419 if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
420 goto fail;
423 uci.EraseMark = cpu_to_le16(ERASE_MARK);
424 uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
425 uci.Reserved[0] = 0;
426 uci.Reserved[1] = 0;
427 uci.Reserved[2] = 0;
428 uci.Reserved[3] = 0;
429 instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
430 if (inftl_write_oob(mtd, instr->addr + 8, 8, &retlen, (char *)&uci) < 0)
431 goto fail;
432 return 0;
433 fail:
434 /* could not format, update the bad block table (caller is responsible
435 for setting the PUtable to BLOCK_RESERVED on failure) */
436 inftl->mbd.mtd->block_markbad(inftl->mbd.mtd, instr->addr);
437 return -1;
441 * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
442 * Units in a Virtual Unit Chain, i.e. all the units are disconnected.
444 * Since the chain is invalid then we will have to erase it from its
445 * head (normally for INFTL we go from the oldest). But if it has a
446 * loop then there is no oldest...
448 static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
450 unsigned int block = first_block, block1;
452 printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
453 first_block);
455 for (;;) {
456 block1 = inftl->PUtable[block];
458 printk(KERN_WARNING "INFTL: formatting block %d\n", block);
459 if (INFTL_formatblock(inftl, block) < 0) {
461 * Cannot format !!!! Mark it as Bad Unit,
463 inftl->PUtable[block] = BLOCK_RESERVED;
464 } else {
465 inftl->PUtable[block] = BLOCK_FREE;
468 /* Goto next block on the chain */
469 block = block1;
471 if (block == BLOCK_NIL || block >= inftl->lastEUN)
472 break;
476 void INFTL_dumptables(struct INFTLrecord *s)
478 int i;
480 printk("-------------------------------------------"
481 "----------------------------------\n");
483 printk("VUtable[%d] ->", s->nb_blocks);
484 for (i = 0; i < s->nb_blocks; i++) {
485 if ((i % 8) == 0)
486 printk("\n%04x: ", i);
487 printk("%04x ", s->VUtable[i]);
490 printk("\n-------------------------------------------"
491 "----------------------------------\n");
493 printk("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
494 for (i = 0; i <= s->lastEUN; i++) {
495 if ((i % 8) == 0)
496 printk("\n%04x: ", i);
497 printk("%04x ", s->PUtable[i]);
500 printk("\n-------------------------------------------"
501 "----------------------------------\n");
503 printk("INFTL ->\n"
504 " EraseSize = %d\n"
505 " h/s/c = %d/%d/%d\n"
506 " numvunits = %d\n"
507 " firstEUN = %d\n"
508 " lastEUN = %d\n"
509 " numfreeEUNs = %d\n"
510 " LastFreeEUN = %d\n"
511 " nb_blocks = %d\n"
512 " nb_boot_blocks = %d",
513 s->EraseSize, s->heads, s->sectors, s->cylinders,
514 s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
515 s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
517 printk("\n-------------------------------------------"
518 "----------------------------------\n");
521 void INFTL_dumpVUchains(struct INFTLrecord *s)
523 int logical, block, i;
525 printk("-------------------------------------------"
526 "----------------------------------\n");
528 printk("INFTL Virtual Unit Chains:\n");
529 for (logical = 0; logical < s->nb_blocks; logical++) {
530 block = s->VUtable[logical];
531 if (block > s->nb_blocks)
532 continue;
533 printk(" LOGICAL %d --> %d ", logical, block);
534 for (i = 0; i < s->nb_blocks; i++) {
535 if (s->PUtable[block] == BLOCK_NIL)
536 break;
537 block = s->PUtable[block];
538 printk("%d ", block);
540 printk("\n");
543 printk("-------------------------------------------"
544 "----------------------------------\n");
547 int INFTL_mount(struct INFTLrecord *s)
549 struct mtd_info *mtd = s->mbd.mtd;
550 unsigned int block, first_block, prev_block, last_block;
551 unsigned int first_logical_block, logical_block, erase_mark;
552 int chain_length, do_format_chain;
553 struct inftl_unithead1 h0;
554 struct inftl_unittail h1;
555 size_t retlen;
556 int i;
557 u8 *ANACtable, ANAC;
559 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_mount(inftl=%p)\n", s);
561 /* Search for INFTL MediaHeader and Spare INFTL Media Header */
562 if (find_boot_record(s) < 0) {
563 printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
564 return -ENXIO;
567 /* Init the logical to physical table */
568 for (i = 0; i < s->nb_blocks; i++)
569 s->VUtable[i] = BLOCK_NIL;
571 logical_block = block = BLOCK_NIL;
573 /* Temporary buffer to store ANAC numbers. */
574 ANACtable = kcalloc(s->nb_blocks, sizeof(u8), GFP_KERNEL);
575 if (!ANACtable) {
576 printk(KERN_WARNING "INFTL: allocation of ANACtable "
577 "failed (%zd bytes)\n",
578 s->nb_blocks * sizeof(u8));
579 return -ENOMEM;
583 * First pass is to explore each physical unit, and construct the
584 * virtual chains that exist (newest physical unit goes into VUtable).
585 * Any block that is in any way invalid will be left in the
586 * NOTEXPLORED state. Then at the end we will try to format it and
587 * mark it as free.
589 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 1, explore each unit\n");
590 for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
591 if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
592 continue;
594 do_format_chain = 0;
595 first_logical_block = BLOCK_NIL;
596 last_block = BLOCK_NIL;
597 block = first_block;
599 for (chain_length = 0; ; chain_length++) {
601 if ((chain_length == 0) &&
602 (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
603 /* Nothing to do here, onto next block */
604 break;
607 if (inftl_read_oob(mtd, block * s->EraseSize + 8,
608 8, &retlen, (char *)&h0) < 0 ||
609 inftl_read_oob(mtd, block * s->EraseSize +
610 2 * SECTORSIZE + 8, 8, &retlen,
611 (char *)&h1) < 0) {
612 /* Should never happen? */
613 do_format_chain++;
614 break;
617 logical_block = le16_to_cpu(h0.virtualUnitNo);
618 prev_block = le16_to_cpu(h0.prevUnitNo);
619 erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
620 ANACtable[block] = h0.ANAC;
622 /* Previous block is relative to start of Partition */
623 if (prev_block < s->nb_blocks)
624 prev_block += s->firstEUN;
626 /* Already explored partial chain? */
627 if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
628 /* Check if chain for this logical */
629 if (logical_block == first_logical_block) {
630 if (last_block != BLOCK_NIL)
631 s->PUtable[last_block] = block;
633 break;
636 /* Check for invalid block */
637 if (erase_mark != ERASE_MARK) {
638 printk(KERN_WARNING "INFTL: corrupt block %d "
639 "in chain %d, chain length %d, erase "
640 "mark 0x%x?\n", block, first_block,
641 chain_length, erase_mark);
643 * Assume end of chain, probably incomplete
644 * fold/erase...
646 if (chain_length == 0)
647 do_format_chain++;
648 break;
651 /* Check for it being free already then... */
652 if ((logical_block == BLOCK_FREE) ||
653 (logical_block == BLOCK_NIL)) {
654 s->PUtable[block] = BLOCK_FREE;
655 break;
658 /* Sanity checks on block numbers */
659 if ((logical_block >= s->nb_blocks) ||
660 ((prev_block >= s->nb_blocks) &&
661 (prev_block != BLOCK_NIL))) {
662 if (chain_length > 0) {
663 printk(KERN_WARNING "INFTL: corrupt "
664 "block %d in chain %d?\n",
665 block, first_block);
666 do_format_chain++;
668 break;
671 if (first_logical_block == BLOCK_NIL) {
672 first_logical_block = logical_block;
673 } else {
674 if (first_logical_block != logical_block) {
675 /* Normal for folded chain... */
676 break;
681 * Current block is valid, so if we followed a virtual
682 * chain to get here then we can set the previous
683 * block pointer in our PUtable now. Then move onto
684 * the previous block in the chain.
686 s->PUtable[block] = BLOCK_NIL;
687 if (last_block != BLOCK_NIL)
688 s->PUtable[last_block] = block;
689 last_block = block;
690 block = prev_block;
692 /* Check for end of chain */
693 if (block == BLOCK_NIL)
694 break;
696 /* Validate next block before following it... */
697 if (block > s->lastEUN) {
698 printk(KERN_WARNING "INFTL: invalid previous "
699 "block %d in chain %d?\n", block,
700 first_block);
701 do_format_chain++;
702 break;
706 if (do_format_chain) {
707 format_chain(s, first_block);
708 continue;
712 * Looks like a valid chain then. It may not really be the
713 * newest block in the chain, but it is the newest we have
714 * found so far. We might update it in later iterations of
715 * this loop if we find something newer.
717 s->VUtable[first_logical_block] = first_block;
718 logical_block = BLOCK_NIL;
721 #ifdef CONFIG_MTD_DEBUG_VERBOSE
722 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
723 INFTL_dumptables(s);
724 #endif
727 * Second pass, check for infinite loops in chains. These are
728 * possible because we don't update the previous pointers when
729 * we fold chains. No big deal, just fix them up in PUtable.
731 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 2, validate virtual chains\n");
732 for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
733 block = s->VUtable[logical_block];
734 last_block = BLOCK_NIL;
736 /* Check for free/reserved/nil */
737 if (block >= BLOCK_RESERVED)
738 continue;
740 ANAC = ANACtable[block];
741 for (i = 0; i < s->numvunits; i++) {
742 if (s->PUtable[block] == BLOCK_NIL)
743 break;
744 if (s->PUtable[block] > s->lastEUN) {
745 printk(KERN_WARNING "INFTL: invalid prev %d, "
746 "in virtual chain %d\n",
747 s->PUtable[block], logical_block);
748 s->PUtable[block] = BLOCK_NIL;
751 if (ANACtable[block] != ANAC) {
753 * Chain must point back to itself. This is ok,
754 * but we will need adjust the tables with this
755 * newest block and oldest block.
757 s->VUtable[logical_block] = block;
758 s->PUtable[last_block] = BLOCK_NIL;
759 break;
762 ANAC--;
763 last_block = block;
764 block = s->PUtable[block];
767 if (i >= s->nb_blocks) {
769 * Uhoo, infinite chain with valid ANACS!
770 * Format whole chain...
772 format_chain(s, first_block);
776 #ifdef CONFIG_MTD_DEBUG_VERBOSE
777 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
778 INFTL_dumptables(s);
779 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
780 INFTL_dumpVUchains(s);
781 #endif
784 * Third pass, format unreferenced blocks and init free block count.
786 s->numfreeEUNs = 0;
787 s->LastFreeEUN = BLOCK_NIL;
789 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 3, format unused blocks\n");
790 for (block = s->firstEUN; block <= s->lastEUN; block++) {
791 if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
792 printk("INFTL: unreferenced block %d, formatting it\n",
793 block);
794 if (INFTL_formatblock(s, block) < 0)
795 s->PUtable[block] = BLOCK_RESERVED;
796 else
797 s->PUtable[block] = BLOCK_FREE;
799 if (s->PUtable[block] == BLOCK_FREE) {
800 s->numfreeEUNs++;
801 if (s->LastFreeEUN == BLOCK_NIL)
802 s->LastFreeEUN = block;
806 kfree(ANACtable);
807 return 0;