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