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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78#include <linux/slab.h>
79#include <linux/crc32.h>
80#include <linux/freezer.h>
81#include <linux/kthread.h>
82#include "ubi.h"
83
84
85#define WL_RESERVED_PEBS 1
86
87
88
89
90
91#define ST_PROTECTION 16
92#define U_PROTECTION 10
93#define LT_PROTECTION 4
94
95
96
97
98
99
100
101#define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
102
103
104
105
106
107
108
109
110
111
112
113
114#define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
115
116
117
118
119
120#define WL_MAX_FAILURES 32
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169struct ubi_wl_prot_entry {
170 struct rb_node rb_pnum;
171 struct rb_node rb_aec;
172 unsigned long long abs_ec;
173 struct ubi_wl_entry *e;
174};
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189struct ubi_work {
190 struct list_head list;
191 int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
192
193 struct ubi_wl_entry *e;
194 int torture;
195};
196
197#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
198static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec);
199static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
200 struct rb_root *root);
201#else
202#define paranoid_check_ec(ubi, pnum, ec) 0
203#define paranoid_check_in_wl_tree(e, root)
204#endif
205
206
207
208
209
210
211
212
213
214static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
215{
216 struct rb_node **p, *parent = NULL;
217
218 p = &root->rb_node;
219 while (*p) {
220 struct ubi_wl_entry *e1;
221
222 parent = *p;
223 e1 = rb_entry(parent, struct ubi_wl_entry, rb);
224
225 if (e->ec < e1->ec)
226 p = &(*p)->rb_left;
227 else if (e->ec > e1->ec)
228 p = &(*p)->rb_right;
229 else {
230 ubi_assert(e->pnum != e1->pnum);
231 if (e->pnum < e1->pnum)
232 p = &(*p)->rb_left;
233 else
234 p = &(*p)->rb_right;
235 }
236 }
237
238 rb_link_node(&e->rb, parent, p);
239 rb_insert_color(&e->rb, root);
240}
241
242
243
244
245
246
247
248
249static int do_work(struct ubi_device *ubi)
250{
251 int err;
252 struct ubi_work *wrk;
253
254 cond_resched();
255
256
257
258
259
260
261
262 down_read(&ubi->work_sem);
263 spin_lock(&ubi->wl_lock);
264 if (list_empty(&ubi->works)) {
265 spin_unlock(&ubi->wl_lock);
266 up_read(&ubi->work_sem);
267 return 0;
268 }
269
270 wrk = list_entry(ubi->works.next, struct ubi_work, list);
271 list_del(&wrk->list);
272 ubi->works_count -= 1;
273 ubi_assert(ubi->works_count >= 0);
274 spin_unlock(&ubi->wl_lock);
275
276
277
278
279
280
281 err = wrk->func(ubi, wrk, 0);
282 if (err)
283 ubi_err("work failed with error code %d", err);
284 up_read(&ubi->work_sem);
285
286 return err;
287}
288
289
290
291
292
293
294
295
296
297
298static int produce_free_peb(struct ubi_device *ubi)
299{
300 int err;
301
302 spin_lock(&ubi->wl_lock);
303 while (!ubi->free.rb_node) {
304 spin_unlock(&ubi->wl_lock);
305
306 dbg_wl("do one work synchronously");
307 err = do_work(ubi);
308 if (err)
309 return err;
310
311 spin_lock(&ubi->wl_lock);
312 }
313 spin_unlock(&ubi->wl_lock);
314
315 return 0;
316}
317
318
319
320
321
322
323
324
325
326static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
327{
328 struct rb_node *p;
329
330 p = root->rb_node;
331 while (p) {
332 struct ubi_wl_entry *e1;
333
334 e1 = rb_entry(p, struct ubi_wl_entry, rb);
335
336 if (e->pnum == e1->pnum) {
337 ubi_assert(e == e1);
338 return 1;
339 }
340
341 if (e->ec < e1->ec)
342 p = p->rb_left;
343 else if (e->ec > e1->ec)
344 p = p->rb_right;
345 else {
346 ubi_assert(e->pnum != e1->pnum);
347 if (e->pnum < e1->pnum)
348 p = p->rb_left;
349 else
350 p = p->rb_right;
351 }
352 }
353
354 return 0;
355}
356
357
358
359
360
361
362
363
364
365
366
367static void prot_tree_add(struct ubi_device *ubi, struct ubi_wl_entry *e,
368 struct ubi_wl_prot_entry *pe, int abs_ec)
369{
370 struct rb_node **p, *parent = NULL;
371 struct ubi_wl_prot_entry *pe1;
372
373 pe->e = e;
374 pe->abs_ec = ubi->abs_ec + abs_ec;
375
376 p = &ubi->prot.pnum.rb_node;
377 while (*p) {
378 parent = *p;
379 pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_pnum);
380
381 if (e->pnum < pe1->e->pnum)
382 p = &(*p)->rb_left;
383 else
384 p = &(*p)->rb_right;
385 }
386 rb_link_node(&pe->rb_pnum, parent, p);
387 rb_insert_color(&pe->rb_pnum, &ubi->prot.pnum);
388
389 p = &ubi->prot.aec.rb_node;
390 parent = NULL;
391 while (*p) {
392 parent = *p;
393 pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_aec);
394
395 if (pe->abs_ec < pe1->abs_ec)
396 p = &(*p)->rb_left;
397 else
398 p = &(*p)->rb_right;
399 }
400 rb_link_node(&pe->rb_aec, parent, p);
401 rb_insert_color(&pe->rb_aec, &ubi->prot.aec);
402}
403
404
405
406
407
408
409
410
411
412static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int max)
413{
414 struct rb_node *p;
415 struct ubi_wl_entry *e;
416
417 e = rb_entry(rb_first(root), struct ubi_wl_entry, rb);
418 max += e->ec;
419
420 p = root->rb_node;
421 while (p) {
422 struct ubi_wl_entry *e1;
423
424 e1 = rb_entry(p, struct ubi_wl_entry, rb);
425 if (e1->ec >= max)
426 p = p->rb_left;
427 else {
428 p = p->rb_right;
429 e = e1;
430 }
431 }
432
433 return e;
434}
435
436
437
438
439
440
441
442
443
444int ubi_wl_get_peb(struct ubi_device *ubi, int dtype)
445{
446 int err, protect, medium_ec;
447 struct ubi_wl_entry *e, *first, *last;
448 struct ubi_wl_prot_entry *pe;
449
450 ubi_assert(dtype == UBI_LONGTERM || dtype == UBI_SHORTTERM ||
451 dtype == UBI_UNKNOWN);
452
453 pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_NOFS);
454 if (!pe)
455 return -ENOMEM;
456
457retry:
458 spin_lock(&ubi->wl_lock);
459 if (!ubi->free.rb_node) {
460 if (ubi->works_count == 0) {
461 ubi_assert(list_empty(&ubi->works));
462 ubi_err("no free eraseblocks");
463 spin_unlock(&ubi->wl_lock);
464 kfree(pe);
465 return -ENOSPC;
466 }
467 spin_unlock(&ubi->wl_lock);
468
469 err = produce_free_peb(ubi);
470 if (err < 0) {
471 kfree(pe);
472 return err;
473 }
474 goto retry;
475 }
476
477 switch (dtype) {
478 case UBI_LONGTERM:
479
480
481
482
483
484
485 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
486 protect = LT_PROTECTION;
487 break;
488 case UBI_UNKNOWN:
489
490
491
492
493
494
495 first = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, rb);
496 last = rb_entry(rb_last(&ubi->free), struct ubi_wl_entry, rb);
497
498 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
499 e = rb_entry(ubi->free.rb_node,
500 struct ubi_wl_entry, rb);
501 else {
502 medium_ec = (first->ec + WL_FREE_MAX_DIFF)/2;
503 e = find_wl_entry(&ubi->free, medium_ec);
504 }
505 protect = U_PROTECTION;
506 break;
507 case UBI_SHORTTERM:
508
509
510
511
512 e = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, rb);
513 protect = ST_PROTECTION;
514 break;
515 default:
516 protect = 0;
517 e = NULL;
518 BUG();
519 }
520
521
522
523
524
525 paranoid_check_in_wl_tree(e, &ubi->free);
526 rb_erase(&e->rb, &ubi->free);
527 prot_tree_add(ubi, e, pe, protect);
528
529 dbg_wl("PEB %d EC %d, protection %d", e->pnum, e->ec, protect);
530 spin_unlock(&ubi->wl_lock);
531
532 return e->pnum;
533}
534
535
536
537
538
539
540
541
542
543
544static int prot_tree_del(struct ubi_device *ubi, int pnum)
545{
546 struct rb_node *p;
547 struct ubi_wl_prot_entry *pe = NULL;
548
549 p = ubi->prot.pnum.rb_node;
550 while (p) {
551
552 pe = rb_entry(p, struct ubi_wl_prot_entry, rb_pnum);
553
554 if (pnum == pe->e->pnum)
555 goto found;
556
557 if (pnum < pe->e->pnum)
558 p = p->rb_left;
559 else
560 p = p->rb_right;
561 }
562
563 return -ENODEV;
564
565found:
566 ubi_assert(pe->e->pnum == pnum);
567 rb_erase(&pe->rb_aec, &ubi->prot.aec);
568 rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
569 kfree(pe);
570 return 0;
571}
572
573
574
575
576
577
578
579
580
581
582static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
583 int torture)
584{
585 int err;
586 struct ubi_ec_hdr *ec_hdr;
587 unsigned long long ec = e->ec;
588
589 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
590
591 err = paranoid_check_ec(ubi, e->pnum, e->ec);
592 if (err > 0)
593 return -EINVAL;
594
595 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
596 if (!ec_hdr)
597 return -ENOMEM;
598
599 err = ubi_io_sync_erase(ubi, e->pnum, torture);
600 if (err < 0)
601 goto out_free;
602
603 ec += err;
604 if (ec > UBI_MAX_ERASECOUNTER) {
605
606
607
608
609 ubi_err("erase counter overflow at PEB %d, EC %llu",
610 e->pnum, ec);
611 err = -EINVAL;
612 goto out_free;
613 }
614
615 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
616
617 ec_hdr->ec = cpu_to_be64(ec);
618
619 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
620 if (err)
621 goto out_free;
622
623 e->ec = ec;
624 spin_lock(&ubi->wl_lock);
625 if (e->ec > ubi->max_ec)
626 ubi->max_ec = e->ec;
627 spin_unlock(&ubi->wl_lock);
628
629out_free:
630 kfree(ec_hdr);
631 return err;
632}
633
634
635
636
637
638
639
640
641
642
643static void check_protection_over(struct ubi_device *ubi)
644{
645 struct ubi_wl_prot_entry *pe;
646
647
648
649
650
651 while (1) {
652 spin_lock(&ubi->wl_lock);
653 if (!ubi->prot.aec.rb_node) {
654 spin_unlock(&ubi->wl_lock);
655 break;
656 }
657
658 pe = rb_entry(rb_first(&ubi->prot.aec),
659 struct ubi_wl_prot_entry, rb_aec);
660
661 if (pe->abs_ec > ubi->abs_ec) {
662 spin_unlock(&ubi->wl_lock);
663 break;
664 }
665
666 dbg_wl("PEB %d protection over, abs_ec %llu, PEB abs_ec %llu",
667 pe->e->pnum, ubi->abs_ec, pe->abs_ec);
668 rb_erase(&pe->rb_aec, &ubi->prot.aec);
669 rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
670 wl_tree_add(pe->e, &ubi->used);
671 spin_unlock(&ubi->wl_lock);
672
673 kfree(pe);
674 cond_resched();
675 }
676}
677
678
679
680
681
682
683
684
685
686static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
687{
688 spin_lock(&ubi->wl_lock);
689 list_add_tail(&wrk->list, &ubi->works);
690 ubi_assert(ubi->works_count >= 0);
691 ubi->works_count += 1;
692 if (ubi->thread_enabled)
693 wake_up_process(ubi->bgt_thread);
694 spin_unlock(&ubi->wl_lock);
695}
696
697static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
698 int cancel);
699
700
701
702
703
704
705
706
707
708
709static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
710 int torture)
711{
712 struct ubi_work *wl_wrk;
713
714 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
715 e->pnum, e->ec, torture);
716
717 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
718 if (!wl_wrk)
719 return -ENOMEM;
720
721 wl_wrk->func = &erase_worker;
722 wl_wrk->e = e;
723 wl_wrk->torture = torture;
724
725 schedule_ubi_work(ubi, wl_wrk);
726 return 0;
727}
728
729
730
731
732
733
734
735
736
737
738
739static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
740 int cancel)
741{
742 int err, put = 0, scrubbing = 0, protect = 0;
743 struct ubi_wl_prot_entry *uninitialized_var(pe);
744 struct ubi_wl_entry *e1, *e2;
745 struct ubi_vid_hdr *vid_hdr;
746
747 kfree(wrk);
748
749 if (cancel)
750 return 0;
751
752 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
753 if (!vid_hdr)
754 return -ENOMEM;
755
756 mutex_lock(&ubi->move_mutex);
757 spin_lock(&ubi->wl_lock);
758 ubi_assert(!ubi->move_from && !ubi->move_to);
759 ubi_assert(!ubi->move_to_put);
760
761 if (!ubi->free.rb_node ||
762 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
763
764
765
766
767
768
769
770
771
772
773 dbg_wl("cancel WL, a list is empty: free %d, used %d",
774 !ubi->free.rb_node, !ubi->used.rb_node);
775 goto out_cancel;
776 }
777
778 if (!ubi->scrub.rb_node) {
779
780
781
782
783
784 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
785 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
786
787 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
788 dbg_wl("no WL needed: min used EC %d, max free EC %d",
789 e1->ec, e2->ec);
790 goto out_cancel;
791 }
792 paranoid_check_in_wl_tree(e1, &ubi->used);
793 rb_erase(&e1->rb, &ubi->used);
794 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
795 e1->pnum, e1->ec, e2->pnum, e2->ec);
796 } else {
797
798 scrubbing = 1;
799 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, rb);
800 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
801 paranoid_check_in_wl_tree(e1, &ubi->scrub);
802 rb_erase(&e1->rb, &ubi->scrub);
803 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
804 }
805
806 paranoid_check_in_wl_tree(e2, &ubi->free);
807 rb_erase(&e2->rb, &ubi->free);
808 ubi->move_from = e1;
809 ubi->move_to = e2;
810 spin_unlock(&ubi->wl_lock);
811
812
813
814
815
816
817
818
819
820
821
822
823 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
824 if (err && err != UBI_IO_BITFLIPS) {
825 if (err == UBI_IO_PEB_FREE) {
826
827
828
829
830
831
832
833
834 dbg_wl("PEB %d has no VID header", e1->pnum);
835 goto out_not_moved;
836 }
837
838 ubi_err("error %d while reading VID header from PEB %d",
839 err, e1->pnum);
840 if (err > 0)
841 err = -EIO;
842 goto out_error;
843 }
844
845 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
846 if (err) {
847
848 if (err < 0)
849 goto out_error;
850 if (err == 1)
851 goto out_not_moved;
852
853
854
855
856
857
858
859
860 dbg_wl("cancelled moving PEB %d", e1->pnum);
861 pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_NOFS);
862 if (!pe) {
863 err = -ENOMEM;
864 goto out_error;
865 }
866
867 protect = 1;
868 }
869
870 ubi_free_vid_hdr(ubi, vid_hdr);
871 if (scrubbing && !protect)
872 ubi_msg("scrubbed PEB %d, data moved to PEB %d",
873 e1->pnum, e2->pnum);
874
875 spin_lock(&ubi->wl_lock);
876 if (protect)
877 prot_tree_add(ubi, e1, pe, protect);
878 if (!ubi->move_to_put)
879 wl_tree_add(e2, &ubi->used);
880 else
881 put = 1;
882 ubi->move_from = ubi->move_to = NULL;
883 ubi->move_to_put = ubi->wl_scheduled = 0;
884 spin_unlock(&ubi->wl_lock);
885
886 if (put) {
887
888
889
890
891 dbg_wl("PEB %d was put meanwhile, erase", e2->pnum);
892 err = schedule_erase(ubi, e2, 0);
893 if (err)
894 goto out_error;
895 }
896
897 if (!protect) {
898 err = schedule_erase(ubi, e1, 0);
899 if (err)
900 goto out_error;
901 }
902
903
904 dbg_wl("done");
905 mutex_unlock(&ubi->move_mutex);
906 return 0;
907
908
909
910
911
912
913out_not_moved:
914 ubi_free_vid_hdr(ubi, vid_hdr);
915 spin_lock(&ubi->wl_lock);
916 if (scrubbing)
917 wl_tree_add(e1, &ubi->scrub);
918 else
919 wl_tree_add(e1, &ubi->used);
920 ubi->move_from = ubi->move_to = NULL;
921 ubi->move_to_put = ubi->wl_scheduled = 0;
922 spin_unlock(&ubi->wl_lock);
923
924 err = schedule_erase(ubi, e2, 0);
925 if (err)
926 goto out_error;
927
928 mutex_unlock(&ubi->move_mutex);
929 return 0;
930
931out_error:
932 ubi_err("error %d while moving PEB %d to PEB %d",
933 err, e1->pnum, e2->pnum);
934
935 ubi_free_vid_hdr(ubi, vid_hdr);
936 spin_lock(&ubi->wl_lock);
937 ubi->move_from = ubi->move_to = NULL;
938 ubi->move_to_put = ubi->wl_scheduled = 0;
939 spin_unlock(&ubi->wl_lock);
940
941 kmem_cache_free(ubi_wl_entry_slab, e1);
942 kmem_cache_free(ubi_wl_entry_slab, e2);
943 ubi_ro_mode(ubi);
944
945 mutex_unlock(&ubi->move_mutex);
946 return err;
947
948out_cancel:
949 ubi->wl_scheduled = 0;
950 spin_unlock(&ubi->wl_lock);
951 mutex_unlock(&ubi->move_mutex);
952 ubi_free_vid_hdr(ubi, vid_hdr);
953 return 0;
954}
955
956
957
958
959
960
961
962
963
964static int ensure_wear_leveling(struct ubi_device *ubi)
965{
966 int err = 0;
967 struct ubi_wl_entry *e1;
968 struct ubi_wl_entry *e2;
969 struct ubi_work *wrk;
970
971 spin_lock(&ubi->wl_lock);
972 if (ubi->wl_scheduled)
973
974 goto out_unlock;
975
976
977
978
979
980 if (!ubi->scrub.rb_node) {
981 if (!ubi->used.rb_node || !ubi->free.rb_node)
982
983 goto out_unlock;
984
985
986
987
988
989
990
991 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
992 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
993
994 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
995 goto out_unlock;
996 dbg_wl("schedule wear-leveling");
997 } else
998 dbg_wl("schedule scrubbing");
999
1000 ubi->wl_scheduled = 1;
1001 spin_unlock(&ubi->wl_lock);
1002
1003 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
1004 if (!wrk) {
1005 err = -ENOMEM;
1006 goto out_cancel;
1007 }
1008
1009 wrk->func = &wear_leveling_worker;
1010 schedule_ubi_work(ubi, wrk);
1011 return err;
1012
1013out_cancel:
1014 spin_lock(&ubi->wl_lock);
1015 ubi->wl_scheduled = 0;
1016out_unlock:
1017 spin_unlock(&ubi->wl_lock);
1018 return err;
1019}
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1033 int cancel)
1034{
1035 struct ubi_wl_entry *e = wl_wrk->e;
1036 int pnum = e->pnum, err, need;
1037
1038 if (cancel) {
1039 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
1040 kfree(wl_wrk);
1041 kmem_cache_free(ubi_wl_entry_slab, e);
1042 return 0;
1043 }
1044
1045 dbg_wl("erase PEB %d EC %d", pnum, e->ec);
1046
1047 err = sync_erase(ubi, e, wl_wrk->torture);
1048 if (!err) {
1049
1050 kfree(wl_wrk);
1051
1052 spin_lock(&ubi->wl_lock);
1053 ubi->abs_ec += 1;
1054 wl_tree_add(e, &ubi->free);
1055 spin_unlock(&ubi->wl_lock);
1056
1057
1058
1059
1060
1061 check_protection_over(ubi);
1062
1063
1064 err = ensure_wear_leveling(ubi);
1065 return err;
1066 }
1067
1068 ubi_err("failed to erase PEB %d, error %d", pnum, err);
1069 kfree(wl_wrk);
1070 kmem_cache_free(ubi_wl_entry_slab, e);
1071
1072 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1073 err == -EBUSY) {
1074 int err1;
1075
1076
1077 err1 = schedule_erase(ubi, e, 0);
1078 if (err1) {
1079 err = err1;
1080 goto out_ro;
1081 }
1082 return err;
1083 } else if (err != -EIO) {
1084
1085
1086
1087
1088
1089 goto out_ro;
1090 }
1091
1092
1093
1094 if (!ubi->bad_allowed) {
1095 ubi_err("bad physical eraseblock %d detected", pnum);
1096 goto out_ro;
1097 }
1098
1099 spin_lock(&ubi->volumes_lock);
1100 need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
1101 if (need > 0) {
1102 need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
1103 ubi->avail_pebs -= need;
1104 ubi->rsvd_pebs += need;
1105 ubi->beb_rsvd_pebs += need;
1106 if (need > 0)
1107 ubi_msg("reserve more %d PEBs", need);
1108 }
1109
1110 if (ubi->beb_rsvd_pebs == 0) {
1111 spin_unlock(&ubi->volumes_lock);
1112 ubi_err("no reserved physical eraseblocks");
1113 goto out_ro;
1114 }
1115
1116 spin_unlock(&ubi->volumes_lock);
1117 ubi_msg("mark PEB %d as bad", pnum);
1118
1119 err = ubi_io_mark_bad(ubi, pnum);
1120 if (err)
1121 goto out_ro;
1122
1123 spin_lock(&ubi->volumes_lock);
1124 ubi->beb_rsvd_pebs -= 1;
1125 ubi->bad_peb_count += 1;
1126 ubi->good_peb_count -= 1;
1127 ubi_calculate_reserved(ubi);
1128 if (ubi->beb_rsvd_pebs == 0)
1129 ubi_warn("last PEB from the reserved pool was used");
1130 spin_unlock(&ubi->volumes_lock);
1131
1132 return err;
1133
1134out_ro:
1135 ubi_ro_mode(ubi);
1136 return err;
1137}
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
1151{
1152 int err;
1153 struct ubi_wl_entry *e;
1154
1155 dbg_wl("PEB %d", pnum);
1156 ubi_assert(pnum >= 0);
1157 ubi_assert(pnum < ubi->peb_count);
1158
1159retry:
1160 spin_lock(&ubi->wl_lock);
1161 e = ubi->lookuptbl[pnum];
1162 if (e == ubi->move_from) {
1163
1164
1165
1166
1167
1168 dbg_wl("PEB %d is being moved, wait", pnum);
1169 spin_unlock(&ubi->wl_lock);
1170
1171
1172 mutex_lock(&ubi->move_mutex);
1173 mutex_unlock(&ubi->move_mutex);
1174 goto retry;
1175 } else if (e == ubi->move_to) {
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185 dbg_wl("PEB %d is the target of data moving", pnum);
1186 ubi_assert(!ubi->move_to_put);
1187 ubi->move_to_put = 1;
1188 spin_unlock(&ubi->wl_lock);
1189 return 0;
1190 } else {
1191 if (in_wl_tree(e, &ubi->used)) {
1192 paranoid_check_in_wl_tree(e, &ubi->used);
1193 rb_erase(&e->rb, &ubi->used);
1194 } else if (in_wl_tree(e, &ubi->scrub)) {
1195 paranoid_check_in_wl_tree(e, &ubi->scrub);
1196 rb_erase(&e->rb, &ubi->scrub);
1197 } else {
1198 err = prot_tree_del(ubi, e->pnum);
1199 if (err) {
1200 ubi_err("PEB %d not found", pnum);
1201 ubi_ro_mode(ubi);
1202 spin_unlock(&ubi->wl_lock);
1203 return err;
1204 }
1205 }
1206 }
1207 spin_unlock(&ubi->wl_lock);
1208
1209 err = schedule_erase(ubi, e, torture);
1210 if (err) {
1211 spin_lock(&ubi->wl_lock);
1212 wl_tree_add(e, &ubi->used);
1213 spin_unlock(&ubi->wl_lock);
1214 }
1215
1216 return err;
1217}
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1230{
1231 struct ubi_wl_entry *e;
1232
1233 dbg_msg("schedule PEB %d for scrubbing", pnum);
1234
1235retry:
1236 spin_lock(&ubi->wl_lock);
1237 e = ubi->lookuptbl[pnum];
1238 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub)) {
1239 spin_unlock(&ubi->wl_lock);
1240 return 0;
1241 }
1242
1243 if (e == ubi->move_to) {
1244
1245
1246
1247
1248
1249
1250 spin_unlock(&ubi->wl_lock);
1251 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1252 yield();
1253 goto retry;
1254 }
1255
1256 if (in_wl_tree(e, &ubi->used)) {
1257 paranoid_check_in_wl_tree(e, &ubi->used);
1258 rb_erase(&e->rb, &ubi->used);
1259 } else {
1260 int err;
1261
1262 err = prot_tree_del(ubi, e->pnum);
1263 if (err) {
1264 ubi_err("PEB %d not found", pnum);
1265 ubi_ro_mode(ubi);
1266 spin_unlock(&ubi->wl_lock);
1267 return err;
1268 }
1269 }
1270
1271 wl_tree_add(e, &ubi->scrub);
1272 spin_unlock(&ubi->wl_lock);
1273
1274
1275
1276
1277
1278 return ensure_wear_leveling(ubi);
1279}
1280
1281
1282
1283
1284
1285
1286
1287
1288int ubi_wl_flush(struct ubi_device *ubi)
1289{
1290 int err;
1291
1292
1293
1294
1295
1296 dbg_wl("flush (%d pending works)", ubi->works_count);
1297 while (ubi->works_count) {
1298 err = do_work(ubi);
1299 if (err)
1300 return err;
1301 }
1302
1303
1304
1305
1306
1307 down_write(&ubi->work_sem);
1308 up_write(&ubi->work_sem);
1309
1310
1311
1312
1313
1314 while (ubi->works_count) {
1315 dbg_wl("flush more (%d pending works)", ubi->works_count);
1316 err = do_work(ubi);
1317 if (err)
1318 return err;
1319 }
1320
1321 return 0;
1322}
1323
1324
1325
1326
1327
1328static void tree_destroy(struct rb_root *root)
1329{
1330 struct rb_node *rb;
1331 struct ubi_wl_entry *e;
1332
1333 rb = root->rb_node;
1334 while (rb) {
1335 if (rb->rb_left)
1336 rb = rb->rb_left;
1337 else if (rb->rb_right)
1338 rb = rb->rb_right;
1339 else {
1340 e = rb_entry(rb, struct ubi_wl_entry, rb);
1341
1342 rb = rb_parent(rb);
1343 if (rb) {
1344 if (rb->rb_left == &e->rb)
1345 rb->rb_left = NULL;
1346 else
1347 rb->rb_right = NULL;
1348 }
1349
1350 kmem_cache_free(ubi_wl_entry_slab, e);
1351 }
1352 }
1353}
1354
1355
1356
1357
1358
1359int ubi_thread(void *u)
1360{
1361 int failures = 0;
1362 struct ubi_device *ubi = u;
1363
1364 ubi_msg("background thread \"%s\" started, PID %d",
1365 ubi->bgt_name, task_pid_nr(current));
1366
1367 set_freezable();
1368 for (;;) {
1369 int err;
1370
1371 if (kthread_should_stop())
1372 break;
1373
1374 if (try_to_freeze())
1375 continue;
1376
1377 spin_lock(&ubi->wl_lock);
1378 if (list_empty(&ubi->works) || ubi->ro_mode ||
1379 !ubi->thread_enabled) {
1380 set_current_state(TASK_INTERRUPTIBLE);
1381 spin_unlock(&ubi->wl_lock);
1382 schedule();
1383 continue;
1384 }
1385 spin_unlock(&ubi->wl_lock);
1386
1387 err = do_work(ubi);
1388 if (err) {
1389 ubi_err("%s: work failed with error code %d",
1390 ubi->bgt_name, err);
1391 if (failures++ > WL_MAX_FAILURES) {
1392
1393
1394
1395
1396 ubi_msg("%s: %d consecutive failures",
1397 ubi->bgt_name, WL_MAX_FAILURES);
1398 ubi_ro_mode(ubi);
1399 ubi->thread_enabled = 0;
1400 continue;
1401 }
1402 } else
1403 failures = 0;
1404
1405 cond_resched();
1406 }
1407
1408 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1409 return 0;
1410}
1411
1412
1413
1414
1415
1416static void cancel_pending(struct ubi_device *ubi)
1417{
1418 while (!list_empty(&ubi->works)) {
1419 struct ubi_work *wrk;
1420
1421 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1422 list_del(&wrk->list);
1423 wrk->func(ubi, wrk, 1);
1424 ubi->works_count -= 1;
1425 ubi_assert(ubi->works_count >= 0);
1426 }
1427}
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1438{
1439 int err;
1440 struct rb_node *rb1, *rb2;
1441 struct ubi_scan_volume *sv;
1442 struct ubi_scan_leb *seb, *tmp;
1443 struct ubi_wl_entry *e;
1444
1445
1446 ubi->used = ubi->free = ubi->scrub = RB_ROOT;
1447 ubi->prot.pnum = ubi->prot.aec = RB_ROOT;
1448 spin_lock_init(&ubi->wl_lock);
1449 mutex_init(&ubi->move_mutex);
1450 init_rwsem(&ubi->work_sem);
1451 ubi->max_ec = si->max_ec;
1452 INIT_LIST_HEAD(&ubi->works);
1453
1454 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1455
1456 err = -ENOMEM;
1457 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1458 if (!ubi->lookuptbl)
1459 return err;
1460
1461 list_for_each_entry_safe(seb, tmp, &si->erase, u.list) {
1462 cond_resched();
1463
1464 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1465 if (!e)
1466 goto out_free;
1467
1468 e->pnum = seb->pnum;
1469 e->ec = seb->ec;
1470 ubi->lookuptbl[e->pnum] = e;
1471 if (schedule_erase(ubi, e, 0)) {
1472 kmem_cache_free(ubi_wl_entry_slab, e);
1473 goto out_free;
1474 }
1475 }
1476
1477 list_for_each_entry(seb, &si->free, u.list) {
1478 cond_resched();
1479
1480 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1481 if (!e)
1482 goto out_free;
1483
1484 e->pnum = seb->pnum;
1485 e->ec = seb->ec;
1486 ubi_assert(e->ec >= 0);
1487 wl_tree_add(e, &ubi->free);
1488 ubi->lookuptbl[e->pnum] = e;
1489 }
1490
1491 list_for_each_entry(seb, &si->corr, u.list) {
1492 cond_resched();
1493
1494 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1495 if (!e)
1496 goto out_free;
1497
1498 e->pnum = seb->pnum;
1499 e->ec = seb->ec;
1500 ubi->lookuptbl[e->pnum] = e;
1501 if (schedule_erase(ubi, e, 0)) {
1502 kmem_cache_free(ubi_wl_entry_slab, e);
1503 goto out_free;
1504 }
1505 }
1506
1507 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1508 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1509 cond_resched();
1510
1511 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1512 if (!e)
1513 goto out_free;
1514
1515 e->pnum = seb->pnum;
1516 e->ec = seb->ec;
1517 ubi->lookuptbl[e->pnum] = e;
1518 if (!seb->scrub) {
1519 dbg_wl("add PEB %d EC %d to the used tree",
1520 e->pnum, e->ec);
1521 wl_tree_add(e, &ubi->used);
1522 } else {
1523 dbg_wl("add PEB %d EC %d to the scrub tree",
1524 e->pnum, e->ec);
1525 wl_tree_add(e, &ubi->scrub);
1526 }
1527 }
1528 }
1529
1530 if (ubi->avail_pebs < WL_RESERVED_PEBS) {
1531 ubi_err("no enough physical eraseblocks (%d, need %d)",
1532 ubi->avail_pebs, WL_RESERVED_PEBS);
1533 goto out_free;
1534 }
1535 ubi->avail_pebs -= WL_RESERVED_PEBS;
1536 ubi->rsvd_pebs += WL_RESERVED_PEBS;
1537
1538
1539 err = ensure_wear_leveling(ubi);
1540 if (err)
1541 goto out_free;
1542
1543 return 0;
1544
1545out_free:
1546 cancel_pending(ubi);
1547 tree_destroy(&ubi->used);
1548 tree_destroy(&ubi->free);
1549 tree_destroy(&ubi->scrub);
1550 kfree(ubi->lookuptbl);
1551 return err;
1552}
1553
1554
1555
1556
1557
1558static void protection_trees_destroy(struct ubi_device *ubi)
1559{
1560 struct rb_node *rb;
1561 struct ubi_wl_prot_entry *pe;
1562
1563 rb = ubi->prot.aec.rb_node;
1564 while (rb) {
1565 if (rb->rb_left)
1566 rb = rb->rb_left;
1567 else if (rb->rb_right)
1568 rb = rb->rb_right;
1569 else {
1570 pe = rb_entry(rb, struct ubi_wl_prot_entry, rb_aec);
1571
1572 rb = rb_parent(rb);
1573 if (rb) {
1574 if (rb->rb_left == &pe->rb_aec)
1575 rb->rb_left = NULL;
1576 else
1577 rb->rb_right = NULL;
1578 }
1579
1580 kmem_cache_free(ubi_wl_entry_slab, pe->e);
1581 kfree(pe);
1582 }
1583 }
1584}
1585
1586
1587
1588
1589
1590void ubi_wl_close(struct ubi_device *ubi)
1591{
1592 dbg_wl("close the WL sub-system");
1593 cancel_pending(ubi);
1594 protection_trees_destroy(ubi);
1595 tree_destroy(&ubi->used);
1596 tree_destroy(&ubi->free);
1597 tree_destroy(&ubi->scrub);
1598 kfree(ubi->lookuptbl);
1599}
1600
1601#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
1614{
1615 int err;
1616 long long read_ec;
1617 struct ubi_ec_hdr *ec_hdr;
1618
1619 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1620 if (!ec_hdr)
1621 return -ENOMEM;
1622
1623 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1624 if (err && err != UBI_IO_BITFLIPS) {
1625
1626 err = 0;
1627 goto out_free;
1628 }
1629
1630 read_ec = be64_to_cpu(ec_hdr->ec);
1631 if (ec != read_ec) {
1632 ubi_err("paranoid check failed for PEB %d", pnum);
1633 ubi_err("read EC is %lld, should be %d", read_ec, ec);
1634 ubi_dbg_dump_stack();
1635 err = 1;
1636 } else
1637 err = 0;
1638
1639out_free:
1640 kfree(ec_hdr);
1641 return err;
1642}
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
1653 struct rb_root *root)
1654{
1655 if (in_wl_tree(e, root))
1656 return 0;
1657
1658 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
1659 e->pnum, e->ec, root);
1660 ubi_dbg_dump_stack();
1661 return 1;
1662}
1663
1664#endif
1665