File: /usr/src/linux/drivers/mtd/nftlmount.c

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