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