1279377Simp// SPDX-License-Identifier: GPL-2.0-or-later
2279377Simp/*
3279377Simp * Copyright (c) International Business Machines Corp., 2006
4279377Simp * Copyright (c) Nokia Corporation, 2006, 2007
5279377Simp *
6279377Simp * Author: Artem Bityutskiy (���������������� ����������)
7279377Simp */
8279377Simp
9279377Simp/*
10279377Simp * This file includes volume table manipulation code. The volume table is an
11279377Simp * on-flash table containing volume meta-data like name, number of reserved
12279377Simp * physical eraseblocks, type, etc. The volume table is stored in the so-called
13279377Simp * "layout volume".
14279377Simp *
15279377Simp * The layout volume is an internal volume which is organized as follows. It
16279377Simp * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical
17279377Simp * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each
18279377Simp * other. This redundancy guarantees robustness to unclean reboots. The volume
19279377Simp * table is basically an array of volume table records. Each record contains
20279377Simp * full information about the volume and protected by a CRC checksum. Note,
21279377Simp * nowadays we use the atomic LEB change operation when updating the volume
22279377Simp * table, so we do not really need 2 LEBs anymore, but we preserve the older
23279377Simp * design for the backward compatibility reasons.
24279377Simp *
25279377Simp * When the volume table is changed, it is first changed in RAM. Then LEB 0 is
26279377Simp * erased, and the updated volume table is written back to LEB 0. Then same for
27279377Simp * LEB 1. This scheme guarantees recoverability from unclean reboots.
28279377Simp *
29279377Simp * In this UBI implementation the on-flash volume table does not contain any
30279377Simp * information about how much data static volumes contain.
31279377Simp *
32279377Simp * But it would still be beneficial to store this information in the volume
33279377Simp * table. For example, suppose we have a static volume X, and all its physical
34279377Simp * eraseblocks became bad for some reasons. Suppose we are attaching the
35279377Simp * corresponding MTD device, for some reason we find no logical eraseblocks
36279377Simp * corresponding to the volume X. According to the volume table volume X does
37279377Simp * exist. So we don't know whether it is just empty or all its physical
38279377Simp * eraseblocks went bad. So we cannot alarm the user properly.
39279377Simp *
40279377Simp * The volume table also stores so-called "update marker", which is used for
41279377Simp * volume updates. Before updating the volume, the update marker is set, and
42279377Simp * after the update operation is finished, the update marker is cleared. So if
43279377Simp * the update operation was interrupted (e.g. by an unclean reboot) - the
44279377Simp * update marker is still there and we know that the volume's contents is
45279377Simp * damaged.
46279377Simp */
47279377Simp
48279377Simp#include <linux/crc32.h>
49279377Simp#include <linux/err.h>
50279377Simp#include <linux/slab.h>
51279377Simp#include <asm/div64.h>
52279377Simp#include "ubi.h"
53279377Simp
54279377Simpstatic void self_vtbl_check(const struct ubi_device *ubi);
55279377Simp
56279377Simp/* Empty volume table record */
57279377Simpstatic struct ubi_vtbl_record empty_vtbl_record;
58279377Simp
59279377Simp/**
60279377Simp * ubi_update_layout_vol - helper for updatting layout volumes on flash
61279377Simp * @ubi: UBI device description object
62279377Simp */
63279377Simpstatic int ubi_update_layout_vol(struct ubi_device *ubi)
64279377Simp{
65279377Simp	struct ubi_volume *layout_vol;
66279377Simp	int i, err;
67279377Simp
68279377Simp	layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
69279377Simp	for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
70279377Simp		err = ubi_eba_atomic_leb_change(ubi, layout_vol, i, ubi->vtbl,
71279377Simp						ubi->vtbl_size);
72279377Simp		if (err)
73279377Simp			return err;
74279377Simp	}
75279377Simp
76279377Simp	return 0;
77279377Simp}
78279377Simp
79279377Simp/**
80279377Simp * ubi_change_vtbl_record - change volume table record.
81279377Simp * @ubi: UBI device description object
82279377Simp * @idx: table index to change
83279377Simp * @vtbl_rec: new volume table record
84279377Simp *
85279377Simp * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
86279377Simp * volume table record is written. The caller does not have to calculate CRC of
87279377Simp * the record as it is done by this function. Returns zero in case of success
88279377Simp * and a negative error code in case of failure.
89279377Simp */
90279377Simpint ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
91279377Simp			   struct ubi_vtbl_record *vtbl_rec)
92279377Simp{
93279377Simp	int err;
94279377Simp	uint32_t crc;
95279377Simp
96279377Simp	ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
97279377Simp
98279377Simp	if (!vtbl_rec)
99279377Simp		vtbl_rec = &empty_vtbl_record;
100279377Simp	else {
101279377Simp		crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
102279377Simp		vtbl_rec->crc = cpu_to_be32(crc);
103279377Simp	}
104279377Simp
105279377Simp	memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
106279377Simp	err = ubi_update_layout_vol(ubi);
107279377Simp
108279377Simp	self_vtbl_check(ubi);
109279377Simp	return err ? err : 0;
110279377Simp}
111279377Simp
112279377Simp/**
113279377Simp * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
114279377Simp * @ubi: UBI device description object
115279377Simp * @rename_list: list of &struct ubi_rename_entry objects
116279377Simp *
117279377Simp * This function re-names multiple volumes specified in @req in the volume
118279377Simp * table. Returns zero in case of success and a negative error code in case of
119279377Simp * failure.
120279377Simp */
121279377Simpint ubi_vtbl_rename_volumes(struct ubi_device *ubi,
122279377Simp			    struct list_head *rename_list)
123279377Simp{
124279377Simp	struct ubi_rename_entry *re;
125279377Simp
126279377Simp	list_for_each_entry(re, rename_list, list) {
127279377Simp		uint32_t crc;
128279377Simp		struct ubi_volume *vol = re->desc->vol;
129279377Simp		struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id];
130279377Simp
131279377Simp		if (re->remove) {
132279377Simp			memcpy(vtbl_rec, &empty_vtbl_record,
133279377Simp			       sizeof(struct ubi_vtbl_record));
134279377Simp			continue;
135279377Simp		}
136279377Simp
137279377Simp		vtbl_rec->name_len = cpu_to_be16(re->new_name_len);
138279377Simp		memcpy(vtbl_rec->name, re->new_name, re->new_name_len);
139279377Simp		memset(vtbl_rec->name + re->new_name_len, 0,
140279377Simp		       UBI_VOL_NAME_MAX + 1 - re->new_name_len);
141279377Simp		crc = crc32(UBI_CRC32_INIT, vtbl_rec,
142279377Simp			    UBI_VTBL_RECORD_SIZE_CRC);
143279377Simp		vtbl_rec->crc = cpu_to_be32(crc);
144279377Simp	}
145279377Simp
146279377Simp	return ubi_update_layout_vol(ubi);
147279377Simp}
148279377Simp
149279377Simp/**
150279377Simp * vtbl_check - check if volume table is not corrupted and sensible.
151279377Simp * @ubi: UBI device description object
152279377Simp * @vtbl: volume table
153279377Simp *
154279377Simp * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
155279377Simp * and %-EINVAL if it contains inconsistent data.
156279377Simp */
157279377Simpstatic int vtbl_check(const struct ubi_device *ubi,
158279377Simp		      const struct ubi_vtbl_record *vtbl)
159279377Simp{
160279377Simp	int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
161279377Simp	int upd_marker, err;
162279377Simp	uint32_t crc;
163279377Simp	const char *name;
164279377Simp
165279377Simp	for (i = 0; i < ubi->vtbl_slots; i++) {
166279377Simp		cond_resched();
167279377Simp
168279377Simp		reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
169279377Simp		alignment = be32_to_cpu(vtbl[i].alignment);
170279377Simp		data_pad = be32_to_cpu(vtbl[i].data_pad);
171279377Simp		upd_marker = vtbl[i].upd_marker;
172279377Simp		vol_type = vtbl[i].vol_type;
173279377Simp		name_len = be16_to_cpu(vtbl[i].name_len);
174279377Simp		name = &vtbl[i].name[0];
175279377Simp
176279377Simp		crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
177279377Simp		if (be32_to_cpu(vtbl[i].crc) != crc) {
178279377Simp			ubi_err(ubi, "bad CRC at record %u: %#08x, not %#08x",
179279377Simp				 i, crc, be32_to_cpu(vtbl[i].crc));
180279377Simp			ubi_dump_vtbl_record(&vtbl[i], i);
181279377Simp			return 1;
182279377Simp		}
183279377Simp
184279377Simp		if (reserved_pebs == 0) {
185279377Simp			if (memcmp(&vtbl[i], &empty_vtbl_record,
186279377Simp						UBI_VTBL_RECORD_SIZE)) {
187279377Simp				err = 2;
188279377Simp				goto bad;
189279377Simp			}
190279377Simp			continue;
191279377Simp		}
192279377Simp
193279377Simp		if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
194279377Simp		    name_len < 0) {
195279377Simp			err = 3;
196279377Simp			goto bad;
197279377Simp		}
198279377Simp
199279377Simp		if (alignment > ubi->leb_size || alignment == 0) {
200279377Simp			err = 4;
201279377Simp			goto bad;
202279377Simp		}
203279377Simp
204279377Simp		n = alignment & (ubi->min_io_size - 1);
205279377Simp		if (alignment != 1 && n) {
206279377Simp			err = 5;
207279377Simp			goto bad;
208279377Simp		}
209279377Simp
210279377Simp		n = ubi->leb_size % alignment;
211279377Simp		if (data_pad != n) {
212279377Simp			ubi_err(ubi, "bad data_pad, has to be %d", n);
213279377Simp			err = 6;
214279377Simp			goto bad;
215279377Simp		}
216279377Simp
217279377Simp		if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
218279377Simp			err = 7;
219279377Simp			goto bad;
220279377Simp		}
221279377Simp
222279377Simp		if (upd_marker != 0 && upd_marker != 1) {
223279377Simp			err = 8;
224279377Simp			goto bad;
225279377Simp		}
226279377Simp
227279377Simp		if (reserved_pebs > ubi->good_peb_count) {
228279377Simp			ubi_err(ubi, "too large reserved_pebs %d, good PEBs %d",
229279377Simp				reserved_pebs, ubi->good_peb_count);
230279377Simp			err = 9;
231279377Simp			goto bad;
232279377Simp		}
233279377Simp
234279377Simp		if (name_len > UBI_VOL_NAME_MAX) {
235279377Simp			err = 10;
236279377Simp			goto bad;
237279377Simp		}
238279377Simp
239279377Simp		if (name[0] == '\0') {
240279377Simp			err = 11;
241279377Simp			goto bad;
242279377Simp		}
243279377Simp
244279377Simp		if (name_len != strnlen(name, name_len + 1)) {
245279377Simp			err = 12;
246279377Simp			goto bad;
247279377Simp		}
248279377Simp	}
249279377Simp
250279377Simp	/* Checks that all names are unique */
251279377Simp	for (i = 0; i < ubi->vtbl_slots - 1; i++) {
252279377Simp		for (n = i + 1; n < ubi->vtbl_slots; n++) {
253279377Simp			int len1 = be16_to_cpu(vtbl[i].name_len);
254279377Simp			int len2 = be16_to_cpu(vtbl[n].name_len);
255279377Simp
256279377Simp			if (len1 > 0 && len1 == len2 &&
257279377Simp			    !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
258279377Simp				ubi_err(ubi, "volumes %d and %d have the same name \"%s\"",
259279377Simp					i, n, vtbl[i].name);
260279377Simp				ubi_dump_vtbl_record(&vtbl[i], i);
261279377Simp				ubi_dump_vtbl_record(&vtbl[n], n);
262279377Simp				return -EINVAL;
263279377Simp			}
264279377Simp		}
265279377Simp	}
266279377Simp
267279377Simp	return 0;
268279377Simp
269279377Simpbad:
270279377Simp	ubi_err(ubi, "volume table check failed: record %d, error %d", i, err);
271279377Simp	ubi_dump_vtbl_record(&vtbl[i], i);
272279377Simp	return -EINVAL;
273279377Simp}
274279377Simp
275279377Simp/**
276279377Simp * create_vtbl - create a copy of volume table.
277279377Simp * @ubi: UBI device description object
278279377Simp * @ai: attaching information
279279377Simp * @copy: number of the volume table copy
280279377Simp * @vtbl: contents of the volume table
281279377Simp *
282279377Simp * This function returns zero in case of success and a negative error code in
283279377Simp * case of failure.
284279377Simp */
285279377Simpstatic int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
286279377Simp		       int copy, void *vtbl)
287279377Simp{
288279377Simp	int err, tries = 0;
289279377Simp	struct ubi_vid_io_buf *vidb;
290279377Simp	struct ubi_vid_hdr *vid_hdr;
291279377Simp	struct ubi_ainf_peb *new_aeb;
292279377Simp
293279377Simp	dbg_gen("create volume table (copy #%d)", copy + 1);
294279377Simp
295279377Simp	vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
296279377Simp	if (!vidb)
297279377Simp		return -ENOMEM;
298279377Simp
299279377Simp	vid_hdr = ubi_get_vid_hdr(vidb);
300279377Simp
301279377Simpretry:
302279377Simp	new_aeb = ubi_early_get_peb(ubi, ai);
303279377Simp	if (IS_ERR(new_aeb)) {
304279377Simp		err = PTR_ERR(new_aeb);
305279377Simp		goto out_free;
306279377Simp	}
307279377Simp
308279377Simp	vid_hdr->vol_type = UBI_LAYOUT_VOLUME_TYPE;
309279377Simp	vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
310279377Simp	vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
311279377Simp	vid_hdr->data_size = vid_hdr->used_ebs =
312279377Simp			     vid_hdr->data_pad = cpu_to_be32(0);
313279377Simp	vid_hdr->lnum = cpu_to_be32(copy);
314279377Simp	vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum);
315279377Simp
316279377Simp	/* The EC header is already there, write the VID header */
317279377Simp	err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vidb);
318279377Simp	if (err)
319279377Simp		goto write_error;
320279377Simp
321279377Simp	/* Write the layout volume contents */
322279377Simp	err = ubi_io_write_data(ubi, vtbl, new_aeb->pnum, 0, ubi->vtbl_size);
323279377Simp	if (err)
324279377Simp		goto write_error;
325279377Simp
326279377Simp	/*
327279377Simp	 * And add it to the attaching information. Don't delete the old version
328279377Simp	 * of this LEB as it will be deleted and freed in 'ubi_add_to_av()'.
329279377Simp	 */
330279377Simp	err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0);
331279377Simp	ubi_free_aeb(ai, new_aeb);
332279377Simp	ubi_free_vid_buf(vidb);
333279377Simp	return err;
334279377Simp
335279377Simpwrite_error:
336279377Simp	if (err == -EIO && ++tries <= 5) {
337279377Simp		/*
338279377Simp		 * Probably this physical eraseblock went bad, try to pick
339279377Simp		 * another one.
340279377Simp		 */
341279377Simp		list_add(&new_aeb->u.list, &ai->erase);
342279377Simp		goto retry;
343279377Simp	}
344279377Simp	ubi_free_aeb(ai, new_aeb);
345279377Simpout_free:
346279377Simp	ubi_free_vid_buf(vidb);
347279377Simp	return err;
348279377Simp
349279377Simp}
350279377Simp
351279377Simp/**
352279377Simp * process_lvol - process the layout volume.
353279377Simp * @ubi: UBI device description object
354279377Simp * @ai: attaching information
355279377Simp * @av: layout volume attaching information
356279377Simp *
357279377Simp * This function is responsible for reading the layout volume, ensuring it is
358279377Simp * not corrupted, and recovering from corruptions if needed. Returns volume
359279377Simp * table in case of success and a negative error code in case of failure.
360279377Simp */
361279377Simpstatic struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
362279377Simp					    struct ubi_attach_info *ai,
363279377Simp					    struct ubi_ainf_volume *av)
364279377Simp{
365279377Simp	int err;
366279377Simp	struct rb_node *rb;
367279377Simp	struct ubi_ainf_peb *aeb;
368279377Simp	struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
369279377Simp	int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
370279377Simp
371279377Simp	/*
372279377Simp	 * UBI goes through the following steps when it changes the layout
373279377Simp	 * volume:
374279377Simp	 * a. erase LEB 0;
375279377Simp	 * b. write new data to LEB 0;
376279377Simp	 * c. erase LEB 1;
377279377Simp	 * d. write new data to LEB 1.
378279377Simp	 *
379279377Simp	 * Before the change, both LEBs contain the same data.
380279377Simp	 *
381279377Simp	 * Due to unclean reboots, the contents of LEB 0 may be lost, but there
382279377Simp	 * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
383279377Simp	 * Similarly, LEB 1 may be lost, but there should be LEB 0. And
384279377Simp	 * finally, unclean reboots may result in a situation when neither LEB
385279377Simp	 * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
386279377Simp	 * 0 contains more recent information.
387279377Simp	 *
388279377Simp	 * So the plan is to first check LEB 0. Then
389279377Simp	 * a. if LEB 0 is OK, it must be containing the most recent data; then
390279377Simp	 *    we compare it with LEB 1, and if they are different, we copy LEB
391279377Simp	 *    0 to LEB 1;
392279377Simp	 * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
393279377Simp	 *    to LEB 0.
394279377Simp	 */
395279377Simp
396279377Simp	dbg_gen("check layout volume");
397279377Simp
398279377Simp	/* Read both LEB 0 and LEB 1 into memory */
399279377Simp	ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
400279377Simp		leb[aeb->lnum] = vzalloc(ubi->vtbl_size);
401279377Simp		if (!leb[aeb->lnum]) {
402279377Simp			err = -ENOMEM;
403279377Simp			goto out_free;
404279377Simp		}
405279377Simp
406279377Simp		err = ubi_io_read_data(ubi, leb[aeb->lnum], aeb->pnum, 0,
407279377Simp				       ubi->vtbl_size);
408279377Simp		if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err))
409279377Simp			/*
410279377Simp			 * Scrub the PEB later. Note, -EBADMSG indicates an
411279377Simp			 * uncorrectable ECC error, but we have our own CRC and
412279377Simp			 * the data will be checked later. If the data is OK,
413279377Simp			 * the PEB will be scrubbed (because we set
414279377Simp			 * aeb->scrub). If the data is not OK, the contents of
415279377Simp			 * the PEB will be recovered from the second copy, and
416279377Simp			 * aeb->scrub will be cleared in
417279377Simp			 * 'ubi_add_to_av()'.
418279377Simp			 */
419279377Simp			aeb->scrub = 1;
420279377Simp		else if (err)
421279377Simp			goto out_free;
422279377Simp	}
423279377Simp
424279377Simp	err = -EINVAL;
425279377Simp	if (leb[0]) {
426279377Simp		leb_corrupted[0] = vtbl_check(ubi, leb[0]);
427279377Simp		if (leb_corrupted[0] < 0)
428279377Simp			goto out_free;
429279377Simp	}
430279377Simp
431279377Simp	if (!leb_corrupted[0]) {
432279377Simp		/* LEB 0 is OK */
433279377Simp		if (leb[1])
434279377Simp			leb_corrupted[1] = memcmp(leb[0], leb[1],
435279377Simp						  ubi->vtbl_size);
436279377Simp		if (leb_corrupted[1]) {
437279377Simp			ubi_warn(ubi, "volume table copy #2 is corrupted");
438279377Simp			err = create_vtbl(ubi, ai, 1, leb[0]);
439279377Simp			if (err)
440279377Simp				goto out_free;
441279377Simp			ubi_msg(ubi, "volume table was restored");
442279377Simp		}
443279377Simp
444279377Simp		/* Both LEB 1 and LEB 2 are OK and consistent */
445279377Simp		vfree(leb[1]);
446279377Simp		return leb[0];
447279377Simp	} else {
448279377Simp		/* LEB 0 is corrupted or does not exist */
449279377Simp		if (leb[1]) {
450279377Simp			leb_corrupted[1] = vtbl_check(ubi, leb[1]);
451279377Simp			if (leb_corrupted[1] < 0)
452279377Simp				goto out_free;
453279377Simp		}
454279377Simp		if (leb_corrupted[1]) {
455279377Simp			/* Both LEB 0 and LEB 1 are corrupted */
456279377Simp			ubi_err(ubi, "both volume tables are corrupted");
457279377Simp			goto out_free;
458279377Simp		}
459279377Simp
460279377Simp		ubi_warn(ubi, "volume table copy #1 is corrupted");
461279377Simp		err = create_vtbl(ubi, ai, 0, leb[1]);
462279377Simp		if (err)
463279377Simp			goto out_free;
464279377Simp		ubi_msg(ubi, "volume table was restored");
465279377Simp
466279377Simp		vfree(leb[0]);
467279377Simp		return leb[1];
468279377Simp	}
469279377Simp
470279377Simpout_free:
471279377Simp	vfree(leb[0]);
472279377Simp	vfree(leb[1]);
473279377Simp	return ERR_PTR(err);
474279377Simp}
475279377Simp
476279377Simp/**
477279377Simp * create_empty_lvol - create empty layout volume.
478279377Simp * @ubi: UBI device description object
479279377Simp * @ai: attaching information
480279377Simp *
481279377Simp * This function returns volume table contents in case of success and a
482279377Simp * negative error code in case of failure.
483279377Simp */
484279377Simpstatic struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
485279377Simp						 struct ubi_attach_info *ai)
486279377Simp{
487279377Simp	int i;
488279377Simp	struct ubi_vtbl_record *vtbl;
489279377Simp
490279377Simp	vtbl = vzalloc(ubi->vtbl_size);
491279377Simp	if (!vtbl)
492279377Simp		return ERR_PTR(-ENOMEM);
493279377Simp
494279377Simp	for (i = 0; i < ubi->vtbl_slots; i++)
495279377Simp		memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
496279377Simp
497279377Simp	for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
498279377Simp		int err;
499279377Simp
500279377Simp		err = create_vtbl(ubi, ai, i, vtbl);
501279377Simp		if (err) {
502279377Simp			vfree(vtbl);
503279377Simp			return ERR_PTR(err);
504279377Simp		}
505279377Simp	}
506279377Simp
507279377Simp	return vtbl;
508279377Simp}
509279377Simp
510279377Simp/**
511279377Simp * init_volumes - initialize volume information for existing volumes.
512279377Simp * @ubi: UBI device description object
513279377Simp * @ai: scanning information
514279377Simp * @vtbl: volume table
515279377Simp *
516279377Simp * This function allocates volume description objects for existing volumes.
517279377Simp * Returns zero in case of success and a negative error code in case of
518279377Simp * failure.
519279377Simp */
520279377Simpstatic int init_volumes(struct ubi_device *ubi,
521279377Simp			const struct ubi_attach_info *ai,
522279377Simp			const struct ubi_vtbl_record *vtbl)
523279377Simp{
524279377Simp	int i, err, reserved_pebs = 0;
525279377Simp	struct ubi_ainf_volume *av;
526279377Simp	struct ubi_volume *vol;
527279377Simp
528279377Simp	for (i = 0; i < ubi->vtbl_slots; i++) {
529279377Simp		cond_resched();
530279377Simp
531279377Simp		if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
532279377Simp			continue; /* Empty record */
533279377Simp
534279377Simp		vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
535279377Simp		if (!vol)
536279377Simp			return -ENOMEM;
537279377Simp
538279377Simp		vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
539279377Simp		vol->alignment = be32_to_cpu(vtbl[i].alignment);
540279377Simp		vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
541279377Simp		vol->upd_marker = vtbl[i].upd_marker;
542279377Simp		vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
543279377Simp					UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
544279377Simp		vol->name_len = be16_to_cpu(vtbl[i].name_len);
545279377Simp		vol->usable_leb_size = ubi->leb_size - vol->data_pad;
546279377Simp		memcpy(vol->name, vtbl[i].name, vol->name_len);
547279377Simp		vol->name[vol->name_len] = '\0';
548279377Simp		vol->vol_id = i;
549279377Simp
550279377Simp		if (vtbl[i].flags & UBI_VTBL_SKIP_CRC_CHECK_FLG)
551279377Simp			vol->skip_check = 1;
552279377Simp
553279377Simp		if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
554279377Simp			/* Auto re-size flag may be set only for one volume */
555279377Simp			if (ubi->autoresize_vol_id != -1) {
556279377Simp				ubi_err(ubi, "more than one auto-resize volume (%d and %d)",
557279377Simp					ubi->autoresize_vol_id, i);
558279377Simp				kfree(vol);
559279377Simp				return -EINVAL;
560279377Simp			}
561279377Simp
562279377Simp			ubi->autoresize_vol_id = i;
563279377Simp		}
564279377Simp
565279377Simp		ubi_assert(!ubi->volumes[i]);
566279377Simp		ubi->volumes[i] = vol;
567279377Simp		ubi->vol_count += 1;
568279377Simp		vol->ubi = ubi;
569279377Simp		reserved_pebs += vol->reserved_pebs;
570279377Simp
571279377Simp		/*
572279377Simp		 * We use ubi->peb_count and not vol->reserved_pebs because
573279377Simp		 * we want to keep the code simple. Otherwise we'd have to
574279377Simp		 * resize/check the bitmap upon volume resize too.
575279377Simp		 * Allocating a few bytes more does not hurt.
576279377Simp		 */
577279377Simp		err = ubi_fastmap_init_checkmap(vol, ubi->peb_count);
578279377Simp		if (err)
579279377Simp			return err;
580279377Simp
581279377Simp		/*
582279377Simp		 * In case of dynamic volume UBI knows nothing about how many
583279377Simp		 * data is stored there. So assume the whole volume is used.
584279377Simp		 */
585279377Simp		if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
586279377Simp			vol->used_ebs = vol->reserved_pebs;
587279377Simp			vol->last_eb_bytes = vol->usable_leb_size;
588279377Simp			vol->used_bytes =
589279377Simp				(long long)vol->used_ebs * vol->usable_leb_size;
590279377Simp			continue;
591279377Simp		}
592279377Simp
593279377Simp		/* Static volumes only */
594279377Simp		av = ubi_find_av(ai, i);
595279377Simp		if (!av || !av->leb_count) {
596279377Simp			/*
597279377Simp			 * No eraseblocks belonging to this volume found. We
598279377Simp			 * don't actually know whether this static volume is
599279377Simp			 * completely corrupted or just contains no data. And
600279377Simp			 * we cannot know this as long as data size is not
601279377Simp			 * stored on flash. So we just assume the volume is
602279377Simp			 * empty. FIXME: this should be handled.
603279377Simp			 */
604279377Simp			continue;
605279377Simp		}
606279377Simp
607279377Simp		if (av->leb_count != av->used_ebs) {
608279377Simp			/*
609279377Simp			 * We found a static volume which misses several
610279377Simp			 * eraseblocks. Treat it as corrupted.
611279377Simp			 */
612279377Simp			ubi_warn(ubi, "static volume %d misses %d LEBs - corrupted",
613279377Simp				 av->vol_id, av->used_ebs - av->leb_count);
614279377Simp			vol->corrupted = 1;
615279377Simp			continue;
616279377Simp		}
617279377Simp
618279377Simp		vol->used_ebs = av->used_ebs;
619279377Simp		vol->used_bytes =
620279377Simp			(long long)(vol->used_ebs - 1) * vol->usable_leb_size;
621279377Simp		vol->used_bytes += av->last_data_size;
622279377Simp		vol->last_eb_bytes = av->last_data_size;
623279377Simp	}
624279377Simp
625279377Simp	/* And add the layout volume */
626279377Simp	vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
627279377Simp	if (!vol)
628279377Simp		return -ENOMEM;
629279377Simp
630279377Simp	vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
631279377Simp	vol->alignment = UBI_LAYOUT_VOLUME_ALIGN;
632279377Simp	vol->vol_type = UBI_DYNAMIC_VOLUME;
633279377Simp	vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
634279377Simp	memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
635279377Simp	vol->usable_leb_size = ubi->leb_size;
636279377Simp	vol->used_ebs = vol->reserved_pebs;
637279377Simp	vol->last_eb_bytes = vol->reserved_pebs;
638279377Simp	vol->used_bytes =
639279377Simp		(long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
640279377Simp	vol->vol_id = UBI_LAYOUT_VOLUME_ID;
641279377Simp	vol->ref_count = 1;
642279377Simp
643279377Simp	ubi_assert(!ubi->volumes[i]);
644279377Simp	ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
645279377Simp	reserved_pebs += vol->reserved_pebs;
646279377Simp	ubi->vol_count += 1;
647279377Simp	vol->ubi = ubi;
648279377Simp	err = ubi_fastmap_init_checkmap(vol, UBI_LAYOUT_VOLUME_EBS);
649279377Simp	if (err)
650279377Simp		return err;
651279377Simp
652279377Simp	if (reserved_pebs > ubi->avail_pebs) {
653279377Simp		ubi_err(ubi, "not enough PEBs, required %d, available %d",
654279377Simp			reserved_pebs, ubi->avail_pebs);
655279377Simp		if (ubi->corr_peb_count)
656279377Simp			ubi_err(ubi, "%d PEBs are corrupted and not used",
657279377Simp				ubi->corr_peb_count);
658279377Simp		return -ENOSPC;
659279377Simp	}
660279377Simp	ubi->rsvd_pebs += reserved_pebs;
661279377Simp	ubi->avail_pebs -= reserved_pebs;
662279377Simp
663279377Simp	return 0;
664279377Simp}
665279377Simp
666279377Simp/**
667279377Simp * check_av - check volume attaching information.
668279377Simp * @vol: UBI volume description object
669279377Simp * @av: volume attaching information
670279377Simp *
671279377Simp * This function returns zero if the volume attaching information is consistent
672279377Simp * to the data read from the volume tabla, and %-EINVAL if not.
673279377Simp */
674279377Simpstatic int check_av(const struct ubi_volume *vol,
675279377Simp		    const struct ubi_ainf_volume *av)
676279377Simp{
677279377Simp	int err;
678279377Simp
679279377Simp	if (av->highest_lnum >= vol->reserved_pebs) {
680279377Simp		err = 1;
681279377Simp		goto bad;
682279377Simp	}
683279377Simp	if (av->leb_count > vol->reserved_pebs) {
684279377Simp		err = 2;
685279377Simp		goto bad;
686279377Simp	}
687279377Simp	if (av->vol_type != vol->vol_type) {
688279377Simp		err = 3;
689279377Simp		goto bad;
690279377Simp	}
691279377Simp	if (av->used_ebs > vol->reserved_pebs) {
692279377Simp		err = 4;
693279377Simp		goto bad;
694279377Simp	}
695279377Simp	if (av->data_pad != vol->data_pad) {
696279377Simp		err = 5;
697279377Simp		goto bad;
698279377Simp	}
699279377Simp	return 0;
700279377Simp
701279377Simpbad:
702279377Simp	ubi_err(vol->ubi, "bad attaching information, error %d", err);
703279377Simp	ubi_dump_av(av);
704279377Simp	ubi_dump_vol_info(vol);
705279377Simp	return -EINVAL;
706279377Simp}
707279377Simp
708279377Simp/**
709279377Simp * check_attaching_info - check that attaching information.
710279377Simp * @ubi: UBI device description object
711279377Simp * @ai: attaching information
712279377Simp *
713279377Simp * Even though we protect on-flash data by CRC checksums, we still don't trust
714279377Simp * the media. This function ensures that attaching information is consistent to
715279377Simp * the information read from the volume table. Returns zero if the attaching
716279377Simp * information is OK and %-EINVAL if it is not.
717279377Simp */
718279377Simpstatic int check_attaching_info(const struct ubi_device *ubi,
719279377Simp			       struct ubi_attach_info *ai)
720279377Simp{
721279377Simp	int err, i;
722279377Simp	struct ubi_ainf_volume *av;
723279377Simp	struct ubi_volume *vol;
724279377Simp
725279377Simp	if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
726279377Simp		ubi_err(ubi, "found %d volumes while attaching, maximum is %d + %d",
727279377Simp			ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
728279377Simp		return -EINVAL;
729279377Simp	}
730279377Simp
731279377Simp	if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
732279377Simp	    ai->highest_vol_id < UBI_INTERNAL_VOL_START) {
733279377Simp		ubi_err(ubi, "too large volume ID %d found",
734279377Simp			ai->highest_vol_id);
735279377Simp		return -EINVAL;
736279377Simp	}
737279377Simp
738279377Simp	for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
739279377Simp		cond_resched();
740279377Simp
741279377Simp		av = ubi_find_av(ai, i);
742279377Simp		vol = ubi->volumes[i];
743279377Simp		if (!vol) {
744279377Simp			if (av)
745279377Simp				ubi_remove_av(ai, av);
746279377Simp			continue;
747279377Simp		}
748279377Simp
749279377Simp		if (vol->reserved_pebs == 0) {
750279377Simp			ubi_assert(i < ubi->vtbl_slots);
751279377Simp
752279377Simp			if (!av)
753279377Simp				continue;
754279377Simp
755279377Simp			/*
756279377Simp			 * During attaching we found a volume which does not
757279377Simp			 * exist according to the information in the volume
758279377Simp			 * table. This must have happened due to an unclean
759279377Simp			 * reboot while the volume was being removed. Discard
760279377Simp			 * these eraseblocks.
761279377Simp			 */
762279377Simp			ubi_msg(ubi, "finish volume %d removal", av->vol_id);
763279377Simp			ubi_remove_av(ai, av);
764279377Simp		} else if (av) {
765279377Simp			err = check_av(vol, av);
766279377Simp			if (err)
767279377Simp				return err;
768279377Simp		}
769279377Simp	}
770279377Simp
771279377Simp	return 0;
772279377Simp}
773279377Simp
774279377Simp/**
775279377Simp * ubi_read_volume_table - read the volume table.
776279377Simp * @ubi: UBI device description object
777279377Simp * @ai: attaching information
778279377Simp *
779279377Simp * This function reads volume table, checks it, recover from errors if needed,
780279377Simp * or creates it if needed. Returns zero in case of success and a negative
781279377Simp * error code in case of failure.
782279377Simp */
783279377Simpint ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
784279377Simp{
785279377Simp	int err;
786279377Simp	struct ubi_ainf_volume *av;
787279377Simp
788279377Simp	empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
789279377Simp
790279377Simp	/*
791279377Simp	 * The number of supported volumes is limited by the eraseblock size
792279377Simp	 * and by the UBI_MAX_VOLUMES constant.
793279377Simp	 */
794279377Simp
795279377Simp	if (ubi->leb_size < UBI_VTBL_RECORD_SIZE) {
796279377Simp		ubi_err(ubi, "LEB size too small for a volume record");
797279377Simp		return -EINVAL;
798279377Simp	}
799279377Simp
800279377Simp	ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
801279377Simp	if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
802279377Simp		ubi->vtbl_slots = UBI_MAX_VOLUMES;
803279377Simp
804279377Simp	ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
805279377Simp	ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
806279377Simp
807279377Simp	av = ubi_find_av(ai, UBI_LAYOUT_VOLUME_ID);
808279377Simp	if (!av) {
809279377Simp		/*
810279377Simp		 * No logical eraseblocks belonging to the layout volume were
811279377Simp		 * found. This could mean that the flash is just empty. In
812279377Simp		 * this case we create empty layout volume.
813279377Simp		 *
814279377Simp		 * But if flash is not empty this must be a corruption or the
815279377Simp		 * MTD device just contains garbage.
816279377Simp		 */
817279377Simp		if (ai->is_empty) {
818279377Simp			ubi->vtbl = create_empty_lvol(ubi, ai);
819279377Simp			if (IS_ERR(ubi->vtbl))
820279377Simp				return PTR_ERR(ubi->vtbl);
821279377Simp		} else {
822279377Simp			ubi_err(ubi, "the layout volume was not found");
823279377Simp			return -EINVAL;
824279377Simp		}
825279377Simp	} else {
826279377Simp		if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) {
827279377Simp			/* This must not happen with proper UBI images */
828279377Simp			ubi_err(ubi, "too many LEBs (%d) in layout volume",
829279377Simp				av->leb_count);
830279377Simp			return -EINVAL;
831279377Simp		}
832279377Simp
833279377Simp		ubi->vtbl = process_lvol(ubi, ai, av);
834279377Simp		if (IS_ERR(ubi->vtbl))
835279377Simp			return PTR_ERR(ubi->vtbl);
836279377Simp	}
837279377Simp
838279377Simp	ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
839279377Simp
840279377Simp	/*
841279377Simp	 * The layout volume is OK, initialize the corresponding in-RAM data
842279377Simp	 * structures.
843279377Simp	 */
844279377Simp	err = init_volumes(ubi, ai, ubi->vtbl);
845279377Simp	if (err)
846279377Simp		goto out_free;
847279377Simp
848279377Simp	/*
849279377Simp	 * Make sure that the attaching information is consistent to the
850279377Simp	 * information stored in the volume table.
851279377Simp	 */
852279377Simp	err = check_attaching_info(ubi, ai);
853279377Simp	if (err)
854279377Simp		goto out_free;
855279377Simp
856279377Simp	return 0;
857279377Simp
858279377Simpout_free:
859279377Simp	vfree(ubi->vtbl);
860279377Simp	ubi_free_all_volumes(ubi);
861279377Simp	return err;
862279377Simp}
863279377Simp
864279377Simp/**
865279377Simp * self_vtbl_check - check volume table.
866279377Simp * @ubi: UBI device description object
867279377Simp */
868279377Simpstatic void self_vtbl_check(const struct ubi_device *ubi)
869279377Simp{
870279377Simp	if (!ubi_dbg_chk_gen(ubi))
871279377Simp		return;
872279377Simp
873279377Simp	if (vtbl_check(ubi, ubi->vtbl)) {
874279377Simp		ubi_err(ubi, "self-check failed");
875279377Simp		BUG();
876279377Simp	}
877279377Simp}
878279377Simp