11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2801c135cSArtem B. Bityutskiy /*
3801c135cSArtem B. Bityutskiy * Copyright (c) International Business Machines Corp., 2006
4801c135cSArtem B. Bityutskiy * Copyright (c) Nokia Corporation, 2006, 2007
5801c135cSArtem B. Bityutskiy *
6801c135cSArtem B. Bityutskiy * Author: Artem Bityutskiy (Битюцкий Артём)
7801c135cSArtem B. Bityutskiy */
8801c135cSArtem B. Bityutskiy
9801c135cSArtem B. Bityutskiy /*
10801c135cSArtem B. Bityutskiy * This file includes volume table manipulation code. The volume table is an
11801c135cSArtem B. Bityutskiy * on-flash table containing volume meta-data like name, number of reserved
12801c135cSArtem B. Bityutskiy * physical eraseblocks, type, etc. The volume table is stored in the so-called
13801c135cSArtem B. Bityutskiy * "layout volume".
14801c135cSArtem B. Bityutskiy *
15801c135cSArtem B. Bityutskiy * The layout volume is an internal volume which is organized as follows. It
16801c135cSArtem B. Bityutskiy * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical
17801c135cSArtem B. Bityutskiy * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each
18801c135cSArtem B. Bityutskiy * other. This redundancy guarantees robustness to unclean reboots. The volume
19801c135cSArtem B. Bityutskiy * table is basically an array of volume table records. Each record contains
20b81000b6SRichard Weinberger * full information about the volume and protected by a CRC checksum. Note,
21b81000b6SRichard Weinberger * nowadays we use the atomic LEB change operation when updating the volume
22b81000b6SRichard Weinberger * table, so we do not really need 2 LEBs anymore, but we preserve the older
23b81000b6SRichard Weinberger * design for the backward compatibility reasons.
24801c135cSArtem B. Bityutskiy *
25b81000b6SRichard Weinberger * When the volume table is changed, it is first changed in RAM. Then LEB 0 is
26801c135cSArtem B. Bityutskiy * erased, and the updated volume table is written back to LEB 0. Then same for
27801c135cSArtem B. Bityutskiy * LEB 1. This scheme guarantees recoverability from unclean reboots.
28801c135cSArtem B. Bityutskiy *
29801c135cSArtem B. Bityutskiy * In this UBI implementation the on-flash volume table does not contain any
30fbd0107fSArtem Bityutskiy * information about how much data static volumes contain.
31801c135cSArtem B. Bityutskiy *
32801c135cSArtem B. Bityutskiy * But it would still be beneficial to store this information in the volume
33801c135cSArtem B. Bityutskiy * table. For example, suppose we have a static volume X, and all its physical
34801c135cSArtem B. Bityutskiy * eraseblocks became bad for some reasons. Suppose we are attaching the
35fbd0107fSArtem Bityutskiy * corresponding MTD device, for some reason we find no logical eraseblocks
36801c135cSArtem B. Bityutskiy * corresponding to the volume X. According to the volume table volume X does
37801c135cSArtem B. Bityutskiy * exist. So we don't know whether it is just empty or all its physical
38fbd0107fSArtem Bityutskiy * eraseblocks went bad. So we cannot alarm the user properly.
39801c135cSArtem B. Bityutskiy *
40801c135cSArtem B. Bityutskiy * The volume table also stores so-called "update marker", which is used for
41801c135cSArtem B. Bityutskiy * volume updates. Before updating the volume, the update marker is set, and
42801c135cSArtem B. Bityutskiy * after the update operation is finished, the update marker is cleared. So if
43801c135cSArtem B. Bityutskiy * the update operation was interrupted (e.g. by an unclean reboot) - the
44801c135cSArtem B. Bityutskiy * update marker is still there and we know that the volume's contents is
45801c135cSArtem B. Bityutskiy * damaged.
46801c135cSArtem B. Bityutskiy */
47801c135cSArtem B. Bityutskiy
48801c135cSArtem B. Bityutskiy #include <linux/crc32.h>
49801c135cSArtem B. Bityutskiy #include <linux/err.h>
505a0e3ad6STejun Heo #include <linux/slab.h>
51801c135cSArtem B. Bityutskiy #include <asm/div64.h>
52801c135cSArtem B. Bityutskiy #include "ubi.h"
53801c135cSArtem B. Bityutskiy
547bf523aeSArtem Bityutskiy static void self_vtbl_check(const struct ubi_device *ubi);
55801c135cSArtem B. Bityutskiy
56801c135cSArtem B. Bityutskiy /* Empty volume table record */
57801c135cSArtem B. Bityutskiy static struct ubi_vtbl_record empty_vtbl_record;
58801c135cSArtem B. Bityutskiy
59801c135cSArtem B. Bityutskiy /**
602848594aSshengyong * ubi_update_layout_vol - helper for updatting layout volumes on flash
612848594aSshengyong * @ubi: UBI device description object
622848594aSshengyong */
ubi_update_layout_vol(struct ubi_device * ubi)632848594aSshengyong static int ubi_update_layout_vol(struct ubi_device *ubi)
642848594aSshengyong {
652848594aSshengyong struct ubi_volume *layout_vol;
662848594aSshengyong int i, err;
672848594aSshengyong
682848594aSshengyong layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)];
692848594aSshengyong for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
702848594aSshengyong err = ubi_eba_atomic_leb_change(ubi, layout_vol, i, ubi->vtbl,
712848594aSshengyong ubi->vtbl_size);
722848594aSshengyong if (err)
732848594aSshengyong return err;
742848594aSshengyong }
752848594aSshengyong
762848594aSshengyong return 0;
772848594aSshengyong }
782848594aSshengyong
792848594aSshengyong /**
80801c135cSArtem B. Bityutskiy * ubi_change_vtbl_record - change volume table record.
81801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
82801c135cSArtem B. Bityutskiy * @idx: table index to change
83801c135cSArtem B. Bityutskiy * @vtbl_rec: new volume table record
84801c135cSArtem B. Bityutskiy *
85801c135cSArtem B. Bityutskiy * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty
86801c135cSArtem B. Bityutskiy * volume table record is written. The caller does not have to calculate CRC of
87801c135cSArtem B. Bityutskiy * the record as it is done by this function. Returns zero in case of success
88801c135cSArtem B. Bityutskiy * and a negative error code in case of failure.
89801c135cSArtem B. Bityutskiy */
ubi_change_vtbl_record(struct ubi_device * ubi,int idx,struct ubi_vtbl_record * vtbl_rec)90801c135cSArtem B. Bityutskiy int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
91801c135cSArtem B. Bityutskiy struct ubi_vtbl_record *vtbl_rec)
92801c135cSArtem B. Bityutskiy {
932848594aSshengyong int err;
94801c135cSArtem B. Bityutskiy uint32_t crc;
95801c135cSArtem B. Bityutskiy
96801c135cSArtem B. Bityutskiy ubi_assert(idx >= 0 && idx < ubi->vtbl_slots);
97801c135cSArtem B. Bityutskiy
98801c135cSArtem B. Bityutskiy if (!vtbl_rec)
99801c135cSArtem B. Bityutskiy vtbl_rec = &empty_vtbl_record;
100801c135cSArtem B. Bityutskiy else {
101801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
1023261ebd7SChristoph Hellwig vtbl_rec->crc = cpu_to_be32(crc);
103801c135cSArtem B. Bityutskiy }
104801c135cSArtem B. Bityutskiy
105801c135cSArtem B. Bityutskiy memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
1062848594aSshengyong err = ubi_update_layout_vol(ubi);
107801c135cSArtem B. Bityutskiy
1087bf523aeSArtem Bityutskiy self_vtbl_check(ubi);
1092848594aSshengyong return err ? err : 0;
110801c135cSArtem B. Bityutskiy }
111801c135cSArtem B. Bityutskiy
112801c135cSArtem B. Bityutskiy /**
113f40ac9cdSArtem Bityutskiy * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table.
114f40ac9cdSArtem Bityutskiy * @ubi: UBI device description object
115ebaaf1afSArtem Bityutskiy * @rename_list: list of &struct ubi_rename_entry objects
116f40ac9cdSArtem Bityutskiy *
117f40ac9cdSArtem Bityutskiy * This function re-names multiple volumes specified in @req in the volume
118f40ac9cdSArtem Bityutskiy * table. Returns zero in case of success and a negative error code in case of
119f40ac9cdSArtem Bityutskiy * failure.
120f40ac9cdSArtem Bityutskiy */
ubi_vtbl_rename_volumes(struct ubi_device * ubi,struct list_head * rename_list)121f40ac9cdSArtem Bityutskiy int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
122f40ac9cdSArtem Bityutskiy struct list_head *rename_list)
123f40ac9cdSArtem Bityutskiy {
124f40ac9cdSArtem Bityutskiy struct ubi_rename_entry *re;
125f40ac9cdSArtem Bityutskiy
126f40ac9cdSArtem Bityutskiy list_for_each_entry(re, rename_list, list) {
127f40ac9cdSArtem Bityutskiy uint32_t crc;
128f40ac9cdSArtem Bityutskiy struct ubi_volume *vol = re->desc->vol;
129f40ac9cdSArtem Bityutskiy struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id];
130f40ac9cdSArtem Bityutskiy
131f40ac9cdSArtem Bityutskiy if (re->remove) {
132f40ac9cdSArtem Bityutskiy memcpy(vtbl_rec, &empty_vtbl_record,
133f40ac9cdSArtem Bityutskiy sizeof(struct ubi_vtbl_record));
134f40ac9cdSArtem Bityutskiy continue;
135f40ac9cdSArtem Bityutskiy }
136f40ac9cdSArtem Bityutskiy
137f40ac9cdSArtem Bityutskiy vtbl_rec->name_len = cpu_to_be16(re->new_name_len);
138f40ac9cdSArtem Bityutskiy memcpy(vtbl_rec->name, re->new_name, re->new_name_len);
139f40ac9cdSArtem Bityutskiy memset(vtbl_rec->name + re->new_name_len, 0,
140f40ac9cdSArtem Bityutskiy UBI_VOL_NAME_MAX + 1 - re->new_name_len);
141f40ac9cdSArtem Bityutskiy crc = crc32(UBI_CRC32_INIT, vtbl_rec,
142f40ac9cdSArtem Bityutskiy UBI_VTBL_RECORD_SIZE_CRC);
143f40ac9cdSArtem Bityutskiy vtbl_rec->crc = cpu_to_be32(crc);
144f40ac9cdSArtem Bityutskiy }
145f40ac9cdSArtem Bityutskiy
1462848594aSshengyong return ubi_update_layout_vol(ubi);
147f40ac9cdSArtem Bityutskiy }
148f40ac9cdSArtem Bityutskiy
149f40ac9cdSArtem Bityutskiy /**
150ebaaf1afSArtem Bityutskiy * vtbl_check - check if volume table is not corrupted and sensible.
151801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
152801c135cSArtem B. Bityutskiy * @vtbl: volume table
153801c135cSArtem B. Bityutskiy *
154801c135cSArtem B. Bityutskiy * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
155801c135cSArtem B. Bityutskiy * and %-EINVAL if it contains inconsistent data.
156801c135cSArtem B. Bityutskiy */
vtbl_check(const struct ubi_device * ubi,const struct ubi_vtbl_record * vtbl)157801c135cSArtem B. Bityutskiy static int vtbl_check(const struct ubi_device *ubi,
158801c135cSArtem B. Bityutskiy const struct ubi_vtbl_record *vtbl)
159801c135cSArtem B. Bityutskiy {
160801c135cSArtem B. Bityutskiy int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
161979c9296SArtem Bityutskiy int upd_marker, err;
162801c135cSArtem B. Bityutskiy uint32_t crc;
163801c135cSArtem B. Bityutskiy const char *name;
164801c135cSArtem B. Bityutskiy
165801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots; i++) {
166801c135cSArtem B. Bityutskiy cond_resched();
167801c135cSArtem B. Bityutskiy
1683261ebd7SChristoph Hellwig reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
1693261ebd7SChristoph Hellwig alignment = be32_to_cpu(vtbl[i].alignment);
1703261ebd7SChristoph Hellwig data_pad = be32_to_cpu(vtbl[i].data_pad);
171801c135cSArtem B. Bityutskiy upd_marker = vtbl[i].upd_marker;
172801c135cSArtem B. Bityutskiy vol_type = vtbl[i].vol_type;
1733261ebd7SChristoph Hellwig name_len = be16_to_cpu(vtbl[i].name_len);
174801c135cSArtem B. Bityutskiy name = &vtbl[i].name[0];
175801c135cSArtem B. Bityutskiy
176801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
1773261ebd7SChristoph Hellwig if (be32_to_cpu(vtbl[i].crc) != crc) {
17832608703STanya Brokhman ubi_err(ubi, "bad CRC at record %u: %#08x, not %#08x",
1793261ebd7SChristoph Hellwig i, crc, be32_to_cpu(vtbl[i].crc));
1801f021e1dSArtem Bityutskiy ubi_dump_vtbl_record(&vtbl[i], i);
181801c135cSArtem B. Bityutskiy return 1;
182801c135cSArtem B. Bityutskiy }
183801c135cSArtem B. Bityutskiy
184801c135cSArtem B. Bityutskiy if (reserved_pebs == 0) {
185801c135cSArtem B. Bityutskiy if (memcmp(&vtbl[i], &empty_vtbl_record,
186801c135cSArtem B. Bityutskiy UBI_VTBL_RECORD_SIZE)) {
187979c9296SArtem Bityutskiy err = 2;
188801c135cSArtem B. Bityutskiy goto bad;
189801c135cSArtem B. Bityutskiy }
190801c135cSArtem B. Bityutskiy continue;
191801c135cSArtem B. Bityutskiy }
192801c135cSArtem B. Bityutskiy
193801c135cSArtem B. Bityutskiy if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
194801c135cSArtem B. Bityutskiy name_len < 0) {
195979c9296SArtem Bityutskiy err = 3;
196801c135cSArtem B. Bityutskiy goto bad;
197801c135cSArtem B. Bityutskiy }
198801c135cSArtem B. Bityutskiy
199801c135cSArtem B. Bityutskiy if (alignment > ubi->leb_size || alignment == 0) {
200979c9296SArtem Bityutskiy err = 4;
201801c135cSArtem B. Bityutskiy goto bad;
202801c135cSArtem B. Bityutskiy }
203801c135cSArtem B. Bityutskiy
204cadb40ccSKyungmin Park n = alignment & (ubi->min_io_size - 1);
205801c135cSArtem B. Bityutskiy if (alignment != 1 && n) {
206979c9296SArtem Bityutskiy err = 5;
207801c135cSArtem B. Bityutskiy goto bad;
208801c135cSArtem B. Bityutskiy }
209801c135cSArtem B. Bityutskiy
210801c135cSArtem B. Bityutskiy n = ubi->leb_size % alignment;
211801c135cSArtem B. Bityutskiy if (data_pad != n) {
21232608703STanya Brokhman ubi_err(ubi, "bad data_pad, has to be %d", n);
213979c9296SArtem Bityutskiy err = 6;
214801c135cSArtem B. Bityutskiy goto bad;
215801c135cSArtem B. Bityutskiy }
216801c135cSArtem B. Bityutskiy
217801c135cSArtem B. Bityutskiy if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
218979c9296SArtem Bityutskiy err = 7;
219801c135cSArtem B. Bityutskiy goto bad;
220801c135cSArtem B. Bityutskiy }
221801c135cSArtem B. Bityutskiy
222801c135cSArtem B. Bityutskiy if (upd_marker != 0 && upd_marker != 1) {
223979c9296SArtem Bityutskiy err = 8;
224801c135cSArtem B. Bityutskiy goto bad;
225801c135cSArtem B. Bityutskiy }
226801c135cSArtem B. Bityutskiy
227801c135cSArtem B. Bityutskiy if (reserved_pebs > ubi->good_peb_count) {
22832608703STanya Brokhman ubi_err(ubi, "too large reserved_pebs %d, good PEBs %d",
229762a9f29SDeepak Saxena reserved_pebs, ubi->good_peb_count);
230979c9296SArtem Bityutskiy err = 9;
231801c135cSArtem B. Bityutskiy goto bad;
232801c135cSArtem B. Bityutskiy }
233801c135cSArtem B. Bityutskiy
234801c135cSArtem B. Bityutskiy if (name_len > UBI_VOL_NAME_MAX) {
235979c9296SArtem Bityutskiy err = 10;
236801c135cSArtem B. Bityutskiy goto bad;
237801c135cSArtem B. Bityutskiy }
238801c135cSArtem B. Bityutskiy
239801c135cSArtem B. Bityutskiy if (name[0] == '\0') {
240979c9296SArtem Bityutskiy err = 11;
241801c135cSArtem B. Bityutskiy goto bad;
242801c135cSArtem B. Bityutskiy }
243801c135cSArtem B. Bityutskiy
244801c135cSArtem B. Bityutskiy if (name_len != strnlen(name, name_len + 1)) {
245979c9296SArtem Bityutskiy err = 12;
246801c135cSArtem B. Bityutskiy goto bad;
247801c135cSArtem B. Bityutskiy }
248801c135cSArtem B. Bityutskiy }
249801c135cSArtem B. Bityutskiy
250801c135cSArtem B. Bityutskiy /* Checks that all names are unique */
251801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots - 1; i++) {
252801c135cSArtem B. Bityutskiy for (n = i + 1; n < ubi->vtbl_slots; n++) {
2533261ebd7SChristoph Hellwig int len1 = be16_to_cpu(vtbl[i].name_len);
2543261ebd7SChristoph Hellwig int len2 = be16_to_cpu(vtbl[n].name_len);
255801c135cSArtem B. Bityutskiy
256801c135cSArtem B. Bityutskiy if (len1 > 0 && len1 == len2 &&
257801c135cSArtem B. Bityutskiy !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
25832608703STanya Brokhman ubi_err(ubi, "volumes %d and %d have the same name \"%s\"",
259049333ceSArtem Bityutskiy i, n, vtbl[i].name);
2601f021e1dSArtem Bityutskiy ubi_dump_vtbl_record(&vtbl[i], i);
2611f021e1dSArtem Bityutskiy ubi_dump_vtbl_record(&vtbl[n], n);
262801c135cSArtem B. Bityutskiy return -EINVAL;
263801c135cSArtem B. Bityutskiy }
264801c135cSArtem B. Bityutskiy }
265801c135cSArtem B. Bityutskiy }
266801c135cSArtem B. Bityutskiy
267801c135cSArtem B. Bityutskiy return 0;
268801c135cSArtem B. Bityutskiy
269801c135cSArtem B. Bityutskiy bad:
27032608703STanya Brokhman ubi_err(ubi, "volume table check failed: record %d, error %d", i, err);
2711f021e1dSArtem Bityutskiy ubi_dump_vtbl_record(&vtbl[i], i);
272801c135cSArtem B. Bityutskiy return -EINVAL;
273801c135cSArtem B. Bityutskiy }
274801c135cSArtem B. Bityutskiy
275801c135cSArtem B. Bityutskiy /**
276801c135cSArtem B. Bityutskiy * create_vtbl - create a copy of volume table.
277801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
278a4e6042fSArtem Bityutskiy * @ai: attaching information
279801c135cSArtem B. Bityutskiy * @copy: number of the volume table copy
280801c135cSArtem B. Bityutskiy * @vtbl: contents of the volume table
281801c135cSArtem B. Bityutskiy *
282801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in
283801c135cSArtem B. Bityutskiy * case of failure.
284801c135cSArtem B. Bityutskiy */
create_vtbl(struct ubi_device * ubi,struct ubi_attach_info * ai,int copy,void * vtbl)285a4e6042fSArtem Bityutskiy static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
286801c135cSArtem B. Bityutskiy int copy, void *vtbl)
287801c135cSArtem B. Bityutskiy {
288801c135cSArtem B. Bityutskiy int err, tries = 0;
2893291b52fSBoris Brezillon struct ubi_vid_io_buf *vidb;
2906bdccffeSRichard Weinberger struct ubi_vid_hdr *vid_hdr;
2912c5ec5ceSArtem Bityutskiy struct ubi_ainf_peb *new_aeb;
292801c135cSArtem B. Bityutskiy
293719bb840SArtem Bityutskiy dbg_gen("create volume table (copy #%d)", copy + 1);
294801c135cSArtem B. Bityutskiy
2953291b52fSBoris Brezillon vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
2963291b52fSBoris Brezillon if (!vidb)
297801c135cSArtem B. Bityutskiy return -ENOMEM;
298801c135cSArtem B. Bityutskiy
2993291b52fSBoris Brezillon vid_hdr = ubi_get_vid_hdr(vidb);
3003291b52fSBoris Brezillon
301801c135cSArtem B. Bityutskiy retry:
302c87fbd7dSArtem Bityutskiy new_aeb = ubi_early_get_peb(ubi, ai);
3032c5ec5ceSArtem Bityutskiy if (IS_ERR(new_aeb)) {
3042c5ec5ceSArtem Bityutskiy err = PTR_ERR(new_aeb);
305801c135cSArtem B. Bityutskiy goto out_free;
306801c135cSArtem B. Bityutskiy }
307801c135cSArtem B. Bityutskiy
3081f4f4347SRichard Weinberger vid_hdr->vol_type = UBI_LAYOUT_VOLUME_TYPE;
30991f2d53cSArtem Bityutskiy vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID);
310801c135cSArtem B. Bityutskiy vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
311801c135cSArtem B. Bityutskiy vid_hdr->data_size = vid_hdr->used_ebs =
3123261ebd7SChristoph Hellwig vid_hdr->data_pad = cpu_to_be32(0);
3133261ebd7SChristoph Hellwig vid_hdr->lnum = cpu_to_be32(copy);
314a4e6042fSArtem Bityutskiy vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum);
315801c135cSArtem B. Bityutskiy
316801c135cSArtem B. Bityutskiy /* The EC header is already there, write the VID header */
3173291b52fSBoris Brezillon err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vidb);
318801c135cSArtem B. Bityutskiy if (err)
319801c135cSArtem B. Bityutskiy goto write_error;
320801c135cSArtem B. Bityutskiy
321801c135cSArtem B. Bityutskiy /* Write the layout volume contents */
3222c5ec5ceSArtem Bityutskiy err = ubi_io_write_data(ubi, vtbl, new_aeb->pnum, 0, ubi->vtbl_size);
323801c135cSArtem B. Bityutskiy if (err)
324801c135cSArtem B. Bityutskiy goto write_error;
325801c135cSArtem B. Bityutskiy
326801c135cSArtem B. Bityutskiy /*
327a4e6042fSArtem Bityutskiy * And add it to the attaching information. Don't delete the old version
3283561188aSArtem Bityutskiy * of this LEB as it will be deleted and freed in 'ubi_add_to_av()'.
329801c135cSArtem B. Bityutskiy */
3303561188aSArtem Bityutskiy err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0);
33191f4285fSBoris Brezillon ubi_free_aeb(ai, new_aeb);
3323291b52fSBoris Brezillon ubi_free_vid_buf(vidb);
333801c135cSArtem B. Bityutskiy return err;
334801c135cSArtem B. Bityutskiy
335801c135cSArtem B. Bityutskiy write_error:
33678d87c95SArtem Bityutskiy if (err == -EIO && ++tries <= 5) {
33778d87c95SArtem Bityutskiy /*
33878d87c95SArtem Bityutskiy * Probably this physical eraseblock went bad, try to pick
33978d87c95SArtem Bityutskiy * another one.
34078d87c95SArtem Bityutskiy */
341a4e6042fSArtem Bityutskiy list_add(&new_aeb->u.list, &ai->erase);
342801c135cSArtem B. Bityutskiy goto retry;
34378d87c95SArtem Bityutskiy }
34491f4285fSBoris Brezillon ubi_free_aeb(ai, new_aeb);
345801c135cSArtem B. Bityutskiy out_free:
3463291b52fSBoris Brezillon ubi_free_vid_buf(vidb);
347801c135cSArtem B. Bityutskiy return err;
348801c135cSArtem B. Bityutskiy
349801c135cSArtem B. Bityutskiy }
350801c135cSArtem B. Bityutskiy
351801c135cSArtem B. Bityutskiy /**
352801c135cSArtem B. Bityutskiy * process_lvol - process the layout volume.
353801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
354a4e6042fSArtem Bityutskiy * @ai: attaching information
355517af48cSArtem Bityutskiy * @av: layout volume attaching information
356801c135cSArtem B. Bityutskiy *
357801c135cSArtem B. Bityutskiy * This function is responsible for reading the layout volume, ensuring it is
358801c135cSArtem B. Bityutskiy * not corrupted, and recovering from corruptions if needed. Returns volume
359801c135cSArtem B. Bityutskiy * table in case of success and a negative error code in case of failure.
360801c135cSArtem B. Bityutskiy */
process_lvol(struct ubi_device * ubi,struct ubi_attach_info * ai,struct ubi_ainf_volume * av)361e88d6e10SArtem Bityutskiy static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
362a4e6042fSArtem Bityutskiy struct ubi_attach_info *ai,
363517af48cSArtem Bityutskiy struct ubi_ainf_volume *av)
364801c135cSArtem B. Bityutskiy {
365801c135cSArtem B. Bityutskiy int err;
366801c135cSArtem B. Bityutskiy struct rb_node *rb;
3672c5ec5ceSArtem Bityutskiy struct ubi_ainf_peb *aeb;
368801c135cSArtem B. Bityutskiy struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
369801c135cSArtem B. Bityutskiy int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
370801c135cSArtem B. Bityutskiy
371801c135cSArtem B. Bityutskiy /*
372801c135cSArtem B. Bityutskiy * UBI goes through the following steps when it changes the layout
373801c135cSArtem B. Bityutskiy * volume:
374801c135cSArtem B. Bityutskiy * a. erase LEB 0;
375801c135cSArtem B. Bityutskiy * b. write new data to LEB 0;
376801c135cSArtem B. Bityutskiy * c. erase LEB 1;
377801c135cSArtem B. Bityutskiy * d. write new data to LEB 1.
378801c135cSArtem B. Bityutskiy *
379801c135cSArtem B. Bityutskiy * Before the change, both LEBs contain the same data.
380801c135cSArtem B. Bityutskiy *
381801c135cSArtem B. Bityutskiy * Due to unclean reboots, the contents of LEB 0 may be lost, but there
382801c135cSArtem B. Bityutskiy * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not.
383801c135cSArtem B. Bityutskiy * Similarly, LEB 1 may be lost, but there should be LEB 0. And
384801c135cSArtem B. Bityutskiy * finally, unclean reboots may result in a situation when neither LEB
385801c135cSArtem B. Bityutskiy * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB
386801c135cSArtem B. Bityutskiy * 0 contains more recent information.
387801c135cSArtem B. Bityutskiy *
388801c135cSArtem B. Bityutskiy * So the plan is to first check LEB 0. Then
389be436f62SShinya Kuribayashi * a. if LEB 0 is OK, it must be containing the most recent data; then
390801c135cSArtem B. Bityutskiy * we compare it with LEB 1, and if they are different, we copy LEB
391801c135cSArtem B. Bityutskiy * 0 to LEB 1;
392801c135cSArtem B. Bityutskiy * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1
393801c135cSArtem B. Bityutskiy * to LEB 0.
394801c135cSArtem B. Bityutskiy */
395801c135cSArtem B. Bityutskiy
396c8566350SArtem Bityutskiy dbg_gen("check layout volume");
397801c135cSArtem B. Bityutskiy
398801c135cSArtem B. Bityutskiy /* Read both LEB 0 and LEB 1 into memory */
399517af48cSArtem Bityutskiy ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
4002c5ec5ceSArtem Bityutskiy leb[aeb->lnum] = vzalloc(ubi->vtbl_size);
4012c5ec5ceSArtem Bityutskiy if (!leb[aeb->lnum]) {
402801c135cSArtem B. Bityutskiy err = -ENOMEM;
403801c135cSArtem B. Bityutskiy goto out_free;
404801c135cSArtem B. Bityutskiy }
405801c135cSArtem B. Bityutskiy
4062c5ec5ceSArtem Bityutskiy err = ubi_io_read_data(ubi, leb[aeb->lnum], aeb->pnum, 0,
407801c135cSArtem B. Bityutskiy ubi->vtbl_size);
408d57f4054SBrian Norris if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err))
409beeea636SArtem Bityutskiy /*
410beeea636SArtem Bityutskiy * Scrub the PEB later. Note, -EBADMSG indicates an
411beeea636SArtem Bityutskiy * uncorrectable ECC error, but we have our own CRC and
412beeea636SArtem Bityutskiy * the data will be checked later. If the data is OK,
413beeea636SArtem Bityutskiy * the PEB will be scrubbed (because we set
4142c5ec5ceSArtem Bityutskiy * aeb->scrub). If the data is not OK, the contents of
415beeea636SArtem Bityutskiy * the PEB will be recovered from the second copy, and
4162c5ec5ceSArtem Bityutskiy * aeb->scrub will be cleared in
4173561188aSArtem Bityutskiy * 'ubi_add_to_av()'.
418beeea636SArtem Bityutskiy */
4192c5ec5ceSArtem Bityutskiy aeb->scrub = 1;
420801c135cSArtem B. Bityutskiy else if (err)
421801c135cSArtem B. Bityutskiy goto out_free;
422801c135cSArtem B. Bityutskiy }
423801c135cSArtem B. Bityutskiy
424801c135cSArtem B. Bityutskiy err = -EINVAL;
425801c135cSArtem B. Bityutskiy if (leb[0]) {
426801c135cSArtem B. Bityutskiy leb_corrupted[0] = vtbl_check(ubi, leb[0]);
427801c135cSArtem B. Bityutskiy if (leb_corrupted[0] < 0)
428801c135cSArtem B. Bityutskiy goto out_free;
429801c135cSArtem B. Bityutskiy }
430801c135cSArtem B. Bityutskiy
431801c135cSArtem B. Bityutskiy if (!leb_corrupted[0]) {
432801c135cSArtem B. Bityutskiy /* LEB 0 is OK */
433801c135cSArtem B. Bityutskiy if (leb[1])
4349c9ec147SArtem Bityutskiy leb_corrupted[1] = memcmp(leb[0], leb[1],
4359c9ec147SArtem Bityutskiy ubi->vtbl_size);
436801c135cSArtem B. Bityutskiy if (leb_corrupted[1]) {
43732608703STanya Brokhman ubi_warn(ubi, "volume table copy #2 is corrupted");
438a4e6042fSArtem Bityutskiy err = create_vtbl(ubi, ai, 1, leb[0]);
439801c135cSArtem B. Bityutskiy if (err)
440801c135cSArtem B. Bityutskiy goto out_free;
44132608703STanya Brokhman ubi_msg(ubi, "volume table was restored");
442801c135cSArtem B. Bityutskiy }
443801c135cSArtem B. Bityutskiy
444801c135cSArtem B. Bityutskiy /* Both LEB 1 and LEB 2 are OK and consistent */
44592ad8f37SArtem Bityutskiy vfree(leb[1]);
446801c135cSArtem B. Bityutskiy return leb[0];
447801c135cSArtem B. Bityutskiy } else {
448801c135cSArtem B. Bityutskiy /* LEB 0 is corrupted or does not exist */
449801c135cSArtem B. Bityutskiy if (leb[1]) {
450801c135cSArtem B. Bityutskiy leb_corrupted[1] = vtbl_check(ubi, leb[1]);
451801c135cSArtem B. Bityutskiy if (leb_corrupted[1] < 0)
452801c135cSArtem B. Bityutskiy goto out_free;
453801c135cSArtem B. Bityutskiy }
454801c135cSArtem B. Bityutskiy if (leb_corrupted[1]) {
455801c135cSArtem B. Bityutskiy /* Both LEB 0 and LEB 1 are corrupted */
45632608703STanya Brokhman ubi_err(ubi, "both volume tables are corrupted");
457801c135cSArtem B. Bityutskiy goto out_free;
458801c135cSArtem B. Bityutskiy }
459801c135cSArtem B. Bityutskiy
46032608703STanya Brokhman ubi_warn(ubi, "volume table copy #1 is corrupted");
461a4e6042fSArtem Bityutskiy err = create_vtbl(ubi, ai, 0, leb[1]);
462801c135cSArtem B. Bityutskiy if (err)
463801c135cSArtem B. Bityutskiy goto out_free;
46432608703STanya Brokhman ubi_msg(ubi, "volume table was restored");
465801c135cSArtem B. Bityutskiy
46692ad8f37SArtem Bityutskiy vfree(leb[0]);
467801c135cSArtem B. Bityutskiy return leb[1];
468801c135cSArtem B. Bityutskiy }
469801c135cSArtem B. Bityutskiy
470801c135cSArtem B. Bityutskiy out_free:
47192ad8f37SArtem Bityutskiy vfree(leb[0]);
47292ad8f37SArtem Bityutskiy vfree(leb[1]);
473801c135cSArtem B. Bityutskiy return ERR_PTR(err);
474801c135cSArtem B. Bityutskiy }
475801c135cSArtem B. Bityutskiy
476801c135cSArtem B. Bityutskiy /**
477801c135cSArtem B. Bityutskiy * create_empty_lvol - create empty layout volume.
478801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
479a4e6042fSArtem Bityutskiy * @ai: attaching information
480801c135cSArtem B. Bityutskiy *
481801c135cSArtem B. Bityutskiy * This function returns volume table contents in case of success and a
482801c135cSArtem B. Bityutskiy * negative error code in case of failure.
483801c135cSArtem B. Bityutskiy */
create_empty_lvol(struct ubi_device * ubi,struct ubi_attach_info * ai)484e88d6e10SArtem Bityutskiy static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
485a4e6042fSArtem Bityutskiy struct ubi_attach_info *ai)
486801c135cSArtem B. Bityutskiy {
487801c135cSArtem B. Bityutskiy int i;
488801c135cSArtem B. Bityutskiy struct ubi_vtbl_record *vtbl;
489801c135cSArtem B. Bityutskiy
490309b5e4eSJoe Perches vtbl = vzalloc(ubi->vtbl_size);
491801c135cSArtem B. Bityutskiy if (!vtbl)
492801c135cSArtem B. Bityutskiy return ERR_PTR(-ENOMEM);
493801c135cSArtem B. Bityutskiy
494801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots; i++)
495801c135cSArtem B. Bityutskiy memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
496801c135cSArtem B. Bityutskiy
497801c135cSArtem B. Bityutskiy for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
498801c135cSArtem B. Bityutskiy int err;
499801c135cSArtem B. Bityutskiy
500a4e6042fSArtem Bityutskiy err = create_vtbl(ubi, ai, i, vtbl);
501801c135cSArtem B. Bityutskiy if (err) {
50292ad8f37SArtem Bityutskiy vfree(vtbl);
503801c135cSArtem B. Bityutskiy return ERR_PTR(err);
504801c135cSArtem B. Bityutskiy }
505801c135cSArtem B. Bityutskiy }
506801c135cSArtem B. Bityutskiy
507801c135cSArtem B. Bityutskiy return vtbl;
508801c135cSArtem B. Bityutskiy }
509801c135cSArtem B. Bityutskiy
510801c135cSArtem B. Bityutskiy /**
511801c135cSArtem B. Bityutskiy * init_volumes - initialize volume information for existing volumes.
512801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
513a4e6042fSArtem Bityutskiy * @ai: scanning information
514801c135cSArtem B. Bityutskiy * @vtbl: volume table
515801c135cSArtem B. Bityutskiy *
516801c135cSArtem B. Bityutskiy * This function allocates volume description objects for existing volumes.
517801c135cSArtem B. Bityutskiy * Returns zero in case of success and a negative error code in case of
518801c135cSArtem B. Bityutskiy * failure.
519801c135cSArtem B. Bityutskiy */
init_volumes(struct ubi_device * ubi,const struct ubi_attach_info * ai,const struct ubi_vtbl_record * vtbl)520afc15a81SArtem Bityutskiy static int init_volumes(struct ubi_device *ubi,
521a4e6042fSArtem Bityutskiy const struct ubi_attach_info *ai,
522801c135cSArtem B. Bityutskiy const struct ubi_vtbl_record *vtbl)
523801c135cSArtem B. Bityutskiy {
52434653fd8SRichard Weinberger int i, err, reserved_pebs = 0;
525517af48cSArtem Bityutskiy struct ubi_ainf_volume *av;
526801c135cSArtem B. Bityutskiy struct ubi_volume *vol;
527801c135cSArtem B. Bityutskiy
528801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots; i++) {
529801c135cSArtem B. Bityutskiy cond_resched();
530801c135cSArtem B. Bityutskiy
5313261ebd7SChristoph Hellwig if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
532801c135cSArtem B. Bityutskiy continue; /* Empty record */
533801c135cSArtem B. Bityutskiy
534801c135cSArtem B. Bityutskiy vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
535801c135cSArtem B. Bityutskiy if (!vol)
536801c135cSArtem B. Bityutskiy return -ENOMEM;
537801c135cSArtem B. Bityutskiy
5383261ebd7SChristoph Hellwig vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
5393261ebd7SChristoph Hellwig vol->alignment = be32_to_cpu(vtbl[i].alignment);
5403261ebd7SChristoph Hellwig vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
541ff998793SPeter Horton vol->upd_marker = vtbl[i].upd_marker;
542801c135cSArtem B. Bityutskiy vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
543801c135cSArtem B. Bityutskiy UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
5443261ebd7SChristoph Hellwig vol->name_len = be16_to_cpu(vtbl[i].name_len);
545801c135cSArtem B. Bityutskiy vol->usable_leb_size = ubi->leb_size - vol->data_pad;
546801c135cSArtem B. Bityutskiy memcpy(vol->name, vtbl[i].name, vol->name_len);
547801c135cSArtem B. Bityutskiy vol->name[vol->name_len] = '\0';
548801c135cSArtem B. Bityutskiy vol->vol_id = i;
549801c135cSArtem B. Bityutskiy
55062652517SQuentin Schulz if (vtbl[i].flags & UBI_VTBL_SKIP_CRC_CHECK_FLG)
55162652517SQuentin Schulz vol->skip_check = 1;
55262652517SQuentin Schulz
5534ccf8cffSArtem Bityutskiy if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) {
5544ccf8cffSArtem Bityutskiy /* Auto re-size flag may be set only for one volume */
5554ccf8cffSArtem Bityutskiy if (ubi->autoresize_vol_id != -1) {
55632608703STanya Brokhman ubi_err(ubi, "more than one auto-resize volume (%d and %d)",
557049333ceSArtem Bityutskiy ubi->autoresize_vol_id, i);
558f7f02837SAdrian Bunk kfree(vol);
5594ccf8cffSArtem Bityutskiy return -EINVAL;
5604ccf8cffSArtem Bityutskiy }
5614ccf8cffSArtem Bityutskiy
5624ccf8cffSArtem Bityutskiy ubi->autoresize_vol_id = i;
5634ccf8cffSArtem Bityutskiy }
5644ccf8cffSArtem Bityutskiy
565801c135cSArtem B. Bityutskiy ubi_assert(!ubi->volumes[i]);
566801c135cSArtem B. Bityutskiy ubi->volumes[i] = vol;
567801c135cSArtem B. Bityutskiy ubi->vol_count += 1;
568801c135cSArtem B. Bityutskiy vol->ubi = ubi;
569801c135cSArtem B. Bityutskiy reserved_pebs += vol->reserved_pebs;
570801c135cSArtem B. Bityutskiy
571801c135cSArtem B. Bityutskiy /*
57225677478SRichard Weinberger * We use ubi->peb_count and not vol->reserved_pebs because
57325677478SRichard Weinberger * we want to keep the code simple. Otherwise we'd have to
57425677478SRichard Weinberger * resize/check the bitmap upon volume resize too.
57525677478SRichard Weinberger * Allocating a few bytes more does not hurt.
57625677478SRichard Weinberger */
57725677478SRichard Weinberger err = ubi_fastmap_init_checkmap(vol, ubi->peb_count);
57825677478SRichard Weinberger if (err)
57925677478SRichard Weinberger return err;
58025677478SRichard Weinberger
58125677478SRichard Weinberger /*
582801c135cSArtem B. Bityutskiy * In case of dynamic volume UBI knows nothing about how many
583801c135cSArtem B. Bityutskiy * data is stored there. So assume the whole volume is used.
584801c135cSArtem B. Bityutskiy */
585801c135cSArtem B. Bityutskiy if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
586801c135cSArtem B. Bityutskiy vol->used_ebs = vol->reserved_pebs;
587801c135cSArtem B. Bityutskiy vol->last_eb_bytes = vol->usable_leb_size;
588d08c3b78SVinit Agnihotri vol->used_bytes =
589d08c3b78SVinit Agnihotri (long long)vol->used_ebs * vol->usable_leb_size;
590801c135cSArtem B. Bityutskiy continue;
591801c135cSArtem B. Bityutskiy }
592801c135cSArtem B. Bityutskiy
593801c135cSArtem B. Bityutskiy /* Static volumes only */
594dcd85fddSArtem Bityutskiy av = ubi_find_av(ai, i);
595e8c235b0SRichard Weinberger if (!av || !av->leb_count) {
596801c135cSArtem B. Bityutskiy /*
597801c135cSArtem B. Bityutskiy * No eraseblocks belonging to this volume found. We
598801c135cSArtem B. Bityutskiy * don't actually know whether this static volume is
599801c135cSArtem B. Bityutskiy * completely corrupted or just contains no data. And
600801c135cSArtem B. Bityutskiy * we cannot know this as long as data size is not
601801c135cSArtem B. Bityutskiy * stored on flash. So we just assume the volume is
602801c135cSArtem B. Bityutskiy * empty. FIXME: this should be handled.
603801c135cSArtem B. Bityutskiy */
604801c135cSArtem B. Bityutskiy continue;
605801c135cSArtem B. Bityutskiy }
606801c135cSArtem B. Bityutskiy
607517af48cSArtem Bityutskiy if (av->leb_count != av->used_ebs) {
608801c135cSArtem B. Bityutskiy /*
609801c135cSArtem B. Bityutskiy * We found a static volume which misses several
610801c135cSArtem B. Bityutskiy * eraseblocks. Treat it as corrupted.
611801c135cSArtem B. Bityutskiy */
61232608703STanya Brokhman ubi_warn(ubi, "static volume %d misses %d LEBs - corrupted",
613517af48cSArtem Bityutskiy av->vol_id, av->used_ebs - av->leb_count);
614801c135cSArtem B. Bityutskiy vol->corrupted = 1;
615801c135cSArtem B. Bityutskiy continue;
616801c135cSArtem B. Bityutskiy }
617801c135cSArtem B. Bityutskiy
618517af48cSArtem Bityutskiy vol->used_ebs = av->used_ebs;
619d08c3b78SVinit Agnihotri vol->used_bytes =
620d08c3b78SVinit Agnihotri (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
621517af48cSArtem Bityutskiy vol->used_bytes += av->last_data_size;
622517af48cSArtem Bityutskiy vol->last_eb_bytes = av->last_data_size;
623801c135cSArtem B. Bityutskiy }
624801c135cSArtem B. Bityutskiy
625d05c77a8SArtem Bityutskiy /* And add the layout volume */
626801c135cSArtem B. Bityutskiy vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
627801c135cSArtem B. Bityutskiy if (!vol)
628801c135cSArtem B. Bityutskiy return -ENOMEM;
629801c135cSArtem B. Bityutskiy
630801c135cSArtem B. Bityutskiy vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS;
6311f4f4347SRichard Weinberger vol->alignment = UBI_LAYOUT_VOLUME_ALIGN;
632801c135cSArtem B. Bityutskiy vol->vol_type = UBI_DYNAMIC_VOLUME;
633801c135cSArtem B. Bityutskiy vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1;
634801c135cSArtem B. Bityutskiy memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1);
635801c135cSArtem B. Bityutskiy vol->usable_leb_size = ubi->leb_size;
636801c135cSArtem B. Bityutskiy vol->used_ebs = vol->reserved_pebs;
637801c135cSArtem B. Bityutskiy vol->last_eb_bytes = vol->reserved_pebs;
638d08c3b78SVinit Agnihotri vol->used_bytes =
639d08c3b78SVinit Agnihotri (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
64091f2d53cSArtem Bityutskiy vol->vol_id = UBI_LAYOUT_VOLUME_ID;
641d05c77a8SArtem Bityutskiy vol->ref_count = 1;
642801c135cSArtem B. Bityutskiy
643801c135cSArtem B. Bityutskiy ubi_assert(!ubi->volumes[i]);
644801c135cSArtem B. Bityutskiy ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol;
645801c135cSArtem B. Bityutskiy reserved_pebs += vol->reserved_pebs;
646801c135cSArtem B. Bityutskiy ubi->vol_count += 1;
647801c135cSArtem B. Bityutskiy vol->ubi = ubi;
64834653fd8SRichard Weinberger err = ubi_fastmap_init_checkmap(vol, UBI_LAYOUT_VOLUME_EBS);
64934653fd8SRichard Weinberger if (err)
65034653fd8SRichard Weinberger return err;
651801c135cSArtem B. Bityutskiy
6525fc01ab6SArtem Bityutskiy if (reserved_pebs > ubi->avail_pebs) {
65332608703STanya Brokhman ubi_err(ubi, "not enough PEBs, required %d, available %d",
654801c135cSArtem B. Bityutskiy reserved_pebs, ubi->avail_pebs);
6555fc01ab6SArtem Bityutskiy if (ubi->corr_peb_count)
65632608703STanya Brokhman ubi_err(ubi, "%d PEBs are corrupted and not used",
6575fc01ab6SArtem Bityutskiy ubi->corr_peb_count);
6587c7feb2eSshengyong return -ENOSPC;
6595fc01ab6SArtem Bityutskiy }
660801c135cSArtem B. Bityutskiy ubi->rsvd_pebs += reserved_pebs;
661801c135cSArtem B. Bityutskiy ubi->avail_pebs -= reserved_pebs;
662801c135cSArtem B. Bityutskiy
663801c135cSArtem B. Bityutskiy return 0;
664801c135cSArtem B. Bityutskiy }
665801c135cSArtem B. Bityutskiy
666801c135cSArtem B. Bityutskiy /**
667517af48cSArtem Bityutskiy * check_av - check volume attaching information.
668801c135cSArtem B. Bityutskiy * @vol: UBI volume description object
669517af48cSArtem Bityutskiy * @av: volume attaching information
670801c135cSArtem B. Bityutskiy *
671a4e6042fSArtem Bityutskiy * This function returns zero if the volume attaching information is consistent
672801c135cSArtem B. Bityutskiy * to the data read from the volume tabla, and %-EINVAL if not.
673801c135cSArtem B. Bityutskiy */
check_av(const struct ubi_volume * vol,const struct ubi_ainf_volume * av)67445fc5c81STanya Brokhman static int check_av(const struct ubi_volume *vol,
675517af48cSArtem Bityutskiy const struct ubi_ainf_volume *av)
676801c135cSArtem B. Bityutskiy {
677979c9296SArtem Bityutskiy int err;
678979c9296SArtem Bityutskiy
679517af48cSArtem Bityutskiy if (av->highest_lnum >= vol->reserved_pebs) {
680979c9296SArtem Bityutskiy err = 1;
681801c135cSArtem B. Bityutskiy goto bad;
682801c135cSArtem B. Bityutskiy }
683517af48cSArtem Bityutskiy if (av->leb_count > vol->reserved_pebs) {
684979c9296SArtem Bityutskiy err = 2;
685801c135cSArtem B. Bityutskiy goto bad;
686801c135cSArtem B. Bityutskiy }
687517af48cSArtem Bityutskiy if (av->vol_type != vol->vol_type) {
688979c9296SArtem Bityutskiy err = 3;
689801c135cSArtem B. Bityutskiy goto bad;
690801c135cSArtem B. Bityutskiy }
691517af48cSArtem Bityutskiy if (av->used_ebs > vol->reserved_pebs) {
692979c9296SArtem Bityutskiy err = 4;
693801c135cSArtem B. Bityutskiy goto bad;
694801c135cSArtem B. Bityutskiy }
695517af48cSArtem Bityutskiy if (av->data_pad != vol->data_pad) {
696979c9296SArtem Bityutskiy err = 5;
697801c135cSArtem B. Bityutskiy goto bad;
698801c135cSArtem B. Bityutskiy }
699801c135cSArtem B. Bityutskiy return 0;
700801c135cSArtem B. Bityutskiy
701801c135cSArtem B. Bityutskiy bad:
70245fc5c81STanya Brokhman ubi_err(vol->ubi, "bad attaching information, error %d", err);
703517af48cSArtem Bityutskiy ubi_dump_av(av);
704766381f0SArtem Bityutskiy ubi_dump_vol_info(vol);
705801c135cSArtem B. Bityutskiy return -EINVAL;
706801c135cSArtem B. Bityutskiy }
707801c135cSArtem B. Bityutskiy
708801c135cSArtem B. Bityutskiy /**
709fbd0107fSArtem Bityutskiy * check_attaching_info - check that attaching information.
710801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
711a4e6042fSArtem Bityutskiy * @ai: attaching information
712801c135cSArtem B. Bityutskiy *
713801c135cSArtem B. Bityutskiy * Even though we protect on-flash data by CRC checksums, we still don't trust
714a4e6042fSArtem Bityutskiy * the media. This function ensures that attaching information is consistent to
715fbd0107fSArtem Bityutskiy * the information read from the volume table. Returns zero if the attaching
716801c135cSArtem B. Bityutskiy * information is OK and %-EINVAL if it is not.
717801c135cSArtem B. Bityutskiy */
check_attaching_info(const struct ubi_device * ubi,struct ubi_attach_info * ai)718fbd0107fSArtem Bityutskiy static int check_attaching_info(const struct ubi_device *ubi,
719a4e6042fSArtem Bityutskiy struct ubi_attach_info *ai)
720801c135cSArtem B. Bityutskiy {
721801c135cSArtem B. Bityutskiy int err, i;
722517af48cSArtem Bityutskiy struct ubi_ainf_volume *av;
723801c135cSArtem B. Bityutskiy struct ubi_volume *vol;
724801c135cSArtem B. Bityutskiy
725a4e6042fSArtem Bityutskiy if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
72632608703STanya Brokhman ubi_err(ubi, "found %d volumes while attaching, maximum is %d + %d",
727a4e6042fSArtem Bityutskiy ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
728801c135cSArtem B. Bityutskiy return -EINVAL;
729801c135cSArtem B. Bityutskiy }
730801c135cSArtem B. Bityutskiy
731a4e6042fSArtem Bityutskiy if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
732a4e6042fSArtem Bityutskiy ai->highest_vol_id < UBI_INTERNAL_VOL_START) {
73332608703STanya Brokhman ubi_err(ubi, "too large volume ID %d found",
73432608703STanya Brokhman ai->highest_vol_id);
735801c135cSArtem B. Bityutskiy return -EINVAL;
736801c135cSArtem B. Bityutskiy }
737801c135cSArtem B. Bityutskiy
738801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
739801c135cSArtem B. Bityutskiy cond_resched();
740801c135cSArtem B. Bityutskiy
741dcd85fddSArtem Bityutskiy av = ubi_find_av(ai, i);
742801c135cSArtem B. Bityutskiy vol = ubi->volumes[i];
743801c135cSArtem B. Bityutskiy if (!vol) {
744517af48cSArtem Bityutskiy if (av)
745d717dc2fSArtem Bityutskiy ubi_remove_av(ai, av);
746801c135cSArtem B. Bityutskiy continue;
747801c135cSArtem B. Bityutskiy }
748801c135cSArtem B. Bityutskiy
749801c135cSArtem B. Bityutskiy if (vol->reserved_pebs == 0) {
750801c135cSArtem B. Bityutskiy ubi_assert(i < ubi->vtbl_slots);
751801c135cSArtem B. Bityutskiy
752517af48cSArtem Bityutskiy if (!av)
753801c135cSArtem B. Bityutskiy continue;
754801c135cSArtem B. Bityutskiy
755801c135cSArtem B. Bityutskiy /*
756fbd0107fSArtem Bityutskiy * During attaching we found a volume which does not
757801c135cSArtem B. Bityutskiy * exist according to the information in the volume
758801c135cSArtem B. Bityutskiy * table. This must have happened due to an unclean
759801c135cSArtem B. Bityutskiy * reboot while the volume was being removed. Discard
760801c135cSArtem B. Bityutskiy * these eraseblocks.
761801c135cSArtem B. Bityutskiy */
76232608703STanya Brokhman ubi_msg(ubi, "finish volume %d removal", av->vol_id);
763d717dc2fSArtem Bityutskiy ubi_remove_av(ai, av);
764517af48cSArtem Bityutskiy } else if (av) {
76545fc5c81STanya Brokhman err = check_av(vol, av);
766801c135cSArtem B. Bityutskiy if (err)
767801c135cSArtem B. Bityutskiy return err;
768801c135cSArtem B. Bityutskiy }
769801c135cSArtem B. Bityutskiy }
770801c135cSArtem B. Bityutskiy
771801c135cSArtem B. Bityutskiy return 0;
772801c135cSArtem B. Bityutskiy }
773801c135cSArtem B. Bityutskiy
774801c135cSArtem B. Bityutskiy /**
775ebaaf1afSArtem Bityutskiy * ubi_read_volume_table - read the volume table.
776801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
777a4e6042fSArtem Bityutskiy * @ai: attaching information
778801c135cSArtem B. Bityutskiy *
779801c135cSArtem B. Bityutskiy * This function reads volume table, checks it, recover from errors if needed,
780801c135cSArtem B. Bityutskiy * or creates it if needed. Returns zero in case of success and a negative
781801c135cSArtem B. Bityutskiy * error code in case of failure.
782801c135cSArtem B. Bityutskiy */
ubi_read_volume_table(struct ubi_device * ubi,struct ubi_attach_info * ai)783a4e6042fSArtem Bityutskiy int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
784801c135cSArtem B. Bityutskiy {
785fc55dacfSHou Tao int err;
786517af48cSArtem Bityutskiy struct ubi_ainf_volume *av;
787801c135cSArtem B. Bityutskiy
7883261ebd7SChristoph Hellwig empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
789801c135cSArtem B. Bityutskiy
790801c135cSArtem B. Bityutskiy /*
791801c135cSArtem B. Bityutskiy * The number of supported volumes is limited by the eraseblock size
792801c135cSArtem B. Bityutskiy * and by the UBI_MAX_VOLUMES constant.
793801c135cSArtem B. Bityutskiy */
794*d1b505c9SRichard Weinberger
795*d1b505c9SRichard Weinberger if (ubi->leb_size < UBI_VTBL_RECORD_SIZE) {
796*d1b505c9SRichard Weinberger ubi_err(ubi, "LEB size too small for a volume record");
797*d1b505c9SRichard Weinberger return -EINVAL;
798*d1b505c9SRichard Weinberger }
799*d1b505c9SRichard Weinberger
800801c135cSArtem B. Bityutskiy ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE;
801801c135cSArtem B. Bityutskiy if (ubi->vtbl_slots > UBI_MAX_VOLUMES)
802801c135cSArtem B. Bityutskiy ubi->vtbl_slots = UBI_MAX_VOLUMES;
803801c135cSArtem B. Bityutskiy
804801c135cSArtem B. Bityutskiy ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
805801c135cSArtem B. Bityutskiy ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
806801c135cSArtem B. Bityutskiy
807dcd85fddSArtem Bityutskiy av = ubi_find_av(ai, UBI_LAYOUT_VOLUME_ID);
808517af48cSArtem Bityutskiy if (!av) {
809801c135cSArtem B. Bityutskiy /*
810801c135cSArtem B. Bityutskiy * No logical eraseblocks belonging to the layout volume were
811801c135cSArtem B. Bityutskiy * found. This could mean that the flash is just empty. In
812801c135cSArtem B. Bityutskiy * this case we create empty layout volume.
813801c135cSArtem B. Bityutskiy *
814801c135cSArtem B. Bityutskiy * But if flash is not empty this must be a corruption or the
815801c135cSArtem B. Bityutskiy * MTD device just contains garbage.
816801c135cSArtem B. Bityutskiy */
817a4e6042fSArtem Bityutskiy if (ai->is_empty) {
818a4e6042fSArtem Bityutskiy ubi->vtbl = create_empty_lvol(ubi, ai);
819801c135cSArtem B. Bityutskiy if (IS_ERR(ubi->vtbl))
820801c135cSArtem B. Bityutskiy return PTR_ERR(ubi->vtbl);
821801c135cSArtem B. Bityutskiy } else {
82232608703STanya Brokhman ubi_err(ubi, "the layout volume was not found");
823801c135cSArtem B. Bityutskiy return -EINVAL;
824801c135cSArtem B. Bityutskiy }
825801c135cSArtem B. Bityutskiy } else {
826517af48cSArtem Bityutskiy if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) {
827801c135cSArtem B. Bityutskiy /* This must not happen with proper UBI images */
82832608703STanya Brokhman ubi_err(ubi, "too many LEBs (%d) in layout volume",
829517af48cSArtem Bityutskiy av->leb_count);
830801c135cSArtem B. Bityutskiy return -EINVAL;
831801c135cSArtem B. Bityutskiy }
832801c135cSArtem B. Bityutskiy
833517af48cSArtem Bityutskiy ubi->vtbl = process_lvol(ubi, ai, av);
834801c135cSArtem B. Bityutskiy if (IS_ERR(ubi->vtbl))
835801c135cSArtem B. Bityutskiy return PTR_ERR(ubi->vtbl);
836801c135cSArtem B. Bityutskiy }
837801c135cSArtem B. Bityutskiy
8385fc01ab6SArtem Bityutskiy ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
839801c135cSArtem B. Bityutskiy
840801c135cSArtem B. Bityutskiy /*
841801c135cSArtem B. Bityutskiy * The layout volume is OK, initialize the corresponding in-RAM data
842801c135cSArtem B. Bityutskiy * structures.
843801c135cSArtem B. Bityutskiy */
844a4e6042fSArtem Bityutskiy err = init_volumes(ubi, ai, ubi->vtbl);
845801c135cSArtem B. Bityutskiy if (err)
846801c135cSArtem B. Bityutskiy goto out_free;
847801c135cSArtem B. Bityutskiy
848801c135cSArtem B. Bityutskiy /*
849a4e6042fSArtem Bityutskiy * Make sure that the attaching information is consistent to the
850801c135cSArtem B. Bityutskiy * information stored in the volume table.
851801c135cSArtem B. Bityutskiy */
852fbd0107fSArtem Bityutskiy err = check_attaching_info(ubi, ai);
853801c135cSArtem B. Bityutskiy if (err)
854801c135cSArtem B. Bityutskiy goto out_free;
855801c135cSArtem B. Bityutskiy
856801c135cSArtem B. Bityutskiy return 0;
857801c135cSArtem B. Bityutskiy
858801c135cSArtem B. Bityutskiy out_free:
85992ad8f37SArtem Bityutskiy vfree(ubi->vtbl);
860fc55dacfSHou Tao ubi_free_all_volumes(ubi);
861801c135cSArtem B. Bityutskiy return err;
862801c135cSArtem B. Bityutskiy }
863801c135cSArtem B. Bityutskiy
864801c135cSArtem B. Bityutskiy /**
8657bf523aeSArtem Bityutskiy * self_vtbl_check - check volume table.
866801c135cSArtem B. Bityutskiy * @ubi: UBI device description object
867801c135cSArtem B. Bityutskiy */
self_vtbl_check(const struct ubi_device * ubi)8687bf523aeSArtem Bityutskiy static void self_vtbl_check(const struct ubi_device *ubi)
869801c135cSArtem B. Bityutskiy {
87064575574SEzequiel Garcia if (!ubi_dbg_chk_gen(ubi))
87192d124f5SArtem Bityutskiy return;
87292d124f5SArtem Bityutskiy
873801c135cSArtem B. Bityutskiy if (vtbl_check(ubi, ubi->vtbl)) {
87432608703STanya Brokhman ubi_err(ubi, "self-check failed");
875801c135cSArtem B. Bityutskiy BUG();
876801c135cSArtem B. Bityutskiy }
877801c135cSArtem B. Bityutskiy }
878