2 * Copyright (c) International Business Machines Corp., 2006
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author: Artem Bityutskiy (Битюцкий Артём)
24 * This unit is responsible for scanning the flash media, checking UBI
25 * headers and providing complete information about the UBI flash image.
27 * The scanning information is represented by a &struct ubi_scan_info' object.
28 * Information about found volumes is represented by &struct ubi_scan_volume
29 * objects which are kept in volume RB-tree with root at the @volumes field.
30 * The RB-tree is indexed by the volume ID.
32 * Found logical eraseblocks are represented by &struct ubi_scan_leb objects.
33 * These objects are kept in per-volume RB-trees with the root at the
34 * corresponding &struct ubi_scan_volume object. To put it differently, we keep
35 * an RB-tree of per-volume objects and each of these objects is the root of
36 * RB-tree of per-eraseblock objects.
38 * Corrupted physical eraseblocks are put to the @corr list, free physical
39 * eraseblocks are put to the @free list and the physical eraseblock to be
40 * erased are put to the @erase list.
44 #include <linux/err.h>
45 #include <linux/crc32.h>
46 #include <asm/div64.h>
49 #include <ubi_uboot.h>
52 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
53 static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
55 #define paranoid_check_si(ubi, si) 0
58 /* Temporary variables used during scanning */
59 static struct ubi_ec_hdr *ech;
60 static struct ubi_vid_hdr *vidh;
63 * add_to_list - add physical eraseblock to a list.
64 * @si: scanning information
65 * @pnum: physical eraseblock number to add
66 * @ec: erase counter of the physical eraseblock
67 * @list: the list to add to
69 * This function adds physical eraseblock @pnum to free, erase, corrupted or
70 * alien lists. Returns zero in case of success and a negative error code in
73 static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
74 struct list_head *list)
76 struct ubi_scan_leb *seb;
78 if (list == &si->free)
79 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
80 else if (list == &si->erase)
81 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
82 else if (list == &si->corr)
83 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
84 else if (list == &si->alien)
85 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
89 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
95 list_add_tail(&seb->u.list, list);
100 * validate_vid_hdr - check that volume identifier header is correct and
102 * @vid_hdr: the volume identifier header to check
103 * @sv: information about the volume this logical eraseblock belongs to
104 * @pnum: physical eraseblock number the VID header came from
106 * This function checks that data stored in @vid_hdr is consistent. Returns
107 * non-zero if an inconsistency was found and zero if not.
109 * Note, UBI does sanity check of everything it reads from the flash media.
110 * Most of the checks are done in the I/O unit. Here we check that the
111 * information in the VID header is consistent to the information in other VID
112 * headers of the same volume.
114 static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
115 const struct ubi_scan_volume *sv, int pnum)
117 int vol_type = vid_hdr->vol_type;
118 int vol_id = be32_to_cpu(vid_hdr->vol_id);
119 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
120 int data_pad = be32_to_cpu(vid_hdr->data_pad);
122 if (sv->leb_count != 0) {
126 * This is not the first logical eraseblock belonging to this
127 * volume. Ensure that the data in its VID header is consistent
128 * to the data in previous logical eraseblock headers.
131 if (vol_id != sv->vol_id) {
132 dbg_err("inconsistent vol_id");
136 if (sv->vol_type == UBI_STATIC_VOLUME)
137 sv_vol_type = UBI_VID_STATIC;
139 sv_vol_type = UBI_VID_DYNAMIC;
141 if (vol_type != sv_vol_type) {
142 dbg_err("inconsistent vol_type");
146 if (used_ebs != sv->used_ebs) {
147 dbg_err("inconsistent used_ebs");
151 if (data_pad != sv->data_pad) {
152 dbg_err("inconsistent data_pad");
160 ubi_err("inconsistent VID header at PEB %d", pnum);
161 ubi_dbg_dump_vid_hdr(vid_hdr);
167 * add_volume - add volume to the scanning information.
168 * @si: scanning information
169 * @vol_id: ID of the volume to add
170 * @pnum: physical eraseblock number
171 * @vid_hdr: volume identifier header
173 * If the volume corresponding to the @vid_hdr logical eraseblock is already
174 * present in the scanning information, this function does nothing. Otherwise
175 * it adds corresponding volume to the scanning information. Returns a pointer
176 * to the scanning volume object in case of success and a negative error code
177 * in case of failure.
179 static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
181 const struct ubi_vid_hdr *vid_hdr)
183 struct ubi_scan_volume *sv;
184 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
186 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
188 /* Walk the volume RB-tree to look if this volume is already present */
191 sv = rb_entry(parent, struct ubi_scan_volume, rb);
193 if (vol_id == sv->vol_id)
196 if (vol_id > sv->vol_id)
202 /* The volume is absent - add it */
203 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
205 return ERR_PTR(-ENOMEM);
207 sv->highest_lnum = sv->leb_count = 0;
210 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
211 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
212 sv->compat = vid_hdr->compat;
213 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
215 if (vol_id > si->highest_vol_id)
216 si->highest_vol_id = vol_id;
218 rb_link_node(&sv->rb, parent, p);
219 rb_insert_color(&sv->rb, &si->volumes);
221 dbg_bld("added volume %d", vol_id);
226 * compare_lebs - find out which logical eraseblock is newer.
227 * @ubi: UBI device description object
228 * @seb: first logical eraseblock to compare
229 * @pnum: physical eraseblock number of the second logical eraseblock to
231 * @vid_hdr: volume identifier header of the second logical eraseblock
233 * This function compares 2 copies of a LEB and informs which one is newer. In
234 * case of success this function returns a positive value, in case of failure, a
235 * negative error code is returned. The success return codes use the following
237 * o bit 0 is cleared: the first PEB (described by @seb) is newer then the
238 * second PEB (described by @pnum and @vid_hdr);
239 * o bit 0 is set: the second PEB is newer;
240 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
241 * o bit 1 is set: bit-flips were detected in the newer LEB;
242 * o bit 2 is cleared: the older LEB is not corrupted;
243 * o bit 2 is set: the older LEB is corrupted.
245 static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
246 int pnum, const struct ubi_vid_hdr *vid_hdr)
249 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
250 uint32_t data_crc, crc;
251 struct ubi_vid_hdr *vh = NULL;
252 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
254 if (seb->sqnum == 0 && sqnum2 == 0) {
255 long long abs, v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver);
258 * UBI constantly increases the logical eraseblock version
259 * number and it can overflow. Thus, we have to bear in mind
260 * that versions that are close to %0xFFFFFFFF are less then
261 * versions that are close to %0.
263 * The UBI WL unit guarantees that the number of pending tasks
264 * is not greater then %0x7FFFFFFF. So, if the difference
265 * between any two versions is greater or equivalent to
266 * %0x7FFFFFFF, there was an overflow and the logical
267 * eraseblock with lower version is actually newer then the one
268 * with higher version.
270 * FIXME: but this is anyway obsolete and will be removed at
273 dbg_bld("using old crappy leb_ver stuff");
276 ubi_err("PEB %d and PEB %d have the same version %lld",
277 seb->pnum, pnum, v1);
285 if (abs < 0x7FFFFFFF)
286 /* Non-overflow situation */
287 second_is_newer = (v2 > v1);
289 second_is_newer = (v2 < v1);
291 /* Obviously the LEB with lower sequence counter is older */
292 second_is_newer = sqnum2 > seb->sqnum;
295 * Now we know which copy is newer. If the copy flag of the PEB with
296 * newer version is not set, then we just return, otherwise we have to
297 * check data CRC. For the second PEB we already have the VID header,
298 * for the first one - we'll need to re-read it from flash.
300 * FIXME: this may be optimized so that we wouldn't read twice.
303 if (second_is_newer) {
304 if (!vid_hdr->copy_flag) {
305 /* It is not a copy, so it is newer */
306 dbg_bld("second PEB %d is newer, copy_flag is unset",
313 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
317 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
319 if (err == UBI_IO_BITFLIPS)
322 dbg_err("VID of PEB %d header is bad, but it "
323 "was OK earlier", pnum);
331 if (!vh->copy_flag) {
332 /* It is not a copy, so it is newer */
333 dbg_bld("first PEB %d is newer, copy_flag is unset",
342 /* Read the data of the copy and check the CRC */
344 len = be32_to_cpu(vid_hdr->data_size);
351 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
352 if (err && err != UBI_IO_BITFLIPS)
355 data_crc = be32_to_cpu(vid_hdr->data_crc);
356 crc = crc32(UBI_CRC32_INIT, buf, len);
357 if (crc != data_crc) {
358 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
359 pnum, crc, data_crc);
362 second_is_newer = !second_is_newer;
364 dbg_bld("PEB %d CRC is OK", pnum);
369 ubi_free_vid_hdr(ubi, vh);
372 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
374 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
376 return second_is_newer | (bitflips << 1) | (corrupted << 2);
381 ubi_free_vid_hdr(ubi, vh);
386 * ubi_scan_add_used - add information about a physical eraseblock to the
387 * 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;
408 unsigned long long sqnum;
409 struct ubi_scan_volume *sv;
410 struct ubi_scan_leb *seb;
411 struct rb_node **p, *parent = NULL;
413 vol_id = be32_to_cpu(vid_hdr->vol_id);
414 lnum = be32_to_cpu(vid_hdr->lnum);
415 sqnum = be64_to_cpu(vid_hdr->sqnum);
416 leb_ver = be32_to_cpu(vid_hdr->leb_ver);
418 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d",
419 pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips);
421 sv = add_volume(si, vol_id, pnum, vid_hdr);
425 if (si->max_sqnum < sqnum)
426 si->max_sqnum = sqnum;
429 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
430 * if this is the first instance of this logical eraseblock or not.
432 p = &sv->root.rb_node;
437 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
438 if (lnum != seb->lnum) {
439 if (lnum < seb->lnum)
447 * There is already a physical eraseblock describing the same
448 * logical eraseblock present.
451 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
452 "LEB ver %u, EC %d", seb->pnum, seb->sqnum,
453 seb->leb_ver, seb->ec);
456 * Make sure that the logical eraseblocks have different
457 * versions. Otherwise the image is bad.
459 if (seb->leb_ver == leb_ver && leb_ver != 0) {
460 ubi_err("two LEBs with same version %u", leb_ver);
461 ubi_dbg_dump_seb(seb, 0);
462 ubi_dbg_dump_vid_hdr(vid_hdr);
467 * Make sure that the logical eraseblocks have different
468 * sequence numbers. Otherwise the image is bad.
470 * FIXME: remove 'sqnum != 0' check when leb_ver is removed.
472 if (seb->sqnum == sqnum && sqnum != 0) {
473 ubi_err("two LEBs with same sequence number %llu",
475 ubi_dbg_dump_seb(seb, 0);
476 ubi_dbg_dump_vid_hdr(vid_hdr);
481 * Now we have to drop the older one and preserve the newer
484 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
490 * This logical eraseblock is newer then the one
493 err = validate_vid_hdr(vid_hdr, sv, pnum);
498 err = add_to_list(si, seb->pnum, seb->ec,
501 err = add_to_list(si, seb->pnum, seb->ec,
508 seb->scrub = ((cmp_res & 2) || bitflips);
510 seb->leb_ver = leb_ver;
512 if (sv->highest_lnum == lnum)
514 be32_to_cpu(vid_hdr->data_size);
519 * This logical eraseblock is older then the one found
523 return add_to_list(si, pnum, ec, &si->corr);
525 return add_to_list(si, pnum, ec, &si->erase);
530 * We've met this logical eraseblock for the first time, add it to the
531 * scanning information.
534 err = validate_vid_hdr(vid_hdr, sv, pnum);
538 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
546 seb->scrub = bitflips;
547 seb->leb_ver = leb_ver;
549 if (sv->highest_lnum <= lnum) {
550 sv->highest_lnum = lnum;
551 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
555 rb_link_node(&seb->u.rb, parent, p);
556 rb_insert_color(&seb->u.rb, &sv->root);
561 * ubi_scan_find_sv - find information about a particular volume in the
562 * scanning information.
563 * @si: scanning information
564 * @vol_id: the requested volume ID
566 * This function returns a pointer to the volume description or %NULL if there
567 * are no data about this volume in the scanning information.
569 struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
572 struct ubi_scan_volume *sv;
573 struct rb_node *p = si->volumes.rb_node;
576 sv = rb_entry(p, struct ubi_scan_volume, rb);
578 if (vol_id == sv->vol_id)
581 if (vol_id > sv->vol_id)
591 * ubi_scan_find_seb - find information about a particular logical
592 * eraseblock in the volume scanning information.
593 * @sv: a pointer to the volume scanning information
594 * @lnum: the requested logical eraseblock
596 * This function returns a pointer to the scanning logical eraseblock or %NULL
597 * if there are no data about it in the scanning volume information.
599 struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
602 struct ubi_scan_leb *seb;
603 struct rb_node *p = sv->root.rb_node;
606 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
608 if (lnum == seb->lnum)
611 if (lnum > seb->lnum)
621 * ubi_scan_rm_volume - delete scanning information about a volume.
622 * @si: scanning information
623 * @sv: the volume scanning information to delete
625 void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
628 struct ubi_scan_leb *seb;
630 dbg_bld("remove scanning information about volume %d", sv->vol_id);
632 while ((rb = rb_first(&sv->root))) {
633 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
634 rb_erase(&seb->u.rb, &sv->root);
635 list_add_tail(&seb->u.list, &si->erase);
638 rb_erase(&sv->rb, &si->volumes);
644 * ubi_scan_erase_peb - erase a physical eraseblock.
645 * @ubi: UBI device description object
646 * @si: scanning information
647 * @pnum: physical eraseblock number to erase;
648 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
650 * This function erases physical eraseblock 'pnum', and writes the erase
651 * counter header to it. This function should only be used on UBI device
652 * initialization stages, when the EBA unit had not been yet initialized. This
653 * function returns zero in case of success and a negative error code in case
656 int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
660 struct ubi_ec_hdr *ec_hdr;
662 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
664 * Erase counter overflow. Upgrade UBI and use 64-bit
665 * erase counters internally.
667 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
671 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
675 ec_hdr->ec = cpu_to_be64(ec);
677 err = ubi_io_sync_erase(ubi, pnum, 0);
681 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
689 * ubi_scan_get_free_peb - get a free physical eraseblock.
690 * @ubi: UBI device description object
691 * @si: scanning information
693 * This function returns a free physical eraseblock. It is supposed to be
694 * called on the UBI initialization stages when the wear-leveling unit is not
695 * initialized yet. This function picks a physical eraseblocks from one of the
696 * lists, writes the EC header if it is needed, and removes it from the list.
698 * This function returns scanning physical eraseblock information in case of
699 * success and an error code in case of failure.
701 struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
702 struct ubi_scan_info *si)
705 struct ubi_scan_leb *seb;
707 if (!list_empty(&si->free)) {
708 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
709 list_del(&seb->u.list);
710 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
714 for (i = 0; i < 2; i++) {
715 struct list_head *head;
716 struct ubi_scan_leb *tmp_seb;
724 * We try to erase the first physical eraseblock from the @head
725 * list and pick it if we succeed, or try to erase the
726 * next one if not. And so forth. We don't want to take care
727 * about bad eraseblocks here - they'll be handled later.
729 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
730 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
731 seb->ec = si->mean_ec;
733 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
738 list_del(&seb->u.list);
739 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
744 ubi_err("no eraseblocks found");
745 return ERR_PTR(-ENOSPC);
749 * process_eb - read UBI headers, check them and add corresponding data
750 * to the scanning information.
751 * @ubi: UBI device description object
752 * @si: scanning information
753 * @pnum: the physical eraseblock number
755 * This function returns a zero if the physical eraseblock was successfully
756 * handled and a negative error code in case of failure.
758 static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum)
760 long long uninitialized_var(ec);
761 int err, bitflips = 0, vol_id, ec_corr = 0;
763 dbg_bld("scan PEB %d", pnum);
765 /* Skip bad physical eraseblocks */
766 err = ubi_io_is_bad(ubi, pnum);
771 * FIXME: this is actually duty of the I/O unit to initialize
772 * this, but MTD does not provide enough information.
774 si->bad_peb_count += 1;
778 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
781 else if (err == UBI_IO_BITFLIPS)
783 else if (err == UBI_IO_PEB_EMPTY)
784 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
785 else if (err == UBI_IO_BAD_EC_HDR) {
787 * We have to also look at the VID header, possibly it is not
788 * corrupted. Set %bitflips flag in order to make this PEB be
789 * moved and EC be re-created.
792 ec = UBI_SCAN_UNKNOWN_EC;
799 /* Make sure UBI version is OK */
800 if (ech->version != UBI_VERSION) {
801 ubi_err("this UBI version is %d, image version is %d",
802 UBI_VERSION, (int)ech->version);
806 ec = be64_to_cpu(ech->ec);
807 if (ec > UBI_MAX_ERASECOUNTER) {
809 * Erase counter overflow. The EC headers have 64 bits
810 * reserved, but we anyway make use of only 31 bit
811 * values, as this seems to be enough for any existing
812 * flash. Upgrade UBI and use 64-bit erase counters
815 ubi_err("erase counter overflow, max is %d",
816 UBI_MAX_ERASECOUNTER);
817 ubi_dbg_dump_ec_hdr(ech);
822 /* OK, we've done with the EC header, let's look at the VID header */
824 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
827 else if (err == UBI_IO_BITFLIPS)
829 else if (err == UBI_IO_BAD_VID_HDR ||
830 (err == UBI_IO_PEB_FREE && ec_corr)) {
831 /* VID header is corrupted */
832 err = add_to_list(si, pnum, ec, &si->corr);
836 } else if (err == UBI_IO_PEB_FREE) {
837 /* No VID header - the physical eraseblock is free */
838 err = add_to_list(si, pnum, ec, &si->free);
844 vol_id = be32_to_cpu(vidh->vol_id);
845 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
846 int lnum = be32_to_cpu(vidh->lnum);
848 /* Unsupported internal volume */
849 switch (vidh->compat) {
850 case UBI_COMPAT_DELETE:
851 ubi_msg("\"delete\" compatible internal volume %d:%d"
852 " found, remove it", vol_id, lnum);
853 err = add_to_list(si, pnum, ec, &si->corr);
859 ubi_msg("read-only compatible internal volume %d:%d"
860 " found, switch to read-only mode",
865 case UBI_COMPAT_PRESERVE:
866 ubi_msg("\"preserve\" compatible internal volume %d:%d"
867 " found", vol_id, lnum);
868 err = add_to_list(si, pnum, ec, &si->alien);
871 si->alien_peb_count += 1;
874 case UBI_COMPAT_REJECT:
875 ubi_err("incompatible internal volume %d:%d found",
881 /* Both UBI headers seem to be fine */
882 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
900 * ubi_scan - scan an MTD device.
901 * @ubi: UBI device description object
903 * This function does full scanning of an MTD device and returns complete
904 * information about it. In case of failure, an error code is returned.
906 struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
909 struct rb_node *rb1, *rb2;
910 struct ubi_scan_volume *sv;
911 struct ubi_scan_leb *seb;
912 struct ubi_scan_info *si;
914 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
916 return ERR_PTR(-ENOMEM);
918 INIT_LIST_HEAD(&si->corr);
919 INIT_LIST_HEAD(&si->free);
920 INIT_LIST_HEAD(&si->erase);
921 INIT_LIST_HEAD(&si->alien);
922 si->volumes = RB_ROOT;
926 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
930 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
934 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
937 dbg_msg("process PEB %d", pnum);
938 err = process_eb(ubi, si, pnum);
943 dbg_msg("scanning is finished");
945 /* Calculate mean erase counter */
947 do_div(si->ec_sum, si->ec_count);
948 si->mean_ec = si->ec_sum;
952 ubi_msg("empty MTD device detected");
955 * In case of unknown erase counter we use the mean erase counter
958 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
959 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
960 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
961 seb->ec = si->mean_ec;
964 list_for_each_entry(seb, &si->free, u.list) {
965 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
966 seb->ec = si->mean_ec;
969 list_for_each_entry(seb, &si->corr, u.list)
970 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
971 seb->ec = si->mean_ec;
973 list_for_each_entry(seb, &si->erase, u.list)
974 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
975 seb->ec = si->mean_ec;
977 err = paranoid_check_si(ubi, si);
984 ubi_free_vid_hdr(ubi, vidh);
990 ubi_free_vid_hdr(ubi, vidh);
994 ubi_scan_destroy_si(si);
999 * destroy_sv - free the scanning volume information
1000 * @sv: scanning volume information
1002 * This function destroys the volume RB-tree (@sv->root) and the scanning
1003 * volume information.
1005 static void destroy_sv(struct ubi_scan_volume *sv)
1007 struct ubi_scan_leb *seb;
1008 struct rb_node *this = sv->root.rb_node;
1012 this = this->rb_left;
1013 else if (this->rb_right)
1014 this = this->rb_right;
1016 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1017 this = rb_parent(this);
1019 if (this->rb_left == &seb->u.rb)
1020 this->rb_left = NULL;
1022 this->rb_right = NULL;
1032 * ubi_scan_destroy_si - destroy scanning information.
1033 * @si: scanning information
1035 void ubi_scan_destroy_si(struct ubi_scan_info *si)
1037 struct ubi_scan_leb *seb, *seb_tmp;
1038 struct ubi_scan_volume *sv;
1041 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1042 list_del(&seb->u.list);
1045 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1046 list_del(&seb->u.list);
1049 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1050 list_del(&seb->u.list);
1053 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1054 list_del(&seb->u.list);
1058 /* Destroy the volume RB-tree */
1059 rb = si->volumes.rb_node;
1063 else if (rb->rb_right)
1066 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1070 if (rb->rb_left == &sv->rb)
1073 rb->rb_right = NULL;
1083 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1086 * paranoid_check_si - check if the scanning information is correct and
1088 * @ubi: UBI device description object
1089 * @si: scanning information
1091 * This function returns zero if the scanning information is all right, %1 if
1092 * not and a negative error code if an error occurred.
1094 static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1096 int pnum, err, vols_found = 0;
1097 struct rb_node *rb1, *rb2;
1098 struct ubi_scan_volume *sv;
1099 struct ubi_scan_leb *seb, *last_seb;
1103 * At first, check that scanning information is OK.
1105 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1113 ubi_err("bad is_empty flag");
1117 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1118 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1119 sv->data_pad < 0 || sv->last_data_size < 0) {
1120 ubi_err("negative values");
1124 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1125 sv->vol_id < UBI_INTERNAL_VOL_START) {
1126 ubi_err("bad vol_id");
1130 if (sv->vol_id > si->highest_vol_id) {
1131 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1132 si->highest_vol_id, sv->vol_id);
1136 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1137 sv->vol_type != UBI_STATIC_VOLUME) {
1138 ubi_err("bad vol_type");
1142 if (sv->data_pad > ubi->leb_size / 2) {
1143 ubi_err("bad data_pad");
1148 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1154 if (seb->pnum < 0 || seb->ec < 0) {
1155 ubi_err("negative values");
1159 if (seb->ec < si->min_ec) {
1160 ubi_err("bad si->min_ec (%d), %d found",
1161 si->min_ec, seb->ec);
1165 if (seb->ec > si->max_ec) {
1166 ubi_err("bad si->max_ec (%d), %d found",
1167 si->max_ec, seb->ec);
1171 if (seb->pnum >= ubi->peb_count) {
1172 ubi_err("too high PEB number %d, total PEBs %d",
1173 seb->pnum, ubi->peb_count);
1177 if (sv->vol_type == UBI_STATIC_VOLUME) {
1178 if (seb->lnum >= sv->used_ebs) {
1179 ubi_err("bad lnum or used_ebs");
1183 if (sv->used_ebs != 0) {
1184 ubi_err("non-zero used_ebs");
1189 if (seb->lnum > sv->highest_lnum) {
1190 ubi_err("incorrect highest_lnum or lnum");
1195 if (sv->leb_count != leb_count) {
1196 ubi_err("bad leb_count, %d objects in the tree",
1206 if (seb->lnum != sv->highest_lnum) {
1207 ubi_err("bad highest_lnum");
1212 if (vols_found != si->vols_found) {
1213 ubi_err("bad si->vols_found %d, should be %d",
1214 si->vols_found, vols_found);
1218 /* Check that scanning information is correct */
1219 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1221 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1228 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1229 if (err && err != UBI_IO_BITFLIPS) {
1230 ubi_err("VID header is not OK (%d)", err);
1236 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1237 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1238 if (sv->vol_type != vol_type) {
1239 ubi_err("bad vol_type");
1243 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
1244 ubi_err("bad sqnum %llu", seb->sqnum);
1248 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
1249 ubi_err("bad vol_id %d", sv->vol_id);
1253 if (sv->compat != vidh->compat) {
1254 ubi_err("bad compat %d", vidh->compat);
1258 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
1259 ubi_err("bad lnum %d", seb->lnum);
1263 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
1264 ubi_err("bad used_ebs %d", sv->used_ebs);
1268 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
1269 ubi_err("bad data_pad %d", sv->data_pad);
1273 if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) {
1274 ubi_err("bad leb_ver %u", seb->leb_ver);
1282 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
1283 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1287 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
1288 ubi_err("bad last_data_size %d", sv->last_data_size);
1294 * Make sure that all the physical eraseblocks are in one of the lists
1297 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
1301 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1302 err = ubi_io_is_bad(ubi, pnum);
1311 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1312 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1315 list_for_each_entry(seb, &si->free, u.list)
1318 list_for_each_entry(seb, &si->corr, u.list)
1321 list_for_each_entry(seb, &si->erase, u.list)
1324 list_for_each_entry(seb, &si->alien, u.list)
1328 for (pnum = 0; pnum < ubi->peb_count; pnum++)
1330 ubi_err("PEB %d is not referred", pnum);
1340 ubi_err("bad scanning information about LEB %d", seb->lnum);
1341 ubi_dbg_dump_seb(seb, 0);
1342 ubi_dbg_dump_sv(sv);
1346 ubi_err("bad scanning information about volume %d", sv->vol_id);
1347 ubi_dbg_dump_sv(sv);
1351 ubi_err("bad scanning information about volume %d", sv->vol_id);
1352 ubi_dbg_dump_sv(sv);
1353 ubi_dbg_dump_vid_hdr(vidh);
1356 ubi_dbg_dump_stack();
1360 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */