2 * Copyright (c) International Business Machines Corp., 2006
4 * SPDX-License-Identifier: GPL-2.0+
6 * Author: Artem Bityutskiy (Битюцкий Артём)
12 * This unit is responsible for scanning the flash media, checking UBI
13 * headers and providing complete information about the UBI flash image.
15 * The scanning information is represented by a &struct ubi_scan_info' object.
16 * Information about found volumes is represented by &struct ubi_scan_volume
17 * objects which are kept in volume RB-tree with root at the @volumes field.
18 * The RB-tree is indexed by the volume ID.
20 * Found logical eraseblocks are represented by &struct ubi_scan_leb objects.
21 * These objects are kept in per-volume RB-trees with the root at the
22 * corresponding &struct ubi_scan_volume object. To put it differently, we keep
23 * an RB-tree of per-volume objects and each of these objects is the root of
24 * RB-tree of per-eraseblock objects.
26 * Corrupted physical eraseblocks are put to the @corr list, free physical
27 * eraseblocks are put to the @free list and the physical eraseblock to be
28 * erased are put to the @erase list.
32 #include <linux/err.h>
33 #include <linux/crc32.h>
34 #include <asm/div64.h>
37 #include <ubi_uboot.h>
40 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
41 static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
43 #define paranoid_check_si(ubi, si) 0
46 /* Temporary variables used during scanning */
47 static struct ubi_ec_hdr *ech;
48 static struct ubi_vid_hdr *vidh;
51 * add_to_list - add physical eraseblock to a list.
52 * @si: scanning information
53 * @pnum: physical eraseblock number to add
54 * @ec: erase counter of the physical eraseblock
55 * @list: the list to add to
57 * This function adds physical eraseblock @pnum to free, erase, corrupted or
58 * alien lists. Returns zero in case of success and a negative error code in
61 static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
62 struct list_head *list)
64 struct ubi_scan_leb *seb;
66 if (list == &si->free)
67 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
68 else if (list == &si->erase)
69 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
70 else if (list == &si->corr)
71 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
72 else if (list == &si->alien)
73 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
77 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
83 list_add_tail(&seb->u.list, list);
88 * validate_vid_hdr - check that volume identifier header is correct and
90 * @vid_hdr: the volume identifier header to check
91 * @sv: information about the volume this logical eraseblock belongs to
92 * @pnum: physical eraseblock number the VID header came from
94 * This function checks that data stored in @vid_hdr is consistent. Returns
95 * non-zero if an inconsistency was found and zero if not.
97 * Note, UBI does sanity check of everything it reads from the flash media.
98 * Most of the checks are done in the I/O unit. Here we check that the
99 * information in the VID header is consistent to the information in other VID
100 * headers of the same volume.
102 static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
103 const struct ubi_scan_volume *sv, int pnum)
105 int vol_type = vid_hdr->vol_type;
106 int vol_id = be32_to_cpu(vid_hdr->vol_id);
107 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
108 int data_pad = be32_to_cpu(vid_hdr->data_pad);
110 if (sv->leb_count != 0) {
114 * This is not the first logical eraseblock belonging to this
115 * volume. Ensure that the data in its VID header is consistent
116 * to the data in previous logical eraseblock headers.
119 if (vol_id != sv->vol_id) {
120 dbg_err("inconsistent vol_id");
124 if (sv->vol_type == UBI_STATIC_VOLUME)
125 sv_vol_type = UBI_VID_STATIC;
127 sv_vol_type = UBI_VID_DYNAMIC;
129 if (vol_type != sv_vol_type) {
130 dbg_err("inconsistent vol_type");
134 if (used_ebs != sv->used_ebs) {
135 dbg_err("inconsistent used_ebs");
139 if (data_pad != sv->data_pad) {
140 dbg_err("inconsistent data_pad");
148 ubi_err("inconsistent VID header at PEB %d", pnum);
149 ubi_dbg_dump_vid_hdr(vid_hdr);
155 * add_volume - add volume to the scanning information.
156 * @si: scanning information
157 * @vol_id: ID of the volume to add
158 * @pnum: physical eraseblock number
159 * @vid_hdr: volume identifier header
161 * If the volume corresponding to the @vid_hdr logical eraseblock is already
162 * present in the scanning information, this function does nothing. Otherwise
163 * it adds corresponding volume to the scanning information. Returns a pointer
164 * to the scanning volume object in case of success and a negative error code
165 * in case of failure.
167 static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
169 const struct ubi_vid_hdr *vid_hdr)
171 struct ubi_scan_volume *sv;
172 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
174 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
176 /* Walk the volume RB-tree to look if this volume is already present */
179 sv = rb_entry(parent, struct ubi_scan_volume, rb);
181 if (vol_id == sv->vol_id)
184 if (vol_id > sv->vol_id)
190 /* The volume is absent - add it */
191 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
193 return ERR_PTR(-ENOMEM);
195 sv->highest_lnum = sv->leb_count = 0;
198 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
199 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
200 sv->compat = vid_hdr->compat;
201 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
203 if (vol_id > si->highest_vol_id)
204 si->highest_vol_id = vol_id;
206 rb_link_node(&sv->rb, parent, p);
207 rb_insert_color(&sv->rb, &si->volumes);
209 dbg_bld("added volume %d", vol_id);
214 * compare_lebs - find out which logical eraseblock is newer.
215 * @ubi: UBI device description object
216 * @seb: first logical eraseblock to compare
217 * @pnum: physical eraseblock number of the second logical eraseblock to
219 * @vid_hdr: volume identifier header of the second logical eraseblock
221 * This function compares 2 copies of a LEB and informs which one is newer. In
222 * case of success this function returns a positive value, in case of failure, a
223 * negative error code is returned. The success return codes use the following
225 * o bit 0 is cleared: the first PEB (described by @seb) is newer then the
226 * second PEB (described by @pnum and @vid_hdr);
227 * o bit 0 is set: the second PEB is newer;
228 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
229 * o bit 1 is set: bit-flips were detected in the newer LEB;
230 * o bit 2 is cleared: the older LEB is not corrupted;
231 * o bit 2 is set: the older LEB is corrupted.
233 static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
234 int pnum, const struct ubi_vid_hdr *vid_hdr)
237 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
238 uint32_t data_crc, crc;
239 struct ubi_vid_hdr *vh = NULL;
240 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
242 if (seb->sqnum == 0 && sqnum2 == 0) {
243 long long abs, v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver);
246 * UBI constantly increases the logical eraseblock version
247 * number and it can overflow. Thus, we have to bear in mind
248 * that versions that are close to %0xFFFFFFFF are less then
249 * versions that are close to %0.
251 * The UBI WL unit guarantees that the number of pending tasks
252 * is not greater then %0x7FFFFFFF. So, if the difference
253 * between any two versions is greater or equivalent to
254 * %0x7FFFFFFF, there was an overflow and the logical
255 * eraseblock with lower version is actually newer then the one
256 * with higher version.
258 * FIXME: but this is anyway obsolete and will be removed at
261 dbg_bld("using old crappy leb_ver stuff");
264 ubi_err("PEB %d and PEB %d have the same version %lld",
265 seb->pnum, pnum, v1);
273 if (abs < 0x7FFFFFFF)
274 /* Non-overflow situation */
275 second_is_newer = (v2 > v1);
277 second_is_newer = (v2 < v1);
279 /* Obviously the LEB with lower sequence counter is older */
280 second_is_newer = sqnum2 > seb->sqnum;
283 * Now we know which copy is newer. If the copy flag of the PEB with
284 * newer version is not set, then we just return, otherwise we have to
285 * check data CRC. For the second PEB we already have the VID header,
286 * for the first one - we'll need to re-read it from flash.
288 * FIXME: this may be optimized so that we wouldn't read twice.
291 if (second_is_newer) {
292 if (!vid_hdr->copy_flag) {
293 /* It is not a copy, so it is newer */
294 dbg_bld("second PEB %d is newer, copy_flag is unset",
301 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
305 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
307 if (err == UBI_IO_BITFLIPS)
310 dbg_err("VID of PEB %d header is bad, but it "
311 "was OK earlier", pnum);
319 if (!vh->copy_flag) {
320 /* It is not a copy, so it is newer */
321 dbg_bld("first PEB %d is newer, copy_flag is unset",
330 /* Read the data of the copy and check the CRC */
332 len = be32_to_cpu(vid_hdr->data_size);
339 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
340 if (err && err != UBI_IO_BITFLIPS)
343 data_crc = be32_to_cpu(vid_hdr->data_crc);
344 crc = crc32(UBI_CRC32_INIT, buf, len);
345 if (crc != data_crc) {
346 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
347 pnum, crc, data_crc);
350 second_is_newer = !second_is_newer;
352 dbg_bld("PEB %d CRC is OK", pnum);
357 ubi_free_vid_hdr(ubi, vh);
360 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
362 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
364 return second_is_newer | (bitflips << 1) | (corrupted << 2);
369 ubi_free_vid_hdr(ubi, vh);
374 * ubi_scan_add_used - add information about a physical eraseblock to the
375 * scanning information.
376 * @ubi: UBI device description object
377 * @si: scanning information
378 * @pnum: the physical eraseblock number
380 * @vid_hdr: the volume identifier header
381 * @bitflips: if bit-flips were detected when this physical eraseblock was read
383 * This function adds information about a used physical eraseblock to the
384 * 'used' tree of the corresponding volume. The function is rather complex
385 * because it has to handle cases when this is not the first physical
386 * eraseblock belonging to the same logical eraseblock, and the newer one has
387 * to be picked, while the older one has to be dropped. This function returns
388 * zero in case of success and a negative error code in case of failure.
390 int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
391 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
394 int err, vol_id, lnum;
396 unsigned long long sqnum;
397 struct ubi_scan_volume *sv;
398 struct ubi_scan_leb *seb;
399 struct rb_node **p, *parent = NULL;
401 vol_id = be32_to_cpu(vid_hdr->vol_id);
402 lnum = be32_to_cpu(vid_hdr->lnum);
403 sqnum = be64_to_cpu(vid_hdr->sqnum);
404 leb_ver = be32_to_cpu(vid_hdr->leb_ver);
406 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d",
407 pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips);
409 sv = add_volume(si, vol_id, pnum, vid_hdr);
413 if (si->max_sqnum < sqnum)
414 si->max_sqnum = sqnum;
417 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
418 * if this is the first instance of this logical eraseblock or not.
420 p = &sv->root.rb_node;
425 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
426 if (lnum != seb->lnum) {
427 if (lnum < seb->lnum)
435 * There is already a physical eraseblock describing the same
436 * logical eraseblock present.
439 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
440 "LEB ver %u, EC %d", seb->pnum, seb->sqnum,
441 seb->leb_ver, seb->ec);
444 * Make sure that the logical eraseblocks have different
445 * versions. Otherwise the image is bad.
447 if (seb->leb_ver == leb_ver && leb_ver != 0) {
448 ubi_err("two LEBs with same version %u", leb_ver);
449 ubi_dbg_dump_seb(seb, 0);
450 ubi_dbg_dump_vid_hdr(vid_hdr);
455 * Make sure that the logical eraseblocks have different
456 * sequence numbers. Otherwise the image is bad.
458 * FIXME: remove 'sqnum != 0' check when leb_ver is removed.
460 if (seb->sqnum == sqnum && sqnum != 0) {
461 ubi_err("two LEBs with same sequence number %llu",
463 ubi_dbg_dump_seb(seb, 0);
464 ubi_dbg_dump_vid_hdr(vid_hdr);
469 * Now we have to drop the older one and preserve the newer
472 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
478 * This logical eraseblock is newer then the one
481 err = validate_vid_hdr(vid_hdr, sv, pnum);
486 err = add_to_list(si, seb->pnum, seb->ec,
489 err = add_to_list(si, seb->pnum, seb->ec,
496 seb->scrub = ((cmp_res & 2) || bitflips);
498 seb->leb_ver = leb_ver;
500 if (sv->highest_lnum == lnum)
502 be32_to_cpu(vid_hdr->data_size);
507 * This logical eraseblock is older then the one found
511 return add_to_list(si, pnum, ec, &si->corr);
513 return add_to_list(si, pnum, ec, &si->erase);
518 * We've met this logical eraseblock for the first time, add it to the
519 * scanning information.
522 err = validate_vid_hdr(vid_hdr, sv, pnum);
526 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
534 seb->scrub = bitflips;
535 seb->leb_ver = leb_ver;
537 if (sv->highest_lnum <= lnum) {
538 sv->highest_lnum = lnum;
539 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
543 rb_link_node(&seb->u.rb, parent, p);
544 rb_insert_color(&seb->u.rb, &sv->root);
549 * ubi_scan_find_sv - find information about a particular volume in the
550 * scanning information.
551 * @si: scanning information
552 * @vol_id: the requested volume ID
554 * This function returns a pointer to the volume description or %NULL if there
555 * are no data about this volume in the scanning information.
557 struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
560 struct ubi_scan_volume *sv;
561 struct rb_node *p = si->volumes.rb_node;
564 sv = rb_entry(p, struct ubi_scan_volume, rb);
566 if (vol_id == sv->vol_id)
569 if (vol_id > sv->vol_id)
579 * ubi_scan_find_seb - find information about a particular logical
580 * eraseblock in the volume scanning information.
581 * @sv: a pointer to the volume scanning information
582 * @lnum: the requested logical eraseblock
584 * This function returns a pointer to the scanning logical eraseblock or %NULL
585 * if there are no data about it in the scanning volume information.
587 struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
590 struct ubi_scan_leb *seb;
591 struct rb_node *p = sv->root.rb_node;
594 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
596 if (lnum == seb->lnum)
599 if (lnum > seb->lnum)
609 * ubi_scan_rm_volume - delete scanning information about a volume.
610 * @si: scanning information
611 * @sv: the volume scanning information to delete
613 void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
616 struct ubi_scan_leb *seb;
618 dbg_bld("remove scanning information about volume %d", sv->vol_id);
620 while ((rb = rb_first(&sv->root))) {
621 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
622 rb_erase(&seb->u.rb, &sv->root);
623 list_add_tail(&seb->u.list, &si->erase);
626 rb_erase(&sv->rb, &si->volumes);
632 * ubi_scan_erase_peb - erase a physical eraseblock.
633 * @ubi: UBI device description object
634 * @si: scanning information
635 * @pnum: physical eraseblock number to erase;
636 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
638 * This function erases physical eraseblock 'pnum', and writes the erase
639 * counter header to it. This function should only be used on UBI device
640 * initialization stages, when the EBA unit had not been yet initialized. This
641 * function returns zero in case of success and a negative error code in case
644 int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
648 struct ubi_ec_hdr *ec_hdr;
650 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
652 * Erase counter overflow. Upgrade UBI and use 64-bit
653 * erase counters internally.
655 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
659 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
663 ec_hdr->ec = cpu_to_be64(ec);
665 err = ubi_io_sync_erase(ubi, pnum, 0);
669 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
677 * ubi_scan_get_free_peb - get a free physical eraseblock.
678 * @ubi: UBI device description object
679 * @si: scanning information
681 * This function returns a free physical eraseblock. It is supposed to be
682 * called on the UBI initialization stages when the wear-leveling unit is not
683 * initialized yet. This function picks a physical eraseblocks from one of the
684 * lists, writes the EC header if it is needed, and removes it from the list.
686 * This function returns scanning physical eraseblock information in case of
687 * success and an error code in case of failure.
689 struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
690 struct ubi_scan_info *si)
693 struct ubi_scan_leb *seb;
695 if (!list_empty(&si->free)) {
696 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
697 list_del(&seb->u.list);
698 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
702 for (i = 0; i < 2; i++) {
703 struct list_head *head;
704 struct ubi_scan_leb *tmp_seb;
712 * We try to erase the first physical eraseblock from the @head
713 * list and pick it if we succeed, or try to erase the
714 * next one if not. And so forth. We don't want to take care
715 * about bad eraseblocks here - they'll be handled later.
717 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
718 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
719 seb->ec = si->mean_ec;
721 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
726 list_del(&seb->u.list);
727 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
732 ubi_err("no eraseblocks found");
733 return ERR_PTR(-ENOSPC);
737 * process_eb - read UBI headers, check them and add corresponding data
738 * to the scanning information.
739 * @ubi: UBI device description object
740 * @si: scanning information
741 * @pnum: the physical eraseblock number
743 * This function returns a zero if the physical eraseblock was successfully
744 * handled and a negative error code in case of failure.
746 static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum)
748 long long uninitialized_var(ec);
749 int err, bitflips = 0, vol_id, ec_corr = 0;
751 dbg_bld("scan PEB %d", pnum);
753 /* Skip bad physical eraseblocks */
754 err = ubi_io_is_bad(ubi, pnum);
759 * FIXME: this is actually duty of the I/O unit to initialize
760 * this, but MTD does not provide enough information.
762 si->bad_peb_count += 1;
766 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
769 else if (err == UBI_IO_BITFLIPS)
771 else if (err == UBI_IO_PEB_EMPTY)
772 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
773 else if (err == UBI_IO_BAD_EC_HDR) {
775 * We have to also look at the VID header, possibly it is not
776 * corrupted. Set %bitflips flag in order to make this PEB be
777 * moved and EC be re-created.
780 ec = UBI_SCAN_UNKNOWN_EC;
787 /* Make sure UBI version is OK */
788 if (ech->version != UBI_VERSION) {
789 ubi_err("this UBI version is %d, image version is %d",
790 UBI_VERSION, (int)ech->version);
794 ec = be64_to_cpu(ech->ec);
795 if (ec > UBI_MAX_ERASECOUNTER) {
797 * Erase counter overflow. The EC headers have 64 bits
798 * reserved, but we anyway make use of only 31 bit
799 * values, as this seems to be enough for any existing
800 * flash. Upgrade UBI and use 64-bit erase counters
803 ubi_err("erase counter overflow, max is %d",
804 UBI_MAX_ERASECOUNTER);
805 ubi_dbg_dump_ec_hdr(ech);
810 /* OK, we've done with the EC header, let's look at the VID header */
812 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
815 else if (err == UBI_IO_BITFLIPS)
817 else if (err == UBI_IO_BAD_VID_HDR ||
818 (err == UBI_IO_PEB_FREE && ec_corr)) {
819 /* VID header is corrupted */
820 err = add_to_list(si, pnum, ec, &si->corr);
824 } else if (err == UBI_IO_PEB_FREE) {
825 /* No VID header - the physical eraseblock is free */
826 err = add_to_list(si, pnum, ec, &si->free);
832 vol_id = be32_to_cpu(vidh->vol_id);
833 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
834 int lnum = be32_to_cpu(vidh->lnum);
836 /* Unsupported internal volume */
837 switch (vidh->compat) {
838 case UBI_COMPAT_DELETE:
839 ubi_msg("\"delete\" compatible internal volume %d:%d"
840 " found, remove it", vol_id, lnum);
841 err = add_to_list(si, pnum, ec, &si->corr);
847 ubi_msg("read-only compatible internal volume %d:%d"
848 " found, switch to read-only mode",
853 case UBI_COMPAT_PRESERVE:
854 ubi_msg("\"preserve\" compatible internal volume %d:%d"
855 " found", vol_id, lnum);
856 err = add_to_list(si, pnum, ec, &si->alien);
859 si->alien_peb_count += 1;
862 case UBI_COMPAT_REJECT:
863 ubi_err("incompatible internal volume %d:%d found",
869 /* Both UBI headers seem to be fine */
870 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
888 * ubi_scan - scan an MTD device.
889 * @ubi: UBI device description object
891 * This function does full scanning of an MTD device and returns complete
892 * information about it. In case of failure, an error code is returned.
894 struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
897 struct rb_node *rb1, *rb2;
898 struct ubi_scan_volume *sv;
899 struct ubi_scan_leb *seb;
900 struct ubi_scan_info *si;
902 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
904 return ERR_PTR(-ENOMEM);
906 INIT_LIST_HEAD(&si->corr);
907 INIT_LIST_HEAD(&si->free);
908 INIT_LIST_HEAD(&si->erase);
909 INIT_LIST_HEAD(&si->alien);
910 si->volumes = RB_ROOT;
914 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
918 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
922 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
925 dbg_msg("process PEB %d", pnum);
926 err = process_eb(ubi, si, pnum);
931 dbg_msg("scanning is finished");
933 /* Calculate mean erase counter */
935 do_div(si->ec_sum, si->ec_count);
936 si->mean_ec = si->ec_sum;
940 ubi_msg("empty MTD device detected");
943 * In case of unknown erase counter we use the mean erase counter
946 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
947 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
948 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
949 seb->ec = si->mean_ec;
952 list_for_each_entry(seb, &si->free, u.list) {
953 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
954 seb->ec = si->mean_ec;
957 list_for_each_entry(seb, &si->corr, u.list)
958 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
959 seb->ec = si->mean_ec;
961 list_for_each_entry(seb, &si->erase, u.list)
962 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
963 seb->ec = si->mean_ec;
965 err = paranoid_check_si(ubi, si);
972 ubi_free_vid_hdr(ubi, vidh);
978 ubi_free_vid_hdr(ubi, vidh);
982 ubi_scan_destroy_si(si);
987 * destroy_sv - free the scanning volume information
988 * @sv: scanning volume information
990 * This function destroys the volume RB-tree (@sv->root) and the scanning
991 * volume information.
993 static void destroy_sv(struct ubi_scan_volume *sv)
995 struct ubi_scan_leb *seb;
996 struct rb_node *this = sv->root.rb_node;
1000 this = this->rb_left;
1001 else if (this->rb_right)
1002 this = this->rb_right;
1004 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1005 this = rb_parent(this);
1007 if (this->rb_left == &seb->u.rb)
1008 this->rb_left = NULL;
1010 this->rb_right = NULL;
1020 * ubi_scan_destroy_si - destroy scanning information.
1021 * @si: scanning information
1023 void ubi_scan_destroy_si(struct ubi_scan_info *si)
1025 struct ubi_scan_leb *seb, *seb_tmp;
1026 struct ubi_scan_volume *sv;
1029 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1030 list_del(&seb->u.list);
1033 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1034 list_del(&seb->u.list);
1037 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1038 list_del(&seb->u.list);
1041 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1042 list_del(&seb->u.list);
1046 /* Destroy the volume RB-tree */
1047 rb = si->volumes.rb_node;
1051 else if (rb->rb_right)
1054 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1058 if (rb->rb_left == &sv->rb)
1061 rb->rb_right = NULL;
1071 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1074 * paranoid_check_si - check if the scanning information is correct and
1076 * @ubi: UBI device description object
1077 * @si: scanning information
1079 * This function returns zero if the scanning information is all right, %1 if
1080 * not and a negative error code if an error occurred.
1082 static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1084 int pnum, err, vols_found = 0;
1085 struct rb_node *rb1, *rb2;
1086 struct ubi_scan_volume *sv;
1087 struct ubi_scan_leb *seb, *last_seb;
1091 * At first, check that scanning information is OK.
1093 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1101 ubi_err("bad is_empty flag");
1105 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1106 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1107 sv->data_pad < 0 || sv->last_data_size < 0) {
1108 ubi_err("negative values");
1112 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1113 sv->vol_id < UBI_INTERNAL_VOL_START) {
1114 ubi_err("bad vol_id");
1118 if (sv->vol_id > si->highest_vol_id) {
1119 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1120 si->highest_vol_id, sv->vol_id);
1124 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1125 sv->vol_type != UBI_STATIC_VOLUME) {
1126 ubi_err("bad vol_type");
1130 if (sv->data_pad > ubi->leb_size / 2) {
1131 ubi_err("bad data_pad");
1136 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1142 if (seb->pnum < 0 || seb->ec < 0) {
1143 ubi_err("negative values");
1147 if (seb->ec < si->min_ec) {
1148 ubi_err("bad si->min_ec (%d), %d found",
1149 si->min_ec, seb->ec);
1153 if (seb->ec > si->max_ec) {
1154 ubi_err("bad si->max_ec (%d), %d found",
1155 si->max_ec, seb->ec);
1159 if (seb->pnum >= ubi->peb_count) {
1160 ubi_err("too high PEB number %d, total PEBs %d",
1161 seb->pnum, ubi->peb_count);
1165 if (sv->vol_type == UBI_STATIC_VOLUME) {
1166 if (seb->lnum >= sv->used_ebs) {
1167 ubi_err("bad lnum or used_ebs");
1171 if (sv->used_ebs != 0) {
1172 ubi_err("non-zero used_ebs");
1177 if (seb->lnum > sv->highest_lnum) {
1178 ubi_err("incorrect highest_lnum or lnum");
1183 if (sv->leb_count != leb_count) {
1184 ubi_err("bad leb_count, %d objects in the tree",
1194 if (seb->lnum != sv->highest_lnum) {
1195 ubi_err("bad highest_lnum");
1200 if (vols_found != si->vols_found) {
1201 ubi_err("bad si->vols_found %d, should be %d",
1202 si->vols_found, vols_found);
1206 /* Check that scanning information is correct */
1207 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1209 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1216 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1217 if (err && err != UBI_IO_BITFLIPS) {
1218 ubi_err("VID header is not OK (%d)", err);
1224 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1225 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1226 if (sv->vol_type != vol_type) {
1227 ubi_err("bad vol_type");
1231 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
1232 ubi_err("bad sqnum %llu", seb->sqnum);
1236 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
1237 ubi_err("bad vol_id %d", sv->vol_id);
1241 if (sv->compat != vidh->compat) {
1242 ubi_err("bad compat %d", vidh->compat);
1246 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
1247 ubi_err("bad lnum %d", seb->lnum);
1251 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
1252 ubi_err("bad used_ebs %d", sv->used_ebs);
1256 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
1257 ubi_err("bad data_pad %d", sv->data_pad);
1261 if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) {
1262 ubi_err("bad leb_ver %u", seb->leb_ver);
1270 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
1271 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1275 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
1276 ubi_err("bad last_data_size %d", sv->last_data_size);
1282 * Make sure that all the physical eraseblocks are in one of the lists
1285 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
1289 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1290 err = ubi_io_is_bad(ubi, pnum);
1299 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1300 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1303 list_for_each_entry(seb, &si->free, u.list)
1306 list_for_each_entry(seb, &si->corr, u.list)
1309 list_for_each_entry(seb, &si->erase, u.list)
1312 list_for_each_entry(seb, &si->alien, u.list)
1316 for (pnum = 0; pnum < ubi->peb_count; pnum++)
1318 ubi_err("PEB %d is not referred", pnum);
1328 ubi_err("bad scanning information about LEB %d", seb->lnum);
1329 ubi_dbg_dump_seb(seb, 0);
1330 ubi_dbg_dump_sv(sv);
1334 ubi_err("bad scanning information about volume %d", sv->vol_id);
1335 ubi_dbg_dump_sv(sv);
1339 ubi_err("bad scanning information about volume %d", sv->vol_id);
1340 ubi_dbg_dump_sv(sv);
1341 ubi_dbg_dump_vid_hdr(vidh);
1344 ubi_dbg_dump_stack();
1348 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */