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