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