1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#ifndef __UBOOT__
32#include <linux/slab.h>
33#include <linux/crc32.h>
34#else
35#include <ubi_uboot.h>
36#endif
37
38#include <linux/err.h>
39#include "ubi.h"
40
41
42#define EBA_RESERVED_PEBS 1
43
44
45
46
47
48
49
50
51
52unsigned long long ubi_next_sqnum(struct ubi_device *ubi)
53{
54 unsigned long long sqnum;
55
56 spin_lock(&ubi->ltree_lock);
57 sqnum = ubi->global_sqnum++;
58 spin_unlock(&ubi->ltree_lock);
59
60 return sqnum;
61}
62
63
64
65
66
67
68
69
70
71static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
72{
73 if (vol_id == UBI_LAYOUT_VOLUME_ID)
74 return UBI_LAYOUT_VOLUME_COMPAT;
75 return 0;
76}
77
78
79
80
81
82
83
84
85
86
87
88static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
89 int lnum)
90{
91 struct rb_node *p;
92
93 p = ubi->ltree.rb_node;
94 while (p) {
95 struct ubi_ltree_entry *le;
96
97 le = rb_entry(p, struct ubi_ltree_entry, rb);
98
99 if (vol_id < le->vol_id)
100 p = p->rb_left;
101 else if (vol_id > le->vol_id)
102 p = p->rb_right;
103 else {
104 if (lnum < le->lnum)
105 p = p->rb_left;
106 else if (lnum > le->lnum)
107 p = p->rb_right;
108 else
109 return le;
110 }
111 }
112
113 return NULL;
114}
115
116
117
118
119
120
121
122
123
124
125
126
127static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
128 int vol_id, int lnum)
129{
130 struct ubi_ltree_entry *le, *le1, *le_free;
131
132 le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
133 if (!le)
134 return ERR_PTR(-ENOMEM);
135
136 le->users = 0;
137 init_rwsem(&le->mutex);
138 le->vol_id = vol_id;
139 le->lnum = lnum;
140
141 spin_lock(&ubi->ltree_lock);
142 le1 = ltree_lookup(ubi, vol_id, lnum);
143
144 if (le1) {
145
146
147
148
149 le_free = le;
150 le = le1;
151 } else {
152 struct rb_node **p, *parent = NULL;
153
154
155
156
157
158 le_free = NULL;
159
160 p = &ubi->ltree.rb_node;
161 while (*p) {
162 parent = *p;
163 le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
164
165 if (vol_id < le1->vol_id)
166 p = &(*p)->rb_left;
167 else if (vol_id > le1->vol_id)
168 p = &(*p)->rb_right;
169 else {
170 ubi_assert(lnum != le1->lnum);
171 if (lnum < le1->lnum)
172 p = &(*p)->rb_left;
173 else
174 p = &(*p)->rb_right;
175 }
176 }
177
178 rb_link_node(&le->rb, parent, p);
179 rb_insert_color(&le->rb, &ubi->ltree);
180 }
181 le->users += 1;
182 spin_unlock(&ubi->ltree_lock);
183
184 kfree(le_free);
185 return le;
186}
187
188
189
190
191
192
193
194
195
196
197static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
198{
199 struct ubi_ltree_entry *le;
200
201 le = ltree_add_entry(ubi, vol_id, lnum);
202 if (IS_ERR(le))
203 return PTR_ERR(le);
204 down_read(&le->mutex);
205 return 0;
206}
207
208
209
210
211
212
213
214static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
215{
216 struct ubi_ltree_entry *le;
217
218 spin_lock(&ubi->ltree_lock);
219 le = ltree_lookup(ubi, vol_id, lnum);
220 le->users -= 1;
221 ubi_assert(le->users >= 0);
222 up_read(&le->mutex);
223 if (le->users == 0) {
224 rb_erase(&le->rb, &ubi->ltree);
225 kfree(le);
226 }
227 spin_unlock(&ubi->ltree_lock);
228}
229
230
231
232
233
234
235
236
237
238
239static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
240{
241 struct ubi_ltree_entry *le;
242
243 le = ltree_add_entry(ubi, vol_id, lnum);
244 if (IS_ERR(le))
245 return PTR_ERR(le);
246 down_write(&le->mutex);
247 return 0;
248}
249
250
251
252
253
254
255
256
257
258
259
260
261static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
262{
263 struct ubi_ltree_entry *le;
264
265 le = ltree_add_entry(ubi, vol_id, lnum);
266 if (IS_ERR(le))
267 return PTR_ERR(le);
268 if (down_write_trylock(&le->mutex))
269 return 0;
270
271
272 spin_lock(&ubi->ltree_lock);
273 le->users -= 1;
274 ubi_assert(le->users >= 0);
275 if (le->users == 0) {
276 rb_erase(&le->rb, &ubi->ltree);
277 kfree(le);
278 }
279 spin_unlock(&ubi->ltree_lock);
280
281 return 1;
282}
283
284
285
286
287
288
289
290static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
291{
292 struct ubi_ltree_entry *le;
293
294 spin_lock(&ubi->ltree_lock);
295 le = ltree_lookup(ubi, vol_id, lnum);
296 le->users -= 1;
297 ubi_assert(le->users >= 0);
298 up_write(&le->mutex);
299 if (le->users == 0) {
300 rb_erase(&le->rb, &ubi->ltree);
301 kfree(le);
302 }
303 spin_unlock(&ubi->ltree_lock);
304}
305
306
307
308
309
310
311
312
313
314
315
316int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
317 int lnum)
318{
319 int err, pnum, vol_id = vol->vol_id;
320
321 if (ubi->ro_mode)
322 return -EROFS;
323
324 err = leb_write_lock(ubi, vol_id, lnum);
325 if (err)
326 return err;
327
328 pnum = vol->eba_tbl[lnum];
329 if (pnum < 0)
330
331 goto out_unlock;
332
333 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
334
335 down_read(&ubi->fm_eba_sem);
336 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
337 up_read(&ubi->fm_eba_sem);
338 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
339
340out_unlock:
341 leb_write_unlock(ubi, vol_id, lnum);
342 return err;
343}
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
365 void *buf, int offset, int len, int check)
366{
367 int err, pnum, scrub = 0, vol_id = vol->vol_id;
368 struct ubi_vid_hdr *vid_hdr;
369 uint32_t uninitialized_var(crc);
370
371 err = leb_read_lock(ubi, vol_id, lnum);
372 if (err)
373 return err;
374
375 pnum = vol->eba_tbl[lnum];
376 if (pnum < 0) {
377
378
379
380
381
382 dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
383 len, offset, vol_id, lnum);
384 leb_read_unlock(ubi, vol_id, lnum);
385 ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
386 memset(buf, 0xFF, len);
387 return 0;
388 }
389
390 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
391 len, offset, vol_id, lnum, pnum);
392
393 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
394 check = 0;
395
396retry:
397 if (check) {
398 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
399 if (!vid_hdr) {
400 err = -ENOMEM;
401 goto out_unlock;
402 }
403
404 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
405 if (err && err != UBI_IO_BITFLIPS) {
406 if (err > 0) {
407
408
409
410
411
412
413
414
415 if (err == UBI_IO_BAD_HDR_EBADMSG ||
416 err == UBI_IO_BAD_HDR) {
417 ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d",
418 pnum, vol_id, lnum);
419 err = -EBADMSG;
420 } else {
421 err = -EINVAL;
422 ubi_ro_mode(ubi);
423 }
424 }
425 goto out_free;
426 } else if (err == UBI_IO_BITFLIPS)
427 scrub = 1;
428
429 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
430 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
431
432 crc = be32_to_cpu(vid_hdr->data_crc);
433 ubi_free_vid_hdr(ubi, vid_hdr);
434 }
435
436 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
437 if (err) {
438 if (err == UBI_IO_BITFLIPS)
439 scrub = 1;
440 else if (mtd_is_eccerr(err)) {
441 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
442 goto out_unlock;
443 scrub = 1;
444 if (!check) {
445 ubi_msg(ubi, "force data checking");
446 check = 1;
447 goto retry;
448 }
449 } else
450 goto out_unlock;
451 }
452
453 if (check) {
454 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
455 if (crc1 != crc) {
456 ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x",
457 crc1, crc);
458 err = -EBADMSG;
459 goto out_unlock;
460 }
461 }
462
463 if (scrub)
464 err = ubi_wl_scrub_peb(ubi, pnum);
465
466 leb_read_unlock(ubi, vol_id, lnum);
467 return err;
468
469out_free:
470 ubi_free_vid_hdr(ubi, vid_hdr);
471out_unlock:
472 leb_read_unlock(ubi, vol_id, lnum);
473 return err;
474}
475
476#ifndef __UBOOT__
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
492 struct ubi_sgl *sgl, int lnum, int offset, int len,
493 int check)
494{
495 int to_read;
496 int ret;
497 struct scatterlist *sg;
498
499 for (;;) {
500 ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT);
501 sg = &sgl->sg[sgl->list_pos];
502 if (len < sg->length - sgl->page_pos)
503 to_read = len;
504 else
505 to_read = sg->length - sgl->page_pos;
506
507 ret = ubi_eba_read_leb(ubi, vol, lnum,
508 sg_virt(sg) + sgl->page_pos, offset,
509 to_read, check);
510 if (ret < 0)
511 return ret;
512
513 offset += to_read;
514 len -= to_read;
515 if (!len) {
516 sgl->page_pos += to_read;
517 if (sgl->page_pos == sg->length) {
518 sgl->list_pos++;
519 sgl->page_pos = 0;
520 }
521
522 break;
523 }
524
525 sgl->list_pos++;
526 sgl->page_pos = 0;
527 }
528
529 return ret;
530}
531#endif
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
550 const void *buf, int offset, int len)
551{
552 int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
553 struct ubi_volume *vol = ubi->volumes[idx];
554 struct ubi_vid_hdr *vid_hdr;
555
556 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
557 if (!vid_hdr)
558 return -ENOMEM;
559
560retry:
561 new_pnum = ubi_wl_get_peb(ubi);
562 if (new_pnum < 0) {
563 ubi_free_vid_hdr(ubi, vid_hdr);
564 up_read(&ubi->fm_eba_sem);
565 return new_pnum;
566 }
567
568 ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
569 pnum, new_pnum);
570
571 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
572 if (err && err != UBI_IO_BITFLIPS) {
573 if (err > 0)
574 err = -EIO;
575 up_read(&ubi->fm_eba_sem);
576 goto out_put;
577 }
578
579 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
580 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
581 if (err) {
582 up_read(&ubi->fm_eba_sem);
583 goto write_error;
584 }
585
586 data_size = offset + len;
587 mutex_lock(&ubi->buf_mutex);
588 memset(ubi->peb_buf + offset, 0xFF, len);
589
590
591 if (offset > 0) {
592 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
593 if (err && err != UBI_IO_BITFLIPS) {
594 up_read(&ubi->fm_eba_sem);
595 goto out_unlock;
596 }
597 }
598
599 memcpy(ubi->peb_buf + offset, buf, len);
600
601 err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
602 if (err) {
603 mutex_unlock(&ubi->buf_mutex);
604 up_read(&ubi->fm_eba_sem);
605 goto write_error;
606 }
607
608 mutex_unlock(&ubi->buf_mutex);
609 ubi_free_vid_hdr(ubi, vid_hdr);
610
611 vol->eba_tbl[lnum] = new_pnum;
612 up_read(&ubi->fm_eba_sem);
613 ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
614
615 ubi_msg(ubi, "data was successfully recovered");
616 return 0;
617
618out_unlock:
619 mutex_unlock(&ubi->buf_mutex);
620out_put:
621 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
622 ubi_free_vid_hdr(ubi, vid_hdr);
623 return err;
624
625write_error:
626
627
628
629
630 ubi_warn(ubi, "failed to write to PEB %d", new_pnum);
631 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
632 if (++tries > UBI_IO_RETRIES) {
633 ubi_free_vid_hdr(ubi, vid_hdr);
634 return err;
635 }
636 ubi_msg(ubi, "try again");
637 goto retry;
638}
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
655 const void *buf, int offset, int len)
656{
657 int err, pnum, tries = 0, vol_id = vol->vol_id;
658 struct ubi_vid_hdr *vid_hdr;
659
660 if (ubi->ro_mode)
661 return -EROFS;
662
663 err = leb_write_lock(ubi, vol_id, lnum);
664 if (err)
665 return err;
666
667 pnum = vol->eba_tbl[lnum];
668 if (pnum >= 0) {
669 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
670 len, offset, vol_id, lnum, pnum);
671
672 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
673 if (err) {
674 ubi_warn(ubi, "failed to write data to PEB %d", pnum);
675 if (err == -EIO && ubi->bad_allowed)
676 err = recover_peb(ubi, pnum, vol_id, lnum, buf,
677 offset, len);
678 if (err)
679 ubi_ro_mode(ubi);
680 }
681 leb_write_unlock(ubi, vol_id, lnum);
682 return err;
683 }
684
685
686
687
688
689 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
690 if (!vid_hdr) {
691 leb_write_unlock(ubi, vol_id, lnum);
692 return -ENOMEM;
693 }
694
695 vid_hdr->vol_type = UBI_VID_DYNAMIC;
696 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
697 vid_hdr->vol_id = cpu_to_be32(vol_id);
698 vid_hdr->lnum = cpu_to_be32(lnum);
699 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
700 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
701
702retry:
703 pnum = ubi_wl_get_peb(ubi);
704 if (pnum < 0) {
705 ubi_free_vid_hdr(ubi, vid_hdr);
706 leb_write_unlock(ubi, vol_id, lnum);
707 up_read(&ubi->fm_eba_sem);
708 return pnum;
709 }
710
711 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
712 len, offset, vol_id, lnum, pnum);
713
714 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
715 if (err) {
716 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
717 vol_id, lnum, pnum);
718 up_read(&ubi->fm_eba_sem);
719 goto write_error;
720 }
721
722 if (len) {
723 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
724 if (err) {
725 ubi_warn(ubi, "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
726 len, offset, vol_id, lnum, pnum);
727 up_read(&ubi->fm_eba_sem);
728 goto write_error;
729 }
730 }
731
732 vol->eba_tbl[lnum] = pnum;
733 up_read(&ubi->fm_eba_sem);
734
735 leb_write_unlock(ubi, vol_id, lnum);
736 ubi_free_vid_hdr(ubi, vid_hdr);
737 return 0;
738
739write_error:
740 if (err != -EIO || !ubi->bad_allowed) {
741 ubi_ro_mode(ubi);
742 leb_write_unlock(ubi, vol_id, lnum);
743 ubi_free_vid_hdr(ubi, vid_hdr);
744 return err;
745 }
746
747
748
749
750
751
752 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
753 if (err || ++tries > UBI_IO_RETRIES) {
754 ubi_ro_mode(ubi);
755 leb_write_unlock(ubi, vol_id, lnum);
756 ubi_free_vid_hdr(ubi, vid_hdr);
757 return err;
758 }
759
760 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
761 ubi_msg(ubi, "try another PEB");
762 goto retry;
763}
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
788 int lnum, const void *buf, int len, int used_ebs)
789{
790 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
791 struct ubi_vid_hdr *vid_hdr;
792 uint32_t crc;
793
794 if (ubi->ro_mode)
795 return -EROFS;
796
797 if (lnum == used_ebs - 1)
798
799 len = ALIGN(data_size, ubi->min_io_size);
800 else
801 ubi_assert(!(len & (ubi->min_io_size - 1)));
802
803 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
804 if (!vid_hdr)
805 return -ENOMEM;
806
807 err = leb_write_lock(ubi, vol_id, lnum);
808 if (err) {
809 ubi_free_vid_hdr(ubi, vid_hdr);
810 return err;
811 }
812
813 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
814 vid_hdr->vol_id = cpu_to_be32(vol_id);
815 vid_hdr->lnum = cpu_to_be32(lnum);
816 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
817 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
818
819 crc = crc32(UBI_CRC32_INIT, buf, data_size);
820 vid_hdr->vol_type = UBI_VID_STATIC;
821 vid_hdr->data_size = cpu_to_be32(data_size);
822 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
823 vid_hdr->data_crc = cpu_to_be32(crc);
824
825retry:
826 pnum = ubi_wl_get_peb(ubi);
827 if (pnum < 0) {
828 ubi_free_vid_hdr(ubi, vid_hdr);
829 leb_write_unlock(ubi, vol_id, lnum);
830 up_read(&ubi->fm_eba_sem);
831 return pnum;
832 }
833
834 dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
835 len, vol_id, lnum, pnum, used_ebs);
836
837 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
838 if (err) {
839 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
840 vol_id, lnum, pnum);
841 up_read(&ubi->fm_eba_sem);
842 goto write_error;
843 }
844
845 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
846 if (err) {
847 ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
848 len, pnum);
849 up_read(&ubi->fm_eba_sem);
850 goto write_error;
851 }
852
853 ubi_assert(vol->eba_tbl[lnum] < 0);
854 vol->eba_tbl[lnum] = pnum;
855 up_read(&ubi->fm_eba_sem);
856
857 leb_write_unlock(ubi, vol_id, lnum);
858 ubi_free_vid_hdr(ubi, vid_hdr);
859 return 0;
860
861write_error:
862 if (err != -EIO || !ubi->bad_allowed) {
863
864
865
866
867
868 ubi_ro_mode(ubi);
869 leb_write_unlock(ubi, vol_id, lnum);
870 ubi_free_vid_hdr(ubi, vid_hdr);
871 return err;
872 }
873
874 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
875 if (err || ++tries > UBI_IO_RETRIES) {
876 ubi_ro_mode(ubi);
877 leb_write_unlock(ubi, vol_id, lnum);
878 ubi_free_vid_hdr(ubi, vid_hdr);
879 return err;
880 }
881
882 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
883 ubi_msg(ubi, "try another PEB");
884 goto retry;
885}
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
905 int lnum, const void *buf, int len)
906{
907 int err, pnum, old_pnum, tries = 0, vol_id = vol->vol_id;
908 struct ubi_vid_hdr *vid_hdr;
909 uint32_t crc;
910
911 if (ubi->ro_mode)
912 return -EROFS;
913
914 if (len == 0) {
915
916
917
918
919 err = ubi_eba_unmap_leb(ubi, vol, lnum);
920 if (err)
921 return err;
922 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
923 }
924
925 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
926 if (!vid_hdr)
927 return -ENOMEM;
928
929 mutex_lock(&ubi->alc_mutex);
930 err = leb_write_lock(ubi, vol_id, lnum);
931 if (err)
932 goto out_mutex;
933
934 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
935 vid_hdr->vol_id = cpu_to_be32(vol_id);
936 vid_hdr->lnum = cpu_to_be32(lnum);
937 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
938 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
939
940 crc = crc32(UBI_CRC32_INIT, buf, len);
941 vid_hdr->vol_type = UBI_VID_DYNAMIC;
942 vid_hdr->data_size = cpu_to_be32(len);
943 vid_hdr->copy_flag = 1;
944 vid_hdr->data_crc = cpu_to_be32(crc);
945
946retry:
947 pnum = ubi_wl_get_peb(ubi);
948 if (pnum < 0) {
949 err = pnum;
950 up_read(&ubi->fm_eba_sem);
951 goto out_leb_unlock;
952 }
953
954 dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
955 vol_id, lnum, vol->eba_tbl[lnum], pnum);
956
957 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
958 if (err) {
959 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
960 vol_id, lnum, pnum);
961 up_read(&ubi->fm_eba_sem);
962 goto write_error;
963 }
964
965 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
966 if (err) {
967 ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
968 len, pnum);
969 up_read(&ubi->fm_eba_sem);
970 goto write_error;
971 }
972
973 old_pnum = vol->eba_tbl[lnum];
974 vol->eba_tbl[lnum] = pnum;
975 up_read(&ubi->fm_eba_sem);
976
977 if (old_pnum >= 0) {
978 err = ubi_wl_put_peb(ubi, vol_id, lnum, old_pnum, 0);
979 if (err)
980 goto out_leb_unlock;
981 }
982
983out_leb_unlock:
984 leb_write_unlock(ubi, vol_id, lnum);
985out_mutex:
986 mutex_unlock(&ubi->alc_mutex);
987 ubi_free_vid_hdr(ubi, vid_hdr);
988 return err;
989
990write_error:
991 if (err != -EIO || !ubi->bad_allowed) {
992
993
994
995
996
997 ubi_ro_mode(ubi);
998 goto out_leb_unlock;
999 }
1000
1001 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
1002 if (err || ++tries > UBI_IO_RETRIES) {
1003 ubi_ro_mode(ubi);
1004 goto out_leb_unlock;
1005 }
1006
1007 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1008 ubi_msg(ubi, "try another PEB");
1009 goto retry;
1010}
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031static int is_error_sane(int err)
1032{
1033 if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
1034 err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
1035 return 0;
1036 return 1;
1037}
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1054 struct ubi_vid_hdr *vid_hdr)
1055{
1056 int err, vol_id, lnum, data_size, aldata_size, idx;
1057 struct ubi_volume *vol;
1058 uint32_t crc;
1059
1060 vol_id = be32_to_cpu(vid_hdr->vol_id);
1061 lnum = be32_to_cpu(vid_hdr->lnum);
1062
1063 dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
1064
1065 if (vid_hdr->vol_type == UBI_VID_STATIC) {
1066 data_size = be32_to_cpu(vid_hdr->data_size);
1067 aldata_size = ALIGN(data_size, ubi->min_io_size);
1068 } else
1069 data_size = aldata_size =
1070 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
1071
1072 idx = vol_id2idx(ubi, vol_id);
1073 spin_lock(&ubi->volumes_lock);
1074
1075
1076
1077
1078
1079
1080 vol = ubi->volumes[idx];
1081 spin_unlock(&ubi->volumes_lock);
1082 if (!vol) {
1083
1084 dbg_wl("volume %d is being removed, cancel", vol_id);
1085 return MOVE_CANCEL_RACE;
1086 }
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103 err = leb_write_trylock(ubi, vol_id, lnum);
1104 if (err) {
1105 dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
1106 return MOVE_RETRY;
1107 }
1108
1109
1110
1111
1112
1113
1114 if (vol->eba_tbl[lnum] != from) {
1115 dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
1116 vol_id, lnum, from, vol->eba_tbl[lnum]);
1117 err = MOVE_CANCEL_RACE;
1118 goto out_unlock_leb;
1119 }
1120
1121
1122
1123
1124
1125
1126
1127 mutex_lock(&ubi->buf_mutex);
1128 dbg_wl("read %d bytes of data", aldata_size);
1129 err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
1130 if (err && err != UBI_IO_BITFLIPS) {
1131 ubi_warn(ubi, "error %d while reading data from PEB %d",
1132 err, from);
1133 err = MOVE_SOURCE_RD_ERR;
1134 goto out_unlock_buf;
1135 }
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147 if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1148 aldata_size = data_size =
1149 ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
1150
1151 cond_resched();
1152 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
1153 cond_resched();
1154
1155
1156
1157
1158
1159
1160
1161 if (data_size > 0) {
1162 vid_hdr->copy_flag = 1;
1163 vid_hdr->data_size = cpu_to_be32(data_size);
1164 vid_hdr->data_crc = cpu_to_be32(crc);
1165 }
1166 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1167
1168 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1169 if (err) {
1170 if (err == -EIO)
1171 err = MOVE_TARGET_WR_ERR;
1172 goto out_unlock_buf;
1173 }
1174
1175 cond_resched();
1176
1177
1178 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1179 if (err) {
1180 if (err != UBI_IO_BITFLIPS) {
1181 ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
1182 err, to);
1183 if (is_error_sane(err))
1184 err = MOVE_TARGET_RD_ERR;
1185 } else
1186 err = MOVE_TARGET_BITFLIPS;
1187 goto out_unlock_buf;
1188 }
1189
1190 if (data_size > 0) {
1191 err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1192 if (err) {
1193 if (err == -EIO)
1194 err = MOVE_TARGET_WR_ERR;
1195 goto out_unlock_buf;
1196 }
1197
1198 cond_resched();
1199
1200
1201
1202
1203
1204 memset(ubi->peb_buf, 0xFF, aldata_size);
1205 err = ubi_io_read_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1206 if (err) {
1207 if (err != UBI_IO_BITFLIPS) {
1208 ubi_warn(ubi, "error %d while reading data back from PEB %d",
1209 err, to);
1210 if (is_error_sane(err))
1211 err = MOVE_TARGET_RD_ERR;
1212 } else
1213 err = MOVE_TARGET_BITFLIPS;
1214 goto out_unlock_buf;
1215 }
1216
1217 cond_resched();
1218
1219 if (crc != crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size)) {
1220 ubi_warn(ubi, "read data back from PEB %d and it is different",
1221 to);
1222 err = -EINVAL;
1223 goto out_unlock_buf;
1224 }
1225 }
1226
1227 ubi_assert(vol->eba_tbl[lnum] == from);
1228 down_read(&ubi->fm_eba_sem);
1229 vol->eba_tbl[lnum] = to;
1230 up_read(&ubi->fm_eba_sem);
1231
1232out_unlock_buf:
1233 mutex_unlock(&ubi->buf_mutex);
1234out_unlock_leb:
1235 leb_write_unlock(ubi, vol_id, lnum);
1236 return err;
1237}
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257static void print_rsvd_warning(struct ubi_device *ubi,
1258 struct ubi_attach_info *ai)
1259{
1260
1261
1262
1263
1264 if (ai->max_sqnum > (1 << 18)) {
1265 int min = ubi->beb_rsvd_level / 10;
1266
1267 if (!min)
1268 min = 1;
1269 if (ubi->beb_rsvd_pebs > min)
1270 return;
1271 }
1272
1273 ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
1274 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1275 if (ubi->corr_peb_count)
1276 ubi_warn(ubi, "%d PEBs are corrupted and not used",
1277 ubi->corr_peb_count);
1278}
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
1291 struct ubi_attach_info *ai_scan)
1292{
1293 int i, j, num_volumes, ret = 0;
1294 int **scan_eba, **fm_eba;
1295 struct ubi_ainf_volume *av;
1296 struct ubi_volume *vol;
1297 struct ubi_ainf_peb *aeb;
1298 struct rb_node *rb;
1299
1300 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1301
1302 scan_eba = kmalloc(sizeof(*scan_eba) * num_volumes, GFP_KERNEL);
1303 if (!scan_eba)
1304 return -ENOMEM;
1305
1306 fm_eba = kmalloc(sizeof(*fm_eba) * num_volumes, GFP_KERNEL);
1307 if (!fm_eba) {
1308 kfree(scan_eba);
1309 return -ENOMEM;
1310 }
1311
1312 for (i = 0; i < num_volumes; i++) {
1313 vol = ubi->volumes[i];
1314 if (!vol)
1315 continue;
1316
1317 scan_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**scan_eba),
1318 GFP_KERNEL);
1319 if (!scan_eba[i]) {
1320 ret = -ENOMEM;
1321 goto out_free;
1322 }
1323
1324 fm_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**fm_eba),
1325 GFP_KERNEL);
1326 if (!fm_eba[i]) {
1327 ret = -ENOMEM;
1328 goto out_free;
1329 }
1330
1331 for (j = 0; j < vol->reserved_pebs; j++)
1332 scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED;
1333
1334 av = ubi_find_av(ai_scan, idx2vol_id(ubi, i));
1335 if (!av)
1336 continue;
1337
1338 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
1339 scan_eba[i][aeb->lnum] = aeb->pnum;
1340
1341 av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i));
1342 if (!av)
1343 continue;
1344
1345 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
1346 fm_eba[i][aeb->lnum] = aeb->pnum;
1347
1348 for (j = 0; j < vol->reserved_pebs; j++) {
1349 if (scan_eba[i][j] != fm_eba[i][j]) {
1350 if (scan_eba[i][j] == UBI_LEB_UNMAPPED ||
1351 fm_eba[i][j] == UBI_LEB_UNMAPPED)
1352 continue;
1353
1354 ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!",
1355 vol->vol_id, i, fm_eba[i][j],
1356 scan_eba[i][j]);
1357 ubi_assert(0);
1358 }
1359 }
1360 }
1361
1362out_free:
1363 for (i = 0; i < num_volumes; i++) {
1364 if (!ubi->volumes[i])
1365 continue;
1366
1367 kfree(scan_eba[i]);
1368 kfree(fm_eba[i]);
1369 }
1370
1371 kfree(scan_eba);
1372 kfree(fm_eba);
1373 return ret;
1374}
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
1385{
1386 int i, j, err, num_volumes;
1387 struct ubi_ainf_volume *av;
1388 struct ubi_volume *vol;
1389 struct ubi_ainf_peb *aeb;
1390 struct rb_node *rb;
1391
1392 dbg_eba("initialize EBA sub-system");
1393
1394 spin_lock_init(&ubi->ltree_lock);
1395 mutex_init(&ubi->alc_mutex);
1396 ubi->ltree = RB_ROOT;
1397
1398 ubi->global_sqnum = ai->max_sqnum + 1;
1399 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1400
1401 for (i = 0; i < num_volumes; i++) {
1402 vol = ubi->volumes[i];
1403 if (!vol)
1404 continue;
1405
1406 cond_resched();
1407
1408 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1409 GFP_KERNEL);
1410 if (!vol->eba_tbl) {
1411 err = -ENOMEM;
1412 goto out_free;
1413 }
1414
1415 for (j = 0; j < vol->reserved_pebs; j++)
1416 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1417
1418 av = ubi_find_av(ai, idx2vol_id(ubi, i));
1419 if (!av)
1420 continue;
1421
1422 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
1423 if (aeb->lnum >= vol->reserved_pebs)
1424
1425
1426
1427
1428 ubi_move_aeb_to_list(av, aeb, &ai->erase);
1429 else
1430 vol->eba_tbl[aeb->lnum] = aeb->pnum;
1431 }
1432 }
1433
1434 if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1435 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
1436 ubi->avail_pebs, EBA_RESERVED_PEBS);
1437 if (ubi->corr_peb_count)
1438 ubi_err(ubi, "%d PEBs are corrupted and not used",
1439 ubi->corr_peb_count);
1440 err = -ENOSPC;
1441 goto out_free;
1442 }
1443 ubi->avail_pebs -= EBA_RESERVED_PEBS;
1444 ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1445
1446 if (ubi->bad_allowed) {
1447 ubi_calculate_reserved(ubi);
1448
1449 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1450
1451 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1452 print_rsvd_warning(ubi, ai);
1453 } else
1454 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1455
1456 ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1457 ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
1458 }
1459
1460 dbg_eba("EBA sub-system is initialized");
1461 return 0;
1462
1463out_free:
1464 for (i = 0; i < num_volumes; i++) {
1465 if (!ubi->volumes[i])
1466 continue;
1467 kfree(ubi->volumes[i]->eba_tbl);
1468 ubi->volumes[i]->eba_tbl = NULL;
1469 }
1470 return err;
1471}
1472