eba.c 33.3 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 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * 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 (Битюцкий Артём)
 */

/*
 * The UBI Eraseblock Association (EBA) unit.
 *
 * This unit is responsible for I/O to/from logical eraseblock.
 *
 * 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.
 *
 * The EBA unit 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
34
 * lock tree elements are &struct ubi_ltree_entry objects. They are indexed by
A
Artem B. Bityutskiy 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48
 * (@vol_id, @lnum) pairs.
 *
 * 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/**
 * 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.
 */
static unsigned long long next_sqnum(struct ubi_device *ubi)
{
	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 192

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

	if (le_free)
193
		kfree(le_free);
A
Artem B. Bityutskiy 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

	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)
{
209
	struct ubi_ltree_entry *le;
A
Artem B. Bityutskiy 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

	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)
{
	int free = 0;
227
	struct ubi_ltree_entry *le;
A
Artem B. Bityutskiy 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240

	spin_lock(&ubi->ltree_lock);
	le = ltree_lookup(ubi, vol_id, lnum);
	le->users -= 1;
	ubi_assert(le->users >= 0);
	if (le->users == 0) {
		rb_erase(&le->rb, &ubi->ltree);
		free = 1;
	}
	spin_unlock(&ubi->ltree_lock);

	up_read(&le->mutex);
	if (free)
241
		kfree(le);
A
Artem B. Bityutskiy 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254
}

/**
 * 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)
{
255
	struct ubi_ltree_entry *le;
A
Artem B. Bityutskiy 已提交
256 257 258 259 260 261 262 263

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

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
/**
 * 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)
{
	int free;
	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);
		free = 1;
	} else
		free = 0;
	spin_unlock(&ubi->ltree_lock);
	if (free)
297
		kfree(le);
298 299 300 301

	return 1;
}

A
Artem B. Bityutskiy 已提交
302 303 304 305 306 307 308 309 310
/**
 * 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)
{
	int free;
311
	struct ubi_ltree_entry *le;
A
Artem B. Bityutskiy 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325

	spin_lock(&ubi->ltree_lock);
	le = ltree_lookup(ubi, vol_id, lnum);
	le->users -= 1;
	ubi_assert(le->users >= 0);
	if (le->users == 0) {
		rb_erase(&le->rb, &ubi->ltree);
		free = 1;
	} else
		free = 0;
	spin_unlock(&ubi->ltree_lock);

	up_write(&le->mutex);
	if (free)
326
		kfree(le);
A
Artem B. Bityutskiy 已提交
327 328 329 330 331
}

/**
 * ubi_eba_unmap_leb - un-map logical eraseblock.
 * @ubi: UBI device description object
332
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
333 334 335 336 337 338
 * @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.
 */
339 340
int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
		      int lnum)
A
Artem B. Bityutskiy 已提交
341
{
342
	int err, pnum, vol_id = vol->vol_id;
A
Artem B. Bityutskiy 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

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

	vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
	err = ubi_wl_put_peb(ubi, pnum, 0);

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

/**
 * ubi_eba_read_leb - read data.
 * @ubi: UBI device description object
369
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
 * @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.
 */
385 386
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 已提交
387
{
388
	int err, pnum, scrub = 0, vol_id = vol->vol_id;
A
Artem B. Bityutskiy 已提交
389
	struct ubi_vid_hdr *vid_hdr;
390
	uint32_t uninitialized_var(crc);
A
Artem B. Bityutskiy 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

	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) {
419
		vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
A
Artem B. Bityutskiy 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
		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.
				 */
				if (err == UBI_IO_BAD_VID_HDR) {
					ubi_warn("bad VID header at PEB %d, LEB"
						 "%d:%d", pnum, vol_id, lnum);
					err = -EBADMSG;
				} else
					ubi_ro_mode(ubi);
			}
			goto out_free;
		} else if (err == UBI_IO_BITFLIPS)
			scrub = 1;

447 448
		ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
		ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
A
Artem B. Bityutskiy 已提交
449

450
		crc = be32_to_cpu(vid_hdr->data_crc);
A
Artem B. Bityutskiy 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
		ubi_free_vid_hdr(ubi, vid_hdr);
	}

	err = ubi_io_read_data(ubi, buf, pnum, offset, len);
	if (err) {
		if (err == UBI_IO_BITFLIPS) {
			scrub = 1;
			err = 0;
		} else if (err == -EBADMSG) {
			if (vol->vol_type == UBI_DYNAMIC_VOLUME)
				goto out_unlock;
			scrub = 1;
			if (!check) {
				ubi_msg("force data checking");
				check = 1;
				goto retry;
			}
		} else
			goto out_unlock;
	}

	if (check) {
473
		uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
A
Artem B. Bityutskiy 已提交
474 475 476 477 478 479 480 481 482 483 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
		if (crc1 != crc) {
			ubi_warn("CRC error: calculated %#08x, must be %#08x",
				 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;
}

/**
 * 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;

518
	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
A
Artem B. Bityutskiy 已提交
519 520 521 522
	if (!vid_hdr) {
		return -ENOMEM;
	}

523 524
	mutex_lock(&ubi->buf_mutex);

A
Artem B. Bityutskiy 已提交
525 526 527
retry:
	new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
	if (new_pnum < 0) {
528
		mutex_unlock(&ubi->buf_mutex);
A
Artem B. Bityutskiy 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541
		ubi_free_vid_hdr(ubi, vid_hdr);
		return new_pnum;
	}

	ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum);

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

542
	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
A
Artem B. Bityutskiy 已提交
543 544 545 546 547
	err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
	if (err)
		goto write_error;

	data_size = offset + len;
548
	memset(ubi->peb_buf1 + offset, 0xFF, len);
A
Artem B. Bityutskiy 已提交
549 550 551

	/* Read everything before the area where the write failure happened */
	if (offset > 0) {
552 553
		err = ubi_io_read_data(ubi, ubi->peb_buf1, pnum, 0, offset);
		if (err && err != UBI_IO_BITFLIPS)
A
Artem B. Bityutskiy 已提交
554 555 556
			goto out_put;
	}

557
	memcpy(ubi->peb_buf1 + offset, buf, len);
A
Artem B. Bityutskiy 已提交
558

559 560
	err = ubi_io_write_data(ubi, ubi->peb_buf1, new_pnum, 0, data_size);
	if (err)
A
Artem B. Bityutskiy 已提交
561 562
		goto write_error;

563
	mutex_unlock(&ubi->buf_mutex);
A
Artem B. Bityutskiy 已提交
564 565 566 567 568 569 570 571 572
	ubi_free_vid_hdr(ubi, vid_hdr);

	vol->eba_tbl[lnum] = new_pnum;
	ubi_wl_put_peb(ubi, pnum, 1);

	ubi_msg("data was successfully recovered");
	return 0;

out_put:
573
	mutex_unlock(&ubi->buf_mutex);
A
Artem B. Bityutskiy 已提交
574 575 576 577 578 579 580 581 582 583 584 585
	ubi_wl_put_peb(ubi, new_pnum, 1);
	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.
	 */
	ubi_warn("failed to write to PEB %d", new_pnum);
	ubi_wl_put_peb(ubi, new_pnum, 1);
	if (++tries > UBI_IO_RETRIES) {
586
		mutex_unlock(&ubi->buf_mutex);
A
Artem B. Bityutskiy 已提交
587 588 589 590 591 592 593 594 595 596
		ubi_free_vid_hdr(ubi, vid_hdr);
		return err;
	}
	ubi_msg("try again");
	goto retry;
}

/**
 * ubi_eba_write_leb - write data to dynamic volume.
 * @ubi: UBI device description object
597
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
598 599 600 601 602 603 604
 * @lnum: logical eraseblock number
 * @buf: the data to write
 * @offset: offset within the logical eraseblock where to write
 * @len: how many bytes to write
 * @dtype: data type
 *
 * This function writes data to logical eraseblock @lnum of a dynamic volume
605
 * @vol. Returns zero in case of success and a negative error code in case
A
Artem B. Bityutskiy 已提交
606 607 608
 * of failure. In case of error, it is possible that something was still
 * written to the flash media, but may be some garbage.
 */
609
int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
A
Artem B. Bityutskiy 已提交
610 611
		      const void *buf, int offset, int len, int dtype)
{
612
	int err, pnum, tries = 0, vol_id = vol->vol_id;
A
Artem B. Bityutskiy 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	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) {
			ubi_warn("failed to write data to PEB %d", pnum);
			if (err == -EIO && ubi->bad_allowed)
631 632
				err = recover_peb(ubi, pnum, vol_id, lnum, buf,
						  offset, len);
A
Artem B. Bityutskiy 已提交
633 634 635 636 637 638 639 640 641 642 643
			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.
	 */
644
	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
A
Artem B. Bityutskiy 已提交
645 646 647 648 649 650
	if (!vid_hdr) {
		leb_write_unlock(ubi, vol_id, lnum);
		return -ENOMEM;
	}

	vid_hdr->vol_type = UBI_VID_DYNAMIC;
651 652 653
	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
	vid_hdr->vol_id = cpu_to_be32(vol_id);
	vid_hdr->lnum = cpu_to_be32(lnum);
A
Artem B. Bityutskiy 已提交
654
	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
655
	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
A
Artem B. Bityutskiy 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

retry:
	pnum = ubi_wl_get_peb(ubi, dtype);
	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) {
		ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
			 vol_id, lnum, pnum);
		goto write_error;
	}

A
Artem Bityutskiy 已提交
675 676 677 678 679 680 681 682
	if (len) {
		err = ubi_io_write_data(ubi, buf, pnum, offset, len);
		if (err) {
			ubi_warn("failed to write %d bytes at offset %d of "
				 "LEB %d:%d, PEB %d", len, offset, vol_id,
				 lnum, pnum);
			goto write_error;
		}
A
Artem B. Bityutskiy 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	}

	vol->eba_tbl[lnum] = pnum;

	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.
	 */
	err = ubi_wl_put_peb(ubi, pnum, 1);
	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;
	}

712
	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
A
Artem B. Bityutskiy 已提交
713 714 715 716 717 718 719
	ubi_msg("try another PEB");
	goto retry;
}

/**
 * ubi_eba_write_leb_st - write data to static volume.
 * @ubi: UBI device description object
720
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
721 722 723 724 725 726 727
 * @lnum: logical eraseblock number
 * @buf: data to write
 * @len: how many bytes to write
 * @dtype: data type
 * @used_ebs: how many logical eraseblocks will this volume contain
 *
 * This function writes data to logical eraseblock @lnum of static volume
728
 * @vol. The @used_ebs argument should contain total number of logical
A
Artem B. Bityutskiy 已提交
729 730 731 732 733 734 735 736 737 738 739
 * 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.
 *
 * It is prohibited to write more then once to logical eraseblocks of static
 * volumes. This function returns zero in case of success and a negative error
 * code in case of failure.
 */
740 741 742
int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
			 int lnum, const void *buf, int len, int dtype,
			 int used_ebs)
A
Artem B. Bityutskiy 已提交
743
{
744
	int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
A
Artem B. Bityutskiy 已提交
745 746 747 748 749 750 751 752 753 754 755 756
	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
		ubi_assert(len % ubi->min_io_size == 0);

757
	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
A
Artem B. Bityutskiy 已提交
758 759 760 761 762 763 764 765 766
	if (!vid_hdr)
		return -ENOMEM;

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

767 768 769
	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
	vid_hdr->vol_id = cpu_to_be32(vol_id);
	vid_hdr->lnum = cpu_to_be32(lnum);
A
Artem B. Bityutskiy 已提交
770
	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
771
	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
A
Artem B. Bityutskiy 已提交
772 773 774

	crc = crc32(UBI_CRC32_INIT, buf, data_size);
	vid_hdr->vol_type = UBI_VID_STATIC;
775 776 777
	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 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831

retry:
	pnum = ubi_wl_get_peb(ubi, dtype);
	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) {
		ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
			 vol_id, lnum, pnum);
		goto write_error;
	}

	err = ubi_io_write_data(ubi, buf, pnum, 0, len);
	if (err) {
		ubi_warn("failed to write %d bytes of data to PEB %d",
			 len, pnum);
		goto write_error;
	}

	ubi_assert(vol->eba_tbl[lnum] < 0);
	vol->eba_tbl[lnum] = pnum;

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

	err = ubi_wl_put_peb(ubi, pnum, 1);
	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;
	}

832
	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
A
Artem B. Bityutskiy 已提交
833 834 835 836 837 838 839
	ubi_msg("try another PEB");
	goto retry;
}

/*
 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
 * @ubi: UBI device description object
A
Artem Bityutskiy 已提交
840
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
841 842 843 844 845 846 847 848 849 850
 * @lnum: logical eraseblock number
 * @buf: data to write
 * @len: how many bytes to write
 * @dtype: data type
 *
 * 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.
851 852 853
 *
 * 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 已提交
854
 */
855 856
int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
			      int lnum, const void *buf, int len, int dtype)
A
Artem B. Bityutskiy 已提交
857
{
858
	int err, pnum, tries = 0, vol_id = vol->vol_id;
A
Artem B. Bityutskiy 已提交
859 860 861 862 863 864
	struct ubi_vid_hdr *vid_hdr;
	uint32_t crc;

	if (ubi->ro_mode)
		return -EROFS;

A
Artem Bityutskiy 已提交
865 866 867 868 869 870 871 872 873 874 875
	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;
		return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
	}

876
	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
A
Artem B. Bityutskiy 已提交
877 878 879
	if (!vid_hdr)
		return -ENOMEM;

880
	mutex_lock(&ubi->alc_mutex);
A
Artem B. Bityutskiy 已提交
881
	err = leb_write_lock(ubi, vol_id, lnum);
882 883
	if (err)
		goto out_mutex;
A
Artem B. Bityutskiy 已提交
884

885 886 887
	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
	vid_hdr->vol_id = cpu_to_be32(vol_id);
	vid_hdr->lnum = cpu_to_be32(lnum);
A
Artem B. Bityutskiy 已提交
888
	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
889
	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
A
Artem B. Bityutskiy 已提交
890 891

	crc = crc32(UBI_CRC32_INIT, buf, len);
892
	vid_hdr->vol_type = UBI_VID_DYNAMIC;
893
	vid_hdr->data_size = cpu_to_be32(len);
A
Artem B. Bityutskiy 已提交
894
	vid_hdr->copy_flag = 1;
895
	vid_hdr->data_crc = cpu_to_be32(crc);
A
Artem B. Bityutskiy 已提交
896 897 898 899

retry:
	pnum = ubi_wl_get_peb(ubi, dtype);
	if (pnum < 0) {
900 901
		err = pnum;
		goto out_leb_unlock;
A
Artem B. Bityutskiy 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	}

	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) {
		ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
			 vol_id, lnum, pnum);
		goto write_error;
	}

	err = ubi_io_write_data(ubi, buf, pnum, 0, len);
	if (err) {
		ubi_warn("failed to write %d bytes of data to PEB %d",
			 len, pnum);
		goto write_error;
	}

921 922
	if (vol->eba_tbl[lnum] >= 0) {
		err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1);
923 924
		if (err)
			goto out_leb_unlock;
A
Artem B. Bityutskiy 已提交
925 926 927
	}

	vol->eba_tbl[lnum] = pnum;
928 929

out_leb_unlock:
A
Artem B. Bityutskiy 已提交
930
	leb_write_unlock(ubi, vol_id, lnum);
931 932
out_mutex:
	mutex_unlock(&ubi->alc_mutex);
A
Artem B. Bityutskiy 已提交
933
	ubi_free_vid_hdr(ubi, vid_hdr);
934
	return err;
A
Artem B. Bityutskiy 已提交
935 936 937 938 939 940 941 942 943

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);
944
		goto out_leb_unlock;
A
Artem B. Bityutskiy 已提交
945 946 947 948 949
	}

	err = ubi_wl_put_peb(ubi, pnum, 1);
	if (err || ++tries > UBI_IO_RETRIES) {
		ubi_ro_mode(ubi);
950
		goto out_leb_unlock;
A
Artem B. Bityutskiy 已提交
951 952
	}

953
	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
A
Artem B. Bityutskiy 已提交
954 955 956 957 958 959 960 961 962 963 964 965 966
	ubi_msg("try another PEB");
	goto retry;
}

/**
 * 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
967 968 969 970 971
 * function. Returns:
 *   o %0  in case of success;
 *   o %1 if the operation was canceled and should be tried later (e.g.,
 *     because a bit-flip was detected at the target PEB);
 *   o %2 if the volume is being deleted and this LEB should not be moved.
A
Artem B. Bityutskiy 已提交
972 973 974 975
 */
int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
		     struct ubi_vid_hdr *vid_hdr)
{
976
	int err, vol_id, lnum, data_size, aldata_size, idx;
A
Artem B. Bityutskiy 已提交
977 978 979
	struct ubi_volume *vol;
	uint32_t crc;

980 981
	vol_id = be32_to_cpu(vid_hdr->vol_id);
	lnum = be32_to_cpu(vid_hdr->lnum);
A
Artem B. Bityutskiy 已提交
982 983 984 985

	dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);

	if (vid_hdr->vol_type == UBI_VID_STATIC) {
986
		data_size = be32_to_cpu(vid_hdr->data_size);
A
Artem B. Bityutskiy 已提交
987 988 989
		aldata_size = ALIGN(data_size, ubi->min_io_size);
	} else
		data_size = aldata_size =
990
			    ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
A
Artem B. Bityutskiy 已提交
991 992

	idx = vol_id2idx(ubi, vol_id);
993
	spin_lock(&ubi->volumes_lock);
A
Artem B. Bityutskiy 已提交
994
	/*
995 996 997 998
	 * Note, we may race with volume deletion, which means that the volume
	 * this logical eraseblock belongs to might be being deleted. Since the
	 * volume deletion unmaps all the volume's logical eraseblocks, it will
	 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
A
Artem B. Bityutskiy 已提交
999 1000 1001
	 */
	vol = ubi->volumes[idx];
	if (!vol) {
1002 1003
		/* No need to do further work, cancel */
		dbg_eba("volume %d is being removed, cancel", vol_id);
A
Artem B. Bityutskiy 已提交
1004
		spin_unlock(&ubi->volumes_lock);
1005
		return 2;
A
Artem B. Bityutskiy 已提交
1006
	}
1007
	spin_unlock(&ubi->volumes_lock);
A
Artem B. Bityutskiy 已提交
1008

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	/*
	 * 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
	 * LEB is already locked, we just do not move it and return %1.
	 */
	err = leb_write_trylock(ubi, vol_id, lnum);
	if (err) {
		dbg_eba("contention on LEB %d:%d, cancel", vol_id, lnum);
		return err;
A
Artem B. Bityutskiy 已提交
1025 1026
	}

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
	/*
	 * 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) {
		dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
			"PEB %d, cancel", vol_id, lnum, from,
			vol->eba_tbl[lnum]);
		err = 1;
		goto out_unlock_leb;
	}
A
Artem B. Bityutskiy 已提交
1039

1040 1041 1042 1043 1044 1045 1046
	/*
	 * OK, now the LEB is locked and we can safely start moving iy. Since
	 * this function utilizes thie @ubi->peb1_buf buffer which is shared
	 * with some other functions, so lock the buffer by taking the
	 * @ubi->buf_mutex.
	 */
	mutex_lock(&ubi->buf_mutex);
A
Artem B. Bityutskiy 已提交
1047
	dbg_eba("read %d bytes of data", aldata_size);
1048
	err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
A
Artem B. Bityutskiy 已提交
1049 1050 1051
	if (err && err != UBI_IO_BITFLIPS) {
		ubi_warn("error %d while reading data from PEB %d",
			 err, from);
1052
		goto out_unlock_buf;
A
Artem B. Bityutskiy 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
	}

	/*
	 * Now we have got to calculate how much data we have to to copy. In
	 * 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 =
1067
			ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
A
Artem B. Bityutskiy 已提交
1068 1069

	cond_resched();
1070
	crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
A
Artem B. Bityutskiy 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	cond_resched();

	/*
	 * It may turn out to me that the whole @from physical eraseblock
	 * 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;
1081 1082
		vid_hdr->data_size = cpu_to_be32(data_size);
		vid_hdr->data_crc = cpu_to_be32(crc);
A
Artem B. Bityutskiy 已提交
1083
	}
1084
	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
A
Artem B. Bityutskiy 已提交
1085 1086 1087

	err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
	if (err)
1088
		goto out_unlock_buf;
A
Artem B. Bityutskiy 已提交
1089 1090 1091 1092 1093 1094 1095 1096

	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) {
		if (err != UBI_IO_BITFLIPS)
			ubi_warn("cannot read VID header back from PEB %d", to);
1097 1098 1099
		else
			err = 1;
		goto out_unlock_buf;
A
Artem B. Bityutskiy 已提交
1100 1101 1102
	}

	if (data_size > 0) {
1103
		err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
A
Artem B. Bityutskiy 已提交
1104
		if (err)
1105
			goto out_unlock_buf;
A
Artem B. Bityutskiy 已提交
1106

1107 1108
		cond_resched();

A
Artem B. Bityutskiy 已提交
1109 1110 1111 1112 1113
		/*
		 * We've written the data and are going to read it back to make
		 * sure it was written correctly.
		 */

1114
		err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
A
Artem B. Bityutskiy 已提交
1115 1116 1117 1118
		if (err) {
			if (err != UBI_IO_BITFLIPS)
				ubi_warn("cannot read data back from PEB %d",
					 to);
1119 1120 1121
			else
				err = 1;
			goto out_unlock_buf;
A
Artem B. Bityutskiy 已提交
1122 1123 1124 1125
		}

		cond_resched();

1126
		if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
A
Artem B. Bityutskiy 已提交
1127 1128
			ubi_warn("read data back from PEB %d - it is different",
				 to);
1129
			goto out_unlock_buf;
A
Artem B. Bityutskiy 已提交
1130 1131 1132 1133 1134 1135
		}
	}

	ubi_assert(vol->eba_tbl[lnum] == from);
	vol->eba_tbl[lnum] = to;

1136
out_unlock_buf:
1137
	mutex_unlock(&ubi->buf_mutex);
1138
out_unlock_leb:
A
Artem B. Bityutskiy 已提交
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
	leb_write_unlock(ubi, vol_id, lnum);
	return err;
}

/**
 * ubi_eba_init_scan - initialize the EBA unit using scanning information.
 * @ubi: UBI device description object
 * @si: scanning information
 *
 * This function returns zero in case of success and a negative error code in
 * case of failure.
 */
int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
{
	int i, j, err, num_volumes;
	struct ubi_scan_volume *sv;
	struct ubi_volume *vol;
	struct ubi_scan_leb *seb;
	struct rb_node *rb;

	dbg_eba("initialize EBA unit");

	spin_lock_init(&ubi->ltree_lock);
1162
	mutex_init(&ubi->alc_mutex);
A
Artem B. Bityutskiy 已提交
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
	ubi->ltree = RB_ROOT;

	ubi->global_sqnum = si->max_sqnum + 1;
	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;

		sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
		if (!sv)
			continue;

		ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
			if (seb->lnum >= vol->reserved_pebs)
				/*
				 * This may happen in case of an unclean reboot
				 * during re-size.
				 */
				ubi_scan_move_to_list(sv, seb, &si->erase);
			vol->eba_tbl[seb->lnum] = seb->pnum;
		}
	}

1200 1201 1202 1203 1204 1205 1206 1207 1208
	if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
		ubi_err("no enough physical eraseblocks (%d, need %d)",
			ubi->avail_pebs, EBA_RESERVED_PEBS);
		err = -ENOSPC;
		goto out_free;
	}
	ubi->avail_pebs -= EBA_RESERVED_PEBS;
	ubi->rsvd_pebs += EBA_RESERVED_PEBS;

A
Artem B. Bityutskiy 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
	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;
			ubi_warn("cannot reserve enough PEBs for bad PEB "
				 "handling, reserved %d, need %d",
				 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
		} else
			ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;

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

	dbg_eba("EBA unit is initialized");
	return 0;

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

/**
 * ubi_eba_close - close EBA unit.
 * @ubi: UBI device description object
 */
void ubi_eba_close(const struct ubi_device *ubi)
{
	int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;

	dbg_eba("close EBA unit");

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