]>
Commit | Line | Data |
---|---|---|
801c135c AB |
1 | /* |
2 | * Copyright (c) International Business Machines Corp., 2006 | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | |
12 | * the GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | * | |
18 | * Author: Artem Bityutskiy (Битюцкий Артём) | |
19 | */ | |
20 | ||
21 | /* | |
fbd0107f | 22 | * UBI attaching sub-system. |
801c135c | 23 | * |
fbd0107f AB |
24 | * This sub-system is responsible for attaching MTD devices and it also |
25 | * implements flash media scanning. | |
801c135c | 26 | * |
a4e6042f | 27 | * The attaching information is represented by a &struct ubi_attach_info' |
fbd0107f AB |
28 | * object. Information about volumes is represented by &struct ubi_ainf_volume |
29 | * objects which are kept in volume RB-tree with root at the @volumes field. | |
30 | * The RB-tree is indexed by the volume ID. | |
801c135c | 31 | * |
fbd0107f AB |
32 | * Logical eraseblocks are represented by &struct ubi_ainf_peb objects. These |
33 | * objects are kept in per-volume RB-trees with the root at the corresponding | |
34 | * &struct ubi_ainf_volume object. To put it differently, we keep an RB-tree of | |
35 | * per-volume objects and each of these objects is the root of RB-tree of | |
36 | * per-LEB objects. | |
801c135c AB |
37 | * |
38 | * Corrupted physical eraseblocks are put to the @corr list, free physical | |
39 | * eraseblocks are put to the @free list and the physical eraseblock to be | |
40 | * erased are put to the @erase list. | |
0525dac9 | 41 | * |
fef2deb3 AB |
42 | * About corruptions |
43 | * ~~~~~~~~~~~~~~~~~ | |
44 | * | |
45 | * UBI protects EC and VID headers with CRC-32 checksums, so it can detect | |
46 | * whether the headers are corrupted or not. Sometimes UBI also protects the | |
47 | * data with CRC-32, e.g., when it executes the atomic LEB change operation, or | |
48 | * when it moves the contents of a PEB for wear-leveling purposes. | |
49 | * | |
0525dac9 | 50 | * UBI tries to distinguish between 2 types of corruptions. |
fef2deb3 AB |
51 | * |
52 | * 1. Corruptions caused by power cuts. These are expected corruptions and UBI | |
53 | * tries to handle them gracefully, without printing too many warnings and | |
fbd0107f AB |
54 | * error messages. The idea is that we do not lose important data in these |
55 | * cases - we may lose only the data which were being written to the media just | |
56 | * before the power cut happened, and the upper layers (e.g., UBIFS) are | |
57 | * supposed to handle such data losses (e.g., by using the FS journal). | |
fef2deb3 AB |
58 | * |
59 | * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like | |
60 | * the reason is a power cut, UBI puts this PEB to the @erase list, and all | |
61 | * PEBs in the @erase list are scheduled for erasure later. | |
0525dac9 AB |
62 | * |
63 | * 2. Unexpected corruptions which are not caused by power cuts. During | |
fbd0107f | 64 | * attaching, such PEBs are put to the @corr list and UBI preserves them. |
fef2deb3 AB |
65 | * Obviously, this lessens the amount of available PEBs, and if at some point |
66 | * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs | |
67 | * about such PEBs every time the MTD device is attached. | |
45aafd32 AB |
68 | * |
69 | * However, it is difficult to reliably distinguish between these types of | |
fbd0107f AB |
70 | * corruptions and UBI's strategy is as follows (in case of attaching by |
71 | * scanning). UBI assumes corruption type 2 if the VID header is corrupted and | |
72 | * the data area does not contain all 0xFFs, and there were no bit-flips or | |
73 | * integrity errors (e.g., ECC errors in case of NAND) while reading the data | |
74 | * area. Otherwise UBI assumes corruption type 1. So the decision criteria | |
75 | * are as follows. | |
76 | * o If the data area contains only 0xFFs, there are no data, and it is safe | |
fef2deb3 AB |
77 | * to just erase this PEB - this is corruption type 1. |
78 | * o If the data area has bit-flips or data integrity errors (ECC errors on | |
45aafd32 | 79 | * NAND), it is probably a PEB which was being erased when power cut |
fef2deb3 AB |
80 | * happened, so this is corruption type 1. However, this is just a guess, |
81 | * which might be wrong. | |
82 | * o Otherwise this it corruption type 2. | |
801c135c AB |
83 | */ |
84 | ||
85 | #include <linux/err.h> | |
5a0e3ad6 | 86 | #include <linux/slab.h> |
801c135c | 87 | #include <linux/crc32.h> |
3013ee31 | 88 | #include <linux/math64.h> |
095751a6 | 89 | #include <linux/random.h> |
801c135c AB |
90 | #include "ubi.h" |
91 | ||
a4e6042f | 92 | static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai); |
801c135c AB |
93 | |
94 | /* Temporary variables used during scanning */ | |
95 | static struct ubi_ec_hdr *ech; | |
96 | static struct ubi_vid_hdr *vidh; | |
97 | ||
941dfb07 | 98 | /** |
78d87c95 | 99 | * add_to_list - add physical eraseblock to a list. |
a4e6042f | 100 | * @ai: attaching information |
78d87c95 AB |
101 | * @pnum: physical eraseblock number to add |
102 | * @ec: erase counter of the physical eraseblock | |
0525dac9 | 103 | * @to_head: if not zero, add to the head of the list |
78d87c95 AB |
104 | * @list: the list to add to |
105 | * | |
fbd0107f AB |
106 | * This function allocates a 'struct ubi_ainf_peb' object for physical |
107 | * eraseblock @pnum and adds it to the "free", "erase", or "alien" lists. | |
0525dac9 AB |
108 | * If @to_head is not zero, PEB will be added to the head of the list, which |
109 | * basically means it will be processed first later. E.g., we add corrupted | |
110 | * PEBs (corrupted due to power cuts) to the head of the erase list to make | |
111 | * sure we erase them first and get rid of corruptions ASAP. This function | |
112 | * returns zero in case of success and a negative error code in case of | |
3fb34124 | 113 | * failure. |
78d87c95 | 114 | */ |
a4e6042f | 115 | static int add_to_list(struct ubi_attach_info *ai, int pnum, int ec, |
afc15a81 | 116 | int to_head, struct list_head *list) |
801c135c | 117 | { |
2c5ec5ce | 118 | struct ubi_ainf_peb *aeb; |
801c135c | 119 | |
a4e6042f | 120 | if (list == &ai->free) { |
801c135c | 121 | dbg_bld("add to free: PEB %d, EC %d", pnum, ec); |
a4e6042f | 122 | } else if (list == &ai->erase) { |
801c135c | 123 | dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); |
a4e6042f | 124 | } else if (list == &ai->alien) { |
801c135c | 125 | dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); |
a4e6042f | 126 | ai->alien_peb_count += 1; |
33789fb9 | 127 | } else |
801c135c AB |
128 | BUG(); |
129 | ||
1fc2e3e5 | 130 | aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL); |
2c5ec5ce | 131 | if (!aeb) |
801c135c AB |
132 | return -ENOMEM; |
133 | ||
2c5ec5ce AB |
134 | aeb->pnum = pnum; |
135 | aeb->ec = ec; | |
0525dac9 | 136 | if (to_head) |
2c5ec5ce | 137 | list_add(&aeb->u.list, list); |
0525dac9 | 138 | else |
2c5ec5ce | 139 | list_add_tail(&aeb->u.list, list); |
801c135c AB |
140 | return 0; |
141 | } | |
142 | ||
3fb34124 AB |
143 | /** |
144 | * add_corrupted - add a corrupted physical eraseblock. | |
a4e6042f | 145 | * @ai: attaching information |
3fb34124 AB |
146 | * @pnum: physical eraseblock number to add |
147 | * @ec: erase counter of the physical eraseblock | |
148 | * | |
fbd0107f AB |
149 | * This function allocates a 'struct ubi_ainf_peb' object for a corrupted |
150 | * physical eraseblock @pnum and adds it to the 'corr' list. The corruption | |
151 | * was presumably not caused by a power cut. Returns zero in case of success | |
152 | * and a negative error code in case of failure. | |
3fb34124 | 153 | */ |
a4e6042f | 154 | static int add_corrupted(struct ubi_attach_info *ai, int pnum, int ec) |
3fb34124 | 155 | { |
2c5ec5ce | 156 | struct ubi_ainf_peb *aeb; |
3fb34124 AB |
157 | |
158 | dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec); | |
159 | ||
1fc2e3e5 | 160 | aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL); |
2c5ec5ce | 161 | if (!aeb) |
3fb34124 AB |
162 | return -ENOMEM; |
163 | ||
a4e6042f | 164 | ai->corr_peb_count += 1; |
2c5ec5ce AB |
165 | aeb->pnum = pnum; |
166 | aeb->ec = ec; | |
a4e6042f | 167 | list_add(&aeb->u.list, &ai->corr); |
3fb34124 AB |
168 | return 0; |
169 | } | |
170 | ||
801c135c | 171 | /** |
ebaaf1af | 172 | * validate_vid_hdr - check volume identifier header. |
801c135c | 173 | * @vid_hdr: the volume identifier header to check |
517af48c | 174 | * @av: information about the volume this logical eraseblock belongs to |
801c135c AB |
175 | * @pnum: physical eraseblock number the VID header came from |
176 | * | |
177 | * This function checks that data stored in @vid_hdr is consistent. Returns | |
178 | * non-zero if an inconsistency was found and zero if not. | |
179 | * | |
180 | * Note, UBI does sanity check of everything it reads from the flash media. | |
85c6e6e2 | 181 | * Most of the checks are done in the I/O sub-system. Here we check that the |
801c135c AB |
182 | * information in the VID header is consistent to the information in other VID |
183 | * headers of the same volume. | |
184 | */ | |
185 | static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr, | |
517af48c | 186 | const struct ubi_ainf_volume *av, int pnum) |
801c135c AB |
187 | { |
188 | int vol_type = vid_hdr->vol_type; | |
3261ebd7 CH |
189 | int vol_id = be32_to_cpu(vid_hdr->vol_id); |
190 | int used_ebs = be32_to_cpu(vid_hdr->used_ebs); | |
191 | int data_pad = be32_to_cpu(vid_hdr->data_pad); | |
801c135c | 192 | |
517af48c AB |
193 | if (av->leb_count != 0) { |
194 | int av_vol_type; | |
801c135c AB |
195 | |
196 | /* | |
197 | * This is not the first logical eraseblock belonging to this | |
198 | * volume. Ensure that the data in its VID header is consistent | |
199 | * to the data in previous logical eraseblock headers. | |
200 | */ | |
201 | ||
517af48c | 202 | if (vol_id != av->vol_id) { |
e2986827 | 203 | ubi_err("inconsistent vol_id"); |
801c135c AB |
204 | goto bad; |
205 | } | |
206 | ||
517af48c AB |
207 | if (av->vol_type == UBI_STATIC_VOLUME) |
208 | av_vol_type = UBI_VID_STATIC; | |
801c135c | 209 | else |
517af48c | 210 | av_vol_type = UBI_VID_DYNAMIC; |
801c135c | 211 | |
517af48c | 212 | if (vol_type != av_vol_type) { |
e2986827 | 213 | ubi_err("inconsistent vol_type"); |
801c135c AB |
214 | goto bad; |
215 | } | |
216 | ||
517af48c | 217 | if (used_ebs != av->used_ebs) { |
e2986827 | 218 | ubi_err("inconsistent used_ebs"); |
801c135c AB |
219 | goto bad; |
220 | } | |
221 | ||
517af48c | 222 | if (data_pad != av->data_pad) { |
e2986827 | 223 | ubi_err("inconsistent data_pad"); |
801c135c AB |
224 | goto bad; |
225 | } | |
226 | } | |
227 | ||
228 | return 0; | |
229 | ||
230 | bad: | |
231 | ubi_err("inconsistent VID header at PEB %d", pnum); | |
a904e3f1 | 232 | ubi_dump_vid_hdr(vid_hdr); |
517af48c | 233 | ubi_dump_av(av); |
801c135c AB |
234 | return -EINVAL; |
235 | } | |
236 | ||
237 | /** | |
a4e6042f AB |
238 | * add_volume - add volume to the attaching information. |
239 | * @ai: attaching information | |
801c135c AB |
240 | * @vol_id: ID of the volume to add |
241 | * @pnum: physical eraseblock number | |
242 | * @vid_hdr: volume identifier header | |
243 | * | |
244 | * If the volume corresponding to the @vid_hdr logical eraseblock is already | |
a4e6042f AB |
245 | * present in the attaching information, this function does nothing. Otherwise |
246 | * it adds corresponding volume to the attaching information. Returns a pointer | |
fbd0107f AB |
247 | * to the allocated "av" object in case of success and a negative error code in |
248 | * case of failure. | |
801c135c | 249 | */ |
a4e6042f | 250 | static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *ai, |
afc15a81 | 251 | int vol_id, int pnum, |
801c135c AB |
252 | const struct ubi_vid_hdr *vid_hdr) |
253 | { | |
517af48c | 254 | struct ubi_ainf_volume *av; |
a4e6042f | 255 | struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; |
801c135c | 256 | |
3261ebd7 | 257 | ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id)); |
801c135c AB |
258 | |
259 | /* Walk the volume RB-tree to look if this volume is already present */ | |
260 | while (*p) { | |
261 | parent = *p; | |
517af48c | 262 | av = rb_entry(parent, struct ubi_ainf_volume, rb); |
801c135c | 263 | |
517af48c AB |
264 | if (vol_id == av->vol_id) |
265 | return av; | |
801c135c | 266 | |
517af48c | 267 | if (vol_id > av->vol_id) |
801c135c AB |
268 | p = &(*p)->rb_left; |
269 | else | |
270 | p = &(*p)->rb_right; | |
271 | } | |
272 | ||
273 | /* The volume is absent - add it */ | |
517af48c AB |
274 | av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL); |
275 | if (!av) | |
801c135c AB |
276 | return ERR_PTR(-ENOMEM); |
277 | ||
517af48c AB |
278 | av->highest_lnum = av->leb_count = 0; |
279 | av->vol_id = vol_id; | |
280 | av->root = RB_ROOT; | |
281 | av->used_ebs = be32_to_cpu(vid_hdr->used_ebs); | |
282 | av->data_pad = be32_to_cpu(vid_hdr->data_pad); | |
283 | av->compat = vid_hdr->compat; | |
284 | av->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME | |
801c135c | 285 | : UBI_STATIC_VOLUME; |
a4e6042f AB |
286 | if (vol_id > ai->highest_vol_id) |
287 | ai->highest_vol_id = vol_id; | |
801c135c | 288 | |
517af48c AB |
289 | rb_link_node(&av->rb, parent, p); |
290 | rb_insert_color(&av->rb, &ai->volumes); | |
a4e6042f | 291 | ai->vols_found += 1; |
801c135c | 292 | dbg_bld("added volume %d", vol_id); |
517af48c | 293 | return av; |
801c135c AB |
294 | } |
295 | ||
296 | /** | |
297 | * compare_lebs - find out which logical eraseblock is newer. | |
298 | * @ubi: UBI device description object | |
2c5ec5ce | 299 | * @aeb: first logical eraseblock to compare |
801c135c AB |
300 | * @pnum: physical eraseblock number of the second logical eraseblock to |
301 | * compare | |
302 | * @vid_hdr: volume identifier header of the second logical eraseblock | |
303 | * | |
304 | * This function compares 2 copies of a LEB and informs which one is newer. In | |
305 | * case of success this function returns a positive value, in case of failure, a | |
306 | * negative error code is returned. The success return codes use the following | |
307 | * bits: | |
2c5ec5ce | 308 | * o bit 0 is cleared: the first PEB (described by @aeb) is newer than the |
801c135c AB |
309 | * second PEB (described by @pnum and @vid_hdr); |
310 | * o bit 0 is set: the second PEB is newer; | |
311 | * o bit 1 is cleared: no bit-flips were detected in the newer LEB; | |
312 | * o bit 1 is set: bit-flips were detected in the newer LEB; | |
313 | * o bit 2 is cleared: the older LEB is not corrupted; | |
314 | * o bit 2 is set: the older LEB is corrupted. | |
315 | */ | |
2c5ec5ce | 316 | static int compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb, |
e88d6e10 | 317 | int pnum, const struct ubi_vid_hdr *vid_hdr) |
801c135c AB |
318 | { |
319 | void *buf; | |
320 | int len, err, second_is_newer, bitflips = 0, corrupted = 0; | |
321 | uint32_t data_crc, crc; | |
8bc22961 | 322 | struct ubi_vid_hdr *vh = NULL; |
3261ebd7 | 323 | unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); |
801c135c | 324 | |
2c5ec5ce | 325 | if (sqnum2 == aeb->sqnum) { |
801c135c | 326 | /* |
9869cd80 AB |
327 | * This must be a really ancient UBI image which has been |
328 | * created before sequence numbers support has been added. At | |
329 | * that times we used 32-bit LEB versions stored in logical | |
330 | * eraseblocks. That was before UBI got into mainline. We do not | |
0525dac9 AB |
331 | * support these images anymore. Well, those images still work, |
332 | * but only if no unclean reboots happened. | |
801c135c | 333 | */ |
9869cd80 AB |
334 | ubi_err("unsupported on-flash UBI format\n"); |
335 | return -EINVAL; | |
336 | } | |
64203195 | 337 | |
9869cd80 | 338 | /* Obviously the LEB with lower sequence counter is older */ |
2c5ec5ce | 339 | second_is_newer = (sqnum2 > aeb->sqnum); |
801c135c AB |
340 | |
341 | /* | |
342 | * Now we know which copy is newer. If the copy flag of the PEB with | |
343 | * newer version is not set, then we just return, otherwise we have to | |
344 | * check data CRC. For the second PEB we already have the VID header, | |
345 | * for the first one - we'll need to re-read it from flash. | |
346 | * | |
9869cd80 | 347 | * Note: this may be optimized so that we wouldn't read twice. |
801c135c AB |
348 | */ |
349 | ||
350 | if (second_is_newer) { | |
351 | if (!vid_hdr->copy_flag) { | |
352 | /* It is not a copy, so it is newer */ | |
353 | dbg_bld("second PEB %d is newer, copy_flag is unset", | |
354 | pnum); | |
355 | return 1; | |
356 | } | |
357 | } else { | |
2c5ec5ce | 358 | if (!aeb->copy_flag) { |
fb22b59b AB |
359 | /* It is not a copy, so it is newer */ |
360 | dbg_bld("first PEB %d is newer, copy_flag is unset", | |
361 | pnum); | |
362 | return bitflips << 1; | |
363 | } | |
801c135c | 364 | |
33818bbb | 365 | vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); |
8bc22961 | 366 | if (!vh) |
801c135c AB |
367 | return -ENOMEM; |
368 | ||
2c5ec5ce | 369 | pnum = aeb->pnum; |
8bc22961 | 370 | err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); |
801c135c AB |
371 | if (err) { |
372 | if (err == UBI_IO_BITFLIPS) | |
373 | bitflips = 1; | |
374 | else { | |
e2986827 | 375 | ubi_err("VID of PEB %d header is bad, but it " |
0525dac9 | 376 | "was OK earlier, err %d", pnum, err); |
801c135c AB |
377 | if (err > 0) |
378 | err = -EIO; | |
379 | ||
380 | goto out_free_vidh; | |
381 | } | |
382 | } | |
383 | ||
8bc22961 | 384 | vid_hdr = vh; |
801c135c AB |
385 | } |
386 | ||
387 | /* Read the data of the copy and check the CRC */ | |
388 | ||
3261ebd7 | 389 | len = be32_to_cpu(vid_hdr->data_size); |
92ad8f37 | 390 | buf = vmalloc(len); |
801c135c AB |
391 | if (!buf) { |
392 | err = -ENOMEM; | |
393 | goto out_free_vidh; | |
394 | } | |
395 | ||
396 | err = ubi_io_read_data(ubi, buf, pnum, 0, len); | |
d57f4054 | 397 | if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) |
801c135c AB |
398 | goto out_free_buf; |
399 | ||
3261ebd7 | 400 | data_crc = be32_to_cpu(vid_hdr->data_crc); |
801c135c AB |
401 | crc = crc32(UBI_CRC32_INIT, buf, len); |
402 | if (crc != data_crc) { | |
403 | dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x", | |
404 | pnum, crc, data_crc); | |
405 | corrupted = 1; | |
406 | bitflips = 0; | |
407 | second_is_newer = !second_is_newer; | |
408 | } else { | |
409 | dbg_bld("PEB %d CRC is OK", pnum); | |
410 | bitflips = !!err; | |
411 | } | |
412 | ||
92ad8f37 | 413 | vfree(buf); |
8bc22961 | 414 | ubi_free_vid_hdr(ubi, vh); |
801c135c AB |
415 | |
416 | if (second_is_newer) | |
417 | dbg_bld("second PEB %d is newer, copy_flag is set", pnum); | |
418 | else | |
419 | dbg_bld("first PEB %d is newer, copy_flag is set", pnum); | |
420 | ||
421 | return second_is_newer | (bitflips << 1) | (corrupted << 2); | |
422 | ||
423 | out_free_buf: | |
92ad8f37 | 424 | vfree(buf); |
801c135c | 425 | out_free_vidh: |
8bc22961 | 426 | ubi_free_vid_hdr(ubi, vh); |
801c135c AB |
427 | return err; |
428 | } | |
429 | ||
430 | /** | |
fbd0107f | 431 | * ubi_add_to_av - add used physical eraseblock to the attaching information. |
801c135c | 432 | * @ubi: UBI device description object |
a4e6042f | 433 | * @ai: attaching information |
801c135c AB |
434 | * @pnum: the physical eraseblock number |
435 | * @ec: erase counter | |
436 | * @vid_hdr: the volume identifier header | |
437 | * @bitflips: if bit-flips were detected when this physical eraseblock was read | |
438 | * | |
79b510c0 AB |
439 | * This function adds information about a used physical eraseblock to the |
440 | * 'used' tree of the corresponding volume. The function is rather complex | |
441 | * because it has to handle cases when this is not the first physical | |
442 | * eraseblock belonging to the same logical eraseblock, and the newer one has | |
443 | * to be picked, while the older one has to be dropped. This function returns | |
444 | * zero in case of success and a negative error code in case of failure. | |
801c135c | 445 | */ |
3561188a AB |
446 | int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum, |
447 | int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips) | |
801c135c AB |
448 | { |
449 | int err, vol_id, lnum; | |
801c135c | 450 | unsigned long long sqnum; |
517af48c | 451 | struct ubi_ainf_volume *av; |
2c5ec5ce | 452 | struct ubi_ainf_peb *aeb; |
801c135c AB |
453 | struct rb_node **p, *parent = NULL; |
454 | ||
3261ebd7 CH |
455 | vol_id = be32_to_cpu(vid_hdr->vol_id); |
456 | lnum = be32_to_cpu(vid_hdr->lnum); | |
457 | sqnum = be64_to_cpu(vid_hdr->sqnum); | |
801c135c | 458 | |
9869cd80 AB |
459 | dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d", |
460 | pnum, vol_id, lnum, ec, sqnum, bitflips); | |
801c135c | 461 | |
517af48c AB |
462 | av = add_volume(ai, vol_id, pnum, vid_hdr); |
463 | if (IS_ERR(av)) | |
464 | return PTR_ERR(av); | |
801c135c | 465 | |
a4e6042f AB |
466 | if (ai->max_sqnum < sqnum) |
467 | ai->max_sqnum = sqnum; | |
76eafe47 | 468 | |
801c135c AB |
469 | /* |
470 | * Walk the RB-tree of logical eraseblocks of volume @vol_id to look | |
471 | * if this is the first instance of this logical eraseblock or not. | |
472 | */ | |
517af48c | 473 | p = &av->root.rb_node; |
801c135c AB |
474 | while (*p) { |
475 | int cmp_res; | |
476 | ||
477 | parent = *p; | |
2c5ec5ce AB |
478 | aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb); |
479 | if (lnum != aeb->lnum) { | |
480 | if (lnum < aeb->lnum) | |
801c135c AB |
481 | p = &(*p)->rb_left; |
482 | else | |
483 | p = &(*p)->rb_right; | |
484 | continue; | |
485 | } | |
486 | ||
487 | /* | |
488 | * There is already a physical eraseblock describing the same | |
489 | * logical eraseblock present. | |
490 | */ | |
491 | ||
2c5ec5ce AB |
492 | dbg_bld("this LEB already exists: PEB %d, sqnum %llu, EC %d", |
493 | aeb->pnum, aeb->sqnum, aeb->ec); | |
801c135c AB |
494 | |
495 | /* | |
496 | * Make sure that the logical eraseblocks have different | |
497 | * sequence numbers. Otherwise the image is bad. | |
498 | * | |
9869cd80 AB |
499 | * However, if the sequence number is zero, we assume it must |
500 | * be an ancient UBI image from the era when UBI did not have | |
501 | * sequence numbers. We still can attach these images, unless | |
502 | * there is a need to distinguish between old and new | |
503 | * eraseblocks, in which case we'll refuse the image in | |
504 | * 'compare_lebs()'. In other words, we attach old clean | |
505 | * images, but refuse attaching old images with duplicated | |
506 | * logical eraseblocks because there was an unclean reboot. | |
801c135c | 507 | */ |
2c5ec5ce | 508 | if (aeb->sqnum == sqnum && sqnum != 0) { |
801c135c AB |
509 | ubi_err("two LEBs with same sequence number %llu", |
510 | sqnum); | |
2c5ec5ce | 511 | ubi_dump_aeb(aeb, 0); |
a904e3f1 | 512 | ubi_dump_vid_hdr(vid_hdr); |
801c135c AB |
513 | return -EINVAL; |
514 | } | |
515 | ||
516 | /* | |
517 | * Now we have to drop the older one and preserve the newer | |
518 | * one. | |
519 | */ | |
2c5ec5ce | 520 | cmp_res = compare_lebs(ubi, aeb, pnum, vid_hdr); |
801c135c AB |
521 | if (cmp_res < 0) |
522 | return cmp_res; | |
523 | ||
524 | if (cmp_res & 1) { | |
525 | /* | |
3f502622 | 526 | * This logical eraseblock is newer than the one |
801c135c AB |
527 | * found earlier. |
528 | */ | |
517af48c | 529 | err = validate_vid_hdr(vid_hdr, av, pnum); |
801c135c AB |
530 | if (err) |
531 | return err; | |
532 | ||
a4e6042f AB |
533 | err = add_to_list(ai, aeb->pnum, aeb->ec, cmp_res & 4, |
534 | &ai->erase); | |
801c135c AB |
535 | if (err) |
536 | return err; | |
537 | ||
2c5ec5ce AB |
538 | aeb->ec = ec; |
539 | aeb->pnum = pnum; | |
540 | aeb->scrub = ((cmp_res & 2) || bitflips); | |
541 | aeb->copy_flag = vid_hdr->copy_flag; | |
542 | aeb->sqnum = sqnum; | |
801c135c | 543 | |
517af48c AB |
544 | if (av->highest_lnum == lnum) |
545 | av->last_data_size = | |
3261ebd7 | 546 | be32_to_cpu(vid_hdr->data_size); |
801c135c AB |
547 | |
548 | return 0; | |
549 | } else { | |
550 | /* | |
025dfdaf | 551 | * This logical eraseblock is older than the one found |
801c135c AB |
552 | * previously. |
553 | */ | |
a4e6042f AB |
554 | return add_to_list(ai, pnum, ec, cmp_res & 4, |
555 | &ai->erase); | |
801c135c AB |
556 | } |
557 | } | |
558 | ||
559 | /* | |
560 | * We've met this logical eraseblock for the first time, add it to the | |
a4e6042f | 561 | * attaching information. |
801c135c AB |
562 | */ |
563 | ||
517af48c | 564 | err = validate_vid_hdr(vid_hdr, av, pnum); |
801c135c AB |
565 | if (err) |
566 | return err; | |
567 | ||
1fc2e3e5 | 568 | aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL); |
2c5ec5ce | 569 | if (!aeb) |
801c135c AB |
570 | return -ENOMEM; |
571 | ||
2c5ec5ce AB |
572 | aeb->ec = ec; |
573 | aeb->pnum = pnum; | |
574 | aeb->lnum = lnum; | |
575 | aeb->scrub = bitflips; | |
576 | aeb->copy_flag = vid_hdr->copy_flag; | |
577 | aeb->sqnum = sqnum; | |
801c135c | 578 | |
517af48c AB |
579 | if (av->highest_lnum <= lnum) { |
580 | av->highest_lnum = lnum; | |
581 | av->last_data_size = be32_to_cpu(vid_hdr->data_size); | |
801c135c AB |
582 | } |
583 | ||
517af48c | 584 | av->leb_count += 1; |
2c5ec5ce | 585 | rb_link_node(&aeb->u.rb, parent, p); |
517af48c | 586 | rb_insert_color(&aeb->u.rb, &av->root); |
801c135c AB |
587 | return 0; |
588 | } | |
589 | ||
590 | /** | |
dcd85fdd | 591 | * ubi_find_av - find volume in the attaching information. |
a4e6042f | 592 | * @ai: attaching information |
801c135c AB |
593 | * @vol_id: the requested volume ID |
594 | * | |
595 | * This function returns a pointer to the volume description or %NULL if there | |
a4e6042f | 596 | * are no data about this volume in the attaching information. |
801c135c | 597 | */ |
dcd85fdd AB |
598 | struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai, |
599 | int vol_id) | |
801c135c | 600 | { |
517af48c | 601 | struct ubi_ainf_volume *av; |
a4e6042f | 602 | struct rb_node *p = ai->volumes.rb_node; |
801c135c AB |
603 | |
604 | while (p) { | |
517af48c | 605 | av = rb_entry(p, struct ubi_ainf_volume, rb); |
801c135c | 606 | |
517af48c AB |
607 | if (vol_id == av->vol_id) |
608 | return av; | |
801c135c | 609 | |
517af48c | 610 | if (vol_id > av->vol_id) |
801c135c AB |
611 | p = p->rb_left; |
612 | else | |
613 | p = p->rb_right; | |
614 | } | |
615 | ||
616 | return NULL; | |
617 | } | |
618 | ||
801c135c | 619 | /** |
d717dc2f | 620 | * ubi_remove_av - delete attaching information about a volume. |
a4e6042f | 621 | * @ai: attaching information |
517af48c | 622 | * @av: the volume attaching information to delete |
801c135c | 623 | */ |
d717dc2f | 624 | void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av) |
801c135c AB |
625 | { |
626 | struct rb_node *rb; | |
2c5ec5ce | 627 | struct ubi_ainf_peb *aeb; |
801c135c | 628 | |
517af48c | 629 | dbg_bld("remove attaching information about volume %d", av->vol_id); |
801c135c | 630 | |
517af48c | 631 | while ((rb = rb_first(&av->root))) { |
2c5ec5ce | 632 | aeb = rb_entry(rb, struct ubi_ainf_peb, u.rb); |
517af48c | 633 | rb_erase(&aeb->u.rb, &av->root); |
a4e6042f | 634 | list_add_tail(&aeb->u.list, &ai->erase); |
801c135c AB |
635 | } |
636 | ||
517af48c AB |
637 | rb_erase(&av->rb, &ai->volumes); |
638 | kfree(av); | |
a4e6042f | 639 | ai->vols_found -= 1; |
801c135c AB |
640 | } |
641 | ||
642 | /** | |
13d33dad | 643 | * early_erase_peb - erase a physical eraseblock. |
801c135c | 644 | * @ubi: UBI device description object |
a4e6042f | 645 | * @ai: attaching information |
801c135c AB |
646 | * @pnum: physical eraseblock number to erase; |
647 | * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown) | |
648 | * | |
649 | * This function erases physical eraseblock 'pnum', and writes the erase | |
650 | * counter header to it. This function should only be used on UBI device | |
85c6e6e2 AB |
651 | * initialization stages, when the EBA sub-system had not been yet initialized. |
652 | * This function returns zero in case of success and a negative error code in | |
653 | * case of failure. | |
801c135c | 654 | */ |
13d33dad AB |
655 | static int early_erase_peb(struct ubi_device *ubi, |
656 | const struct ubi_attach_info *ai, int pnum, int ec) | |
801c135c AB |
657 | { |
658 | int err; | |
659 | struct ubi_ec_hdr *ec_hdr; | |
660 | ||
801c135c AB |
661 | if ((long long)ec >= UBI_MAX_ERASECOUNTER) { |
662 | /* | |
663 | * Erase counter overflow. Upgrade UBI and use 64-bit | |
664 | * erase counters internally. | |
665 | */ | |
666 | ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec); | |
667 | return -EINVAL; | |
668 | } | |
669 | ||
dcec4c3b FM |
670 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
671 | if (!ec_hdr) | |
672 | return -ENOMEM; | |
673 | ||
3261ebd7 | 674 | ec_hdr->ec = cpu_to_be64(ec); |
801c135c AB |
675 | |
676 | err = ubi_io_sync_erase(ubi, pnum, 0); | |
677 | if (err < 0) | |
678 | goto out_free; | |
679 | ||
680 | err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); | |
681 | ||
682 | out_free: | |
683 | kfree(ec_hdr); | |
684 | return err; | |
685 | } | |
686 | ||
687 | /** | |
c87fbd7d | 688 | * ubi_early_get_peb - get a free physical eraseblock. |
801c135c | 689 | * @ubi: UBI device description object |
a4e6042f | 690 | * @ai: attaching information |
801c135c AB |
691 | * |
692 | * This function returns a free physical eraseblock. It is supposed to be | |
85c6e6e2 AB |
693 | * called on the UBI initialization stages when the wear-leveling sub-system is |
694 | * not initialized yet. This function picks a physical eraseblocks from one of | |
695 | * the lists, writes the EC header if it is needed, and removes it from the | |
696 | * list. | |
801c135c | 697 | * |
fbd0107f AB |
698 | * This function returns a pointer to the "aeb" of the found free PEB in case |
699 | * of success and an error code in case of failure. | |
801c135c | 700 | */ |
c87fbd7d AB |
701 | struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi, |
702 | struct ubi_attach_info *ai) | |
801c135c | 703 | { |
5fc01ab6 | 704 | int err = 0; |
2c5ec5ce | 705 | struct ubi_ainf_peb *aeb, *tmp_aeb; |
801c135c | 706 | |
a4e6042f AB |
707 | if (!list_empty(&ai->free)) { |
708 | aeb = list_entry(ai->free.next, struct ubi_ainf_peb, u.list); | |
2c5ec5ce AB |
709 | list_del(&aeb->u.list); |
710 | dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec); | |
711 | return aeb; | |
801c135c AB |
712 | } |
713 | ||
5fc01ab6 AB |
714 | /* |
715 | * We try to erase the first physical eraseblock from the erase list | |
716 | * and pick it if we succeed, or try to erase the next one if not. And | |
717 | * so forth. We don't want to take care about bad eraseblocks here - | |
718 | * they'll be handled later. | |
719 | */ | |
a4e6042f | 720 | list_for_each_entry_safe(aeb, tmp_aeb, &ai->erase, u.list) { |
2c5ec5ce | 721 | if (aeb->ec == UBI_SCAN_UNKNOWN_EC) |
a4e6042f | 722 | aeb->ec = ai->mean_ec; |
801c135c | 723 | |
13d33dad | 724 | err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1); |
5fc01ab6 AB |
725 | if (err) |
726 | continue; | |
801c135c | 727 | |
2c5ec5ce AB |
728 | aeb->ec += 1; |
729 | list_del(&aeb->u.list); | |
730 | dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec); | |
731 | return aeb; | |
801c135c AB |
732 | } |
733 | ||
5fc01ab6 | 734 | ubi_err("no free eraseblocks"); |
801c135c AB |
735 | return ERR_PTR(-ENOSPC); |
736 | } | |
737 | ||
feeba4b8 | 738 | /** |
45aafd32 | 739 | * check_corruption - check the data area of PEB. |
feeba4b8 AB |
740 | * @ubi: UBI device description object |
741 | * @vid_hrd: the (corrupted) VID header of this PEB | |
742 | * @pnum: the physical eraseblock number to check | |
743 | * | |
744 | * This is a helper function which is used to distinguish between VID header | |
745 | * corruptions caused by power cuts and other reasons. If the PEB contains only | |
45aafd32 | 746 | * 0xFF bytes in the data area, the VID header is most probably corrupted |
feeba4b8 | 747 | * because of a power cut (%0 is returned in this case). Otherwise, it was |
45aafd32 AB |
748 | * probably corrupted for some other reasons (%1 is returned in this case). A |
749 | * negative error code is returned if a read error occurred. | |
feeba4b8 AB |
750 | * |
751 | * If the corruption reason was a power cut, UBI can safely erase this PEB. | |
752 | * Otherwise, it should preserve it to avoid possibly destroying important | |
753 | * information. | |
754 | */ | |
45aafd32 AB |
755 | static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr, |
756 | int pnum) | |
feeba4b8 AB |
757 | { |
758 | int err; | |
759 | ||
760 | mutex_lock(&ubi->buf_mutex); | |
0ca39d74 | 761 | memset(ubi->peb_buf, 0x00, ubi->leb_size); |
feeba4b8 | 762 | |
0ca39d74 | 763 | err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start, |
feeba4b8 | 764 | ubi->leb_size); |
d57f4054 | 765 | if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) { |
45aafd32 AB |
766 | /* |
767 | * Bit-flips or integrity errors while reading the data area. | |
768 | * It is difficult to say for sure what type of corruption is | |
769 | * this, but presumably a power cut happened while this PEB was | |
770 | * erased, so it became unstable and corrupted, and should be | |
771 | * erased. | |
772 | */ | |
1b1d76e2 DC |
773 | err = 0; |
774 | goto out_unlock; | |
45aafd32 AB |
775 | } |
776 | ||
777 | if (err) | |
1b1d76e2 | 778 | goto out_unlock; |
feeba4b8 | 779 | |
0ca39d74 | 780 | if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size)) |
1b1d76e2 | 781 | goto out_unlock; |
feeba4b8 AB |
782 | |
783 | ubi_err("PEB %d contains corrupted VID header, and the data does not " | |
784 | "contain all 0xFF, this may be a non-UBI PEB or a severe VID " | |
785 | "header corruption which requires manual inspection", pnum); | |
a904e3f1 | 786 | ubi_dump_vid_hdr(vid_hdr); |
feeba4b8 AB |
787 | dbg_msg("hexdump of PEB %d offset %d, length %d", |
788 | pnum, ubi->leb_start, ubi->leb_size); | |
789 | ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, | |
0ca39d74 | 790 | ubi->peb_buf, ubi->leb_size, 1); |
1b1d76e2 DC |
791 | err = 1; |
792 | ||
793 | out_unlock: | |
feeba4b8 | 794 | mutex_unlock(&ubi->buf_mutex); |
1b1d76e2 | 795 | return err; |
feeba4b8 AB |
796 | } |
797 | ||
801c135c | 798 | /** |
fbd0107f | 799 | * scan_peb - scan and process UBI headers of a PEB. |
801c135c | 800 | * @ubi: UBI device description object |
a4e6042f | 801 | * @ai: attaching information |
801c135c AB |
802 | * @pnum: the physical eraseblock number |
803 | * | |
fbd0107f AB |
804 | * This function reads UBI headers of PEB @pnum, checks them, and adds |
805 | * information about this PEB to the corresponding list or RB-tree in the | |
806 | * "attaching info" structure. Returns zero if the physical eraseblock was | |
807 | * successfully handled and a negative error code in case of failure. | |
801c135c | 808 | */ |
fbd0107f AB |
809 | static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai, |
810 | int pnum) | |
801c135c | 811 | { |
c18a8418 | 812 | long long uninitialized_var(ec); |
e0e718c2 | 813 | int err, bitflips = 0, vol_id, ec_err = 0; |
801c135c AB |
814 | |
815 | dbg_bld("scan PEB %d", pnum); | |
816 | ||
817 | /* Skip bad physical eraseblocks */ | |
818 | err = ubi_io_is_bad(ubi, pnum); | |
819 | if (err < 0) | |
820 | return err; | |
821 | else if (err) { | |
a4e6042f | 822 | ai->bad_peb_count += 1; |
801c135c AB |
823 | return 0; |
824 | } | |
825 | ||
826 | err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); | |
827 | if (err < 0) | |
828 | return err; | |
b3321508 AB |
829 | switch (err) { |
830 | case 0: | |
831 | break; | |
832 | case UBI_IO_BITFLIPS: | |
801c135c | 833 | bitflips = 1; |
b3321508 AB |
834 | break; |
835 | case UBI_IO_FF: | |
a4e6042f AB |
836 | ai->empty_peb_count += 1; |
837 | return add_to_list(ai, pnum, UBI_SCAN_UNKNOWN_EC, 0, | |
838 | &ai->erase); | |
b3321508 | 839 | case UBI_IO_FF_BITFLIPS: |
a4e6042f AB |
840 | ai->empty_peb_count += 1; |
841 | return add_to_list(ai, pnum, UBI_SCAN_UNKNOWN_EC, 1, | |
842 | &ai->erase); | |
b3321508 | 843 | case UBI_IO_BAD_HDR_EBADMSG: |
b3321508 | 844 | case UBI_IO_BAD_HDR: |
801c135c AB |
845 | /* |
846 | * We have to also look at the VID header, possibly it is not | |
847 | * corrupted. Set %bitflips flag in order to make this PEB be | |
848 | * moved and EC be re-created. | |
849 | */ | |
e0e718c2 | 850 | ec_err = err; |
801c135c AB |
851 | ec = UBI_SCAN_UNKNOWN_EC; |
852 | bitflips = 1; | |
b3321508 AB |
853 | break; |
854 | default: | |
855 | ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err); | |
856 | return -EINVAL; | |
801c135c AB |
857 | } |
858 | ||
e0e718c2 | 859 | if (!ec_err) { |
fe96efc1 AB |
860 | int image_seq; |
861 | ||
801c135c AB |
862 | /* Make sure UBI version is OK */ |
863 | if (ech->version != UBI_VERSION) { | |
864 | ubi_err("this UBI version is %d, image version is %d", | |
865 | UBI_VERSION, (int)ech->version); | |
866 | return -EINVAL; | |
867 | } | |
868 | ||
3261ebd7 | 869 | ec = be64_to_cpu(ech->ec); |
801c135c AB |
870 | if (ec > UBI_MAX_ERASECOUNTER) { |
871 | /* | |
872 | * Erase counter overflow. The EC headers have 64 bits | |
873 | * reserved, but we anyway make use of only 31 bit | |
874 | * values, as this seems to be enough for any existing | |
875 | * flash. Upgrade UBI and use 64-bit erase counters | |
876 | * internally. | |
877 | */ | |
878 | ubi_err("erase counter overflow, max is %d", | |
879 | UBI_MAX_ERASECOUNTER); | |
a904e3f1 | 880 | ubi_dump_ec_hdr(ech); |
801c135c AB |
881 | return -EINVAL; |
882 | } | |
fe96efc1 | 883 | |
32bc4820 AH |
884 | /* |
885 | * Make sure that all PEBs have the same image sequence number. | |
886 | * This allows us to detect situations when users flash UBI | |
887 | * images incorrectly, so that the flash has the new UBI image | |
888 | * and leftovers from the old one. This feature was added | |
889 | * relatively recently, and the sequence number was always | |
890 | * zero, because old UBI implementations always set it to zero. | |
891 | * For this reasons, we do not panic if some PEBs have zero | |
892 | * sequence number, while other PEBs have non-zero sequence | |
893 | * number. | |
894 | */ | |
3dc948da | 895 | image_seq = be32_to_cpu(ech->image_seq); |
2eadaad6 | 896 | if (!ubi->image_seq && image_seq) |
fe96efc1 | 897 | ubi->image_seq = image_seq; |
2eadaad6 AB |
898 | if (ubi->image_seq && image_seq && |
899 | ubi->image_seq != image_seq) { | |
fe96efc1 AB |
900 | ubi_err("bad image sequence number %d in PEB %d, " |
901 | "expected %d", image_seq, pnum, ubi->image_seq); | |
a904e3f1 | 902 | ubi_dump_ec_hdr(ech); |
fe96efc1 AB |
903 | return -EINVAL; |
904 | } | |
801c135c AB |
905 | } |
906 | ||
907 | /* OK, we've done with the EC header, let's look at the VID header */ | |
908 | ||
909 | err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0); | |
910 | if (err < 0) | |
911 | return err; | |
b3321508 AB |
912 | switch (err) { |
913 | case 0: | |
914 | break; | |
915 | case UBI_IO_BITFLIPS: | |
801c135c | 916 | bitflips = 1; |
b3321508 AB |
917 | break; |
918 | case UBI_IO_BAD_HDR_EBADMSG: | |
0525dac9 AB |
919 | if (ec_err == UBI_IO_BAD_HDR_EBADMSG) |
920 | /* | |
921 | * Both EC and VID headers are corrupted and were read | |
922 | * with data integrity error, probably this is a bad | |
923 | * PEB, bit it is not marked as bad yet. This may also | |
924 | * be a result of power cut during erasure. | |
925 | */ | |
a4e6042f | 926 | ai->maybe_bad_peb_count += 1; |
b3321508 | 927 | case UBI_IO_BAD_HDR: |
feeba4b8 AB |
928 | if (ec_err) |
929 | /* | |
930 | * Both headers are corrupted. There is a possibility | |
931 | * that this a valid UBI PEB which has corresponding | |
932 | * LEB, but the headers are corrupted. However, it is | |
933 | * impossible to distinguish it from a PEB which just | |
45aafd32 | 934 | * contains garbage because of a power cut during erase |
feeba4b8 | 935 | * operation. So we just schedule this PEB for erasure. |
7ac760c2 | 936 | * |
25985edc | 937 | * Besides, in case of NOR flash, we deliberately |
7ac760c2 AB |
938 | * corrupt both headers because NOR flash erasure is |
939 | * slow and can start from the end. | |
feeba4b8 AB |
940 | */ |
941 | err = 0; | |
942 | else | |
943 | /* | |
944 | * The EC was OK, but the VID header is corrupted. We | |
945 | * have to check what is in the data area. | |
946 | */ | |
45aafd32 | 947 | err = check_corruption(ubi, vidh, pnum); |
df3fca4c AB |
948 | |
949 | if (err < 0) | |
950 | return err; | |
951 | else if (!err) | |
feeba4b8 | 952 | /* This corruption is caused by a power cut */ |
a4e6042f | 953 | err = add_to_list(ai, pnum, ec, 1, &ai->erase); |
feeba4b8 AB |
954 | else |
955 | /* This is an unexpected corruption */ | |
a4e6042f | 956 | err = add_corrupted(ai, pnum, ec); |
feeba4b8 AB |
957 | if (err) |
958 | return err; | |
959 | goto adjust_mean_ec; | |
b3321508 | 960 | case UBI_IO_FF_BITFLIPS: |
a4e6042f | 961 | err = add_to_list(ai, pnum, ec, 1, &ai->erase); |
801c135c AB |
962 | if (err) |
963 | return err; | |
964 | goto adjust_mean_ec; | |
b3321508 AB |
965 | case UBI_IO_FF: |
966 | if (ec_err) | |
a4e6042f | 967 | err = add_to_list(ai, pnum, ec, 1, &ai->erase); |
b3321508 | 968 | else |
a4e6042f | 969 | err = add_to_list(ai, pnum, ec, 0, &ai->free); |
801c135c AB |
970 | if (err) |
971 | return err; | |
972 | goto adjust_mean_ec; | |
b3321508 AB |
973 | default: |
974 | ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d", | |
975 | err); | |
976 | return -EINVAL; | |
801c135c AB |
977 | } |
978 | ||
3261ebd7 | 979 | vol_id = be32_to_cpu(vidh->vol_id); |
91f2d53c | 980 | if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) { |
3261ebd7 | 981 | int lnum = be32_to_cpu(vidh->lnum); |
801c135c AB |
982 | |
983 | /* Unsupported internal volume */ | |
984 | switch (vidh->compat) { | |
985 | case UBI_COMPAT_DELETE: | |
986 | ubi_msg("\"delete\" compatible internal volume %d:%d" | |
158132c9 | 987 | " found, will remove it", vol_id, lnum); |
a4e6042f | 988 | err = add_to_list(ai, pnum, ec, 1, &ai->erase); |
801c135c AB |
989 | if (err) |
990 | return err; | |
158132c9 | 991 | return 0; |
801c135c AB |
992 | |
993 | case UBI_COMPAT_RO: | |
994 | ubi_msg("read-only compatible internal volume %d:%d" | |
995 | " found, switch to read-only mode", | |
996 | vol_id, lnum); | |
997 | ubi->ro_mode = 1; | |
998 | break; | |
999 | ||
1000 | case UBI_COMPAT_PRESERVE: | |
1001 | ubi_msg("\"preserve\" compatible internal volume %d:%d" | |
1002 | " found", vol_id, lnum); | |
a4e6042f | 1003 | err = add_to_list(ai, pnum, ec, 0, &ai->alien); |
801c135c AB |
1004 | if (err) |
1005 | return err; | |
801c135c AB |
1006 | return 0; |
1007 | ||
1008 | case UBI_COMPAT_REJECT: | |
1009 | ubi_err("incompatible internal volume %d:%d found", | |
1010 | vol_id, lnum); | |
1011 | return -EINVAL; | |
1012 | } | |
1013 | } | |
1014 | ||
e0e718c2 | 1015 | if (ec_err) |
29a88c99 AB |
1016 | ubi_warn("valid VID header but corrupted EC header at PEB %d", |
1017 | pnum); | |
3561188a | 1018 | err = ubi_add_to_av(ubi, ai, pnum, ec, vidh, bitflips); |
801c135c AB |
1019 | if (err) |
1020 | return err; | |
1021 | ||
1022 | adjust_mean_ec: | |
e0e718c2 | 1023 | if (!ec_err) { |
a4e6042f AB |
1024 | ai->ec_sum += ec; |
1025 | ai->ec_count += 1; | |
1026 | if (ec > ai->max_ec) | |
1027 | ai->max_ec = ec; | |
1028 | if (ec < ai->min_ec) | |
1029 | ai->min_ec = ec; | |
801c135c AB |
1030 | } |
1031 | ||
1032 | return 0; | |
1033 | } | |
1034 | ||
0798cea8 | 1035 | /** |
fbd0107f | 1036 | * late_analysis - analyze the overall situation with PEB. |
0798cea8 | 1037 | * @ubi: UBI device description object |
a4e6042f | 1038 | * @ai: attaching information |
0798cea8 | 1039 | * |
fbd0107f AB |
1040 | * This is a helper function which takes a look what PEBs we have after we |
1041 | * gather information about all of them ("ai" is compete). It decides whether | |
1042 | * the flash is empty and should be formatted of whether there are too many | |
1043 | * corrupted PEBs and we should not attach this MTD device. Returns zero if we | |
1044 | * should proceed with attaching the MTD device, and %-EINVAL if we should not. | |
0798cea8 | 1045 | */ |
fbd0107f | 1046 | static int late_analysis(struct ubi_device *ubi, struct ubi_attach_info *ai) |
0798cea8 | 1047 | { |
2c5ec5ce | 1048 | struct ubi_ainf_peb *aeb; |
0525dac9 | 1049 | int max_corr, peb_count; |
0798cea8 | 1050 | |
a4e6042f | 1051 | peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count; |
0525dac9 | 1052 | max_corr = peb_count / 20 ?: 8; |
0798cea8 AB |
1053 | |
1054 | /* | |
0525dac9 | 1055 | * Few corrupted PEBs is not a problem and may be just a result of |
0798cea8 AB |
1056 | * unclean reboots. However, many of them may indicate some problems |
1057 | * with the flash HW or driver. | |
1058 | */ | |
a4e6042f | 1059 | if (ai->corr_peb_count) { |
0525dac9 | 1060 | ubi_err("%d PEBs are corrupted and preserved", |
a4e6042f | 1061 | ai->corr_peb_count); |
0525dac9 | 1062 | printk(KERN_ERR "Corrupted PEBs are:"); |
a4e6042f | 1063 | list_for_each_entry(aeb, &ai->corr, u.list) |
2c5ec5ce | 1064 | printk(KERN_CONT " %d", aeb->pnum); |
0798cea8 AB |
1065 | printk(KERN_CONT "\n"); |
1066 | ||
1067 | /* | |
1068 | * If too many PEBs are corrupted, we refuse attaching, | |
1069 | * otherwise, only print a warning. | |
1070 | */ | |
a4e6042f | 1071 | if (ai->corr_peb_count >= max_corr) { |
feddbb34 | 1072 | ubi_err("too many corrupted PEBs, refusing"); |
0798cea8 AB |
1073 | return -EINVAL; |
1074 | } | |
1075 | } | |
1076 | ||
a4e6042f | 1077 | if (ai->empty_peb_count + ai->maybe_bad_peb_count == peb_count) { |
0525dac9 AB |
1078 | /* |
1079 | * All PEBs are empty, or almost all - a couple PEBs look like | |
1080 | * they may be bad PEBs which were not marked as bad yet. | |
1081 | * | |
1082 | * This piece of code basically tries to distinguish between | |
1083 | * the following situations: | |
1084 | * | |
1085 | * 1. Flash is empty, but there are few bad PEBs, which are not | |
1086 | * marked as bad so far, and which were read with error. We | |
1087 | * want to go ahead and format this flash. While formatting, | |
1088 | * the faulty PEBs will probably be marked as bad. | |
1089 | * | |
1090 | * 2. Flash contains non-UBI data and we do not want to format | |
1091 | * it and destroy possibly important information. | |
1092 | */ | |
a4e6042f AB |
1093 | if (ai->maybe_bad_peb_count <= 2) { |
1094 | ai->is_empty = 1; | |
0798cea8 | 1095 | ubi_msg("empty MTD device detected"); |
0525dac9 AB |
1096 | get_random_bytes(&ubi->image_seq, |
1097 | sizeof(ubi->image_seq)); | |
0798cea8 | 1098 | } else { |
0525dac9 AB |
1099 | ubi_err("MTD device is not UBI-formatted and possibly " |
1100 | "contains non-UBI data - refusing it"); | |
0798cea8 AB |
1101 | return -EINVAL; |
1102 | } | |
0525dac9 | 1103 | |
0798cea8 AB |
1104 | } |
1105 | ||
0798cea8 AB |
1106 | return 0; |
1107 | } | |
1108 | ||
801c135c AB |
1109 | /** |
1110 | * ubi_scan - scan an MTD device. | |
1111 | * @ubi: UBI device description object | |
1112 | * | |
1113 | * This function does full scanning of an MTD device and returns complete | |
fbd0107f AB |
1114 | * information about it in form of a "struct ubi_attach_info" object. In case |
1115 | * of failure, an error code is returned. | |
801c135c | 1116 | */ |
afc15a81 | 1117 | struct ubi_attach_info *ubi_scan(struct ubi_device *ubi) |
801c135c AB |
1118 | { |
1119 | int err, pnum; | |
1120 | struct rb_node *rb1, *rb2; | |
517af48c | 1121 | struct ubi_ainf_volume *av; |
2c5ec5ce | 1122 | struct ubi_ainf_peb *aeb; |
a4e6042f | 1123 | struct ubi_attach_info *ai; |
801c135c | 1124 | |
a4e6042f AB |
1125 | ai = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL); |
1126 | if (!ai) | |
801c135c AB |
1127 | return ERR_PTR(-ENOMEM); |
1128 | ||
a4e6042f AB |
1129 | INIT_LIST_HEAD(&ai->corr); |
1130 | INIT_LIST_HEAD(&ai->free); | |
1131 | INIT_LIST_HEAD(&ai->erase); | |
1132 | INIT_LIST_HEAD(&ai->alien); | |
1133 | ai->volumes = RB_ROOT; | |
801c135c AB |
1134 | |
1135 | err = -ENOMEM; | |
1fc2e3e5 AB |
1136 | ai->aeb_slab_cache = kmem_cache_create("ubi_aeb_slab_cache", |
1137 | sizeof(struct ubi_ainf_peb), | |
1138 | 0, 0, NULL); | |
1139 | if (!ai->aeb_slab_cache) | |
a4e6042f | 1140 | goto out_ai; |
6c1e875c | 1141 | |
801c135c AB |
1142 | ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
1143 | if (!ech) | |
a4e6042f | 1144 | goto out_ai; |
801c135c | 1145 | |
33818bbb | 1146 | vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); |
801c135c AB |
1147 | if (!vidh) |
1148 | goto out_ech; | |
1149 | ||
1150 | for (pnum = 0; pnum < ubi->peb_count; pnum++) { | |
1151 | cond_resched(); | |
1152 | ||
c8566350 | 1153 | dbg_gen("process PEB %d", pnum); |
fbd0107f | 1154 | err = scan_peb(ubi, ai, pnum); |
801c135c AB |
1155 | if (err < 0) |
1156 | goto out_vidh; | |
1157 | } | |
1158 | ||
1159 | dbg_msg("scanning is finished"); | |
1160 | ||
4bc1dca4 | 1161 | /* Calculate mean erase counter */ |
a4e6042f AB |
1162 | if (ai->ec_count) |
1163 | ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count); | |
801c135c | 1164 | |
fbd0107f | 1165 | err = late_analysis(ubi, ai); |
0798cea8 AB |
1166 | if (err) |
1167 | goto out_vidh; | |
4a406856 | 1168 | |
801c135c AB |
1169 | /* |
1170 | * In case of unknown erase counter we use the mean erase counter | |
1171 | * value. | |
1172 | */ | |
517af48c AB |
1173 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { |
1174 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) | |
2c5ec5ce | 1175 | if (aeb->ec == UBI_SCAN_UNKNOWN_EC) |
a4e6042f | 1176 | aeb->ec = ai->mean_ec; |
801c135c AB |
1177 | } |
1178 | ||
a4e6042f | 1179 | list_for_each_entry(aeb, &ai->free, u.list) { |
2c5ec5ce | 1180 | if (aeb->ec == UBI_SCAN_UNKNOWN_EC) |
a4e6042f | 1181 | aeb->ec = ai->mean_ec; |
801c135c AB |
1182 | } |
1183 | ||
a4e6042f | 1184 | list_for_each_entry(aeb, &ai->corr, u.list) |
2c5ec5ce | 1185 | if (aeb->ec == UBI_SCAN_UNKNOWN_EC) |
a4e6042f | 1186 | aeb->ec = ai->mean_ec; |
801c135c | 1187 | |
a4e6042f | 1188 | list_for_each_entry(aeb, &ai->erase, u.list) |
2c5ec5ce | 1189 | if (aeb->ec == UBI_SCAN_UNKNOWN_EC) |
a4e6042f | 1190 | aeb->ec = ai->mean_ec; |
801c135c | 1191 | |
a4e6042f | 1192 | err = self_check_ai(ubi, ai); |
adbf05e3 | 1193 | if (err) |
801c135c | 1194 | goto out_vidh; |
801c135c AB |
1195 | |
1196 | ubi_free_vid_hdr(ubi, vidh); | |
1197 | kfree(ech); | |
1198 | ||
a4e6042f | 1199 | return ai; |
801c135c AB |
1200 | |
1201 | out_vidh: | |
1202 | ubi_free_vid_hdr(ubi, vidh); | |
1203 | out_ech: | |
1204 | kfree(ech); | |
a4e6042f | 1205 | out_ai: |
66a2af38 | 1206 | ubi_destroy_ai(ai); |
801c135c AB |
1207 | return ERR_PTR(err); |
1208 | } | |
1209 | ||
1210 | /** | |
fbd0107f AB |
1211 | * destroy_av - free volume attaching information. |
1212 | * @av: volume attaching information | |
a4e6042f | 1213 | * @ai: attaching information |
801c135c | 1214 | * |
fbd0107f | 1215 | * This function destroys the volume attaching information. |
801c135c | 1216 | */ |
517af48c | 1217 | static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av) |
801c135c | 1218 | { |
2c5ec5ce | 1219 | struct ubi_ainf_peb *aeb; |
517af48c | 1220 | struct rb_node *this = av->root.rb_node; |
801c135c AB |
1221 | |
1222 | while (this) { | |
1223 | if (this->rb_left) | |
1224 | this = this->rb_left; | |
1225 | else if (this->rb_right) | |
1226 | this = this->rb_right; | |
1227 | else { | |
2c5ec5ce | 1228 | aeb = rb_entry(this, struct ubi_ainf_peb, u.rb); |
801c135c AB |
1229 | this = rb_parent(this); |
1230 | if (this) { | |
2c5ec5ce | 1231 | if (this->rb_left == &aeb->u.rb) |
801c135c AB |
1232 | this->rb_left = NULL; |
1233 | else | |
1234 | this->rb_right = NULL; | |
1235 | } | |
1236 | ||
1fc2e3e5 | 1237 | kmem_cache_free(ai->aeb_slab_cache, aeb); |
801c135c AB |
1238 | } |
1239 | } | |
517af48c | 1240 | kfree(av); |
801c135c AB |
1241 | } |
1242 | ||
1243 | /** | |
66a2af38 | 1244 | * ubi_destroy_ai - destroy attaching information. |
a4e6042f | 1245 | * @ai: attaching information |
801c135c | 1246 | */ |
66a2af38 | 1247 | void ubi_destroy_ai(struct ubi_attach_info *ai) |
801c135c | 1248 | { |
2c5ec5ce | 1249 | struct ubi_ainf_peb *aeb, *aeb_tmp; |
517af48c | 1250 | struct ubi_ainf_volume *av; |
801c135c AB |
1251 | struct rb_node *rb; |
1252 | ||
a4e6042f | 1253 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) { |
2c5ec5ce | 1254 | list_del(&aeb->u.list); |
1fc2e3e5 | 1255 | kmem_cache_free(ai->aeb_slab_cache, aeb); |
801c135c | 1256 | } |
a4e6042f | 1257 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) { |
2c5ec5ce | 1258 | list_del(&aeb->u.list); |
1fc2e3e5 | 1259 | kmem_cache_free(ai->aeb_slab_cache, aeb); |
801c135c | 1260 | } |
a4e6042f | 1261 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) { |
2c5ec5ce | 1262 | list_del(&aeb->u.list); |
1fc2e3e5 | 1263 | kmem_cache_free(ai->aeb_slab_cache, aeb); |
801c135c | 1264 | } |
a4e6042f | 1265 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) { |
2c5ec5ce | 1266 | list_del(&aeb->u.list); |
1fc2e3e5 | 1267 | kmem_cache_free(ai->aeb_slab_cache, aeb); |
801c135c AB |
1268 | } |
1269 | ||
1270 | /* Destroy the volume RB-tree */ | |
a4e6042f | 1271 | rb = ai->volumes.rb_node; |
801c135c AB |
1272 | while (rb) { |
1273 | if (rb->rb_left) | |
1274 | rb = rb->rb_left; | |
1275 | else if (rb->rb_right) | |
1276 | rb = rb->rb_right; | |
1277 | else { | |
517af48c | 1278 | av = rb_entry(rb, struct ubi_ainf_volume, rb); |
801c135c AB |
1279 | |
1280 | rb = rb_parent(rb); | |
1281 | if (rb) { | |
517af48c | 1282 | if (rb->rb_left == &av->rb) |
801c135c AB |
1283 | rb->rb_left = NULL; |
1284 | else | |
1285 | rb->rb_right = NULL; | |
1286 | } | |
1287 | ||
517af48c | 1288 | destroy_av(ai, av); |
801c135c AB |
1289 | } |
1290 | } | |
1291 | ||
1fc2e3e5 AB |
1292 | if (ai->aeb_slab_cache) |
1293 | kmem_cache_destroy(ai->aeb_slab_cache); | |
a29852be | 1294 | |
a4e6042f | 1295 | kfree(ai); |
801c135c AB |
1296 | } |
1297 | ||
801c135c | 1298 | /** |
a4e6042f | 1299 | * self_check_ai - check the attaching information. |
801c135c | 1300 | * @ubi: UBI device description object |
a4e6042f | 1301 | * @ai: attaching information |
801c135c | 1302 | * |
a4e6042f | 1303 | * This function returns zero if the attaching information is all right, and a |
adbf05e3 | 1304 | * negative error code if not or if an error occurred. |
801c135c | 1305 | */ |
a4e6042f | 1306 | static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai) |
801c135c AB |
1307 | { |
1308 | int pnum, err, vols_found = 0; | |
1309 | struct rb_node *rb1, *rb2; | |
517af48c | 1310 | struct ubi_ainf_volume *av; |
2c5ec5ce | 1311 | struct ubi_ainf_peb *aeb, *last_aeb; |
801c135c AB |
1312 | uint8_t *buf; |
1313 | ||
2a734bb8 | 1314 | if (!ubi->dbg->chk_gen) |
92d124f5 AB |
1315 | return 0; |
1316 | ||
801c135c | 1317 | /* |
a4e6042f | 1318 | * At first, check that attaching information is OK. |
801c135c | 1319 | */ |
517af48c | 1320 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { |
801c135c AB |
1321 | int leb_count = 0; |
1322 | ||
1323 | cond_resched(); | |
1324 | ||
1325 | vols_found += 1; | |
1326 | ||
a4e6042f | 1327 | if (ai->is_empty) { |
801c135c | 1328 | ubi_err("bad is_empty flag"); |
517af48c | 1329 | goto bad_av; |
801c135c AB |
1330 | } |
1331 | ||
517af48c AB |
1332 | if (av->vol_id < 0 || av->highest_lnum < 0 || |
1333 | av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 || | |
1334 | av->data_pad < 0 || av->last_data_size < 0) { | |
801c135c | 1335 | ubi_err("negative values"); |
517af48c | 1336 | goto bad_av; |
801c135c AB |
1337 | } |
1338 | ||
517af48c AB |
1339 | if (av->vol_id >= UBI_MAX_VOLUMES && |
1340 | av->vol_id < UBI_INTERNAL_VOL_START) { | |
801c135c | 1341 | ubi_err("bad vol_id"); |
517af48c | 1342 | goto bad_av; |
801c135c AB |
1343 | } |
1344 | ||
517af48c | 1345 | if (av->vol_id > ai->highest_vol_id) { |
801c135c | 1346 | ubi_err("highest_vol_id is %d, but vol_id %d is there", |
517af48c | 1347 | ai->highest_vol_id, av->vol_id); |
801c135c AB |
1348 | goto out; |
1349 | } | |
1350 | ||
517af48c AB |
1351 | if (av->vol_type != UBI_DYNAMIC_VOLUME && |
1352 | av->vol_type != UBI_STATIC_VOLUME) { | |
801c135c | 1353 | ubi_err("bad vol_type"); |
517af48c | 1354 | goto bad_av; |
801c135c AB |
1355 | } |
1356 | ||
517af48c | 1357 | if (av->data_pad > ubi->leb_size / 2) { |
801c135c | 1358 | ubi_err("bad data_pad"); |
517af48c | 1359 | goto bad_av; |
801c135c AB |
1360 | } |
1361 | ||
2c5ec5ce | 1362 | last_aeb = NULL; |
517af48c | 1363 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { |
801c135c AB |
1364 | cond_resched(); |
1365 | ||
2c5ec5ce | 1366 | last_aeb = aeb; |
801c135c AB |
1367 | leb_count += 1; |
1368 | ||
2c5ec5ce | 1369 | if (aeb->pnum < 0 || aeb->ec < 0) { |
801c135c | 1370 | ubi_err("negative values"); |
2c5ec5ce | 1371 | goto bad_aeb; |
801c135c AB |
1372 | } |
1373 | ||
a4e6042f AB |
1374 | if (aeb->ec < ai->min_ec) { |
1375 | ubi_err("bad ai->min_ec (%d), %d found", | |
1376 | ai->min_ec, aeb->ec); | |
2c5ec5ce | 1377 | goto bad_aeb; |
801c135c AB |
1378 | } |
1379 | ||
a4e6042f AB |
1380 | if (aeb->ec > ai->max_ec) { |
1381 | ubi_err("bad ai->max_ec (%d), %d found", | |
1382 | ai->max_ec, aeb->ec); | |
2c5ec5ce | 1383 | goto bad_aeb; |
801c135c AB |
1384 | } |
1385 | ||
2c5ec5ce | 1386 | if (aeb->pnum >= ubi->peb_count) { |
801c135c | 1387 | ubi_err("too high PEB number %d, total PEBs %d", |
2c5ec5ce AB |
1388 | aeb->pnum, ubi->peb_count); |
1389 | goto bad_aeb; | |
801c135c AB |
1390 | } |
1391 | ||
517af48c AB |
1392 | if (av->vol_type == UBI_STATIC_VOLUME) { |
1393 | if (aeb->lnum >= av->used_ebs) { | |
801c135c | 1394 | ubi_err("bad lnum or used_ebs"); |
2c5ec5ce | 1395 | goto bad_aeb; |
801c135c AB |
1396 | } |
1397 | } else { | |
517af48c | 1398 | if (av->used_ebs != 0) { |
801c135c | 1399 | ubi_err("non-zero used_ebs"); |
2c5ec5ce | 1400 | goto bad_aeb; |
801c135c AB |
1401 | } |
1402 | } | |
1403 | ||
517af48c | 1404 | if (aeb->lnum > av->highest_lnum) { |
801c135c | 1405 | ubi_err("incorrect highest_lnum or lnum"); |
2c5ec5ce | 1406 | goto bad_aeb; |
801c135c AB |
1407 | } |
1408 | } | |
1409 | ||
517af48c | 1410 | if (av->leb_count != leb_count) { |
801c135c AB |
1411 | ubi_err("bad leb_count, %d objects in the tree", |
1412 | leb_count); | |
517af48c | 1413 | goto bad_av; |
801c135c AB |
1414 | } |
1415 | ||
2c5ec5ce | 1416 | if (!last_aeb) |
801c135c AB |
1417 | continue; |
1418 | ||
2c5ec5ce | 1419 | aeb = last_aeb; |
801c135c | 1420 | |
517af48c | 1421 | if (aeb->lnum != av->highest_lnum) { |
801c135c | 1422 | ubi_err("bad highest_lnum"); |
2c5ec5ce | 1423 | goto bad_aeb; |
801c135c AB |
1424 | } |
1425 | } | |
1426 | ||
a4e6042f AB |
1427 | if (vols_found != ai->vols_found) { |
1428 | ubi_err("bad ai->vols_found %d, should be %d", | |
1429 | ai->vols_found, vols_found); | |
801c135c AB |
1430 | goto out; |
1431 | } | |
1432 | ||
a4e6042f | 1433 | /* Check that attaching information is correct */ |
517af48c | 1434 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { |
2c5ec5ce | 1435 | last_aeb = NULL; |
517af48c | 1436 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { |
801c135c AB |
1437 | int vol_type; |
1438 | ||
1439 | cond_resched(); | |
1440 | ||
2c5ec5ce | 1441 | last_aeb = aeb; |
801c135c | 1442 | |
2c5ec5ce | 1443 | err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidh, 1); |
801c135c AB |
1444 | if (err && err != UBI_IO_BITFLIPS) { |
1445 | ubi_err("VID header is not OK (%d)", err); | |
1446 | if (err > 0) | |
1447 | err = -EIO; | |
1448 | return err; | |
1449 | } | |
1450 | ||
1451 | vol_type = vidh->vol_type == UBI_VID_DYNAMIC ? | |
1452 | UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; | |
517af48c | 1453 | if (av->vol_type != vol_type) { |
801c135c AB |
1454 | ubi_err("bad vol_type"); |
1455 | goto bad_vid_hdr; | |
1456 | } | |
1457 | ||
2c5ec5ce AB |
1458 | if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) { |
1459 | ubi_err("bad sqnum %llu", aeb->sqnum); | |
801c135c AB |
1460 | goto bad_vid_hdr; |
1461 | } | |
1462 | ||
517af48c AB |
1463 | if (av->vol_id != be32_to_cpu(vidh->vol_id)) { |
1464 | ubi_err("bad vol_id %d", av->vol_id); | |
801c135c AB |
1465 | goto bad_vid_hdr; |
1466 | } | |
1467 | ||
517af48c | 1468 | if (av->compat != vidh->compat) { |
801c135c AB |
1469 | ubi_err("bad compat %d", vidh->compat); |
1470 | goto bad_vid_hdr; | |
1471 | } | |
1472 | ||
2c5ec5ce AB |
1473 | if (aeb->lnum != be32_to_cpu(vidh->lnum)) { |
1474 | ubi_err("bad lnum %d", aeb->lnum); | |
801c135c AB |
1475 | goto bad_vid_hdr; |
1476 | } | |
1477 | ||
517af48c AB |
1478 | if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) { |
1479 | ubi_err("bad used_ebs %d", av->used_ebs); | |
801c135c AB |
1480 | goto bad_vid_hdr; |
1481 | } | |
1482 | ||
517af48c AB |
1483 | if (av->data_pad != be32_to_cpu(vidh->data_pad)) { |
1484 | ubi_err("bad data_pad %d", av->data_pad); | |
801c135c AB |
1485 | goto bad_vid_hdr; |
1486 | } | |
801c135c AB |
1487 | } |
1488 | ||
2c5ec5ce | 1489 | if (!last_aeb) |
801c135c AB |
1490 | continue; |
1491 | ||
517af48c AB |
1492 | if (av->highest_lnum != be32_to_cpu(vidh->lnum)) { |
1493 | ubi_err("bad highest_lnum %d", av->highest_lnum); | |
801c135c AB |
1494 | goto bad_vid_hdr; |
1495 | } | |
1496 | ||
517af48c AB |
1497 | if (av->last_data_size != be32_to_cpu(vidh->data_size)) { |
1498 | ubi_err("bad last_data_size %d", av->last_data_size); | |
801c135c AB |
1499 | goto bad_vid_hdr; |
1500 | } | |
1501 | } | |
1502 | ||
1503 | /* | |
1504 | * Make sure that all the physical eraseblocks are in one of the lists | |
1505 | * or trees. | |
1506 | */ | |
d9b0744d | 1507 | buf = kzalloc(ubi->peb_count, GFP_KERNEL); |
801c135c AB |
1508 | if (!buf) |
1509 | return -ENOMEM; | |
1510 | ||
801c135c AB |
1511 | for (pnum = 0; pnum < ubi->peb_count; pnum++) { |
1512 | err = ubi_io_is_bad(ubi, pnum); | |
341e1a0c AB |
1513 | if (err < 0) { |
1514 | kfree(buf); | |
801c135c | 1515 | return err; |
9c9ec147 | 1516 | } else if (err) |
d9b0744d | 1517 | buf[pnum] = 1; |
801c135c AB |
1518 | } |
1519 | ||
517af48c AB |
1520 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) |
1521 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) | |
2c5ec5ce | 1522 | buf[aeb->pnum] = 1; |
801c135c | 1523 | |
a4e6042f | 1524 | list_for_each_entry(aeb, &ai->free, u.list) |
2c5ec5ce | 1525 | buf[aeb->pnum] = 1; |
801c135c | 1526 | |
a4e6042f | 1527 | list_for_each_entry(aeb, &ai->corr, u.list) |
2c5ec5ce | 1528 | buf[aeb->pnum] = 1; |
801c135c | 1529 | |
a4e6042f | 1530 | list_for_each_entry(aeb, &ai->erase, u.list) |
2c5ec5ce | 1531 | buf[aeb->pnum] = 1; |
801c135c | 1532 | |
a4e6042f | 1533 | list_for_each_entry(aeb, &ai->alien, u.list) |
2c5ec5ce | 1534 | buf[aeb->pnum] = 1; |
801c135c AB |
1535 | |
1536 | err = 0; | |
1537 | for (pnum = 0; pnum < ubi->peb_count; pnum++) | |
d9b0744d | 1538 | if (!buf[pnum]) { |
801c135c AB |
1539 | ubi_err("PEB %d is not referred", pnum); |
1540 | err = 1; | |
1541 | } | |
1542 | ||
1543 | kfree(buf); | |
1544 | if (err) | |
1545 | goto out; | |
1546 | return 0; | |
1547 | ||
2c5ec5ce | 1548 | bad_aeb: |
a4e6042f | 1549 | ubi_err("bad attaching information about LEB %d", aeb->lnum); |
2c5ec5ce | 1550 | ubi_dump_aeb(aeb, 0); |
517af48c | 1551 | ubi_dump_av(av); |
801c135c AB |
1552 | goto out; |
1553 | ||
517af48c AB |
1554 | bad_av: |
1555 | ubi_err("bad attaching information about volume %d", av->vol_id); | |
1556 | ubi_dump_av(av); | |
801c135c AB |
1557 | goto out; |
1558 | ||
1559 | bad_vid_hdr: | |
517af48c AB |
1560 | ubi_err("bad attaching information about volume %d", av->vol_id); |
1561 | ubi_dump_av(av); | |
a904e3f1 | 1562 | ubi_dump_vid_hdr(vidh); |
801c135c AB |
1563 | |
1564 | out: | |
25886a36 | 1565 | dump_stack(); |
adbf05e3 | 1566 | return -EINVAL; |
801c135c | 1567 | } |