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