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.18 2005/11/07 11:14:20 gleixner 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.18 $";
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 mtd_info
*mtd
= inftl
->mbd
.mtd
;
61 struct INFTLPartition
*ip
;
64 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: find_boot_record(inftl=%p)\n", 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
71 inftl
->EraseSize
= inftl
->mbd
.mtd
->erasesize
;
72 inftl
->nb_blocks
= inftl
->mbd
.mtd
->size
/ inftl
->EraseSize
;
74 inftl
->MediaUnit
= BLOCK_NIL
;
76 /* Search for a valid boot record */
77 for (block
= 0; block
< inftl
->nb_blocks
; block
++) {
81 * Check for BNAND header first. Then whinge if it's found
82 * but later checks fail.
84 ret
= mtd
->read(mtd
, block
* inftl
->EraseSize
,
85 SECTORSIZE
, &retlen
, buf
);
86 /* We ignore ret in case the ECC of the MediaHeader is invalid
87 (which is apparently acceptable) */
88 if (retlen
!= SECTORSIZE
) {
89 static int warncount
= 5;
92 printk(KERN_WARNING
"INFTL: block read at 0x%x "
93 "of mtd%d failed: %d\n",
94 block
* inftl
->EraseSize
,
95 inftl
->mbd
.mtd
->index
, ret
);
97 printk(KERN_WARNING
"INFTL: further "
98 "failures for this block will "
104 if (retlen
< 6 || memcmp(buf
, "BNAND", 6)) {
105 /* BNAND\0 not found. Continue */
109 /* To be safer with BIOS, also use erase mark as discriminant */
110 if ((ret
= mtd
->read_oob(mtd
, block
* inftl
->EraseSize
+
111 SECTORSIZE
+ 8, 8, &retlen
,
113 printk(KERN_WARNING
"INFTL: ANAND header found at "
114 "0x%x in mtd%d, but OOB data read failed "
115 "(err %d)\n", block
* inftl
->EraseSize
,
116 inftl
->mbd
.mtd
->index
, ret
);
122 * This is the first we've seen.
123 * Copy the media header structure into place.
125 memcpy(mh
, buf
, sizeof(struct INFTLMediaHeader
));
127 /* Read the spare media header at offset 4096 */
128 mtd
->read(mtd
, block
* inftl
->EraseSize
+ 4096,
129 SECTORSIZE
, &retlen
, buf
);
130 if (retlen
!= SECTORSIZE
) {
131 printk(KERN_WARNING
"INFTL: Unable to read spare "
135 /* Check if this one is the same as the first one we found. */
136 if (memcmp(mh
, buf
, sizeof(struct INFTLMediaHeader
))) {
137 printk(KERN_WARNING
"INFTL: Primary and spare Media "
138 "Headers disagree.\n");
142 mh
->NoOfBootImageBlocks
= le32_to_cpu(mh
->NoOfBootImageBlocks
);
143 mh
->NoOfBinaryPartitions
= le32_to_cpu(mh
->NoOfBinaryPartitions
);
144 mh
->NoOfBDTLPartitions
= le32_to_cpu(mh
->NoOfBDTLPartitions
);
145 mh
->BlockMultiplierBits
= le32_to_cpu(mh
->BlockMultiplierBits
);
146 mh
->FormatFlags
= le32_to_cpu(mh
->FormatFlags
);
147 mh
->PercentUsed
= le32_to_cpu(mh
->PercentUsed
);
149 #ifdef CONFIG_MTD_DEBUG_VERBOSE
150 if (CONFIG_MTD_DEBUG_VERBOSE
>= 2) {
151 printk("INFTL: Media Header ->\n"
152 " bootRecordID = %s\n"
153 " NoOfBootImageBlocks = %d\n"
154 " NoOfBinaryPartitions = %d\n"
155 " NoOfBDTLPartitions = %d\n"
156 " BlockMultiplerBits = %d\n"
158 " OsakVersion = 0x%x\n"
159 " PercentUsed = %d\n",
160 mh
->bootRecordID
, mh
->NoOfBootImageBlocks
,
161 mh
->NoOfBinaryPartitions
,
162 mh
->NoOfBDTLPartitions
,
163 mh
->BlockMultiplierBits
, mh
->FormatFlags
,
164 mh
->OsakVersion
, mh
->PercentUsed
);
168 if (mh
->NoOfBDTLPartitions
== 0) {
169 printk(KERN_WARNING
"INFTL: Media Header sanity check "
170 "failed: NoOfBDTLPartitions (%d) == 0, "
171 "must be at least 1\n", mh
->NoOfBDTLPartitions
);
175 if ((mh
->NoOfBDTLPartitions
+ mh
->NoOfBinaryPartitions
) > 4) {
176 printk(KERN_WARNING
"INFTL: Media Header sanity check "
177 "failed: Total Partitions (%d) > 4, "
178 "BDTL=%d Binary=%d\n", mh
->NoOfBDTLPartitions
+
179 mh
->NoOfBinaryPartitions
,
180 mh
->NoOfBDTLPartitions
,
181 mh
->NoOfBinaryPartitions
);
185 if (mh
->BlockMultiplierBits
> 1) {
186 printk(KERN_WARNING
"INFTL: sorry, we don't support "
187 "UnitSizeFactor 0x%02x\n",
188 mh
->BlockMultiplierBits
);
190 } else if (mh
->BlockMultiplierBits
== 1) {
191 printk(KERN_WARNING
"INFTL: support for INFTL with "
192 "UnitSizeFactor 0x%02x is experimental\n",
193 mh
->BlockMultiplierBits
);
194 inftl
->EraseSize
= inftl
->mbd
.mtd
->erasesize
<<
195 mh
->BlockMultiplierBits
;
196 inftl
->nb_blocks
= inftl
->mbd
.mtd
->size
/ inftl
->EraseSize
;
197 block
>>= mh
->BlockMultiplierBits
;
200 /* Scan the partitions */
201 for (i
= 0; (i
< 4); i
++) {
202 ip
= &mh
->Partitions
[i
];
203 ip
->virtualUnits
= le32_to_cpu(ip
->virtualUnits
);
204 ip
->firstUnit
= le32_to_cpu(ip
->firstUnit
);
205 ip
->lastUnit
= le32_to_cpu(ip
->lastUnit
);
206 ip
->flags
= le32_to_cpu(ip
->flags
);
207 ip
->spareUnits
= le32_to_cpu(ip
->spareUnits
);
208 ip
->Reserved0
= le32_to_cpu(ip
->Reserved0
);
210 #ifdef CONFIG_MTD_DEBUG_VERBOSE
211 if (CONFIG_MTD_DEBUG_VERBOSE
>= 2) {
212 printk(" PARTITION[%d] ->\n"
213 " virtualUnits = %d\n"
217 " spareUnits = %d\n",
218 i
, ip
->virtualUnits
, ip
->firstUnit
,
219 ip
->lastUnit
, ip
->flags
,
224 if (ip
->Reserved0
!= ip
->firstUnit
) {
225 struct erase_info
*instr
= &inftl
->instr
;
227 instr
->mtd
= inftl
->mbd
.mtd
;
230 * Most likely this is using the
231 * undocumented qiuck mount feature.
232 * We don't support that, we will need
233 * to erase the hidden block for full
236 instr
->addr
= ip
->Reserved0
* inftl
->EraseSize
;
237 instr
->len
= inftl
->EraseSize
;
238 mtd
->erase(mtd
, instr
);
240 if ((ip
->lastUnit
- ip
->firstUnit
+ 1) < ip
->virtualUnits
) {
241 printk(KERN_WARNING
"INFTL: Media Header "
242 "Partition %d sanity check failed\n"
243 " firstUnit %d : lastUnit %d > "
244 "virtualUnits %d\n", i
, ip
->lastUnit
,
245 ip
->firstUnit
, ip
->Reserved0
);
248 if (ip
->Reserved1
!= 0) {
249 printk(KERN_WARNING
"INFTL: Media Header "
250 "Partition %d sanity check failed: "
251 "Reserved1 %d != 0\n",
256 if (ip
->flags
& INFTL_BDTL
)
261 printk(KERN_WARNING
"INFTL: Media Header Partition "
262 "sanity check failed:\n No partition "
263 "marked as Disk Partition\n");
267 inftl
->nb_boot_blocks
= ip
->firstUnit
;
268 inftl
->numvunits
= ip
->virtualUnits
;
269 if (inftl
->numvunits
> (inftl
->nb_blocks
-
270 inftl
->nb_boot_blocks
- 2)) {
271 printk(KERN_WARNING
"INFTL: Media Header sanity check "
272 "failed:\n numvunits (%d) > nb_blocks "
273 "(%d) - nb_boot_blocks(%d) - 2\n",
274 inftl
->numvunits
, inftl
->nb_blocks
,
275 inftl
->nb_boot_blocks
);
279 inftl
->mbd
.size
= inftl
->numvunits
*
280 (inftl
->EraseSize
/ SECTORSIZE
);
283 * Block count is set to last used EUN (we won't need to keep
284 * any meta-data past that point).
286 inftl
->firstEUN
= ip
->firstUnit
;
287 inftl
->lastEUN
= ip
->lastUnit
;
288 inftl
->nb_blocks
= ip
->lastUnit
+ 1;
291 inftl
->PUtable
= kmalloc(inftl
->nb_blocks
* sizeof(u16
), GFP_KERNEL
);
292 if (!inftl
->PUtable
) {
293 printk(KERN_WARNING
"INFTL: allocation of PUtable "
294 "failed (%zd bytes)\n",
295 inftl
->nb_blocks
* sizeof(u16
));
299 inftl
->VUtable
= kmalloc(inftl
->nb_blocks
* sizeof(u16
), GFP_KERNEL
);
300 if (!inftl
->VUtable
) {
301 kfree(inftl
->PUtable
);
302 printk(KERN_WARNING
"INFTL: allocation of VUtable "
303 "failed (%zd bytes)\n",
304 inftl
->nb_blocks
* sizeof(u16
));
308 /* Mark the blocks before INFTL MediaHeader as reserved */
309 for (i
= 0; i
< inftl
->nb_boot_blocks
; i
++)
310 inftl
->PUtable
[i
] = BLOCK_RESERVED
;
311 /* Mark all remaining blocks as potentially containing data */
312 for (; i
< inftl
->nb_blocks
; i
++)
313 inftl
->PUtable
[i
] = BLOCK_NOTEXPLORED
;
315 /* Mark this boot record (NFTL MediaHeader) block as reserved */
316 inftl
->PUtable
[block
] = BLOCK_RESERVED
;
318 /* Read Bad Erase Unit Table and modify PUtable[] accordingly */
319 for (i
= 0; i
< inftl
->nb_blocks
; i
++) {
321 /* If any of the physical eraseblocks are bad, don't
323 for (physblock
= 0; physblock
< inftl
->EraseSize
; physblock
+= inftl
->mbd
.mtd
->erasesize
) {
324 if (inftl
->mbd
.mtd
->block_isbad(inftl
->mbd
.mtd
, i
* inftl
->EraseSize
+ physblock
))
325 inftl
->PUtable
[i
] = BLOCK_RESERVED
;
329 inftl
->MediaUnit
= block
;
337 static int memcmpb(void *a
, int c
, int n
)
340 for (i
= 0; i
< n
; i
++) {
341 if (c
!= ((unsigned char *)a
)[i
])
348 * check_free_sector: check if a free sector is actually FREE,
349 * i.e. All 0xff in data and oob area.
351 static int check_free_sectors(struct INFTLrecord
*inftl
, unsigned int address
,
352 int len
, int check_oob
)
354 u8 buf
[SECTORSIZE
+ inftl
->mbd
.mtd
->oobsize
];
355 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
359 for (i
= 0; i
< len
; i
+= SECTORSIZE
) {
360 if (mtd
->read(mtd
, address
, SECTORSIZE
, &retlen
, buf
))
362 if (memcmpb(buf
, 0xff, SECTORSIZE
) != 0)
366 if(mtd
->read_oob(mtd
, address
, mtd
->oobsize
,
367 &retlen
, &buf
[SECTORSIZE
]) < 0)
369 if (memcmpb(buf
+ SECTORSIZE
, 0xff, mtd
->oobsize
) != 0)
372 address
+= SECTORSIZE
;
379 * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
380 * Unit and Update INFTL metadata. Each erase operation is
381 * checked with check_free_sectors.
383 * Return: 0 when succeed, -1 on error.
385 * ToDo: 1. Is it neceressary to check_free_sector after erasing ??
387 int INFTL_formatblock(struct INFTLrecord
*inftl
, int block
)
390 struct inftl_unittail uci
;
391 struct erase_info
*instr
= &inftl
->instr
;
392 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
395 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_formatblock(inftl=%p,"
396 "block=%d)\n", inftl
, block
);
398 memset(instr
, 0, sizeof(struct erase_info
));
400 /* FIXME: Shouldn't we be setting the 'discarded' flag to zero
403 /* Use async erase interface, test return code */
404 instr
->mtd
= inftl
->mbd
.mtd
;
405 instr
->addr
= block
* inftl
->EraseSize
;
406 instr
->len
= inftl
->mbd
.mtd
->erasesize
;
407 /* Erase one physical eraseblock at a time, even though the NAND api
408 allows us to group them. This way we if we have a failure, we can
409 mark only the failed block in the bbt. */
410 for (physblock
= 0; physblock
< inftl
->EraseSize
;
411 physblock
+= instr
->len
, instr
->addr
+= instr
->len
) {
412 mtd
->erase(inftl
->mbd
.mtd
, instr
);
414 if (instr
->state
== MTD_ERASE_FAILED
) {
415 printk(KERN_WARNING
"INFTL: error while formatting block %d\n",
421 * Check the "freeness" of Erase Unit before updating metadata.
422 * FixMe: is this check really necessary? Since we have check
423 * the return code after the erase operation.
425 if (check_free_sectors(inftl
, instr
->addr
, instr
->len
, 1) != 0)
429 uci
.EraseMark
= cpu_to_le16(ERASE_MARK
);
430 uci
.EraseMark1
= cpu_to_le16(ERASE_MARK
);
435 instr
->addr
= block
* inftl
->EraseSize
+ SECTORSIZE
* 2;
436 if (mtd
->write_oob(mtd
, instr
->addr
+ 8, 8, &retlen
, (char *)&uci
) < 0)
440 /* could not format, update the bad block table (caller is responsible
441 for setting the PUtable to BLOCK_RESERVED on failure) */
442 inftl
->mbd
.mtd
->block_markbad(inftl
->mbd
.mtd
, instr
->addr
);
447 * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
448 * Units in a Virtual Unit Chain, i.e. all the units are disconnected.
450 * Since the chain is invalid then we will have to erase it from its
451 * head (normally for INFTL we go from the oldest). But if it has a
452 * loop then there is no oldest...
454 static void format_chain(struct INFTLrecord
*inftl
, unsigned int first_block
)
456 unsigned int block
= first_block
, block1
;
458 printk(KERN_WARNING
"INFTL: formatting chain at block %d\n",
462 block1
= inftl
->PUtable
[block
];
464 printk(KERN_WARNING
"INFTL: formatting block %d\n", block
);
465 if (INFTL_formatblock(inftl
, block
) < 0) {
467 * Cannot format !!!! Mark it as Bad Unit,
469 inftl
->PUtable
[block
] = BLOCK_RESERVED
;
471 inftl
->PUtable
[block
] = BLOCK_FREE
;
474 /* Goto next block on the chain */
477 if (block
== BLOCK_NIL
|| block
>= inftl
->lastEUN
)
482 void INFTL_dumptables(struct INFTLrecord
*s
)
486 printk("-------------------------------------------"
487 "----------------------------------\n");
489 printk("VUtable[%d] ->", s
->nb_blocks
);
490 for (i
= 0; i
< s
->nb_blocks
; i
++) {
492 printk("\n%04x: ", i
);
493 printk("%04x ", s
->VUtable
[i
]);
496 printk("\n-------------------------------------------"
497 "----------------------------------\n");
499 printk("PUtable[%d-%d=%d] ->", s
->firstEUN
, s
->lastEUN
, s
->nb_blocks
);
500 for (i
= 0; i
<= s
->lastEUN
; i
++) {
502 printk("\n%04x: ", i
);
503 printk("%04x ", s
->PUtable
[i
]);
506 printk("\n-------------------------------------------"
507 "----------------------------------\n");
511 " h/s/c = %d/%d/%d\n"
515 " numfreeEUNs = %d\n"
516 " LastFreeEUN = %d\n"
518 " nb_boot_blocks = %d",
519 s
->EraseSize
, s
->heads
, s
->sectors
, s
->cylinders
,
520 s
->numvunits
, s
->firstEUN
, s
->lastEUN
, s
->numfreeEUNs
,
521 s
->LastFreeEUN
, s
->nb_blocks
, s
->nb_boot_blocks
);
523 printk("\n-------------------------------------------"
524 "----------------------------------\n");
527 void INFTL_dumpVUchains(struct INFTLrecord
*s
)
529 int logical
, block
, i
;
531 printk("-------------------------------------------"
532 "----------------------------------\n");
534 printk("INFTL Virtual Unit Chains:\n");
535 for (logical
= 0; logical
< s
->nb_blocks
; logical
++) {
536 block
= s
->VUtable
[logical
];
537 if (block
> s
->nb_blocks
)
539 printk(" LOGICAL %d --> %d ", logical
, block
);
540 for (i
= 0; i
< s
->nb_blocks
; i
++) {
541 if (s
->PUtable
[block
] == BLOCK_NIL
)
543 block
= s
->PUtable
[block
];
544 printk("%d ", block
);
549 printk("-------------------------------------------"
550 "----------------------------------\n");
553 int INFTL_mount(struct INFTLrecord
*s
)
555 struct mtd_info
*mtd
= s
->mbd
.mtd
;
556 unsigned int block
, first_block
, prev_block
, last_block
;
557 unsigned int first_logical_block
, logical_block
, erase_mark
;
558 int chain_length
, do_format_chain
;
559 struct inftl_unithead1 h0
;
560 struct inftl_unittail h1
;
565 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_mount(inftl=%p)\n", s
);
567 /* Search for INFTL MediaHeader and Spare INFTL Media Header */
568 if (find_boot_record(s
) < 0) {
569 printk(KERN_WARNING
"INFTL: could not find valid boot record?\n");
573 /* Init the logical to physical table */
574 for (i
= 0; i
< s
->nb_blocks
; i
++)
575 s
->VUtable
[i
] = BLOCK_NIL
;
577 logical_block
= block
= BLOCK_NIL
;
579 /* Temporary buffer to store ANAC numbers. */
580 ANACtable
= kmalloc(s
->nb_blocks
* sizeof(u8
), GFP_KERNEL
);
582 printk(KERN_WARNING
"INFTL: allocation of ANACtable "
583 "failed (%zd bytes)\n",
584 s
->nb_blocks
* sizeof(u8
));
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
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
)
602 first_logical_block
= BLOCK_NIL
;
603 last_block
= BLOCK_NIL
;
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 */
614 if (mtd
->read_oob(mtd
, block
* s
->EraseSize
+ 8,
615 8, &retlen
, (char *)&h0
) < 0 ||
616 mtd
->read_oob(mtd
, block
* s
->EraseSize
+
617 2 * SECTORSIZE
+ 8, 8, &retlen
,
619 /* Should never happen? */
624 logical_block
= le16_to_cpu(h0
.virtualUnitNo
);
625 prev_block
= le16_to_cpu(h0
.prevUnitNo
);
626 erase_mark
= le16_to_cpu((h1
.EraseMark
| h1
.EraseMark1
));
627 ANACtable
[block
] = h0
.ANAC
;
629 /* Previous block is relative to start of Partition */
630 if (prev_block
< s
->nb_blocks
)
631 prev_block
+= s
->firstEUN
;
633 /* Already explored partial chain? */
634 if (s
->PUtable
[block
] != BLOCK_NOTEXPLORED
) {
635 /* Check if chain for this logical */
636 if (logical_block
== first_logical_block
) {
637 if (last_block
!= BLOCK_NIL
)
638 s
->PUtable
[last_block
] = block
;
643 /* Check for invalid block */
644 if (erase_mark
!= ERASE_MARK
) {
645 printk(KERN_WARNING
"INFTL: corrupt block %d "
646 "in chain %d, chain length %d, erase "
647 "mark 0x%x?\n", block
, first_block
,
648 chain_length
, erase_mark
);
650 * Assume end of chain, probably incomplete
653 if (chain_length
== 0)
658 /* Check for it being free already then... */
659 if ((logical_block
== BLOCK_FREE
) ||
660 (logical_block
== BLOCK_NIL
)) {
661 s
->PUtable
[block
] = BLOCK_FREE
;
665 /* Sanity checks on block numbers */
666 if ((logical_block
>= s
->nb_blocks
) ||
667 ((prev_block
>= s
->nb_blocks
) &&
668 (prev_block
!= BLOCK_NIL
))) {
669 if (chain_length
> 0) {
670 printk(KERN_WARNING
"INFTL: corrupt "
671 "block %d in chain %d?\n",
678 if (first_logical_block
== BLOCK_NIL
) {
679 first_logical_block
= logical_block
;
681 if (first_logical_block
!= logical_block
) {
682 /* Normal for folded chain... */
688 * Current block is valid, so if we followed a virtual
689 * chain to get here then we can set the previous
690 * block pointer in our PUtable now. Then move onto
691 * the previous block in the chain.
693 s
->PUtable
[block
] = BLOCK_NIL
;
694 if (last_block
!= BLOCK_NIL
)
695 s
->PUtable
[last_block
] = block
;
699 /* Check for end of chain */
700 if (block
== BLOCK_NIL
)
703 /* Validate next block before following it... */
704 if (block
> s
->lastEUN
) {
705 printk(KERN_WARNING
"INFTL: invalid previous "
706 "block %d in chain %d?\n", block
,
713 if (do_format_chain
) {
714 format_chain(s
, first_block
);
719 * Looks like a valid chain then. It may not really be the
720 * newest block in the chain, but it is the newest we have
721 * found so far. We might update it in later iterations of
722 * this loop if we find something newer.
724 s
->VUtable
[first_logical_block
] = first_block
;
725 logical_block
= BLOCK_NIL
;
728 #ifdef CONFIG_MTD_DEBUG_VERBOSE
729 if (CONFIG_MTD_DEBUG_VERBOSE
>= 2)
734 * Second pass, check for infinite loops in chains. These are
735 * possible because we don't update the previous pointers when
736 * we fold chains. No big deal, just fix them up in PUtable.
738 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: pass 2, validate virtual chains\n");
739 for (logical_block
= 0; logical_block
< s
->numvunits
; logical_block
++) {
740 block
= s
->VUtable
[logical_block
];
741 last_block
= BLOCK_NIL
;
743 /* Check for free/reserved/nil */
744 if (block
>= BLOCK_RESERVED
)
747 ANAC
= ANACtable
[block
];
748 for (i
= 0; i
< s
->numvunits
; i
++) {
749 if (s
->PUtable
[block
] == BLOCK_NIL
)
751 if (s
->PUtable
[block
] > s
->lastEUN
) {
752 printk(KERN_WARNING
"INFTL: invalid prev %d, "
753 "in virtual chain %d\n",
754 s
->PUtable
[block
], logical_block
);
755 s
->PUtable
[block
] = BLOCK_NIL
;
758 if (ANACtable
[block
] != ANAC
) {
760 * Chain must point back to itself. This is ok,
761 * but we will need adjust the tables with this
762 * newest block and oldest block.
764 s
->VUtable
[logical_block
] = block
;
765 s
->PUtable
[last_block
] = BLOCK_NIL
;
771 block
= s
->PUtable
[block
];
774 if (i
>= s
->nb_blocks
) {
776 * Uhoo, infinite chain with valid ANACS!
777 * Format whole chain...
779 format_chain(s
, first_block
);
783 #ifdef CONFIG_MTD_DEBUG_VERBOSE
784 if (CONFIG_MTD_DEBUG_VERBOSE
>= 2)
786 if (CONFIG_MTD_DEBUG_VERBOSE
>= 2)
787 INFTL_dumpVUchains(s
);
791 * Third pass, format unreferenced blocks and init free block count.
794 s
->LastFreeEUN
= BLOCK_NIL
;
796 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: pass 3, format unused blocks\n");
797 for (block
= s
->firstEUN
; block
<= s
->lastEUN
; block
++) {
798 if (s
->PUtable
[block
] == BLOCK_NOTEXPLORED
) {
799 printk("INFTL: unreferenced block %d, formatting it\n",
801 if (INFTL_formatblock(s
, block
) < 0)
802 s
->PUtable
[block
] = BLOCK_RESERVED
;
804 s
->PUtable
[block
] = BLOCK_FREE
;
806 if (s
->PUtable
[block
] == BLOCK_FREE
) {
808 if (s
->LastFreeEUN
== BLOCK_NIL
)
809 s
->LastFreeEUN
= block
;