1 /* 2 * NFTL mount code with extensive checks 3 * 4 * Author: Fabrice Bellard (fabrice.bellard@netgem.com) 5 * Copyright © 2000 Netgem S.A. 6 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 #include <linux/kernel.h> 24 #include <asm/errno.h> 25 #include <linux/delay.h> 26 #include <linux/slab.h> 27 #include <linux/mtd/mtd.h> 28 #include <linux/mtd/rawnand.h> 29 #include <linux/mtd/nftl.h> 30 31 #define SECTORSIZE 512 32 33 /* find_boot_record: Find the NFTL Media Header and its Spare copy which contains the 34 * various device information of the NFTL partition and Bad Unit Table. Update 35 * the ReplUnitTable[] table according to the Bad Unit Table. ReplUnitTable[] 36 * is used for management of Erase Unit in other routines in nftl.c and nftlmount.c 37 */ 38 static int find_boot_record(struct NFTLrecord *nftl) 39 { 40 struct nftl_uci1 h1; 41 unsigned int block, boot_record_count = 0; 42 size_t retlen; 43 u8 buf[SECTORSIZE]; 44 struct NFTLMediaHeader *mh = &nftl->MediaHdr; 45 struct mtd_info *mtd = nftl->mbd.mtd; 46 unsigned int i; 47 48 /* Assume logical EraseSize == physical erasesize for starting the scan. 49 We'll sort it out later if we find a MediaHeader which says otherwise */ 50 /* Actually, we won't. The new DiskOnChip driver has already scanned 51 the MediaHeader and adjusted the virtual erasesize it presents in 52 the mtd device accordingly. We could even get rid of 53 nftl->EraseSize if there were any point in doing so. */ 54 nftl->EraseSize = nftl->mbd.mtd->erasesize; 55 nftl->nb_blocks = (u32)nftl->mbd.mtd->size / nftl->EraseSize; 56 57 nftl->MediaUnit = BLOCK_NIL; 58 nftl->SpareMediaUnit = BLOCK_NIL; 59 60 /* search for a valid boot record */ 61 for (block = 0; block < nftl->nb_blocks; block++) { 62 int ret; 63 64 /* Check for ANAND header first. Then can whinge if it's found but later 65 checks fail */ 66 ret = mtd_read(mtd, block * nftl->EraseSize, SECTORSIZE, 67 &retlen, buf); 68 /* We ignore ret in case the ECC of the MediaHeader is invalid 69 (which is apparently acceptable) */ 70 if (retlen != SECTORSIZE) { 71 static int warncount = 5; 72 73 if (warncount) { 74 printk(KERN_WARNING "Block read at 0x%x of mtd%d failed: %d\n", 75 block * nftl->EraseSize, nftl->mbd.mtd->index, ret); 76 if (!--warncount) 77 printk(KERN_WARNING "Further failures for this block will not be printed\n"); 78 } 79 continue; 80 } 81 82 if (retlen < 6 || memcmp(buf, "ANAND", 6)) { 83 /* ANAND\0 not found. Continue */ 84 #if 0 85 printk(KERN_DEBUG "ANAND header not found at 0x%x in mtd%d\n", 86 block * nftl->EraseSize, nftl->mbd.mtd->index); 87 #endif 88 continue; 89 } 90 91 /* To be safer with BIOS, also use erase mark as discriminant */ 92 ret = nftl_read_oob(mtd, block * nftl->EraseSize + 93 SECTORSIZE + 8, 8, &retlen, 94 (char *)&h1); 95 if (ret < 0) { 96 printk(KERN_WARNING "ANAND header found at 0x%x in mtd%d, but OOB data read failed (err %d)\n", 97 block * nftl->EraseSize, nftl->mbd.mtd->index, ret); 98 continue; 99 } 100 101 #if 0 /* Some people seem to have devices without ECC or erase marks 102 on the Media Header blocks. There are enough other sanity 103 checks in here that we can probably do without it. 104 */ 105 if (le16_to_cpu(h1.EraseMark | h1.EraseMark1) != ERASE_MARK) { 106 printk(KERN_NOTICE "ANAND header found at 0x%x in mtd%d, but erase mark not present (0x%04x,0x%04x instead)\n", 107 block * nftl->EraseSize, nftl->mbd.mtd->index, 108 le16_to_cpu(h1.EraseMark), le16_to_cpu(h1.EraseMark1)); 109 continue; 110 } 111 112 /* Finally reread to check ECC */ 113 ret = mtd->read(mtd, block * nftl->EraseSize, SECTORSIZE, 114 &retlen, buf); 115 if (ret < 0) { 116 printk(KERN_NOTICE "ANAND header found at 0x%x in mtd%d, but ECC read failed (err %d)\n", 117 block * nftl->EraseSize, nftl->mbd.mtd->index, ret); 118 continue; 119 } 120 121 /* Paranoia. Check the ANAND header is still there after the ECC read */ 122 if (memcmp(buf, "ANAND", 6)) { 123 printk(KERN_NOTICE "ANAND header found at 0x%x in mtd%d, but went away on reread!\n", 124 block * nftl->EraseSize, nftl->mbd.mtd->index); 125 printk(KERN_NOTICE "New data are: %02x %02x %02x %02x %02x %02x\n", 126 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); 127 continue; 128 } 129 #endif 130 /* OK, we like it. */ 131 132 if (boot_record_count) { 133 /* We've already processed one. So we just check if 134 this one is the same as the first one we found */ 135 if (memcmp(mh, buf, sizeof(struct NFTLMediaHeader))) { 136 printk(KERN_NOTICE "NFTL Media Headers at 0x%x and 0x%x disagree.\n", 137 nftl->MediaUnit * nftl->EraseSize, block * nftl->EraseSize); 138 /* if (debug) Print both side by side */ 139 if (boot_record_count < 2) { 140 /* We haven't yet seen two real ones */ 141 return -1; 142 } 143 continue; 144 } 145 if (boot_record_count == 1) 146 nftl->SpareMediaUnit = block; 147 148 /* Mark this boot record (NFTL MediaHeader) block as reserved */ 149 nftl->ReplUnitTable[block] = BLOCK_RESERVED; 150 151 152 boot_record_count++; 153 continue; 154 } 155 156 /* This is the first we've seen. Copy the media header structure into place */ 157 memcpy(mh, buf, sizeof(struct NFTLMediaHeader)); 158 159 /* Do some sanity checks on it */ 160 #if 0 161 The new DiskOnChip driver scans the MediaHeader itself, and presents a virtual 162 erasesize based on UnitSizeFactor. So the erasesize we read from the mtd 163 device is already correct. 164 if (mh->UnitSizeFactor == 0) { 165 printk(KERN_NOTICE "NFTL: UnitSizeFactor 0x00 detected. This violates the spec but we think we know what it means...\n"); 166 } else if (mh->UnitSizeFactor < 0xfc) { 167 printk(KERN_NOTICE "Sorry, we don't support UnitSizeFactor 0x%02x\n", 168 mh->UnitSizeFactor); 169 return -1; 170 } else if (mh->UnitSizeFactor != 0xff) { 171 printk(KERN_NOTICE "WARNING: Support for NFTL with UnitSizeFactor 0x%02x is experimental\n", 172 mh->UnitSizeFactor); 173 nftl->EraseSize = nftl->mbd.mtd->erasesize << (0xff - mh->UnitSizeFactor); 174 nftl->nb_blocks = (u32)nftl->mbd.mtd->size / nftl->EraseSize; 175 } 176 #endif 177 nftl->nb_boot_blocks = le16_to_cpu(mh->FirstPhysicalEUN); 178 if ((nftl->nb_boot_blocks + 2) >= nftl->nb_blocks) { 179 printk(KERN_NOTICE "NFTL Media Header sanity check failed:\n"); 180 printk(KERN_NOTICE "nb_boot_blocks (%d) + 2 > nb_blocks (%d)\n", 181 nftl->nb_boot_blocks, nftl->nb_blocks); 182 return -1; 183 } 184 185 nftl->numvunits = le32_to_cpu(mh->FormattedSize) / nftl->EraseSize; 186 if (nftl->numvunits > (nftl->nb_blocks - nftl->nb_boot_blocks - 2)) { 187 printk(KERN_NOTICE "NFTL Media Header sanity check failed:\n"); 188 printk(KERN_NOTICE "numvunits (%d) > nb_blocks (%d) - nb_boot_blocks(%d) - 2\n", 189 nftl->numvunits, nftl->nb_blocks, nftl->nb_boot_blocks); 190 return -1; 191 } 192 193 nftl->mbd.size = nftl->numvunits * (nftl->EraseSize / SECTORSIZE); 194 195 /* If we're not using the last sectors in the device for some reason, 196 reduce nb_blocks accordingly so we forget they're there */ 197 nftl->nb_blocks = le16_to_cpu(mh->NumEraseUnits) + le16_to_cpu(mh->FirstPhysicalEUN); 198 199 /* XXX: will be suppressed */ 200 nftl->lastEUN = nftl->nb_blocks - 1; 201 202 /* memory alloc */ 203 nftl->EUNtable = kmalloc(nftl->nb_blocks * sizeof(u16), GFP_KERNEL); 204 if (!nftl->EUNtable) { 205 printk(KERN_NOTICE "NFTL: allocation of EUNtable failed\n"); 206 return -ENOMEM; 207 } 208 209 nftl->ReplUnitTable = kmalloc(nftl->nb_blocks * sizeof(u16), GFP_KERNEL); 210 if (!nftl->ReplUnitTable) { 211 kfree(nftl->EUNtable); 212 printk(KERN_NOTICE "NFTL: allocation of ReplUnitTable failed\n"); 213 return -ENOMEM; 214 } 215 216 /* mark the bios blocks (blocks before NFTL MediaHeader) as reserved */ 217 for (i = 0; i < nftl->nb_boot_blocks; i++) 218 nftl->ReplUnitTable[i] = BLOCK_RESERVED; 219 /* mark all remaining blocks as potentially containing data */ 220 for (; i < nftl->nb_blocks; i++) { 221 nftl->ReplUnitTable[i] = BLOCK_NOTEXPLORED; 222 } 223 224 /* Mark this boot record (NFTL MediaHeader) block as reserved */ 225 nftl->ReplUnitTable[block] = BLOCK_RESERVED; 226 227 /* read the Bad Erase Unit Table and modify ReplUnitTable[] accordingly */ 228 for (i = 0; i < nftl->nb_blocks; i++) { 229 #if 0 230 The new DiskOnChip driver already scanned the bad block table. Just query it. 231 if ((i & (SECTORSIZE - 1)) == 0) { 232 /* read one sector for every SECTORSIZE of blocks */ 233 ret = mtd->read(nftl->mbd.mtd, 234 block * nftl->EraseSize + i + 235 SECTORSIZE, SECTORSIZE, 236 &retlen, buf); 237 if (ret < 0) { 238 printk(KERN_NOTICE "Read of bad sector table failed (err %d)\n", 239 ret); 240 kfree(nftl->ReplUnitTable); 241 kfree(nftl->EUNtable); 242 return -1; 243 } 244 } 245 /* mark the Bad Erase Unit as RESERVED in ReplUnitTable */ 246 if (buf[i & (SECTORSIZE - 1)] != 0xff) 247 nftl->ReplUnitTable[i] = BLOCK_RESERVED; 248 #endif 249 if (mtd_block_isbad(nftl->mbd.mtd, 250 i * nftl->EraseSize)) 251 nftl->ReplUnitTable[i] = BLOCK_RESERVED; 252 } 253 254 nftl->MediaUnit = block; 255 boot_record_count++; 256 257 } /* foreach (block) */ 258 259 return boot_record_count?0:-1; 260 } 261 262 static int memcmpb(void *a, int c, int n) 263 { 264 int i; 265 for (i = 0; i < n; i++) { 266 if (c != ((unsigned char *)a)[i]) 267 return 1; 268 } 269 return 0; 270 } 271 272 /* check_free_sector: check if a free sector is actually FREE, i.e. All 0xff in data and oob area */ 273 static int check_free_sectors(struct NFTLrecord *nftl, unsigned int address, int len, 274 int check_oob) 275 { 276 u8 buf[SECTORSIZE + nftl->mbd.mtd->oobsize]; 277 struct mtd_info *mtd = nftl->mbd.mtd; 278 size_t retlen; 279 int i; 280 281 for (i = 0; i < len; i += SECTORSIZE) { 282 if (mtd_read(mtd, address, SECTORSIZE, &retlen, buf)) 283 return -1; 284 if (memcmpb(buf, 0xff, SECTORSIZE) != 0) 285 return -1; 286 287 if (check_oob) { 288 if(nftl_read_oob(mtd, address, mtd->oobsize, 289 &retlen, &buf[SECTORSIZE]) < 0) 290 return -1; 291 if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0) 292 return -1; 293 } 294 address += SECTORSIZE; 295 } 296 297 return 0; 298 } 299 300 /* NFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase Unit and 301 * Update NFTL metadata. Each erase operation is checked with check_free_sectors 302 * 303 * Return: 0 when succeed, -1 on error. 304 * 305 * ToDo: 1. Is it necessary to check_free_sector after erasing ?? 306 */ 307 int NFTL_formatblock(struct NFTLrecord *nftl, int block) 308 { 309 size_t retlen; 310 unsigned int nb_erases, erase_mark; 311 struct nftl_uci1 uci; 312 struct erase_info *instr = &nftl->instr; 313 struct mtd_info *mtd = nftl->mbd.mtd; 314 315 /* Read the Unit Control Information #1 for Wear-Leveling */ 316 if (nftl_read_oob(mtd, block * nftl->EraseSize + SECTORSIZE + 8, 317 8, &retlen, (char *)&uci) < 0) 318 goto default_uci1; 319 320 erase_mark = le16_to_cpu ((uci.EraseMark | uci.EraseMark1)); 321 if (erase_mark != ERASE_MARK) { 322 default_uci1: 323 uci.EraseMark = cpu_to_le16(ERASE_MARK); 324 uci.EraseMark1 = cpu_to_le16(ERASE_MARK); 325 uci.WearInfo = cpu_to_le32(0); 326 } 327 328 memset(instr, 0, sizeof(struct erase_info)); 329 330 /* XXX: use async erase interface, XXX: test return code */ 331 instr->addr = block * nftl->EraseSize; 332 instr->len = nftl->EraseSize; 333 if (mtd_erase(mtd, instr)) { 334 printk("Error while formatting block %d\n", block); 335 goto fail; 336 } 337 338 /* increase and write Wear-Leveling info */ 339 nb_erases = le32_to_cpu(uci.WearInfo); 340 nb_erases++; 341 342 /* wrap (almost impossible with current flash) or free block */ 343 if (nb_erases == 0) 344 nb_erases = 1; 345 346 /* check the "freeness" of Erase Unit before updating metadata 347 * FixMe: is this check really necessary ? since we have check the 348 * return code after the erase operation. */ 349 if (check_free_sectors(nftl, instr->addr, nftl->EraseSize, 1) != 0) 350 goto fail; 351 352 uci.WearInfo = le32_to_cpu(nb_erases); 353 if (nftl_write_oob(mtd, block * nftl->EraseSize + SECTORSIZE + 354 8, 8, &retlen, (char *)&uci) < 0) 355 goto fail; 356 return 0; 357 fail: 358 /* could not format, update the bad block table (caller is responsible 359 for setting the ReplUnitTable to BLOCK_RESERVED on failure) */ 360 mtd_block_markbad(nftl->mbd.mtd, instr->addr); 361 return -1; 362 } 363 364 /* check_sectors_in_chain: Check that each sector of a Virtual Unit Chain is correct. 365 * Mark as 'IGNORE' each incorrect sector. This check is only done if the chain 366 * was being folded when NFTL was interrupted. 367 * 368 * The check_free_sectors in this function is necessary. There is a possible 369 * situation that after writing the Data area, the Block Control Information is 370 * not updated according (due to power failure or something) which leaves the block 371 * in an inconsistent state. So we have to check if a block is really FREE in this 372 * case. */ 373 static void check_sectors_in_chain(struct NFTLrecord *nftl, unsigned int first_block) 374 { 375 struct mtd_info *mtd = nftl->mbd.mtd; 376 unsigned int block, i, status; 377 struct nftl_bci bci; 378 int sectors_per_block; 379 size_t retlen; 380 381 sectors_per_block = nftl->EraseSize / SECTORSIZE; 382 block = first_block; 383 for (;;) { 384 for (i = 0; i < sectors_per_block; i++) { 385 if (nftl_read_oob(mtd, 386 block * nftl->EraseSize + i * SECTORSIZE, 387 8, &retlen, (char *)&bci) < 0) 388 status = SECTOR_IGNORE; 389 else 390 status = bci.Status | bci.Status1; 391 392 switch(status) { 393 case SECTOR_FREE: 394 /* verify that the sector is really free. If not, mark 395 as ignore */ 396 if (memcmpb(&bci, 0xff, 8) != 0 || 397 check_free_sectors(nftl, block * nftl->EraseSize + i * SECTORSIZE, 398 SECTORSIZE, 0) != 0) { 399 printk("Incorrect free sector %d in block %d: " 400 "marking it as ignored\n", 401 i, block); 402 403 /* sector not free actually : mark it as SECTOR_IGNORE */ 404 bci.Status = SECTOR_IGNORE; 405 bci.Status1 = SECTOR_IGNORE; 406 nftl_write_oob(mtd, block * 407 nftl->EraseSize + 408 i * SECTORSIZE, 8, 409 &retlen, (char *)&bci); 410 } 411 break; 412 default: 413 break; 414 } 415 } 416 417 /* proceed to next Erase Unit on the chain */ 418 block = nftl->ReplUnitTable[block]; 419 if (!(block == BLOCK_NIL || block < nftl->nb_blocks)) 420 printk("incorrect ReplUnitTable[] : %d\n", block); 421 if (block == BLOCK_NIL || block >= nftl->nb_blocks) 422 break; 423 } 424 } 425 426 /* calc_chain_length: Walk through a Virtual Unit Chain and estimate chain length */ 427 static int calc_chain_length(struct NFTLrecord *nftl, unsigned int first_block) 428 { 429 unsigned int length = 0, block = first_block; 430 431 for (;;) { 432 length++; 433 /* avoid infinite loops, although this is guaranteed not to 434 happen because of the previous checks */ 435 if (length >= nftl->nb_blocks) { 436 printk("nftl: length too long %d !\n", length); 437 break; 438 } 439 440 block = nftl->ReplUnitTable[block]; 441 if (!(block == BLOCK_NIL || block < nftl->nb_blocks)) 442 printk("incorrect ReplUnitTable[] : %d\n", block); 443 if (block == BLOCK_NIL || block >= nftl->nb_blocks) 444 break; 445 } 446 return length; 447 } 448 449 /* format_chain: Format an invalid Virtual Unit chain. It frees all the Erase Units in a 450 * Virtual Unit Chain, i.e. all the units are disconnected. 451 * 452 * It is not strictly correct to begin from the first block of the chain because 453 * if we stop the code, we may see again a valid chain if there was a first_block 454 * flag in a block inside it. But is it really a problem ? 455 * 456 * FixMe: Figure out what the last statement means. What if power failure when we are 457 * in the for (;;) loop formatting blocks ?? 458 */ 459 static void format_chain(struct NFTLrecord *nftl, unsigned int first_block) 460 { 461 unsigned int block = first_block, block1; 462 463 printk("Formatting chain at block %d\n", first_block); 464 465 for (;;) { 466 block1 = nftl->ReplUnitTable[block]; 467 468 printk("Formatting block %d\n", block); 469 if (NFTL_formatblock(nftl, block) < 0) { 470 /* cannot format !!!! Mark it as Bad Unit */ 471 nftl->ReplUnitTable[block] = BLOCK_RESERVED; 472 } else { 473 nftl->ReplUnitTable[block] = BLOCK_FREE; 474 } 475 476 /* goto next block on the chain */ 477 block = block1; 478 479 if (!(block == BLOCK_NIL || block < nftl->nb_blocks)) 480 printk("incorrect ReplUnitTable[] : %d\n", block); 481 if (block == BLOCK_NIL || block >= nftl->nb_blocks) 482 break; 483 } 484 } 485 486 /* check_and_mark_free_block: Verify that a block is free in the NFTL sense (valid erase mark) or 487 * totally free (only 0xff). 488 * 489 * Definition: Free Erase Unit -- A properly erased/formatted Free Erase Unit should have meet the 490 * following criteria: 491 * 1. */ 492 static int check_and_mark_free_block(struct NFTLrecord *nftl, int block) 493 { 494 struct mtd_info *mtd = nftl->mbd.mtd; 495 struct nftl_uci1 h1; 496 unsigned int erase_mark; 497 size_t retlen; 498 499 /* check erase mark. */ 500 if (nftl_read_oob(mtd, block * nftl->EraseSize + SECTORSIZE + 8, 8, 501 &retlen, (char *)&h1) < 0) 502 return -1; 503 504 erase_mark = le16_to_cpu ((h1.EraseMark | h1.EraseMark1)); 505 if (erase_mark != ERASE_MARK) { 506 /* if no erase mark, the block must be totally free. This is 507 possible in two cases : empty filesystem or interrupted erase (very unlikely) */ 508 if (check_free_sectors (nftl, block * nftl->EraseSize, nftl->EraseSize, 1) != 0) 509 return -1; 510 511 /* free block : write erase mark */ 512 h1.EraseMark = cpu_to_le16(ERASE_MARK); 513 h1.EraseMark1 = cpu_to_le16(ERASE_MARK); 514 h1.WearInfo = cpu_to_le32(0); 515 if (nftl_write_oob(mtd, 516 block * nftl->EraseSize + SECTORSIZE + 8, 8, 517 &retlen, (char *)&h1) < 0) 518 return -1; 519 } else { 520 #if 0 521 /* if erase mark present, need to skip it when doing check */ 522 for (i = 0; i < nftl->EraseSize; i += SECTORSIZE) { 523 /* check free sector */ 524 if (check_free_sectors (nftl, block * nftl->EraseSize + i, 525 SECTORSIZE, 0) != 0) 526 return -1; 527 528 if (nftl_read_oob(mtd, block * nftl->EraseSize + i, 529 16, &retlen, buf) < 0) 530 return -1; 531 if (i == SECTORSIZE) { 532 /* skip erase mark */ 533 if (memcmpb(buf, 0xff, 8)) 534 return -1; 535 } else { 536 if (memcmpb(buf, 0xff, 16)) 537 return -1; 538 } 539 } 540 #endif 541 } 542 543 return 0; 544 } 545 546 /* get_fold_mark: Read fold mark from Unit Control Information #2, we use FOLD_MARK_IN_PROGRESS 547 * to indicate that we are in the progression of a Virtual Unit Chain folding. If the UCI #2 548 * is FOLD_MARK_IN_PROGRESS when mounting the NFTL, the (previous) folding process is interrupted 549 * for some reason. A clean up/check of the VUC is necessary in this case. 550 * 551 * WARNING: return 0 if read error 552 */ 553 static int get_fold_mark(struct NFTLrecord *nftl, unsigned int block) 554 { 555 struct mtd_info *mtd = nftl->mbd.mtd; 556 struct nftl_uci2 uci; 557 size_t retlen; 558 559 if (nftl_read_oob(mtd, block * nftl->EraseSize + 2 * SECTORSIZE + 8, 560 8, &retlen, (char *)&uci) < 0) 561 return 0; 562 563 return le16_to_cpu((uci.FoldMark | uci.FoldMark1)); 564 } 565 566 int NFTL_mount(struct NFTLrecord *s) 567 { 568 int i; 569 unsigned int first_logical_block, logical_block, rep_block, nb_erases, erase_mark; 570 unsigned int block, first_block, is_first_block; 571 int chain_length, do_format_chain; 572 struct nftl_uci0 h0; 573 struct nftl_uci1 h1; 574 struct mtd_info *mtd = s->mbd.mtd; 575 size_t retlen; 576 577 /* search for NFTL MediaHeader and Spare NFTL Media Header */ 578 if (find_boot_record(s) < 0) { 579 printk("Could not find valid boot record\n"); 580 return -1; 581 } 582 583 /* init the logical to physical table */ 584 for (i = 0; i < s->nb_blocks; i++) { 585 s->EUNtable[i] = BLOCK_NIL; 586 } 587 588 /* first pass : explore each block chain */ 589 first_logical_block = 0; 590 for (first_block = 0; first_block < s->nb_blocks; first_block++) { 591 /* if the block was not already explored, we can look at it */ 592 if (s->ReplUnitTable[first_block] == BLOCK_NOTEXPLORED) { 593 block = first_block; 594 chain_length = 0; 595 do_format_chain = 0; 596 597 for (;;) { 598 /* read the block header. If error, we format the chain */ 599 if (nftl_read_oob(mtd, 600 block * s->EraseSize + 8, 8, 601 &retlen, (char *)&h0) < 0 || 602 nftl_read_oob(mtd, 603 block * s->EraseSize + 604 SECTORSIZE + 8, 8, 605 &retlen, (char *)&h1) < 0) { 606 s->ReplUnitTable[block] = BLOCK_NIL; 607 do_format_chain = 1; 608 break; 609 } 610 611 logical_block = le16_to_cpu ((h0.VirtUnitNum | h0.SpareVirtUnitNum)); 612 rep_block = le16_to_cpu ((h0.ReplUnitNum | h0.SpareReplUnitNum)); 613 nb_erases = le32_to_cpu (h1.WearInfo); 614 erase_mark = le16_to_cpu ((h1.EraseMark | h1.EraseMark1)); 615 616 is_first_block = !(logical_block >> 15); 617 logical_block = logical_block & 0x7fff; 618 619 /* invalid/free block test */ 620 if (erase_mark != ERASE_MARK || logical_block >= s->nb_blocks) { 621 if (chain_length == 0) { 622 /* if not currently in a chain, we can handle it safely */ 623 if (check_and_mark_free_block(s, block) < 0) { 624 /* not really free: format it */ 625 printk("Formatting block %d\n", block); 626 if (NFTL_formatblock(s, block) < 0) { 627 /* could not format: reserve the block */ 628 s->ReplUnitTable[block] = BLOCK_RESERVED; 629 } else { 630 s->ReplUnitTable[block] = BLOCK_FREE; 631 } 632 } else { 633 /* free block: mark it */ 634 s->ReplUnitTable[block] = BLOCK_FREE; 635 } 636 /* directly examine the next block. */ 637 goto examine_ReplUnitTable; 638 } else { 639 /* the block was in a chain : this is bad. We 640 must format all the chain */ 641 printk("Block %d: free but referenced in chain %d\n", 642 block, first_block); 643 s->ReplUnitTable[block] = BLOCK_NIL; 644 do_format_chain = 1; 645 break; 646 } 647 } 648 649 /* we accept only first blocks here */ 650 if (chain_length == 0) { 651 /* this block is not the first block in chain : 652 ignore it, it will be included in a chain 653 later, or marked as not explored */ 654 if (!is_first_block) 655 goto examine_ReplUnitTable; 656 first_logical_block = logical_block; 657 } else { 658 if (logical_block != first_logical_block) { 659 printk("Block %d: incorrect logical block: %d expected: %d\n", 660 block, logical_block, first_logical_block); 661 /* the chain is incorrect : we must format it, 662 but we need to read it completely */ 663 do_format_chain = 1; 664 } 665 if (is_first_block) { 666 /* we accept that a block is marked as first 667 block while being last block in a chain 668 only if the chain is being folded */ 669 if (get_fold_mark(s, block) != FOLD_MARK_IN_PROGRESS || 670 rep_block != 0xffff) { 671 printk("Block %d: incorrectly marked as first block in chain\n", 672 block); 673 /* the chain is incorrect : we must format it, 674 but we need to read it completely */ 675 do_format_chain = 1; 676 } else { 677 printk("Block %d: folding in progress - ignoring first block flag\n", 678 block); 679 } 680 } 681 } 682 chain_length++; 683 if (rep_block == 0xffff) { 684 /* no more blocks after */ 685 s->ReplUnitTable[block] = BLOCK_NIL; 686 break; 687 } else if (rep_block >= s->nb_blocks) { 688 printk("Block %d: referencing invalid block %d\n", 689 block, rep_block); 690 do_format_chain = 1; 691 s->ReplUnitTable[block] = BLOCK_NIL; 692 break; 693 } else if (s->ReplUnitTable[rep_block] != BLOCK_NOTEXPLORED) { 694 /* same problem as previous 'is_first_block' test: 695 we accept that the last block of a chain has 696 the first_block flag set if folding is in 697 progress. We handle here the case where the 698 last block appeared first */ 699 if (s->ReplUnitTable[rep_block] == BLOCK_NIL && 700 s->EUNtable[first_logical_block] == rep_block && 701 get_fold_mark(s, first_block) == FOLD_MARK_IN_PROGRESS) { 702 /* EUNtable[] will be set after */ 703 printk("Block %d: folding in progress - ignoring first block flag\n", 704 rep_block); 705 s->ReplUnitTable[block] = rep_block; 706 s->EUNtable[first_logical_block] = BLOCK_NIL; 707 } else { 708 printk("Block %d: referencing block %d already in another chain\n", 709 block, rep_block); 710 /* XXX: should handle correctly fold in progress chains */ 711 do_format_chain = 1; 712 s->ReplUnitTable[block] = BLOCK_NIL; 713 } 714 break; 715 } else { 716 /* this is OK */ 717 s->ReplUnitTable[block] = rep_block; 718 block = rep_block; 719 } 720 } 721 722 /* the chain was completely explored. Now we can decide 723 what to do with it */ 724 if (do_format_chain) { 725 /* invalid chain : format it */ 726 format_chain(s, first_block); 727 } else { 728 unsigned int first_block1, chain_to_format, chain_length1; 729 int fold_mark; 730 731 /* valid chain : get foldmark */ 732 fold_mark = get_fold_mark(s, first_block); 733 if (fold_mark == 0) { 734 /* cannot get foldmark : format the chain */ 735 printk("Could read foldmark at block %d\n", first_block); 736 format_chain(s, first_block); 737 } else { 738 if (fold_mark == FOLD_MARK_IN_PROGRESS) 739 check_sectors_in_chain(s, first_block); 740 741 /* now handle the case where we find two chains at the 742 same virtual address : we select the longer one, 743 because the shorter one is the one which was being 744 folded if the folding was not done in place */ 745 first_block1 = s->EUNtable[first_logical_block]; 746 if (first_block1 != BLOCK_NIL) { 747 /* XXX: what to do if same length ? */ 748 chain_length1 = calc_chain_length(s, first_block1); 749 printk("Two chains at blocks %d (len=%d) and %d (len=%d)\n", 750 first_block1, chain_length1, first_block, chain_length); 751 752 if (chain_length >= chain_length1) { 753 chain_to_format = first_block1; 754 s->EUNtable[first_logical_block] = first_block; 755 } else { 756 chain_to_format = first_block; 757 } 758 format_chain(s, chain_to_format); 759 } else { 760 s->EUNtable[first_logical_block] = first_block; 761 } 762 } 763 } 764 } 765 examine_ReplUnitTable:; 766 } 767 768 /* second pass to format unreferenced blocks and init free block count */ 769 s->numfreeEUNs = 0; 770 s->LastFreeEUN = le16_to_cpu(s->MediaHdr.FirstPhysicalEUN); 771 772 for (block = 0; block < s->nb_blocks; block++) { 773 if (s->ReplUnitTable[block] == BLOCK_NOTEXPLORED) { 774 printk("Unreferenced block %d, formatting it\n", block); 775 if (NFTL_formatblock(s, block) < 0) 776 s->ReplUnitTable[block] = BLOCK_RESERVED; 777 else 778 s->ReplUnitTable[block] = BLOCK_FREE; 779 } 780 if (s->ReplUnitTable[block] == BLOCK_FREE) { 781 s->numfreeEUNs++; 782 s->LastFreeEUN = block; 783 } 784 } 785 786 return 0; 787 } 788