Lines Matching full:ubi
9 * The UBI Eraseblock Association (EBA) sub-system.
34 #include "ubi.h"
62 * @ubi: UBI device description object
68 unsigned long long ubi_next_sqnum(struct ubi_device *ubi) in ubi_next_sqnum() argument
72 spin_lock(&ubi->ltree_lock); in ubi_next_sqnum()
73 sqnum = ubi->global_sqnum++; in ubi_next_sqnum()
74 spin_unlock(&ubi->ltree_lock); in ubi_next_sqnum()
81 * @ubi: UBI device description object
87 static int ubi_get_compat(const struct ubi_device *ubi, int vol_id) in ubi_get_compat() argument
199 * @ubi: UBI device description object
205 * @ubi->ltree_lock has to be locked.
207 static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id, in ltree_lookup() argument
212 p = ubi->ltree.rb_node; in ltree_lookup()
237 * @ubi: UBI device description object
246 static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi, in ltree_add_entry() argument
260 spin_lock(&ubi->ltree_lock); in ltree_add_entry()
261 le1 = ltree_lookup(ubi, vol_id, lnum); in ltree_add_entry()
275 * @ubi->ltree RB-tree. in ltree_add_entry()
279 p = &ubi->ltree.rb_node; in ltree_add_entry()
298 rb_insert_color(&le->rb, &ubi->ltree); in ltree_add_entry()
301 spin_unlock(&ubi->ltree_lock); in ltree_add_entry()
309 * @ubi: UBI device description object
316 static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum) in leb_read_lock() argument
320 le = ltree_add_entry(ubi, vol_id, lnum); in leb_read_lock()
329 * @ubi: UBI device description object
333 static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum) in leb_read_unlock() argument
337 spin_lock(&ubi->ltree_lock); in leb_read_unlock()
338 le = ltree_lookup(ubi, vol_id, lnum); in leb_read_unlock()
343 rb_erase(&le->rb, &ubi->ltree); in leb_read_unlock()
346 spin_unlock(&ubi->ltree_lock); in leb_read_unlock()
351 * @ubi: UBI device description object
358 static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum) in leb_write_lock() argument
362 le = ltree_add_entry(ubi, vol_id, lnum); in leb_write_lock()
371 * @ubi: UBI device description object
380 static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum) in leb_write_trylock() argument
384 le = ltree_add_entry(ubi, vol_id, lnum); in leb_write_trylock()
391 spin_lock(&ubi->ltree_lock); in leb_write_trylock()
395 rb_erase(&le->rb, &ubi->ltree); in leb_write_trylock()
398 spin_unlock(&ubi->ltree_lock); in leb_write_trylock()
405 * @ubi: UBI device description object
409 static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum) in leb_write_unlock() argument
413 spin_lock(&ubi->ltree_lock); in leb_write_unlock()
414 le = ltree_lookup(ubi, vol_id, lnum); in leb_write_unlock()
419 rb_erase(&le->rb, &ubi->ltree); in leb_write_unlock()
422 spin_unlock(&ubi->ltree_lock); in leb_write_unlock()
439 * @ubi: UBI device description object
447 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_eba_unmap_leb() argument
452 if (ubi->ro_mode) in ubi_eba_unmap_leb()
455 err = leb_write_lock(ubi, vol_id, lnum); in ubi_eba_unmap_leb()
466 down_read(&ubi->fm_eba_sem); in ubi_eba_unmap_leb()
468 up_read(&ubi->fm_eba_sem); in ubi_eba_unmap_leb()
469 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0); in ubi_eba_unmap_leb()
472 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_unmap_leb()
479 * @ubi: UBI device description object
494 static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, in check_mapping() argument
501 if (!ubi->fast_attach) in check_mapping()
507 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in check_mapping()
511 err = ubi_io_read_vid_hdr(ubi, *pnum, vidb, 0); in check_mapping()
528 down_read(&ubi->fm_eba_sem); in check_mapping()
530 up_read(&ubi->fm_eba_sem); in check_mapping()
531 ubi_wl_put_peb(ubi, vol->vol_id, lnum, *pnum, torture); in check_mapping()
535 ubi_err(ubi, "unable to read VID header back from PEB %i: %i", in check_mapping()
549 ubi_err(ubi, "EBA mismatch! PEB %i is LEB %i:%i instead of LEB %i:%i", in check_mapping()
551 ubi_ro_mode(ubi); in check_mapping()
566 static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, in check_mapping() argument
575 * @ubi: UBI device description object
592 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, in ubi_eba_read_leb() argument
600 err = leb_read_lock(ubi, vol_id, lnum); in ubi_eba_read_leb()
606 err = check_mapping(ubi, vol, lnum, &pnum); in ubi_eba_read_leb()
619 leb_read_unlock(ubi, vol_id, lnum); in ubi_eba_read_leb()
633 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in ubi_eba_read_leb()
641 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1); in ubi_eba_read_leb()
654 ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d", in ubi_eba_read_leb()
667 * This is valid and works as the layer above UBI in ubi_eba_read_leb()
671 if (ubi->fast_attach) { in ubi_eba_read_leb()
675 ubi_ro_mode(ubi); in ubi_eba_read_leb()
690 err = ubi_io_read_data(ubi, buf, pnum, offset, len); in ubi_eba_read_leb()
699 ubi_msg(ubi, "force data checking"); in ubi_eba_read_leb()
710 ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x", in ubi_eba_read_leb()
718 err = ubi_wl_scrub_peb(ubi, pnum); in ubi_eba_read_leb()
720 leb_read_unlock(ubi, vol_id, lnum); in ubi_eba_read_leb()
726 leb_read_unlock(ubi, vol_id, lnum); in ubi_eba_read_leb()
732 * @ubi: UBI device description object
735 * @sgl: UBI scatter gather list to store the read data
741 * storing the read data into a buffer it writes to an UBI scatter gather
744 int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_eba_read_leb_sg() argument
760 ret = ubi_eba_read_leb(ubi, vol, lnum, in ubi_eba_read_leb_sg()
807 struct ubi_device *ubi = vol->ubi; in try_recover_peb() local
814 new_pnum = ubi_wl_get_peb(ubi); in try_recover_peb()
820 ubi_msg(ubi, "recover PEB %d, move data to PEB %d", in try_recover_peb()
823 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1); in try_recover_peb()
833 mutex_lock(&ubi->buf_mutex); in try_recover_peb()
834 memset(ubi->peb_buf + offset, 0xFF, len); in try_recover_peb()
838 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset); in try_recover_peb()
845 memcpy(ubi->peb_buf + offset, buf, len); in try_recover_peb()
848 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size); in try_recover_peb()
849 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in try_recover_peb()
853 err = ubi_io_write_vid_hdr(ubi, new_pnum, vidb); in try_recover_peb()
857 err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size); in try_recover_peb()
860 mutex_unlock(&ubi->buf_mutex); in try_recover_peb()
866 up_read(&ubi->fm_eba_sem); in try_recover_peb()
869 ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); in try_recover_peb()
870 ubi_msg(ubi, "data was successfully recovered"); in try_recover_peb()
876 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1); in try_recover_peb()
877 ubi_warn(ubi, "failed to write to PEB %d", new_pnum); in try_recover_peb()
885 * @ubi: UBI device description object
899 static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum, in recover_peb() argument
902 int err, idx = vol_id2idx(ubi, vol_id), tries; in recover_peb()
903 struct ubi_volume *vol = ubi->volumes[idx]; in recover_peb()
906 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in recover_peb()
918 ubi_msg(ubi, "try again"); in recover_peb()
945 struct ubi_device *ubi = vol->ubi; in try_write_vid_and_data() local
948 pnum = ubi_wl_get_peb(ubi); in try_write_vid_and_data()
959 err = ubi_io_write_vid_hdr(ubi, pnum, vidb); in try_write_vid_and_data()
961 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d", in try_write_vid_and_data()
967 err = ubi_io_write_data(ubi, buf, pnum, offset, len); in try_write_vid_and_data()
969 ubi_warn(ubi, in try_write_vid_and_data()
979 up_read(&ubi->fm_eba_sem); in try_write_vid_and_data()
982 err2 = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); in try_write_vid_and_data()
984 ubi_warn(ubi, "failed to return physical eraseblock %d, error %d", in try_write_vid_and_data()
988 err2 = ubi_wl_put_peb(ubi, vol_id, lnum, opnum, 0); in try_write_vid_and_data()
990 ubi_warn(ubi, "failed to return physical eraseblock %d, error %d", in try_write_vid_and_data()
1000 * @ubi: UBI device description object
1013 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, in ubi_eba_write_leb() argument
1020 if (ubi->ro_mode) in ubi_eba_write_leb()
1023 err = leb_write_lock(ubi, vol_id, lnum); in ubi_eba_write_leb()
1029 err = check_mapping(ubi, vol, lnum, &pnum); in ubi_eba_write_leb()
1038 err = ubi_io_write_data(ubi, buf, pnum, offset, len); in ubi_eba_write_leb()
1040 ubi_warn(ubi, "failed to write data to PEB %d", pnum); in ubi_eba_write_leb()
1041 if (err == -EIO && ubi->bad_allowed) in ubi_eba_write_leb()
1042 err = recover_peb(ubi, pnum, vol_id, lnum, buf, in ubi_eba_write_leb()
1053 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in ubi_eba_write_leb()
1055 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_write_leb()
1062 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb()
1065 vid_hdr->compat = ubi_get_compat(ubi, vol_id); in ubi_eba_write_leb()
1070 if (err != -EIO || !ubi->bad_allowed) in ubi_eba_write_leb()
1079 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb()
1080 ubi_msg(ubi, "try another PEB"); in ubi_eba_write_leb()
1087 ubi_ro_mode(ubi); in ubi_eba_write_leb()
1089 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_write_leb()
1096 * @ubi: UBI device description object
1116 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_eba_write_leb_st() argument
1124 if (ubi->ro_mode) in ubi_eba_write_leb_st()
1129 len = ALIGN(data_size, ubi->min_io_size); in ubi_eba_write_leb_st()
1131 ubi_assert(!(len & (ubi->min_io_size - 1))); in ubi_eba_write_leb_st()
1133 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in ubi_eba_write_leb_st()
1139 err = leb_write_lock(ubi, vol_id, lnum); in ubi_eba_write_leb_st()
1143 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb_st()
1146 vid_hdr->compat = ubi_get_compat(ubi, vol_id); in ubi_eba_write_leb_st()
1159 if (err != -EIO || !ubi->bad_allowed) in ubi_eba_write_leb_st()
1162 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb_st()
1163 ubi_msg(ubi, "try another PEB"); in ubi_eba_write_leb_st()
1167 ubi_ro_mode(ubi); in ubi_eba_write_leb_st()
1169 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_write_leb_st()
1179 * @ubi: UBI device description object
1191 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
1192 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
1194 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_eba_atomic_leb_change() argument
1202 if (ubi->ro_mode) in ubi_eba_atomic_leb_change()
1210 err = ubi_eba_unmap_leb(ubi, vol, lnum); in ubi_eba_atomic_leb_change()
1213 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0); in ubi_eba_atomic_leb_change()
1216 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in ubi_eba_atomic_leb_change()
1222 mutex_lock(&ubi->alc_mutex); in ubi_eba_atomic_leb_change()
1223 err = leb_write_lock(ubi, vol_id, lnum); in ubi_eba_atomic_leb_change()
1227 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_atomic_leb_change()
1230 vid_hdr->compat = ubi_get_compat(ubi, vol_id); in ubi_eba_atomic_leb_change()
1243 if (err != -EIO || !ubi->bad_allowed) in ubi_eba_atomic_leb_change()
1246 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_atomic_leb_change()
1247 ubi_msg(ubi, "try another PEB"); in ubi_eba_atomic_leb_change()
1256 ubi_ro_mode(ubi); in ubi_eba_atomic_leb_change()
1258 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_atomic_leb_change()
1261 mutex_unlock(&ubi->alc_mutex); in ubi_eba_atomic_leb_change()
1273 * fatal and UBI will be switched to R/O mode later.
1295 * @ubi: UBI device description object
1307 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, in ubi_eba_copy_leb() argument
1315 ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem)); in ubi_eba_copy_leb()
1324 aldata_size = ALIGN(data_size, ubi->min_io_size); in ubi_eba_copy_leb()
1327 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad); in ubi_eba_copy_leb()
1329 idx = vol_id2idx(ubi, vol_id); in ubi_eba_copy_leb()
1330 spin_lock(&ubi->volumes_lock); in ubi_eba_copy_leb()
1337 vol = ubi->volumes[idx]; in ubi_eba_copy_leb()
1338 spin_unlock(&ubi->volumes_lock); in ubi_eba_copy_leb()
1353 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are in ubi_eba_copy_leb()
1354 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the in ubi_eba_copy_leb()
1360 err = leb_write_trylock(ubi, vol_id, lnum); in ubi_eba_copy_leb()
1368 * probably waiting on @ubi->move_mutex. No need to continue the work, in ubi_eba_copy_leb()
1380 * this function utilizes the @ubi->peb_buf buffer which is shared in ubi_eba_copy_leb()
1382 * @ubi->buf_mutex. in ubi_eba_copy_leb()
1384 mutex_lock(&ubi->buf_mutex); in ubi_eba_copy_leb()
1386 err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size); in ubi_eba_copy_leb()
1388 ubi_warn(ubi, "error %d while reading data from PEB %d", in ubi_eba_copy_leb()
1406 ubi_calc_data_len(ubi, ubi->peb_buf, data_size); in ubi_eba_copy_leb()
1409 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size); in ubi_eba_copy_leb()
1423 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_copy_leb()
1425 err = ubi_io_write_vid_hdr(ubi, to, vidb); in ubi_eba_copy_leb()
1435 err = ubi_io_read_vid_hdr(ubi, to, vidb, 1); in ubi_eba_copy_leb()
1438 ubi_warn(ubi, "error %d while reading VID header back from PEB %d", in ubi_eba_copy_leb()
1448 err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size); in ubi_eba_copy_leb()
1464 spin_lock(&ubi->volumes_lock); in ubi_eba_copy_leb()
1466 spin_unlock(&ubi->volumes_lock); in ubi_eba_copy_leb()
1469 mutex_unlock(&ubi->buf_mutex); in ubi_eba_copy_leb()
1471 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_copy_leb()
1477 * @ubi: UBI device description object
1478 * @ai: UBI attach info object
1480 * This is a helper function for 'ubi_eba_init()' which is called when UBI
1484 * o if this is a new UBI image, then just print the warning
1485 * o if this is an UBI image which has already been used for some time, print
1489 * The idea is that when UBI is used, PEBs become bad, and the reserved pool
1494 static void print_rsvd_warning(struct ubi_device *ubi, in print_rsvd_warning() argument
1502 int min = ubi->beb_rsvd_level / 10; in print_rsvd_warning()
1506 if (ubi->beb_rsvd_pebs > min) in print_rsvd_warning()
1510 ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d", in print_rsvd_warning()
1511 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level); in print_rsvd_warning()
1512 if (ubi->corr_peb_count) in print_rsvd_warning()
1513 ubi_warn(ubi, "%d PEBs are corrupted and not used", in print_rsvd_warning()
1514 ubi->corr_peb_count); in print_rsvd_warning()
1519 * @ubi: UBI device description object
1520 * @ai_fastmap: UBI attach info object created by fastmap
1521 * @ai_scan: UBI attach info object created by scanning
1527 int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap, in self_check_eba() argument
1537 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; in self_check_eba()
1550 vol = ubi->volumes[i]; in self_check_eba()
1574 av = ubi_find_av(ai_scan, idx2vol_id(ubi, i)); in self_check_eba()
1581 av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i)); in self_check_eba()
1594 ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!", in self_check_eba()
1604 if (!ubi->volumes[i]) in self_check_eba()
1618 * @ubi: UBI device description object
1624 int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai) in ubi_eba_init() argument
1634 spin_lock_init(&ubi->ltree_lock); in ubi_eba_init()
1635 mutex_init(&ubi->alc_mutex); in ubi_eba_init()
1636 ubi->ltree = RB_ROOT; in ubi_eba_init()
1638 ubi->global_sqnum = ai->max_sqnum + 1; in ubi_eba_init()
1639 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; in ubi_eba_init()
1644 vol = ubi->volumes[i]; in ubi_eba_init()
1658 av = ubi_find_av(ai, idx2vol_id(ubi, i)); in ubi_eba_init()
1678 if (ubi->avail_pebs < EBA_RESERVED_PEBS) { in ubi_eba_init()
1679 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)", in ubi_eba_init()
1680 ubi->avail_pebs, EBA_RESERVED_PEBS); in ubi_eba_init()
1681 if (ubi->corr_peb_count) in ubi_eba_init()
1682 ubi_err(ubi, "%d PEBs are corrupted and not used", in ubi_eba_init()
1683 ubi->corr_peb_count); in ubi_eba_init()
1687 ubi->avail_pebs -= EBA_RESERVED_PEBS; in ubi_eba_init()
1688 ubi->rsvd_pebs += EBA_RESERVED_PEBS; in ubi_eba_init()
1690 if (ubi->bad_allowed) { in ubi_eba_init()
1691 ubi_calculate_reserved(ubi); in ubi_eba_init()
1693 if (ubi->avail_pebs < ubi->beb_rsvd_level) { in ubi_eba_init()
1695 ubi->beb_rsvd_pebs = ubi->avail_pebs; in ubi_eba_init()
1696 print_rsvd_warning(ubi, ai); in ubi_eba_init()
1698 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level; in ubi_eba_init()
1700 ubi->avail_pebs -= ubi->beb_rsvd_pebs; in ubi_eba_init()
1701 ubi->rsvd_pebs += ubi->beb_rsvd_pebs; in ubi_eba_init()
1709 if (!ubi->volumes[i]) in ubi_eba_init()
1711 ubi_eba_replace_table(ubi->volumes[i], NULL); in ubi_eba_init()