Lines Matching refs:ubi

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()
90 static int ubi_get_compat(const struct ubi_device *ubi, int vol_id) in ubi_get_compat() argument
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()
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()
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()
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()
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()
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()
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()
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()
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()
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
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()
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()
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()
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 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); in try_write_vid_and_data()
987 err = ubi_wl_put_peb(ubi, vol_id, lnum, opnum, 0); in try_write_vid_and_data()
1007 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, in ubi_eba_write_leb() argument
1014 if (ubi->ro_mode) in ubi_eba_write_leb()
1017 err = leb_write_lock(ubi, vol_id, lnum); in ubi_eba_write_leb()
1023 err = check_mapping(ubi, vol, lnum, &pnum); in ubi_eba_write_leb()
1032 err = ubi_io_write_data(ubi, buf, pnum, offset, len); in ubi_eba_write_leb()
1034 ubi_warn(ubi, "failed to write data to PEB %d", pnum); in ubi_eba_write_leb()
1035 if (err == -EIO && ubi->bad_allowed) in ubi_eba_write_leb()
1036 err = recover_peb(ubi, pnum, vol_id, lnum, buf, in ubi_eba_write_leb()
1047 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in ubi_eba_write_leb()
1049 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_write_leb()
1056 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb()
1059 vid_hdr->compat = ubi_get_compat(ubi, vol_id); in ubi_eba_write_leb()
1064 if (err != -EIO || !ubi->bad_allowed) in ubi_eba_write_leb()
1073 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb()
1074 ubi_msg(ubi, "try another PEB"); in ubi_eba_write_leb()
1081 ubi_ro_mode(ubi); in ubi_eba_write_leb()
1083 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_write_leb()
1110 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_eba_write_leb_st() argument
1118 if (ubi->ro_mode) in ubi_eba_write_leb_st()
1123 len = ALIGN(data_size, ubi->min_io_size); in ubi_eba_write_leb_st()
1125 ubi_assert(!(len & (ubi->min_io_size - 1))); in ubi_eba_write_leb_st()
1127 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in ubi_eba_write_leb_st()
1133 err = leb_write_lock(ubi, vol_id, lnum); in ubi_eba_write_leb_st()
1137 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb_st()
1140 vid_hdr->compat = ubi_get_compat(ubi, vol_id); in ubi_eba_write_leb_st()
1153 if (err != -EIO || !ubi->bad_allowed) in ubi_eba_write_leb_st()
1156 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_write_leb_st()
1157 ubi_msg(ubi, "try another PEB"); in ubi_eba_write_leb_st()
1161 ubi_ro_mode(ubi); in ubi_eba_write_leb_st()
1163 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_write_leb_st()
1188 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_eba_atomic_leb_change() argument
1196 if (ubi->ro_mode) in ubi_eba_atomic_leb_change()
1204 err = ubi_eba_unmap_leb(ubi, vol, lnum); in ubi_eba_atomic_leb_change()
1207 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0); in ubi_eba_atomic_leb_change()
1210 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in ubi_eba_atomic_leb_change()
1216 mutex_lock(&ubi->alc_mutex); in ubi_eba_atomic_leb_change()
1217 err = leb_write_lock(ubi, vol_id, lnum); in ubi_eba_atomic_leb_change()
1221 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_atomic_leb_change()
1224 vid_hdr->compat = ubi_get_compat(ubi, vol_id); in ubi_eba_atomic_leb_change()
1237 if (err != -EIO || !ubi->bad_allowed) in ubi_eba_atomic_leb_change()
1240 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_atomic_leb_change()
1241 ubi_msg(ubi, "try another PEB"); in ubi_eba_atomic_leb_change()
1250 ubi_ro_mode(ubi); in ubi_eba_atomic_leb_change()
1252 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_atomic_leb_change()
1255 mutex_unlock(&ubi->alc_mutex); in ubi_eba_atomic_leb_change()
1301 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, in ubi_eba_copy_leb() argument
1309 ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem)); in ubi_eba_copy_leb()
1318 aldata_size = ALIGN(data_size, ubi->min_io_size); in ubi_eba_copy_leb()
1321 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad); in ubi_eba_copy_leb()
1323 idx = vol_id2idx(ubi, vol_id); in ubi_eba_copy_leb()
1324 spin_lock(&ubi->volumes_lock); in ubi_eba_copy_leb()
1331 vol = ubi->volumes[idx]; in ubi_eba_copy_leb()
1332 spin_unlock(&ubi->volumes_lock); in ubi_eba_copy_leb()
1354 err = leb_write_trylock(ubi, vol_id, lnum); in ubi_eba_copy_leb()
1378 mutex_lock(&ubi->buf_mutex); in ubi_eba_copy_leb()
1380 err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size); in ubi_eba_copy_leb()
1382 ubi_warn(ubi, "error %d while reading data from PEB %d", in ubi_eba_copy_leb()
1400 ubi_calc_data_len(ubi, ubi->peb_buf, data_size); in ubi_eba_copy_leb()
1403 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size); in ubi_eba_copy_leb()
1417 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_eba_copy_leb()
1419 err = ubi_io_write_vid_hdr(ubi, to, vidb); in ubi_eba_copy_leb()
1429 err = ubi_io_read_vid_hdr(ubi, to, vidb, 1); in ubi_eba_copy_leb()
1432 ubi_warn(ubi, "error %d while reading VID header back from PEB %d", in ubi_eba_copy_leb()
1442 err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size); in ubi_eba_copy_leb()
1456 mutex_unlock(&ubi->buf_mutex); in ubi_eba_copy_leb()
1458 leb_write_unlock(ubi, vol_id, lnum); in ubi_eba_copy_leb()
1481 static void print_rsvd_warning(struct ubi_device *ubi, in print_rsvd_warning() argument
1489 int min = ubi->beb_rsvd_level / 10; in print_rsvd_warning()
1493 if (ubi->beb_rsvd_pebs > min) in print_rsvd_warning()
1497 ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d", in print_rsvd_warning()
1498 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level); in print_rsvd_warning()
1499 if (ubi->corr_peb_count) in print_rsvd_warning()
1500 ubi_warn(ubi, "%d PEBs are corrupted and not used", in print_rsvd_warning()
1501 ubi->corr_peb_count); in print_rsvd_warning()
1514 int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap, in self_check_eba() argument
1524 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; in self_check_eba()
1537 vol = ubi->volumes[i]; in self_check_eba()
1560 av = ubi_find_av(ai_scan, idx2vol_id(ubi, i)); in self_check_eba()
1567 av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i)); in self_check_eba()
1580 ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!", in self_check_eba()
1590 if (!ubi->volumes[i]) in self_check_eba()
1610 int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai) in ubi_eba_init() argument
1620 spin_lock_init(&ubi->ltree_lock); in ubi_eba_init()
1621 mutex_init(&ubi->alc_mutex); in ubi_eba_init()
1622 ubi->ltree = RB_ROOT; in ubi_eba_init()
1624 ubi->global_sqnum = ai->max_sqnum + 1; in ubi_eba_init()
1625 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; in ubi_eba_init()
1630 vol = ubi->volumes[i]; in ubi_eba_init()
1644 av = ubi_find_av(ai, idx2vol_id(ubi, i)); in ubi_eba_init()
1664 if (ubi->avail_pebs < EBA_RESERVED_PEBS) { in ubi_eba_init()
1665 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)", in ubi_eba_init()
1666 ubi->avail_pebs, EBA_RESERVED_PEBS); in ubi_eba_init()
1667 if (ubi->corr_peb_count) in ubi_eba_init()
1668 ubi_err(ubi, "%d PEBs are corrupted and not used", in ubi_eba_init()
1669 ubi->corr_peb_count); in ubi_eba_init()
1673 ubi->avail_pebs -= EBA_RESERVED_PEBS; in ubi_eba_init()
1674 ubi->rsvd_pebs += EBA_RESERVED_PEBS; in ubi_eba_init()
1676 if (ubi->bad_allowed) { in ubi_eba_init()
1677 ubi_calculate_reserved(ubi); in ubi_eba_init()
1679 if (ubi->avail_pebs < ubi->beb_rsvd_level) { in ubi_eba_init()
1681 ubi->beb_rsvd_pebs = ubi->avail_pebs; in ubi_eba_init()
1682 print_rsvd_warning(ubi, ai); in ubi_eba_init()
1684 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level; in ubi_eba_init()
1686 ubi->avail_pebs -= ubi->beb_rsvd_pebs; in ubi_eba_init()
1687 ubi->rsvd_pebs += ubi->beb_rsvd_pebs; in ubi_eba_init()
1695 if (!ubi->volumes[i]) in ubi_eba_init()
1697 ubi_eba_replace_table(ubi->volumes[i], NULL); in ubi_eba_init()