Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2006 |
| 3 | * Copyright (c) Nokia Corporation, 2006, 2007 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | * Author: Artem Bityutskiy (Битюцкий Артём) |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * This file includes volume table manipulation code. The volume table is an |
| 24 | * on-flash table containing volume meta-data like name, number of reserved |
| 25 | * physical eraseblocks, type, etc. The volume table is stored in the so-called |
| 26 | * "layout volume". |
| 27 | * |
| 28 | * The layout volume is an internal volume which is organized as follows. It |
| 29 | * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical |
| 30 | * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each |
| 31 | * other. This redundancy guarantees robustness to unclean reboots. The volume |
| 32 | * table is basically an array of volume table records. Each record contains |
| 33 | * full information about the volume and protected by a CRC checksum. |
| 34 | * |
| 35 | * The volume table is changed, it is first changed in RAM. Then LEB 0 is |
| 36 | * erased, and the updated volume table is written back to LEB 0. Then same for |
| 37 | * LEB 1. This scheme guarantees recoverability from unclean reboots. |
| 38 | * |
| 39 | * In this UBI implementation the on-flash volume table does not contain any |
| 40 | * information about how many data static volumes contain. This information may |
| 41 | * be found from the scanning data. |
| 42 | * |
| 43 | * But it would still be beneficial to store this information in the volume |
| 44 | * table. For example, suppose we have a static volume X, and all its physical |
| 45 | * eraseblocks became bad for some reasons. Suppose we are attaching the |
| 46 | * corresponding MTD device, the scanning has found no logical eraseblocks |
| 47 | * corresponding to the volume X. According to the volume table volume X does |
| 48 | * exist. So we don't know whether it is just empty or all its physical |
| 49 | * eraseblocks went bad. So we cannot alarm the user about this corruption. |
| 50 | * |
| 51 | * The volume table also stores so-called "update marker", which is used for |
| 52 | * volume updates. Before updating the volume, the update marker is set, and |
| 53 | * after the update operation is finished, the update marker is cleared. So if |
| 54 | * the update operation was interrupted (e.g. by an unclean reboot) - the |
| 55 | * update marker is still there and we know that the volume's contents is |
| 56 | * damaged. |
| 57 | */ |
| 58 | |
| 59 | #include <linux/crc32.h> |
| 60 | #include <linux/err.h> |
| 61 | #include <asm/div64.h> |
| 62 | #include "ubi.h" |
| 63 | |
| 64 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID |
| 65 | static void paranoid_vtbl_check(const struct ubi_device *ubi); |
| 66 | #else |
| 67 | #define paranoid_vtbl_check(ubi) |
| 68 | #endif |
| 69 | |
| 70 | /* Empty volume table record */ |
| 71 | static struct ubi_vtbl_record empty_vtbl_record; |
| 72 | |
| 73 | /** |
| 74 | * ubi_change_vtbl_record - change volume table record. |
| 75 | * @ubi: UBI device description object |
| 76 | * @idx: table index to change |
| 77 | * @vtbl_rec: new volume table record |
| 78 | * |
| 79 | * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty |
| 80 | * volume table record is written. The caller does not have to calculate CRC of |
| 81 | * the record as it is done by this function. Returns zero in case of success |
| 82 | * and a negative error code in case of failure. |
| 83 | */ |
| 84 | int ubi_change_vtbl_record(struct ubi_device *ubi, int idx, |
| 85 | struct ubi_vtbl_record *vtbl_rec) |
| 86 | { |
| 87 | int i, err; |
| 88 | uint32_t crc; |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 89 | struct ubi_volume *layout_vol; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 90 | |
| 91 | ubi_assert(idx >= 0 && idx < ubi->vtbl_slots); |
Artem Bityutskiy | 91f2d53 | 2008-01-24 11:23:23 +0200 | [diff] [blame] | 92 | layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)]; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 93 | |
| 94 | if (!vtbl_rec) |
| 95 | vtbl_rec = &empty_vtbl_record; |
| 96 | else { |
| 97 | crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 98 | vtbl_rec->crc = cpu_to_be32(crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 99 | } |
| 100 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 101 | memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record)); |
| 102 | for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) { |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 103 | err = ubi_eba_unmap_leb(ubi, layout_vol, i); |
Artem Bityutskiy | cae0a77 | 2007-12-17 12:46:48 +0200 | [diff] [blame] | 104 | if (err) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 105 | return err; |
Artem Bityutskiy | cae0a77 | 2007-12-17 12:46:48 +0200 | [diff] [blame] | 106 | |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 107 | err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 108 | ubi->vtbl_size, UBI_LONGTERM); |
Artem Bityutskiy | cae0a77 | 2007-12-17 12:46:48 +0200 | [diff] [blame] | 109 | if (err) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 110 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | paranoid_vtbl_check(ubi); |
Artem Bityutskiy | 6dc4a87 | 2008-02-01 13:48:49 +0200 | [diff] [blame] | 114 | return 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Artem Bityutskiy | f40ac9c | 2008-07-13 21:47:47 +0300 | [diff] [blame] | 118 | * ubi_vtbl_rename_volumes - rename UBI volumes in the volume table. |
| 119 | * @ubi: UBI device description object |
Artem Bityutskiy | ebaaf1a | 2008-07-18 13:34:32 +0300 | [diff] [blame] | 120 | * @rename_list: list of &struct ubi_rename_entry objects |
Artem Bityutskiy | f40ac9c | 2008-07-13 21:47:47 +0300 | [diff] [blame] | 121 | * |
| 122 | * This function re-names multiple volumes specified in @req in the volume |
| 123 | * table. Returns zero in case of success and a negative error code in case of |
| 124 | * failure. |
| 125 | */ |
| 126 | int ubi_vtbl_rename_volumes(struct ubi_device *ubi, |
| 127 | struct list_head *rename_list) |
| 128 | { |
| 129 | int i, err; |
| 130 | struct ubi_rename_entry *re; |
| 131 | struct ubi_volume *layout_vol; |
| 132 | |
| 133 | list_for_each_entry(re, rename_list, list) { |
| 134 | uint32_t crc; |
| 135 | struct ubi_volume *vol = re->desc->vol; |
| 136 | struct ubi_vtbl_record *vtbl_rec = &ubi->vtbl[vol->vol_id]; |
| 137 | |
| 138 | if (re->remove) { |
| 139 | memcpy(vtbl_rec, &empty_vtbl_record, |
| 140 | sizeof(struct ubi_vtbl_record)); |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | vtbl_rec->name_len = cpu_to_be16(re->new_name_len); |
| 145 | memcpy(vtbl_rec->name, re->new_name, re->new_name_len); |
| 146 | memset(vtbl_rec->name + re->new_name_len, 0, |
| 147 | UBI_VOL_NAME_MAX + 1 - re->new_name_len); |
| 148 | crc = crc32(UBI_CRC32_INIT, vtbl_rec, |
| 149 | UBI_VTBL_RECORD_SIZE_CRC); |
| 150 | vtbl_rec->crc = cpu_to_be32(crc); |
| 151 | } |
| 152 | |
| 153 | layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)]; |
| 154 | for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) { |
| 155 | err = ubi_eba_unmap_leb(ubi, layout_vol, i); |
| 156 | if (err) |
| 157 | return err; |
| 158 | |
| 159 | err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0, |
| 160 | ubi->vtbl_size, UBI_LONGTERM); |
| 161 | if (err) |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /** |
Artem Bityutskiy | ebaaf1a | 2008-07-18 13:34:32 +0300 | [diff] [blame] | 169 | * vtbl_check - check if volume table is not corrupted and sensible. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 170 | * @ubi: UBI device description object |
| 171 | * @vtbl: volume table |
| 172 | * |
| 173 | * This function returns zero if @vtbl is all right, %1 if CRC is incorrect, |
| 174 | * and %-EINVAL if it contains inconsistent data. |
| 175 | */ |
| 176 | static int vtbl_check(const struct ubi_device *ubi, |
| 177 | const struct ubi_vtbl_record *vtbl) |
| 178 | { |
| 179 | int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len; |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 180 | int upd_marker, err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 181 | uint32_t crc; |
| 182 | const char *name; |
| 183 | |
| 184 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 185 | cond_resched(); |
| 186 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 187 | reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs); |
| 188 | alignment = be32_to_cpu(vtbl[i].alignment); |
| 189 | data_pad = be32_to_cpu(vtbl[i].data_pad); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 190 | upd_marker = vtbl[i].upd_marker; |
| 191 | vol_type = vtbl[i].vol_type; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 192 | name_len = be16_to_cpu(vtbl[i].name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 193 | name = &vtbl[i].name[0]; |
| 194 | |
| 195 | crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 196 | if (be32_to_cpu(vtbl[i].crc) != crc) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 197 | ubi_err("bad CRC at record %u: %#08x, not %#08x", |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 198 | i, crc, be32_to_cpu(vtbl[i].crc)); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 199 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | if (reserved_pebs == 0) { |
| 204 | if (memcmp(&vtbl[i], &empty_vtbl_record, |
| 205 | UBI_VTBL_RECORD_SIZE)) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 206 | err = 2; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 207 | goto bad; |
| 208 | } |
| 209 | continue; |
| 210 | } |
| 211 | |
| 212 | if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 || |
| 213 | name_len < 0) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 214 | err = 3; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 215 | goto bad; |
| 216 | } |
| 217 | |
| 218 | if (alignment > ubi->leb_size || alignment == 0) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 219 | err = 4; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 220 | goto bad; |
| 221 | } |
| 222 | |
Kyungmin Park | cadb40c | 2008-05-22 10:32:18 +0900 | [diff] [blame] | 223 | n = alignment & (ubi->min_io_size - 1); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 224 | if (alignment != 1 && n) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 225 | err = 5; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 226 | goto bad; |
| 227 | } |
| 228 | |
| 229 | n = ubi->leb_size % alignment; |
| 230 | if (data_pad != n) { |
| 231 | dbg_err("bad data_pad, has to be %d", n); |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 232 | err = 6; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 233 | goto bad; |
| 234 | } |
| 235 | |
| 236 | if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 237 | err = 7; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 238 | goto bad; |
| 239 | } |
| 240 | |
| 241 | if (upd_marker != 0 && upd_marker != 1) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 242 | err = 8; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 243 | goto bad; |
| 244 | } |
| 245 | |
| 246 | if (reserved_pebs > ubi->good_peb_count) { |
Deepak Saxena | 762a9f2 | 2008-10-08 12:56:24 -0700 | [diff] [blame] | 247 | dbg_err("too large reserved_pebs %d, good PEBs %d", |
| 248 | reserved_pebs, ubi->good_peb_count); |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 249 | err = 9; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 250 | goto bad; |
| 251 | } |
| 252 | |
| 253 | if (name_len > UBI_VOL_NAME_MAX) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 254 | err = 10; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 255 | goto bad; |
| 256 | } |
| 257 | |
| 258 | if (name[0] == '\0') { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 259 | err = 11; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 260 | goto bad; |
| 261 | } |
| 262 | |
| 263 | if (name_len != strnlen(name, name_len + 1)) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 264 | err = 12; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 265 | goto bad; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /* Checks that all names are unique */ |
| 270 | for (i = 0; i < ubi->vtbl_slots - 1; i++) { |
| 271 | for (n = i + 1; n < ubi->vtbl_slots; n++) { |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 272 | int len1 = be16_to_cpu(vtbl[i].name_len); |
| 273 | int len2 = be16_to_cpu(vtbl[n].name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 274 | |
| 275 | if (len1 > 0 && len1 == len2 && |
| 276 | !strncmp(vtbl[i].name, vtbl[n].name, len1)) { |
| 277 | ubi_err("volumes %d and %d have the same name" |
| 278 | " \"%s\"", i, n, vtbl[i].name); |
| 279 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); |
| 280 | ubi_dbg_dump_vtbl_record(&vtbl[n], n); |
| 281 | return -EINVAL; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | |
| 288 | bad: |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 289 | ubi_err("volume table check failed: record %d, error %d", i, err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 290 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * create_vtbl - create a copy of volume table. |
| 296 | * @ubi: UBI device description object |
| 297 | * @si: scanning information |
| 298 | * @copy: number of the volume table copy |
| 299 | * @vtbl: contents of the volume table |
| 300 | * |
| 301 | * This function returns zero in case of success and a negative error code in |
| 302 | * case of failure. |
| 303 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 304 | static int create_vtbl(struct ubi_device *ubi, struct ubi_scan_info *si, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 305 | int copy, void *vtbl) |
| 306 | { |
| 307 | int err, tries = 0; |
| 308 | static struct ubi_vid_hdr *vid_hdr; |
| 309 | struct ubi_scan_volume *sv; |
| 310 | struct ubi_scan_leb *new_seb, *old_seb = NULL; |
| 311 | |
| 312 | ubi_msg("create volume table (copy #%d)", copy + 1); |
| 313 | |
Artem Bityutskiy | 33818bb | 2007-08-28 21:29:32 +0300 | [diff] [blame] | 314 | vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 315 | if (!vid_hdr) |
| 316 | return -ENOMEM; |
| 317 | |
| 318 | /* |
| 319 | * Check if there is a logical eraseblock which would have to contain |
| 320 | * this volume table copy was found during scanning. It has to be wiped |
| 321 | * out. |
| 322 | */ |
Artem Bityutskiy | 91f2d53 | 2008-01-24 11:23:23 +0200 | [diff] [blame] | 323 | sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 324 | if (sv) |
| 325 | old_seb = ubi_scan_find_seb(sv, copy); |
| 326 | |
| 327 | retry: |
| 328 | new_seb = ubi_scan_get_free_peb(ubi, si); |
| 329 | if (IS_ERR(new_seb)) { |
| 330 | err = PTR_ERR(new_seb); |
| 331 | goto out_free; |
| 332 | } |
| 333 | |
| 334 | vid_hdr->vol_type = UBI_VID_DYNAMIC; |
Artem Bityutskiy | 91f2d53 | 2008-01-24 11:23:23 +0200 | [diff] [blame] | 335 | vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 336 | vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT; |
| 337 | vid_hdr->data_size = vid_hdr->used_ebs = |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 338 | vid_hdr->data_pad = cpu_to_be32(0); |
| 339 | vid_hdr->lnum = cpu_to_be32(copy); |
| 340 | vid_hdr->sqnum = cpu_to_be64(++si->max_sqnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 341 | |
| 342 | /* The EC header is already there, write the VID header */ |
| 343 | err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr); |
| 344 | if (err) |
| 345 | goto write_error; |
| 346 | |
| 347 | /* Write the layout volume contents */ |
| 348 | err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size); |
| 349 | if (err) |
| 350 | goto write_error; |
| 351 | |
| 352 | /* |
| 353 | * And add it to the scanning information. Don't delete the old |
| 354 | * @old_seb as it will be deleted and freed in 'ubi_scan_add_used()'. |
| 355 | */ |
| 356 | err = ubi_scan_add_used(ubi, si, new_seb->pnum, new_seb->ec, |
| 357 | vid_hdr, 0); |
| 358 | kfree(new_seb); |
| 359 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 360 | return err; |
| 361 | |
| 362 | write_error: |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 363 | if (err == -EIO && ++tries <= 5) { |
| 364 | /* |
| 365 | * Probably this physical eraseblock went bad, try to pick |
| 366 | * another one. |
| 367 | */ |
| 368 | list_add_tail(&new_seb->u.list, &si->corr); |
Florin Malita | c4e90ec | 2007-05-03 11:49:57 -0400 | [diff] [blame] | 369 | goto retry; |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 370 | } |
| 371 | kfree(new_seb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 372 | out_free: |
| 373 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 374 | return err; |
| 375 | |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * process_lvol - process the layout volume. |
| 380 | * @ubi: UBI device description object |
| 381 | * @si: scanning information |
| 382 | * @sv: layout volume scanning information |
| 383 | * |
| 384 | * This function is responsible for reading the layout volume, ensuring it is |
| 385 | * not corrupted, and recovering from corruptions if needed. Returns volume |
| 386 | * table in case of success and a negative error code in case of failure. |
| 387 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 388 | static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 389 | struct ubi_scan_info *si, |
| 390 | struct ubi_scan_volume *sv) |
| 391 | { |
| 392 | int err; |
| 393 | struct rb_node *rb; |
| 394 | struct ubi_scan_leb *seb; |
| 395 | struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL }; |
| 396 | int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1}; |
| 397 | |
| 398 | /* |
| 399 | * UBI goes through the following steps when it changes the layout |
| 400 | * volume: |
| 401 | * a. erase LEB 0; |
| 402 | * b. write new data to LEB 0; |
| 403 | * c. erase LEB 1; |
| 404 | * d. write new data to LEB 1. |
| 405 | * |
| 406 | * Before the change, both LEBs contain the same data. |
| 407 | * |
| 408 | * Due to unclean reboots, the contents of LEB 0 may be lost, but there |
| 409 | * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not. |
| 410 | * Similarly, LEB 1 may be lost, but there should be LEB 0. And |
| 411 | * finally, unclean reboots may result in a situation when neither LEB |
| 412 | * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB |
| 413 | * 0 contains more recent information. |
| 414 | * |
| 415 | * So the plan is to first check LEB 0. Then |
| 416 | * a. if LEB 0 is OK, it must be containing the most resent data; then |
| 417 | * we compare it with LEB 1, and if they are different, we copy LEB |
| 418 | * 0 to LEB 1; |
| 419 | * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1 |
| 420 | * to LEB 0. |
| 421 | */ |
| 422 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 423 | dbg_gen("check layout volume"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 424 | |
| 425 | /* Read both LEB 0 and LEB 1 into memory */ |
| 426 | ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) { |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 427 | leb[seb->lnum] = vmalloc(ubi->vtbl_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 428 | if (!leb[seb->lnum]) { |
| 429 | err = -ENOMEM; |
| 430 | goto out_free; |
| 431 | } |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 432 | memset(leb[seb->lnum], 0, ubi->vtbl_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 433 | |
| 434 | err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0, |
| 435 | ubi->vtbl_size); |
| 436 | if (err == UBI_IO_BITFLIPS || err == -EBADMSG) |
Artem Bityutskiy | beeea63 | 2008-05-20 09:54:02 +0300 | [diff] [blame] | 437 | /* |
| 438 | * Scrub the PEB later. Note, -EBADMSG indicates an |
| 439 | * uncorrectable ECC error, but we have our own CRC and |
| 440 | * the data will be checked later. If the data is OK, |
| 441 | * the PEB will be scrubbed (because we set |
| 442 | * seb->scrub). If the data is not OK, the contents of |
| 443 | * the PEB will be recovered from the second copy, and |
| 444 | * seb->scrub will be cleared in |
| 445 | * 'ubi_scan_add_used()'. |
| 446 | */ |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 447 | seb->scrub = 1; |
| 448 | else if (err) |
| 449 | goto out_free; |
| 450 | } |
| 451 | |
| 452 | err = -EINVAL; |
| 453 | if (leb[0]) { |
| 454 | leb_corrupted[0] = vtbl_check(ubi, leb[0]); |
| 455 | if (leb_corrupted[0] < 0) |
| 456 | goto out_free; |
| 457 | } |
| 458 | |
| 459 | if (!leb_corrupted[0]) { |
| 460 | /* LEB 0 is OK */ |
| 461 | if (leb[1]) |
Artem Bityutskiy | 9c9ec14 | 2008-07-18 13:19:52 +0300 | [diff] [blame] | 462 | leb_corrupted[1] = memcmp(leb[0], leb[1], |
| 463 | ubi->vtbl_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 464 | if (leb_corrupted[1]) { |
| 465 | ubi_warn("volume table copy #2 is corrupted"); |
| 466 | err = create_vtbl(ubi, si, 1, leb[0]); |
| 467 | if (err) |
| 468 | goto out_free; |
| 469 | ubi_msg("volume table was restored"); |
| 470 | } |
| 471 | |
| 472 | /* Both LEB 1 and LEB 2 are OK and consistent */ |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 473 | vfree(leb[1]); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 474 | return leb[0]; |
| 475 | } else { |
| 476 | /* LEB 0 is corrupted or does not exist */ |
| 477 | if (leb[1]) { |
| 478 | leb_corrupted[1] = vtbl_check(ubi, leb[1]); |
| 479 | if (leb_corrupted[1] < 0) |
| 480 | goto out_free; |
| 481 | } |
| 482 | if (leb_corrupted[1]) { |
| 483 | /* Both LEB 0 and LEB 1 are corrupted */ |
| 484 | ubi_err("both volume tables are corrupted"); |
| 485 | goto out_free; |
| 486 | } |
| 487 | |
| 488 | ubi_warn("volume table copy #1 is corrupted"); |
| 489 | err = create_vtbl(ubi, si, 0, leb[1]); |
| 490 | if (err) |
| 491 | goto out_free; |
| 492 | ubi_msg("volume table was restored"); |
| 493 | |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 494 | vfree(leb[0]); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 495 | return leb[1]; |
| 496 | } |
| 497 | |
| 498 | out_free: |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 499 | vfree(leb[0]); |
| 500 | vfree(leb[1]); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 501 | return ERR_PTR(err); |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * create_empty_lvol - create empty layout volume. |
| 506 | * @ubi: UBI device description object |
| 507 | * @si: scanning information |
| 508 | * |
| 509 | * This function returns volume table contents in case of success and a |
| 510 | * negative error code in case of failure. |
| 511 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 512 | static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 513 | struct ubi_scan_info *si) |
| 514 | { |
| 515 | int i; |
| 516 | struct ubi_vtbl_record *vtbl; |
| 517 | |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 518 | vtbl = vmalloc(ubi->vtbl_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 519 | if (!vtbl) |
| 520 | return ERR_PTR(-ENOMEM); |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 521 | memset(vtbl, 0, ubi->vtbl_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 522 | |
| 523 | for (i = 0; i < ubi->vtbl_slots; i++) |
| 524 | memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE); |
| 525 | |
| 526 | for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) { |
| 527 | int err; |
| 528 | |
| 529 | err = create_vtbl(ubi, si, i, vtbl); |
| 530 | if (err) { |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 531 | vfree(vtbl); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 532 | return ERR_PTR(err); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | return vtbl; |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * init_volumes - initialize volume information for existing volumes. |
| 541 | * @ubi: UBI device description object |
| 542 | * @si: scanning information |
| 543 | * @vtbl: volume table |
| 544 | * |
| 545 | * This function allocates volume description objects for existing volumes. |
| 546 | * Returns zero in case of success and a negative error code in case of |
| 547 | * failure. |
| 548 | */ |
| 549 | static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si, |
| 550 | const struct ubi_vtbl_record *vtbl) |
| 551 | { |
| 552 | int i, reserved_pebs = 0; |
| 553 | struct ubi_scan_volume *sv; |
| 554 | struct ubi_volume *vol; |
| 555 | |
| 556 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 557 | cond_resched(); |
| 558 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 559 | if (be32_to_cpu(vtbl[i].reserved_pebs) == 0) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 560 | continue; /* Empty record */ |
| 561 | |
| 562 | vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); |
| 563 | if (!vol) |
| 564 | return -ENOMEM; |
| 565 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 566 | vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs); |
| 567 | vol->alignment = be32_to_cpu(vtbl[i].alignment); |
| 568 | vol->data_pad = be32_to_cpu(vtbl[i].data_pad); |
Peter Horton | ff99879 | 2010-01-05 11:14:36 +0000 | [diff] [blame] | 569 | vol->upd_marker = vtbl[i].upd_marker; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 570 | vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ? |
| 571 | UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 572 | vol->name_len = be16_to_cpu(vtbl[i].name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 573 | vol->usable_leb_size = ubi->leb_size - vol->data_pad; |
| 574 | memcpy(vol->name, vtbl[i].name, vol->name_len); |
| 575 | vol->name[vol->name_len] = '\0'; |
| 576 | vol->vol_id = i; |
| 577 | |
Artem Bityutskiy | 4ccf8cf | 2008-01-16 15:44:24 +0200 | [diff] [blame] | 578 | if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) { |
| 579 | /* Auto re-size flag may be set only for one volume */ |
| 580 | if (ubi->autoresize_vol_id != -1) { |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 581 | ubi_err("more than one auto-resize volume (%d " |
Artem Bityutskiy | 4ccf8cf | 2008-01-16 15:44:24 +0200 | [diff] [blame] | 582 | "and %d)", ubi->autoresize_vol_id, i); |
Adrian Bunk | f7f0283 | 2008-03-03 20:07:52 +0200 | [diff] [blame] | 583 | kfree(vol); |
Artem Bityutskiy | 4ccf8cf | 2008-01-16 15:44:24 +0200 | [diff] [blame] | 584 | return -EINVAL; |
| 585 | } |
| 586 | |
| 587 | ubi->autoresize_vol_id = i; |
| 588 | } |
| 589 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 590 | ubi_assert(!ubi->volumes[i]); |
| 591 | ubi->volumes[i] = vol; |
| 592 | ubi->vol_count += 1; |
| 593 | vol->ubi = ubi; |
| 594 | reserved_pebs += vol->reserved_pebs; |
| 595 | |
| 596 | /* |
| 597 | * In case of dynamic volume UBI knows nothing about how many |
| 598 | * data is stored there. So assume the whole volume is used. |
| 599 | */ |
| 600 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { |
| 601 | vol->used_ebs = vol->reserved_pebs; |
| 602 | vol->last_eb_bytes = vol->usable_leb_size; |
Vinit Agnihotri | d08c3b7 | 2007-07-10 13:04:59 +0300 | [diff] [blame] | 603 | vol->used_bytes = |
| 604 | (long long)vol->used_ebs * vol->usable_leb_size; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 605 | continue; |
| 606 | } |
| 607 | |
| 608 | /* Static volumes only */ |
| 609 | sv = ubi_scan_find_sv(si, i); |
| 610 | if (!sv) { |
| 611 | /* |
| 612 | * No eraseblocks belonging to this volume found. We |
| 613 | * don't actually know whether this static volume is |
| 614 | * completely corrupted or just contains no data. And |
| 615 | * we cannot know this as long as data size is not |
| 616 | * stored on flash. So we just assume the volume is |
| 617 | * empty. FIXME: this should be handled. |
| 618 | */ |
| 619 | continue; |
| 620 | } |
| 621 | |
| 622 | if (sv->leb_count != sv->used_ebs) { |
| 623 | /* |
| 624 | * We found a static volume which misses several |
| 625 | * eraseblocks. Treat it as corrupted. |
| 626 | */ |
| 627 | ubi_warn("static volume %d misses %d LEBs - corrupted", |
| 628 | sv->vol_id, sv->used_ebs - sv->leb_count); |
| 629 | vol->corrupted = 1; |
| 630 | continue; |
| 631 | } |
| 632 | |
| 633 | vol->used_ebs = sv->used_ebs; |
Vinit Agnihotri | d08c3b7 | 2007-07-10 13:04:59 +0300 | [diff] [blame] | 634 | vol->used_bytes = |
| 635 | (long long)(vol->used_ebs - 1) * vol->usable_leb_size; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 636 | vol->used_bytes += sv->last_data_size; |
| 637 | vol->last_eb_bytes = sv->last_data_size; |
| 638 | } |
| 639 | |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 640 | /* And add the layout volume */ |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 641 | vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); |
| 642 | if (!vol) |
| 643 | return -ENOMEM; |
| 644 | |
| 645 | vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS; |
| 646 | vol->alignment = 1; |
| 647 | vol->vol_type = UBI_DYNAMIC_VOLUME; |
| 648 | vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1; |
| 649 | memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1); |
| 650 | vol->usable_leb_size = ubi->leb_size; |
| 651 | vol->used_ebs = vol->reserved_pebs; |
| 652 | vol->last_eb_bytes = vol->reserved_pebs; |
Vinit Agnihotri | d08c3b7 | 2007-07-10 13:04:59 +0300 | [diff] [blame] | 653 | vol->used_bytes = |
| 654 | (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad); |
Artem Bityutskiy | 91f2d53 | 2008-01-24 11:23:23 +0200 | [diff] [blame] | 655 | vol->vol_id = UBI_LAYOUT_VOLUME_ID; |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 656 | vol->ref_count = 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 657 | |
| 658 | ubi_assert(!ubi->volumes[i]); |
| 659 | ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol; |
| 660 | reserved_pebs += vol->reserved_pebs; |
| 661 | ubi->vol_count += 1; |
| 662 | vol->ubi = ubi; |
| 663 | |
| 664 | if (reserved_pebs > ubi->avail_pebs) |
| 665 | ubi_err("not enough PEBs, required %d, available %d", |
| 666 | reserved_pebs, ubi->avail_pebs); |
| 667 | ubi->rsvd_pebs += reserved_pebs; |
| 668 | ubi->avail_pebs -= reserved_pebs; |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * check_sv - check volume scanning information. |
| 675 | * @vol: UBI volume description object |
| 676 | * @sv: volume scanning information |
| 677 | * |
| 678 | * This function returns zero if the volume scanning information is consistent |
| 679 | * to the data read from the volume tabla, and %-EINVAL if not. |
| 680 | */ |
| 681 | static int check_sv(const struct ubi_volume *vol, |
| 682 | const struct ubi_scan_volume *sv) |
| 683 | { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 684 | int err; |
| 685 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 686 | if (sv->highest_lnum >= vol->reserved_pebs) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 687 | err = 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 688 | goto bad; |
| 689 | } |
| 690 | if (sv->leb_count > vol->reserved_pebs) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 691 | err = 2; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 692 | goto bad; |
| 693 | } |
| 694 | if (sv->vol_type != vol->vol_type) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 695 | err = 3; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 696 | goto bad; |
| 697 | } |
| 698 | if (sv->used_ebs > vol->reserved_pebs) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 699 | err = 4; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 700 | goto bad; |
| 701 | } |
| 702 | if (sv->data_pad != vol->data_pad) { |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 703 | err = 5; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 704 | goto bad; |
| 705 | } |
| 706 | return 0; |
| 707 | |
| 708 | bad: |
Artem Bityutskiy | 979c929 | 2008-05-14 16:10:33 +0300 | [diff] [blame] | 709 | ubi_err("bad scanning information, error %d", err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 710 | ubi_dbg_dump_sv(sv); |
| 711 | ubi_dbg_dump_vol_info(vol); |
| 712 | return -EINVAL; |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * check_scanning_info - check that scanning information. |
| 717 | * @ubi: UBI device description object |
| 718 | * @si: scanning information |
| 719 | * |
| 720 | * Even though we protect on-flash data by CRC checksums, we still don't trust |
| 721 | * the media. This function ensures that scanning information is consistent to |
| 722 | * the information read from the volume table. Returns zero if the scanning |
| 723 | * information is OK and %-EINVAL if it is not. |
| 724 | */ |
| 725 | static int check_scanning_info(const struct ubi_device *ubi, |
| 726 | struct ubi_scan_info *si) |
| 727 | { |
| 728 | int err, i; |
| 729 | struct ubi_scan_volume *sv; |
| 730 | struct ubi_volume *vol; |
| 731 | |
| 732 | if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) { |
| 733 | ubi_err("scanning found %d volumes, maximum is %d + %d", |
| 734 | si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots); |
| 735 | return -EINVAL; |
| 736 | } |
| 737 | |
Kyungmin Park | cadb40c | 2008-05-22 10:32:18 +0900 | [diff] [blame] | 738 | if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT && |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 739 | si->highest_vol_id < UBI_INTERNAL_VOL_START) { |
| 740 | ubi_err("too large volume ID %d found by scanning", |
| 741 | si->highest_vol_id); |
| 742 | return -EINVAL; |
| 743 | } |
| 744 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 745 | for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) { |
| 746 | cond_resched(); |
| 747 | |
| 748 | sv = ubi_scan_find_sv(si, i); |
| 749 | vol = ubi->volumes[i]; |
| 750 | if (!vol) { |
| 751 | if (sv) |
| 752 | ubi_scan_rm_volume(si, sv); |
| 753 | continue; |
| 754 | } |
| 755 | |
| 756 | if (vol->reserved_pebs == 0) { |
| 757 | ubi_assert(i < ubi->vtbl_slots); |
| 758 | |
| 759 | if (!sv) |
| 760 | continue; |
| 761 | |
| 762 | /* |
| 763 | * During scanning we found a volume which does not |
| 764 | * exist according to the information in the volume |
| 765 | * table. This must have happened due to an unclean |
| 766 | * reboot while the volume was being removed. Discard |
| 767 | * these eraseblocks. |
| 768 | */ |
| 769 | ubi_msg("finish volume %d removal", sv->vol_id); |
| 770 | ubi_scan_rm_volume(si, sv); |
| 771 | } else if (sv) { |
| 772 | err = check_sv(vol, sv); |
| 773 | if (err) |
| 774 | return err; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | /** |
Artem Bityutskiy | ebaaf1a | 2008-07-18 13:34:32 +0300 | [diff] [blame] | 782 | * ubi_read_volume_table - read the volume table. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 783 | * @ubi: UBI device description object |
| 784 | * @si: scanning information |
| 785 | * |
| 786 | * This function reads volume table, checks it, recover from errors if needed, |
| 787 | * or creates it if needed. Returns zero in case of success and a negative |
| 788 | * error code in case of failure. |
| 789 | */ |
| 790 | int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si) |
| 791 | { |
| 792 | int i, err; |
| 793 | struct ubi_scan_volume *sv; |
| 794 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 795 | empty_vtbl_record.crc = cpu_to_be32(0xf116c36b); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 796 | |
| 797 | /* |
| 798 | * The number of supported volumes is limited by the eraseblock size |
| 799 | * and by the UBI_MAX_VOLUMES constant. |
| 800 | */ |
| 801 | ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE; |
| 802 | if (ubi->vtbl_slots > UBI_MAX_VOLUMES) |
| 803 | ubi->vtbl_slots = UBI_MAX_VOLUMES; |
| 804 | |
| 805 | ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE; |
| 806 | ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size); |
| 807 | |
Artem Bityutskiy | 91f2d53 | 2008-01-24 11:23:23 +0200 | [diff] [blame] | 808 | sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 809 | if (!sv) { |
| 810 | /* |
| 811 | * No logical eraseblocks belonging to the layout volume were |
| 812 | * found. This could mean that the flash is just empty. In |
| 813 | * this case we create empty layout volume. |
| 814 | * |
| 815 | * But if flash is not empty this must be a corruption or the |
| 816 | * MTD device just contains garbage. |
| 817 | */ |
| 818 | if (si->is_empty) { |
| 819 | ubi->vtbl = create_empty_lvol(ubi, si); |
| 820 | if (IS_ERR(ubi->vtbl)) |
| 821 | return PTR_ERR(ubi->vtbl); |
| 822 | } else { |
| 823 | ubi_err("the layout volume was not found"); |
| 824 | return -EINVAL; |
| 825 | } |
| 826 | } else { |
| 827 | if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) { |
| 828 | /* This must not happen with proper UBI images */ |
| 829 | dbg_err("too many LEBs (%d) in layout volume", |
| 830 | sv->leb_count); |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | |
| 834 | ubi->vtbl = process_lvol(ubi, si, sv); |
| 835 | if (IS_ERR(ubi->vtbl)) |
| 836 | return PTR_ERR(ubi->vtbl); |
| 837 | } |
| 838 | |
| 839 | ubi->avail_pebs = ubi->good_peb_count; |
| 840 | |
| 841 | /* |
| 842 | * The layout volume is OK, initialize the corresponding in-RAM data |
| 843 | * structures. |
| 844 | */ |
| 845 | err = init_volumes(ubi, si, ubi->vtbl); |
| 846 | if (err) |
| 847 | goto out_free; |
| 848 | |
| 849 | /* |
| 850 | * Get sure that the scanning information is consistent to the |
| 851 | * information stored in the volume table. |
| 852 | */ |
| 853 | err = check_scanning_info(ubi, si); |
| 854 | if (err) |
| 855 | goto out_free; |
| 856 | |
| 857 | return 0; |
| 858 | |
| 859 | out_free: |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 860 | vfree(ubi->vtbl); |
Artem Bityutskiy | 9c9ec14 | 2008-07-18 13:19:52 +0300 | [diff] [blame] | 861 | for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) { |
| 862 | kfree(ubi->volumes[i]); |
| 863 | ubi->volumes[i] = NULL; |
| 864 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 865 | return err; |
| 866 | } |
| 867 | |
| 868 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID |
| 869 | |
| 870 | /** |
| 871 | * paranoid_vtbl_check - check volume table. |
| 872 | * @ubi: UBI device description object |
| 873 | */ |
| 874 | static void paranoid_vtbl_check(const struct ubi_device *ubi) |
| 875 | { |
| 876 | if (vtbl_check(ubi, ubi->vtbl)) { |
| 877 | ubi_err("paranoid check failed"); |
| 878 | BUG(); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ |