blob: 83236c31c89232a0a944558271681da7bfd31064 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 * Copyright (c) Nokia Corporation, 2006, 2007
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Author: Artem Bityutskiy (Битюцкий Артём)
20 */
21
22/*
23 * This file includes volume table manipulation code. The volume table is an
24 * on-flash table containing volume meta-data like name, number of reserved
25 * physical eraseblocks, type, etc. The volume table is stored in the so-called
26 * "layout volume".
27 *
28 * The layout volume is an internal volume which is organized as follows. It
29 * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical
30 * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each
31 * other. This redundancy guarantees robustness to unclean reboots. The volume
32 * table is basically an array of volume table records. Each record contains
33 * full information about the volume and protected by a CRC checksum.
34 *
35 * The volume table is changed, it is first changed in RAM. Then LEB 0 is
36 * erased, and the updated volume table is written back to LEB 0. Then same for
37 * LEB 1. This scheme guarantees recoverability from unclean reboots.
38 *
39 * In this UBI implementation the on-flash volume table does not contain any
40 * information about how many data static volumes contain. This information may
41 * be found from the scanning data.
42 *
43 * But it would still be beneficial to store this information in the volume
44 * table. For example, suppose we have a static volume X, and all its physical
45 * eraseblocks became bad for some reasons. Suppose we are attaching the
46 * corresponding MTD device, the scanning has found no logical eraseblocks
47 * corresponding to the volume X. According to the volume table volume X does
48 * exist. So we don't know whether it is just empty or all its physical
49 * eraseblocks went bad. So we cannot alarm the user about this corruption.
50 *
51 * The volume table also stores so-called "update marker", which is used for
52 * volume updates. Before updating the volume, the update marker is set, and
53 * after the update operation is finished, the update marker is cleared. So if
54 * the update operation was interrupted (e.g. by an unclean reboot) - the
55 * update marker is still there and we know that the volume's contents is
56 * damaged.
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/* Empty volume table record */
71static struct ubi_vtbl_record empty_vtbl_record;
72
73/**
74 * ubi_change_vtbl_record - change volume table record.
75 * @ubi: UBI device description object
76 * @idx: table index to change
77 * @vtbl_rec: new volume table record
78 *
79 * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
80 * volume table record is written. The caller does not have to calculate CRC of
81 * the record as it is done by this function. Returns zero in case of success
82 * and a negative error code in case of failure.
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
90 ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
91
92 if (!vtbl_rec)
93 vtbl_rec = &empty_vtbl_record;
94 else {
95 crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
96 vtbl_rec->crc = cpu_to_ubi32(crc);
97 }
98
99 dbg_msg("change record %d", idx);
100 ubi_dbg_dump_vtbl_record(vtbl_rec, idx);
101
102 mutex_lock(&ubi->vtbl_mutex);
103 memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
104 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
105 err = ubi_eba_unmap_leb(ubi, UBI_LAYOUT_VOL_ID, i);
106 if (err) {
107 mutex_unlock(&ubi->vtbl_mutex);
108 return err;
109 }
110 err = ubi_eba_write_leb(ubi, UBI_LAYOUT_VOL_ID, i, ubi->vtbl, 0,
111 ubi->vtbl_size, UBI_LONGTERM);
112 if (err) {
113 mutex_unlock(&ubi->vtbl_mutex);
114 return err;
115 }
116 }
117
118 paranoid_vtbl_check(ubi);
119 mutex_unlock(&ubi->vtbl_mutex);
120 return ubi_wl_flush(ubi);
121}
122
123/**
124 * vol_til_check - check if volume table is not corrupted and contains sensible
125 * data.
126 *
127 * @ubi: UBI device description object
128 * @vtbl: volume table
129 *
130 * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
131 * and %-EINVAL if it contains inconsistent data.
132 */
133static int vtbl_check(const struct ubi_device *ubi,
134 const struct ubi_vtbl_record *vtbl)
135{
136 int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
137 int upd_marker;
138 uint32_t crc;
139 const char *name;
140
141 for (i = 0; i < ubi->vtbl_slots; i++) {
142 cond_resched();
143
144 reserved_pebs = ubi32_to_cpu(vtbl[i].reserved_pebs);
145 alignment = ubi32_to_cpu(vtbl[i].alignment);
146 data_pad = ubi32_to_cpu(vtbl[i].data_pad);
147 upd_marker = vtbl[i].upd_marker;
148 vol_type = vtbl[i].vol_type;
149 name_len = ubi16_to_cpu(vtbl[i].name_len);
150 name = &vtbl[i].name[0];
151
152 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
153 if (ubi32_to_cpu(vtbl[i].crc) != crc) {
154 ubi_err("bad CRC at record %u: %#08x, not %#08x",
155 i, crc, ubi32_to_cpu(vtbl[i].crc));
156 ubi_dbg_dump_vtbl_record(&vtbl[i], i);
157 return 1;
158 }
159
160 if (reserved_pebs == 0) {
161 if (memcmp(&vtbl[i], &empty_vtbl_record,
162 UBI_VTBL_RECORD_SIZE)) {
163 dbg_err("bad empty record");
164 goto bad;
165 }
166 continue;
167 }
168
169 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
170 name_len < 0) {
171 dbg_err("negative values");
172 goto bad;
173 }
174
175 if (alignment > ubi->leb_size || alignment == 0) {
176 dbg_err("bad alignment");
177 goto bad;
178 }
179
180 n = alignment % ubi->min_io_size;
181 if (alignment != 1 && n) {
182 dbg_err("alignment is not multiple of min I/O unit");
183 goto bad;
184 }
185
186 n = ubi->leb_size % alignment;
187 if (data_pad != n) {
188 dbg_err("bad data_pad, has to be %d", n);
189 goto bad;
190 }
191
192 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
193 dbg_err("bad vol_type");
194 goto bad;
195 }
196
197 if (upd_marker != 0 && upd_marker != 1) {
198 dbg_err("bad upd_marker");
199 goto bad;
200 }
201
202 if (reserved_pebs > ubi->good_peb_count) {
203 dbg_err("too large reserved_pebs, good PEBs %d",
204 ubi->good_peb_count);
205 goto bad;
206 }
207
208 if (name_len > UBI_VOL_NAME_MAX) {
209 dbg_err("too long volume name, max %d",
210 UBI_VOL_NAME_MAX);
211 goto bad;
212 }
213
214 if (name[0] == '\0') {
215 dbg_err("NULL volume name");
216 goto bad;
217 }
218
219 if (name_len != strnlen(name, name_len + 1)) {
220 dbg_err("bad name_len");
221 goto bad;
222 }
223 }
224
225 /* Checks that all names are unique */
226 for (i = 0; i < ubi->vtbl_slots - 1; i++) {
227 for (n = i + 1; n < ubi->vtbl_slots; n++) {
228 int len1 = ubi16_to_cpu(vtbl[i].name_len);
229 int len2 = ubi16_to_cpu(vtbl[n].name_len);
230
231 if (len1 > 0 && len1 == len2 &&
232 !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
233 ubi_err("volumes %d and %d have the same name"
234 " \"%s\"", i, n, vtbl[i].name);
235 ubi_dbg_dump_vtbl_record(&vtbl[i], i);
236 ubi_dbg_dump_vtbl_record(&vtbl[n], n);
237 return -EINVAL;
238 }
239 }
240 }
241
242 return 0;
243
244bad:
245 ubi_err("volume table check failed, record %d", i);
246 ubi_dbg_dump_vtbl_record(&vtbl[i], i);
247 return -EINVAL;
248}
249
250/**
251 * create_vtbl - create a copy of volume table.
252 * @ubi: UBI device description object
253 * @si: scanning information
254 * @copy: number of the volume table copy
255 * @vtbl: contents of the volume table
256 *
257 * This function returns zero in case of success and a negative error code in
258 * case of failure.
259 */
260static int create_vtbl(const struct ubi_device *ubi, struct ubi_scan_info *si,
261 int copy, void *vtbl)
262{
263 int err, tries = 0;
264 static struct ubi_vid_hdr *vid_hdr;
265 struct ubi_scan_volume *sv;
266 struct ubi_scan_leb *new_seb, *old_seb = NULL;
267
268 ubi_msg("create volume table (copy #%d)", copy + 1);
269
270 vid_hdr = ubi_zalloc_vid_hdr(ubi);
271 if (!vid_hdr)
272 return -ENOMEM;
273
274 /*
275 * Check if there is a logical eraseblock which would have to contain
276 * this volume table copy was found during scanning. It has to be wiped
277 * out.
278 */
279 sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOL_ID);
280 if (sv)
281 old_seb = ubi_scan_find_seb(sv, copy);
282
283retry:
284 new_seb = ubi_scan_get_free_peb(ubi, si);
285 if (IS_ERR(new_seb)) {
286 err = PTR_ERR(new_seb);
287 goto out_free;
288 }
289
290 vid_hdr->vol_type = UBI_VID_DYNAMIC;
291 vid_hdr->vol_id = cpu_to_ubi32(UBI_LAYOUT_VOL_ID);
292 vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
293 vid_hdr->data_size = vid_hdr->used_ebs =
294 vid_hdr->data_pad = cpu_to_ubi32(0);
295 vid_hdr->lnum = cpu_to_ubi32(copy);
296 vid_hdr->sqnum = cpu_to_ubi64(++si->max_sqnum);
297 vid_hdr->leb_ver = cpu_to_ubi32(old_seb ? old_seb->leb_ver + 1: 0);
298
299 /* The EC header is already there, write the VID header */
300 err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr);
301 if (err)
302 goto write_error;
303
304 /* Write the layout volume contents */
305 err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size);
306 if (err)
307 goto write_error;
308
309 /*
310 * And add it to the scanning information. Don't delete the old
311 * @old_seb as it will be deleted and freed in 'ubi_scan_add_used()'.
312 */
313 err = ubi_scan_add_used(ubi, si, new_seb->pnum, new_seb->ec,
314 vid_hdr, 0);
315 kfree(new_seb);
316 ubi_free_vid_hdr(ubi, vid_hdr);
317 return err;
318
319write_error:
Florin Malitac4e90ec2007-05-03 11:49:57 -0400320 /* Maybe this physical eraseblock went bad, try to pick another one */
321 if (++tries <= 5)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400322 err = ubi_scan_add_to_list(si, new_seb->pnum, new_seb->ec,
323 &si->corr);
Florin Malitac4e90ec2007-05-03 11:49:57 -0400324 kfree(new_seb);
325 if (!err)
326 goto retry;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400327out_free:
328 ubi_free_vid_hdr(ubi, vid_hdr);
329 return err;
330
331}
332
333/**
334 * process_lvol - process the layout volume.
335 * @ubi: UBI device description object
336 * @si: scanning information
337 * @sv: layout volume scanning information
338 *
339 * This function is responsible for reading the layout volume, ensuring it is
340 * not corrupted, and recovering from corruptions if needed. Returns volume
341 * table in case of success and a negative error code in case of failure.
342 */
343static struct ubi_vtbl_record *process_lvol(const struct ubi_device *ubi,
344 struct ubi_scan_info *si,
345 struct ubi_scan_volume *sv)
346{
347 int err;
348 struct rb_node *rb;
349 struct ubi_scan_leb *seb;
350 struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
351 int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
352
353 /*
354 * UBI goes through the following steps when it changes the layout
355 * volume:
356 * a. erase LEB 0;
357 * b. write new data to LEB 0;
358 * c. erase LEB 1;
359 * d. write new data to LEB 1.
360 *
361 * Before the change, both LEBs contain the same data.
362 *
363 * Due to unclean reboots, the contents of LEB 0 may be lost, but there
364 * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
365 * Similarly, LEB 1 may be lost, but there should be LEB 0. And
366 * finally, unclean reboots may result in a situation when neither LEB
367 * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
368 * 0 contains more recent information.
369 *
370 * So the plan is to first check LEB 0. Then
371 * a. if LEB 0 is OK, it must be containing the most resent data; then
372 * we compare it with LEB 1, and if they are different, we copy LEB
373 * 0 to LEB 1;
374 * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
375 * to LEB 0.
376 */
377
378 dbg_msg("check layout volume");
379
380 /* Read both LEB 0 and LEB 1 into memory */
381 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
382 leb[seb->lnum] = kzalloc(ubi->vtbl_size, GFP_KERNEL);
383 if (!leb[seb->lnum]) {
384 err = -ENOMEM;
385 goto out_free;
386 }
387
388 err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0,
389 ubi->vtbl_size);
390 if (err == UBI_IO_BITFLIPS || err == -EBADMSG)
391 /* Scrub the PEB later */
392 seb->scrub = 1;
393 else if (err)
394 goto out_free;
395 }
396
397 err = -EINVAL;
398 if (leb[0]) {
399 leb_corrupted[0] = vtbl_check(ubi, leb[0]);
400 if (leb_corrupted[0] < 0)
401 goto out_free;
402 }
403
404 if (!leb_corrupted[0]) {
405 /* LEB 0 is OK */
406 if (leb[1])
407 leb_corrupted[1] = memcmp(leb[0], leb[1], ubi->vtbl_size);
408 if (leb_corrupted[1]) {
409 ubi_warn("volume table copy #2 is corrupted");
410 err = create_vtbl(ubi, si, 1, leb[0]);
411 if (err)
412 goto out_free;
413 ubi_msg("volume table was restored");
414 }
415
416 /* Both LEB 1 and LEB 2 are OK and consistent */
417 kfree(leb[1]);
418 return leb[0];
419 } else {
420 /* LEB 0 is corrupted or does not exist */
421 if (leb[1]) {
422 leb_corrupted[1] = vtbl_check(ubi, leb[1]);
423 if (leb_corrupted[1] < 0)
424 goto out_free;
425 }
426 if (leb_corrupted[1]) {
427 /* Both LEB 0 and LEB 1 are corrupted */
428 ubi_err("both volume tables are corrupted");
429 goto out_free;
430 }
431
432 ubi_warn("volume table copy #1 is corrupted");
433 err = create_vtbl(ubi, si, 0, leb[1]);
434 if (err)
435 goto out_free;
436 ubi_msg("volume table was restored");
437
438 kfree(leb[0]);
439 return leb[1];
440 }
441
442out_free:
443 kfree(leb[0]);
444 kfree(leb[1]);
445 return ERR_PTR(err);
446}
447
448/**
449 * create_empty_lvol - create empty layout volume.
450 * @ubi: UBI device description object
451 * @si: scanning information
452 *
453 * This function returns volume table contents in case of success and a
454 * negative error code in case of failure.
455 */
456static struct ubi_vtbl_record *create_empty_lvol(const struct ubi_device *ubi,
457 struct ubi_scan_info *si)
458{
459 int i;
460 struct ubi_vtbl_record *vtbl;
461
462 vtbl = kzalloc(ubi->vtbl_size, GFP_KERNEL);
463 if (!vtbl)
464 return ERR_PTR(-ENOMEM);
465
466 for (i = 0; i < ubi->vtbl_slots; i++)
467 memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
468
469 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
470 int err;
471
472 err = create_vtbl(ubi, si, i, vtbl);
473 if (err) {
474 kfree(vtbl);
475 return ERR_PTR(err);
476 }
477 }
478
479 return vtbl;
480}
481
482/**
483 * init_volumes - initialize volume information for existing volumes.
484 * @ubi: UBI device description object
485 * @si: scanning information
486 * @vtbl: volume table
487 *
488 * This function allocates volume description objects for existing volumes.
489 * Returns zero in case of success and a negative error code in case of
490 * failure.
491 */
492static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
493 const struct ubi_vtbl_record *vtbl)
494{
495 int i, reserved_pebs = 0;
496 struct ubi_scan_volume *sv;
497 struct ubi_volume *vol;
498
499 for (i = 0; i < ubi->vtbl_slots; i++) {
500 cond_resched();
501
502 if (ubi32_to_cpu(vtbl[i].reserved_pebs) == 0)
503 continue; /* Empty record */
504
505 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
506 if (!vol)
507 return -ENOMEM;
508
509 vol->reserved_pebs = ubi32_to_cpu(vtbl[i].reserved_pebs);
510 vol->alignment = ubi32_to_cpu(vtbl[i].alignment);
511 vol->data_pad = ubi32_to_cpu(vtbl[i].data_pad);
512 vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
513 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
514 vol->name_len = ubi16_to_cpu(vtbl[i].name_len);
515 vol->usable_leb_size = ubi->leb_size - vol->data_pad;
516 memcpy(vol->name, vtbl[i].name, vol->name_len);
517 vol->name[vol->name_len] = '\0';
518 vol->vol_id = i;
519
520 ubi_assert(!ubi->volumes[i]);
521 ubi->volumes[i] = vol;
522 ubi->vol_count += 1;
523 vol->ubi = ubi;
524 reserved_pebs += vol->reserved_pebs;
525
526 /*
527 * In case of dynamic volume UBI knows nothing about how many
528 * data is stored there. So assume the whole volume is used.
529 */
530 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
531 vol->used_ebs = vol->reserved_pebs;
532 vol->last_eb_bytes = vol->usable_leb_size;
533 vol->used_bytes = vol->used_ebs * vol->usable_leb_size;
534 continue;
535 }
536
537 /* Static volumes only */
538 sv = ubi_scan_find_sv(si, i);
539 if (!sv) {
540 /*
541 * No eraseblocks belonging to this volume found. We
542 * don't actually know whether this static volume is
543 * completely corrupted or just contains no data. And
544 * we cannot know this as long as data size is not
545 * stored on flash. So we just assume the volume is
546 * empty. FIXME: this should be handled.
547 */
548 continue;
549 }
550
551 if (sv->leb_count != sv->used_ebs) {
552 /*
553 * We found a static volume which misses several
554 * eraseblocks. Treat it as corrupted.
555 */
556 ubi_warn("static volume %d misses %d LEBs - corrupted",
557 sv->vol_id, sv->used_ebs - sv->leb_count);
558 vol->corrupted = 1;
559 continue;
560 }
561
562 vol->used_ebs = sv->used_ebs;
563 vol->used_bytes = (vol->used_ebs - 1) * vol->usable_leb_size;
564 vol->used_bytes += sv->last_data_size;
565 vol->last_eb_bytes = sv->last_data_size;
566 }
567
568 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
569 if (!vol)
570 return -ENOMEM;
571
572 vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
573 vol->alignment = 1;
574 vol->vol_type = UBI_DYNAMIC_VOLUME;
575 vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
576 memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
577 vol->usable_leb_size = ubi->leb_size;
578 vol->used_ebs = vol->reserved_pebs;
579 vol->last_eb_bytes = vol->reserved_pebs;
580 vol->used_bytes = vol->used_ebs * (ubi->leb_size - vol->data_pad);
581 vol->vol_id = UBI_LAYOUT_VOL_ID;
582
583 ubi_assert(!ubi->volumes[i]);
584 ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
585 reserved_pebs += vol->reserved_pebs;
586 ubi->vol_count += 1;
587 vol->ubi = ubi;
588
589 if (reserved_pebs > ubi->avail_pebs)
590 ubi_err("not enough PEBs, required %d, available %d",
591 reserved_pebs, ubi->avail_pebs);
592 ubi->rsvd_pebs += reserved_pebs;
593 ubi->avail_pebs -= reserved_pebs;
594
595 return 0;
596}
597
598/**
599 * check_sv - check volume scanning information.
600 * @vol: UBI volume description object
601 * @sv: volume scanning information
602 *
603 * This function returns zero if the volume scanning information is consistent
604 * to the data read from the volume tabla, and %-EINVAL if not.
605 */
606static int check_sv(const struct ubi_volume *vol,
607 const struct ubi_scan_volume *sv)
608{
609 if (sv->highest_lnum >= vol->reserved_pebs) {
610 dbg_err("bad highest_lnum");
611 goto bad;
612 }
613 if (sv->leb_count > vol->reserved_pebs) {
614 dbg_err("bad leb_count");
615 goto bad;
616 }
617 if (sv->vol_type != vol->vol_type) {
618 dbg_err("bad vol_type");
619 goto bad;
620 }
621 if (sv->used_ebs > vol->reserved_pebs) {
622 dbg_err("bad used_ebs");
623 goto bad;
624 }
625 if (sv->data_pad != vol->data_pad) {
626 dbg_err("bad data_pad");
627 goto bad;
628 }
629 return 0;
630
631bad:
632 ubi_err("bad scanning information");
633 ubi_dbg_dump_sv(sv);
634 ubi_dbg_dump_vol_info(vol);
635 return -EINVAL;
636}
637
638/**
639 * check_scanning_info - check that scanning information.
640 * @ubi: UBI device description object
641 * @si: scanning information
642 *
643 * Even though we protect on-flash data by CRC checksums, we still don't trust
644 * the media. This function ensures that scanning information is consistent to
645 * the information read from the volume table. Returns zero if the scanning
646 * information is OK and %-EINVAL if it is not.
647 */
648static int check_scanning_info(const struct ubi_device *ubi,
649 struct ubi_scan_info *si)
650{
651 int err, i;
652 struct ubi_scan_volume *sv;
653 struct ubi_volume *vol;
654
655 if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
656 ubi_err("scanning found %d volumes, maximum is %d + %d",
657 si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
658 return -EINVAL;
659 }
660
661 if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT&&
662 si->highest_vol_id < UBI_INTERNAL_VOL_START) {
663 ubi_err("too large volume ID %d found by scanning",
664 si->highest_vol_id);
665 return -EINVAL;
666 }
667
668
669 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
670 cond_resched();
671
672 sv = ubi_scan_find_sv(si, i);
673 vol = ubi->volumes[i];
674 if (!vol) {
675 if (sv)
676 ubi_scan_rm_volume(si, sv);
677 continue;
678 }
679
680 if (vol->reserved_pebs == 0) {
681 ubi_assert(i < ubi->vtbl_slots);
682
683 if (!sv)
684 continue;
685
686 /*
687 * During scanning we found a volume which does not
688 * exist according to the information in the volume
689 * table. This must have happened due to an unclean
690 * reboot while the volume was being removed. Discard
691 * these eraseblocks.
692 */
693 ubi_msg("finish volume %d removal", sv->vol_id);
694 ubi_scan_rm_volume(si, sv);
695 } else if (sv) {
696 err = check_sv(vol, sv);
697 if (err)
698 return err;
699 }
700 }
701
702 return 0;
703}
704
705/**
706 * ubi_read_volume_table - read volume table.
707 * information.
708 * @ubi: UBI device description object
709 * @si: scanning information
710 *
711 * This function reads volume table, checks it, recover from errors if needed,
712 * or creates it if needed. Returns zero in case of success and a negative
713 * error code in case of failure.
714 */
715int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
716{
717 int i, err;
718 struct ubi_scan_volume *sv;
719
720 empty_vtbl_record.crc = cpu_to_ubi32(0xf116c36b);
721
722 /*
723 * The number of supported volumes is limited by the eraseblock size
724 * and by the UBI_MAX_VOLUMES constant.
725 */
726 ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
727 if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
728 ubi->vtbl_slots = UBI_MAX_VOLUMES;
729
730 ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
731 ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
732
733 sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOL_ID);
734 if (!sv) {
735 /*
736 * No logical eraseblocks belonging to the layout volume were
737 * found. This could mean that the flash is just empty. In
738 * this case we create empty layout volume.
739 *
740 * But if flash is not empty this must be a corruption or the
741 * MTD device just contains garbage.
742 */
743 if (si->is_empty) {
744 ubi->vtbl = create_empty_lvol(ubi, si);
745 if (IS_ERR(ubi->vtbl))
746 return PTR_ERR(ubi->vtbl);
747 } else {
748 ubi_err("the layout volume was not found");
749 return -EINVAL;
750 }
751 } else {
752 if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) {
753 /* This must not happen with proper UBI images */
754 dbg_err("too many LEBs (%d) in layout volume",
755 sv->leb_count);
756 return -EINVAL;
757 }
758
759 ubi->vtbl = process_lvol(ubi, si, sv);
760 if (IS_ERR(ubi->vtbl))
761 return PTR_ERR(ubi->vtbl);
762 }
763
764 ubi->avail_pebs = ubi->good_peb_count;
765
766 /*
767 * The layout volume is OK, initialize the corresponding in-RAM data
768 * structures.
769 */
770 err = init_volumes(ubi, si, ubi->vtbl);
771 if (err)
772 goto out_free;
773
774 /*
775 * Get sure that the scanning information is consistent to the
776 * information stored in the volume table.
777 */
778 err = check_scanning_info(ubi, si);
779 if (err)
780 goto out_free;
781
782 return 0;
783
784out_free:
785 kfree(ubi->vtbl);
786 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++)
787 if (ubi->volumes[i]) {
788 kfree(ubi->volumes[i]);
789 ubi->volumes[i] = NULL;
790 }
791 return err;
792}
793
794#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
795
796/**
797 * paranoid_vtbl_check - check volume table.
798 * @ubi: UBI device description object
799 */
800static void paranoid_vtbl_check(const struct ubi_device *ubi)
801{
802 if (vtbl_check(ubi, ubi->vtbl)) {
803 ubi_err("paranoid check failed");
804 BUG();
805 }
806}
807
808#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */