Lines Matching +full:ubi +full:- +full:volume +full:-
1 // SPDX-License-Identifier: GPL-2.0-or-later
9 * UBI attaching sub-system.
11 * This sub-system is responsible for attaching MTD devices and it also
16 * objects which are kept in volume RB-tree with root at the @volumes field.
17 * The RB-tree is indexed by the volume ID.
20 * objects are kept in per-volume RB-trees with the root at the corresponding
21 * &struct ubi_ainf_volume object. To put it differently, we keep an RB-tree of
22 * per-volume objects and each of these objects is the root of RB-tree of
23 * per-LEB objects.
32 * UBI protects EC and VID headers with CRC-32 checksums, so it can detect
33 * whether the headers are corrupted or not. Sometimes UBI also protects the
34 * data with CRC-32, e.g., when it executes the atomic LEB change operation, or
35 * when it moves the contents of a PEB for wear-leveling purposes.
37 * UBI tries to distinguish between 2 types of corruptions.
39 * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
42 * cases - we may lose only the data which were being written to the media just
46 * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
47 * the reason is a power cut, UBI puts this PEB to the @erase list, and all
51 * attaching, such PEBs are put to the @corr list and UBI preserves them.
53 * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
57 * corruptions and UBI's strategy is as follows (in case of attaching by
58 * scanning). UBI assumes corruption type 2 if the VID header is corrupted and
59 * the data area does not contain all 0xFFs, and there were no bit-flips or
61 * area. Otherwise UBI assumes corruption type 1. So the decision criteria
64 * to just erase this PEB - this is corruption type 1.
65 * o If the data area has bit-flips or data integrity errors (ECC errors on
77 #include "ubi.h"
79 static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai);
86 * find_or_add_av - internal function to find a volume, add a volume or do
89 * @vol_id: the requested volume ID
91 * expected operation. If only %AV_ADD is set, -EEXIST is returned
92 * if the volume already exists. If only %AV_FIND is set, NULL is
93 * returned if the volume does not exist. And if both flags are
94 * set, the helper first tries to find an existing volume, and if
97 * volume or not.
99 * This function returns a pointer to a volume description or an ERR_PTR if
101 * the volume does not exist.
108 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; in find_or_add_av()
110 /* Walk the volume RB-tree to look if this volume is already present */ in find_or_add_av()
115 if (vol_id == av->vol_id) { in find_or_add_av()
119 return ERR_PTR(-EEXIST); in find_or_add_av()
124 if (vol_id > av->vol_id) in find_or_add_av()
125 p = &(*p)->rb_left; in find_or_add_av()
127 p = &(*p)->rb_right; in find_or_add_av()
133 /* The volume is absent - add it */ in find_or_add_av()
136 return ERR_PTR(-ENOMEM); in find_or_add_av()
138 av->vol_id = vol_id; in find_or_add_av()
140 if (vol_id > ai->highest_vol_id) in find_or_add_av()
141 ai->highest_vol_id = vol_id; in find_or_add_av()
143 rb_link_node(&av->rb, parent, p); in find_or_add_av()
144 rb_insert_color(&av->rb, &ai->volumes); in find_or_add_av()
145 ai->vols_found += 1; in find_or_add_av()
147 dbg_bld("added volume %d", vol_id); in find_or_add_av()
152 * ubi_find_or_add_av - search for a volume in the attaching information and
155 * @vol_id: the requested volume ID
156 * @created: whether the volume has been created or not
158 * This function returns a pointer to the new volume description or an
168 * ubi_alloc_aeb - allocate an aeb element
183 aeb = kmem_cache_zalloc(ai->aeb_slab_cache, GFP_KERNEL); in ubi_alloc_aeb()
187 aeb->pnum = pnum; in ubi_alloc_aeb()
188 aeb->ec = ec; in ubi_alloc_aeb()
189 aeb->vol_id = UBI_UNKNOWN; in ubi_alloc_aeb()
190 aeb->lnum = UBI_UNKNOWN; in ubi_alloc_aeb()
196 * ubi_free_aeb - free an aeb element
205 kmem_cache_free(ai->aeb_slab_cache, aeb); in ubi_free_aeb()
209 * add_to_list - add physical eraseblock to a list.
212 * @vol_id: the last used volume id for the PEB
234 if (list == &ai->free) { in add_to_list()
236 } else if (list == &ai->erase) { in add_to_list()
238 } else if (list == &ai->alien) { in add_to_list()
240 ai->alien_peb_count += 1; in add_to_list()
246 return -ENOMEM; in add_to_list()
248 aeb->vol_id = vol_id; in add_to_list()
249 aeb->lnum = lnum; in add_to_list()
251 list_add(&aeb->u.list, list); in add_to_list()
253 list_add_tail(&aeb->u.list, list); in add_to_list()
258 * add_corrupted - add a corrupted physical eraseblock.
276 return -ENOMEM; in add_corrupted()
278 ai->corr_peb_count += 1; in add_corrupted()
279 list_add(&aeb->u.list, &ai->corr); in add_corrupted()
284 * add_fastmap - add a Fastmap related physical eraseblock.
287 * @vid_hdr: the volume identifier header
303 return -ENOMEM; in add_fastmap()
305 aeb->vol_id = be32_to_cpu(vid_hdr->vol_id); in add_fastmap()
306 aeb->sqnum = be64_to_cpu(vid_hdr->sqnum); in add_fastmap()
307 list_add(&aeb->u.list, &ai->fastmap); in add_fastmap()
310 aeb->vol_id, aeb->sqnum); in add_fastmap()
316 * validate_vid_hdr - check volume identifier header.
317 * @ubi: UBI device description object
318 * @vid_hdr: the volume identifier header to check
319 * @av: information about the volume this logical eraseblock belongs to
323 * non-zero if an inconsistency was found and zero if not.
325 * Note, UBI does sanity check of everything it reads from the flash media.
326 * Most of the checks are done in the I/O sub-system. Here we check that the
328 * headers of the same volume.
330 static int validate_vid_hdr(const struct ubi_device *ubi, in validate_vid_hdr() argument
334 int vol_type = vid_hdr->vol_type; in validate_vid_hdr()
335 int vol_id = be32_to_cpu(vid_hdr->vol_id); in validate_vid_hdr()
336 int used_ebs = be32_to_cpu(vid_hdr->used_ebs); in validate_vid_hdr()
337 int data_pad = be32_to_cpu(vid_hdr->data_pad); in validate_vid_hdr()
339 if (av->leb_count != 0) { in validate_vid_hdr()
344 * volume. Ensure that the data in its VID header is consistent in validate_vid_hdr()
348 if (vol_id != av->vol_id) { in validate_vid_hdr()
349 ubi_err(ubi, "inconsistent vol_id"); in validate_vid_hdr()
353 if (av->vol_type == UBI_STATIC_VOLUME) in validate_vid_hdr()
359 ubi_err(ubi, "inconsistent vol_type"); in validate_vid_hdr()
363 if (used_ebs != av->used_ebs) { in validate_vid_hdr()
364 ubi_err(ubi, "inconsistent used_ebs"); in validate_vid_hdr()
368 if (data_pad != av->data_pad) { in validate_vid_hdr()
369 ubi_err(ubi, "inconsistent data_pad"); in validate_vid_hdr()
377 ubi_err(ubi, "inconsistent VID header at PEB %d", pnum); in validate_vid_hdr()
380 return -EINVAL; in validate_vid_hdr()
384 * add_volume - add volume to the attaching information.
386 * @vol_id: ID of the volume to add
388 * @vid_hdr: volume identifier header
390 * If the volume corresponding to the @vid_hdr logical eraseblock is already
392 * it adds corresponding volume to the attaching information. Returns a pointer
403 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id)); in add_volume()
409 av->used_ebs = be32_to_cpu(vid_hdr->used_ebs); in add_volume()
410 av->data_pad = be32_to_cpu(vid_hdr->data_pad); in add_volume()
411 av->compat = vid_hdr->compat; in add_volume()
412 av->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME in add_volume()
419 * ubi_compare_lebs - find out which logical eraseblock is newer.
420 * @ubi: UBI device description object
424 * @vid_hdr: volume identifier header of the second logical eraseblock
433 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
434 * o bit 1 is set: bit-flips were detected in the newer LEB;
438 int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb, in ubi_compare_lebs() argument
444 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); in ubi_compare_lebs()
446 if (sqnum2 == aeb->sqnum) { in ubi_compare_lebs()
448 * This must be a really ancient UBI image which has been in ubi_compare_lebs()
450 * that times we used 32-bit LEB versions stored in logical in ubi_compare_lebs()
451 * eraseblocks. That was before UBI got into mainline. We do not in ubi_compare_lebs()
455 ubi_err(ubi, "unsupported on-flash UBI format"); in ubi_compare_lebs()
456 return -EINVAL; in ubi_compare_lebs()
460 second_is_newer = (sqnum2 > aeb->sqnum); in ubi_compare_lebs()
466 * for the first one - we'll need to re-read it from flash. in ubi_compare_lebs()
472 if (!vid_hdr->copy_flag) { in ubi_compare_lebs()
479 if (!aeb->copy_flag) { in ubi_compare_lebs()
486 vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in ubi_compare_lebs()
488 return -ENOMEM; in ubi_compare_lebs()
490 pnum = aeb->pnum; in ubi_compare_lebs()
491 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0); in ubi_compare_lebs()
496 ubi_err(ubi, "VID of PEB %d header is bad, but it was OK earlier, err %d", in ubi_compare_lebs()
499 err = -EIO; in ubi_compare_lebs()
510 len = be32_to_cpu(vid_hdr->data_size); in ubi_compare_lebs()
512 mutex_lock(&ubi->buf_mutex); in ubi_compare_lebs()
513 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len); in ubi_compare_lebs()
517 data_crc = be32_to_cpu(vid_hdr->data_crc); in ubi_compare_lebs()
518 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, len); in ubi_compare_lebs()
529 mutex_unlock(&ubi->buf_mutex); in ubi_compare_lebs()
541 mutex_unlock(&ubi->buf_mutex); in ubi_compare_lebs()
548 * ubi_add_to_av - add used physical eraseblock to the attaching information.
549 * @ubi: UBI device description object
553 * @vid_hdr: the volume identifier header
554 * @bitflips: if bit-flips were detected when this physical eraseblock was read
557 * 'used' tree of the corresponding volume. The function is rather complex
563 int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum, in ubi_add_to_av() argument
572 vol_id = be32_to_cpu(vid_hdr->vol_id); in ubi_add_to_av()
573 lnum = be32_to_cpu(vid_hdr->lnum); in ubi_add_to_av()
574 sqnum = be64_to_cpu(vid_hdr->sqnum); in ubi_add_to_av()
583 if (ai->max_sqnum < sqnum) in ubi_add_to_av()
584 ai->max_sqnum = sqnum; in ubi_add_to_av()
587 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look in ubi_add_to_av()
590 p = &av->root.rb_node; in ubi_add_to_av()
596 if (lnum != aeb->lnum) { in ubi_add_to_av()
597 if (lnum < aeb->lnum) in ubi_add_to_av()
598 p = &(*p)->rb_left; in ubi_add_to_av()
600 p = &(*p)->rb_right; in ubi_add_to_av()
610 aeb->pnum, aeb->sqnum, aeb->ec); in ubi_add_to_av()
617 * be an ancient UBI image from the era when UBI did not have in ubi_add_to_av()
625 if (aeb->sqnum == sqnum && sqnum != 0) { in ubi_add_to_av()
626 ubi_err(ubi, "two LEBs with same sequence number %llu", in ubi_add_to_av()
630 return -EINVAL; in ubi_add_to_av()
637 cmp_res = ubi_compare_lebs(ubi, aeb, pnum, vid_hdr); in ubi_add_to_av()
646 err = validate_vid_hdr(ubi, vid_hdr, av, pnum); in ubi_add_to_av()
650 err = add_to_list(ai, aeb->pnum, aeb->vol_id, in ubi_add_to_av()
651 aeb->lnum, aeb->ec, cmp_res & 4, in ubi_add_to_av()
652 &ai->erase); in ubi_add_to_av()
656 aeb->ec = ec; in ubi_add_to_av()
657 aeb->pnum = pnum; in ubi_add_to_av()
658 aeb->vol_id = vol_id; in ubi_add_to_av()
659 aeb->lnum = lnum; in ubi_add_to_av()
660 aeb->scrub = ((cmp_res & 2) || bitflips); in ubi_add_to_av()
661 aeb->copy_flag = vid_hdr->copy_flag; in ubi_add_to_av()
662 aeb->sqnum = sqnum; in ubi_add_to_av()
664 if (av->highest_lnum == lnum) in ubi_add_to_av()
665 av->last_data_size = in ubi_add_to_av()
666 be32_to_cpu(vid_hdr->data_size); in ubi_add_to_av()
675 cmp_res & 4, &ai->erase); in ubi_add_to_av()
684 err = validate_vid_hdr(ubi, vid_hdr, av, pnum); in ubi_add_to_av()
690 return -ENOMEM; in ubi_add_to_av()
692 aeb->vol_id = vol_id; in ubi_add_to_av()
693 aeb->lnum = lnum; in ubi_add_to_av()
694 aeb->scrub = bitflips; in ubi_add_to_av()
695 aeb->copy_flag = vid_hdr->copy_flag; in ubi_add_to_av()
696 aeb->sqnum = sqnum; in ubi_add_to_av()
698 if (av->highest_lnum <= lnum) { in ubi_add_to_av()
699 av->highest_lnum = lnum; in ubi_add_to_av()
700 av->last_data_size = be32_to_cpu(vid_hdr->data_size); in ubi_add_to_av()
703 av->leb_count += 1; in ubi_add_to_av()
704 rb_link_node(&aeb->u.rb, parent, p); in ubi_add_to_av()
705 rb_insert_color(&aeb->u.rb, &av->root); in ubi_add_to_av()
710 * ubi_add_av - add volume to the attaching information.
712 * @vol_id: the requested volume ID
714 * This function returns a pointer to the new volume description or an
725 * ubi_find_av - find volume in the attaching information.
727 * @vol_id: the requested volume ID
729 * This function returns a pointer to the volume description or %NULL if there
730 * are no data about this volume in the attaching information.
745 * ubi_remove_av - delete attaching information about a volume.
747 * @av: the volume attaching information to delete
751 dbg_bld("remove attaching information about volume %d", av->vol_id); in ubi_remove_av()
753 rb_erase(&av->rb, &ai->volumes); in ubi_remove_av()
754 destroy_av(ai, av, &ai->erase); in ubi_remove_av()
755 ai->vols_found -= 1; in ubi_remove_av()
759 * early_erase_peb - erase a physical eraseblock.
760 * @ubi: UBI device description object
766 * counter header to it. This function should only be used on UBI device
767 * initialization stages, when the EBA sub-system had not been yet initialized.
771 static int early_erase_peb(struct ubi_device *ubi, in early_erase_peb() argument
779 * Erase counter overflow. Upgrade UBI and use 64-bit in early_erase_peb()
782 ubi_err(ubi, "erase counter overflow at PEB %d, EC %d", in early_erase_peb()
784 return -EINVAL; in early_erase_peb()
787 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in early_erase_peb()
789 return -ENOMEM; in early_erase_peb()
791 ec_hdr->ec = cpu_to_be64(ec); in early_erase_peb()
793 err = ubi_io_sync_erase(ubi, pnum, 0); in early_erase_peb()
797 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); in early_erase_peb()
805 * ubi_early_get_peb - get a free physical eraseblock.
806 * @ubi: UBI device description object
810 * called on the UBI initialization stages when the wear-leveling sub-system is
818 struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi, in ubi_early_get_peb() argument
824 if (!list_empty(&ai->free)) { in ubi_early_get_peb()
825 aeb = list_entry(ai->free.next, struct ubi_ainf_peb, u.list); in ubi_early_get_peb()
826 list_del(&aeb->u.list); in ubi_early_get_peb()
827 dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec); in ubi_early_get_peb()
834 * so forth. We don't want to take care about bad eraseblocks here - in ubi_early_get_peb()
837 list_for_each_entry_safe(aeb, tmp_aeb, &ai->erase, u.list) { in ubi_early_get_peb()
838 if (aeb->ec == UBI_UNKNOWN) in ubi_early_get_peb()
839 aeb->ec = ai->mean_ec; in ubi_early_get_peb()
841 err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1); in ubi_early_get_peb()
845 aeb->ec += 1; in ubi_early_get_peb()
846 list_del(&aeb->u.list); in ubi_early_get_peb()
847 dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec); in ubi_early_get_peb()
851 ubi_err(ubi, "no free eraseblocks"); in ubi_early_get_peb()
852 return ERR_PTR(-ENOSPC); in ubi_early_get_peb()
856 * check_corruption - check the data area of PEB.
857 * @ubi: UBI device description object
868 * If the corruption reason was a power cut, UBI can safely erase this PEB.
872 static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr, in check_corruption() argument
877 mutex_lock(&ubi->buf_mutex); in check_corruption()
878 memset(ubi->peb_buf, 0x00, ubi->leb_size); in check_corruption()
880 err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start, in check_corruption()
881 ubi->leb_size); in check_corruption()
884 * Bit-flips or integrity errors while reading the data area. in check_corruption()
897 if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size)) in check_corruption()
900 ubi_err(ubi, "PEB %d contains corrupted VID header, and the data does not contain all 0xFF", in check_corruption()
902 …ubi_err(ubi, "this may be a non-UBI PEB or a severe VID header corruption which requires manual in… in check_corruption()
905 pnum, ubi->leb_start, ubi->leb_size); in check_corruption()
907 ubi->peb_buf, ubi->leb_size, 1); in check_corruption()
911 mutex_unlock(&ubi->buf_mutex); in check_corruption()
930 * scan_peb - scan and process UBI headers of a PEB.
931 * @ubi: UBI device description object
936 * This function reads UBI headers of PEB @pnum, checks them, and adds
937 * information about this PEB to the corresponding list or RB-tree in the
941 static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai, in scan_peb() argument
944 struct ubi_ec_hdr *ech = ai->ech; in scan_peb()
945 struct ubi_vid_io_buf *vidb = ai->vidb; in scan_peb()
948 int err, bitflips = 0, vol_id = -1, ec_err = 0; in scan_peb()
953 err = ubi_io_is_bad(ubi, pnum); in scan_peb()
957 ai->bad_peb_count += 1; in scan_peb()
961 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); in scan_peb()
971 ai->empty_peb_count += 1; in scan_peb()
973 UBI_UNKNOWN, 0, &ai->erase); in scan_peb()
975 ai->empty_peb_count += 1; in scan_peb()
977 UBI_UNKNOWN, 1, &ai->erase); in scan_peb()
983 * moved and EC be re-created. in scan_peb()
990 ubi_err(ubi, "'ubi_io_read_ec_hdr()' returned unknown code %d", in scan_peb()
992 return -EINVAL; in scan_peb()
998 /* Make sure UBI version is OK */ in scan_peb()
999 if (ech->version != UBI_VERSION) { in scan_peb()
1000 ubi_err(ubi, "this UBI version is %d, image version is %d", in scan_peb()
1001 UBI_VERSION, (int)ech->version); in scan_peb()
1002 return -EINVAL; in scan_peb()
1005 ec = be64_to_cpu(ech->ec); in scan_peb()
1011 * flash. Upgrade UBI and use 64-bit erase counters in scan_peb()
1014 ubi_err(ubi, "erase counter overflow, max is %d", in scan_peb()
1017 return -EINVAL; in scan_peb()
1022 * This allows us to detect situations when users flash UBI in scan_peb()
1023 * images incorrectly, so that the flash has the new UBI image in scan_peb()
1026 * zero, because old UBI implementations always set it to zero. in scan_peb()
1028 * sequence number, while other PEBs have non-zero sequence in scan_peb()
1031 image_seq = be32_to_cpu(ech->image_seq); in scan_peb()
1032 if (!ubi->image_seq) in scan_peb()
1033 ubi->image_seq = image_seq; in scan_peb()
1034 if (image_seq && ubi->image_seq != image_seq) { in scan_peb()
1035 ubi_err(ubi, "bad image sequence number %d in PEB %d, expected %d", in scan_peb()
1036 image_seq, pnum, ubi->image_seq); in scan_peb()
1038 return -EINVAL; in scan_peb()
1044 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0); in scan_peb()
1061 ai->maybe_bad_peb_count += 1; in scan_peb()
1076 ai->force_full_scan = 1; in scan_peb()
1081 * that this a valid UBI PEB which has corresponding in scan_peb()
1097 err = check_corruption(ubi, vidh, pnum); in scan_peb()
1104 UBI_UNKNOWN, ec, 1, &ai->erase); in scan_peb()
1113 ec, 1, &ai->erase); in scan_peb()
1120 UBI_UNKNOWN, ec, 1, &ai->erase); in scan_peb()
1123 UBI_UNKNOWN, ec, 0, &ai->free); in scan_peb()
1128 ubi_err(ubi, "'ubi_io_read_vid_hdr()' returned unknown code %d", in scan_peb()
1130 return -EINVAL; in scan_peb()
1133 vol_id = be32_to_cpu(vidh->vol_id); in scan_peb()
1135 int lnum = be32_to_cpu(vidh->lnum); in scan_peb()
1137 /* Unsupported internal volume */ in scan_peb()
1138 switch (vidh->compat) { in scan_peb()
1140 ubi_msg(ubi, "\"delete\" compatible internal volume %d:%d found, will remove it", in scan_peb()
1144 ec, 1, &ai->erase); in scan_peb()
1150 ubi_msg(ubi, "read-only compatible internal volume %d:%d found, switch to read-only mode", in scan_peb()
1152 ubi->ro_mode = 1; in scan_peb()
1156 ubi_msg(ubi, "\"preserve\" compatible internal volume %d:%d found", in scan_peb()
1159 ec, 0, &ai->alien); in scan_peb()
1165 ubi_err(ubi, "incompatible internal volume %d:%d found", in scan_peb()
1167 return -EINVAL; in scan_peb()
1172 ubi_warn(ubi, "valid VID header but corrupted EC header at PEB %d", in scan_peb()
1178 err = ubi_add_to_av(ubi, ai, pnum, ec, vidh, bitflips); in scan_peb()
1185 ai->ec_sum += ec; in scan_peb()
1186 ai->ec_count += 1; in scan_peb()
1187 if (ec > ai->max_ec) in scan_peb()
1188 ai->max_ec = ec; in scan_peb()
1189 if (ec < ai->min_ec) in scan_peb()
1190 ai->min_ec = ec; in scan_peb()
1197 * late_analysis - analyze the overall situation with PEB.
1198 * @ubi: UBI device description object
1205 * should proceed with attaching the MTD device, and %-EINVAL if we should not.
1207 static int late_analysis(struct ubi_device *ubi, struct ubi_attach_info *ai) in late_analysis() argument
1212 peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count; in late_analysis()
1220 if (ai->corr_peb_count) { in late_analysis()
1221 ubi_err(ubi, "%d PEBs are corrupted and preserved", in late_analysis()
1222 ai->corr_peb_count); in late_analysis()
1224 list_for_each_entry(aeb, &ai->corr, u.list) in late_analysis()
1225 pr_cont(" %d", aeb->pnum); in late_analysis()
1232 if (ai->corr_peb_count >= max_corr) { in late_analysis()
1233 ubi_err(ubi, "too many corrupted PEBs, refusing"); in late_analysis()
1234 return -EINVAL; in late_analysis()
1238 if (ai->empty_peb_count + ai->maybe_bad_peb_count == peb_count) { in late_analysis()
1240 * All PEBs are empty, or almost all - a couple PEBs look like in late_analysis()
1251 * 2. Flash contains non-UBI data and we do not want to format in late_analysis()
1254 if (ai->maybe_bad_peb_count <= 2) { in late_analysis()
1255 ai->is_empty = 1; in late_analysis()
1256 ubi_msg(ubi, "empty MTD device detected"); in late_analysis()
1257 get_random_bytes(&ubi->image_seq, in late_analysis()
1258 sizeof(ubi->image_seq)); in late_analysis()
1260 ubi_err(ubi, "MTD device is not UBI-formatted and possibly contains non-UBI data - refusing it"); in late_analysis()
1261 return -EINVAL; in late_analysis()
1270 * destroy_av - free volume attaching information.
1271 * @av: volume attaching information
1275 * This function destroys the volume attaching information.
1281 struct rb_node *this = av->root.rb_node; in destroy_av()
1284 if (this->rb_left) in destroy_av()
1285 this = this->rb_left; in destroy_av()
1286 else if (this->rb_right) in destroy_av()
1287 this = this->rb_right; in destroy_av()
1292 if (this->rb_left == &aeb->u.rb) in destroy_av()
1293 this->rb_left = NULL; in destroy_av()
1295 this->rb_right = NULL; in destroy_av()
1299 list_add_tail(&aeb->u.list, list); in destroy_av()
1308 * destroy_ai - destroy attaching information.
1317 list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) { in destroy_ai()
1318 list_del(&aeb->u.list); in destroy_ai()
1321 list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) { in destroy_ai()
1322 list_del(&aeb->u.list); in destroy_ai()
1325 list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) { in destroy_ai()
1326 list_del(&aeb->u.list); in destroy_ai()
1329 list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) { in destroy_ai()
1330 list_del(&aeb->u.list); in destroy_ai()
1333 list_for_each_entry_safe(aeb, aeb_tmp, &ai->fastmap, u.list) { in destroy_ai()
1334 list_del(&aeb->u.list); in destroy_ai()
1338 /* Destroy the volume RB-tree */ in destroy_ai()
1339 rb = ai->volumes.rb_node; in destroy_ai()
1341 if (rb->rb_left) in destroy_ai()
1342 rb = rb->rb_left; in destroy_ai()
1343 else if (rb->rb_right) in destroy_ai()
1344 rb = rb->rb_right; in destroy_ai()
1350 if (rb->rb_left == &av->rb) in destroy_ai()
1351 rb->rb_left = NULL; in destroy_ai()
1353 rb->rb_right = NULL; in destroy_ai()
1360 kmem_cache_destroy(ai->aeb_slab_cache); in destroy_ai()
1365 * scan_all - scan entire MTD device.
1366 * @ubi: UBI device description object
1374 static int scan_all(struct ubi_device *ubi, struct ubi_attach_info *ai, in scan_all() argument
1382 err = -ENOMEM; in scan_all()
1384 ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in scan_all()
1385 if (!ai->ech) in scan_all()
1388 ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in scan_all()
1389 if (!ai->vidb) in scan_all()
1392 for (pnum = start; pnum < ubi->peb_count; pnum++) { in scan_all()
1396 err = scan_peb(ubi, ai, pnum, false); in scan_all()
1401 ubi_msg(ubi, "scanning is finished"); in scan_all()
1404 if (ai->ec_count) in scan_all()
1405 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count); in scan_all()
1407 err = late_analysis(ubi, ai); in scan_all()
1415 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { in scan_all()
1416 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) in scan_all()
1417 if (aeb->ec == UBI_UNKNOWN) in scan_all()
1418 aeb->ec = ai->mean_ec; in scan_all()
1421 list_for_each_entry(aeb, &ai->free, u.list) { in scan_all()
1422 if (aeb->ec == UBI_UNKNOWN) in scan_all()
1423 aeb->ec = ai->mean_ec; in scan_all()
1426 list_for_each_entry(aeb, &ai->corr, u.list) in scan_all()
1427 if (aeb->ec == UBI_UNKNOWN) in scan_all()
1428 aeb->ec = ai->mean_ec; in scan_all()
1430 list_for_each_entry(aeb, &ai->erase, u.list) in scan_all()
1431 if (aeb->ec == UBI_UNKNOWN) in scan_all()
1432 aeb->ec = ai->mean_ec; in scan_all()
1434 err = self_check_ai(ubi, ai); in scan_all()
1438 ubi_free_vid_buf(ai->vidb); in scan_all()
1439 kfree(ai->ech); in scan_all()
1444 ubi_free_vid_buf(ai->vidb); in scan_all()
1446 kfree(ai->ech); in scan_all()
1458 INIT_LIST_HEAD(&ai->corr); in alloc_ai()
1459 INIT_LIST_HEAD(&ai->free); in alloc_ai()
1460 INIT_LIST_HEAD(&ai->erase); in alloc_ai()
1461 INIT_LIST_HEAD(&ai->alien); in alloc_ai()
1462 INIT_LIST_HEAD(&ai->fastmap); in alloc_ai()
1463 ai->volumes = RB_ROOT; in alloc_ai()
1464 ai->aeb_slab_cache = kmem_cache_create("ubi_aeb_slab_cache", in alloc_ai()
1467 if (!ai->aeb_slab_cache) { in alloc_ai()
1478 * scan_fast - try to find a fastmap and attach from it.
1479 * @ubi: UBI device description object
1487 static int scan_fast(struct ubi_device *ubi, struct ubi_attach_info **ai) in scan_fast() argument
1492 err = -ENOMEM; in scan_fast()
1498 scan_ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in scan_fast()
1499 if (!scan_ai->ech) in scan_fast()
1502 scan_ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in scan_fast()
1503 if (!scan_ai->vidb) in scan_fast()
1510 err = scan_peb(ubi, scan_ai, pnum, true); in scan_fast()
1515 ubi_free_vid_buf(scan_ai->vidb); in scan_fast()
1516 kfree(scan_ai->ech); in scan_fast()
1518 if (scan_ai->force_full_scan) in scan_fast()
1521 err = ubi_scan_fastmap(ubi, *ai, scan_ai); in scan_fast()
1536 ubi_free_vid_buf(scan_ai->vidb); in scan_fast()
1538 kfree(scan_ai->ech); in scan_fast()
1548 * ubi_attach - attach an MTD device.
1549 * @ubi: UBI device descriptor
1550 * @force_scan: if set to non-zero attach by scanning
1555 int ubi_attach(struct ubi_device *ubi, int force_scan) in ubi_attach() argument
1562 return -ENOMEM; in ubi_attach()
1566 if ((int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd) <= UBI_FM_MAX_START) { in ubi_attach()
1567 ubi->fm_disabled = 1; in ubi_attach()
1572 err = scan_all(ubi, ai, 0); in ubi_attach()
1574 err = scan_fast(ubi, &ai); in ubi_attach()
1580 return -ENOMEM; in ubi_attach()
1582 err = scan_all(ubi, ai, 0); in ubi_attach()
1584 err = scan_all(ubi, ai, UBI_FM_MAX_START); in ubi_attach()
1589 err = scan_all(ubi, ai, 0); in ubi_attach()
1594 ubi->bad_peb_count = ai->bad_peb_count; in ubi_attach()
1595 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count; in ubi_attach()
1596 ubi->corr_peb_count = ai->corr_peb_count; in ubi_attach()
1597 ubi->max_ec = ai->max_ec; in ubi_attach()
1598 ubi->mean_ec = ai->mean_ec; in ubi_attach()
1599 dbg_gen("max. sequence number: %llu", ai->max_sqnum); in ubi_attach()
1601 err = ubi_read_volume_table(ubi, ai); in ubi_attach()
1605 err = ubi_wl_init(ubi, ai); in ubi_attach()
1609 err = ubi_eba_init(ubi, ai); in ubi_attach()
1614 if (ubi->fm && ubi_dbg_chk_fastmap(ubi)) { in ubi_attach()
1619 err = -ENOMEM; in ubi_attach()
1623 err = scan_all(ubi, scan_ai, 0); in ubi_attach()
1629 err = self_check_eba(ubi, ai, scan_ai); in ubi_attach()
1641 ubi_wl_close(ubi); in ubi_attach()
1643 ubi_free_all_volumes(ubi); in ubi_attach()
1644 vfree(ubi->vtbl); in ubi_attach()
1651 * self_check_ai - check the attaching information.
1652 * @ubi: UBI device description object
1658 static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai) in self_check_ai() argument
1660 struct ubi_vid_io_buf *vidb = ai->vidb; in self_check_ai()
1668 if (!ubi_dbg_chk_gen(ubi)) in self_check_ai()
1674 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { in self_check_ai()
1681 if (ai->is_empty) { in self_check_ai()
1682 ubi_err(ubi, "bad is_empty flag"); in self_check_ai()
1686 if (av->vol_id < 0 || av->highest_lnum < 0 || in self_check_ai()
1687 av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 || in self_check_ai()
1688 av->data_pad < 0 || av->last_data_size < 0) { in self_check_ai()
1689 ubi_err(ubi, "negative values"); in self_check_ai()
1693 if (av->vol_id >= UBI_MAX_VOLUMES && in self_check_ai()
1694 av->vol_id < UBI_INTERNAL_VOL_START) { in self_check_ai()
1695 ubi_err(ubi, "bad vol_id"); in self_check_ai()
1699 if (av->vol_id > ai->highest_vol_id) { in self_check_ai()
1700 ubi_err(ubi, "highest_vol_id is %d, but vol_id %d is there", in self_check_ai()
1701 ai->highest_vol_id, av->vol_id); in self_check_ai()
1705 if (av->vol_type != UBI_DYNAMIC_VOLUME && in self_check_ai()
1706 av->vol_type != UBI_STATIC_VOLUME) { in self_check_ai()
1707 ubi_err(ubi, "bad vol_type"); in self_check_ai()
1711 if (av->data_pad > ubi->leb_size / 2) { in self_check_ai()
1712 ubi_err(ubi, "bad data_pad"); in self_check_ai()
1717 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { in self_check_ai()
1723 if (aeb->pnum < 0 || aeb->ec < 0) { in self_check_ai()
1724 ubi_err(ubi, "negative values"); in self_check_ai()
1728 if (aeb->ec < ai->min_ec) { in self_check_ai()
1729 ubi_err(ubi, "bad ai->min_ec (%d), %d found", in self_check_ai()
1730 ai->min_ec, aeb->ec); in self_check_ai()
1734 if (aeb->ec > ai->max_ec) { in self_check_ai()
1735 ubi_err(ubi, "bad ai->max_ec (%d), %d found", in self_check_ai()
1736 ai->max_ec, aeb->ec); in self_check_ai()
1740 if (aeb->pnum >= ubi->peb_count) { in self_check_ai()
1741 ubi_err(ubi, "too high PEB number %d, total PEBs %d", in self_check_ai()
1742 aeb->pnum, ubi->peb_count); in self_check_ai()
1746 if (av->vol_type == UBI_STATIC_VOLUME) { in self_check_ai()
1747 if (aeb->lnum >= av->used_ebs) { in self_check_ai()
1748 ubi_err(ubi, "bad lnum or used_ebs"); in self_check_ai()
1752 if (av->used_ebs != 0) { in self_check_ai()
1753 ubi_err(ubi, "non-zero used_ebs"); in self_check_ai()
1758 if (aeb->lnum > av->highest_lnum) { in self_check_ai()
1759 ubi_err(ubi, "incorrect highest_lnum or lnum"); in self_check_ai()
1764 if (av->leb_count != leb_count) { in self_check_ai()
1765 ubi_err(ubi, "bad leb_count, %d objects in the tree", in self_check_ai()
1775 if (aeb->lnum != av->highest_lnum) { in self_check_ai()
1776 ubi_err(ubi, "bad highest_lnum"); in self_check_ai()
1781 if (vols_found != ai->vols_found) { in self_check_ai()
1782 ubi_err(ubi, "bad ai->vols_found %d, should be %d", in self_check_ai()
1783 ai->vols_found, vols_found); in self_check_ai()
1788 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { in self_check_ai()
1790 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { in self_check_ai()
1797 err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidb, 1); in self_check_ai()
1799 ubi_err(ubi, "VID header is not OK (%d)", in self_check_ai()
1802 err = -EIO; in self_check_ai()
1806 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ? in self_check_ai()
1808 if (av->vol_type != vol_type) { in self_check_ai()
1809 ubi_err(ubi, "bad vol_type"); in self_check_ai()
1813 if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) { in self_check_ai()
1814 ubi_err(ubi, "bad sqnum %llu", aeb->sqnum); in self_check_ai()
1818 if (av->vol_id != be32_to_cpu(vidh->vol_id)) { in self_check_ai()
1819 ubi_err(ubi, "bad vol_id %d", av->vol_id); in self_check_ai()
1823 if (av->compat != vidh->compat) { in self_check_ai()
1824 ubi_err(ubi, "bad compat %d", vidh->compat); in self_check_ai()
1828 if (aeb->lnum != be32_to_cpu(vidh->lnum)) { in self_check_ai()
1829 ubi_err(ubi, "bad lnum %d", aeb->lnum); in self_check_ai()
1833 if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) { in self_check_ai()
1834 ubi_err(ubi, "bad used_ebs %d", av->used_ebs); in self_check_ai()
1838 if (av->data_pad != be32_to_cpu(vidh->data_pad)) { in self_check_ai()
1839 ubi_err(ubi, "bad data_pad %d", av->data_pad); in self_check_ai()
1847 if (av->highest_lnum != be32_to_cpu(vidh->lnum)) { in self_check_ai()
1848 ubi_err(ubi, "bad highest_lnum %d", av->highest_lnum); in self_check_ai()
1852 if (av->last_data_size != be32_to_cpu(vidh->data_size)) { in self_check_ai()
1853 ubi_err(ubi, "bad last_data_size %d", in self_check_ai()
1854 av->last_data_size); in self_check_ai()
1863 buf = kzalloc(ubi->peb_count, GFP_KERNEL); in self_check_ai()
1865 return -ENOMEM; in self_check_ai()
1867 for (pnum = 0; pnum < ubi->peb_count; pnum++) { in self_check_ai()
1868 err = ubi_io_is_bad(ubi, pnum); in self_check_ai()
1876 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) in self_check_ai()
1877 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) in self_check_ai()
1878 buf[aeb->pnum] = 1; in self_check_ai()
1880 list_for_each_entry(aeb, &ai->free, u.list) in self_check_ai()
1881 buf[aeb->pnum] = 1; in self_check_ai()
1883 list_for_each_entry(aeb, &ai->corr, u.list) in self_check_ai()
1884 buf[aeb->pnum] = 1; in self_check_ai()
1886 list_for_each_entry(aeb, &ai->erase, u.list) in self_check_ai()
1887 buf[aeb->pnum] = 1; in self_check_ai()
1889 list_for_each_entry(aeb, &ai->alien, u.list) in self_check_ai()
1890 buf[aeb->pnum] = 1; in self_check_ai()
1893 for (pnum = 0; pnum < ubi->peb_count; pnum++) in self_check_ai()
1895 ubi_err(ubi, "PEB %d is not referred", pnum); in self_check_ai()
1905 ubi_err(ubi, "bad attaching information about LEB %d", aeb->lnum); in self_check_ai()
1911 ubi_err(ubi, "bad attaching information about volume %d", av->vol_id); in self_check_ai()
1916 ubi_err(ubi, "bad attaching information about volume %d", av->vol_id); in self_check_ai()
1922 return -EINVAL; in self_check_ai()