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