]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/mtd/ubi/scan.c
UBI: fix check_data_ff return code
[mirror_ubuntu-zesty-kernel.git] / drivers / mtd / ubi / scan.c
CommitLineData
801c135c
AB
1/*
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/*
85c6e6e2 22 * UBI scanning sub-system.
801c135c 23 *
85c6e6e2 24 * This sub-system is responsible for scanning the flash media, checking UBI
801c135c
AB
25 * headers and providing complete information about the UBI flash image.
26 *
78d87c95 27 * The scanning information is represented by a &struct ubi_scan_info' object.
801c135c
AB
28 * 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 *
0525dac9 32 * Scanned logical eraseblocks are represented by &struct ubi_scan_leb objects.
801c135c
AB
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.
0525dac9
AB
41 *
42 * UBI tries to distinguish between 2 types of corruptions.
43 * 1. Corruptions caused by power cuts. These are harmless and expected
44 * corruptions and UBI tries to handle them gracefully, without printing too
45 * many warnings and error messages. The idea is that we do not lose
46 * important data in these case - we may lose only the data which was being
47 * written to the media just before the power cut happened, and the upper
48 * layers are supposed to handle these situations. UBI puts these PEBs to
49 * the head of the @erase list and they are scheduled for erasure.
50 *
51 * 2. Unexpected corruptions which are not caused by power cuts. During
52 * scanning, such PEBs are put to the @corr list and UBI preserves them.
53 * Obviously, this lessens the amount of available PEBs, and if at some
54 * point UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly
55 * informs about such PEBs every time the MTD device is attached.
801c135c
AB
56 */
57
58#include <linux/err.h>
5a0e3ad6 59#include <linux/slab.h>
801c135c 60#include <linux/crc32.h>
3013ee31 61#include <linux/math64.h>
095751a6 62#include <linux/random.h>
801c135c
AB
63#include "ubi.h"
64
65#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
e88d6e10 66static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
801c135c
AB
67#else
68#define paranoid_check_si(ubi, si) 0
69#endif
70
71/* Temporary variables used during scanning */
72static struct ubi_ec_hdr *ech;
73static struct ubi_vid_hdr *vidh;
74
941dfb07 75/**
78d87c95
AB
76 * add_to_list - add physical eraseblock to a list.
77 * @si: scanning information
78 * @pnum: physical eraseblock number to add
79 * @ec: erase counter of the physical eraseblock
0525dac9 80 * @to_head: if not zero, add to the head of the list
78d87c95
AB
81 * @list: the list to add to
82 *
3fb34124 83 * This function adds physical eraseblock @pnum to free, erase, or alien lists.
0525dac9
AB
84 * If @to_head is not zero, PEB will be added to the head of the list, which
85 * basically means it will be processed first later. E.g., we add corrupted
86 * PEBs (corrupted due to power cuts) to the head of the erase list to make
87 * sure we erase them first and get rid of corruptions ASAP. This function
88 * returns zero in case of success and a negative error code in case of
3fb34124 89 * failure.
78d87c95 90 */
0525dac9 91static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, int to_head,
78d87c95 92 struct list_head *list)
801c135c
AB
93{
94 struct ubi_scan_leb *seb;
95
33789fb9 96 if (list == &si->free) {
801c135c 97 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
33789fb9 98 } else if (list == &si->erase) {
801c135c 99 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
33789fb9 100 } else if (list == &si->alien) {
801c135c 101 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
33789fb9
AB
102 si->alien_peb_count += 1;
103 } else
801c135c
AB
104 BUG();
105
106 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
107 if (!seb)
108 return -ENOMEM;
109
110 seb->pnum = pnum;
111 seb->ec = ec;
0525dac9
AB
112 if (to_head)
113 list_add(&seb->u.list, list);
114 else
115 list_add_tail(&seb->u.list, list);
801c135c
AB
116 return 0;
117}
118
3fb34124
AB
119/**
120 * add_corrupted - add a corrupted physical eraseblock.
121 * @si: scanning information
122 * @pnum: physical eraseblock number to add
123 * @ec: erase counter of the physical eraseblock
124 *
125 * This function adds corrupted physical eraseblock @pnum to the 'corr' list.
feeba4b8
AB
126 * The corruption was presumably not caused by a power cut. Returns zero in
127 * case of success and a negative error code in case of failure.
3fb34124
AB
128 */
129static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)
130{
131 struct ubi_scan_leb *seb;
132
133 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
134
135 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
136 if (!seb)
137 return -ENOMEM;
138
139 si->corr_peb_count += 1;
140 seb->pnum = pnum;
141 seb->ec = ec;
142 list_add(&seb->u.list, &si->corr);
143 return 0;
144}
145
801c135c 146/**
ebaaf1af 147 * validate_vid_hdr - check volume identifier header.
801c135c
AB
148 * @vid_hdr: the volume identifier header to check
149 * @sv: information about the volume this logical eraseblock belongs to
150 * @pnum: physical eraseblock number the VID header came from
151 *
152 * This function checks that data stored in @vid_hdr is consistent. Returns
153 * non-zero if an inconsistency was found and zero if not.
154 *
155 * Note, UBI does sanity check of everything it reads from the flash media.
85c6e6e2 156 * Most of the checks are done in the I/O sub-system. Here we check that the
801c135c
AB
157 * information in the VID header is consistent to the information in other VID
158 * headers of the same volume.
159 */
160static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
161 const struct ubi_scan_volume *sv, int pnum)
162{
163 int vol_type = vid_hdr->vol_type;
3261ebd7
CH
164 int vol_id = be32_to_cpu(vid_hdr->vol_id);
165 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
166 int data_pad = be32_to_cpu(vid_hdr->data_pad);
801c135c
AB
167
168 if (sv->leb_count != 0) {
169 int sv_vol_type;
170
171 /*
172 * This is not the first logical eraseblock belonging to this
173 * volume. Ensure that the data in its VID header is consistent
174 * to the data in previous logical eraseblock headers.
175 */
176
177 if (vol_id != sv->vol_id) {
178 dbg_err("inconsistent vol_id");
179 goto bad;
180 }
181
182 if (sv->vol_type == UBI_STATIC_VOLUME)
183 sv_vol_type = UBI_VID_STATIC;
184 else
185 sv_vol_type = UBI_VID_DYNAMIC;
186
187 if (vol_type != sv_vol_type) {
188 dbg_err("inconsistent vol_type");
189 goto bad;
190 }
191
192 if (used_ebs != sv->used_ebs) {
193 dbg_err("inconsistent used_ebs");
194 goto bad;
195 }
196
197 if (data_pad != sv->data_pad) {
198 dbg_err("inconsistent data_pad");
199 goto bad;
200 }
201 }
202
203 return 0;
204
205bad:
206 ubi_err("inconsistent VID header at PEB %d", pnum);
207 ubi_dbg_dump_vid_hdr(vid_hdr);
208 ubi_dbg_dump_sv(sv);
209 return -EINVAL;
210}
211
212/**
213 * add_volume - add volume to the scanning information.
214 * @si: scanning information
215 * @vol_id: ID of the volume to add
216 * @pnum: physical eraseblock number
217 * @vid_hdr: volume identifier header
218 *
219 * If the volume corresponding to the @vid_hdr logical eraseblock is already
220 * present in the scanning information, this function does nothing. Otherwise
221 * it adds corresponding volume to the scanning information. Returns a pointer
222 * to the scanning volume object in case of success and a negative error code
223 * in case of failure.
224 */
225static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
226 int pnum,
227 const struct ubi_vid_hdr *vid_hdr)
228{
229 struct ubi_scan_volume *sv;
230 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
231
3261ebd7 232 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
801c135c
AB
233
234 /* Walk the volume RB-tree to look if this volume is already present */
235 while (*p) {
236 parent = *p;
237 sv = rb_entry(parent, struct ubi_scan_volume, rb);
238
239 if (vol_id == sv->vol_id)
240 return sv;
241
242 if (vol_id > sv->vol_id)
243 p = &(*p)->rb_left;
244 else
245 p = &(*p)->rb_right;
246 }
247
248 /* The volume is absent - add it */
249 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
250 if (!sv)
251 return ERR_PTR(-ENOMEM);
252
253 sv->highest_lnum = sv->leb_count = 0;
801c135c
AB
254 sv->vol_id = vol_id;
255 sv->root = RB_ROOT;
3261ebd7
CH
256 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
257 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
801c135c
AB
258 sv->compat = vid_hdr->compat;
259 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
260 : UBI_STATIC_VOLUME;
261 if (vol_id > si->highest_vol_id)
262 si->highest_vol_id = vol_id;
263
264 rb_link_node(&sv->rb, parent, p);
265 rb_insert_color(&sv->rb, &si->volumes);
266 si->vols_found += 1;
267 dbg_bld("added volume %d", vol_id);
268 return sv;
269}
270
271/**
272 * compare_lebs - find out which logical eraseblock is newer.
273 * @ubi: UBI device description object
274 * @seb: first logical eraseblock to compare
275 * @pnum: physical eraseblock number of the second logical eraseblock to
276 * compare
277 * @vid_hdr: volume identifier header of the second logical eraseblock
278 *
279 * This function compares 2 copies of a LEB and informs which one is newer. In
280 * case of success this function returns a positive value, in case of failure, a
281 * negative error code is returned. The success return codes use the following
282 * bits:
3f502622 283 * o bit 0 is cleared: the first PEB (described by @seb) is newer than the
801c135c
AB
284 * second PEB (described by @pnum and @vid_hdr);
285 * o bit 0 is set: the second PEB is newer;
286 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
287 * o bit 1 is set: bit-flips were detected in the newer LEB;
288 * o bit 2 is cleared: the older LEB is not corrupted;
289 * o bit 2 is set: the older LEB is corrupted.
290 */
e88d6e10
AB
291static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
292 int pnum, const struct ubi_vid_hdr *vid_hdr)
801c135c
AB
293{
294 void *buf;
295 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
296 uint32_t data_crc, crc;
8bc22961 297 struct ubi_vid_hdr *vh = NULL;
3261ebd7 298 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
801c135c 299
9869cd80 300 if (sqnum2 == seb->sqnum) {
801c135c 301 /*
9869cd80
AB
302 * This must be a really ancient UBI image which has been
303 * created before sequence numbers support has been added. At
304 * that times we used 32-bit LEB versions stored in logical
305 * eraseblocks. That was before UBI got into mainline. We do not
0525dac9
AB
306 * support these images anymore. Well, those images still work,
307 * but only if no unclean reboots happened.
801c135c 308 */
9869cd80
AB
309 ubi_err("unsupported on-flash UBI format\n");
310 return -EINVAL;
311 }
64203195 312
9869cd80
AB
313 /* Obviously the LEB with lower sequence counter is older */
314 second_is_newer = !!(sqnum2 > seb->sqnum);
801c135c
AB
315
316 /*
317 * Now we know which copy is newer. If the copy flag of the PEB with
318 * newer version is not set, then we just return, otherwise we have to
319 * check data CRC. For the second PEB we already have the VID header,
320 * for the first one - we'll need to re-read it from flash.
321 *
9869cd80 322 * Note: this may be optimized so that we wouldn't read twice.
801c135c
AB
323 */
324
325 if (second_is_newer) {
326 if (!vid_hdr->copy_flag) {
327 /* It is not a copy, so it is newer */
328 dbg_bld("second PEB %d is newer, copy_flag is unset",
329 pnum);
330 return 1;
331 }
332 } else {
fb22b59b
AB
333 if (!seb->copy_flag) {
334 /* It is not a copy, so it is newer */
335 dbg_bld("first PEB %d is newer, copy_flag is unset",
336 pnum);
337 return bitflips << 1;
338 }
801c135c 339
33818bbb 340 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
8bc22961 341 if (!vh)
801c135c
AB
342 return -ENOMEM;
343
fb22b59b 344 pnum = seb->pnum;
8bc22961 345 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
801c135c
AB
346 if (err) {
347 if (err == UBI_IO_BITFLIPS)
348 bitflips = 1;
349 else {
350 dbg_err("VID of PEB %d header is bad, but it "
0525dac9 351 "was OK earlier, err %d", pnum, err);
801c135c
AB
352 if (err > 0)
353 err = -EIO;
354
355 goto out_free_vidh;
356 }
357 }
358
8bc22961 359 vid_hdr = vh;
801c135c
AB
360 }
361
362 /* Read the data of the copy and check the CRC */
363
3261ebd7 364 len = be32_to_cpu(vid_hdr->data_size);
92ad8f37 365 buf = vmalloc(len);
801c135c
AB
366 if (!buf) {
367 err = -ENOMEM;
368 goto out_free_vidh;
369 }
370
371 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
b77bcb07 372 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
801c135c
AB
373 goto out_free_buf;
374
3261ebd7 375 data_crc = be32_to_cpu(vid_hdr->data_crc);
801c135c
AB
376 crc = crc32(UBI_CRC32_INIT, buf, len);
377 if (crc != data_crc) {
378 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
379 pnum, crc, data_crc);
380 corrupted = 1;
381 bitflips = 0;
382 second_is_newer = !second_is_newer;
383 } else {
384 dbg_bld("PEB %d CRC is OK", pnum);
385 bitflips = !!err;
386 }
387
92ad8f37 388 vfree(buf);
8bc22961 389 ubi_free_vid_hdr(ubi, vh);
801c135c
AB
390
391 if (second_is_newer)
392 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
393 else
394 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
395
396 return second_is_newer | (bitflips << 1) | (corrupted << 2);
397
398out_free_buf:
92ad8f37 399 vfree(buf);
801c135c 400out_free_vidh:
8bc22961 401 ubi_free_vid_hdr(ubi, vh);
801c135c
AB
402 return err;
403}
404
405/**
ebaaf1af 406 * ubi_scan_add_used - add physical eraseblock to the scanning information.
801c135c
AB
407 * @ubi: UBI device description object
408 * @si: scanning information
409 * @pnum: the physical eraseblock number
410 * @ec: erase counter
411 * @vid_hdr: the volume identifier header
412 * @bitflips: if bit-flips were detected when this physical eraseblock was read
413 *
79b510c0
AB
414 * This function adds information about a used physical eraseblock to the
415 * 'used' tree of the corresponding volume. The function is rather complex
416 * because it has to handle cases when this is not the first physical
417 * eraseblock belonging to the same logical eraseblock, and the newer one has
418 * to be picked, while the older one has to be dropped. This function returns
419 * zero in case of success and a negative error code in case of failure.
801c135c 420 */
e88d6e10 421int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
801c135c
AB
422 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
423 int bitflips)
424{
425 int err, vol_id, lnum;
801c135c
AB
426 unsigned long long sqnum;
427 struct ubi_scan_volume *sv;
428 struct ubi_scan_leb *seb;
429 struct rb_node **p, *parent = NULL;
430
3261ebd7
CH
431 vol_id = be32_to_cpu(vid_hdr->vol_id);
432 lnum = be32_to_cpu(vid_hdr->lnum);
433 sqnum = be64_to_cpu(vid_hdr->sqnum);
801c135c 434
9869cd80
AB
435 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
436 pnum, vol_id, lnum, ec, sqnum, bitflips);
801c135c
AB
437
438 sv = add_volume(si, vol_id, pnum, vid_hdr);
0e4a008a 439 if (IS_ERR(sv))
801c135c
AB
440 return PTR_ERR(sv);
441
76eafe47
BS
442 if (si->max_sqnum < sqnum)
443 si->max_sqnum = sqnum;
444
801c135c
AB
445 /*
446 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
447 * if this is the first instance of this logical eraseblock or not.
448 */
449 p = &sv->root.rb_node;
450 while (*p) {
451 int cmp_res;
452
453 parent = *p;
454 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
455 if (lnum != seb->lnum) {
456 if (lnum < seb->lnum)
457 p = &(*p)->rb_left;
458 else
459 p = &(*p)->rb_right;
460 continue;
461 }
462
463 /*
464 * There is already a physical eraseblock describing the same
465 * logical eraseblock present.
466 */
467
468 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
9869cd80 469 "EC %d", seb->pnum, seb->sqnum, seb->ec);
801c135c
AB
470
471 /*
472 * Make sure that the logical eraseblocks have different
473 * sequence numbers. Otherwise the image is bad.
474 *
9869cd80
AB
475 * However, if the sequence number is zero, we assume it must
476 * be an ancient UBI image from the era when UBI did not have
477 * sequence numbers. We still can attach these images, unless
478 * there is a need to distinguish between old and new
479 * eraseblocks, in which case we'll refuse the image in
480 * 'compare_lebs()'. In other words, we attach old clean
481 * images, but refuse attaching old images with duplicated
482 * logical eraseblocks because there was an unclean reboot.
801c135c
AB
483 */
484 if (seb->sqnum == sqnum && sqnum != 0) {
485 ubi_err("two LEBs with same sequence number %llu",
486 sqnum);
487 ubi_dbg_dump_seb(seb, 0);
488 ubi_dbg_dump_vid_hdr(vid_hdr);
489 return -EINVAL;
490 }
491
492 /*
493 * Now we have to drop the older one and preserve the newer
494 * one.
495 */
496 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
497 if (cmp_res < 0)
498 return cmp_res;
499
500 if (cmp_res & 1) {
501 /*
3f502622 502 * This logical eraseblock is newer than the one
801c135c
AB
503 * found earlier.
504 */
505 err = validate_vid_hdr(vid_hdr, sv, pnum);
506 if (err)
507 return err;
508
0525dac9
AB
509 err = add_to_list(si, seb->pnum, seb->ec, cmp_res & 4,
510 &si->erase);
801c135c
AB
511 if (err)
512 return err;
513
514 seb->ec = ec;
515 seb->pnum = pnum;
516 seb->scrub = ((cmp_res & 2) || bitflips);
fb22b59b 517 seb->copy_flag = vid_hdr->copy_flag;
801c135c 518 seb->sqnum = sqnum;
801c135c
AB
519
520 if (sv->highest_lnum == lnum)
521 sv->last_data_size =
3261ebd7 522 be32_to_cpu(vid_hdr->data_size);
801c135c
AB
523
524 return 0;
525 } else {
526 /*
025dfdaf 527 * This logical eraseblock is older than the one found
801c135c
AB
528 * previously.
529 */
0525dac9
AB
530 return add_to_list(si, pnum, ec, cmp_res & 4,
531 &si->erase);
801c135c
AB
532 }
533 }
534
535 /*
536 * We've met this logical eraseblock for the first time, add it to the
537 * scanning information.
538 */
539
540 err = validate_vid_hdr(vid_hdr, sv, pnum);
541 if (err)
542 return err;
543
544 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
545 if (!seb)
546 return -ENOMEM;
547
548 seb->ec = ec;
549 seb->pnum = pnum;
550 seb->lnum = lnum;
801c135c 551 seb->scrub = bitflips;
fb22b59b
AB
552 seb->copy_flag = vid_hdr->copy_flag;
553 seb->sqnum = sqnum;
801c135c
AB
554
555 if (sv->highest_lnum <= lnum) {
556 sv->highest_lnum = lnum;
3261ebd7 557 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
801c135c
AB
558 }
559
801c135c
AB
560 sv->leb_count += 1;
561 rb_link_node(&seb->u.rb, parent, p);
562 rb_insert_color(&seb->u.rb, &sv->root);
563 return 0;
564}
565
566/**
ebaaf1af 567 * ubi_scan_find_sv - find volume in the scanning information.
801c135c
AB
568 * @si: scanning information
569 * @vol_id: the requested volume ID
570 *
571 * This function returns a pointer to the volume description or %NULL if there
572 * are no data about this volume in the scanning information.
573 */
574struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
575 int vol_id)
576{
577 struct ubi_scan_volume *sv;
578 struct rb_node *p = si->volumes.rb_node;
579
580 while (p) {
581 sv = rb_entry(p, struct ubi_scan_volume, rb);
582
583 if (vol_id == sv->vol_id)
584 return sv;
585
586 if (vol_id > sv->vol_id)
587 p = p->rb_left;
588 else
589 p = p->rb_right;
590 }
591
592 return NULL;
593}
594
595/**
ebaaf1af 596 * ubi_scan_find_seb - find LEB in the volume scanning information.
801c135c
AB
597 * @sv: a pointer to the volume scanning information
598 * @lnum: the requested logical eraseblock
599 *
600 * This function returns a pointer to the scanning logical eraseblock or %NULL
601 * if there are no data about it in the scanning volume information.
602 */
603struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
604 int lnum)
605{
606 struct ubi_scan_leb *seb;
607 struct rb_node *p = sv->root.rb_node;
608
609 while (p) {
610 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
611
612 if (lnum == seb->lnum)
613 return seb;
614
615 if (lnum > seb->lnum)
616 p = p->rb_left;
617 else
618 p = p->rb_right;
619 }
620
621 return NULL;
622}
623
624/**
625 * ubi_scan_rm_volume - delete scanning information about a volume.
626 * @si: scanning information
627 * @sv: the volume scanning information to delete
628 */
629void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
630{
631 struct rb_node *rb;
632 struct ubi_scan_leb *seb;
633
634 dbg_bld("remove scanning information about volume %d", sv->vol_id);
635
636 while ((rb = rb_first(&sv->root))) {
637 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
638 rb_erase(&seb->u.rb, &sv->root);
639 list_add_tail(&seb->u.list, &si->erase);
640 }
641
642 rb_erase(&sv->rb, &si->volumes);
643 kfree(sv);
644 si->vols_found -= 1;
645}
646
647/**
648 * ubi_scan_erase_peb - erase a physical eraseblock.
649 * @ubi: UBI device description object
650 * @si: scanning information
651 * @pnum: physical eraseblock number to erase;
652 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
653 *
654 * This function erases physical eraseblock 'pnum', and writes the erase
655 * counter header to it. This function should only be used on UBI device
85c6e6e2
AB
656 * initialization stages, when the EBA sub-system had not been yet initialized.
657 * This function returns zero in case of success and a negative error code in
658 * case of failure.
801c135c 659 */
e88d6e10
AB
660int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
661 int pnum, int ec)
801c135c
AB
662{
663 int err;
664 struct ubi_ec_hdr *ec_hdr;
665
801c135c
AB
666 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
667 /*
668 * Erase counter overflow. Upgrade UBI and use 64-bit
669 * erase counters internally.
670 */
671 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
672 return -EINVAL;
673 }
674
dcec4c3b
FM
675 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
676 if (!ec_hdr)
677 return -ENOMEM;
678
3261ebd7 679 ec_hdr->ec = cpu_to_be64(ec);
801c135c
AB
680
681 err = ubi_io_sync_erase(ubi, pnum, 0);
682 if (err < 0)
683 goto out_free;
684
685 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
686
687out_free:
688 kfree(ec_hdr);
689 return err;
690}
691
692/**
693 * ubi_scan_get_free_peb - get a free physical eraseblock.
694 * @ubi: UBI device description object
695 * @si: scanning information
696 *
697 * This function returns a free physical eraseblock. It is supposed to be
85c6e6e2
AB
698 * called on the UBI initialization stages when the wear-leveling sub-system is
699 * not initialized yet. This function picks a physical eraseblocks from one of
700 * the lists, writes the EC header if it is needed, and removes it from the
701 * list.
801c135c
AB
702 *
703 * This function returns scanning physical eraseblock information in case of
704 * success and an error code in case of failure.
705 */
e88d6e10 706struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
801c135c
AB
707 struct ubi_scan_info *si)
708{
5fc01ab6
AB
709 int err = 0;
710 struct ubi_scan_leb *seb, *tmp_seb;
801c135c
AB
711
712 if (!list_empty(&si->free)) {
713 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
714 list_del(&seb->u.list);
715 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
716 return seb;
717 }
718
5fc01ab6
AB
719 /*
720 * We try to erase the first physical eraseblock from the erase list
721 * and pick it if we succeed, or try to erase the next one if not. And
722 * so forth. We don't want to take care about bad eraseblocks here -
723 * they'll be handled later.
724 */
725 list_for_each_entry_safe(seb, tmp_seb, &si->erase, u.list) {
726 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
727 seb->ec = si->mean_ec;
801c135c 728
5fc01ab6
AB
729 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
730 if (err)
731 continue;
801c135c 732
5fc01ab6
AB
733 seb->ec += 1;
734 list_del(&seb->u.list);
735 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
736 return seb;
801c135c
AB
737 }
738
5fc01ab6 739 ubi_err("no free eraseblocks");
801c135c
AB
740 return ERR_PTR(-ENOSPC);
741}
742
feeba4b8
AB
743/**
744 * check_data_ff - make sure PEB contains only 0xFF data.
745 * @ubi: UBI device description object
746 * @vid_hrd: the (corrupted) VID header of this PEB
747 * @pnum: the physical eraseblock number to check
748 *
749 * This is a helper function which is used to distinguish between VID header
750 * corruptions caused by power cuts and other reasons. If the PEB contains only
751 * 0xFF bytes at the data area, the VID header is most probably corrupted
752 * because of a power cut (%0 is returned in this case). Otherwise, it was
753 * corrupted for some other reasons (%1 is returned in this case). A negative
754 * error code is returned if a read error occurred.
755 *
756 * If the corruption reason was a power cut, UBI can safely erase this PEB.
757 * Otherwise, it should preserve it to avoid possibly destroying important
758 * information.
759 */
760static int check_data_ff(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
761 int pnum)
762{
763 int err;
764
765 mutex_lock(&ubi->buf_mutex);
766 memset(ubi->peb_buf1, 0x00, ubi->leb_size);
767
768 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, ubi->leb_start,
769 ubi->leb_size);
770 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
771 return err;
772
773 if (ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->leb_size)) {
774 mutex_unlock(&ubi->buf_mutex);
775 return 0;
776 }
777
778 ubi_err("PEB %d contains corrupted VID header, and the data does not "
779 "contain all 0xFF, this may be a non-UBI PEB or a severe VID "
780 "header corruption which requires manual inspection", pnum);
781 ubi_dbg_dump_vid_hdr(vid_hdr);
782 dbg_msg("hexdump of PEB %d offset %d, length %d",
783 pnum, ubi->leb_start, ubi->leb_size);
784 ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
785 ubi->peb_buf1, ubi->leb_size, 1);
786 mutex_unlock(&ubi->buf_mutex);
df3fca4c 787 return 1;
feeba4b8
AB
788}
789
801c135c 790/**
ebaaf1af 791 * process_eb - read, check UBI headers, and add them to scanning information.
801c135c
AB
792 * @ubi: UBI device description object
793 * @si: scanning information
794 * @pnum: the physical eraseblock number
795 *
78d87c95 796 * This function returns a zero if the physical eraseblock was successfully
801c135c
AB
797 * handled and a negative error code in case of failure.
798 */
9c9ec147
AB
799static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
800 int pnum)
801c135c 801{
c18a8418 802 long long uninitialized_var(ec);
e0e718c2 803 int err, bitflips = 0, vol_id, ec_err = 0;
801c135c
AB
804
805 dbg_bld("scan PEB %d", pnum);
806
807 /* Skip bad physical eraseblocks */
808 err = ubi_io_is_bad(ubi, pnum);
809 if (err < 0)
810 return err;
811 else if (err) {
812 /*
85c6e6e2
AB
813 * FIXME: this is actually duty of the I/O sub-system to
814 * initialize this, but MTD does not provide enough
815 * information.
801c135c
AB
816 */
817 si->bad_peb_count += 1;
818 return 0;
819 }
820
821 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
822 if (err < 0)
823 return err;
b3321508
AB
824 switch (err) {
825 case 0:
826 break;
827 case UBI_IO_BITFLIPS:
801c135c 828 bitflips = 1;
b3321508
AB
829 break;
830 case UBI_IO_FF:
0525dac9
AB
831 si->empty_peb_count += 1;
832 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 0,
833 &si->erase);
b3321508 834 case UBI_IO_FF_BITFLIPS:
0525dac9
AB
835 si->empty_peb_count += 1;
836 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 1,
837 &si->erase);
b3321508 838 case UBI_IO_BAD_HDR_EBADMSG:
b3321508 839 case UBI_IO_BAD_HDR:
801c135c
AB
840 /*
841 * We have to also look at the VID header, possibly it is not
842 * corrupted. Set %bitflips flag in order to make this PEB be
843 * moved and EC be re-created.
844 */
e0e718c2 845 ec_err = err;
801c135c
AB
846 ec = UBI_SCAN_UNKNOWN_EC;
847 bitflips = 1;
b3321508
AB
848 break;
849 default:
850 ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
851 return -EINVAL;
801c135c
AB
852 }
853
e0e718c2 854 if (!ec_err) {
fe96efc1
AB
855 int image_seq;
856
801c135c
AB
857 /* Make sure UBI version is OK */
858 if (ech->version != UBI_VERSION) {
859 ubi_err("this UBI version is %d, image version is %d",
860 UBI_VERSION, (int)ech->version);
861 return -EINVAL;
862 }
863
3261ebd7 864 ec = be64_to_cpu(ech->ec);
801c135c
AB
865 if (ec > UBI_MAX_ERASECOUNTER) {
866 /*
867 * Erase counter overflow. The EC headers have 64 bits
868 * reserved, but we anyway make use of only 31 bit
869 * values, as this seems to be enough for any existing
870 * flash. Upgrade UBI and use 64-bit erase counters
871 * internally.
872 */
873 ubi_err("erase counter overflow, max is %d",
874 UBI_MAX_ERASECOUNTER);
875 ubi_dbg_dump_ec_hdr(ech);
876 return -EINVAL;
877 }
fe96efc1 878
32bc4820
AH
879 /*
880 * Make sure that all PEBs have the same image sequence number.
881 * This allows us to detect situations when users flash UBI
882 * images incorrectly, so that the flash has the new UBI image
883 * and leftovers from the old one. This feature was added
884 * relatively recently, and the sequence number was always
885 * zero, because old UBI implementations always set it to zero.
886 * For this reasons, we do not panic if some PEBs have zero
887 * sequence number, while other PEBs have non-zero sequence
888 * number.
889 */
3dc948da 890 image_seq = be32_to_cpu(ech->image_seq);
2eadaad6 891 if (!ubi->image_seq && image_seq)
fe96efc1 892 ubi->image_seq = image_seq;
2eadaad6
AB
893 if (ubi->image_seq && image_seq &&
894 ubi->image_seq != image_seq) {
fe96efc1
AB
895 ubi_err("bad image sequence number %d in PEB %d, "
896 "expected %d", image_seq, pnum, ubi->image_seq);
897 ubi_dbg_dump_ec_hdr(ech);
898 return -EINVAL;
899 }
801c135c
AB
900 }
901
902 /* OK, we've done with the EC header, let's look at the VID header */
903
904 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
905 if (err < 0)
906 return err;
b3321508
AB
907 switch (err) {
908 case 0:
909 break;
910 case UBI_IO_BITFLIPS:
801c135c 911 bitflips = 1;
b3321508
AB
912 break;
913 case UBI_IO_BAD_HDR_EBADMSG:
0525dac9
AB
914 if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
915 /*
916 * Both EC and VID headers are corrupted and were read
917 * with data integrity error, probably this is a bad
918 * PEB, bit it is not marked as bad yet. This may also
919 * be a result of power cut during erasure.
920 */
921 si->maybe_bad_peb_count += 1;
b3321508 922 case UBI_IO_BAD_HDR:
feeba4b8
AB
923 if (ec_err)
924 /*
925 * Both headers are corrupted. There is a possibility
926 * that this a valid UBI PEB which has corresponding
927 * LEB, but the headers are corrupted. However, it is
928 * impossible to distinguish it from a PEB which just
929 * contains garbage because a power cut during erase
930 * operation. So we just schedule this PEB for erasure.
931 */
932 err = 0;
933 else
934 /*
935 * The EC was OK, but the VID header is corrupted. We
936 * have to check what is in the data area.
937 */
938 err = check_data_ff(ubi, vidh, pnum);
df3fca4c
AB
939
940 if (err < 0)
941 return err;
942 else if (!err)
feeba4b8
AB
943 /* This corruption is caused by a power cut */
944 err = add_to_list(si, pnum, ec, 1, &si->erase);
945 else
946 /* This is an unexpected corruption */
947 err = add_corrupted(si, pnum, ec);
948 if (err)
949 return err;
950 goto adjust_mean_ec;
b3321508 951 case UBI_IO_FF_BITFLIPS:
0525dac9 952 err = add_to_list(si, pnum, ec, 1, &si->erase);
801c135c
AB
953 if (err)
954 return err;
955 goto adjust_mean_ec;
b3321508
AB
956 case UBI_IO_FF:
957 if (ec_err)
0525dac9 958 err = add_to_list(si, pnum, ec, 1, &si->erase);
b3321508 959 else
0525dac9 960 err = add_to_list(si, pnum, ec, 0, &si->free);
801c135c
AB
961 if (err)
962 return err;
963 goto adjust_mean_ec;
b3321508
AB
964 default:
965 ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
966 err);
967 return -EINVAL;
801c135c
AB
968 }
969
3261ebd7 970 vol_id = be32_to_cpu(vidh->vol_id);
91f2d53c 971 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
3261ebd7 972 int lnum = be32_to_cpu(vidh->lnum);
801c135c
AB
973
974 /* Unsupported internal volume */
975 switch (vidh->compat) {
976 case UBI_COMPAT_DELETE:
977 ubi_msg("\"delete\" compatible internal volume %d:%d"
158132c9 978 " found, will remove it", vol_id, lnum);
0525dac9 979 err = add_to_list(si, pnum, ec, 1, &si->erase);
801c135c
AB
980 if (err)
981 return err;
158132c9 982 return 0;
801c135c
AB
983
984 case UBI_COMPAT_RO:
985 ubi_msg("read-only compatible internal volume %d:%d"
986 " found, switch to read-only mode",
987 vol_id, lnum);
988 ubi->ro_mode = 1;
989 break;
990
991 case UBI_COMPAT_PRESERVE:
992 ubi_msg("\"preserve\" compatible internal volume %d:%d"
993 " found", vol_id, lnum);
0525dac9 994 err = add_to_list(si, pnum, ec, 0, &si->alien);
801c135c
AB
995 if (err)
996 return err;
801c135c
AB
997 return 0;
998
999 case UBI_COMPAT_REJECT:
1000 ubi_err("incompatible internal volume %d:%d found",
1001 vol_id, lnum);
1002 return -EINVAL;
1003 }
1004 }
1005
e0e718c2 1006 if (ec_err)
29a88c99
AB
1007 ubi_warn("valid VID header but corrupted EC header at PEB %d",
1008 pnum);
801c135c
AB
1009 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
1010 if (err)
1011 return err;
1012
1013adjust_mean_ec:
e0e718c2 1014 if (!ec_err) {
4bc1dca4
AB
1015 si->ec_sum += ec;
1016 si->ec_count += 1;
801c135c
AB
1017 if (ec > si->max_ec)
1018 si->max_ec = ec;
1019 if (ec < si->min_ec)
1020 si->min_ec = ec;
1021 }
1022
1023 return 0;
1024}
1025
0798cea8
AB
1026/**
1027 * check_what_we_have - check what PEB were found by scanning.
1028 * @ubi: UBI device description object
1029 * @si: scanning information
1030 *
1031 * This is a helper function which takes a look what PEBs were found by
1032 * scanning, and decides whether the flash is empty and should be formatted and
1033 * whether there are too many corrupted PEBs and we should not attach this
1034 * MTD device. Returns zero if we should proceed with attaching the MTD device,
1035 * and %-EINVAL if we should not.
1036 */
f5d5b1f8 1037static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
0798cea8
AB
1038{
1039 struct ubi_scan_leb *seb;
0525dac9 1040 int max_corr, peb_count;
0798cea8 1041
0525dac9
AB
1042 peb_count = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
1043 max_corr = peb_count / 20 ?: 8;
0798cea8
AB
1044
1045 /*
0525dac9 1046 * Few corrupted PEBs is not a problem and may be just a result of
0798cea8
AB
1047 * unclean reboots. However, many of them may indicate some problems
1048 * with the flash HW or driver.
1049 */
0525dac9
AB
1050 if (si->corr_peb_count) {
1051 ubi_err("%d PEBs are corrupted and preserved",
1052 si->corr_peb_count);
1053 printk(KERN_ERR "Corrupted PEBs are:");
0798cea8
AB
1054 list_for_each_entry(seb, &si->corr, u.list)
1055 printk(KERN_CONT " %d", seb->pnum);
1056 printk(KERN_CONT "\n");
1057
1058 /*
1059 * If too many PEBs are corrupted, we refuse attaching,
1060 * otherwise, only print a warning.
1061 */
1062 if (si->corr_peb_count >= max_corr) {
1063 ubi_err("too many corrupted PEBs, refusing this device");
1064 return -EINVAL;
1065 }
1066 }
1067
0525dac9
AB
1068 if (si->empty_peb_count + si->maybe_bad_peb_count == peb_count) {
1069 /*
1070 * All PEBs are empty, or almost all - a couple PEBs look like
1071 * they may be bad PEBs which were not marked as bad yet.
1072 *
1073 * This piece of code basically tries to distinguish between
1074 * the following situations:
1075 *
1076 * 1. Flash is empty, but there are few bad PEBs, which are not
1077 * marked as bad so far, and which were read with error. We
1078 * want to go ahead and format this flash. While formatting,
1079 * the faulty PEBs will probably be marked as bad.
1080 *
1081 * 2. Flash contains non-UBI data and we do not want to format
1082 * it and destroy possibly important information.
1083 */
1084 if (si->maybe_bad_peb_count <= 2) {
0798cea8
AB
1085 si->is_empty = 1;
1086 ubi_msg("empty MTD device detected");
0525dac9
AB
1087 get_random_bytes(&ubi->image_seq,
1088 sizeof(ubi->image_seq));
0798cea8 1089 } else {
0525dac9
AB
1090 ubi_err("MTD device is not UBI-formatted and possibly "
1091 "contains non-UBI data - refusing it");
0798cea8
AB
1092 return -EINVAL;
1093 }
0525dac9 1094
0798cea8
AB
1095 }
1096
0798cea8
AB
1097 return 0;
1098}
1099
801c135c
AB
1100/**
1101 * ubi_scan - scan an MTD device.
1102 * @ubi: UBI device description object
1103 *
1104 * This function does full scanning of an MTD device and returns complete
1105 * information about it. In case of failure, an error code is returned.
1106 */
1107struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
1108{
1109 int err, pnum;
1110 struct rb_node *rb1, *rb2;
1111 struct ubi_scan_volume *sv;
1112 struct ubi_scan_leb *seb;
1113 struct ubi_scan_info *si;
1114
1115 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
1116 if (!si)
1117 return ERR_PTR(-ENOMEM);
1118
1119 INIT_LIST_HEAD(&si->corr);
1120 INIT_LIST_HEAD(&si->free);
1121 INIT_LIST_HEAD(&si->erase);
1122 INIT_LIST_HEAD(&si->alien);
1123 si->volumes = RB_ROOT;
801c135c
AB
1124
1125 err = -ENOMEM;
1126 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1127 if (!ech)
1128 goto out_si;
1129
33818bbb 1130 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
801c135c
AB
1131 if (!vidh)
1132 goto out_ech;
1133
1134 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1135 cond_resched();
1136
c8566350 1137 dbg_gen("process PEB %d", pnum);
801c135c
AB
1138 err = process_eb(ubi, si, pnum);
1139 if (err < 0)
1140 goto out_vidh;
1141 }
1142
1143 dbg_msg("scanning is finished");
1144
4bc1dca4 1145 /* Calculate mean erase counter */
3013ee31
AB
1146 if (si->ec_count)
1147 si->mean_ec = div_u64(si->ec_sum, si->ec_count);
801c135c 1148
0798cea8
AB
1149 err = check_what_we_have(ubi, si);
1150 if (err)
1151 goto out_vidh;
4a406856 1152
801c135c
AB
1153 /*
1154 * In case of unknown erase counter we use the mean erase counter
1155 * value.
1156 */
1157 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1158 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1159 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1160 seb->ec = si->mean_ec;
1161 }
1162
1163 list_for_each_entry(seb, &si->free, u.list) {
1164 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1165 seb->ec = si->mean_ec;
1166 }
1167
1168 list_for_each_entry(seb, &si->corr, u.list)
1169 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1170 seb->ec = si->mean_ec;
1171
1172 list_for_each_entry(seb, &si->erase, u.list)
1173 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1174 seb->ec = si->mean_ec;
1175
1176 err = paranoid_check_si(ubi, si);
adbf05e3 1177 if (err)
801c135c 1178 goto out_vidh;
801c135c
AB
1179
1180 ubi_free_vid_hdr(ubi, vidh);
1181 kfree(ech);
1182
1183 return si;
1184
1185out_vidh:
1186 ubi_free_vid_hdr(ubi, vidh);
1187out_ech:
1188 kfree(ech);
1189out_si:
1190 ubi_scan_destroy_si(si);
1191 return ERR_PTR(err);
1192}
1193
1194/**
1195 * destroy_sv - free the scanning volume information
1196 * @sv: scanning volume information
1197 *
1198 * This function destroys the volume RB-tree (@sv->root) and the scanning
1199 * volume information.
1200 */
1201static void destroy_sv(struct ubi_scan_volume *sv)
1202{
1203 struct ubi_scan_leb *seb;
1204 struct rb_node *this = sv->root.rb_node;
1205
1206 while (this) {
1207 if (this->rb_left)
1208 this = this->rb_left;
1209 else if (this->rb_right)
1210 this = this->rb_right;
1211 else {
1212 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1213 this = rb_parent(this);
1214 if (this) {
1215 if (this->rb_left == &seb->u.rb)
1216 this->rb_left = NULL;
1217 else
1218 this->rb_right = NULL;
1219 }
1220
1221 kfree(seb);
1222 }
1223 }
1224 kfree(sv);
1225}
1226
1227/**
1228 * ubi_scan_destroy_si - destroy scanning information.
1229 * @si: scanning information
1230 */
1231void ubi_scan_destroy_si(struct ubi_scan_info *si)
1232{
1233 struct ubi_scan_leb *seb, *seb_tmp;
1234 struct ubi_scan_volume *sv;
1235 struct rb_node *rb;
1236
1237 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1238 list_del(&seb->u.list);
1239 kfree(seb);
1240 }
1241 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1242 list_del(&seb->u.list);
1243 kfree(seb);
1244 }
1245 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1246 list_del(&seb->u.list);
1247 kfree(seb);
1248 }
1249 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1250 list_del(&seb->u.list);
1251 kfree(seb);
1252 }
1253
1254 /* Destroy the volume RB-tree */
1255 rb = si->volumes.rb_node;
1256 while (rb) {
1257 if (rb->rb_left)
1258 rb = rb->rb_left;
1259 else if (rb->rb_right)
1260 rb = rb->rb_right;
1261 else {
1262 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1263
1264 rb = rb_parent(rb);
1265 if (rb) {
1266 if (rb->rb_left == &sv->rb)
1267 rb->rb_left = NULL;
1268 else
1269 rb->rb_right = NULL;
1270 }
1271
1272 destroy_sv(sv);
1273 }
1274 }
1275
1276 kfree(si);
1277}
1278
1279#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1280
1281/**
ebaaf1af 1282 * paranoid_check_si - check the scanning information.
801c135c
AB
1283 * @ubi: UBI device description object
1284 * @si: scanning information
1285 *
adbf05e3
AB
1286 * This function returns zero if the scanning information is all right, and a
1287 * negative error code if not or if an error occurred.
801c135c 1288 */
e88d6e10 1289static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
801c135c
AB
1290{
1291 int pnum, err, vols_found = 0;
1292 struct rb_node *rb1, *rb2;
1293 struct ubi_scan_volume *sv;
1294 struct ubi_scan_leb *seb, *last_seb;
1295 uint8_t *buf;
1296
1297 /*
78d87c95 1298 * At first, check that scanning information is OK.
801c135c
AB
1299 */
1300 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1301 int leb_count = 0;
1302
1303 cond_resched();
1304
1305 vols_found += 1;
1306
1307 if (si->is_empty) {
1308 ubi_err("bad is_empty flag");
1309 goto bad_sv;
1310 }
1311
1312 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1313 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1314 sv->data_pad < 0 || sv->last_data_size < 0) {
1315 ubi_err("negative values");
1316 goto bad_sv;
1317 }
1318
1319 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1320 sv->vol_id < UBI_INTERNAL_VOL_START) {
1321 ubi_err("bad vol_id");
1322 goto bad_sv;
1323 }
1324
1325 if (sv->vol_id > si->highest_vol_id) {
1326 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1327 si->highest_vol_id, sv->vol_id);
1328 goto out;
1329 }
1330
1331 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1332 sv->vol_type != UBI_STATIC_VOLUME) {
1333 ubi_err("bad vol_type");
1334 goto bad_sv;
1335 }
1336
1337 if (sv->data_pad > ubi->leb_size / 2) {
1338 ubi_err("bad data_pad");
1339 goto bad_sv;
1340 }
1341
1342 last_seb = NULL;
1343 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1344 cond_resched();
1345
1346 last_seb = seb;
1347 leb_count += 1;
1348
1349 if (seb->pnum < 0 || seb->ec < 0) {
1350 ubi_err("negative values");
1351 goto bad_seb;
1352 }
1353
1354 if (seb->ec < si->min_ec) {
1355 ubi_err("bad si->min_ec (%d), %d found",
1356 si->min_ec, seb->ec);
1357 goto bad_seb;
1358 }
1359
1360 if (seb->ec > si->max_ec) {
1361 ubi_err("bad si->max_ec (%d), %d found",
1362 si->max_ec, seb->ec);
1363 goto bad_seb;
1364 }
1365
1366 if (seb->pnum >= ubi->peb_count) {
1367 ubi_err("too high PEB number %d, total PEBs %d",
1368 seb->pnum, ubi->peb_count);
1369 goto bad_seb;
1370 }
1371
1372 if (sv->vol_type == UBI_STATIC_VOLUME) {
1373 if (seb->lnum >= sv->used_ebs) {
1374 ubi_err("bad lnum or used_ebs");
1375 goto bad_seb;
1376 }
1377 } else {
1378 if (sv->used_ebs != 0) {
1379 ubi_err("non-zero used_ebs");
1380 goto bad_seb;
1381 }
1382 }
1383
1384 if (seb->lnum > sv->highest_lnum) {
1385 ubi_err("incorrect highest_lnum or lnum");
1386 goto bad_seb;
1387 }
1388 }
1389
1390 if (sv->leb_count != leb_count) {
1391 ubi_err("bad leb_count, %d objects in the tree",
1392 leb_count);
1393 goto bad_sv;
1394 }
1395
1396 if (!last_seb)
1397 continue;
1398
1399 seb = last_seb;
1400
1401 if (seb->lnum != sv->highest_lnum) {
1402 ubi_err("bad highest_lnum");
1403 goto bad_seb;
1404 }
1405 }
1406
1407 if (vols_found != si->vols_found) {
1408 ubi_err("bad si->vols_found %d, should be %d",
1409 si->vols_found, vols_found);
1410 goto out;
1411 }
1412
1413 /* Check that scanning information is correct */
1414 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1415 last_seb = NULL;
1416 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1417 int vol_type;
1418
1419 cond_resched();
1420
1421 last_seb = seb;
1422
1423 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1424 if (err && err != UBI_IO_BITFLIPS) {
1425 ubi_err("VID header is not OK (%d)", err);
1426 if (err > 0)
1427 err = -EIO;
1428 return err;
1429 }
1430
1431 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1432 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1433 if (sv->vol_type != vol_type) {
1434 ubi_err("bad vol_type");
1435 goto bad_vid_hdr;
1436 }
1437
3261ebd7 1438 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
801c135c
AB
1439 ubi_err("bad sqnum %llu", seb->sqnum);
1440 goto bad_vid_hdr;
1441 }
1442
3261ebd7 1443 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
801c135c
AB
1444 ubi_err("bad vol_id %d", sv->vol_id);
1445 goto bad_vid_hdr;
1446 }
1447
1448 if (sv->compat != vidh->compat) {
1449 ubi_err("bad compat %d", vidh->compat);
1450 goto bad_vid_hdr;
1451 }
1452
3261ebd7 1453 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
801c135c
AB
1454 ubi_err("bad lnum %d", seb->lnum);
1455 goto bad_vid_hdr;
1456 }
1457
3261ebd7 1458 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
801c135c
AB
1459 ubi_err("bad used_ebs %d", sv->used_ebs);
1460 goto bad_vid_hdr;
1461 }
1462
3261ebd7 1463 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
801c135c
AB
1464 ubi_err("bad data_pad %d", sv->data_pad);
1465 goto bad_vid_hdr;
1466 }
801c135c
AB
1467 }
1468
1469 if (!last_seb)
1470 continue;
1471
3261ebd7 1472 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
801c135c
AB
1473 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1474 goto bad_vid_hdr;
1475 }
1476
3261ebd7 1477 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
801c135c
AB
1478 ubi_err("bad last_data_size %d", sv->last_data_size);
1479 goto bad_vid_hdr;
1480 }
1481 }
1482
1483 /*
1484 * Make sure that all the physical eraseblocks are in one of the lists
1485 * or trees.
1486 */
d9b0744d 1487 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
801c135c
AB
1488 if (!buf)
1489 return -ENOMEM;
1490
801c135c
AB
1491 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1492 err = ubi_io_is_bad(ubi, pnum);
341e1a0c
AB
1493 if (err < 0) {
1494 kfree(buf);
801c135c 1495 return err;
9c9ec147 1496 } else if (err)
d9b0744d 1497 buf[pnum] = 1;
801c135c
AB
1498 }
1499
1500 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1501 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
d9b0744d 1502 buf[seb->pnum] = 1;
801c135c
AB
1503
1504 list_for_each_entry(seb, &si->free, u.list)
d9b0744d 1505 buf[seb->pnum] = 1;
801c135c
AB
1506
1507 list_for_each_entry(seb, &si->corr, u.list)
d9b0744d 1508 buf[seb->pnum] = 1;
801c135c
AB
1509
1510 list_for_each_entry(seb, &si->erase, u.list)
d9b0744d 1511 buf[seb->pnum] = 1;
801c135c
AB
1512
1513 list_for_each_entry(seb, &si->alien, u.list)
d9b0744d 1514 buf[seb->pnum] = 1;
801c135c
AB
1515
1516 err = 0;
1517 for (pnum = 0; pnum < ubi->peb_count; pnum++)
d9b0744d 1518 if (!buf[pnum]) {
801c135c
AB
1519 ubi_err("PEB %d is not referred", pnum);
1520 err = 1;
1521 }
1522
1523 kfree(buf);
1524 if (err)
1525 goto out;
1526 return 0;
1527
1528bad_seb:
1529 ubi_err("bad scanning information about LEB %d", seb->lnum);
1530 ubi_dbg_dump_seb(seb, 0);
1531 ubi_dbg_dump_sv(sv);
1532 goto out;
1533
1534bad_sv:
1535 ubi_err("bad scanning information about volume %d", sv->vol_id);
1536 ubi_dbg_dump_sv(sv);
1537 goto out;
1538
1539bad_vid_hdr:
1540 ubi_err("bad scanning information about volume %d", sv->vol_id);
1541 ubi_dbg_dump_sv(sv);
1542 ubi_dbg_dump_vid_hdr(vidh);
1543
1544out:
1545 ubi_dbg_dump_stack();
adbf05e3 1546 return -EINVAL;
801c135c
AB
1547}
1548
1549#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */