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#include <linux/crc32.h>
60#include <linux/err.h>
61#include <asm/div64.h>
62#include "ubi.h"
63
64#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
65static void paranoid_vtbl_check(const struct ubi_device *ubi);
66#else
67#define paranoid_vtbl_check(ubi)
68#endif
69
70
71static struct ubi_vtbl_record empty_vtbl_record;
72
73
74
75
76
77
78
79
80
81
82
83
84int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
85 struct ubi_vtbl_record *vtbl_rec)
86{
87 int i, err;
88 uint32_t crc;
89 struct ubi_volume *layout_vol;
90
91 ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
92 layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
93
94 if (!vtbl_rec)
95 vtbl_rec = &empty_vtbl_record;
96 else {
97 crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
98 vtbl_rec->crc = cpu_to_be32(crc);
99 }
100
101 memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
102 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
103 err = ubi_eba_unmap_leb(ubi, layout_vol, i);
104 if (err)
105 return err;
106
107 err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
108 ubi->vtbl_size, UBI_LONGTERM);
109 if (err)
110 return err;
111 }
112
113 paranoid_vtbl_check(ubi);
114 return 0;
115}
116
117
118
119
120
121
122
123
124
125
126int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
127 struct list_head *rename_list)
128{
129 int i, err;
130 struct ubi_rename_entry *re;
131 struct ubi_volume *layout_vol;
132
133 list_for_each_entry(re, rename_list, list) {
134 uint32_t crc;
135 struct ubi_volume *vol = re->desc->vol;
136 struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id];
137
138 if (re->remove) {
139 memcpy(vtbl_rec, &empty_vtbl_record,
140 sizeof(struct ubi_vtbl_record));
141 continue;
142 }
143
144 vtbl_rec->name_len = cpu_to_be16(re->new_name_len);
145 memcpy(vtbl_rec->name, re->new_name, re->new_name_len);
146 memset(vtbl_rec->name + re->new_name_len, 0,
147 UBI_VOL_NAME_MAX + 1 - re->new_name_len);
148 crc = crc32(UBI_CRC32_INIT, vtbl_rec,
149 UBI_VTBL_RECORD_SIZE_CRC);
150 vtbl_rec->crc = cpu_to_be32(crc);
151 }
152
153 layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
154 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
155 err = ubi_eba_unmap_leb(ubi, layout_vol, i);
156 if (err)
157 return err;
158
159 err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
160 ubi->vtbl_size, UBI_LONGTERM);
161 if (err)
162 return err;
163 }
164
165 return 0;
166}
167
168
169
170
171
172
173
174
175
176static int vtbl_check(const struct ubi_device *ubi,
177 const struct ubi_vtbl_record *vtbl)
178{
179 int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
180 int upd_marker, err;
181 uint32_t crc;
182 const char *name;
183
184 for (i = 0; i < ubi->vtbl_slots; i++) {
185 cond_resched();
186
187 reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
188 alignment = be32_to_cpu(vtbl[i].alignment);
189 data_pad = be32_to_cpu(vtbl[i].data_pad);
190 upd_marker = vtbl[i].upd_marker;
191 vol_type = vtbl[i].vol_type;
192 name_len = be16_to_cpu(vtbl[i].name_len);
193 name = &vtbl[i].name[0];
194
195 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
196 if (be32_to_cpu(vtbl[i].crc) != crc) {
197 ubi_err("bad CRC at record %u: %#08x, not %#08x",
198 i, crc, be32_to_cpu(vtbl[i].crc));
199 ubi_dbg_dump_vtbl_record(&vtbl[i], i);
200 return 1;
201 }
202
203 if (reserved_pebs == 0) {
204 if (memcmp(&vtbl[i], &empty_vtbl_record,
205 UBI_VTBL_RECORD_SIZE)) {
206 err = 2;
207 goto bad;
208 }
209 continue;
210 }
211
212 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
213 name_len < 0) {
214 err = 3;
215 goto bad;
216 }
217
218 if (alignment > ubi->leb_size || alignment == 0) {
219 err = 4;
220 goto bad;
221 }
222
223 n = alignment & (ubi->min_io_size - 1);
224 if (alignment != 1 && n) {
225 err = 5;
226 goto bad;
227 }
228
229 n = ubi->leb_size % alignment;
230 if (data_pad != n) {
231 dbg_err("bad data_pad, has to be %d", n);
232 err = 6;
233 goto bad;
234 }
235
236 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
237 err = 7;
238 goto bad;
239 }
240
241 if (upd_marker != 0 && upd_marker != 1) {
242 err = 8;
243 goto bad;
244 }
245
246 if (reserved_pebs > ubi->good_peb_count) {
247 dbg_err("too large reserved_pebs %d, good PEBs %d",
248 reserved_pebs, ubi->good_peb_count);
249 err = 9;
250 goto bad;
251 }
252
253 if (name_len > UBI_VOL_NAME_MAX) {
254 err = 10;
255 goto bad;
256 }
257
258 if (name[0] == '\0') {
259 err = 11;
260 goto bad;
261 }
262
263 if (name_len != strnlen(name, name_len + 1)) {
264 err = 12;
265 goto bad;
266 }
267 }
268
269
270 for (i = 0; i < ubi->vtbl_slots - 1; i++) {
271 for (n = i + 1; n < ubi->vtbl_slots; n++) {
272 int len1 = be16_to_cpu(vtbl[i].name_len);
273 int len2 = be16_to_cpu(vtbl[n].name_len);
274
275 if (len1 > 0 && len1 == len2 &&
276 !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
277 ubi_err("volumes %d and %d have the same name"
278 " \"%s\"", i, n, vtbl[i].name);
279 ubi_dbg_dump_vtbl_record(&vtbl[i], i);
280 ubi_dbg_dump_vtbl_record(&vtbl[n], n);
281 return -EINVAL;
282 }
283 }
284 }
285
286 return 0;
287
288bad:
289 ubi_err("volume table check failed: record %d, error %d", i, err);
290 ubi_dbg_dump_vtbl_record(&vtbl[i], i);
291 return -EINVAL;
292}
293
294
295
296
297
298
299
300
301
302
303
304static int create_vtbl(struct ubi_device *ubi, struct ubi_scan_info *si,
305 int copy, void *vtbl)
306{
307 int err, tries = 0;
308 static struct ubi_vid_hdr *vid_hdr;
309 struct ubi_scan_volume *sv;
310 struct ubi_scan_leb *new_seb, *old_seb = NULL;
311
312 ubi_msg("create volume table (copy #%d)", copy + 1);
313
314 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
315 if (!vid_hdr)
316 return -ENOMEM;
317
318
319
320
321
322
323 sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID);
324 if (sv)
325 old_seb = ubi_scan_find_seb(sv, copy);
326
327retry:
328 new_seb = ubi_scan_get_free_peb(ubi, si);
329 if (IS_ERR(new_seb)) {
330 err = PTR_ERR(new_seb);
331 goto out_free;
332 }
333
334 vid_hdr->vol_type = UBI_VID_DYNAMIC;
335 vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
336 vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
337 vid_hdr->data_size = vid_hdr->used_ebs =
338 vid_hdr->data_pad = cpu_to_be32(0);
339 vid_hdr->lnum = cpu_to_be32(copy);
340 vid_hdr->sqnum = cpu_to_be64(++si->max_sqnum);
341
342
343 err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr);
344 if (err)
345 goto write_error;
346
347
348 err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size);
349 if (err)
350 goto write_error;
351
352
353
354
355
356 err = ubi_scan_add_used(ubi, si, new_seb->pnum, new_seb->ec,
357 vid_hdr, 0);
358 kfree(new_seb);
359 ubi_free_vid_hdr(ubi, vid_hdr);
360 return err;
361
362write_error:
363 if (err == -EIO && ++tries <= 5) {
364
365
366
367
368 list_add_tail(&new_seb->u.list, &si->corr);
369 goto retry;
370 }
371 kfree(new_seb);
372out_free:
373 ubi_free_vid_hdr(ubi, vid_hdr);
374 return err;
375
376}
377
378
379
380
381
382
383
384
385
386
387
388static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
389 struct ubi_scan_info *si,
390 struct ubi_scan_volume *sv)
391{
392 int err;
393 struct rb_node *rb;
394 struct ubi_scan_leb *seb;
395 struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
396 int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423 dbg_gen("check layout volume");
424
425
426 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
427 leb[seb->lnum] = vmalloc(ubi->vtbl_size);
428 if (!leb[seb->lnum]) {
429 err = -ENOMEM;
430 goto out_free;
431 }
432 memset(leb[seb->lnum], 0, ubi->vtbl_size);
433
434 err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0,
435 ubi->vtbl_size);
436 if (err == UBI_IO_BITFLIPS || err == -EBADMSG)
437
438
439
440
441
442
443
444
445
446
447 seb->scrub = 1;
448 else if (err)
449 goto out_free;
450 }
451
452 err = -EINVAL;
453 if (leb[0]) {
454 leb_corrupted[0] = vtbl_check(ubi, leb[0]);
455 if (leb_corrupted[0] < 0)
456 goto out_free;
457 }
458
459 if (!leb_corrupted[0]) {
460
461 if (leb[1])
462 leb_corrupted[1] = memcmp(leb[0], leb[1],
463 ubi->vtbl_size);
464 if (leb_corrupted[1]) {
465 ubi_warn("volume table copy #2 is corrupted");
466 err = create_vtbl(ubi, si, 1, leb[0]);
467 if (err)
468 goto out_free;
469 ubi_msg("volume table was restored");
470 }
471
472
473 vfree(leb[1]);
474 return leb[0];
475 } else {
476
477 if (leb[1]) {
478 leb_corrupted[1] = vtbl_check(ubi, leb[1]);
479 if (leb_corrupted[1] < 0)
480 goto out_free;
481 }
482 if (leb_corrupted[1]) {
483
484 ubi_err("both volume tables are corrupted");
485 goto out_free;
486 }
487
488 ubi_warn("volume table copy #1 is corrupted");
489 err = create_vtbl(ubi, si, 0, leb[1]);
490 if (err)
491 goto out_free;
492 ubi_msg("volume table was restored");
493
494 vfree(leb[0]);
495 return leb[1];
496 }
497
498out_free:
499 vfree(leb[0]);
500 vfree(leb[1]);
501 return ERR_PTR(err);
502}
503
504
505
506
507
508
509
510
511
512static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
513 struct ubi_scan_info *si)
514{
515 int i;
516 struct ubi_vtbl_record *vtbl;
517
518 vtbl = vmalloc(ubi->vtbl_size);
519 if (!vtbl)
520 return ERR_PTR(-ENOMEM);
521 memset(vtbl, 0, ubi->vtbl_size);
522
523 for (i = 0; i < ubi->vtbl_slots; i++)
524 memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
525
526 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
527 int err;
528
529 err = create_vtbl(ubi, si, i, vtbl);
530 if (err) {
531 vfree(vtbl);
532 return ERR_PTR(err);
533 }
534 }
535
536 return vtbl;
537}
538
539
540
541
542
543
544
545
546
547
548
549static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
550 const struct ubi_vtbl_record *vtbl)
551{
552 int i, reserved_pebs = 0;
553 struct ubi_scan_volume *sv;
554 struct ubi_volume *vol;
555
556 for (i = 0; i < ubi->vtbl_slots; i++) {
557 cond_resched();
558
559 if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
560 continue;
561
562 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
563 if (!vol)
564 return -ENOMEM;
565
566 vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
567 vol->alignment = be32_to_cpu(vtbl[i].alignment);
568 vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
569 vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
570 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
571 vol->name_len = be16_to_cpu(vtbl[i].name_len);
572 vol->usable_leb_size = ubi->leb_size - vol->data_pad;
573 memcpy(vol->name, vtbl[i].name, vol->name_len);
574 vol->name[vol->name_len] = '\0';
575 vol->vol_id = i;
576
577 if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
578
579 if (ubi->autoresize_vol_id != -1) {
580 ubi_err("more than one auto-resize volume (%d "
581 "and %d)", ubi->autoresize_vol_id, i);
582 kfree(vol);
583 return -EINVAL;
584 }
585
586 ubi->autoresize_vol_id = i;
587 }
588
589 ubi_assert(!ubi->volumes[i]);
590 ubi->volumes[i] = vol;
591 ubi->vol_count += 1;
592 vol->ubi = ubi;
593 reserved_pebs += vol->reserved_pebs;
594
595
596
597
598
599 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
600 vol->used_ebs = vol->reserved_pebs;
601 vol->last_eb_bytes = vol->usable_leb_size;
602 vol->used_bytes =
603 (long long)vol->used_ebs * vol->usable_leb_size;
604 continue;
605 }
606
607
608 sv = ubi_scan_find_sv(si, i);
609 if (!sv) {
610
611
612
613
614
615
616
617
618 continue;
619 }
620
621 if (sv->leb_count != sv->used_ebs) {
622
623
624
625
626 ubi_warn("static volume %d misses %d LEBs - corrupted",
627 sv->vol_id, sv->used_ebs - sv->leb_count);
628 vol->corrupted = 1;
629 continue;
630 }
631
632 vol->used_ebs = sv->used_ebs;
633 vol->used_bytes =
634 (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
635 vol->used_bytes += sv->last_data_size;
636 vol->last_eb_bytes = sv->last_data_size;
637 }
638
639
640 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
641 if (!vol)
642 return -ENOMEM;
643
644 vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
645 vol->alignment = 1;
646 vol->vol_type = UBI_DYNAMIC_VOLUME;
647 vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
648 memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
649 vol->usable_leb_size = ubi->leb_size;
650 vol->used_ebs = vol->reserved_pebs;
651 vol->last_eb_bytes = vol->reserved_pebs;
652 vol->used_bytes =
653 (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
654 vol->vol_id = UBI_LAYOUT_VOLUME_ID;
655 vol->ref_count = 1;
656
657 ubi_assert(!ubi->volumes[i]);
658 ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
659 reserved_pebs += vol->reserved_pebs;
660 ubi->vol_count += 1;
661 vol->ubi = ubi;
662
663 if (reserved_pebs > ubi->avail_pebs)
664 ubi_err("not enough PEBs, required %d, available %d",
665 reserved_pebs, ubi->avail_pebs);
666 ubi->rsvd_pebs += reserved_pebs;
667 ubi->avail_pebs -= reserved_pebs;
668
669 return 0;
670}
671
672
673
674
675
676
677
678
679
680static int check_sv(const struct ubi_volume *vol,
681 const struct ubi_scan_volume *sv)
682{
683 int err;
684
685 if (sv->highest_lnum >= vol->reserved_pebs) {
686 err = 1;
687 goto bad;
688 }
689 if (sv->leb_count > vol->reserved_pebs) {
690 err = 2;
691 goto bad;
692 }
693 if (sv->vol_type != vol->vol_type) {
694 err = 3;
695 goto bad;
696 }
697 if (sv->used_ebs > vol->reserved_pebs) {
698 err = 4;
699 goto bad;
700 }
701 if (sv->data_pad != vol->data_pad) {
702 err = 5;
703 goto bad;
704 }
705 return 0;
706
707bad:
708 ubi_err("bad scanning information, error %d", err);
709 ubi_dbg_dump_sv(sv);
710 ubi_dbg_dump_vol_info(vol);
711 return -EINVAL;
712}
713
714
715
716
717
718
719
720
721
722
723
724static int check_scanning_info(const struct ubi_device *ubi,
725 struct ubi_scan_info *si)
726{
727 int err, i;
728 struct ubi_scan_volume *sv;
729 struct ubi_volume *vol;
730
731 if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
732 ubi_err("scanning found %d volumes, maximum is %d + %d",
733 si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
734 return -EINVAL;
735 }
736
737 if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
738 si->highest_vol_id < UBI_INTERNAL_VOL_START) {
739 ubi_err("too large volume ID %d found by scanning",
740 si->highest_vol_id);
741 return -EINVAL;
742 }
743
744 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
745 cond_resched();
746
747 sv = ubi_scan_find_sv(si, i);
748 vol = ubi->volumes[i];
749 if (!vol) {
750 if (sv)
751 ubi_scan_rm_volume(si, sv);
752 continue;
753 }
754
755 if (vol->reserved_pebs == 0) {
756 ubi_assert(i < ubi->vtbl_slots);
757
758 if (!sv)
759 continue;
760
761
762
763
764
765
766
767
768 ubi_msg("finish volume %d removal", sv->vol_id);
769 ubi_scan_rm_volume(si, sv);
770 } else if (sv) {
771 err = check_sv(vol, sv);
772 if (err)
773 return err;
774 }
775 }
776
777 return 0;
778}
779
780
781
782
783
784
785
786
787
788
789int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
790{
791 int i, err;
792 struct ubi_scan_volume *sv;
793
794 empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
795
796
797
798
799
800 ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
801 if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
802 ubi->vtbl_slots = UBI_MAX_VOLUMES;
803
804 ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
805 ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
806
807 sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID);
808 if (!sv) {
809
810
811
812
813
814
815
816
817 if (si->is_empty) {
818 ubi->vtbl = create_empty_lvol(ubi, si);
819 if (IS_ERR(ubi->vtbl))
820 return PTR_ERR(ubi->vtbl);
821 } else {
822 ubi_err("the layout volume was not found");
823 return -EINVAL;
824 }
825 } else {
826 if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) {
827
828 dbg_err("too many LEBs (%d) in layout volume",
829 sv->leb_count);
830 return -EINVAL;
831 }
832
833 ubi->vtbl = process_lvol(ubi, si, sv);
834 if (IS_ERR(ubi->vtbl))
835 return PTR_ERR(ubi->vtbl);
836 }
837
838 ubi->avail_pebs = ubi->good_peb_count;
839
840
841
842
843
844 err = init_volumes(ubi, si, ubi->vtbl);
845 if (err)
846 goto out_free;
847
848
849
850
851
852 err = check_scanning_info(ubi, si);
853 if (err)
854 goto out_free;
855
856 return 0;
857
858out_free:
859 vfree(ubi->vtbl);
860 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
861 kfree(ubi->volumes[i]);
862 ubi->volumes[i] = NULL;
863 }
864 return err;
865}
866
867#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
868
869
870
871
872
873static void paranoid_vtbl_check(const struct ubi_device *ubi)
874{
875 if (vtbl_check(ubi, ubi->vtbl)) {
876 ubi_err("paranoid check failed");
877 BUG();
878 }
879}
880
881#endif
882