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 are supposed to handle these situations. UBI puts these PEBs to
49 * the head of the @erase list and they are scheduled for erasure.
51 * 2. Unexpected corruptions which are not caused by power cuts. During
52 * scanning, such PEBs are put to the @corr list and UBI preserves them.
53 * Obviously, this lessens the amount of available PEBs, and if at some
54 * point UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly
55 * informs about such PEBs every time the MTD device is attached.
58 #include <linux/err.h>
59 #include <linux/slab.h>
60 #include <linux/crc32.h>
61 #include <linux/math64.h>
62 #include <linux/random.h>
65 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
66 static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
68 #define paranoid_check_si(ubi, si) 0
71 /* Temporary variables used during scanning */
72 static struct ubi_ec_hdr *ech;
73 static struct ubi_vid_hdr *vidh;
76 * add_to_list - add physical eraseblock to a list.
77 * @si: scanning information
78 * @pnum: physical eraseblock number to add
79 * @ec: erase counter of the physical eraseblock
80 * @to_head: if not zero, add to the head of the list
81 * @list: the list to add to
83 * This function adds physical eraseblock @pnum to free, erase, or alien lists.
84 * If @to_head is not zero, PEB will be added to the head of the list, which
85 * basically means it will be processed first later. E.g., we add corrupted
86 * PEBs (corrupted due to power cuts) to the head of the erase list to make
87 * sure we erase them first and get rid of corruptions ASAP. This function
88 * returns zero in case of success and a negative error code in case of
91 static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, int to_head,
92 struct list_head *list)
94 struct ubi_scan_leb *seb;
96 if (list == &si->free) {
97 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
98 } else if (list == &si->erase) {
99 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
100 } else if (list == &si->alien) {
101 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
102 si->alien_peb_count += 1;
106 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
113 list_add(&seb->u.list, list);
115 list_add_tail(&seb->u.list, list);
120 * add_corrupted - add a corrupted physical eraseblock.
121 * @si: scanning information
122 * @pnum: physical eraseblock number to add
123 * @ec: erase counter of the physical eraseblock
125 * This function adds corrupted physical eraseblock @pnum to the 'corr' list.
126 * The corruption was presumably not caused by a power cut. Returns zero in
127 * case of success and a negative error code in case of failure.
129 static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)
131 struct ubi_scan_leb *seb;
133 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
135 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
139 si->corr_peb_count += 1;
142 list_add(&seb->u.list, &si->corr);
147 * validate_vid_hdr - check volume identifier header.
148 * @vid_hdr: the volume identifier header to check
149 * @sv: information about the volume this logical eraseblock belongs to
150 * @pnum: physical eraseblock number the VID header came from
152 * This function checks that data stored in @vid_hdr is consistent. Returns
153 * non-zero if an inconsistency was found and zero if not.
155 * Note, UBI does sanity check of everything it reads from the flash media.
156 * Most of the checks are done in the I/O sub-system. Here we check that the
157 * information in the VID header is consistent to the information in other VID
158 * headers of the same volume.
160 static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
161 const struct ubi_scan_volume *sv, int pnum)
163 int vol_type = vid_hdr->vol_type;
164 int vol_id = be32_to_cpu(vid_hdr->vol_id);
165 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
166 int data_pad = be32_to_cpu(vid_hdr->data_pad);
168 if (sv->leb_count != 0) {
172 * This is not the first logical eraseblock belonging to this
173 * volume. Ensure that the data in its VID header is consistent
174 * to the data in previous logical eraseblock headers.
177 if (vol_id != sv->vol_id) {
178 dbg_err("inconsistent vol_id");
182 if (sv->vol_type == UBI_STATIC_VOLUME)
183 sv_vol_type = UBI_VID_STATIC;
185 sv_vol_type = UBI_VID_DYNAMIC;
187 if (vol_type != sv_vol_type) {
188 dbg_err("inconsistent vol_type");
192 if (used_ebs != sv->used_ebs) {
193 dbg_err("inconsistent used_ebs");
197 if (data_pad != sv->data_pad) {
198 dbg_err("inconsistent data_pad");
206 ubi_err("inconsistent VID header at PEB %d", pnum);
207 ubi_dbg_dump_vid_hdr(vid_hdr);
213 * add_volume - add volume to the scanning information.
214 * @si: scanning information
215 * @vol_id: ID of the volume to add
216 * @pnum: physical eraseblock number
217 * @vid_hdr: volume identifier header
219 * If the volume corresponding to the @vid_hdr logical eraseblock is already
220 * present in the scanning information, this function does nothing. Otherwise
221 * it adds corresponding volume to the scanning information. Returns a pointer
222 * to the scanning volume object in case of success and a negative error code
223 * in case of failure.
225 static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
227 const struct ubi_vid_hdr *vid_hdr)
229 struct ubi_scan_volume *sv;
230 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
232 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
234 /* Walk the volume RB-tree to look if this volume is already present */
237 sv = rb_entry(parent, struct ubi_scan_volume, rb);
239 if (vol_id == sv->vol_id)
242 if (vol_id > sv->vol_id)
248 /* The volume is absent - add it */
249 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
251 return ERR_PTR(-ENOMEM);
253 sv->highest_lnum = sv->leb_count = 0;
256 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
257 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
258 sv->compat = vid_hdr->compat;
259 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
261 if (vol_id > si->highest_vol_id)
262 si->highest_vol_id = vol_id;
264 rb_link_node(&sv->rb, parent, p);
265 rb_insert_color(&sv->rb, &si->volumes);
267 dbg_bld("added volume %d", vol_id);
272 * compare_lebs - find out which logical eraseblock is newer.
273 * @ubi: UBI device description object
274 * @seb: first logical eraseblock to compare
275 * @pnum: physical eraseblock number of the second logical eraseblock to
277 * @vid_hdr: volume identifier header of the second logical eraseblock
279 * This function compares 2 copies of a LEB and informs which one is newer. In
280 * case of success this function returns a positive value, in case of failure, a
281 * negative error code is returned. The success return codes use the following
283 * o bit 0 is cleared: the first PEB (described by @seb) is newer than the
284 * second PEB (described by @pnum and @vid_hdr);
285 * o bit 0 is set: the second PEB is newer;
286 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
287 * o bit 1 is set: bit-flips were detected in the newer LEB;
288 * o bit 2 is cleared: the older LEB is not corrupted;
289 * o bit 2 is set: the older LEB is corrupted.
291 static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
292 int pnum, const struct ubi_vid_hdr *vid_hdr)
295 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
296 uint32_t data_crc, crc;
297 struct ubi_vid_hdr *vh = NULL;
298 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
300 if (sqnum2 == seb->sqnum) {
302 * This must be a really ancient UBI image which has been
303 * created before sequence numbers support has been added. At
304 * that times we used 32-bit LEB versions stored in logical
305 * eraseblocks. That was before UBI got into mainline. We do not
306 * support these images anymore. Well, those images still work,
307 * but only if no unclean reboots happened.
309 ubi_err("unsupported on-flash UBI format\n");
313 /* Obviously the LEB with lower sequence counter is older */
314 second_is_newer = !!(sqnum2 > seb->sqnum);
317 * Now we know which copy is newer. If the copy flag of the PEB with
318 * newer version is not set, then we just return, otherwise we have to
319 * check data CRC. For the second PEB we already have the VID header,
320 * for the first one - we'll need to re-read it from flash.
322 * Note: this may be optimized so that we wouldn't read twice.
325 if (second_is_newer) {
326 if (!vid_hdr->copy_flag) {
327 /* It is not a copy, so it is newer */
328 dbg_bld("second PEB %d is newer, copy_flag is unset",
335 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
339 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
341 if (err == UBI_IO_BITFLIPS)
344 dbg_err("VID of PEB %d header is bad, but it "
345 "was OK earlier, err %d", pnum, err);
353 if (!vh->copy_flag) {
354 /* It is not a copy, so it is newer */
355 dbg_bld("first PEB %d is newer, copy_flag is unset",
364 /* Read the data of the copy and check the CRC */
366 len = be32_to_cpu(vid_hdr->data_size);
373 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
374 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
377 data_crc = be32_to_cpu(vid_hdr->data_crc);
378 crc = crc32(UBI_CRC32_INIT, buf, len);
379 if (crc != data_crc) {
380 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
381 pnum, crc, data_crc);
384 second_is_newer = !second_is_newer;
386 dbg_bld("PEB %d CRC is OK", pnum);
391 ubi_free_vid_hdr(ubi, vh);
394 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
396 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
398 return second_is_newer | (bitflips << 1) | (corrupted << 2);
403 ubi_free_vid_hdr(ubi, vh);
408 * ubi_scan_add_used - add physical eraseblock to the scanning information.
409 * @ubi: UBI device description object
410 * @si: scanning information
411 * @pnum: the physical eraseblock number
413 * @vid_hdr: the volume identifier header
414 * @bitflips: if bit-flips were detected when this physical eraseblock was read
416 * This function adds information about a used physical eraseblock to the
417 * 'used' tree of the corresponding volume. The function is rather complex
418 * because it has to handle cases when this is not the first physical
419 * eraseblock belonging to the same logical eraseblock, and the newer one has
420 * to be picked, while the older one has to be dropped. This function returns
421 * zero in case of success and a negative error code in case of failure.
423 int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
424 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
427 int err, vol_id, lnum;
428 unsigned long long sqnum;
429 struct ubi_scan_volume *sv;
430 struct ubi_scan_leb *seb;
431 struct rb_node **p, *parent = NULL;
433 vol_id = be32_to_cpu(vid_hdr->vol_id);
434 lnum = be32_to_cpu(vid_hdr->lnum);
435 sqnum = be64_to_cpu(vid_hdr->sqnum);
437 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
438 pnum, vol_id, lnum, ec, sqnum, bitflips);
440 sv = add_volume(si, vol_id, pnum, vid_hdr);
444 if (si->max_sqnum < sqnum)
445 si->max_sqnum = sqnum;
448 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
449 * if this is the first instance of this logical eraseblock or not.
451 p = &sv->root.rb_node;
456 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
457 if (lnum != seb->lnum) {
458 if (lnum < seb->lnum)
466 * There is already a physical eraseblock describing the same
467 * logical eraseblock present.
470 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
471 "EC %d", seb->pnum, seb->sqnum, seb->ec);
474 * Make sure that the logical eraseblocks have different
475 * sequence numbers. Otherwise the image is bad.
477 * However, if the sequence number is zero, we assume it must
478 * be an ancient UBI image from the era when UBI did not have
479 * sequence numbers. We still can attach these images, unless
480 * there is a need to distinguish between old and new
481 * eraseblocks, in which case we'll refuse the image in
482 * 'compare_lebs()'. In other words, we attach old clean
483 * images, but refuse attaching old images with duplicated
484 * logical eraseblocks because there was an unclean reboot.
486 if (seb->sqnum == sqnum && sqnum != 0) {
487 ubi_err("two LEBs with same sequence number %llu",
489 ubi_dbg_dump_seb(seb, 0);
490 ubi_dbg_dump_vid_hdr(vid_hdr);
495 * Now we have to drop the older one and preserve the newer
498 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
504 * This logical eraseblock is newer than the one
507 err = validate_vid_hdr(vid_hdr, sv, pnum);
511 err = add_to_list(si, seb->pnum, seb->ec, cmp_res & 4,
518 seb->scrub = ((cmp_res & 2) || bitflips);
521 if (sv->highest_lnum == lnum)
523 be32_to_cpu(vid_hdr->data_size);
528 * This logical eraseblock is older than the one found
531 return add_to_list(si, pnum, ec, cmp_res & 4,
537 * We've met this logical eraseblock for the first time, add it to the
538 * scanning information.
541 err = validate_vid_hdr(vid_hdr, sv, pnum);
545 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
553 seb->scrub = bitflips;
555 if (sv->highest_lnum <= lnum) {
556 sv->highest_lnum = lnum;
557 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
561 rb_link_node(&seb->u.rb, parent, p);
562 rb_insert_color(&seb->u.rb, &sv->root);
567 * ubi_scan_find_sv - find volume in the scanning information.
568 * @si: scanning information
569 * @vol_id: the requested volume ID
571 * This function returns a pointer to the volume description or %NULL if there
572 * are no data about this volume in the scanning information.
574 struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
577 struct ubi_scan_volume *sv;
578 struct rb_node *p = si->volumes.rb_node;
581 sv = rb_entry(p, struct ubi_scan_volume, rb);
583 if (vol_id == sv->vol_id)
586 if (vol_id > sv->vol_id)
596 * ubi_scan_find_seb - find LEB in the volume scanning information.
597 * @sv: a pointer to the volume scanning information
598 * @lnum: the requested logical eraseblock
600 * This function returns a pointer to the scanning logical eraseblock or %NULL
601 * if there are no data about it in the scanning volume information.
603 struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
606 struct ubi_scan_leb *seb;
607 struct rb_node *p = sv->root.rb_node;
610 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
612 if (lnum == seb->lnum)
615 if (lnum > seb->lnum)
625 * ubi_scan_rm_volume - delete scanning information about a volume.
626 * @si: scanning information
627 * @sv: the volume scanning information to delete
629 void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
632 struct ubi_scan_leb *seb;
634 dbg_bld("remove scanning information about volume %d", sv->vol_id);
636 while ((rb = rb_first(&sv->root))) {
637 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
638 rb_erase(&seb->u.rb, &sv->root);
639 list_add_tail(&seb->u.list, &si->erase);
642 rb_erase(&sv->rb, &si->volumes);
648 * ubi_scan_erase_peb - erase a physical eraseblock.
649 * @ubi: UBI device description object
650 * @si: scanning information
651 * @pnum: physical eraseblock number to erase;
652 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
654 * This function erases physical eraseblock 'pnum', and writes the erase
655 * counter header to it. This function should only be used on UBI device
656 * initialization stages, when the EBA sub-system had not been yet initialized.
657 * This function returns zero in case of success and a negative error code in
660 int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
664 struct ubi_ec_hdr *ec_hdr;
666 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
668 * Erase counter overflow. Upgrade UBI and use 64-bit
669 * erase counters internally.
671 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
675 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
679 ec_hdr->ec = cpu_to_be64(ec);
681 err = ubi_io_sync_erase(ubi, pnum, 0);
685 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
693 * ubi_scan_get_free_peb - get a free physical eraseblock.
694 * @ubi: UBI device description object
695 * @si: scanning information
697 * This function returns a free physical eraseblock. It is supposed to be
698 * called on the UBI initialization stages when the wear-leveling sub-system is
699 * not initialized yet. This function picks a physical eraseblocks from one of
700 * the lists, writes the EC header if it is needed, and removes it from the
703 * This function returns scanning physical eraseblock information in case of
704 * success and an error code in case of failure.
706 struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
707 struct ubi_scan_info *si)
710 struct ubi_scan_leb *seb;
712 if (!list_empty(&si->free)) {
713 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
714 list_del(&seb->u.list);
715 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
719 for (i = 0; i < 2; i++) {
720 struct list_head *head;
721 struct ubi_scan_leb *tmp_seb;
729 * We try to erase the first physical eraseblock from the @head
730 * list and pick it if we succeed, or try to erase the
731 * next one if not. And so forth. We don't want to take care
732 * about bad eraseblocks here - they'll be handled later.
734 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
735 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
736 seb->ec = si->mean_ec;
738 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
743 list_del(&seb->u.list);
744 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
749 ubi_err("no eraseblocks found");
750 return ERR_PTR(-ENOSPC);
754 * check_data_ff - make sure PEB contains only 0xFF data.
755 * @ubi: UBI device description object
756 * @vid_hrd: the (corrupted) VID header of this PEB
757 * @pnum: the physical eraseblock number to check
759 * This is a helper function which is used to distinguish between VID header
760 * corruptions caused by power cuts and other reasons. If the PEB contains only
761 * 0xFF bytes at the data area, the VID header is most probably corrupted
762 * because of a power cut (%0 is returned in this case). Otherwise, it was
763 * corrupted for some other reasons (%1 is returned in this case). A negative
764 * error code is returned if a read error occurred.
766 * If the corruption reason was a power cut, UBI can safely erase this PEB.
767 * Otherwise, it should preserve it to avoid possibly destroying important
770 static int check_data_ff(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
775 mutex_lock(&ubi->buf_mutex);
776 memset(ubi->peb_buf1, 0x00, ubi->leb_size);
778 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, ubi->leb_start,
780 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
783 if (ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->leb_size)) {
784 mutex_unlock(&ubi->buf_mutex);
788 ubi_err("PEB %d contains corrupted VID header, and the data does not "
789 "contain all 0xFF, this may be a non-UBI PEB or a severe VID "
790 "header corruption which requires manual inspection", pnum);
791 ubi_dbg_dump_vid_hdr(vid_hdr);
792 dbg_msg("hexdump of PEB %d offset %d, length %d",
793 pnum, ubi->leb_start, ubi->leb_size);
794 ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
795 ubi->peb_buf1, ubi->leb_size, 1);
796 mutex_unlock(&ubi->buf_mutex);
801 * process_eb - read, check UBI headers, and add them to scanning information.
802 * @ubi: UBI device description object
803 * @si: scanning information
804 * @pnum: the physical eraseblock number
806 * This function returns a zero if the physical eraseblock was successfully
807 * handled and a negative error code in case of failure.
809 static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
812 long long uninitialized_var(ec);
813 int err, bitflips = 0, vol_id, ec_err = 0;
815 dbg_bld("scan PEB %d", pnum);
817 /* Skip bad physical eraseblocks */
818 err = ubi_io_is_bad(ubi, pnum);
823 * FIXME: this is actually duty of the I/O sub-system to
824 * initialize this, but MTD does not provide enough
827 si->bad_peb_count += 1;
831 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
837 case UBI_IO_BITFLIPS:
841 si->empty_peb_count += 1;
842 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 0,
844 case UBI_IO_FF_BITFLIPS:
845 si->empty_peb_count += 1;
846 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 1,
848 case UBI_IO_BAD_HDR_EBADMSG:
851 * We have to also look at the VID header, possibly it is not
852 * corrupted. Set %bitflips flag in order to make this PEB be
853 * moved and EC be re-created.
856 ec = UBI_SCAN_UNKNOWN_EC;
860 ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
867 /* Make sure UBI version is OK */
868 if (ech->version != UBI_VERSION) {
869 ubi_err("this UBI version is %d, image version is %d",
870 UBI_VERSION, (int)ech->version);
874 ec = be64_to_cpu(ech->ec);
875 if (ec > UBI_MAX_ERASECOUNTER) {
877 * Erase counter overflow. The EC headers have 64 bits
878 * reserved, but we anyway make use of only 31 bit
879 * values, as this seems to be enough for any existing
880 * flash. Upgrade UBI and use 64-bit erase counters
883 ubi_err("erase counter overflow, max is %d",
884 UBI_MAX_ERASECOUNTER);
885 ubi_dbg_dump_ec_hdr(ech);
890 * Make sure that all PEBs have the same image sequence number.
891 * This allows us to detect situations when users flash UBI
892 * images incorrectly, so that the flash has the new UBI image
893 * and leftovers from the old one. This feature was added
894 * relatively recently, and the sequence number was always
895 * zero, because old UBI implementations always set it to zero.
896 * For this reasons, we do not panic if some PEBs have zero
897 * sequence number, while other PEBs have non-zero sequence
900 image_seq = be32_to_cpu(ech->image_seq);
901 if (!ubi->image_seq && image_seq)
902 ubi->image_seq = image_seq;
903 if (ubi->image_seq && image_seq &&
904 ubi->image_seq != image_seq) {
905 ubi_err("bad image sequence number %d in PEB %d, "
906 "expected %d", image_seq, pnum, ubi->image_seq);
907 ubi_dbg_dump_ec_hdr(ech);
912 /* OK, we've done with the EC header, let's look at the VID header */
914 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
920 case UBI_IO_BITFLIPS:
923 case UBI_IO_BAD_HDR_EBADMSG:
924 if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
926 * Both EC and VID headers are corrupted and were read
927 * with data integrity error, probably this is a bad
928 * PEB, bit it is not marked as bad yet. This may also
929 * be a result of power cut during erasure.
931 si->maybe_bad_peb_count += 1;
935 * Both headers are corrupted. There is a possibility
936 * that this a valid UBI PEB which has corresponding
937 * LEB, but the headers are corrupted. However, it is
938 * impossible to distinguish it from a PEB which just
939 * contains garbage because a power cut during erase
940 * operation. So we just schedule this PEB for erasure.
945 * The EC was OK, but the VID header is corrupted. We
946 * have to check what is in the data area.
948 err = check_data_ff(ubi, vidh, pnum);
950 /* This corruption is caused by a power cut */
951 err = add_to_list(si, pnum, ec, 1, &si->erase);
953 /* This is an unexpected corruption */
954 err = add_corrupted(si, pnum, ec);
958 case UBI_IO_FF_BITFLIPS:
959 err = add_to_list(si, pnum, ec, 1, &si->erase);
965 err = add_to_list(si, pnum, ec, 1, &si->erase);
967 err = add_to_list(si, pnum, ec, 0, &si->free);
972 ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
977 vol_id = be32_to_cpu(vidh->vol_id);
978 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
979 int lnum = be32_to_cpu(vidh->lnum);
981 /* Unsupported internal volume */
982 switch (vidh->compat) {
983 case UBI_COMPAT_DELETE:
984 ubi_msg("\"delete\" compatible internal volume %d:%d"
985 " found, will remove it", vol_id, lnum);
986 err = add_to_list(si, pnum, ec, 1, &si->erase);
992 ubi_msg("read-only compatible internal volume %d:%d"
993 " found, switch to read-only mode",
998 case UBI_COMPAT_PRESERVE:
999 ubi_msg("\"preserve\" compatible internal volume %d:%d"
1000 " found", vol_id, lnum);
1001 err = add_to_list(si, pnum, ec, 0, &si->alien);
1006 case UBI_COMPAT_REJECT:
1007 ubi_err("incompatible internal volume %d:%d found",
1014 ubi_warn("valid VID header but corrupted EC header at PEB %d",
1016 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
1024 if (ec > si->max_ec)
1026 if (ec < si->min_ec)
1034 * check_what_we_have - check what PEB were found by scanning.
1035 * @ubi: UBI device description object
1036 * @si: scanning information
1038 * This is a helper function which takes a look what PEBs were found by
1039 * scanning, and decides whether the flash is empty and should be formatted and
1040 * whether there are too many corrupted PEBs and we should not attach this
1041 * MTD device. Returns zero if we should proceed with attaching the MTD device,
1042 * and %-EINVAL if we should not.
1044 static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
1046 struct ubi_scan_leb *seb;
1047 int max_corr, peb_count;
1049 peb_count = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
1050 max_corr = peb_count / 20 ?: 8;
1053 * Few corrupted PEBs is not a problem and may be just a result of
1054 * unclean reboots. However, many of them may indicate some problems
1055 * with the flash HW or driver.
1057 if (si->corr_peb_count) {
1058 ubi_err("%d PEBs are corrupted and preserved",
1059 si->corr_peb_count);
1060 printk(KERN_ERR "Corrupted PEBs are:");
1061 list_for_each_entry(seb, &si->corr, u.list)
1062 printk(KERN_CONT " %d", seb->pnum);
1063 printk(KERN_CONT "\n");
1066 * If too many PEBs are corrupted, we refuse attaching,
1067 * otherwise, only print a warning.
1069 if (si->corr_peb_count >= max_corr) {
1070 ubi_err("too many corrupted PEBs, refusing this device");
1075 if (si->empty_peb_count + si->maybe_bad_peb_count == peb_count) {
1077 * All PEBs are empty, or almost all - a couple PEBs look like
1078 * they may be bad PEBs which were not marked as bad yet.
1080 * This piece of code basically tries to distinguish between
1081 * the following situations:
1083 * 1. Flash is empty, but there are few bad PEBs, which are not
1084 * marked as bad so far, and which were read with error. We
1085 * want to go ahead and format this flash. While formatting,
1086 * the faulty PEBs will probably be marked as bad.
1088 * 2. Flash contains non-UBI data and we do not want to format
1089 * it and destroy possibly important information.
1091 if (si->maybe_bad_peb_count <= 2) {
1093 ubi_msg("empty MTD device detected");
1094 get_random_bytes(&ubi->image_seq,
1095 sizeof(ubi->image_seq));
1097 ubi_err("MTD device is not UBI-formatted and possibly "
1098 "contains non-UBI data - refusing it");
1108 * ubi_scan - scan an MTD device.
1109 * @ubi: UBI device description object
1111 * This function does full scanning of an MTD device and returns complete
1112 * information about it. In case of failure, an error code is returned.
1114 struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
1117 struct rb_node *rb1, *rb2;
1118 struct ubi_scan_volume *sv;
1119 struct ubi_scan_leb *seb;
1120 struct ubi_scan_info *si;
1122 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
1124 return ERR_PTR(-ENOMEM);
1126 INIT_LIST_HEAD(&si->corr);
1127 INIT_LIST_HEAD(&si->free);
1128 INIT_LIST_HEAD(&si->erase);
1129 INIT_LIST_HEAD(&si->alien);
1130 si->volumes = RB_ROOT;
1133 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1137 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
1141 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1144 dbg_gen("process PEB %d", pnum);
1145 err = process_eb(ubi, si, pnum);
1150 dbg_msg("scanning is finished");
1152 /* Calculate mean erase counter */
1154 si->mean_ec = div_u64(si->ec_sum, si->ec_count);
1156 err = check_what_we_have(ubi, si);
1161 * In case of unknown erase counter we use the mean erase counter
1164 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1165 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1166 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1167 seb->ec = si->mean_ec;
1170 list_for_each_entry(seb, &si->free, u.list) {
1171 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1172 seb->ec = si->mean_ec;
1175 list_for_each_entry(seb, &si->corr, u.list)
1176 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1177 seb->ec = si->mean_ec;
1179 list_for_each_entry(seb, &si->erase, u.list)
1180 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1181 seb->ec = si->mean_ec;
1183 err = paranoid_check_si(ubi, si);
1187 ubi_free_vid_hdr(ubi, vidh);
1193 ubi_free_vid_hdr(ubi, vidh);
1197 ubi_scan_destroy_si(si);
1198 return ERR_PTR(err);
1202 * destroy_sv - free the scanning volume information
1203 * @sv: scanning volume information
1205 * This function destroys the volume RB-tree (@sv->root) and the scanning
1206 * volume information.
1208 static void destroy_sv(struct ubi_scan_volume *sv)
1210 struct ubi_scan_leb *seb;
1211 struct rb_node *this = sv->root.rb_node;
1215 this = this->rb_left;
1216 else if (this->rb_right)
1217 this = this->rb_right;
1219 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1220 this = rb_parent(this);
1222 if (this->rb_left == &seb->u.rb)
1223 this->rb_left = NULL;
1225 this->rb_right = NULL;
1235 * ubi_scan_destroy_si - destroy scanning information.
1236 * @si: scanning information
1238 void ubi_scan_destroy_si(struct ubi_scan_info *si)
1240 struct ubi_scan_leb *seb, *seb_tmp;
1241 struct ubi_scan_volume *sv;
1244 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1245 list_del(&seb->u.list);
1248 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1249 list_del(&seb->u.list);
1252 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1253 list_del(&seb->u.list);
1256 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1257 list_del(&seb->u.list);
1261 /* Destroy the volume RB-tree */
1262 rb = si->volumes.rb_node;
1266 else if (rb->rb_right)
1269 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1273 if (rb->rb_left == &sv->rb)
1276 rb->rb_right = NULL;
1286 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1289 * paranoid_check_si - check the scanning information.
1290 * @ubi: UBI device description object
1291 * @si: scanning information
1293 * This function returns zero if the scanning information is all right, and a
1294 * negative error code if not or if an error occurred.
1296 static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1298 int pnum, err, vols_found = 0;
1299 struct rb_node *rb1, *rb2;
1300 struct ubi_scan_volume *sv;
1301 struct ubi_scan_leb *seb, *last_seb;
1305 * At first, check that scanning information is OK.
1307 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1315 ubi_err("bad is_empty flag");
1319 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1320 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1321 sv->data_pad < 0 || sv->last_data_size < 0) {
1322 ubi_err("negative values");
1326 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1327 sv->vol_id < UBI_INTERNAL_VOL_START) {
1328 ubi_err("bad vol_id");
1332 if (sv->vol_id > si->highest_vol_id) {
1333 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1334 si->highest_vol_id, sv->vol_id);
1338 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1339 sv->vol_type != UBI_STATIC_VOLUME) {
1340 ubi_err("bad vol_type");
1344 if (sv->data_pad > ubi->leb_size / 2) {
1345 ubi_err("bad data_pad");
1350 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1356 if (seb->pnum < 0 || seb->ec < 0) {
1357 ubi_err("negative values");
1361 if (seb->ec < si->min_ec) {
1362 ubi_err("bad si->min_ec (%d), %d found",
1363 si->min_ec, seb->ec);
1367 if (seb->ec > si->max_ec) {
1368 ubi_err("bad si->max_ec (%d), %d found",
1369 si->max_ec, seb->ec);
1373 if (seb->pnum >= ubi->peb_count) {
1374 ubi_err("too high PEB number %d, total PEBs %d",
1375 seb->pnum, ubi->peb_count);
1379 if (sv->vol_type == UBI_STATIC_VOLUME) {
1380 if (seb->lnum >= sv->used_ebs) {
1381 ubi_err("bad lnum or used_ebs");
1385 if (sv->used_ebs != 0) {
1386 ubi_err("non-zero used_ebs");
1391 if (seb->lnum > sv->highest_lnum) {
1392 ubi_err("incorrect highest_lnum or lnum");
1397 if (sv->leb_count != leb_count) {
1398 ubi_err("bad leb_count, %d objects in the tree",
1408 if (seb->lnum != sv->highest_lnum) {
1409 ubi_err("bad highest_lnum");
1414 if (vols_found != si->vols_found) {
1415 ubi_err("bad si->vols_found %d, should be %d",
1416 si->vols_found, vols_found);
1420 /* Check that scanning information is correct */
1421 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1423 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1430 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1431 if (err && err != UBI_IO_BITFLIPS) {
1432 ubi_err("VID header is not OK (%d)", err);
1438 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1439 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1440 if (sv->vol_type != vol_type) {
1441 ubi_err("bad vol_type");
1445 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
1446 ubi_err("bad sqnum %llu", seb->sqnum);
1450 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
1451 ubi_err("bad vol_id %d", sv->vol_id);
1455 if (sv->compat != vidh->compat) {
1456 ubi_err("bad compat %d", vidh->compat);
1460 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
1461 ubi_err("bad lnum %d", seb->lnum);
1465 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
1466 ubi_err("bad used_ebs %d", sv->used_ebs);
1470 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
1471 ubi_err("bad data_pad %d", sv->data_pad);
1479 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
1480 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1484 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
1485 ubi_err("bad last_data_size %d", sv->last_data_size);
1491 * Make sure that all the physical eraseblocks are in one of the lists
1494 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
1498 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1499 err = ubi_io_is_bad(ubi, pnum);
1507 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1508 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1511 list_for_each_entry(seb, &si->free, u.list)
1514 list_for_each_entry(seb, &si->corr, u.list)
1517 list_for_each_entry(seb, &si->erase, u.list)
1520 list_for_each_entry(seb, &si->alien, u.list)
1524 for (pnum = 0; pnum < ubi->peb_count; pnum++)
1526 ubi_err("PEB %d is not referred", pnum);
1536 ubi_err("bad scanning information about LEB %d", seb->lnum);
1537 ubi_dbg_dump_seb(seb, 0);
1538 ubi_dbg_dump_sv(sv);
1542 ubi_err("bad scanning information about volume %d", sv->vol_id);
1543 ubi_dbg_dump_sv(sv);
1547 ubi_err("bad scanning information about volume %d", sv->vol_id);
1548 ubi_dbg_dump_sv(sv);
1549 ubi_dbg_dump_vid_hdr(vidh);
1552 ubi_dbg_dump_stack();
1556 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */