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