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(struct ubi_device
*ubi
, struct ubi_scan_info
*si
);
50 #define paranoid_check_si(ubi, si) 0
53 /* Temporary variables used during scanning */
54 static struct ubi_ec_hdr
*ech
;
55 static struct ubi_vid_hdr
*vidh
;
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
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
68 static int add_to_list(struct ubi_scan_info
*si
, int pnum
, int ec
,
69 struct list_head
*list
)
71 struct ubi_scan_leb
*seb
;
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
);
84 seb
= kmalloc(sizeof(struct ubi_scan_leb
), GFP_KERNEL
);
90 list_add_tail(&seb
->u
.list
, list
);
95 * commit_to_mean_value - commit intermediate results to the final mean erase
97 * @si: scanning information
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.
107 static void commit_to_mean_value(struct ubi_scan_info
*si
)
109 si
->ec_sum
/= si
->ec_count
;
110 if (si
->ec_sum
% si
->ec_count
>= si
->ec_count
/ 2)
112 si
->mean_ec
+= si
->ec_sum
;
116 * validate_vid_hdr - check that volume identifier header is correct and
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
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.
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.
130 static int validate_vid_hdr(const struct ubi_vid_hdr
*vid_hdr
,
131 const struct ubi_scan_volume
*sv
, int pnum
)
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
);
138 if (sv
->leb_count
!= 0) {
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.
147 if (vol_id
!= sv
->vol_id
) {
148 dbg_err("inconsistent vol_id");
152 if (sv
->vol_type
== UBI_STATIC_VOLUME
)
153 sv_vol_type
= UBI_VID_STATIC
;
155 sv_vol_type
= UBI_VID_DYNAMIC
;
157 if (vol_type
!= sv_vol_type
) {
158 dbg_err("inconsistent vol_type");
162 if (used_ebs
!= sv
->used_ebs
) {
163 dbg_err("inconsistent used_ebs");
167 if (data_pad
!= sv
->data_pad
) {
168 dbg_err("inconsistent data_pad");
176 ubi_err("inconsistent VID header at PEB %d", pnum
);
177 ubi_dbg_dump_vid_hdr(vid_hdr
);
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
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.
195 static struct ubi_scan_volume
*add_volume(struct ubi_scan_info
*si
, int vol_id
,
197 const struct ubi_vid_hdr
*vid_hdr
)
199 struct ubi_scan_volume
*sv
;
200 struct rb_node
**p
= &si
->volumes
.rb_node
, *parent
= NULL
;
202 ubi_assert(vol_id
== be32_to_cpu(vid_hdr
->vol_id
));
204 /* Walk the volume RB-tree to look if this volume is already present */
207 sv
= rb_entry(parent
, struct ubi_scan_volume
, rb
);
209 if (vol_id
== sv
->vol_id
)
212 if (vol_id
> sv
->vol_id
)
218 /* The volume is absent - add it */
219 sv
= kmalloc(sizeof(struct ubi_scan_volume
), GFP_KERNEL
);
221 return ERR_PTR(-ENOMEM
);
223 sv
->highest_lnum
= sv
->leb_count
= 0;
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
231 if (vol_id
> si
->highest_vol_id
)
232 si
->highest_vol_id
= vol_id
;
234 rb_link_node(&sv
->rb
, parent
, p
);
235 rb_insert_color(&sv
->rb
, &si
->volumes
);
237 dbg_bld("added volume %d", vol_id
);
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
247 * @vid_hdr: volume identifier header of the second logical eraseblock
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
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.
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
)
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
);
270 if (seb
->sqnum
== 0 && sqnum2
== 0) {
271 long long abs
, v1
= seb
->leb_ver
, v2
= be32_to_cpu(vid_hdr
->leb_ver
);
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.
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.
286 * FIXME: but this is anyway obsolete and will be removed at
289 dbg_bld("using old crappy leb_ver stuff");
292 ubi_err("PEB %d and PEB %d have the same version %lld",
293 seb
->pnum
, pnum
, v1
);
301 if (abs
< 0x7FFFFFFF)
302 /* Non-overflow situation */
303 second_is_newer
= (v2
> v1
);
305 second_is_newer
= (v2
< v1
);
307 /* Obviously the LEB with lower sequence counter is older */
308 second_is_newer
= sqnum2
> seb
->sqnum
;
311 * Now we know which copy is newer. If the copy flag of the PEB with
312 * newer version is not set, then we just return, otherwise we have to
313 * check data CRC. For the second PEB we already have the VID header,
314 * for the first one - we'll need to re-read it from flash.
316 * FIXME: this may be optimized so that we wouldn't read twice.
319 if (second_is_newer
) {
320 if (!vid_hdr
->copy_flag
) {
321 /* It is not a copy, so it is newer */
322 dbg_bld("second PEB %d is newer, copy_flag is unset",
329 vh
= ubi_zalloc_vid_hdr(ubi
, GFP_KERNEL
);
333 err
= ubi_io_read_vid_hdr(ubi
, pnum
, vh
, 0);
335 if (err
== UBI_IO_BITFLIPS
)
338 dbg_err("VID of PEB %d header is bad, but it "
339 "was OK earlier", pnum
);
347 if (!vh
->copy_flag
) {
348 /* It is not a copy, so it is newer */
349 dbg_bld("first PEB %d is newer, copy_flag is unset",
358 /* Read the data of the copy and check the CRC */
360 len
= be32_to_cpu(vid_hdr
->data_size
);
367 err
= ubi_io_read_data(ubi
, buf
, pnum
, 0, len
);
368 if (err
&& err
!= UBI_IO_BITFLIPS
)
371 data_crc
= be32_to_cpu(vid_hdr
->data_crc
);
372 crc
= crc32(UBI_CRC32_INIT
, buf
, len
);
373 if (crc
!= data_crc
) {
374 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
375 pnum
, crc
, data_crc
);
378 second_is_newer
= !second_is_newer
;
380 dbg_bld("PEB %d CRC is OK", pnum
);
385 ubi_free_vid_hdr(ubi
, vh
);
388 dbg_bld("second PEB %d is newer, copy_flag is set", pnum
);
390 dbg_bld("first PEB %d is newer, copy_flag is set", pnum
);
392 return second_is_newer
| (bitflips
<< 1) | (corrupted
<< 2);
397 ubi_free_vid_hdr(ubi
, vh
);
402 * ubi_scan_add_used - add information about a physical eraseblock to the
403 * scanning information.
404 * @ubi: UBI device description object
405 * @si: scanning information
406 * @pnum: the physical eraseblock number
408 * @vid_hdr: the volume identifier header
409 * @bitflips: if bit-flips were detected when this physical eraseblock was read
411 * This function adds information about a used physical eraseblock to the
412 * 'used' tree of the corresponding volume. The function is rather complex
413 * because it has to handle cases when this is not the first physical
414 * eraseblock belonging to the same logical eraseblock, and the newer one has
415 * to be picked, while the older one has to be dropped. This function returns
416 * zero in case of success and a negative error code in case of failure.
418 int ubi_scan_add_used(struct ubi_device
*ubi
, struct ubi_scan_info
*si
,
419 int pnum
, int ec
, const struct ubi_vid_hdr
*vid_hdr
,
422 int err
, vol_id
, lnum
;
424 unsigned long long sqnum
;
425 struct ubi_scan_volume
*sv
;
426 struct ubi_scan_leb
*seb
;
427 struct rb_node
**p
, *parent
= NULL
;
429 vol_id
= be32_to_cpu(vid_hdr
->vol_id
);
430 lnum
= be32_to_cpu(vid_hdr
->lnum
);
431 sqnum
= be64_to_cpu(vid_hdr
->sqnum
);
432 leb_ver
= be32_to_cpu(vid_hdr
->leb_ver
);
434 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d",
435 pnum
, vol_id
, lnum
, ec
, sqnum
, leb_ver
, bitflips
);
437 sv
= add_volume(si
, vol_id
, pnum
, vid_hdr
);
441 if (si
->max_sqnum
< sqnum
)
442 si
->max_sqnum
= sqnum
;
445 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
446 * if this is the first instance of this logical eraseblock or not.
448 p
= &sv
->root
.rb_node
;
453 seb
= rb_entry(parent
, struct ubi_scan_leb
, u
.rb
);
454 if (lnum
!= seb
->lnum
) {
455 if (lnum
< seb
->lnum
)
463 * There is already a physical eraseblock describing the same
464 * logical eraseblock present.
467 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
468 "LEB ver %u, EC %d", seb
->pnum
, seb
->sqnum
,
469 seb
->leb_ver
, seb
->ec
);
472 * Make sure that the logical eraseblocks have different
473 * versions. Otherwise the image is bad.
475 if (seb
->leb_ver
== leb_ver
&& leb_ver
!= 0) {
476 ubi_err("two LEBs with same version %u", leb_ver
);
477 ubi_dbg_dump_seb(seb
, 0);
478 ubi_dbg_dump_vid_hdr(vid_hdr
);
483 * Make sure that the logical eraseblocks have different
484 * sequence numbers. Otherwise the image is bad.
486 * FIXME: remove 'sqnum != 0' check when leb_ver is removed.
488 if (seb
->sqnum
== sqnum
&& sqnum
!= 0) {
489 ubi_err("two LEBs with same sequence number %llu",
491 ubi_dbg_dump_seb(seb
, 0);
492 ubi_dbg_dump_vid_hdr(vid_hdr
);
497 * Now we have to drop the older one and preserve the newer
500 cmp_res
= compare_lebs(ubi
, seb
, pnum
, vid_hdr
);
506 * This logical eraseblock is newer then the one
509 err
= validate_vid_hdr(vid_hdr
, sv
, pnum
);
514 err
= add_to_list(si
, seb
->pnum
, seb
->ec
,
517 err
= add_to_list(si
, seb
->pnum
, seb
->ec
,
524 seb
->scrub
= ((cmp_res
& 2) || bitflips
);
526 seb
->leb_ver
= leb_ver
;
528 if (sv
->highest_lnum
== lnum
)
530 be32_to_cpu(vid_hdr
->data_size
);
535 * This logical eraseblock is older then the one found
539 return add_to_list(si
, pnum
, ec
, &si
->corr
);
541 return add_to_list(si
, pnum
, ec
, &si
->erase
);
546 * We've met this logical eraseblock for the first time, add it to the
547 * scanning information.
550 err
= validate_vid_hdr(vid_hdr
, sv
, pnum
);
554 seb
= kmalloc(sizeof(struct ubi_scan_leb
), GFP_KERNEL
);
562 seb
->scrub
= bitflips
;
563 seb
->leb_ver
= leb_ver
;
565 if (sv
->highest_lnum
<= lnum
) {
566 sv
->highest_lnum
= lnum
;
567 sv
->last_data_size
= be32_to_cpu(vid_hdr
->data_size
);
571 rb_link_node(&seb
->u
.rb
, parent
, p
);
572 rb_insert_color(&seb
->u
.rb
, &sv
->root
);
577 * ubi_scan_find_sv - find information about a particular volume in the
578 * scanning information.
579 * @si: scanning information
580 * @vol_id: the requested volume ID
582 * This function returns a pointer to the volume description or %NULL if there
583 * are no data about this volume in the scanning information.
585 struct ubi_scan_volume
*ubi_scan_find_sv(const struct ubi_scan_info
*si
,
588 struct ubi_scan_volume
*sv
;
589 struct rb_node
*p
= si
->volumes
.rb_node
;
592 sv
= rb_entry(p
, struct ubi_scan_volume
, rb
);
594 if (vol_id
== sv
->vol_id
)
597 if (vol_id
> sv
->vol_id
)
607 * ubi_scan_find_seb - find information about a particular logical
608 * eraseblock 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 unit had not been yet initialized. This
669 * function returns zero in case of success and a negative error code in case
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 unit is not
711 * initialized yet. This function picks a physical eraseblocks from one of the
712 * lists, writes the EC header if it is needed, and removes it from the list.
714 * This function returns scanning physical eraseblock information in case of
715 * success and an error code in case of failure.
717 struct ubi_scan_leb
*ubi_scan_get_free_peb(struct ubi_device
*ubi
,
718 struct ubi_scan_info
*si
)
721 struct ubi_scan_leb
*seb
;
723 if (!list_empty(&si
->free
)) {
724 seb
= list_entry(si
->free
.next
, struct ubi_scan_leb
, u
.list
);
725 list_del(&seb
->u
.list
);
726 dbg_bld("return free PEB %d, EC %d", seb
->pnum
, seb
->ec
);
730 for (i
= 0; i
< 2; i
++) {
731 struct list_head
*head
;
732 struct ubi_scan_leb
*tmp_seb
;
740 * We try to erase the first physical eraseblock from the @head
741 * list and pick it if we succeed, or try to erase the
742 * next one if not. And so forth. We don't want to take care
743 * about bad eraseblocks here - they'll be handled later.
745 list_for_each_entry_safe(seb
, tmp_seb
, head
, u
.list
) {
746 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
747 seb
->ec
= si
->mean_ec
;
749 err
= ubi_scan_erase_peb(ubi
, si
, seb
->pnum
, seb
->ec
+1);
754 list_del(&seb
->u
.list
);
755 dbg_bld("return PEB %d, EC %d", seb
->pnum
, seb
->ec
);
760 ubi_err("no eraseblocks found");
761 return ERR_PTR(-ENOSPC
);
765 * process_eb - read UBI headers, check them and add corresponding data
766 * to the scanning information.
767 * @ubi: UBI device description object
768 * @si: scanning information
769 * @pnum: the physical eraseblock number
771 * This function returns a zero if the physical eraseblock was successfully
772 * handled and a negative error code in case of failure.
774 static int process_eb(struct ubi_device
*ubi
, struct ubi_scan_info
*si
, int pnum
)
776 long long uninitialized_var(ec
);
777 int err
, bitflips
= 0, vol_id
, ec_corr
= 0;
779 dbg_bld("scan PEB %d", pnum
);
781 /* Skip bad physical eraseblocks */
782 err
= ubi_io_is_bad(ubi
, pnum
);
787 * FIXME: this is actually duty of the I/O unit to initialize
788 * this, but MTD does not provide enough information.
790 si
->bad_peb_count
+= 1;
794 err
= ubi_io_read_ec_hdr(ubi
, pnum
, ech
, 0);
797 else if (err
== UBI_IO_BITFLIPS
)
799 else if (err
== UBI_IO_PEB_EMPTY
)
800 return add_to_list(si
, pnum
, UBI_SCAN_UNKNOWN_EC
, &si
->erase
);
801 else if (err
== UBI_IO_BAD_EC_HDR
) {
803 * We have to also look at the VID header, possibly it is not
804 * corrupted. Set %bitflips flag in order to make this PEB be
805 * moved and EC be re-created.
808 ec
= UBI_SCAN_UNKNOWN_EC
;
815 /* Make sure UBI version is OK */
816 if (ech
->version
!= UBI_VERSION
) {
817 ubi_err("this UBI version is %d, image version is %d",
818 UBI_VERSION
, (int)ech
->version
);
822 ec
= be64_to_cpu(ech
->ec
);
823 if (ec
> UBI_MAX_ERASECOUNTER
) {
825 * Erase counter overflow. The EC headers have 64 bits
826 * reserved, but we anyway make use of only 31 bit
827 * values, as this seems to be enough for any existing
828 * flash. Upgrade UBI and use 64-bit erase counters
831 ubi_err("erase counter overflow, max is %d",
832 UBI_MAX_ERASECOUNTER
);
833 ubi_dbg_dump_ec_hdr(ech
);
838 /* OK, we've done with the EC header, let's look at the VID header */
840 err
= ubi_io_read_vid_hdr(ubi
, pnum
, vidh
, 0);
843 else if (err
== UBI_IO_BITFLIPS
)
845 else if (err
== UBI_IO_BAD_VID_HDR
||
846 (err
== UBI_IO_PEB_FREE
&& ec_corr
)) {
847 /* VID header is corrupted */
848 err
= add_to_list(si
, pnum
, ec
, &si
->corr
);
852 } else if (err
== UBI_IO_PEB_FREE
) {
853 /* No VID header - the physical eraseblock is free */
854 err
= add_to_list(si
, pnum
, ec
, &si
->free
);
860 vol_id
= be32_to_cpu(vidh
->vol_id
);
861 if (vol_id
> UBI_MAX_VOLUMES
&& vol_id
!= UBI_LAYOUT_VOLUME_ID
) {
862 int lnum
= be32_to_cpu(vidh
->lnum
);
864 /* Unsupported internal volume */
865 switch (vidh
->compat
) {
866 case UBI_COMPAT_DELETE
:
867 ubi_msg("\"delete\" compatible internal volume %d:%d"
868 " found, remove it", vol_id
, lnum
);
869 err
= add_to_list(si
, pnum
, ec
, &si
->corr
);
875 ubi_msg("read-only compatible internal volume %d:%d"
876 " found, switch to read-only mode",
881 case UBI_COMPAT_PRESERVE
:
882 ubi_msg("\"preserve\" compatible internal volume %d:%d"
883 " found", vol_id
, lnum
);
884 err
= add_to_list(si
, pnum
, ec
, &si
->alien
);
887 si
->alien_peb_count
+= 1;
890 case UBI_COMPAT_REJECT
:
891 ubi_err("incompatible internal volume %d:%d found",
897 /* Both UBI headers seem to be fine */
898 err
= ubi_scan_add_used(ubi
, si
, pnum
, ec
, vidh
, bitflips
);
904 if (si
->ec_sum
+ ec
< ec
) {
905 commit_to_mean_value(si
);
923 * ubi_scan - scan an MTD device.
924 * @ubi: UBI device description object
926 * This function does full scanning of an MTD device and returns complete
927 * information about it. In case of failure, an error code is returned.
929 struct ubi_scan_info
*ubi_scan(struct ubi_device
*ubi
)
932 struct rb_node
*rb1
, *rb2
;
933 struct ubi_scan_volume
*sv
;
934 struct ubi_scan_leb
*seb
;
935 struct ubi_scan_info
*si
;
937 si
= kzalloc(sizeof(struct ubi_scan_info
), GFP_KERNEL
);
939 return ERR_PTR(-ENOMEM
);
941 INIT_LIST_HEAD(&si
->corr
);
942 INIT_LIST_HEAD(&si
->free
);
943 INIT_LIST_HEAD(&si
->erase
);
944 INIT_LIST_HEAD(&si
->alien
);
945 si
->volumes
= RB_ROOT
;
949 ech
= kzalloc(ubi
->ec_hdr_alsize
, GFP_KERNEL
);
953 vidh
= ubi_zalloc_vid_hdr(ubi
, GFP_KERNEL
);
957 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++) {
960 dbg_msg("process PEB %d", pnum
);
961 err
= process_eb(ubi
, si
, pnum
);
966 dbg_msg("scanning is finished");
968 /* Finish mean erase counter calculations */
970 commit_to_mean_value(si
);
973 ubi_msg("empty MTD device detected");
976 * In case of unknown erase counter we use the mean erase counter
979 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
980 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
)
981 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
982 seb
->ec
= si
->mean_ec
;
985 list_for_each_entry(seb
, &si
->free
, u
.list
) {
986 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
987 seb
->ec
= si
->mean_ec
;
990 list_for_each_entry(seb
, &si
->corr
, u
.list
)
991 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
992 seb
->ec
= si
->mean_ec
;
994 list_for_each_entry(seb
, &si
->erase
, u
.list
)
995 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
996 seb
->ec
= si
->mean_ec
;
998 err
= paranoid_check_si(ubi
, si
);
1005 ubi_free_vid_hdr(ubi
, vidh
);
1011 ubi_free_vid_hdr(ubi
, vidh
);
1015 ubi_scan_destroy_si(si
);
1016 return ERR_PTR(err
);
1020 * destroy_sv - free the scanning volume information
1021 * @sv: scanning volume information
1023 * This function destroys the volume RB-tree (@sv->root) and the scanning
1024 * volume information.
1026 static void destroy_sv(struct ubi_scan_volume
*sv
)
1028 struct ubi_scan_leb
*seb
;
1029 struct rb_node
*this = sv
->root
.rb_node
;
1033 this = this->rb_left
;
1034 else if (this->rb_right
)
1035 this = this->rb_right
;
1037 seb
= rb_entry(this, struct ubi_scan_leb
, u
.rb
);
1038 this = rb_parent(this);
1040 if (this->rb_left
== &seb
->u
.rb
)
1041 this->rb_left
= NULL
;
1043 this->rb_right
= NULL
;
1053 * ubi_scan_destroy_si - destroy scanning information.
1054 * @si: scanning information
1056 void ubi_scan_destroy_si(struct ubi_scan_info
*si
)
1058 struct ubi_scan_leb
*seb
, *seb_tmp
;
1059 struct ubi_scan_volume
*sv
;
1062 list_for_each_entry_safe(seb
, seb_tmp
, &si
->alien
, u
.list
) {
1063 list_del(&seb
->u
.list
);
1066 list_for_each_entry_safe(seb
, seb_tmp
, &si
->erase
, u
.list
) {
1067 list_del(&seb
->u
.list
);
1070 list_for_each_entry_safe(seb
, seb_tmp
, &si
->corr
, u
.list
) {
1071 list_del(&seb
->u
.list
);
1074 list_for_each_entry_safe(seb
, seb_tmp
, &si
->free
, u
.list
) {
1075 list_del(&seb
->u
.list
);
1079 /* Destroy the volume RB-tree */
1080 rb
= si
->volumes
.rb_node
;
1084 else if (rb
->rb_right
)
1087 sv
= rb_entry(rb
, struct ubi_scan_volume
, rb
);
1091 if (rb
->rb_left
== &sv
->rb
)
1094 rb
->rb_right
= NULL
;
1104 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1107 * paranoid_check_si - check if the scanning information is correct and
1109 * @ubi: UBI device description object
1110 * @si: scanning information
1112 * This function returns zero if the scanning information is all right, %1 if
1113 * not and a negative error code if an error occurred.
1115 static int paranoid_check_si(struct ubi_device
*ubi
, struct ubi_scan_info
*si
)
1117 int pnum
, err
, vols_found
= 0;
1118 struct rb_node
*rb1
, *rb2
;
1119 struct ubi_scan_volume
*sv
;
1120 struct ubi_scan_leb
*seb
, *last_seb
;
1124 * At first, check that scanning information is OK.
1126 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
1134 ubi_err("bad is_empty flag");
1138 if (sv
->vol_id
< 0 || sv
->highest_lnum
< 0 ||
1139 sv
->leb_count
< 0 || sv
->vol_type
< 0 || sv
->used_ebs
< 0 ||
1140 sv
->data_pad
< 0 || sv
->last_data_size
< 0) {
1141 ubi_err("negative values");
1145 if (sv
->vol_id
>= UBI_MAX_VOLUMES
&&
1146 sv
->vol_id
< UBI_INTERNAL_VOL_START
) {
1147 ubi_err("bad vol_id");
1151 if (sv
->vol_id
> si
->highest_vol_id
) {
1152 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1153 si
->highest_vol_id
, sv
->vol_id
);
1157 if (sv
->vol_type
!= UBI_DYNAMIC_VOLUME
&&
1158 sv
->vol_type
!= UBI_STATIC_VOLUME
) {
1159 ubi_err("bad vol_type");
1163 if (sv
->data_pad
> ubi
->leb_size
/ 2) {
1164 ubi_err("bad data_pad");
1169 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
) {
1175 if (seb
->pnum
< 0 || seb
->ec
< 0) {
1176 ubi_err("negative values");
1180 if (seb
->ec
< si
->min_ec
) {
1181 ubi_err("bad si->min_ec (%d), %d found",
1182 si
->min_ec
, seb
->ec
);
1186 if (seb
->ec
> si
->max_ec
) {
1187 ubi_err("bad si->max_ec (%d), %d found",
1188 si
->max_ec
, seb
->ec
);
1192 if (seb
->pnum
>= ubi
->peb_count
) {
1193 ubi_err("too high PEB number %d, total PEBs %d",
1194 seb
->pnum
, ubi
->peb_count
);
1198 if (sv
->vol_type
== UBI_STATIC_VOLUME
) {
1199 if (seb
->lnum
>= sv
->used_ebs
) {
1200 ubi_err("bad lnum or used_ebs");
1204 if (sv
->used_ebs
!= 0) {
1205 ubi_err("non-zero used_ebs");
1210 if (seb
->lnum
> sv
->highest_lnum
) {
1211 ubi_err("incorrect highest_lnum or lnum");
1216 if (sv
->leb_count
!= leb_count
) {
1217 ubi_err("bad leb_count, %d objects in the tree",
1227 if (seb
->lnum
!= sv
->highest_lnum
) {
1228 ubi_err("bad highest_lnum");
1233 if (vols_found
!= si
->vols_found
) {
1234 ubi_err("bad si->vols_found %d, should be %d",
1235 si
->vols_found
, vols_found
);
1239 /* Check that scanning information is correct */
1240 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
1242 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
) {
1249 err
= ubi_io_read_vid_hdr(ubi
, seb
->pnum
, vidh
, 1);
1250 if (err
&& err
!= UBI_IO_BITFLIPS
) {
1251 ubi_err("VID header is not OK (%d)", err
);
1257 vol_type
= vidh
->vol_type
== UBI_VID_DYNAMIC
?
1258 UBI_DYNAMIC_VOLUME
: UBI_STATIC_VOLUME
;
1259 if (sv
->vol_type
!= vol_type
) {
1260 ubi_err("bad vol_type");
1264 if (seb
->sqnum
!= be64_to_cpu(vidh
->sqnum
)) {
1265 ubi_err("bad sqnum %llu", seb
->sqnum
);
1269 if (sv
->vol_id
!= be32_to_cpu(vidh
->vol_id
)) {
1270 ubi_err("bad vol_id %d", sv
->vol_id
);
1274 if (sv
->compat
!= vidh
->compat
) {
1275 ubi_err("bad compat %d", vidh
->compat
);
1279 if (seb
->lnum
!= be32_to_cpu(vidh
->lnum
)) {
1280 ubi_err("bad lnum %d", seb
->lnum
);
1284 if (sv
->used_ebs
!= be32_to_cpu(vidh
->used_ebs
)) {
1285 ubi_err("bad used_ebs %d", sv
->used_ebs
);
1289 if (sv
->data_pad
!= be32_to_cpu(vidh
->data_pad
)) {
1290 ubi_err("bad data_pad %d", sv
->data_pad
);
1294 if (seb
->leb_ver
!= be32_to_cpu(vidh
->leb_ver
)) {
1295 ubi_err("bad leb_ver %u", seb
->leb_ver
);
1303 if (sv
->highest_lnum
!= be32_to_cpu(vidh
->lnum
)) {
1304 ubi_err("bad highest_lnum %d", sv
->highest_lnum
);
1308 if (sv
->last_data_size
!= be32_to_cpu(vidh
->data_size
)) {
1309 ubi_err("bad last_data_size %d", sv
->last_data_size
);
1315 * Make sure that all the physical eraseblocks are in one of the lists
1318 buf
= kzalloc(ubi
->peb_count
, GFP_KERNEL
);
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 */