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