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