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#include <linux/err.h>
44#include <linux/crc32.h>
45#include "ubi.h"
46
47#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
48static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
49#else
50#define paranoid_check_si(ubi, si) 0
51#endif
52
53
54static struct ubi_ec_hdr *ech;
55static struct ubi_vid_hdr *vidh;
56
57
58
59
60
61
62
63
64
65
66
67
68static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
69 struct list_head *list)
70{
71 struct ubi_scan_leb *seb;
72
73 if (list == &si->free)
74 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
75 else if (list == &si->erase)
76 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
77 else if (list == &si->corr)
78 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
79 else if (list == &si->alien)
80 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
81 else
82 BUG();
83
84 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
85 if (!seb)
86 return -ENOMEM;
87
88 seb->pnum = pnum;
89 seb->ec = ec;
90 list_add_tail(&seb->u.list, list);
91 return 0;
92}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107static void commit_to_mean_value(struct ubi_scan_info *si)
108{
109 si->ec_sum /= si->ec_count;
110 if (si->ec_sum % si->ec_count >= si->ec_count / 2)
111 si->mean_ec += 1;
112 si->mean_ec += si->ec_sum;
113}
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
131 const struct ubi_scan_volume *sv, int pnum)
132{
133 int vol_type = vid_hdr->vol_type;
134 int vol_id = be32_to_cpu(vid_hdr->vol_id);
135 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
136 int data_pad = be32_to_cpu(vid_hdr->data_pad);
137
138 if (sv->leb_count != 0) {
139 int sv_vol_type;
140
141
142
143
144
145
146
147 if (vol_id != sv->vol_id) {
148 dbg_err("inconsistent vol_id");
149 goto bad;
150 }
151
152 if (sv->vol_type == UBI_STATIC_VOLUME)
153 sv_vol_type = UBI_VID_STATIC;
154 else
155 sv_vol_type = UBI_VID_DYNAMIC;
156
157 if (vol_type != sv_vol_type) {
158 dbg_err("inconsistent vol_type");
159 goto bad;
160 }
161
162 if (used_ebs != sv->used_ebs) {
163 dbg_err("inconsistent used_ebs");
164 goto bad;
165 }
166
167 if (data_pad != sv->data_pad) {
168 dbg_err("inconsistent data_pad");
169 goto bad;
170 }
171 }
172
173 return 0;
174
175bad:
176 ubi_err("inconsistent VID header at PEB %d", pnum);
177 ubi_dbg_dump_vid_hdr(vid_hdr);
178 ubi_dbg_dump_sv(sv);
179 return -EINVAL;
180}
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
196 int pnum,
197 const struct ubi_vid_hdr *vid_hdr)
198{
199 struct ubi_scan_volume *sv;
200 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
201
202 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
203
204
205 while (*p) {
206 parent = *p;
207 sv = rb_entry(parent, struct ubi_scan_volume, rb);
208
209 if (vol_id == sv->vol_id)
210 return sv;
211
212 if (vol_id > sv->vol_id)
213 p = &(*p)->rb_left;
214 else
215 p = &(*p)->rb_right;
216 }
217
218
219 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
220 if (!sv)
221 return ERR_PTR(-ENOMEM);
222
223 sv->highest_lnum = sv->leb_count = 0;
224 sv->vol_id = vol_id;
225 sv->root = RB_ROOT;
226 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
227 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
228 sv->compat = vid_hdr->compat;
229 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
230 : UBI_STATIC_VOLUME;
231 if (vol_id > si->highest_vol_id)
232 si->highest_vol_id = vol_id;
233
234 rb_link_node(&sv->rb, parent, p);
235 rb_insert_color(&sv->rb, &si->volumes);
236 si->vols_found += 1;
237 dbg_bld("added volume %d", vol_id);
238 return sv;
239}
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
262 int pnum, const struct ubi_vid_hdr *vid_hdr)
263{
264 void *buf;
265 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
266 uint32_t data_crc, crc;
267 struct ubi_vid_hdr *vh = NULL;
268 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
269
270 if (seb->sqnum == 0 && sqnum2 == 0) {
271 long long abs, v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver);
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289 dbg_bld("using old crappy leb_ver stuff");
290
291 if (v1 == v2) {
292 ubi_err("PEB %d and PEB %d have the same version %lld",
293 seb->pnum, pnum, v1);
294 return -EINVAL;
295 }
296
297 abs = v1 - v2;
298 if (abs < 0)
299 abs = -abs;
300
301 if (abs < 0x7FFFFFFF)
302
303 second_is_newer = (v2 > v1);
304 else
305 second_is_newer = (v2 < v1);
306 } else
307
308 second_is_newer = sqnum2 > seb->sqnum;
309
310
311
312
313
314
315
316
317
318
319 if (second_is_newer) {
320 if (!vid_hdr->copy_flag) {
321
322 dbg_bld("second PEB %d is newer, copy_flag is unset",
323 pnum);
324 return 1;
325 }
326 } else {
327 pnum = seb->pnum;
328
329 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
330 if (!vh)
331 return -ENOMEM;
332
333 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
334 if (err) {
335 if (err == UBI_IO_BITFLIPS)
336 bitflips = 1;
337 else {
338 dbg_err("VID of PEB %d header is bad, but it "
339 "was OK earlier", pnum);
340 if (err > 0)
341 err = -EIO;
342
343 goto out_free_vidh;
344 }
345 }
346
347 if (!vh->copy_flag) {
348
349 dbg_bld("first PEB %d is newer, copy_flag is unset",
350 pnum);
351 err = bitflips << 1;
352 goto out_free_vidh;
353 }
354
355 vid_hdr = vh;
356 }
357
358
359
360 len = be32_to_cpu(vid_hdr->data_size);
361 buf = vmalloc(len);
362 if (!buf) {
363 err = -ENOMEM;
364 goto out_free_vidh;
365 }
366
367 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
368 if (err && err != UBI_IO_BITFLIPS)
369 goto out_free_buf;
370
371 data_crc = be32_to_cpu(vid_hdr->data_crc);
372 crc = crc32(UBI_CRC32_INIT, buf, len);
373 if (crc != data_crc) {
374 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
375 pnum, crc, data_crc);
376 corrupted = 1;
377 bitflips = 0;
378 second_is_newer = !second_is_newer;
379 } else {
380 dbg_bld("PEB %d CRC is OK", pnum);
381 bitflips = !!err;
382 }
383
384 vfree(buf);
385 ubi_free_vid_hdr(ubi, vh);
386
387 if (second_is_newer)
388 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
389 else
390 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
391
392 return second_is_newer | (bitflips << 1) | (corrupted << 2);
393
394out_free_buf:
395 vfree(buf);
396out_free_vidh:
397 ubi_free_vid_hdr(ubi, vh);
398 return err;
399}
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
419 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
420 int bitflips)
421{
422 int err, vol_id, lnum;
423 uint32_t leb_ver;
424 unsigned long long sqnum;
425 struct ubi_scan_volume *sv;
426 struct ubi_scan_leb *seb;
427 struct rb_node **p, *parent = NULL;
428
429 vol_id = be32_to_cpu(vid_hdr->vol_id);
430 lnum = be32_to_cpu(vid_hdr->lnum);
431 sqnum = be64_to_cpu(vid_hdr->sqnum);
432 leb_ver = be32_to_cpu(vid_hdr->leb_ver);
433
434 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d",
435 pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips);
436
437 sv = add_volume(si, vol_id, pnum, vid_hdr);
438 if (IS_ERR(sv) < 0)
439 return PTR_ERR(sv);
440
441 if (si->max_sqnum < sqnum)
442 si->max_sqnum = sqnum;
443
444
445
446
447
448 p = &sv->root.rb_node;
449 while (*p) {
450 int cmp_res;
451
452 parent = *p;
453 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
454 if (lnum != seb->lnum) {
455 if (lnum < seb->lnum)
456 p = &(*p)->rb_left;
457 else
458 p = &(*p)->rb_right;
459 continue;
460 }
461
462
463
464
465
466
467 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
468 "LEB ver %u, EC %d", seb->pnum, seb->sqnum,
469 seb->leb_ver, seb->ec);
470
471
472
473
474
475 if (seb->leb_ver == leb_ver && leb_ver != 0) {
476 ubi_err("two LEBs with same version %u", leb_ver);
477 ubi_dbg_dump_seb(seb, 0);
478 ubi_dbg_dump_vid_hdr(vid_hdr);
479 return -EINVAL;
480 }
481
482
483
484
485
486
487
488 if (seb->sqnum == sqnum && sqnum != 0) {
489 ubi_err("two LEBs with same sequence number %llu",
490 sqnum);
491 ubi_dbg_dump_seb(seb, 0);
492 ubi_dbg_dump_vid_hdr(vid_hdr);
493 return -EINVAL;
494 }
495
496
497
498
499
500 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
501 if (cmp_res < 0)
502 return cmp_res;
503
504 if (cmp_res & 1) {
505
506
507
508
509 err = validate_vid_hdr(vid_hdr, sv, pnum);
510 if (err)
511 return err;
512
513 if (cmp_res & 4)
514 err = add_to_list(si, seb->pnum, seb->ec,
515 &si->corr);
516 else
517 err = add_to_list(si, seb->pnum, seb->ec,
518 &si->erase);
519 if (err)
520 return err;
521
522 seb->ec = ec;
523 seb->pnum = pnum;
524 seb->scrub = ((cmp_res & 2) || bitflips);
525 seb->sqnum = sqnum;
526 seb->leb_ver = leb_ver;
527
528 if (sv->highest_lnum == lnum)
529 sv->last_data_size =
530 be32_to_cpu(vid_hdr->data_size);
531
532 return 0;
533 } else {
534
535
536
537
538 if (cmp_res & 4)
539 return add_to_list(si, pnum, ec, &si->corr);
540 else
541 return add_to_list(si, pnum, ec, &si->erase);
542 }
543 }
544
545
546
547
548
549
550 err = validate_vid_hdr(vid_hdr, sv, pnum);
551 if (err)
552 return err;
553
554 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
555 if (!seb)
556 return -ENOMEM;
557
558 seb->ec = ec;
559 seb->pnum = pnum;
560 seb->lnum = lnum;
561 seb->sqnum = sqnum;
562 seb->scrub = bitflips;
563 seb->leb_ver = leb_ver;
564
565 if (sv->highest_lnum <= lnum) {
566 sv->highest_lnum = lnum;
567 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
568 }
569
570 sv->leb_count += 1;
571 rb_link_node(&seb->u.rb, parent, p);
572 rb_insert_color(&seb->u.rb, &sv->root);
573 return 0;
574}
575
576
577
578
579
580
581
582
583
584
585struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
586 int vol_id)
587{
588 struct ubi_scan_volume *sv;
589 struct rb_node *p = si->volumes.rb_node;
590
591 while (p) {
592 sv = rb_entry(p, struct ubi_scan_volume, rb);
593
594 if (vol_id == sv->vol_id)
595 return sv;
596
597 if (vol_id > sv->vol_id)
598 p = p->rb_left;
599 else
600 p = p->rb_right;
601 }
602
603 return NULL;
604}
605
606
607
608
609
610
611
612
613
614
615struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
616 int lnum)
617{
618 struct ubi_scan_leb *seb;
619 struct rb_node *p = sv->root.rb_node;
620
621 while (p) {
622 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
623
624 if (lnum == seb->lnum)
625 return seb;
626
627 if (lnum > seb->lnum)
628 p = p->rb_left;
629 else
630 p = p->rb_right;
631 }
632
633 return NULL;
634}
635
636
637
638
639
640
641void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
642{
643 struct rb_node *rb;
644 struct ubi_scan_leb *seb;
645
646 dbg_bld("remove scanning information about volume %d", sv->vol_id);
647
648 while ((rb = rb_first(&sv->root))) {
649 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
650 rb_erase(&seb->u.rb, &sv->root);
651 list_add_tail(&seb->u.list, &si->erase);
652 }
653
654 rb_erase(&sv->rb, &si->volumes);
655 kfree(sv);
656 si->vols_found -= 1;
657}
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
673 int pnum, int ec)
674{
675 int err;
676 struct ubi_ec_hdr *ec_hdr;
677
678 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
679
680
681
682
683 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
684 return -EINVAL;
685 }
686
687 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
688 if (!ec_hdr)
689 return -ENOMEM;
690
691 ec_hdr->ec = cpu_to_be64(ec);
692
693 err = ubi_io_sync_erase(ubi, pnum, 0);
694 if (err < 0)
695 goto out_free;
696
697 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
698
699out_free:
700 kfree(ec_hdr);
701 return err;
702}
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
718 struct ubi_scan_info *si)
719{
720 int err = 0, i;
721 struct ubi_scan_leb *seb;
722
723 if (!list_empty(&si->free)) {
724 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
725 list_del(&seb->u.list);
726 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
727 return seb;
728 }
729
730 for (i = 0; i < 2; i++) {
731 struct list_head *head;
732 struct ubi_scan_leb *tmp_seb;
733
734 if (i == 0)
735 head = &si->erase;
736 else
737 head = &si->corr;
738
739
740
741
742
743
744
745 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
746 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
747 seb->ec = si->mean_ec;
748
749 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
750 if (err)
751 continue;
752
753 seb->ec += 1;
754 list_del(&seb->u.list);
755 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
756 return seb;
757 }
758 }
759
760 ubi_err("no eraseblocks found");
761 return ERR_PTR(-ENOSPC);
762}
763
764
765
766
767
768
769
770
771
772
773
774static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum)
775{
776 long long uninitialized_var(ec);
777 int err, bitflips = 0, vol_id, ec_corr = 0;
778
779 dbg_bld("scan PEB %d", pnum);
780
781
782 err = ubi_io_is_bad(ubi, pnum);
783 if (err < 0)
784 return err;
785 else if (err) {
786
787
788
789
790 si->bad_peb_count += 1;
791 return 0;
792 }
793
794 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
795 if (err < 0)
796 return err;
797 else if (err == UBI_IO_BITFLIPS)
798 bitflips = 1;
799 else if (err == UBI_IO_PEB_EMPTY)
800 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
801 else if (err == UBI_IO_BAD_EC_HDR) {
802
803
804
805
806
807 ec_corr = 1;
808 ec = UBI_SCAN_UNKNOWN_EC;
809 bitflips = 1;
810 }
811
812 si->is_empty = 0;
813
814 if (!ec_corr) {
815
816 if (ech->version != UBI_VERSION) {
817 ubi_err("this UBI version is %d, image version is %d",
818 UBI_VERSION, (int)ech->version);
819 return -EINVAL;
820 }
821
822 ec = be64_to_cpu(ech->ec);
823 if (ec > UBI_MAX_ERASECOUNTER) {
824
825
826
827
828
829
830
831 ubi_err("erase counter overflow, max is %d",
832 UBI_MAX_ERASECOUNTER);
833 ubi_dbg_dump_ec_hdr(ech);
834 return -EINVAL;
835 }
836 }
837
838
839
840 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
841 if (err < 0)
842 return err;
843 else if (err == UBI_IO_BITFLIPS)
844 bitflips = 1;
845 else if (err == UBI_IO_BAD_VID_HDR ||
846 (err == UBI_IO_PEB_FREE && ec_corr)) {
847
848 err = add_to_list(si, pnum, ec, &si->corr);
849 if (err)
850 return err;
851 goto adjust_mean_ec;
852 } else if (err == UBI_IO_PEB_FREE) {
853
854 err = add_to_list(si, pnum, ec, &si->free);
855 if (err)
856 return err;
857 goto adjust_mean_ec;
858 }
859
860 vol_id = be32_to_cpu(vidh->vol_id);
861 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
862 int lnum = be32_to_cpu(vidh->lnum);
863
864
865 switch (vidh->compat) {
866 case UBI_COMPAT_DELETE:
867 ubi_msg("\"delete\" compatible internal volume %d:%d"
868 " found, remove it", vol_id, lnum);
869 err = add_to_list(si, pnum, ec, &si->corr);
870 if (err)
871 return err;
872 break;
873
874 case UBI_COMPAT_RO:
875 ubi_msg("read-only compatible internal volume %d:%d"
876 " found, switch to read-only mode",
877 vol_id, lnum);
878 ubi->ro_mode = 1;
879 break;
880
881 case UBI_COMPAT_PRESERVE:
882 ubi_msg("\"preserve\" compatible internal volume %d:%d"
883 " found", vol_id, lnum);
884 err = add_to_list(si, pnum, ec, &si->alien);
885 if (err)
886 return err;
887 si->alien_peb_count += 1;
888 return 0;
889
890 case UBI_COMPAT_REJECT:
891 ubi_err("incompatible internal volume %d:%d found",
892 vol_id, lnum);
893 return -EINVAL;
894 }
895 }
896
897
898 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
899 if (err)
900 return err;
901
902adjust_mean_ec:
903 if (!ec_corr) {
904 if (si->ec_sum + ec < ec) {
905 commit_to_mean_value(si);
906 si->ec_sum = 0;
907 si->ec_count = 0;
908 } else {
909 si->ec_sum += ec;
910 si->ec_count += 1;
911 }
912
913 if (ec > si->max_ec)
914 si->max_ec = ec;
915 if (ec < si->min_ec)
916 si->min_ec = ec;
917 }
918
919 return 0;
920}
921
922
923
924
925
926
927
928
929struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
930{
931 int err, pnum;
932 struct rb_node *rb1, *rb2;
933 struct ubi_scan_volume *sv;
934 struct ubi_scan_leb *seb;
935 struct ubi_scan_info *si;
936
937 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
938 if (!si)
939 return ERR_PTR(-ENOMEM);
940
941 INIT_LIST_HEAD(&si->corr);
942 INIT_LIST_HEAD(&si->free);
943 INIT_LIST_HEAD(&si->erase);
944 INIT_LIST_HEAD(&si->alien);
945 si->volumes = RB_ROOT;
946 si->is_empty = 1;
947
948 err = -ENOMEM;
949 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
950 if (!ech)
951 goto out_si;
952
953 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
954 if (!vidh)
955 goto out_ech;
956
957 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
958 cond_resched();
959
960 dbg_msg("process PEB %d", pnum);
961 err = process_eb(ubi, si, pnum);
962 if (err < 0)
963 goto out_vidh;
964 }
965
966 dbg_msg("scanning is finished");
967
968
969 if (si->ec_count)
970 commit_to_mean_value(si);
971
972 if (si->is_empty)
973 ubi_msg("empty MTD device detected");
974
975
976
977
978
979 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
980 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
981 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
982 seb->ec = si->mean_ec;
983 }
984
985 list_for_each_entry(seb, &si->free, u.list) {
986 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
987 seb->ec = si->mean_ec;
988 }
989
990 list_for_each_entry(seb, &si->corr, u.list)
991 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
992 seb->ec = si->mean_ec;
993
994 list_for_each_entry(seb, &si->erase, u.list)
995 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
996 seb->ec = si->mean_ec;
997
998 err = paranoid_check_si(ubi, si);
999 if (err) {
1000 if (err > 0)
1001 err = -EINVAL;
1002 goto out_vidh;
1003 }
1004
1005 ubi_free_vid_hdr(ubi, vidh);
1006 kfree(ech);
1007
1008 return si;
1009
1010out_vidh:
1011 ubi_free_vid_hdr(ubi, vidh);
1012out_ech:
1013 kfree(ech);
1014out_si:
1015 ubi_scan_destroy_si(si);
1016 return ERR_PTR(err);
1017}
1018
1019
1020
1021
1022
1023
1024
1025
1026static void destroy_sv(struct ubi_scan_volume *sv)
1027{
1028 struct ubi_scan_leb *seb;
1029 struct rb_node *this = sv->root.rb_node;
1030
1031 while (this) {
1032 if (this->rb_left)
1033 this = this->rb_left;
1034 else if (this->rb_right)
1035 this = this->rb_right;
1036 else {
1037 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1038 this = rb_parent(this);
1039 if (this) {
1040 if (this->rb_left == &seb->u.rb)
1041 this->rb_left = NULL;
1042 else
1043 this->rb_right = NULL;
1044 }
1045
1046 kfree(seb);
1047 }
1048 }
1049 kfree(sv);
1050}
1051
1052
1053
1054
1055
1056void ubi_scan_destroy_si(struct ubi_scan_info *si)
1057{
1058 struct ubi_scan_leb *seb, *seb_tmp;
1059 struct ubi_scan_volume *sv;
1060 struct rb_node *rb;
1061
1062 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1063 list_del(&seb->u.list);
1064 kfree(seb);
1065 }
1066 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1067 list_del(&seb->u.list);
1068 kfree(seb);
1069 }
1070 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1071 list_del(&seb->u.list);
1072 kfree(seb);
1073 }
1074 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1075 list_del(&seb->u.list);
1076 kfree(seb);
1077 }
1078
1079
1080 rb = si->volumes.rb_node;
1081 while (rb) {
1082 if (rb->rb_left)
1083 rb = rb->rb_left;
1084 else if (rb->rb_right)
1085 rb = rb->rb_right;
1086 else {
1087 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1088
1089 rb = rb_parent(rb);
1090 if (rb) {
1091 if (rb->rb_left == &sv->rb)
1092 rb->rb_left = NULL;
1093 else
1094 rb->rb_right = NULL;
1095 }
1096
1097 destroy_sv(sv);
1098 }
1099 }
1100
1101 kfree(si);
1102}
1103
1104#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1116{
1117 int pnum, err, vols_found = 0;
1118 struct rb_node *rb1, *rb2;
1119 struct ubi_scan_volume *sv;
1120 struct ubi_scan_leb *seb, *last_seb;
1121 uint8_t *buf;
1122
1123
1124
1125
1126 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1127 int leb_count = 0;
1128
1129 cond_resched();
1130
1131 vols_found += 1;
1132
1133 if (si->is_empty) {
1134 ubi_err("bad is_empty flag");
1135 goto bad_sv;
1136 }
1137
1138 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1139 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1140 sv->data_pad < 0 || sv->last_data_size < 0) {
1141 ubi_err("negative values");
1142 goto bad_sv;
1143 }
1144
1145 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1146 sv->vol_id < UBI_INTERNAL_VOL_START) {
1147 ubi_err("bad vol_id");
1148 goto bad_sv;
1149 }
1150
1151 if (sv->vol_id > si->highest_vol_id) {
1152 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1153 si->highest_vol_id, sv->vol_id);
1154 goto out;
1155 }
1156
1157 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1158 sv->vol_type != UBI_STATIC_VOLUME) {
1159 ubi_err("bad vol_type");
1160 goto bad_sv;
1161 }
1162
1163 if (sv->data_pad > ubi->leb_size / 2) {
1164 ubi_err("bad data_pad");
1165 goto bad_sv;
1166 }
1167
1168 last_seb = NULL;
1169 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1170 cond_resched();
1171
1172 last_seb = seb;
1173 leb_count += 1;
1174
1175 if (seb->pnum < 0 || seb->ec < 0) {
1176 ubi_err("negative values");
1177 goto bad_seb;
1178 }
1179
1180 if (seb->ec < si->min_ec) {
1181 ubi_err("bad si->min_ec (%d), %d found",
1182 si->min_ec, seb->ec);
1183 goto bad_seb;
1184 }
1185
1186 if (seb->ec > si->max_ec) {
1187 ubi_err("bad si->max_ec (%d), %d found",
1188 si->max_ec, seb->ec);
1189 goto bad_seb;
1190 }
1191
1192 if (seb->pnum >= ubi->peb_count) {
1193 ubi_err("too high PEB number %d, total PEBs %d",
1194 seb->pnum, ubi->peb_count);
1195 goto bad_seb;
1196 }
1197
1198 if (sv->vol_type == UBI_STATIC_VOLUME) {
1199 if (seb->lnum >= sv->used_ebs) {
1200 ubi_err("bad lnum or used_ebs");
1201 goto bad_seb;
1202 }
1203 } else {
1204 if (sv->used_ebs != 0) {
1205 ubi_err("non-zero used_ebs");
1206 goto bad_seb;
1207 }
1208 }
1209
1210 if (seb->lnum > sv->highest_lnum) {
1211 ubi_err("incorrect highest_lnum or lnum");
1212 goto bad_seb;
1213 }
1214 }
1215
1216 if (sv->leb_count != leb_count) {
1217 ubi_err("bad leb_count, %d objects in the tree",
1218 leb_count);
1219 goto bad_sv;
1220 }
1221
1222 if (!last_seb)
1223 continue;
1224
1225 seb = last_seb;
1226
1227 if (seb->lnum != sv->highest_lnum) {
1228 ubi_err("bad highest_lnum");
1229 goto bad_seb;
1230 }
1231 }
1232
1233 if (vols_found != si->vols_found) {
1234 ubi_err("bad si->vols_found %d, should be %d",
1235 si->vols_found, vols_found);
1236 goto out;
1237 }
1238
1239
1240 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1241 last_seb = NULL;
1242 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1243 int vol_type;
1244
1245 cond_resched();
1246
1247 last_seb = seb;
1248
1249 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1250 if (err && err != UBI_IO_BITFLIPS) {
1251 ubi_err("VID header is not OK (%d)", err);
1252 if (err > 0)
1253 err = -EIO;
1254 return err;
1255 }
1256
1257 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1258 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1259 if (sv->vol_type != vol_type) {
1260 ubi_err("bad vol_type");
1261 goto bad_vid_hdr;
1262 }
1263
1264 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
1265 ubi_err("bad sqnum %llu", seb->sqnum);
1266 goto bad_vid_hdr;
1267 }
1268
1269 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
1270 ubi_err("bad vol_id %d", sv->vol_id);
1271 goto bad_vid_hdr;
1272 }
1273
1274 if (sv->compat != vidh->compat) {
1275 ubi_err("bad compat %d", vidh->compat);
1276 goto bad_vid_hdr;
1277 }
1278
1279 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
1280 ubi_err("bad lnum %d", seb->lnum);
1281 goto bad_vid_hdr;
1282 }
1283
1284 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
1285 ubi_err("bad used_ebs %d", sv->used_ebs);
1286 goto bad_vid_hdr;
1287 }
1288
1289 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
1290 ubi_err("bad data_pad %d", sv->data_pad);
1291 goto bad_vid_hdr;
1292 }
1293
1294 if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) {
1295 ubi_err("bad leb_ver %u", seb->leb_ver);
1296 goto bad_vid_hdr;
1297 }
1298 }
1299
1300 if (!last_seb)
1301 continue;
1302
1303 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
1304 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1305 goto bad_vid_hdr;
1306 }
1307
1308 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
1309 ubi_err("bad last_data_size %d", sv->last_data_size);
1310 goto bad_vid_hdr;
1311 }
1312 }
1313
1314
1315
1316
1317
1318 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
1319 if (!buf)
1320 return -ENOMEM;
1321
1322 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1323 err = ubi_io_is_bad(ubi, pnum);
1324 if (err < 0) {
1325 kfree(buf);
1326 return err;
1327 }
1328 else if (err)
1329 buf[pnum] = 1;
1330 }
1331
1332 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1333 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1334 buf[seb->pnum] = 1;
1335
1336 list_for_each_entry(seb, &si->free, u.list)
1337 buf[seb->pnum] = 1;
1338
1339 list_for_each_entry(seb, &si->corr, u.list)
1340 buf[seb->pnum] = 1;
1341
1342 list_for_each_entry(seb, &si->erase, u.list)
1343 buf[seb->pnum] = 1;
1344
1345 list_for_each_entry(seb, &si->alien, u.list)
1346 buf[seb->pnum] = 1;
1347
1348 err = 0;
1349 for (pnum = 0; pnum < ubi->peb_count; pnum++)
1350 if (!buf[pnum]) {
1351 ubi_err("PEB %d is not referred", pnum);
1352 err = 1;
1353 }
1354
1355 kfree(buf);
1356 if (err)
1357 goto out;
1358 return 0;
1359
1360bad_seb:
1361 ubi_err("bad scanning information about LEB %d", seb->lnum);
1362 ubi_dbg_dump_seb(seb, 0);
1363 ubi_dbg_dump_sv(sv);
1364 goto out;
1365
1366bad_sv:
1367 ubi_err("bad scanning information about volume %d", sv->vol_id);
1368 ubi_dbg_dump_sv(sv);
1369 goto out;
1370
1371bad_vid_hdr:
1372 ubi_err("bad scanning information about volume %d", sv->vol_id);
1373 ubi_dbg_dump_sv(sv);
1374 ubi_dbg_dump_vid_hdr(vidh);
1375
1376out:
1377 ubi_dbg_dump_stack();
1378 return 1;
1379}
1380
1381#endif
1382