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