Lines Matching +full:wear +full:- +full:leveling
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
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.
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
59 * the data area does not contain all 0xFFs, and there were no bit-flips or
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
86 * find_or_add_av - internal function to find a volume, add a volume or do
91 * expected operation. If only %AV_ADD is set, -EEXIST is returned
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()
152 * ubi_find_or_add_av - search for a volume in the attaching information and
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.
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.
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.
323 * non-zero if an inconsistency was found and zero if not.
326 * Most of the checks are done in the I/O sub-system. Here we check that the
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()
348 if (vol_id != av->vol_id) { in validate_vid_hdr()
353 if (av->vol_type == UBI_STATIC_VOLUME) in validate_vid_hdr()
363 if (used_ebs != av->used_ebs) { in validate_vid_hdr()
368 if (data_pad != av->data_pad) { in validate_vid_hdr()
380 return -EINVAL; in validate_vid_hdr()
384 * add_volume - add volume to the attaching information.
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.
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;
444 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); in ubi_compare_lebs()
446 if (sqnum2 == aeb->sqnum) { in ubi_compare_lebs()
450 * that times we used 32-bit LEB versions stored in logical 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()
488 return -ENOMEM; in ubi_compare_lebs()
490 pnum = aeb->pnum; 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.
554 * @bitflips: if bit-flips were detected when this physical eraseblock was read
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()
625 if (aeb->sqnum == sqnum && sqnum != 0) { in ubi_add_to_av()
630 return -EINVAL; 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()
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.
725 * ubi_find_av - find volume in the attaching information.
745 * ubi_remove_av - delete attaching information about a volume.
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.
767 * initialization stages, when the EBA sub-system had not been yet initialized.
779 * Erase counter overflow. Upgrade UBI and use 64-bit 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()
805 * ubi_early_get_peb - get a free physical eraseblock.
810 * called on the UBI initialization stages when the wear-leveling sub-system is
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()
852 return ERR_PTR(-ENOSPC); in ubi_early_get_peb()
856 * check_corruption - check the data area of PEB.
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()
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.
937 * information about this PEB to the corresponding list or RB-tree in the
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()
957 ai->bad_peb_count += 1; 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()
992 return -EINVAL; in scan_peb()
999 if (ech->version != UBI_VERSION) { 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()
1017 return -EINVAL; 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()
1036 image_seq, pnum, ubi->image_seq); in scan_peb()
1038 return -EINVAL; in scan_peb()
1061 ai->maybe_bad_peb_count += 1; in scan_peb()
1076 ai->force_full_scan = 1; 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()
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()
1138 switch (vidh->compat) { 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()
1159 ec, 0, &ai->alien); in scan_peb()
1167 return -EINVAL; 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.
1205 * should proceed with attaching the MTD device, and %-EINVAL if we should not.
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()
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()
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()
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.
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.
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()
1404 if (ai->ec_count) in scan_all()
1405 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count); 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()
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.
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()
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()
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.
1550 * @force_scan: if set to non-zero attach by scanning
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()
1580 return -ENOMEM; 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()
1614 if (ubi->fm && ubi_dbg_chk_fastmap(ubi)) { in ubi_attach()
1619 err = -ENOMEM; in ubi_attach()
1644 vfree(ubi->vtbl); in ubi_attach()
1651 * self_check_ai - check the attaching information.
1660 struct ubi_vid_io_buf *vidb = ai->vidb; 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()
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()
1693 if (av->vol_id >= UBI_MAX_VOLUMES && in self_check_ai()
1694 av->vol_id < UBI_INTERNAL_VOL_START) { in self_check_ai()
1699 if (av->vol_id > ai->highest_vol_id) { 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()
1711 if (av->data_pad > ubi->leb_size / 2) { 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()
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()
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()
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()
1764 if (av->leb_count != leb_count) { in self_check_ai()
1775 if (aeb->lnum != av->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()
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()
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()
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()
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()
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()