blob: 17cec0c0154448d85271a4422ac8907f9dedc00d [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040062#include <asm/div64.h>
63#include "ubi.h"
64
Artem Bityutskiy92d124f2011-03-14 18:17:40 +020065#ifdef CONFIG_MTD_UBI_DEBUG
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040066static 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;
Artem Bityutskiy89b96b62007-12-16 20:00:38 +020090 struct ubi_volume *layout_vol;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040091
92 ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
Artem Bityutskiy91f2d532008-01-24 11:23:23 +020093 layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040094
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);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +030099 vtbl_rec->crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400100 }
101
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400102 memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
103 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200104 err = ubi_eba_unmap_leb(ubi, layout_vol, i);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200105 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400106 return err;
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200107
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200108 err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400109 ubi->vtbl_size, UBI_LONGTERM);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200110 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400111 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400112 }
113
114 paranoid_vtbl_check(ubi);
Artem Bityutskiy6dc4a872008-02-01 13:48:49 +0200115 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400116}
117
118/**
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300119 * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
120 * @ubi: UBI device description object
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300121 * @rename_list: list of &struct ubi_rename_entry objects
Artem Bityutskiyf40ac9c2008-07-13 21:47:47 +0300122 *
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
169/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300170 * vtbl_check - check if volume table is not corrupted and sensible.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400171 * @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;
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300181 int upd_marker, err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400182 uint32_t crc;
183 const char *name;
184
185 for (i = 0; i < ubi->vtbl_slots; i++) {
186 cond_resched();
187
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300188 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);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400191 upd_marker = vtbl[i].upd_marker;
192 vol_type = vtbl[i].vol_type;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300193 name_len = be16_to_cpu(vtbl[i].name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400194 name = &vtbl[i].name[0];
195
196 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300197 if (be32_to_cpu(vtbl[i].crc) != crc) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400198 ubi_err("bad CRC at record %u: %#08x, not %#08x",
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300199 i, crc, be32_to_cpu(vtbl[i].crc));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400200 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)) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300207 err = 2;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400208 goto bad;
209 }
210 continue;
211 }
212
213 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
214 name_len < 0) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300215 err = 3;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400216 goto bad;
217 }
218
219 if (alignment > ubi->leb_size || alignment == 0) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300220 err = 4;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400221 goto bad;
222 }
223
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900224 n = alignment & (ubi->min_io_size - 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400225 if (alignment != 1 && n) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300226 err = 5;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400227 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);
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300233 err = 6;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400234 goto bad;
235 }
236
237 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300238 err = 7;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400239 goto bad;
240 }
241
242 if (upd_marker != 0 && upd_marker != 1) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300243 err = 8;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400244 goto bad;
245 }
246
247 if (reserved_pebs > ubi->good_peb_count) {
Deepak Saxena762a9f22008-10-08 12:56:24 -0700248 dbg_err("too large reserved_pebs %d, good PEBs %d",
249 reserved_pebs, ubi->good_peb_count);
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300250 err = 9;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400251 goto bad;
252 }
253
254 if (name_len > UBI_VOL_NAME_MAX) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300255 err = 10;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400256 goto bad;
257 }
258
259 if (name[0] == '\0') {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300260 err = 11;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400261 goto bad;
262 }
263
264 if (name_len != strnlen(name, name_len + 1)) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300265 err = 12;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400266 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++) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300273 int len1 = be16_to_cpu(vtbl[i].name_len);
274 int len2 = be16_to_cpu(vtbl[n].name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400275
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:
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300290 ubi_err("volume table check failed: record %d, error %d", i, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400291 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 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300305static int create_vtbl(struct ubi_device *ubi, struct ubi_scan_info *si,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400306 int copy, void *vtbl)
307{
308 int err, tries = 0;
Richard Weinberger6bdccff2011-12-22 16:12:57 +0100309 struct ubi_vid_hdr *vid_hdr;
Artem Bityutskiy4788b602011-06-03 19:41:00 +0300310 struct ubi_scan_leb *new_seb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400311
312 ubi_msg("create volume table (copy #%d)", copy + 1);
313
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300314 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400315 if (!vid_hdr)
316 return -ENOMEM;
317
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400318retry:
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
Richard Weinberger1f4f4342012-01-10 17:57:03 +0100325 vid_hdr->vol_type = UBI_LAYOUT_VOLUME_TYPE;
Artem Bityutskiy91f2d532008-01-24 11:23:23 +0200326 vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400327 vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
328 vid_hdr->data_size = vid_hdr->used_ebs =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300329 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);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400332
333 /* The EC header is already there, write the VID header */
334 err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr);
335 if (err)
336 goto write_error;
337
338 /* Write the layout volume contents */
339 err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size);
340 if (err)
341 goto write_error;
342
343 /*
Artem Bityutskiy4788b602011-06-03 19:41:00 +0300344 * And add it to the scanning information. Don't delete the old version
345 * of this LEB as it will be deleted and freed in 'ubi_scan_add_used()'.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400346 */
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:
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300354 if (err == -EIO && ++tries <= 5) {
355 /*
356 * Probably this physical eraseblock went bad, try to pick
357 * another one.
358 */
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300359 list_add(&new_seb->u.list, &si->erase);
Florin Malitac4e90ec2007-05-03 11:49:57 -0400360 goto retry;
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300361 }
362 kfree(new_seb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400363out_free:
364 ubi_free_vid_hdr(ubi, vid_hdr);
365 return err;
366
367}
368
369/**
370 * process_lvol - process the layout volume.
371 * @ubi: UBI device description object
372 * @si: scanning information
373 * @sv: layout volume scanning information
374 *
375 * This function is responsible for reading the layout volume, ensuring it is
376 * not corrupted, and recovering from corruptions if needed. Returns volume
377 * table in case of success and a negative error code in case of failure.
378 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300379static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400380 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 * UBI goes through the following steps when it changes the layout
391 * volume:
392 * a. erase LEB 0;
393 * b. write new data to LEB 0;
394 * c. erase LEB 1;
395 * d. write new data to LEB 1.
396 *
397 * Before the change, both LEBs contain the same data.
398 *
399 * Due to unclean reboots, the contents of LEB 0 may be lost, but there
400 * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
401 * Similarly, LEB 1 may be lost, but there should be LEB 0. And
402 * finally, unclean reboots may result in a situation when neither LEB
403 * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
404 * 0 contains more recent information.
405 *
406 * So the plan is to first check LEB 0. Then
Shinya Kuribayashibe436f62010-05-06 19:22:09 +0900407 * a. if LEB 0 is OK, it must be containing the most recent data; then
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400408 * we compare it with LEB 1, and if they are different, we copy LEB
409 * 0 to LEB 1;
410 * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
411 * to LEB 0.
412 */
413
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300414 dbg_gen("check layout volume");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400415
416 /* Read both LEB 0 and LEB 1 into memory */
417 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
Joe Perches309b5e42010-11-04 20:07:40 -0700418 leb[seb->lnum] = vzalloc(ubi->vtbl_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400419 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);
Brian Norrisd57f40542011-09-20 18:34:25 -0700426 if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err))
Artem Bityutskiybeeea632008-05-20 09:54:02 +0300427 /*
428 * Scrub the PEB later. Note, -EBADMSG indicates an
429 * uncorrectable ECC error, but we have our own CRC and
430 * the data will be checked later. If the data is OK,
431 * the PEB will be scrubbed (because we set
432 * seb->scrub). If the data is not OK, the contents of
433 * the PEB will be recovered from the second copy, and
434 * seb->scrub will be cleared in
435 * 'ubi_scan_add_used()'.
436 */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400437 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 /* LEB 0 is OK */
451 if (leb[1])
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300452 leb_corrupted[1] = memcmp(leb[0], leb[1],
453 ubi->vtbl_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400454 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 /* Both LEB 1 and LEB 2 are OK and consistent */
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300463 vfree(leb[1]);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400464 return leb[0];
465 } else {
466 /* LEB 0 is corrupted or does not exist */
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 /* Both LEB 0 and LEB 1 are corrupted */
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
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300484 vfree(leb[0]);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400485 return leb[1];
486 }
487
488out_free:
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300489 vfree(leb[0]);
490 vfree(leb[1]);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400491 return ERR_PTR(err);
492}
493
494/**
495 * create_empty_lvol - create empty layout volume.
496 * @ubi: UBI device description object
497 * @si: scanning information
498 *
499 * This function returns volume table contents in case of success and a
500 * negative error code in case of failure.
501 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300502static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400503 struct ubi_scan_info *si)
504{
505 int i;
506 struct ubi_vtbl_record *vtbl;
507
Joe Perches309b5e42010-11-04 20:07:40 -0700508 vtbl = vzalloc(ubi->vtbl_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400509 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) {
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300520 vfree(vtbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400521 return ERR_PTR(err);
522 }
523 }
524
525 return vtbl;
526}
527
528/**
529 * init_volumes - initialize volume information for existing volumes.
530 * @ubi: UBI device description object
531 * @si: scanning information
532 * @vtbl: volume table
533 *
534 * This function allocates volume description objects for existing volumes.
535 * Returns zero in case of success and a negative error code in case of
536 * failure.
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
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300548 if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400549 continue; /* Empty record */
550
551 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
552 if (!vol)
553 return -ENOMEM;
554
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300555 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);
Peter Hortonff998792010-01-05 11:14:36 +0000558 vol->upd_marker = vtbl[i].upd_marker;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400559 vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
560 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300561 vol->name_len = be16_to_cpu(vtbl[i].name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400562 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
Artem Bityutskiy4ccf8cf2008-01-16 15:44:24 +0200567 if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
568 /* Auto re-size flag may be set only for one volume */
569 if (ubi->autoresize_vol_id != -1) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200570 ubi_err("more than one auto-resize volume (%d "
Artem Bityutskiy4ccf8cf2008-01-16 15:44:24 +0200571 "and %d)", ubi->autoresize_vol_id, i);
Adrian Bunkf7f02832008-03-03 20:07:52 +0200572 kfree(vol);
Artem Bityutskiy4ccf8cf2008-01-16 15:44:24 +0200573 return -EINVAL;
574 }
575
576 ubi->autoresize_vol_id = i;
577 }
578
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400579 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 * In case of dynamic volume UBI knows nothing about how many
587 * data is stored there. So assume the whole volume is used.
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;
Vinit Agnihotrid08c3b72007-07-10 13:04:59 +0300592 vol->used_bytes =
593 (long long)vol->used_ebs * vol->usable_leb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400594 continue;
595 }
596
597 /* Static volumes only */
598 sv = ubi_scan_find_sv(si, i);
599 if (!sv) {
600 /*
601 * No eraseblocks belonging to this volume found. We
602 * don't actually know whether this static volume is
603 * completely corrupted or just contains no data. And
604 * we cannot know this as long as data size is not
605 * stored on flash. So we just assume the volume is
606 * empty. FIXME: this should be handled.
607 */
608 continue;
609 }
610
611 if (sv->leb_count != sv->used_ebs) {
612 /*
613 * We found a static volume which misses several
614 * eraseblocks. Treat it as corrupted.
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;
Vinit Agnihotrid08c3b72007-07-10 13:04:59 +0300623 vol->used_bytes =
624 (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400625 vol->used_bytes += sv->last_data_size;
626 vol->last_eb_bytes = sv->last_data_size;
627 }
628
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200629 /* And add the layout volume */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400630 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
631 if (!vol)
632 return -ENOMEM;
633
634 vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
Richard Weinberger1f4f4342012-01-10 17:57:03 +0100635 vol->alignment = UBI_LAYOUT_VOLUME_ALIGN;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400636 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;
Vinit Agnihotrid08c3b72007-07-10 13:04:59 +0300642 vol->used_bytes =
643 (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
Artem Bityutskiy91f2d532008-01-24 11:23:23 +0200644 vol->vol_id = UBI_LAYOUT_VOLUME_ID;
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200645 vol->ref_count = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400646
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
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300653 if (reserved_pebs > ubi->avail_pebs) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400654 ubi_err("not enough PEBs, required %d, available %d",
655 reserved_pebs, ubi->avail_pebs);
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300656 if (ubi->corr_peb_count)
657 ubi_err("%d PEBs are corrupted and not used",
658 ubi->corr_peb_count);
659 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400660 ubi->rsvd_pebs += reserved_pebs;
661 ubi->avail_pebs -= reserved_pebs;
662
663 return 0;
664}
665
666/**
667 * check_sv - check volume scanning information.
668 * @vol: UBI volume description object
669 * @sv: volume scanning information
670 *
671 * This function returns zero if the volume scanning information is consistent
672 * to the data read from the volume tabla, and %-EINVAL if not.
673 */
674static int check_sv(const struct ubi_volume *vol,
675 const struct ubi_scan_volume *sv)
676{
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300677 int err;
678
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400679 if (sv->highest_lnum >= vol->reserved_pebs) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300680 err = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400681 goto bad;
682 }
683 if (sv->leb_count > vol->reserved_pebs) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300684 err = 2;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400685 goto bad;
686 }
687 if (sv->vol_type != vol->vol_type) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300688 err = 3;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400689 goto bad;
690 }
691 if (sv->used_ebs > vol->reserved_pebs) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300692 err = 4;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400693 goto bad;
694 }
695 if (sv->data_pad != vol->data_pad) {
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300696 err = 5;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400697 goto bad;
698 }
699 return 0;
700
701bad:
Artem Bityutskiy979c9292008-05-14 16:10:33 +0300702 ubi_err("bad scanning information, error %d", err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400703 ubi_dbg_dump_sv(sv);
704 ubi_dbg_dump_vol_info(vol);
705 return -EINVAL;
706}
707
708/**
709 * check_scanning_info - check that scanning information.
710 * @ubi: UBI device description object
711 * @si: scanning information
712 *
713 * Even though we protect on-flash data by CRC checksums, we still don't trust
714 * the media. This function ensures that scanning information is consistent to
715 * the information read from the volume table. Returns zero if the scanning
716 * information is OK and %-EINVAL if it is not.
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
Kyungmin Parkcadb40c2008-05-22 10:32:18 +0900731 if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400732 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
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400738 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 * During scanning we found a volume which does not
757 * exist according to the information in the volume
758 * table. This must have happened due to an unclean
759 * reboot while the volume was being removed. Discard
760 * these eraseblocks.
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/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300775 * ubi_read_volume_table - read the volume table.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400776 * @ubi: UBI device description object
777 * @si: scanning information
778 *
779 * This function reads volume table, checks it, recover from errors if needed,
780 * or creates it if needed. Returns zero in case of success and a negative
781 * error code in case of failure.
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
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300788 empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400789
790 /*
791 * The number of supported volumes is limited by the eraseblock size
792 * and by the UBI_MAX_VOLUMES constant.
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
Artem Bityutskiy91f2d532008-01-24 11:23:23 +0200801 sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400802 if (!sv) {
803 /*
804 * No logical eraseblocks belonging to the layout volume were
805 * found. This could mean that the flash is just empty. In
806 * this case we create empty layout volume.
807 *
808 * But if flash is not empty this must be a corruption or the
809 * MTD device just contains garbage.
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 /* This must not happen with proper UBI images */
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
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300832 ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400833
834 /*
835 * The layout volume is OK, initialize the corresponding in-RAM data
836 * structures.
837 */
838 err = init_volumes(ubi, si, ubi->vtbl);
839 if (err)
840 goto out_free;
841
842 /*
Shinya Kuribayashibe436f62010-05-06 19:22:09 +0900843 * Make sure that the scanning information is consistent to the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400844 * information stored in the volume table.
845 */
846 err = check_scanning_info(ubi, si);
847 if (err)
848 goto out_free;
849
850 return 0;
851
852out_free:
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300853 vfree(ubi->vtbl);
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300854 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
855 kfree(ubi->volumes[i]);
856 ubi->volumes[i] = NULL;
857 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400858 return err;
859}
860
Artem Bityutskiy92d124f2011-03-14 18:17:40 +0200861#ifdef CONFIG_MTD_UBI_DEBUG
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400862
863/**
864 * paranoid_vtbl_check - check volume table.
865 * @ubi: UBI device description object
866 */
867static void paranoid_vtbl_check(const struct ubi_device *ubi)
868{
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300869 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +0200870 return;
871
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400872 if (vtbl_check(ubi, ubi->vtbl)) {
873 ubi_err("paranoid check failed");
874 BUG();
875 }
876}
877
Artem Bityutskiy92d124f2011-03-14 18:17:40 +0200878#endif /* CONFIG_MTD_UBI_DEBUG */