eba.c 46.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (c) International Business Machines Corp., 2006
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Author: Artem Bityutskiy (Битюцкий Артём)
 */

/*
Artem Bityutskiy's avatar
Artem Bityutskiy committed
22
 * The UBI Eraseblock Association (EBA) sub-system.
23
 *
Artem Bityutskiy's avatar
Artem Bityutskiy committed
24
 * This sub-system is responsible for I/O to/from logical eraseblock.
25 26 27 28 29
 *
 * Although in this implementation the EBA table is fully kept and managed in
 * RAM, which assumes poor scalability, it might be (partially) maintained on
 * flash in future implementations.
 *
Artem Bityutskiy's avatar
Artem Bityutskiy committed
30 31 32 33 34 35
 * The EBA sub-system implements per-logical eraseblock locking. Before
 * accessing a logical eraseblock it is locked for reading or writing. The
 * per-logical eraseblock locking is implemented by means of the lock tree. The
 * lock tree is an RB-tree which refers all the currently locked logical
 * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects.
 * They are indexed by (@vol_id, @lnum) pairs.
36 37 38 39 40 41 42 43 44 45 46 47 48
 *
 * EBA also maintains the global sequence counter which is incremented each
 * time a logical eraseblock is mapped to a physical eraseblock and it is
 * stored in the volume identifier header. This means that each VID header has
 * a unique sequence number. The sequence number is only increased an we assume
 * 64 bits is enough to never overflow.
 */

#include <linux/slab.h>
#include <linux/crc32.h>
#include <linux/err.h>
#include "ubi.h"

49 50 51
/* Number of physical eraseblocks reserved for atomic LEB change operation */
#define EBA_RESERVED_PEBS 1

Boris Brezillon's avatar
Boris Brezillon committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/**
 * struct ubi_eba_entry - structure encoding a single LEB -> PEB association
 * @pnum: the physical eraseblock number attached to the LEB
 *
 * This structure is encoding a LEB -> PEB association. Note that the LEB
 * number is not stored here, because it is the index used to access the
 * entries table.
 */
struct ubi_eba_entry {
	int pnum;
};

/**
 * struct ubi_eba_table - LEB -> PEB association information
 * @entries: the LEB to PEB mapping (one entry per LEB).
 *
 * This structure is private to the EBA logic and should be kept here.
 * It is encoding the LEB to PEB association table, and is subject to
 * changes.
 */
struct ubi_eba_table {
	struct ubi_eba_entry *entries;
};

76 77 78 79 80 81 82 83
/**
 * next_sqnum - get next sequence number.
 * @ubi: UBI device description object
 *
 * This function returns next sequence number to use, which is just the current
 * global sequence counter value. It also increases the global sequence
 * counter.
 */
84
unsigned long long ubi_next_sqnum(struct ubi_device *ubi)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
	unsigned long long sqnum;

	spin_lock(&ubi->ltree_lock);
	sqnum = ubi->global_sqnum++;
	spin_unlock(&ubi->ltree_lock);

	return sqnum;
}

/**
 * ubi_get_compat - get compatibility flags of a volume.
 * @ubi: UBI device description object
 * @vol_id: volume ID
 *
 * This function returns compatibility flags for an internal volume. User
 * volumes have no compatibility flags, so %0 is returned.
 */
static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
{
105
	if (vol_id == UBI_LAYOUT_VOLUME_ID)
106 107 108 109
		return UBI_LAYOUT_VOLUME_COMPAT;
	return 0;
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123
/**
 * ubi_eba_get_ldesc - get information about a LEB
 * @vol: volume description object
 * @lnum: logical eraseblock number
 * @ldesc: the LEB descriptor to fill
 *
 * Used to query information about a specific LEB.
 * It is currently only returning the physical position of the LEB, but will be
 * extended to provide more information.
 */
void ubi_eba_get_ldesc(struct ubi_volume *vol, int lnum,
		       struct ubi_eba_leb_desc *ldesc)
{
	ldesc->lnum = lnum;
Boris Brezillon's avatar
Boris Brezillon committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	ldesc->pnum = vol->eba_tbl->entries[lnum].pnum;
}

/**
 * ubi_eba_create_table - allocate a new EBA table and initialize it with all
 *			  LEBs unmapped
 * @vol: volume containing the EBA table to copy
 * @nentries: number of entries in the table
 *
 * Allocate a new EBA table and initialize it with all LEBs unmapped.
 * Returns a valid pointer if it succeed, an ERR_PTR() otherwise.
 */
struct ubi_eba_table *ubi_eba_create_table(struct ubi_volume *vol,
					   int nentries)
{
	struct ubi_eba_table *tbl;
	int err = -ENOMEM;
	int i;

	tbl = kzalloc(sizeof(*tbl), GFP_KERNEL);
	if (!tbl)
		return ERR_PTR(-ENOMEM);

	tbl->entries = kmalloc_array(nentries, sizeof(*tbl->entries),
				     GFP_KERNEL);
	if (!tbl->entries)
		goto err;

	for (i = 0; i < nentries; i++)
		tbl->entries[i].pnum = UBI_LEB_UNMAPPED;

	return tbl;

err:
	kfree(tbl->entries);
	kfree(tbl);

	return ERR_PTR(err);
}

/**
 * ubi_eba_destroy_table - destroy an EBA table
 * @tbl: the table to destroy
 *
 * Destroy an EBA table.
 */
void ubi_eba_destroy_table(struct ubi_eba_table *tbl)
{
	if (!tbl)
		return;

	kfree(tbl->entries);
	kfree(tbl);
}

/**
 * ubi_eba_copy_table - copy the EBA table attached to vol into another table
 * @vol: volume containing the EBA table to copy
 * @dst: destination
 * @nentries: number of entries to copy
 *
 * Copy the EBA table stored in vol into the one pointed by dst.
 */
void ubi_eba_copy_table(struct ubi_volume *vol, struct ubi_eba_table *dst,
			int nentries)
{
	struct ubi_eba_table *src;
	int i;

	ubi_assert(dst && vol && vol->eba_tbl);

	src = vol->eba_tbl;

	for (i = 0; i < nentries; i++)
		dst->entries[i].pnum = src->entries[i].pnum;
}

/**
 * ubi_eba_replace_table - assign a new EBA table to a volume
 * @vol: volume containing the EBA table to copy
 * @tbl: new EBA table
 *
 * Assign a new EBA table to the volume and release the old one.
 */
void ubi_eba_replace_table(struct ubi_volume *vol, struct ubi_eba_table *tbl)
{
	ubi_eba_destroy_table(vol->eba_tbl);
	vol->eba_tbl = tbl;
212 213
}

214 215 216 217 218 219
/**
 * ltree_lookup - look up the lock tree.
 * @ubi: UBI device description object
 * @vol_id: volume ID
 * @lnum: logical eraseblock number
 *
220
 * This function returns a pointer to the corresponding &struct ubi_ltree_entry
221 222 223
 * object if the logical eraseblock is locked and %NULL if it is not.
 * @ubi->ltree_lock has to be locked.
 */
224 225
static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
					    int lnum)
226 227 228 229 230
{
	struct rb_node *p;

	p = ubi->ltree.rb_node;
	while (p) {
231
		struct ubi_ltree_entry *le;
232

233
		le = rb_entry(p, struct ubi_ltree_entry, rb);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

		if (vol_id < le->vol_id)
			p = p->rb_left;
		else if (vol_id > le->vol_id)
			p = p->rb_right;
		else {
			if (lnum < le->lnum)
				p = p->rb_left;
			else if (lnum > le->lnum)
				p = p->rb_right;
			else
				return le;
		}
	}

	return NULL;
}

/**
 * ltree_add_entry - add new entry to the lock tree.
 * @ubi: UBI device description object
 * @vol_id: volume ID
 * @lnum: logical eraseblock number
 *
 * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
 * lock tree. If such entry is already there, its usage counter is increased.
 * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
 * failed.
 */
263 264
static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
					       int vol_id, int lnum)
265
{
266
	struct ubi_ltree_entry *le, *le1, *le_free;
267

268
	le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
269 270 271
	if (!le)
		return ERR_PTR(-ENOMEM);

272 273
	le->users = 0;
	init_rwsem(&le->mutex);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	le->vol_id = vol_id;
	le->lnum = lnum;

	spin_lock(&ubi->ltree_lock);
	le1 = ltree_lookup(ubi, vol_id, lnum);

	if (le1) {
		/*
		 * This logical eraseblock is already locked. The newly
		 * allocated lock entry is not needed.
		 */
		le_free = le;
		le = le1;
	} else {
		struct rb_node **p, *parent = NULL;

		/*
		 * No lock entry, add the newly allocated one to the
		 * @ubi->ltree RB-tree.
		 */
		le_free = NULL;

		p = &ubi->ltree.rb_node;
		while (*p) {
			parent = *p;
299
			le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

			if (vol_id < le1->vol_id)
				p = &(*p)->rb_left;
			else if (vol_id > le1->vol_id)
				p = &(*p)->rb_right;
			else {
				ubi_assert(lnum != le1->lnum);
				if (lnum < le1->lnum)
					p = &(*p)->rb_left;
				else
					p = &(*p)->rb_right;
			}
		}

		rb_link_node(&le->rb, parent, p);
		rb_insert_color(&le->rb, &ubi->ltree);
	}
	le->users += 1;
	spin_unlock(&ubi->ltree_lock);

320
	kfree(le_free);
321 322 323 324 325 326 327 328 329 330 331 332 333 334
	return le;
}

/**
 * leb_read_lock - lock logical eraseblock for reading.
 * @ubi: UBI device description object
 * @vol_id: volume ID
 * @lnum: logical eraseblock number
 *
 * This function locks a logical eraseblock for reading. Returns zero in case
 * of success and a negative error code in case of failure.
 */
static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
{
335
	struct ubi_ltree_entry *le;
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

	le = ltree_add_entry(ubi, vol_id, lnum);
	if (IS_ERR(le))
		return PTR_ERR(le);
	down_read(&le->mutex);
	return 0;
}

/**
 * leb_read_unlock - unlock logical eraseblock.
 * @ubi: UBI device description object
 * @vol_id: volume ID
 * @lnum: logical eraseblock number
 */
static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
{
352
	struct ubi_ltree_entry *le;
353 354 355 356 357

	spin_lock(&ubi->ltree_lock);
	le = ltree_lookup(ubi, vol_id, lnum);
	le->users -= 1;
	ubi_assert(le->users >= 0);
Artem Bityutskiy's avatar
Artem Bityutskiy committed
358
	up_read(&le->mutex);
359 360
	if (le->users == 0) {
		rb_erase(&le->rb, &ubi->ltree);
Artem Bityutskiy's avatar
Artem Bityutskiy committed
361
		kfree(le);
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
	}
	spin_unlock(&ubi->ltree_lock);
}

/**
 * leb_write_lock - lock logical eraseblock for writing.
 * @ubi: UBI device description object
 * @vol_id: volume ID
 * @lnum: logical eraseblock number
 *
 * This function locks a logical eraseblock for writing. Returns zero in case
 * of success and a negative error code in case of failure.
 */
static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
{
377
	struct ubi_ltree_entry *le;
378 379 380 381 382 383 384 385

	le = ltree_add_entry(ubi, vol_id, lnum);
	if (IS_ERR(le))
		return PTR_ERR(le);
	down_write(&le->mutex);
	return 0;
}

386
/**
387
 * leb_write_trylock - try to lock logical eraseblock for writing.
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
 * @ubi: UBI device description object
 * @vol_id: volume ID
 * @lnum: logical eraseblock number
 *
 * This function locks a logical eraseblock for writing if there is no
 * contention and does nothing if there is contention. Returns %0 in case of
 * success, %1 in case of contention, and and a negative error code in case of
 * failure.
 */
static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
{
	struct ubi_ltree_entry *le;

	le = ltree_add_entry(ubi, vol_id, lnum);
	if (IS_ERR(le))
		return PTR_ERR(le);
	if (down_write_trylock(&le->mutex))
		return 0;

	/* Contention, cancel */
	spin_lock(&ubi->ltree_lock);
	le->users -= 1;
	ubi_assert(le->users >= 0);
	if (le->users == 0) {
		rb_erase(&le->rb, &ubi->ltree);
413
		kfree(le);
Artem Bityutskiy's avatar
Artem Bityutskiy committed
414 415
	}
	spin_unlock(&ubi->ltree_lock);
416 417 418 419

	return 1;
}

420 421 422 423 424 425 426 427
/**
 * leb_write_unlock - unlock logical eraseblock.
 * @ubi: UBI device description object
 * @vol_id: volume ID
 * @lnum: logical eraseblock number
 */
static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
{
428
	struct ubi_ltree_entry *le;
429 430 431 432 433

	spin_lock(&ubi->ltree_lock);
	le = ltree_lookup(ubi, vol_id, lnum);
	le->users -= 1;
	ubi_assert(le->users >= 0);
Artem Bityutskiy's avatar
Artem Bityutskiy committed
434
	up_write(&le->mutex);
435 436
	if (le->users == 0) {
		rb_erase(&le->rb, &ubi->ltree);
437
		kfree(le);
Artem Bityutskiy's avatar
Artem Bityutskiy committed
438 439
	}
	spin_unlock(&ubi->ltree_lock);
440 441
}

442 443 444 445 446 447 448 449 450
/**
 * ubi_eba_is_mapped - check if a LEB is mapped.
 * @vol: volume description object
 * @lnum: logical eraseblock number
 *
 * This function returns true if the LEB is mapped, false otherwise.
 */
bool ubi_eba_is_mapped(struct ubi_volume *vol, int lnum)
{
Boris Brezillon's avatar
Boris Brezillon committed
451
	return vol->eba_tbl->entries[lnum].pnum >= 0;
452 453
}

454 455 456
/**
 * ubi_eba_unmap_leb - un-map logical eraseblock.
 * @ubi: UBI device description object
457
 * @vol: volume description object
458 459 460 461 462 463
 * @lnum: logical eraseblock number
 *
 * This function un-maps logical eraseblock @lnum and schedules corresponding
 * physical eraseblock for erasure. Returns zero in case of success and a
 * negative error code in case of failure.
 */
464 465
int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
		      int lnum)
466
{
467
	int err, pnum, vol_id = vol->vol_id;
468 469 470 471 472 473 474 475

	if (ubi->ro_mode)
		return -EROFS;

	err = leb_write_lock(ubi, vol_id, lnum);
	if (err)
		return err;

Boris Brezillon's avatar
Boris Brezillon committed
476
	pnum = vol->eba_tbl->entries[lnum].pnum;
477 478 479 480 481 482
	if (pnum < 0)
		/* This logical eraseblock is already unmapped */
		goto out_unlock;

	dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);

483
	down_read(&ubi->fm_eba_sem);
Boris Brezillon's avatar
Boris Brezillon committed
484
	vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED;
485
	up_read(&ubi->fm_eba_sem);
486
	err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
487 488 489 490 491 492

out_unlock:
	leb_write_unlock(ubi, vol_id, lnum);
	return err;
}

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
#ifdef CONFIG_MTD_UBI_FASTMAP
/**
 * check_mapping - check and fixup a mapping
 * @ubi: UBI device description object
 * @vol: volume description object
 * @lnum: logical eraseblock number
 * @pnum: physical eraseblock number
 *
 * Checks whether a given mapping is valid. Fastmap cannot track LEB unmap
 * operations, if such an operation is interrupted the mapping still looks
 * good, but upon first read an ECC is reported to the upper layer.
 * Normaly during the full-scan at attach time this is fixed, for Fastmap
 * we have to deal with it while reading.
 * If the PEB behind a LEB shows this symthom we change the mapping to
 * %UBI_LEB_UNMAPPED and schedule the PEB for erasure.
 *
 * Returns 0 on success, negative error code in case of failure.
 */
static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
			 int *pnum)
{
	int err;
	struct ubi_vid_io_buf *vidb;
516
	struct ubi_vid_hdr *vid_hdr;
517 518 519 520

	if (!ubi->fast_attach)
		return 0;

521 522 523
	if (!vol->checkmap || test_bit(lnum, vol->checkmap))
		return 0;

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
	if (!vidb)
		return -ENOMEM;

	err = ubi_io_read_vid_hdr(ubi, *pnum, vidb, 0);
	if (err > 0 && err != UBI_IO_BITFLIPS) {
		int torture = 0;

		switch (err) {
			case UBI_IO_FF:
			case UBI_IO_FF_BITFLIPS:
			case UBI_IO_BAD_HDR:
			case UBI_IO_BAD_HDR_EBADMSG:
				break;
			default:
				ubi_assert(0);
		}

		if (err == UBI_IO_BAD_HDR_EBADMSG || err == UBI_IO_FF_BITFLIPS)
			torture = 1;

		down_read(&ubi->fm_eba_sem);
		vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED;
		up_read(&ubi->fm_eba_sem);
		ubi_wl_put_peb(ubi, vol->vol_id, lnum, *pnum, torture);

		*pnum = UBI_LEB_UNMAPPED;
	} else if (err < 0) {
		ubi_err(ubi, "unable to read VID header back from PEB %i: %i",
			*pnum, err);

		goto out_free;
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
	} else {
		int found_vol_id, found_lnum;

		ubi_assert(err == 0 || err == UBI_IO_BITFLIPS);

		vid_hdr = ubi_get_vid_hdr(vidb);
		found_vol_id = be32_to_cpu(vid_hdr->vol_id);
		found_lnum = be32_to_cpu(vid_hdr->lnum);

		if (found_lnum != lnum || found_vol_id != vol->vol_id) {
			ubi_err(ubi, "EBA mismatch! PEB %i is LEB %i:%i instead of LEB %i:%i",
				*pnum, found_vol_id, found_lnum, vol->vol_id, lnum);
			ubi_ro_mode(ubi);
			err = -EINVAL;
			goto out_free;
		}
572 573
	}

574
	set_bit(lnum, vol->checkmap);
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
	err = 0;

out_free:
	ubi_free_vid_buf(vidb);

	return err;
}
#else
static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
		  int *pnum)
{
	return 0;
}
#endif

590 591 592
/**
 * ubi_eba_read_leb - read data.
 * @ubi: UBI device description object
593
 * @vol: volume description object
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
 * @lnum: logical eraseblock number
 * @buf: buffer to store the read data
 * @offset: offset from where to read
 * @len: how many bytes to read
 * @check: data CRC check flag
 *
 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
 * bytes. The @check flag only makes sense for static volumes and forces
 * eraseblock data CRC checking.
 *
 * In case of success this function returns zero. In case of a static volume,
 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
 * returned for any volume type if an ECC error was detected by the MTD device
 * driver. Other negative error cored may be returned in case of other errors.
 */
609 610
int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
		     void *buf, int offset, int len, int check)
611
{
612
	int err, pnum, scrub = 0, vol_id = vol->vol_id;
613
	struct ubi_vid_io_buf *vidb;
614
	struct ubi_vid_hdr *vid_hdr;
615
	uint32_t uninitialized_var(crc);
616 617 618 619 620

	err = leb_read_lock(ubi, vol_id, lnum);
	if (err)
		return err;

Boris Brezillon's avatar
Boris Brezillon committed
621
	pnum = vol->eba_tbl->entries[lnum].pnum;
622 623 624 625 626 627 628
	if (pnum >= 0) {
		err = check_mapping(ubi, vol, lnum, &pnum);
		if (err < 0)
			goto out_unlock;
	}

	if (pnum == UBI_LEB_UNMAPPED) {
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
		/*
		 * The logical eraseblock is not mapped, fill the whole buffer
		 * with 0xFF bytes. The exception is static volumes for which
		 * it is an error to read unmapped logical eraseblocks.
		 */
		dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
			len, offset, vol_id, lnum);
		leb_read_unlock(ubi, vol_id, lnum);
		ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
		memset(buf, 0xFF, len);
		return 0;
	}

	dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
		len, offset, vol_id, lnum, pnum);

	if (vol->vol_type == UBI_DYNAMIC_VOLUME)
		check = 0;

retry:
	if (check) {
650 651
		vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
		if (!vidb) {
652 653 654 655
			err = -ENOMEM;
			goto out_unlock;
		}

656 657 658
		vid_hdr = ubi_get_vid_hdr(vidb);

		err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
659 660 661 662 663 664 665 666 667 668
		if (err && err != UBI_IO_BITFLIPS) {
			if (err > 0) {
				/*
				 * The header is either absent or corrupted.
				 * The former case means there is a bug -
				 * switch to read-only mode just in case.
				 * The latter case means a real corruption - we
				 * may try to recover data. FIXME: but this is
				 * not implemented.
				 */
Artem Bityutskiy's avatar
Artem Bityutskiy committed
669
				if (err == UBI_IO_BAD_HDR_EBADMSG ||
670
				    err == UBI_IO_BAD_HDR) {
671
					ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d",
672
						 pnum, vol_id, lnum);
673
					err = -EBADMSG;
674
				} else {
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
					/*
					 * Ending up here in the non-Fastmap case
					 * is a clear bug as the VID header had to
					 * be present at scan time to have it referenced.
					 * With fastmap the story is more complicated.
					 * Fastmap has the mapping info without the need
					 * of a full scan. So the LEB could have been
					 * unmapped, Fastmap cannot know this and keeps
					 * the LEB referenced.
					 * This is valid and works as the layer above UBI
					 * has to do bookkeeping about used/referenced
					 * LEBs in any case.
					 */
					if (ubi->fast_attach) {
						err = -EBADMSG;
					} else {
						err = -EINVAL;
						ubi_ro_mode(ubi);
					}
694
				}
695 696 697 698 699
			}
			goto out_free;
		} else if (err == UBI_IO_BITFLIPS)
			scrub = 1;

700 701
		ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
		ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
702

703
		crc = be32_to_cpu(vid_hdr->data_crc);
704
		ubi_free_vid_buf(vidb);
705 706 707 708
	}

	err = ubi_io_read_data(ubi, buf, pnum, offset, len);
	if (err) {
709
		if (err == UBI_IO_BITFLIPS)
710
			scrub = 1;
711
		else if (mtd_is_eccerr(err)) {
712 713 714 715
			if (vol->vol_type == UBI_DYNAMIC_VOLUME)
				goto out_unlock;
			scrub = 1;
			if (!check) {
716
				ubi_msg(ubi, "force data checking");
717 718 719 720 721 722 723 724
				check = 1;
				goto retry;
			}
		} else
			goto out_unlock;
	}

	if (check) {
725
		uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
726
		if (crc1 != crc) {
727
			ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x",
728 729 730 731 732 733 734 735 736 737 738 739 740
				 crc1, crc);
			err = -EBADMSG;
			goto out_unlock;
		}
	}

	if (scrub)
		err = ubi_wl_scrub_peb(ubi, pnum);

	leb_read_unlock(ubi, vol_id, lnum);
	return err;

out_free:
741
	ubi_free_vid_buf(vidb);
742 743 744 745 746
out_unlock:
	leb_read_unlock(ubi, vol_id, lnum);
	return err;
}

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
/**
 * ubi_eba_read_leb_sg - read data into a scatter gather list.
 * @ubi: UBI device description object
 * @vol: volume description object
 * @lnum: logical eraseblock number
 * @sgl: UBI scatter gather list to store the read data
 * @offset: offset from where to read
 * @len: how many bytes to read
 * @check: data CRC check flag
 *
 * This function works exactly like ubi_eba_read_leb(). But instead of
 * storing the read data into a buffer it writes to an UBI scatter gather
 * list.
 */
int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
			struct ubi_sgl *sgl, int lnum, int offset, int len,
			int check)
{
	int to_read;
	int ret;
	struct scatterlist *sg;

	for (;;) {
		ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT);
		sg = &sgl->sg[sgl->list_pos];
		if (len < sg->length - sgl->page_pos)
			to_read = len;
		else
			to_read = sg->length - sgl->page_pos;

		ret = ubi_eba_read_leb(ubi, vol, lnum,
				       sg_virt(sg) + sgl->page_pos, offset,
				       to_read, check);
		if (ret < 0)
			return ret;

		offset += to_read;
		len -= to_read;
		if (!len) {
			sgl->page_pos += to_read;
			if (sgl->page_pos == sg->length) {
				sgl->list_pos++;
				sgl->page_pos = 0;
			}

			break;
		}

		sgl->list_pos++;
		sgl->page_pos = 0;
	}

	return ret;
}

802
/**
803 804
 * try_recover_peb - try to recover from write failure.
 * @vol: volume description object
805 806 807 808 809
 * @pnum: the physical eraseblock to recover
 * @lnum: logical eraseblock number
 * @buf: data which was not written because of the write failure
 * @offset: offset of the failed write
 * @len: how many bytes should have been written
810
 * @vidb: VID buffer
811
 * @retry: whether the caller should retry in case of failure
812 813 814 815
 *
 * This function is called in case of a write failure and moves all good data
 * from the potentially bad physical eraseblock to a good physical eraseblock.
 * This function also writes the data which was not written due to the failure.
816 817 818
 * Returns 0 in case of success, and a negative error code in case of failure.
 * In case of failure, the %retry parameter is set to false if this is a fatal
 * error (retrying won't help), and true otherwise.
819
 */
820 821
static int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum,
			   const void *buf, int offset, int len,
822
			   struct ubi_vid_io_buf *vidb, bool *retry)
823
{
824
	struct ubi_device *ubi = vol->ubi;
825
	struct ubi_vid_hdr *vid_hdr;
826
	int new_pnum, err, vol_id = vol->vol_id, data_size;
827
	uint32_t crc;
828

829
	*retry = false;
830

831
	new_pnum = ubi_wl_get_peb(ubi);
832
	if (new_pnum < 0) {
833 834
		err = new_pnum;
		goto out_put;
835 836
	}

837 838
	ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
		pnum, new_pnum);
839

840
	err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
841 842 843 844 845 846
	if (err && err != UBI_IO_BITFLIPS) {
		if (err > 0)
			err = -EIO;
		goto out_put;
	}

847
	vid_hdr = ubi_get_vid_hdr(vidb);
848
	ubi_assert(vid_hdr->vol_type == UBI_VID_DYNAMIC);
849

Artem Bityutskiy's avatar
Artem Bityutskiy committed
850
	mutex_lock(&ubi->buf_mutex);
851
	memset(ubi->peb_buf + offset, 0xFF, len);
852 853 854

	/* Read everything before the area where the write failure happened */
	if (offset > 0) {
855
		err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
856
		if (err && err != UBI_IO_BITFLIPS)
Artem Bityutskiy's avatar
Artem Bityutskiy committed
857
			goto out_unlock;
858 859
	}

860 861
	*retry = true;

862
	memcpy(ubi->peb_buf + offset, buf, len);
863

864 865 866 867 868 869
	data_size = offset + len;
	crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
	vid_hdr->copy_flag = 1;
	vid_hdr->data_size = cpu_to_be32(data_size);
	vid_hdr->data_crc = cpu_to_be32(crc);
870
	err = ubi_io_write_vid_hdr(ubi, new_pnum, vidb);
871 872
	if (err)
		goto out_unlock;
873

874
	err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
875

876
out_unlock:
877
	mutex_unlock(&ubi->buf_mutex);
878

879
	if (!err)
Boris Brezillon's avatar
Boris Brezillon committed
880
		vol->eba_tbl->entries[lnum].pnum = new_pnum;
881 882

out_put:
883
	up_read(&ubi->fm_eba_sem);
884

885 886 887 888 889 890 891 892 893 894 895
	if (!err) {
		ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
		ubi_msg(ubi, "data was successfully recovered");
	} else if (new_pnum >= 0) {
		/*
		 * Bad luck? This physical eraseblock is bad too? Crud. Let's
		 * try to get another one.
		 */
		ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
		ubi_warn(ubi, "failed to write to PEB %d", new_pnum);
	}
896 897

	return err;
898
}
899

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
/**
 * recover_peb - recover from write failure.
 * @ubi: UBI device description object
 * @pnum: the physical eraseblock to recover
 * @vol_id: volume ID
 * @lnum: logical eraseblock number
 * @buf: data which was not written because of the write failure
 * @offset: offset of the failed write
 * @len: how many bytes should have been written
 *
 * This function is called in case of a write failure and moves all good data
 * from the potentially bad physical eraseblock to a good physical eraseblock.
 * This function also writes the data which was not written due to the failure.
 * Returns 0 in case of success, and a negative error code in case of failure.
 * This function tries %UBI_IO_RETRIES before giving up.
 */
static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
		       const void *buf, int offset, int len)
{
	int err, idx = vol_id2idx(ubi, vol_id), tries;
	struct ubi_volume *vol = ubi->volumes[idx];
921
	struct ubi_vid_io_buf *vidb;
922

923 924
	vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
	if (!vidb)
925 926 927 928 929
		return -ENOMEM;

	for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
		bool retry;

930 931
		err = try_recover_peb(vol, pnum, lnum, buf, offset, len, vidb,
				      &retry);
932 933 934 935
		if (!err || !retry)
			break;

		ubi_msg(ubi, "try again");
936
	}
937

938
	ubi_free_vid_buf(vidb);
939 940

	return err;
941 942
}

943 944 945 946
/**
 * try_write_vid_and_data - try to write VID header and data to a new PEB.
 * @vol: volume description object
 * @lnum: logical eraseblock number
947
 * @vidb: the VID buffer to write
948 949 950 951 952 953 954 955 956 957 958
 * @buf: buffer containing the data
 * @offset: where to start writing data
 * @len: how many bytes should be written
 *
 * This function tries to write VID header and data belonging to logical
 * eraseblock @lnum of volume @vol to a new physical eraseblock. Returns zero
 * in case of success and a negative error code in case of failure.
 * In case of error, it is possible that something was still written to the
 * flash media, but may be some garbage.
 */
static int try_write_vid_and_data(struct ubi_volume *vol, int lnum,
959
				  struct ubi_vid_io_buf *vidb, const void *buf,
960 961 962 963 964 965 966 967 968 969 970
				  int offset, int len)
{
	struct ubi_device *ubi = vol->ubi;
	int pnum, opnum, err, vol_id = vol->vol_id;

	pnum = ubi_wl_get_peb(ubi);
	if (pnum < 0) {
		err = pnum;
		goto out_put;
	}

Boris Brezillon's avatar
Boris Brezillon committed
971
	opnum = vol->eba_tbl->entries[lnum].pnum;
972 973 974 975

	dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
		len, offset, vol_id, lnum, pnum);

976
	err = ubi_io_write_vid_hdr(ubi, pnum, vidb);
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
	if (err) {
		ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
			 vol_id, lnum, pnum);
		goto out_put;
	}

	if (len) {
		err = ubi_io_write_data(ubi, buf, pnum, offset, len);
		if (err) {
			ubi_warn(ubi,
				 "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
				 len, offset, vol_id, lnum, pnum);
			goto out_put;
		}
	}

Boris Brezillon's avatar
Boris Brezillon committed
993
	vol->eba_tbl->entries[lnum].pnum = pnum;
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005

out_put:
	up_read(&ubi->fm_eba_sem);

	if (err && pnum >= 0)
		err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
	else if (!err && opnum >= 0)
		err = ubi_wl_put_peb(ubi, vol_id, lnum, opnum, 0);

	return err;
}

1006 1007 1008
/**
 * ubi_eba_write_leb - write data to dynamic volume.
 * @ubi: UBI device description object
1009
 * @vol: volume description object
1010 1011 1012 1013 1014 1015
 * @lnum: logical eraseblock number
 * @buf: the data to write
 * @offset: offset within the logical eraseblock where to write
 * @len: how many bytes to write
 *
 * This function writes data to logical eraseblock @lnum of a dynamic volume
1016
 * @vol. Returns zero in case of success and a negative error code in case
1017 1018
 * of failure. In case of error, it is possible that something was still
 * written to the flash media, but may be some garbage.
1019
 * This function retries %UBI_IO_RETRIES times before giving up.
1020
 */
1021
int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
1022
		      const void *buf, int offset, int len)
1023
{
1024
	int err, pnum, tries, vol_id = vol->vol_id;
1025
	struct ubi_vid_io_buf *vidb;
1026 1027 1028 1029 1030 1031 1032 1033 1034
	struct ubi_vid_hdr *vid_hdr;

	if (ubi->ro_mode)
		return -EROFS;

	err = leb_write_lock(ubi, vol_id, lnum);
	if (err)
		return err;

Boris Brezillon's avatar
Boris Brezillon committed
1035
	pnum = vol->eba_tbl->entries[lnum].pnum;
1036 1037 1038 1039 1040 1041
	if (pnum >= 0) {
		err = check_mapping(ubi, vol, lnum, &pnum);
		if (err < 0)
			goto out;
	}

1042 1043 1044 1045 1046 1047
	if (pnum >= 0) {
		dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
			len, offset, vol_id, lnum, pnum);

		err = ubi_io_write_data(ubi, buf, pnum, offset, len);
		if (err) {
1048
			ubi_warn(ubi, "failed to write data to PEB %d", pnum);
1049
			if (err == -EIO && ubi->bad_allowed)
1050 1051
				err = recover_peb(ubi, pnum, vol_id, lnum, buf,
						  offset, len);
1052
		}
1053 1054

		goto out;
1055 1056 1057 1058 1059 1060
	}

	/*
	 * The logical eraseblock is not mapped. We have to get a free physical
	 * eraseblock and write the volume identifier header there first.
	 */
1061 1062
	vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
	if (!vidb) {
1063 1064 1065 1066
		leb_write_unlock(ubi, vol_id, lnum);
		return -ENOMEM;
	}

1067 1068
	vid_hdr = ubi_get_vid_hdr(vidb);

1069
	vid_hdr->vol_type = UBI_VID_DYNAMIC;
1070
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1071 1072
	vid_hdr->vol_id = cpu_to_be32(vol_id);
	vid_hdr->lnum = cpu_to_be32(lnum);
1073
	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
1074
	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
1075

1076
	for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
1077
		err = try_write_vid_and_data(vol, lnum, vidb, buf, offset, len);
1078 1079
		if (err != -EIO || !ubi->bad_allowed)
			break;
1080

1081 1082 1083 1084 1085 1086 1087 1088
		/*
		 * Fortunately, this is the first write operation to this
		 * physical eraseblock, so just put it and request a new one.
		 * We assume that if this physical eraseblock went bad, the
		 * erase code will handle that.
		 */
		vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
		ubi_msg(ubi, "try another PEB");
1089 1090
	}

1091
	ubi_free_vid_buf(vidb);
1092

1093 1094
out:
	if (err)
1095 1096
		ubi_ro_mode(ubi);

1097
	leb_write_unlock(ubi, vol_id, lnum);
1098

1099
	return err;
1100 1101 1102 1103 1104
}

/**
 * ubi_eba_write_leb_st - write data to static volume.
 * @ubi: UBI device description object
1105
 * @vol: volume description object
1106 1107 1108 1109 1110 1111
 * @lnum: logical eraseblock number
 * @buf: data to write
 * @len: how many bytes to write
 * @used_ebs: how many logical eraseblocks will this volume contain
 *
 * This function writes data to logical eraseblock @lnum of static volume
1112
 * @vol. The @used_ebs argument should contain total number of logical
1113 1114 1115 1116 1117 1118 1119
 * eraseblock in this static volume.
 *
 * When writing to the last logical eraseblock, the @len argument doesn't have
 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
 * to the real data size, although the @buf buffer has to contain the
 * alignment. In all other cases, @len has to be aligned.
 *
1120
 * It is prohibited to write more than once to logical eraseblocks of static
1121 1122 1123
 * volumes. This function returns zero in case of success and a negative error
 * code in case of failure.
 */
1124
int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
1125
			 int lnum, const void *buf, int len, int used_ebs)
1126
{
1127
	int err, tries, data_size = len, vol_id = vol->vol_id;
1128
	struct ubi_vid_io_buf *vidb;
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	struct ubi_vid_hdr *vid_hdr;
	uint32_t crc;

	if (ubi->ro_mode)
		return -EROFS;

	if (lnum == used_ebs - 1)
		/* If this is the last LEB @len may be unaligned */
		len = ALIGN(data_size, ubi->min_io_size);
	else
1139
		ubi_assert(!(len & (ubi->min_io_size - 1)));
1140

1141 1142
	vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
	if (!vidb)
1143 1144
		return -ENOMEM;

1145 1146
	vid_hdr = ubi_get_vid_hdr(vidb);

1147
	err = leb_write_lock(ubi, vol_id, lnum);
1148 1149
	if (err)
		goto out;
1150

1151
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1152 1153
	vid_hdr->vol_id = cpu_to_be32(vol_id);
	vid_hdr->lnum = cpu_to_be32(lnum);
1154
	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
1155
	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
1156 1157 1158

	crc = crc32(UBI_CRC32_INIT, buf, data_size);
	vid_hdr->vol_type = UBI_VID_STATIC;
1159 1160 1161
	vid_hdr->data_size = cpu_to_be32(data_size);
	vid_hdr->used_ebs = cpu_to_be32(used_ebs);
	vid_hdr->data_crc = cpu_to_be32(crc);
1162

Boris Brezillon's avatar
Boris Brezillon committed
1163
	ubi_assert(vol->eba_tbl->entries[lnum].pnum < 0);
1164

1165
	for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
1166
		err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
1167 1168
		if (err != -EIO || !ubi->bad_allowed)
			break;
1169

1170 1171
		vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
		ubi_msg(ubi, "try another PEB");
1172 1173
	}

1174 1175
	if (err)
		ubi_ro_mode(ubi);
1176 1177 1178

	leb_write_unlock(ubi, vol_id, lnum);

1179
out:
1180
	ubi_free_vid_buf(vidb);
1181

1182
	return err;
1183 1184 1185 1186 1187
}

/*
 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
 * @ubi: UBI device description object
1188
 * @vol: volume description object
1189 1190 1191 1192 1193 1194 1195 1196 1197
 * @lnum: logical eraseblock number
 * @buf: data to write
 * @len: how many bytes to write
 *
 * This function changes the contents of a logical eraseblock atomically. @buf
 * has to contain new logical eraseblock data, and @len - the length of the
 * data, which has to be aligned. This function guarantees that in case of an
 * unclean reboot the old contents is preserved. Returns zero in case of
 * success and a negative error code in case of failure.
1198 1199 1200
 *
 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
1201
 */
1202
int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
1203
			      int lnum, const void *buf, int len)
1204
{
1205
	int err, tries, vol_id = vol->vol_id;
1206
	struct ubi_vid_io_buf *vidb;
1207 1208 1209 1210 1211 1212
	struct ubi_vid_hdr *vid_hdr;
	uint32_t crc;

	if (ubi->ro_mode)
		return -EROFS;

1213 1214 1215 1216 1217 1218 1219 1220
	if (len == 0) {
		/*
		 * Special case when data length is zero. In this case the LEB
		 * has to be unmapped and mapped somewhere else.
		 */
		err = ubi_eba_unmap_leb(ubi, vol, lnum);
		if (err)
			return err;
1221
		return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
1222 1223
	}

1224 1225
	vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
	if (!vidb)
1226 1227
		return -ENOMEM;

1228 1229
	vid_hdr = ubi_get_vid_hdr(vidb);

1230
	mutex_lock(&ubi->alc_mutex);
1231
	err = leb_write_lock(ubi, vol_id, lnum);
1232 1233
	if (err)
		goto out_mutex;
1234

1235
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1236 1237
	vid_hdr->vol_id = cpu_to_be32(vol_id);
	vid_hdr->lnum = cpu_to_be32(lnum);
1238
	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
1239
	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
1240 1241

	crc = crc32(UBI_CRC32_INIT, buf, len);
1242
	vid_hdr->vol_type = UBI_VID_DYNAMIC;
1243
	vid_hdr->data_size = cpu_to_be32(len);
1244
	vid_hdr->copy_flag = 1;
1245
	vid_hdr->data_crc = cpu_to_be32(crc);
1246

1247
	dbg_eba("change LEB %d:%d", vol_id, lnum);
1248

1249
	for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
1250
		err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
1251 1252
		if (err != -EIO || !ubi->bad_allowed)
			break;
1253

1254 1255
		vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
		ubi_msg(ubi, "try another PEB");
1256 1257
	}

1258 1259 1260 1261 1262 1263 1264
	/*
	 * This flash device does not admit of bad eraseblocks or
	 * something nasty and unexpected happened. Switch to read-only
	 * mode just in case.
	 */
	if (err)
		ubi_ro_mode(ubi);
1265

1266
	leb_write_unlock(ubi, vol_id, lnum);
1267

1268 1269
out_mutex:
	mutex_unlock(&ubi->alc_mutex);
1270
	ubi_free_vid_buf(vidb);
1271
	return err;
1272 1273
}

1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
/**
 * is_error_sane - check whether a read error is sane.
 * @err: code of the error happened during reading
 *
 * This is a helper function for 'ubi_eba_copy_leb()' which is called when we
 * cannot read data from the target PEB (an error @err happened). If the error
 * code is sane, then we treat this error as non-fatal. Otherwise the error is
 * fatal and UBI will be switched to R/O mode later.
 *
 * The idea is that we try not to switch to R/O mode if the read error is
 * something which suggests there was a real read problem. E.g., %-EIO. Or a
 * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O
 * mode, simply because we do not know what happened at the MTD level, and we
 * cannot handle this. E.g., the underlying driver may have become crazy, and
 * it is safer to switch to R/O mode to preserve the data.
 *
 * And bear in mind, this is about reading from the target PEB, i.e. the PEB
 * which we have just written.
 */
static int is_error_sane(int err)
{
1295
	if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
Artem Bityutskiy's avatar
Artem Bityutskiy committed
1296
	    err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
1297 1298 1299 1300
		return 0;
	return 1;
}

1301 1302 1303 1304 1305 1306 1307 1308 1309
/**
 * ubi_eba_copy_leb - copy logical eraseblock.
 * @ubi: UBI device description object
 * @from: physical eraseblock number from where to copy
 * @to: physical eraseblock number where to copy
 * @vid_hdr: VID header of the @from physical eraseblock
 *
 * This function copies logical eraseblock from physical eraseblock @from to
 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
1310
 * function. Returns:
1311
 *   o %0 in case of success;
1312
 *   o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
1313
 *   o a negative error code in case of failure.
1314 1315
 */
int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1316
		     struct ubi_vid_io_buf *vidb)
1317
{
1318
	int err, vol_id, lnum, data_size, aldata_size, idx;
1319
	struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb);
1320 1321 1322
	struct ubi_volume *vol;
	uint32_t crc;

1323 1324
	ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem));

1325 1326
	vol_id = be32_to_cpu(vid_hdr->vol_id);
	lnum = be32_to_cpu(vid_hdr->lnum);
1327

1328
	dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
1329 1330

	if (vid_hdr->vol_type == UBI_VID_STATIC) {
1331
		data_size = be32_to_cpu(vid_hdr->data_size);
1332 1333 1334
		aldata_size = ALIGN(data_size, ubi->min_io_size);
	} else
		data_size = aldata_size =
1335
			    ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
1336 1337

	idx = vol_id2idx(ubi, vol_id);
1338
	spin_lock(&ubi->volumes_lock);
1339
	/*
1340 1341
	 * Note, we may race with volume deletion, which means that the volume
	 * this logical eraseblock belongs to might be being deleted. Since the
1342
	 * volume deletion un-maps all the volume's logical eraseblocks, it will
1343
	 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
1344 1345
	 */
	vol = ubi->volumes[idx];
1346
	spin_unlock(&ubi->volumes_lock);
1347
	if (!vol) {
1348
		/* No need to do further work, cancel */