nftlmount.c 26 KB

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