Fix common misspellings
[linux-block.git] / drivers / mtd / ubi / vtbl.c
CommitLineData
801c135c
AB
1/*
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>
5a0e3ad6 61#include <linux/slab.h>
801c135c
AB
62#include <asm/div64.h>
63#include "ubi.h"
64
92d124f5 65#ifdef CONFIG_MTD_UBI_DEBUG
801c135c
AB
66static void paranoid_vtbl_check(const struct ubi_device *ubi);
67#else
68#define paranoid_vtbl_check(ubi)
69#endif
70
71/* Empty volume table record */
72static struct ubi_vtbl_record empty_vtbl_record;
73
74/**
75 * ubi_change_vtbl_record - change volume table record.
76 * @ubi: UBI device description object
77 * @idx: table index to change
78 * @vtbl_rec: new volume table record
79 *
80 * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
81 * volume table record is written. The caller does not have to calculate CRC of
82 * the record as it is done by this function. Returns zero in case of success
83 * and a negative error code in case of failure.
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;
89b96b69 90 struct ubi_volume *layout_vol;
801c135c
AB
91
92 ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
91f2d53c 93 layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
801c135c
AB
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);
3261ebd7 99 vtbl_rec->crc = cpu_to_be32(crc);
801c135c
AB
100 }
101
801c135c
AB
102 memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
103 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
89b96b69 104 err = ubi_eba_unmap_leb(ubi, layout_vol, i);
cae0a771 105 if (err)
801c135c 106 return err;
cae0a771 107
89b96b69 108 err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
801c135c 109 ubi->vtbl_size, UBI_LONGTERM);
cae0a771 110 if (err)
801c135c 111 return err;
801c135c
AB
112 }
113
114 paranoid_vtbl_check(ubi);
6dc4a871 115 return 0;
801c135c
AB
116}
117
f40ac9cd
AB
118/**
119 * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
120 * @ubi: UBI device description object
ebaaf1af 121 * @rename_list: list of &struct ubi_rename_entry objects
f40ac9cd
AB
122 *
123 * This function re-names multiple volumes specified in @req in the volume
124 * table. Returns zero in case of success and a negative error code in case of
125 * failure.
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
801c135c 169/**
ebaaf1af 170 * vtbl_check - check if volume table is not corrupted and sensible.
801c135c
AB
171 * @ubi: UBI device description object
172 * @vtbl: volume table
173 *
174 * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
175 * and %-EINVAL if it contains inconsistent data.
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;
979c9296 181 int upd_marker, err;
801c135c
AB
182 uint32_t crc;
183 const char *name;
184
185 for (i = 0; i < ubi->vtbl_slots; i++) {
186 cond_resched();
187
3261ebd7
CH
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);
801c135c
AB
191 upd_marker = vtbl[i].upd_marker;
192 vol_type = vtbl[i].vol_type;
3261ebd7 193 name_len = be16_to_cpu(vtbl[i].name_len);
801c135c
AB
194 name = &vtbl[i].name[0];
195
196 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
3261ebd7 197 if (be32_to_cpu(vtbl[i].crc) != crc) {
801c135c 198 ubi_err("bad CRC at record %u: %#08x, not %#08x",
3261ebd7 199 i, crc, be32_to_cpu(vtbl[i].crc));
801c135c
AB
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)) {
979c9296 207 err = 2;
801c135c
AB
208 goto bad;
209 }
210 continue;
211 }
212
213 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
214 name_len < 0) {
979c9296 215 err = 3;
801c135c
AB
216 goto bad;
217 }
218
219 if (alignment > ubi->leb_size || alignment == 0) {
979c9296 220 err = 4;
801c135c
AB
221 goto bad;
222 }
223
cadb40cc 224 n = alignment & (ubi->min_io_size - 1);
801c135c 225 if (alignment != 1 && n) {
979c9296 226 err = 5;
801c135c
AB
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);
979c9296 233 err = 6;
801c135c
AB
234 goto bad;
235 }
236
237 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
979c9296 238 err = 7;
801c135c
AB
239 goto bad;
240 }
241
242 if (upd_marker != 0 && upd_marker != 1) {
979c9296 243 err = 8;
801c135c
AB
244 goto bad;
245 }
246
247 if (reserved_pebs > ubi->good_peb_count) {
762a9f29
DS
248 dbg_err("too large reserved_pebs %d, good PEBs %d",
249 reserved_pebs, ubi->good_peb_count);
979c9296 250 err = 9;
801c135c
AB
251 goto bad;
252 }
253
254 if (name_len > UBI_VOL_NAME_MAX) {
979c9296 255 err = 10;
801c135c
AB
256 goto bad;
257 }
258
259 if (name[0] == '\0') {
979c9296 260 err = 11;
801c135c
AB
261 goto bad;
262 }
263
264 if (name_len != strnlen(name, name_len + 1)) {
979c9296 265 err = 12;
801c135c
AB
266 goto bad;
267 }
268 }
269
270 /* Checks that all names are unique */
271 for (i = 0; i < ubi->vtbl_slots - 1; i++) {
272 for (n = i + 1; n < ubi->vtbl_slots; n++) {
3261ebd7
CH
273 int len1 = be16_to_cpu(vtbl[i].name_len);
274 int len2 = be16_to_cpu(vtbl[n].name_len);
801c135c
AB
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:
979c9296 290 ubi_err("volume table check failed: record %d, error %d", i, err);
801c135c
AB
291 ubi_dbg_dump_vtbl_record(&vtbl[i], i);
292 return -EINVAL;
293}
294
295/**
296 * create_vtbl - create a copy of volume table.
297 * @ubi: UBI device description object
298 * @si: scanning information
299 * @copy: number of the volume table copy
300 * @vtbl: contents of the volume table
301 *
302 * This function returns zero in case of success and a negative error code in
303 * case of failure.
304 */
e88d6e10 305static int create_vtbl(struct ubi_device *ubi, struct ubi_scan_info *si,
801c135c
AB
306 int copy, void *vtbl)
307{
308 int err, tries = 0;
309 static struct ubi_vid_hdr *vid_hdr;
310 struct ubi_scan_volume *sv;
311 struct ubi_scan_leb *new_seb, *old_seb = NULL;
312
313 ubi_msg("create volume table (copy #%d)", copy + 1);
314
33818bbb 315 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
801c135c
AB
316 if (!vid_hdr)
317 return -ENOMEM;
318
319 /*
320 * Check if there is a logical eraseblock which would have to contain
321 * this volume table copy was found during scanning. It has to be wiped
322 * out.
323 */
91f2d53c 324 sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID);
801c135c
AB
325 if (sv)
326 old_seb = ubi_scan_find_seb(sv, copy);
327
328retry:
329 new_seb = ubi_scan_get_free_peb(ubi, si);
330 if (IS_ERR(new_seb)) {
331 err = PTR_ERR(new_seb);
332 goto out_free;
333 }
334
335 vid_hdr->vol_type = UBI_VID_DYNAMIC;
91f2d53c 336 vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
801c135c
AB
337 vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
338 vid_hdr->data_size = vid_hdr->used_ebs =
3261ebd7
CH
339 vid_hdr->data_pad = cpu_to_be32(0);
340 vid_hdr->lnum = cpu_to_be32(copy);
341 vid_hdr->sqnum = cpu_to_be64(++si->max_sqnum);
801c135c
AB
342
343 /* The EC header is already there, write the VID header */
344 err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr);
345 if (err)
346 goto write_error;
347
348 /* Write the layout volume contents */
349 err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size);
350 if (err)
351 goto write_error;
352
353 /*
354 * And add it to the scanning information. Don't delete the old
355 * @old_seb as it will be deleted and freed in 'ubi_scan_add_used()'.
356 */
357 err = ubi_scan_add_used(ubi, si, new_seb->pnum, new_seb->ec,
358 vid_hdr, 0);
359 kfree(new_seb);
360 ubi_free_vid_hdr(ubi, vid_hdr);
361 return err;
362
363write_error:
78d87c95
AB
364 if (err == -EIO && ++tries <= 5) {
365 /*
366 * Probably this physical eraseblock went bad, try to pick
367 * another one.
368 */
0525dac9 369 list_add(&new_seb->u.list, &si->erase);
c4e90ec0 370 goto retry;
78d87c95
AB
371 }
372 kfree(new_seb);
801c135c
AB
373out_free:
374 ubi_free_vid_hdr(ubi, vid_hdr);
375 return err;
376
377}
378
379/**
380 * process_lvol - process the layout volume.
381 * @ubi: UBI device description object
382 * @si: scanning information
383 * @sv: layout volume scanning information
384 *
385 * This function is responsible for reading the layout volume, ensuring it is
386 * not corrupted, and recovering from corruptions if needed. Returns volume
387 * table in case of success and a negative error code in case of failure.
388 */
e88d6e10 389static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
801c135c
AB
390 struct ubi_scan_info *si,
391 struct ubi_scan_volume *sv)
392{
393 int err;
394 struct rb_node *rb;
395 struct ubi_scan_leb *seb;
396 struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
397 int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
398
399 /*
400 * UBI goes through the following steps when it changes the layout
401 * volume:
402 * a. erase LEB 0;
403 * b. write new data to LEB 0;
404 * c. erase LEB 1;
405 * d. write new data to LEB 1.
406 *
407 * Before the change, both LEBs contain the same data.
408 *
409 * Due to unclean reboots, the contents of LEB 0 may be lost, but there
410 * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
411 * Similarly, LEB 1 may be lost, but there should be LEB 0. And
412 * finally, unclean reboots may result in a situation when neither LEB
413 * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
414 * 0 contains more recent information.
415 *
416 * So the plan is to first check LEB 0. Then
be436f62 417 * a. if LEB 0 is OK, it must be containing the most recent data; then
801c135c
AB
418 * we compare it with LEB 1, and if they are different, we copy LEB
419 * 0 to LEB 1;
420 * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
421 * to LEB 0.
422 */
423
c8566350 424 dbg_gen("check layout volume");
801c135c
AB
425
426 /* Read both LEB 0 and LEB 1 into memory */
427 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
309b5e4e 428 leb[seb->lnum] = vzalloc(ubi->vtbl_size);
801c135c
AB
429 if (!leb[seb->lnum]) {
430 err = -ENOMEM;
431 goto out_free;
432 }
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)
beeea636
AB
437 /*
438 * Scrub the PEB later. Note, -EBADMSG indicates an
439 * uncorrectable ECC error, but we have our own CRC and
440 * the data will be checked later. If the data is OK,
441 * the PEB will be scrubbed (because we set
442 * seb->scrub). If the data is not OK, the contents of
443 * the PEB will be recovered from the second copy, and
444 * seb->scrub will be cleared in
445 * 'ubi_scan_add_used()'.
446 */
801c135c
AB
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 /* LEB 0 is OK */
461 if (leb[1])
9c9ec147
AB
462 leb_corrupted[1] = memcmp(leb[0], leb[1],
463 ubi->vtbl_size);
801c135c
AB
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 /* Both LEB 1 and LEB 2 are OK and consistent */
92ad8f37 473 vfree(leb[1]);
801c135c
AB
474 return leb[0];
475 } else {
476 /* LEB 0 is corrupted or does not exist */
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 /* Both LEB 0 and LEB 1 are corrupted */
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
92ad8f37 494 vfree(leb[0]);
801c135c
AB
495 return leb[1];
496 }
497
498out_free:
92ad8f37
AB
499 vfree(leb[0]);
500 vfree(leb[1]);
801c135c
AB
501 return ERR_PTR(err);
502}
503
504/**
505 * create_empty_lvol - create empty layout volume.
506 * @ubi: UBI device description object
507 * @si: scanning information
508 *
509 * This function returns volume table contents in case of success and a
510 * negative error code in case of failure.
511 */
e88d6e10 512static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
801c135c
AB
513 struct ubi_scan_info *si)
514{
515 int i;
516 struct ubi_vtbl_record *vtbl;
517
309b5e4e 518 vtbl = vzalloc(ubi->vtbl_size);
801c135c
AB
519 if (!vtbl)
520 return ERR_PTR(-ENOMEM);
521
522 for (i = 0; i < ubi->vtbl_slots; i++)
523 memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
524
525 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
526 int err;
527
528 err = create_vtbl(ubi, si, i, vtbl);
529 if (err) {
92ad8f37 530 vfree(vtbl);
801c135c
AB
531 return ERR_PTR(err);
532 }
533 }
534
535 return vtbl;
536}
537
538/**
539 * init_volumes - initialize volume information for existing volumes.
540 * @ubi: UBI device description object
541 * @si: scanning information
542 * @vtbl: volume table
543 *
544 * This function allocates volume description objects for existing volumes.
545 * Returns zero in case of success and a negative error code in case of
546 * failure.
547 */
548static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
549 const struct ubi_vtbl_record *vtbl)
550{
551 int i, reserved_pebs = 0;
552 struct ubi_scan_volume *sv;
553 struct ubi_volume *vol;
554
555 for (i = 0; i < ubi->vtbl_slots; i++) {
556 cond_resched();
557
3261ebd7 558 if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
801c135c
AB
559 continue; /* Empty record */
560
561 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
562 if (!vol)
563 return -ENOMEM;
564
3261ebd7
CH
565 vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
566 vol->alignment = be32_to_cpu(vtbl[i].alignment);
567 vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
ff998793 568 vol->upd_marker = vtbl[i].upd_marker;
801c135c
AB
569 vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
570 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
3261ebd7 571 vol->name_len = be16_to_cpu(vtbl[i].name_len);
801c135c
AB
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
4ccf8cff
AB
577 if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
578 /* Auto re-size flag may be set only for one volume */
579 if (ubi->autoresize_vol_id != -1) {
025dfdaf 580 ubi_err("more than one auto-resize volume (%d "
4ccf8cff 581 "and %d)", ubi->autoresize_vol_id, i);
f7f02837 582 kfree(vol);
4ccf8cff
AB
583 return -EINVAL;
584 }
585
586 ubi->autoresize_vol_id = i;
587 }
588
801c135c
AB
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 * In case of dynamic volume UBI knows nothing about how many
597 * data is stored there. So assume the whole volume is used.
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;
d08c3b78
VA
602 vol->used_bytes =
603 (long long)vol->used_ebs * vol->usable_leb_size;
801c135c
AB
604 continue;
605 }
606
607 /* Static volumes only */
608 sv = ubi_scan_find_sv(si, i);
609 if (!sv) {
610 /*
611 * No eraseblocks belonging to this volume found. We
612 * don't actually know whether this static volume is
613 * completely corrupted or just contains no data. And
614 * we cannot know this as long as data size is not
615 * stored on flash. So we just assume the volume is
616 * empty. FIXME: this should be handled.
617 */
618 continue;
619 }
620
621 if (sv->leb_count != sv->used_ebs) {
622 /*
623 * We found a static volume which misses several
624 * eraseblocks. Treat it as corrupted.
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;
d08c3b78
VA
633 vol->used_bytes =
634 (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
801c135c
AB
635 vol->used_bytes += sv->last_data_size;
636 vol->last_eb_bytes = sv->last_data_size;
637 }
638
d05c77a8 639 /* And add the layout volume */
801c135c
AB
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;
d08c3b78
VA
652 vol->used_bytes =
653 (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
91f2d53c 654 vol->vol_id = UBI_LAYOUT_VOLUME_ID;
d05c77a8 655 vol->ref_count = 1;
801c135c
AB
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
5fc01ab6 663 if (reserved_pebs > ubi->avail_pebs) {
801c135c
AB
664 ubi_err("not enough PEBs, required %d, available %d",
665 reserved_pebs, ubi->avail_pebs);
5fc01ab6
AB
666 if (ubi->corr_peb_count)
667 ubi_err("%d PEBs are corrupted and not used",
668 ubi->corr_peb_count);
669 }
801c135c
AB
670 ubi->rsvd_pebs += reserved_pebs;
671 ubi->avail_pebs -= reserved_pebs;
672
673 return 0;
674}
675
676/**
677 * check_sv - check volume scanning information.
678 * @vol: UBI volume description object
679 * @sv: volume scanning information
680 *
681 * This function returns zero if the volume scanning information is consistent
682 * to the data read from the volume tabla, and %-EINVAL if not.
683 */
684static int check_sv(const struct ubi_volume *vol,
685 const struct ubi_scan_volume *sv)
686{
979c9296
AB
687 int err;
688
801c135c 689 if (sv->highest_lnum >= vol->reserved_pebs) {
979c9296 690 err = 1;
801c135c
AB
691 goto bad;
692 }
693 if (sv->leb_count > vol->reserved_pebs) {
979c9296 694 err = 2;
801c135c
AB
695 goto bad;
696 }
697 if (sv->vol_type != vol->vol_type) {
979c9296 698 err = 3;
801c135c
AB
699 goto bad;
700 }
701 if (sv->used_ebs > vol->reserved_pebs) {
979c9296 702 err = 4;
801c135c
AB
703 goto bad;
704 }
705 if (sv->data_pad != vol->data_pad) {
979c9296 706 err = 5;
801c135c
AB
707 goto bad;
708 }
709 return 0;
710
711bad:
979c9296 712 ubi_err("bad scanning information, error %d", err);
801c135c
AB
713 ubi_dbg_dump_sv(sv);
714 ubi_dbg_dump_vol_info(vol);
715 return -EINVAL;
716}
717
718/**
719 * check_scanning_info - check that scanning information.
720 * @ubi: UBI device description object
721 * @si: scanning information
722 *
723 * Even though we protect on-flash data by CRC checksums, we still don't trust
724 * the media. This function ensures that scanning information is consistent to
725 * the information read from the volume table. Returns zero if the scanning
726 * information is OK and %-EINVAL if it is not.
727 */
728static int check_scanning_info(const struct ubi_device *ubi,
729 struct ubi_scan_info *si)
730{
731 int err, i;
732 struct ubi_scan_volume *sv;
733 struct ubi_volume *vol;
734
735 if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
736 ubi_err("scanning found %d volumes, maximum is %d + %d",
737 si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
738 return -EINVAL;
739 }
740
cadb40cc 741 if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
801c135c
AB
742 si->highest_vol_id < UBI_INTERNAL_VOL_START) {
743 ubi_err("too large volume ID %d found by scanning",
744 si->highest_vol_id);
745 return -EINVAL;
746 }
747
801c135c
AB
748 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
749 cond_resched();
750
751 sv = ubi_scan_find_sv(si, i);
752 vol = ubi->volumes[i];
753 if (!vol) {
754 if (sv)
755 ubi_scan_rm_volume(si, sv);
756 continue;
757 }
758
759 if (vol->reserved_pebs == 0) {
760 ubi_assert(i < ubi->vtbl_slots);
761
762 if (!sv)
763 continue;
764
765 /*
766 * During scanning we found a volume which does not
767 * exist according to the information in the volume
768 * table. This must have happened due to an unclean
769 * reboot while the volume was being removed. Discard
770 * these eraseblocks.
771 */
772 ubi_msg("finish volume %d removal", sv->vol_id);
773 ubi_scan_rm_volume(si, sv);
774 } else if (sv) {
775 err = check_sv(vol, sv);
776 if (err)
777 return err;
778 }
779 }
780
781 return 0;
782}
783
784/**
ebaaf1af 785 * ubi_read_volume_table - read the volume table.
801c135c
AB
786 * @ubi: UBI device description object
787 * @si: scanning information
788 *
789 * This function reads volume table, checks it, recover from errors if needed,
790 * or creates it if needed. Returns zero in case of success and a negative
791 * error code in case of failure.
792 */
793int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
794{
795 int i, err;
796 struct ubi_scan_volume *sv;
797
3261ebd7 798 empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
801c135c
AB
799
800 /*
801 * The number of supported volumes is limited by the eraseblock size
802 * and by the UBI_MAX_VOLUMES constant.
803 */
804 ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
805 if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
806 ubi->vtbl_slots = UBI_MAX_VOLUMES;
807
808 ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
809 ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
810
91f2d53c 811 sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID);
801c135c
AB
812 if (!sv) {
813 /*
814 * No logical eraseblocks belonging to the layout volume were
815 * found. This could mean that the flash is just empty. In
816 * this case we create empty layout volume.
817 *
818 * But if flash is not empty this must be a corruption or the
819 * MTD device just contains garbage.
820 */
821 if (si->is_empty) {
822 ubi->vtbl = create_empty_lvol(ubi, si);
823 if (IS_ERR(ubi->vtbl))
824 return PTR_ERR(ubi->vtbl);
825 } else {
826 ubi_err("the layout volume was not found");
827 return -EINVAL;
828 }
829 } else {
830 if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) {
831 /* This must not happen with proper UBI images */
832 dbg_err("too many LEBs (%d) in layout volume",
833 sv->leb_count);
834 return -EINVAL;
835 }
836
837 ubi->vtbl = process_lvol(ubi, si, sv);
838 if (IS_ERR(ubi->vtbl))
839 return PTR_ERR(ubi->vtbl);
840 }
841
5fc01ab6 842 ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
801c135c
AB
843
844 /*
845 * The layout volume is OK, initialize the corresponding in-RAM data
846 * structures.
847 */
848 err = init_volumes(ubi, si, ubi->vtbl);
849 if (err)
850 goto out_free;
851
852 /*
be436f62 853 * Make sure that the scanning information is consistent to the
801c135c
AB
854 * information stored in the volume table.
855 */
856 err = check_scanning_info(ubi, si);
857 if (err)
858 goto out_free;
859
860 return 0;
861
862out_free:
92ad8f37 863 vfree(ubi->vtbl);
9c9ec147
AB
864 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
865 kfree(ubi->volumes[i]);
866 ubi->volumes[i] = NULL;
867 }
801c135c
AB
868 return err;
869}
870
92d124f5 871#ifdef CONFIG_MTD_UBI_DEBUG
801c135c
AB
872
873/**
874 * paranoid_vtbl_check - check volume table.
875 * @ubi: UBI device description object
876 */
877static void paranoid_vtbl_check(const struct ubi_device *ubi)
878{
92d124f5
AB
879 if (!(ubi_chk_flags & UBI_CHK_GEN))
880 return;
881
801c135c
AB
882 if (vtbl_check(ubi, ubi->vtbl)) {
883 ubi_err("paranoid check failed");
884 BUG();
885 }
886}
887
92d124f5 888#endif /* CONFIG_MTD_UBI_DEBUG */