Lines Matching full:ubi

19 #include "ubi.h"
23 * @ubi: UBI device description object
25 static inline int *init_seen(struct ubi_device *ubi) in init_seen() argument
29 if (!ubi_dbg_chk_fastmap(ubi)) in init_seen()
32 ret = kcalloc(ubi->peb_count, sizeof(int), GFP_KERNEL); in init_seen()
41 * @seen: integer array of @ubi->peb_count size
50 * @ubi: UBI device description object
52 * @seen: integer array of @ubi->peb_count size
54 static inline void set_seen(struct ubi_device *ubi, int pnum, int *seen) in set_seen() argument
56 if (!ubi_dbg_chk_fastmap(ubi) || !seen) in set_seen()
64 * @ubi: UBI device description object
65 * @seen: integer array of @ubi->peb_count size
67 static int self_check_seen(struct ubi_device *ubi, int *seen) in self_check_seen() argument
71 if (!ubi_dbg_chk_fastmap(ubi) || !seen) in self_check_seen()
74 for (pnum = 0; pnum < ubi->peb_count; pnum++) { in self_check_seen()
75 if (!seen[pnum] && ubi->lookuptbl[pnum]) { in self_check_seen()
76 ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum); in self_check_seen()
85 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
86 * @ubi: UBI device description object
88 size_t ubi_calc_fm_size(struct ubi_device *ubi) in ubi_calc_fm_size() argument
96 (ubi->peb_count * sizeof(struct ubi_fm_ec)) + in ubi_calc_fm_size()
98 (ubi->peb_count * sizeof(__be32))) + in ubi_calc_fm_size()
100 return roundup(size, ubi->leb_size); in ubi_calc_fm_size()
106 * @ubi: UBI device description object
112 static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id) in new_fm_vhdr() argument
116 new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); in new_fm_vhdr()
123 /* UBI implementations without fastmap support have to delete the in new_fm_vhdr()
134 * @ai: UBI attach info object
264 * @ubi: the UBI device object
272 static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai, in update_vol() argument
304 cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh); in update_vol()
361 * @ubi: UBI device object
368 static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai, in process_pool_aeb() argument
401 ubi_err(ubi, "orphaned volume in fastmap pool!"); in process_pool_aeb()
408 return update_vol(ubi, ai, av, new_vh, new_aeb); in process_pool_aeb()
416 * @ai: UBI attach info object
443 * @ubi: UBI device object
454 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai, in scan_pool() argument
458 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai, in scan_pool()
468 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in scan_pool()
472 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); in scan_pool()
490 if (ubi_io_is_bad(ubi, pnum)) { in scan_pool()
491 ubi_err(ubi, "bad PEB in fastmap pool!"); in scan_pool()
496 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); in scan_pool()
498 ubi_err(ubi, "unable to read EC header! PEB:%i err:%i", in scan_pool()
506 * Older UBI implementations have image_seq set to zero, so in scan_pool()
511 if (image_seq && (image_seq != ubi->image_seq)) { in scan_pool()
512 ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x", in scan_pool()
513 be32_to_cpu(ech->image_seq), ubi->image_seq); in scan_pool()
518 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); in scan_pool()
551 err = process_pool_aeb(ubi, ai, vh, new_aeb); in scan_pool()
558 ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!"); in scan_pool()
566 ubi_free_vid_hdr(ubi, vh); in scan_pool()
573 * @ai: The UBI attach info object
597 * @ubi: UBI device object
598 * @ai: UBI attach info object
604 static int ubi_attach_fastmap(struct ubi_device *ubi, in ubi_attach_fastmap() argument
618 size_t fm_pos = 0, fm_size = ubi->fm_size; in ubi_attach_fastmap()
620 void *fm_raw = ubi->fm_buf; in ubi_attach_fastmap()
638 ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
648 ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
658 ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
669 ubi_err(ubi, "bad pool size: %i", pool_size); in ubi_attach_fastmap()
674 ubi_err(ubi, "bad WL pool size: %i", wl_pool_size); in ubi_attach_fastmap()
681 ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size); in ubi_attach_fastmap()
687 ubi_err(ubi, "bad maximal WL pool size: %i", in ubi_attach_fastmap()
747 ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
761 ubi_err(ubi, "volume (ID %i) already exists", in ubi_attach_fastmap()
777 ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
797 ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum); in ubi_attach_fastmap()
813 ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free); in ubi_attach_fastmap()
817 ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free); in ubi_attach_fastmap()
839 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count - in ubi_attach_fastmap()
843 if (count_fastmap_pebs(ai) != ubi->peb_count - in ubi_attach_fastmap()
869 * @ubi: UBI device object
870 * @ai: UBI attach info to be filled
877 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai, in ubi_scan_fastmap() argument
889 down_write(&ubi->fm_protect); in ubi_scan_fastmap()
890 memset(ubi->fm_buf, 0, ubi->fm_size); in ubi_scan_fastmap()
905 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb)); in ubi_scan_fastmap()
912 ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
919 ubi_err(ubi, "bad fastmap version: %i, expected: %i", in ubi_scan_fastmap()
927 ubi_err(ubi, "number of fastmap blocks is invalid: %i", in ubi_scan_fastmap()
933 fm_size = ubi->leb_size * used_blocks; in ubi_scan_fastmap()
934 if (fm_size != ubi->fm_size) { in ubi_scan_fastmap()
935 ubi_err(ubi, "bad fastmap size: %zi, expected: %zi", in ubi_scan_fastmap()
936 fm_size, ubi->fm_size); in ubi_scan_fastmap()
941 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in ubi_scan_fastmap()
947 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); in ubi_scan_fastmap()
958 if (ubi_io_is_bad(ubi, pnum)) { in ubi_scan_fastmap()
963 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); in ubi_scan_fastmap()
965 ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)", in ubi_scan_fastmap()
974 if (!ubi->image_seq) in ubi_scan_fastmap()
975 ubi->image_seq = image_seq; in ubi_scan_fastmap()
978 * Older UBI implementations have image_seq set to zero, so in ubi_scan_fastmap()
981 if (image_seq && (image_seq != ubi->image_seq)) { in ubi_scan_fastmap()
982 ubi_err(ubi, "wrong image seq:%d instead of %d", in ubi_scan_fastmap()
983 be32_to_cpu(ech->image_seq), ubi->image_seq); in ubi_scan_fastmap()
988 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); in ubi_scan_fastmap()
990 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)", in ubi_scan_fastmap()
997 ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
1005 ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
1016 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum, in ubi_scan_fastmap()
1017 ubi->leb_start, ubi->leb_size); in ubi_scan_fastmap()
1019 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, " in ubi_scan_fastmap()
1028 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf); in ubi_scan_fastmap()
1031 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size); in ubi_scan_fastmap()
1033 ubi_err(ubi, "fastmap data CRC is invalid"); in ubi_scan_fastmap()
1034 ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x", in ubi_scan_fastmap()
1044 ret = ubi_attach_fastmap(ubi, ai, fm); in ubi_scan_fastmap()
1068 ubi->fm = fm; in ubi_scan_fastmap()
1069 ubi->fm_pool.max_size = ubi->fm->max_pool_size; in ubi_scan_fastmap()
1070 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size; in ubi_scan_fastmap()
1071 ubi_msg(ubi, "attached by fastmap"); in ubi_scan_fastmap()
1072 ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size); in ubi_scan_fastmap()
1073 ubi_msg(ubi, "fastmap WL pool size: %d", in ubi_scan_fastmap()
1074 ubi->fm_wl_pool.max_size); in ubi_scan_fastmap()
1075 ubi->fm_disabled = 0; in ubi_scan_fastmap()
1077 ubi_free_vid_hdr(ubi, vh); in ubi_scan_fastmap()
1080 up_write(&ubi->fm_protect); in ubi_scan_fastmap()
1082 ubi_err(ubi, "Attach by fastmap failed, doing a full scan!"); in ubi_scan_fastmap()
1086 ubi_free_vid_hdr(ubi, vh); in ubi_scan_fastmap()
1096 * @ubi: UBI device object
1101 static int ubi_write_fastmap(struct ubi_device *ubi, in ubi_write_fastmap() argument
1121 fm_raw = ubi->fm_buf; in ubi_write_fastmap()
1122 memset(ubi->fm_buf, 0, ubi->fm_size); in ubi_write_fastmap()
1124 avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID); in ubi_write_fastmap()
1130 dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID); in ubi_write_fastmap()
1136 seen_pebs = init_seen(ubi); in ubi_write_fastmap()
1142 spin_lock(&ubi->volumes_lock); in ubi_write_fastmap()
1143 spin_lock(&ubi->wl_lock); in ubi_write_fastmap()
1147 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1151 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1169 fmpl->size = cpu_to_be16(ubi->fm_pool.size); in ubi_write_fastmap()
1170 fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size); in ubi_write_fastmap()
1172 for (i = 0; i < ubi->fm_pool.size; i++) { in ubi_write_fastmap()
1173 fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]); in ubi_write_fastmap()
1174 set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs); in ubi_write_fastmap()
1180 fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size); in ubi_write_fastmap()
1181 fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size); in ubi_write_fastmap()
1183 for (i = 0; i < ubi->fm_wl_pool.size; i++) { in ubi_write_fastmap()
1184 fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]); in ubi_write_fastmap()
1185 set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs); in ubi_write_fastmap()
1188 ubi_for_each_free_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1192 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1197 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1201 ubi_for_each_used_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1205 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1210 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1213 ubi_for_each_protected_peb(ubi, i, wl_e) { in ubi_write_fastmap()
1217 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1222 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1226 ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1230 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1235 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1240 list_for_each_entry(ubi_wrk, &ubi->works, list) { in ubi_write_fastmap()
1248 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1253 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1259 vol = ubi->volumes[i]; in ubi_write_fastmap()
1268 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1282 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1291 fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count); in ubi_write_fastmap()
1293 avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_write_fastmap()
1296 spin_unlock(&ubi->wl_lock); in ubi_write_fastmap()
1297 spin_unlock(&ubi->volumes_lock); in ubi_write_fastmap()
1300 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr); in ubi_write_fastmap()
1302 ubi_err(ubi, "unable to write vid_hdr to fastmap SB!"); in ubi_write_fastmap()
1308 set_seen(ubi, new_fm->e[i]->pnum, seen_pebs); in ubi_write_fastmap()
1314 ubi->fm_size)); in ubi_write_fastmap()
1317 dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_write_fastmap()
1321 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr); in ubi_write_fastmap()
1323 ubi_err(ubi, "unable to write vid_hdr to PEB %i!", in ubi_write_fastmap()
1330 ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size), in ubi_write_fastmap()
1331 new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size); in ubi_write_fastmap()
1333 ubi_err(ubi, "unable to write fastmap to PEB %i!", in ubi_write_fastmap()
1340 ubi->fm = new_fm; in ubi_write_fastmap()
1342 ret = self_check_seen(ubi, seen_pebs); in ubi_write_fastmap()
1346 ubi_free_vid_hdr(ubi, avhdr); in ubi_write_fastmap()
1347 ubi_free_vid_hdr(ubi, dvhdr); in ubi_write_fastmap()
1355 * @ubi: UBI device object
1360 static int erase_block(struct ubi_device *ubi, int pnum) in erase_block() argument
1366 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in erase_block()
1370 ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0); in erase_block()
1378 ret = ubi_io_sync_erase(ubi, pnum, 0); in erase_block()
1390 ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); in erase_block()
1402 * @ubi: UBI device object
1404 * This function ensures that upon next UBI attach a full scan
1405 * is issued. We need this if UBI is about to write a new fastmap
1412 static int invalidate_fastmap(struct ubi_device *ubi) in invalidate_fastmap() argument
1419 if (!ubi->fm) in invalidate_fastmap()
1422 ubi->fm = NULL; in invalidate_fastmap()
1429 vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID); in invalidate_fastmap()
1434 e = ubi_wl_get_fm_peb(ubi, 1); in invalidate_fastmap()
1439 * Create fake fastmap such that UBI will fall back in invalidate_fastmap()
1442 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in invalidate_fastmap()
1443 ret = ubi_io_write_vid_hdr(ubi, e->pnum, vh); in invalidate_fastmap()
1445 ubi_wl_put_fm_peb(ubi, e, 0, 0); in invalidate_fastmap()
1452 ubi->fm = fm; in invalidate_fastmap()
1455 ubi_free_vid_hdr(ubi, vh); in invalidate_fastmap()
1466 * @ubi: UBI device object
1469 static void return_fm_pebs(struct ubi_device *ubi, in return_fm_pebs() argument
1479 ubi_wl_put_fm_peb(ubi, fm->e[i], i, in return_fm_pebs()
1487 * ubi_update_fastmap - will be called by UBI if a volume changes or
1489 * @ubi: UBI device object
1493 int ubi_update_fastmap(struct ubi_device *ubi) in ubi_update_fastmap() argument
1499 down_write(&ubi->fm_protect); in ubi_update_fastmap()
1501 ubi_refill_pools(ubi); in ubi_update_fastmap()
1503 if (ubi->ro_mode || ubi->fm_disabled) { in ubi_update_fastmap()
1504 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1508 ret = ubi_ensure_anchor_pebs(ubi); in ubi_update_fastmap()
1510 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1516 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1520 new_fm->used_blocks = ubi->fm_size / ubi->leb_size; in ubi_update_fastmap()
1521 old_fm = ubi->fm; in ubi_update_fastmap()
1522 ubi->fm = NULL; in ubi_update_fastmap()
1525 ubi_err(ubi, "fastmap too large"); in ubi_update_fastmap()
1531 spin_lock(&ubi->wl_lock); in ubi_update_fastmap()
1532 tmp_e = ubi_wl_get_fm_peb(ubi, 0); in ubi_update_fastmap()
1533 spin_unlock(&ubi->wl_lock); in ubi_update_fastmap()
1537 ret = erase_block(ubi, old_fm->e[i]->pnum); in ubi_update_fastmap()
1539 ubi_err(ubi, "could not erase old fastmap PEB"); in ubi_update_fastmap()
1542 ubi_wl_put_fm_peb(ubi, new_fm->e[j], in ubi_update_fastmap()
1551 ubi_err(ubi, "could not get any free erase block"); in ubi_update_fastmap()
1554 ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0); in ubi_update_fastmap()
1565 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, in ubi_update_fastmap()
1575 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, in ubi_update_fastmap()
1581 spin_lock(&ubi->wl_lock); in ubi_update_fastmap()
1582 tmp_e = ubi_wl_get_fm_peb(ubi, 1); in ubi_update_fastmap()
1583 spin_unlock(&ubi->wl_lock); in ubi_update_fastmap()
1588 ret = erase_block(ubi, old_fm->e[0]->pnum); in ubi_update_fastmap()
1590 ubi_err(ubi, "could not erase old anchor PEB"); in ubi_update_fastmap()
1593 ubi_wl_put_fm_peb(ubi, new_fm->e[i], in ubi_update_fastmap()
1604 ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0, in ubi_update_fastmap()
1611 ubi_err(ubi, "could not find any anchor PEB"); in ubi_update_fastmap()
1614 ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0); in ubi_update_fastmap()
1624 down_write(&ubi->work_sem); in ubi_update_fastmap()
1625 down_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1626 ret = ubi_write_fastmap(ubi, new_fm); in ubi_update_fastmap()
1627 up_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1628 up_write(&ubi->work_sem); in ubi_update_fastmap()
1634 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1639 ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret); in ubi_update_fastmap()
1641 ret = invalidate_fastmap(ubi); in ubi_update_fastmap()
1643 ubi_err(ubi, "Unable to invalidiate current fastmap!"); in ubi_update_fastmap()
1644 ubi_ro_mode(ubi); in ubi_update_fastmap()
1646 return_fm_pebs(ubi, old_fm); in ubi_update_fastmap()
1647 return_fm_pebs(ubi, new_fm); in ubi_update_fastmap()