Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) International Business Machines Corp., 2006 |
| 4 | * |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 5 | * Author: Artem Bityutskiy (Битюцкий Артём) |
| 6 | */ |
| 7 | |
| 8 | /* |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 9 | * The UBI Eraseblock Association (EBA) sub-system. |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 10 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 11 | * This sub-system is responsible for I/O to/from logical eraseblock. |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 12 | * |
| 13 | * Although in this implementation the EBA table is fully kept and managed in |
| 14 | * RAM, which assumes poor scalability, it might be (partially) maintained on |
| 15 | * flash in future implementations. |
| 16 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 17 | * The EBA sub-system implements per-logical eraseblock locking. Before |
| 18 | * accessing a logical eraseblock it is locked for reading or writing. The |
| 19 | * per-logical eraseblock locking is implemented by means of the lock tree. The |
| 20 | * lock tree is an RB-tree which refers all the currently locked logical |
| 21 | * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects. |
| 22 | * They are indexed by (@vol_id, @lnum) pairs. |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 23 | * |
| 24 | * EBA also maintains the global sequence counter which is incremented each |
| 25 | * time a logical eraseblock is mapped to a physical eraseblock and it is |
| 26 | * stored in the volume identifier header. This means that each VID header has |
| 27 | * a unique sequence number. The sequence number is only increased an we assume |
| 28 | * 64 bits is enough to never overflow. |
| 29 | */ |
| 30 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 31 | #ifndef __UBOOT__ |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 32 | #include <dm/devres.h> |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 33 | #include <linux/slab.h> |
| 34 | #include <linux/crc32.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 35 | #include <u-boot/crc.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 36 | #else |
| 37 | #include <ubi_uboot.h> |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 38 | #endif |
| 39 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 40 | #include <linux/err.h> |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 41 | #include "ubi.h" |
| 42 | |
| 43 | /* Number of physical eraseblocks reserved for atomic LEB change operation */ |
| 44 | #define EBA_RESERVED_PEBS 1 |
| 45 | |
| 46 | /** |
| 47 | * next_sqnum - get next sequence number. |
| 48 | * @ubi: UBI device description object |
| 49 | * |
| 50 | * This function returns next sequence number to use, which is just the current |
| 51 | * global sequence counter value. It also increases the global sequence |
| 52 | * counter. |
| 53 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 54 | unsigned long long ubi_next_sqnum(struct ubi_device *ubi) |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 55 | { |
| 56 | unsigned long long sqnum; |
| 57 | |
| 58 | spin_lock(&ubi->ltree_lock); |
| 59 | sqnum = ubi->global_sqnum++; |
| 60 | spin_unlock(&ubi->ltree_lock); |
| 61 | |
| 62 | return sqnum; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * ubi_get_compat - get compatibility flags of a volume. |
| 67 | * @ubi: UBI device description object |
| 68 | * @vol_id: volume ID |
| 69 | * |
| 70 | * This function returns compatibility flags for an internal volume. User |
| 71 | * volumes have no compatibility flags, so %0 is returned. |
| 72 | */ |
| 73 | static int ubi_get_compat(const struct ubi_device *ubi, int vol_id) |
| 74 | { |
| 75 | if (vol_id == UBI_LAYOUT_VOLUME_ID) |
| 76 | return UBI_LAYOUT_VOLUME_COMPAT; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * ltree_lookup - look up the lock tree. |
| 82 | * @ubi: UBI device description object |
| 83 | * @vol_id: volume ID |
| 84 | * @lnum: logical eraseblock number |
| 85 | * |
| 86 | * This function returns a pointer to the corresponding &struct ubi_ltree_entry |
| 87 | * object if the logical eraseblock is locked and %NULL if it is not. |
| 88 | * @ubi->ltree_lock has to be locked. |
| 89 | */ |
| 90 | static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id, |
| 91 | int lnum) |
| 92 | { |
| 93 | struct rb_node *p; |
| 94 | |
| 95 | p = ubi->ltree.rb_node; |
| 96 | while (p) { |
| 97 | struct ubi_ltree_entry *le; |
| 98 | |
| 99 | le = rb_entry(p, struct ubi_ltree_entry, rb); |
| 100 | |
| 101 | if (vol_id < le->vol_id) |
| 102 | p = p->rb_left; |
| 103 | else if (vol_id > le->vol_id) |
| 104 | p = p->rb_right; |
| 105 | else { |
| 106 | if (lnum < le->lnum) |
| 107 | p = p->rb_left; |
| 108 | else if (lnum > le->lnum) |
| 109 | p = p->rb_right; |
| 110 | else |
| 111 | return le; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * ltree_add_entry - add new entry to the lock tree. |
| 120 | * @ubi: UBI device description object |
| 121 | * @vol_id: volume ID |
| 122 | * @lnum: logical eraseblock number |
| 123 | * |
| 124 | * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the |
| 125 | * lock tree. If such entry is already there, its usage counter is increased. |
| 126 | * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation |
| 127 | * failed. |
| 128 | */ |
| 129 | static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi, |
| 130 | int vol_id, int lnum) |
| 131 | { |
| 132 | struct ubi_ltree_entry *le, *le1, *le_free; |
| 133 | |
| 134 | le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS); |
| 135 | if (!le) |
| 136 | return ERR_PTR(-ENOMEM); |
| 137 | |
| 138 | le->users = 0; |
| 139 | init_rwsem(&le->mutex); |
| 140 | le->vol_id = vol_id; |
| 141 | le->lnum = lnum; |
| 142 | |
| 143 | spin_lock(&ubi->ltree_lock); |
| 144 | le1 = ltree_lookup(ubi, vol_id, lnum); |
| 145 | |
| 146 | if (le1) { |
| 147 | /* |
| 148 | * This logical eraseblock is already locked. The newly |
| 149 | * allocated lock entry is not needed. |
| 150 | */ |
| 151 | le_free = le; |
| 152 | le = le1; |
| 153 | } else { |
| 154 | struct rb_node **p, *parent = NULL; |
| 155 | |
| 156 | /* |
| 157 | * No lock entry, add the newly allocated one to the |
| 158 | * @ubi->ltree RB-tree. |
| 159 | */ |
| 160 | le_free = NULL; |
| 161 | |
| 162 | p = &ubi->ltree.rb_node; |
| 163 | while (*p) { |
| 164 | parent = *p; |
| 165 | le1 = rb_entry(parent, struct ubi_ltree_entry, rb); |
| 166 | |
| 167 | if (vol_id < le1->vol_id) |
| 168 | p = &(*p)->rb_left; |
| 169 | else if (vol_id > le1->vol_id) |
| 170 | p = &(*p)->rb_right; |
| 171 | else { |
| 172 | ubi_assert(lnum != le1->lnum); |
| 173 | if (lnum < le1->lnum) |
| 174 | p = &(*p)->rb_left; |
| 175 | else |
| 176 | p = &(*p)->rb_right; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | rb_link_node(&le->rb, parent, p); |
| 181 | rb_insert_color(&le->rb, &ubi->ltree); |
| 182 | } |
| 183 | le->users += 1; |
| 184 | spin_unlock(&ubi->ltree_lock); |
| 185 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 186 | kfree(le_free); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 187 | return le; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * leb_read_lock - lock logical eraseblock for reading. |
| 192 | * @ubi: UBI device description object |
| 193 | * @vol_id: volume ID |
| 194 | * @lnum: logical eraseblock number |
| 195 | * |
| 196 | * This function locks a logical eraseblock for reading. Returns zero in case |
| 197 | * of success and a negative error code in case of failure. |
| 198 | */ |
| 199 | static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum) |
| 200 | { |
| 201 | struct ubi_ltree_entry *le; |
| 202 | |
| 203 | le = ltree_add_entry(ubi, vol_id, lnum); |
| 204 | if (IS_ERR(le)) |
| 205 | return PTR_ERR(le); |
| 206 | down_read(&le->mutex); |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * leb_read_unlock - unlock logical eraseblock. |
| 212 | * @ubi: UBI device description object |
| 213 | * @vol_id: volume ID |
| 214 | * @lnum: logical eraseblock number |
| 215 | */ |
| 216 | static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum) |
| 217 | { |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 218 | struct ubi_ltree_entry *le; |
| 219 | |
| 220 | spin_lock(&ubi->ltree_lock); |
| 221 | le = ltree_lookup(ubi, vol_id, lnum); |
| 222 | le->users -= 1; |
| 223 | ubi_assert(le->users >= 0); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 224 | up_read(&le->mutex); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 225 | if (le->users == 0) { |
| 226 | rb_erase(&le->rb, &ubi->ltree); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 227 | kfree(le); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 228 | } |
| 229 | spin_unlock(&ubi->ltree_lock); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
| 233 | * leb_write_lock - lock logical eraseblock for writing. |
| 234 | * @ubi: UBI device description object |
| 235 | * @vol_id: volume ID |
| 236 | * @lnum: logical eraseblock number |
| 237 | * |
| 238 | * This function locks a logical eraseblock for writing. Returns zero in case |
| 239 | * of success and a negative error code in case of failure. |
| 240 | */ |
| 241 | static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum) |
| 242 | { |
| 243 | struct ubi_ltree_entry *le; |
| 244 | |
| 245 | le = ltree_add_entry(ubi, vol_id, lnum); |
| 246 | if (IS_ERR(le)) |
| 247 | return PTR_ERR(le); |
| 248 | down_write(&le->mutex); |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * leb_write_lock - lock logical eraseblock for writing. |
| 254 | * @ubi: UBI device description object |
| 255 | * @vol_id: volume ID |
| 256 | * @lnum: logical eraseblock number |
| 257 | * |
| 258 | * This function locks a logical eraseblock for writing if there is no |
| 259 | * contention and does nothing if there is contention. Returns %0 in case of |
| 260 | * success, %1 in case of contention, and and a negative error code in case of |
| 261 | * failure. |
| 262 | */ |
| 263 | static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum) |
| 264 | { |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 265 | struct ubi_ltree_entry *le; |
| 266 | |
| 267 | le = ltree_add_entry(ubi, vol_id, lnum); |
| 268 | if (IS_ERR(le)) |
| 269 | return PTR_ERR(le); |
| 270 | if (down_write_trylock(&le->mutex)) |
| 271 | return 0; |
| 272 | |
| 273 | /* Contention, cancel */ |
| 274 | spin_lock(&ubi->ltree_lock); |
| 275 | le->users -= 1; |
| 276 | ubi_assert(le->users >= 0); |
| 277 | if (le->users == 0) { |
| 278 | rb_erase(&le->rb, &ubi->ltree); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 279 | kfree(le); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 280 | } |
| 281 | spin_unlock(&ubi->ltree_lock); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 282 | |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * leb_write_unlock - unlock logical eraseblock. |
| 288 | * @ubi: UBI device description object |
| 289 | * @vol_id: volume ID |
| 290 | * @lnum: logical eraseblock number |
| 291 | */ |
| 292 | static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum) |
| 293 | { |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 294 | struct ubi_ltree_entry *le; |
| 295 | |
| 296 | spin_lock(&ubi->ltree_lock); |
| 297 | le = ltree_lookup(ubi, vol_id, lnum); |
| 298 | le->users -= 1; |
| 299 | ubi_assert(le->users >= 0); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 300 | up_write(&le->mutex); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 301 | if (le->users == 0) { |
| 302 | rb_erase(&le->rb, &ubi->ltree); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 303 | kfree(le); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 304 | } |
| 305 | spin_unlock(&ubi->ltree_lock); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /** |
| 309 | * ubi_eba_unmap_leb - un-map logical eraseblock. |
| 310 | * @ubi: UBI device description object |
| 311 | * @vol: volume description object |
| 312 | * @lnum: logical eraseblock number |
| 313 | * |
| 314 | * This function un-maps logical eraseblock @lnum and schedules corresponding |
| 315 | * physical eraseblock for erasure. Returns zero in case of success and a |
| 316 | * negative error code in case of failure. |
| 317 | */ |
| 318 | int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol, |
| 319 | int lnum) |
| 320 | { |
| 321 | int err, pnum, vol_id = vol->vol_id; |
| 322 | |
| 323 | if (ubi->ro_mode) |
| 324 | return -EROFS; |
| 325 | |
| 326 | err = leb_write_lock(ubi, vol_id, lnum); |
| 327 | if (err) |
| 328 | return err; |
| 329 | |
| 330 | pnum = vol->eba_tbl[lnum]; |
| 331 | if (pnum < 0) |
| 332 | /* This logical eraseblock is already unmapped */ |
| 333 | goto out_unlock; |
| 334 | |
| 335 | dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum); |
| 336 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 337 | down_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 338 | vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 339 | up_read(&ubi->fm_eba_sem); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 340 | err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 341 | |
| 342 | out_unlock: |
| 343 | leb_write_unlock(ubi, vol_id, lnum); |
| 344 | return err; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * ubi_eba_read_leb - read data. |
| 349 | * @ubi: UBI device description object |
| 350 | * @vol: volume description object |
| 351 | * @lnum: logical eraseblock number |
| 352 | * @buf: buffer to store the read data |
| 353 | * @offset: offset from where to read |
| 354 | * @len: how many bytes to read |
| 355 | * @check: data CRC check flag |
| 356 | * |
| 357 | * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF |
| 358 | * bytes. The @check flag only makes sense for static volumes and forces |
| 359 | * eraseblock data CRC checking. |
| 360 | * |
| 361 | * In case of success this function returns zero. In case of a static volume, |
| 362 | * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be |
| 363 | * returned for any volume type if an ECC error was detected by the MTD device |
| 364 | * driver. Other negative error cored may be returned in case of other errors. |
| 365 | */ |
| 366 | int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, |
| 367 | void *buf, int offset, int len, int check) |
| 368 | { |
| 369 | int err, pnum, scrub = 0, vol_id = vol->vol_id; |
| 370 | struct ubi_vid_hdr *vid_hdr; |
| 371 | uint32_t uninitialized_var(crc); |
| 372 | |
| 373 | err = leb_read_lock(ubi, vol_id, lnum); |
| 374 | if (err) |
| 375 | return err; |
| 376 | |
| 377 | pnum = vol->eba_tbl[lnum]; |
| 378 | if (pnum < 0) { |
| 379 | /* |
| 380 | * The logical eraseblock is not mapped, fill the whole buffer |
| 381 | * with 0xFF bytes. The exception is static volumes for which |
| 382 | * it is an error to read unmapped logical eraseblocks. |
| 383 | */ |
| 384 | dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)", |
| 385 | len, offset, vol_id, lnum); |
| 386 | leb_read_unlock(ubi, vol_id, lnum); |
| 387 | ubi_assert(vol->vol_type != UBI_STATIC_VOLUME); |
| 388 | memset(buf, 0xFF, len); |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d", |
| 393 | len, offset, vol_id, lnum, pnum); |
| 394 | |
| 395 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) |
| 396 | check = 0; |
| 397 | |
| 398 | retry: |
| 399 | if (check) { |
| 400 | vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); |
| 401 | if (!vid_hdr) { |
| 402 | err = -ENOMEM; |
| 403 | goto out_unlock; |
| 404 | } |
| 405 | |
| 406 | err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1); |
| 407 | if (err && err != UBI_IO_BITFLIPS) { |
| 408 | if (err > 0) { |
| 409 | /* |
| 410 | * The header is either absent or corrupted. |
| 411 | * The former case means there is a bug - |
| 412 | * switch to read-only mode just in case. |
| 413 | * The latter case means a real corruption - we |
| 414 | * may try to recover data. FIXME: but this is |
| 415 | * not implemented. |
| 416 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 417 | if (err == UBI_IO_BAD_HDR_EBADMSG || |
| 418 | err == UBI_IO_BAD_HDR) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 419 | ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 420 | pnum, vol_id, lnum); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 421 | err = -EBADMSG; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 422 | } else { |
| 423 | err = -EINVAL; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 424 | ubi_ro_mode(ubi); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 425 | } |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 426 | } |
| 427 | goto out_free; |
| 428 | } else if (err == UBI_IO_BITFLIPS) |
| 429 | scrub = 1; |
| 430 | |
| 431 | ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs)); |
| 432 | ubi_assert(len == be32_to_cpu(vid_hdr->data_size)); |
| 433 | |
| 434 | crc = be32_to_cpu(vid_hdr->data_crc); |
| 435 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 436 | } |
| 437 | |
| 438 | err = ubi_io_read_data(ubi, buf, pnum, offset, len); |
| 439 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 440 | if (err == UBI_IO_BITFLIPS) |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 441 | scrub = 1; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 442 | else if (mtd_is_eccerr(err)) { |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 443 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) |
| 444 | goto out_unlock; |
| 445 | scrub = 1; |
| 446 | if (!check) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 447 | ubi_msg(ubi, "force data checking"); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 448 | check = 1; |
| 449 | goto retry; |
| 450 | } |
| 451 | } else |
| 452 | goto out_unlock; |
| 453 | } |
| 454 | |
| 455 | if (check) { |
| 456 | uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len); |
| 457 | if (crc1 != crc) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 458 | ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x", |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 459 | crc1, crc); |
| 460 | err = -EBADMSG; |
| 461 | goto out_unlock; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if (scrub) |
| 466 | err = ubi_wl_scrub_peb(ubi, pnum); |
| 467 | |
| 468 | leb_read_unlock(ubi, vol_id, lnum); |
| 469 | return err; |
| 470 | |
| 471 | out_free: |
| 472 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 473 | out_unlock: |
| 474 | leb_read_unlock(ubi, vol_id, lnum); |
| 475 | return err; |
| 476 | } |
| 477 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 478 | #ifndef __UBOOT__ |
| 479 | /** |
| 480 | * ubi_eba_read_leb_sg - read data into a scatter gather list. |
| 481 | * @ubi: UBI device description object |
| 482 | * @vol: volume description object |
| 483 | * @lnum: logical eraseblock number |
| 484 | * @sgl: UBI scatter gather list to store the read data |
| 485 | * @offset: offset from where to read |
| 486 | * @len: how many bytes to read |
| 487 | * @check: data CRC check flag |
| 488 | * |
| 489 | * This function works exactly like ubi_eba_read_leb(). But instead of |
| 490 | * storing the read data into a buffer it writes to an UBI scatter gather |
| 491 | * list. |
| 492 | */ |
| 493 | int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol, |
| 494 | struct ubi_sgl *sgl, int lnum, int offset, int len, |
| 495 | int check) |
| 496 | { |
| 497 | int to_read; |
| 498 | int ret; |
| 499 | struct scatterlist *sg; |
| 500 | |
| 501 | for (;;) { |
| 502 | ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT); |
| 503 | sg = &sgl->sg[sgl->list_pos]; |
| 504 | if (len < sg->length - sgl->page_pos) |
| 505 | to_read = len; |
| 506 | else |
| 507 | to_read = sg->length - sgl->page_pos; |
| 508 | |
| 509 | ret = ubi_eba_read_leb(ubi, vol, lnum, |
| 510 | sg_virt(sg) + sgl->page_pos, offset, |
| 511 | to_read, check); |
| 512 | if (ret < 0) |
| 513 | return ret; |
| 514 | |
| 515 | offset += to_read; |
| 516 | len -= to_read; |
| 517 | if (!len) { |
| 518 | sgl->page_pos += to_read; |
| 519 | if (sgl->page_pos == sg->length) { |
| 520 | sgl->list_pos++; |
| 521 | sgl->page_pos = 0; |
| 522 | } |
| 523 | |
| 524 | break; |
| 525 | } |
| 526 | |
| 527 | sgl->list_pos++; |
| 528 | sgl->page_pos = 0; |
| 529 | } |
| 530 | |
| 531 | return ret; |
| 532 | } |
| 533 | #endif |
| 534 | |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 535 | /** |
| 536 | * recover_peb - recover from write failure. |
| 537 | * @ubi: UBI device description object |
| 538 | * @pnum: the physical eraseblock to recover |
| 539 | * @vol_id: volume ID |
| 540 | * @lnum: logical eraseblock number |
| 541 | * @buf: data which was not written because of the write failure |
| 542 | * @offset: offset of the failed write |
| 543 | * @len: how many bytes should have been written |
| 544 | * |
| 545 | * This function is called in case of a write failure and moves all good data |
| 546 | * from the potentially bad physical eraseblock to a good physical eraseblock. |
| 547 | * This function also writes the data which was not written due to the failure. |
| 548 | * Returns new physical eraseblock number in case of success, and a negative |
| 549 | * error code in case of failure. |
| 550 | */ |
| 551 | static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum, |
| 552 | const void *buf, int offset, int len) |
| 553 | { |
| 554 | int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0; |
| 555 | struct ubi_volume *vol = ubi->volumes[idx]; |
| 556 | struct ubi_vid_hdr *vid_hdr; |
| 557 | |
| 558 | vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 559 | if (!vid_hdr) |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 560 | return -ENOMEM; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 561 | |
| 562 | retry: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 563 | new_pnum = ubi_wl_get_peb(ubi); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 564 | if (new_pnum < 0) { |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 565 | ubi_free_vid_hdr(ubi, vid_hdr); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 566 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 567 | return new_pnum; |
| 568 | } |
| 569 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 570 | ubi_msg(ubi, "recover PEB %d, move data to PEB %d", |
| 571 | pnum, new_pnum); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 572 | |
| 573 | err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1); |
| 574 | if (err && err != UBI_IO_BITFLIPS) { |
| 575 | if (err > 0) |
| 576 | err = -EIO; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 577 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 578 | goto out_put; |
| 579 | } |
| 580 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 581 | vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 582 | err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 583 | if (err) { |
| 584 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 585 | goto write_error; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 586 | } |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 587 | |
| 588 | data_size = offset + len; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 589 | mutex_lock(&ubi->buf_mutex); |
| 590 | memset(ubi->peb_buf + offset, 0xFF, len); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 591 | |
| 592 | /* Read everything before the area where the write failure happened */ |
| 593 | if (offset > 0) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 594 | err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 595 | if (err && err != UBI_IO_BITFLIPS) { |
| 596 | up_read(&ubi->fm_eba_sem); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 597 | goto out_unlock; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 598 | } |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 599 | } |
| 600 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 601 | memcpy(ubi->peb_buf + offset, buf, len); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 602 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 603 | err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size); |
| 604 | if (err) { |
| 605 | mutex_unlock(&ubi->buf_mutex); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 606 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 607 | goto write_error; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 608 | } |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 609 | |
| 610 | mutex_unlock(&ubi->buf_mutex); |
| 611 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 612 | |
| 613 | vol->eba_tbl[lnum] = new_pnum; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 614 | up_read(&ubi->fm_eba_sem); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 615 | ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 616 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 617 | ubi_msg(ubi, "data was successfully recovered"); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 618 | return 0; |
| 619 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 620 | out_unlock: |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 621 | mutex_unlock(&ubi->buf_mutex); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 622 | out_put: |
| 623 | ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 624 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 625 | return err; |
| 626 | |
| 627 | write_error: |
| 628 | /* |
| 629 | * Bad luck? This physical eraseblock is bad too? Crud. Let's try to |
| 630 | * get another one. |
| 631 | */ |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 632 | ubi_warn(ubi, "failed to write to PEB %d", new_pnum); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 633 | ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 634 | if (++tries > UBI_IO_RETRIES) { |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 635 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 636 | return err; |
| 637 | } |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 638 | ubi_msg(ubi, "try again"); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 639 | goto retry; |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * ubi_eba_write_leb - write data to dynamic volume. |
| 644 | * @ubi: UBI device description object |
| 645 | * @vol: volume description object |
| 646 | * @lnum: logical eraseblock number |
| 647 | * @buf: the data to write |
| 648 | * @offset: offset within the logical eraseblock where to write |
| 649 | * @len: how many bytes to write |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 650 | * |
| 651 | * This function writes data to logical eraseblock @lnum of a dynamic volume |
| 652 | * @vol. Returns zero in case of success and a negative error code in case |
| 653 | * of failure. In case of error, it is possible that something was still |
| 654 | * written to the flash media, but may be some garbage. |
| 655 | */ |
| 656 | int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 657 | const void *buf, int offset, int len) |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 658 | { |
| 659 | int err, pnum, tries = 0, vol_id = vol->vol_id; |
| 660 | struct ubi_vid_hdr *vid_hdr; |
| 661 | |
| 662 | if (ubi->ro_mode) |
| 663 | return -EROFS; |
| 664 | |
| 665 | err = leb_write_lock(ubi, vol_id, lnum); |
| 666 | if (err) |
| 667 | return err; |
| 668 | |
| 669 | pnum = vol->eba_tbl[lnum]; |
| 670 | if (pnum >= 0) { |
| 671 | dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d", |
| 672 | len, offset, vol_id, lnum, pnum); |
| 673 | |
| 674 | err = ubi_io_write_data(ubi, buf, pnum, offset, len); |
| 675 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 676 | ubi_warn(ubi, "failed to write data to PEB %d", pnum); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 677 | if (err == -EIO && ubi->bad_allowed) |
| 678 | err = recover_peb(ubi, pnum, vol_id, lnum, buf, |
| 679 | offset, len); |
| 680 | if (err) |
| 681 | ubi_ro_mode(ubi); |
| 682 | } |
| 683 | leb_write_unlock(ubi, vol_id, lnum); |
| 684 | return err; |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * The logical eraseblock is not mapped. We have to get a free physical |
| 689 | * eraseblock and write the volume identifier header there first. |
| 690 | */ |
| 691 | vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); |
| 692 | if (!vid_hdr) { |
| 693 | leb_write_unlock(ubi, vol_id, lnum); |
| 694 | return -ENOMEM; |
| 695 | } |
| 696 | |
| 697 | vid_hdr->vol_type = UBI_VID_DYNAMIC; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 698 | vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 699 | vid_hdr->vol_id = cpu_to_be32(vol_id); |
| 700 | vid_hdr->lnum = cpu_to_be32(lnum); |
| 701 | vid_hdr->compat = ubi_get_compat(ubi, vol_id); |
| 702 | vid_hdr->data_pad = cpu_to_be32(vol->data_pad); |
| 703 | |
| 704 | retry: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 705 | pnum = ubi_wl_get_peb(ubi); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 706 | if (pnum < 0) { |
| 707 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 708 | leb_write_unlock(ubi, vol_id, lnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 709 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 710 | return pnum; |
| 711 | } |
| 712 | |
| 713 | dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d", |
| 714 | len, offset, vol_id, lnum, pnum); |
| 715 | |
| 716 | err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr); |
| 717 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 718 | ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d", |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 719 | vol_id, lnum, pnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 720 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 721 | goto write_error; |
| 722 | } |
| 723 | |
| 724 | if (len) { |
| 725 | err = ubi_io_write_data(ubi, buf, pnum, offset, len); |
| 726 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 727 | ubi_warn(ubi, "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 728 | len, offset, vol_id, lnum, pnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 729 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 730 | goto write_error; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | vol->eba_tbl[lnum] = pnum; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 735 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 736 | |
| 737 | leb_write_unlock(ubi, vol_id, lnum); |
| 738 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 739 | return 0; |
| 740 | |
| 741 | write_error: |
| 742 | if (err != -EIO || !ubi->bad_allowed) { |
| 743 | ubi_ro_mode(ubi); |
| 744 | leb_write_unlock(ubi, vol_id, lnum); |
| 745 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 746 | return err; |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Fortunately, this is the first write operation to this physical |
| 751 | * eraseblock, so just put it and request a new one. We assume that if |
| 752 | * this physical eraseblock went bad, the erase code will handle that. |
| 753 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 754 | err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 755 | if (err || ++tries > UBI_IO_RETRIES) { |
| 756 | ubi_ro_mode(ubi); |
| 757 | leb_write_unlock(ubi, vol_id, lnum); |
| 758 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 759 | return err; |
| 760 | } |
| 761 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 762 | vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 763 | ubi_msg(ubi, "try another PEB"); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 764 | goto retry; |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * ubi_eba_write_leb_st - write data to static volume. |
| 769 | * @ubi: UBI device description object |
| 770 | * @vol: volume description object |
| 771 | * @lnum: logical eraseblock number |
| 772 | * @buf: data to write |
| 773 | * @len: how many bytes to write |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 774 | * @used_ebs: how many logical eraseblocks will this volume contain |
| 775 | * |
| 776 | * This function writes data to logical eraseblock @lnum of static volume |
| 777 | * @vol. The @used_ebs argument should contain total number of logical |
| 778 | * eraseblock in this static volume. |
| 779 | * |
| 780 | * When writing to the last logical eraseblock, the @len argument doesn't have |
| 781 | * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent |
| 782 | * to the real data size, although the @buf buffer has to contain the |
| 783 | * alignment. In all other cases, @len has to be aligned. |
| 784 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 785 | * It is prohibited to write more than once to logical eraseblocks of static |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 786 | * volumes. This function returns zero in case of success and a negative error |
| 787 | * code in case of failure. |
| 788 | */ |
| 789 | int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 790 | int lnum, const void *buf, int len, int used_ebs) |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 791 | { |
| 792 | int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id; |
| 793 | struct ubi_vid_hdr *vid_hdr; |
| 794 | uint32_t crc; |
| 795 | |
| 796 | if (ubi->ro_mode) |
| 797 | return -EROFS; |
| 798 | |
| 799 | if (lnum == used_ebs - 1) |
| 800 | /* If this is the last LEB @len may be unaligned */ |
| 801 | len = ALIGN(data_size, ubi->min_io_size); |
| 802 | else |
| 803 | ubi_assert(!(len & (ubi->min_io_size - 1))); |
| 804 | |
| 805 | vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); |
| 806 | if (!vid_hdr) |
| 807 | return -ENOMEM; |
| 808 | |
| 809 | err = leb_write_lock(ubi, vol_id, lnum); |
| 810 | if (err) { |
| 811 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 812 | return err; |
| 813 | } |
| 814 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 815 | vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 816 | vid_hdr->vol_id = cpu_to_be32(vol_id); |
| 817 | vid_hdr->lnum = cpu_to_be32(lnum); |
| 818 | vid_hdr->compat = ubi_get_compat(ubi, vol_id); |
| 819 | vid_hdr->data_pad = cpu_to_be32(vol->data_pad); |
| 820 | |
| 821 | crc = crc32(UBI_CRC32_INIT, buf, data_size); |
| 822 | vid_hdr->vol_type = UBI_VID_STATIC; |
| 823 | vid_hdr->data_size = cpu_to_be32(data_size); |
| 824 | vid_hdr->used_ebs = cpu_to_be32(used_ebs); |
| 825 | vid_hdr->data_crc = cpu_to_be32(crc); |
| 826 | |
| 827 | retry: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 828 | pnum = ubi_wl_get_peb(ubi); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 829 | if (pnum < 0) { |
| 830 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 831 | leb_write_unlock(ubi, vol_id, lnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 832 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 833 | return pnum; |
| 834 | } |
| 835 | |
| 836 | dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d", |
| 837 | len, vol_id, lnum, pnum, used_ebs); |
| 838 | |
| 839 | err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr); |
| 840 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 841 | ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d", |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 842 | vol_id, lnum, pnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 843 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 844 | goto write_error; |
| 845 | } |
| 846 | |
| 847 | err = ubi_io_write_data(ubi, buf, pnum, 0, len); |
| 848 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 849 | ubi_warn(ubi, "failed to write %d bytes of data to PEB %d", |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 850 | len, pnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 851 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 852 | goto write_error; |
| 853 | } |
| 854 | |
| 855 | ubi_assert(vol->eba_tbl[lnum] < 0); |
| 856 | vol->eba_tbl[lnum] = pnum; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 857 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 858 | |
| 859 | leb_write_unlock(ubi, vol_id, lnum); |
| 860 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 861 | return 0; |
| 862 | |
| 863 | write_error: |
| 864 | if (err != -EIO || !ubi->bad_allowed) { |
| 865 | /* |
| 866 | * This flash device does not admit of bad eraseblocks or |
| 867 | * something nasty and unexpected happened. Switch to read-only |
| 868 | * mode just in case. |
| 869 | */ |
| 870 | ubi_ro_mode(ubi); |
| 871 | leb_write_unlock(ubi, vol_id, lnum); |
| 872 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 873 | return err; |
| 874 | } |
| 875 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 876 | err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 877 | if (err || ++tries > UBI_IO_RETRIES) { |
| 878 | ubi_ro_mode(ubi); |
| 879 | leb_write_unlock(ubi, vol_id, lnum); |
| 880 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 881 | return err; |
| 882 | } |
| 883 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 884 | vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 885 | ubi_msg(ubi, "try another PEB"); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 886 | goto retry; |
| 887 | } |
| 888 | |
| 889 | /* |
| 890 | * ubi_eba_atomic_leb_change - change logical eraseblock atomically. |
| 891 | * @ubi: UBI device description object |
| 892 | * @vol: volume description object |
| 893 | * @lnum: logical eraseblock number |
| 894 | * @buf: data to write |
| 895 | * @len: how many bytes to write |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 896 | * |
| 897 | * This function changes the contents of a logical eraseblock atomically. @buf |
| 898 | * has to contain new logical eraseblock data, and @len - the length of the |
| 899 | * data, which has to be aligned. This function guarantees that in case of an |
| 900 | * unclean reboot the old contents is preserved. Returns zero in case of |
| 901 | * success and a negative error code in case of failure. |
| 902 | * |
| 903 | * UBI reserves one LEB for the "atomic LEB change" operation, so only one |
| 904 | * LEB change may be done at a time. This is ensured by @ubi->alc_mutex. |
| 905 | */ |
| 906 | int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 907 | int lnum, const void *buf, int len) |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 908 | { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 909 | int err, pnum, old_pnum, tries = 0, vol_id = vol->vol_id; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 910 | struct ubi_vid_hdr *vid_hdr; |
| 911 | uint32_t crc; |
| 912 | |
| 913 | if (ubi->ro_mode) |
| 914 | return -EROFS; |
| 915 | |
| 916 | if (len == 0) { |
| 917 | /* |
| 918 | * Special case when data length is zero. In this case the LEB |
| 919 | * has to be unmapped and mapped somewhere else. |
| 920 | */ |
| 921 | err = ubi_eba_unmap_leb(ubi, vol, lnum); |
| 922 | if (err) |
| 923 | return err; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 924 | return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); |
| 928 | if (!vid_hdr) |
| 929 | return -ENOMEM; |
| 930 | |
| 931 | mutex_lock(&ubi->alc_mutex); |
| 932 | err = leb_write_lock(ubi, vol_id, lnum); |
| 933 | if (err) |
| 934 | goto out_mutex; |
| 935 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 936 | vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 937 | vid_hdr->vol_id = cpu_to_be32(vol_id); |
| 938 | vid_hdr->lnum = cpu_to_be32(lnum); |
| 939 | vid_hdr->compat = ubi_get_compat(ubi, vol_id); |
| 940 | vid_hdr->data_pad = cpu_to_be32(vol->data_pad); |
| 941 | |
| 942 | crc = crc32(UBI_CRC32_INIT, buf, len); |
| 943 | vid_hdr->vol_type = UBI_VID_DYNAMIC; |
| 944 | vid_hdr->data_size = cpu_to_be32(len); |
| 945 | vid_hdr->copy_flag = 1; |
| 946 | vid_hdr->data_crc = cpu_to_be32(crc); |
| 947 | |
| 948 | retry: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 949 | pnum = ubi_wl_get_peb(ubi); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 950 | if (pnum < 0) { |
| 951 | err = pnum; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 952 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 953 | goto out_leb_unlock; |
| 954 | } |
| 955 | |
| 956 | dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d", |
| 957 | vol_id, lnum, vol->eba_tbl[lnum], pnum); |
| 958 | |
| 959 | err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr); |
| 960 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 961 | ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d", |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 962 | vol_id, lnum, pnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 963 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 964 | goto write_error; |
| 965 | } |
| 966 | |
| 967 | err = ubi_io_write_data(ubi, buf, pnum, 0, len); |
| 968 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 969 | ubi_warn(ubi, "failed to write %d bytes of data to PEB %d", |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 970 | len, pnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 971 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 972 | goto write_error; |
| 973 | } |
| 974 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 975 | old_pnum = vol->eba_tbl[lnum]; |
| 976 | vol->eba_tbl[lnum] = pnum; |
| 977 | up_read(&ubi->fm_eba_sem); |
| 978 | |
| 979 | if (old_pnum >= 0) { |
| 980 | err = ubi_wl_put_peb(ubi, vol_id, lnum, old_pnum, 0); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 981 | if (err) |
| 982 | goto out_leb_unlock; |
| 983 | } |
| 984 | |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 985 | out_leb_unlock: |
| 986 | leb_write_unlock(ubi, vol_id, lnum); |
| 987 | out_mutex: |
| 988 | mutex_unlock(&ubi->alc_mutex); |
| 989 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 990 | return err; |
| 991 | |
| 992 | write_error: |
| 993 | if (err != -EIO || !ubi->bad_allowed) { |
| 994 | /* |
| 995 | * This flash device does not admit of bad eraseblocks or |
| 996 | * something nasty and unexpected happened. Switch to read-only |
| 997 | * mode just in case. |
| 998 | */ |
| 999 | ubi_ro_mode(ubi); |
| 1000 | goto out_leb_unlock; |
| 1001 | } |
| 1002 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1003 | err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1004 | if (err || ++tries > UBI_IO_RETRIES) { |
| 1005 | ubi_ro_mode(ubi); |
| 1006 | goto out_leb_unlock; |
| 1007 | } |
| 1008 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1009 | vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1010 | ubi_msg(ubi, "try another PEB"); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1011 | goto retry; |
| 1012 | } |
| 1013 | |
| 1014 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1015 | * is_error_sane - check whether a read error is sane. |
| 1016 | * @err: code of the error happened during reading |
| 1017 | * |
| 1018 | * This is a helper function for 'ubi_eba_copy_leb()' which is called when we |
| 1019 | * cannot read data from the target PEB (an error @err happened). If the error |
| 1020 | * code is sane, then we treat this error as non-fatal. Otherwise the error is |
| 1021 | * fatal and UBI will be switched to R/O mode later. |
| 1022 | * |
| 1023 | * The idea is that we try not to switch to R/O mode if the read error is |
| 1024 | * something which suggests there was a real read problem. E.g., %-EIO. Or a |
| 1025 | * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O |
| 1026 | * mode, simply because we do not know what happened at the MTD level, and we |
| 1027 | * cannot handle this. E.g., the underlying driver may have become crazy, and |
| 1028 | * it is safer to switch to R/O mode to preserve the data. |
| 1029 | * |
| 1030 | * And bear in mind, this is about reading from the target PEB, i.e. the PEB |
| 1031 | * which we have just written. |
| 1032 | */ |
| 1033 | static int is_error_sane(int err) |
| 1034 | { |
| 1035 | if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR || |
| 1036 | err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT) |
| 1037 | return 0; |
| 1038 | return 1; |
| 1039 | } |
| 1040 | |
| 1041 | /** |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1042 | * ubi_eba_copy_leb - copy logical eraseblock. |
| 1043 | * @ubi: UBI device description object |
| 1044 | * @from: physical eraseblock number from where to copy |
| 1045 | * @to: physical eraseblock number where to copy |
| 1046 | * @vid_hdr: VID header of the @from physical eraseblock |
| 1047 | * |
| 1048 | * This function copies logical eraseblock from physical eraseblock @from to |
| 1049 | * physical eraseblock @to. The @vid_hdr buffer may be changed by this |
| 1050 | * function. Returns: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1051 | * o %0 in case of success; |
| 1052 | * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc; |
| 1053 | * o a negative error code in case of failure. |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1054 | */ |
| 1055 | int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, |
| 1056 | struct ubi_vid_hdr *vid_hdr) |
| 1057 | { |
| 1058 | int err, vol_id, lnum, data_size, aldata_size, idx; |
| 1059 | struct ubi_volume *vol; |
| 1060 | uint32_t crc; |
| 1061 | |
| 1062 | vol_id = be32_to_cpu(vid_hdr->vol_id); |
| 1063 | lnum = be32_to_cpu(vid_hdr->lnum); |
| 1064 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1065 | dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1066 | |
| 1067 | if (vid_hdr->vol_type == UBI_VID_STATIC) { |
| 1068 | data_size = be32_to_cpu(vid_hdr->data_size); |
| 1069 | aldata_size = ALIGN(data_size, ubi->min_io_size); |
| 1070 | } else |
| 1071 | data_size = aldata_size = |
| 1072 | ubi->leb_size - be32_to_cpu(vid_hdr->data_pad); |
| 1073 | |
| 1074 | idx = vol_id2idx(ubi, vol_id); |
| 1075 | spin_lock(&ubi->volumes_lock); |
| 1076 | /* |
| 1077 | * Note, we may race with volume deletion, which means that the volume |
| 1078 | * this logical eraseblock belongs to might be being deleted. Since the |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1079 | * volume deletion un-maps all the volume's logical eraseblocks, it will |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1080 | * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish. |
| 1081 | */ |
| 1082 | vol = ubi->volumes[idx]; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1083 | spin_unlock(&ubi->volumes_lock); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1084 | if (!vol) { |
| 1085 | /* No need to do further work, cancel */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1086 | dbg_wl("volume %d is being removed, cancel", vol_id); |
| 1087 | return MOVE_CANCEL_RACE; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1088 | } |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1089 | |
| 1090 | /* |
| 1091 | * We do not want anybody to write to this logical eraseblock while we |
| 1092 | * are moving it, so lock it. |
| 1093 | * |
| 1094 | * Note, we are using non-waiting locking here, because we cannot sleep |
| 1095 | * on the LEB, since it may cause deadlocks. Indeed, imagine a task is |
| 1096 | * unmapping the LEB which is mapped to the PEB we are going to move |
| 1097 | * (@from). This task locks the LEB and goes sleep in the |
| 1098 | * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are |
| 1099 | * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1100 | * LEB is already locked, we just do not move it and return |
| 1101 | * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because |
| 1102 | * we do not know the reasons of the contention - it may be just a |
| 1103 | * normal I/O on this LEB, so we want to re-try. |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1104 | */ |
| 1105 | err = leb_write_trylock(ubi, vol_id, lnum); |
| 1106 | if (err) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1107 | dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum); |
| 1108 | return MOVE_RETRY; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * The LEB might have been put meanwhile, and the task which put it is |
| 1113 | * probably waiting on @ubi->move_mutex. No need to continue the work, |
| 1114 | * cancel it. |
| 1115 | */ |
| 1116 | if (vol->eba_tbl[lnum] != from) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1117 | dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel", |
| 1118 | vol_id, lnum, from, vol->eba_tbl[lnum]); |
| 1119 | err = MOVE_CANCEL_RACE; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1120 | goto out_unlock_leb; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1124 | * OK, now the LEB is locked and we can safely start moving it. Since |
| 1125 | * this function utilizes the @ubi->peb_buf buffer which is shared |
| 1126 | * with some other functions - we lock the buffer by taking the |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1127 | * @ubi->buf_mutex. |
| 1128 | */ |
| 1129 | mutex_lock(&ubi->buf_mutex); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1130 | dbg_wl("read %d bytes of data", aldata_size); |
| 1131 | err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1132 | if (err && err != UBI_IO_BITFLIPS) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1133 | ubi_warn(ubi, "error %d while reading data from PEB %d", |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1134 | err, from); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1135 | err = MOVE_SOURCE_RD_ERR; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1136 | goto out_unlock_buf; |
| 1137 | } |
| 1138 | |
| 1139 | /* |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1140 | * Now we have got to calculate how much data we have to copy. In |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1141 | * case of a static volume it is fairly easy - the VID header contains |
| 1142 | * the data size. In case of a dynamic volume it is more difficult - we |
| 1143 | * have to read the contents, cut 0xFF bytes from the end and copy only |
| 1144 | * the first part. We must do this to avoid writing 0xFF bytes as it |
| 1145 | * may have some side-effects. And not only this. It is important not |
| 1146 | * to include those 0xFFs to CRC because later the they may be filled |
| 1147 | * by data. |
| 1148 | */ |
| 1149 | if (vid_hdr->vol_type == UBI_VID_DYNAMIC) |
| 1150 | aldata_size = data_size = |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1151 | ubi_calc_data_len(ubi, ubi->peb_buf, data_size); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1152 | |
| 1153 | cond_resched(); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1154 | crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1155 | cond_resched(); |
| 1156 | |
| 1157 | /* |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1158 | * It may turn out to be that the whole @from physical eraseblock |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1159 | * contains only 0xFF bytes. Then we have to only write the VID header |
| 1160 | * and do not write any data. This also means we should not set |
| 1161 | * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc. |
| 1162 | */ |
| 1163 | if (data_size > 0) { |
| 1164 | vid_hdr->copy_flag = 1; |
| 1165 | vid_hdr->data_size = cpu_to_be32(data_size); |
| 1166 | vid_hdr->data_crc = cpu_to_be32(crc); |
| 1167 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1168 | vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1169 | |
| 1170 | err = ubi_io_write_vid_hdr(ubi, to, vid_hdr); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1171 | if (err) { |
| 1172 | if (err == -EIO) |
| 1173 | err = MOVE_TARGET_WR_ERR; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1174 | goto out_unlock_buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1175 | } |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1176 | |
| 1177 | cond_resched(); |
| 1178 | |
| 1179 | /* Read the VID header back and check if it was written correctly */ |
| 1180 | err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1); |
| 1181 | if (err) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1182 | if (err != UBI_IO_BITFLIPS) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1183 | ubi_warn(ubi, "error %d while reading VID header back from PEB %d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1184 | err, to); |
| 1185 | if (is_error_sane(err)) |
| 1186 | err = MOVE_TARGET_RD_ERR; |
| 1187 | } else |
| 1188 | err = MOVE_TARGET_BITFLIPS; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1189 | goto out_unlock_buf; |
| 1190 | } |
| 1191 | |
| 1192 | if (data_size > 0) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1193 | err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size); |
| 1194 | if (err) { |
| 1195 | if (err == -EIO) |
| 1196 | err = MOVE_TARGET_WR_ERR; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1197 | goto out_unlock_buf; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1198 | } |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1199 | |
| 1200 | cond_resched(); |
| 1201 | |
| 1202 | /* |
| 1203 | * We've written the data and are going to read it back to make |
| 1204 | * sure it was written correctly. |
| 1205 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1206 | memset(ubi->peb_buf, 0xFF, aldata_size); |
| 1207 | err = ubi_io_read_data(ubi, ubi->peb_buf, to, 0, aldata_size); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1208 | if (err) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1209 | if (err != UBI_IO_BITFLIPS) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1210 | ubi_warn(ubi, "error %d while reading data back from PEB %d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1211 | err, to); |
| 1212 | if (is_error_sane(err)) |
| 1213 | err = MOVE_TARGET_RD_ERR; |
| 1214 | } else |
| 1215 | err = MOVE_TARGET_BITFLIPS; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1216 | goto out_unlock_buf; |
| 1217 | } |
| 1218 | |
| 1219 | cond_resched(); |
| 1220 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1221 | if (crc != crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size)) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1222 | ubi_warn(ubi, "read data back from PEB %d and it is different", |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1223 | to); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1224 | err = -EINVAL; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1225 | goto out_unlock_buf; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | ubi_assert(vol->eba_tbl[lnum] == from); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1230 | down_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1231 | vol->eba_tbl[lnum] = to; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1232 | up_read(&ubi->fm_eba_sem); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1233 | |
| 1234 | out_unlock_buf: |
| 1235 | mutex_unlock(&ubi->buf_mutex); |
| 1236 | out_unlock_leb: |
| 1237 | leb_write_unlock(ubi, vol_id, lnum); |
| 1238 | return err; |
| 1239 | } |
| 1240 | |
| 1241 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1242 | * print_rsvd_warning - warn about not having enough reserved PEBs. |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1243 | * @ubi: UBI device description object |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1244 | * |
| 1245 | * This is a helper function for 'ubi_eba_init()' which is called when UBI |
| 1246 | * cannot reserve enough PEBs for bad block handling. This function makes a |
| 1247 | * decision whether we have to print a warning or not. The algorithm is as |
| 1248 | * follows: |
| 1249 | * o if this is a new UBI image, then just print the warning |
| 1250 | * o if this is an UBI image which has already been used for some time, print |
| 1251 | * a warning only if we can reserve less than 10% of the expected amount of |
| 1252 | * the reserved PEB. |
| 1253 | * |
| 1254 | * The idea is that when UBI is used, PEBs become bad, and the reserved pool |
| 1255 | * of PEBs becomes smaller, which is normal and we do not want to scare users |
| 1256 | * with a warning every time they attach the MTD device. This was an issue |
| 1257 | * reported by real users. |
| 1258 | */ |
| 1259 | static void print_rsvd_warning(struct ubi_device *ubi, |
| 1260 | struct ubi_attach_info *ai) |
| 1261 | { |
| 1262 | /* |
| 1263 | * The 1 << 18 (256KiB) number is picked randomly, just a reasonably |
| 1264 | * large number to distinguish between newly flashed and used images. |
| 1265 | */ |
| 1266 | if (ai->max_sqnum > (1 << 18)) { |
| 1267 | int min = ubi->beb_rsvd_level / 10; |
| 1268 | |
| 1269 | if (!min) |
| 1270 | min = 1; |
| 1271 | if (ubi->beb_rsvd_pebs > min) |
| 1272 | return; |
| 1273 | } |
| 1274 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1275 | ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1276 | ubi->beb_rsvd_pebs, ubi->beb_rsvd_level); |
| 1277 | if (ubi->corr_peb_count) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1278 | ubi_warn(ubi, "%d PEBs are corrupted and not used", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1279 | ubi->corr_peb_count); |
| 1280 | } |
| 1281 | |
| 1282 | /** |
| 1283 | * self_check_eba - run a self check on the EBA table constructed by fastmap. |
| 1284 | * @ubi: UBI device description object |
| 1285 | * @ai_fastmap: UBI attach info object created by fastmap |
| 1286 | * @ai_scan: UBI attach info object created by scanning |
| 1287 | * |
| 1288 | * Returns < 0 in case of an internal error, 0 otherwise. |
| 1289 | * If a bad EBA table entry was found it will be printed out and |
| 1290 | * ubi_assert() triggers. |
| 1291 | */ |
| 1292 | int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap, |
| 1293 | struct ubi_attach_info *ai_scan) |
| 1294 | { |
| 1295 | int i, j, num_volumes, ret = 0; |
| 1296 | int **scan_eba, **fm_eba; |
| 1297 | struct ubi_ainf_volume *av; |
| 1298 | struct ubi_volume *vol; |
| 1299 | struct ubi_ainf_peb *aeb; |
| 1300 | struct rb_node *rb; |
| 1301 | |
| 1302 | num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; |
| 1303 | |
| 1304 | scan_eba = kmalloc(sizeof(*scan_eba) * num_volumes, GFP_KERNEL); |
| 1305 | if (!scan_eba) |
| 1306 | return -ENOMEM; |
| 1307 | |
| 1308 | fm_eba = kmalloc(sizeof(*fm_eba) * num_volumes, GFP_KERNEL); |
| 1309 | if (!fm_eba) { |
| 1310 | kfree(scan_eba); |
| 1311 | return -ENOMEM; |
| 1312 | } |
| 1313 | |
| 1314 | for (i = 0; i < num_volumes; i++) { |
| 1315 | vol = ubi->volumes[i]; |
| 1316 | if (!vol) |
| 1317 | continue; |
| 1318 | |
| 1319 | scan_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**scan_eba), |
| 1320 | GFP_KERNEL); |
| 1321 | if (!scan_eba[i]) { |
| 1322 | ret = -ENOMEM; |
| 1323 | goto out_free; |
| 1324 | } |
| 1325 | |
| 1326 | fm_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**fm_eba), |
| 1327 | GFP_KERNEL); |
| 1328 | if (!fm_eba[i]) { |
| 1329 | ret = -ENOMEM; |
| 1330 | goto out_free; |
| 1331 | } |
| 1332 | |
| 1333 | for (j = 0; j < vol->reserved_pebs; j++) |
| 1334 | scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED; |
| 1335 | |
| 1336 | av = ubi_find_av(ai_scan, idx2vol_id(ubi, i)); |
| 1337 | if (!av) |
| 1338 | continue; |
| 1339 | |
| 1340 | ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) |
| 1341 | scan_eba[i][aeb->lnum] = aeb->pnum; |
| 1342 | |
| 1343 | av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i)); |
| 1344 | if (!av) |
| 1345 | continue; |
| 1346 | |
| 1347 | ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) |
| 1348 | fm_eba[i][aeb->lnum] = aeb->pnum; |
| 1349 | |
| 1350 | for (j = 0; j < vol->reserved_pebs; j++) { |
| 1351 | if (scan_eba[i][j] != fm_eba[i][j]) { |
| 1352 | if (scan_eba[i][j] == UBI_LEB_UNMAPPED || |
| 1353 | fm_eba[i][j] == UBI_LEB_UNMAPPED) |
| 1354 | continue; |
| 1355 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1356 | ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1357 | vol->vol_id, i, fm_eba[i][j], |
| 1358 | scan_eba[i][j]); |
| 1359 | ubi_assert(0); |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | out_free: |
| 1365 | for (i = 0; i < num_volumes; i++) { |
| 1366 | if (!ubi->volumes[i]) |
| 1367 | continue; |
| 1368 | |
| 1369 | kfree(scan_eba[i]); |
| 1370 | kfree(fm_eba[i]); |
| 1371 | } |
| 1372 | |
| 1373 | kfree(scan_eba); |
| 1374 | kfree(fm_eba); |
| 1375 | return ret; |
| 1376 | } |
| 1377 | |
| 1378 | /** |
| 1379 | * ubi_eba_init - initialize the EBA sub-system using attaching information. |
| 1380 | * @ubi: UBI device description object |
| 1381 | * @ai: attaching information |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1382 | * |
| 1383 | * This function returns zero in case of success and a negative error code in |
| 1384 | * case of failure. |
| 1385 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1386 | int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai) |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1387 | { |
| 1388 | int i, j, err, num_volumes; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1389 | struct ubi_ainf_volume *av; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1390 | struct ubi_volume *vol; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1391 | struct ubi_ainf_peb *aeb; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1392 | struct rb_node *rb; |
| 1393 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1394 | dbg_eba("initialize EBA sub-system"); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1395 | |
| 1396 | spin_lock_init(&ubi->ltree_lock); |
| 1397 | mutex_init(&ubi->alc_mutex); |
| 1398 | ubi->ltree = RB_ROOT; |
| 1399 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1400 | ubi->global_sqnum = ai->max_sqnum + 1; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1401 | num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; |
| 1402 | |
| 1403 | for (i = 0; i < num_volumes; i++) { |
| 1404 | vol = ubi->volumes[i]; |
| 1405 | if (!vol) |
| 1406 | continue; |
| 1407 | |
| 1408 | cond_resched(); |
| 1409 | |
| 1410 | vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), |
| 1411 | GFP_KERNEL); |
| 1412 | if (!vol->eba_tbl) { |
| 1413 | err = -ENOMEM; |
| 1414 | goto out_free; |
| 1415 | } |
| 1416 | |
| 1417 | for (j = 0; j < vol->reserved_pebs; j++) |
| 1418 | vol->eba_tbl[j] = UBI_LEB_UNMAPPED; |
| 1419 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1420 | av = ubi_find_av(ai, idx2vol_id(ubi, i)); |
| 1421 | if (!av) |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1422 | continue; |
| 1423 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1424 | ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) { |
| 1425 | if (aeb->lnum >= vol->reserved_pebs) |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1426 | /* |
| 1427 | * This may happen in case of an unclean reboot |
| 1428 | * during re-size. |
| 1429 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1430 | ubi_move_aeb_to_list(av, aeb, &ai->erase); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1431 | else |
| 1432 | vol->eba_tbl[aeb->lnum] = aeb->pnum; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | if (ubi->avail_pebs < EBA_RESERVED_PEBS) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1437 | ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)", |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1438 | ubi->avail_pebs, EBA_RESERVED_PEBS); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1439 | if (ubi->corr_peb_count) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1440 | ubi_err(ubi, "%d PEBs are corrupted and not used", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1441 | ubi->corr_peb_count); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1442 | err = -ENOSPC; |
| 1443 | goto out_free; |
| 1444 | } |
| 1445 | ubi->avail_pebs -= EBA_RESERVED_PEBS; |
| 1446 | ubi->rsvd_pebs += EBA_RESERVED_PEBS; |
| 1447 | |
| 1448 | if (ubi->bad_allowed) { |
| 1449 | ubi_calculate_reserved(ubi); |
| 1450 | |
| 1451 | if (ubi->avail_pebs < ubi->beb_rsvd_level) { |
| 1452 | /* No enough free physical eraseblocks */ |
| 1453 | ubi->beb_rsvd_pebs = ubi->avail_pebs; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1454 | print_rsvd_warning(ubi, ai); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1455 | } else |
| 1456 | ubi->beb_rsvd_pebs = ubi->beb_rsvd_level; |
| 1457 | |
| 1458 | ubi->avail_pebs -= ubi->beb_rsvd_pebs; |
| 1459 | ubi->rsvd_pebs += ubi->beb_rsvd_pebs; |
| 1460 | } |
| 1461 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1462 | dbg_eba("EBA sub-system is initialized"); |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1463 | return 0; |
| 1464 | |
| 1465 | out_free: |
| 1466 | for (i = 0; i < num_volumes; i++) { |
| 1467 | if (!ubi->volumes[i]) |
| 1468 | continue; |
| 1469 | kfree(ubi->volumes[i]->eba_tbl); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1470 | ubi->volumes[i]->eba_tbl = NULL; |
Kyungmin Park | c880c53 | 2008-11-19 16:25:44 +0100 | [diff] [blame] | 1471 | } |
| 1472 | return err; |
| 1473 | } |