]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mtd/ubi/scan.c
UBI: remove bogus assertion
[linux-2.6-omap-h63xx.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(struct ubi_device *ubi, struct ubi_scan_info *si);
49 #else
50 #define paranoid_check_si(ubi, si) 0
51 #endif
52
53 /* Temporary variables used during scanning */
54 static struct ubi_ec_hdr *ech;
55 static struct ubi_vid_hdr *vidh;
56
57 /**
58  * add_to_list - add physical eraseblock to a list.
59  * @si: scanning information
60  * @pnum: physical eraseblock number to add
61  * @ec: erase counter of the physical eraseblock
62  * @list: the list to add to
63  *
64  * This function adds physical eraseblock @pnum to free, erase, corrupted or
65  * alien lists. Returns zero in case of success and a negative error code in
66  * case of failure.
67  */
68 static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
69                        struct list_head *list)
70 {
71         struct ubi_scan_leb *seb;
72
73         if (list == &si->free)
74                 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
75         else if (list == &si->erase)
76                 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
77         else if (list == &si->corr)
78                 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
79         else if (list == &si->alien)
80                 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
81         else
82                 BUG();
83
84         seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
85         if (!seb)
86                 return -ENOMEM;
87
88         seb->pnum = pnum;
89         seb->ec = ec;
90         list_add_tail(&seb->u.list, list);
91         return 0;
92 }
93
94 /**
95  * commit_to_mean_value - commit intermediate results to the final mean erase
96  * counter value.
97  * @si: scanning information
98  *
99  * This is a helper function which calculates partial mean erase counter mean
100  * value and adds it to the resulting mean value. As we can work only in
101  * integer arithmetic and we want to calculate the mean value of erase counter
102  * accurately, we first sum erase counter values in @si->ec_sum variable and
103  * count these components in @si->ec_count. If this temporary @si->ec_sum is
104  * going to overflow, we calculate the partial mean value
105  * (@si->ec_sum/@si->ec_count) and add it to @si->mean_ec.
106  */
107 static void commit_to_mean_value(struct ubi_scan_info *si)
108 {
109         si->ec_sum /= si->ec_count;
110         if (si->ec_sum % si->ec_count >= si->ec_count / 2)
111                 si->mean_ec += 1;
112         si->mean_ec += si->ec_sum;
113 }
114
115 /**
116  * validate_vid_hdr - check that volume identifier header is correct and
117  * consistent.
118  * @vid_hdr: the volume identifier header to check
119  * @sv: information about the volume this logical eraseblock belongs to
120  * @pnum: physical eraseblock number the VID header came from
121  *
122  * This function checks that data stored in @vid_hdr is consistent. Returns
123  * non-zero if an inconsistency was found and zero if not.
124  *
125  * Note, UBI does sanity check of everything it reads from the flash media.
126  * Most of the checks are done in the I/O unit. Here we check that the
127  * information in the VID header is consistent to the information in other VID
128  * headers of the same volume.
129  */
130 static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
131                             const struct ubi_scan_volume *sv, int pnum)
132 {
133         int vol_type = vid_hdr->vol_type;
134         int vol_id = be32_to_cpu(vid_hdr->vol_id);
135         int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
136         int data_pad = be32_to_cpu(vid_hdr->data_pad);
137
138         if (sv->leb_count != 0) {
139                 int sv_vol_type;
140
141                 /*
142                  * This is not the first logical eraseblock belonging to this
143                  * volume. Ensure that the data in its VID header is consistent
144                  * to the data in previous logical eraseblock headers.
145                  */
146
147                 if (vol_id != sv->vol_id) {
148                         dbg_err("inconsistent vol_id");
149                         goto bad;
150                 }
151
152                 if (sv->vol_type == UBI_STATIC_VOLUME)
153                         sv_vol_type = UBI_VID_STATIC;
154                 else
155                         sv_vol_type = UBI_VID_DYNAMIC;
156
157                 if (vol_type != sv_vol_type) {
158                         dbg_err("inconsistent vol_type");
159                         goto bad;
160                 }
161
162                 if (used_ebs != sv->used_ebs) {
163                         dbg_err("inconsistent used_ebs");
164                         goto bad;
165                 }
166
167                 if (data_pad != sv->data_pad) {
168                         dbg_err("inconsistent data_pad");
169                         goto bad;
170                 }
171         }
172
173         return 0;
174
175 bad:
176         ubi_err("inconsistent VID header at PEB %d", pnum);
177         ubi_dbg_dump_vid_hdr(vid_hdr);
178         ubi_dbg_dump_sv(sv);
179         return -EINVAL;
180 }
181
182 /**
183  * add_volume - add volume to the scanning information.
184  * @si: scanning information
185  * @vol_id: ID of the volume to add
186  * @pnum: physical eraseblock number
187  * @vid_hdr: volume identifier header
188  *
189  * If the volume corresponding to the @vid_hdr logical eraseblock is already
190  * present in the scanning information, this function does nothing. Otherwise
191  * it adds corresponding volume to the scanning information. Returns a pointer
192  * to the scanning volume object in case of success and a negative error code
193  * in case of failure.
194  */
195 static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
196                                           int pnum,
197                                           const struct ubi_vid_hdr *vid_hdr)
198 {
199         struct ubi_scan_volume *sv;
200         struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
201
202         ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
203
204         /* Walk the volume RB-tree to look if this volume is already present */
205         while (*p) {
206                 parent = *p;
207                 sv = rb_entry(parent, struct ubi_scan_volume, rb);
208
209                 if (vol_id == sv->vol_id)
210                         return sv;
211
212                 if (vol_id > sv->vol_id)
213                         p = &(*p)->rb_left;
214                 else
215                         p = &(*p)->rb_right;
216         }
217
218         /* The volume is absent - add it */
219         sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
220         if (!sv)
221                 return ERR_PTR(-ENOMEM);
222
223         sv->highest_lnum = sv->leb_count = 0;
224         sv->vol_id = vol_id;
225         sv->root = RB_ROOT;
226         sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
227         sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
228         sv->compat = vid_hdr->compat;
229         sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
230                                                             : UBI_STATIC_VOLUME;
231         if (vol_id > si->highest_vol_id)
232                 si->highest_vol_id = vol_id;
233
234         rb_link_node(&sv->rb, parent, p);
235         rb_insert_color(&sv->rb, &si->volumes);
236         si->vols_found += 1;
237         dbg_bld("added volume %d", vol_id);
238         return sv;
239 }
240
241 /**
242  * compare_lebs - find out which logical eraseblock is newer.
243  * @ubi: UBI device description object
244  * @seb: first logical eraseblock to compare
245  * @pnum: physical eraseblock number of the second logical eraseblock to
246  * compare
247  * @vid_hdr: volume identifier header of the second logical eraseblock
248  *
249  * This function compares 2 copies of a LEB and informs which one is newer. In
250  * case of success this function returns a positive value, in case of failure, a
251  * negative error code is returned. The success return codes use the following
252  * bits:
253  *     o bit 0 is cleared: the first PEB (described by @seb) is newer then the
254  *       second PEB (described by @pnum and @vid_hdr);
255  *     o bit 0 is set: the second PEB is newer;
256  *     o bit 1 is cleared: no bit-flips were detected in the newer LEB;
257  *     o bit 1 is set: bit-flips were detected in the newer LEB;
258  *     o bit 2 is cleared: the older LEB is not corrupted;
259  *     o bit 2 is set: the older LEB is corrupted.
260  */
261 static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
262                         int pnum, const struct ubi_vid_hdr *vid_hdr)
263 {
264         void *buf;
265         int len, err, second_is_newer, bitflips = 0, corrupted = 0;
266         uint32_t data_crc, crc;
267         struct ubi_vid_hdr *vh = NULL;
268         unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
269
270         if (seb->sqnum == 0 && sqnum2 == 0) {
271                 long long abs, v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver);
272
273                 /*
274                  * UBI constantly increases the logical eraseblock version
275                  * number and it can overflow. Thus, we have to bear in mind
276                  * that versions that are close to %0xFFFFFFFF are less then
277                  * versions that are close to %0.
278                  *
279                  * The UBI WL unit guarantees that the number of pending tasks
280                  * is not greater then %0x7FFFFFFF. So, if the difference
281                  * between any two versions is greater or equivalent to
282                  * %0x7FFFFFFF, there was an overflow and the logical
283                  * eraseblock with lower version is actually newer then the one
284                  * with higher version.
285                  *
286                  * FIXME: but this is anyway obsolete and will be removed at
287                  * some point.
288                  */
289
290                 dbg_bld("using old crappy leb_ver stuff");
291
292                 abs = v1 - v2;
293                 if (abs < 0)
294                         abs = -abs;
295
296                 if (abs < 0x7FFFFFFF)
297                         /* Non-overflow situation */
298                         second_is_newer = (v2 > v1);
299                 else
300                         second_is_newer = (v2 < v1);
301         } else
302                 /* Obviously the LEB with lower sequence counter is older */
303                 second_is_newer = sqnum2 > seb->sqnum;
304
305         /*
306          * Now we know which copy is newer. If the copy flag of the PEB with
307          * newer version is not set, then we just return, otherwise we have to
308          * check data CRC. For the second PEB we already have the VID header,
309          * for the first one - we'll need to re-read it from flash.
310          *
311          * FIXME: this may be optimized so that we wouldn't read twice.
312          */
313
314         if (second_is_newer) {
315                 if (!vid_hdr->copy_flag) {
316                         /* It is not a copy, so it is newer */
317                         dbg_bld("second PEB %d is newer, copy_flag is unset",
318                                 pnum);
319                         return 1;
320                 }
321         } else {
322                 pnum = seb->pnum;
323
324                 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
325                 if (!vh)
326                         return -ENOMEM;
327
328                 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
329                 if (err) {
330                         if (err == UBI_IO_BITFLIPS)
331                                 bitflips = 1;
332                         else {
333                                 dbg_err("VID of PEB %d header is bad, but it "
334                                         "was OK earlier", pnum);
335                                 if (err > 0)
336                                         err = -EIO;
337
338                                 goto out_free_vidh;
339                         }
340                 }
341
342                 if (!vh->copy_flag) {
343                         /* It is not a copy, so it is newer */
344                         dbg_bld("first PEB %d is newer, copy_flag is unset",
345                                 pnum);
346                         err = bitflips << 1;
347                         goto out_free_vidh;
348                 }
349
350                 vid_hdr = vh;
351         }
352
353         /* Read the data of the copy and check the CRC */
354
355         len = be32_to_cpu(vid_hdr->data_size);
356         buf = vmalloc(len);
357         if (!buf) {
358                 err = -ENOMEM;
359                 goto out_free_vidh;
360         }
361
362         err = ubi_io_read_data(ubi, buf, pnum, 0, len);
363         if (err && err != UBI_IO_BITFLIPS)
364                 goto out_free_buf;
365
366         data_crc = be32_to_cpu(vid_hdr->data_crc);
367         crc = crc32(UBI_CRC32_INIT, buf, len);
368         if (crc != data_crc) {
369                 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
370                         pnum, crc, data_crc);
371                 corrupted = 1;
372                 bitflips = 0;
373                 second_is_newer = !second_is_newer;
374         } else {
375                 dbg_bld("PEB %d CRC is OK", pnum);
376                 bitflips = !!err;
377         }
378
379         vfree(buf);
380         ubi_free_vid_hdr(ubi, vh);
381
382         if (second_is_newer)
383                 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
384         else
385                 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
386
387         return second_is_newer | (bitflips << 1) | (corrupted << 2);
388
389 out_free_buf:
390         vfree(buf);
391 out_free_vidh:
392         ubi_free_vid_hdr(ubi, vh);
393         return err;
394 }
395
396 /**
397  * ubi_scan_add_used - add information about a physical eraseblock to the
398  * scanning information.
399  * @ubi: UBI device description object
400  * @si: scanning information
401  * @pnum: the physical eraseblock number
402  * @ec: erase counter
403  * @vid_hdr: the volume identifier header
404  * @bitflips: if bit-flips were detected when this physical eraseblock was read
405  *
406  * This function adds information about a used physical eraseblock to the
407  * 'used' tree of the corresponding volume. The function is rather complex
408  * because it has to handle cases when this is not the first physical
409  * eraseblock belonging to the same logical eraseblock, and the newer one has
410  * to be picked, while the older one has to be dropped. This function returns
411  * zero in case of success and a negative error code in case of failure.
412  */
413 int ubi_scan_add_used(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 = be32_to_cpu(vid_hdr->vol_id);
425         lnum = be32_to_cpu(vid_hdr->lnum);
426         sqnum = be64_to_cpu(vid_hdr->sqnum);
427         leb_ver = be32_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         if (si->max_sqnum < sqnum)
437                 si->max_sqnum = sqnum;
438
439         /*
440          * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
441          * if this is the first instance of this logical eraseblock or not.
442          */
443         p = &sv->root.rb_node;
444         while (*p) {
445                 int cmp_res;
446
447                 parent = *p;
448                 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
449                 if (lnum != seb->lnum) {
450                         if (lnum < seb->lnum)
451                                 p = &(*p)->rb_left;
452                         else
453                                 p = &(*p)->rb_right;
454                         continue;
455                 }
456
457                 /*
458                  * There is already a physical eraseblock describing the same
459                  * logical eraseblock present.
460                  */
461
462                 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
463                         "LEB ver %u, EC %d", seb->pnum, seb->sqnum,
464                         seb->leb_ver, seb->ec);
465
466                 /*
467                  * Make sure that the logical eraseblocks have different
468                  * versions. Otherwise the image is bad.
469                  */
470                 if (seb->leb_ver == leb_ver && leb_ver != 0) {
471                         ubi_err("two LEBs with same version %u", leb_ver);
472                         ubi_dbg_dump_seb(seb, 0);
473                         ubi_dbg_dump_vid_hdr(vid_hdr);
474                         return -EINVAL;
475                 }
476
477                 /*
478                  * Make sure that the logical eraseblocks have different
479                  * sequence numbers. Otherwise the image is bad.
480                  *
481                  * FIXME: remove 'sqnum != 0' check when leb_ver is removed.
482                  */
483                 if (seb->sqnum == sqnum && sqnum != 0) {
484                         ubi_err("two LEBs with same sequence number %llu",
485                                 sqnum);
486                         ubi_dbg_dump_seb(seb, 0);
487                         ubi_dbg_dump_vid_hdr(vid_hdr);
488                         return -EINVAL;
489                 }
490
491                 /*
492                  * Now we have to drop the older one and preserve the newer
493                  * one.
494                  */
495                 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
496                 if (cmp_res < 0)
497                         return cmp_res;
498
499                 if (cmp_res & 1) {
500                         /*
501                          * This logical eraseblock is newer then the one
502                          * found earlier.
503                          */
504                         err = validate_vid_hdr(vid_hdr, sv, pnum);
505                         if (err)
506                                 return err;
507
508                         if (cmp_res & 4)
509                                 err = add_to_list(si, seb->pnum, seb->ec,
510                                                   &si->corr);
511                         else
512                                 err = add_to_list(si, seb->pnum, seb->ec,
513                                                   &si->erase);
514                         if (err)
515                                 return err;
516
517                         seb->ec = ec;
518                         seb->pnum = pnum;
519                         seb->scrub = ((cmp_res & 2) || bitflips);
520                         seb->sqnum = sqnum;
521                         seb->leb_ver = leb_ver;
522
523                         if (sv->highest_lnum == lnum)
524                                 sv->last_data_size =
525                                         be32_to_cpu(vid_hdr->data_size);
526
527                         return 0;
528                 } else {
529                         /*
530                          * This logical eraseblock is older then the one found
531                          * previously.
532                          */
533                         if (cmp_res & 4)
534                                 return add_to_list(si, pnum, ec, &si->corr);
535                         else
536                                 return add_to_list(si, pnum, ec, &si->erase);
537                 }
538         }
539
540         /*
541          * We've met this logical eraseblock for the first time, add it to the
542          * scanning information.
543          */
544
545         err = validate_vid_hdr(vid_hdr, sv, pnum);
546         if (err)
547                 return err;
548
549         seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
550         if (!seb)
551                 return -ENOMEM;
552
553         seb->ec = ec;
554         seb->pnum = pnum;
555         seb->lnum = lnum;
556         seb->sqnum = sqnum;
557         seb->scrub = bitflips;
558         seb->leb_ver = leb_ver;
559
560         if (sv->highest_lnum <= lnum) {
561                 sv->highest_lnum = lnum;
562                 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
563         }
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(struct ubi_device *ubi, const struct ubi_scan_info *si,
668                        int pnum, int ec)
669 {
670         int err;
671         struct ubi_ec_hdr *ec_hdr;
672
673         if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
674                 /*
675                  * Erase counter overflow. Upgrade UBI and use 64-bit
676                  * erase counters internally.
677                  */
678                 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
679                 return -EINVAL;
680         }
681
682         ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
683         if (!ec_hdr)
684                 return -ENOMEM;
685
686         ec_hdr->ec = cpu_to_be64(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(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 = be64_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 = be32_to_cpu(vidh->vol_id);
856         if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOL_ID) {
857                 int lnum = be32_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, GFP_KERNEL);
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(struct ubi_device *ubi, struct ubi_scan_info *si)
1111 {
1112         int pnum, err, vols_found = 0;
1113         struct rb_node *rb1, *rb2;
1114         struct ubi_scan_volume *sv;
1115         struct ubi_scan_leb *seb, *last_seb;
1116         uint8_t *buf;
1117
1118         /*
1119          * At first, check that scanning information is OK.
1120          */
1121         ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1122                 int leb_count = 0;
1123
1124                 cond_resched();
1125
1126                 vols_found += 1;
1127
1128                 if (si->is_empty) {
1129                         ubi_err("bad is_empty flag");
1130                         goto bad_sv;
1131                 }
1132
1133                 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1134                     sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1135                     sv->data_pad < 0 || sv->last_data_size < 0) {
1136                         ubi_err("negative values");
1137                         goto bad_sv;
1138                 }
1139
1140                 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1141                     sv->vol_id < UBI_INTERNAL_VOL_START) {
1142                         ubi_err("bad vol_id");
1143                         goto bad_sv;
1144                 }
1145
1146                 if (sv->vol_id > si->highest_vol_id) {
1147                         ubi_err("highest_vol_id is %d, but vol_id %d is there",
1148                                 si->highest_vol_id, sv->vol_id);
1149                         goto out;
1150                 }
1151
1152                 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1153                     sv->vol_type != UBI_STATIC_VOLUME) {
1154                         ubi_err("bad vol_type");
1155                         goto bad_sv;
1156                 }
1157
1158                 if (sv->data_pad > ubi->leb_size / 2) {
1159                         ubi_err("bad data_pad");
1160                         goto bad_sv;
1161                 }
1162
1163                 last_seb = NULL;
1164                 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1165                         cond_resched();
1166
1167                         last_seb = seb;
1168                         leb_count += 1;
1169
1170                         if (seb->pnum < 0 || seb->ec < 0) {
1171                                 ubi_err("negative values");
1172                                 goto bad_seb;
1173                         }
1174
1175                         if (seb->ec < si->min_ec) {
1176                                 ubi_err("bad si->min_ec (%d), %d found",
1177                                         si->min_ec, seb->ec);
1178                                 goto bad_seb;
1179                         }
1180
1181                         if (seb->ec > si->max_ec) {
1182                                 ubi_err("bad si->max_ec (%d), %d found",
1183                                         si->max_ec, seb->ec);
1184                                 goto bad_seb;
1185                         }
1186
1187                         if (seb->pnum >= ubi->peb_count) {
1188                                 ubi_err("too high PEB number %d, total PEBs %d",
1189                                         seb->pnum, ubi->peb_count);
1190                                 goto bad_seb;
1191                         }
1192
1193                         if (sv->vol_type == UBI_STATIC_VOLUME) {
1194                                 if (seb->lnum >= sv->used_ebs) {
1195                                         ubi_err("bad lnum or used_ebs");
1196                                         goto bad_seb;
1197                                 }
1198                         } else {
1199                                 if (sv->used_ebs != 0) {
1200                                         ubi_err("non-zero used_ebs");
1201                                         goto bad_seb;
1202                                 }
1203                         }
1204
1205                         if (seb->lnum > sv->highest_lnum) {
1206                                 ubi_err("incorrect highest_lnum or lnum");
1207                                 goto bad_seb;
1208                         }
1209                 }
1210
1211                 if (sv->leb_count != leb_count) {
1212                         ubi_err("bad leb_count, %d objects in the tree",
1213                                 leb_count);
1214                         goto bad_sv;
1215                 }
1216
1217                 if (!last_seb)
1218                         continue;
1219
1220                 seb = last_seb;
1221
1222                 if (seb->lnum != sv->highest_lnum) {
1223                         ubi_err("bad highest_lnum");
1224                         goto bad_seb;
1225                 }
1226         }
1227
1228         if (vols_found != si->vols_found) {
1229                 ubi_err("bad si->vols_found %d, should be %d",
1230                         si->vols_found, vols_found);
1231                 goto out;
1232         }
1233
1234         /* Check that scanning information is correct */
1235         ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1236                 last_seb = NULL;
1237                 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1238                         int vol_type;
1239
1240                         cond_resched();
1241
1242                         last_seb = seb;
1243
1244                         err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1245                         if (err && err != UBI_IO_BITFLIPS) {
1246                                 ubi_err("VID header is not OK (%d)", err);
1247                                 if (err > 0)
1248                                         err = -EIO;
1249                                 return err;
1250                         }
1251
1252                         vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1253                                    UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1254                         if (sv->vol_type != vol_type) {
1255                                 ubi_err("bad vol_type");
1256                                 goto bad_vid_hdr;
1257                         }
1258
1259                         if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
1260                                 ubi_err("bad sqnum %llu", seb->sqnum);
1261                                 goto bad_vid_hdr;
1262                         }
1263
1264                         if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
1265                                 ubi_err("bad vol_id %d", sv->vol_id);
1266                                 goto bad_vid_hdr;
1267                         }
1268
1269                         if (sv->compat != vidh->compat) {
1270                                 ubi_err("bad compat %d", vidh->compat);
1271                                 goto bad_vid_hdr;
1272                         }
1273
1274                         if (seb->lnum != be32_to_cpu(vidh->lnum)) {
1275                                 ubi_err("bad lnum %d", seb->lnum);
1276                                 goto bad_vid_hdr;
1277                         }
1278
1279                         if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
1280                                 ubi_err("bad used_ebs %d", sv->used_ebs);
1281                                 goto bad_vid_hdr;
1282                         }
1283
1284                         if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
1285                                 ubi_err("bad data_pad %d", sv->data_pad);
1286                                 goto bad_vid_hdr;
1287                         }
1288
1289                         if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) {
1290                                 ubi_err("bad leb_ver %u", seb->leb_ver);
1291                                 goto bad_vid_hdr;
1292                         }
1293                 }
1294
1295                 if (!last_seb)
1296                         continue;
1297
1298                 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
1299                         ubi_err("bad highest_lnum %d", sv->highest_lnum);
1300                         goto bad_vid_hdr;
1301                 }
1302
1303                 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
1304                         ubi_err("bad last_data_size %d", sv->last_data_size);
1305                         goto bad_vid_hdr;
1306                 }
1307         }
1308
1309         /*
1310          * Make sure that all the physical eraseblocks are in one of the lists
1311          * or trees.
1312          */
1313         buf = kzalloc(ubi->peb_count, GFP_KERNEL);
1314         if (!buf)
1315                 return -ENOMEM;
1316
1317         for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1318                 err = ubi_io_is_bad(ubi, pnum);
1319                 if (err < 0) {
1320                         kfree(buf);
1321                         return err;
1322                 }
1323                 else if (err)
1324                         buf[pnum] = 1;
1325         }
1326
1327         ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1328                 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1329                         buf[seb->pnum] = 1;
1330
1331         list_for_each_entry(seb, &si->free, u.list)
1332                 buf[seb->pnum] = 1;
1333
1334         list_for_each_entry(seb, &si->corr, u.list)
1335                 buf[seb->pnum] = 1;
1336
1337         list_for_each_entry(seb, &si->erase, u.list)
1338                 buf[seb->pnum] = 1;
1339
1340         list_for_each_entry(seb, &si->alien, u.list)
1341                 buf[seb->pnum] = 1;
1342
1343         err = 0;
1344         for (pnum = 0; pnum < ubi->peb_count; pnum++)
1345                 if (!buf[pnum]) {
1346                         ubi_err("PEB %d is not referred", pnum);
1347                         err = 1;
1348                 }
1349
1350         kfree(buf);
1351         if (err)
1352                 goto out;
1353         return 0;
1354
1355 bad_seb:
1356         ubi_err("bad scanning information about LEB %d", seb->lnum);
1357         ubi_dbg_dump_seb(seb, 0);
1358         ubi_dbg_dump_sv(sv);
1359         goto out;
1360
1361 bad_sv:
1362         ubi_err("bad scanning information about volume %d", sv->vol_id);
1363         ubi_dbg_dump_sv(sv);
1364         goto out;
1365
1366 bad_vid_hdr:
1367         ubi_err("bad scanning information about volume %d", sv->vol_id);
1368         ubi_dbg_dump_sv(sv);
1369         ubi_dbg_dump_vid_hdr(vidh);
1370
1371 out:
1372         ubi_dbg_dump_stack();
1373         return 1;
1374 }
1375
1376 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */