]>
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 | * | |
78d87c95 | 27 | * The scanning information is represented by a &struct ubi_scan_info' object. |
801c135c AB |
28 | * Information about found volumes is represented by &struct ubi_scan_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. | |
31 | * | |
32 | * Found logical eraseblocks are represented by &struct ubi_scan_leb objects. | |
33 | * These objects are kept in per-volume RB-trees with the root at the | |
34 | * corresponding &struct ubi_scan_volume object. To put it differently, we keep | |
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. | |
41 | */ | |
42 | ||
43 | #include <linux/err.h> | |
44 | #include <linux/crc32.h> | |
4bc1dca4 | 45 | #include <asm/div64.h> |
801c135c AB |
46 | #include "ubi.h" |
47 | ||
48 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | |
e88d6e10 | 49 | static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si); |
801c135c AB |
50 | #else |
51 | #define paranoid_check_si(ubi, si) 0 | |
52 | #endif | |
53 | ||
54 | /* Temporary variables used during scanning */ | |
55 | static struct ubi_ec_hdr *ech; | |
56 | static struct ubi_vid_hdr *vidh; | |
57 | ||
941dfb07 | 58 | /** |
78d87c95 AB |
59 | * add_to_list - add physical eraseblock to a list. |
60 | * @si: scanning information | |
61 | * @pnum: physical eraseblock number to add | |
62 | * @ec: erase counter of the physical eraseblock | |
63 | * @list: the list to add to | |
64 | * | |
65 | * This function adds physical eraseblock @pnum to free, erase, corrupted or | |
66 | * alien lists. Returns zero in case of success and a negative error code in | |
67 | * case of failure. | |
68 | */ | |
69 | static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, | |
70 | struct list_head *list) | |
801c135c AB |
71 | { |
72 | struct ubi_scan_leb *seb; | |
73 | ||
74 | if (list == &si->free) | |
75 | dbg_bld("add to free: PEB %d, EC %d", pnum, ec); | |
76 | else if (list == &si->erase) | |
77 | dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); | |
78 | else if (list == &si->corr) | |
79 | dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec); | |
80 | else if (list == &si->alien) | |
81 | dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); | |
82 | else | |
83 | BUG(); | |
84 | ||
85 | seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); | |
86 | if (!seb) | |
87 | return -ENOMEM; | |
88 | ||
89 | seb->pnum = pnum; | |
90 | seb->ec = ec; | |
91 | list_add_tail(&seb->u.list, list); | |
92 | return 0; | |
93 | } | |
94 | ||
801c135c | 95 | /** |
ebaaf1af | 96 | * validate_vid_hdr - check volume identifier header. |
801c135c AB |
97 | * @vid_hdr: the volume identifier header to check |
98 | * @sv: information about the volume this logical eraseblock belongs to | |
99 | * @pnum: physical eraseblock number the VID header came from | |
100 | * | |
101 | * This function checks that data stored in @vid_hdr is consistent. Returns | |
102 | * non-zero if an inconsistency was found and zero if not. | |
103 | * | |
104 | * Note, UBI does sanity check of everything it reads from the flash media. | |
85c6e6e2 | 105 | * Most of the checks are done in the I/O sub-system. Here we check that the |
801c135c AB |
106 | * information in the VID header is consistent to the information in other VID |
107 | * headers of the same volume. | |
108 | */ | |
109 | static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr, | |
110 | const struct ubi_scan_volume *sv, int pnum) | |
111 | { | |
112 | int vol_type = vid_hdr->vol_type; | |
3261ebd7 CH |
113 | int vol_id = be32_to_cpu(vid_hdr->vol_id); |
114 | int used_ebs = be32_to_cpu(vid_hdr->used_ebs); | |
115 | int data_pad = be32_to_cpu(vid_hdr->data_pad); | |
801c135c AB |
116 | |
117 | if (sv->leb_count != 0) { | |
118 | int sv_vol_type; | |
119 | ||
120 | /* | |
121 | * This is not the first logical eraseblock belonging to this | |
122 | * volume. Ensure that the data in its VID header is consistent | |
123 | * to the data in previous logical eraseblock headers. | |
124 | */ | |
125 | ||
126 | if (vol_id != sv->vol_id) { | |
127 | dbg_err("inconsistent vol_id"); | |
128 | goto bad; | |
129 | } | |
130 | ||
131 | if (sv->vol_type == UBI_STATIC_VOLUME) | |
132 | sv_vol_type = UBI_VID_STATIC; | |
133 | else | |
134 | sv_vol_type = UBI_VID_DYNAMIC; | |
135 | ||
136 | if (vol_type != sv_vol_type) { | |
137 | dbg_err("inconsistent vol_type"); | |
138 | goto bad; | |
139 | } | |
140 | ||
141 | if (used_ebs != sv->used_ebs) { | |
142 | dbg_err("inconsistent used_ebs"); | |
143 | goto bad; | |
144 | } | |
145 | ||
146 | if (data_pad != sv->data_pad) { | |
147 | dbg_err("inconsistent data_pad"); | |
148 | goto bad; | |
149 | } | |
150 | } | |
151 | ||
152 | return 0; | |
153 | ||
154 | bad: | |
155 | ubi_err("inconsistent VID header at PEB %d", pnum); | |
156 | ubi_dbg_dump_vid_hdr(vid_hdr); | |
157 | ubi_dbg_dump_sv(sv); | |
158 | return -EINVAL; | |
159 | } | |
160 | ||
161 | /** | |
162 | * add_volume - add volume to the scanning information. | |
163 | * @si: scanning information | |
164 | * @vol_id: ID of the volume to add | |
165 | * @pnum: physical eraseblock number | |
166 | * @vid_hdr: volume identifier header | |
167 | * | |
168 | * If the volume corresponding to the @vid_hdr logical eraseblock is already | |
169 | * present in the scanning information, this function does nothing. Otherwise | |
170 | * it adds corresponding volume to the scanning information. Returns a pointer | |
171 | * to the scanning volume object in case of success and a negative error code | |
172 | * in case of failure. | |
173 | */ | |
174 | static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id, | |
175 | int pnum, | |
176 | const struct ubi_vid_hdr *vid_hdr) | |
177 | { | |
178 | struct ubi_scan_volume *sv; | |
179 | struct rb_node **p = &si->volumes.rb_node, *parent = NULL; | |
180 | ||
3261ebd7 | 181 | ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id)); |
801c135c AB |
182 | |
183 | /* Walk the volume RB-tree to look if this volume is already present */ | |
184 | while (*p) { | |
185 | parent = *p; | |
186 | sv = rb_entry(parent, struct ubi_scan_volume, rb); | |
187 | ||
188 | if (vol_id == sv->vol_id) | |
189 | return sv; | |
190 | ||
191 | if (vol_id > sv->vol_id) | |
192 | p = &(*p)->rb_left; | |
193 | else | |
194 | p = &(*p)->rb_right; | |
195 | } | |
196 | ||
197 | /* The volume is absent - add it */ | |
198 | sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL); | |
199 | if (!sv) | |
200 | return ERR_PTR(-ENOMEM); | |
201 | ||
202 | sv->highest_lnum = sv->leb_count = 0; | |
801c135c AB |
203 | sv->vol_id = vol_id; |
204 | sv->root = RB_ROOT; | |
3261ebd7 CH |
205 | sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs); |
206 | sv->data_pad = be32_to_cpu(vid_hdr->data_pad); | |
801c135c AB |
207 | sv->compat = vid_hdr->compat; |
208 | sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME | |
209 | : UBI_STATIC_VOLUME; | |
210 | if (vol_id > si->highest_vol_id) | |
211 | si->highest_vol_id = vol_id; | |
212 | ||
213 | rb_link_node(&sv->rb, parent, p); | |
214 | rb_insert_color(&sv->rb, &si->volumes); | |
215 | si->vols_found += 1; | |
216 | dbg_bld("added volume %d", vol_id); | |
217 | return sv; | |
218 | } | |
219 | ||
220 | /** | |
221 | * compare_lebs - find out which logical eraseblock is newer. | |
222 | * @ubi: UBI device description object | |
223 | * @seb: first logical eraseblock to compare | |
224 | * @pnum: physical eraseblock number of the second logical eraseblock to | |
225 | * compare | |
226 | * @vid_hdr: volume identifier header of the second logical eraseblock | |
227 | * | |
228 | * This function compares 2 copies of a LEB and informs which one is newer. In | |
229 | * case of success this function returns a positive value, in case of failure, a | |
230 | * negative error code is returned. The success return codes use the following | |
231 | * bits: | |
232 | * o bit 0 is cleared: the first PEB (described by @seb) is newer then the | |
233 | * second PEB (described by @pnum and @vid_hdr); | |
234 | * o bit 0 is set: the second PEB is newer; | |
235 | * o bit 1 is cleared: no bit-flips were detected in the newer LEB; | |
236 | * o bit 1 is set: bit-flips were detected in the newer LEB; | |
237 | * o bit 2 is cleared: the older LEB is not corrupted; | |
238 | * o bit 2 is set: the older LEB is corrupted. | |
239 | */ | |
e88d6e10 AB |
240 | static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb, |
241 | int pnum, const struct ubi_vid_hdr *vid_hdr) | |
801c135c AB |
242 | { |
243 | void *buf; | |
244 | int len, err, second_is_newer, bitflips = 0, corrupted = 0; | |
245 | uint32_t data_crc, crc; | |
8bc22961 | 246 | struct ubi_vid_hdr *vh = NULL; |
3261ebd7 | 247 | unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); |
801c135c AB |
248 | |
249 | if (seb->sqnum == 0 && sqnum2 == 0) { | |
9c9ec147 AB |
250 | long long abs; |
251 | long long v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver); | |
801c135c AB |
252 | |
253 | /* | |
254 | * UBI constantly increases the logical eraseblock version | |
255 | * number and it can overflow. Thus, we have to bear in mind | |
256 | * that versions that are close to %0xFFFFFFFF are less then | |
257 | * versions that are close to %0. | |
258 | * | |
85c6e6e2 AB |
259 | * The UBI WL sub-system guarantees that the number of pending |
260 | * tasks is not greater then %0x7FFFFFFF. So, if the difference | |
801c135c AB |
261 | * between any two versions is greater or equivalent to |
262 | * %0x7FFFFFFF, there was an overflow and the logical | |
263 | * eraseblock with lower version is actually newer then the one | |
264 | * with higher version. | |
265 | * | |
266 | * FIXME: but this is anyway obsolete and will be removed at | |
267 | * some point. | |
268 | */ | |
801c135c AB |
269 | dbg_bld("using old crappy leb_ver stuff"); |
270 | ||
64203195 AB |
271 | if (v1 == v2) { |
272 | ubi_err("PEB %d and PEB %d have the same version %lld", | |
273 | seb->pnum, pnum, v1); | |
274 | return -EINVAL; | |
275 | } | |
276 | ||
801c135c AB |
277 | abs = v1 - v2; |
278 | if (abs < 0) | |
279 | abs = -abs; | |
280 | ||
281 | if (abs < 0x7FFFFFFF) | |
282 | /* Non-overflow situation */ | |
283 | second_is_newer = (v2 > v1); | |
284 | else | |
285 | second_is_newer = (v2 < v1); | |
286 | } else | |
287 | /* Obviously the LEB with lower sequence counter is older */ | |
288 | second_is_newer = sqnum2 > seb->sqnum; | |
289 | ||
290 | /* | |
291 | * Now we know which copy is newer. If the copy flag of the PEB with | |
292 | * newer version is not set, then we just return, otherwise we have to | |
293 | * check data CRC. For the second PEB we already have the VID header, | |
294 | * for the first one - we'll need to re-read it from flash. | |
295 | * | |
296 | * FIXME: this may be optimized so that we wouldn't read twice. | |
297 | */ | |
298 | ||
299 | if (second_is_newer) { | |
300 | if (!vid_hdr->copy_flag) { | |
301 | /* It is not a copy, so it is newer */ | |
302 | dbg_bld("second PEB %d is newer, copy_flag is unset", | |
303 | pnum); | |
304 | return 1; | |
305 | } | |
306 | } else { | |
307 | pnum = seb->pnum; | |
308 | ||
33818bbb | 309 | vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); |
8bc22961 | 310 | if (!vh) |
801c135c AB |
311 | return -ENOMEM; |
312 | ||
8bc22961 | 313 | err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); |
801c135c AB |
314 | if (err) { |
315 | if (err == UBI_IO_BITFLIPS) | |
316 | bitflips = 1; | |
317 | else { | |
318 | dbg_err("VID of PEB %d header is bad, but it " | |
319 | "was OK earlier", pnum); | |
320 | if (err > 0) | |
321 | err = -EIO; | |
322 | ||
323 | goto out_free_vidh; | |
324 | } | |
325 | } | |
326 | ||
8bc22961 | 327 | if (!vh->copy_flag) { |
801c135c AB |
328 | /* It is not a copy, so it is newer */ |
329 | dbg_bld("first PEB %d is newer, copy_flag is unset", | |
330 | pnum); | |
331 | err = bitflips << 1; | |
332 | goto out_free_vidh; | |
333 | } | |
334 | ||
8bc22961 | 335 | vid_hdr = vh; |
801c135c AB |
336 | } |
337 | ||
338 | /* Read the data of the copy and check the CRC */ | |
339 | ||
3261ebd7 | 340 | len = be32_to_cpu(vid_hdr->data_size); |
92ad8f37 | 341 | buf = vmalloc(len); |
801c135c AB |
342 | if (!buf) { |
343 | err = -ENOMEM; | |
344 | goto out_free_vidh; | |
345 | } | |
346 | ||
347 | err = ubi_io_read_data(ubi, buf, pnum, 0, len); | |
348 | if (err && err != UBI_IO_BITFLIPS) | |
349 | goto out_free_buf; | |
350 | ||
3261ebd7 | 351 | data_crc = be32_to_cpu(vid_hdr->data_crc); |
801c135c AB |
352 | crc = crc32(UBI_CRC32_INIT, buf, len); |
353 | if (crc != data_crc) { | |
354 | dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x", | |
355 | pnum, crc, data_crc); | |
356 | corrupted = 1; | |
357 | bitflips = 0; | |
358 | second_is_newer = !second_is_newer; | |
359 | } else { | |
360 | dbg_bld("PEB %d CRC is OK", pnum); | |
361 | bitflips = !!err; | |
362 | } | |
363 | ||
92ad8f37 | 364 | vfree(buf); |
8bc22961 | 365 | ubi_free_vid_hdr(ubi, vh); |
801c135c AB |
366 | |
367 | if (second_is_newer) | |
368 | dbg_bld("second PEB %d is newer, copy_flag is set", pnum); | |
369 | else | |
370 | dbg_bld("first PEB %d is newer, copy_flag is set", pnum); | |
371 | ||
372 | return second_is_newer | (bitflips << 1) | (corrupted << 2); | |
373 | ||
374 | out_free_buf: | |
92ad8f37 | 375 | vfree(buf); |
801c135c | 376 | out_free_vidh: |
8bc22961 | 377 | ubi_free_vid_hdr(ubi, vh); |
801c135c AB |
378 | return err; |
379 | } | |
380 | ||
381 | /** | |
ebaaf1af | 382 | * ubi_scan_add_used - add physical eraseblock to the scanning information. |
801c135c AB |
383 | * @ubi: UBI device description object |
384 | * @si: scanning information | |
385 | * @pnum: the physical eraseblock number | |
386 | * @ec: erase counter | |
387 | * @vid_hdr: the volume identifier header | |
388 | * @bitflips: if bit-flips were detected when this physical eraseblock was read | |
389 | * | |
79b510c0 AB |
390 | * This function adds information about a used physical eraseblock to the |
391 | * 'used' tree of the corresponding volume. The function is rather complex | |
392 | * because it has to handle cases when this is not the first physical | |
393 | * eraseblock belonging to the same logical eraseblock, and the newer one has | |
394 | * to be picked, while the older one has to be dropped. This function returns | |
395 | * zero in case of success and a negative error code in case of failure. | |
801c135c | 396 | */ |
e88d6e10 | 397 | int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si, |
801c135c AB |
398 | int pnum, int ec, const struct ubi_vid_hdr *vid_hdr, |
399 | int bitflips) | |
400 | { | |
401 | int err, vol_id, lnum; | |
402 | uint32_t leb_ver; | |
403 | unsigned long long sqnum; | |
404 | struct ubi_scan_volume *sv; | |
405 | struct ubi_scan_leb *seb; | |
406 | struct rb_node **p, *parent = NULL; | |
407 | ||
3261ebd7 CH |
408 | vol_id = be32_to_cpu(vid_hdr->vol_id); |
409 | lnum = be32_to_cpu(vid_hdr->lnum); | |
410 | sqnum = be64_to_cpu(vid_hdr->sqnum); | |
411 | leb_ver = be32_to_cpu(vid_hdr->leb_ver); | |
801c135c AB |
412 | |
413 | dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d", | |
414 | pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips); | |
415 | ||
416 | sv = add_volume(si, vol_id, pnum, vid_hdr); | |
417 | if (IS_ERR(sv) < 0) | |
418 | return PTR_ERR(sv); | |
419 | ||
76eafe47 BS |
420 | if (si->max_sqnum < sqnum) |
421 | si->max_sqnum = sqnum; | |
422 | ||
801c135c AB |
423 | /* |
424 | * Walk the RB-tree of logical eraseblocks of volume @vol_id to look | |
425 | * if this is the first instance of this logical eraseblock or not. | |
426 | */ | |
427 | p = &sv->root.rb_node; | |
428 | while (*p) { | |
429 | int cmp_res; | |
430 | ||
431 | parent = *p; | |
432 | seb = rb_entry(parent, struct ubi_scan_leb, u.rb); | |
433 | if (lnum != seb->lnum) { | |
434 | if (lnum < seb->lnum) | |
435 | p = &(*p)->rb_left; | |
436 | else | |
437 | p = &(*p)->rb_right; | |
438 | continue; | |
439 | } | |
440 | ||
441 | /* | |
442 | * There is already a physical eraseblock describing the same | |
443 | * logical eraseblock present. | |
444 | */ | |
445 | ||
446 | dbg_bld("this LEB already exists: PEB %d, sqnum %llu, " | |
447 | "LEB ver %u, EC %d", seb->pnum, seb->sqnum, | |
448 | seb->leb_ver, seb->ec); | |
449 | ||
450 | /* | |
451 | * Make sure that the logical eraseblocks have different | |
452 | * versions. Otherwise the image is bad. | |
453 | */ | |
454 | if (seb->leb_ver == leb_ver && leb_ver != 0) { | |
455 | ubi_err("two LEBs with same version %u", leb_ver); | |
456 | ubi_dbg_dump_seb(seb, 0); | |
457 | ubi_dbg_dump_vid_hdr(vid_hdr); | |
458 | return -EINVAL; | |
459 | } | |
460 | ||
461 | /* | |
462 | * Make sure that the logical eraseblocks have different | |
463 | * sequence numbers. Otherwise the image is bad. | |
464 | * | |
465 | * FIXME: remove 'sqnum != 0' check when leb_ver is removed. | |
466 | */ | |
467 | if (seb->sqnum == sqnum && sqnum != 0) { | |
468 | ubi_err("two LEBs with same sequence number %llu", | |
469 | sqnum); | |
470 | ubi_dbg_dump_seb(seb, 0); | |
471 | ubi_dbg_dump_vid_hdr(vid_hdr); | |
472 | return -EINVAL; | |
473 | } | |
474 | ||
475 | /* | |
476 | * Now we have to drop the older one and preserve the newer | |
477 | * one. | |
478 | */ | |
479 | cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr); | |
480 | if (cmp_res < 0) | |
481 | return cmp_res; | |
482 | ||
483 | if (cmp_res & 1) { | |
484 | /* | |
485 | * This logical eraseblock is newer then the one | |
486 | * found earlier. | |
487 | */ | |
488 | err = validate_vid_hdr(vid_hdr, sv, pnum); | |
489 | if (err) | |
490 | return err; | |
491 | ||
492 | if (cmp_res & 4) | |
78d87c95 AB |
493 | err = add_to_list(si, seb->pnum, seb->ec, |
494 | &si->corr); | |
801c135c | 495 | else |
78d87c95 AB |
496 | err = add_to_list(si, seb->pnum, seb->ec, |
497 | &si->erase); | |
801c135c AB |
498 | if (err) |
499 | return err; | |
500 | ||
501 | seb->ec = ec; | |
502 | seb->pnum = pnum; | |
503 | seb->scrub = ((cmp_res & 2) || bitflips); | |
504 | seb->sqnum = sqnum; | |
505 | seb->leb_ver = leb_ver; | |
506 | ||
507 | if (sv->highest_lnum == lnum) | |
508 | sv->last_data_size = | |
3261ebd7 | 509 | be32_to_cpu(vid_hdr->data_size); |
801c135c AB |
510 | |
511 | return 0; | |
512 | } else { | |
513 | /* | |
514 | * This logical eraseblock is older then the one found | |
515 | * previously. | |
516 | */ | |
517 | if (cmp_res & 4) | |
78d87c95 | 518 | return add_to_list(si, pnum, ec, &si->corr); |
801c135c | 519 | else |
78d87c95 | 520 | return add_to_list(si, pnum, ec, &si->erase); |
801c135c AB |
521 | } |
522 | } | |
523 | ||
524 | /* | |
525 | * We've met this logical eraseblock for the first time, add it to the | |
526 | * scanning information. | |
527 | */ | |
528 | ||
529 | err = validate_vid_hdr(vid_hdr, sv, pnum); | |
530 | if (err) | |
531 | return err; | |
532 | ||
533 | seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); | |
534 | if (!seb) | |
535 | return -ENOMEM; | |
536 | ||
537 | seb->ec = ec; | |
538 | seb->pnum = pnum; | |
539 | seb->lnum = lnum; | |
540 | seb->sqnum = sqnum; | |
541 | seb->scrub = bitflips; | |
542 | seb->leb_ver = leb_ver; | |
543 | ||
544 | if (sv->highest_lnum <= lnum) { | |
545 | sv->highest_lnum = lnum; | |
3261ebd7 | 546 | sv->last_data_size = be32_to_cpu(vid_hdr->data_size); |
801c135c AB |
547 | } |
548 | ||
801c135c AB |
549 | sv->leb_count += 1; |
550 | rb_link_node(&seb->u.rb, parent, p); | |
551 | rb_insert_color(&seb->u.rb, &sv->root); | |
552 | return 0; | |
553 | } | |
554 | ||
555 | /** | |
ebaaf1af | 556 | * ubi_scan_find_sv - find volume in the scanning information. |
801c135c AB |
557 | * @si: scanning information |
558 | * @vol_id: the requested volume ID | |
559 | * | |
560 | * This function returns a pointer to the volume description or %NULL if there | |
561 | * are no data about this volume in the scanning information. | |
562 | */ | |
563 | struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si, | |
564 | int vol_id) | |
565 | { | |
566 | struct ubi_scan_volume *sv; | |
567 | struct rb_node *p = si->volumes.rb_node; | |
568 | ||
569 | while (p) { | |
570 | sv = rb_entry(p, struct ubi_scan_volume, rb); | |
571 | ||
572 | if (vol_id == sv->vol_id) | |
573 | return sv; | |
574 | ||
575 | if (vol_id > sv->vol_id) | |
576 | p = p->rb_left; | |
577 | else | |
578 | p = p->rb_right; | |
579 | } | |
580 | ||
581 | return NULL; | |
582 | } | |
583 | ||
584 | /** | |
ebaaf1af | 585 | * ubi_scan_find_seb - find LEB in the volume scanning information. |
801c135c AB |
586 | * @sv: a pointer to the volume scanning information |
587 | * @lnum: the requested logical eraseblock | |
588 | * | |
589 | * This function returns a pointer to the scanning logical eraseblock or %NULL | |
590 | * if there are no data about it in the scanning volume information. | |
591 | */ | |
592 | struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv, | |
593 | int lnum) | |
594 | { | |
595 | struct ubi_scan_leb *seb; | |
596 | struct rb_node *p = sv->root.rb_node; | |
597 | ||
598 | while (p) { | |
599 | seb = rb_entry(p, struct ubi_scan_leb, u.rb); | |
600 | ||
601 | if (lnum == seb->lnum) | |
602 | return seb; | |
603 | ||
604 | if (lnum > seb->lnum) | |
605 | p = p->rb_left; | |
606 | else | |
607 | p = p->rb_right; | |
608 | } | |
609 | ||
610 | return NULL; | |
611 | } | |
612 | ||
613 | /** | |
614 | * ubi_scan_rm_volume - delete scanning information about a volume. | |
615 | * @si: scanning information | |
616 | * @sv: the volume scanning information to delete | |
617 | */ | |
618 | void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv) | |
619 | { | |
620 | struct rb_node *rb; | |
621 | struct ubi_scan_leb *seb; | |
622 | ||
623 | dbg_bld("remove scanning information about volume %d", sv->vol_id); | |
624 | ||
625 | while ((rb = rb_first(&sv->root))) { | |
626 | seb = rb_entry(rb, struct ubi_scan_leb, u.rb); | |
627 | rb_erase(&seb->u.rb, &sv->root); | |
628 | list_add_tail(&seb->u.list, &si->erase); | |
629 | } | |
630 | ||
631 | rb_erase(&sv->rb, &si->volumes); | |
632 | kfree(sv); | |
633 | si->vols_found -= 1; | |
634 | } | |
635 | ||
636 | /** | |
637 | * ubi_scan_erase_peb - erase a physical eraseblock. | |
638 | * @ubi: UBI device description object | |
639 | * @si: scanning information | |
640 | * @pnum: physical eraseblock number to erase; | |
641 | * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown) | |
642 | * | |
643 | * This function erases physical eraseblock 'pnum', and writes the erase | |
644 | * counter header to it. This function should only be used on UBI device | |
85c6e6e2 AB |
645 | * initialization stages, when the EBA sub-system had not been yet initialized. |
646 | * This function returns zero in case of success and a negative error code in | |
647 | * case of failure. | |
801c135c | 648 | */ |
e88d6e10 AB |
649 | int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si, |
650 | int pnum, int ec) | |
801c135c AB |
651 | { |
652 | int err; | |
653 | struct ubi_ec_hdr *ec_hdr; | |
654 | ||
801c135c AB |
655 | if ((long long)ec >= UBI_MAX_ERASECOUNTER) { |
656 | /* | |
657 | * Erase counter overflow. Upgrade UBI and use 64-bit | |
658 | * erase counters internally. | |
659 | */ | |
660 | ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec); | |
661 | return -EINVAL; | |
662 | } | |
663 | ||
dcec4c3b FM |
664 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
665 | if (!ec_hdr) | |
666 | return -ENOMEM; | |
667 | ||
3261ebd7 | 668 | ec_hdr->ec = cpu_to_be64(ec); |
801c135c AB |
669 | |
670 | err = ubi_io_sync_erase(ubi, pnum, 0); | |
671 | if (err < 0) | |
672 | goto out_free; | |
673 | ||
674 | err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); | |
675 | ||
676 | out_free: | |
677 | kfree(ec_hdr); | |
678 | return err; | |
679 | } | |
680 | ||
681 | /** | |
682 | * ubi_scan_get_free_peb - get a free physical eraseblock. | |
683 | * @ubi: UBI device description object | |
684 | * @si: scanning information | |
685 | * | |
686 | * This function returns a free physical eraseblock. It is supposed to be | |
85c6e6e2 AB |
687 | * called on the UBI initialization stages when the wear-leveling sub-system is |
688 | * not initialized yet. This function picks a physical eraseblocks from one of | |
689 | * the lists, writes the EC header if it is needed, and removes it from the | |
690 | * list. | |
801c135c AB |
691 | * |
692 | * This function returns scanning physical eraseblock information in case of | |
693 | * success and an error code in case of failure. | |
694 | */ | |
e88d6e10 | 695 | struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi, |
801c135c AB |
696 | struct ubi_scan_info *si) |
697 | { | |
698 | int err = 0, i; | |
699 | struct ubi_scan_leb *seb; | |
700 | ||
701 | if (!list_empty(&si->free)) { | |
702 | seb = list_entry(si->free.next, struct ubi_scan_leb, u.list); | |
703 | list_del(&seb->u.list); | |
704 | dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec); | |
705 | return seb; | |
706 | } | |
707 | ||
708 | for (i = 0; i < 2; i++) { | |
709 | struct list_head *head; | |
710 | struct ubi_scan_leb *tmp_seb; | |
711 | ||
712 | if (i == 0) | |
713 | head = &si->erase; | |
714 | else | |
715 | head = &si->corr; | |
716 | ||
717 | /* | |
718 | * We try to erase the first physical eraseblock from the @head | |
719 | * list and pick it if we succeed, or try to erase the | |
720 | * next one if not. And so forth. We don't want to take care | |
721 | * about bad eraseblocks here - they'll be handled later. | |
722 | */ | |
723 | list_for_each_entry_safe(seb, tmp_seb, head, u.list) { | |
724 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) | |
725 | seb->ec = si->mean_ec; | |
726 | ||
727 | err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1); | |
728 | if (err) | |
729 | continue; | |
730 | ||
731 | seb->ec += 1; | |
732 | list_del(&seb->u.list); | |
733 | dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec); | |
734 | return seb; | |
735 | } | |
736 | } | |
737 | ||
738 | ubi_err("no eraseblocks found"); | |
739 | return ERR_PTR(-ENOSPC); | |
740 | } | |
741 | ||
742 | /** | |
ebaaf1af | 743 | * process_eb - read, check UBI headers, and add them to scanning information. |
801c135c AB |
744 | * @ubi: UBI device description object |
745 | * @si: scanning information | |
746 | * @pnum: the physical eraseblock number | |
747 | * | |
78d87c95 | 748 | * This function returns a zero if the physical eraseblock was successfully |
801c135c AB |
749 | * handled and a negative error code in case of failure. |
750 | */ | |
9c9ec147 AB |
751 | static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, |
752 | int pnum) | |
801c135c | 753 | { |
c18a8418 | 754 | long long uninitialized_var(ec); |
801c135c AB |
755 | int err, bitflips = 0, vol_id, ec_corr = 0; |
756 | ||
757 | dbg_bld("scan PEB %d", pnum); | |
758 | ||
759 | /* Skip bad physical eraseblocks */ | |
760 | err = ubi_io_is_bad(ubi, pnum); | |
761 | if (err < 0) | |
762 | return err; | |
763 | else if (err) { | |
764 | /* | |
85c6e6e2 AB |
765 | * FIXME: this is actually duty of the I/O sub-system to |
766 | * initialize this, but MTD does not provide enough | |
767 | * information. | |
801c135c AB |
768 | */ |
769 | si->bad_peb_count += 1; | |
770 | return 0; | |
771 | } | |
772 | ||
773 | err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); | |
774 | if (err < 0) | |
775 | return err; | |
776 | else if (err == UBI_IO_BITFLIPS) | |
777 | bitflips = 1; | |
778 | else if (err == UBI_IO_PEB_EMPTY) | |
78d87c95 | 779 | return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase); |
801c135c AB |
780 | else if (err == UBI_IO_BAD_EC_HDR) { |
781 | /* | |
782 | * We have to also look at the VID header, possibly it is not | |
783 | * corrupted. Set %bitflips flag in order to make this PEB be | |
784 | * moved and EC be re-created. | |
785 | */ | |
786 | ec_corr = 1; | |
787 | ec = UBI_SCAN_UNKNOWN_EC; | |
788 | bitflips = 1; | |
789 | } | |
790 | ||
791 | si->is_empty = 0; | |
792 | ||
793 | if (!ec_corr) { | |
794 | /* Make sure UBI version is OK */ | |
795 | if (ech->version != UBI_VERSION) { | |
796 | ubi_err("this UBI version is %d, image version is %d", | |
797 | UBI_VERSION, (int)ech->version); | |
798 | return -EINVAL; | |
799 | } | |
800 | ||
3261ebd7 | 801 | ec = be64_to_cpu(ech->ec); |
801c135c AB |
802 | if (ec > UBI_MAX_ERASECOUNTER) { |
803 | /* | |
804 | * Erase counter overflow. The EC headers have 64 bits | |
805 | * reserved, but we anyway make use of only 31 bit | |
806 | * values, as this seems to be enough for any existing | |
807 | * flash. Upgrade UBI and use 64-bit erase counters | |
808 | * internally. | |
809 | */ | |
810 | ubi_err("erase counter overflow, max is %d", | |
811 | UBI_MAX_ERASECOUNTER); | |
812 | ubi_dbg_dump_ec_hdr(ech); | |
813 | return -EINVAL; | |
814 | } | |
815 | } | |
816 | ||
817 | /* OK, we've done with the EC header, let's look at the VID header */ | |
818 | ||
819 | err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0); | |
820 | if (err < 0) | |
821 | return err; | |
822 | else if (err == UBI_IO_BITFLIPS) | |
823 | bitflips = 1; | |
824 | else if (err == UBI_IO_BAD_VID_HDR || | |
825 | (err == UBI_IO_PEB_FREE && ec_corr)) { | |
826 | /* VID header is corrupted */ | |
78d87c95 | 827 | err = add_to_list(si, pnum, ec, &si->corr); |
801c135c AB |
828 | if (err) |
829 | return err; | |
830 | goto adjust_mean_ec; | |
831 | } else if (err == UBI_IO_PEB_FREE) { | |
832 | /* No VID header - the physical eraseblock is free */ | |
78d87c95 | 833 | err = add_to_list(si, pnum, ec, &si->free); |
801c135c AB |
834 | if (err) |
835 | return err; | |
836 | goto adjust_mean_ec; | |
837 | } | |
838 | ||
3261ebd7 | 839 | vol_id = be32_to_cpu(vidh->vol_id); |
91f2d53c | 840 | if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) { |
3261ebd7 | 841 | int lnum = be32_to_cpu(vidh->lnum); |
801c135c AB |
842 | |
843 | /* Unsupported internal volume */ | |
844 | switch (vidh->compat) { | |
845 | case UBI_COMPAT_DELETE: | |
846 | ubi_msg("\"delete\" compatible internal volume %d:%d" | |
847 | " found, remove it", vol_id, lnum); | |
78d87c95 | 848 | err = add_to_list(si, pnum, ec, &si->corr); |
801c135c AB |
849 | if (err) |
850 | return err; | |
851 | break; | |
852 | ||
853 | case UBI_COMPAT_RO: | |
854 | ubi_msg("read-only compatible internal volume %d:%d" | |
855 | " found, switch to read-only mode", | |
856 | vol_id, lnum); | |
857 | ubi->ro_mode = 1; | |
858 | break; | |
859 | ||
860 | case UBI_COMPAT_PRESERVE: | |
861 | ubi_msg("\"preserve\" compatible internal volume %d:%d" | |
862 | " found", vol_id, lnum); | |
78d87c95 | 863 | err = add_to_list(si, pnum, ec, &si->alien); |
801c135c AB |
864 | if (err) |
865 | return err; | |
866 | si->alien_peb_count += 1; | |
867 | return 0; | |
868 | ||
869 | case UBI_COMPAT_REJECT: | |
870 | ubi_err("incompatible internal volume %d:%d found", | |
871 | vol_id, lnum); | |
872 | return -EINVAL; | |
873 | } | |
874 | } | |
875 | ||
876 | /* Both UBI headers seem to be fine */ | |
877 | err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips); | |
878 | if (err) | |
879 | return err; | |
880 | ||
881 | adjust_mean_ec: | |
882 | if (!ec_corr) { | |
4bc1dca4 AB |
883 | si->ec_sum += ec; |
884 | si->ec_count += 1; | |
801c135c AB |
885 | if (ec > si->max_ec) |
886 | si->max_ec = ec; | |
887 | if (ec < si->min_ec) | |
888 | si->min_ec = ec; | |
889 | } | |
890 | ||
891 | return 0; | |
892 | } | |
893 | ||
894 | /** | |
895 | * ubi_scan - scan an MTD device. | |
896 | * @ubi: UBI device description object | |
897 | * | |
898 | * This function does full scanning of an MTD device and returns complete | |
899 | * information about it. In case of failure, an error code is returned. | |
900 | */ | |
901 | struct ubi_scan_info *ubi_scan(struct ubi_device *ubi) | |
902 | { | |
903 | int err, pnum; | |
904 | struct rb_node *rb1, *rb2; | |
905 | struct ubi_scan_volume *sv; | |
906 | struct ubi_scan_leb *seb; | |
907 | struct ubi_scan_info *si; | |
908 | ||
909 | si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL); | |
910 | if (!si) | |
911 | return ERR_PTR(-ENOMEM); | |
912 | ||
913 | INIT_LIST_HEAD(&si->corr); | |
914 | INIT_LIST_HEAD(&si->free); | |
915 | INIT_LIST_HEAD(&si->erase); | |
916 | INIT_LIST_HEAD(&si->alien); | |
917 | si->volumes = RB_ROOT; | |
918 | si->is_empty = 1; | |
919 | ||
920 | err = -ENOMEM; | |
921 | ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); | |
922 | if (!ech) | |
923 | goto out_si; | |
924 | ||
33818bbb | 925 | vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); |
801c135c AB |
926 | if (!vidh) |
927 | goto out_ech; | |
928 | ||
929 | for (pnum = 0; pnum < ubi->peb_count; pnum++) { | |
930 | cond_resched(); | |
931 | ||
c8566350 | 932 | dbg_gen("process PEB %d", pnum); |
801c135c AB |
933 | err = process_eb(ubi, si, pnum); |
934 | if (err < 0) | |
935 | goto out_vidh; | |
936 | } | |
937 | ||
938 | dbg_msg("scanning is finished"); | |
939 | ||
4bc1dca4 AB |
940 | /* Calculate mean erase counter */ |
941 | if (si->ec_count) { | |
942 | do_div(si->ec_sum, si->ec_count); | |
943 | si->mean_ec = si->ec_sum; | |
944 | } | |
801c135c AB |
945 | |
946 | if (si->is_empty) | |
947 | ubi_msg("empty MTD device detected"); | |
948 | ||
949 | /* | |
950 | * In case of unknown erase counter we use the mean erase counter | |
951 | * value. | |
952 | */ | |
953 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { | |
954 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) | |
955 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) | |
956 | seb->ec = si->mean_ec; | |
957 | } | |
958 | ||
959 | list_for_each_entry(seb, &si->free, u.list) { | |
960 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) | |
961 | seb->ec = si->mean_ec; | |
962 | } | |
963 | ||
964 | list_for_each_entry(seb, &si->corr, u.list) | |
965 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) | |
966 | seb->ec = si->mean_ec; | |
967 | ||
968 | list_for_each_entry(seb, &si->erase, u.list) | |
969 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) | |
970 | seb->ec = si->mean_ec; | |
971 | ||
972 | err = paranoid_check_si(ubi, si); | |
973 | if (err) { | |
974 | if (err > 0) | |
975 | err = -EINVAL; | |
976 | goto out_vidh; | |
977 | } | |
978 | ||
979 | ubi_free_vid_hdr(ubi, vidh); | |
980 | kfree(ech); | |
981 | ||
982 | return si; | |
983 | ||
984 | out_vidh: | |
985 | ubi_free_vid_hdr(ubi, vidh); | |
986 | out_ech: | |
987 | kfree(ech); | |
988 | out_si: | |
989 | ubi_scan_destroy_si(si); | |
990 | return ERR_PTR(err); | |
991 | } | |
992 | ||
993 | /** | |
994 | * destroy_sv - free the scanning volume information | |
995 | * @sv: scanning volume information | |
996 | * | |
997 | * This function destroys the volume RB-tree (@sv->root) and the scanning | |
998 | * volume information. | |
999 | */ | |
1000 | static void destroy_sv(struct ubi_scan_volume *sv) | |
1001 | { | |
1002 | struct ubi_scan_leb *seb; | |
1003 | struct rb_node *this = sv->root.rb_node; | |
1004 | ||
1005 | while (this) { | |
1006 | if (this->rb_left) | |
1007 | this = this->rb_left; | |
1008 | else if (this->rb_right) | |
1009 | this = this->rb_right; | |
1010 | else { | |
1011 | seb = rb_entry(this, struct ubi_scan_leb, u.rb); | |
1012 | this = rb_parent(this); | |
1013 | if (this) { | |
1014 | if (this->rb_left == &seb->u.rb) | |
1015 | this->rb_left = NULL; | |
1016 | else | |
1017 | this->rb_right = NULL; | |
1018 | } | |
1019 | ||
1020 | kfree(seb); | |
1021 | } | |
1022 | } | |
1023 | kfree(sv); | |
1024 | } | |
1025 | ||
1026 | /** | |
1027 | * ubi_scan_destroy_si - destroy scanning information. | |
1028 | * @si: scanning information | |
1029 | */ | |
1030 | void ubi_scan_destroy_si(struct ubi_scan_info *si) | |
1031 | { | |
1032 | struct ubi_scan_leb *seb, *seb_tmp; | |
1033 | struct ubi_scan_volume *sv; | |
1034 | struct rb_node *rb; | |
1035 | ||
1036 | list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) { | |
1037 | list_del(&seb->u.list); | |
1038 | kfree(seb); | |
1039 | } | |
1040 | list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) { | |
1041 | list_del(&seb->u.list); | |
1042 | kfree(seb); | |
1043 | } | |
1044 | list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) { | |
1045 | list_del(&seb->u.list); | |
1046 | kfree(seb); | |
1047 | } | |
1048 | list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) { | |
1049 | list_del(&seb->u.list); | |
1050 | kfree(seb); | |
1051 | } | |
1052 | ||
1053 | /* Destroy the volume RB-tree */ | |
1054 | rb = si->volumes.rb_node; | |
1055 | while (rb) { | |
1056 | if (rb->rb_left) | |
1057 | rb = rb->rb_left; | |
1058 | else if (rb->rb_right) | |
1059 | rb = rb->rb_right; | |
1060 | else { | |
1061 | sv = rb_entry(rb, struct ubi_scan_volume, rb); | |
1062 | ||
1063 | rb = rb_parent(rb); | |
1064 | if (rb) { | |
1065 | if (rb->rb_left == &sv->rb) | |
1066 | rb->rb_left = NULL; | |
1067 | else | |
1068 | rb->rb_right = NULL; | |
1069 | } | |
1070 | ||
1071 | destroy_sv(sv); | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | kfree(si); | |
1076 | } | |
1077 | ||
1078 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | |
1079 | ||
1080 | /** | |
ebaaf1af | 1081 | * paranoid_check_si - check the scanning information. |
801c135c AB |
1082 | * @ubi: UBI device description object |
1083 | * @si: scanning information | |
1084 | * | |
1085 | * This function returns zero if the scanning information is all right, %1 if | |
1086 | * not and a negative error code if an error occurred. | |
1087 | */ | |
e88d6e10 | 1088 | static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si) |
801c135c AB |
1089 | { |
1090 | int pnum, err, vols_found = 0; | |
1091 | struct rb_node *rb1, *rb2; | |
1092 | struct ubi_scan_volume *sv; | |
1093 | struct ubi_scan_leb *seb, *last_seb; | |
1094 | uint8_t *buf; | |
1095 | ||
1096 | /* | |
78d87c95 | 1097 | * At first, check that scanning information is OK. |
801c135c AB |
1098 | */ |
1099 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { | |
1100 | int leb_count = 0; | |
1101 | ||
1102 | cond_resched(); | |
1103 | ||
1104 | vols_found += 1; | |
1105 | ||
1106 | if (si->is_empty) { | |
1107 | ubi_err("bad is_empty flag"); | |
1108 | goto bad_sv; | |
1109 | } | |
1110 | ||
1111 | if (sv->vol_id < 0 || sv->highest_lnum < 0 || | |
1112 | sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 || | |
1113 | sv->data_pad < 0 || sv->last_data_size < 0) { | |
1114 | ubi_err("negative values"); | |
1115 | goto bad_sv; | |
1116 | } | |
1117 | ||
1118 | if (sv->vol_id >= UBI_MAX_VOLUMES && | |
1119 | sv->vol_id < UBI_INTERNAL_VOL_START) { | |
1120 | ubi_err("bad vol_id"); | |
1121 | goto bad_sv; | |
1122 | } | |
1123 | ||
1124 | if (sv->vol_id > si->highest_vol_id) { | |
1125 | ubi_err("highest_vol_id is %d, but vol_id %d is there", | |
1126 | si->highest_vol_id, sv->vol_id); | |
1127 | goto out; | |
1128 | } | |
1129 | ||
1130 | if (sv->vol_type != UBI_DYNAMIC_VOLUME && | |
1131 | sv->vol_type != UBI_STATIC_VOLUME) { | |
1132 | ubi_err("bad vol_type"); | |
1133 | goto bad_sv; | |
1134 | } | |
1135 | ||
1136 | if (sv->data_pad > ubi->leb_size / 2) { | |
1137 | ubi_err("bad data_pad"); | |
1138 | goto bad_sv; | |
1139 | } | |
1140 | ||
1141 | last_seb = NULL; | |
1142 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { | |
1143 | cond_resched(); | |
1144 | ||
1145 | last_seb = seb; | |
1146 | leb_count += 1; | |
1147 | ||
1148 | if (seb->pnum < 0 || seb->ec < 0) { | |
1149 | ubi_err("negative values"); | |
1150 | goto bad_seb; | |
1151 | } | |
1152 | ||
1153 | if (seb->ec < si->min_ec) { | |
1154 | ubi_err("bad si->min_ec (%d), %d found", | |
1155 | si->min_ec, seb->ec); | |
1156 | goto bad_seb; | |
1157 | } | |
1158 | ||
1159 | if (seb->ec > si->max_ec) { | |
1160 | ubi_err("bad si->max_ec (%d), %d found", | |
1161 | si->max_ec, seb->ec); | |
1162 | goto bad_seb; | |
1163 | } | |
1164 | ||
1165 | if (seb->pnum >= ubi->peb_count) { | |
1166 | ubi_err("too high PEB number %d, total PEBs %d", | |
1167 | seb->pnum, ubi->peb_count); | |
1168 | goto bad_seb; | |
1169 | } | |
1170 | ||
1171 | if (sv->vol_type == UBI_STATIC_VOLUME) { | |
1172 | if (seb->lnum >= sv->used_ebs) { | |
1173 | ubi_err("bad lnum or used_ebs"); | |
1174 | goto bad_seb; | |
1175 | } | |
1176 | } else { | |
1177 | if (sv->used_ebs != 0) { | |
1178 | ubi_err("non-zero used_ebs"); | |
1179 | goto bad_seb; | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | if (seb->lnum > sv->highest_lnum) { | |
1184 | ubi_err("incorrect highest_lnum or lnum"); | |
1185 | goto bad_seb; | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | if (sv->leb_count != leb_count) { | |
1190 | ubi_err("bad leb_count, %d objects in the tree", | |
1191 | leb_count); | |
1192 | goto bad_sv; | |
1193 | } | |
1194 | ||
1195 | if (!last_seb) | |
1196 | continue; | |
1197 | ||
1198 | seb = last_seb; | |
1199 | ||
1200 | if (seb->lnum != sv->highest_lnum) { | |
1201 | ubi_err("bad highest_lnum"); | |
1202 | goto bad_seb; | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | if (vols_found != si->vols_found) { | |
1207 | ubi_err("bad si->vols_found %d, should be %d", | |
1208 | si->vols_found, vols_found); | |
1209 | goto out; | |
1210 | } | |
1211 | ||
1212 | /* Check that scanning information is correct */ | |
1213 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { | |
1214 | last_seb = NULL; | |
1215 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { | |
1216 | int vol_type; | |
1217 | ||
1218 | cond_resched(); | |
1219 | ||
1220 | last_seb = seb; | |
1221 | ||
1222 | err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1); | |
1223 | if (err && err != UBI_IO_BITFLIPS) { | |
1224 | ubi_err("VID header is not OK (%d)", err); | |
1225 | if (err > 0) | |
1226 | err = -EIO; | |
1227 | return err; | |
1228 | } | |
1229 | ||
1230 | vol_type = vidh->vol_type == UBI_VID_DYNAMIC ? | |
1231 | UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; | |
1232 | if (sv->vol_type != vol_type) { | |
1233 | ubi_err("bad vol_type"); | |
1234 | goto bad_vid_hdr; | |
1235 | } | |
1236 | ||
3261ebd7 | 1237 | if (seb->sqnum != be64_to_cpu(vidh->sqnum)) { |
801c135c AB |
1238 | ubi_err("bad sqnum %llu", seb->sqnum); |
1239 | goto bad_vid_hdr; | |
1240 | } | |
1241 | ||
3261ebd7 | 1242 | if (sv->vol_id != be32_to_cpu(vidh->vol_id)) { |
801c135c AB |
1243 | ubi_err("bad vol_id %d", sv->vol_id); |
1244 | goto bad_vid_hdr; | |
1245 | } | |
1246 | ||
1247 | if (sv->compat != vidh->compat) { | |
1248 | ubi_err("bad compat %d", vidh->compat); | |
1249 | goto bad_vid_hdr; | |
1250 | } | |
1251 | ||
3261ebd7 | 1252 | if (seb->lnum != be32_to_cpu(vidh->lnum)) { |
801c135c AB |
1253 | ubi_err("bad lnum %d", seb->lnum); |
1254 | goto bad_vid_hdr; | |
1255 | } | |
1256 | ||
3261ebd7 | 1257 | if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) { |
801c135c AB |
1258 | ubi_err("bad used_ebs %d", sv->used_ebs); |
1259 | goto bad_vid_hdr; | |
1260 | } | |
1261 | ||
3261ebd7 | 1262 | if (sv->data_pad != be32_to_cpu(vidh->data_pad)) { |
801c135c AB |
1263 | ubi_err("bad data_pad %d", sv->data_pad); |
1264 | goto bad_vid_hdr; | |
1265 | } | |
1266 | ||
3261ebd7 | 1267 | if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) { |
801c135c AB |
1268 | ubi_err("bad leb_ver %u", seb->leb_ver); |
1269 | goto bad_vid_hdr; | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | if (!last_seb) | |
1274 | continue; | |
1275 | ||
3261ebd7 | 1276 | if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) { |
801c135c AB |
1277 | ubi_err("bad highest_lnum %d", sv->highest_lnum); |
1278 | goto bad_vid_hdr; | |
1279 | } | |
1280 | ||
3261ebd7 | 1281 | if (sv->last_data_size != be32_to_cpu(vidh->data_size)) { |
801c135c AB |
1282 | ubi_err("bad last_data_size %d", sv->last_data_size); |
1283 | goto bad_vid_hdr; | |
1284 | } | |
1285 | } | |
1286 | ||
1287 | /* | |
1288 | * Make sure that all the physical eraseblocks are in one of the lists | |
1289 | * or trees. | |
1290 | */ | |
d9b0744d | 1291 | buf = kzalloc(ubi->peb_count, GFP_KERNEL); |
801c135c AB |
1292 | if (!buf) |
1293 | return -ENOMEM; | |
1294 | ||
801c135c AB |
1295 | for (pnum = 0; pnum < ubi->peb_count; pnum++) { |
1296 | err = ubi_io_is_bad(ubi, pnum); | |
341e1a0c AB |
1297 | if (err < 0) { |
1298 | kfree(buf); | |
801c135c | 1299 | return err; |
9c9ec147 | 1300 | } else if (err) |
d9b0744d | 1301 | buf[pnum] = 1; |
801c135c AB |
1302 | } |
1303 | ||
1304 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) | |
1305 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) | |
d9b0744d | 1306 | buf[seb->pnum] = 1; |
801c135c AB |
1307 | |
1308 | list_for_each_entry(seb, &si->free, u.list) | |
d9b0744d | 1309 | buf[seb->pnum] = 1; |
801c135c AB |
1310 | |
1311 | list_for_each_entry(seb, &si->corr, u.list) | |
d9b0744d | 1312 | buf[seb->pnum] = 1; |
801c135c AB |
1313 | |
1314 | list_for_each_entry(seb, &si->erase, u.list) | |
d9b0744d | 1315 | buf[seb->pnum] = 1; |
801c135c AB |
1316 | |
1317 | list_for_each_entry(seb, &si->alien, u.list) | |
d9b0744d | 1318 | buf[seb->pnum] = 1; |
801c135c AB |
1319 | |
1320 | err = 0; | |
1321 | for (pnum = 0; pnum < ubi->peb_count; pnum++) | |
d9b0744d | 1322 | if (!buf[pnum]) { |
801c135c AB |
1323 | ubi_err("PEB %d is not referred", pnum); |
1324 | err = 1; | |
1325 | } | |
1326 | ||
1327 | kfree(buf); | |
1328 | if (err) | |
1329 | goto out; | |
1330 | return 0; | |
1331 | ||
1332 | bad_seb: | |
1333 | ubi_err("bad scanning information about LEB %d", seb->lnum); | |
1334 | ubi_dbg_dump_seb(seb, 0); | |
1335 | ubi_dbg_dump_sv(sv); | |
1336 | goto out; | |
1337 | ||
1338 | bad_sv: | |
1339 | ubi_err("bad scanning information about volume %d", sv->vol_id); | |
1340 | ubi_dbg_dump_sv(sv); | |
1341 | goto out; | |
1342 | ||
1343 | bad_vid_hdr: | |
1344 | ubi_err("bad scanning information about volume %d", sv->vol_id); | |
1345 | ubi_dbg_dump_sv(sv); | |
1346 | ubi_dbg_dump_vid_hdr(vidh); | |
1347 | ||
1348 | out: | |
1349 | ubi_dbg_dump_stack(); | |
1350 | return 1; | |
1351 | } | |
1352 | ||
1353 | #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ |