eba.c 39.9 KB
Newer Older
A
Artem B. Bityutskiy 已提交
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 (Битюцкий Артём)
 */

/*
A
Artem Bityutskiy 已提交
22
 * The UBI Eraseblock Association (EBA) sub-system.
A
Artem B. Bityutskiy 已提交
23
 *
A
Artem Bityutskiy 已提交
24
 * This sub-system is responsible for I/O to/from logical eraseblock.
A
Artem B. Bityutskiy 已提交
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.
 *
A
Artem Bityutskiy 已提交
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.
A
Artem B. Bityutskiy 已提交
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

A
Artem B. Bityutskiy 已提交
52 53 54 55 56 57 58 59
/**
 * 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.
 */
R
Richard Weinberger 已提交
60
unsigned long long ubi_next_sqnum(struct ubi_device *ubi)
A
Artem B. Bityutskiy 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
{
	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)
{
81
	if (vol_id == UBI_LAYOUT_VOLUME_ID)
A
Artem B. Bityutskiy 已提交
82 83 84 85 86 87 88 89 90 91
		return UBI_LAYOUT_VOLUME_COMPAT;
	return 0;
}

/**
 * ltree_lookup - look up the lock tree.
 * @ubi: UBI device description object
 * @vol_id: volume ID
 * @lnum: logical eraseblock number
 *
92
 * This function returns a pointer to the corresponding &struct ubi_ltree_entry
A
Artem B. Bityutskiy 已提交
93 94 95
 * object if the logical eraseblock is locked and %NULL if it is not.
 * @ubi->ltree_lock has to be locked.
 */
96 97
static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
					    int lnum)
A
Artem B. Bityutskiy 已提交
98 99 100 101 102
{
	struct rb_node *p;

	p = ubi->ltree.rb_node;
	while (p) {
103
		struct ubi_ltree_entry *le;
A
Artem B. Bityutskiy 已提交
104

105
		le = rb_entry(p, struct ubi_ltree_entry, rb);
A
Artem B. Bityutskiy 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

		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.
 */
135 136
static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
					       int vol_id, int lnum)
A
Artem B. Bityutskiy 已提交
137
{
138
	struct ubi_ltree_entry *le, *le1, *le_free;
A
Artem B. Bityutskiy 已提交
139

140
	le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
A
Artem B. Bityutskiy 已提交
141 142 143
	if (!le)
		return ERR_PTR(-ENOMEM);

144 145
	le->users = 0;
	init_rwsem(&le->mutex);
A
Artem B. Bityutskiy 已提交
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
	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;
171
			le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
A
Artem B. Bityutskiy 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

			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);

192
	kfree(le_free);
A
Artem B. Bityutskiy 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206
	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)
{
207
	struct ubi_ltree_entry *le;
A
Artem B. Bityutskiy 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

	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)
{
224
	struct ubi_ltree_entry *le;
A
Artem B. Bityutskiy 已提交
225 226 227 228 229

	spin_lock(&ubi->ltree_lock);
	le = ltree_lookup(ubi, vol_id, lnum);
	le->users -= 1;
	ubi_assert(le->users >= 0);
A
Artem Bityutskiy 已提交
230
	up_read(&le->mutex);
A
Artem B. Bityutskiy 已提交
231 232
	if (le->users == 0) {
		rb_erase(&le->rb, &ubi->ltree);
A
Artem Bityutskiy 已提交
233
		kfree(le);
A
Artem B. Bityutskiy 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	}
	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)
{
249
	struct ubi_ltree_entry *le;
A
Artem B. Bityutskiy 已提交
250 251 252 253 254 255 256 257

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

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
/**
 * 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 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);
285
		kfree(le);
A
Artem Bityutskiy 已提交
286 287
	}
	spin_unlock(&ubi->ltree_lock);
288 289 290 291

	return 1;
}

A
Artem B. Bityutskiy 已提交
292 293 294 295 296 297 298 299
/**
 * 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)
{
300
	struct ubi_ltree_entry *le;
A
Artem B. Bityutskiy 已提交
301 302 303 304 305

	spin_lock(&ubi->ltree_lock);
	le = ltree_lookup(ubi, vol_id, lnum);
	le->users -= 1;
	ubi_assert(le->users >= 0);
A
Artem Bityutskiy 已提交
306
	up_write(&le->mutex);
A
Artem B. Bityutskiy 已提交
307 308
	if (le->users == 0) {
		rb_erase(&le->rb, &ubi->ltree);
309
		kfree(le);
A
Artem Bityutskiy 已提交
310 311
	}
	spin_unlock(&ubi->ltree_lock);
A
Artem B. Bityutskiy 已提交
312 313 314 315 316
}

/**
 * ubi_eba_unmap_leb - un-map logical eraseblock.
 * @ubi: UBI device description object
317
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
318 319 320 321 322 323
 * @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.
 */
324 325
int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
		      int lnum)
A
Artem B. Bityutskiy 已提交
326
{
327
	int err, pnum, vol_id = vol->vol_id;
A
Artem B. Bityutskiy 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

	if (ubi->ro_mode)
		return -EROFS;

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

	pnum = vol->eba_tbl[lnum];
	if (pnum < 0)
		/* This logical eraseblock is already unmapped */
		goto out_unlock;

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

R
Richard Weinberger 已提交
343
	down_read(&ubi->fm_sem);
A
Artem B. Bityutskiy 已提交
344
	vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
R
Richard Weinberger 已提交
345
	up_read(&ubi->fm_sem);
346
	err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
A
Artem B. Bityutskiy 已提交
347 348 349 350 351 352 353 354 355

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

/**
 * ubi_eba_read_leb - read data.
 * @ubi: UBI device description object
356
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
 * @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.
 */
372 373
int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
		     void *buf, int offset, int len, int check)
A
Artem B. Bityutskiy 已提交
374
{
375
	int err, pnum, scrub = 0, vol_id = vol->vol_id;
A
Artem B. Bityutskiy 已提交
376
	struct ubi_vid_hdr *vid_hdr;
377
	uint32_t uninitialized_var(crc);
A
Artem B. Bityutskiy 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

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

	pnum = vol->eba_tbl[lnum];
	if (pnum < 0) {
		/*
		 * 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) {
406
		vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
A
Artem B. Bityutskiy 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
		if (!vid_hdr) {
			err = -ENOMEM;
			goto out_unlock;
		}

		err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
		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.
				 */
A
Artem Bityutskiy 已提交
423
				if (err == UBI_IO_BAD_HDR_EBADMSG ||
424
				    err == UBI_IO_BAD_HDR) {
425
					ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d",
A
Artem Bityutskiy 已提交
426
						 pnum, vol_id, lnum);
A
Artem B. Bityutskiy 已提交
427
					err = -EBADMSG;
428
				} else {
429
					err = -EINVAL;
A
Artem B. Bityutskiy 已提交
430
					ubi_ro_mode(ubi);
431
				}
A
Artem B. Bityutskiy 已提交
432 433 434 435 436
			}
			goto out_free;
		} else if (err == UBI_IO_BITFLIPS)
			scrub = 1;

437 438
		ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
		ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
A
Artem B. Bityutskiy 已提交
439

440
		crc = be32_to_cpu(vid_hdr->data_crc);
A
Artem B. Bityutskiy 已提交
441 442 443 444 445
		ubi_free_vid_hdr(ubi, vid_hdr);
	}

	err = ubi_io_read_data(ubi, buf, pnum, offset, len);
	if (err) {
446
		if (err == UBI_IO_BITFLIPS)
A
Artem B. Bityutskiy 已提交
447
			scrub = 1;
448
		else if (mtd_is_eccerr(err)) {
A
Artem B. Bityutskiy 已提交
449 450 451 452
			if (vol->vol_type == UBI_DYNAMIC_VOLUME)
				goto out_unlock;
			scrub = 1;
			if (!check) {
453
				ubi_msg(ubi, "force data checking");
A
Artem B. Bityutskiy 已提交
454 455 456 457 458 459 460 461
				check = 1;
				goto retry;
			}
		} else
			goto out_unlock;
	}

	if (check) {
462
		uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
A
Artem B. Bityutskiy 已提交
463
		if (crc1 != crc) {
464
			ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x",
A
Artem B. Bityutskiy 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
				 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:
	ubi_free_vid_hdr(ubi, vid_hdr);
out_unlock:
	leb_read_unlock(ubi, vol_id, lnum);
	return err;
}

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
/**
 * 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;
}

A
Artem B. Bityutskiy 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
/**
 * 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 new physical eraseblock number in case of success, and a negative
 * error code in case of failure.
 */
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), new_pnum, data_size, tries = 0;
	struct ubi_volume *vol = ubi->volumes[idx];
	struct ubi_vid_hdr *vid_hdr;

562
	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
563
	if (!vid_hdr)
A
Artem B. Bityutskiy 已提交
564 565 566
		return -ENOMEM;

retry:
R
Richard Weinberger 已提交
567
	new_pnum = ubi_wl_get_peb(ubi);
A
Artem B. Bityutskiy 已提交
568 569 570 571 572
	if (new_pnum < 0) {
		ubi_free_vid_hdr(ubi, vid_hdr);
		return new_pnum;
	}

573 574
	ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
		pnum, new_pnum);
A
Artem B. Bityutskiy 已提交
575 576 577 578 579 580 581 582

	err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
	if (err && err != UBI_IO_BITFLIPS) {
		if (err > 0)
			err = -EIO;
		goto out_put;
	}

R
Richard Weinberger 已提交
583
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
A
Artem B. Bityutskiy 已提交
584 585 586 587 588
	err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
	if (err)
		goto write_error;

	data_size = offset + len;
A
Artem Bityutskiy 已提交
589
	mutex_lock(&ubi->buf_mutex);
590
	memset(ubi->peb_buf + offset, 0xFF, len);
A
Artem B. Bityutskiy 已提交
591 592 593

	/* Read everything before the area where the write failure happened */
	if (offset > 0) {
594
		err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
595
		if (err && err != UBI_IO_BITFLIPS)
A
Artem Bityutskiy 已提交
596
			goto out_unlock;
A
Artem B. Bityutskiy 已提交
597 598
	}

599
	memcpy(ubi->peb_buf + offset, buf, len);
A
Artem B. Bityutskiy 已提交
600

601
	err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
A
Artem Bityutskiy 已提交
602 603
	if (err) {
		mutex_unlock(&ubi->buf_mutex);
A
Artem B. Bityutskiy 已提交
604
		goto write_error;
A
Artem Bityutskiy 已提交
605
	}
A
Artem B. Bityutskiy 已提交
606

607
	mutex_unlock(&ubi->buf_mutex);
A
Artem B. Bityutskiy 已提交
608 609
	ubi_free_vid_hdr(ubi, vid_hdr);

R
Richard Weinberger 已提交
610
	down_read(&ubi->fm_sem);
A
Artem B. Bityutskiy 已提交
611
	vol->eba_tbl[lnum] = new_pnum;
R
Richard Weinberger 已提交
612
	up_read(&ubi->fm_sem);
613
	ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
A
Artem B. Bityutskiy 已提交
614

615
	ubi_msg(ubi, "data was successfully recovered");
A
Artem B. Bityutskiy 已提交
616 617
	return 0;

A
Artem Bityutskiy 已提交
618
out_unlock:
619
	mutex_unlock(&ubi->buf_mutex);
A
Artem Bityutskiy 已提交
620
out_put:
621
	ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
A
Artem B. Bityutskiy 已提交
622 623 624 625 626 627 628 629
	ubi_free_vid_hdr(ubi, vid_hdr);
	return err;

write_error:
	/*
	 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
	 * get another one.
	 */
630
	ubi_warn(ubi, "failed to write to PEB %d", new_pnum);
631
	ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
A
Artem B. Bityutskiy 已提交
632 633 634 635
	if (++tries > UBI_IO_RETRIES) {
		ubi_free_vid_hdr(ubi, vid_hdr);
		return err;
	}
636
	ubi_msg(ubi, "try again");
A
Artem B. Bityutskiy 已提交
637 638 639 640 641 642
	goto retry;
}

/**
 * ubi_eba_write_leb - write data to dynamic volume.
 * @ubi: UBI device description object
643
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
644 645 646 647 648 649
 * @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
650
 * @vol. Returns zero in case of success and a negative error code in case
A
Artem B. Bityutskiy 已提交
651 652 653
 * of failure. In case of error, it is possible that something was still
 * written to the flash media, but may be some garbage.
 */
654
int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
R
Richard Weinberger 已提交
655
		      const void *buf, int offset, int len)
A
Artem B. Bityutskiy 已提交
656
{
657
	int err, pnum, tries = 0, vol_id = vol->vol_id;
A
Artem B. Bityutskiy 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	struct ubi_vid_hdr *vid_hdr;

	if (ubi->ro_mode)
		return -EROFS;

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

	pnum = vol->eba_tbl[lnum];
	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) {
674
			ubi_warn(ubi, "failed to write data to PEB %d", pnum);
A
Artem B. Bityutskiy 已提交
675
			if (err == -EIO && ubi->bad_allowed)
676 677
				err = recover_peb(ubi, pnum, vol_id, lnum, buf,
						  offset, len);
A
Artem B. Bityutskiy 已提交
678 679 680 681 682 683 684 685 686 687 688
			if (err)
				ubi_ro_mode(ubi);
		}
		leb_write_unlock(ubi, vol_id, lnum);
		return err;
	}

	/*
	 * The logical eraseblock is not mapped. We have to get a free physical
	 * eraseblock and write the volume identifier header there first.
	 */
689
	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
A
Artem B. Bityutskiy 已提交
690 691 692 693 694 695
	if (!vid_hdr) {
		leb_write_unlock(ubi, vol_id, lnum);
		return -ENOMEM;
	}

	vid_hdr->vol_type = UBI_VID_DYNAMIC;
R
Richard Weinberger 已提交
696
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
697 698
	vid_hdr->vol_id = cpu_to_be32(vol_id);
	vid_hdr->lnum = cpu_to_be32(lnum);
A
Artem B. Bityutskiy 已提交
699
	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
700
	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
A
Artem B. Bityutskiy 已提交
701 702

retry:
R
Richard Weinberger 已提交
703
	pnum = ubi_wl_get_peb(ubi);
A
Artem B. Bityutskiy 已提交
704 705 706 707 708 709 710 711 712 713 714
	if (pnum < 0) {
		ubi_free_vid_hdr(ubi, vid_hdr);
		leb_write_unlock(ubi, vol_id, lnum);
		return pnum;
	}

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

	err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
	if (err) {
715
		ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
A
Artem B. Bityutskiy 已提交
716 717 718 719
			 vol_id, lnum, pnum);
		goto write_error;
	}

A
Artem Bityutskiy 已提交
720 721 722
	if (len) {
		err = ubi_io_write_data(ubi, buf, pnum, offset, len);
		if (err) {
723
			ubi_warn(ubi, "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
A
Artem Bityutskiy 已提交
724
				 len, offset, vol_id, lnum, pnum);
A
Artem Bityutskiy 已提交
725 726
			goto write_error;
		}
A
Artem B. Bityutskiy 已提交
727 728
	}

R
Richard Weinberger 已提交
729
	down_read(&ubi->fm_sem);
A
Artem B. Bityutskiy 已提交
730
	vol->eba_tbl[lnum] = pnum;
R
Richard Weinberger 已提交
731
	up_read(&ubi->fm_sem);
A
Artem B. Bityutskiy 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

	leb_write_unlock(ubi, vol_id, lnum);
	ubi_free_vid_hdr(ubi, vid_hdr);
	return 0;

write_error:
	if (err != -EIO || !ubi->bad_allowed) {
		ubi_ro_mode(ubi);
		leb_write_unlock(ubi, vol_id, lnum);
		ubi_free_vid_hdr(ubi, vid_hdr);
		return err;
	}

	/*
	 * 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.
	 */
750
	err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
A
Artem B. Bityutskiy 已提交
751 752 753 754 755 756 757
	if (err || ++tries > UBI_IO_RETRIES) {
		ubi_ro_mode(ubi);
		leb_write_unlock(ubi, vol_id, lnum);
		ubi_free_vid_hdr(ubi, vid_hdr);
		return err;
	}

R
Richard Weinberger 已提交
758
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
759
	ubi_msg(ubi, "try another PEB");
A
Artem B. Bityutskiy 已提交
760 761 762 763 764 765
	goto retry;
}

/**
 * ubi_eba_write_leb_st - write data to static volume.
 * @ubi: UBI device description object
766
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
767 768 769 770 771 772
 * @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
773
 * @vol. The @used_ebs argument should contain total number of logical
A
Artem B. Bityutskiy 已提交
774 775 776 777 778 779 780
 * 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.
 *
781
 * It is prohibited to write more than once to logical eraseblocks of static
A
Artem B. Bityutskiy 已提交
782 783 784
 * volumes. This function returns zero in case of success and a negative error
 * code in case of failure.
 */
785
int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
R
Richard Weinberger 已提交
786
			 int lnum, const void *buf, int len, int used_ebs)
A
Artem B. Bityutskiy 已提交
787
{
788
	int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
A
Artem B. Bityutskiy 已提交
789 790 791 792 793 794 795 796 797 798
	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
799
		ubi_assert(!(len & (ubi->min_io_size - 1)));
A
Artem B. Bityutskiy 已提交
800

801
	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
A
Artem B. Bityutskiy 已提交
802 803 804 805 806 807 808 809 810
	if (!vid_hdr)
		return -ENOMEM;

	err = leb_write_lock(ubi, vol_id, lnum);
	if (err) {
		ubi_free_vid_hdr(ubi, vid_hdr);
		return err;
	}

R
Richard Weinberger 已提交
811
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
812 813
	vid_hdr->vol_id = cpu_to_be32(vol_id);
	vid_hdr->lnum = cpu_to_be32(lnum);
A
Artem B. Bityutskiy 已提交
814
	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
815
	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
A
Artem B. Bityutskiy 已提交
816 817 818

	crc = crc32(UBI_CRC32_INIT, buf, data_size);
	vid_hdr->vol_type = UBI_VID_STATIC;
819 820 821
	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);
A
Artem B. Bityutskiy 已提交
822 823

retry:
R
Richard Weinberger 已提交
824
	pnum = ubi_wl_get_peb(ubi);
A
Artem B. Bityutskiy 已提交
825 826 827 828 829 830 831 832 833 834 835
	if (pnum < 0) {
		ubi_free_vid_hdr(ubi, vid_hdr);
		leb_write_unlock(ubi, vol_id, lnum);
		return pnum;
	}

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

	err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
	if (err) {
836
		ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
A
Artem B. Bityutskiy 已提交
837 838 839 840 841 842
			 vol_id, lnum, pnum);
		goto write_error;
	}

	err = ubi_io_write_data(ubi, buf, pnum, 0, len);
	if (err) {
843
		ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
A
Artem B. Bityutskiy 已提交
844 845 846 847 848
			 len, pnum);
		goto write_error;
	}

	ubi_assert(vol->eba_tbl[lnum] < 0);
R
Richard Weinberger 已提交
849
	down_read(&ubi->fm_sem);
A
Artem B. Bityutskiy 已提交
850
	vol->eba_tbl[lnum] = pnum;
R
Richard Weinberger 已提交
851
	up_read(&ubi->fm_sem);
A
Artem B. Bityutskiy 已提交
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869

	leb_write_unlock(ubi, vol_id, lnum);
	ubi_free_vid_hdr(ubi, vid_hdr);
	return 0;

write_error:
	if (err != -EIO || !ubi->bad_allowed) {
		/*
		 * This flash device does not admit of bad eraseblocks or
		 * something nasty and unexpected happened. Switch to read-only
		 * mode just in case.
		 */
		ubi_ro_mode(ubi);
		leb_write_unlock(ubi, vol_id, lnum);
		ubi_free_vid_hdr(ubi, vid_hdr);
		return err;
	}

870
	err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
A
Artem B. Bityutskiy 已提交
871 872 873 874 875 876 877
	if (err || ++tries > UBI_IO_RETRIES) {
		ubi_ro_mode(ubi);
		leb_write_unlock(ubi, vol_id, lnum);
		ubi_free_vid_hdr(ubi, vid_hdr);
		return err;
	}

R
Richard Weinberger 已提交
878
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
879
	ubi_msg(ubi, "try another PEB");
A
Artem B. Bityutskiy 已提交
880 881 882 883 884 885
	goto retry;
}

/*
 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
 * @ubi: UBI device description object
A
Artem Bityutskiy 已提交
886
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
887 888 889 890 891 892 893 894 895
 * @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.
896 897 898
 *
 * 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.
A
Artem B. Bityutskiy 已提交
899
 */
900
int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
R
Richard Weinberger 已提交
901
			      int lnum, const void *buf, int len)
A
Artem B. Bityutskiy 已提交
902
{
903
	int err, pnum, tries = 0, vol_id = vol->vol_id;
A
Artem B. Bityutskiy 已提交
904 905 906 907 908 909
	struct ubi_vid_hdr *vid_hdr;
	uint32_t crc;

	if (ubi->ro_mode)
		return -EROFS;

A
Artem Bityutskiy 已提交
910 911 912 913 914 915 916 917
	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;
R
Richard Weinberger 已提交
918
		return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
A
Artem Bityutskiy 已提交
919 920
	}

921
	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
A
Artem B. Bityutskiy 已提交
922 923 924
	if (!vid_hdr)
		return -ENOMEM;

925
	mutex_lock(&ubi->alc_mutex);
A
Artem B. Bityutskiy 已提交
926
	err = leb_write_lock(ubi, vol_id, lnum);
927 928
	if (err)
		goto out_mutex;
A
Artem B. Bityutskiy 已提交
929

R
Richard Weinberger 已提交
930
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
931 932
	vid_hdr->vol_id = cpu_to_be32(vol_id);
	vid_hdr->lnum = cpu_to_be32(lnum);
A
Artem B. Bityutskiy 已提交
933
	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
934
	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
A
Artem B. Bityutskiy 已提交
935 936

	crc = crc32(UBI_CRC32_INIT, buf, len);
937
	vid_hdr->vol_type = UBI_VID_DYNAMIC;
938
	vid_hdr->data_size = cpu_to_be32(len);
A
Artem B. Bityutskiy 已提交
939
	vid_hdr->copy_flag = 1;
940
	vid_hdr->data_crc = cpu_to_be32(crc);
A
Artem B. Bityutskiy 已提交
941 942

retry:
R
Richard Weinberger 已提交
943
	pnum = ubi_wl_get_peb(ubi);
A
Artem B. Bityutskiy 已提交
944
	if (pnum < 0) {
945 946
		err = pnum;
		goto out_leb_unlock;
A
Artem B. Bityutskiy 已提交
947 948 949 950 951 952 953
	}

	dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
		vol_id, lnum, vol->eba_tbl[lnum], pnum);

	err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
	if (err) {
954
		ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
A
Artem B. Bityutskiy 已提交
955 956 957 958 959 960
			 vol_id, lnum, pnum);
		goto write_error;
	}

	err = ubi_io_write_data(ubi, buf, pnum, 0, len);
	if (err) {
961
		ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
A
Artem B. Bityutskiy 已提交
962 963 964 965
			 len, pnum);
		goto write_error;
	}

966
	if (vol->eba_tbl[lnum] >= 0) {
967
		err = ubi_wl_put_peb(ubi, vol_id, lnum, vol->eba_tbl[lnum], 0);
968 969
		if (err)
			goto out_leb_unlock;
A
Artem B. Bityutskiy 已提交
970 971
	}

R
Richard Weinberger 已提交
972
	down_read(&ubi->fm_sem);
A
Artem B. Bityutskiy 已提交
973
	vol->eba_tbl[lnum] = pnum;
R
Richard Weinberger 已提交
974
	up_read(&ubi->fm_sem);
975 976

out_leb_unlock:
A
Artem B. Bityutskiy 已提交
977
	leb_write_unlock(ubi, vol_id, lnum);
978 979
out_mutex:
	mutex_unlock(&ubi->alc_mutex);
A
Artem B. Bityutskiy 已提交
980
	ubi_free_vid_hdr(ubi, vid_hdr);
981
	return err;
A
Artem B. Bityutskiy 已提交
982 983 984 985 986 987 988 989 990

write_error:
	if (err != -EIO || !ubi->bad_allowed) {
		/*
		 * This flash device does not admit of bad eraseblocks or
		 * something nasty and unexpected happened. Switch to read-only
		 * mode just in case.
		 */
		ubi_ro_mode(ubi);
991
		goto out_leb_unlock;
A
Artem B. Bityutskiy 已提交
992 993
	}

994
	err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
A
Artem B. Bityutskiy 已提交
995 996
	if (err || ++tries > UBI_IO_RETRIES) {
		ubi_ro_mode(ubi);
997
		goto out_leb_unlock;
A
Artem B. Bityutskiy 已提交
998 999
	}

R
Richard Weinberger 已提交
1000
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1001
	ubi_msg(ubi, "try another PEB");
A
Artem B. Bityutskiy 已提交
1002 1003 1004
	goto retry;
}

A
Artem Bityutskiy 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
/**
 * 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)
{
A
Artem Bityutskiy 已提交
1026
	if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
A
Artem Bityutskiy 已提交
1027
	    err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
A
Artem Bityutskiy 已提交
1028 1029 1030 1031
		return 0;
	return 1;
}

A
Artem B. Bityutskiy 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040
/**
 * 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
1041
 * function. Returns:
1042
 *   o %0 in case of success;
1043
 *   o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
1044
 *   o a negative error code in case of failure.
A
Artem B. Bityutskiy 已提交
1045 1046 1047 1048
 */
int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
		     struct ubi_vid_hdr *vid_hdr)
{
1049
	int err, vol_id, lnum, data_size, aldata_size, idx;
A
Artem B. Bityutskiy 已提交
1050 1051 1052
	struct ubi_volume *vol;
	uint32_t crc;

1053 1054
	vol_id = be32_to_cpu(vid_hdr->vol_id);
	lnum = be32_to_cpu(vid_hdr->lnum);
A
Artem B. Bityutskiy 已提交
1055

1056
	dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
A
Artem B. Bityutskiy 已提交
1057 1058

	if (vid_hdr->vol_type == UBI_VID_STATIC) {
1059
		data_size = be32_to_cpu(vid_hdr->data_size);
A
Artem B. Bityutskiy 已提交
1060 1061 1062
		aldata_size = ALIGN(data_size, ubi->min_io_size);
	} else
		data_size = aldata_size =
1063
			    ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
A
Artem B. Bityutskiy 已提交
1064 1065

	idx = vol_id2idx(ubi, vol_id);
1066
	spin_lock(&ubi->volumes_lock);
A
Artem B. Bityutskiy 已提交
1067
	/*
1068 1069
	 * Note, we may race with volume deletion, which means that the volume
	 * this logical eraseblock belongs to might be being deleted. Since the
1070
	 * volume deletion un-maps all the volume's logical eraseblocks, it will
1071
	 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
A
Artem B. Bityutskiy 已提交
1072 1073
	 */
	vol = ubi->volumes[idx];
A
Artem Bityutskiy 已提交
1074
	spin_unlock(&ubi->volumes_lock);
A
Artem B. Bityutskiy 已提交
1075
	if (!vol) {
1076
		/* No need to do further work, cancel */
1077
		dbg_wl("volume %d is being removed, cancel", vol_id);
A
Artem Bityutskiy 已提交
1078
		return MOVE_CANCEL_RACE;
A
Artem B. Bityutskiy 已提交
1079 1080
	}

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
	/*
	 * We do not want anybody to write to this logical eraseblock while we
	 * are moving it, so lock it.
	 *
	 * Note, we are using non-waiting locking here, because we cannot sleep
	 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
	 * unmapping the LEB which is mapped to the PEB we are going to move
	 * (@from). This task locks the LEB and goes sleep in the
	 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
	 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
A
Artem Bityutskiy 已提交
1091
	 * LEB is already locked, we just do not move it and return
1092 1093 1094
	 * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because
	 * we do not know the reasons of the contention - it may be just a
	 * normal I/O on this LEB, so we want to re-try.
1095 1096 1097
	 */
	err = leb_write_trylock(ubi, vol_id, lnum);
	if (err) {
1098
		dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
1099
		return MOVE_RETRY;
A
Artem B. Bityutskiy 已提交
1100 1101
	}

1102 1103 1104 1105 1106 1107
	/*
	 * The LEB might have been put meanwhile, and the task which put it is
	 * probably waiting on @ubi->move_mutex. No need to continue the work,
	 * cancel it.
	 */
	if (vol->eba_tbl[lnum] != from) {
A
Artem Bityutskiy 已提交
1108 1109
		dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
		       vol_id, lnum, from, vol->eba_tbl[lnum]);
A
Artem Bityutskiy 已提交
1110
		err = MOVE_CANCEL_RACE;
1111 1112
		goto out_unlock_leb;
	}
A
Artem B. Bityutskiy 已提交
1113

1114
	/*
Z
Zoltan Sogor 已提交
1115
	 * OK, now the LEB is locked and we can safely start moving it. Since
1116
	 * this function utilizes the @ubi->peb_buf buffer which is shared
A
Artem Bityutskiy 已提交
1117
	 * with some other functions - we lock the buffer by taking the
1118 1119 1120
	 * @ubi->buf_mutex.
	 */
	mutex_lock(&ubi->buf_mutex);
1121
	dbg_wl("read %d bytes of data", aldata_size);
1122
	err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
A
Artem B. Bityutskiy 已提交
1123
	if (err && err != UBI_IO_BITFLIPS) {
1124
		ubi_warn(ubi, "error %d while reading data from PEB %d",
A
Artem B. Bityutskiy 已提交
1125
			 err, from);
A
Artem Bityutskiy 已提交
1126
		err = MOVE_SOURCE_RD_ERR;
1127
		goto out_unlock_buf;
A
Artem B. Bityutskiy 已提交
1128 1129 1130
	}

	/*
1131
	 * Now we have got to calculate how much data we have to copy. In
A
Artem B. Bityutskiy 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
	 * case of a static volume it is fairly easy - the VID header contains
	 * the data size. In case of a dynamic volume it is more difficult - we
	 * have to read the contents, cut 0xFF bytes from the end and copy only
	 * the first part. We must do this to avoid writing 0xFF bytes as it
	 * may have some side-effects. And not only this. It is important not
	 * to include those 0xFFs to CRC because later the they may be filled
	 * by data.
	 */
	if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
		aldata_size = data_size =
1142
			ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
A
Artem B. Bityutskiy 已提交
1143 1144

	cond_resched();
1145
	crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
A
Artem B. Bityutskiy 已提交
1146 1147 1148
	cond_resched();

	/*
A
Artem Bityutskiy 已提交
1149
	 * It may turn out to be that the whole @from physical eraseblock
A
Artem B. Bityutskiy 已提交
1150 1151 1152 1153 1154 1155
	 * contains only 0xFF bytes. Then we have to only write the VID header
	 * and do not write any data. This also means we should not set
	 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
	 */
	if (data_size > 0) {
		vid_hdr->copy_flag = 1;
1156 1157
		vid_hdr->data_size = cpu_to_be32(data_size);
		vid_hdr->data_crc = cpu_to_be32(crc);
A
Artem B. Bityutskiy 已提交
1158
	}
R
Richard Weinberger 已提交
1159
	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
A
Artem B. Bityutskiy 已提交
1160 1161

	err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1162 1163
	if (err) {
		if (err == -EIO)
A
Artem Bityutskiy 已提交
1164
			err = MOVE_TARGET_WR_ERR;
1165
		goto out_unlock_buf;
1166
	}
A
Artem B. Bityutskiy 已提交
1167 1168 1169 1170 1171 1172

	cond_resched();

	/* Read the VID header back and check if it was written correctly */
	err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
	if (err) {
1173
		if (err != UBI_IO_BITFLIPS) {
1174
			ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
A
Artem Bityutskiy 已提交
1175
				 err, to);
A
Artem Bityutskiy 已提交
1176
			if (is_error_sane(err))
1177 1178
				err = MOVE_TARGET_RD_ERR;
		} else
1179
			err = MOVE_TARGET_BITFLIPS;
1180
		goto out_unlock_buf;
A
Artem B. Bityutskiy 已提交
1181 1182 1183
	}

	if (data_size > 0) {
1184
		err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1185 1186
		if (err) {
			if (err == -EIO)
A
Artem Bityutskiy 已提交
1187
				err = MOVE_TARGET_WR_ERR;
1188
			goto out_unlock_buf;
1189
		}
A
Artem B. Bityutskiy 已提交
1190

1191 1192
		cond_resched();

A
Artem B. Bityutskiy 已提交
1193 1194 1195 1196
		/*
		 * We've written the data and are going to read it back to make
		 * sure it was written correctly.
		 */
1197 1198
		memset(ubi->peb_buf, 0xFF, aldata_size);
		err = ubi_io_read_data(ubi, ubi->peb_buf, to, 0, aldata_size);
A
Artem B. Bityutskiy 已提交
1199
		if (err) {
1200
			if (err != UBI_IO_BITFLIPS) {
1201
				ubi_warn(ubi, "error %d while reading data back from PEB %d",
A
Artem Bityutskiy 已提交
1202
					 err, to);
A
Artem Bityutskiy 已提交
1203
				if (is_error_sane(err))
1204 1205
					err = MOVE_TARGET_RD_ERR;
			} else
1206
				err = MOVE_TARGET_BITFLIPS;
1207
			goto out_unlock_buf;
A
Artem B. Bityutskiy 已提交
1208 1209 1210 1211
		}

		cond_resched();

1212
		if (crc != crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size)) {
1213
			ubi_warn(ubi, "read data back from PEB %d and it is different",
A
Artem Bityutskiy 已提交
1214
				 to);
1215
			err = -EINVAL;
1216
			goto out_unlock_buf;
A
Artem B. Bityutskiy 已提交
1217 1218 1219 1220
		}
	}

	ubi_assert(vol->eba_tbl[lnum] == from);
R
Richard Weinberger 已提交
1221
	down_read(&ubi->fm_sem);
A
Artem B. Bityutskiy 已提交
1222
	vol->eba_tbl[lnum] = to;
R
Richard Weinberger 已提交
1223
	up_read(&ubi->fm_sem);
A
Artem B. Bityutskiy 已提交
1224

1225
out_unlock_buf:
1226
	mutex_unlock(&ubi->buf_mutex);
1227
out_unlock_leb:
A
Artem B. Bityutskiy 已提交
1228 1229 1230 1231
	leb_write_unlock(ubi, vol_id, lnum);
	return err;
}

1232 1233 1234 1235
/**
 * print_rsvd_warning - warn about not having enough reserved PEBs.
 * @ubi: UBI device description object
 *
1236
 * This is a helper function for 'ubi_eba_init()' which is called when UBI
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
 * cannot reserve enough PEBs for bad block handling. This function makes a
 * decision whether we have to print a warning or not. The algorithm is as
 * follows:
 *   o if this is a new UBI image, then just print the warning
 *   o if this is an UBI image which has already been used for some time, print
 *     a warning only if we can reserve less than 10% of the expected amount of
 *     the reserved PEB.
 *
 * The idea is that when UBI is used, PEBs become bad, and the reserved pool
 * of PEBs becomes smaller, which is normal and we do not want to scare users
 * with a warning every time they attach the MTD device. This was an issue
 * reported by real users.
 */
static void print_rsvd_warning(struct ubi_device *ubi,
A
Artem Bityutskiy 已提交
1251
			       struct ubi_attach_info *ai)
1252 1253 1254 1255 1256
{
	/*
	 * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
	 * large number to distinguish between newly flashed and used images.
	 */
A
Artem Bityutskiy 已提交
1257
	if (ai->max_sqnum > (1 << 18)) {
1258 1259 1260 1261 1262 1263 1264 1265
		int min = ubi->beb_rsvd_level / 10;

		if (!min)
			min = 1;
		if (ubi->beb_rsvd_pebs > min)
			return;
	}

1266
	ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
A
Artem Bityutskiy 已提交
1267
		 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
A
Artem Bityutskiy 已提交
1268
	if (ubi->corr_peb_count)
1269
		ubi_warn(ubi, "%d PEBs are corrupted and not used",
A
Artem Bityutskiy 已提交
1270
			 ubi->corr_peb_count);
1271 1272
}

R
Richard Weinberger 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
/**
 * self_check_eba - run a self check on the EBA table constructed by fastmap.
 * @ubi: UBI device description object
 * @ai_fastmap: UBI attach info object created by fastmap
 * @ai_scan: UBI attach info object created by scanning
 *
 * Returns < 0 in case of an internal error, 0 otherwise.
 * If a bad EBA table entry was found it will be printed out and
 * ubi_assert() triggers.
 */
int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
		   struct ubi_attach_info *ai_scan)
{
	int i, j, num_volumes, ret = 0;
	int **scan_eba, **fm_eba;
	struct ubi_ainf_volume *av;
	struct ubi_volume *vol;
	struct ubi_ainf_peb *aeb;
	struct rb_node *rb;

	num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;

	scan_eba = kmalloc(sizeof(*scan_eba) * num_volumes, GFP_KERNEL);
	if (!scan_eba)
		return -ENOMEM;

	fm_eba = kmalloc(sizeof(*fm_eba) * num_volumes, GFP_KERNEL);
	if (!fm_eba) {
		kfree(scan_eba);
		return -ENOMEM;
	}

	for (i = 0; i < num_volumes; i++) {
		vol = ubi->volumes[i];
		if (!vol)
			continue;

		scan_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**scan_eba),
				      GFP_KERNEL);
		if (!scan_eba[i]) {
			ret = -ENOMEM;
			goto out_free;
		}

		fm_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**fm_eba),
				    GFP_KERNEL);
		if (!fm_eba[i]) {
			ret = -ENOMEM;
			goto out_free;
		}

		for (j = 0; j < vol->reserved_pebs; j++)
			scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED;

		av = ubi_find_av(ai_scan, idx2vol_id(ubi, i));
		if (!av)
			continue;

		ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
			scan_eba[i][aeb->lnum] = aeb->pnum;

		av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i));
		if (!av)
			continue;

		ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
			fm_eba[i][aeb->lnum] = aeb->pnum;

		for (j = 0; j < vol->reserved_pebs; j++) {
			if (scan_eba[i][j] != fm_eba[i][j]) {
				if (scan_eba[i][j] == UBI_LEB_UNMAPPED ||
					fm_eba[i][j] == UBI_LEB_UNMAPPED)
					continue;

1347
				ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!",
R
Richard Weinberger 已提交
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
					vol->vol_id, i, fm_eba[i][j],
					scan_eba[i][j]);
				ubi_assert(0);
			}
		}
	}

out_free:
	for (i = 0; i < num_volumes; i++) {
		if (!ubi->volumes[i])
			continue;

		kfree(scan_eba[i]);
		kfree(fm_eba[i]);
	}

	kfree(scan_eba);
	kfree(fm_eba);
	return ret;
}

A
Artem B. Bityutskiy 已提交
1369
/**
1370
 * ubi_eba_init - initialize the EBA sub-system using attaching information.
A
Artem B. Bityutskiy 已提交
1371
 * @ubi: UBI device description object
A
Artem Bityutskiy 已提交
1372
 * @ai: attaching information
A
Artem B. Bityutskiy 已提交
1373 1374 1375 1376
 *
 * This function returns zero in case of success and a negative error code in
 * case of failure.
 */
1377
int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
A
Artem B. Bityutskiy 已提交
1378 1379
{
	int i, j, err, num_volumes;
A
Artem Bityutskiy 已提交
1380
	struct ubi_ainf_volume *av;
A
Artem B. Bityutskiy 已提交
1381
	struct ubi_volume *vol;
A
Artem Bityutskiy 已提交
1382
	struct ubi_ainf_peb *aeb;
A
Artem B. Bityutskiy 已提交
1383 1384
	struct rb_node *rb;

A
Artem Bityutskiy 已提交
1385
	dbg_eba("initialize EBA sub-system");
A
Artem B. Bityutskiy 已提交
1386 1387

	spin_lock_init(&ubi->ltree_lock);
1388
	mutex_init(&ubi->alc_mutex);
A
Artem B. Bityutskiy 已提交
1389 1390
	ubi->ltree = RB_ROOT;

A
Artem Bityutskiy 已提交
1391
	ubi->global_sqnum = ai->max_sqnum + 1;
A
Artem B. Bityutskiy 已提交
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
	num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;

	for (i = 0; i < num_volumes; i++) {
		vol = ubi->volumes[i];
		if (!vol)
			continue;

		cond_resched();

		vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
				       GFP_KERNEL);
		if (!vol->eba_tbl) {
			err = -ENOMEM;
			goto out_free;
		}

		for (j = 0; j < vol->reserved_pebs; j++)
			vol->eba_tbl[j] = UBI_LEB_UNMAPPED;

A
Artem Bityutskiy 已提交
1411
		av = ubi_find_av(ai, idx2vol_id(ubi, i));
A
Artem Bityutskiy 已提交
1412
		if (!av)
A
Artem B. Bityutskiy 已提交
1413 1414
			continue;

A
Artem Bityutskiy 已提交
1415
		ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
A
Artem Bityutskiy 已提交
1416
			if (aeb->lnum >= vol->reserved_pebs)
A
Artem B. Bityutskiy 已提交
1417 1418 1419 1420
				/*
				 * This may happen in case of an unclean reboot
				 * during re-size.
				 */
1421
				ubi_move_aeb_to_list(av, aeb, &ai->erase);
B
Brian Norris 已提交
1422 1423
			else
				vol->eba_tbl[aeb->lnum] = aeb->pnum;
A
Artem B. Bityutskiy 已提交
1424 1425 1426
		}
	}

1427
	if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1428
		ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
1429
			ubi->avail_pebs, EBA_RESERVED_PEBS);
A
Artem Bityutskiy 已提交
1430
		if (ubi->corr_peb_count)
1431
			ubi_err(ubi, "%d PEBs are corrupted and not used",
A
Artem Bityutskiy 已提交
1432
				ubi->corr_peb_count);
1433 1434 1435 1436 1437 1438
		err = -ENOSPC;
		goto out_free;
	}
	ubi->avail_pebs -= EBA_RESERVED_PEBS;
	ubi->rsvd_pebs += EBA_RESERVED_PEBS;

A
Artem B. Bityutskiy 已提交
1439 1440 1441 1442 1443 1444
	if (ubi->bad_allowed) {
		ubi_calculate_reserved(ubi);

		if (ubi->avail_pebs < ubi->beb_rsvd_level) {
			/* No enough free physical eraseblocks */
			ubi->beb_rsvd_pebs = ubi->avail_pebs;
A
Artem Bityutskiy 已提交
1445
			print_rsvd_warning(ubi, ai);
A
Artem B. Bityutskiy 已提交
1446 1447 1448 1449 1450 1451 1452
		} else
			ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;

		ubi->avail_pebs -= ubi->beb_rsvd_pebs;
		ubi->rsvd_pebs  += ubi->beb_rsvd_pebs;
	}

A
Artem Bityutskiy 已提交
1453
	dbg_eba("EBA sub-system is initialized");
A
Artem B. Bityutskiy 已提交
1454 1455 1456 1457 1458 1459 1460
	return 0;

out_free:
	for (i = 0; i < num_volumes; i++) {
		if (!ubi->volumes[i])
			continue;
		kfree(ubi->volumes[i]->eba_tbl);
1461
		ubi->volumes[i]->eba_tbl = NULL;
A
Artem B. Bityutskiy 已提交
1462 1463 1464
	}
	return err;
}