xref: /openbmc/u-boot/drivers/mtd/ubi/vtbl.c (revision 7da69236)
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 #ifdef UBI_LINUX
60 #include <linux/crc32.h>
61 #include <linux/err.h>
62 #include <asm/div64.h>
63 #endif
64 
65 #include <ubi_uboot.h>
66 #include "ubi.h"
67 
68 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
69 static void paranoid_vtbl_check(const struct ubi_device *ubi);
70 #else
71 #define paranoid_vtbl_check(ubi)
72 #endif
73 
74 /* Empty volume table record */
75 static struct ubi_vtbl_record empty_vtbl_record;
76 
77 /**
78  * ubi_change_vtbl_record - change volume table record.
79  * @ubi: UBI device description object
80  * @idx: table index to change
81  * @vtbl_rec: new volume table record
82  *
83  * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
84  * volume table record is written. The caller does not have to calculate CRC of
85  * the record as it is done by this function. Returns zero in case of success
86  * and a negative error code in case of failure.
87  */
88 int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
89 			   struct ubi_vtbl_record *vtbl_rec)
90 {
91 	int i, err;
92 	uint32_t crc;
93 	struct ubi_volume *layout_vol;
94 
95 	ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
96 	layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
97 
98 	if (!vtbl_rec)
99 		vtbl_rec = &empty_vtbl_record;
100 	else {
101 		crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
102 		vtbl_rec->crc = cpu_to_be32(crc);
103 	}
104 
105 	memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
106 	for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
107 		err = ubi_eba_unmap_leb(ubi, layout_vol, i);
108 		if (err)
109 			return err;
110 
111 		err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
112 					ubi->vtbl_size, UBI_LONGTERM);
113 		if (err)
114 			return err;
115 	}
116 
117 	paranoid_vtbl_check(ubi);
118 	return 0;
119 }
120 
121 /**
122  * vtbl_check - check if volume table is not corrupted and contains sensible
123  *              data.
124  * @ubi: UBI device description object
125  * @vtbl: volume table
126  *
127  * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
128  * and %-EINVAL if it contains inconsistent data.
129  */
130 static int vtbl_check(const struct ubi_device *ubi,
131 		      const struct ubi_vtbl_record *vtbl)
132 {
133 	int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
134 	int upd_marker, err;
135 	uint32_t crc;
136 	const char *name;
137 
138 	for (i = 0; i < ubi->vtbl_slots; i++) {
139 		cond_resched();
140 
141 		reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
142 		alignment = be32_to_cpu(vtbl[i].alignment);
143 		data_pad = be32_to_cpu(vtbl[i].data_pad);
144 		upd_marker = vtbl[i].upd_marker;
145 		vol_type = vtbl[i].vol_type;
146 		name_len = be16_to_cpu(vtbl[i].name_len);
147 		name = (const char *) &vtbl[i].name[0];
148 
149 		crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
150 		if (be32_to_cpu(vtbl[i].crc) != crc) {
151 			ubi_err("bad CRC at record %u: %#08x, not %#08x",
152 				 i, crc, be32_to_cpu(vtbl[i].crc));
153 			ubi_dbg_dump_vtbl_record(&vtbl[i], i);
154 			return 1;
155 		}
156 
157 		if (reserved_pebs == 0) {
158 			if (memcmp(&vtbl[i], &empty_vtbl_record,
159 						UBI_VTBL_RECORD_SIZE)) {
160 				err = 2;
161 				goto bad;
162 			}
163 			continue;
164 		}
165 
166 		if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
167 		    name_len < 0) {
168 			err = 3;
169 			goto bad;
170 		}
171 
172 		if (alignment > ubi->leb_size || alignment == 0) {
173 			err = 4;
174 			goto bad;
175 		}
176 
177 		n = alignment & (ubi->min_io_size - 1);
178 		if (alignment != 1 && n) {
179 			err = 5;
180 			goto bad;
181 		}
182 
183 		n = ubi->leb_size % alignment;
184 		if (data_pad != n) {
185 			dbg_err("bad data_pad, has to be %d", n);
186 			err = 6;
187 			goto bad;
188 		}
189 
190 		if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
191 			err = 7;
192 			goto bad;
193 		}
194 
195 		if (upd_marker != 0 && upd_marker != 1) {
196 			err = 8;
197 			goto bad;
198 		}
199 
200 		if (reserved_pebs > ubi->good_peb_count) {
201 			dbg_err("too large reserved_pebs, good PEBs %d",
202 				ubi->good_peb_count);
203 			err = 9;
204 			goto bad;
205 		}
206 
207 		if (name_len > UBI_VOL_NAME_MAX) {
208 			err = 10;
209 			goto bad;
210 		}
211 
212 		if (name[0] == '\0') {
213 			err = 11;
214 			goto bad;
215 		}
216 
217 		if (name_len != strnlen(name, name_len + 1)) {
218 			err = 12;
219 			goto bad;
220 		}
221 	}
222 
223 	/* Checks that all names are unique */
224 	for (i = 0; i < ubi->vtbl_slots - 1; i++) {
225 		for (n = i + 1; n < ubi->vtbl_slots; n++) {
226 			int len1 = be16_to_cpu(vtbl[i].name_len);
227 			int len2 = be16_to_cpu(vtbl[n].name_len);
228 
229 			if (len1 > 0 && len1 == len2 &&
230 			    !strncmp((char *)vtbl[i].name, (char *)vtbl[n].name, len1)) {
231 				ubi_err("volumes %d and %d have the same name"
232 					" \"%s\"", i, n, vtbl[i].name);
233 				ubi_dbg_dump_vtbl_record(&vtbl[i], i);
234 				ubi_dbg_dump_vtbl_record(&vtbl[n], n);
235 				return -EINVAL;
236 			}
237 		}
238 	}
239 
240 	return 0;
241 
242 bad:
243 	ubi_err("volume table check failed: record %d, error %d", i, err);
244 	ubi_dbg_dump_vtbl_record(&vtbl[i], i);
245 	return -EINVAL;
246 }
247 
248 /**
249  * create_vtbl - create a copy of volume table.
250  * @ubi: UBI device description object
251  * @si: scanning information
252  * @copy: number of the volume table copy
253  * @vtbl: contents of the volume table
254  *
255  * This function returns zero in case of success and a negative error code in
256  * case of failure.
257  */
258 static int create_vtbl(struct ubi_device *ubi, struct ubi_scan_info *si,
259 		       int copy, void *vtbl)
260 {
261 	int err, tries = 0;
262 	static struct ubi_vid_hdr *vid_hdr;
263 	struct ubi_scan_volume *sv;
264 	struct ubi_scan_leb *new_seb, *old_seb = NULL;
265 
266 	ubi_msg("create volume table (copy #%d)", copy + 1);
267 
268 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
269 	if (!vid_hdr)
270 		return -ENOMEM;
271 
272 	/*
273 	 * Check if there is a logical eraseblock which would have to contain
274 	 * this volume table copy was found during scanning. It has to be wiped
275 	 * out.
276 	 */
277 	sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID);
278 	if (sv)
279 		old_seb = ubi_scan_find_seb(sv, copy);
280 
281 retry:
282 	new_seb = ubi_scan_get_free_peb(ubi, si);
283 	if (IS_ERR(new_seb)) {
284 		err = PTR_ERR(new_seb);
285 		goto out_free;
286 	}
287 
288 	vid_hdr->vol_type = UBI_VID_DYNAMIC;
289 	vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
290 	vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
291 	vid_hdr->data_size = vid_hdr->used_ebs =
292 			     vid_hdr->data_pad = cpu_to_be32(0);
293 	vid_hdr->lnum = cpu_to_be32(copy);
294 	vid_hdr->sqnum = cpu_to_be64(++si->max_sqnum);
295 	vid_hdr->leb_ver = cpu_to_be32(old_seb ? old_seb->leb_ver + 1: 0);
296 
297 	/* The EC header is already there, write the VID header */
298 	err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr);
299 	if (err)
300 		goto write_error;
301 
302 	/* Write the layout volume contents */
303 	err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size);
304 	if (err)
305 		goto write_error;
306 
307 	/*
308 	 * And add it to the scanning information. Don't delete the old
309 	 * @old_seb as it will be deleted and freed in 'ubi_scan_add_used()'.
310 	 */
311 	err = ubi_scan_add_used(ubi, si, new_seb->pnum, new_seb->ec,
312 				vid_hdr, 0);
313 	kfree(new_seb);
314 	ubi_free_vid_hdr(ubi, vid_hdr);
315 	return err;
316 
317 write_error:
318 	if (err == -EIO && ++tries <= 5) {
319 		/*
320 		 * Probably this physical eraseblock went bad, try to pick
321 		 * another one.
322 		 */
323 		list_add_tail(&new_seb->u.list, &si->corr);
324 		goto retry;
325 	}
326 	kfree(new_seb);
327 out_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  */
343 static struct ubi_vtbl_record *process_lvol(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] = vmalloc(ubi->vtbl_size);
383 		if (!leb[seb->lnum]) {
384 			err = -ENOMEM;
385 			goto out_free;
386 		}
387 		memset(leb[seb->lnum], 0, ubi->vtbl_size);
388 
389 		err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0,
390 				       ubi->vtbl_size);
391 		if (err == UBI_IO_BITFLIPS || err == -EBADMSG)
392 			/*
393 			 * Scrub the PEB later. Note, -EBADMSG indicates an
394 			 * uncorrectable ECC error, but we have our own CRC and
395 			 * the data will be checked later. If the data is OK,
396 			 * the PEB will be scrubbed (because we set
397 			 * seb->scrub). If the data is not OK, the contents of
398 			 * the PEB will be recovered from the second copy, and
399 			 * seb->scrub will be cleared in
400 			 * 'ubi_scan_add_used()'.
401 			 */
402 			seb->scrub = 1;
403 		else if (err)
404 			goto out_free;
405 	}
406 
407 	err = -EINVAL;
408 	if (leb[0]) {
409 		leb_corrupted[0] = vtbl_check(ubi, leb[0]);
410 		if (leb_corrupted[0] < 0)
411 			goto out_free;
412 	}
413 
414 	if (!leb_corrupted[0]) {
415 		/* LEB 0 is OK */
416 		if (leb[1])
417 			leb_corrupted[1] = memcmp(leb[0], leb[1], ubi->vtbl_size);
418 		if (leb_corrupted[1]) {
419 			ubi_warn("volume table copy #2 is corrupted");
420 			err = create_vtbl(ubi, si, 1, leb[0]);
421 			if (err)
422 				goto out_free;
423 			ubi_msg("volume table was restored");
424 		}
425 
426 		/* Both LEB 1 and LEB 2 are OK and consistent */
427 		vfree(leb[1]);
428 		return leb[0];
429 	} else {
430 		/* LEB 0 is corrupted or does not exist */
431 		if (leb[1]) {
432 			leb_corrupted[1] = vtbl_check(ubi, leb[1]);
433 			if (leb_corrupted[1] < 0)
434 				goto out_free;
435 		}
436 		if (leb_corrupted[1]) {
437 			/* Both LEB 0 and LEB 1 are corrupted */
438 			ubi_err("both volume tables are corrupted");
439 			goto out_free;
440 		}
441 
442 		ubi_warn("volume table copy #1 is corrupted");
443 		err = create_vtbl(ubi, si, 0, leb[1]);
444 		if (err)
445 			goto out_free;
446 		ubi_msg("volume table was restored");
447 
448 		vfree(leb[0]);
449 		return leb[1];
450 	}
451 
452 out_free:
453 	vfree(leb[0]);
454 	vfree(leb[1]);
455 	return ERR_PTR(err);
456 }
457 
458 /**
459  * create_empty_lvol - create empty layout volume.
460  * @ubi: UBI device description object
461  * @si: scanning information
462  *
463  * This function returns volume table contents in case of success and a
464  * negative error code in case of failure.
465  */
466 static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
467 						 struct ubi_scan_info *si)
468 {
469 	int i;
470 	struct ubi_vtbl_record *vtbl;
471 
472 	vtbl = vmalloc(ubi->vtbl_size);
473 	if (!vtbl)
474 		return ERR_PTR(-ENOMEM);
475 	memset(vtbl, 0, ubi->vtbl_size);
476 
477 	for (i = 0; i < ubi->vtbl_slots; i++)
478 		memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
479 
480 	for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
481 		int err;
482 
483 		err = create_vtbl(ubi, si, i, vtbl);
484 		if (err) {
485 			vfree(vtbl);
486 			return ERR_PTR(err);
487 		}
488 	}
489 
490 	return vtbl;
491 }
492 
493 /**
494  * init_volumes - initialize volume information for existing volumes.
495  * @ubi: UBI device description object
496  * @si: scanning information
497  * @vtbl: volume table
498  *
499  * This function allocates volume description objects for existing volumes.
500  * Returns zero in case of success and a negative error code in case of
501  * failure.
502  */
503 static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
504 			const struct ubi_vtbl_record *vtbl)
505 {
506 	int i, reserved_pebs = 0;
507 	struct ubi_scan_volume *sv;
508 	struct ubi_volume *vol;
509 
510 	for (i = 0; i < ubi->vtbl_slots; i++) {
511 		cond_resched();
512 
513 		if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
514 			continue; /* Empty record */
515 
516 		vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
517 		if (!vol)
518 			return -ENOMEM;
519 
520 		vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
521 		vol->alignment = be32_to_cpu(vtbl[i].alignment);
522 		vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
523 		vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
524 					UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
525 		vol->name_len = be16_to_cpu(vtbl[i].name_len);
526 		vol->usable_leb_size = ubi->leb_size - vol->data_pad;
527 		memcpy(vol->name, vtbl[i].name, vol->name_len);
528 		vol->name[vol->name_len] = '\0';
529 		vol->vol_id = i;
530 
531 		if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
532 			/* Auto re-size flag may be set only for one volume */
533 			if (ubi->autoresize_vol_id != -1) {
534 				ubi_err("more then one auto-resize volume (%d "
535 					"and %d)", ubi->autoresize_vol_id, i);
536 				kfree(vol);
537 				return -EINVAL;
538 			}
539 
540 			ubi->autoresize_vol_id = i;
541 		}
542 
543 		ubi_assert(!ubi->volumes[i]);
544 		ubi->volumes[i] = vol;
545 		ubi->vol_count += 1;
546 		vol->ubi = ubi;
547 		reserved_pebs += vol->reserved_pebs;
548 
549 		/*
550 		 * In case of dynamic volume UBI knows nothing about how many
551 		 * data is stored there. So assume the whole volume is used.
552 		 */
553 		if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
554 			vol->used_ebs = vol->reserved_pebs;
555 			vol->last_eb_bytes = vol->usable_leb_size;
556 			vol->used_bytes =
557 				(long long)vol->used_ebs * vol->usable_leb_size;
558 			continue;
559 		}
560 
561 		/* Static volumes only */
562 		sv = ubi_scan_find_sv(si, i);
563 		if (!sv) {
564 			/*
565 			 * No eraseblocks belonging to this volume found. We
566 			 * don't actually know whether this static volume is
567 			 * completely corrupted or just contains no data. And
568 			 * we cannot know this as long as data size is not
569 			 * stored on flash. So we just assume the volume is
570 			 * empty. FIXME: this should be handled.
571 			 */
572 			continue;
573 		}
574 
575 		if (sv->leb_count != sv->used_ebs) {
576 			/*
577 			 * We found a static volume which misses several
578 			 * eraseblocks. Treat it as corrupted.
579 			 */
580 			ubi_warn("static volume %d misses %d LEBs - corrupted",
581 				 sv->vol_id, sv->used_ebs - sv->leb_count);
582 			vol->corrupted = 1;
583 			continue;
584 		}
585 
586 		vol->used_ebs = sv->used_ebs;
587 		vol->used_bytes =
588 			(long long)(vol->used_ebs - 1) * vol->usable_leb_size;
589 		vol->used_bytes += sv->last_data_size;
590 		vol->last_eb_bytes = sv->last_data_size;
591 	}
592 
593 	/* And add the layout volume */
594 	vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
595 	if (!vol)
596 		return -ENOMEM;
597 
598 	vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
599 	vol->alignment = 1;
600 	vol->vol_type = UBI_DYNAMIC_VOLUME;
601 	vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
602 	memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
603 	vol->usable_leb_size = ubi->leb_size;
604 	vol->used_ebs = vol->reserved_pebs;
605 	vol->last_eb_bytes = vol->reserved_pebs;
606 	vol->used_bytes =
607 		(long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
608 	vol->vol_id = UBI_LAYOUT_VOLUME_ID;
609 	vol->ref_count = 1;
610 
611 	ubi_assert(!ubi->volumes[i]);
612 	ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
613 	reserved_pebs += vol->reserved_pebs;
614 	ubi->vol_count += 1;
615 	vol->ubi = ubi;
616 
617 	if (reserved_pebs > ubi->avail_pebs)
618 		ubi_err("not enough PEBs, required %d, available %d",
619 			reserved_pebs, ubi->avail_pebs);
620 	ubi->rsvd_pebs += reserved_pebs;
621 	ubi->avail_pebs -= reserved_pebs;
622 
623 	return 0;
624 }
625 
626 /**
627  * check_sv - check volume scanning information.
628  * @vol: UBI volume description object
629  * @sv: volume scanning information
630  *
631  * This function returns zero if the volume scanning information is consistent
632  * to the data read from the volume tabla, and %-EINVAL if not.
633  */
634 static int check_sv(const struct ubi_volume *vol,
635 		    const struct ubi_scan_volume *sv)
636 {
637 	int err;
638 
639 	if (sv->highest_lnum >= vol->reserved_pebs) {
640 		err = 1;
641 		goto bad;
642 	}
643 	if (sv->leb_count > vol->reserved_pebs) {
644 		err = 2;
645 		goto bad;
646 	}
647 	if (sv->vol_type != vol->vol_type) {
648 		err = 3;
649 		goto bad;
650 	}
651 	if (sv->used_ebs > vol->reserved_pebs) {
652 		err = 4;
653 		goto bad;
654 	}
655 	if (sv->data_pad != vol->data_pad) {
656 		err = 5;
657 		goto bad;
658 	}
659 	return 0;
660 
661 bad:
662 	ubi_err("bad scanning information, error %d", err);
663 	ubi_dbg_dump_sv(sv);
664 	ubi_dbg_dump_vol_info(vol);
665 	return -EINVAL;
666 }
667 
668 /**
669  * check_scanning_info - check that scanning information.
670  * @ubi: UBI device description object
671  * @si: scanning information
672  *
673  * Even though we protect on-flash data by CRC checksums, we still don't trust
674  * the media. This function ensures that scanning information is consistent to
675  * the information read from the volume table. Returns zero if the scanning
676  * information is OK and %-EINVAL if it is not.
677  */
678 static int check_scanning_info(const struct ubi_device *ubi,
679 			       struct ubi_scan_info *si)
680 {
681 	int err, i;
682 	struct ubi_scan_volume *sv;
683 	struct ubi_volume *vol;
684 
685 	if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
686 		ubi_err("scanning found %d volumes, maximum is %d + %d",
687 			si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
688 		return -EINVAL;
689 	}
690 
691 	if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
692 	    si->highest_vol_id < UBI_INTERNAL_VOL_START) {
693 		ubi_err("too large volume ID %d found by scanning",
694 			si->highest_vol_id);
695 		return -EINVAL;
696 	}
697 
698 	for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
699 		cond_resched();
700 
701 		sv = ubi_scan_find_sv(si, i);
702 		vol = ubi->volumes[i];
703 		if (!vol) {
704 			if (sv)
705 				ubi_scan_rm_volume(si, sv);
706 			continue;
707 		}
708 
709 		if (vol->reserved_pebs == 0) {
710 			ubi_assert(i < ubi->vtbl_slots);
711 
712 			if (!sv)
713 				continue;
714 
715 			/*
716 			 * During scanning we found a volume which does not
717 			 * exist according to the information in the volume
718 			 * table. This must have happened due to an unclean
719 			 * reboot while the volume was being removed. Discard
720 			 * these eraseblocks.
721 			 */
722 			ubi_msg("finish volume %d removal", sv->vol_id);
723 			ubi_scan_rm_volume(si, sv);
724 		} else if (sv) {
725 			err = check_sv(vol, sv);
726 			if (err)
727 				return err;
728 		}
729 	}
730 
731 	return 0;
732 }
733 
734 /**
735  * ubi_read_volume_table - read volume table.
736  * information.
737  * @ubi: UBI device description object
738  * @si: scanning information
739  *
740  * This function reads volume table, checks it, recover from errors if needed,
741  * or creates it if needed. Returns zero in case of success and a negative
742  * error code in case of failure.
743  */
744 int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
745 {
746 	int i, err;
747 	struct ubi_scan_volume *sv;
748 
749 	empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
750 
751 	/*
752 	 * The number of supported volumes is limited by the eraseblock size
753 	 * and by the UBI_MAX_VOLUMES constant.
754 	 */
755 	ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
756 	if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
757 		ubi->vtbl_slots = UBI_MAX_VOLUMES;
758 
759 	ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
760 	ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
761 
762 	sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID);
763 	if (!sv) {
764 		/*
765 		 * No logical eraseblocks belonging to the layout volume were
766 		 * found. This could mean that the flash is just empty. In
767 		 * this case we create empty layout volume.
768 		 *
769 		 * But if flash is not empty this must be a corruption or the
770 		 * MTD device just contains garbage.
771 		 */
772 		if (si->is_empty) {
773 			ubi->vtbl = create_empty_lvol(ubi, si);
774 			if (IS_ERR(ubi->vtbl))
775 				return PTR_ERR(ubi->vtbl);
776 		} else {
777 			ubi_err("the layout volume was not found");
778 			return -EINVAL;
779 		}
780 	} else {
781 		if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) {
782 			/* This must not happen with proper UBI images */
783 			dbg_err("too many LEBs (%d) in layout volume",
784 				sv->leb_count);
785 			return -EINVAL;
786 		}
787 
788 		ubi->vtbl = process_lvol(ubi, si, sv);
789 		if (IS_ERR(ubi->vtbl))
790 			return PTR_ERR(ubi->vtbl);
791 	}
792 
793 	ubi->avail_pebs = ubi->good_peb_count;
794 
795 	/*
796 	 * The layout volume is OK, initialize the corresponding in-RAM data
797 	 * structures.
798 	 */
799 	err = init_volumes(ubi, si, ubi->vtbl);
800 	if (err)
801 		goto out_free;
802 
803 	/*
804 	 * Get sure that the scanning information is consistent to the
805 	 * information stored in the volume table.
806 	 */
807 	err = check_scanning_info(ubi, si);
808 	if (err)
809 		goto out_free;
810 
811 	return 0;
812 
813 out_free:
814 	vfree(ubi->vtbl);
815 	for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++)
816 		if (ubi->volumes[i]) {
817 			kfree(ubi->volumes[i]);
818 			ubi->volumes[i] = NULL;
819 		}
820 	return err;
821 }
822 
823 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
824 
825 /**
826  * paranoid_vtbl_check - check volume table.
827  * @ubi: UBI device description object
828  */
829 static void paranoid_vtbl_check(const struct ubi_device *ubi)
830 {
831 	if (vtbl_check(ubi, ubi->vtbl)) {
832 		ubi_err("paranoid check failed");
833 		BUG();
834 	}
835 }
836 
837 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */
838