Lines Matching +full:- +full:av
4 * SPDX-License-Identifier: GPL-2.0+
10 * UBI attaching sub-system.
12 * This sub-system is responsible for attaching MTD devices and it also
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.
21 * objects are kept in per-volume RB-trees with the root at the corresponding
22 * &struct ubi_ainf_volume object. To put it differently, we keep an RB-tree of
23 * per-volume objects and each of these objects is the root of RB-tree of
24 * per-LEB objects.
33 * UBI protects EC and VID headers with CRC-32 checksums, so it can detect
35 * data with CRC-32, e.g., when it executes the atomic LEB change operation, or
36 * when it moves the contents of a PEB for wear-leveling purposes.
43 * cases - we may lose only the data which were being written to the media just
47 * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
60 * the data area does not contain all 0xFFs, and there were no bit-flips or
65 * to just erase this PEB - this is corruption type 1.
66 * o If the data area has bit-flips or data integrity errors (ECC errors on
95 * add_to_list - add physical eraseblock to a list.
120 if (list == &ai->free) { in add_to_list()
122 } else if (list == &ai->erase) { in add_to_list()
124 } else if (list == &ai->alien) { in add_to_list()
126 ai->alien_peb_count += 1; in add_to_list()
130 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL); in add_to_list()
132 return -ENOMEM; in add_to_list()
134 aeb->pnum = pnum; in add_to_list()
135 aeb->vol_id = vol_id; in add_to_list()
136 aeb->lnum = lnum; in add_to_list()
137 aeb->ec = ec; in add_to_list()
139 list_add(&aeb->u.list, list); in add_to_list()
141 list_add_tail(&aeb->u.list, list); in add_to_list()
146 * add_corrupted - add a corrupted physical eraseblock.
162 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL); in add_corrupted()
164 return -ENOMEM; in add_corrupted()
166 ai->corr_peb_count += 1; in add_corrupted()
167 aeb->pnum = pnum; in add_corrupted()
168 aeb->ec = ec; in add_corrupted()
169 list_add(&aeb->u.list, &ai->corr); in add_corrupted()
174 * validate_vid_hdr - check volume identifier header.
177 * @av: information about the volume this logical eraseblock belongs to
181 * non-zero if an inconsistency was found and zero if not.
184 * Most of the checks are done in the I/O sub-system. Here we check that the
190 const struct ubi_ainf_volume *av, int pnum) in validate_vid_hdr() argument
192 int vol_type = vid_hdr->vol_type; in validate_vid_hdr()
193 int vol_id = be32_to_cpu(vid_hdr->vol_id); in validate_vid_hdr()
194 int used_ebs = be32_to_cpu(vid_hdr->used_ebs); in validate_vid_hdr()
195 int data_pad = be32_to_cpu(vid_hdr->data_pad); in validate_vid_hdr()
197 if (av->leb_count != 0) { in validate_vid_hdr()
206 if (vol_id != av->vol_id) { in validate_vid_hdr()
211 if (av->vol_type == UBI_STATIC_VOLUME) in validate_vid_hdr()
221 if (used_ebs != av->used_ebs) { in validate_vid_hdr()
226 if (data_pad != av->data_pad) { in validate_vid_hdr()
237 ubi_dump_av(av); in validate_vid_hdr()
238 return -EINVAL; in validate_vid_hdr()
242 * add_volume - add volume to the attaching information.
251 * to the allocated "av" object in case of success and a negative error code in
258 struct ubi_ainf_volume *av; in add_volume() local
259 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; in add_volume()
261 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id)); in add_volume()
263 /* Walk the volume RB-tree to look if this volume is already present */ in add_volume()
266 av = rb_entry(parent, struct ubi_ainf_volume, rb); in add_volume()
268 if (vol_id == av->vol_id) in add_volume()
269 return av; in add_volume()
271 if (vol_id > av->vol_id) in add_volume()
272 p = &(*p)->rb_left; in add_volume()
274 p = &(*p)->rb_right; in add_volume()
277 /* The volume is absent - add it */ in add_volume()
278 av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL); in add_volume()
279 if (!av) in add_volume()
280 return ERR_PTR(-ENOMEM); in add_volume()
282 av->highest_lnum = av->leb_count = 0; in add_volume()
283 av->vol_id = vol_id; in add_volume()
284 av->root = RB_ROOT; in add_volume()
285 av->used_ebs = be32_to_cpu(vid_hdr->used_ebs); in add_volume()
286 av->data_pad = be32_to_cpu(vid_hdr->data_pad); in add_volume()
287 av->compat = vid_hdr->compat; in add_volume()
288 av->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME in add_volume()
290 if (vol_id > ai->highest_vol_id) in add_volume()
291 ai->highest_vol_id = vol_id; in add_volume()
293 rb_link_node(&av->rb, parent, p); in add_volume()
294 rb_insert_color(&av->rb, &ai->volumes); in add_volume()
295 ai->vols_found += 1; in add_volume()
297 return av; in add_volume()
301 * ubi_compare_lebs - find out which logical eraseblock is newer.
315 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
316 * o bit 1 is set: bit-flips were detected in the newer LEB;
326 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); in ubi_compare_lebs()
328 if (sqnum2 == aeb->sqnum) { in ubi_compare_lebs()
332 * that times we used 32-bit LEB versions stored in logical in ubi_compare_lebs()
337 ubi_err(ubi, "unsupported on-flash UBI format"); in ubi_compare_lebs()
338 return -EINVAL; in ubi_compare_lebs()
342 second_is_newer = (sqnum2 > aeb->sqnum); in ubi_compare_lebs()
348 * for the first one - we'll need to re-read it from flash. in ubi_compare_lebs()
354 if (!vid_hdr->copy_flag) { in ubi_compare_lebs()
361 if (!aeb->copy_flag) { in ubi_compare_lebs()
370 return -ENOMEM; in ubi_compare_lebs()
372 pnum = aeb->pnum; in ubi_compare_lebs()
381 err = -EIO; in ubi_compare_lebs()
392 len = be32_to_cpu(vid_hdr->data_size); in ubi_compare_lebs()
394 mutex_lock(&ubi->buf_mutex); in ubi_compare_lebs()
395 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len); in ubi_compare_lebs()
399 data_crc = be32_to_cpu(vid_hdr->data_crc); in ubi_compare_lebs()
400 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, len); in ubi_compare_lebs()
411 mutex_unlock(&ubi->buf_mutex); in ubi_compare_lebs()
423 mutex_unlock(&ubi->buf_mutex); in ubi_compare_lebs()
430 * ubi_add_to_av - add used physical eraseblock to the attaching information.
436 * @bitflips: if bit-flips were detected when this physical eraseblock was read
450 struct ubi_ainf_volume *av; in ubi_add_to_av() local
454 vol_id = be32_to_cpu(vid_hdr->vol_id); in ubi_add_to_av()
455 lnum = be32_to_cpu(vid_hdr->lnum); in ubi_add_to_av()
456 sqnum = be64_to_cpu(vid_hdr->sqnum); in ubi_add_to_av()
461 av = add_volume(ai, vol_id, pnum, vid_hdr); in ubi_add_to_av()
462 if (IS_ERR(av)) in ubi_add_to_av()
463 return PTR_ERR(av); in ubi_add_to_av()
465 if (ai->max_sqnum < sqnum) in ubi_add_to_av()
466 ai->max_sqnum = sqnum; in ubi_add_to_av()
469 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look in ubi_add_to_av()
472 p = &av->root.rb_node; in ubi_add_to_av()
478 if (lnum != aeb->lnum) { in ubi_add_to_av()
479 if (lnum < aeb->lnum) in ubi_add_to_av()
480 p = &(*p)->rb_left; in ubi_add_to_av()
482 p = &(*p)->rb_right; in ubi_add_to_av()
492 aeb->pnum, aeb->sqnum, aeb->ec); in ubi_add_to_av()
507 if (aeb->sqnum == sqnum && sqnum != 0) { in ubi_add_to_av()
512 return -EINVAL; in ubi_add_to_av()
528 err = validate_vid_hdr(ubi, vid_hdr, av, pnum); in ubi_add_to_av()
532 err = add_to_list(ai, aeb->pnum, aeb->vol_id, in ubi_add_to_av()
533 aeb->lnum, aeb->ec, cmp_res & 4, in ubi_add_to_av()
534 &ai->erase); in ubi_add_to_av()
538 aeb->ec = ec; in ubi_add_to_av()
539 aeb->pnum = pnum; in ubi_add_to_av()
540 aeb->vol_id = vol_id; in ubi_add_to_av()
541 aeb->lnum = lnum; in ubi_add_to_av()
542 aeb->scrub = ((cmp_res & 2) || bitflips); in ubi_add_to_av()
543 aeb->copy_flag = vid_hdr->copy_flag; in ubi_add_to_av()
544 aeb->sqnum = sqnum; in ubi_add_to_av()
546 if (av->highest_lnum == lnum) in ubi_add_to_av()
547 av->last_data_size = in ubi_add_to_av()
548 be32_to_cpu(vid_hdr->data_size); in ubi_add_to_av()
557 cmp_res & 4, &ai->erase); in ubi_add_to_av()
566 err = validate_vid_hdr(ubi, vid_hdr, av, pnum); in ubi_add_to_av()
570 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL); in ubi_add_to_av()
572 return -ENOMEM; in ubi_add_to_av()
574 aeb->ec = ec; in ubi_add_to_av()
575 aeb->pnum = pnum; in ubi_add_to_av()
576 aeb->vol_id = vol_id; in ubi_add_to_av()
577 aeb->lnum = lnum; in ubi_add_to_av()
578 aeb->scrub = bitflips; in ubi_add_to_av()
579 aeb->copy_flag = vid_hdr->copy_flag; in ubi_add_to_av()
580 aeb->sqnum = sqnum; in ubi_add_to_av()
582 if (av->highest_lnum <= lnum) { in ubi_add_to_av()
583 av->highest_lnum = lnum; in ubi_add_to_av()
584 av->last_data_size = be32_to_cpu(vid_hdr->data_size); in ubi_add_to_av()
587 av->leb_count += 1; in ubi_add_to_av()
588 rb_link_node(&aeb->u.rb, parent, p); in ubi_add_to_av()
589 rb_insert_color(&aeb->u.rb, &av->root); in ubi_add_to_av()
594 * ubi_find_av - find volume in the attaching information.
604 struct ubi_ainf_volume *av; in ubi_find_av() local
605 struct rb_node *p = ai->volumes.rb_node; in ubi_find_av()
608 av = rb_entry(p, struct ubi_ainf_volume, rb); in ubi_find_av()
610 if (vol_id == av->vol_id) in ubi_find_av()
611 return av; in ubi_find_av()
613 if (vol_id > av->vol_id) in ubi_find_av()
614 p = p->rb_left; in ubi_find_av()
616 p = p->rb_right; in ubi_find_av()
623 * ubi_remove_av - delete attaching information about a volume.
625 * @av: the volume attaching information to delete
627 void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av) in ubi_remove_av() argument
632 dbg_bld("remove attaching information about volume %d", av->vol_id); in ubi_remove_av()
634 while ((rb = rb_first(&av->root))) { in ubi_remove_av()
636 rb_erase(&aeb->u.rb, &av->root); in ubi_remove_av()
637 list_add_tail(&aeb->u.list, &ai->erase); in ubi_remove_av()
640 rb_erase(&av->rb, &ai->volumes); in ubi_remove_av()
641 kfree(av); in ubi_remove_av()
642 ai->vols_found -= 1; in ubi_remove_av()
646 * early_erase_peb - erase a physical eraseblock.
654 * initialization stages, when the EBA sub-system had not been yet initialized.
666 * Erase counter overflow. Upgrade UBI and use 64-bit in early_erase_peb()
671 return -EINVAL; in early_erase_peb()
674 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in early_erase_peb()
676 return -ENOMEM; in early_erase_peb()
678 ec_hdr->ec = cpu_to_be64(ec); in early_erase_peb()
692 * ubi_early_get_peb - get a free physical eraseblock.
697 * called on the UBI initialization stages when the wear-leveling sub-system is
711 if (!list_empty(&ai->free)) { in ubi_early_get_peb()
712 aeb = list_entry(ai->free.next, struct ubi_ainf_peb, u.list); in ubi_early_get_peb()
713 list_del(&aeb->u.list); in ubi_early_get_peb()
714 dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec); in ubi_early_get_peb()
721 * so forth. We don't want to take care about bad eraseblocks here - in ubi_early_get_peb()
724 list_for_each_entry_safe(aeb, tmp_aeb, &ai->erase, u.list) { in ubi_early_get_peb()
725 if (aeb->ec == UBI_UNKNOWN) in ubi_early_get_peb()
726 aeb->ec = ai->mean_ec; in ubi_early_get_peb()
728 err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1); in ubi_early_get_peb()
732 aeb->ec += 1; in ubi_early_get_peb()
733 list_del(&aeb->u.list); in ubi_early_get_peb()
734 dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec); in ubi_early_get_peb()
739 return ERR_PTR(-ENOSPC); in ubi_early_get_peb()
743 * check_corruption - check the data area of PEB.
764 mutex_lock(&ubi->buf_mutex); in check_corruption()
765 memset(ubi->peb_buf, 0x00, ubi->leb_size); in check_corruption()
767 err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start, in check_corruption()
768 ubi->leb_size); in check_corruption()
771 * Bit-flips or integrity errors while reading the data area. in check_corruption()
784 if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size)) in check_corruption()
789 …ubi_err(ubi, "this may be a non-UBI PEB or a severe VID header corruption which requires manual in… in check_corruption()
792 pnum, ubi->leb_start, ubi->leb_size); in check_corruption()
794 ubi->peb_buf, ubi->leb_size, 1); in check_corruption()
798 mutex_unlock(&ubi->buf_mutex); in check_corruption()
803 * scan_peb - scan and process UBI headers of a PEB.
811 * information about this PEB to the corresponding list or RB-tree in the
819 int err, bitflips = 0, vol_id = -1, ec_err = 0; in scan_peb()
828 ai->bad_peb_count += 1; in scan_peb()
842 ai->empty_peb_count += 1; in scan_peb()
844 UBI_UNKNOWN, 0, &ai->erase); in scan_peb()
846 ai->empty_peb_count += 1; in scan_peb()
848 UBI_UNKNOWN, 1, &ai->erase); in scan_peb()
854 * moved and EC be re-created. in scan_peb()
863 return -EINVAL; in scan_peb()
870 if (ech->version != UBI_VERSION) { in scan_peb()
872 UBI_VERSION, (int)ech->version); in scan_peb()
873 return -EINVAL; in scan_peb()
876 ec = be64_to_cpu(ech->ec); in scan_peb()
882 * flash. Upgrade UBI and use 64-bit erase counters in scan_peb()
888 return -EINVAL; in scan_peb()
899 * sequence number, while other PEBs have non-zero sequence in scan_peb()
902 image_seq = be32_to_cpu(ech->image_seq); in scan_peb()
903 if (!ubi->image_seq) in scan_peb()
904 ubi->image_seq = image_seq; in scan_peb()
905 if (image_seq && ubi->image_seq != image_seq) { in scan_peb()
907 image_seq, pnum, ubi->image_seq); in scan_peb()
909 return -EINVAL; in scan_peb()
932 ai->maybe_bad_peb_count += 1; in scan_peb()
960 UBI_UNKNOWN, ec, 1, &ai->erase); in scan_peb()
969 ec, 1, &ai->erase); in scan_peb()
976 UBI_UNKNOWN, ec, 1, &ai->erase); in scan_peb()
979 UBI_UNKNOWN, ec, 0, &ai->free); in scan_peb()
986 return -EINVAL; in scan_peb()
989 vol_id = be32_to_cpu(vidh->vol_id); in scan_peb()
993 *sqnum = be64_to_cpu(vidh->sqnum); in scan_peb()
995 int lnum = be32_to_cpu(vidh->lnum); in scan_peb()
998 switch (vidh->compat) { in scan_peb()
1006 ec, 1, &ai->erase); in scan_peb()
1012 ubi_msg(ubi, "read-only compatible internal volume %d:%d found, switch to read-only mode", in scan_peb()
1014 ubi->ro_mode = 1; in scan_peb()
1021 ec, 0, &ai->alien); in scan_peb()
1029 return -EINVAL; in scan_peb()
1042 ai->ec_sum += ec; in scan_peb()
1043 ai->ec_count += 1; in scan_peb()
1044 if (ec > ai->max_ec) in scan_peb()
1045 ai->max_ec = ec; in scan_peb()
1046 if (ec < ai->min_ec) in scan_peb()
1047 ai->min_ec = ec; in scan_peb()
1054 * late_analysis - analyze the overall situation with PEB.
1062 * should proceed with attaching the MTD device, and %-EINVAL if we should not.
1069 peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count; in late_analysis()
1077 if (ai->corr_peb_count) { in late_analysis()
1079 ai->corr_peb_count); in late_analysis()
1081 list_for_each_entry(aeb, &ai->corr, u.list) in late_analysis()
1082 pr_cont(" %d", aeb->pnum); in late_analysis()
1089 if (ai->corr_peb_count >= max_corr) { in late_analysis()
1091 return -EINVAL; in late_analysis()
1095 if (ai->empty_peb_count + ai->maybe_bad_peb_count == peb_count) { in late_analysis()
1097 * All PEBs are empty, or almost all - a couple PEBs look like in late_analysis()
1108 * 2. Flash contains non-UBI data and we do not want to format in late_analysis()
1111 if (ai->maybe_bad_peb_count <= 2) { in late_analysis()
1112 ai->is_empty = 1; in late_analysis()
1114 get_random_bytes(&ubi->image_seq, in late_analysis()
1115 sizeof(ubi->image_seq)); in late_analysis()
1117 ubi_err(ubi, "MTD device is not UBI-formatted and possibly contains non-UBI data - refusing it"); in late_analysis()
1118 return -EINVAL; in late_analysis()
1127 * destroy_av - free volume attaching information.
1128 * @av: volume attaching information
1133 static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av) in destroy_av() argument
1136 struct rb_node *this = av->root.rb_node; in destroy_av()
1139 if (this->rb_left) in destroy_av()
1140 this = this->rb_left; in destroy_av()
1141 else if (this->rb_right) in destroy_av()
1142 this = this->rb_right; in destroy_av()
1147 if (this->rb_left == &aeb->u.rb) in destroy_av()
1148 this->rb_left = NULL; in destroy_av()
1150 this->rb_right = NULL; in destroy_av()
1153 kmem_cache_free(ai->aeb_slab_cache, aeb); in destroy_av()
1156 kfree(av); in destroy_av()
1160 * destroy_ai - destroy attaching information.
1166 struct ubi_ainf_volume *av; in destroy_ai() local
1169 list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) { in destroy_ai()
1170 list_del(&aeb->u.list); in destroy_ai()
1171 kmem_cache_free(ai->aeb_slab_cache, aeb); in destroy_ai()
1173 list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) { in destroy_ai()
1174 list_del(&aeb->u.list); in destroy_ai()
1175 kmem_cache_free(ai->aeb_slab_cache, aeb); in destroy_ai()
1177 list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) { in destroy_ai()
1178 list_del(&aeb->u.list); in destroy_ai()
1179 kmem_cache_free(ai->aeb_slab_cache, aeb); in destroy_ai()
1181 list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) { in destroy_ai()
1182 list_del(&aeb->u.list); in destroy_ai()
1183 kmem_cache_free(ai->aeb_slab_cache, aeb); in destroy_ai()
1186 /* Destroy the volume RB-tree */ in destroy_ai()
1187 rb = ai->volumes.rb_node; in destroy_ai()
1189 if (rb->rb_left) in destroy_ai()
1190 rb = rb->rb_left; in destroy_ai()
1191 else if (rb->rb_right) in destroy_ai()
1192 rb = rb->rb_right; in destroy_ai()
1194 av = rb_entry(rb, struct ubi_ainf_volume, rb); in destroy_ai()
1198 if (rb->rb_left == &av->rb) in destroy_ai()
1199 rb->rb_left = NULL; in destroy_ai()
1201 rb->rb_right = NULL; in destroy_ai()
1204 destroy_av(ai, av); in destroy_ai()
1208 kmem_cache_destroy(ai->aeb_slab_cache); in destroy_ai()
1214 * scan_all - scan entire MTD device.
1228 struct ubi_ainf_volume *av; in scan_all() local
1231 err = -ENOMEM; in scan_all()
1233 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in scan_all()
1241 for (pnum = start; pnum < ubi->peb_count; pnum++) { in scan_all()
1253 if (ai->ec_count) in scan_all()
1254 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count); in scan_all()
1264 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { in scan_all()
1265 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) in scan_all()
1266 if (aeb->ec == UBI_UNKNOWN) in scan_all()
1267 aeb->ec = ai->mean_ec; in scan_all()
1270 list_for_each_entry(aeb, &ai->free, u.list) { in scan_all()
1271 if (aeb->ec == UBI_UNKNOWN) in scan_all()
1272 aeb->ec = ai->mean_ec; in scan_all()
1275 list_for_each_entry(aeb, &ai->corr, u.list) in scan_all()
1276 if (aeb->ec == UBI_UNKNOWN) in scan_all()
1277 aeb->ec = ai->mean_ec; in scan_all()
1279 list_for_each_entry(aeb, &ai->erase, u.list) in scan_all()
1280 if (aeb->ec == UBI_UNKNOWN) in scan_all()
1281 aeb->ec = ai->mean_ec; in scan_all()
1307 INIT_LIST_HEAD(&ai->corr); in alloc_ai()
1308 INIT_LIST_HEAD(&ai->free); in alloc_ai()
1309 INIT_LIST_HEAD(&ai->erase); in alloc_ai()
1310 INIT_LIST_HEAD(&ai->alien); in alloc_ai()
1311 ai->volumes = RB_ROOT; in alloc_ai()
1312 ai->aeb_slab_cache = kmem_cache_create("ubi_aeb_slab_cache", in alloc_ai()
1315 if (!ai->aeb_slab_cache) { in alloc_ai()
1326 * scan_fastmap - try to find a fastmap and attach from it.
1337 int err, pnum, fm_anchor = -1; in scan_fast()
1340 err = -ENOMEM; in scan_fast()
1342 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in scan_fast()
1351 int vol_id = -1; in scan_fast()
1352 unsigned long long sqnum = -1; in scan_fast()
1375 return -ENOMEM; in scan_fast()
1390 * ubi_attach - attach an MTD device.
1392 * @force_scan: if set to non-zero attach by scanning
1404 return -ENOMEM; in ubi_attach()
1408 if ((int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd) <= UBI_FM_MAX_START) { in ubi_attach()
1409 ubi->fm_disabled = 1; in ubi_attach()
1422 return -ENOMEM; in ubi_attach()
1436 ubi->bad_peb_count = ai->bad_peb_count; in ubi_attach()
1437 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count; in ubi_attach()
1438 ubi->corr_peb_count = ai->corr_peb_count; in ubi_attach()
1439 ubi->max_ec = ai->max_ec; in ubi_attach()
1440 ubi->mean_ec = ai->mean_ec; in ubi_attach()
1441 dbg_gen("max. sequence number: %llu", ai->max_sqnum); in ubi_attach()
1456 if (ubi->fm && ubi_dbg_chk_fastmap(ubi)) { in ubi_attach()
1461 err = -ENOMEM; in ubi_attach()
1486 vfree(ubi->vtbl); in ubi_attach()
1493 * self_check_ai - check the attaching information.
1504 struct ubi_ainf_volume *av; in self_check_ai() local
1514 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { in self_check_ai()
1521 if (ai->is_empty) { in self_check_ai()
1526 if (av->vol_id < 0 || av->highest_lnum < 0 || in self_check_ai()
1527 av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 || in self_check_ai()
1528 av->data_pad < 0 || av->last_data_size < 0) { in self_check_ai()
1533 if (av->vol_id >= UBI_MAX_VOLUMES && in self_check_ai()
1534 av->vol_id < UBI_INTERNAL_VOL_START) { in self_check_ai()
1539 if (av->vol_id > ai->highest_vol_id) { in self_check_ai()
1541 ai->highest_vol_id, av->vol_id); in self_check_ai()
1545 if (av->vol_type != UBI_DYNAMIC_VOLUME && in self_check_ai()
1546 av->vol_type != UBI_STATIC_VOLUME) { in self_check_ai()
1551 if (av->data_pad > ubi->leb_size / 2) { in self_check_ai()
1557 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { in self_check_ai()
1563 if (aeb->pnum < 0 || aeb->ec < 0) { in self_check_ai()
1568 if (aeb->ec < ai->min_ec) { in self_check_ai()
1569 ubi_err(ubi, "bad ai->min_ec (%d), %d found", in self_check_ai()
1570 ai->min_ec, aeb->ec); in self_check_ai()
1574 if (aeb->ec > ai->max_ec) { in self_check_ai()
1575 ubi_err(ubi, "bad ai->max_ec (%d), %d found", in self_check_ai()
1576 ai->max_ec, aeb->ec); in self_check_ai()
1580 if (aeb->pnum >= ubi->peb_count) { in self_check_ai()
1582 aeb->pnum, ubi->peb_count); in self_check_ai()
1586 if (av->vol_type == UBI_STATIC_VOLUME) { in self_check_ai()
1587 if (aeb->lnum >= av->used_ebs) { in self_check_ai()
1592 if (av->used_ebs != 0) { in self_check_ai()
1593 ubi_err(ubi, "non-zero used_ebs"); in self_check_ai()
1598 if (aeb->lnum > av->highest_lnum) { in self_check_ai()
1604 if (av->leb_count != leb_count) { in self_check_ai()
1615 if (aeb->lnum != av->highest_lnum) { in self_check_ai()
1621 if (vols_found != ai->vols_found) { in self_check_ai()
1622 ubi_err(ubi, "bad ai->vols_found %d, should be %d", in self_check_ai()
1623 ai->vols_found, vols_found); in self_check_ai()
1628 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { in self_check_ai()
1630 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { in self_check_ai()
1637 err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidh, 1); in self_check_ai()
1642 err = -EIO; in self_check_ai()
1646 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ? in self_check_ai()
1648 if (av->vol_type != vol_type) { in self_check_ai()
1653 if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) { in self_check_ai()
1654 ubi_err(ubi, "bad sqnum %llu", aeb->sqnum); in self_check_ai()
1658 if (av->vol_id != be32_to_cpu(vidh->vol_id)) { in self_check_ai()
1659 ubi_err(ubi, "bad vol_id %d", av->vol_id); in self_check_ai()
1663 if (av->compat != vidh->compat) { in self_check_ai()
1664 ubi_err(ubi, "bad compat %d", vidh->compat); in self_check_ai()
1668 if (aeb->lnum != be32_to_cpu(vidh->lnum)) { in self_check_ai()
1669 ubi_err(ubi, "bad lnum %d", aeb->lnum); in self_check_ai()
1673 if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) { in self_check_ai()
1674 ubi_err(ubi, "bad used_ebs %d", av->used_ebs); in self_check_ai()
1678 if (av->data_pad != be32_to_cpu(vidh->data_pad)) { in self_check_ai()
1679 ubi_err(ubi, "bad data_pad %d", av->data_pad); in self_check_ai()
1687 if (av->highest_lnum != be32_to_cpu(vidh->lnum)) { in self_check_ai()
1688 ubi_err(ubi, "bad highest_lnum %d", av->highest_lnum); in self_check_ai()
1692 if (av->last_data_size != be32_to_cpu(vidh->data_size)) { in self_check_ai()
1694 av->last_data_size); in self_check_ai()
1703 buf = kzalloc(ubi->peb_count, GFP_KERNEL); in self_check_ai()
1705 return -ENOMEM; in self_check_ai()
1707 for (pnum = 0; pnum < ubi->peb_count; pnum++) { in self_check_ai()
1716 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) in self_check_ai()
1717 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) in self_check_ai()
1718 buf[aeb->pnum] = 1; in self_check_ai()
1720 list_for_each_entry(aeb, &ai->free, u.list) in self_check_ai()
1721 buf[aeb->pnum] = 1; in self_check_ai()
1723 list_for_each_entry(aeb, &ai->corr, u.list) in self_check_ai()
1724 buf[aeb->pnum] = 1; in self_check_ai()
1726 list_for_each_entry(aeb, &ai->erase, u.list) in self_check_ai()
1727 buf[aeb->pnum] = 1; in self_check_ai()
1729 list_for_each_entry(aeb, &ai->alien, u.list) in self_check_ai()
1730 buf[aeb->pnum] = 1; in self_check_ai()
1733 for (pnum = 0; pnum < ubi->peb_count; pnum++) in self_check_ai()
1745 ubi_err(ubi, "bad attaching information about LEB %d", aeb->lnum); in self_check_ai()
1747 ubi_dump_av(av); in self_check_ai()
1751 ubi_err(ubi, "bad attaching information about volume %d", av->vol_id); in self_check_ai()
1752 ubi_dump_av(av); in self_check_ai()
1756 ubi_err(ubi, "bad attaching information about volume %d", av->vol_id); in self_check_ai()
1757 ubi_dump_av(av); in self_check_ai()
1762 return -EINVAL; in self_check_ai()