xref: /linux/drivers/mtd/ubi/vtbl.c (revision 2b8232ce512105e28453f301d1510de8363bccd1)
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>
61 #include <asm/div64.h>
62 #include "ubi.h"
63 
64 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
65 static 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 */
71 static 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  */
84 int 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_be32(crc);
97 	}
98 
99 	mutex_lock(&ubi->vtbl_mutex);
100 	memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
101 	for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
102 		err = ubi_eba_unmap_leb(ubi, UBI_LAYOUT_VOL_ID, i);
103 		if (err) {
104 			mutex_unlock(&ubi->vtbl_mutex);
105 			return err;
106 		}
107 		err = ubi_eba_write_leb(ubi, UBI_LAYOUT_VOL_ID, i, ubi->vtbl, 0,
108 					ubi->vtbl_size, UBI_LONGTERM);
109 		if (err) {
110 			mutex_unlock(&ubi->vtbl_mutex);
111 			return err;
112 		}
113 	}
114 
115 	paranoid_vtbl_check(ubi);
116 	mutex_unlock(&ubi->vtbl_mutex);
117 	return ubi_wl_flush(ubi);
118 }
119 
120 /**
121  * vol_til_check - check if volume table is not corrupted and contains sensible
122  * data.
123  *
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;
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 = &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 				dbg_err("bad empty record");
161 				goto bad;
162 			}
163 			continue;
164 		}
165 
166 		if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
167 		    name_len < 0) {
168 			dbg_err("negative values");
169 			goto bad;
170 		}
171 
172 		if (alignment > ubi->leb_size || alignment == 0) {
173 			dbg_err("bad alignment");
174 			goto bad;
175 		}
176 
177 		n = alignment % ubi->min_io_size;
178 		if (alignment != 1 && n) {
179 			dbg_err("alignment is not multiple of min I/O unit");
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 			goto bad;
187 		}
188 
189 		if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
190 			dbg_err("bad vol_type");
191 			goto bad;
192 		}
193 
194 		if (upd_marker != 0 && upd_marker != 1) {
195 			dbg_err("bad upd_marker");
196 			goto bad;
197 		}
198 
199 		if (reserved_pebs > ubi->good_peb_count) {
200 			dbg_err("too large reserved_pebs, good PEBs %d",
201 				ubi->good_peb_count);
202 			goto bad;
203 		}
204 
205 		if (name_len > UBI_VOL_NAME_MAX) {
206 			dbg_err("too long volume name, max %d",
207 				UBI_VOL_NAME_MAX);
208 			goto bad;
209 		}
210 
211 		if (name[0] == '\0') {
212 			dbg_err("NULL volume name");
213 			goto bad;
214 		}
215 
216 		if (name_len != strnlen(name, name_len + 1)) {
217 			dbg_err("bad name_len");
218 			goto bad;
219 		}
220 	}
221 
222 	/* Checks that all names are unique */
223 	for (i = 0; i < ubi->vtbl_slots - 1; i++) {
224 		for (n = i + 1; n < ubi->vtbl_slots; n++) {
225 			int len1 = be16_to_cpu(vtbl[i].name_len);
226 			int len2 = be16_to_cpu(vtbl[n].name_len);
227 
228 			if (len1 > 0 && len1 == len2 &&
229 			    !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
230 				ubi_err("volumes %d and %d have the same name"
231 					" \"%s\"", i, n, vtbl[i].name);
232 				ubi_dbg_dump_vtbl_record(&vtbl[i], i);
233 				ubi_dbg_dump_vtbl_record(&vtbl[n], n);
234 				return -EINVAL;
235 			}
236 		}
237 	}
238 
239 	return 0;
240 
241 bad:
242 	ubi_err("volume table check failed, record %d", i);
243 	ubi_dbg_dump_vtbl_record(&vtbl[i], i);
244 	return -EINVAL;
245 }
246 
247 /**
248  * create_vtbl - create a copy of volume table.
249  * @ubi: UBI device description object
250  * @si: scanning information
251  * @copy: number of the volume table copy
252  * @vtbl: contents of the volume table
253  *
254  * This function returns zero in case of success and a negative error code in
255  * case of failure.
256  */
257 static int create_vtbl(const struct ubi_device *ubi, struct ubi_scan_info *si,
258 		       int copy, void *vtbl)
259 {
260 	int err, tries = 0;
261 	static struct ubi_vid_hdr *vid_hdr;
262 	struct ubi_scan_volume *sv;
263 	struct ubi_scan_leb *new_seb, *old_seb = NULL;
264 
265 	ubi_msg("create volume table (copy #%d)", copy + 1);
266 
267 	vid_hdr = ubi_zalloc_vid_hdr(ubi);
268 	if (!vid_hdr)
269 		return -ENOMEM;
270 
271 	/*
272 	 * Check if there is a logical eraseblock which would have to contain
273 	 * this volume table copy was found during scanning. It has to be wiped
274 	 * out.
275 	 */
276 	sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOL_ID);
277 	if (sv)
278 		old_seb = ubi_scan_find_seb(sv, copy);
279 
280 retry:
281 	new_seb = ubi_scan_get_free_peb(ubi, si);
282 	if (IS_ERR(new_seb)) {
283 		err = PTR_ERR(new_seb);
284 		goto out_free;
285 	}
286 
287 	vid_hdr->vol_type = UBI_VID_DYNAMIC;
288 	vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOL_ID);
289 	vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
290 	vid_hdr->data_size = vid_hdr->used_ebs =
291 			     vid_hdr->data_pad = cpu_to_be32(0);
292 	vid_hdr->lnum = cpu_to_be32(copy);
293 	vid_hdr->sqnum = cpu_to_be64(++si->max_sqnum);
294 	vid_hdr->leb_ver = cpu_to_be32(old_seb ? old_seb->leb_ver + 1: 0);
295 
296 	/* The EC header is already there, write the VID header */
297 	err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr);
298 	if (err)
299 		goto write_error;
300 
301 	/* Write the layout volume contents */
302 	err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size);
303 	if (err)
304 		goto write_error;
305 
306 	/*
307 	 * And add it to the scanning information. Don't delete the old
308 	 * @old_seb as it will be deleted and freed in 'ubi_scan_add_used()'.
309 	 */
310 	err = ubi_scan_add_used(ubi, si, new_seb->pnum, new_seb->ec,
311 				vid_hdr, 0);
312 	kfree(new_seb);
313 	ubi_free_vid_hdr(ubi, vid_hdr);
314 	return err;
315 
316 write_error:
317 	if (err == -EIO && ++tries <= 5) {
318 		/*
319 		 * Probably this physical eraseblock went bad, try to pick
320 		 * another one.
321 		 */
322 		list_add_tail(&new_seb->u.list, &si->corr);
323 		goto retry;
324 	}
325 	kfree(new_seb);
326 out_free:
327 	ubi_free_vid_hdr(ubi, vid_hdr);
328 	return err;
329 
330 }
331 
332 /**
333  * process_lvol - process the layout volume.
334  * @ubi: UBI device description object
335  * @si: scanning information
336  * @sv: layout volume scanning information
337  *
338  * This function is responsible for reading the layout volume, ensuring it is
339  * not corrupted, and recovering from corruptions if needed. Returns volume
340  * table in case of success and a negative error code in case of failure.
341  */
342 static struct ubi_vtbl_record *process_lvol(const struct ubi_device *ubi,
343 					    struct ubi_scan_info *si,
344 					    struct ubi_scan_volume *sv)
345 {
346 	int err;
347 	struct rb_node *rb;
348 	struct ubi_scan_leb *seb;
349 	struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
350 	int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
351 
352 	/*
353 	 * UBI goes through the following steps when it changes the layout
354 	 * volume:
355 	 * a. erase LEB 0;
356 	 * b. write new data to LEB 0;
357 	 * c. erase LEB 1;
358 	 * d. write new data to LEB 1.
359 	 *
360 	 * Before the change, both LEBs contain the same data.
361 	 *
362 	 * Due to unclean reboots, the contents of LEB 0 may be lost, but there
363 	 * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
364 	 * Similarly, LEB 1 may be lost, but there should be LEB 0. And
365 	 * finally, unclean reboots may result in a situation when neither LEB
366 	 * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
367 	 * 0 contains more recent information.
368 	 *
369 	 * So the plan is to first check LEB 0. Then
370 	 * a. if LEB 0 is OK, it must be containing the most resent data; then
371 	 *    we compare it with LEB 1, and if they are different, we copy LEB
372 	 *    0 to LEB 1;
373 	 * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
374 	 *    to LEB 0.
375 	 */
376 
377 	dbg_msg("check layout volume");
378 
379 	/* Read both LEB 0 and LEB 1 into memory */
380 	ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
381 		leb[seb->lnum] = vmalloc(ubi->vtbl_size);
382 		if (!leb[seb->lnum]) {
383 			err = -ENOMEM;
384 			goto out_free;
385 		}
386 		memset(leb[seb->lnum], 0, ubi->vtbl_size);
387 
388 		err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0,
389 				       ubi->vtbl_size);
390 		if (err == UBI_IO_BITFLIPS || err == -EBADMSG)
391 			/* Scrub the PEB later */
392 			seb->scrub = 1;
393 		else if (err)
394 			goto out_free;
395 	}
396 
397 	err = -EINVAL;
398 	if (leb[0]) {
399 		leb_corrupted[0] = vtbl_check(ubi, leb[0]);
400 		if (leb_corrupted[0] < 0)
401 			goto out_free;
402 	}
403 
404 	if (!leb_corrupted[0]) {
405 		/* LEB 0 is OK */
406 		if (leb[1])
407 			leb_corrupted[1] = memcmp(leb[0], leb[1], ubi->vtbl_size);
408 		if (leb_corrupted[1]) {
409 			ubi_warn("volume table copy #2 is corrupted");
410 			err = create_vtbl(ubi, si, 1, leb[0]);
411 			if (err)
412 				goto out_free;
413 			ubi_msg("volume table was restored");
414 		}
415 
416 		/* Both LEB 1 and LEB 2 are OK and consistent */
417 		vfree(leb[1]);
418 		return leb[0];
419 	} else {
420 		/* LEB 0 is corrupted or does not exist */
421 		if (leb[1]) {
422 			leb_corrupted[1] = vtbl_check(ubi, leb[1]);
423 			if (leb_corrupted[1] < 0)
424 				goto out_free;
425 		}
426 		if (leb_corrupted[1]) {
427 			/* Both LEB 0 and LEB 1 are corrupted */
428 			ubi_err("both volume tables are corrupted");
429 			goto out_free;
430 		}
431 
432 		ubi_warn("volume table copy #1 is corrupted");
433 		err = create_vtbl(ubi, si, 0, leb[1]);
434 		if (err)
435 			goto out_free;
436 		ubi_msg("volume table was restored");
437 
438 		vfree(leb[0]);
439 		return leb[1];
440 	}
441 
442 out_free:
443 	vfree(leb[0]);
444 	vfree(leb[1]);
445 	return ERR_PTR(err);
446 }
447 
448 /**
449  * create_empty_lvol - create empty layout volume.
450  * @ubi: UBI device description object
451  * @si: scanning information
452  *
453  * This function returns volume table contents in case of success and a
454  * negative error code in case of failure.
455  */
456 static struct ubi_vtbl_record *create_empty_lvol(const struct ubi_device *ubi,
457 						 struct ubi_scan_info *si)
458 {
459 	int i;
460 	struct ubi_vtbl_record *vtbl;
461 
462 	vtbl = vmalloc(ubi->vtbl_size);
463 	if (!vtbl)
464 		return ERR_PTR(-ENOMEM);
465 	memset(vtbl, 0, ubi->vtbl_size);
466 
467 	for (i = 0; i < ubi->vtbl_slots; i++)
468 		memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
469 
470 	for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
471 		int err;
472 
473 		err = create_vtbl(ubi, si, i, vtbl);
474 		if (err) {
475 			vfree(vtbl);
476 			return ERR_PTR(err);
477 		}
478 	}
479 
480 	return vtbl;
481 }
482 
483 /**
484  * init_volumes - initialize volume information for existing volumes.
485  * @ubi: UBI device description object
486  * @si: scanning information
487  * @vtbl: volume table
488  *
489  * This function allocates volume description objects for existing volumes.
490  * Returns zero in case of success and a negative error code in case of
491  * failure.
492  */
493 static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
494 			const struct ubi_vtbl_record *vtbl)
495 {
496 	int i, reserved_pebs = 0;
497 	struct ubi_scan_volume *sv;
498 	struct ubi_volume *vol;
499 
500 	for (i = 0; i < ubi->vtbl_slots; i++) {
501 		cond_resched();
502 
503 		if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
504 			continue; /* Empty record */
505 
506 		vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
507 		if (!vol)
508 			return -ENOMEM;
509 
510 		vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
511 		vol->alignment = be32_to_cpu(vtbl[i].alignment);
512 		vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
513 		vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
514 					UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
515 		vol->name_len = be16_to_cpu(vtbl[i].name_len);
516 		vol->usable_leb_size = ubi->leb_size - vol->data_pad;
517 		memcpy(vol->name, vtbl[i].name, vol->name_len);
518 		vol->name[vol->name_len] = '\0';
519 		vol->vol_id = i;
520 
521 		ubi_assert(!ubi->volumes[i]);
522 		ubi->volumes[i] = vol;
523 		ubi->vol_count += 1;
524 		vol->ubi = ubi;
525 		reserved_pebs += vol->reserved_pebs;
526 
527 		/*
528 		 * In case of dynamic volume UBI knows nothing about how many
529 		 * data is stored there. So assume the whole volume is used.
530 		 */
531 		if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
532 			vol->used_ebs = vol->reserved_pebs;
533 			vol->last_eb_bytes = vol->usable_leb_size;
534 			vol->used_bytes =
535 				(long long)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 =
566 			(long long)(vol->used_ebs - 1) * vol->usable_leb_size;
567 		vol->used_bytes += sv->last_data_size;
568 		vol->last_eb_bytes = sv->last_data_size;
569 	}
570 
571 	vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
572 	if (!vol)
573 		return -ENOMEM;
574 
575 	vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
576 	vol->alignment = 1;
577 	vol->vol_type = UBI_DYNAMIC_VOLUME;
578 	vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
579 	memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
580 	vol->usable_leb_size = ubi->leb_size;
581 	vol->used_ebs = vol->reserved_pebs;
582 	vol->last_eb_bytes = vol->reserved_pebs;
583 	vol->used_bytes =
584 		(long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
585 	vol->vol_id = UBI_LAYOUT_VOL_ID;
586 
587 	ubi_assert(!ubi->volumes[i]);
588 	ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
589 	reserved_pebs += vol->reserved_pebs;
590 	ubi->vol_count += 1;
591 	vol->ubi = ubi;
592 
593 	if (reserved_pebs > ubi->avail_pebs)
594 		ubi_err("not enough PEBs, required %d, available %d",
595 			reserved_pebs, ubi->avail_pebs);
596 	ubi->rsvd_pebs += reserved_pebs;
597 	ubi->avail_pebs -= reserved_pebs;
598 
599 	return 0;
600 }
601 
602 /**
603  * check_sv - check volume scanning information.
604  * @vol: UBI volume description object
605  * @sv: volume scanning information
606  *
607  * This function returns zero if the volume scanning information is consistent
608  * to the data read from the volume tabla, and %-EINVAL if not.
609  */
610 static int check_sv(const struct ubi_volume *vol,
611 		    const struct ubi_scan_volume *sv)
612 {
613 	if (sv->highest_lnum >= vol->reserved_pebs) {
614 		dbg_err("bad highest_lnum");
615 		goto bad;
616 	}
617 	if (sv->leb_count > vol->reserved_pebs) {
618 		dbg_err("bad leb_count");
619 		goto bad;
620 	}
621 	if (sv->vol_type != vol->vol_type) {
622 		dbg_err("bad vol_type");
623 		goto bad;
624 	}
625 	if (sv->used_ebs > vol->reserved_pebs) {
626 		dbg_err("bad used_ebs");
627 		goto bad;
628 	}
629 	if (sv->data_pad != vol->data_pad) {
630 		dbg_err("bad data_pad");
631 		goto bad;
632 	}
633 	return 0;
634 
635 bad:
636 	ubi_err("bad scanning information");
637 	ubi_dbg_dump_sv(sv);
638 	ubi_dbg_dump_vol_info(vol);
639 	return -EINVAL;
640 }
641 
642 /**
643  * check_scanning_info - check that scanning information.
644  * @ubi: UBI device description object
645  * @si: scanning information
646  *
647  * Even though we protect on-flash data by CRC checksums, we still don't trust
648  * the media. This function ensures that scanning information is consistent to
649  * the information read from the volume table. Returns zero if the scanning
650  * information is OK and %-EINVAL if it is not.
651  */
652 static int check_scanning_info(const struct ubi_device *ubi,
653 			       struct ubi_scan_info *si)
654 {
655 	int err, i;
656 	struct ubi_scan_volume *sv;
657 	struct ubi_volume *vol;
658 
659 	if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
660 		ubi_err("scanning found %d volumes, maximum is %d + %d",
661 			si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
662 		return -EINVAL;
663 	}
664 
665 	if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT&&
666 	    si->highest_vol_id < UBI_INTERNAL_VOL_START) {
667 		ubi_err("too large volume ID %d found by scanning",
668 			si->highest_vol_id);
669 		return -EINVAL;
670 	}
671 
672 
673 	for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
674 		cond_resched();
675 
676 		sv = ubi_scan_find_sv(si, i);
677 		vol = ubi->volumes[i];
678 		if (!vol) {
679 			if (sv)
680 				ubi_scan_rm_volume(si, sv);
681 			continue;
682 		}
683 
684 		if (vol->reserved_pebs == 0) {
685 			ubi_assert(i < ubi->vtbl_slots);
686 
687 			if (!sv)
688 				continue;
689 
690 			/*
691 			 * During scanning we found a volume which does not
692 			 * exist according to the information in the volume
693 			 * table. This must have happened due to an unclean
694 			 * reboot while the volume was being removed. Discard
695 			 * these eraseblocks.
696 			 */
697 			ubi_msg("finish volume %d removal", sv->vol_id);
698 			ubi_scan_rm_volume(si, sv);
699 		} else if (sv) {
700 			err = check_sv(vol, sv);
701 			if (err)
702 				return err;
703 		}
704 	}
705 
706 	return 0;
707 }
708 
709 /**
710  * ubi_read_volume_table - read volume table.
711  * information.
712  * @ubi: UBI device description object
713  * @si: scanning information
714  *
715  * This function reads volume table, checks it, recover from errors if needed,
716  * or creates it if needed. Returns zero in case of success and a negative
717  * error code in case of failure.
718  */
719 int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
720 {
721 	int i, err;
722 	struct ubi_scan_volume *sv;
723 
724 	empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
725 
726 	/*
727 	 * The number of supported volumes is limited by the eraseblock size
728 	 * and by the UBI_MAX_VOLUMES constant.
729 	 */
730 	ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
731 	if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
732 		ubi->vtbl_slots = UBI_MAX_VOLUMES;
733 
734 	ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
735 	ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
736 
737 	sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOL_ID);
738 	if (!sv) {
739 		/*
740 		 * No logical eraseblocks belonging to the layout volume were
741 		 * found. This could mean that the flash is just empty. In
742 		 * this case we create empty layout volume.
743 		 *
744 		 * But if flash is not empty this must be a corruption or the
745 		 * MTD device just contains garbage.
746 		 */
747 		if (si->is_empty) {
748 			ubi->vtbl = create_empty_lvol(ubi, si);
749 			if (IS_ERR(ubi->vtbl))
750 				return PTR_ERR(ubi->vtbl);
751 		} else {
752 			ubi_err("the layout volume was not found");
753 			return -EINVAL;
754 		}
755 	} else {
756 		if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) {
757 			/* This must not happen with proper UBI images */
758 			dbg_err("too many LEBs (%d) in layout volume",
759 				sv->leb_count);
760 			return -EINVAL;
761 		}
762 
763 		ubi->vtbl = process_lvol(ubi, si, sv);
764 		if (IS_ERR(ubi->vtbl))
765 			return PTR_ERR(ubi->vtbl);
766 	}
767 
768 	ubi->avail_pebs = ubi->good_peb_count;
769 
770 	/*
771 	 * The layout volume is OK, initialize the corresponding in-RAM data
772 	 * structures.
773 	 */
774 	err = init_volumes(ubi, si, ubi->vtbl);
775 	if (err)
776 		goto out_free;
777 
778 	/*
779 	 * Get sure that the scanning information is consistent to the
780 	 * information stored in the volume table.
781 	 */
782 	err = check_scanning_info(ubi, si);
783 	if (err)
784 		goto out_free;
785 
786 	return 0;
787 
788 out_free:
789 	vfree(ubi->vtbl);
790 	for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++)
791 		if (ubi->volumes[i]) {
792 			kfree(ubi->volumes[i]);
793 			ubi->volumes[i] = NULL;
794 		}
795 	return err;
796 }
797 
798 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
799 
800 /**
801  * paranoid_vtbl_check - check volume table.
802  * @ubi: UBI device description object
803  */
804 static void paranoid_vtbl_check(const struct ubi_device *ubi)
805 {
806 	if (vtbl_check(ubi, ubi->vtbl)) {
807 		ubi_err("paranoid check failed");
808 		BUG();
809 	}
810 }
811 
812 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */
813