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 | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame^] | 92 | layout_vol = ubi->volumes[vol_id2idx(UBI_LAYOUT_VOL_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 | mutex_lock(&ubi->vtbl_mutex); |
| 102 | memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record)); |
| 103 | for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) { |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame^] | 104 | err = ubi_eba_unmap_leb(ubi, layout_vol, i); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 105 | if (err) { |
| 106 | mutex_unlock(&ubi->vtbl_mutex); |
| 107 | return err; |
| 108 | } |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame^] | 109 | 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] | 110 | ubi->vtbl_size, UBI_LONGTERM); |
| 111 | if (err) { |
| 112 | mutex_unlock(&ubi->vtbl_mutex); |
| 113 | return err; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | paranoid_vtbl_check(ubi); |
| 118 | mutex_unlock(&ubi->vtbl_mutex); |
| 119 | return ubi_wl_flush(ubi); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * vol_til_check - check if volume table is not corrupted and contains sensible |
| 124 | * data. |
| 125 | * |
| 126 | * @ubi: UBI device description object |
| 127 | * @vtbl: volume table |
| 128 | * |
| 129 | * This function returns zero if @vtbl is all right, %1 if CRC is incorrect, |
| 130 | * and %-EINVAL if it contains inconsistent data. |
| 131 | */ |
| 132 | static int vtbl_check(const struct ubi_device *ubi, |
| 133 | const struct ubi_vtbl_record *vtbl) |
| 134 | { |
| 135 | int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len; |
| 136 | int upd_marker; |
| 137 | uint32_t crc; |
| 138 | const char *name; |
| 139 | |
| 140 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 141 | cond_resched(); |
| 142 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 143 | reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs); |
| 144 | alignment = be32_to_cpu(vtbl[i].alignment); |
| 145 | data_pad = be32_to_cpu(vtbl[i].data_pad); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 146 | upd_marker = vtbl[i].upd_marker; |
| 147 | vol_type = vtbl[i].vol_type; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 148 | name_len = be16_to_cpu(vtbl[i].name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 149 | name = &vtbl[i].name[0]; |
| 150 | |
| 151 | crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 152 | if (be32_to_cpu(vtbl[i].crc) != crc) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 153 | ubi_err("bad CRC at record %u: %#08x, not %#08x", |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 154 | i, crc, be32_to_cpu(vtbl[i].crc)); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 155 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | if (reserved_pebs == 0) { |
| 160 | if (memcmp(&vtbl[i], &empty_vtbl_record, |
| 161 | UBI_VTBL_RECORD_SIZE)) { |
| 162 | dbg_err("bad empty record"); |
| 163 | goto bad; |
| 164 | } |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 || |
| 169 | name_len < 0) { |
| 170 | dbg_err("negative values"); |
| 171 | goto bad; |
| 172 | } |
| 173 | |
| 174 | if (alignment > ubi->leb_size || alignment == 0) { |
| 175 | dbg_err("bad alignment"); |
| 176 | goto bad; |
| 177 | } |
| 178 | |
| 179 | n = alignment % ubi->min_io_size; |
| 180 | if (alignment != 1 && n) { |
| 181 | dbg_err("alignment is not multiple of min I/O unit"); |
| 182 | goto bad; |
| 183 | } |
| 184 | |
| 185 | n = ubi->leb_size % alignment; |
| 186 | if (data_pad != n) { |
| 187 | dbg_err("bad data_pad, has to be %d", n); |
| 188 | goto bad; |
| 189 | } |
| 190 | |
| 191 | if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { |
| 192 | dbg_err("bad vol_type"); |
| 193 | goto bad; |
| 194 | } |
| 195 | |
| 196 | if (upd_marker != 0 && upd_marker != 1) { |
| 197 | dbg_err("bad upd_marker"); |
| 198 | goto bad; |
| 199 | } |
| 200 | |
| 201 | if (reserved_pebs > ubi->good_peb_count) { |
| 202 | dbg_err("too large reserved_pebs, good PEBs %d", |
| 203 | ubi->good_peb_count); |
| 204 | goto bad; |
| 205 | } |
| 206 | |
| 207 | if (name_len > UBI_VOL_NAME_MAX) { |
| 208 | dbg_err("too long volume name, max %d", |
| 209 | UBI_VOL_NAME_MAX); |
| 210 | goto bad; |
| 211 | } |
| 212 | |
| 213 | if (name[0] == '\0') { |
| 214 | dbg_err("NULL volume name"); |
| 215 | goto bad; |
| 216 | } |
| 217 | |
| 218 | if (name_len != strnlen(name, name_len + 1)) { |
| 219 | dbg_err("bad name_len"); |
| 220 | goto bad; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* Checks that all names are unique */ |
| 225 | for (i = 0; i < ubi->vtbl_slots - 1; i++) { |
| 226 | for (n = i + 1; n < ubi->vtbl_slots; n++) { |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 227 | int len1 = be16_to_cpu(vtbl[i].name_len); |
| 228 | int len2 = be16_to_cpu(vtbl[n].name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 229 | |
| 230 | if (len1 > 0 && len1 == len2 && |
| 231 | !strncmp(vtbl[i].name, vtbl[n].name, len1)) { |
| 232 | ubi_err("volumes %d and %d have the same name" |
| 233 | " \"%s\"", i, n, vtbl[i].name); |
| 234 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); |
| 235 | ubi_dbg_dump_vtbl_record(&vtbl[n], n); |
| 236 | return -EINVAL; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | |
| 243 | bad: |
| 244 | ubi_err("volume table check failed, record %d", i); |
| 245 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * create_vtbl - create a copy of volume table. |
| 251 | * @ubi: UBI device description object |
| 252 | * @si: scanning information |
| 253 | * @copy: number of the volume table copy |
| 254 | * @vtbl: contents of the volume table |
| 255 | * |
| 256 | * This function returns zero in case of success and a negative error code in |
| 257 | * case of failure. |
| 258 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 259 | 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] | 260 | int copy, void *vtbl) |
| 261 | { |
| 262 | int err, tries = 0; |
| 263 | static struct ubi_vid_hdr *vid_hdr; |
| 264 | struct ubi_scan_volume *sv; |
| 265 | struct ubi_scan_leb *new_seb, *old_seb = NULL; |
| 266 | |
| 267 | ubi_msg("create volume table (copy #%d)", copy + 1); |
| 268 | |
Artem Bityutskiy | 33818bb | 2007-08-28 21:29:32 +0300 | [diff] [blame] | 269 | vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 270 | if (!vid_hdr) |
| 271 | return -ENOMEM; |
| 272 | |
| 273 | /* |
| 274 | * Check if there is a logical eraseblock which would have to contain |
| 275 | * this volume table copy was found during scanning. It has to be wiped |
| 276 | * out. |
| 277 | */ |
| 278 | sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOL_ID); |
| 279 | if (sv) |
| 280 | old_seb = ubi_scan_find_seb(sv, copy); |
| 281 | |
| 282 | retry: |
| 283 | new_seb = ubi_scan_get_free_peb(ubi, si); |
| 284 | if (IS_ERR(new_seb)) { |
| 285 | err = PTR_ERR(new_seb); |
| 286 | goto out_free; |
| 287 | } |
| 288 | |
| 289 | vid_hdr->vol_type = UBI_VID_DYNAMIC; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 290 | vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOL_ID); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 291 | vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT; |
| 292 | vid_hdr->data_size = vid_hdr->used_ebs = |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 293 | vid_hdr->data_pad = cpu_to_be32(0); |
| 294 | vid_hdr->lnum = cpu_to_be32(copy); |
| 295 | vid_hdr->sqnum = cpu_to_be64(++si->max_sqnum); |
| 296 | vid_hdr->leb_ver = cpu_to_be32(old_seb ? old_seb->leb_ver + 1: 0); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 297 | |
| 298 | /* The EC header is already there, write the VID header */ |
| 299 | err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr); |
| 300 | if (err) |
| 301 | goto write_error; |
| 302 | |
| 303 | /* Write the layout volume contents */ |
| 304 | err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size); |
| 305 | if (err) |
| 306 | goto write_error; |
| 307 | |
| 308 | /* |
| 309 | * And add it to the scanning information. Don't delete the old |
| 310 | * @old_seb as it will be deleted and freed in 'ubi_scan_add_used()'. |
| 311 | */ |
| 312 | err = ubi_scan_add_used(ubi, si, new_seb->pnum, new_seb->ec, |
| 313 | vid_hdr, 0); |
| 314 | kfree(new_seb); |
| 315 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 316 | return err; |
| 317 | |
| 318 | write_error: |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 319 | if (err == -EIO && ++tries <= 5) { |
| 320 | /* |
| 321 | * Probably this physical eraseblock went bad, try to pick |
| 322 | * another one. |
| 323 | */ |
| 324 | list_add_tail(&new_seb->u.list, &si->corr); |
Florin Malita | c4e90ec | 2007-05-03 11:49:57 -0400 | [diff] [blame] | 325 | goto retry; |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 326 | } |
| 327 | kfree(new_seb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 328 | out_free: |
| 329 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 330 | return err; |
| 331 | |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * process_lvol - process the layout volume. |
| 336 | * @ubi: UBI device description object |
| 337 | * @si: scanning information |
| 338 | * @sv: layout volume scanning information |
| 339 | * |
| 340 | * This function is responsible for reading the layout volume, ensuring it is |
| 341 | * not corrupted, and recovering from corruptions if needed. Returns volume |
| 342 | * table in case of success and a negative error code in case of failure. |
| 343 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 344 | static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 345 | struct ubi_scan_info *si, |
| 346 | struct ubi_scan_volume *sv) |
| 347 | { |
| 348 | int err; |
| 349 | struct rb_node *rb; |
| 350 | struct ubi_scan_leb *seb; |
| 351 | struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL }; |
| 352 | int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1}; |
| 353 | |
| 354 | /* |
| 355 | * UBI goes through the following steps when it changes the layout |
| 356 | * volume: |
| 357 | * a. erase LEB 0; |
| 358 | * b. write new data to LEB 0; |
| 359 | * c. erase LEB 1; |
| 360 | * d. write new data to LEB 1. |
| 361 | * |
| 362 | * Before the change, both LEBs contain the same data. |
| 363 | * |
| 364 | * Due to unclean reboots, the contents of LEB 0 may be lost, but there |
| 365 | * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not. |
| 366 | * Similarly, LEB 1 may be lost, but there should be LEB 0. And |
| 367 | * finally, unclean reboots may result in a situation when neither LEB |
| 368 | * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB |
| 369 | * 0 contains more recent information. |
| 370 | * |
| 371 | * So the plan is to first check LEB 0. Then |
| 372 | * a. if LEB 0 is OK, it must be containing the most resent data; then |
| 373 | * we compare it with LEB 1, and if they are different, we copy LEB |
| 374 | * 0 to LEB 1; |
| 375 | * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1 |
| 376 | * to LEB 0. |
| 377 | */ |
| 378 | |
| 379 | dbg_msg("check layout volume"); |
| 380 | |
| 381 | /* Read both LEB 0 and LEB 1 into memory */ |
| 382 | ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) { |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 383 | leb[seb->lnum] = vmalloc(ubi->vtbl_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 384 | if (!leb[seb->lnum]) { |
| 385 | err = -ENOMEM; |
| 386 | goto out_free; |
| 387 | } |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 388 | memset(leb[seb->lnum], 0, ubi->vtbl_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 389 | |
| 390 | err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0, |
| 391 | ubi->vtbl_size); |
| 392 | if (err == UBI_IO_BITFLIPS || err == -EBADMSG) |
| 393 | /* Scrub the PEB later */ |
| 394 | seb->scrub = 1; |
| 395 | else if (err) |
| 396 | goto out_free; |
| 397 | } |
| 398 | |
| 399 | err = -EINVAL; |
| 400 | if (leb[0]) { |
| 401 | leb_corrupted[0] = vtbl_check(ubi, leb[0]); |
| 402 | if (leb_corrupted[0] < 0) |
| 403 | goto out_free; |
| 404 | } |
| 405 | |
| 406 | if (!leb_corrupted[0]) { |
| 407 | /* LEB 0 is OK */ |
| 408 | if (leb[1]) |
| 409 | leb_corrupted[1] = memcmp(leb[0], leb[1], ubi->vtbl_size); |
| 410 | if (leb_corrupted[1]) { |
| 411 | ubi_warn("volume table copy #2 is corrupted"); |
| 412 | err = create_vtbl(ubi, si, 1, leb[0]); |
| 413 | if (err) |
| 414 | goto out_free; |
| 415 | ubi_msg("volume table was restored"); |
| 416 | } |
| 417 | |
| 418 | /* Both LEB 1 and LEB 2 are OK and consistent */ |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 419 | vfree(leb[1]); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 420 | return leb[0]; |
| 421 | } else { |
| 422 | /* LEB 0 is corrupted or does not exist */ |
| 423 | if (leb[1]) { |
| 424 | leb_corrupted[1] = vtbl_check(ubi, leb[1]); |
| 425 | if (leb_corrupted[1] < 0) |
| 426 | goto out_free; |
| 427 | } |
| 428 | if (leb_corrupted[1]) { |
| 429 | /* Both LEB 0 and LEB 1 are corrupted */ |
| 430 | ubi_err("both volume tables are corrupted"); |
| 431 | goto out_free; |
| 432 | } |
| 433 | |
| 434 | ubi_warn("volume table copy #1 is corrupted"); |
| 435 | err = create_vtbl(ubi, si, 0, leb[1]); |
| 436 | if (err) |
| 437 | goto out_free; |
| 438 | ubi_msg("volume table was restored"); |
| 439 | |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 440 | vfree(leb[0]); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 441 | return leb[1]; |
| 442 | } |
| 443 | |
| 444 | out_free: |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 445 | vfree(leb[0]); |
| 446 | vfree(leb[1]); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 447 | return ERR_PTR(err); |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * create_empty_lvol - create empty layout volume. |
| 452 | * @ubi: UBI device description object |
| 453 | * @si: scanning information |
| 454 | * |
| 455 | * This function returns volume table contents in case of success and a |
| 456 | * negative error code in case of failure. |
| 457 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 458 | 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] | 459 | struct ubi_scan_info *si) |
| 460 | { |
| 461 | int i; |
| 462 | struct ubi_vtbl_record *vtbl; |
| 463 | |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 464 | vtbl = vmalloc(ubi->vtbl_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 465 | if (!vtbl) |
| 466 | return ERR_PTR(-ENOMEM); |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 467 | memset(vtbl, 0, ubi->vtbl_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 468 | |
| 469 | for (i = 0; i < ubi->vtbl_slots; i++) |
| 470 | memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE); |
| 471 | |
| 472 | for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) { |
| 473 | int err; |
| 474 | |
| 475 | err = create_vtbl(ubi, si, i, vtbl); |
| 476 | if (err) { |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 477 | vfree(vtbl); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 478 | return ERR_PTR(err); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return vtbl; |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * init_volumes - initialize volume information for existing volumes. |
| 487 | * @ubi: UBI device description object |
| 488 | * @si: scanning information |
| 489 | * @vtbl: volume table |
| 490 | * |
| 491 | * This function allocates volume description objects for existing volumes. |
| 492 | * Returns zero in case of success and a negative error code in case of |
| 493 | * failure. |
| 494 | */ |
| 495 | static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si, |
| 496 | const struct ubi_vtbl_record *vtbl) |
| 497 | { |
| 498 | int i, reserved_pebs = 0; |
| 499 | struct ubi_scan_volume *sv; |
| 500 | struct ubi_volume *vol; |
| 501 | |
| 502 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 503 | cond_resched(); |
| 504 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 505 | if (be32_to_cpu(vtbl[i].reserved_pebs) == 0) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 506 | continue; /* Empty record */ |
| 507 | |
| 508 | vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); |
| 509 | if (!vol) |
| 510 | return -ENOMEM; |
| 511 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 512 | vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs); |
| 513 | vol->alignment = be32_to_cpu(vtbl[i].alignment); |
| 514 | vol->data_pad = be32_to_cpu(vtbl[i].data_pad); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 515 | vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ? |
| 516 | UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 517 | vol->name_len = be16_to_cpu(vtbl[i].name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 518 | vol->usable_leb_size = ubi->leb_size - vol->data_pad; |
| 519 | memcpy(vol->name, vtbl[i].name, vol->name_len); |
| 520 | vol->name[vol->name_len] = '\0'; |
| 521 | vol->vol_id = i; |
| 522 | |
| 523 | ubi_assert(!ubi->volumes[i]); |
| 524 | ubi->volumes[i] = vol; |
| 525 | ubi->vol_count += 1; |
| 526 | vol->ubi = ubi; |
| 527 | reserved_pebs += vol->reserved_pebs; |
| 528 | |
| 529 | /* |
| 530 | * In case of dynamic volume UBI knows nothing about how many |
| 531 | * data is stored there. So assume the whole volume is used. |
| 532 | */ |
| 533 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { |
| 534 | vol->used_ebs = vol->reserved_pebs; |
| 535 | vol->last_eb_bytes = vol->usable_leb_size; |
Vinit Agnihotri | d08c3b7 | 2007-07-10 13:04:59 +0300 | [diff] [blame] | 536 | vol->used_bytes = |
| 537 | (long long)vol->used_ebs * vol->usable_leb_size; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 538 | continue; |
| 539 | } |
| 540 | |
| 541 | /* Static volumes only */ |
| 542 | sv = ubi_scan_find_sv(si, i); |
| 543 | if (!sv) { |
| 544 | /* |
| 545 | * No eraseblocks belonging to this volume found. We |
| 546 | * don't actually know whether this static volume is |
| 547 | * completely corrupted or just contains no data. And |
| 548 | * we cannot know this as long as data size is not |
| 549 | * stored on flash. So we just assume the volume is |
| 550 | * empty. FIXME: this should be handled. |
| 551 | */ |
| 552 | continue; |
| 553 | } |
| 554 | |
| 555 | if (sv->leb_count != sv->used_ebs) { |
| 556 | /* |
| 557 | * We found a static volume which misses several |
| 558 | * eraseblocks. Treat it as corrupted. |
| 559 | */ |
| 560 | ubi_warn("static volume %d misses %d LEBs - corrupted", |
| 561 | sv->vol_id, sv->used_ebs - sv->leb_count); |
| 562 | vol->corrupted = 1; |
| 563 | continue; |
| 564 | } |
| 565 | |
| 566 | vol->used_ebs = sv->used_ebs; |
Vinit Agnihotri | d08c3b7 | 2007-07-10 13:04:59 +0300 | [diff] [blame] | 567 | vol->used_bytes = |
| 568 | (long long)(vol->used_ebs - 1) * vol->usable_leb_size; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 569 | vol->used_bytes += sv->last_data_size; |
| 570 | vol->last_eb_bytes = sv->last_data_size; |
| 571 | } |
| 572 | |
| 573 | vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); |
| 574 | if (!vol) |
| 575 | return -ENOMEM; |
| 576 | |
| 577 | vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS; |
| 578 | vol->alignment = 1; |
| 579 | vol->vol_type = UBI_DYNAMIC_VOLUME; |
| 580 | vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1; |
| 581 | memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1); |
| 582 | vol->usable_leb_size = ubi->leb_size; |
| 583 | vol->used_ebs = vol->reserved_pebs; |
| 584 | vol->last_eb_bytes = vol->reserved_pebs; |
Vinit Agnihotri | d08c3b7 | 2007-07-10 13:04:59 +0300 | [diff] [blame] | 585 | vol->used_bytes = |
| 586 | (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 587 | vol->vol_id = UBI_LAYOUT_VOL_ID; |
| 588 | |
| 589 | ubi_assert(!ubi->volumes[i]); |
| 590 | ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol; |
| 591 | reserved_pebs += vol->reserved_pebs; |
| 592 | ubi->vol_count += 1; |
| 593 | vol->ubi = ubi; |
| 594 | |
| 595 | if (reserved_pebs > ubi->avail_pebs) |
| 596 | ubi_err("not enough PEBs, required %d, available %d", |
| 597 | reserved_pebs, ubi->avail_pebs); |
| 598 | ubi->rsvd_pebs += reserved_pebs; |
| 599 | ubi->avail_pebs -= reserved_pebs; |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * check_sv - check volume scanning information. |
| 606 | * @vol: UBI volume description object |
| 607 | * @sv: volume scanning information |
| 608 | * |
| 609 | * This function returns zero if the volume scanning information is consistent |
| 610 | * to the data read from the volume tabla, and %-EINVAL if not. |
| 611 | */ |
| 612 | static int check_sv(const struct ubi_volume *vol, |
| 613 | const struct ubi_scan_volume *sv) |
| 614 | { |
| 615 | if (sv->highest_lnum >= vol->reserved_pebs) { |
| 616 | dbg_err("bad highest_lnum"); |
| 617 | goto bad; |
| 618 | } |
| 619 | if (sv->leb_count > vol->reserved_pebs) { |
| 620 | dbg_err("bad leb_count"); |
| 621 | goto bad; |
| 622 | } |
| 623 | if (sv->vol_type != vol->vol_type) { |
| 624 | dbg_err("bad vol_type"); |
| 625 | goto bad; |
| 626 | } |
| 627 | if (sv->used_ebs > vol->reserved_pebs) { |
| 628 | dbg_err("bad used_ebs"); |
| 629 | goto bad; |
| 630 | } |
| 631 | if (sv->data_pad != vol->data_pad) { |
| 632 | dbg_err("bad data_pad"); |
| 633 | goto bad; |
| 634 | } |
| 635 | return 0; |
| 636 | |
| 637 | bad: |
| 638 | ubi_err("bad scanning information"); |
| 639 | ubi_dbg_dump_sv(sv); |
| 640 | ubi_dbg_dump_vol_info(vol); |
| 641 | return -EINVAL; |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * check_scanning_info - check that scanning information. |
| 646 | * @ubi: UBI device description object |
| 647 | * @si: scanning information |
| 648 | * |
| 649 | * Even though we protect on-flash data by CRC checksums, we still don't trust |
| 650 | * the media. This function ensures that scanning information is consistent to |
| 651 | * the information read from the volume table. Returns zero if the scanning |
| 652 | * information is OK and %-EINVAL if it is not. |
| 653 | */ |
| 654 | static int check_scanning_info(const struct ubi_device *ubi, |
| 655 | struct ubi_scan_info *si) |
| 656 | { |
| 657 | int err, i; |
| 658 | struct ubi_scan_volume *sv; |
| 659 | struct ubi_volume *vol; |
| 660 | |
| 661 | if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) { |
| 662 | ubi_err("scanning found %d volumes, maximum is %d + %d", |
| 663 | si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots); |
| 664 | return -EINVAL; |
| 665 | } |
| 666 | |
| 667 | if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT&& |
| 668 | si->highest_vol_id < UBI_INTERNAL_VOL_START) { |
| 669 | ubi_err("too large volume ID %d found by scanning", |
| 670 | si->highest_vol_id); |
| 671 | return -EINVAL; |
| 672 | } |
| 673 | |
| 674 | |
| 675 | for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) { |
| 676 | cond_resched(); |
| 677 | |
| 678 | sv = ubi_scan_find_sv(si, i); |
| 679 | vol = ubi->volumes[i]; |
| 680 | if (!vol) { |
| 681 | if (sv) |
| 682 | ubi_scan_rm_volume(si, sv); |
| 683 | continue; |
| 684 | } |
| 685 | |
| 686 | if (vol->reserved_pebs == 0) { |
| 687 | ubi_assert(i < ubi->vtbl_slots); |
| 688 | |
| 689 | if (!sv) |
| 690 | continue; |
| 691 | |
| 692 | /* |
| 693 | * During scanning we found a volume which does not |
| 694 | * exist according to the information in the volume |
| 695 | * table. This must have happened due to an unclean |
| 696 | * reboot while the volume was being removed. Discard |
| 697 | * these eraseblocks. |
| 698 | */ |
| 699 | ubi_msg("finish volume %d removal", sv->vol_id); |
| 700 | ubi_scan_rm_volume(si, sv); |
| 701 | } else if (sv) { |
| 702 | err = check_sv(vol, sv); |
| 703 | if (err) |
| 704 | return err; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * ubi_read_volume_table - read volume table. |
| 713 | * information. |
| 714 | * @ubi: UBI device description object |
| 715 | * @si: scanning information |
| 716 | * |
| 717 | * This function reads volume table, checks it, recover from errors if needed, |
| 718 | * or creates it if needed. Returns zero in case of success and a negative |
| 719 | * error code in case of failure. |
| 720 | */ |
| 721 | int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si) |
| 722 | { |
| 723 | int i, err; |
| 724 | struct ubi_scan_volume *sv; |
| 725 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 726 | empty_vtbl_record.crc = cpu_to_be32(0xf116c36b); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 727 | |
| 728 | /* |
| 729 | * The number of supported volumes is limited by the eraseblock size |
| 730 | * and by the UBI_MAX_VOLUMES constant. |
| 731 | */ |
| 732 | ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE; |
| 733 | if (ubi->vtbl_slots > UBI_MAX_VOLUMES) |
| 734 | ubi->vtbl_slots = UBI_MAX_VOLUMES; |
| 735 | |
| 736 | ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE; |
| 737 | ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size); |
| 738 | |
| 739 | sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOL_ID); |
| 740 | if (!sv) { |
| 741 | /* |
| 742 | * No logical eraseblocks belonging to the layout volume were |
| 743 | * found. This could mean that the flash is just empty. In |
| 744 | * this case we create empty layout volume. |
| 745 | * |
| 746 | * But if flash is not empty this must be a corruption or the |
| 747 | * MTD device just contains garbage. |
| 748 | */ |
| 749 | if (si->is_empty) { |
| 750 | ubi->vtbl = create_empty_lvol(ubi, si); |
| 751 | if (IS_ERR(ubi->vtbl)) |
| 752 | return PTR_ERR(ubi->vtbl); |
| 753 | } else { |
| 754 | ubi_err("the layout volume was not found"); |
| 755 | return -EINVAL; |
| 756 | } |
| 757 | } else { |
| 758 | if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) { |
| 759 | /* This must not happen with proper UBI images */ |
| 760 | dbg_err("too many LEBs (%d) in layout volume", |
| 761 | sv->leb_count); |
| 762 | return -EINVAL; |
| 763 | } |
| 764 | |
| 765 | ubi->vtbl = process_lvol(ubi, si, sv); |
| 766 | if (IS_ERR(ubi->vtbl)) |
| 767 | return PTR_ERR(ubi->vtbl); |
| 768 | } |
| 769 | |
| 770 | ubi->avail_pebs = ubi->good_peb_count; |
| 771 | |
| 772 | /* |
| 773 | * The layout volume is OK, initialize the corresponding in-RAM data |
| 774 | * structures. |
| 775 | */ |
| 776 | err = init_volumes(ubi, si, ubi->vtbl); |
| 777 | if (err) |
| 778 | goto out_free; |
| 779 | |
| 780 | /* |
| 781 | * Get sure that the scanning information is consistent to the |
| 782 | * information stored in the volume table. |
| 783 | */ |
| 784 | err = check_scanning_info(ubi, si); |
| 785 | if (err) |
| 786 | goto out_free; |
| 787 | |
| 788 | return 0; |
| 789 | |
| 790 | out_free: |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 791 | vfree(ubi->vtbl); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 792 | for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) |
| 793 | if (ubi->volumes[i]) { |
| 794 | kfree(ubi->volumes[i]); |
| 795 | ubi->volumes[i] = NULL; |
| 796 | } |
| 797 | return err; |
| 798 | } |
| 799 | |
| 800 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID |
| 801 | |
| 802 | /** |
| 803 | * paranoid_vtbl_check - check volume table. |
| 804 | * @ubi: UBI device description object |
| 805 | */ |
| 806 | static void paranoid_vtbl_check(const struct ubi_device *ubi) |
| 807 | { |
| 808 | if (vtbl_check(ubi, ubi->vtbl)) { |
| 809 | ubi_err("paranoid check failed"); |
| 810 | BUG(); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ |