Lines Matching full:ubi

9  * The UBI Eraseblock Association (EBA) sub-system.
34 #include "ubi.h"
65 * @ubi: UBI device description object
71 unsigned long long ubi_next_sqnum(struct ubi_device *ubi) in ubi_next_sqnum() argument
75 spin_lock(&ubi->ltree_lock); in ubi_next_sqnum()
76 sqnum = ubi->global_sqnum++; in ubi_next_sqnum()
77 spin_unlock(&ubi->ltree_lock); in ubi_next_sqnum()
84 * @ubi: UBI device description object
90 static int ubi_get_compat(const struct ubi_device *ubi, int vol_id) in ubi_get_compat() argument
202 * @ubi: UBI device description object
208 * @ubi->ltree_lock has to be locked.
210 static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id, in ltree_lookup() argument
215 p = ubi->ltree.rb_node; in ltree_lookup()
240 * @ubi: UBI device description object
249 static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi, in ltree_add_entry() argument
263 spin_lock(&ubi->ltree_lock); in ltree_add_entry()
264 le1 = ltree_lookup(ubi, vol_id, lnum); in ltree_add_entry()
278 * @ubi->ltree RB-tree. in ltree_add_entry()
282 p = &ubi->ltree.rb_node; in ltree_add_entry()
301 rb_insert_color(&le->rb, &ubi->ltree); in ltree_add_entry()
304 spin_unlock(&ubi->ltree_lock); in ltree_add_entry()
312 * @ubi: UBI device description object
319 static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum) in leb_read_lock() argument
323 le = ltree_add_entry(ubi, vol_id, lnum); in leb_read_lock()
332 * @ubi: UBI device description object
336 static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum) in leb_read_unlock() argument
340 spin_lock(&ubi->ltree_lock); in leb_read_unlock()
341 le = ltree_lookup(ubi, vol_id, lnum); in leb_read_unlock()
346 rb_erase(&le->rb, &ubi->ltree); in leb_read_unlock()
349 spin_unlock(&ubi->ltree_lock); in leb_read_unlock()
354 * @ubi: UBI device description object
361 static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum) in leb_write_lock() argument
365 le = ltree_add_entry(ubi, vol_id, lnum); in leb_write_lock()
374 * @ubi: UBI device description object
383 static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum) in leb_write_trylock() argument
387 le = ltree_add_entry(ubi, vol_id, lnum); in leb_write_trylock()
394 spin_lock(&ubi->ltree_lock); in leb_write_trylock()
398 rb_erase(&le->rb, &ubi->ltree); in leb_write_trylock()
401 spin_unlock(&ubi->ltree_lock); in leb_write_trylock()
408 * @ubi: UBI device description object
412 static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum) in leb_write_unlock() argument
416 spin_lock(&ubi->ltree_lock); in leb_write_unlock()
417 le = ltree_lookup(ubi, vol_id, lnum); in leb_write_unlock()
422 rb_erase(&le->rb, &ubi->ltree); in leb_write_unlock()
425 spin_unlock(&ubi->ltree_lock); in leb_write_unlock()
442 * @ubi: UBI device description object
450 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_eba_unmap_leb() argument
455 if (ubi->ro_mode) in ubi_eba_unmap_leb()
458 err = leb_write_lock(ubi, vol_id, lnum); in ubi_eba_unmap_leb()
469 down_read(&ubi->fm_eba_sem); in ubi_eba_unmap_leb()
471 up_read(&ubi->fm_eba_sem); in ubi_eba_unmap_leb()
472 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0); in ubi_eba_unmap_leb()
475 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_unmap_leb()
482 * @ubi: UBI device description object
497 static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, in check_mapping() argument
504 if (!ubi->fast_attach) in check_mapping()
510 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in check_mapping()
514 err = ubi_io_read_vid_hdr(ubi, *pnum, vidb, 0); in check_mapping()
531 down_read(&ubi->fm_eba_sem); in check_mapping()
533 up_read(&ubi->fm_eba_sem); in check_mapping()
534 ubi_wl_put_peb(ubi, vol->vol_id, lnum, *pnum, torture); in check_mapping()
538 ubi_err(ubi, "unable to read VID header back from PEB %i: %i", in check_mapping()
552 ubi_err(ubi, "EBA mismatch! PEB %i is LEB %i:%i instead of LEB %i:%i", in check_mapping()
554 ubi_ro_mode(ubi); in check_mapping()
569 static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, in check_mapping() argument
578 * @ubi: UBI device description object
595 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, in ubi_eba_read_leb() argument
603 err = leb_read_lock(ubi, vol_id, lnum); in ubi_eba_read_leb()
609 err = check_mapping(ubi, vol, lnum, &pnum); in ubi_eba_read_leb()
622 leb_read_unlock(ubi, vol_id, lnum); in ubi_eba_read_leb()
636 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in ubi_eba_read_leb()
644 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1); in ubi_eba_read_leb()
657 ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d", in ubi_eba_read_leb()
670 * This is valid and works as the layer above UBI in ubi_eba_read_leb()
674 if (ubi->fast_attach) { in ubi_eba_read_leb()
678 ubi_ro_mode(ubi); in ubi_eba_read_leb()
693 err = ubi_io_read_data(ubi, buf, pnum, offset, len); in ubi_eba_read_leb()
702 ubi_msg(ubi, "force data checking"); in ubi_eba_read_leb()
713 ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x", in ubi_eba_read_leb()
721 err = ubi_wl_scrub_peb(ubi, pnum); in ubi_eba_read_leb()
723 leb_read_unlock(ubi, vol_id, lnum); in ubi_eba_read_leb()
729 leb_read_unlock(ubi, vol_id, lnum); in ubi_eba_read_leb()
735 * @ubi: UBI device description object
738 * @sgl: UBI scatter gather list to store the read data
744 * storing the read data into a buffer it writes to an UBI scatter gather
747 int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_eba_read_leb_sg() argument
763 ret = ubi_eba_read_leb(ubi, vol, lnum, in ubi_eba_read_leb_sg()
810 struct ubi_device *ubi = vol->ubi; in try_recover_peb() local
817 new_pnum = ubi_wl_get_peb(ubi); in try_recover_peb()
823 ubi_msg(ubi, "recover PEB %d, move data to PEB %d", in try_recover_peb()
826 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1); in try_recover_peb()
836 mutex_lock(&ubi->buf_mutex); in try_recover_peb()
837 memset(ubi->peb_buf + offset, 0xFF, len); in try_recover_peb()
841 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset); in try_recover_peb()
848 memcpy(ubi->peb_buf + offset, buf, len); in try_recover_peb()
851 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size); in try_recover_peb()
852 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in try_recover_peb()
856 err = ubi_io_write_vid_hdr(ubi, new_pnum, vidb); in try_recover_peb()
860 err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size); in try_recover_peb()
863 mutex_unlock(&ubi->buf_mutex); in try_recover_peb()
869 up_read(&ubi->fm_eba_sem); in try_recover_peb()
872 ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); in try_recover_peb()
873 ubi_msg(ubi, "data was successfully recovered"); in try_recover_peb()
879 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1); in try_recover_peb()
880 ubi_warn(ubi, "failed to write to PEB %d", new_pnum); in try_recover_peb()
888 * @ubi: UBI device description object
902 static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum, in recover_peb() argument
905 int err, idx = vol_id2idx(ubi, vol_id), tries; in recover_peb()
906 struct ubi_volume *vol = ubi->volumes[idx]; in recover_peb()
909 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in recover_peb()
921 ubi_msg(ubi, "try again"); in recover_peb()
948 struct ubi_device *ubi = vol->ubi; in try_write_vid_and_data() local
951 pnum = ubi_wl_get_peb(ubi); in try_write_vid_and_data()
962 err = ubi_io_write_vid_hdr(ubi, pnum, vidb); in try_write_vid_and_data()
964 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d", in try_write_vid_and_data()
970 err = ubi_io_write_data(ubi, buf, pnum, offset, len); in try_write_vid_and_data()
972 ubi_warn(ubi, in try_write_vid_and_data()
982 up_read(&ubi->fm_eba_sem); in try_write_vid_and_data()
985 err2 = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); in try_write_vid_and_data()
987 ubi_warn(ubi, "failed to return physical eraseblock %d, error %d", in try_write_vid_and_data()
991 err2 = ubi_wl_put_peb(ubi, vol_id, lnum, opnum, 0); in try_write_vid_and_data()
993 ubi_warn(ubi, "failed to return physical eraseblock %d, error %d", in try_write_vid_and_data()
1003 * @ubi: UBI device description object
1016 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, in ubi_eba_write_leb() argument
1023 if (ubi->ro_mode) in ubi_eba_write_leb()
1026 err = leb_write_lock(ubi, vol_id, lnum); in ubi_eba_write_leb()
1032 err = check_mapping(ubi, vol, lnum, &pnum); in ubi_eba_write_leb()
1041 err = ubi_io_write_data(ubi, buf, pnum, offset, len); in ubi_eba_write_leb()
1043 ubi_warn(ubi, "failed to write data to PEB %d", pnum); in ubi_eba_write_leb()
1044 if (err == -EIO && ubi->bad_allowed) in ubi_eba_write_leb()
1045 err = recover_peb(ubi, pnum, vol_id, lnum, buf, in ubi_eba_write_leb()
1056 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in ubi_eba_write_leb()
1058 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_write_leb()
1065 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb()
1068 vid_hdr->compat = ubi_get_compat(ubi, vol_id); in ubi_eba_write_leb()
1073 if (err != -EIO || !ubi->bad_allowed) in ubi_eba_write_leb()
1082 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb()
1083 ubi_msg(ubi, "try another PEB"); in ubi_eba_write_leb()
1090 ubi_ro_mode(ubi); in ubi_eba_write_leb()
1092 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_write_leb()
1099 * @ubi: UBI device description object
1119 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_eba_write_leb_st() argument
1127 if (ubi->ro_mode) in ubi_eba_write_leb_st()
1132 len = ALIGN(data_size, ubi->min_io_size); in ubi_eba_write_leb_st()
1134 ubi_assert(!(len & (ubi->min_io_size - 1))); in ubi_eba_write_leb_st()
1136 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in ubi_eba_write_leb_st()
1142 err = leb_write_lock(ubi, vol_id, lnum); in ubi_eba_write_leb_st()
1146 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb_st()
1149 vid_hdr->compat = ubi_get_compat(ubi, vol_id); in ubi_eba_write_leb_st()
1162 if (err != -EIO || !ubi->bad_allowed) in ubi_eba_write_leb_st()
1165 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb_st()
1166 ubi_msg(ubi, "try another PEB"); in ubi_eba_write_leb_st()
1170 ubi_ro_mode(ubi); in ubi_eba_write_leb_st()
1172 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_write_leb_st()
1182 * @ubi: UBI device description object
1194 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
1195 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
1197 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_eba_atomic_leb_change() argument
1205 if (ubi->ro_mode) in ubi_eba_atomic_leb_change()
1213 err = ubi_eba_unmap_leb(ubi, vol, lnum); in ubi_eba_atomic_leb_change()
1216 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0); in ubi_eba_atomic_leb_change()
1219 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in ubi_eba_atomic_leb_change()
1225 mutex_lock(&ubi->alc_mutex); in ubi_eba_atomic_leb_change()
1226 err = leb_write_lock(ubi, vol_id, lnum); in ubi_eba_atomic_leb_change()
1230 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_atomic_leb_change()
1233 vid_hdr->compat = ubi_get_compat(ubi, vol_id); in ubi_eba_atomic_leb_change()
1246 if (err != -EIO || !ubi->bad_allowed) in ubi_eba_atomic_leb_change()
1249 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_atomic_leb_change()
1250 ubi_msg(ubi, "try another PEB"); in ubi_eba_atomic_leb_change()
1259 ubi_ro_mode(ubi); in ubi_eba_atomic_leb_change()
1261 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_atomic_leb_change()
1264 mutex_unlock(&ubi->alc_mutex); in ubi_eba_atomic_leb_change()
1276 * fatal and UBI will be switched to R/O mode later.
1298 * @ubi: UBI device description object
1310 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, in ubi_eba_copy_leb() argument
1318 ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem)); in ubi_eba_copy_leb()
1327 aldata_size = ALIGN(data_size, ubi->min_io_size); in ubi_eba_copy_leb()
1330 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad); in ubi_eba_copy_leb()
1332 idx = vol_id2idx(ubi, vol_id); in ubi_eba_copy_leb()
1333 spin_lock(&ubi->volumes_lock); in ubi_eba_copy_leb()
1340 vol = ubi->volumes[idx]; in ubi_eba_copy_leb()
1341 spin_unlock(&ubi->volumes_lock); in ubi_eba_copy_leb()
1356 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are in ubi_eba_copy_leb()
1357 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the in ubi_eba_copy_leb()
1363 err = leb_write_trylock(ubi, vol_id, lnum); in ubi_eba_copy_leb()
1371 * probably waiting on @ubi->move_mutex. No need to continue the work, in ubi_eba_copy_leb()
1383 * this function utilizes the @ubi->peb_buf buffer which is shared in ubi_eba_copy_leb()
1385 * @ubi->buf_mutex. in ubi_eba_copy_leb()
1387 mutex_lock(&ubi->buf_mutex); in ubi_eba_copy_leb()
1389 err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size); in ubi_eba_copy_leb()
1391 ubi_warn(ubi, "error %d while reading data from PEB %d", in ubi_eba_copy_leb()
1409 ubi_calc_data_len(ubi, ubi->peb_buf, data_size); in ubi_eba_copy_leb()
1412 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size); in ubi_eba_copy_leb()
1426 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_copy_leb()
1428 err = ubi_io_write_vid_hdr(ubi, to, vidb); in ubi_eba_copy_leb()
1438 err = ubi_io_read_vid_hdr(ubi, to, vidb, 1); in ubi_eba_copy_leb()
1441 ubi_warn(ubi, "error %d while reading VID header back from PEB %d", in ubi_eba_copy_leb()
1451 err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size); in ubi_eba_copy_leb()
1465 mutex_unlock(&ubi->buf_mutex); in ubi_eba_copy_leb()
1467 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_copy_leb()
1473 * @ubi: UBI device description object
1474 * @ai: UBI attach info object
1476 * This is a helper function for 'ubi_eba_init()' which is called when UBI
1480 * o if this is a new UBI image, then just print the warning
1481 * o if this is an UBI image which has already been used for some time, print
1485 * The idea is that when UBI is used, PEBs become bad, and the reserved pool
1490 static void print_rsvd_warning(struct ubi_device *ubi, in print_rsvd_warning() argument
1498 int min = ubi->beb_rsvd_level / 10; in print_rsvd_warning()
1502 if (ubi->beb_rsvd_pebs > min) in print_rsvd_warning()
1506 ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d", in print_rsvd_warning()
1507 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level); in print_rsvd_warning()
1508 if (ubi->corr_peb_count) in print_rsvd_warning()
1509 ubi_warn(ubi, "%d PEBs are corrupted and not used", in print_rsvd_warning()
1510 ubi->corr_peb_count); in print_rsvd_warning()
1515 * @ubi: UBI device description object
1516 * @ai_fastmap: UBI attach info object created by fastmap
1517 * @ai_scan: UBI attach info object created by scanning
1523 int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap, in self_check_eba() argument
1533 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; in self_check_eba()
1546 vol = ubi->volumes[i]; in self_check_eba()
1570 av = ubi_find_av(ai_scan, idx2vol_id(ubi, i)); in self_check_eba()
1577 av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i)); in self_check_eba()
1590 ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!", in self_check_eba()
1600 if (!ubi->volumes[i]) in self_check_eba()
1614 * @ubi: UBI device description object
1620 int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai) in ubi_eba_init() argument
1630 spin_lock_init(&ubi->ltree_lock); in ubi_eba_init()
1631 mutex_init(&ubi->alc_mutex); in ubi_eba_init()
1632 ubi->ltree = RB_ROOT; in ubi_eba_init()
1634 ubi->global_sqnum = ai->max_sqnum + 1; in ubi_eba_init()
1635 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; in ubi_eba_init()
1640 vol = ubi->volumes[i]; in ubi_eba_init()
1654 av = ubi_find_av(ai, idx2vol_id(ubi, i)); in ubi_eba_init()
1674 if (ubi->avail_pebs < EBA_RESERVED_PEBS) { in ubi_eba_init()
1675 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)", in ubi_eba_init()
1676 ubi->avail_pebs, EBA_RESERVED_PEBS); in ubi_eba_init()
1677 if (ubi->corr_peb_count) in ubi_eba_init()
1678 ubi_err(ubi, "%d PEBs are corrupted and not used", in ubi_eba_init()
1679 ubi->corr_peb_count); in ubi_eba_init()
1683 ubi->avail_pebs -= EBA_RESERVED_PEBS; in ubi_eba_init()
1684 ubi->rsvd_pebs += EBA_RESERVED_PEBS; in ubi_eba_init()
1686 if (ubi->bad_allowed) { in ubi_eba_init()
1687 ubi_calculate_reserved(ubi); in ubi_eba_init()
1689 if (ubi->avail_pebs < ubi->beb_rsvd_level) { in ubi_eba_init()
1691 ubi->beb_rsvd_pebs = ubi->avail_pebs; in ubi_eba_init()
1692 print_rsvd_warning(ubi, ai); in ubi_eba_init()
1694 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level; in ubi_eba_init()
1696 ubi->avail_pebs -= ubi->beb_rsvd_pebs; in ubi_eba_init()
1697 ubi->rsvd_pebs += ubi->beb_rsvd_pebs; in ubi_eba_init()
1705 if (!ubi->volumes[i]) in ubi_eba_init()
1707 ubi_eba_replace_table(ubi->volumes[i], NULL); in ubi_eba_init()