blob: 9926f1f9aad8d30bedb790be5fbf92d4aebab620 [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:
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300320 if (err == -EIO && ++tries <= 5) {
321 /*
322 * Probably this physical eraseblock went bad, try to pick
323 * another one.
324 */
325 list_add_tail(&new_seb->u.list, &si->corr);
Florin Malitac4e90ec2007-05-03 11:49:57 -0400326 goto retry;
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300327 }
328 kfree(new_seb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400329out_free:
330 ubi_free_vid_hdr(ubi, vid_hdr);
331 return err;
332
333}
334
335/**
336 * process_lvol - process the layout volume.
337 * @ubi: UBI device description object
338 * @si: scanning information
339 * @sv: layout volume scanning information
340 *
341 * This function is responsible for reading the layout volume, ensuring it is
342 * not corrupted, and recovering from corruptions if needed. Returns volume
343 * table in case of success and a negative error code in case of failure.
344 */
345static struct ubi_vtbl_record *process_lvol(const struct ubi_device *ubi,
346 struct ubi_scan_info *si,
347 struct ubi_scan_volume *sv)
348{
349 int err;
350 struct rb_node *rb;
351 struct ubi_scan_leb *seb;
352 struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
353 int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
354
355 /*
356 * UBI goes through the following steps when it changes the layout
357 * volume:
358 * a. erase LEB 0;
359 * b. write new data to LEB 0;
360 * c. erase LEB 1;
361 * d. write new data to LEB 1.
362 *
363 * Before the change, both LEBs contain the same data.
364 *
365 * Due to unclean reboots, the contents of LEB 0 may be lost, but there
366 * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
367 * Similarly, LEB 1 may be lost, but there should be LEB 0. And
368 * finally, unclean reboots may result in a situation when neither LEB
369 * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
370 * 0 contains more recent information.
371 *
372 * So the plan is to first check LEB 0. Then
373 * a. if LEB 0 is OK, it must be containing the most resent data; then
374 * we compare it with LEB 1, and if they are different, we copy LEB
375 * 0 to LEB 1;
376 * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
377 * to LEB 0.
378 */
379
380 dbg_msg("check layout volume");
381
382 /* Read both LEB 0 and LEB 1 into memory */
383 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
384 leb[seb->lnum] = kzalloc(ubi->vtbl_size, GFP_KERNEL);
385 if (!leb[seb->lnum]) {
386 err = -ENOMEM;
387 goto out_free;
388 }
389
390 err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0,
391 ubi->vtbl_size);
392 if (err == UBI_IO_BITFLIPS || err == -EBADMSG)
393 /* Scrub the PEB later */
394 seb->scrub = 1;
395 else if (err)
396 goto out_free;
397 }
398
399 err = -EINVAL;
400 if (leb[0]) {
401 leb_corrupted[0] = vtbl_check(ubi, leb[0]);
402 if (leb_corrupted[0] < 0)
403 goto out_free;
404 }
405
406 if (!leb_corrupted[0]) {
407 /* LEB 0 is OK */
408 if (leb[1])
409 leb_corrupted[1] = memcmp(leb[0], leb[1], ubi->vtbl_size);
410 if (leb_corrupted[1]) {
411 ubi_warn("volume table copy #2 is corrupted");
412 err = create_vtbl(ubi, si, 1, leb[0]);
413 if (err)
414 goto out_free;
415 ubi_msg("volume table was restored");
416 }
417
418 /* Both LEB 1 and LEB 2 are OK and consistent */
419 kfree(leb[1]);
420 return leb[0];
421 } else {
422 /* LEB 0 is corrupted or does not exist */
423 if (leb[1]) {
424 leb_corrupted[1] = vtbl_check(ubi, leb[1]);
425 if (leb_corrupted[1] < 0)
426 goto out_free;
427 }
428 if (leb_corrupted[1]) {
429 /* Both LEB 0 and LEB 1 are corrupted */
430 ubi_err("both volume tables are corrupted");
431 goto out_free;
432 }
433
434 ubi_warn("volume table copy #1 is corrupted");
435 err = create_vtbl(ubi, si, 0, leb[1]);
436 if (err)
437 goto out_free;
438 ubi_msg("volume table was restored");
439
440 kfree(leb[0]);
441 return leb[1];
442 }
443
444out_free:
445 kfree(leb[0]);
446 kfree(leb[1]);
447 return ERR_PTR(err);
448}
449
450/**
451 * create_empty_lvol - create empty layout volume.
452 * @ubi: UBI device description object
453 * @si: scanning information
454 *
455 * This function returns volume table contents in case of success and a
456 * negative error code in case of failure.
457 */
458static struct ubi_vtbl_record *create_empty_lvol(const struct ubi_device *ubi,
459 struct ubi_scan_info *si)
460{
461 int i;
462 struct ubi_vtbl_record *vtbl;
463
464 vtbl = kzalloc(ubi->vtbl_size, GFP_KERNEL);
465 if (!vtbl)
466 return ERR_PTR(-ENOMEM);
467
468 for (i = 0; i < ubi->vtbl_slots; i++)
469 memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
470
471 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
472 int err;
473
474 err = create_vtbl(ubi, si, i, vtbl);
475 if (err) {
476 kfree(vtbl);
477 return ERR_PTR(err);
478 }
479 }
480
481 return vtbl;
482}
483
484/**
485 * init_volumes - initialize volume information for existing volumes.
486 * @ubi: UBI device description object
487 * @si: scanning information
488 * @vtbl: volume table
489 *
490 * This function allocates volume description objects for existing volumes.
491 * Returns zero in case of success and a negative error code in case of
492 * failure.
493 */
494static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
495 const struct ubi_vtbl_record *vtbl)
496{
497 int i, reserved_pebs = 0;
498 struct ubi_scan_volume *sv;
499 struct ubi_volume *vol;
500
501 for (i = 0; i < ubi->vtbl_slots; i++) {
502 cond_resched();
503
504 if (ubi32_to_cpu(vtbl[i].reserved_pebs) == 0)
505 continue; /* Empty record */
506
507 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
508 if (!vol)
509 return -ENOMEM;
510
511 vol->reserved_pebs = ubi32_to_cpu(vtbl[i].reserved_pebs);
512 vol->alignment = ubi32_to_cpu(vtbl[i].alignment);
513 vol->data_pad = ubi32_to_cpu(vtbl[i].data_pad);
514 vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
515 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
516 vol->name_len = ubi16_to_cpu(vtbl[i].name_len);
517 vol->usable_leb_size = ubi->leb_size - vol->data_pad;
518 memcpy(vol->name, vtbl[i].name, vol->name_len);
519 vol->name[vol->name_len] = '\0';
520 vol->vol_id = i;
521
522 ubi_assert(!ubi->volumes[i]);
523 ubi->volumes[i] = vol;
524 ubi->vol_count += 1;
525 vol->ubi = ubi;
526 reserved_pebs += vol->reserved_pebs;
527
528 /*
529 * In case of dynamic volume UBI knows nothing about how many
530 * data is stored there. So assume the whole volume is used.
531 */
532 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
533 vol->used_ebs = vol->reserved_pebs;
534 vol->last_eb_bytes = vol->usable_leb_size;
535 vol->used_bytes = vol->used_ebs * vol->usable_leb_size;
536 continue;
537 }
538
539 /* Static volumes only */
540 sv = ubi_scan_find_sv(si, i);
541 if (!sv) {
542 /*
543 * No eraseblocks belonging to this volume found. We
544 * don't actually know whether this static volume is
545 * completely corrupted or just contains no data. And
546 * we cannot know this as long as data size is not
547 * stored on flash. So we just assume the volume is
548 * empty. FIXME: this should be handled.
549 */
550 continue;
551 }
552
553 if (sv->leb_count != sv->used_ebs) {
554 /*
555 * We found a static volume which misses several
556 * eraseblocks. Treat it as corrupted.
557 */
558 ubi_warn("static volume %d misses %d LEBs - corrupted",
559 sv->vol_id, sv->used_ebs - sv->leb_count);
560 vol->corrupted = 1;
561 continue;
562 }
563
564 vol->used_ebs = sv->used_ebs;
565 vol->used_bytes = (vol->used_ebs - 1) * vol->usable_leb_size;
566 vol->used_bytes += sv->last_data_size;
567 vol->last_eb_bytes = sv->last_data_size;
568 }
569
570 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
571 if (!vol)
572 return -ENOMEM;
573
574 vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
575 vol->alignment = 1;
576 vol->vol_type = UBI_DYNAMIC_VOLUME;
577 vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
578 memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
579 vol->usable_leb_size = ubi->leb_size;
580 vol->used_ebs = vol->reserved_pebs;
581 vol->last_eb_bytes = vol->reserved_pebs;
582 vol->used_bytes = vol->used_ebs * (ubi->leb_size - vol->data_pad);
583 vol->vol_id = UBI_LAYOUT_VOL_ID;
584
585 ubi_assert(!ubi->volumes[i]);
586 ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
587 reserved_pebs += vol->reserved_pebs;
588 ubi->vol_count += 1;
589 vol->ubi = ubi;
590
591 if (reserved_pebs > ubi->avail_pebs)
592 ubi_err("not enough PEBs, required %d, available %d",
593 reserved_pebs, ubi->avail_pebs);
594 ubi->rsvd_pebs += reserved_pebs;
595 ubi->avail_pebs -= reserved_pebs;
596
597 return 0;
598}
599
600/**
601 * check_sv - check volume scanning information.
602 * @vol: UBI volume description object
603 * @sv: volume scanning information
604 *
605 * This function returns zero if the volume scanning information is consistent
606 * to the data read from the volume tabla, and %-EINVAL if not.
607 */
608static int check_sv(const struct ubi_volume *vol,
609 const struct ubi_scan_volume *sv)
610{
611 if (sv->highest_lnum >= vol->reserved_pebs) {
612 dbg_err("bad highest_lnum");
613 goto bad;
614 }
615 if (sv->leb_count > vol->reserved_pebs) {
616 dbg_err("bad leb_count");
617 goto bad;
618 }
619 if (sv->vol_type != vol->vol_type) {
620 dbg_err("bad vol_type");
621 goto bad;
622 }
623 if (sv->used_ebs > vol->reserved_pebs) {
624 dbg_err("bad used_ebs");
625 goto bad;
626 }
627 if (sv->data_pad != vol->data_pad) {
628 dbg_err("bad data_pad");
629 goto bad;
630 }
631 return 0;
632
633bad:
634 ubi_err("bad scanning information");
635 ubi_dbg_dump_sv(sv);
636 ubi_dbg_dump_vol_info(vol);
637 return -EINVAL;
638}
639
640/**
641 * check_scanning_info - check that scanning information.
642 * @ubi: UBI device description object
643 * @si: scanning information
644 *
645 * Even though we protect on-flash data by CRC checksums, we still don't trust
646 * the media. This function ensures that scanning information is consistent to
647 * the information read from the volume table. Returns zero if the scanning
648 * information is OK and %-EINVAL if it is not.
649 */
650static int check_scanning_info(const struct ubi_device *ubi,
651 struct ubi_scan_info *si)
652{
653 int err, i;
654 struct ubi_scan_volume *sv;
655 struct ubi_volume *vol;
656
657 if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
658 ubi_err("scanning found %d volumes, maximum is %d + %d",
659 si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
660 return -EINVAL;
661 }
662
663 if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT&&
664 si->highest_vol_id < UBI_INTERNAL_VOL_START) {
665 ubi_err("too large volume ID %d found by scanning",
666 si->highest_vol_id);
667 return -EINVAL;
668 }
669
670
671 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
672 cond_resched();
673
674 sv = ubi_scan_find_sv(si, i);
675 vol = ubi->volumes[i];
676 if (!vol) {
677 if (sv)
678 ubi_scan_rm_volume(si, sv);
679 continue;
680 }
681
682 if (vol->reserved_pebs == 0) {
683 ubi_assert(i < ubi->vtbl_slots);
684
685 if (!sv)
686 continue;
687
688 /*
689 * During scanning we found a volume which does not
690 * exist according to the information in the volume
691 * table. This must have happened due to an unclean
692 * reboot while the volume was being removed. Discard
693 * these eraseblocks.
694 */
695 ubi_msg("finish volume %d removal", sv->vol_id);
696 ubi_scan_rm_volume(si, sv);
697 } else if (sv) {
698 err = check_sv(vol, sv);
699 if (err)
700 return err;
701 }
702 }
703
704 return 0;
705}
706
707/**
708 * ubi_read_volume_table - read volume table.
709 * information.
710 * @ubi: UBI device description object
711 * @si: scanning information
712 *
713 * This function reads volume table, checks it, recover from errors if needed,
714 * or creates it if needed. Returns zero in case of success and a negative
715 * error code in case of failure.
716 */
717int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
718{
719 int i, err;
720 struct ubi_scan_volume *sv;
721
722 empty_vtbl_record.crc = cpu_to_ubi32(0xf116c36b);
723
724 /*
725 * The number of supported volumes is limited by the eraseblock size
726 * and by the UBI_MAX_VOLUMES constant.
727 */
728 ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
729 if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
730 ubi->vtbl_slots = UBI_MAX_VOLUMES;
731
732 ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
733 ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
734
735 sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOL_ID);
736 if (!sv) {
737 /*
738 * No logical eraseblocks belonging to the layout volume were
739 * found. This could mean that the flash is just empty. In
740 * this case we create empty layout volume.
741 *
742 * But if flash is not empty this must be a corruption or the
743 * MTD device just contains garbage.
744 */
745 if (si->is_empty) {
746 ubi->vtbl = create_empty_lvol(ubi, si);
747 if (IS_ERR(ubi->vtbl))
748 return PTR_ERR(ubi->vtbl);
749 } else {
750 ubi_err("the layout volume was not found");
751 return -EINVAL;
752 }
753 } else {
754 if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) {
755 /* This must not happen with proper UBI images */
756 dbg_err("too many LEBs (%d) in layout volume",
757 sv->leb_count);
758 return -EINVAL;
759 }
760
761 ubi->vtbl = process_lvol(ubi, si, sv);
762 if (IS_ERR(ubi->vtbl))
763 return PTR_ERR(ubi->vtbl);
764 }
765
766 ubi->avail_pebs = ubi->good_peb_count;
767
768 /*
769 * The layout volume is OK, initialize the corresponding in-RAM data
770 * structures.
771 */
772 err = init_volumes(ubi, si, ubi->vtbl);
773 if (err)
774 goto out_free;
775
776 /*
777 * Get sure that the scanning information is consistent to the
778 * information stored in the volume table.
779 */
780 err = check_scanning_info(ubi, si);
781 if (err)
782 goto out_free;
783
784 return 0;
785
786out_free:
787 kfree(ubi->vtbl);
788 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++)
789 if (ubi->volumes[i]) {
790 kfree(ubi->volumes[i]);
791 ubi->volumes[i] = NULL;
792 }
793 return err;
794}
795
796#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
797
798/**
799 * paranoid_vtbl_check - check volume table.
800 * @ubi: UBI device description object
801 */
802static void paranoid_vtbl_check(const struct ubi_device *ubi)
803{
804 if (vtbl_check(ubi, ubi->vtbl)) {
805 ubi_err("paranoid check failed");
806 BUG();
807 }
808}
809
810#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */