Lines Matching refs:ubi
30 static inline int *init_seen(struct ubi_device *ubi) in init_seen() argument
34 if (!ubi_dbg_chk_fastmap(ubi)) in init_seen()
37 ret = kcalloc(ubi->peb_count, sizeof(int), GFP_KERNEL); in init_seen()
59 static inline void set_seen(struct ubi_device *ubi, int pnum, int *seen) in set_seen() argument
61 if (!ubi_dbg_chk_fastmap(ubi) || !seen) in set_seen()
72 static int self_check_seen(struct ubi_device *ubi, int *seen) in self_check_seen() argument
76 if (!ubi_dbg_chk_fastmap(ubi) || !seen) in self_check_seen()
79 for (pnum = 0; pnum < ubi->peb_count; pnum++) { in self_check_seen()
80 if (!seen[pnum] && ubi->lookuptbl[pnum]) { in self_check_seen()
81 ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum); in self_check_seen()
93 size_t ubi_calc_fm_size(struct ubi_device *ubi) in ubi_calc_fm_size() argument
101 (ubi->peb_count * sizeof(struct ubi_fm_ec)) + in ubi_calc_fm_size()
103 (ubi->peb_count * sizeof(__be32))) + in ubi_calc_fm_size()
105 return roundup(size, ubi->leb_size); in ubi_calc_fm_size()
117 static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id) in new_fm_vhdr() argument
121 new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); in new_fm_vhdr()
277 static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai, in update_vol() argument
309 cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh); in update_vol()
373 static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai, in process_pool_aeb() argument
406 ubi_err(ubi, "orphaned volume in fastmap pool!"); in process_pool_aeb()
413 return update_vol(ubi, ai, av, new_vh, new_aeb); in process_pool_aeb()
459 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai, in scan_pool() argument
463 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai, in scan_pool()
473 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in scan_pool()
477 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); in scan_pool()
495 if (ubi_io_is_bad(ubi, pnum)) { in scan_pool()
496 ubi_err(ubi, "bad PEB in fastmap pool!"); in scan_pool()
501 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); in scan_pool()
503 ubi_err(ubi, "unable to read EC header! PEB:%i err:%i", in scan_pool()
516 if (image_seq && (image_seq != ubi->image_seq)) { in scan_pool()
517 ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x", in scan_pool()
518 be32_to_cpu(ech->image_seq), ubi->image_seq); in scan_pool()
523 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); in scan_pool()
556 err = process_pool_aeb(ubi, ai, vh, new_aeb); in scan_pool()
563 ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!"); in scan_pool()
571 ubi_free_vid_hdr(ubi, vh); in scan_pool()
609 static int ubi_attach_fastmap(struct ubi_device *ubi, in ubi_attach_fastmap() argument
623 size_t fm_pos = 0, fm_size = ubi->fm_size; in ubi_attach_fastmap()
625 void *fm_raw = ubi->fm_buf; in ubi_attach_fastmap()
643 ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
653 ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
663 ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
674 ubi_err(ubi, "bad pool size: %i", pool_size); in ubi_attach_fastmap()
679 ubi_err(ubi, "bad WL pool size: %i", wl_pool_size); in ubi_attach_fastmap()
686 ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size); in ubi_attach_fastmap()
692 ubi_err(ubi, "bad maximal WL pool size: %i", in ubi_attach_fastmap()
752 ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
766 ubi_err(ubi, "volume (ID %i) already exists", in ubi_attach_fastmap()
782 ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
802 ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum); in ubi_attach_fastmap()
818 ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free); in ubi_attach_fastmap()
822 ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free); in ubi_attach_fastmap()
844 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count - in ubi_attach_fastmap()
848 if (count_fastmap_pebs(ai) != ubi->peb_count - in ubi_attach_fastmap()
882 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai, in ubi_scan_fastmap() argument
894 down_write(&ubi->fm_protect); in ubi_scan_fastmap()
895 memset(ubi->fm_buf, 0, ubi->fm_size); in ubi_scan_fastmap()
910 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb)); in ubi_scan_fastmap()
917 ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
924 ubi_err(ubi, "bad fastmap version: %i, expected: %i", in ubi_scan_fastmap()
932 ubi_err(ubi, "number of fastmap blocks is invalid: %i", in ubi_scan_fastmap()
938 fm_size = ubi->leb_size * used_blocks; in ubi_scan_fastmap()
939 if (fm_size != ubi->fm_size) { in ubi_scan_fastmap()
940 ubi_err(ubi, "bad fastmap size: %zi, expected: %zi", in ubi_scan_fastmap()
941 fm_size, ubi->fm_size); in ubi_scan_fastmap()
946 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in ubi_scan_fastmap()
952 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); in ubi_scan_fastmap()
963 if (ubi_io_is_bad(ubi, pnum)) { in ubi_scan_fastmap()
968 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); in ubi_scan_fastmap()
970 ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)", in ubi_scan_fastmap()
979 if (!ubi->image_seq) in ubi_scan_fastmap()
980 ubi->image_seq = image_seq; in ubi_scan_fastmap()
986 if (image_seq && (image_seq != ubi->image_seq)) { in ubi_scan_fastmap()
987 ubi_err(ubi, "wrong image seq:%d instead of %d", in ubi_scan_fastmap()
988 be32_to_cpu(ech->image_seq), ubi->image_seq); in ubi_scan_fastmap()
993 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); in ubi_scan_fastmap()
995 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)", in ubi_scan_fastmap()
1002 ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
1010 ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
1021 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum, in ubi_scan_fastmap()
1022 ubi->leb_start, ubi->leb_size); in ubi_scan_fastmap()
1024 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, " in ubi_scan_fastmap()
1033 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf); in ubi_scan_fastmap()
1036 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size); in ubi_scan_fastmap()
1038 ubi_err(ubi, "fastmap data CRC is invalid"); in ubi_scan_fastmap()
1039 ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x", in ubi_scan_fastmap()
1049 ret = ubi_attach_fastmap(ubi, ai, fm); in ubi_scan_fastmap()
1073 ubi->fm = fm; in ubi_scan_fastmap()
1074 ubi->fm_pool.max_size = ubi->fm->max_pool_size; in ubi_scan_fastmap()
1075 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size; in ubi_scan_fastmap()
1076 ubi_msg(ubi, "attached by fastmap"); in ubi_scan_fastmap()
1077 ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size); in ubi_scan_fastmap()
1078 ubi_msg(ubi, "fastmap WL pool size: %d", in ubi_scan_fastmap()
1079 ubi->fm_wl_pool.max_size); in ubi_scan_fastmap()
1080 ubi->fm_disabled = 0; in ubi_scan_fastmap()
1082 ubi_free_vid_hdr(ubi, vh); in ubi_scan_fastmap()
1085 up_write(&ubi->fm_protect); in ubi_scan_fastmap()
1087 ubi_err(ubi, "Attach by fastmap failed, doing a full scan!"); in ubi_scan_fastmap()
1091 ubi_free_vid_hdr(ubi, vh); in ubi_scan_fastmap()
1106 static int ubi_write_fastmap(struct ubi_device *ubi, in ubi_write_fastmap() argument
1126 fm_raw = ubi->fm_buf; in ubi_write_fastmap()
1127 memset(ubi->fm_buf, 0, ubi->fm_size); in ubi_write_fastmap()
1129 avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID); in ubi_write_fastmap()
1135 dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID); in ubi_write_fastmap()
1141 seen_pebs = init_seen(ubi); in ubi_write_fastmap()
1147 spin_lock(&ubi->volumes_lock); in ubi_write_fastmap()
1148 spin_lock(&ubi->wl_lock); in ubi_write_fastmap()
1152 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1156 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1174 fmpl->size = cpu_to_be16(ubi->fm_pool.size); in ubi_write_fastmap()
1175 fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size); in ubi_write_fastmap()
1177 for (i = 0; i < ubi->fm_pool.size; i++) { in ubi_write_fastmap()
1178 fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]); in ubi_write_fastmap()
1179 set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs); in ubi_write_fastmap()
1185 fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size); in ubi_write_fastmap()
1186 fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size); in ubi_write_fastmap()
1188 for (i = 0; i < ubi->fm_wl_pool.size; i++) { in ubi_write_fastmap()
1189 fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]); in ubi_write_fastmap()
1190 set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs); in ubi_write_fastmap()
1193 ubi_for_each_free_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1197 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1202 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1206 ubi_for_each_used_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1210 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1215 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1218 ubi_for_each_protected_peb(ubi, i, wl_e) { in ubi_write_fastmap()
1222 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1227 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1231 ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1235 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1240 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1245 list_for_each_entry(ubi_wrk, &ubi->works, list) { in ubi_write_fastmap()
1253 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1258 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1264 vol = ubi->volumes[i]; in ubi_write_fastmap()
1273 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1287 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1296 fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count); in ubi_write_fastmap()
1298 avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_write_fastmap()
1301 spin_unlock(&ubi->wl_lock); in ubi_write_fastmap()
1302 spin_unlock(&ubi->volumes_lock); in ubi_write_fastmap()
1305 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr); in ubi_write_fastmap()
1307 ubi_err(ubi, "unable to write vid_hdr to fastmap SB!"); in ubi_write_fastmap()
1313 set_seen(ubi, new_fm->e[i]->pnum, seen_pebs); in ubi_write_fastmap()
1319 ubi->fm_size)); in ubi_write_fastmap()
1322 dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_write_fastmap()
1326 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr); in ubi_write_fastmap()
1328 ubi_err(ubi, "unable to write vid_hdr to PEB %i!", in ubi_write_fastmap()
1335 ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size), in ubi_write_fastmap()
1336 new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size); in ubi_write_fastmap()
1338 ubi_err(ubi, "unable to write fastmap to PEB %i!", in ubi_write_fastmap()
1345 ubi->fm = new_fm; in ubi_write_fastmap()
1347 ret = self_check_seen(ubi, seen_pebs); in ubi_write_fastmap()
1351 ubi_free_vid_hdr(ubi, avhdr); in ubi_write_fastmap()
1352 ubi_free_vid_hdr(ubi, dvhdr); in ubi_write_fastmap()
1365 static int erase_block(struct ubi_device *ubi, int pnum) in erase_block() argument
1371 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in erase_block()
1375 ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0); in erase_block()
1383 ret = ubi_io_sync_erase(ubi, pnum, 0); in erase_block()
1395 ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); in erase_block()
1417 static int invalidate_fastmap(struct ubi_device *ubi) in invalidate_fastmap() argument
1424 if (!ubi->fm) in invalidate_fastmap()
1427 ubi->fm = NULL; in invalidate_fastmap()
1434 vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID); in invalidate_fastmap()
1439 e = ubi_wl_get_fm_peb(ubi, 1); in invalidate_fastmap()
1447 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in invalidate_fastmap()
1448 ret = ubi_io_write_vid_hdr(ubi, e->pnum, vh); in invalidate_fastmap()
1450 ubi_wl_put_fm_peb(ubi, e, 0, 0); in invalidate_fastmap()
1457 ubi->fm = fm; in invalidate_fastmap()
1460 ubi_free_vid_hdr(ubi, vh); in invalidate_fastmap()
1474 static void return_fm_pebs(struct ubi_device *ubi, in return_fm_pebs() argument
1484 ubi_wl_put_fm_peb(ubi, fm->e[i], i, in return_fm_pebs()
1498 int ubi_update_fastmap(struct ubi_device *ubi) in ubi_update_fastmap() argument
1504 down_write(&ubi->fm_protect); in ubi_update_fastmap()
1506 ubi_refill_pools(ubi); in ubi_update_fastmap()
1508 if (ubi->ro_mode || ubi->fm_disabled) { in ubi_update_fastmap()
1509 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1513 ret = ubi_ensure_anchor_pebs(ubi); in ubi_update_fastmap()
1515 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1521 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1525 new_fm->used_blocks = ubi->fm_size / ubi->leb_size; in ubi_update_fastmap()
1526 old_fm = ubi->fm; in ubi_update_fastmap()
1527 ubi->fm = NULL; in ubi_update_fastmap()
1530 ubi_err(ubi, "fastmap too large"); in ubi_update_fastmap()
1536 spin_lock(&ubi->wl_lock); in ubi_update_fastmap()
1537 tmp_e = ubi_wl_get_fm_peb(ubi, 0); in ubi_update_fastmap()
1538 spin_unlock(&ubi->wl_lock); in ubi_update_fastmap()
1542 ret = erase_block(ubi, old_fm->e[i]->pnum); in ubi_update_fastmap()
1544 ubi_err(ubi, "could not erase old fastmap PEB"); in ubi_update_fastmap()
1547 ubi_wl_put_fm_peb(ubi, new_fm->e[j], in ubi_update_fastmap()
1556 ubi_err(ubi, "could not get any free erase block"); in ubi_update_fastmap()
1559 ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0); in ubi_update_fastmap()
1570 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, in ubi_update_fastmap()
1580 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, in ubi_update_fastmap()
1586 spin_lock(&ubi->wl_lock); in ubi_update_fastmap()
1587 tmp_e = ubi_wl_get_fm_peb(ubi, 1); in ubi_update_fastmap()
1588 spin_unlock(&ubi->wl_lock); in ubi_update_fastmap()
1593 ret = erase_block(ubi, old_fm->e[0]->pnum); in ubi_update_fastmap()
1595 ubi_err(ubi, "could not erase old anchor PEB"); in ubi_update_fastmap()
1598 ubi_wl_put_fm_peb(ubi, new_fm->e[i], in ubi_update_fastmap()
1609 ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0, in ubi_update_fastmap()
1616 ubi_err(ubi, "could not find any anchor PEB"); in ubi_update_fastmap()
1619 ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0); in ubi_update_fastmap()
1629 down_write(&ubi->work_sem); in ubi_update_fastmap()
1630 down_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1631 ret = ubi_write_fastmap(ubi, new_fm); in ubi_update_fastmap()
1632 up_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1633 up_write(&ubi->work_sem); in ubi_update_fastmap()
1639 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1644 ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret); in ubi_update_fastmap()
1646 ret = invalidate_fastmap(ubi); in ubi_update_fastmap()
1648 ubi_err(ubi, "Unable to invalidiate current fastmap!"); in ubi_update_fastmap()
1649 ubi_ro_mode(ubi); in ubi_update_fastmap()
1651 return_fm_pebs(ubi, old_fm); in ubi_update_fastmap()
1652 return_fm_pebs(ubi, new_fm); in ubi_update_fastmap()