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/slab.h>
45 #include <linux/crc32.h>
46 #include <linux/math64.h>
47 #include <linux/random.h>
50 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
51 static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
53 #define paranoid_check_si(ubi, si) 0
56 /* Temporary variables used during scanning */
57 static struct ubi_ec_hdr *ech;
58 static struct ubi_vid_hdr *vidh;
61 * add_to_list - add physical eraseblock to a list.
62 * @si: scanning information
63 * @pnum: physical eraseblock number to add
64 * @ec: erase counter of the physical eraseblock
65 * @list: the list to add to
67 * This function adds physical eraseblock @pnum to free, erase, or alien lists.
68 * Returns zero in case of success and a negative error code in case of
71 static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
72 struct list_head *list)
74 struct ubi_scan_leb *seb;
76 if (list == &si->free) {
77 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
78 si->free_peb_count += 1;
79 } else if (list == &si->erase) {
80 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
81 si->erase_peb_count += 1;
82 } else if (list == &si->alien) {
83 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
84 si->alien_peb_count += 1;
88 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
94 list_add_tail(&seb->u.list, list);
99 * add_corrupted - add a corrupted physical eraseblock.
100 * @si: scanning information
101 * @pnum: physical eraseblock number to add
102 * @ec: erase counter of the physical eraseblock
104 * This function adds corrupted physical eraseblock @pnum to the 'corr' list.
105 * Returns zero in case of success and a negative error code in case of
108 static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)
110 struct ubi_scan_leb *seb;
112 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
114 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
118 si->corr_peb_count += 1;
121 list_add(&seb->u.list, &si->corr);
126 * validate_vid_hdr - check volume identifier header.
127 * @vid_hdr: the volume identifier header to check
128 * @sv: information about the volume this logical eraseblock belongs to
129 * @pnum: physical eraseblock number the VID header came from
131 * This function checks that data stored in @vid_hdr is consistent. Returns
132 * non-zero if an inconsistency was found and zero if not.
134 * Note, UBI does sanity check of everything it reads from the flash media.
135 * Most of the checks are done in the I/O sub-system. Here we check that the
136 * information in the VID header is consistent to the information in other VID
137 * headers of the same volume.
139 static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
140 const struct ubi_scan_volume *sv, int pnum)
142 int vol_type = vid_hdr->vol_type;
143 int vol_id = be32_to_cpu(vid_hdr->vol_id);
144 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
145 int data_pad = be32_to_cpu(vid_hdr->data_pad);
147 if (sv->leb_count != 0) {
151 * This is not the first logical eraseblock belonging to this
152 * volume. Ensure that the data in its VID header is consistent
153 * to the data in previous logical eraseblock headers.
156 if (vol_id != sv->vol_id) {
157 dbg_err("inconsistent vol_id");
161 if (sv->vol_type == UBI_STATIC_VOLUME)
162 sv_vol_type = UBI_VID_STATIC;
164 sv_vol_type = UBI_VID_DYNAMIC;
166 if (vol_type != sv_vol_type) {
167 dbg_err("inconsistent vol_type");
171 if (used_ebs != sv->used_ebs) {
172 dbg_err("inconsistent used_ebs");
176 if (data_pad != sv->data_pad) {
177 dbg_err("inconsistent data_pad");
185 ubi_err("inconsistent VID header at PEB %d", pnum);
186 ubi_dbg_dump_vid_hdr(vid_hdr);
192 * add_volume - add volume to the scanning information.
193 * @si: scanning information
194 * @vol_id: ID of the volume to add
195 * @pnum: physical eraseblock number
196 * @vid_hdr: volume identifier header
198 * If the volume corresponding to the @vid_hdr logical eraseblock is already
199 * present in the scanning information, this function does nothing. Otherwise
200 * it adds corresponding volume to the scanning information. Returns a pointer
201 * to the scanning volume object in case of success and a negative error code
202 * in case of failure.
204 static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
206 const struct ubi_vid_hdr *vid_hdr)
208 struct ubi_scan_volume *sv;
209 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
211 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
213 /* Walk the volume RB-tree to look if this volume is already present */
216 sv = rb_entry(parent, struct ubi_scan_volume, rb);
218 if (vol_id == sv->vol_id)
221 if (vol_id > sv->vol_id)
227 /* The volume is absent - add it */
228 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
230 return ERR_PTR(-ENOMEM);
232 sv->highest_lnum = sv->leb_count = 0;
235 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
236 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
237 sv->compat = vid_hdr->compat;
238 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
240 if (vol_id > si->highest_vol_id)
241 si->highest_vol_id = vol_id;
243 rb_link_node(&sv->rb, parent, p);
244 rb_insert_color(&sv->rb, &si->volumes);
246 dbg_bld("added volume %d", vol_id);
251 * compare_lebs - find out which logical eraseblock is newer.
252 * @ubi: UBI device description object
253 * @seb: first logical eraseblock to compare
254 * @pnum: physical eraseblock number of the second logical eraseblock to
256 * @vid_hdr: volume identifier header of the second logical eraseblock
258 * This function compares 2 copies of a LEB and informs which one is newer. In
259 * case of success this function returns a positive value, in case of failure, a
260 * negative error code is returned. The success return codes use the following
262 * o bit 0 is cleared: the first PEB (described by @seb) is newer than the
263 * second PEB (described by @pnum and @vid_hdr);
264 * o bit 0 is set: the second PEB is newer;
265 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
266 * o bit 1 is set: bit-flips were detected in the newer LEB;
267 * o bit 2 is cleared: the older LEB is not corrupted;
268 * o bit 2 is set: the older LEB is corrupted.
270 static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
271 int pnum, const struct ubi_vid_hdr *vid_hdr)
274 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
275 uint32_t data_crc, crc;
276 struct ubi_vid_hdr *vh = NULL;
277 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
279 if (sqnum2 == seb->sqnum) {
281 * This must be a really ancient UBI image which has been
282 * created before sequence numbers support has been added. At
283 * that times we used 32-bit LEB versions stored in logical
284 * eraseblocks. That was before UBI got into mainline. We do not
285 * support these images anymore. Well, those images will work
286 * still work, but only if no unclean reboots happened.
288 ubi_err("unsupported on-flash UBI format\n");
292 /* Obviously the LEB with lower sequence counter is older */
293 second_is_newer = !!(sqnum2 > seb->sqnum);
296 * Now we know which copy is newer. If the copy flag of the PEB with
297 * newer version is not set, then we just return, otherwise we have to
298 * check data CRC. For the second PEB we already have the VID header,
299 * for the first one - we'll need to re-read it from flash.
301 * Note: this may be optimized so that we wouldn't read twice.
304 if (second_is_newer) {
305 if (!vid_hdr->copy_flag) {
306 /* It is not a copy, so it is newer */
307 dbg_bld("second PEB %d is newer, copy_flag is unset",
314 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
318 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
320 if (err == UBI_IO_BITFLIPS)
323 dbg_err("VID of PEB %d header is bad, but it "
324 "was OK earlier", pnum);
332 if (!vh->copy_flag) {
333 /* It is not a copy, so it is newer */
334 dbg_bld("first PEB %d is newer, copy_flag is unset",
343 /* Read the data of the copy and check the CRC */
345 len = be32_to_cpu(vid_hdr->data_size);
352 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
353 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
356 data_crc = be32_to_cpu(vid_hdr->data_crc);
357 crc = crc32(UBI_CRC32_INIT, buf, len);
358 if (crc != data_crc) {
359 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
360 pnum, crc, data_crc);
363 second_is_newer = !second_is_newer;
365 dbg_bld("PEB %d CRC is OK", pnum);
370 ubi_free_vid_hdr(ubi, vh);
373 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
375 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
377 return second_is_newer | (bitflips << 1) | (corrupted << 2);
382 ubi_free_vid_hdr(ubi, vh);
387 * ubi_scan_add_used - add physical eraseblock to the scanning information.
388 * @ubi: UBI device description object
389 * @si: scanning information
390 * @pnum: the physical eraseblock number
392 * @vid_hdr: the volume identifier header
393 * @bitflips: if bit-flips were detected when this physical eraseblock was read
395 * This function adds information about a used physical eraseblock to the
396 * 'used' tree of the corresponding volume. The function is rather complex
397 * because it has to handle cases when this is not the first physical
398 * eraseblock belonging to the same logical eraseblock, and the newer one has
399 * to be picked, while the older one has to be dropped. This function returns
400 * zero in case of success and a negative error code in case of failure.
402 int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
403 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
406 int err, vol_id, lnum;
407 unsigned long long sqnum;
408 struct ubi_scan_volume *sv;
409 struct ubi_scan_leb *seb;
410 struct rb_node **p, *parent = NULL;
412 vol_id = be32_to_cpu(vid_hdr->vol_id);
413 lnum = be32_to_cpu(vid_hdr->lnum);
414 sqnum = be64_to_cpu(vid_hdr->sqnum);
416 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
417 pnum, vol_id, lnum, ec, sqnum, bitflips);
419 sv = add_volume(si, vol_id, pnum, vid_hdr);
423 if (si->max_sqnum < sqnum)
424 si->max_sqnum = sqnum;
427 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
428 * if this is the first instance of this logical eraseblock or not.
430 p = &sv->root.rb_node;
435 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
436 if (lnum != seb->lnum) {
437 if (lnum < seb->lnum)
445 * There is already a physical eraseblock describing the same
446 * logical eraseblock present.
449 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
450 "EC %d", seb->pnum, seb->sqnum, seb->ec);
453 * Make sure that the logical eraseblocks have different
454 * sequence numbers. Otherwise the image is bad.
456 * However, if the sequence number is zero, we assume it must
457 * be an ancient UBI image from the era when UBI did not have
458 * sequence numbers. We still can attach these images, unless
459 * there is a need to distinguish between old and new
460 * eraseblocks, in which case we'll refuse the image in
461 * 'compare_lebs()'. In other words, we attach old clean
462 * images, but refuse attaching old images with duplicated
463 * logical eraseblocks because there was an unclean reboot.
465 if (seb->sqnum == sqnum && sqnum != 0) {
466 ubi_err("two LEBs with same sequence number %llu",
468 ubi_dbg_dump_seb(seb, 0);
469 ubi_dbg_dump_vid_hdr(vid_hdr);
474 * Now we have to drop the older one and preserve the newer
477 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
483 * This logical eraseblock is newer than the one
486 err = validate_vid_hdr(vid_hdr, sv, pnum);
491 err = add_corrupted(si, seb->pnum, seb->ec);
493 err = add_to_list(si, seb->pnum, seb->ec,
500 seb->scrub = ((cmp_res & 2) || bitflips);
503 if (sv->highest_lnum == lnum)
505 be32_to_cpu(vid_hdr->data_size);
510 * This logical eraseblock is older than the one found
514 return add_corrupted(si, pnum, ec);
516 return add_to_list(si, pnum, ec, &si->erase);
521 * We've met this logical eraseblock for the first time, add it to the
522 * scanning information.
525 err = validate_vid_hdr(vid_hdr, sv, pnum);
529 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
537 seb->scrub = bitflips;
539 if (sv->highest_lnum <= lnum) {
540 sv->highest_lnum = lnum;
541 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
545 rb_link_node(&seb->u.rb, parent, p);
546 rb_insert_color(&seb->u.rb, &sv->root);
547 si->used_peb_count += 1;
552 * ubi_scan_find_sv - find volume in the scanning information.
553 * @si: scanning information
554 * @vol_id: the requested volume ID
556 * This function returns a pointer to the volume description or %NULL if there
557 * are no data about this volume in the scanning information.
559 struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
562 struct ubi_scan_volume *sv;
563 struct rb_node *p = si->volumes.rb_node;
566 sv = rb_entry(p, struct ubi_scan_volume, rb);
568 if (vol_id == sv->vol_id)
571 if (vol_id > sv->vol_id)
581 * ubi_scan_find_seb - find LEB in the volume scanning information.
582 * @sv: a pointer to the volume scanning information
583 * @lnum: the requested logical eraseblock
585 * This function returns a pointer to the scanning logical eraseblock or %NULL
586 * if there are no data about it in the scanning volume information.
588 struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
591 struct ubi_scan_leb *seb;
592 struct rb_node *p = sv->root.rb_node;
595 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
597 if (lnum == seb->lnum)
600 if (lnum > seb->lnum)
610 * ubi_scan_rm_volume - delete scanning information about a volume.
611 * @si: scanning information
612 * @sv: the volume scanning information to delete
614 void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
617 struct ubi_scan_leb *seb;
619 dbg_bld("remove scanning information about volume %d", sv->vol_id);
621 while ((rb = rb_first(&sv->root))) {
622 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
623 rb_erase(&seb->u.rb, &sv->root);
624 list_add_tail(&seb->u.list, &si->erase);
627 rb_erase(&sv->rb, &si->volumes);
633 * ubi_scan_erase_peb - erase a physical eraseblock.
634 * @ubi: UBI device description object
635 * @si: scanning information
636 * @pnum: physical eraseblock number to erase;
637 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
639 * This function erases physical eraseblock 'pnum', and writes the erase
640 * counter header to it. This function should only be used on UBI device
641 * initialization stages, when the EBA sub-system had not been yet initialized.
642 * This function returns zero in case of success and a negative error code in
645 int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
649 struct ubi_ec_hdr *ec_hdr;
651 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
653 * Erase counter overflow. Upgrade UBI and use 64-bit
654 * erase counters internally.
656 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
660 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
664 ec_hdr->ec = cpu_to_be64(ec);
666 err = ubi_io_sync_erase(ubi, pnum, 0);
670 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
678 * ubi_scan_get_free_peb - get a free physical eraseblock.
679 * @ubi: UBI device description object
680 * @si: scanning information
682 * This function returns a free physical eraseblock. It is supposed to be
683 * called on the UBI initialization stages when the wear-leveling sub-system is
684 * not initialized yet. This function picks a physical eraseblocks from one of
685 * the lists, writes the EC header if it is needed, and removes it from the
688 * This function returns scanning physical eraseblock information in case of
689 * success and an error code in case of failure.
691 struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
692 struct ubi_scan_info *si)
695 struct ubi_scan_leb *seb;
697 if (!list_empty(&si->free)) {
698 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
699 list_del(&seb->u.list);
700 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
704 for (i = 0; i < 2; i++) {
705 struct list_head *head;
706 struct ubi_scan_leb *tmp_seb;
714 * We try to erase the first physical eraseblock from the @head
715 * list and pick it if we succeed, or try to erase the
716 * next one if not. And so forth. We don't want to take care
717 * about bad eraseblocks here - they'll be handled later.
719 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
720 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
721 seb->ec = si->mean_ec;
723 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
728 list_del(&seb->u.list);
729 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
734 ubi_err("no eraseblocks found");
735 return ERR_PTR(-ENOSPC);
739 * process_eb - read, check UBI headers, and add them to scanning information.
740 * @ubi: UBI device description object
741 * @si: scanning information
742 * @pnum: the physical eraseblock number
744 * This function returns a zero if the physical eraseblock was successfully
745 * handled and a negative error code in case of failure.
747 static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
750 long long uninitialized_var(ec);
751 int err, bitflips = 0, vol_id, ec_err = 0;
753 dbg_bld("scan PEB %d", pnum);
755 /* Skip bad physical eraseblocks */
756 err = ubi_io_is_bad(ubi, pnum);
761 * FIXME: this is actually duty of the I/O sub-system to
762 * initialize this, but MTD does not provide enough
765 si->bad_peb_count += 1;
769 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
775 case UBI_IO_BITFLIPS:
779 case UBI_IO_FF_BITFLIPS:
780 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
781 case UBI_IO_BAD_HDR_EBADMSG:
782 si->read_err_count += 1;
785 * We have to also look at the VID header, possibly it is not
786 * corrupted. Set %bitflips flag in order to make this PEB be
787 * moved and EC be re-created.
790 ec = UBI_SCAN_UNKNOWN_EC;
794 ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
801 /* Make sure UBI version is OK */
802 if (ech->version != UBI_VERSION) {
803 ubi_err("this UBI version is %d, image version is %d",
804 UBI_VERSION, (int)ech->version);
808 ec = be64_to_cpu(ech->ec);
809 if (ec > UBI_MAX_ERASECOUNTER) {
811 * Erase counter overflow. The EC headers have 64 bits
812 * reserved, but we anyway make use of only 31 bit
813 * values, as this seems to be enough for any existing
814 * flash. Upgrade UBI and use 64-bit erase counters
817 ubi_err("erase counter overflow, max is %d",
818 UBI_MAX_ERASECOUNTER);
819 ubi_dbg_dump_ec_hdr(ech);
824 * Make sure that all PEBs have the same image sequence number.
825 * This allows us to detect situations when users flash UBI
826 * images incorrectly, so that the flash has the new UBI image
827 * and leftovers from the old one. This feature was added
828 * relatively recently, and the sequence number was always
829 * zero, because old UBI implementations always set it to zero.
830 * For this reasons, we do not panic if some PEBs have zero
831 * sequence number, while other PEBs have non-zero sequence
834 image_seq = be32_to_cpu(ech->image_seq);
835 if (!ubi->image_seq && image_seq)
836 ubi->image_seq = image_seq;
837 if (ubi->image_seq && image_seq &&
838 ubi->image_seq != image_seq) {
839 ubi_err("bad image sequence number %d in PEB %d, "
840 "expected %d", image_seq, pnum, ubi->image_seq);
841 ubi_dbg_dump_ec_hdr(ech);
846 /* OK, we've done with the EC header, let's look at the VID header */
848 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
854 case UBI_IO_BITFLIPS:
857 case UBI_IO_BAD_HDR_EBADMSG:
858 si->read_err_count += 1;
860 case UBI_IO_FF_BITFLIPS:
861 err = add_corrupted(si, pnum, ec);
867 err = add_corrupted(si, pnum, ec);
869 err = add_to_list(si, pnum, ec, &si->free);
874 ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
879 vol_id = be32_to_cpu(vidh->vol_id);
880 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
881 int lnum = be32_to_cpu(vidh->lnum);
883 /* Unsupported internal volume */
884 switch (vidh->compat) {
885 case UBI_COMPAT_DELETE:
886 ubi_msg("\"delete\" compatible internal volume %d:%d"
887 " found, will remove it", vol_id, lnum);
888 err = add_to_list(si, pnum, ec, &si->erase);
894 ubi_msg("read-only compatible internal volume %d:%d"
895 " found, switch to read-only mode",
900 case UBI_COMPAT_PRESERVE:
901 ubi_msg("\"preserve\" compatible internal volume %d:%d"
902 " found", vol_id, lnum);
903 err = add_to_list(si, pnum, ec, &si->alien);
908 case UBI_COMPAT_REJECT:
909 ubi_err("incompatible internal volume %d:%d found",
916 ubi_warn("valid VID header but corrupted EC header at PEB %d",
918 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
936 * check_what_we_have - check what PEB were found by scanning.
937 * @ubi: UBI device description object
938 * @si: scanning information
940 * This is a helper function which takes a look what PEBs were found by
941 * scanning, and decides whether the flash is empty and should be formatted and
942 * whether there are too many corrupted PEBs and we should not attach this
943 * MTD device. Returns zero if we should proceed with attaching the MTD device,
944 * and %-EINVAL if we should not.
946 static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
948 struct ubi_scan_leb *seb;
951 max_corr = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
952 max_corr = max_corr / 20 ?: 8;
955 * Few corrupted PEBs are not a problem and may be just a result of
956 * unclean reboots. However, many of them may indicate some problems
957 * with the flash HW or driver.
959 if (si->corr_peb_count >= 8) {
960 ubi_warn("%d PEBs are corrupted", si->corr_peb_count);
961 printk(KERN_WARNING "corrupted PEBs are:");
962 list_for_each_entry(seb, &si->corr, u.list)
963 printk(KERN_CONT " %d", seb->pnum);
964 printk(KERN_CONT "\n");
967 * If too many PEBs are corrupted, we refuse attaching,
968 * otherwise, only print a warning.
970 if (si->corr_peb_count >= max_corr) {
971 ubi_err("too many corrupted PEBs, refusing this device");
976 if (si->free_peb_count + si->used_peb_count +
977 si->alien_peb_count == 0) {
978 /* No UBI-formatted eraseblocks were found */
979 if (si->corr_peb_count == si->read_err_count &&
980 si->corr_peb_count < 8) {
981 /* No or just few corrupted PEBs, and all of them had a
982 * read error. We assume that those are bad PEBs, which
983 * were just not marked as bad so far.
985 * This piece of code basically tries to distinguish
986 * between the following 2 situations:
988 * 1. Flash is empty, but there are few bad PEBs, which
989 * are not marked as bad so far, and which were read
990 * with error. We want to go ahead and format this
991 * flash. While formating, the faulty PEBs will
992 * probably be marked as bad.
994 * 2. Flash probably contains non-UBI data and we do
995 * not want to format it and destroy possibly needed
996 * data (e.g., consider the case when the bootloader
997 * MTD partition was accidentally fed to UBI).
1000 ubi_msg("empty MTD device detected");
1001 get_random_bytes(&ubi->image_seq, sizeof(ubi->image_seq));
1003 ubi_err("MTD device possibly contains non-UBI data, "
1009 if (si->corr_peb_count > 0)
1010 ubi_msg("corrupted PEBs will be formatted");
1015 * ubi_scan - scan an MTD device.
1016 * @ubi: UBI device description object
1018 * This function does full scanning of an MTD device and returns complete
1019 * information about it. In case of failure, an error code is returned.
1021 struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
1024 struct rb_node *rb1, *rb2;
1025 struct ubi_scan_volume *sv;
1026 struct ubi_scan_leb *seb;
1027 struct ubi_scan_info *si;
1029 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
1031 return ERR_PTR(-ENOMEM);
1033 INIT_LIST_HEAD(&si->corr);
1034 INIT_LIST_HEAD(&si->free);
1035 INIT_LIST_HEAD(&si->erase);
1036 INIT_LIST_HEAD(&si->alien);
1037 si->volumes = RB_ROOT;
1040 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1044 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
1048 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1051 dbg_gen("process PEB %d", pnum);
1052 err = process_eb(ubi, si, pnum);
1057 dbg_msg("scanning is finished");
1059 /* Calculate mean erase counter */
1061 si->mean_ec = div_u64(si->ec_sum, si->ec_count);
1063 err = check_what_we_have(ubi, si);
1068 * In case of unknown erase counter we use the mean erase counter
1071 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1072 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1073 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1074 seb->ec = si->mean_ec;
1077 list_for_each_entry(seb, &si->free, u.list) {
1078 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1079 seb->ec = si->mean_ec;
1082 list_for_each_entry(seb, &si->corr, u.list)
1083 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1084 seb->ec = si->mean_ec;
1086 list_for_each_entry(seb, &si->erase, u.list)
1087 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1088 seb->ec = si->mean_ec;
1090 err = paranoid_check_si(ubi, si);
1094 ubi_free_vid_hdr(ubi, vidh);
1100 ubi_free_vid_hdr(ubi, vidh);
1104 ubi_scan_destroy_si(si);
1105 return ERR_PTR(err);
1109 * destroy_sv - free the scanning volume information
1110 * @sv: scanning volume information
1112 * This function destroys the volume RB-tree (@sv->root) and the scanning
1113 * volume information.
1115 static void destroy_sv(struct ubi_scan_volume *sv)
1117 struct ubi_scan_leb *seb;
1118 struct rb_node *this = sv->root.rb_node;
1122 this = this->rb_left;
1123 else if (this->rb_right)
1124 this = this->rb_right;
1126 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1127 this = rb_parent(this);
1129 if (this->rb_left == &seb->u.rb)
1130 this->rb_left = NULL;
1132 this->rb_right = NULL;
1142 * ubi_scan_destroy_si - destroy scanning information.
1143 * @si: scanning information
1145 void ubi_scan_destroy_si(struct ubi_scan_info *si)
1147 struct ubi_scan_leb *seb, *seb_tmp;
1148 struct ubi_scan_volume *sv;
1151 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1152 list_del(&seb->u.list);
1155 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1156 list_del(&seb->u.list);
1159 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1160 list_del(&seb->u.list);
1163 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1164 list_del(&seb->u.list);
1168 /* Destroy the volume RB-tree */
1169 rb = si->volumes.rb_node;
1173 else if (rb->rb_right)
1176 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1180 if (rb->rb_left == &sv->rb)
1183 rb->rb_right = NULL;
1193 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1196 * paranoid_check_si - check the scanning information.
1197 * @ubi: UBI device description object
1198 * @si: scanning information
1200 * This function returns zero if the scanning information is all right, and a
1201 * negative error code if not or if an error occurred.
1203 static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1205 int pnum, err, vols_found = 0;
1206 struct rb_node *rb1, *rb2;
1207 struct ubi_scan_volume *sv;
1208 struct ubi_scan_leb *seb, *last_seb;
1212 * At first, check that scanning information is OK.
1214 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1222 ubi_err("bad is_empty flag");
1226 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1227 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1228 sv->data_pad < 0 || sv->last_data_size < 0) {
1229 ubi_err("negative values");
1233 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1234 sv->vol_id < UBI_INTERNAL_VOL_START) {
1235 ubi_err("bad vol_id");
1239 if (sv->vol_id > si->highest_vol_id) {
1240 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1241 si->highest_vol_id, sv->vol_id);
1245 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1246 sv->vol_type != UBI_STATIC_VOLUME) {
1247 ubi_err("bad vol_type");
1251 if (sv->data_pad > ubi->leb_size / 2) {
1252 ubi_err("bad data_pad");
1257 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1263 if (seb->pnum < 0 || seb->ec < 0) {
1264 ubi_err("negative values");
1268 if (seb->ec < si->min_ec) {
1269 ubi_err("bad si->min_ec (%d), %d found",
1270 si->min_ec, seb->ec);
1274 if (seb->ec > si->max_ec) {
1275 ubi_err("bad si->max_ec (%d), %d found",
1276 si->max_ec, seb->ec);
1280 if (seb->pnum >= ubi->peb_count) {
1281 ubi_err("too high PEB number %d, total PEBs %d",
1282 seb->pnum, ubi->peb_count);
1286 if (sv->vol_type == UBI_STATIC_VOLUME) {
1287 if (seb->lnum >= sv->used_ebs) {
1288 ubi_err("bad lnum or used_ebs");
1292 if (sv->used_ebs != 0) {
1293 ubi_err("non-zero used_ebs");
1298 if (seb->lnum > sv->highest_lnum) {
1299 ubi_err("incorrect highest_lnum or lnum");
1304 if (sv->leb_count != leb_count) {
1305 ubi_err("bad leb_count, %d objects in the tree",
1315 if (seb->lnum != sv->highest_lnum) {
1316 ubi_err("bad highest_lnum");
1321 if (vols_found != si->vols_found) {
1322 ubi_err("bad si->vols_found %d, should be %d",
1323 si->vols_found, vols_found);
1327 /* Check that scanning information is correct */
1328 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1330 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1337 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1338 if (err && err != UBI_IO_BITFLIPS) {
1339 ubi_err("VID header is not OK (%d)", err);
1345 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1346 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1347 if (sv->vol_type != vol_type) {
1348 ubi_err("bad vol_type");
1352 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
1353 ubi_err("bad sqnum %llu", seb->sqnum);
1357 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
1358 ubi_err("bad vol_id %d", sv->vol_id);
1362 if (sv->compat != vidh->compat) {
1363 ubi_err("bad compat %d", vidh->compat);
1367 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
1368 ubi_err("bad lnum %d", seb->lnum);
1372 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
1373 ubi_err("bad used_ebs %d", sv->used_ebs);
1377 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
1378 ubi_err("bad data_pad %d", sv->data_pad);
1386 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
1387 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1391 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
1392 ubi_err("bad last_data_size %d", sv->last_data_size);
1398 * Make sure that all the physical eraseblocks are in one of the lists
1401 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
1405 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1406 err = ubi_io_is_bad(ubi, pnum);
1414 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1415 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1418 list_for_each_entry(seb, &si->free, u.list)
1421 list_for_each_entry(seb, &si->corr, u.list)
1424 list_for_each_entry(seb, &si->erase, u.list)
1427 list_for_each_entry(seb, &si->alien, u.list)
1431 for (pnum = 0; pnum < ubi->peb_count; pnum++)
1433 ubi_err("PEB %d is not referred", pnum);
1443 ubi_err("bad scanning information about LEB %d", seb->lnum);
1444 ubi_dbg_dump_seb(seb, 0);
1445 ubi_dbg_dump_sv(sv);
1449 ubi_err("bad scanning information about volume %d", sv->vol_id);
1450 ubi_dbg_dump_sv(sv);
1454 ubi_err("bad scanning information about volume %d", sv->vol_id);
1455 ubi_dbg_dump_sv(sv);
1456 ubi_dbg_dump_vid_hdr(vidh);
1459 ubi_dbg_dump_stack();
1463 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */