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