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 * 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 <linux/math64.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 volume identifier header.
97 * @vid_hdr: the volume identifier header to check
98 * @sv: information about the volume this logical eraseblock belongs to
99 * @pnum: physical eraseblock number the VID header came from
101 * This function checks that data stored in @vid_hdr is consistent. Returns
102 * non-zero if an inconsistency was found and zero if not.
104 * Note, UBI does sanity check of everything it reads from the flash media.
105 * Most of the checks are done in the I/O sub-system. Here we check that the
106 * information in the VID header is consistent to the information in other VID
107 * headers of the same volume.
109 static int validate_vid_hdr(const struct ubi_vid_hdr
*vid_hdr
,
110 const struct ubi_scan_volume
*sv
, int pnum
)
112 int vol_type
= vid_hdr
->vol_type
;
113 int vol_id
= be32_to_cpu(vid_hdr
->vol_id
);
114 int used_ebs
= be32_to_cpu(vid_hdr
->used_ebs
);
115 int data_pad
= be32_to_cpu(vid_hdr
->data_pad
);
117 if (sv
->leb_count
!= 0) {
121 * This is not the first logical eraseblock belonging to this
122 * volume. Ensure that the data in its VID header is consistent
123 * to the data in previous logical eraseblock headers.
126 if (vol_id
!= sv
->vol_id
) {
127 dbg_err("inconsistent vol_id");
131 if (sv
->vol_type
== UBI_STATIC_VOLUME
)
132 sv_vol_type
= UBI_VID_STATIC
;
134 sv_vol_type
= UBI_VID_DYNAMIC
;
136 if (vol_type
!= sv_vol_type
) {
137 dbg_err("inconsistent vol_type");
141 if (used_ebs
!= sv
->used_ebs
) {
142 dbg_err("inconsistent used_ebs");
146 if (data_pad
!= sv
->data_pad
) {
147 dbg_err("inconsistent data_pad");
155 ubi_err("inconsistent VID header at PEB %d", pnum
);
156 ubi_dbg_dump_vid_hdr(vid_hdr
);
162 * add_volume - add volume to the scanning information.
163 * @si: scanning information
164 * @vol_id: ID of the volume to add
165 * @pnum: physical eraseblock number
166 * @vid_hdr: volume identifier header
168 * If the volume corresponding to the @vid_hdr logical eraseblock is already
169 * present in the scanning information, this function does nothing. Otherwise
170 * it adds corresponding volume to the scanning information. Returns a pointer
171 * to the scanning volume object in case of success and a negative error code
172 * in case of failure.
174 static struct ubi_scan_volume
*add_volume(struct ubi_scan_info
*si
, int vol_id
,
176 const struct ubi_vid_hdr
*vid_hdr
)
178 struct ubi_scan_volume
*sv
;
179 struct rb_node
**p
= &si
->volumes
.rb_node
, *parent
= NULL
;
181 ubi_assert(vol_id
== be32_to_cpu(vid_hdr
->vol_id
));
183 /* Walk the volume RB-tree to look if this volume is already present */
186 sv
= rb_entry(parent
, struct ubi_scan_volume
, rb
);
188 if (vol_id
== sv
->vol_id
)
191 if (vol_id
> sv
->vol_id
)
197 /* The volume is absent - add it */
198 sv
= kmalloc(sizeof(struct ubi_scan_volume
), GFP_KERNEL
);
200 return ERR_PTR(-ENOMEM
);
202 sv
->highest_lnum
= sv
->leb_count
= 0;
205 sv
->used_ebs
= be32_to_cpu(vid_hdr
->used_ebs
);
206 sv
->data_pad
= be32_to_cpu(vid_hdr
->data_pad
);
207 sv
->compat
= vid_hdr
->compat
;
208 sv
->vol_type
= vid_hdr
->vol_type
== UBI_VID_DYNAMIC
? UBI_DYNAMIC_VOLUME
210 if (vol_id
> si
->highest_vol_id
)
211 si
->highest_vol_id
= vol_id
;
213 rb_link_node(&sv
->rb
, parent
, p
);
214 rb_insert_color(&sv
->rb
, &si
->volumes
);
216 dbg_bld("added volume %d", vol_id
);
221 * compare_lebs - find out which logical eraseblock is newer.
222 * @ubi: UBI device description object
223 * @seb: first logical eraseblock to compare
224 * @pnum: physical eraseblock number of the second logical eraseblock to
226 * @vid_hdr: volume identifier header of the second logical eraseblock
228 * This function compares 2 copies of a LEB and informs which one is newer. In
229 * case of success this function returns a positive value, in case of failure, a
230 * negative error code is returned. The success return codes use the following
232 * o bit 0 is cleared: the first PEB (described by @seb) is newer then the
233 * second PEB (described by @pnum and @vid_hdr);
234 * o bit 0 is set: the second PEB is newer;
235 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
236 * o bit 1 is set: bit-flips were detected in the newer LEB;
237 * o bit 2 is cleared: the older LEB is not corrupted;
238 * o bit 2 is set: the older LEB is corrupted.
240 static int compare_lebs(struct ubi_device
*ubi
, const struct ubi_scan_leb
*seb
,
241 int pnum
, const struct ubi_vid_hdr
*vid_hdr
)
244 int len
, err
, second_is_newer
, bitflips
= 0, corrupted
= 0;
245 uint32_t data_crc
, crc
;
246 struct ubi_vid_hdr
*vh
= NULL
;
247 unsigned long long sqnum2
= be64_to_cpu(vid_hdr
->sqnum
);
249 if (sqnum2
== seb
->sqnum
) {
251 * This must be a really ancient UBI image which has been
252 * created before sequence numbers support has been added. At
253 * that times we used 32-bit LEB versions stored in logical
254 * eraseblocks. That was before UBI got into mainline. We do not
255 * support these images anymore. Well, those images will work
256 * still work, but only if no unclean reboots happened.
258 ubi_err("unsupported on-flash UBI format\n");
262 /* Obviously the LEB with lower sequence counter is older */
263 second_is_newer
= !!(sqnum2
> seb
->sqnum
);
266 * Now we know which copy is newer. If the copy flag of the PEB with
267 * newer version is not set, then we just return, otherwise we have to
268 * check data CRC. For the second PEB we already have the VID header,
269 * for the first one - we'll need to re-read it from flash.
271 * Note: this may be optimized so that we wouldn't read twice.
274 if (second_is_newer
) {
275 if (!vid_hdr
->copy_flag
) {
276 /* It is not a copy, so it is newer */
277 dbg_bld("second PEB %d is newer, copy_flag is unset",
284 vh
= ubi_zalloc_vid_hdr(ubi
, GFP_KERNEL
);
288 err
= ubi_io_read_vid_hdr(ubi
, pnum
, vh
, 0);
290 if (err
== UBI_IO_BITFLIPS
)
293 dbg_err("VID of PEB %d header is bad, but it "
294 "was OK earlier", pnum
);
302 if (!vh
->copy_flag
) {
303 /* It is not a copy, so it is newer */
304 dbg_bld("first PEB %d is newer, copy_flag is unset",
313 /* Read the data of the copy and check the CRC */
315 len
= be32_to_cpu(vid_hdr
->data_size
);
322 err
= ubi_io_read_data(ubi
, buf
, pnum
, 0, len
);
323 if (err
&& err
!= UBI_IO_BITFLIPS
&& err
!= -EBADMSG
)
326 data_crc
= be32_to_cpu(vid_hdr
->data_crc
);
327 crc
= crc32(UBI_CRC32_INIT
, buf
, len
);
328 if (crc
!= data_crc
) {
329 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
330 pnum
, crc
, data_crc
);
333 second_is_newer
= !second_is_newer
;
335 dbg_bld("PEB %d CRC is OK", pnum
);
340 ubi_free_vid_hdr(ubi
, vh
);
343 dbg_bld("second PEB %d is newer, copy_flag is set", pnum
);
345 dbg_bld("first PEB %d is newer, copy_flag is set", pnum
);
347 return second_is_newer
| (bitflips
<< 1) | (corrupted
<< 2);
352 ubi_free_vid_hdr(ubi
, vh
);
357 * ubi_scan_add_used - add physical eraseblock to the scanning information.
358 * @ubi: UBI device description object
359 * @si: scanning information
360 * @pnum: the physical eraseblock number
362 * @vid_hdr: the volume identifier header
363 * @bitflips: if bit-flips were detected when this physical eraseblock was read
365 * This function adds information about a used physical eraseblock to the
366 * 'used' tree of the corresponding volume. The function is rather complex
367 * because it has to handle cases when this is not the first physical
368 * eraseblock belonging to the same logical eraseblock, and the newer one has
369 * to be picked, while the older one has to be dropped. This function returns
370 * zero in case of success and a negative error code in case of failure.
372 int ubi_scan_add_used(struct ubi_device
*ubi
, struct ubi_scan_info
*si
,
373 int pnum
, int ec
, const struct ubi_vid_hdr
*vid_hdr
,
376 int err
, vol_id
, lnum
;
377 unsigned long long sqnum
;
378 struct ubi_scan_volume
*sv
;
379 struct ubi_scan_leb
*seb
;
380 struct rb_node
**p
, *parent
= NULL
;
382 vol_id
= be32_to_cpu(vid_hdr
->vol_id
);
383 lnum
= be32_to_cpu(vid_hdr
->lnum
);
384 sqnum
= be64_to_cpu(vid_hdr
->sqnum
);
386 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
387 pnum
, vol_id
, lnum
, ec
, sqnum
, bitflips
);
389 sv
= add_volume(si
, vol_id
, pnum
, vid_hdr
);
393 if (si
->max_sqnum
< sqnum
)
394 si
->max_sqnum
= sqnum
;
397 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
398 * if this is the first instance of this logical eraseblock or not.
400 p
= &sv
->root
.rb_node
;
405 seb
= rb_entry(parent
, struct ubi_scan_leb
, u
.rb
);
406 if (lnum
!= seb
->lnum
) {
407 if (lnum
< seb
->lnum
)
415 * There is already a physical eraseblock describing the same
416 * logical eraseblock present.
419 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
420 "EC %d", seb
->pnum
, seb
->sqnum
, seb
->ec
);
423 * Make sure that the logical eraseblocks have different
424 * sequence numbers. Otherwise the image is bad.
426 * However, if the sequence number is zero, we assume it must
427 * be an ancient UBI image from the era when UBI did not have
428 * sequence numbers. We still can attach these images, unless
429 * there is a need to distinguish between old and new
430 * eraseblocks, in which case we'll refuse the image in
431 * 'compare_lebs()'. In other words, we attach old clean
432 * images, but refuse attaching old images with duplicated
433 * logical eraseblocks because there was an unclean reboot.
435 if (seb
->sqnum
== sqnum
&& sqnum
!= 0) {
436 ubi_err("two LEBs with same sequence number %llu",
438 ubi_dbg_dump_seb(seb
, 0);
439 ubi_dbg_dump_vid_hdr(vid_hdr
);
444 * Now we have to drop the older one and preserve the newer
447 cmp_res
= compare_lebs(ubi
, seb
, pnum
, vid_hdr
);
453 * This logical eraseblock is newer then the one
456 err
= validate_vid_hdr(vid_hdr
, sv
, pnum
);
461 err
= add_to_list(si
, seb
->pnum
, seb
->ec
,
464 err
= add_to_list(si
, seb
->pnum
, seb
->ec
,
471 seb
->scrub
= ((cmp_res
& 2) || bitflips
);
474 if (sv
->highest_lnum
== lnum
)
476 be32_to_cpu(vid_hdr
->data_size
);
481 * This logical eraseblock is older than the one found
485 return add_to_list(si
, pnum
, ec
, &si
->corr
);
487 return add_to_list(si
, pnum
, ec
, &si
->erase
);
492 * We've met this logical eraseblock for the first time, add it to the
493 * scanning information.
496 err
= validate_vid_hdr(vid_hdr
, sv
, pnum
);
500 seb
= kmalloc(sizeof(struct ubi_scan_leb
), GFP_KERNEL
);
508 seb
->scrub
= bitflips
;
510 if (sv
->highest_lnum
<= lnum
) {
511 sv
->highest_lnum
= lnum
;
512 sv
->last_data_size
= be32_to_cpu(vid_hdr
->data_size
);
516 rb_link_node(&seb
->u
.rb
, parent
, p
);
517 rb_insert_color(&seb
->u
.rb
, &sv
->root
);
522 * ubi_scan_find_sv - find volume in the scanning information.
523 * @si: scanning information
524 * @vol_id: the requested volume ID
526 * This function returns a pointer to the volume description or %NULL if there
527 * are no data about this volume in the scanning information.
529 struct ubi_scan_volume
*ubi_scan_find_sv(const struct ubi_scan_info
*si
,
532 struct ubi_scan_volume
*sv
;
533 struct rb_node
*p
= si
->volumes
.rb_node
;
536 sv
= rb_entry(p
, struct ubi_scan_volume
, rb
);
538 if (vol_id
== sv
->vol_id
)
541 if (vol_id
> sv
->vol_id
)
551 * ubi_scan_find_seb - find LEB in the volume scanning information.
552 * @sv: a pointer to the volume scanning information
553 * @lnum: the requested logical eraseblock
555 * This function returns a pointer to the scanning logical eraseblock or %NULL
556 * if there are no data about it in the scanning volume information.
558 struct ubi_scan_leb
*ubi_scan_find_seb(const struct ubi_scan_volume
*sv
,
561 struct ubi_scan_leb
*seb
;
562 struct rb_node
*p
= sv
->root
.rb_node
;
565 seb
= rb_entry(p
, struct ubi_scan_leb
, u
.rb
);
567 if (lnum
== seb
->lnum
)
570 if (lnum
> seb
->lnum
)
580 * ubi_scan_rm_volume - delete scanning information about a volume.
581 * @si: scanning information
582 * @sv: the volume scanning information to delete
584 void ubi_scan_rm_volume(struct ubi_scan_info
*si
, struct ubi_scan_volume
*sv
)
587 struct ubi_scan_leb
*seb
;
589 dbg_bld("remove scanning information about volume %d", sv
->vol_id
);
591 while ((rb
= rb_first(&sv
->root
))) {
592 seb
= rb_entry(rb
, struct ubi_scan_leb
, u
.rb
);
593 rb_erase(&seb
->u
.rb
, &sv
->root
);
594 list_add_tail(&seb
->u
.list
, &si
->erase
);
597 rb_erase(&sv
->rb
, &si
->volumes
);
603 * ubi_scan_erase_peb - erase a physical eraseblock.
604 * @ubi: UBI device description object
605 * @si: scanning information
606 * @pnum: physical eraseblock number to erase;
607 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
609 * This function erases physical eraseblock 'pnum', and writes the erase
610 * counter header to it. This function should only be used on UBI device
611 * initialization stages, when the EBA sub-system had not been yet initialized.
612 * This function returns zero in case of success and a negative error code in
615 int ubi_scan_erase_peb(struct ubi_device
*ubi
, const struct ubi_scan_info
*si
,
619 struct ubi_ec_hdr
*ec_hdr
;
621 if ((long long)ec
>= UBI_MAX_ERASECOUNTER
) {
623 * Erase counter overflow. Upgrade UBI and use 64-bit
624 * erase counters internally.
626 ubi_err("erase counter overflow at PEB %d, EC %d", pnum
, ec
);
630 ec_hdr
= kzalloc(ubi
->ec_hdr_alsize
, GFP_KERNEL
);
634 ec_hdr
->ec
= cpu_to_be64(ec
);
636 err
= ubi_io_sync_erase(ubi
, pnum
, 0);
640 err
= ubi_io_write_ec_hdr(ubi
, pnum
, ec_hdr
);
648 * ubi_scan_get_free_peb - get a free physical eraseblock.
649 * @ubi: UBI device description object
650 * @si: scanning information
652 * This function returns a free physical eraseblock. It is supposed to be
653 * called on the UBI initialization stages when the wear-leveling sub-system is
654 * not initialized yet. This function picks a physical eraseblocks from one of
655 * the lists, writes the EC header if it is needed, and removes it from the
658 * This function returns scanning physical eraseblock information in case of
659 * success and an error code in case of failure.
661 struct ubi_scan_leb
*ubi_scan_get_free_peb(struct ubi_device
*ubi
,
662 struct ubi_scan_info
*si
)
665 struct ubi_scan_leb
*seb
;
667 if (!list_empty(&si
->free
)) {
668 seb
= list_entry(si
->free
.next
, struct ubi_scan_leb
, u
.list
);
669 list_del(&seb
->u
.list
);
670 dbg_bld("return free PEB %d, EC %d", seb
->pnum
, seb
->ec
);
674 for (i
= 0; i
< 2; i
++) {
675 struct list_head
*head
;
676 struct ubi_scan_leb
*tmp_seb
;
684 * We try to erase the first physical eraseblock from the @head
685 * list and pick it if we succeed, or try to erase the
686 * next one if not. And so forth. We don't want to take care
687 * about bad eraseblocks here - they'll be handled later.
689 list_for_each_entry_safe(seb
, tmp_seb
, head
, u
.list
) {
690 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
691 seb
->ec
= si
->mean_ec
;
693 err
= ubi_scan_erase_peb(ubi
, si
, seb
->pnum
, seb
->ec
+1);
698 list_del(&seb
->u
.list
);
699 dbg_bld("return PEB %d, EC %d", seb
->pnum
, seb
->ec
);
704 ubi_err("no eraseblocks found");
705 return ERR_PTR(-ENOSPC
);
709 * process_eb - read, check UBI headers, and add them to scanning information.
710 * @ubi: UBI device description object
711 * @si: scanning information
712 * @pnum: the physical eraseblock number
714 * This function returns a zero if the physical eraseblock was successfully
715 * handled and a negative error code in case of failure.
717 static int process_eb(struct ubi_device
*ubi
, struct ubi_scan_info
*si
,
720 long long uninitialized_var(ec
);
721 int err
, bitflips
= 0, vol_id
, ec_corr
= 0;
723 dbg_bld("scan PEB %d", pnum
);
725 /* Skip bad physical eraseblocks */
726 err
= ubi_io_is_bad(ubi
, pnum
);
731 * FIXME: this is actually duty of the I/O sub-system to
732 * initialize this, but MTD does not provide enough
735 si
->bad_peb_count
+= 1;
739 err
= ubi_io_read_ec_hdr(ubi
, pnum
, ech
, 0);
742 else if (err
== UBI_IO_BITFLIPS
)
744 else if (err
== UBI_IO_PEB_EMPTY
)
745 return add_to_list(si
, pnum
, UBI_SCAN_UNKNOWN_EC
, &si
->erase
);
746 else if (err
== UBI_IO_BAD_EC_HDR
) {
748 * We have to also look at the VID header, possibly it is not
749 * corrupted. Set %bitflips flag in order to make this PEB be
750 * moved and EC be re-created.
753 ec
= UBI_SCAN_UNKNOWN_EC
;
762 /* Make sure UBI version is OK */
763 if (ech
->version
!= UBI_VERSION
) {
764 ubi_err("this UBI version is %d, image version is %d",
765 UBI_VERSION
, (int)ech
->version
);
769 ec
= be64_to_cpu(ech
->ec
);
770 if (ec
> UBI_MAX_ERASECOUNTER
) {
772 * Erase counter overflow. The EC headers have 64 bits
773 * reserved, but we anyway make use of only 31 bit
774 * values, as this seems to be enough for any existing
775 * flash. Upgrade UBI and use 64-bit erase counters
778 ubi_err("erase counter overflow, max is %d",
779 UBI_MAX_ERASECOUNTER
);
780 ubi_dbg_dump_ec_hdr(ech
);
784 image_seq
= be32_to_cpu(ech
->image_seq
);
785 if (!si
->image_seq_set
) {
786 ubi
->image_seq
= image_seq
;
787 si
->image_seq_set
= 1;
788 } else if (ubi
->image_seq
!= image_seq
) {
789 ubi_err("bad image sequence number %d in PEB %d, "
790 "expected %d", image_seq
, pnum
, ubi
->image_seq
);
791 ubi_dbg_dump_ec_hdr(ech
);
797 /* OK, we've done with the EC header, let's look at the VID header */
799 err
= ubi_io_read_vid_hdr(ubi
, pnum
, vidh
, 0);
802 else if (err
== UBI_IO_BITFLIPS
)
804 else if (err
== UBI_IO_BAD_VID_HDR
||
805 (err
== UBI_IO_PEB_FREE
&& ec_corr
)) {
806 /* VID header is corrupted */
807 err
= add_to_list(si
, pnum
, ec
, &si
->corr
);
811 } else if (err
== UBI_IO_PEB_FREE
) {
812 /* No VID header - the physical eraseblock is free */
813 err
= add_to_list(si
, pnum
, ec
, &si
->free
);
819 vol_id
= be32_to_cpu(vidh
->vol_id
);
820 if (vol_id
> UBI_MAX_VOLUMES
&& vol_id
!= UBI_LAYOUT_VOLUME_ID
) {
821 int lnum
= be32_to_cpu(vidh
->lnum
);
823 /* Unsupported internal volume */
824 switch (vidh
->compat
) {
825 case UBI_COMPAT_DELETE
:
826 ubi_msg("\"delete\" compatible internal volume %d:%d"
827 " found, remove it", vol_id
, lnum
);
828 err
= add_to_list(si
, pnum
, ec
, &si
->corr
);
834 ubi_msg("read-only compatible internal volume %d:%d"
835 " found, switch to read-only mode",
840 case UBI_COMPAT_PRESERVE
:
841 ubi_msg("\"preserve\" compatible internal volume %d:%d"
842 " found", vol_id
, lnum
);
843 err
= add_to_list(si
, pnum
, ec
, &si
->alien
);
846 si
->alien_peb_count
+= 1;
849 case UBI_COMPAT_REJECT
:
850 ubi_err("incompatible internal volume %d:%d found",
856 /* Both UBI headers seem to be fine */
857 err
= ubi_scan_add_used(ubi
, si
, pnum
, ec
, vidh
, bitflips
);
875 * ubi_scan - scan an MTD device.
876 * @ubi: UBI device description object
878 * This function does full scanning of an MTD device and returns complete
879 * information about it. In case of failure, an error code is returned.
881 struct ubi_scan_info
*ubi_scan(struct ubi_device
*ubi
)
884 struct rb_node
*rb1
, *rb2
;
885 struct ubi_scan_volume
*sv
;
886 struct ubi_scan_leb
*seb
;
887 struct ubi_scan_info
*si
;
889 si
= kzalloc(sizeof(struct ubi_scan_info
), GFP_KERNEL
);
891 return ERR_PTR(-ENOMEM
);
893 INIT_LIST_HEAD(&si
->corr
);
894 INIT_LIST_HEAD(&si
->free
);
895 INIT_LIST_HEAD(&si
->erase
);
896 INIT_LIST_HEAD(&si
->alien
);
897 si
->volumes
= RB_ROOT
;
901 ech
= kzalloc(ubi
->ec_hdr_alsize
, GFP_KERNEL
);
905 vidh
= ubi_zalloc_vid_hdr(ubi
, GFP_KERNEL
);
909 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++) {
912 dbg_gen("process PEB %d", pnum
);
913 err
= process_eb(ubi
, si
, pnum
);
918 dbg_msg("scanning is finished");
920 /* Calculate mean erase counter */
922 si
->mean_ec
= div_u64(si
->ec_sum
, si
->ec_count
);
925 ubi_msg("empty MTD device detected");
928 * In case of unknown erase counter we use the mean erase counter
931 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
932 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
)
933 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
934 seb
->ec
= si
->mean_ec
;
937 list_for_each_entry(seb
, &si
->free
, u
.list
) {
938 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
939 seb
->ec
= si
->mean_ec
;
942 list_for_each_entry(seb
, &si
->corr
, u
.list
)
943 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
944 seb
->ec
= si
->mean_ec
;
946 list_for_each_entry(seb
, &si
->erase
, u
.list
)
947 if (seb
->ec
== UBI_SCAN_UNKNOWN_EC
)
948 seb
->ec
= si
->mean_ec
;
950 err
= paranoid_check_si(ubi
, si
);
957 ubi_free_vid_hdr(ubi
, vidh
);
963 ubi_free_vid_hdr(ubi
, vidh
);
967 ubi_scan_destroy_si(si
);
972 * destroy_sv - free the scanning volume information
973 * @sv: scanning volume information
975 * This function destroys the volume RB-tree (@sv->root) and the scanning
976 * volume information.
978 static void destroy_sv(struct ubi_scan_volume
*sv
)
980 struct ubi_scan_leb
*seb
;
981 struct rb_node
*this = sv
->root
.rb_node
;
985 this = this->rb_left
;
986 else if (this->rb_right
)
987 this = this->rb_right
;
989 seb
= rb_entry(this, struct ubi_scan_leb
, u
.rb
);
990 this = rb_parent(this);
992 if (this->rb_left
== &seb
->u
.rb
)
993 this->rb_left
= NULL
;
995 this->rb_right
= NULL
;
1005 * ubi_scan_destroy_si - destroy scanning information.
1006 * @si: scanning information
1008 void ubi_scan_destroy_si(struct ubi_scan_info
*si
)
1010 struct ubi_scan_leb
*seb
, *seb_tmp
;
1011 struct ubi_scan_volume
*sv
;
1014 list_for_each_entry_safe(seb
, seb_tmp
, &si
->alien
, u
.list
) {
1015 list_del(&seb
->u
.list
);
1018 list_for_each_entry_safe(seb
, seb_tmp
, &si
->erase
, u
.list
) {
1019 list_del(&seb
->u
.list
);
1022 list_for_each_entry_safe(seb
, seb_tmp
, &si
->corr
, u
.list
) {
1023 list_del(&seb
->u
.list
);
1026 list_for_each_entry_safe(seb
, seb_tmp
, &si
->free
, u
.list
) {
1027 list_del(&seb
->u
.list
);
1031 /* Destroy the volume RB-tree */
1032 rb
= si
->volumes
.rb_node
;
1036 else if (rb
->rb_right
)
1039 sv
= rb_entry(rb
, struct ubi_scan_volume
, rb
);
1043 if (rb
->rb_left
== &sv
->rb
)
1046 rb
->rb_right
= NULL
;
1056 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1059 * paranoid_check_si - check the scanning information.
1060 * @ubi: UBI device description object
1061 * @si: scanning information
1063 * This function returns zero if the scanning information is all right, %1 if
1064 * not and a negative error code if an error occurred.
1066 static int paranoid_check_si(struct ubi_device
*ubi
, struct ubi_scan_info
*si
)
1068 int pnum
, err
, vols_found
= 0;
1069 struct rb_node
*rb1
, *rb2
;
1070 struct ubi_scan_volume
*sv
;
1071 struct ubi_scan_leb
*seb
, *last_seb
;
1075 * At first, check that scanning information is OK.
1077 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
1085 ubi_err("bad is_empty flag");
1089 if (sv
->vol_id
< 0 || sv
->highest_lnum
< 0 ||
1090 sv
->leb_count
< 0 || sv
->vol_type
< 0 || sv
->used_ebs
< 0 ||
1091 sv
->data_pad
< 0 || sv
->last_data_size
< 0) {
1092 ubi_err("negative values");
1096 if (sv
->vol_id
>= UBI_MAX_VOLUMES
&&
1097 sv
->vol_id
< UBI_INTERNAL_VOL_START
) {
1098 ubi_err("bad vol_id");
1102 if (sv
->vol_id
> si
->highest_vol_id
) {
1103 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1104 si
->highest_vol_id
, sv
->vol_id
);
1108 if (sv
->vol_type
!= UBI_DYNAMIC_VOLUME
&&
1109 sv
->vol_type
!= UBI_STATIC_VOLUME
) {
1110 ubi_err("bad vol_type");
1114 if (sv
->data_pad
> ubi
->leb_size
/ 2) {
1115 ubi_err("bad data_pad");
1120 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
) {
1126 if (seb
->pnum
< 0 || seb
->ec
< 0) {
1127 ubi_err("negative values");
1131 if (seb
->ec
< si
->min_ec
) {
1132 ubi_err("bad si->min_ec (%d), %d found",
1133 si
->min_ec
, seb
->ec
);
1137 if (seb
->ec
> si
->max_ec
) {
1138 ubi_err("bad si->max_ec (%d), %d found",
1139 si
->max_ec
, seb
->ec
);
1143 if (seb
->pnum
>= ubi
->peb_count
) {
1144 ubi_err("too high PEB number %d, total PEBs %d",
1145 seb
->pnum
, ubi
->peb_count
);
1149 if (sv
->vol_type
== UBI_STATIC_VOLUME
) {
1150 if (seb
->lnum
>= sv
->used_ebs
) {
1151 ubi_err("bad lnum or used_ebs");
1155 if (sv
->used_ebs
!= 0) {
1156 ubi_err("non-zero used_ebs");
1161 if (seb
->lnum
> sv
->highest_lnum
) {
1162 ubi_err("incorrect highest_lnum or lnum");
1167 if (sv
->leb_count
!= leb_count
) {
1168 ubi_err("bad leb_count, %d objects in the tree",
1178 if (seb
->lnum
!= sv
->highest_lnum
) {
1179 ubi_err("bad highest_lnum");
1184 if (vols_found
!= si
->vols_found
) {
1185 ubi_err("bad si->vols_found %d, should be %d",
1186 si
->vols_found
, vols_found
);
1190 /* Check that scanning information is correct */
1191 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
) {
1193 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
) {
1200 err
= ubi_io_read_vid_hdr(ubi
, seb
->pnum
, vidh
, 1);
1201 if (err
&& err
!= UBI_IO_BITFLIPS
) {
1202 ubi_err("VID header is not OK (%d)", err
);
1208 vol_type
= vidh
->vol_type
== UBI_VID_DYNAMIC
?
1209 UBI_DYNAMIC_VOLUME
: UBI_STATIC_VOLUME
;
1210 if (sv
->vol_type
!= vol_type
) {
1211 ubi_err("bad vol_type");
1215 if (seb
->sqnum
!= be64_to_cpu(vidh
->sqnum
)) {
1216 ubi_err("bad sqnum %llu", seb
->sqnum
);
1220 if (sv
->vol_id
!= be32_to_cpu(vidh
->vol_id
)) {
1221 ubi_err("bad vol_id %d", sv
->vol_id
);
1225 if (sv
->compat
!= vidh
->compat
) {
1226 ubi_err("bad compat %d", vidh
->compat
);
1230 if (seb
->lnum
!= be32_to_cpu(vidh
->lnum
)) {
1231 ubi_err("bad lnum %d", seb
->lnum
);
1235 if (sv
->used_ebs
!= be32_to_cpu(vidh
->used_ebs
)) {
1236 ubi_err("bad used_ebs %d", sv
->used_ebs
);
1240 if (sv
->data_pad
!= be32_to_cpu(vidh
->data_pad
)) {
1241 ubi_err("bad data_pad %d", sv
->data_pad
);
1249 if (sv
->highest_lnum
!= be32_to_cpu(vidh
->lnum
)) {
1250 ubi_err("bad highest_lnum %d", sv
->highest_lnum
);
1254 if (sv
->last_data_size
!= be32_to_cpu(vidh
->data_size
)) {
1255 ubi_err("bad last_data_size %d", sv
->last_data_size
);
1261 * Make sure that all the physical eraseblocks are in one of the lists
1264 buf
= kzalloc(ubi
->peb_count
, GFP_KERNEL
);
1268 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++) {
1269 err
= ubi_io_is_bad(ubi
, pnum
);
1277 ubi_rb_for_each_entry(rb1
, sv
, &si
->volumes
, rb
)
1278 ubi_rb_for_each_entry(rb2
, seb
, &sv
->root
, u
.rb
)
1281 list_for_each_entry(seb
, &si
->free
, u
.list
)
1284 list_for_each_entry(seb
, &si
->corr
, u
.list
)
1287 list_for_each_entry(seb
, &si
->erase
, u
.list
)
1290 list_for_each_entry(seb
, &si
->alien
, u
.list
)
1294 for (pnum
= 0; pnum
< ubi
->peb_count
; pnum
++)
1296 ubi_err("PEB %d is not referred", pnum
);
1306 ubi_err("bad scanning information about LEB %d", seb
->lnum
);
1307 ubi_dbg_dump_seb(seb
, 0);
1308 ubi_dbg_dump_sv(sv
);
1312 ubi_err("bad scanning information about volume %d", sv
->vol_id
);
1313 ubi_dbg_dump_sv(sv
);
1317 ubi_err("bad scanning information about volume %d", sv
->vol_id
);
1318 ubi_dbg_dump_sv(sv
);
1319 ubi_dbg_dump_vid_hdr(vidh
);
1322 ubi_dbg_dump_stack();
1326 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */