]>
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 | /* | |
85c6e6e2 | 22 | * UBI scanning sub-system. |
801c135c | 23 | * |
85c6e6e2 | 24 | * This sub-system is responsible for scanning the flash media, checking UBI |
801c135c AB |
25 | * headers and providing complete information about the UBI flash image. |
26 | * | |
a4e6042f AB |
27 | * The attaching information is represented by a &struct ubi_attach_info' |
28 | * object. Information about found volumes is represented by | |
29 | * &struct ubi_ainf_volume objects which are kept in volume RB-tree with root | |
30 | * at the @volumes field. The RB-tree is indexed by the volume ID. | |
801c135c | 31 | * |
227423d2 | 32 | * Scanned logical eraseblocks are represented by &struct ubi_ainf_peb objects. |
801c135c | 33 | * These objects are kept in per-volume RB-trees with the root at the |
cb28a932 | 34 | * corresponding &struct ubi_ainf_volume object. To put it differently, we keep |
801c135c AB |
35 | * an RB-tree of per-volume objects and each of these objects is the root of |
36 | * RB-tree of per-eraseblock objects. | |
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 | |
54 | * error messages. The idea is that we do not lose important data in these case | |
55 | * - we may lose only the data which was being written to the media just before | |
56 | * the power cut happened, and the upper layers (e.g., UBIFS) are supposed to | |
57 | * handle such data losses (e.g., by using the FS journal). | |
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 | |
fef2deb3 AB |
64 | * scanning, such PEBs are put to the @corr list and UBI preserves them. |
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 | |
fef2deb3 AB |
70 | * corruptions and UBI's strategy is as follows. UBI assumes corruption type 2 |
71 | * if the VID header is corrupted and the data area does not contain all 0xFFs, | |
72 | * and there were no bit-flips or integrity errors while reading the data area. | |
73 | * Otherwise UBI assumes corruption type 1. So the decision criteria are as | |
74 | * follows. | |
75 | * o If the data area contains only 0xFFs, there is no data, and it is safe | |
76 | * to just erase this PEB - this is corruption type 1. | |
77 | * o If the data area has bit-flips or data integrity errors (ECC errors on | |
45aafd32 | 78 | * NAND), it is probably a PEB which was being erased when power cut |
fef2deb3 AB |
79 | * happened, so this is corruption type 1. However, this is just a guess, |
80 | * which might be wrong. | |
81 | * o Otherwise this it corruption type 2. | |
801c135c AB |
82 | */ |
83 | ||
84 | #include <linux/err.h> | |
5a0e3ad6 | 85 | #include <linux/slab.h> |
801c135c | 86 | #include <linux/crc32.h> |
3013ee31 | 87 | #include <linux/math64.h> |
095751a6 | 88 | #include <linux/random.h> |
801c135c AB |
89 | #include "ubi.h" |
90 | ||
a4e6042f | 91 | static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai); |
801c135c AB |
92 | |
93 | /* Temporary variables used during scanning */ | |
94 | static struct ubi_ec_hdr *ech; | |
95 | static struct ubi_vid_hdr *vidh; | |
96 | ||
941dfb07 | 97 | /** |
78d87c95 | 98 | * add_to_list - add physical eraseblock to a list. |
a4e6042f | 99 | * @ai: attaching information |
78d87c95 AB |
100 | * @pnum: physical eraseblock number to add |
101 | * @ec: erase counter of the physical eraseblock | |
0525dac9 | 102 | * @to_head: if not zero, add to the head of the list |
78d87c95 AB |
103 | * @list: the list to add to |
104 | * | |
3fb34124 | 105 | * This function adds physical eraseblock @pnum to free, erase, or alien lists. |
0525dac9 AB |
106 | * If @to_head is not zero, PEB will be added to the head of the list, which |
107 | * basically means it will be processed first later. E.g., we add corrupted | |
108 | * PEBs (corrupted due to power cuts) to the head of the erase list to make | |
109 | * sure we erase them first and get rid of corruptions ASAP. This function | |
110 | * returns zero in case of success and a negative error code in case of | |
3fb34124 | 111 | * failure. |
78d87c95 | 112 | */ |
a4e6042f | 113 | static int add_to_list(struct ubi_attach_info *ai, int pnum, int ec, |
afc15a81 | 114 | int to_head, struct list_head *list) |
801c135c | 115 | { |
2c5ec5ce | 116 | struct ubi_ainf_peb *aeb; |
801c135c | 117 | |
a4e6042f | 118 | if (list == &ai->free) { |
801c135c | 119 | dbg_bld("add to free: PEB %d, EC %d", pnum, ec); |
a4e6042f | 120 | } else if (list == &ai->erase) { |
801c135c | 121 | dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); |
a4e6042f | 122 | } else if (list == &ai->alien) { |
801c135c | 123 | dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); |
a4e6042f | 124 | ai->alien_peb_count += 1; |
33789fb9 | 125 | } else |
801c135c AB |
126 | BUG(); |
127 | ||
a4e6042f | 128 | aeb = kmem_cache_alloc(ai->scan_leb_slab, GFP_KERNEL); |
2c5ec5ce | 129 | if (!aeb) |
801c135c AB |
130 | return -ENOMEM; |
131 | ||
2c5ec5ce AB |
132 | aeb->pnum = pnum; |
133 | aeb->ec = ec; | |
0525dac9 | 134 | if (to_head) |
2c5ec5ce | 135 | list_add(&aeb->u.list, list); |
0525dac9 | 136 | else |
2c5ec5ce | 137 | list_add_tail(&aeb->u.list, list); |
801c135c AB |
138 | return 0; |
139 | } | |
140 | ||
3fb34124 AB |
141 | /** |
142 | * add_corrupted - add a corrupted physical eraseblock. | |
a4e6042f | 143 | * @ai: attaching information |
3fb34124 AB |
144 | * @pnum: physical eraseblock number to add |
145 | * @ec: erase counter of the physical eraseblock | |
146 | * | |
147 | * This function adds corrupted physical eraseblock @pnum to the 'corr' list. | |
feeba4b8 AB |
148 | * The corruption was presumably not caused by a power cut. Returns zero in |
149 | * case of success and a negative error code in case of failure. | |
3fb34124 | 150 | */ |
a4e6042f | 151 | static int add_corrupted(struct ubi_attach_info *ai, int pnum, int ec) |
3fb34124 | 152 | { |
2c5ec5ce | 153 | struct ubi_ainf_peb *aeb; |
3fb34124 AB |
154 | |
155 | dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec); | |
156 | ||
a4e6042f | 157 | aeb = kmem_cache_alloc(ai->scan_leb_slab, GFP_KERNEL); |
2c5ec5ce | 158 | if (!aeb) |
3fb34124 AB |
159 | return -ENOMEM; |
160 | ||
a4e6042f | 161 | ai->corr_peb_count += 1; |
2c5ec5ce AB |
162 | aeb->pnum = pnum; |
163 | aeb->ec = ec; | |
a4e6042f | 164 | list_add(&aeb->u.list, &ai->corr); |
3fb34124 AB |
165 | return 0; |
166 | } | |
167 | ||
801c135c | 168 | /** |
ebaaf1af | 169 | * validate_vid_hdr - check volume identifier header. |
801c135c | 170 | * @vid_hdr: the volume identifier header to check |
517af48c | 171 | * @av: information about the volume this logical eraseblock belongs to |
801c135c AB |
172 | * @pnum: physical eraseblock number the VID header came from |
173 | * | |
174 | * This function checks that data stored in @vid_hdr is consistent. Returns | |
175 | * non-zero if an inconsistency was found and zero if not. | |
176 | * | |
177 | * Note, UBI does sanity check of everything it reads from the flash media. | |
85c6e6e2 | 178 | * Most of the checks are done in the I/O sub-system. Here we check that the |
801c135c AB |
179 | * information in the VID header is consistent to the information in other VID |
180 | * headers of the same volume. | |
181 | */ | |
182 | static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr, | |
517af48c | 183 | const struct ubi_ainf_volume *av, int pnum) |
801c135c AB |
184 | { |
185 | int vol_type = vid_hdr->vol_type; | |
3261ebd7 CH |
186 | int vol_id = be32_to_cpu(vid_hdr->vol_id); |
187 | int used_ebs = be32_to_cpu(vid_hdr->used_ebs); | |
188 | int data_pad = be32_to_cpu(vid_hdr->data_pad); | |
801c135c | 189 | |
517af48c AB |
190 | if (av->leb_count != 0) { |
191 | int av_vol_type; | |
801c135c AB |
192 | |
193 | /* | |
194 | * This is not the first logical eraseblock belonging to this | |
195 | * volume. Ensure that the data in its VID header is consistent | |
196 | * to the data in previous logical eraseblock headers. | |
197 | */ | |
198 | ||
517af48c | 199 | if (vol_id != av->vol_id) { |
e2986827 | 200 | ubi_err("inconsistent vol_id"); |
801c135c AB |
201 | goto bad; |
202 | } | |
203 | ||
517af48c AB |
204 | if (av->vol_type == UBI_STATIC_VOLUME) |
205 | av_vol_type = UBI_VID_STATIC; | |
801c135c | 206 | else |
517af48c | 207 | av_vol_type = UBI_VID_DYNAMIC; |
801c135c | 208 | |
517af48c | 209 | if (vol_type != av_vol_type) { |
e2986827 | 210 | ubi_err("inconsistent vol_type"); |
801c135c AB |
211 | goto bad; |
212 | } | |
213 | ||
517af48c | 214 | if (used_ebs != av->used_ebs) { |
e2986827 | 215 | ubi_err("inconsistent used_ebs"); |
801c135c AB |
216 | goto bad; |
217 | } | |
218 | ||
517af48c | 219 | if (data_pad != av->data_pad) { |
e2986827 | 220 | ubi_err("inconsistent data_pad"); |
801c135c AB |
221 | goto bad; |
222 | } | |
223 | } | |
224 | ||
225 | return 0; | |
226 | ||
227 | bad: | |
228 | ubi_err("inconsistent VID header at PEB %d", pnum); | |
a904e3f1 | 229 | ubi_dump_vid_hdr(vid_hdr); |
517af48c | 230 | ubi_dump_av(av); |
801c135c AB |
231 | return -EINVAL; |
232 | } | |
233 | ||
234 | /** | |
a4e6042f AB |
235 | * add_volume - add volume to the attaching information. |
236 | * @ai: attaching information | |
801c135c AB |
237 | * @vol_id: ID of the volume to add |
238 | * @pnum: physical eraseblock number | |
239 | * @vid_hdr: volume identifier header | |
240 | * | |
241 | * If the volume corresponding to the @vid_hdr logical eraseblock is already | |
a4e6042f AB |
242 | * present in the attaching information, this function does nothing. Otherwise |
243 | * it adds corresponding volume to the attaching information. Returns a pointer | |
801c135c AB |
244 | * to the scanning volume object in case of success and a negative error code |
245 | * in case of failure. | |
246 | */ | |
a4e6042f | 247 | static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *ai, |
afc15a81 | 248 | int vol_id, int pnum, |
801c135c AB |
249 | const struct ubi_vid_hdr *vid_hdr) |
250 | { | |
517af48c | 251 | struct ubi_ainf_volume *av; |
a4e6042f | 252 | struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; |
801c135c | 253 | |
3261ebd7 | 254 | ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id)); |
801c135c AB |
255 | |
256 | /* Walk the volume RB-tree to look if this volume is already present */ | |
257 | while (*p) { | |
258 | parent = *p; | |
517af48c | 259 | av = rb_entry(parent, struct ubi_ainf_volume, rb); |
801c135c | 260 | |
517af48c AB |
261 | if (vol_id == av->vol_id) |
262 | return av; | |
801c135c | 263 | |
517af48c | 264 | if (vol_id > av->vol_id) |
801c135c AB |
265 | p = &(*p)->rb_left; |
266 | else | |
267 | p = &(*p)->rb_right; | |
268 | } | |
269 | ||
270 | /* The volume is absent - add it */ | |
517af48c AB |
271 | av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL); |
272 | if (!av) | |
801c135c AB |
273 | return ERR_PTR(-ENOMEM); |
274 | ||
517af48c AB |
275 | av->highest_lnum = av->leb_count = 0; |
276 | av->vol_id = vol_id; | |
277 | av->root = RB_ROOT; | |
278 | av->used_ebs = be32_to_cpu(vid_hdr->used_ebs); | |
279 | av->data_pad = be32_to_cpu(vid_hdr->data_pad); | |
280 | av->compat = vid_hdr->compat; | |
281 | av->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME | |
801c135c | 282 | : UBI_STATIC_VOLUME; |
a4e6042f AB |
283 | if (vol_id > ai->highest_vol_id) |
284 | ai->highest_vol_id = vol_id; | |
801c135c | 285 | |
517af48c AB |
286 | rb_link_node(&av->rb, parent, p); |
287 | rb_insert_color(&av->rb, &ai->volumes); | |
a4e6042f | 288 | ai->vols_found += 1; |
801c135c | 289 | dbg_bld("added volume %d", vol_id); |
517af48c | 290 | return av; |
801c135c AB |
291 | } |
292 | ||
293 | /** | |
294 | * compare_lebs - find out which logical eraseblock is newer. | |
295 | * @ubi: UBI device description object | |
2c5ec5ce | 296 | * @aeb: first logical eraseblock to compare |
801c135c AB |
297 | * @pnum: physical eraseblock number of the second logical eraseblock to |
298 | * compare | |
299 | * @vid_hdr: volume identifier header of the second logical eraseblock | |
300 | * | |
301 | * This function compares 2 copies of a LEB and informs which one is newer. In | |
302 | * case of success this function returns a positive value, in case of failure, a | |
303 | * negative error code is returned. The success return codes use the following | |
304 | * bits: | |
2c5ec5ce | 305 | * o bit 0 is cleared: the first PEB (described by @aeb) is newer than the |
801c135c AB |
306 | * second PEB (described by @pnum and @vid_hdr); |
307 | * o bit 0 is set: the second PEB is newer; | |
308 | * o bit 1 is cleared: no bit-flips were detected in the newer LEB; | |
309 | * o bit 1 is set: bit-flips were detected in the newer LEB; | |
310 | * o bit 2 is cleared: the older LEB is not corrupted; | |
311 | * o bit 2 is set: the older LEB is corrupted. | |
312 | */ | |
2c5ec5ce | 313 | static int compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb, |
e88d6e10 | 314 | int pnum, const struct ubi_vid_hdr *vid_hdr) |
801c135c AB |
315 | { |
316 | void *buf; | |
317 | int len, err, second_is_newer, bitflips = 0, corrupted = 0; | |
318 | uint32_t data_crc, crc; | |
8bc22961 | 319 | struct ubi_vid_hdr *vh = NULL; |
3261ebd7 | 320 | unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); |
801c135c | 321 | |
2c5ec5ce | 322 | if (sqnum2 == aeb->sqnum) { |
801c135c | 323 | /* |
9869cd80 AB |
324 | * This must be a really ancient UBI image which has been |
325 | * created before sequence numbers support has been added. At | |
326 | * that times we used 32-bit LEB versions stored in logical | |
327 | * eraseblocks. That was before UBI got into mainline. We do not | |
0525dac9 AB |
328 | * support these images anymore. Well, those images still work, |
329 | * but only if no unclean reboots happened. | |
801c135c | 330 | */ |
9869cd80 AB |
331 | ubi_err("unsupported on-flash UBI format\n"); |
332 | return -EINVAL; | |
333 | } | |
64203195 | 334 | |
9869cd80 | 335 | /* Obviously the LEB with lower sequence counter is older */ |
2c5ec5ce | 336 | second_is_newer = (sqnum2 > aeb->sqnum); |
801c135c AB |
337 | |
338 | /* | |
339 | * Now we know which copy is newer. If the copy flag of the PEB with | |
340 | * newer version is not set, then we just return, otherwise we have to | |
341 | * check data CRC. For the second PEB we already have the VID header, | |
342 | * for the first one - we'll need to re-read it from flash. | |
343 | * | |
9869cd80 | 344 | * Note: this may be optimized so that we wouldn't read twice. |
801c135c AB |
345 | */ |
346 | ||
347 | if (second_is_newer) { | |
348 | if (!vid_hdr->copy_flag) { | |
349 | /* It is not a copy, so it is newer */ | |
350 | dbg_bld("second PEB %d is newer, copy_flag is unset", | |
351 | pnum); | |
352 | return 1; | |
353 | } | |
354 | } else { | |
2c5ec5ce | 355 | if (!aeb->copy_flag) { |
fb22b59b AB |
356 | /* It is not a copy, so it is newer */ |
357 | dbg_bld("first PEB %d is newer, copy_flag is unset", | |
358 | pnum); | |
359 | return bitflips << 1; | |
360 | } | |
801c135c | 361 | |
33818bbb | 362 | vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); |
8bc22961 | 363 | if (!vh) |
801c135c AB |
364 | return -ENOMEM; |
365 | ||
2c5ec5ce | 366 | pnum = aeb->pnum; |
8bc22961 | 367 | err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); |
801c135c AB |
368 | if (err) { |
369 | if (err == UBI_IO_BITFLIPS) | |
370 | bitflips = 1; | |
371 | else { | |
e2986827 | 372 | ubi_err("VID of PEB %d header is bad, but it " |
0525dac9 | 373 | "was OK earlier, err %d", pnum, err); |
801c135c AB |
374 | if (err > 0) |
375 | err = -EIO; | |
376 | ||
377 | goto out_free_vidh; | |
378 | } | |
379 | } | |
380 | ||
8bc22961 | 381 | vid_hdr = vh; |
801c135c AB |
382 | } |
383 | ||
384 | /* Read the data of the copy and check the CRC */ | |
385 | ||
3261ebd7 | 386 | len = be32_to_cpu(vid_hdr->data_size); |
92ad8f37 | 387 | buf = vmalloc(len); |
801c135c AB |
388 | if (!buf) { |
389 | err = -ENOMEM; | |
390 | goto out_free_vidh; | |
391 | } | |
392 | ||
393 | err = ubi_io_read_data(ubi, buf, pnum, 0, len); | |
d57f4054 | 394 | if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) |
801c135c AB |
395 | goto out_free_buf; |
396 | ||
3261ebd7 | 397 | data_crc = be32_to_cpu(vid_hdr->data_crc); |
801c135c AB |
398 | crc = crc32(UBI_CRC32_INIT, buf, len); |
399 | if (crc != data_crc) { | |
400 | dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x", | |
401 | pnum, crc, data_crc); | |
402 | corrupted = 1; | |
403 | bitflips = 0; | |
404 | second_is_newer = !second_is_newer; | |
405 | } else { | |
406 | dbg_bld("PEB %d CRC is OK", pnum); | |
407 | bitflips = !!err; | |
408 | } | |
409 | ||
92ad8f37 | 410 | vfree(buf); |
8bc22961 | 411 | ubi_free_vid_hdr(ubi, vh); |
801c135c AB |
412 | |
413 | if (second_is_newer) | |
414 | dbg_bld("second PEB %d is newer, copy_flag is set", pnum); | |
415 | else | |
416 | dbg_bld("first PEB %d is newer, copy_flag is set", pnum); | |
417 | ||
418 | return second_is_newer | (bitflips << 1) | (corrupted << 2); | |
419 | ||
420 | out_free_buf: | |
92ad8f37 | 421 | vfree(buf); |
801c135c | 422 | out_free_vidh: |
8bc22961 | 423 | ubi_free_vid_hdr(ubi, vh); |
801c135c AB |
424 | return err; |
425 | } | |
426 | ||
427 | /** | |
3561188a | 428 | * ubi_add_to_av - add physical eraseblock to the attaching information. |
801c135c | 429 | * @ubi: UBI device description object |
a4e6042f | 430 | * @ai: attaching information |
801c135c AB |
431 | * @pnum: the physical eraseblock number |
432 | * @ec: erase counter | |
433 | * @vid_hdr: the volume identifier header | |
434 | * @bitflips: if bit-flips were detected when this physical eraseblock was read | |
435 | * | |
79b510c0 AB |
436 | * This function adds information about a used physical eraseblock to the |
437 | * 'used' tree of the corresponding volume. The function is rather complex | |
438 | * because it has to handle cases when this is not the first physical | |
439 | * eraseblock belonging to the same logical eraseblock, and the newer one has | |
440 | * to be picked, while the older one has to be dropped. This function returns | |
441 | * zero in case of success and a negative error code in case of failure. | |
801c135c | 442 | */ |
3561188a AB |
443 | int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum, |
444 | int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips) | |
801c135c AB |
445 | { |
446 | int err, vol_id, lnum; | |
801c135c | 447 | unsigned long long sqnum; |
517af48c | 448 | struct ubi_ainf_volume *av; |
2c5ec5ce | 449 | struct ubi_ainf_peb *aeb; |
801c135c AB |
450 | struct rb_node **p, *parent = NULL; |
451 | ||
3261ebd7 CH |
452 | vol_id = be32_to_cpu(vid_hdr->vol_id); |
453 | lnum = be32_to_cpu(vid_hdr->lnum); | |
454 | sqnum = be64_to_cpu(vid_hdr->sqnum); | |
801c135c | 455 | |
9869cd80 AB |
456 | dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d", |
457 | pnum, vol_id, lnum, ec, sqnum, bitflips); | |
801c135c | 458 | |
517af48c AB |
459 | av = add_volume(ai, vol_id, pnum, vid_hdr); |
460 | if (IS_ERR(av)) | |
461 | return PTR_ERR(av); | |
801c135c | 462 | |
a4e6042f AB |
463 | if (ai->max_sqnum < sqnum) |
464 | ai->max_sqnum = sqnum; | |
76eafe47 | 465 | |
801c135c AB |
466 | /* |
467 | * Walk the RB-tree of logical eraseblocks of volume @vol_id to look | |
468 | * if this is the first instance of this logical eraseblock or not. | |
469 | */ | |
517af48c | 470 | p = &av->root.rb_node; |
801c135c AB |
471 | while (*p) { |
472 | int cmp_res; | |
473 | ||
474 | parent = *p; | |
2c5ec5ce AB |
475 | aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb); |
476 | if (lnum != aeb->lnum) { | |
477 | if (lnum < aeb->lnum) | |
801c135c AB |
478 | p = &(*p)->rb_left; |
479 | else | |
480 | p = &(*p)->rb_right; | |
481 | continue; | |
482 | } | |
483 | ||
484 | /* | |
485 | * There is already a physical eraseblock describing the same | |
486 | * logical eraseblock present. | |
487 | */ | |
488 | ||
2c5ec5ce AB |
489 | dbg_bld("this LEB already exists: PEB %d, sqnum %llu, EC %d", |
490 | aeb->pnum, aeb->sqnum, aeb->ec); | |
801c135c AB |
491 | |
492 | /* | |
493 | * Make sure that the logical eraseblocks have different | |
494 | * sequence numbers. Otherwise the image is bad. | |
495 | * | |
9869cd80 AB |
496 | * However, if the sequence number is zero, we assume it must |
497 | * be an ancient UBI image from the era when UBI did not have | |
498 | * sequence numbers. We still can attach these images, unless | |
499 | * there is a need to distinguish between old and new | |
500 | * eraseblocks, in which case we'll refuse the image in | |
501 | * 'compare_lebs()'. In other words, we attach old clean | |
502 | * images, but refuse attaching old images with duplicated | |
503 | * logical eraseblocks because there was an unclean reboot. | |
801c135c | 504 | */ |
2c5ec5ce | 505 | if (aeb->sqnum == sqnum && sqnum != 0) { |
801c135c AB |
506 | ubi_err("two LEBs with same sequence number %llu", |
507 | sqnum); | |
2c5ec5ce | 508 | ubi_dump_aeb(aeb, 0); |
a904e3f1 | 509 | ubi_dump_vid_hdr(vid_hdr); |
801c135c AB |
510 | return -EINVAL; |
511 | } | |
512 | ||
513 | /* | |
514 | * Now we have to drop the older one and preserve the newer | |
515 | * one. | |
516 | */ | |
2c5ec5ce | 517 | cmp_res = compare_lebs(ubi, aeb, pnum, vid_hdr); |
801c135c AB |
518 | if (cmp_res < 0) |
519 | return cmp_res; | |
520 | ||
521 | if (cmp_res & 1) { | |
522 | /* | |
3f502622 | 523 | * This logical eraseblock is newer than the one |
801c135c AB |
524 | * found earlier. |
525 | */ | |
517af48c | 526 | err = validate_vid_hdr(vid_hdr, av, pnum); |
801c135c AB |
527 | if (err) |
528 | return err; | |
529 | ||
a4e6042f AB |
530 | err = add_to_list(ai, aeb->pnum, aeb->ec, cmp_res & 4, |
531 | &ai->erase); | |
801c135c AB |
532 | if (err) |
533 | return err; | |
534 | ||
2c5ec5ce AB |
535 | aeb->ec = ec; |
536 | aeb->pnum = pnum; | |
537 | aeb->scrub = ((cmp_res & 2) || bitflips); | |
538 | aeb->copy_flag = vid_hdr->copy_flag; | |
539 | aeb->sqnum = sqnum; | |
801c135c | 540 | |
517af48c AB |
541 | if (av->highest_lnum == lnum) |
542 | av->last_data_size = | |
3261ebd7 | 543 | be32_to_cpu(vid_hdr->data_size); |
801c135c AB |
544 | |
545 | return 0; | |
546 | } else { | |
547 | /* | |
025dfdaf | 548 | * This logical eraseblock is older than the one found |
801c135c AB |
549 | * previously. |
550 | */ | |
a4e6042f AB |
551 | return add_to_list(ai, pnum, ec, cmp_res & 4, |
552 | &ai->erase); | |
801c135c AB |
553 | } |
554 | } | |
555 | ||
556 | /* | |
557 | * We've met this logical eraseblock for the first time, add it to the | |
a4e6042f | 558 | * attaching information. |
801c135c AB |
559 | */ |
560 | ||
517af48c | 561 | err = validate_vid_hdr(vid_hdr, av, pnum); |
801c135c AB |
562 | if (err) |
563 | return err; | |
564 | ||
a4e6042f | 565 | aeb = kmem_cache_alloc(ai->scan_leb_slab, GFP_KERNEL); |
2c5ec5ce | 566 | if (!aeb) |
801c135c AB |
567 | return -ENOMEM; |
568 | ||
2c5ec5ce AB |
569 | aeb->ec = ec; |
570 | aeb->pnum = pnum; | |
571 | aeb->lnum = lnum; | |
572 | aeb->scrub = bitflips; | |
573 | aeb->copy_flag = vid_hdr->copy_flag; | |
574 | aeb->sqnum = sqnum; | |
801c135c | 575 | |
517af48c AB |
576 | if (av->highest_lnum <= lnum) { |
577 | av->highest_lnum = lnum; | |
578 | av->last_data_size = be32_to_cpu(vid_hdr->data_size); | |
801c135c AB |
579 | } |
580 | ||
517af48c | 581 | av->leb_count += 1; |
2c5ec5ce | 582 | rb_link_node(&aeb->u.rb, parent, p); |
517af48c | 583 | rb_insert_color(&aeb->u.rb, &av->root); |
801c135c AB |
584 | return 0; |
585 | } | |
586 | ||
587 | /** | |
dcd85fdd | 588 | * ubi_find_av - find volume in the attaching information. |
a4e6042f | 589 | * @ai: attaching information |
801c135c AB |
590 | * @vol_id: the requested volume ID |
591 | * | |
592 | * This function returns a pointer to the volume description or %NULL if there | |
a4e6042f | 593 | * are no data about this volume in the attaching information. |
801c135c | 594 | */ |
dcd85fdd AB |
595 | struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai, |
596 | int vol_id) | |
801c135c | 597 | { |
517af48c | 598 | struct ubi_ainf_volume *av; |
a4e6042f | 599 | struct rb_node *p = ai->volumes.rb_node; |
801c135c AB |
600 | |
601 | while (p) { | |
517af48c | 602 | av = rb_entry(p, struct ubi_ainf_volume, rb); |
801c135c | 603 | |
517af48c AB |
604 | if (vol_id == av->vol_id) |
605 | return av; | |
801c135c | 606 | |
517af48c | 607 | if (vol_id > av->vol_id) |
801c135c AB |
608 | p = p->rb_left; |
609 | else | |
610 | p = p->rb_right; | |
611 | } | |
612 | ||
613 | return NULL; | |
614 | } | |
615 | ||
801c135c | 616 | /** |
d717dc2f | 617 | * ubi_remove_av - delete attaching information about a volume. |
a4e6042f | 618 | * @ai: attaching information |
517af48c | 619 | * @av: the volume attaching information to delete |
801c135c | 620 | */ |
d717dc2f | 621 | void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av) |
801c135c AB |
622 | { |
623 | struct rb_node *rb; | |
2c5ec5ce | 624 | struct ubi_ainf_peb *aeb; |
801c135c | 625 | |
517af48c | 626 | dbg_bld("remove attaching information about volume %d", av->vol_id); |
801c135c | 627 | |
517af48c | 628 | while ((rb = rb_first(&av->root))) { |
2c5ec5ce | 629 | aeb = rb_entry(rb, struct ubi_ainf_peb, u.rb); |
517af48c | 630 | rb_erase(&aeb->u.rb, &av->root); |
a4e6042f | 631 | list_add_tail(&aeb->u.list, &ai->erase); |
801c135c AB |
632 | } |
633 | ||
517af48c AB |
634 | rb_erase(&av->rb, &ai->volumes); |
635 | kfree(av); | |
a4e6042f | 636 | ai->vols_found -= 1; |
801c135c AB |
637 | } |
638 | ||
639 | /** | |
13d33dad | 640 | * early_erase_peb - erase a physical eraseblock. |
801c135c | 641 | * @ubi: UBI device description object |
a4e6042f | 642 | * @ai: attaching information |
801c135c AB |
643 | * @pnum: physical eraseblock number to erase; |
644 | * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown) | |
645 | * | |
646 | * This function erases physical eraseblock 'pnum', and writes the erase | |
647 | * counter header to it. This function should only be used on UBI device | |
85c6e6e2 AB |
648 | * initialization stages, when the EBA sub-system had not been yet initialized. |
649 | * This function returns zero in case of success and a negative error code in | |
650 | * case of failure. | |
801c135c | 651 | */ |
13d33dad AB |
652 | static int early_erase_peb(struct ubi_device *ubi, |
653 | const struct ubi_attach_info *ai, int pnum, int ec) | |
801c135c AB |
654 | { |
655 | int err; | |
656 | struct ubi_ec_hdr *ec_hdr; | |
657 | ||
801c135c AB |
658 | if ((long long)ec >= UBI_MAX_ERASECOUNTER) { |
659 | /* | |
660 | * Erase counter overflow. Upgrade UBI and use 64-bit | |
661 | * erase counters internally. | |
662 | */ | |
663 | ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec); | |
664 | return -EINVAL; | |
665 | } | |
666 | ||
dcec4c3b FM |
667 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
668 | if (!ec_hdr) | |
669 | return -ENOMEM; | |
670 | ||
3261ebd7 | 671 | ec_hdr->ec = cpu_to_be64(ec); |
801c135c AB |
672 | |
673 | err = ubi_io_sync_erase(ubi, pnum, 0); | |
674 | if (err < 0) | |
675 | goto out_free; | |
676 | ||
677 | err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); | |
678 | ||
679 | out_free: | |
680 | kfree(ec_hdr); | |
681 | return err; | |
682 | } | |
683 | ||
684 | /** | |
c87fbd7d | 685 | * ubi_early_get_peb - get a free physical eraseblock. |
801c135c | 686 | * @ubi: UBI device description object |
a4e6042f | 687 | * @ai: attaching information |
801c135c AB |
688 | * |
689 | * This function returns a free physical eraseblock. It is supposed to be | |
85c6e6e2 AB |
690 | * called on the UBI initialization stages when the wear-leveling sub-system is |
691 | * not initialized yet. This function picks a physical eraseblocks from one of | |
692 | * the lists, writes the EC header if it is needed, and removes it from the | |
693 | * list. | |
801c135c AB |
694 | * |
695 | * This function returns scanning physical eraseblock information in case of | |
696 | * success and an error code in case of failure. | |
697 | */ | |
c87fbd7d AB |
698 | struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi, |
699 | struct ubi_attach_info *ai) | |
801c135c | 700 | { |
5fc01ab6 | 701 | int err = 0; |
2c5ec5ce | 702 | struct ubi_ainf_peb *aeb, *tmp_aeb; |
801c135c | 703 | |
a4e6042f AB |
704 | if (!list_empty(&ai->free)) { |
705 | aeb = list_entry(ai->free.next, struct ubi_ainf_peb, u.list); | |
2c5ec5ce AB |
706 | list_del(&aeb->u.list); |
707 | dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec); | |
708 | return aeb; | |
801c135c AB |
709 | } |
710 | ||
5fc01ab6 AB |
711 | /* |
712 | * We try to erase the first physical eraseblock from the erase list | |
713 | * and pick it if we succeed, or try to erase the next one if not. And | |
714 | * so forth. We don't want to take care about bad eraseblocks here - | |
715 | * they'll be handled later. | |
716 | */ | |
a4e6042f | 717 | list_for_each_entry_safe(aeb, tmp_aeb, &ai->erase, u.list) { |
2c5ec5ce | 718 | if (aeb->ec == UBI_SCAN_UNKNOWN_EC) |
a4e6042f | 719 | aeb->ec = ai->mean_ec; |
801c135c | 720 | |
13d33dad | 721 | err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1); |
5fc01ab6 AB |
722 | if (err) |
723 | continue; | |
801c135c | 724 | |
2c5ec5ce AB |
725 | aeb->ec += 1; |
726 | list_del(&aeb->u.list); | |
727 | dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec); | |
728 | return aeb; | |
801c135c AB |
729 | } |
730 | ||
5fc01ab6 | 731 | ubi_err("no free eraseblocks"); |
801c135c AB |
732 | return ERR_PTR(-ENOSPC); |
733 | } | |
734 | ||
feeba4b8 | 735 | /** |
45aafd32 | 736 | * check_corruption - check the data area of PEB. |
feeba4b8 AB |
737 | * @ubi: UBI device description object |
738 | * @vid_hrd: the (corrupted) VID header of this PEB | |
739 | * @pnum: the physical eraseblock number to check | |
740 | * | |
741 | * This is a helper function which is used to distinguish between VID header | |
742 | * corruptions caused by power cuts and other reasons. If the PEB contains only | |
45aafd32 | 743 | * 0xFF bytes in the data area, the VID header is most probably corrupted |
feeba4b8 | 744 | * because of a power cut (%0 is returned in this case). Otherwise, it was |
45aafd32 AB |
745 | * probably corrupted for some other reasons (%1 is returned in this case). A |
746 | * negative error code is returned if a read error occurred. | |
feeba4b8 AB |
747 | * |
748 | * If the corruption reason was a power cut, UBI can safely erase this PEB. | |
749 | * Otherwise, it should preserve it to avoid possibly destroying important | |
750 | * information. | |
751 | */ | |
45aafd32 AB |
752 | static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr, |
753 | int pnum) | |
feeba4b8 AB |
754 | { |
755 | int err; | |
756 | ||
757 | mutex_lock(&ubi->buf_mutex); | |
0ca39d74 | 758 | memset(ubi->peb_buf, 0x00, ubi->leb_size); |
feeba4b8 | 759 | |
0ca39d74 | 760 | err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start, |
feeba4b8 | 761 | ubi->leb_size); |
d57f4054 | 762 | if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) { |
45aafd32 AB |
763 | /* |
764 | * Bit-flips or integrity errors while reading the data area. | |
765 | * It is difficult to say for sure what type of corruption is | |
766 | * this, but presumably a power cut happened while this PEB was | |
767 | * erased, so it became unstable and corrupted, and should be | |
768 | * erased. | |
769 | */ | |
1b1d76e2 DC |
770 | err = 0; |
771 | goto out_unlock; | |
45aafd32 AB |
772 | } |
773 | ||
774 | if (err) | |
1b1d76e2 | 775 | goto out_unlock; |
feeba4b8 | 776 | |
0ca39d74 | 777 | if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size)) |
1b1d76e2 | 778 | goto out_unlock; |
feeba4b8 AB |
779 | |
780 | ubi_err("PEB %d contains corrupted VID header, and the data does not " | |
781 | "contain all 0xFF, this may be a non-UBI PEB or a severe VID " | |
782 | "header corruption which requires manual inspection", pnum); | |
a904e3f1 | 783 | ubi_dump_vid_hdr(vid_hdr); |
feeba4b8 AB |
784 | dbg_msg("hexdump of PEB %d offset %d, length %d", |
785 | pnum, ubi->leb_start, ubi->leb_size); | |
786 | ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, | |
0ca39d74 | 787 | ubi->peb_buf, ubi->leb_size, 1); |
1b1d76e2 DC |
788 | err = 1; |
789 | ||
790 | out_unlock: | |
feeba4b8 | 791 | mutex_unlock(&ubi->buf_mutex); |
1b1d76e2 | 792 | return err; |
feeba4b8 AB |
793 | } |
794 | ||
801c135c | 795 | /** |
a4e6042f | 796 | * process_eb - read, check UBI headers, and add them to attaching information. |
801c135c | 797 | * @ubi: UBI device description object |
a4e6042f | 798 | * @ai: attaching information |
801c135c AB |
799 | * @pnum: the physical eraseblock number |
800 | * | |
78d87c95 | 801 | * This function returns a zero if the physical eraseblock was successfully |
801c135c AB |
802 | * handled and a negative error code in case of failure. |
803 | */ | |
a4e6042f | 804 | static int process_eb(struct ubi_device *ubi, struct ubi_attach_info *ai, |
9c9ec147 | 805 | int pnum) |
801c135c | 806 | { |
c18a8418 | 807 | long long uninitialized_var(ec); |
e0e718c2 | 808 | int err, bitflips = 0, vol_id, ec_err = 0; |
801c135c AB |
809 | |
810 | dbg_bld("scan PEB %d", pnum); | |
811 | ||
812 | /* Skip bad physical eraseblocks */ | |
813 | err = ubi_io_is_bad(ubi, pnum); | |
814 | if (err < 0) | |
815 | return err; | |
816 | else if (err) { | |
817 | /* | |
85c6e6e2 AB |
818 | * FIXME: this is actually duty of the I/O sub-system to |
819 | * initialize this, but MTD does not provide enough | |
820 | * information. | |
801c135c | 821 | */ |
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 AB |
1035 | /** |
1036 | * check_what_we_have - check what PEB were found by scanning. | |
1037 | * @ubi: UBI device description object | |
a4e6042f | 1038 | * @ai: attaching information |
0798cea8 AB |
1039 | * |
1040 | * This is a helper function which takes a look what PEBs were found by | |
1041 | * scanning, and decides whether the flash is empty and should be formatted and | |
1042 | * whether there are too many corrupted PEBs and we should not attach this | |
1043 | * MTD device. Returns zero if we should proceed with attaching the MTD device, | |
1044 | * and %-EINVAL if we should not. | |
1045 | */ | |
afc15a81 | 1046 | static int check_what_we_have(struct ubi_device *ubi, |
a4e6042f | 1047 | struct ubi_attach_info *ai) |
0798cea8 | 1048 | { |
2c5ec5ce | 1049 | struct ubi_ainf_peb *aeb; |
0525dac9 | 1050 | int max_corr, peb_count; |
0798cea8 | 1051 | |
a4e6042f | 1052 | peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count; |
0525dac9 | 1053 | max_corr = peb_count / 20 ?: 8; |
0798cea8 AB |
1054 | |
1055 | /* | |
0525dac9 | 1056 | * Few corrupted PEBs is not a problem and may be just a result of |
0798cea8 AB |
1057 | * unclean reboots. However, many of them may indicate some problems |
1058 | * with the flash HW or driver. | |
1059 | */ | |
a4e6042f | 1060 | if (ai->corr_peb_count) { |
0525dac9 | 1061 | ubi_err("%d PEBs are corrupted and preserved", |
a4e6042f | 1062 | ai->corr_peb_count); |
0525dac9 | 1063 | printk(KERN_ERR "Corrupted PEBs are:"); |
a4e6042f | 1064 | list_for_each_entry(aeb, &ai->corr, u.list) |
2c5ec5ce | 1065 | printk(KERN_CONT " %d", aeb->pnum); |
0798cea8 AB |
1066 | printk(KERN_CONT "\n"); |
1067 | ||
1068 | /* | |
1069 | * If too many PEBs are corrupted, we refuse attaching, | |
1070 | * otherwise, only print a warning. | |
1071 | */ | |
a4e6042f | 1072 | if (ai->corr_peb_count >= max_corr) { |
feddbb34 | 1073 | ubi_err("too many corrupted PEBs, refusing"); |
0798cea8 AB |
1074 | return -EINVAL; |
1075 | } | |
1076 | } | |
1077 | ||
a4e6042f | 1078 | if (ai->empty_peb_count + ai->maybe_bad_peb_count == peb_count) { |
0525dac9 AB |
1079 | /* |
1080 | * All PEBs are empty, or almost all - a couple PEBs look like | |
1081 | * they may be bad PEBs which were not marked as bad yet. | |
1082 | * | |
1083 | * This piece of code basically tries to distinguish between | |
1084 | * the following situations: | |
1085 | * | |
1086 | * 1. Flash is empty, but there are few bad PEBs, which are not | |
1087 | * marked as bad so far, and which were read with error. We | |
1088 | * want to go ahead and format this flash. While formatting, | |
1089 | * the faulty PEBs will probably be marked as bad. | |
1090 | * | |
1091 | * 2. Flash contains non-UBI data and we do not want to format | |
1092 | * it and destroy possibly important information. | |
1093 | */ | |
a4e6042f AB |
1094 | if (ai->maybe_bad_peb_count <= 2) { |
1095 | ai->is_empty = 1; | |
0798cea8 | 1096 | ubi_msg("empty MTD device detected"); |
0525dac9 AB |
1097 | get_random_bytes(&ubi->image_seq, |
1098 | sizeof(ubi->image_seq)); | |
0798cea8 | 1099 | } else { |
0525dac9 AB |
1100 | ubi_err("MTD device is not UBI-formatted and possibly " |
1101 | "contains non-UBI data - refusing it"); | |
0798cea8 AB |
1102 | return -EINVAL; |
1103 | } | |
0525dac9 | 1104 | |
0798cea8 AB |
1105 | } |
1106 | ||
0798cea8 AB |
1107 | return 0; |
1108 | } | |
1109 | ||
801c135c AB |
1110 | /** |
1111 | * ubi_scan - scan an MTD device. | |
1112 | * @ubi: UBI device description object | |
1113 | * | |
1114 | * This function does full scanning of an MTD device and returns complete | |
1115 | * information about it. In case of failure, an error code is returned. | |
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; | |
a4e6042f | 1136 | ai->scan_leb_slab = kmem_cache_create("ubi_scan_leb_slab", |
227423d2 | 1137 | sizeof(struct ubi_ainf_peb), |
6c1e875c | 1138 | 0, 0, NULL); |
a4e6042f AB |
1139 | if (!ai->scan_leb_slab) |
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); |
a4e6042f | 1154 | err = process_eb(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 | |
a4e6042f | 1165 | err = check_what_we_have(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 | /** | |
517af48c AB |
1211 | * destroy_av - free the scanning volume information |
1212 | * @av: scanning volume information | |
a4e6042f | 1213 | * @ai: attaching information |
801c135c | 1214 | * |
517af48c | 1215 | * This function destroys the volume RB-tree (@av->root) and the scanning |
801c135c AB |
1216 | * volume information. |
1217 | */ | |
517af48c | 1218 | static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av) |
801c135c | 1219 | { |
2c5ec5ce | 1220 | struct ubi_ainf_peb *aeb; |
517af48c | 1221 | struct rb_node *this = av->root.rb_node; |
801c135c AB |
1222 | |
1223 | while (this) { | |
1224 | if (this->rb_left) | |
1225 | this = this->rb_left; | |
1226 | else if (this->rb_right) | |
1227 | this = this->rb_right; | |
1228 | else { | |
2c5ec5ce | 1229 | aeb = rb_entry(this, struct ubi_ainf_peb, u.rb); |
801c135c AB |
1230 | this = rb_parent(this); |
1231 | if (this) { | |
2c5ec5ce | 1232 | if (this->rb_left == &aeb->u.rb) |
801c135c AB |
1233 | this->rb_left = NULL; |
1234 | else | |
1235 | this->rb_right = NULL; | |
1236 | } | |
1237 | ||
a4e6042f | 1238 | kmem_cache_free(ai->scan_leb_slab, aeb); |
801c135c AB |
1239 | } |
1240 | } | |
517af48c | 1241 | kfree(av); |
801c135c AB |
1242 | } |
1243 | ||
1244 | /** | |
66a2af38 | 1245 | * ubi_destroy_ai - destroy attaching information. |
a4e6042f | 1246 | * @ai: attaching information |
801c135c | 1247 | */ |
66a2af38 | 1248 | void ubi_destroy_ai(struct ubi_attach_info *ai) |
801c135c | 1249 | { |
2c5ec5ce | 1250 | struct ubi_ainf_peb *aeb, *aeb_tmp; |
517af48c | 1251 | struct ubi_ainf_volume *av; |
801c135c AB |
1252 | struct rb_node *rb; |
1253 | ||
a4e6042f | 1254 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) { |
2c5ec5ce | 1255 | list_del(&aeb->u.list); |
a4e6042f | 1256 | kmem_cache_free(ai->scan_leb_slab, aeb); |
801c135c | 1257 | } |
a4e6042f | 1258 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) { |
2c5ec5ce | 1259 | list_del(&aeb->u.list); |
a4e6042f | 1260 | kmem_cache_free(ai->scan_leb_slab, aeb); |
801c135c | 1261 | } |
a4e6042f | 1262 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) { |
2c5ec5ce | 1263 | list_del(&aeb->u.list); |
a4e6042f | 1264 | kmem_cache_free(ai->scan_leb_slab, aeb); |
801c135c | 1265 | } |
a4e6042f | 1266 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) { |
2c5ec5ce | 1267 | list_del(&aeb->u.list); |
a4e6042f | 1268 | kmem_cache_free(ai->scan_leb_slab, aeb); |
801c135c AB |
1269 | } |
1270 | ||
1271 | /* Destroy the volume RB-tree */ | |
a4e6042f | 1272 | rb = ai->volumes.rb_node; |
801c135c AB |
1273 | while (rb) { |
1274 | if (rb->rb_left) | |
1275 | rb = rb->rb_left; | |
1276 | else if (rb->rb_right) | |
1277 | rb = rb->rb_right; | |
1278 | else { | |
517af48c | 1279 | av = rb_entry(rb, struct ubi_ainf_volume, rb); |
801c135c AB |
1280 | |
1281 | rb = rb_parent(rb); | |
1282 | if (rb) { | |
517af48c | 1283 | if (rb->rb_left == &av->rb) |
801c135c AB |
1284 | rb->rb_left = NULL; |
1285 | else | |
1286 | rb->rb_right = NULL; | |
1287 | } | |
1288 | ||
517af48c | 1289 | destroy_av(ai, av); |
801c135c AB |
1290 | } |
1291 | } | |
1292 | ||
a4e6042f AB |
1293 | if (ai->scan_leb_slab) |
1294 | kmem_cache_destroy(ai->scan_leb_slab); | |
a29852be | 1295 | |
a4e6042f | 1296 | kfree(ai); |
801c135c AB |
1297 | } |
1298 | ||
801c135c | 1299 | /** |
a4e6042f | 1300 | * self_check_ai - check the attaching information. |
801c135c | 1301 | * @ubi: UBI device description object |
a4e6042f | 1302 | * @ai: attaching information |
801c135c | 1303 | * |
a4e6042f | 1304 | * This function returns zero if the attaching information is all right, and a |
adbf05e3 | 1305 | * negative error code if not or if an error occurred. |
801c135c | 1306 | */ |
a4e6042f | 1307 | static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai) |
801c135c AB |
1308 | { |
1309 | int pnum, err, vols_found = 0; | |
1310 | struct rb_node *rb1, *rb2; | |
517af48c | 1311 | struct ubi_ainf_volume *av; |
2c5ec5ce | 1312 | struct ubi_ainf_peb *aeb, *last_aeb; |
801c135c AB |
1313 | uint8_t *buf; |
1314 | ||
2a734bb8 | 1315 | if (!ubi->dbg->chk_gen) |
92d124f5 AB |
1316 | return 0; |
1317 | ||
801c135c | 1318 | /* |
a4e6042f | 1319 | * At first, check that attaching information is OK. |
801c135c | 1320 | */ |
517af48c | 1321 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { |
801c135c AB |
1322 | int leb_count = 0; |
1323 | ||
1324 | cond_resched(); | |
1325 | ||
1326 | vols_found += 1; | |
1327 | ||
a4e6042f | 1328 | if (ai->is_empty) { |
801c135c | 1329 | ubi_err("bad is_empty flag"); |
517af48c | 1330 | goto bad_av; |
801c135c AB |
1331 | } |
1332 | ||
517af48c AB |
1333 | if (av->vol_id < 0 || av->highest_lnum < 0 || |
1334 | av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 || | |
1335 | av->data_pad < 0 || av->last_data_size < 0) { | |
801c135c | 1336 | ubi_err("negative values"); |
517af48c | 1337 | goto bad_av; |
801c135c AB |
1338 | } |
1339 | ||
517af48c AB |
1340 | if (av->vol_id >= UBI_MAX_VOLUMES && |
1341 | av->vol_id < UBI_INTERNAL_VOL_START) { | |
801c135c | 1342 | ubi_err("bad vol_id"); |
517af48c | 1343 | goto bad_av; |
801c135c AB |
1344 | } |
1345 | ||
517af48c | 1346 | if (av->vol_id > ai->highest_vol_id) { |
801c135c | 1347 | ubi_err("highest_vol_id is %d, but vol_id %d is there", |
517af48c | 1348 | ai->highest_vol_id, av->vol_id); |
801c135c AB |
1349 | goto out; |
1350 | } | |
1351 | ||
517af48c AB |
1352 | if (av->vol_type != UBI_DYNAMIC_VOLUME && |
1353 | av->vol_type != UBI_STATIC_VOLUME) { | |
801c135c | 1354 | ubi_err("bad vol_type"); |
517af48c | 1355 | goto bad_av; |
801c135c AB |
1356 | } |
1357 | ||
517af48c | 1358 | if (av->data_pad > ubi->leb_size / 2) { |
801c135c | 1359 | ubi_err("bad data_pad"); |
517af48c | 1360 | goto bad_av; |
801c135c AB |
1361 | } |
1362 | ||
2c5ec5ce | 1363 | last_aeb = NULL; |
517af48c | 1364 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { |
801c135c AB |
1365 | cond_resched(); |
1366 | ||
2c5ec5ce | 1367 | last_aeb = aeb; |
801c135c AB |
1368 | leb_count += 1; |
1369 | ||
2c5ec5ce | 1370 | if (aeb->pnum < 0 || aeb->ec < 0) { |
801c135c | 1371 | ubi_err("negative values"); |
2c5ec5ce | 1372 | goto bad_aeb; |
801c135c AB |
1373 | } |
1374 | ||
a4e6042f AB |
1375 | if (aeb->ec < ai->min_ec) { |
1376 | ubi_err("bad ai->min_ec (%d), %d found", | |
1377 | ai->min_ec, aeb->ec); | |
2c5ec5ce | 1378 | goto bad_aeb; |
801c135c AB |
1379 | } |
1380 | ||
a4e6042f AB |
1381 | if (aeb->ec > ai->max_ec) { |
1382 | ubi_err("bad ai->max_ec (%d), %d found", | |
1383 | ai->max_ec, aeb->ec); | |
2c5ec5ce | 1384 | goto bad_aeb; |
801c135c AB |
1385 | } |
1386 | ||
2c5ec5ce | 1387 | if (aeb->pnum >= ubi->peb_count) { |
801c135c | 1388 | ubi_err("too high PEB number %d, total PEBs %d", |
2c5ec5ce AB |
1389 | aeb->pnum, ubi->peb_count); |
1390 | goto bad_aeb; | |
801c135c AB |
1391 | } |
1392 | ||
517af48c AB |
1393 | if (av->vol_type == UBI_STATIC_VOLUME) { |
1394 | if (aeb->lnum >= av->used_ebs) { | |
801c135c | 1395 | ubi_err("bad lnum or used_ebs"); |
2c5ec5ce | 1396 | goto bad_aeb; |
801c135c AB |
1397 | } |
1398 | } else { | |
517af48c | 1399 | if (av->used_ebs != 0) { |
801c135c | 1400 | ubi_err("non-zero used_ebs"); |
2c5ec5ce | 1401 | goto bad_aeb; |
801c135c AB |
1402 | } |
1403 | } | |
1404 | ||
517af48c | 1405 | if (aeb->lnum > av->highest_lnum) { |
801c135c | 1406 | ubi_err("incorrect highest_lnum or lnum"); |
2c5ec5ce | 1407 | goto bad_aeb; |
801c135c AB |
1408 | } |
1409 | } | |
1410 | ||
517af48c | 1411 | if (av->leb_count != leb_count) { |
801c135c AB |
1412 | ubi_err("bad leb_count, %d objects in the tree", |
1413 | leb_count); | |
517af48c | 1414 | goto bad_av; |
801c135c AB |
1415 | } |
1416 | ||
2c5ec5ce | 1417 | if (!last_aeb) |
801c135c AB |
1418 | continue; |
1419 | ||
2c5ec5ce | 1420 | aeb = last_aeb; |
801c135c | 1421 | |
517af48c | 1422 | if (aeb->lnum != av->highest_lnum) { |
801c135c | 1423 | ubi_err("bad highest_lnum"); |
2c5ec5ce | 1424 | goto bad_aeb; |
801c135c AB |
1425 | } |
1426 | } | |
1427 | ||
a4e6042f AB |
1428 | if (vols_found != ai->vols_found) { |
1429 | ubi_err("bad ai->vols_found %d, should be %d", | |
1430 | ai->vols_found, vols_found); | |
801c135c AB |
1431 | goto out; |
1432 | } | |
1433 | ||
a4e6042f | 1434 | /* Check that attaching information is correct */ |
517af48c | 1435 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { |
2c5ec5ce | 1436 | last_aeb = NULL; |
517af48c | 1437 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { |
801c135c AB |
1438 | int vol_type; |
1439 | ||
1440 | cond_resched(); | |
1441 | ||
2c5ec5ce | 1442 | last_aeb = aeb; |
801c135c | 1443 | |
2c5ec5ce | 1444 | err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidh, 1); |
801c135c AB |
1445 | if (err && err != UBI_IO_BITFLIPS) { |
1446 | ubi_err("VID header is not OK (%d)", err); | |
1447 | if (err > 0) | |
1448 | err = -EIO; | |
1449 | return err; | |
1450 | } | |
1451 | ||
1452 | vol_type = vidh->vol_type == UBI_VID_DYNAMIC ? | |
1453 | UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; | |
517af48c | 1454 | if (av->vol_type != vol_type) { |
801c135c AB |
1455 | ubi_err("bad vol_type"); |
1456 | goto bad_vid_hdr; | |
1457 | } | |
1458 | ||
2c5ec5ce AB |
1459 | if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) { |
1460 | ubi_err("bad sqnum %llu", aeb->sqnum); | |
801c135c AB |
1461 | goto bad_vid_hdr; |
1462 | } | |
1463 | ||
517af48c AB |
1464 | if (av->vol_id != be32_to_cpu(vidh->vol_id)) { |
1465 | ubi_err("bad vol_id %d", av->vol_id); | |
801c135c AB |
1466 | goto bad_vid_hdr; |
1467 | } | |
1468 | ||
517af48c | 1469 | if (av->compat != vidh->compat) { |
801c135c AB |
1470 | ubi_err("bad compat %d", vidh->compat); |
1471 | goto bad_vid_hdr; | |
1472 | } | |
1473 | ||
2c5ec5ce AB |
1474 | if (aeb->lnum != be32_to_cpu(vidh->lnum)) { |
1475 | ubi_err("bad lnum %d", aeb->lnum); | |
801c135c AB |
1476 | goto bad_vid_hdr; |
1477 | } | |
1478 | ||
517af48c AB |
1479 | if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) { |
1480 | ubi_err("bad used_ebs %d", av->used_ebs); | |
801c135c AB |
1481 | goto bad_vid_hdr; |
1482 | } | |
1483 | ||
517af48c AB |
1484 | if (av->data_pad != be32_to_cpu(vidh->data_pad)) { |
1485 | ubi_err("bad data_pad %d", av->data_pad); | |
801c135c AB |
1486 | goto bad_vid_hdr; |
1487 | } | |
801c135c AB |
1488 | } |
1489 | ||
2c5ec5ce | 1490 | if (!last_aeb) |
801c135c AB |
1491 | continue; |
1492 | ||
517af48c AB |
1493 | if (av->highest_lnum != be32_to_cpu(vidh->lnum)) { |
1494 | ubi_err("bad highest_lnum %d", av->highest_lnum); | |
801c135c AB |
1495 | goto bad_vid_hdr; |
1496 | } | |
1497 | ||
517af48c AB |
1498 | if (av->last_data_size != be32_to_cpu(vidh->data_size)) { |
1499 | ubi_err("bad last_data_size %d", av->last_data_size); | |
801c135c AB |
1500 | goto bad_vid_hdr; |
1501 | } | |
1502 | } | |
1503 | ||
1504 | /* | |
1505 | * Make sure that all the physical eraseblocks are in one of the lists | |
1506 | * or trees. | |
1507 | */ | |
d9b0744d | 1508 | buf = kzalloc(ubi->peb_count, GFP_KERNEL); |
801c135c AB |
1509 | if (!buf) |
1510 | return -ENOMEM; | |
1511 | ||
801c135c AB |
1512 | for (pnum = 0; pnum < ubi->peb_count; pnum++) { |
1513 | err = ubi_io_is_bad(ubi, pnum); | |
341e1a0c AB |
1514 | if (err < 0) { |
1515 | kfree(buf); | |
801c135c | 1516 | return err; |
9c9ec147 | 1517 | } else if (err) |
d9b0744d | 1518 | buf[pnum] = 1; |
801c135c AB |
1519 | } |
1520 | ||
517af48c AB |
1521 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) |
1522 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) | |
2c5ec5ce | 1523 | buf[aeb->pnum] = 1; |
801c135c | 1524 | |
a4e6042f | 1525 | list_for_each_entry(aeb, &ai->free, u.list) |
2c5ec5ce | 1526 | buf[aeb->pnum] = 1; |
801c135c | 1527 | |
a4e6042f | 1528 | list_for_each_entry(aeb, &ai->corr, u.list) |
2c5ec5ce | 1529 | buf[aeb->pnum] = 1; |
801c135c | 1530 | |
a4e6042f | 1531 | list_for_each_entry(aeb, &ai->erase, u.list) |
2c5ec5ce | 1532 | buf[aeb->pnum] = 1; |
801c135c | 1533 | |
a4e6042f | 1534 | list_for_each_entry(aeb, &ai->alien, u.list) |
2c5ec5ce | 1535 | buf[aeb->pnum] = 1; |
801c135c AB |
1536 | |
1537 | err = 0; | |
1538 | for (pnum = 0; pnum < ubi->peb_count; pnum++) | |
d9b0744d | 1539 | if (!buf[pnum]) { |
801c135c AB |
1540 | ubi_err("PEB %d is not referred", pnum); |
1541 | err = 1; | |
1542 | } | |
1543 | ||
1544 | kfree(buf); | |
1545 | if (err) | |
1546 | goto out; | |
1547 | return 0; | |
1548 | ||
2c5ec5ce | 1549 | bad_aeb: |
a4e6042f | 1550 | ubi_err("bad attaching information about LEB %d", aeb->lnum); |
2c5ec5ce | 1551 | ubi_dump_aeb(aeb, 0); |
517af48c | 1552 | ubi_dump_av(av); |
801c135c AB |
1553 | goto out; |
1554 | ||
517af48c AB |
1555 | bad_av: |
1556 | ubi_err("bad attaching information about volume %d", av->vol_id); | |
1557 | ubi_dump_av(av); | |
801c135c AB |
1558 | goto out; |
1559 | ||
1560 | bad_vid_hdr: | |
517af48c AB |
1561 | ubi_err("bad attaching information about volume %d", av->vol_id); |
1562 | ubi_dump_av(av); | |
a904e3f1 | 1563 | ubi_dump_vid_hdr(vidh); |
801c135c AB |
1564 | |
1565 | out: | |
25886a36 | 1566 | dump_stack(); |
adbf05e3 | 1567 | return -EINVAL; |
801c135c | 1568 | } |