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