blob: 1c1401d6958b4dce4e018b9fd40ec00667fcf560 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Artem Bityutskiy (Битюцкий Артём)
19 */
20
21/*
22 * UBI scanning unit.
23 *
24 * This unit is responsible for scanning the flash media, checking UBI
25 * headers and providing complete information about the UBI flash image.
26 *
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030027 * The scanning information is represented by a &struct ubi_scan_info' object.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040028 * Information about found volumes is represented by &struct ubi_scan_volume
29 * objects which are kept in volume RB-tree with root at the @volumes field.
30 * The RB-tree is indexed by the volume ID.
31 *
32 * Found logical eraseblocks are represented by &struct ubi_scan_leb objects.
33 * These objects are kept in per-volume RB-trees with the root at the
34 * corresponding &struct ubi_scan_volume object. To put it differently, we keep
35 * an RB-tree of per-volume objects and each of these objects is the root of
36 * RB-tree of per-eraseblock objects.
37 *
38 * Corrupted physical eraseblocks are put to the @corr list, free physical
39 * eraseblocks are put to the @free list and the physical eraseblock to be
40 * erased are put to the @erase list.
41 */
42
43#include <linux/err.h>
44#include <linux/crc32.h>
45#include "ubi.h"
46
47#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
48static int paranoid_check_si(const struct ubi_device *ubi,
49 struct ubi_scan_info *si);
50#else
51#define paranoid_check_si(ubi, si) 0
52#endif
53
54/* Temporary variables used during scanning */
55static struct ubi_ec_hdr *ech;
56static struct ubi_vid_hdr *vidh;
57
Artem Bityutskiy941dfb02007-05-05 16:33:13 +030058/**
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030059 * add_to_list - add physical eraseblock to a list.
60 * @si: scanning information
61 * @pnum: physical eraseblock number to add
62 * @ec: erase counter of the physical eraseblock
63 * @list: the list to add to
64 *
65 * This function adds physical eraseblock @pnum to free, erase, corrupted or
66 * alien lists. Returns zero in case of success and a negative error code in
67 * case of failure.
68 */
69static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
70 struct list_head *list)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040071{
72 struct ubi_scan_leb *seb;
73
74 if (list == &si->free)
75 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
76 else if (list == &si->erase)
77 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
78 else if (list == &si->corr)
79 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
80 else if (list == &si->alien)
81 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
82 else
83 BUG();
84
85 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
86 if (!seb)
87 return -ENOMEM;
88
89 seb->pnum = pnum;
90 seb->ec = ec;
91 list_add_tail(&seb->u.list, list);
92 return 0;
93}
94
95/**
96 * commit_to_mean_value - commit intermediate results to the final mean erase
97 * counter value.
98 * @si: scanning information
99 *
100 * This is a helper function which calculates partial mean erase counter mean
101 * value and adds it to the resulting mean value. As we can work only in
102 * integer arithmetic and we want to calculate the mean value of erase counter
103 * accurately, we first sum erase counter values in @si->ec_sum variable and
104 * count these components in @si->ec_count. If this temporary @si->ec_sum is
105 * going to overflow, we calculate the partial mean value
106 * (@si->ec_sum/@si->ec_count) and add it to @si->mean_ec.
107 */
108static void commit_to_mean_value(struct ubi_scan_info *si)
109{
110 si->ec_sum /= si->ec_count;
111 if (si->ec_sum % si->ec_count >= si->ec_count / 2)
112 si->mean_ec += 1;
113 si->mean_ec += si->ec_sum;
114}
115
116/**
117 * validate_vid_hdr - check that volume identifier header is correct and
118 * consistent.
119 * @vid_hdr: the volume identifier header to check
120 * @sv: information about the volume this logical eraseblock belongs to
121 * @pnum: physical eraseblock number the VID header came from
122 *
123 * This function checks that data stored in @vid_hdr is consistent. Returns
124 * non-zero if an inconsistency was found and zero if not.
125 *
126 * Note, UBI does sanity check of everything it reads from the flash media.
127 * Most of the checks are done in the I/O unit. Here we check that the
128 * information in the VID header is consistent to the information in other VID
129 * headers of the same volume.
130 */
131static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
132 const struct ubi_scan_volume *sv, int pnum)
133{
134 int vol_type = vid_hdr->vol_type;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300135 int vol_id = be32_to_cpu(vid_hdr->vol_id);
136 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
137 int data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400138
139 if (sv->leb_count != 0) {
140 int sv_vol_type;
141
142 /*
143 * This is not the first logical eraseblock belonging to this
144 * volume. Ensure that the data in its VID header is consistent
145 * to the data in previous logical eraseblock headers.
146 */
147
148 if (vol_id != sv->vol_id) {
149 dbg_err("inconsistent vol_id");
150 goto bad;
151 }
152
153 if (sv->vol_type == UBI_STATIC_VOLUME)
154 sv_vol_type = UBI_VID_STATIC;
155 else
156 sv_vol_type = UBI_VID_DYNAMIC;
157
158 if (vol_type != sv_vol_type) {
159 dbg_err("inconsistent vol_type");
160 goto bad;
161 }
162
163 if (used_ebs != sv->used_ebs) {
164 dbg_err("inconsistent used_ebs");
165 goto bad;
166 }
167
168 if (data_pad != sv->data_pad) {
169 dbg_err("inconsistent data_pad");
170 goto bad;
171 }
172 }
173
174 return 0;
175
176bad:
177 ubi_err("inconsistent VID header at PEB %d", pnum);
178 ubi_dbg_dump_vid_hdr(vid_hdr);
179 ubi_dbg_dump_sv(sv);
180 return -EINVAL;
181}
182
183/**
184 * add_volume - add volume to the scanning information.
185 * @si: scanning information
186 * @vol_id: ID of the volume to add
187 * @pnum: physical eraseblock number
188 * @vid_hdr: volume identifier header
189 *
190 * If the volume corresponding to the @vid_hdr logical eraseblock is already
191 * present in the scanning information, this function does nothing. Otherwise
192 * it adds corresponding volume to the scanning information. Returns a pointer
193 * to the scanning volume object in case of success and a negative error code
194 * in case of failure.
195 */
196static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
197 int pnum,
198 const struct ubi_vid_hdr *vid_hdr)
199{
200 struct ubi_scan_volume *sv;
201 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
202
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300203 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400204
205 /* Walk the volume RB-tree to look if this volume is already present */
206 while (*p) {
207 parent = *p;
208 sv = rb_entry(parent, struct ubi_scan_volume, rb);
209
210 if (vol_id == sv->vol_id)
211 return sv;
212
213 if (vol_id > sv->vol_id)
214 p = &(*p)->rb_left;
215 else
216 p = &(*p)->rb_right;
217 }
218
219 /* The volume is absent - add it */
220 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
221 if (!sv)
222 return ERR_PTR(-ENOMEM);
223
224 sv->highest_lnum = sv->leb_count = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400225 sv->vol_id = vol_id;
226 sv->root = RB_ROOT;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300227 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
228 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400229 sv->compat = vid_hdr->compat;
230 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
231 : UBI_STATIC_VOLUME;
232 if (vol_id > si->highest_vol_id)
233 si->highest_vol_id = vol_id;
234
235 rb_link_node(&sv->rb, parent, p);
236 rb_insert_color(&sv->rb, &si->volumes);
237 si->vols_found += 1;
238 dbg_bld("added volume %d", vol_id);
239 return sv;
240}
241
242/**
243 * compare_lebs - find out which logical eraseblock is newer.
244 * @ubi: UBI device description object
245 * @seb: first logical eraseblock to compare
246 * @pnum: physical eraseblock number of the second logical eraseblock to
247 * compare
248 * @vid_hdr: volume identifier header of the second logical eraseblock
249 *
250 * This function compares 2 copies of a LEB and informs which one is newer. In
251 * case of success this function returns a positive value, in case of failure, a
252 * negative error code is returned. The success return codes use the following
253 * bits:
254 * o bit 0 is cleared: the first PEB (described by @seb) is newer then the
255 * second PEB (described by @pnum and @vid_hdr);
256 * o bit 0 is set: the second PEB is newer;
257 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
258 * o bit 1 is set: bit-flips were detected in the newer LEB;
259 * o bit 2 is cleared: the older LEB is not corrupted;
260 * o bit 2 is set: the older LEB is corrupted.
261 */
262static int compare_lebs(const struct ubi_device *ubi,
263 const struct ubi_scan_leb *seb, int pnum,
264 const struct ubi_vid_hdr *vid_hdr)
265{
266 void *buf;
267 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
268 uint32_t data_crc, crc;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300269 struct ubi_vid_hdr *vh = NULL;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300270 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400271
272 if (seb->sqnum == 0 && sqnum2 == 0) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300273 long long abs, v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400274
275 /*
276 * UBI constantly increases the logical eraseblock version
277 * number and it can overflow. Thus, we have to bear in mind
278 * that versions that are close to %0xFFFFFFFF are less then
279 * versions that are close to %0.
280 *
281 * The UBI WL unit guarantees that the number of pending tasks
282 * is not greater then %0x7FFFFFFF. So, if the difference
283 * between any two versions is greater or equivalent to
284 * %0x7FFFFFFF, there was an overflow and the logical
285 * eraseblock with lower version is actually newer then the one
286 * with higher version.
287 *
288 * FIXME: but this is anyway obsolete and will be removed at
289 * some point.
290 */
291
292 dbg_bld("using old crappy leb_ver stuff");
293
294 abs = v1 - v2;
295 if (abs < 0)
296 abs = -abs;
297
298 if (abs < 0x7FFFFFFF)
299 /* Non-overflow situation */
300 second_is_newer = (v2 > v1);
301 else
302 second_is_newer = (v2 < v1);
303 } else
304 /* Obviously the LEB with lower sequence counter is older */
305 second_is_newer = sqnum2 > seb->sqnum;
306
307 /*
308 * Now we know which copy is newer. If the copy flag of the PEB with
309 * newer version is not set, then we just return, otherwise we have to
310 * check data CRC. For the second PEB we already have the VID header,
311 * for the first one - we'll need to re-read it from flash.
312 *
313 * FIXME: this may be optimized so that we wouldn't read twice.
314 */
315
316 if (second_is_newer) {
317 if (!vid_hdr->copy_flag) {
318 /* It is not a copy, so it is newer */
319 dbg_bld("second PEB %d is newer, copy_flag is unset",
320 pnum);
321 return 1;
322 }
323 } else {
324 pnum = seb->pnum;
325
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300326 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300327 if (!vh)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400328 return -ENOMEM;
329
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300330 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400331 if (err) {
332 if (err == UBI_IO_BITFLIPS)
333 bitflips = 1;
334 else {
335 dbg_err("VID of PEB %d header is bad, but it "
336 "was OK earlier", pnum);
337 if (err > 0)
338 err = -EIO;
339
340 goto out_free_vidh;
341 }
342 }
343
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300344 if (!vh->copy_flag) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400345 /* It is not a copy, so it is newer */
346 dbg_bld("first PEB %d is newer, copy_flag is unset",
347 pnum);
348 err = bitflips << 1;
349 goto out_free_vidh;
350 }
351
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300352 vid_hdr = vh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400353 }
354
355 /* Read the data of the copy and check the CRC */
356
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300357 len = be32_to_cpu(vid_hdr->data_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300358 buf = vmalloc(len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400359 if (!buf) {
360 err = -ENOMEM;
361 goto out_free_vidh;
362 }
363
364 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
365 if (err && err != UBI_IO_BITFLIPS)
366 goto out_free_buf;
367
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300368 data_crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400369 crc = crc32(UBI_CRC32_INIT, buf, len);
370 if (crc != data_crc) {
371 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
372 pnum, crc, data_crc);
373 corrupted = 1;
374 bitflips = 0;
375 second_is_newer = !second_is_newer;
376 } else {
377 dbg_bld("PEB %d CRC is OK", pnum);
378 bitflips = !!err;
379 }
380
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300381 vfree(buf);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300382 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400383
384 if (second_is_newer)
385 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
386 else
387 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
388
389 return second_is_newer | (bitflips << 1) | (corrupted << 2);
390
391out_free_buf:
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300392 vfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400393out_free_vidh:
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300394 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400395 ubi_assert(err < 0);
396 return err;
397}
398
399/**
400 * ubi_scan_add_used - add information about a physical eraseblock to the
401 * scanning information.
402 * @ubi: UBI device description object
403 * @si: scanning information
404 * @pnum: the physical eraseblock number
405 * @ec: erase counter
406 * @vid_hdr: the volume identifier header
407 * @bitflips: if bit-flips were detected when this physical eraseblock was read
408 *
Artem Bityutskiy79b510c2007-05-05 17:36:17 +0300409 * This function adds information about a used physical eraseblock to the
410 * 'used' tree of the corresponding volume. The function is rather complex
411 * because it has to handle cases when this is not the first physical
412 * eraseblock belonging to the same logical eraseblock, and the newer one has
413 * to be picked, while the older one has to be dropped. This function returns
414 * zero in case of success and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400415 */
416int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si,
417 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
418 int bitflips)
419{
420 int err, vol_id, lnum;
421 uint32_t leb_ver;
422 unsigned long long sqnum;
423 struct ubi_scan_volume *sv;
424 struct ubi_scan_leb *seb;
425 struct rb_node **p, *parent = NULL;
426
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300427 vol_id = be32_to_cpu(vid_hdr->vol_id);
428 lnum = be32_to_cpu(vid_hdr->lnum);
429 sqnum = be64_to_cpu(vid_hdr->sqnum);
430 leb_ver = be32_to_cpu(vid_hdr->leb_ver);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400431
432 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d",
433 pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips);
434
435 sv = add_volume(si, vol_id, pnum, vid_hdr);
436 if (IS_ERR(sv) < 0)
437 return PTR_ERR(sv);
438
Brijesh Singh76eafe42007-07-06 14:35:43 +0300439 if (si->max_sqnum < sqnum)
440 si->max_sqnum = sqnum;
441
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400442 /*
443 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
444 * if this is the first instance of this logical eraseblock or not.
445 */
446 p = &sv->root.rb_node;
447 while (*p) {
448 int cmp_res;
449
450 parent = *p;
451 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
452 if (lnum != seb->lnum) {
453 if (lnum < seb->lnum)
454 p = &(*p)->rb_left;
455 else
456 p = &(*p)->rb_right;
457 continue;
458 }
459
460 /*
461 * There is already a physical eraseblock describing the same
462 * logical eraseblock present.
463 */
464
465 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
466 "LEB ver %u, EC %d", seb->pnum, seb->sqnum,
467 seb->leb_ver, seb->ec);
468
469 /*
470 * Make sure that the logical eraseblocks have different
471 * versions. Otherwise the image is bad.
472 */
473 if (seb->leb_ver == leb_ver && leb_ver != 0) {
474 ubi_err("two LEBs with same version %u", leb_ver);
475 ubi_dbg_dump_seb(seb, 0);
476 ubi_dbg_dump_vid_hdr(vid_hdr);
477 return -EINVAL;
478 }
479
480 /*
481 * Make sure that the logical eraseblocks have different
482 * sequence numbers. Otherwise the image is bad.
483 *
484 * FIXME: remove 'sqnum != 0' check when leb_ver is removed.
485 */
486 if (seb->sqnum == sqnum && sqnum != 0) {
487 ubi_err("two LEBs with same sequence number %llu",
488 sqnum);
489 ubi_dbg_dump_seb(seb, 0);
490 ubi_dbg_dump_vid_hdr(vid_hdr);
491 return -EINVAL;
492 }
493
494 /*
495 * Now we have to drop the older one and preserve the newer
496 * one.
497 */
498 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
499 if (cmp_res < 0)
500 return cmp_res;
501
502 if (cmp_res & 1) {
503 /*
504 * This logical eraseblock is newer then the one
505 * found earlier.
506 */
507 err = validate_vid_hdr(vid_hdr, sv, pnum);
508 if (err)
509 return err;
510
511 if (cmp_res & 4)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300512 err = add_to_list(si, seb->pnum, seb->ec,
513 &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400514 else
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300515 err = add_to_list(si, seb->pnum, seb->ec,
516 &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400517 if (err)
518 return err;
519
520 seb->ec = ec;
521 seb->pnum = pnum;
522 seb->scrub = ((cmp_res & 2) || bitflips);
523 seb->sqnum = sqnum;
524 seb->leb_ver = leb_ver;
525
526 if (sv->highest_lnum == lnum)
527 sv->last_data_size =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300528 be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400529
530 return 0;
531 } else {
532 /*
533 * This logical eraseblock is older then the one found
534 * previously.
535 */
536 if (cmp_res & 4)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300537 return add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400538 else
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300539 return add_to_list(si, pnum, ec, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400540 }
541 }
542
543 /*
544 * We've met this logical eraseblock for the first time, add it to the
545 * scanning information.
546 */
547
548 err = validate_vid_hdr(vid_hdr, sv, pnum);
549 if (err)
550 return err;
551
552 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
553 if (!seb)
554 return -ENOMEM;
555
556 seb->ec = ec;
557 seb->pnum = pnum;
558 seb->lnum = lnum;
559 seb->sqnum = sqnum;
560 seb->scrub = bitflips;
561 seb->leb_ver = leb_ver;
562
563 if (sv->highest_lnum <= lnum) {
564 sv->highest_lnum = lnum;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300565 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400566 }
567
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400568 sv->leb_count += 1;
569 rb_link_node(&seb->u.rb, parent, p);
570 rb_insert_color(&seb->u.rb, &sv->root);
571 return 0;
572}
573
574/**
575 * ubi_scan_find_sv - find information about a particular volume in the
576 * scanning information.
577 * @si: scanning information
578 * @vol_id: the requested volume ID
579 *
580 * This function returns a pointer to the volume description or %NULL if there
581 * are no data about this volume in the scanning information.
582 */
583struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
584 int vol_id)
585{
586 struct ubi_scan_volume *sv;
587 struct rb_node *p = si->volumes.rb_node;
588
589 while (p) {
590 sv = rb_entry(p, struct ubi_scan_volume, rb);
591
592 if (vol_id == sv->vol_id)
593 return sv;
594
595 if (vol_id > sv->vol_id)
596 p = p->rb_left;
597 else
598 p = p->rb_right;
599 }
600
601 return NULL;
602}
603
604/**
605 * ubi_scan_find_seb - find information about a particular logical
606 * eraseblock in the volume scanning information.
607 * @sv: a pointer to the volume scanning information
608 * @lnum: the requested logical eraseblock
609 *
610 * This function returns a pointer to the scanning logical eraseblock or %NULL
611 * if there are no data about it in the scanning volume information.
612 */
613struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
614 int lnum)
615{
616 struct ubi_scan_leb *seb;
617 struct rb_node *p = sv->root.rb_node;
618
619 while (p) {
620 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
621
622 if (lnum == seb->lnum)
623 return seb;
624
625 if (lnum > seb->lnum)
626 p = p->rb_left;
627 else
628 p = p->rb_right;
629 }
630
631 return NULL;
632}
633
634/**
635 * ubi_scan_rm_volume - delete scanning information about a volume.
636 * @si: scanning information
637 * @sv: the volume scanning information to delete
638 */
639void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
640{
641 struct rb_node *rb;
642 struct ubi_scan_leb *seb;
643
644 dbg_bld("remove scanning information about volume %d", sv->vol_id);
645
646 while ((rb = rb_first(&sv->root))) {
647 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
648 rb_erase(&seb->u.rb, &sv->root);
649 list_add_tail(&seb->u.list, &si->erase);
650 }
651
652 rb_erase(&sv->rb, &si->volumes);
653 kfree(sv);
654 si->vols_found -= 1;
655}
656
657/**
658 * ubi_scan_erase_peb - erase a physical eraseblock.
659 * @ubi: UBI device description object
660 * @si: scanning information
661 * @pnum: physical eraseblock number to erase;
662 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
663 *
664 * This function erases physical eraseblock 'pnum', and writes the erase
665 * counter header to it. This function should only be used on UBI device
666 * initialization stages, when the EBA unit had not been yet initialized. This
667 * function returns zero in case of success and a negative error code in case
668 * of failure.
669 */
670int ubi_scan_erase_peb(const struct ubi_device *ubi,
671 const struct ubi_scan_info *si, int pnum, int ec)
672{
673 int err;
674 struct ubi_ec_hdr *ec_hdr;
675
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400676 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
677 /*
678 * Erase counter overflow. Upgrade UBI and use 64-bit
679 * erase counters internally.
680 */
681 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
682 return -EINVAL;
683 }
684
Florin Malitadcec4c32007-07-19 15:22:41 -0400685 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
686 if (!ec_hdr)
687 return -ENOMEM;
688
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300689 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400690
691 err = ubi_io_sync_erase(ubi, pnum, 0);
692 if (err < 0)
693 goto out_free;
694
695 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
696
697out_free:
698 kfree(ec_hdr);
699 return err;
700}
701
702/**
703 * ubi_scan_get_free_peb - get a free physical eraseblock.
704 * @ubi: UBI device description object
705 * @si: scanning information
706 *
707 * This function returns a free physical eraseblock. It is supposed to be
708 * called on the UBI initialization stages when the wear-leveling unit is not
709 * initialized yet. This function picks a physical eraseblocks from one of the
710 * lists, writes the EC header if it is needed, and removes it from the list.
711 *
712 * This function returns scanning physical eraseblock information in case of
713 * success and an error code in case of failure.
714 */
715struct ubi_scan_leb *ubi_scan_get_free_peb(const struct ubi_device *ubi,
716 struct ubi_scan_info *si)
717{
718 int err = 0, i;
719 struct ubi_scan_leb *seb;
720
721 if (!list_empty(&si->free)) {
722 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
723 list_del(&seb->u.list);
724 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
725 return seb;
726 }
727
728 for (i = 0; i < 2; i++) {
729 struct list_head *head;
730 struct ubi_scan_leb *tmp_seb;
731
732 if (i == 0)
733 head = &si->erase;
734 else
735 head = &si->corr;
736
737 /*
738 * We try to erase the first physical eraseblock from the @head
739 * list and pick it if we succeed, or try to erase the
740 * next one if not. And so forth. We don't want to take care
741 * about bad eraseblocks here - they'll be handled later.
742 */
743 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
744 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
745 seb->ec = si->mean_ec;
746
747 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
748 if (err)
749 continue;
750
751 seb->ec += 1;
752 list_del(&seb->u.list);
753 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
754 return seb;
755 }
756 }
757
758 ubi_err("no eraseblocks found");
759 return ERR_PTR(-ENOSPC);
760}
761
762/**
763 * process_eb - read UBI headers, check them and add corresponding data
764 * to the scanning information.
765 * @ubi: UBI device description object
766 * @si: scanning information
767 * @pnum: the physical eraseblock number
768 *
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300769 * This function returns a zero if the physical eraseblock was successfully
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400770 * handled and a negative error code in case of failure.
771 */
772static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum)
773{
774 long long ec;
775 int err, bitflips = 0, vol_id, ec_corr = 0;
776
777 dbg_bld("scan PEB %d", pnum);
778
779 /* Skip bad physical eraseblocks */
780 err = ubi_io_is_bad(ubi, pnum);
781 if (err < 0)
782 return err;
783 else if (err) {
784 /*
785 * FIXME: this is actually duty of the I/O unit to initialize
786 * this, but MTD does not provide enough information.
787 */
788 si->bad_peb_count += 1;
789 return 0;
790 }
791
792 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
793 if (err < 0)
794 return err;
795 else if (err == UBI_IO_BITFLIPS)
796 bitflips = 1;
797 else if (err == UBI_IO_PEB_EMPTY)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300798 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400799 else if (err == UBI_IO_BAD_EC_HDR) {
800 /*
801 * We have to also look at the VID header, possibly it is not
802 * corrupted. Set %bitflips flag in order to make this PEB be
803 * moved and EC be re-created.
804 */
805 ec_corr = 1;
806 ec = UBI_SCAN_UNKNOWN_EC;
807 bitflips = 1;
808 }
809
810 si->is_empty = 0;
811
812 if (!ec_corr) {
813 /* Make sure UBI version is OK */
814 if (ech->version != UBI_VERSION) {
815 ubi_err("this UBI version is %d, image version is %d",
816 UBI_VERSION, (int)ech->version);
817 return -EINVAL;
818 }
819
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300820 ec = be64_to_cpu(ech->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400821 if (ec > UBI_MAX_ERASECOUNTER) {
822 /*
823 * Erase counter overflow. The EC headers have 64 bits
824 * reserved, but we anyway make use of only 31 bit
825 * values, as this seems to be enough for any existing
826 * flash. Upgrade UBI and use 64-bit erase counters
827 * internally.
828 */
829 ubi_err("erase counter overflow, max is %d",
830 UBI_MAX_ERASECOUNTER);
831 ubi_dbg_dump_ec_hdr(ech);
832 return -EINVAL;
833 }
834 }
835
836 /* OK, we've done with the EC header, let's look at the VID header */
837
838 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
839 if (err < 0)
840 return err;
841 else if (err == UBI_IO_BITFLIPS)
842 bitflips = 1;
843 else if (err == UBI_IO_BAD_VID_HDR ||
844 (err == UBI_IO_PEB_FREE && ec_corr)) {
845 /* VID header is corrupted */
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300846 err = add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400847 if (err)
848 return err;
849 goto adjust_mean_ec;
850 } else if (err == UBI_IO_PEB_FREE) {
851 /* No VID header - the physical eraseblock is free */
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300852 err = add_to_list(si, pnum, ec, &si->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400853 if (err)
854 return err;
855 goto adjust_mean_ec;
856 }
857
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300858 vol_id = be32_to_cpu(vidh->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400859 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOL_ID) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300860 int lnum = be32_to_cpu(vidh->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400861
862 /* Unsupported internal volume */
863 switch (vidh->compat) {
864 case UBI_COMPAT_DELETE:
865 ubi_msg("\"delete\" compatible internal volume %d:%d"
866 " found, remove it", vol_id, lnum);
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300867 err = add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400868 if (err)
869 return err;
870 break;
871
872 case UBI_COMPAT_RO:
873 ubi_msg("read-only compatible internal volume %d:%d"
874 " found, switch to read-only mode",
875 vol_id, lnum);
876 ubi->ro_mode = 1;
877 break;
878
879 case UBI_COMPAT_PRESERVE:
880 ubi_msg("\"preserve\" compatible internal volume %d:%d"
881 " found", vol_id, lnum);
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300882 err = add_to_list(si, pnum, ec, &si->alien);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400883 if (err)
884 return err;
885 si->alien_peb_count += 1;
886 return 0;
887
888 case UBI_COMPAT_REJECT:
889 ubi_err("incompatible internal volume %d:%d found",
890 vol_id, lnum);
891 return -EINVAL;
892 }
893 }
894
895 /* Both UBI headers seem to be fine */
896 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
897 if (err)
898 return err;
899
900adjust_mean_ec:
901 if (!ec_corr) {
902 if (si->ec_sum + ec < ec) {
903 commit_to_mean_value(si);
904 si->ec_sum = 0;
905 si->ec_count = 0;
906 } else {
907 si->ec_sum += ec;
908 si->ec_count += 1;
909 }
910
911 if (ec > si->max_ec)
912 si->max_ec = ec;
913 if (ec < si->min_ec)
914 si->min_ec = ec;
915 }
916
917 return 0;
918}
919
920/**
921 * ubi_scan - scan an MTD device.
922 * @ubi: UBI device description object
923 *
924 * This function does full scanning of an MTD device and returns complete
925 * information about it. In case of failure, an error code is returned.
926 */
927struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
928{
929 int err, pnum;
930 struct rb_node *rb1, *rb2;
931 struct ubi_scan_volume *sv;
932 struct ubi_scan_leb *seb;
933 struct ubi_scan_info *si;
934
935 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
936 if (!si)
937 return ERR_PTR(-ENOMEM);
938
939 INIT_LIST_HEAD(&si->corr);
940 INIT_LIST_HEAD(&si->free);
941 INIT_LIST_HEAD(&si->erase);
942 INIT_LIST_HEAD(&si->alien);
943 si->volumes = RB_ROOT;
944 si->is_empty = 1;
945
946 err = -ENOMEM;
947 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
948 if (!ech)
949 goto out_si;
950
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300951 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400952 if (!vidh)
953 goto out_ech;
954
955 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
956 cond_resched();
957
958 dbg_msg("process PEB %d", pnum);
959 err = process_eb(ubi, si, pnum);
960 if (err < 0)
961 goto out_vidh;
962 }
963
964 dbg_msg("scanning is finished");
965
966 /* Finish mean erase counter calculations */
967 if (si->ec_count)
968 commit_to_mean_value(si);
969
970 if (si->is_empty)
971 ubi_msg("empty MTD device detected");
972
973 /*
974 * In case of unknown erase counter we use the mean erase counter
975 * value.
976 */
977 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
978 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
979 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
980 seb->ec = si->mean_ec;
981 }
982
983 list_for_each_entry(seb, &si->free, u.list) {
984 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
985 seb->ec = si->mean_ec;
986 }
987
988 list_for_each_entry(seb, &si->corr, u.list)
989 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
990 seb->ec = si->mean_ec;
991
992 list_for_each_entry(seb, &si->erase, u.list)
993 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
994 seb->ec = si->mean_ec;
995
996 err = paranoid_check_si(ubi, si);
997 if (err) {
998 if (err > 0)
999 err = -EINVAL;
1000 goto out_vidh;
1001 }
1002
1003 ubi_free_vid_hdr(ubi, vidh);
1004 kfree(ech);
1005
1006 return si;
1007
1008out_vidh:
1009 ubi_free_vid_hdr(ubi, vidh);
1010out_ech:
1011 kfree(ech);
1012out_si:
1013 ubi_scan_destroy_si(si);
1014 return ERR_PTR(err);
1015}
1016
1017/**
1018 * destroy_sv - free the scanning volume information
1019 * @sv: scanning volume information
1020 *
1021 * This function destroys the volume RB-tree (@sv->root) and the scanning
1022 * volume information.
1023 */
1024static void destroy_sv(struct ubi_scan_volume *sv)
1025{
1026 struct ubi_scan_leb *seb;
1027 struct rb_node *this = sv->root.rb_node;
1028
1029 while (this) {
1030 if (this->rb_left)
1031 this = this->rb_left;
1032 else if (this->rb_right)
1033 this = this->rb_right;
1034 else {
1035 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1036 this = rb_parent(this);
1037 if (this) {
1038 if (this->rb_left == &seb->u.rb)
1039 this->rb_left = NULL;
1040 else
1041 this->rb_right = NULL;
1042 }
1043
1044 kfree(seb);
1045 }
1046 }
1047 kfree(sv);
1048}
1049
1050/**
1051 * ubi_scan_destroy_si - destroy scanning information.
1052 * @si: scanning information
1053 */
1054void ubi_scan_destroy_si(struct ubi_scan_info *si)
1055{
1056 struct ubi_scan_leb *seb, *seb_tmp;
1057 struct ubi_scan_volume *sv;
1058 struct rb_node *rb;
1059
1060 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1061 list_del(&seb->u.list);
1062 kfree(seb);
1063 }
1064 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1065 list_del(&seb->u.list);
1066 kfree(seb);
1067 }
1068 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1069 list_del(&seb->u.list);
1070 kfree(seb);
1071 }
1072 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1073 list_del(&seb->u.list);
1074 kfree(seb);
1075 }
1076
1077 /* Destroy the volume RB-tree */
1078 rb = si->volumes.rb_node;
1079 while (rb) {
1080 if (rb->rb_left)
1081 rb = rb->rb_left;
1082 else if (rb->rb_right)
1083 rb = rb->rb_right;
1084 else {
1085 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1086
1087 rb = rb_parent(rb);
1088 if (rb) {
1089 if (rb->rb_left == &sv->rb)
1090 rb->rb_left = NULL;
1091 else
1092 rb->rb_right = NULL;
1093 }
1094
1095 destroy_sv(sv);
1096 }
1097 }
1098
1099 kfree(si);
1100}
1101
1102#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1103
1104/**
1105 * paranoid_check_si - check if the scanning information is correct and
1106 * consistent.
1107 * @ubi: UBI device description object
1108 * @si: scanning information
1109 *
1110 * This function returns zero if the scanning information is all right, %1 if
1111 * not and a negative error code if an error occurred.
1112 */
1113static int paranoid_check_si(const struct ubi_device *ubi,
1114 struct ubi_scan_info *si)
1115{
1116 int pnum, err, vols_found = 0;
1117 struct rb_node *rb1, *rb2;
1118 struct ubi_scan_volume *sv;
1119 struct ubi_scan_leb *seb, *last_seb;
1120 uint8_t *buf;
1121
1122 /*
Artem Bityutskiy78d87c92007-05-05 11:24:02 +03001123 * At first, check that scanning information is OK.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001124 */
1125 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1126 int leb_count = 0;
1127
1128 cond_resched();
1129
1130 vols_found += 1;
1131
1132 if (si->is_empty) {
1133 ubi_err("bad is_empty flag");
1134 goto bad_sv;
1135 }
1136
1137 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1138 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1139 sv->data_pad < 0 || sv->last_data_size < 0) {
1140 ubi_err("negative values");
1141 goto bad_sv;
1142 }
1143
1144 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1145 sv->vol_id < UBI_INTERNAL_VOL_START) {
1146 ubi_err("bad vol_id");
1147 goto bad_sv;
1148 }
1149
1150 if (sv->vol_id > si->highest_vol_id) {
1151 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1152 si->highest_vol_id, sv->vol_id);
1153 goto out;
1154 }
1155
1156 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1157 sv->vol_type != UBI_STATIC_VOLUME) {
1158 ubi_err("bad vol_type");
1159 goto bad_sv;
1160 }
1161
1162 if (sv->data_pad > ubi->leb_size / 2) {
1163 ubi_err("bad data_pad");
1164 goto bad_sv;
1165 }
1166
1167 last_seb = NULL;
1168 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1169 cond_resched();
1170
1171 last_seb = seb;
1172 leb_count += 1;
1173
1174 if (seb->pnum < 0 || seb->ec < 0) {
1175 ubi_err("negative values");
1176 goto bad_seb;
1177 }
1178
1179 if (seb->ec < si->min_ec) {
1180 ubi_err("bad si->min_ec (%d), %d found",
1181 si->min_ec, seb->ec);
1182 goto bad_seb;
1183 }
1184
1185 if (seb->ec > si->max_ec) {
1186 ubi_err("bad si->max_ec (%d), %d found",
1187 si->max_ec, seb->ec);
1188 goto bad_seb;
1189 }
1190
1191 if (seb->pnum >= ubi->peb_count) {
1192 ubi_err("too high PEB number %d, total PEBs %d",
1193 seb->pnum, ubi->peb_count);
1194 goto bad_seb;
1195 }
1196
1197 if (sv->vol_type == UBI_STATIC_VOLUME) {
1198 if (seb->lnum >= sv->used_ebs) {
1199 ubi_err("bad lnum or used_ebs");
1200 goto bad_seb;
1201 }
1202 } else {
1203 if (sv->used_ebs != 0) {
1204 ubi_err("non-zero used_ebs");
1205 goto bad_seb;
1206 }
1207 }
1208
1209 if (seb->lnum > sv->highest_lnum) {
1210 ubi_err("incorrect highest_lnum or lnum");
1211 goto bad_seb;
1212 }
1213 }
1214
1215 if (sv->leb_count != leb_count) {
1216 ubi_err("bad leb_count, %d objects in the tree",
1217 leb_count);
1218 goto bad_sv;
1219 }
1220
1221 if (!last_seb)
1222 continue;
1223
1224 seb = last_seb;
1225
1226 if (seb->lnum != sv->highest_lnum) {
1227 ubi_err("bad highest_lnum");
1228 goto bad_seb;
1229 }
1230 }
1231
1232 if (vols_found != si->vols_found) {
1233 ubi_err("bad si->vols_found %d, should be %d",
1234 si->vols_found, vols_found);
1235 goto out;
1236 }
1237
1238 /* Check that scanning information is correct */
1239 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1240 last_seb = NULL;
1241 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1242 int vol_type;
1243
1244 cond_resched();
1245
1246 last_seb = seb;
1247
1248 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1249 if (err && err != UBI_IO_BITFLIPS) {
1250 ubi_err("VID header is not OK (%d)", err);
1251 if (err > 0)
1252 err = -EIO;
1253 return err;
1254 }
1255
1256 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1257 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1258 if (sv->vol_type != vol_type) {
1259 ubi_err("bad vol_type");
1260 goto bad_vid_hdr;
1261 }
1262
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001263 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001264 ubi_err("bad sqnum %llu", seb->sqnum);
1265 goto bad_vid_hdr;
1266 }
1267
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001268 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001269 ubi_err("bad vol_id %d", sv->vol_id);
1270 goto bad_vid_hdr;
1271 }
1272
1273 if (sv->compat != vidh->compat) {
1274 ubi_err("bad compat %d", vidh->compat);
1275 goto bad_vid_hdr;
1276 }
1277
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001278 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001279 ubi_err("bad lnum %d", seb->lnum);
1280 goto bad_vid_hdr;
1281 }
1282
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001283 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001284 ubi_err("bad used_ebs %d", sv->used_ebs);
1285 goto bad_vid_hdr;
1286 }
1287
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001288 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001289 ubi_err("bad data_pad %d", sv->data_pad);
1290 goto bad_vid_hdr;
1291 }
1292
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001293 if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001294 ubi_err("bad leb_ver %u", seb->leb_ver);
1295 goto bad_vid_hdr;
1296 }
1297 }
1298
1299 if (!last_seb)
1300 continue;
1301
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001302 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001303 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1304 goto bad_vid_hdr;
1305 }
1306
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001307 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001308 ubi_err("bad last_data_size %d", sv->last_data_size);
1309 goto bad_vid_hdr;
1310 }
1311 }
1312
1313 /*
1314 * Make sure that all the physical eraseblocks are in one of the lists
1315 * or trees.
1316 */
1317 buf = kmalloc(ubi->peb_count, GFP_KERNEL);
1318 if (!buf)
1319 return -ENOMEM;
1320
1321 memset(buf, 1, ubi->peb_count);
1322 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1323 err = ubi_io_is_bad(ubi, pnum);
Artem Bityutskiy341e1a02007-05-03 11:59:51 +03001324 if (err < 0) {
1325 kfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001326 return err;
Artem Bityutskiy341e1a02007-05-03 11:59:51 +03001327 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001328 else if (err)
1329 buf[pnum] = 0;
1330 }
1331
1332 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1333 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1334 buf[seb->pnum] = 0;
1335
1336 list_for_each_entry(seb, &si->free, u.list)
1337 buf[seb->pnum] = 0;
1338
1339 list_for_each_entry(seb, &si->corr, u.list)
1340 buf[seb->pnum] = 0;
1341
1342 list_for_each_entry(seb, &si->erase, u.list)
1343 buf[seb->pnum] = 0;
1344
1345 list_for_each_entry(seb, &si->alien, u.list)
1346 buf[seb->pnum] = 0;
1347
1348 err = 0;
1349 for (pnum = 0; pnum < ubi->peb_count; pnum++)
1350 if (buf[pnum]) {
1351 ubi_err("PEB %d is not referred", pnum);
1352 err = 1;
1353 }
1354
1355 kfree(buf);
1356 if (err)
1357 goto out;
1358 return 0;
1359
1360bad_seb:
1361 ubi_err("bad scanning information about LEB %d", seb->lnum);
1362 ubi_dbg_dump_seb(seb, 0);
1363 ubi_dbg_dump_sv(sv);
1364 goto out;
1365
1366bad_sv:
1367 ubi_err("bad scanning information about volume %d", sv->vol_id);
1368 ubi_dbg_dump_sv(sv);
1369 goto out;
1370
1371bad_vid_hdr:
1372 ubi_err("bad scanning information about volume %d", sv->vol_id);
1373 ubi_dbg_dump_sv(sv);
1374 ubi_dbg_dump_vid_hdr(vidh);
1375
1376out:
1377 ubi_dbg_dump_stack();
1378 return 1;
1379}
1380
1381#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */