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