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