1 /* 2 * inftlmount.c -- INFTL mount code with extensive checks. 3 * 4 * Author: Greg Ungerer (gerg@snapgear.com) 5 * Copyright © 2002-2003, Greg Ungerer (gerg@snapgear.com) 6 * 7 * Based heavily on the nftlmount.c code which is: 8 * Author: Fabrice Bellard (fabrice.bellard@netgem.com) 9 * Copyright © 2000 Netgem S.A. 10 * 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. 15 * 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. 20 * 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 24 */ 25 26 #include <linux/kernel.h> 27 #include <linux/module.h> 28 #include <asm/errno.h> 29 #include <asm/io.h> 30 #include <linux/uaccess.h> 31 #include <linux/delay.h> 32 #include <linux/slab.h> 33 #include <linux/mtd/mtd.h> 34 #include <linux/mtd/nftl.h> 35 #include <linux/mtd/inftl.h> 36 37 /* 38 * find_boot_record: Find the INFTL Media Header and its Spare copy which 39 * contains the various device information of the INFTL partition and 40 * Bad Unit Table. Update the PUtable[] table according to the Bad 41 * Unit Table. PUtable[] is used for management of Erase Unit in 42 * other routines in inftlcore.c and inftlmount.c. 43 */ 44 static int find_boot_record(struct INFTLrecord *inftl) 45 { 46 struct inftl_unittail h1; 47 //struct inftl_oob oob; 48 unsigned int i, block; 49 u8 buf[SECTORSIZE]; 50 struct INFTLMediaHeader *mh = &inftl->MediaHdr; 51 struct mtd_info *mtd = inftl->mbd.mtd; 52 struct INFTLPartition *ip; 53 size_t retlen; 54 55 pr_debug("INFTL: find_boot_record(inftl=%p)\n", inftl); 56 57 /* 58 * Assume logical EraseSize == physical erasesize for starting the 59 * scan. We'll sort it out later if we find a MediaHeader which says 60 * otherwise. 61 */ 62 inftl->EraseSize = inftl->mbd.mtd->erasesize; 63 inftl->nb_blocks = (u32)inftl->mbd.mtd->size / inftl->EraseSize; 64 65 inftl->MediaUnit = BLOCK_NIL; 66 67 /* Search for a valid boot record */ 68 for (block = 0; block < inftl->nb_blocks; block++) { 69 int ret; 70 71 /* 72 * Check for BNAND header first. Then whinge if it's found 73 * but later checks fail. 74 */ 75 ret = mtd_read(mtd, block * inftl->EraseSize, SECTORSIZE, 76 &retlen, buf); 77 /* We ignore ret in case the ECC of the MediaHeader is invalid 78 (which is apparently acceptable) */ 79 if (retlen != SECTORSIZE) { 80 static int warncount = 5; 81 82 if (warncount) { 83 printk(KERN_WARNING "INFTL: block read at 0x%x " 84 "of mtd%d failed: %d\n", 85 block * inftl->EraseSize, 86 inftl->mbd.mtd->index, ret); 87 if (!--warncount) 88 printk(KERN_WARNING "INFTL: further " 89 "failures for this block will " 90 "not be printed\n"); 91 } 92 continue; 93 } 94 95 if (retlen < 6 || memcmp(buf, "BNAND", 6)) { 96 /* BNAND\0 not found. Continue */ 97 continue; 98 } 99 100 /* To be safer with BIOS, also use erase mark as discriminant */ 101 ret = inftl_read_oob(mtd, 102 block * inftl->EraseSize + SECTORSIZE + 8, 103 8, &retlen,(char *)&h1); 104 if (ret < 0) { 105 printk(KERN_WARNING "INFTL: ANAND header found at " 106 "0x%x in mtd%d, but OOB data read failed " 107 "(err %d)\n", block * inftl->EraseSize, 108 inftl->mbd.mtd->index, ret); 109 continue; 110 } 111 112 113 /* 114 * This is the first we've seen. 115 * Copy the media header structure into place. 116 */ 117 memcpy(mh, buf, sizeof(struct INFTLMediaHeader)); 118 119 /* Read the spare media header at offset 4096 */ 120 mtd_read(mtd, block * inftl->EraseSize + 4096, SECTORSIZE, 121 &retlen, buf); 122 if (retlen != SECTORSIZE) { 123 printk(KERN_WARNING "INFTL: Unable to read spare " 124 "Media Header\n"); 125 return -1; 126 } 127 /* Check if this one is the same as the first one we found. */ 128 if (memcmp(mh, buf, sizeof(struct INFTLMediaHeader))) { 129 printk(KERN_WARNING "INFTL: Primary and spare Media " 130 "Headers disagree.\n"); 131 return -1; 132 } 133 134 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks); 135 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions); 136 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions); 137 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits); 138 mh->FormatFlags = le32_to_cpu(mh->FormatFlags); 139 mh->PercentUsed = le32_to_cpu(mh->PercentUsed); 140 141 pr_debug("INFTL: Media Header ->\n" 142 " bootRecordID = %s\n" 143 " NoOfBootImageBlocks = %d\n" 144 " NoOfBinaryPartitions = %d\n" 145 " NoOfBDTLPartitions = %d\n" 146 " BlockMultiplerBits = %d\n" 147 " FormatFlgs = %d\n" 148 " OsakVersion = 0x%x\n" 149 " PercentUsed = %d\n", 150 mh->bootRecordID, mh->NoOfBootImageBlocks, 151 mh->NoOfBinaryPartitions, 152 mh->NoOfBDTLPartitions, 153 mh->BlockMultiplierBits, mh->FormatFlags, 154 mh->OsakVersion, mh->PercentUsed); 155 156 if (mh->NoOfBDTLPartitions == 0) { 157 printk(KERN_WARNING "INFTL: Media Header sanity check " 158 "failed: NoOfBDTLPartitions (%d) == 0, " 159 "must be at least 1\n", mh->NoOfBDTLPartitions); 160 return -1; 161 } 162 163 if ((mh->NoOfBDTLPartitions + mh->NoOfBinaryPartitions) > 4) { 164 printk(KERN_WARNING "INFTL: Media Header sanity check " 165 "failed: Total Partitions (%d) > 4, " 166 "BDTL=%d Binary=%d\n", mh->NoOfBDTLPartitions + 167 mh->NoOfBinaryPartitions, 168 mh->NoOfBDTLPartitions, 169 mh->NoOfBinaryPartitions); 170 return -1; 171 } 172 173 if (mh->BlockMultiplierBits > 1) { 174 printk(KERN_WARNING "INFTL: sorry, we don't support " 175 "UnitSizeFactor 0x%02x\n", 176 mh->BlockMultiplierBits); 177 return -1; 178 } else if (mh->BlockMultiplierBits == 1) { 179 printk(KERN_WARNING "INFTL: support for INFTL with " 180 "UnitSizeFactor 0x%02x is experimental\n", 181 mh->BlockMultiplierBits); 182 inftl->EraseSize = inftl->mbd.mtd->erasesize << 183 mh->BlockMultiplierBits; 184 inftl->nb_blocks = (u32)inftl->mbd.mtd->size / inftl->EraseSize; 185 block >>= mh->BlockMultiplierBits; 186 } 187 188 /* Scan the partitions */ 189 for (i = 0; (i < 4); i++) { 190 ip = &mh->Partitions[i]; 191 ip->virtualUnits = le32_to_cpu(ip->virtualUnits); 192 ip->firstUnit = le32_to_cpu(ip->firstUnit); 193 ip->lastUnit = le32_to_cpu(ip->lastUnit); 194 ip->flags = le32_to_cpu(ip->flags); 195 ip->spareUnits = le32_to_cpu(ip->spareUnits); 196 ip->Reserved0 = le32_to_cpu(ip->Reserved0); 197 198 pr_debug(" PARTITION[%d] ->\n" 199 " virtualUnits = %d\n" 200 " firstUnit = %d\n" 201 " lastUnit = %d\n" 202 " flags = 0x%x\n" 203 " spareUnits = %d\n", 204 i, ip->virtualUnits, ip->firstUnit, 205 ip->lastUnit, ip->flags, 206 ip->spareUnits); 207 208 if (ip->Reserved0 != ip->firstUnit) { 209 struct erase_info *instr = &inftl->instr; 210 211 /* 212 * Most likely this is using the 213 * undocumented qiuck mount feature. 214 * We don't support that, we will need 215 * to erase the hidden block for full 216 * compatibility. 217 */ 218 instr->addr = ip->Reserved0 * inftl->EraseSize; 219 instr->len = inftl->EraseSize; 220 mtd_erase(mtd, instr); 221 } 222 if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) { 223 printk(KERN_WARNING "INFTL: Media Header " 224 "Partition %d sanity check failed\n" 225 " firstUnit %d : lastUnit %d > " 226 "virtualUnits %d\n", i, ip->lastUnit, 227 ip->firstUnit, ip->Reserved0); 228 return -1; 229 } 230 if (ip->Reserved1 != 0) { 231 printk(KERN_WARNING "INFTL: Media Header " 232 "Partition %d sanity check failed: " 233 "Reserved1 %d != 0\n", 234 i, ip->Reserved1); 235 return -1; 236 } 237 238 if (ip->flags & INFTL_BDTL) 239 break; 240 } 241 242 if (i >= 4) { 243 printk(KERN_WARNING "INFTL: Media Header Partition " 244 "sanity check failed:\n No partition " 245 "marked as Disk Partition\n"); 246 return -1; 247 } 248 249 inftl->nb_boot_blocks = ip->firstUnit; 250 inftl->numvunits = ip->virtualUnits; 251 if (inftl->numvunits > (inftl->nb_blocks - 252 inftl->nb_boot_blocks - 2)) { 253 printk(KERN_WARNING "INFTL: Media Header sanity check " 254 "failed:\n numvunits (%d) > nb_blocks " 255 "(%d) - nb_boot_blocks(%d) - 2\n", 256 inftl->numvunits, inftl->nb_blocks, 257 inftl->nb_boot_blocks); 258 return -1; 259 } 260 261 inftl->mbd.size = inftl->numvunits * 262 (inftl->EraseSize / SECTORSIZE); 263 264 /* 265 * Block count is set to last used EUN (we won't need to keep 266 * any meta-data past that point). 267 */ 268 inftl->firstEUN = ip->firstUnit; 269 inftl->lastEUN = ip->lastUnit; 270 inftl->nb_blocks = ip->lastUnit + 1; 271 272 /* Memory alloc */ 273 inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL); 274 if (!inftl->PUtable) { 275 printk(KERN_WARNING "INFTL: allocation of PUtable " 276 "failed (%zd bytes)\n", 277 inftl->nb_blocks * sizeof(u16)); 278 return -ENOMEM; 279 } 280 281 inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL); 282 if (!inftl->VUtable) { 283 kfree(inftl->PUtable); 284 printk(KERN_WARNING "INFTL: allocation of VUtable " 285 "failed (%zd bytes)\n", 286 inftl->nb_blocks * sizeof(u16)); 287 return -ENOMEM; 288 } 289 290 /* Mark the blocks before INFTL MediaHeader as reserved */ 291 for (i = 0; i < inftl->nb_boot_blocks; i++) 292 inftl->PUtable[i] = BLOCK_RESERVED; 293 /* Mark all remaining blocks as potentially containing data */ 294 for (; i < inftl->nb_blocks; i++) 295 inftl->PUtable[i] = BLOCK_NOTEXPLORED; 296 297 /* Mark this boot record (NFTL MediaHeader) block as reserved */ 298 inftl->PUtable[block] = BLOCK_RESERVED; 299 300 /* Read Bad Erase Unit Table and modify PUtable[] accordingly */ 301 for (i = 0; i < inftl->nb_blocks; i++) { 302 int physblock; 303 /* If any of the physical eraseblocks are bad, don't 304 use the unit. */ 305 for (physblock = 0; physblock < inftl->EraseSize; physblock += inftl->mbd.mtd->erasesize) { 306 if (mtd_block_isbad(inftl->mbd.mtd, 307 i * inftl->EraseSize + physblock)) 308 inftl->PUtable[i] = BLOCK_RESERVED; 309 } 310 } 311 312 inftl->MediaUnit = block; 313 return 0; 314 } 315 316 /* Not found. */ 317 return -1; 318 } 319 320 static int memcmpb(void *a, int c, int n) 321 { 322 int i; 323 for (i = 0; i < n; i++) { 324 if (c != ((unsigned char *)a)[i]) 325 return 1; 326 } 327 return 0; 328 } 329 330 /* 331 * check_free_sector: check if a free sector is actually FREE, 332 * i.e. All 0xff in data and oob area. 333 */ 334 static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address, 335 int len, int check_oob) 336 { 337 struct mtd_info *mtd = inftl->mbd.mtd; 338 size_t retlen; 339 int i, ret; 340 u8 *buf; 341 342 buf = kmalloc(SECTORSIZE + mtd->oobsize, GFP_KERNEL); 343 if (!buf) 344 return -1; 345 346 ret = -1; 347 for (i = 0; i < len; i += SECTORSIZE) { 348 if (mtd_read(mtd, address, SECTORSIZE, &retlen, buf)) 349 goto out; 350 if (memcmpb(buf, 0xff, SECTORSIZE) != 0) 351 goto out; 352 353 if (check_oob) { 354 if(inftl_read_oob(mtd, address, mtd->oobsize, 355 &retlen, &buf[SECTORSIZE]) < 0) 356 goto out; 357 if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0) 358 goto out; 359 } 360 address += SECTORSIZE; 361 } 362 363 ret = 0; 364 365 out: 366 kfree(buf); 367 return ret; 368 } 369 370 /* 371 * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase 372 * Unit and Update INFTL metadata. Each erase operation is 373 * checked with check_free_sectors. 374 * 375 * Return: 0 when succeed, -1 on error. 376 * 377 * ToDo: 1. Is it necessary to check_free_sector after erasing ?? 378 */ 379 int INFTL_formatblock(struct INFTLrecord *inftl, int block) 380 { 381 size_t retlen; 382 struct inftl_unittail uci; 383 struct erase_info *instr = &inftl->instr; 384 struct mtd_info *mtd = inftl->mbd.mtd; 385 int physblock; 386 387 pr_debug("INFTL: INFTL_formatblock(inftl=%p,block=%d)\n", inftl, block); 388 389 memset(instr, 0, sizeof(struct erase_info)); 390 391 /* FIXME: Shouldn't we be setting the 'discarded' flag to zero 392 _first_? */ 393 394 /* Use async erase interface, test return code */ 395 instr->addr = block * inftl->EraseSize; 396 instr->len = inftl->mbd.mtd->erasesize; 397 /* Erase one physical eraseblock at a time, even though the NAND api 398 allows us to group them. This way we if we have a failure, we can 399 mark only the failed block in the bbt. */ 400 for (physblock = 0; physblock < inftl->EraseSize; 401 physblock += instr->len, instr->addr += instr->len) { 402 int ret; 403 404 ret = mtd_erase(inftl->mbd.mtd, instr); 405 if (ret) { 406 printk(KERN_WARNING "INFTL: error while formatting block %d\n", 407 block); 408 goto fail; 409 } 410 411 /* 412 * Check the "freeness" of Erase Unit before updating metadata. 413 * FixMe: is this check really necessary? Since we have check 414 * the return code after the erase operation. 415 */ 416 if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0) 417 goto fail; 418 } 419 420 uci.EraseMark = cpu_to_le16(ERASE_MARK); 421 uci.EraseMark1 = cpu_to_le16(ERASE_MARK); 422 uci.Reserved[0] = 0; 423 uci.Reserved[1] = 0; 424 uci.Reserved[2] = 0; 425 uci.Reserved[3] = 0; 426 instr->addr = block * inftl->EraseSize + SECTORSIZE * 2; 427 if (inftl_write_oob(mtd, instr->addr + 8, 8, &retlen, (char *)&uci) < 0) 428 goto fail; 429 return 0; 430 fail: 431 /* could not format, update the bad block table (caller is responsible 432 for setting the PUtable to BLOCK_RESERVED on failure) */ 433 mtd_block_markbad(inftl->mbd.mtd, instr->addr); 434 return -1; 435 } 436 437 /* 438 * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase 439 * Units in a Virtual Unit Chain, i.e. all the units are disconnected. 440 * 441 * Since the chain is invalid then we will have to erase it from its 442 * head (normally for INFTL we go from the oldest). But if it has a 443 * loop then there is no oldest... 444 */ 445 static void format_chain(struct INFTLrecord *inftl, unsigned int first_block) 446 { 447 unsigned int block = first_block, block1; 448 449 printk(KERN_WARNING "INFTL: formatting chain at block %d\n", 450 first_block); 451 452 for (;;) { 453 block1 = inftl->PUtable[block]; 454 455 printk(KERN_WARNING "INFTL: formatting block %d\n", block); 456 if (INFTL_formatblock(inftl, block) < 0) { 457 /* 458 * Cannot format !!!! Mark it as Bad Unit, 459 */ 460 inftl->PUtable[block] = BLOCK_RESERVED; 461 } else { 462 inftl->PUtable[block] = BLOCK_FREE; 463 } 464 465 /* Goto next block on the chain */ 466 block = block1; 467 468 if (block == BLOCK_NIL || block >= inftl->lastEUN) 469 break; 470 } 471 } 472 473 void INFTL_dumptables(struct INFTLrecord *s) 474 { 475 int i; 476 477 pr_debug("-------------------------------------------" 478 "----------------------------------\n"); 479 480 pr_debug("VUtable[%d] ->", s->nb_blocks); 481 for (i = 0; i < s->nb_blocks; i++) { 482 if ((i % 8) == 0) 483 pr_debug("\n%04x: ", i); 484 pr_debug("%04x ", s->VUtable[i]); 485 } 486 487 pr_debug("\n-------------------------------------------" 488 "----------------------------------\n"); 489 490 pr_debug("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks); 491 for (i = 0; i <= s->lastEUN; i++) { 492 if ((i % 8) == 0) 493 pr_debug("\n%04x: ", i); 494 pr_debug("%04x ", s->PUtable[i]); 495 } 496 497 pr_debug("\n-------------------------------------------" 498 "----------------------------------\n"); 499 500 pr_debug("INFTL ->\n" 501 " EraseSize = %d\n" 502 " h/s/c = %d/%d/%d\n" 503 " numvunits = %d\n" 504 " firstEUN = %d\n" 505 " lastEUN = %d\n" 506 " numfreeEUNs = %d\n" 507 " LastFreeEUN = %d\n" 508 " nb_blocks = %d\n" 509 " nb_boot_blocks = %d", 510 s->EraseSize, s->heads, s->sectors, s->cylinders, 511 s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs, 512 s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks); 513 514 pr_debug("\n-------------------------------------------" 515 "----------------------------------\n"); 516 } 517 518 void INFTL_dumpVUchains(struct INFTLrecord *s) 519 { 520 int logical, block, i; 521 522 pr_debug("-------------------------------------------" 523 "----------------------------------\n"); 524 525 pr_debug("INFTL Virtual Unit Chains:\n"); 526 for (logical = 0; logical < s->nb_blocks; logical++) { 527 block = s->VUtable[logical]; 528 if (block >= s->nb_blocks) 529 continue; 530 pr_debug(" LOGICAL %d --> %d ", logical, block); 531 for (i = 0; i < s->nb_blocks; i++) { 532 if (s->PUtable[block] == BLOCK_NIL) 533 break; 534 block = s->PUtable[block]; 535 pr_debug("%d ", block); 536 } 537 pr_debug("\n"); 538 } 539 540 pr_debug("-------------------------------------------" 541 "----------------------------------\n"); 542 } 543 544 int INFTL_mount(struct INFTLrecord *s) 545 { 546 struct mtd_info *mtd = s->mbd.mtd; 547 unsigned int block, first_block, prev_block, last_block; 548 unsigned int first_logical_block, logical_block, erase_mark; 549 int chain_length, do_format_chain; 550 struct inftl_unithead1 h0; 551 struct inftl_unittail h1; 552 size_t retlen; 553 int i; 554 u8 *ANACtable, ANAC; 555 556 pr_debug("INFTL: INFTL_mount(inftl=%p)\n", s); 557 558 /* Search for INFTL MediaHeader and Spare INFTL Media Header */ 559 if (find_boot_record(s) < 0) { 560 printk(KERN_WARNING "INFTL: could not find valid boot record?\n"); 561 return -ENXIO; 562 } 563 564 /* Init the logical to physical table */ 565 for (i = 0; i < s->nb_blocks; i++) 566 s->VUtable[i] = BLOCK_NIL; 567 568 logical_block = block = BLOCK_NIL; 569 570 /* Temporary buffer to store ANAC numbers. */ 571 ANACtable = kcalloc(s->nb_blocks, sizeof(u8), GFP_KERNEL); 572 if (!ANACtable) { 573 printk(KERN_WARNING "INFTL: allocation of ANACtable " 574 "failed (%zd bytes)\n", 575 s->nb_blocks * sizeof(u8)); 576 return -ENOMEM; 577 } 578 579 /* 580 * First pass is to explore each physical unit, and construct the 581 * virtual chains that exist (newest physical unit goes into VUtable). 582 * Any block that is in any way invalid will be left in the 583 * NOTEXPLORED state. Then at the end we will try to format it and 584 * mark it as free. 585 */ 586 pr_debug("INFTL: pass 1, explore each unit\n"); 587 for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) { 588 if (s->PUtable[first_block] != BLOCK_NOTEXPLORED) 589 continue; 590 591 do_format_chain = 0; 592 first_logical_block = BLOCK_NIL; 593 last_block = BLOCK_NIL; 594 block = first_block; 595 596 for (chain_length = 0; ; chain_length++) { 597 598 if ((chain_length == 0) && 599 (s->PUtable[block] != BLOCK_NOTEXPLORED)) { 600 /* Nothing to do here, onto next block */ 601 break; 602 } 603 604 if (inftl_read_oob(mtd, block * s->EraseSize + 8, 605 8, &retlen, (char *)&h0) < 0 || 606 inftl_read_oob(mtd, block * s->EraseSize + 607 2 * SECTORSIZE + 8, 8, &retlen, 608 (char *)&h1) < 0) { 609 /* Should never happen? */ 610 do_format_chain++; 611 break; 612 } 613 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; 618 619 /* Previous block is relative to start of Partition */ 620 if (prev_block < s->nb_blocks) 621 prev_block += s->firstEUN; 622 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; 629 } 630 break; 631 } 632 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); 639 /* 640 * Assume end of chain, probably incomplete 641 * fold/erase... 642 */ 643 if (chain_length == 0) 644 do_format_chain++; 645 break; 646 } 647 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; 652 break; 653 } 654 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", 662 block, first_block); 663 do_format_chain++; 664 } 665 break; 666 } 667 668 if (first_logical_block == BLOCK_NIL) { 669 first_logical_block = logical_block; 670 } else { 671 if (first_logical_block != logical_block) { 672 /* Normal for folded chain... */ 673 break; 674 } 675 } 676 677 /* 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. 682 */ 683 s->PUtable[block] = BLOCK_NIL; 684 if (last_block != BLOCK_NIL) 685 s->PUtable[last_block] = block; 686 last_block = block; 687 block = prev_block; 688 689 /* Check for end of chain */ 690 if (block == BLOCK_NIL) 691 break; 692 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, 697 first_block); 698 do_format_chain++; 699 break; 700 } 701 } 702 703 if (do_format_chain) { 704 format_chain(s, first_block); 705 continue; 706 } 707 708 /* 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. 713 */ 714 s->VUtable[first_logical_block] = first_block; 715 logical_block = BLOCK_NIL; 716 } 717 718 INFTL_dumptables(s); 719 720 /* 721 * Second pass, check for infinite loops in chains. These are 722 * possible because we don't update the previous pointers when 723 * we fold chains. No big deal, just fix them up in PUtable. 724 */ 725 pr_debug("INFTL: pass 2, validate virtual chains\n"); 726 for (logical_block = 0; logical_block < s->numvunits; logical_block++) { 727 block = s->VUtable[logical_block]; 728 last_block = BLOCK_NIL; 729 730 /* Check for free/reserved/nil */ 731 if (block >= BLOCK_RESERVED) 732 continue; 733 734 ANAC = ANACtable[block]; 735 for (i = 0; i < s->numvunits; i++) { 736 if (s->PUtable[block] == BLOCK_NIL) 737 break; 738 if (s->PUtable[block] > s->lastEUN) { 739 printk(KERN_WARNING "INFTL: invalid prev %d, " 740 "in virtual chain %d\n", 741 s->PUtable[block], logical_block); 742 s->PUtable[block] = BLOCK_NIL; 743 744 } 745 if (ANACtable[block] != ANAC) { 746 /* 747 * Chain must point back to itself. This is ok, 748 * but we will need adjust the tables with this 749 * newest block and oldest block. 750 */ 751 s->VUtable[logical_block] = block; 752 s->PUtable[last_block] = BLOCK_NIL; 753 break; 754 } 755 756 ANAC--; 757 last_block = block; 758 block = s->PUtable[block]; 759 } 760 761 if (i >= s->nb_blocks) { 762 /* 763 * Uhoo, infinite chain with valid ANACS! 764 * Format whole chain... 765 */ 766 format_chain(s, first_block); 767 } 768 } 769 770 INFTL_dumptables(s); 771 INFTL_dumpVUchains(s); 772 773 /* 774 * Third pass, format unreferenced blocks and init free block count. 775 */ 776 s->numfreeEUNs = 0; 777 s->LastFreeEUN = BLOCK_NIL; 778 779 pr_debug("INFTL: pass 3, format unused blocks\n"); 780 for (block = s->firstEUN; block <= s->lastEUN; block++) { 781 if (s->PUtable[block] == BLOCK_NOTEXPLORED) { 782 printk("INFTL: unreferenced block %d, formatting it\n", 783 block); 784 if (INFTL_formatblock(s, block) < 0) 785 s->PUtable[block] = BLOCK_RESERVED; 786 else 787 s->PUtable[block] = BLOCK_FREE; 788 } 789 if (s->PUtable[block] == BLOCK_FREE) { 790 s->numfreeEUNs++; 791 if (s->LastFreeEUN == BLOCK_NIL) 792 s->LastFreeEUN = block; 793 } 794 } 795 796 kfree(ANACtable); 797 return 0; 798 } 799