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