blob: 6281da3dadaca4a8b50cd4da57ec060b381b9b6e [file] [log] [blame]
Thomas Gleixner97894cd2005-11-07 11:15:26 +00001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * NFTL mount code with extensive checks
3 *
Thomas Gleixner97894cd2005-11-07 11:15:26 +00004 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
David Woodhousea1452a32010-08-08 20:58:20 +01005 * Copyright © 2000 Netgem S.A.
6 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <linux/kernel.h>
24#include <asm/errno.h>
25#include <linux/delay.h>
26#include <linux/slab.h>
27#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020028#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mtd/nftl.h>
30
31#define SECTORSIZE 512
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* find_boot_record: Find the NFTL Media Header and its Spare copy which contains the
34 * various device information of the NFTL partition and Bad Unit Table. Update
Brian Norris92394b5c2011-07-20 09:53:42 -070035 * the ReplUnitTable[] table according to the Bad Unit Table. ReplUnitTable[]
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * is used for management of Erase Unit in other routines in nftl.c and nftlmount.c
37 */
38static int find_boot_record(struct NFTLrecord *nftl)
39{
40 struct nftl_uci1 h1;
41 unsigned int block, boot_record_count = 0;
42 size_t retlen;
43 u8 buf[SECTORSIZE];
44 struct NFTLMediaHeader *mh = &nftl->MediaHdr;
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +020045 struct mtd_info *mtd = nftl->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 unsigned int i;
47
Thomas Gleixner97894cd2005-11-07 11:15:26 +000048 /* Assume logical EraseSize == physical erasesize for starting the scan.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 We'll sort it out later if we find a MediaHeader which says otherwise */
50 /* Actually, we won't. The new DiskOnChip driver has already scanned
51 the MediaHeader and adjusted the virtual erasesize it presents in
52 the mtd device accordingly. We could even get rid of
53 nftl->EraseSize if there were any point in doing so. */
54 nftl->EraseSize = nftl->mbd.mtd->erasesize;
Adrian Hunter69423d92008-12-10 13:37:21 +000055 nftl->nb_blocks = (u32)nftl->mbd.mtd->size / nftl->EraseSize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 nftl->MediaUnit = BLOCK_NIL;
58 nftl->SpareMediaUnit = BLOCK_NIL;
59
60 /* search for a valid boot record */
61 for (block = 0; block < nftl->nb_blocks; block++) {
62 int ret;
63
64 /* Check for ANAND header first. Then can whinge if it's found but later
65 checks fail */
Artem Bityutskiy329ad392011-12-23 17:30:16 +020066 ret = mtd_read(mtd, block * nftl->EraseSize, SECTORSIZE,
67 &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /* We ignore ret in case the ECC of the MediaHeader is invalid
69 (which is apparently acceptable) */
70 if (retlen != SECTORSIZE) {
71 static int warncount = 5;
72
73 if (warncount) {
74 printk(KERN_WARNING "Block read at 0x%x of mtd%d failed: %d\n",
75 block * nftl->EraseSize, nftl->mbd.mtd->index, ret);
76 if (!--warncount)
77 printk(KERN_WARNING "Further failures for this block will not be printed\n");
78 }
79 continue;
80 }
81
82 if (retlen < 6 || memcmp(buf, "ANAND", 6)) {
83 /* ANAND\0 not found. Continue */
84#if 0
Thomas Gleixner97894cd2005-11-07 11:15:26 +000085 printk(KERN_DEBUG "ANAND header not found at 0x%x in mtd%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 block * nftl->EraseSize, nftl->mbd.mtd->index);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000087#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 continue;
89 }
90
91 /* To be safer with BIOS, also use erase mark as discriminant */
Andy Shevchenko768c57c2015-01-07 22:37:20 +020092 ret = nftl_read_oob(mtd, block * nftl->EraseSize +
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +020093 SECTORSIZE + 8, 8, &retlen,
Andy Shevchenko768c57c2015-01-07 22:37:20 +020094 (char *)&h1);
95 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 printk(KERN_WARNING "ANAND header found at 0x%x in mtd%d, but OOB data read failed (err %d)\n",
97 block * nftl->EraseSize, nftl->mbd.mtd->index, ret);
98 continue;
99 }
100
101#if 0 /* Some people seem to have devices without ECC or erase marks
102 on the Media Header blocks. There are enough other sanity
103 checks in here that we can probably do without it.
104 */
105 if (le16_to_cpu(h1.EraseMark | h1.EraseMark1) != ERASE_MARK) {
106 printk(KERN_NOTICE "ANAND header found at 0x%x in mtd%d, but erase mark not present (0x%04x,0x%04x instead)\n",
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000107 block * nftl->EraseSize, nftl->mbd.mtd->index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 le16_to_cpu(h1.EraseMark), le16_to_cpu(h1.EraseMark1));
109 continue;
110 }
111
112 /* Finally reread to check ECC */
Andy Shevchenko768c57c2015-01-07 22:37:20 +0200113 ret = mtd->read(mtd, block * nftl->EraseSize, SECTORSIZE,
114 &retlen, buf);
115 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 printk(KERN_NOTICE "ANAND header found at 0x%x in mtd%d, but ECC read failed (err %d)\n",
117 block * nftl->EraseSize, nftl->mbd.mtd->index, ret);
118 continue;
119 }
120
121 /* Paranoia. Check the ANAND header is still there after the ECC read */
122 if (memcmp(buf, "ANAND", 6)) {
123 printk(KERN_NOTICE "ANAND header found at 0x%x in mtd%d, but went away on reread!\n",
124 block * nftl->EraseSize, nftl->mbd.mtd->index);
Antonio Cardaceac9cd362018-02-13 17:52:45 +0000125 printk(KERN_NOTICE "New data are: %6ph\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 continue;
127 }
128#endif
129 /* OK, we like it. */
130
131 if (boot_record_count) {
132 /* We've already processed one. So we just check if
133 this one is the same as the first one we found */
134 if (memcmp(mh, buf, sizeof(struct NFTLMediaHeader))) {
135 printk(KERN_NOTICE "NFTL Media Headers at 0x%x and 0x%x disagree.\n",
136 nftl->MediaUnit * nftl->EraseSize, block * nftl->EraseSize);
137 /* if (debug) Print both side by side */
138 if (boot_record_count < 2) {
139 /* We haven't yet seen two real ones */
140 return -1;
141 }
142 continue;
143 }
144 if (boot_record_count == 1)
145 nftl->SpareMediaUnit = block;
146
147 /* Mark this boot record (NFTL MediaHeader) block as reserved */
148 nftl->ReplUnitTable[block] = BLOCK_RESERVED;
149
150
151 boot_record_count++;
152 continue;
153 }
154
155 /* This is the first we've seen. Copy the media header structure into place */
156 memcpy(mh, buf, sizeof(struct NFTLMediaHeader));
157
158 /* Do some sanity checks on it */
159#if 0
160The new DiskOnChip driver scans the MediaHeader itself, and presents a virtual
161erasesize based on UnitSizeFactor. So the erasesize we read from the mtd
162device is already correct.
163 if (mh->UnitSizeFactor == 0) {
164 printk(KERN_NOTICE "NFTL: UnitSizeFactor 0x00 detected. This violates the spec but we think we know what it means...\n");
165 } else if (mh->UnitSizeFactor < 0xfc) {
166 printk(KERN_NOTICE "Sorry, we don't support UnitSizeFactor 0x%02x\n",
167 mh->UnitSizeFactor);
168 return -1;
169 } else if (mh->UnitSizeFactor != 0xff) {
170 printk(KERN_NOTICE "WARNING: Support for NFTL with UnitSizeFactor 0x%02x is experimental\n",
171 mh->UnitSizeFactor);
172 nftl->EraseSize = nftl->mbd.mtd->erasesize << (0xff - mh->UnitSizeFactor);
Adrian Hunter69423d92008-12-10 13:37:21 +0000173 nftl->nb_blocks = (u32)nftl->mbd.mtd->size / nftl->EraseSize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175#endif
176 nftl->nb_boot_blocks = le16_to_cpu(mh->FirstPhysicalEUN);
177 if ((nftl->nb_boot_blocks + 2) >= nftl->nb_blocks) {
178 printk(KERN_NOTICE "NFTL Media Header sanity check failed:\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000179 printk(KERN_NOTICE "nb_boot_blocks (%d) + 2 > nb_blocks (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 nftl->nb_boot_blocks, nftl->nb_blocks);
181 return -1;
182 }
183
184 nftl->numvunits = le32_to_cpu(mh->FormattedSize) / nftl->EraseSize;
185 if (nftl->numvunits > (nftl->nb_blocks - nftl->nb_boot_blocks - 2)) {
186 printk(KERN_NOTICE "NFTL Media Header sanity check failed:\n");
187 printk(KERN_NOTICE "numvunits (%d) > nb_blocks (%d) - nb_boot_blocks(%d) - 2\n",
188 nftl->numvunits, nftl->nb_blocks, nftl->nb_boot_blocks);
189 return -1;
190 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 nftl->mbd.size = nftl->numvunits * (nftl->EraseSize / SECTORSIZE);
193
194 /* If we're not using the last sectors in the device for some reason,
195 reduce nb_blocks accordingly so we forget they're there */
196 nftl->nb_blocks = le16_to_cpu(mh->NumEraseUnits) + le16_to_cpu(mh->FirstPhysicalEUN);
197
198 /* XXX: will be suppressed */
199 nftl->lastEUN = nftl->nb_blocks - 1;
200
201 /* memory alloc */
202 nftl->EUNtable = kmalloc(nftl->nb_blocks * sizeof(u16), GFP_KERNEL);
203 if (!nftl->EUNtable) {
204 printk(KERN_NOTICE "NFTL: allocation of EUNtable failed\n");
205 return -ENOMEM;
206 }
207
208 nftl->ReplUnitTable = kmalloc(nftl->nb_blocks * sizeof(u16), GFP_KERNEL);
209 if (!nftl->ReplUnitTable) {
210 kfree(nftl->EUNtable);
211 printk(KERN_NOTICE "NFTL: allocation of ReplUnitTable failed\n");
212 return -ENOMEM;
213 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* mark the bios blocks (blocks before NFTL MediaHeader) as reserved */
216 for (i = 0; i < nftl->nb_boot_blocks; i++)
217 nftl->ReplUnitTable[i] = BLOCK_RESERVED;
218 /* mark all remaining blocks as potentially containing data */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000219 for (; i < nftl->nb_blocks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 nftl->ReplUnitTable[i] = BLOCK_NOTEXPLORED;
221 }
222
223 /* Mark this boot record (NFTL MediaHeader) block as reserved */
224 nftl->ReplUnitTable[block] = BLOCK_RESERVED;
225
226 /* read the Bad Erase Unit Table and modify ReplUnitTable[] accordingly */
227 for (i = 0; i < nftl->nb_blocks; i++) {
228#if 0
229The new DiskOnChip driver already scanned the bad block table. Just query it.
230 if ((i & (SECTORSIZE - 1)) == 0) {
231 /* read one sector for every SECTORSIZE of blocks */
Andy Shevchenko768c57c2015-01-07 22:37:20 +0200232 ret = mtd->read(nftl->mbd.mtd,
233 block * nftl->EraseSize + i +
234 SECTORSIZE, SECTORSIZE,
235 &retlen, buf);
236 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 printk(KERN_NOTICE "Read of bad sector table failed (err %d)\n",
238 ret);
239 kfree(nftl->ReplUnitTable);
240 kfree(nftl->EUNtable);
241 return -1;
242 }
243 }
244 /* mark the Bad Erase Unit as RESERVED in ReplUnitTable */
245 if (buf[i & (SECTORSIZE - 1)] != 0xff)
246 nftl->ReplUnitTable[i] = BLOCK_RESERVED;
247#endif
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200248 if (mtd_block_isbad(nftl->mbd.mtd,
249 i * nftl->EraseSize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 nftl->ReplUnitTable[i] = BLOCK_RESERVED;
251 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 nftl->MediaUnit = block;
254 boot_record_count++;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 } /* foreach (block) */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return boot_record_count?0:-1;
259}
260
261static int memcmpb(void *a, int c, int n)
262{
263 int i;
264 for (i = 0; i < n; i++) {
265 if (c != ((unsigned char *)a)[i])
266 return 1;
267 }
268 return 0;
269}
270
271/* check_free_sector: check if a free sector is actually FREE, i.e. All 0xff in data and oob area */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000272static int check_free_sectors(struct NFTLrecord *nftl, unsigned int address, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int check_oob)
274{
Thomas Gleixner9223a452006-05-23 17:21:03 +0200275 struct mtd_info *mtd = nftl->mbd.mtd;
276 size_t retlen;
Kees Cook27ab41e2018-04-29 08:00:53 -0700277 int i, ret;
278 u8 *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Kees Cook27ab41e2018-04-29 08:00:53 -0700280 buf = kmalloc(SECTORSIZE + mtd->oobsize, GFP_KERNEL);
281 if (!buf)
282 return -1;
283
284 ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 for (i = 0; i < len; i += SECTORSIZE) {
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200286 if (mtd_read(mtd, address, SECTORSIZE, &retlen, buf))
Kees Cook27ab41e2018-04-29 08:00:53 -0700287 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
Kees Cook27ab41e2018-04-29 08:00:53 -0700289 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (check_oob) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200292 if(nftl_read_oob(mtd, address, mtd->oobsize,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200293 &retlen, &buf[SECTORSIZE]) < 0)
Kees Cook27ab41e2018-04-29 08:00:53 -0700294 goto out;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200295 if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
Kees Cook27ab41e2018-04-29 08:00:53 -0700296 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298 address += SECTORSIZE;
299 }
300
Kees Cook27ab41e2018-04-29 08:00:53 -0700301 ret = 0;
302
303out:
304 kfree(buf);
305 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/* NFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase Unit and
309 * Update NFTL metadata. Each erase operation is checked with check_free_sectors
310 *
311 * Return: 0 when succeed, -1 on error.
312 *
Brian Norris92394b5c2011-07-20 09:53:42 -0700313 * ToDo: 1. Is it necessary to check_free_sector after erasing ??
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315int NFTL_formatblock(struct NFTLrecord *nftl, int block)
316{
317 size_t retlen;
318 unsigned int nb_erases, erase_mark;
319 struct nftl_uci1 uci;
320 struct erase_info *instr = &nftl->instr;
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200321 struct mtd_info *mtd = nftl->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* Read the Unit Control Information #1 for Wear-Leveling */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200324 if (nftl_read_oob(mtd, block * nftl->EraseSize + SECTORSIZE + 8,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200325 8, &retlen, (char *)&uci) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 goto default_uci1;
327
328 erase_mark = le16_to_cpu ((uci.EraseMark | uci.EraseMark1));
329 if (erase_mark != ERASE_MARK) {
330 default_uci1:
331 uci.EraseMark = cpu_to_le16(ERASE_MARK);
332 uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
333 uci.WearInfo = cpu_to_le32(0);
334 }
335
336 memset(instr, 0, sizeof(struct erase_info));
337
338 /* XXX: use async erase interface, XXX: test return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 instr->addr = block * nftl->EraseSize;
340 instr->len = nftl->EraseSize;
Boris Brezillon884cfd92018-02-12 22:03:09 +0100341 if (mtd_erase(mtd, instr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 printk("Error while formatting block %d\n", block);
343 goto fail;
344 }
345
346 /* increase and write Wear-Leveling info */
347 nb_erases = le32_to_cpu(uci.WearInfo);
348 nb_erases++;
349
Brian Norris92394b5c2011-07-20 09:53:42 -0700350 /* wrap (almost impossible with current flash) or free block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (nb_erases == 0)
352 nb_erases = 1;
353
354 /* check the "freeness" of Erase Unit before updating metadata
355 * FixMe: is this check really necessary ? since we have check the
356 * return code after the erase operation. */
357 if (check_free_sectors(nftl, instr->addr, nftl->EraseSize, 1) != 0)
358 goto fail;
359
360 uci.WearInfo = le32_to_cpu(nb_erases);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200361 if (nftl_write_oob(mtd, block * nftl->EraseSize + SECTORSIZE +
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200362 8, 8, &retlen, (char *)&uci) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 goto fail;
364 return 0;
365fail:
366 /* could not format, update the bad block table (caller is responsible
367 for setting the ReplUnitTable to BLOCK_RESERVED on failure) */
Artem Bityutskiy5942ddb2011-12-23 19:37:38 +0200368 mtd_block_markbad(nftl->mbd.mtd, instr->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -1;
370}
371
372/* check_sectors_in_chain: Check that each sector of a Virtual Unit Chain is correct.
373 * Mark as 'IGNORE' each incorrect sector. This check is only done if the chain
374 * was being folded when NFTL was interrupted.
375 *
Brian Norris92394b5c2011-07-20 09:53:42 -0700376 * The check_free_sectors in this function is necessary. There is a possible
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * situation that after writing the Data area, the Block Control Information is
378 * not updated according (due to power failure or something) which leaves the block
Brian Norris92394b5c2011-07-20 09:53:42 -0700379 * in an inconsistent state. So we have to check if a block is really FREE in this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * case. */
381static void check_sectors_in_chain(struct NFTLrecord *nftl, unsigned int first_block)
382{
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200383 struct mtd_info *mtd = nftl->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned int block, i, status;
385 struct nftl_bci bci;
386 int sectors_per_block;
387 size_t retlen;
388
389 sectors_per_block = nftl->EraseSize / SECTORSIZE;
390 block = first_block;
391 for (;;) {
392 for (i = 0; i < sectors_per_block; i++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200393 if (nftl_read_oob(mtd,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200394 block * nftl->EraseSize + i * SECTORSIZE,
395 8, &retlen, (char *)&bci) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 status = SECTOR_IGNORE;
397 else
398 status = bci.Status | bci.Status1;
399
400 switch(status) {
401 case SECTOR_FREE:
402 /* verify that the sector is really free. If not, mark
403 as ignore */
404 if (memcmpb(&bci, 0xff, 8) != 0 ||
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000405 check_free_sectors(nftl, block * nftl->EraseSize + i * SECTORSIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 SECTORSIZE, 0) != 0) {
407 printk("Incorrect free sector %d in block %d: "
408 "marking it as ignored\n",
409 i, block);
410
411 /* sector not free actually : mark it as SECTOR_IGNORE */
412 bci.Status = SECTOR_IGNORE;
413 bci.Status1 = SECTOR_IGNORE;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200414 nftl_write_oob(mtd, block *
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200415 nftl->EraseSize +
416 i * SECTORSIZE, 8,
417 &retlen, (char *)&bci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 break;
420 default:
421 break;
422 }
423 }
424
425 /* proceed to next Erase Unit on the chain */
426 block = nftl->ReplUnitTable[block];
427 if (!(block == BLOCK_NIL || block < nftl->nb_blocks))
428 printk("incorrect ReplUnitTable[] : %d\n", block);
429 if (block == BLOCK_NIL || block >= nftl->nb_blocks)
430 break;
431 }
432}
433
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200434/* calc_chain_length: Walk through a Virtual Unit Chain and estimate chain length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static int calc_chain_length(struct NFTLrecord *nftl, unsigned int first_block)
436{
437 unsigned int length = 0, block = first_block;
438
439 for (;;) {
440 length++;
Brian Norris92394b5c2011-07-20 09:53:42 -0700441 /* avoid infinite loops, although this is guaranteed not to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 happen because of the previous checks */
443 if (length >= nftl->nb_blocks) {
444 printk("nftl: length too long %d !\n", length);
445 break;
446 }
447
448 block = nftl->ReplUnitTable[block];
449 if (!(block == BLOCK_NIL || block < nftl->nb_blocks))
450 printk("incorrect ReplUnitTable[] : %d\n", block);
451 if (block == BLOCK_NIL || block >= nftl->nb_blocks)
452 break;
453 }
454 return length;
455}
456
457/* format_chain: Format an invalid Virtual Unit chain. It frees all the Erase Units in a
458 * Virtual Unit Chain, i.e. all the units are disconnected.
459 *
Brian Norris92394b5c2011-07-20 09:53:42 -0700460 * It is not strictly correct to begin from the first block of the chain because
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * if we stop the code, we may see again a valid chain if there was a first_block
462 * flag in a block inside it. But is it really a problem ?
463 *
Brian Norris92394b5c2011-07-20 09:53:42 -0700464 * FixMe: Figure out what the last statement means. What if power failure when we are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * in the for (;;) loop formatting blocks ??
466 */
467static void format_chain(struct NFTLrecord *nftl, unsigned int first_block)
468{
469 unsigned int block = first_block, block1;
470
471 printk("Formatting chain at block %d\n", first_block);
472
473 for (;;) {
474 block1 = nftl->ReplUnitTable[block];
475
476 printk("Formatting block %d\n", block);
477 if (NFTL_formatblock(nftl, block) < 0) {
478 /* cannot format !!!! Mark it as Bad Unit */
479 nftl->ReplUnitTable[block] = BLOCK_RESERVED;
480 } else {
481 nftl->ReplUnitTable[block] = BLOCK_FREE;
482 }
483
484 /* goto next block on the chain */
485 block = block1;
486
487 if (!(block == BLOCK_NIL || block < nftl->nb_blocks))
488 printk("incorrect ReplUnitTable[] : %d\n", block);
489 if (block == BLOCK_NIL || block >= nftl->nb_blocks)
490 break;
491 }
492}
493
494/* check_and_mark_free_block: Verify that a block is free in the NFTL sense (valid erase mark) or
495 * totally free (only 0xff).
496 *
497 * Definition: Free Erase Unit -- A properly erased/formatted Free Erase Unit should have meet the
Brian Norris92394b5c2011-07-20 09:53:42 -0700498 * following criteria:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * 1. */
500static int check_and_mark_free_block(struct NFTLrecord *nftl, int block)
501{
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200502 struct mtd_info *mtd = nftl->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct nftl_uci1 h1;
504 unsigned int erase_mark;
505 size_t retlen;
506
507 /* check erase mark. */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200508 if (nftl_read_oob(mtd, block * nftl->EraseSize + SECTORSIZE + 8, 8,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200509 &retlen, (char *)&h1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return -1;
511
512 erase_mark = le16_to_cpu ((h1.EraseMark | h1.EraseMark1));
513 if (erase_mark != ERASE_MARK) {
514 /* if no erase mark, the block must be totally free. This is
Brian Norris92394b5c2011-07-20 09:53:42 -0700515 possible in two cases : empty filesystem or interrupted erase (very unlikely) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (check_free_sectors (nftl, block * nftl->EraseSize, nftl->EraseSize, 1) != 0)
517 return -1;
518
519 /* free block : write erase mark */
520 h1.EraseMark = cpu_to_le16(ERASE_MARK);
521 h1.EraseMark1 = cpu_to_le16(ERASE_MARK);
522 h1.WearInfo = cpu_to_le32(0);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200523 if (nftl_write_oob(mtd,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200524 block * nftl->EraseSize + SECTORSIZE + 8, 8,
525 &retlen, (char *)&h1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -1;
527 } else {
528#if 0
529 /* if erase mark present, need to skip it when doing check */
530 for (i = 0; i < nftl->EraseSize; i += SECTORSIZE) {
531 /* check free sector */
532 if (check_free_sectors (nftl, block * nftl->EraseSize + i,
533 SECTORSIZE, 0) != 0)
534 return -1;
535
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200536 if (nftl_read_oob(mtd, block * nftl->EraseSize + i,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200537 16, &retlen, buf) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -1;
539 if (i == SECTORSIZE) {
540 /* skip erase mark */
541 if (memcmpb(buf, 0xff, 8))
542 return -1;
543 } else {
544 if (memcmpb(buf, 0xff, 16))
545 return -1;
546 }
547 }
548#endif
549 }
550
551 return 0;
552}
553
554/* get_fold_mark: Read fold mark from Unit Control Information #2, we use FOLD_MARK_IN_PROGRESS
555 * to indicate that we are in the progression of a Virtual Unit Chain folding. If the UCI #2
556 * is FOLD_MARK_IN_PROGRESS when mounting the NFTL, the (previous) folding process is interrupted
Brian Norris92394b5c2011-07-20 09:53:42 -0700557 * for some reason. A clean up/check of the VUC is necessary in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 *
559 * WARNING: return 0 if read error
560 */
561static int get_fold_mark(struct NFTLrecord *nftl, unsigned int block)
562{
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200563 struct mtd_info *mtd = nftl->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 struct nftl_uci2 uci;
565 size_t retlen;
566
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200567 if (nftl_read_oob(mtd, block * nftl->EraseSize + 2 * SECTORSIZE + 8,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200568 8, &retlen, (char *)&uci) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 0;
570
571 return le16_to_cpu((uci.FoldMark | uci.FoldMark1));
572}
573
574int NFTL_mount(struct NFTLrecord *s)
575{
576 int i;
577 unsigned int first_logical_block, logical_block, rep_block, nb_erases, erase_mark;
578 unsigned int block, first_block, is_first_block;
579 int chain_length, do_format_chain;
580 struct nftl_uci0 h0;
581 struct nftl_uci1 h1;
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200582 struct mtd_info *mtd = s->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 size_t retlen;
584
585 /* search for NFTL MediaHeader and Spare NFTL Media Header */
586 if (find_boot_record(s) < 0) {
587 printk("Could not find valid boot record\n");
588 return -1;
589 }
590
591 /* init the logical to physical table */
592 for (i = 0; i < s->nb_blocks; i++) {
593 s->EUNtable[i] = BLOCK_NIL;
594 }
595
596 /* first pass : explore each block chain */
597 first_logical_block = 0;
598 for (first_block = 0; first_block < s->nb_blocks; first_block++) {
599 /* if the block was not already explored, we can look at it */
600 if (s->ReplUnitTable[first_block] == BLOCK_NOTEXPLORED) {
601 block = first_block;
602 chain_length = 0;
603 do_format_chain = 0;
604
605 for (;;) {
606 /* read the block header. If error, we format the chain */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200607 if (nftl_read_oob(mtd,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200608 block * s->EraseSize + 8, 8,
609 &retlen, (char *)&h0) < 0 ||
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200610 nftl_read_oob(mtd,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200611 block * s->EraseSize +
612 SECTORSIZE + 8, 8,
613 &retlen, (char *)&h1) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 s->ReplUnitTable[block] = BLOCK_NIL;
615 do_format_chain = 1;
616 break;
617 }
618
619 logical_block = le16_to_cpu ((h0.VirtUnitNum | h0.SpareVirtUnitNum));
620 rep_block = le16_to_cpu ((h0.ReplUnitNum | h0.SpareReplUnitNum));
621 nb_erases = le32_to_cpu (h1.WearInfo);
622 erase_mark = le16_to_cpu ((h1.EraseMark | h1.EraseMark1));
623
624 is_first_block = !(logical_block >> 15);
625 logical_block = logical_block & 0x7fff;
626
627 /* invalid/free block test */
628 if (erase_mark != ERASE_MARK || logical_block >= s->nb_blocks) {
629 if (chain_length == 0) {
630 /* if not currently in a chain, we can handle it safely */
631 if (check_and_mark_free_block(s, block) < 0) {
632 /* not really free: format it */
633 printk("Formatting block %d\n", block);
634 if (NFTL_formatblock(s, block) < 0) {
635 /* could not format: reserve the block */
636 s->ReplUnitTable[block] = BLOCK_RESERVED;
637 } else {
638 s->ReplUnitTable[block] = BLOCK_FREE;
639 }
640 } else {
641 /* free block: mark it */
642 s->ReplUnitTable[block] = BLOCK_FREE;
643 }
644 /* directly examine the next block. */
645 goto examine_ReplUnitTable;
646 } else {
647 /* the block was in a chain : this is bad. We
648 must format all the chain */
649 printk("Block %d: free but referenced in chain %d\n",
650 block, first_block);
651 s->ReplUnitTable[block] = BLOCK_NIL;
652 do_format_chain = 1;
653 break;
654 }
655 }
656
657 /* we accept only first blocks here */
658 if (chain_length == 0) {
659 /* this block is not the first block in chain :
660 ignore it, it will be included in a chain
661 later, or marked as not explored */
662 if (!is_first_block)
663 goto examine_ReplUnitTable;
664 first_logical_block = logical_block;
665 } else {
666 if (logical_block != first_logical_block) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000667 printk("Block %d: incorrect logical block: %d expected: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 block, logical_block, first_logical_block);
669 /* the chain is incorrect : we must format it,
Brian Norris92394b5c2011-07-20 09:53:42 -0700670 but we need to read it completely */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 do_format_chain = 1;
672 }
673 if (is_first_block) {
674 /* we accept that a block is marked as first
675 block while being last block in a chain
676 only if the chain is being folded */
677 if (get_fold_mark(s, block) != FOLD_MARK_IN_PROGRESS ||
678 rep_block != 0xffff) {
679 printk("Block %d: incorrectly marked as first block in chain\n",
680 block);
681 /* the chain is incorrect : we must format it,
Brian Norris92394b5c2011-07-20 09:53:42 -0700682 but we need to read it completely */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 do_format_chain = 1;
684 } else {
685 printk("Block %d: folding in progress - ignoring first block flag\n",
686 block);
687 }
688 }
689 }
690 chain_length++;
691 if (rep_block == 0xffff) {
692 /* no more blocks after */
693 s->ReplUnitTable[block] = BLOCK_NIL;
694 break;
695 } else if (rep_block >= s->nb_blocks) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000696 printk("Block %d: referencing invalid block %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 block, rep_block);
698 do_format_chain = 1;
699 s->ReplUnitTable[block] = BLOCK_NIL;
700 break;
701 } else if (s->ReplUnitTable[rep_block] != BLOCK_NOTEXPLORED) {
702 /* same problem as previous 'is_first_block' test:
703 we accept that the last block of a chain has
704 the first_block flag set if folding is in
705 progress. We handle here the case where the
706 last block appeared first */
707 if (s->ReplUnitTable[rep_block] == BLOCK_NIL &&
708 s->EUNtable[first_logical_block] == rep_block &&
709 get_fold_mark(s, first_block) == FOLD_MARK_IN_PROGRESS) {
710 /* EUNtable[] will be set after */
711 printk("Block %d: folding in progress - ignoring first block flag\n",
712 rep_block);
713 s->ReplUnitTable[block] = rep_block;
714 s->EUNtable[first_logical_block] = BLOCK_NIL;
715 } else {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000716 printk("Block %d: referencing block %d already in another chain\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 block, rep_block);
718 /* XXX: should handle correctly fold in progress chains */
719 do_format_chain = 1;
720 s->ReplUnitTable[block] = BLOCK_NIL;
721 }
722 break;
723 } else {
724 /* this is OK */
725 s->ReplUnitTable[block] = rep_block;
726 block = rep_block;
727 }
728 }
729
730 /* the chain was completely explored. Now we can decide
731 what to do with it */
732 if (do_format_chain) {
733 /* invalid chain : format it */
734 format_chain(s, first_block);
735 } else {
736 unsigned int first_block1, chain_to_format, chain_length1;
737 int fold_mark;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* valid chain : get foldmark */
740 fold_mark = get_fold_mark(s, first_block);
741 if (fold_mark == 0) {
742 /* cannot get foldmark : format the chain */
743 printk("Could read foldmark at block %d\n", first_block);
744 format_chain(s, first_block);
745 } else {
746 if (fold_mark == FOLD_MARK_IN_PROGRESS)
747 check_sectors_in_chain(s, first_block);
748
749 /* now handle the case where we find two chains at the
750 same virtual address : we select the longer one,
751 because the shorter one is the one which was being
752 folded if the folding was not done in place */
753 first_block1 = s->EUNtable[first_logical_block];
754 if (first_block1 != BLOCK_NIL) {
755 /* XXX: what to do if same length ? */
756 chain_length1 = calc_chain_length(s, first_block1);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000757 printk("Two chains at blocks %d (len=%d) and %d (len=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 first_block1, chain_length1, first_block, chain_length);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (chain_length >= chain_length1) {
761 chain_to_format = first_block1;
762 s->EUNtable[first_logical_block] = first_block;
763 } else {
764 chain_to_format = first_block;
765 }
766 format_chain(s, chain_to_format);
767 } else {
768 s->EUNtable[first_logical_block] = first_block;
769 }
770 }
771 }
772 }
773 examine_ReplUnitTable:;
774 }
775
776 /* second pass to format unreferenced blocks and init free block count */
777 s->numfreeEUNs = 0;
778 s->LastFreeEUN = le16_to_cpu(s->MediaHdr.FirstPhysicalEUN);
779
780 for (block = 0; block < s->nb_blocks; block++) {
781 if (s->ReplUnitTable[block] == BLOCK_NOTEXPLORED) {
782 printk("Unreferenced block %d, formatting it\n", block);
783 if (NFTL_formatblock(s, block) < 0)
784 s->ReplUnitTable[block] = BLOCK_RESERVED;
785 else
786 s->ReplUnitTable[block] = BLOCK_FREE;
787 }
788 if (s->ReplUnitTable[block] == BLOCK_FREE) {
789 s->numfreeEUNs++;
790 s->LastFreeEUN = block;
791 }
792 }
793
794 return 0;
795}