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