Lines Matching full:ubi

10 #include "ubi.h"
14 * @ubi: UBI device description object
16 static inline unsigned long *init_seen(struct ubi_device *ubi) in init_seen() argument
20 if (!ubi_dbg_chk_fastmap(ubi)) in init_seen()
23 ret = bitmap_zalloc(ubi->peb_count, GFP_KERNEL); in init_seen()
32 * @seen: integer array of @ubi->peb_count size
41 * @ubi: UBI device description object
43 * @seen: integer array of @ubi->peb_count size
45 static inline void set_seen(struct ubi_device *ubi, int pnum, unsigned long *seen) in set_seen() argument
47 if (!ubi_dbg_chk_fastmap(ubi) || !seen) in set_seen()
55 * @ubi: UBI device description object
56 * @seen: integer array of @ubi->peb_count size
58 static int self_check_seen(struct ubi_device *ubi, unsigned long *seen) in self_check_seen() argument
62 if (!ubi_dbg_chk_fastmap(ubi) || !seen) in self_check_seen()
65 for (pnum = 0; pnum < ubi->peb_count; pnum++) { in self_check_seen()
66 if (!test_bit(pnum, seen) && ubi->lookuptbl[pnum]) { in self_check_seen()
67 ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum); in self_check_seen()
76 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
77 * @ubi: UBI device description object
79 size_t ubi_calc_fm_size(struct ubi_device *ubi) in ubi_calc_fm_size() argument
87 (ubi->peb_count * sizeof(struct ubi_fm_ec)) + in ubi_calc_fm_size()
89 (ubi->peb_count * sizeof(__be32))) + in ubi_calc_fm_size()
91 return roundup(size, ubi->leb_size); in ubi_calc_fm_size()
97 * @ubi: UBI device description object
103 static struct ubi_vid_io_buf *new_fm_vbuf(struct ubi_device *ubi, int vol_id) in new_fm_vbuf() argument
108 new = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in new_fm_vbuf()
116 /* UBI implementations without fastmap support have to delete the in new_fm_vbuf()
127 * @ai: UBI attach info object
233 * @ubi: the UBI device object
241 static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai, in update_vol() argument
273 cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh); in update_vol()
328 * @ubi: UBI device object
335 static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai, in process_pool_aeb() argument
351 ubi_err(ubi, "orphaned volume in fastmap pool!"); in process_pool_aeb()
358 return update_vol(ubi, ai, av, new_vh, new_aeb); in process_pool_aeb()
366 * @ai: UBI attach info object
389 * @ubi: UBI device object
399 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai, in scan_pool() argument
409 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in scan_pool()
413 vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in scan_pool()
433 if (ubi_io_is_bad(ubi, pnum)) { in scan_pool()
434 ubi_err(ubi, "bad PEB in fastmap pool!"); in scan_pool()
439 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); in scan_pool()
441 ubi_err(ubi, "unable to read EC header! PEB:%i err:%i", in scan_pool()
449 * Older UBI implementations have image_seq set to zero, so in scan_pool()
454 if (image_seq && (image_seq != ubi->image_seq)) { in scan_pool()
455 ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x", in scan_pool()
456 be32_to_cpu(ech->image_seq), ubi->image_seq); in scan_pool()
461 err = ubi_io_read_vid_hdr(ubi, pnum, vb, 0); in scan_pool()
494 err = process_pool_aeb(ubi, ai, vh, new_aeb); in scan_pool()
501 ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!"); in scan_pool()
516 * @ai: The UBI attach info object
540 * @ubi: UBI device object
541 * @ai: UBI attach info object
547 static int ubi_attach_fastmap(struct ubi_device *ubi, in ubi_attach_fastmap() argument
561 size_t fm_pos = 0, fm_size = ubi->fm_size; in ubi_attach_fastmap()
563 void *fm_raw = ubi->fm_buf; in ubi_attach_fastmap()
581 ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
591 ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
601 ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
612 ubi_err(ubi, "bad pool size: %i", pool_size); in ubi_attach_fastmap()
617 ubi_err(ubi, "bad WL pool size: %i", wl_pool_size); in ubi_attach_fastmap()
624 ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size); in ubi_attach_fastmap()
630 ubi_err(ubi, "bad maximal WL pool size: %i", in ubi_attach_fastmap()
698 ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
711 ubi_err(ubi, "volume (ID %i) already exists", in ubi_attach_fastmap()
728 ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
748 ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum); in ubi_attach_fastmap()
764 ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free); in ubi_attach_fastmap()
768 ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free); in ubi_attach_fastmap()
789 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count - in ubi_attach_fastmap()
812 * @ai: UBI attach info to be filled
850 * @ubi: UBI device object
851 * @ai: UBI attach info to be filled
852 * @scan_ai: UBI attach info from the first 64 PEBs,
859 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai, in ubi_scan_fastmap() argument
888 down_write(&ubi->fm_protect); in ubi_scan_fastmap()
889 memset(ubi->fm_buf, 0, ubi->fm_size); in ubi_scan_fastmap()
904 ret = ubi_io_read_data(ubi, fmsb, fm_anchor, 0, sizeof(*fmsb)); in ubi_scan_fastmap()
911 ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
918 ubi_err(ubi, "bad fastmap version: %i, expected: %i", in ubi_scan_fastmap()
926 ubi_err(ubi, "number of fastmap blocks is invalid: %i", in ubi_scan_fastmap()
932 fm_size = ubi->leb_size * used_blocks; in ubi_scan_fastmap()
933 if (fm_size != ubi->fm_size) { in ubi_scan_fastmap()
934 ubi_err(ubi, "bad fastmap size: %zi, expected: %zi", in ubi_scan_fastmap()
935 fm_size, ubi->fm_size); in ubi_scan_fastmap()
940 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in ubi_scan_fastmap()
946 vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in ubi_scan_fastmap()
959 if (ubi_io_is_bad(ubi, pnum)) { in ubi_scan_fastmap()
965 ubi_err(ubi, "Fastmap anchor PEB mismatch: PEB: %i vs. %i", in ubi_scan_fastmap()
971 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); in ubi_scan_fastmap()
973 ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)", in ubi_scan_fastmap()
982 if (!ubi->image_seq) in ubi_scan_fastmap()
983 ubi->image_seq = image_seq; in ubi_scan_fastmap()
986 * Older UBI implementations have image_seq set to zero, so in ubi_scan_fastmap()
989 if (image_seq && (image_seq != ubi->image_seq)) { in ubi_scan_fastmap()
990 ubi_err(ubi, "wrong image seq:%d instead of %d", in ubi_scan_fastmap()
991 be32_to_cpu(ech->image_seq), ubi->image_seq); in ubi_scan_fastmap()
996 ret = ubi_io_read_vid_hdr(ubi, pnum, vb, 0); in ubi_scan_fastmap()
998 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)", in ubi_scan_fastmap()
1005 ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
1013 ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
1024 ret = ubi_io_read_data(ubi, ubi->fm_buf + (ubi->leb_size * i), in ubi_scan_fastmap()
1025 pnum, 0, ubi->leb_size); in ubi_scan_fastmap()
1027 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, " in ubi_scan_fastmap()
1036 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf); in ubi_scan_fastmap()
1039 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size); in ubi_scan_fastmap()
1041 ubi_err(ubi, "fastmap data CRC is invalid"); in ubi_scan_fastmap()
1042 ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x", in ubi_scan_fastmap()
1052 ret = ubi_attach_fastmap(ubi, ai, fm); in ubi_scan_fastmap()
1076 ubi->fm = fm; in ubi_scan_fastmap()
1077 ubi->fm_pool.max_size = ubi->fm->max_pool_size; in ubi_scan_fastmap()
1078 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size; in ubi_scan_fastmap()
1079 ubi_msg(ubi, "attached by fastmap"); in ubi_scan_fastmap()
1080 ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size); in ubi_scan_fastmap()
1081 ubi_msg(ubi, "fastmap WL pool size: %d", in ubi_scan_fastmap()
1082 ubi->fm_wl_pool.max_size); in ubi_scan_fastmap()
1083 ubi->fm_disabled = 0; in ubi_scan_fastmap()
1084 ubi->fast_attach = 1; in ubi_scan_fastmap()
1089 up_write(&ubi->fm_protect); in ubi_scan_fastmap()
1091 ubi_err(ubi, "Attach by fastmap failed, doing a full scan!"); in ubi_scan_fastmap()
1105 struct ubi_device *ubi = vol->ubi; in ubi_fastmap_init_checkmap() local
1107 if (!ubi->fast_attach) in ubi_fastmap_init_checkmap()
1124 * @ubi: UBI device object
1129 static int ubi_write_fastmap(struct ubi_device *ubi, in ubi_write_fastmap() argument
1150 fm_raw = ubi->fm_buf; in ubi_write_fastmap()
1151 memset(ubi->fm_buf, 0, ubi->fm_size); in ubi_write_fastmap()
1153 avbuf = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID); in ubi_write_fastmap()
1159 dvbuf = new_fm_vbuf(ubi, UBI_FM_DATA_VOLUME_ID); in ubi_write_fastmap()
1168 seen_pebs = init_seen(ubi); in ubi_write_fastmap()
1174 spin_lock(&ubi->volumes_lock); in ubi_write_fastmap()
1175 spin_lock(&ubi->wl_lock); in ubi_write_fastmap()
1179 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1183 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1201 fmpl->size = cpu_to_be16(ubi->fm_pool.size); in ubi_write_fastmap()
1202 fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size); in ubi_write_fastmap()
1204 for (i = 0; i < ubi->fm_pool.size; i++) { in ubi_write_fastmap()
1205 fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]); in ubi_write_fastmap()
1206 set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs); in ubi_write_fastmap()
1212 fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size); in ubi_write_fastmap()
1213 fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size); in ubi_write_fastmap()
1215 for (i = 0; i < ubi->fm_wl_pool.size; i++) { in ubi_write_fastmap()
1216 fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]); in ubi_write_fastmap()
1217 set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs); in ubi_write_fastmap()
1220 ubi_for_each_free_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1224 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1229 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1233 ubi_for_each_used_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1237 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1242 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1245 ubi_for_each_protected_peb(ubi, i, wl_e) { in ubi_write_fastmap()
1249 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1254 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1258 ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1262 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1267 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1272 list_for_each_entry(ubi_wrk, &ubi->works, list) { in ubi_write_fastmap()
1280 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1285 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1291 vol = ubi->volumes[i]; in ubi_write_fastmap()
1300 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1314 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1327 fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count); in ubi_write_fastmap()
1329 avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_write_fastmap()
1332 spin_unlock(&ubi->wl_lock); in ubi_write_fastmap()
1333 spin_unlock(&ubi->volumes_lock); in ubi_write_fastmap()
1336 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avbuf); in ubi_write_fastmap()
1338 ubi_err(ubi, "unable to write vid_hdr to fastmap SB!"); in ubi_write_fastmap()
1344 set_seen(ubi, new_fm->e[i]->pnum, seen_pebs); in ubi_write_fastmap()
1350 ubi->fm_size)); in ubi_write_fastmap()
1353 dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_write_fastmap()
1357 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvbuf); in ubi_write_fastmap()
1359 ubi_err(ubi, "unable to write vid_hdr to PEB %i!", in ubi_write_fastmap()
1366 ret = ubi_io_write_data(ubi, fm_raw + (i * ubi->leb_size), in ubi_write_fastmap()
1367 new_fm->e[i]->pnum, 0, ubi->leb_size); in ubi_write_fastmap()
1369 ubi_err(ubi, "unable to write fastmap to PEB %i!", in ubi_write_fastmap()
1376 ubi->fm = new_fm; in ubi_write_fastmap()
1378 ret = self_check_seen(ubi, seen_pebs); in ubi_write_fastmap()
1394 * @ubi: UBI device object
1399 static int erase_block(struct ubi_device *ubi, int pnum) in erase_block() argument
1405 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in erase_block()
1409 ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0); in erase_block()
1417 ret = ubi_io_sync_erase(ubi, pnum, 0); in erase_block()
1429 ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); in erase_block()
1441 * @ubi: UBI device object
1443 * This function ensures that upon next UBI attach a full scan
1444 * is issued. We need this if UBI is about to write a new fastmap
1451 static int invalidate_fastmap(struct ubi_device *ubi) in invalidate_fastmap() argument
1459 if (!ubi->fm) in invalidate_fastmap()
1462 ubi->fm = NULL; in invalidate_fastmap()
1469 vb = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID); in invalidate_fastmap()
1476 e = ubi_wl_get_fm_peb(ubi, 1); in invalidate_fastmap()
1481 * Create fake fastmap such that UBI will fall back in invalidate_fastmap()
1484 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in invalidate_fastmap()
1485 ret = ubi_io_write_vid_hdr(ubi, e->pnum, vb); in invalidate_fastmap()
1487 ubi_wl_put_fm_peb(ubi, e, 0, 0); in invalidate_fastmap()
1494 ubi->fm = fm; in invalidate_fastmap()
1508 * @ubi: UBI device object
1511 static void return_fm_pebs(struct ubi_device *ubi, in return_fm_pebs() argument
1521 ubi_wl_put_fm_peb(ubi, fm->e[i], i, in return_fm_pebs()
1529 * ubi_update_fastmap - will be called by UBI if a volume changes or
1531 * @ubi: UBI device object
1535 int ubi_update_fastmap(struct ubi_device *ubi) in ubi_update_fastmap() argument
1541 down_write(&ubi->fm_protect); in ubi_update_fastmap()
1542 down_write(&ubi->work_sem); in ubi_update_fastmap()
1543 down_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1545 ubi_refill_pools(ubi); in ubi_update_fastmap()
1547 if (ubi->ro_mode || ubi->fm_disabled) { in ubi_update_fastmap()
1548 up_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1549 up_write(&ubi->work_sem); in ubi_update_fastmap()
1550 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1556 up_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1557 up_write(&ubi->work_sem); in ubi_update_fastmap()
1558 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1562 new_fm->used_blocks = ubi->fm_size / ubi->leb_size; in ubi_update_fastmap()
1563 old_fm = ubi->fm; in ubi_update_fastmap()
1564 ubi->fm = NULL; in ubi_update_fastmap()
1567 ubi_err(ubi, "fastmap too large"); in ubi_update_fastmap()
1573 spin_lock(&ubi->wl_lock); in ubi_update_fastmap()
1574 tmp_e = ubi_wl_get_fm_peb(ubi, 0); in ubi_update_fastmap()
1575 spin_unlock(&ubi->wl_lock); in ubi_update_fastmap()
1579 ret = erase_block(ubi, old_fm->e[i]->pnum); in ubi_update_fastmap()
1581 ubi_err(ubi, "could not erase old fastmap PEB"); in ubi_update_fastmap()
1584 ubi_wl_put_fm_peb(ubi, new_fm->e[j], in ubi_update_fastmap()
1593 ubi_err(ubi, "could not get any free erase block"); in ubi_update_fastmap()
1596 ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0); in ubi_update_fastmap()
1607 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, in ubi_update_fastmap()
1617 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, in ubi_update_fastmap()
1623 spin_lock(&ubi->wl_lock); in ubi_update_fastmap()
1624 tmp_e = ubi->fm_anchor; in ubi_update_fastmap()
1625 ubi->fm_anchor = NULL; in ubi_update_fastmap()
1626 spin_unlock(&ubi->wl_lock); in ubi_update_fastmap()
1631 ret = erase_block(ubi, old_fm->e[0]->pnum); in ubi_update_fastmap()
1633 ubi_err(ubi, "could not erase old anchor PEB"); in ubi_update_fastmap()
1636 ubi_wl_put_fm_peb(ubi, new_fm->e[i], in ubi_update_fastmap()
1647 ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0, in ubi_update_fastmap()
1654 ubi_err(ubi, "could not find any anchor PEB"); in ubi_update_fastmap()
1657 ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0); in ubi_update_fastmap()
1667 ret = ubi_write_fastmap(ubi, new_fm); in ubi_update_fastmap()
1673 up_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1674 up_write(&ubi->work_sem); in ubi_update_fastmap()
1675 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1678 ubi_ensure_anchor_pebs(ubi); in ubi_update_fastmap()
1683 ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret); in ubi_update_fastmap()
1685 ret = invalidate_fastmap(ubi); in ubi_update_fastmap()
1687 ubi_err(ubi, "Unable to invalidate current fastmap!"); in ubi_update_fastmap()
1688 ubi_ro_mode(ubi); in ubi_update_fastmap()
1690 return_fm_pebs(ubi, old_fm); in ubi_update_fastmap()
1691 return_fm_pebs(ubi, new_fm); in ubi_update_fastmap()