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