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 (Битюцкий Артём)
22 * UBI scanning sub-system.
24 * This sub-system 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 * Scanned logical eraseblocks are represented by &struct ubi_scan_leb objects.
33 * These objects are kept in per-volume RB-trees with the root at the
34 * corresponding &struct ubi_scan_volume object. To put it differently, we keep
35 * an RB-tree of per-volume objects and each of these objects is the root of
36 * RB-tree of per-eraseblock objects.
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.
42 * UBI tries to distinguish between 2 types of corruptions.
43 * 1. Corruptions caused by power cuts. These are harmless and expected
44 * corruptions and UBI tries to handle them gracefully, without printing too
45 * many warnings and error messages. The idea is that we do not lose
46 * important data in these case - we may lose only the data which was being
47 * written to the media just before the power cut happened, and the upper
48 * layers (e.g., UBIFS) are supposed to handle these situations. UBI puts
49 * these PEBs to the head of the @erase list and they are scheduled for
52 * 2. Unexpected corruptions which are not caused by power cuts. During
53 * scanning, such PEBs are put to the @corr list and UBI preserves them.
54 * Obviously, this lessens the amount of available PEBs, and if at some
55 * point UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly
56 * informs about such PEBs every time the MTD device is attached.
58 * However, it is difficult to reliably distinguish between these types of
59 * corruptions and UBI's strategy is as follows. UBI assumes (2.) if the VID
60 * header is corrupted and the data area does not contain all 0xFFs, and there
61 * were not bit-flips or integrity errors while reading the data area. Otherwise
62 * UBI assumes (1.). The assumptions are:
63 * o if the data area contains only 0xFFs, there is no data, and it is safe
64 * to just erase this PEB.
65 * o if the data area has bit-flips and data integrity errors (ECC errors on
66 * NAND), it is probably a PEB which was being erased when power cut
70 #include <linux/err.h>
71 #include <linux/slab.h>
72 #include <linux/crc32.h>
73 #include <linux/math64.h>
74 #include <linux/random.h>
77 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
78 static int paranoid_check_si(struct ubi_device
*ubi
, struct ubi_scan_info
*si
);
80 #define paranoid_check_si(ubi, si) 0
83 /* Temporary variables used during scanning */
84 static struct ubi_ec_hdr
*ech
;
85 static struct ubi_vid_hdr
*vidh
;
88 * add_to_list - add physical eraseblock to a list.
89 * @si: scanning information
90 * @pnum: physical eraseblock number to add
91 * @ec: erase counter of the physical eraseblock
92 * @to_head: if not zero, add to the head of the list
93 * @list: the list to add to
95 * This function adds physical eraseblock @pnum to free, erase, or alien lists.
96 * If @to_head is not zero, PEB will be added to the head of the list, which
97 * basically means it will be processed first later. E.g., we add corrupted
98 * PEBs (corrupted due to power cuts) to the head of the erase list to make
99 * sure we erase them first and get rid of corruptions ASAP. This function
100 * returns zero in case of success and a negative error code in case of
103 static int add_to_list(struct ubi_scan_info
*si
, int pnum
, int ec
, int to_head
,
104 struct list_head
*list
)
106 struct ubi_scan_leb
*seb
;
108 if (list
== &si
->free
) {
109 dbg_bld("add to free: PEB %d, EC %d", pnum
, ec
);
110 } else if (list
== &si
->erase
) {
111 dbg_bld("add to erase: PEB %d, EC %d", pnum
, ec
);
112 } else if (list
== &si
->alien
) {
113 dbg_bld("add to alien: PEB %d, EC %d", pnum
, ec
);
114 si
->alien_peb_count
+= 1;
118 seb
= kmalloc(sizeof(struct ubi_scan_leb
), GFP_KERNEL
);
125 list_add(&seb
->u
.list
, list
);
127 list_add_tail(&seb
->u
.list
, list
);
132 * add_corrupted - add a corrupted physical eraseblock.
133 * @si: scanning information
134 * @pnum: physical eraseblock number to add
135 * @ec: erase counter of the physical eraseblock
137 * This function adds corrupted physical eraseblock @pnum to the 'corr' list.
138 * The corruption was presumably not caused by a power cut. Returns zero in
139 * case of success and a negative error code in case of failure.
141 static int add_corrupted(struct ubi_scan_info
*si
, int pnum
, int ec
)
143 struct ubi_scan_leb
*seb
;
145 dbg_bld("add to corrupted: PEB %d, EC %d", pnum
, ec
);
147 seb
= kmalloc(sizeof(struct ubi_scan_leb
), GFP_KERNEL
);
151 si
->corr_peb_count
+= 1;
154 list_add(&seb
->u
.list
, &si
->corr
);
159 * validate_vid_hdr - check volume identifier header.
160 * @vid_hdr: the volume identifier header to check
161 * @sv: information about the volume this logical eraseblock belongs to
162 * @pnum: physical eraseblock number the VID header came from
164 * This function checks that data stored in @vid_hdr is consistent. Returns
165 * non-zero if an inconsistency was found and zero if not.
167 * Note, UBI does sanity check of everything it reads from the flash media.
168 * Most of the checks are done in the I/O sub-system. Here we check that the
169 * information in the VID header is consistent to the information in other VID
170 * headers of the same volume.
172 static int validate_vid_hdr(const struct ubi_vid_hdr
*vid_hdr
,
173 const struct ubi_scan_volume
*sv
, int pnum
)
175 int vol_type
= vid_hdr
->vol_type
;
176 int vol_id
= be32_to_cpu(vid_hdr
->vol_id
);
177 int used_ebs
= be32_to_cpu(vid_hdr
->used_ebs
);
178 int data_pad
= be32_to_cpu(vid_hdr
->data_pad
);
180 if (sv
->leb_count
!= 0) {
184 * This is not the first logical eraseblock belonging to this
185 * volume. Ensure that the data in its VID header is consistent
186 * to the data in previous logical eraseblock headers.
189 if (vol_id
!= sv
->vol_id
) {
190 dbg_err("inconsistent vol_id");
194 if (sv
->vol_type
== UBI_STATIC_VOLUME
)
195 sv_vol_type
= UBI_VID_STATIC
;
197 sv_vol_type
= UBI_VID_DYNAMIC
;
199 if (vol_type
!= sv_vol_type
) {
200 dbg_err("inconsistent vol_type");
204 if (used_ebs
!= sv
->used_ebs
) {
205 dbg_err("inconsistent used_ebs");
209 if (data_pad
!= sv
->data_pad
) {
210 dbg_err("inconsistent data_pad");
218 ubi_err("inconsistent VID header at PEB %d", pnum
);
219 ubi_dbg_dump_vid_hdr(vid_hdr
);
225 * add_volume - add volume to the scanning information.
226 * @si: scanning information
227 * @vol_id: ID of the volume to add
228 * @pnum: physical eraseblock number
229 * @vid_hdr: volume identifier header
231 * If the volume corresponding to the @vid_hdr logical eraseblock is already
232 * present in the scanning information, this function does nothing. Otherwise
233 * it adds corresponding volume to the scanning information. Returns a pointer
234 * to the scanning volume object in case of success and a negative error code
235 * in case of failure.
237 static struct ubi_scan_volume
*add_volume(struct ubi_scan_info
*si
, int vol_id
,
239 const struct ubi_vid_hdr
*vid_hdr
)
241 struct ubi_scan_volume
*sv
;
242 struct rb_node
**p
= &si
->volumes
.rb_node
, *parent
= NULL
;
244 ubi_assert(vol_id
== be32_to_cpu(vid_hdr
->vol_id
));
246 /* Walk the volume RB-tree to look if this volume is already present */
249 sv
= rb_entry(parent
, struct ubi_scan_volume
, rb
);
251 if (vol_id
== sv
->vol_id
)
254 if (vol_id
> sv
->vol_id
)
260 /* The volume is absent - add it */
261 sv
= kmalloc(sizeof(struct ubi_scan_volume
), GFP_KERNEL
);
263 return ERR_PTR(-ENOMEM
);
265 sv
->highest_lnum
= sv
->leb_count
= 0;
268 sv
->used_ebs
= be32_to_cpu(vid_hdr
->used_ebs
);
269 sv
->data_pad
= be32_to_cpu(vid_hdr
->data_pad
);
270 sv
->compat
= vid_hdr
->compat
;
271 sv
->vol_type
= vid_hdr
->vol_type
== UBI_VID_DYNAMIC
? UBI_DYNAMIC_VOLUME
273 if (vol_id
> si
->highest_vol_id
)
274 si
->highest_vol_id
= vol_id
;
276 rb_link_node(&sv
->rb
, parent
, p
);
277 rb_insert_color(&sv
->rb
, &si
->volumes
);
279 dbg_bld("added volume %d", vol_id
);
284 * compare_lebs - find out which logical eraseblock is newer.
285 * @ubi: UBI device description object
286 * @seb: first logical eraseblock to compare
287 * @pnum: physical eraseblock number of the second logical eraseblock to
289 * @vid_hdr: volume identifier header of the second logical eraseblock
291 * This function compares 2 copies of a LEB and informs which one is newer. In
292 * case of success this function returns a positive value, in case of failure, a
293 * negative error code is returned. The success return codes use the following
295 * o bit 0 is cleared: the first PEB (described by @seb) is newer than the
296 * second PEB (described by @pnum and @vid_hdr);
297 * o bit 0 is set: the second PEB is newer;
298 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
299 * o bit 1 is set: bit-flips were detected in the newer LEB;
300 * o bit 2 is cleared: the older LEB is not corrupted;
301 * o bit 2 is set: the older LEB is corrupted.
303 static int compare_lebs(struct ubi_device
*ubi
, const struct ubi_scan_leb
*seb
,
304 int pnum
, const struct ubi_vid_hdr
*vid_hdr
)
307 int len
, err
, second_is_newer
, bitflips
= 0, corrupted
= 0;
308 uint32_t data_crc
, crc
;
309 struct ubi_vid_hdr
*vh
= NULL
;
310 unsigned long long sqnum2
= be64_to_cpu(vid_hdr
->sqnum
);
312 if (sqnum2
== seb
->sqnum
) {
314 * This must be a really ancient UBI image which has been
315 * created before sequence numbers support has been added. At
316 * that times we used 32-bit LEB versions stored in logical
317 * eraseblocks. That was before UBI got into mainline. We do not
318 * support these images anymore. Well, those images still work,
319 * but only if no unclean reboots happened.
321 ubi_err("unsupported on-flash UBI format\n");
325 /* Obviously the LEB with lower sequence counter is older */
326 second_is_newer
= !!(sqnum2
> seb
->sqnum
);
329 * Now we know which copy is newer. If the copy flag of the PEB with
330 * newer version is not set, then we just return, otherwise we have to
331 * check data CRC. For the second PEB we already have the VID header,
332 * for the first one - we'll need to re-read it from flash.
334 * Note: this may be optimized so that we wouldn't read twice.
337 if (second_is_newer
) {
338 if (!vid_hdr
->copy_flag
) {
339 /* It is not a copy, so it is newer */
340 dbg_bld("second PEB %d is newer, copy_flag is unset",
345 if (!seb
->copy_flag
) {
346 /* It is not a copy, so it is newer */
347 dbg_bld("first PEB %d is newer, copy_flag is unset",
349 return bitflips
<< 1;
352 vh
= ubi_zalloc_vid_hdr(ubi
, GFP_KERNEL
);
357 err
= ubi_io_read_vid_hdr(ubi
, pnum
, vh
, 0);
359 if (err
== UBI_IO_BITFLIPS
)
362 dbg_err("VID of PEB %d header is bad, but it "
363 "was OK earlier, err %d", pnum
, err
);
374 /* Read the data of the copy and check the CRC */
376 len
= be32_to_cpu(vid_hdr
->data_size
);
383 err
= ubi_io_read_data(ubi
, buf
, pnum
, 0, len
);
384 if (err
&& err
!= UBI_IO_BITFLIPS
&& err
!= -EBADMSG
)
387 data_crc
= be32_to_cpu(vid_hdr
->data_crc
);
388 crc
= crc32(UBI_CRC32_INIT
, buf
, len
);
389 if (crc
!= data_crc
) {
390 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
391 pnum
, crc
, data_crc
);
394 second_is_newer
= !second_is_newer
;
396 dbg_bld("PEB %d CRC is OK", pnum
);
401 ubi_free_vid_hdr(ubi
, vh
);
404 dbg_bld("second PEB %d is newer, copy_flag is set", pnum
);
406 dbg_bld("first PEB %d is newer, copy_flag is set", pnum
);
408 return second_is_newer
| (bitflips
<< 1) | (corrupted
<< 2);
413 ubi_free_vid_hdr(ubi
, vh
);
418 * ubi_scan_add_used - add physical eraseblock to the scanning information.
419 * @ubi: UBI device description object
420 * @si: scanning information
421 * @pnum: the physical eraseblock number
423 * @vid_hdr: the volume identifier header
424 * @bitflips: if bit-flips were detected when this physical eraseblock was read
426 * This function adds information about a used physical eraseblock to the
427 * 'used' tree of the corresponding volume. The function is rather complex
428 * because it has to handle cases when this is not the first physical
429 * eraseblock belonging to the same logical eraseblock, and the newer one has
430 * to be picked, while the older one has to be dropped. This function returns
431 * zero in case of success and a negative error code in case of failure.
433 int ubi_scan_add_used(struct ubi_device
*ubi
, struct ubi_scan_info
*si
,
434 int pnum
, int ec
, const struct ubi_vid_hdr
*vid_hdr
,
437 int err
, vol_id
, lnum
;
438 unsigned long long sqnum
;
439 struct ubi_scan_volume
*sv
;
440 struct ubi_scan_leb
*seb
;
441 struct rb_node
**p
, *parent
= NULL
;
443 vol_id
= be32_to_cpu(vid_hdr
->vol_id
);
444 lnum
= be32_to_cpu(vid_hdr
->lnum
);
445 sqnum
= be64_to_cpu(vid_hdr
->sqnum
);
447 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
448 pnum
, vol_id
, lnum
, ec
, sqnum
, bitflips
);
450 sv
= add_volume(si
, vol_id
, pnum
, vid_hdr
);
454 if (si
->max_sqnum
< sqnum
)
455 si
->max_sqnum
= sqnum
;
458 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
459 * if this is the first instance of this logical eraseblock or not.
461 p
= &sv
->root
.rb_node
;
466 seb
= rb_entry(parent
, struct ubi_scan_leb
, u
.rb
);
467 if (lnum
!= seb
->lnum
) {
468 if (lnum
< seb
->lnum
)
476 * There is already a physical eraseblock describing the same
477 * logical eraseblock present.
480 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
481 "EC %d", seb
->pnum
, seb
->sqnum
, seb
->ec
);
484 * Make sure that the logical eraseblocks have different
485 * sequence numbers. Otherwise the image is bad.
487 * However, if the sequence number is zero, we assume it must
488 * be an ancient UBI image from the era when UBI did not have
489 * sequence numbers. We still can attach these images, unless
490 * there is a need to distinguish between old and new
491 * eraseblocks, in which case we'll refuse the image in
492 * 'compare_lebs()'. In other words, we attach old clean
493 * images, but refuse attaching old images with duplicated
494 * logical eraseblocks because there was an unclean reboot.
496 if (seb
->sqnum
== sqnum
&& sqnum
!= 0) {
497 ubi_err("two LEBs with same sequence number %llu",
499 ubi_dbg_dump_seb(seb
, 0);
500 ubi_dbg_dump_vid_hdr(vid_hdr
);
505 * Now we have to drop the older one and preserve the newer
508 cmp_res
= compare_lebs(ubi
, seb
, pnum
, vid_hdr
);
514 * This logical eraseblock is newer than the one
517 err
= validate_vid_hdr(vid_hdr
, sv
, pnum
);
521 err
= add_to_list(si
, seb
->pnum
, seb
->ec
, cmp_res
& 4,
528 seb
->scrub
= ((cmp_res
& 2) || bitflips
);
529 seb
->copy_flag
= vid_hdr
->copy_flag
;
532 if (sv
->highest_lnum
== lnum
)
534 be32_to_cpu(vid_hdr
->data_size
);
539 * This logical eraseblock is older than the one found
542 return add_to_list(si
, pnum
, ec
, cmp_res
& 4,
548 * We've met this logical eraseblock for the first time, add it to the
549 * scanning information.
552 err
= validate_vid_hdr(vid_hdr
, sv
, pnum
);
556 seb
= kmalloc(sizeof(struct ubi_scan_leb
), GFP_KERNEL
);
563 seb
->scrub
= bitflips
;
564 seb
->copy_flag
= vid_hdr
->copy_flag
;
567 if (sv
->highest_lnum
<= lnum
) {
568 sv
->highest_lnum
= lnum
;
569 sv
->last_data_size
= be32_to_cpu(vid_hdr
->data_size
);
573 rb_link_node(&seb
->u
.rb
, parent
, p
);
574 rb_insert_color(&seb
->u
.rb
, &sv
->root
);
579 * ubi_scan_find_sv - find volume in the scanning information.
580 * @si: scanning information
581 * @vol_id: the requested volume ID
583 * This function returns a pointer to the volume description or %NULL if there
584 * are no data about this volume in the scanning information.
586 struct ubi_scan_volume
*ubi_scan_find_sv(const struct ubi_scan_info
*si
,
589 struct ubi_scan_volume
*sv
;
590 struct rb_node
*p
= si
->volumes
.rb_node
;
593 sv
= rb_entry(p
, struct ubi_scan_volume
, rb
);
595 if (vol_id
== sv
->vol_id
)
598 if (vol_id
> sv
->vol_id
)
608 * ubi_scan_find_seb - find LEB in the volume scanning information.
609 * @sv: a pointer to the volume scanning information
610 * @lnum: the requested logical eraseblock
612 * This function returns a pointer to the scanning logical eraseblock or %NULL
613 * if there are no data about it in the scanning volume information.
615 struct ubi_scan_leb
*ubi_scan_find_seb(const struct ubi_scan_volume
*sv
,
618 struct ubi_scan_leb
*seb
;
619 struct rb_node
*p
= sv
->root
.rb_node
;
622 seb
= rb_entry(p
, struct ubi_scan_leb
, u
.rb
);
624 if (lnum
== seb
->lnum
)
627 if (lnum
> seb
->lnum
)
637 * ubi_scan_rm_volume - delete scanning information about a volume.
638 * @si: scanning information
639 * @sv: the volume scanning information to delete
641 void ubi_scan_rm_volume(struct ubi_scan_info
*si
, struct ubi_scan_volume
*sv
)
644 struct ubi_scan_leb
*seb
;
646 dbg_bld("remove scanning information about volume %d", sv
->vol_id
);
648 while ((rb
= rb_first(&sv
->root
))) {
649 seb
= rb_entry(rb
, struct ubi_scan_leb
, u
.rb
);
650 rb_erase(&seb
->u
.rb
, &sv
->root
);
651 list_add_tail(&seb
->u
.list
, &si
->erase
);
654 rb_erase(&sv
->rb
, &si
->volumes
);
660 * ubi_scan_erase_peb - erase a physical eraseblock.
661 * @ubi: UBI device description object
662 * @si: scanning information
663 * @pnum: physical eraseblock number to erase;
664 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
666 * This function erases physical eraseblock 'pnum', and writes the erase
667 * counter header to it. This function should only be used on UBI device
668 * initialization stages, when the EBA sub-system had not been yet initialized.
669 * This function returns zero in case of success and a negative error code in
672 int ubi_scan_erase_peb(struct ubi_device
*ubi
, const struct ubi_scan_info
*si
,
676 struct ubi_ec_hdr
*ec_hdr
;
678 if ((long long)ec
>= UBI_MAX_ERASECOUNTER
) {
680 * Erase counter overflow. Upgrade UBI and use 64-bit
681 * erase counters internally.
683 ubi_err("erase counter overflow at PEB %d, EC %d", pnum
, ec
);
687 ec_hdr
= kzalloc(ubi
->ec_hdr_alsize
, GFP_KERNEL
);
691 ec_hdr
->ec
= cpu_to_be64(ec
);
693 err
= ubi_io_sync_erase(ubi
, pnum
, 0);
697 err
= ubi_io_write_ec_hdr(ubi
, pnum
, ec_hdr
);
705 * ubi_scan_get_free_peb - get a free physical eraseblock.
706 * @ubi: UBI device description object
707 * @si: scanning information
709 * This function returns a free physical eraseblock. It is supposed to be
710 * called on the UBI initialization stages when the wear-leveling sub-system is
711 * not initialized yet. This function picks a physical eraseblocks from one of
712 * the lists, writes the EC header if it is needed, and removes it from the
715 * This function returns scanning physical eraseblock information in case of
716 * success and an error code in case of failure.
718 struct ubi_scan_leb
*ubi_scan_get_free_peb(struct ubi_device
*ubi
,
719 struct ubi_scan_info
*si
)
722 struct ubi_scan_leb
*seb
, *tmp_seb
;
724 if (!list_empty(&si
->free
)) {
725 seb
= list_entry(si
->free
.next
, struct ubi_scan_leb
, u
.list
);
726 list_del(&seb
->u
.list
);
727 dbg_bld("return free PEB %d, EC %d", seb
->pnum
, seb
->ec
);
732 * We try to erase the first physical eraseblock from the erase list
733 * and pick it if we succeed, or try to erase the next one if not. And
734 * so forth. We don't want to take care about bad eraseblocks here -
735 * they'll be handled later.
737 list_for_each_entry_safe(seb
, tmp_seb
, &si
->erase
, u
.list
) {
738 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
739 seb
->ec
= si
->mean_ec
;
741 err
= ubi_scan_erase_peb(ubi
, si
, seb
->pnum
, seb
->ec
+1);
746 list_del(&seb
->u
.list
);
747 dbg_bld("return PEB %d, EC %d", seb
->pnum
, seb
->ec
);
751 ubi_err("no free eraseblocks");
752 return ERR_PTR(-ENOSPC
);
756 * check_corruption - check the data area of PEB.
757 * @ubi: UBI device description object
758 * @vid_hrd: the (corrupted) VID header of this PEB
759 * @pnum: the physical eraseblock number to check
761 * This is a helper function which is used to distinguish between VID header
762 * corruptions caused by power cuts and other reasons. If the PEB contains only
763 * 0xFF bytes in the data area, the VID header is most probably corrupted
764 * because of a power cut (%0 is returned in this case). Otherwise, it was
765 * probably corrupted for some other reasons (%1 is returned in this case). A
766 * negative error code is returned if a read error occurred.
768 * If the corruption reason was a power cut, UBI can safely erase this PEB.
769 * Otherwise, it should preserve it to avoid possibly destroying important
772 static int check_corruption(struct ubi_device
*ubi
, struct ubi_vid_hdr
*vid_hdr
,
777 mutex_lock(&ubi
->buf_mutex
);
778 memset(ubi
->peb_buf1
, 0x00, ubi
->leb_size
);
780 err
= ubi_io_read(ubi
, ubi
->peb_buf1
, pnum
, ubi
->leb_start
,
782 if (err
== UBI_IO_BITFLIPS
|| err
== -EBADMSG
) {
784 * Bit-flips or integrity errors while reading the data area.
785 * It is difficult to say for sure what type of corruption is
786 * this, but presumably a power cut happened while this PEB was
787 * erased, so it became unstable and corrupted, and should be
796 if (ubi_check_pattern(ubi
->peb_buf1
, 0xFF, ubi
->leb_size
)) {
797 mutex_unlock(&ubi
->buf_mutex
);
801 ubi_err("PEB %d contains corrupted VID header, and the data does not "
802 "contain all 0xFF, this may be a non-UBI PEB or a severe VID "
803 "header corruption which requires manual inspection", pnum
);
804 ubi_dbg_dump_vid_hdr(vid_hdr
);
805 dbg_msg("hexdump of PEB %d offset %d, length %d",
806 pnum
, ubi
->leb_start
, ubi
->leb_size
);
807 ubi_dbg_print_hex_dump(KERN_DEBUG
, "", DUMP_PREFIX_OFFSET
, 32, 1,
808 ubi
->peb_buf1
, ubi
->leb_size
, 1);
809 mutex_unlock(&ubi
->buf_mutex
);
814 * process_eb - read, check UBI headers, and add them to scanning information.
815 * @ubi: UBI device description object
816 * @si: scanning information
817 * @pnum: the physical eraseblock number
819 * This function returns a zero if the physical eraseblock was successfully
820 * handled and a negative error code in case of failure.
822 static int process_eb(struct ubi_device
*ubi
, struct ubi_scan_info
*si
,
825 long long uninitialized_var(ec
);
826 int err
, bitflips
= 0, vol_id
, ec_err
= 0;
828 dbg_bld("scan PEB %d", pnum
);
830 /* Skip bad physical eraseblocks */
831 err
= ubi_io_is_bad(ubi
, pnum
);
836 * FIXME: this is actually duty of the I/O sub-system to
837 * initialize this, but MTD does not provide enough
840 si
->bad_peb_count
+= 1;
844 err
= ubi_io_read_ec_hdr(ubi
, pnum
, ech
, 0);
850 case UBI_IO_BITFLIPS
:
854 si
->empty_peb_count
+= 1;
855 return add_to_list(si
, pnum
, UBI_SCAN_UNKNOWN_EC
, 0,
857 case UBI_IO_FF_BITFLIPS
:
858 si
->empty_peb_count
+= 1;
859 return add_to_list(si
, pnum
, UBI_SCAN_UNKNOWN_EC
, 1,
861 case UBI_IO_BAD_HDR_EBADMSG
:
864 * We have to also look at the VID header, possibly it is not
865 * corrupted. Set %bitflips flag in order to make this PEB be
866 * moved and EC be re-created.
869 ec
= UBI_SCAN_UNKNOWN_EC
;
873 ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err
);
880 /* Make sure UBI version is OK */
881 if (ech
->version
!= UBI_VERSION
) {
882 ubi_err("this UBI version is %d, image version is %d",
883 UBI_VERSION
, (int)ech
->version
);
887 ec
= be64_to_cpu(ech
->ec
);
888 if (ec
> UBI_MAX_ERASECOUNTER
) {
890 * Erase counter overflow. The EC headers have 64 bits
891 * reserved, but we anyway make use of only 31 bit
892 * values, as this seems to be enough for any existing
893 * flash. Upgrade UBI and use 64-bit erase counters
896 ubi_err("erase counter overflow, max is %d",
897 UBI_MAX_ERASECOUNTER
);
898 ubi_dbg_dump_ec_hdr(ech
);
903 * Make sure that all PEBs have the same image sequence number.
904 * This allows us to detect situations when users flash UBI
905 * images incorrectly, so that the flash has the new UBI image
906 * and leftovers from the old one. This feature was added
907 * relatively recently, and the sequence number was always
908 * zero, because old UBI implementations always set it to zero.
909 * For this reasons, we do not panic if some PEBs have zero
910 * sequence number, while other PEBs have non-zero sequence
913 image_seq
= be32_to_cpu(ech
->image_seq
);
914 if (!ubi
->image_seq
&& image_seq
)
915 ubi
->image_seq
= image_seq
;
916 if (ubi
->image_seq
&& image_seq
&&
917 ubi
->image_seq
!= image_seq
) {
918 ubi_err("bad image sequence number %d in PEB %d, "
919 "expected %d", image_seq
, pnum
, ubi
->image_seq
);
920 ubi_dbg_dump_ec_hdr(ech
);
925 /* OK, we've done with the EC header, let's look at the VID header */
927 err
= ubi_io_read_vid_hdr(ubi
, pnum
, vidh
, 0);
933 case UBI_IO_BITFLIPS
:
936 case UBI_IO_BAD_HDR_EBADMSG
:
937 if (ec_err
== UBI_IO_BAD_HDR_EBADMSG
)
939 * Both EC and VID headers are corrupted and were read
940 * with data integrity error, probably this is a bad
941 * PEB, bit it is not marked as bad yet. This may also
942 * be a result of power cut during erasure.
944 si
->maybe_bad_peb_count
+= 1;
948 * Both headers are corrupted. There is a possibility
949 * that this a valid UBI PEB which has corresponding
950 * LEB, but the headers are corrupted. However, it is
951 * impossible to distinguish it from a PEB which just
952 * contains garbage because of a power cut during erase
953 * operation. So we just schedule this PEB for erasure.
958 * The EC was OK, but the VID header is corrupted. We
959 * have to check what is in the data area.
961 err
= check_corruption(ubi
, vidh
, pnum
);
966 /* This corruption is caused by a power cut */
967 err
= add_to_list(si
, pnum
, ec
, 1, &si
->erase
);
969 /* This is an unexpected corruption */
970 err
= add_corrupted(si
, pnum
, ec
);
974 case UBI_IO_FF_BITFLIPS
:
975 err
= add_to_list(si
, pnum
, ec
, 1, &si
->erase
);
981 err
= add_to_list(si
, pnum
, ec
, 1, &si
->erase
);
983 err
= add_to_list(si
, pnum
, ec
, 0, &si
->free
);
988 ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
993 vol_id
= be32_to_cpu(vidh
->vol_id
);
994 if (vol_id
> UBI_MAX_VOLUMES
&& vol_id
!= UBI_LAYOUT_VOLUME_ID
) {
995 int lnum
= be32_to_cpu(vidh
->lnum
);
997 /* Unsupported internal volume */
998 switch (vidh
->compat
) {
999 case UBI_COMPAT_DELETE
:
1000 ubi_msg("\"delete\" compatible internal volume %d:%d"
1001 " found, will remove it", vol_id
, lnum
);
1002 err
= add_to_list(si
, pnum
, ec
, 1, &si
->erase
);
1008 ubi_msg("read-only compatible internal volume %d:%d"
1009 " found, switch to read-only mode",
1014 case UBI_COMPAT_PRESERVE
:
1015 ubi_msg("\"preserve\" compatible internal volume %d:%d"
1016 " found", vol_id
, lnum
);
1017 err
= add_to_list(si
, pnum
, ec
, 0, &si
->alien
);
1022 case UBI_COMPAT_REJECT
:
1023 ubi_err("incompatible internal volume %d:%d found",
1030 ubi_warn("valid VID header but corrupted EC header at PEB %d",
1032 err
= ubi_scan_add_used(ubi
, si
, pnum
, ec
, vidh
, bitflips
);
1040 if (ec
> si
->max_ec
)
1042 if (ec
< si
->min_ec
)
1050 * check_what_we_have - check what PEB were found by scanning.
1051 * @ubi: UBI device description object
1052 * @si: scanning information
1054 * This is a helper function which takes a look what PEBs were found by
1055 * scanning, and decides whether the flash is empty and should be formatted and
1056 * whether there are too many corrupted PEBs and we should not attach this
1057 * MTD device. Returns zero if we should proceed with attaching the MTD device,
1058 * and %-EINVAL if we should not.
1060 static int check_what_we_have(struct ubi_device
*ubi
, struct ubi_scan_info
*si
)
1062 struct ubi_scan_leb
*seb
;
1063 int max_corr
, peb_count
;
1065 peb_count
= ubi
->peb_count
- si
->bad_peb_count
- si
->alien_peb_count
;
1066 max_corr
= peb_count
/ 20 ?: 8;
1069 * Few corrupted PEBs is not a problem and may be just a result of
1070 * unclean reboots. However, many of them may indicate some problems
1071 * with the flash HW or driver.
1073 if (si
->corr_peb_count
) {
1074 ubi_err("%d PEBs are corrupted and preserved",
1075 si
->corr_peb_count
);
1076 printk(KERN_ERR
"Corrupted PEBs are:");
1077 list_for_each_entry(seb
, &si
->corr
, u
.list
)
1078 printk(KERN_CONT
" %d", seb
->pnum
);
1079 printk(KERN_CONT
"\n");
1082 * If too many PEBs are corrupted, we refuse attaching,
1083 * otherwise, only print a warning.
1085 if (si
->corr_peb_count
>= max_corr
) {
1086 ubi_err("too many corrupted PEBs, refusing this device");
1091 if (si
->empty_peb_count
+ si
->maybe_bad_peb_count
== peb_count
) {
1093 * All PEBs are empty, or almost all - a couple PEBs look like
1094 * they may be bad PEBs which were not marked as bad yet.
1096 * This piece of code basically tries to distinguish between
1097 * the following situations:
1099 * 1. Flash is empty, but there are few bad PEBs, which are not
1100 * marked as bad so far, and which were read with error. We
1101 * want to go ahead and format this flash. While formatting,
1102 * the faulty PEBs will probably be marked as bad.
1104 * 2. Flash contains non-UBI data and we do not want to format
1105 * it and destroy possibly important information.
1107 if (si
->maybe_bad_peb_count
<= 2) {
1109 ubi_msg("empty MTD device detected");
1110 get_random_bytes(&ubi
->image_seq
,
1111 sizeof(ubi
->image_seq
));
1113 ubi_err("MTD device is not UBI-formatted and possibly "
1114 "contains non-UBI data - refusing it");
1124 * ubi_scan - scan an MTD device.
1125 * @ubi: UBI device description object
1127 * This function does full scanning of an MTD device and returns complete
1128 * information about it. In case of failure, an error code is returned.
1130 struct ubi_scan_info
*ubi_scan(struct ubi_device
*ubi
)
1133 struct rb_node
*rb1
, *rb2
;
1134 struct ubi_scan_volume
*sv
;
1135 struct ubi_scan_leb
*seb
;
1136 struct ubi_scan_info
*si
;
1138 si
= kzalloc(sizeof(struct ubi_scan_info
), GFP_KERNEL
);
1140 return ERR_PTR(-ENOMEM
);
1142 INIT_LIST_HEAD(&si
->corr
);
1143 INIT_LIST_HEAD(&si
->free
);
1144 INIT_LIST_HEAD(&si
->erase
);
1145 INIT_LIST_HEAD(&si
->alien
);
1146 si
->volumes
= RB_ROOT
;
1149 ech
= kzalloc(ubi
->ec_hdr_alsize
, GFP_KERNEL
);
1153 vidh
= ubi_zalloc_vid_hdr(ubi
, GFP_KERNEL
);
1157 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++) {
1160 dbg_gen("process PEB %d", pnum
);
1161 err
= process_eb(ubi
, si
, pnum
);
1166 dbg_msg("scanning is finished");
1168 /* Calculate mean erase counter */
1170 si
->mean_ec
= div_u64(si
->ec_sum
, si
->ec_count
);
1172 err
= check_what_we_have(ubi
, si
);
1177 * In case of unknown erase counter we use the mean erase counter
1180 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
1181 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
)
1182 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
1183 seb
->ec
= si
->mean_ec
;
1186 list_for_each_entry(seb
, &si
->free
, u
.list
) {
1187 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
1188 seb
->ec
= si
->mean_ec
;
1191 list_for_each_entry(seb
, &si
->corr
, u
.list
)
1192 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
1193 seb
->ec
= si
->mean_ec
;
1195 list_for_each_entry(seb
, &si
->erase
, u
.list
)
1196 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
1197 seb
->ec
= si
->mean_ec
;
1199 err
= paranoid_check_si(ubi
, si
);
1203 ubi_free_vid_hdr(ubi
, vidh
);
1209 ubi_free_vid_hdr(ubi
, vidh
);
1213 ubi_scan_destroy_si(si
);
1214 return ERR_PTR(err
);
1218 * destroy_sv - free the scanning volume information
1219 * @sv: scanning volume information
1221 * This function destroys the volume RB-tree (@sv->root) and the scanning
1222 * volume information.
1224 static void destroy_sv(struct ubi_scan_volume
*sv
)
1226 struct ubi_scan_leb
*seb
;
1227 struct rb_node
*this = sv
->root
.rb_node
;
1231 this = this->rb_left
;
1232 else if (this->rb_right
)
1233 this = this->rb_right
;
1235 seb
= rb_entry(this, struct ubi_scan_leb
, u
.rb
);
1236 this = rb_parent(this);
1238 if (this->rb_left
== &seb
->u
.rb
)
1239 this->rb_left
= NULL
;
1241 this->rb_right
= NULL
;
1251 * ubi_scan_destroy_si - destroy scanning information.
1252 * @si: scanning information
1254 void ubi_scan_destroy_si(struct ubi_scan_info
*si
)
1256 struct ubi_scan_leb
*seb
, *seb_tmp
;
1257 struct ubi_scan_volume
*sv
;
1260 list_for_each_entry_safe(seb
, seb_tmp
, &si
->alien
, u
.list
) {
1261 list_del(&seb
->u
.list
);
1264 list_for_each_entry_safe(seb
, seb_tmp
, &si
->erase
, u
.list
) {
1265 list_del(&seb
->u
.list
);
1268 list_for_each_entry_safe(seb
, seb_tmp
, &si
->corr
, u
.list
) {
1269 list_del(&seb
->u
.list
);
1272 list_for_each_entry_safe(seb
, seb_tmp
, &si
->free
, u
.list
) {
1273 list_del(&seb
->u
.list
);
1277 /* Destroy the volume RB-tree */
1278 rb
= si
->volumes
.rb_node
;
1282 else if (rb
->rb_right
)
1285 sv
= rb_entry(rb
, struct ubi_scan_volume
, rb
);
1289 if (rb
->rb_left
== &sv
->rb
)
1292 rb
->rb_right
= NULL
;
1302 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1305 * paranoid_check_si - check the scanning information.
1306 * @ubi: UBI device description object
1307 * @si: scanning information
1309 * This function returns zero if the scanning information is all right, and a
1310 * negative error code if not or if an error occurred.
1312 static int paranoid_check_si(struct ubi_device
*ubi
, struct ubi_scan_info
*si
)
1314 int pnum
, err
, vols_found
= 0;
1315 struct rb_node
*rb1
, *rb2
;
1316 struct ubi_scan_volume
*sv
;
1317 struct ubi_scan_leb
*seb
, *last_seb
;
1321 * At first, check that scanning information is OK.
1323 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
1331 ubi_err("bad is_empty flag");
1335 if (sv
->vol_id
< 0 || sv
->highest_lnum
< 0 ||
1336 sv
->leb_count
< 0 || sv
->vol_type
< 0 || sv
->used_ebs
< 0 ||
1337 sv
->data_pad
< 0 || sv
->last_data_size
< 0) {
1338 ubi_err("negative values");
1342 if (sv
->vol_id
>= UBI_MAX_VOLUMES
&&
1343 sv
->vol_id
< UBI_INTERNAL_VOL_START
) {
1344 ubi_err("bad vol_id");
1348 if (sv
->vol_id
> si
->highest_vol_id
) {
1349 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1350 si
->highest_vol_id
, sv
->vol_id
);
1354 if (sv
->vol_type
!= UBI_DYNAMIC_VOLUME
&&
1355 sv
->vol_type
!= UBI_STATIC_VOLUME
) {
1356 ubi_err("bad vol_type");
1360 if (sv
->data_pad
> ubi
->leb_size
/ 2) {
1361 ubi_err("bad data_pad");
1366 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
) {
1372 if (seb
->pnum
< 0 || seb
->ec
< 0) {
1373 ubi_err("negative values");
1377 if (seb
->ec
< si
->min_ec
) {
1378 ubi_err("bad si->min_ec (%d), %d found",
1379 si
->min_ec
, seb
->ec
);
1383 if (seb
->ec
> si
->max_ec
) {
1384 ubi_err("bad si->max_ec (%d), %d found",
1385 si
->max_ec
, seb
->ec
);
1389 if (seb
->pnum
>= ubi
->peb_count
) {
1390 ubi_err("too high PEB number %d, total PEBs %d",
1391 seb
->pnum
, ubi
->peb_count
);
1395 if (sv
->vol_type
== UBI_STATIC_VOLUME
) {
1396 if (seb
->lnum
>= sv
->used_ebs
) {
1397 ubi_err("bad lnum or used_ebs");
1401 if (sv
->used_ebs
!= 0) {
1402 ubi_err("non-zero used_ebs");
1407 if (seb
->lnum
> sv
->highest_lnum
) {
1408 ubi_err("incorrect highest_lnum or lnum");
1413 if (sv
->leb_count
!= leb_count
) {
1414 ubi_err("bad leb_count, %d objects in the tree",
1424 if (seb
->lnum
!= sv
->highest_lnum
) {
1425 ubi_err("bad highest_lnum");
1430 if (vols_found
!= si
->vols_found
) {
1431 ubi_err("bad si->vols_found %d, should be %d",
1432 si
->vols_found
, vols_found
);
1436 /* Check that scanning information is correct */
1437 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
1439 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
) {
1446 err
= ubi_io_read_vid_hdr(ubi
, seb
->pnum
, vidh
, 1);
1447 if (err
&& err
!= UBI_IO_BITFLIPS
) {
1448 ubi_err("VID header is not OK (%d)", err
);
1454 vol_type
= vidh
->vol_type
== UBI_VID_DYNAMIC
?
1455 UBI_DYNAMIC_VOLUME
: UBI_STATIC_VOLUME
;
1456 if (sv
->vol_type
!= vol_type
) {
1457 ubi_err("bad vol_type");
1461 if (seb
->sqnum
!= be64_to_cpu(vidh
->sqnum
)) {
1462 ubi_err("bad sqnum %llu", seb
->sqnum
);
1466 if (sv
->vol_id
!= be32_to_cpu(vidh
->vol_id
)) {
1467 ubi_err("bad vol_id %d", sv
->vol_id
);
1471 if (sv
->compat
!= vidh
->compat
) {
1472 ubi_err("bad compat %d", vidh
->compat
);
1476 if (seb
->lnum
!= be32_to_cpu(vidh
->lnum
)) {
1477 ubi_err("bad lnum %d", seb
->lnum
);
1481 if (sv
->used_ebs
!= be32_to_cpu(vidh
->used_ebs
)) {
1482 ubi_err("bad used_ebs %d", sv
->used_ebs
);
1486 if (sv
->data_pad
!= be32_to_cpu(vidh
->data_pad
)) {
1487 ubi_err("bad data_pad %d", sv
->data_pad
);
1495 if (sv
->highest_lnum
!= be32_to_cpu(vidh
->lnum
)) {
1496 ubi_err("bad highest_lnum %d", sv
->highest_lnum
);
1500 if (sv
->last_data_size
!= be32_to_cpu(vidh
->data_size
)) {
1501 ubi_err("bad last_data_size %d", sv
->last_data_size
);
1507 * Make sure that all the physical eraseblocks are in one of the lists
1510 buf
= kzalloc(ubi
->peb_count
, GFP_KERNEL
);
1514 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++) {
1515 err
= ubi_io_is_bad(ubi
, pnum
);
1523 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
)
1524 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
)
1527 list_for_each_entry(seb
, &si
->free
, u
.list
)
1530 list_for_each_entry(seb
, &si
->corr
, u
.list
)
1533 list_for_each_entry(seb
, &si
->erase
, u
.list
)
1536 list_for_each_entry(seb
, &si
->alien
, u
.list
)
1540 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++)
1542 ubi_err("PEB %d is not referred", pnum
);
1552 ubi_err("bad scanning information about LEB %d", seb
->lnum
);
1553 ubi_dbg_dump_seb(seb
, 0);
1554 ubi_dbg_dump_sv(sv
);
1558 ubi_err("bad scanning information about volume %d", sv
->vol_id
);
1559 ubi_dbg_dump_sv(sv
);
1563 ubi_err("bad scanning information about volume %d", sv
->vol_id
);
1564 ubi_dbg_dump_sv(sv
);
1565 ubi_dbg_dump_vid_hdr(vidh
);
1568 ubi_dbg_dump_stack();
1572 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */