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>
45 #include <asm/div64.h>
48 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
49 static int paranoid_check_si(struct ubi_device
*ubi
, 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 * validate_vid_hdr - check that volume identifier header is correct and
98 * @vid_hdr: the volume identifier header to check
99 * @sv: information about the volume this logical eraseblock belongs to
100 * @pnum: physical eraseblock number the VID header came from
102 * This function checks that data stored in @vid_hdr is consistent. Returns
103 * non-zero if an inconsistency was found and zero if not.
105 * Note, UBI does sanity check of everything it reads from the flash media.
106 * Most of the checks are done in the I/O unit. Here we check that the
107 * information in the VID header is consistent to the information in other VID
108 * headers of the same volume.
110 static int validate_vid_hdr(const struct ubi_vid_hdr
*vid_hdr
,
111 const struct ubi_scan_volume
*sv
, int pnum
)
113 int vol_type
= vid_hdr
->vol_type
;
114 int vol_id
= be32_to_cpu(vid_hdr
->vol_id
);
115 int used_ebs
= be32_to_cpu(vid_hdr
->used_ebs
);
116 int data_pad
= be32_to_cpu(vid_hdr
->data_pad
);
118 if (sv
->leb_count
!= 0) {
122 * This is not the first logical eraseblock belonging to this
123 * volume. Ensure that the data in its VID header is consistent
124 * to the data in previous logical eraseblock headers.
127 if (vol_id
!= sv
->vol_id
) {
128 dbg_err("inconsistent vol_id");
132 if (sv
->vol_type
== UBI_STATIC_VOLUME
)
133 sv_vol_type
= UBI_VID_STATIC
;
135 sv_vol_type
= UBI_VID_DYNAMIC
;
137 if (vol_type
!= sv_vol_type
) {
138 dbg_err("inconsistent vol_type");
142 if (used_ebs
!= sv
->used_ebs
) {
143 dbg_err("inconsistent used_ebs");
147 if (data_pad
!= sv
->data_pad
) {
148 dbg_err("inconsistent data_pad");
156 ubi_err("inconsistent VID header at PEB %d", pnum
);
157 ubi_dbg_dump_vid_hdr(vid_hdr
);
163 * add_volume - add volume to the scanning information.
164 * @si: scanning information
165 * @vol_id: ID of the volume to add
166 * @pnum: physical eraseblock number
167 * @vid_hdr: volume identifier header
169 * If the volume corresponding to the @vid_hdr logical eraseblock is already
170 * present in the scanning information, this function does nothing. Otherwise
171 * it adds corresponding volume to the scanning information. Returns a pointer
172 * to the scanning volume object in case of success and a negative error code
173 * in case of failure.
175 static struct ubi_scan_volume
*add_volume(struct ubi_scan_info
*si
, int vol_id
,
177 const struct ubi_vid_hdr
*vid_hdr
)
179 struct ubi_scan_volume
*sv
;
180 struct rb_node
**p
= &si
->volumes
.rb_node
, *parent
= NULL
;
182 ubi_assert(vol_id
== be32_to_cpu(vid_hdr
->vol_id
));
184 /* Walk the volume RB-tree to look if this volume is already present */
187 sv
= rb_entry(parent
, struct ubi_scan_volume
, rb
);
189 if (vol_id
== sv
->vol_id
)
192 if (vol_id
> sv
->vol_id
)
198 /* The volume is absent - add it */
199 sv
= kmalloc(sizeof(struct ubi_scan_volume
), GFP_KERNEL
);
201 return ERR_PTR(-ENOMEM
);
203 sv
->highest_lnum
= sv
->leb_count
= 0;
206 sv
->used_ebs
= be32_to_cpu(vid_hdr
->used_ebs
);
207 sv
->data_pad
= be32_to_cpu(vid_hdr
->data_pad
);
208 sv
->compat
= vid_hdr
->compat
;
209 sv
->vol_type
= vid_hdr
->vol_type
== UBI_VID_DYNAMIC
? UBI_DYNAMIC_VOLUME
211 if (vol_id
> si
->highest_vol_id
)
212 si
->highest_vol_id
= vol_id
;
214 rb_link_node(&sv
->rb
, parent
, p
);
215 rb_insert_color(&sv
->rb
, &si
->volumes
);
217 dbg_bld("added volume %d", vol_id
);
222 * compare_lebs - find out which logical eraseblock is newer.
223 * @ubi: UBI device description object
224 * @seb: first logical eraseblock to compare
225 * @pnum: physical eraseblock number of the second logical eraseblock to
227 * @vid_hdr: volume identifier header of the second logical eraseblock
229 * This function compares 2 copies of a LEB and informs which one is newer. In
230 * case of success this function returns a positive value, in case of failure, a
231 * negative error code is returned. The success return codes use the following
233 * o bit 0 is cleared: the first PEB (described by @seb) is newer then the
234 * second PEB (described by @pnum and @vid_hdr);
235 * o bit 0 is set: the second PEB is newer;
236 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
237 * o bit 1 is set: bit-flips were detected in the newer LEB;
238 * o bit 2 is cleared: the older LEB is not corrupted;
239 * o bit 2 is set: the older LEB is corrupted.
241 static int compare_lebs(struct ubi_device
*ubi
, const struct ubi_scan_leb
*seb
,
242 int pnum
, const struct ubi_vid_hdr
*vid_hdr
)
245 int len
, err
, second_is_newer
, bitflips
= 0, corrupted
= 0;
246 uint32_t data_crc
, crc
;
247 struct ubi_vid_hdr
*vh
= NULL
;
248 unsigned long long sqnum2
= be64_to_cpu(vid_hdr
->sqnum
);
250 if (seb
->sqnum
== 0 && sqnum2
== 0) {
251 long long abs
, v1
= seb
->leb_ver
, v2
= be32_to_cpu(vid_hdr
->leb_ver
);
254 * UBI constantly increases the logical eraseblock version
255 * number and it can overflow. Thus, we have to bear in mind
256 * that versions that are close to %0xFFFFFFFF are less then
257 * versions that are close to %0.
259 * The UBI WL unit guarantees that the number of pending tasks
260 * is not greater then %0x7FFFFFFF. So, if the difference
261 * between any two versions is greater or equivalent to
262 * %0x7FFFFFFF, there was an overflow and the logical
263 * eraseblock with lower version is actually newer then the one
264 * with higher version.
266 * FIXME: but this is anyway obsolete and will be removed at
269 dbg_bld("using old crappy leb_ver stuff");
272 ubi_err("PEB %d and PEB %d have the same version %lld",
273 seb
->pnum
, pnum
, v1
);
281 if (abs
< 0x7FFFFFFF)
282 /* Non-overflow situation */
283 second_is_newer
= (v2
> v1
);
285 second_is_newer
= (v2
< v1
);
287 /* Obviously the LEB with lower sequence counter is older */
288 second_is_newer
= sqnum2
> seb
->sqnum
;
291 * Now we know which copy is newer. If the copy flag of the PEB with
292 * newer version is not set, then we just return, otherwise we have to
293 * check data CRC. For the second PEB we already have the VID header,
294 * for the first one - we'll need to re-read it from flash.
296 * FIXME: this may be optimized so that we wouldn't read twice.
299 if (second_is_newer
) {
300 if (!vid_hdr
->copy_flag
) {
301 /* It is not a copy, so it is newer */
302 dbg_bld("second PEB %d is newer, copy_flag is unset",
309 vh
= ubi_zalloc_vid_hdr(ubi
, GFP_KERNEL
);
313 err
= ubi_io_read_vid_hdr(ubi
, pnum
, vh
, 0);
315 if (err
== UBI_IO_BITFLIPS
)
318 dbg_err("VID of PEB %d header is bad, but it "
319 "was OK earlier", pnum
);
327 if (!vh
->copy_flag
) {
328 /* It is not a copy, so it is newer */
329 dbg_bld("first PEB %d is newer, copy_flag is unset",
338 /* Read the data of the copy and check the CRC */
340 len
= be32_to_cpu(vid_hdr
->data_size
);
347 err
= ubi_io_read_data(ubi
, buf
, pnum
, 0, len
);
348 if (err
&& err
!= UBI_IO_BITFLIPS
)
351 data_crc
= be32_to_cpu(vid_hdr
->data_crc
);
352 crc
= crc32(UBI_CRC32_INIT
, buf
, len
);
353 if (crc
!= data_crc
) {
354 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
355 pnum
, crc
, data_crc
);
358 second_is_newer
= !second_is_newer
;
360 dbg_bld("PEB %d CRC is OK", pnum
);
365 ubi_free_vid_hdr(ubi
, vh
);
368 dbg_bld("second PEB %d is newer, copy_flag is set", pnum
);
370 dbg_bld("first PEB %d is newer, copy_flag is set", pnum
);
372 return second_is_newer
| (bitflips
<< 1) | (corrupted
<< 2);
377 ubi_free_vid_hdr(ubi
, vh
);
382 * ubi_scan_add_used - add information about a physical eraseblock to the
383 * scanning information.
384 * @ubi: UBI device description object
385 * @si: scanning information
386 * @pnum: the physical eraseblock number
388 * @vid_hdr: the volume identifier header
389 * @bitflips: if bit-flips were detected when this physical eraseblock was read
391 * This function adds information about a used physical eraseblock to the
392 * 'used' tree of the corresponding volume. The function is rather complex
393 * because it has to handle cases when this is not the first physical
394 * eraseblock belonging to the same logical eraseblock, and the newer one has
395 * to be picked, while the older one has to be dropped. This function returns
396 * zero in case of success and a negative error code in case of failure.
398 int ubi_scan_add_used(struct ubi_device
*ubi
, struct ubi_scan_info
*si
,
399 int pnum
, int ec
, const struct ubi_vid_hdr
*vid_hdr
,
402 int err
, vol_id
, lnum
;
404 unsigned long long sqnum
;
405 struct ubi_scan_volume
*sv
;
406 struct ubi_scan_leb
*seb
;
407 struct rb_node
**p
, *parent
= NULL
;
409 vol_id
= be32_to_cpu(vid_hdr
->vol_id
);
410 lnum
= be32_to_cpu(vid_hdr
->lnum
);
411 sqnum
= be64_to_cpu(vid_hdr
->sqnum
);
412 leb_ver
= be32_to_cpu(vid_hdr
->leb_ver
);
414 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d",
415 pnum
, vol_id
, lnum
, ec
, sqnum
, leb_ver
, bitflips
);
417 sv
= add_volume(si
, vol_id
, pnum
, vid_hdr
);
421 if (si
->max_sqnum
< sqnum
)
422 si
->max_sqnum
= sqnum
;
425 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
426 * if this is the first instance of this logical eraseblock or not.
428 p
= &sv
->root
.rb_node
;
433 seb
= rb_entry(parent
, struct ubi_scan_leb
, u
.rb
);
434 if (lnum
!= seb
->lnum
) {
435 if (lnum
< seb
->lnum
)
443 * There is already a physical eraseblock describing the same
444 * logical eraseblock present.
447 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
448 "LEB ver %u, EC %d", seb
->pnum
, seb
->sqnum
,
449 seb
->leb_ver
, seb
->ec
);
452 * Make sure that the logical eraseblocks have different
453 * versions. Otherwise the image is bad.
455 if (seb
->leb_ver
== leb_ver
&& leb_ver
!= 0) {
456 ubi_err("two LEBs with same version %u", leb_ver
);
457 ubi_dbg_dump_seb(seb
, 0);
458 ubi_dbg_dump_vid_hdr(vid_hdr
);
463 * Make sure that the logical eraseblocks have different
464 * sequence numbers. Otherwise the image is bad.
466 * FIXME: remove 'sqnum != 0' check when leb_ver is removed.
468 if (seb
->sqnum
== sqnum
&& sqnum
!= 0) {
469 ubi_err("two LEBs with same sequence number %llu",
471 ubi_dbg_dump_seb(seb
, 0);
472 ubi_dbg_dump_vid_hdr(vid_hdr
);
477 * Now we have to drop the older one and preserve the newer
480 cmp_res
= compare_lebs(ubi
, seb
, pnum
, vid_hdr
);
486 * This logical eraseblock is newer then the one
489 err
= validate_vid_hdr(vid_hdr
, sv
, pnum
);
494 err
= add_to_list(si
, seb
->pnum
, seb
->ec
,
497 err
= add_to_list(si
, seb
->pnum
, seb
->ec
,
504 seb
->scrub
= ((cmp_res
& 2) || bitflips
);
506 seb
->leb_ver
= leb_ver
;
508 if (sv
->highest_lnum
== lnum
)
510 be32_to_cpu(vid_hdr
->data_size
);
515 * This logical eraseblock is older then the one found
519 return add_to_list(si
, pnum
, ec
, &si
->corr
);
521 return add_to_list(si
, pnum
, ec
, &si
->erase
);
526 * We've met this logical eraseblock for the first time, add it to the
527 * scanning information.
530 err
= validate_vid_hdr(vid_hdr
, sv
, pnum
);
534 seb
= kmalloc(sizeof(struct ubi_scan_leb
), GFP_KERNEL
);
542 seb
->scrub
= bitflips
;
543 seb
->leb_ver
= leb_ver
;
545 if (sv
->highest_lnum
<= lnum
) {
546 sv
->highest_lnum
= lnum
;
547 sv
->last_data_size
= be32_to_cpu(vid_hdr
->data_size
);
551 rb_link_node(&seb
->u
.rb
, parent
, p
);
552 rb_insert_color(&seb
->u
.rb
, &sv
->root
);
557 * ubi_scan_find_sv - find information about a particular volume in the
558 * scanning information.
559 * @si: scanning information
560 * @vol_id: the requested volume ID
562 * This function returns a pointer to the volume description or %NULL if there
563 * are no data about this volume in the scanning information.
565 struct ubi_scan_volume
*ubi_scan_find_sv(const struct ubi_scan_info
*si
,
568 struct ubi_scan_volume
*sv
;
569 struct rb_node
*p
= si
->volumes
.rb_node
;
572 sv
= rb_entry(p
, struct ubi_scan_volume
, rb
);
574 if (vol_id
== sv
->vol_id
)
577 if (vol_id
> sv
->vol_id
)
587 * ubi_scan_find_seb - find information about a particular logical
588 * eraseblock in the volume scanning information.
589 * @sv: a pointer to the volume scanning information
590 * @lnum: the requested logical eraseblock
592 * This function returns a pointer to the scanning logical eraseblock or %NULL
593 * if there are no data about it in the scanning volume information.
595 struct ubi_scan_leb
*ubi_scan_find_seb(const struct ubi_scan_volume
*sv
,
598 struct ubi_scan_leb
*seb
;
599 struct rb_node
*p
= sv
->root
.rb_node
;
602 seb
= rb_entry(p
, struct ubi_scan_leb
, u
.rb
);
604 if (lnum
== seb
->lnum
)
607 if (lnum
> seb
->lnum
)
617 * ubi_scan_rm_volume - delete scanning information about a volume.
618 * @si: scanning information
619 * @sv: the volume scanning information to delete
621 void ubi_scan_rm_volume(struct ubi_scan_info
*si
, struct ubi_scan_volume
*sv
)
624 struct ubi_scan_leb
*seb
;
626 dbg_bld("remove scanning information about volume %d", sv
->vol_id
);
628 while ((rb
= rb_first(&sv
->root
))) {
629 seb
= rb_entry(rb
, struct ubi_scan_leb
, u
.rb
);
630 rb_erase(&seb
->u
.rb
, &sv
->root
);
631 list_add_tail(&seb
->u
.list
, &si
->erase
);
634 rb_erase(&sv
->rb
, &si
->volumes
);
640 * ubi_scan_erase_peb - erase a physical eraseblock.
641 * @ubi: UBI device description object
642 * @si: scanning information
643 * @pnum: physical eraseblock number to erase;
644 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
646 * This function erases physical eraseblock 'pnum', and writes the erase
647 * counter header to it. This function should only be used on UBI device
648 * initialization stages, when the EBA unit had not been yet initialized. This
649 * function returns zero in case of success and a negative error code in case
652 int ubi_scan_erase_peb(struct ubi_device
*ubi
, const struct ubi_scan_info
*si
,
656 struct ubi_ec_hdr
*ec_hdr
;
658 if ((long long)ec
>= UBI_MAX_ERASECOUNTER
) {
660 * Erase counter overflow. Upgrade UBI and use 64-bit
661 * erase counters internally.
663 ubi_err("erase counter overflow at PEB %d, EC %d", pnum
, ec
);
667 ec_hdr
= kzalloc(ubi
->ec_hdr_alsize
, GFP_KERNEL
);
671 ec_hdr
->ec
= cpu_to_be64(ec
);
673 err
= ubi_io_sync_erase(ubi
, pnum
, 0);
677 err
= ubi_io_write_ec_hdr(ubi
, pnum
, ec_hdr
);
685 * ubi_scan_get_free_peb - get a free physical eraseblock.
686 * @ubi: UBI device description object
687 * @si: scanning information
689 * This function returns a free physical eraseblock. It is supposed to be
690 * called on the UBI initialization stages when the wear-leveling unit is not
691 * initialized yet. This function picks a physical eraseblocks from one of the
692 * lists, writes the EC header if it is needed, and removes it from the list.
694 * This function returns scanning physical eraseblock information in case of
695 * success and an error code in case of failure.
697 struct ubi_scan_leb
*ubi_scan_get_free_peb(struct ubi_device
*ubi
,
698 struct ubi_scan_info
*si
)
701 struct ubi_scan_leb
*seb
;
703 if (!list_empty(&si
->free
)) {
704 seb
= list_entry(si
->free
.next
, struct ubi_scan_leb
, u
.list
);
705 list_del(&seb
->u
.list
);
706 dbg_bld("return free PEB %d, EC %d", seb
->pnum
, seb
->ec
);
710 for (i
= 0; i
< 2; i
++) {
711 struct list_head
*head
;
712 struct ubi_scan_leb
*tmp_seb
;
720 * We try to erase the first physical eraseblock from the @head
721 * list and pick it if we succeed, or try to erase the
722 * next one if not. And so forth. We don't want to take care
723 * about bad eraseblocks here - they'll be handled later.
725 list_for_each_entry_safe(seb
, tmp_seb
, head
, u
.list
) {
726 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
727 seb
->ec
= si
->mean_ec
;
729 err
= ubi_scan_erase_peb(ubi
, si
, seb
->pnum
, seb
->ec
+1);
734 list_del(&seb
->u
.list
);
735 dbg_bld("return PEB %d, EC %d", seb
->pnum
, seb
->ec
);
740 ubi_err("no eraseblocks found");
741 return ERR_PTR(-ENOSPC
);
745 * process_eb - read UBI headers, check them and add corresponding data
746 * to the scanning information.
747 * @ubi: UBI device description object
748 * @si: scanning information
749 * @pnum: the physical eraseblock number
751 * This function returns a zero if the physical eraseblock was successfully
752 * handled and a negative error code in case of failure.
754 static int process_eb(struct ubi_device
*ubi
, struct ubi_scan_info
*si
, int pnum
)
756 long long uninitialized_var(ec
);
757 int err
, bitflips
= 0, vol_id
, ec_corr
= 0;
759 dbg_bld("scan PEB %d", pnum
);
761 /* Skip bad physical eraseblocks */
762 err
= ubi_io_is_bad(ubi
, pnum
);
767 * FIXME: this is actually duty of the I/O unit to initialize
768 * this, but MTD does not provide enough information.
770 si
->bad_peb_count
+= 1;
774 err
= ubi_io_read_ec_hdr(ubi
, pnum
, ech
, 0);
777 else if (err
== UBI_IO_BITFLIPS
)
779 else if (err
== UBI_IO_PEB_EMPTY
)
780 return add_to_list(si
, pnum
, UBI_SCAN_UNKNOWN_EC
, &si
->erase
);
781 else if (err
== UBI_IO_BAD_EC_HDR
) {
783 * We have to also look at the VID header, possibly it is not
784 * corrupted. Set %bitflips flag in order to make this PEB be
785 * moved and EC be re-created.
788 ec
= UBI_SCAN_UNKNOWN_EC
;
795 /* Make sure UBI version is OK */
796 if (ech
->version
!= UBI_VERSION
) {
797 ubi_err("this UBI version is %d, image version is %d",
798 UBI_VERSION
, (int)ech
->version
);
802 ec
= be64_to_cpu(ech
->ec
);
803 if (ec
> UBI_MAX_ERASECOUNTER
) {
805 * Erase counter overflow. The EC headers have 64 bits
806 * reserved, but we anyway make use of only 31 bit
807 * values, as this seems to be enough for any existing
808 * flash. Upgrade UBI and use 64-bit erase counters
811 ubi_err("erase counter overflow, max is %d",
812 UBI_MAX_ERASECOUNTER
);
813 ubi_dbg_dump_ec_hdr(ech
);
818 /* OK, we've done with the EC header, let's look at the VID header */
820 err
= ubi_io_read_vid_hdr(ubi
, pnum
, vidh
, 0);
823 else if (err
== UBI_IO_BITFLIPS
)
825 else if (err
== UBI_IO_BAD_VID_HDR
||
826 (err
== UBI_IO_PEB_FREE
&& ec_corr
)) {
827 /* VID header is corrupted */
828 err
= add_to_list(si
, pnum
, ec
, &si
->corr
);
832 } else if (err
== UBI_IO_PEB_FREE
) {
833 /* No VID header - the physical eraseblock is free */
834 err
= add_to_list(si
, pnum
, ec
, &si
->free
);
840 vol_id
= be32_to_cpu(vidh
->vol_id
);
841 if (vol_id
> UBI_MAX_VOLUMES
&& vol_id
!= UBI_LAYOUT_VOLUME_ID
) {
842 int lnum
= be32_to_cpu(vidh
->lnum
);
844 /* Unsupported internal volume */
845 switch (vidh
->compat
) {
846 case UBI_COMPAT_DELETE
:
847 ubi_msg("\"delete\" compatible internal volume %d:%d"
848 " found, remove it", vol_id
, lnum
);
849 err
= add_to_list(si
, pnum
, ec
, &si
->corr
);
855 ubi_msg("read-only compatible internal volume %d:%d"
856 " found, switch to read-only mode",
861 case UBI_COMPAT_PRESERVE
:
862 ubi_msg("\"preserve\" compatible internal volume %d:%d"
863 " found", vol_id
, lnum
);
864 err
= add_to_list(si
, pnum
, ec
, &si
->alien
);
867 si
->alien_peb_count
+= 1;
870 case UBI_COMPAT_REJECT
:
871 ubi_err("incompatible internal volume %d:%d found",
877 /* Both UBI headers seem to be fine */
878 err
= ubi_scan_add_used(ubi
, si
, pnum
, ec
, vidh
, bitflips
);
896 * ubi_scan - scan an MTD device.
897 * @ubi: UBI device description object
899 * This function does full scanning of an MTD device and returns complete
900 * information about it. In case of failure, an error code is returned.
902 struct ubi_scan_info
*ubi_scan(struct ubi_device
*ubi
)
905 struct rb_node
*rb1
, *rb2
;
906 struct ubi_scan_volume
*sv
;
907 struct ubi_scan_leb
*seb
;
908 struct ubi_scan_info
*si
;
910 si
= kzalloc(sizeof(struct ubi_scan_info
), GFP_KERNEL
);
912 return ERR_PTR(-ENOMEM
);
914 INIT_LIST_HEAD(&si
->corr
);
915 INIT_LIST_HEAD(&si
->free
);
916 INIT_LIST_HEAD(&si
->erase
);
917 INIT_LIST_HEAD(&si
->alien
);
918 si
->volumes
= RB_ROOT
;
922 ech
= kzalloc(ubi
->ec_hdr_alsize
, GFP_KERNEL
);
926 vidh
= ubi_zalloc_vid_hdr(ubi
, GFP_KERNEL
);
930 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++) {
933 dbg_msg("process PEB %d", pnum
);
934 err
= process_eb(ubi
, si
, pnum
);
939 dbg_msg("scanning is finished");
941 /* Calculate mean erase counter */
943 do_div(si
->ec_sum
, si
->ec_count
);
944 si
->mean_ec
= si
->ec_sum
;
948 ubi_msg("empty MTD device detected");
951 * In case of unknown erase counter we use the mean erase counter
954 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
955 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
)
956 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
957 seb
->ec
= si
->mean_ec
;
960 list_for_each_entry(seb
, &si
->free
, u
.list
) {
961 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
962 seb
->ec
= si
->mean_ec
;
965 list_for_each_entry(seb
, &si
->corr
, u
.list
)
966 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
967 seb
->ec
= si
->mean_ec
;
969 list_for_each_entry(seb
, &si
->erase
, u
.list
)
970 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
971 seb
->ec
= si
->mean_ec
;
973 err
= paranoid_check_si(ubi
, si
);
980 ubi_free_vid_hdr(ubi
, vidh
);
986 ubi_free_vid_hdr(ubi
, vidh
);
990 ubi_scan_destroy_si(si
);
995 * destroy_sv - free the scanning volume information
996 * @sv: scanning volume information
998 * This function destroys the volume RB-tree (@sv->root) and the scanning
999 * volume information.
1001 static void destroy_sv(struct ubi_scan_volume
*sv
)
1003 struct ubi_scan_leb
*seb
;
1004 struct rb_node
*this = sv
->root
.rb_node
;
1008 this = this->rb_left
;
1009 else if (this->rb_right
)
1010 this = this->rb_right
;
1012 seb
= rb_entry(this, struct ubi_scan_leb
, u
.rb
);
1013 this = rb_parent(this);
1015 if (this->rb_left
== &seb
->u
.rb
)
1016 this->rb_left
= NULL
;
1018 this->rb_right
= NULL
;
1028 * ubi_scan_destroy_si - destroy scanning information.
1029 * @si: scanning information
1031 void ubi_scan_destroy_si(struct ubi_scan_info
*si
)
1033 struct ubi_scan_leb
*seb
, *seb_tmp
;
1034 struct ubi_scan_volume
*sv
;
1037 list_for_each_entry_safe(seb
, seb_tmp
, &si
->alien
, u
.list
) {
1038 list_del(&seb
->u
.list
);
1041 list_for_each_entry_safe(seb
, seb_tmp
, &si
->erase
, u
.list
) {
1042 list_del(&seb
->u
.list
);
1045 list_for_each_entry_safe(seb
, seb_tmp
, &si
->corr
, u
.list
) {
1046 list_del(&seb
->u
.list
);
1049 list_for_each_entry_safe(seb
, seb_tmp
, &si
->free
, u
.list
) {
1050 list_del(&seb
->u
.list
);
1054 /* Destroy the volume RB-tree */
1055 rb
= si
->volumes
.rb_node
;
1059 else if (rb
->rb_right
)
1062 sv
= rb_entry(rb
, struct ubi_scan_volume
, rb
);
1066 if (rb
->rb_left
== &sv
->rb
)
1069 rb
->rb_right
= NULL
;
1079 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1082 * paranoid_check_si - check if the scanning information is correct and
1084 * @ubi: UBI device description object
1085 * @si: scanning information
1087 * This function returns zero if the scanning information is all right, %1 if
1088 * not and a negative error code if an error occurred.
1090 static int paranoid_check_si(struct ubi_device
*ubi
, struct ubi_scan_info
*si
)
1092 int pnum
, err
, vols_found
= 0;
1093 struct rb_node
*rb1
, *rb2
;
1094 struct ubi_scan_volume
*sv
;
1095 struct ubi_scan_leb
*seb
, *last_seb
;
1099 * At first, check that scanning information is OK.
1101 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
1109 ubi_err("bad is_empty flag");
1113 if (sv
->vol_id
< 0 || sv
->highest_lnum
< 0 ||
1114 sv
->leb_count
< 0 || sv
->vol_type
< 0 || sv
->used_ebs
< 0 ||
1115 sv
->data_pad
< 0 || sv
->last_data_size
< 0) {
1116 ubi_err("negative values");
1120 if (sv
->vol_id
>= UBI_MAX_VOLUMES
&&
1121 sv
->vol_id
< UBI_INTERNAL_VOL_START
) {
1122 ubi_err("bad vol_id");
1126 if (sv
->vol_id
> si
->highest_vol_id
) {
1127 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1128 si
->highest_vol_id
, sv
->vol_id
);
1132 if (sv
->vol_type
!= UBI_DYNAMIC_VOLUME
&&
1133 sv
->vol_type
!= UBI_STATIC_VOLUME
) {
1134 ubi_err("bad vol_type");
1138 if (sv
->data_pad
> ubi
->leb_size
/ 2) {
1139 ubi_err("bad data_pad");
1144 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
) {
1150 if (seb
->pnum
< 0 || seb
->ec
< 0) {
1151 ubi_err("negative values");
1155 if (seb
->ec
< si
->min_ec
) {
1156 ubi_err("bad si->min_ec (%d), %d found",
1157 si
->min_ec
, seb
->ec
);
1161 if (seb
->ec
> si
->max_ec
) {
1162 ubi_err("bad si->max_ec (%d), %d found",
1163 si
->max_ec
, seb
->ec
);
1167 if (seb
->pnum
>= ubi
->peb_count
) {
1168 ubi_err("too high PEB number %d, total PEBs %d",
1169 seb
->pnum
, ubi
->peb_count
);
1173 if (sv
->vol_type
== UBI_STATIC_VOLUME
) {
1174 if (seb
->lnum
>= sv
->used_ebs
) {
1175 ubi_err("bad lnum or used_ebs");
1179 if (sv
->used_ebs
!= 0) {
1180 ubi_err("non-zero used_ebs");
1185 if (seb
->lnum
> sv
->highest_lnum
) {
1186 ubi_err("incorrect highest_lnum or lnum");
1191 if (sv
->leb_count
!= leb_count
) {
1192 ubi_err("bad leb_count, %d objects in the tree",
1202 if (seb
->lnum
!= sv
->highest_lnum
) {
1203 ubi_err("bad highest_lnum");
1208 if (vols_found
!= si
->vols_found
) {
1209 ubi_err("bad si->vols_found %d, should be %d",
1210 si
->vols_found
, vols_found
);
1214 /* Check that scanning information is correct */
1215 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
1217 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
) {
1224 err
= ubi_io_read_vid_hdr(ubi
, seb
->pnum
, vidh
, 1);
1225 if (err
&& err
!= UBI_IO_BITFLIPS
) {
1226 ubi_err("VID header is not OK (%d)", err
);
1232 vol_type
= vidh
->vol_type
== UBI_VID_DYNAMIC
?
1233 UBI_DYNAMIC_VOLUME
: UBI_STATIC_VOLUME
;
1234 if (sv
->vol_type
!= vol_type
) {
1235 ubi_err("bad vol_type");
1239 if (seb
->sqnum
!= be64_to_cpu(vidh
->sqnum
)) {
1240 ubi_err("bad sqnum %llu", seb
->sqnum
);
1244 if (sv
->vol_id
!= be32_to_cpu(vidh
->vol_id
)) {
1245 ubi_err("bad vol_id %d", sv
->vol_id
);
1249 if (sv
->compat
!= vidh
->compat
) {
1250 ubi_err("bad compat %d", vidh
->compat
);
1254 if (seb
->lnum
!= be32_to_cpu(vidh
->lnum
)) {
1255 ubi_err("bad lnum %d", seb
->lnum
);
1259 if (sv
->used_ebs
!= be32_to_cpu(vidh
->used_ebs
)) {
1260 ubi_err("bad used_ebs %d", sv
->used_ebs
);
1264 if (sv
->data_pad
!= be32_to_cpu(vidh
->data_pad
)) {
1265 ubi_err("bad data_pad %d", sv
->data_pad
);
1269 if (seb
->leb_ver
!= be32_to_cpu(vidh
->leb_ver
)) {
1270 ubi_err("bad leb_ver %u", seb
->leb_ver
);
1278 if (sv
->highest_lnum
!= be32_to_cpu(vidh
->lnum
)) {
1279 ubi_err("bad highest_lnum %d", sv
->highest_lnum
);
1283 if (sv
->last_data_size
!= be32_to_cpu(vidh
->data_size
)) {
1284 ubi_err("bad last_data_size %d", sv
->last_data_size
);
1290 * Make sure that all the physical eraseblocks are in one of the lists
1293 buf
= kzalloc(ubi
->peb_count
, GFP_KERNEL
);
1297 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++) {
1298 err
= ubi_io_is_bad(ubi
, pnum
);
1307 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
)
1308 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
)
1311 list_for_each_entry(seb
, &si
->free
, u
.list
)
1314 list_for_each_entry(seb
, &si
->corr
, u
.list
)
1317 list_for_each_entry(seb
, &si
->erase
, u
.list
)
1320 list_for_each_entry(seb
, &si
->alien
, u
.list
)
1324 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++)
1326 ubi_err("PEB %d is not referred", pnum
);
1336 ubi_err("bad scanning information about LEB %d", seb
->lnum
);
1337 ubi_dbg_dump_seb(seb
, 0);
1338 ubi_dbg_dump_sv(sv
);
1342 ubi_err("bad scanning information about volume %d", sv
->vol_id
);
1343 ubi_dbg_dump_sv(sv
);
1347 ubi_err("bad scanning information about volume %d", sv
->vol_id
);
1348 ubi_dbg_dump_sv(sv
);
1349 ubi_dbg_dump_vid_hdr(vidh
);
1352 ubi_dbg_dump_stack();
1356 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */