2 * Copyright (c) International Business Machines Corp., 2006
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.
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.
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
18 * Author: Artem Bityutskiy (Битюцкий Артём)
24 * This unit is responsible for scanning the flash media, checking UBI
25 * headers and providing complete information about the UBI flash image.
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.
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.
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.
43 #include <linux/err.h>
44 #include <linux/crc32.h>
47 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
48 static int paranoid_check_si(const struct ubi_device
*ubi
,
49 struct ubi_scan_info
*si
);
51 #define paranoid_check_si(ubi, si) 0
54 /* Temporary variables used during scanning */
55 static struct ubi_ec_hdr
*ech
;
56 static struct ubi_vid_hdr
*vidh
;
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
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
69 static int add_to_list(struct ubi_scan_info
*si
, int pnum
, int ec
,
70 struct list_head
*list
)
72 struct ubi_scan_leb
*seb
;
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
);
85 seb
= kmalloc(sizeof(struct ubi_scan_leb
), GFP_KERNEL
);
91 list_add_tail(&seb
->u
.list
, list
);
96 * commit_to_mean_value - commit intermediate results to the final mean erase
98 * @si: scanning information
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.
108 static void commit_to_mean_value(struct ubi_scan_info
*si
)
110 si
->ec_sum
/= si
->ec_count
;
111 if (si
->ec_sum
% si
->ec_count
>= si
->ec_count
/ 2)
113 si
->mean_ec
+= si
->ec_sum
;
117 * validate_vid_hdr - check that volume identifier header is correct and
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
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.
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.
131 static int validate_vid_hdr(const struct ubi_vid_hdr
*vid_hdr
,
132 const struct ubi_scan_volume
*sv
, int pnum
)
134 int vol_type
= vid_hdr
->vol_type
;
135 int vol_id
= be32_to_cpu(vid_hdr
->vol_id
);
136 int used_ebs
= be32_to_cpu(vid_hdr
->used_ebs
);
137 int data_pad
= be32_to_cpu(vid_hdr
->data_pad
);
139 if (sv
->leb_count
!= 0) {
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.
148 if (vol_id
!= sv
->vol_id
) {
149 dbg_err("inconsistent vol_id");
153 if (sv
->vol_type
== UBI_STATIC_VOLUME
)
154 sv_vol_type
= UBI_VID_STATIC
;
156 sv_vol_type
= UBI_VID_DYNAMIC
;
158 if (vol_type
!= sv_vol_type
) {
159 dbg_err("inconsistent vol_type");
163 if (used_ebs
!= sv
->used_ebs
) {
164 dbg_err("inconsistent used_ebs");
168 if (data_pad
!= sv
->data_pad
) {
169 dbg_err("inconsistent data_pad");
177 ubi_err("inconsistent VID header at PEB %d", pnum
);
178 ubi_dbg_dump_vid_hdr(vid_hdr
);
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
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.
196 static struct ubi_scan_volume
*add_volume(struct ubi_scan_info
*si
, int vol_id
,
198 const struct ubi_vid_hdr
*vid_hdr
)
200 struct ubi_scan_volume
*sv
;
201 struct rb_node
**p
= &si
->volumes
.rb_node
, *parent
= NULL
;
203 ubi_assert(vol_id
== be32_to_cpu(vid_hdr
->vol_id
));
205 /* Walk the volume RB-tree to look if this volume is already present */
208 sv
= rb_entry(parent
, struct ubi_scan_volume
, rb
);
210 if (vol_id
== sv
->vol_id
)
213 if (vol_id
> sv
->vol_id
)
219 /* The volume is absent - add it */
220 sv
= kmalloc(sizeof(struct ubi_scan_volume
), GFP_KERNEL
);
222 return ERR_PTR(-ENOMEM
);
224 sv
->highest_lnum
= sv
->leb_count
= 0;
227 sv
->used_ebs
= be32_to_cpu(vid_hdr
->used_ebs
);
228 sv
->data_pad
= be32_to_cpu(vid_hdr
->data_pad
);
229 sv
->compat
= vid_hdr
->compat
;
230 sv
->vol_type
= vid_hdr
->vol_type
== UBI_VID_DYNAMIC
? UBI_DYNAMIC_VOLUME
232 if (vol_id
> si
->highest_vol_id
)
233 si
->highest_vol_id
= vol_id
;
235 rb_link_node(&sv
->rb
, parent
, p
);
236 rb_insert_color(&sv
->rb
, &si
->volumes
);
238 dbg_bld("added volume %d", vol_id
);
243 * compare_lebs - find out which logical eraseblock is newer.
244 * @ubi: UBI device description object
245 * @seb: first logical eraseblock to compare
246 * @pnum: physical eraseblock number of the second logical eraseblock to
248 * @vid_hdr: volume identifier header of the second logical eraseblock
250 * This function compares 2 copies of a LEB and informs which one is newer. In
251 * case of success this function returns a positive value, in case of failure, a
252 * negative error code is returned. The success return codes use the following
254 * o bit 0 is cleared: the first PEB (described by @seb) is newer then the
255 * second PEB (described by @pnum and @vid_hdr);
256 * o bit 0 is set: the second PEB is newer;
257 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
258 * o bit 1 is set: bit-flips were detected in the newer LEB;
259 * o bit 2 is cleared: the older LEB is not corrupted;
260 * o bit 2 is set: the older LEB is corrupted.
262 static int compare_lebs(const struct ubi_device
*ubi
,
263 const struct ubi_scan_leb
*seb
, int pnum
,
264 const struct ubi_vid_hdr
*vid_hdr
)
267 int len
, err
, second_is_newer
, bitflips
= 0, corrupted
= 0;
268 uint32_t data_crc
, crc
;
269 struct ubi_vid_hdr
*vh
= NULL
;
270 unsigned long long sqnum2
= be64_to_cpu(vid_hdr
->sqnum
);
272 if (seb
->sqnum
== 0 && sqnum2
== 0) {
273 long long abs
, v1
= seb
->leb_ver
, v2
= be32_to_cpu(vid_hdr
->leb_ver
);
276 * UBI constantly increases the logical eraseblock version
277 * number and it can overflow. Thus, we have to bear in mind
278 * that versions that are close to %0xFFFFFFFF are less then
279 * versions that are close to %0.
281 * The UBI WL unit guarantees that the number of pending tasks
282 * is not greater then %0x7FFFFFFF. So, if the difference
283 * between any two versions is greater or equivalent to
284 * %0x7FFFFFFF, there was an overflow and the logical
285 * eraseblock with lower version is actually newer then the one
286 * with higher version.
288 * FIXME: but this is anyway obsolete and will be removed at
292 dbg_bld("using old crappy leb_ver stuff");
298 if (abs
< 0x7FFFFFFF)
299 /* Non-overflow situation */
300 second_is_newer
= (v2
> v1
);
302 second_is_newer
= (v2
< v1
);
304 /* Obviously the LEB with lower sequence counter is older */
305 second_is_newer
= sqnum2
> seb
->sqnum
;
308 * Now we know which copy is newer. If the copy flag of the PEB with
309 * newer version is not set, then we just return, otherwise we have to
310 * check data CRC. For the second PEB we already have the VID header,
311 * for the first one - we'll need to re-read it from flash.
313 * FIXME: this may be optimized so that we wouldn't read twice.
316 if (second_is_newer
) {
317 if (!vid_hdr
->copy_flag
) {
318 /* It is not a copy, so it is newer */
319 dbg_bld("second PEB %d is newer, copy_flag is unset",
326 vh
= ubi_zalloc_vid_hdr(ubi
);
330 err
= ubi_io_read_vid_hdr(ubi
, pnum
, vh
, 0);
332 if (err
== UBI_IO_BITFLIPS
)
335 dbg_err("VID of PEB %d header is bad, but it "
336 "was OK earlier", pnum
);
344 if (!vh
->copy_flag
) {
345 /* It is not a copy, so it is newer */
346 dbg_bld("first PEB %d is newer, copy_flag is unset",
355 /* Read the data of the copy and check the CRC */
357 len
= be32_to_cpu(vid_hdr
->data_size
);
364 err
= ubi_io_read_data(ubi
, buf
, pnum
, 0, len
);
365 if (err
&& err
!= UBI_IO_BITFLIPS
)
368 data_crc
= be32_to_cpu(vid_hdr
->data_crc
);
369 crc
= crc32(UBI_CRC32_INIT
, buf
, len
);
370 if (crc
!= data_crc
) {
371 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
372 pnum
, crc
, data_crc
);
375 second_is_newer
= !second_is_newer
;
377 dbg_bld("PEB %d CRC is OK", pnum
);
382 ubi_free_vid_hdr(ubi
, vh
);
385 dbg_bld("second PEB %d is newer, copy_flag is set", pnum
);
387 dbg_bld("first PEB %d is newer, copy_flag is set", pnum
);
389 return second_is_newer
| (bitflips
<< 1) | (corrupted
<< 2);
394 ubi_free_vid_hdr(ubi
, vh
);
400 * ubi_scan_add_used - add information about a physical eraseblock to the
401 * scanning information.
402 * @ubi: UBI device description object
403 * @si: scanning information
404 * @pnum: the physical eraseblock number
406 * @vid_hdr: the volume identifier header
407 * @bitflips: if bit-flips were detected when this physical eraseblock was read
409 * This function adds information about a used physical eraseblock to the
410 * 'used' tree of the corresponding volume. The function is rather complex
411 * because it has to handle cases when this is not the first physical
412 * eraseblock belonging to the same logical eraseblock, and the newer one has
413 * to be picked, while the older one has to be dropped. This function returns
414 * zero in case of success and a negative error code in case of failure.
416 int ubi_scan_add_used(const struct ubi_device
*ubi
, struct ubi_scan_info
*si
,
417 int pnum
, int ec
, const struct ubi_vid_hdr
*vid_hdr
,
420 int err
, vol_id
, lnum
;
422 unsigned long long sqnum
;
423 struct ubi_scan_volume
*sv
;
424 struct ubi_scan_leb
*seb
;
425 struct rb_node
**p
, *parent
= NULL
;
427 vol_id
= be32_to_cpu(vid_hdr
->vol_id
);
428 lnum
= be32_to_cpu(vid_hdr
->lnum
);
429 sqnum
= be64_to_cpu(vid_hdr
->sqnum
);
430 leb_ver
= be32_to_cpu(vid_hdr
->leb_ver
);
432 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d",
433 pnum
, vol_id
, lnum
, ec
, sqnum
, leb_ver
, bitflips
);
435 sv
= add_volume(si
, vol_id
, pnum
, vid_hdr
);
439 if (si
->max_sqnum
< sqnum
)
440 si
->max_sqnum
= sqnum
;
443 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
444 * if this is the first instance of this logical eraseblock or not.
446 p
= &sv
->root
.rb_node
;
451 seb
= rb_entry(parent
, struct ubi_scan_leb
, u
.rb
);
452 if (lnum
!= seb
->lnum
) {
453 if (lnum
< seb
->lnum
)
461 * There is already a physical eraseblock describing the same
462 * logical eraseblock present.
465 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
466 "LEB ver %u, EC %d", seb
->pnum
, seb
->sqnum
,
467 seb
->leb_ver
, seb
->ec
);
470 * Make sure that the logical eraseblocks have different
471 * versions. Otherwise the image is bad.
473 if (seb
->leb_ver
== leb_ver
&& leb_ver
!= 0) {
474 ubi_err("two LEBs with same version %u", leb_ver
);
475 ubi_dbg_dump_seb(seb
, 0);
476 ubi_dbg_dump_vid_hdr(vid_hdr
);
481 * Make sure that the logical eraseblocks have different
482 * sequence numbers. Otherwise the image is bad.
484 * FIXME: remove 'sqnum != 0' check when leb_ver is removed.
486 if (seb
->sqnum
== sqnum
&& sqnum
!= 0) {
487 ubi_err("two LEBs with same sequence number %llu",
489 ubi_dbg_dump_seb(seb
, 0);
490 ubi_dbg_dump_vid_hdr(vid_hdr
);
495 * Now we have to drop the older one and preserve the newer
498 cmp_res
= compare_lebs(ubi
, seb
, pnum
, vid_hdr
);
504 * This logical eraseblock is newer then the one
507 err
= validate_vid_hdr(vid_hdr
, sv
, pnum
);
512 err
= add_to_list(si
, seb
->pnum
, seb
->ec
,
515 err
= add_to_list(si
, seb
->pnum
, seb
->ec
,
522 seb
->scrub
= ((cmp_res
& 2) || bitflips
);
524 seb
->leb_ver
= leb_ver
;
526 if (sv
->highest_lnum
== lnum
)
528 be32_to_cpu(vid_hdr
->data_size
);
533 * This logical eraseblock is older then the one found
537 return add_to_list(si
, pnum
, ec
, &si
->corr
);
539 return add_to_list(si
, pnum
, ec
, &si
->erase
);
544 * We've met this logical eraseblock for the first time, add it to the
545 * scanning information.
548 err
= validate_vid_hdr(vid_hdr
, sv
, pnum
);
552 seb
= kmalloc(sizeof(struct ubi_scan_leb
), GFP_KERNEL
);
560 seb
->scrub
= bitflips
;
561 seb
->leb_ver
= leb_ver
;
563 if (sv
->highest_lnum
<= lnum
) {
564 sv
->highest_lnum
= lnum
;
565 sv
->last_data_size
= be32_to_cpu(vid_hdr
->data_size
);
569 rb_link_node(&seb
->u
.rb
, parent
, p
);
570 rb_insert_color(&seb
->u
.rb
, &sv
->root
);
575 * ubi_scan_find_sv - find information about a particular volume in the
576 * scanning information.
577 * @si: scanning information
578 * @vol_id: the requested volume ID
580 * This function returns a pointer to the volume description or %NULL if there
581 * are no data about this volume in the scanning information.
583 struct ubi_scan_volume
*ubi_scan_find_sv(const struct ubi_scan_info
*si
,
586 struct ubi_scan_volume
*sv
;
587 struct rb_node
*p
= si
->volumes
.rb_node
;
590 sv
= rb_entry(p
, struct ubi_scan_volume
, rb
);
592 if (vol_id
== sv
->vol_id
)
595 if (vol_id
> sv
->vol_id
)
605 * ubi_scan_find_seb - find information about a particular logical
606 * eraseblock in the volume scanning information.
607 * @sv: a pointer to the volume scanning information
608 * @lnum: the requested logical eraseblock
610 * This function returns a pointer to the scanning logical eraseblock or %NULL
611 * if there are no data about it in the scanning volume information.
613 struct ubi_scan_leb
*ubi_scan_find_seb(const struct ubi_scan_volume
*sv
,
616 struct ubi_scan_leb
*seb
;
617 struct rb_node
*p
= sv
->root
.rb_node
;
620 seb
= rb_entry(p
, struct ubi_scan_leb
, u
.rb
);
622 if (lnum
== seb
->lnum
)
625 if (lnum
> seb
->lnum
)
635 * ubi_scan_rm_volume - delete scanning information about a volume.
636 * @si: scanning information
637 * @sv: the volume scanning information to delete
639 void ubi_scan_rm_volume(struct ubi_scan_info
*si
, struct ubi_scan_volume
*sv
)
642 struct ubi_scan_leb
*seb
;
644 dbg_bld("remove scanning information about volume %d", sv
->vol_id
);
646 while ((rb
= rb_first(&sv
->root
))) {
647 seb
= rb_entry(rb
, struct ubi_scan_leb
, u
.rb
);
648 rb_erase(&seb
->u
.rb
, &sv
->root
);
649 list_add_tail(&seb
->u
.list
, &si
->erase
);
652 rb_erase(&sv
->rb
, &si
->volumes
);
658 * ubi_scan_erase_peb - erase a physical eraseblock.
659 * @ubi: UBI device description object
660 * @si: scanning information
661 * @pnum: physical eraseblock number to erase;
662 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
664 * This function erases physical eraseblock 'pnum', and writes the erase
665 * counter header to it. This function should only be used on UBI device
666 * initialization stages, when the EBA unit had not been yet initialized. This
667 * function returns zero in case of success and a negative error code in case
670 int ubi_scan_erase_peb(const struct ubi_device
*ubi
,
671 const struct ubi_scan_info
*si
, int pnum
, int ec
)
674 struct ubi_ec_hdr
*ec_hdr
;
676 if ((long long)ec
>= UBI_MAX_ERASECOUNTER
) {
678 * Erase counter overflow. Upgrade UBI and use 64-bit
679 * erase counters internally.
681 ubi_err("erase counter overflow at PEB %d, EC %d", pnum
, ec
);
685 ec_hdr
= kzalloc(ubi
->ec_hdr_alsize
, GFP_KERNEL
);
689 ec_hdr
->ec
= cpu_to_be64(ec
);
691 err
= ubi_io_sync_erase(ubi
, pnum
, 0);
695 err
= ubi_io_write_ec_hdr(ubi
, pnum
, ec_hdr
);
703 * ubi_scan_get_free_peb - get a free physical eraseblock.
704 * @ubi: UBI device description object
705 * @si: scanning information
707 * This function returns a free physical eraseblock. It is supposed to be
708 * called on the UBI initialization stages when the wear-leveling unit is not
709 * initialized yet. This function picks a physical eraseblocks from one of the
710 * lists, writes the EC header if it is needed, and removes it from the list.
712 * This function returns scanning physical eraseblock information in case of
713 * success and an error code in case of failure.
715 struct ubi_scan_leb
*ubi_scan_get_free_peb(const struct ubi_device
*ubi
,
716 struct ubi_scan_info
*si
)
719 struct ubi_scan_leb
*seb
;
721 if (!list_empty(&si
->free
)) {
722 seb
= list_entry(si
->free
.next
, struct ubi_scan_leb
, u
.list
);
723 list_del(&seb
->u
.list
);
724 dbg_bld("return free PEB %d, EC %d", seb
->pnum
, seb
->ec
);
728 for (i
= 0; i
< 2; i
++) {
729 struct list_head
*head
;
730 struct ubi_scan_leb
*tmp_seb
;
738 * We try to erase the first physical eraseblock from the @head
739 * list and pick it if we succeed, or try to erase the
740 * next one if not. And so forth. We don't want to take care
741 * about bad eraseblocks here - they'll be handled later.
743 list_for_each_entry_safe(seb
, tmp_seb
, head
, u
.list
) {
744 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
745 seb
->ec
= si
->mean_ec
;
747 err
= ubi_scan_erase_peb(ubi
, si
, seb
->pnum
, seb
->ec
+1);
752 list_del(&seb
->u
.list
);
753 dbg_bld("return PEB %d, EC %d", seb
->pnum
, seb
->ec
);
758 ubi_err("no eraseblocks found");
759 return ERR_PTR(-ENOSPC
);
763 * process_eb - read UBI headers, check them and add corresponding data
764 * to the scanning information.
765 * @ubi: UBI device description object
766 * @si: scanning information
767 * @pnum: the physical eraseblock number
769 * This function returns a zero if the physical eraseblock was successfully
770 * handled and a negative error code in case of failure.
772 static int process_eb(struct ubi_device
*ubi
, struct ubi_scan_info
*si
, int pnum
)
775 int err
, bitflips
= 0, vol_id
, ec_corr
= 0;
777 dbg_bld("scan PEB %d", pnum
);
779 /* Skip bad physical eraseblocks */
780 err
= ubi_io_is_bad(ubi
, pnum
);
785 * FIXME: this is actually duty of the I/O unit to initialize
786 * this, but MTD does not provide enough information.
788 si
->bad_peb_count
+= 1;
792 err
= ubi_io_read_ec_hdr(ubi
, pnum
, ech
, 0);
795 else if (err
== UBI_IO_BITFLIPS
)
797 else if (err
== UBI_IO_PEB_EMPTY
)
798 return add_to_list(si
, pnum
, UBI_SCAN_UNKNOWN_EC
, &si
->erase
);
799 else if (err
== UBI_IO_BAD_EC_HDR
) {
801 * We have to also look at the VID header, possibly it is not
802 * corrupted. Set %bitflips flag in order to make this PEB be
803 * moved and EC be re-created.
806 ec
= UBI_SCAN_UNKNOWN_EC
;
813 /* Make sure UBI version is OK */
814 if (ech
->version
!= UBI_VERSION
) {
815 ubi_err("this UBI version is %d, image version is %d",
816 UBI_VERSION
, (int)ech
->version
);
820 ec
= be64_to_cpu(ech
->ec
);
821 if (ec
> UBI_MAX_ERASECOUNTER
) {
823 * Erase counter overflow. The EC headers have 64 bits
824 * reserved, but we anyway make use of only 31 bit
825 * values, as this seems to be enough for any existing
826 * flash. Upgrade UBI and use 64-bit erase counters
829 ubi_err("erase counter overflow, max is %d",
830 UBI_MAX_ERASECOUNTER
);
831 ubi_dbg_dump_ec_hdr(ech
);
836 /* OK, we've done with the EC header, let's look at the VID header */
838 err
= ubi_io_read_vid_hdr(ubi
, pnum
, vidh
, 0);
841 else if (err
== UBI_IO_BITFLIPS
)
843 else if (err
== UBI_IO_BAD_VID_HDR
||
844 (err
== UBI_IO_PEB_FREE
&& ec_corr
)) {
845 /* VID header is corrupted */
846 err
= add_to_list(si
, pnum
, ec
, &si
->corr
);
850 } else if (err
== UBI_IO_PEB_FREE
) {
851 /* No VID header - the physical eraseblock is free */
852 err
= add_to_list(si
, pnum
, ec
, &si
->free
);
858 vol_id
= be32_to_cpu(vidh
->vol_id
);
859 if (vol_id
> UBI_MAX_VOLUMES
&& vol_id
!= UBI_LAYOUT_VOL_ID
) {
860 int lnum
= be32_to_cpu(vidh
->lnum
);
862 /* Unsupported internal volume */
863 switch (vidh
->compat
) {
864 case UBI_COMPAT_DELETE
:
865 ubi_msg("\"delete\" compatible internal volume %d:%d"
866 " found, remove it", vol_id
, lnum
);
867 err
= add_to_list(si
, pnum
, ec
, &si
->corr
);
873 ubi_msg("read-only compatible internal volume %d:%d"
874 " found, switch to read-only mode",
879 case UBI_COMPAT_PRESERVE
:
880 ubi_msg("\"preserve\" compatible internal volume %d:%d"
881 " found", vol_id
, lnum
);
882 err
= add_to_list(si
, pnum
, ec
, &si
->alien
);
885 si
->alien_peb_count
+= 1;
888 case UBI_COMPAT_REJECT
:
889 ubi_err("incompatible internal volume %d:%d found",
895 /* Both UBI headers seem to be fine */
896 err
= ubi_scan_add_used(ubi
, si
, pnum
, ec
, vidh
, bitflips
);
902 if (si
->ec_sum
+ ec
< ec
) {
903 commit_to_mean_value(si
);
921 * ubi_scan - scan an MTD device.
922 * @ubi: UBI device description object
924 * This function does full scanning of an MTD device and returns complete
925 * information about it. In case of failure, an error code is returned.
927 struct ubi_scan_info
*ubi_scan(struct ubi_device
*ubi
)
930 struct rb_node
*rb1
, *rb2
;
931 struct ubi_scan_volume
*sv
;
932 struct ubi_scan_leb
*seb
;
933 struct ubi_scan_info
*si
;
935 si
= kzalloc(sizeof(struct ubi_scan_info
), GFP_KERNEL
);
937 return ERR_PTR(-ENOMEM
);
939 INIT_LIST_HEAD(&si
->corr
);
940 INIT_LIST_HEAD(&si
->free
);
941 INIT_LIST_HEAD(&si
->erase
);
942 INIT_LIST_HEAD(&si
->alien
);
943 si
->volumes
= RB_ROOT
;
947 ech
= kzalloc(ubi
->ec_hdr_alsize
, GFP_KERNEL
);
951 vidh
= ubi_zalloc_vid_hdr(ubi
);
955 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++) {
958 dbg_msg("process PEB %d", pnum
);
959 err
= process_eb(ubi
, si
, pnum
);
964 dbg_msg("scanning is finished");
966 /* Finish mean erase counter calculations */
968 commit_to_mean_value(si
);
971 ubi_msg("empty MTD device detected");
974 * In case of unknown erase counter we use the mean erase counter
977 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
978 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
)
979 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
980 seb
->ec
= si
->mean_ec
;
983 list_for_each_entry(seb
, &si
->free
, u
.list
) {
984 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
985 seb
->ec
= si
->mean_ec
;
988 list_for_each_entry(seb
, &si
->corr
, u
.list
)
989 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
990 seb
->ec
= si
->mean_ec
;
992 list_for_each_entry(seb
, &si
->erase
, u
.list
)
993 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
994 seb
->ec
= si
->mean_ec
;
996 err
= paranoid_check_si(ubi
, si
);
1003 ubi_free_vid_hdr(ubi
, vidh
);
1009 ubi_free_vid_hdr(ubi
, vidh
);
1013 ubi_scan_destroy_si(si
);
1014 return ERR_PTR(err
);
1018 * destroy_sv - free the scanning volume information
1019 * @sv: scanning volume information
1021 * This function destroys the volume RB-tree (@sv->root) and the scanning
1022 * volume information.
1024 static void destroy_sv(struct ubi_scan_volume
*sv
)
1026 struct ubi_scan_leb
*seb
;
1027 struct rb_node
*this = sv
->root
.rb_node
;
1031 this = this->rb_left
;
1032 else if (this->rb_right
)
1033 this = this->rb_right
;
1035 seb
= rb_entry(this, struct ubi_scan_leb
, u
.rb
);
1036 this = rb_parent(this);
1038 if (this->rb_left
== &seb
->u
.rb
)
1039 this->rb_left
= NULL
;
1041 this->rb_right
= NULL
;
1051 * ubi_scan_destroy_si - destroy scanning information.
1052 * @si: scanning information
1054 void ubi_scan_destroy_si(struct ubi_scan_info
*si
)
1056 struct ubi_scan_leb
*seb
, *seb_tmp
;
1057 struct ubi_scan_volume
*sv
;
1060 list_for_each_entry_safe(seb
, seb_tmp
, &si
->alien
, u
.list
) {
1061 list_del(&seb
->u
.list
);
1064 list_for_each_entry_safe(seb
, seb_tmp
, &si
->erase
, u
.list
) {
1065 list_del(&seb
->u
.list
);
1068 list_for_each_entry_safe(seb
, seb_tmp
, &si
->corr
, u
.list
) {
1069 list_del(&seb
->u
.list
);
1072 list_for_each_entry_safe(seb
, seb_tmp
, &si
->free
, u
.list
) {
1073 list_del(&seb
->u
.list
);
1077 /* Destroy the volume RB-tree */
1078 rb
= si
->volumes
.rb_node
;
1082 else if (rb
->rb_right
)
1085 sv
= rb_entry(rb
, struct ubi_scan_volume
, rb
);
1089 if (rb
->rb_left
== &sv
->rb
)
1092 rb
->rb_right
= NULL
;
1102 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1105 * paranoid_check_si - check if the scanning information is correct and
1107 * @ubi: UBI device description object
1108 * @si: scanning information
1110 * This function returns zero if the scanning information is all right, %1 if
1111 * not and a negative error code if an error occurred.
1113 static int paranoid_check_si(const struct ubi_device
*ubi
,
1114 struct ubi_scan_info
*si
)
1116 int pnum
, err
, vols_found
= 0;
1117 struct rb_node
*rb1
, *rb2
;
1118 struct ubi_scan_volume
*sv
;
1119 struct ubi_scan_leb
*seb
, *last_seb
;
1123 * At first, check that scanning information is OK.
1125 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
1133 ubi_err("bad is_empty flag");
1137 if (sv
->vol_id
< 0 || sv
->highest_lnum
< 0 ||
1138 sv
->leb_count
< 0 || sv
->vol_type
< 0 || sv
->used_ebs
< 0 ||
1139 sv
->data_pad
< 0 || sv
->last_data_size
< 0) {
1140 ubi_err("negative values");
1144 if (sv
->vol_id
>= UBI_MAX_VOLUMES
&&
1145 sv
->vol_id
< UBI_INTERNAL_VOL_START
) {
1146 ubi_err("bad vol_id");
1150 if (sv
->vol_id
> si
->highest_vol_id
) {
1151 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1152 si
->highest_vol_id
, sv
->vol_id
);
1156 if (sv
->vol_type
!= UBI_DYNAMIC_VOLUME
&&
1157 sv
->vol_type
!= UBI_STATIC_VOLUME
) {
1158 ubi_err("bad vol_type");
1162 if (sv
->data_pad
> ubi
->leb_size
/ 2) {
1163 ubi_err("bad data_pad");
1168 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
) {
1174 if (seb
->pnum
< 0 || seb
->ec
< 0) {
1175 ubi_err("negative values");
1179 if (seb
->ec
< si
->min_ec
) {
1180 ubi_err("bad si->min_ec (%d), %d found",
1181 si
->min_ec
, seb
->ec
);
1185 if (seb
->ec
> si
->max_ec
) {
1186 ubi_err("bad si->max_ec (%d), %d found",
1187 si
->max_ec
, seb
->ec
);
1191 if (seb
->pnum
>= ubi
->peb_count
) {
1192 ubi_err("too high PEB number %d, total PEBs %d",
1193 seb
->pnum
, ubi
->peb_count
);
1197 if (sv
->vol_type
== UBI_STATIC_VOLUME
) {
1198 if (seb
->lnum
>= sv
->used_ebs
) {
1199 ubi_err("bad lnum or used_ebs");
1203 if (sv
->used_ebs
!= 0) {
1204 ubi_err("non-zero used_ebs");
1209 if (seb
->lnum
> sv
->highest_lnum
) {
1210 ubi_err("incorrect highest_lnum or lnum");
1215 if (sv
->leb_count
!= leb_count
) {
1216 ubi_err("bad leb_count, %d objects in the tree",
1226 if (seb
->lnum
!= sv
->highest_lnum
) {
1227 ubi_err("bad highest_lnum");
1232 if (vols_found
!= si
->vols_found
) {
1233 ubi_err("bad si->vols_found %d, should be %d",
1234 si
->vols_found
, vols_found
);
1238 /* Check that scanning information is correct */
1239 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
1241 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
) {
1248 err
= ubi_io_read_vid_hdr(ubi
, seb
->pnum
, vidh
, 1);
1249 if (err
&& err
!= UBI_IO_BITFLIPS
) {
1250 ubi_err("VID header is not OK (%d)", err
);
1256 vol_type
= vidh
->vol_type
== UBI_VID_DYNAMIC
?
1257 UBI_DYNAMIC_VOLUME
: UBI_STATIC_VOLUME
;
1258 if (sv
->vol_type
!= vol_type
) {
1259 ubi_err("bad vol_type");
1263 if (seb
->sqnum
!= be64_to_cpu(vidh
->sqnum
)) {
1264 ubi_err("bad sqnum %llu", seb
->sqnum
);
1268 if (sv
->vol_id
!= be32_to_cpu(vidh
->vol_id
)) {
1269 ubi_err("bad vol_id %d", sv
->vol_id
);
1273 if (sv
->compat
!= vidh
->compat
) {
1274 ubi_err("bad compat %d", vidh
->compat
);
1278 if (seb
->lnum
!= be32_to_cpu(vidh
->lnum
)) {
1279 ubi_err("bad lnum %d", seb
->lnum
);
1283 if (sv
->used_ebs
!= be32_to_cpu(vidh
->used_ebs
)) {
1284 ubi_err("bad used_ebs %d", sv
->used_ebs
);
1288 if (sv
->data_pad
!= be32_to_cpu(vidh
->data_pad
)) {
1289 ubi_err("bad data_pad %d", sv
->data_pad
);
1293 if (seb
->leb_ver
!= be32_to_cpu(vidh
->leb_ver
)) {
1294 ubi_err("bad leb_ver %u", seb
->leb_ver
);
1302 if (sv
->highest_lnum
!= be32_to_cpu(vidh
->lnum
)) {
1303 ubi_err("bad highest_lnum %d", sv
->highest_lnum
);
1307 if (sv
->last_data_size
!= be32_to_cpu(vidh
->data_size
)) {
1308 ubi_err("bad last_data_size %d", sv
->last_data_size
);
1314 * Make sure that all the physical eraseblocks are in one of the lists
1317 buf
= kmalloc(ubi
->peb_count
, GFP_KERNEL
);
1321 memset(buf
, 1, ubi
->peb_count
);
1322 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++) {
1323 err
= ubi_io_is_bad(ubi
, pnum
);
1332 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
)
1333 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
)
1336 list_for_each_entry(seb
, &si
->free
, u
.list
)
1339 list_for_each_entry(seb
, &si
->corr
, u
.list
)
1342 list_for_each_entry(seb
, &si
->erase
, u
.list
)
1345 list_for_each_entry(seb
, &si
->alien
, u
.list
)
1349 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++)
1351 ubi_err("PEB %d is not referred", pnum
);
1361 ubi_err("bad scanning information about LEB %d", seb
->lnum
);
1362 ubi_dbg_dump_seb(seb
, 0);
1363 ubi_dbg_dump_sv(sv
);
1367 ubi_err("bad scanning information about volume %d", sv
->vol_id
);
1368 ubi_dbg_dump_sv(sv
);
1372 ubi_err("bad scanning information about volume %d", sv
->vol_id
);
1373 ubi_dbg_dump_sv(sv
);
1374 ubi_dbg_dump_vid_hdr(vidh
);
1377 ubi_dbg_dump_stack();
1381 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */