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