44 #include <linux/slab.h>
50 #define EBA_RESERVED_PEBS 1
62 unsigned long long sqnum;
101 p = ubi->
ltree.rb_node;
107 if (vol_id < le->vol_id)
109 else if (vol_id > le->
vol_id)
114 else if (lnum > le->
lnum)
136 int vol_id,
int lnum)
150 le1 = ltree_lookup(ubi, vol_id, lnum);
168 p = &ubi->
ltree.rb_node;
173 if (vol_id < le1->vol_id)
175 else if (vol_id > le1->
vol_id)
179 if (lnum < le1->lnum)
186 rb_link_node(&le->
rb, parent, p);
205 static int leb_read_lock(
struct ubi_device *ubi,
int vol_id,
int lnum)
209 le = ltree_add_entry(ubi, vol_id, lnum);
222 static void leb_read_unlock(
struct ubi_device *ubi,
int vol_id,
int lnum)
227 le = ltree_lookup(ubi, vol_id, lnum);
231 if (le->
users == 0) {
247 static int leb_write_lock(
struct ubi_device *ubi,
int vol_id,
int lnum)
251 le = ltree_add_entry(ubi, vol_id, lnum);
269 static int leb_write_trylock(
struct ubi_device *ubi,
int vol_id,
int lnum)
273 le = ltree_add_entry(ubi, vol_id, lnum);
283 if (le->
users == 0) {
298 static void leb_write_unlock(
struct ubi_device *ubi,
int vol_id,
int lnum)
303 le = ltree_lookup(ubi, vol_id, lnum);
307 if (le->
users == 0) {
332 err = leb_write_lock(ubi, vol_id, lnum);
341 dbg_eba(
"erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
349 leb_write_unlock(ubi, vol_id, lnum);
375 int err, pnum, scrub = 0, vol_id = vol->
vol_id;
379 err = leb_read_lock(ubi, vol_id, lnum);
390 dbg_eba(
"read %d bytes from offset %d of LEB %d:%d (unmapped)",
391 len, offset, vol_id, lnum);
392 leb_read_unlock(ubi, vol_id, lnum);
398 dbg_eba(
"read %d bytes from offset %d of LEB %d:%d, PEB %d",
399 len, offset, vol_id, lnum, pnum);
406 vid_hdr = ubi_zalloc_vid_hdr(ubi,
GFP_NOFS);
425 ubi_warn(
"corrupted VID header at PEB %d, LEB %d:%d",
439 ubi_free_vid_hdr(ubi, vid_hdr);
442 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
447 }
else if (mtd_is_eccerr(err)) {
452 ubi_msg(
"force data checking");
463 ubi_warn(
"CRC error: calculated %#08x, must be %#08x",
473 leb_read_unlock(ubi, vol_id, lnum);
477 ubi_free_vid_hdr(ubi, vid_hdr);
479 leb_read_unlock(ubi, vol_id, lnum);
499 static int recover_peb(
struct ubi_device *ubi,
int pnum,
int vol_id,
int lnum,
502 int err,
idx = vol_id2idx(ubi, vol_id), new_pnum,
data_size, tries = 0;
506 vid_hdr = ubi_zalloc_vid_hdr(ubi,
GFP_NOFS);
513 ubi_free_vid_hdr(ubi, vid_hdr);
517 ubi_msg(
"recover PEB %d, move data to PEB %d", pnum, new_pnum);
531 data_size = offset + len;
537 err = ubi_io_read_data(ubi, ubi->
peb_buf, pnum, 0, offset);
544 err = ubi_io_write_data(ubi, ubi->
peb_buf, new_pnum, 0, data_size);
551 ubi_free_vid_hdr(ubi, vid_hdr);
558 ubi_msg(
"data was successfully recovered");
565 ubi_free_vid_hdr(ubi, vid_hdr);
573 ubi_warn(
"failed to write to PEB %d", new_pnum);
576 ubi_free_vid_hdr(ubi, vid_hdr);
598 const void *buf,
int offset,
int len)
600 int err, pnum, tries = 0, vol_id = vol->
vol_id;
606 err = leb_write_lock(ubi, vol_id, lnum);
612 dbg_eba(
"write %d bytes at offset %d of LEB %d:%d, PEB %d",
613 len, offset, vol_id, lnum, pnum);
615 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
617 ubi_warn(
"failed to write data to PEB %d", pnum);
619 err = recover_peb(ubi, pnum, vol_id, lnum, buf,
624 leb_write_unlock(ubi, vol_id, lnum);
632 vid_hdr = ubi_zalloc_vid_hdr(ubi,
GFP_NOFS);
634 leb_write_unlock(ubi, vol_id, lnum);
642 vid_hdr->
compat = ubi_get_compat(ubi, vol_id);
648 ubi_free_vid_hdr(ubi, vid_hdr);
649 leb_write_unlock(ubi, vol_id, lnum);
653 dbg_eba(
"write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
654 len, offset, vol_id, lnum, pnum);
658 ubi_warn(
"failed to write VID header to LEB %d:%d, PEB %d",
664 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
666 ubi_warn(
"failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
667 len, offset, vol_id, lnum, pnum);
676 leb_write_unlock(ubi, vol_id, lnum);
677 ubi_free_vid_hdr(ubi, vid_hdr);
683 leb_write_unlock(ubi, vol_id, lnum);
684 ubi_free_vid_hdr(ubi, vid_hdr);
696 leb_write_unlock(ubi, vol_id, lnum);
697 ubi_free_vid_hdr(ubi, vid_hdr);
729 int lnum,
const void *buf,
int len,
int used_ebs)
731 int err, pnum, tries = 0, data_size = len, vol_id = vol->
vol_id;
738 if (lnum == used_ebs - 1)
744 vid_hdr = ubi_zalloc_vid_hdr(ubi,
GFP_NOFS);
748 err = leb_write_lock(ubi, vol_id, lnum);
750 ubi_free_vid_hdr(ubi, vid_hdr);
757 vid_hdr->
compat = ubi_get_compat(ubi, vol_id);
769 ubi_free_vid_hdr(ubi, vid_hdr);
770 leb_write_unlock(ubi, vol_id, lnum);
774 dbg_eba(
"write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
775 len, vol_id, lnum, pnum, used_ebs);
779 ubi_warn(
"failed to write VID header to LEB %d:%d, PEB %d",
784 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
786 ubi_warn(
"failed to write %d bytes of data to PEB %d",
796 leb_write_unlock(ubi, vol_id, lnum);
797 ubi_free_vid_hdr(ubi, vid_hdr);
808 leb_write_unlock(ubi, vol_id, lnum);
809 ubi_free_vid_hdr(ubi, vid_hdr);
816 leb_write_unlock(ubi, vol_id, lnum);
817 ubi_free_vid_hdr(ubi, vid_hdr);
844 int lnum,
const void *buf,
int len)
846 int err, pnum, tries = 0, vol_id = vol->
vol_id;
864 vid_hdr = ubi_zalloc_vid_hdr(ubi,
GFP_NOFS);
869 err = leb_write_lock(ubi, vol_id, lnum);
876 vid_hdr->
compat = ubi_get_compat(ubi, vol_id);
892 dbg_eba(
"change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
893 vol_id, lnum, vol->
eba_tbl[lnum], pnum);
897 ubi_warn(
"failed to write VID header to LEB %d:%d, PEB %d",
902 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
904 ubi_warn(
"failed to write %d bytes of data to PEB %d",
920 leb_write_unlock(ubi, vol_id, lnum);
923 ubi_free_vid_hdr(ubi, vid_hdr);
967 static int is_error_sane(
int err)
999 dbg_wl(
"copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
1005 data_size = aldata_size =
1008 idx = vol_id2idx(ubi, vol_id);
1020 dbg_wl(
"volume %d is being removed, cancel", vol_id);
1039 err = leb_write_trylock(ubi, vol_id, lnum);
1041 dbg_wl(
"contention on LEB %d:%d, cancel", vol_id, lnum);
1050 if (vol->
eba_tbl[lnum] != from) {
1051 dbg_wl(
"LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
1052 vol_id, lnum, from, vol->
eba_tbl[lnum]);
1054 goto out_unlock_leb;
1064 dbg_wl(
"read %d bytes of data", aldata_size);
1065 err = ubi_io_read_data(ubi, ubi->
peb_buf, from, 0, aldata_size);
1067 ubi_warn(
"error %d while reading data from PEB %d",
1070 goto out_unlock_buf;
1084 aldata_size = data_size =
1097 if (data_size > 0) {
1108 goto out_unlock_buf;
1117 ubi_warn(
"error %d while reading VID header back from PEB %d",
1119 if (is_error_sane(err))
1123 goto out_unlock_buf;
1126 if (data_size > 0) {
1127 err = ubi_io_write_data(ubi, ubi->
peb_buf, to, 0, aldata_size);
1131 goto out_unlock_buf;
1141 err = ubi_io_read_data(ubi, ubi->
peb_buf, to, 0, aldata_size);
1144 ubi_warn(
"error %d while reading data back from PEB %d",
1146 if (is_error_sane(err))
1150 goto out_unlock_buf;
1156 ubi_warn(
"read data back from PEB %d and it is different",
1159 goto out_unlock_buf;
1171 leb_write_unlock(ubi, vol_id, lnum);
1193 static void print_rsvd_warning(
struct ubi_device *ubi,
1209 ubi_warn(
"cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
1212 ubi_warn(
"%d PEBs are corrupted and not used",
1229 int i,
j, num_volumes,
ret = 0;
1230 int **scan_eba, **fm_eba;
1248 for (i = 0; i < num_volumes; i++) {
1285 if (scan_eba[i][j] != fm_eba[i][j]) {
1290 ubi_err(
"LEB:%i:%i is PEB:%i instead of %i!",
1291 vol->
vol_id, i, fm_eba[i][j],
1299 for (i = 0; i < num_volumes; i++) {
1322 int i,
j,
err, num_volumes;
1328 dbg_eba(
"initialize EBA sub-system");
1337 for (i = 0; i < num_volumes; i++) {
1364 ubi_move_aeb_to_list(av, aeb, &ai->
erase);
1370 ubi_err(
"no enough physical eraseblocks (%d, need %d)",
1373 ubi_err(
"%d PEBs are corrupted and not used",
1387 print_rsvd_warning(ubi, ai);
1395 dbg_eba(
"EBA sub-system is initialized");
1399 for (i = 0; i < num_volumes; i++) {