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