kapi.c 22.6 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
/*
 * 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 (Битюцкий Артём)
 */

/* This file mostly implements UBI kernel API functions */

#include <linux/module.h>
#include <linux/err.h>
C
Corentin Chary 已提交
25 26
#include <linux/namei.h>
#include <linux/fs.h>
A
Artem B. Bityutskiy 已提交
27 28 29
#include <asm/div64.h>
#include "ubi.h"

D
Dmitry Pervushin 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/**
 * ubi_do_get_device_info - get information about UBI device.
 * @ubi: UBI device description object
 * @di: the information is stored here
 *
 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
 * device is locked and cannot disappear.
 */
void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
{
	di->ubi_num = ubi->ubi_num;
	di->leb_size = ubi->leb_size;
	di->min_io_size = ubi->min_io_size;
	di->ro_mode = ubi->ro_mode;
	di->cdev = ubi->cdev.dev;
}
EXPORT_SYMBOL_GPL(ubi_do_get_device_info);

A
Artem B. Bityutskiy 已提交
48 49 50 51 52
/**
 * ubi_get_device_info - get information about UBI device.
 * @ubi_num: UBI device number
 * @di: the information is stored here
 *
53 54
 * This function returns %0 in case of success, %-EINVAL if the UBI device
 * number is invalid, and %-ENODEV if there is no such UBI device.
A
Artem B. Bityutskiy 已提交
55 56 57
 */
int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
{
58 59 60 61 62 63
	struct ubi_device *ubi;

	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
		return -EINVAL;
	ubi = ubi_get_device(ubi_num);
	if (!ubi)
A
Artem B. Bityutskiy 已提交
64
		return -ENODEV;
D
Dmitry Pervushin 已提交
65
	ubi_do_get_device_info(ubi, di);
66
	ubi_put_device(ubi);
A
Artem B. Bityutskiy 已提交
67 68 69 70 71
	return 0;
}
EXPORT_SYMBOL_GPL(ubi_get_device_info);

/**
D
Dmitry Pervushin 已提交
72 73 74
 * ubi_do_get_volume_info - get information about UBI volume.
 * @ubi: UBI device description object
 * @vol: volume description object
A
Artem B. Bityutskiy 已提交
75 76
 * @vi: the information is stored here
 */
D
Dmitry Pervushin 已提交
77 78
void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
			    struct ubi_volume_info *vi)
A
Artem B. Bityutskiy 已提交
79 80 81 82 83 84 85 86 87 88 89 90
{
	vi->vol_id = vol->vol_id;
	vi->ubi_num = ubi->ubi_num;
	vi->size = vol->reserved_pebs;
	vi->used_bytes = vol->used_bytes;
	vi->vol_type = vol->vol_type;
	vi->corrupted = vol->corrupted;
	vi->upd_marker = vol->upd_marker;
	vi->alignment = vol->alignment;
	vi->usable_leb_size = vol->usable_leb_size;
	vi->name_len = vol->name_len;
	vi->name = vol->name;
A
Artem Bityutskiy 已提交
91
	vi->cdev = vol->cdev.dev;
A
Artem B. Bityutskiy 已提交
92
}
D
Dmitry Pervushin 已提交
93 94 95 96 97 98 99 100 101 102 103

/**
 * ubi_get_volume_info - get information about UBI volume.
 * @desc: volume descriptor
 * @vi: the information is stored here
 */
void ubi_get_volume_info(struct ubi_volume_desc *desc,
			 struct ubi_volume_info *vi)
{
	ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
}
A
Artem B. Bityutskiy 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
EXPORT_SYMBOL_GPL(ubi_get_volume_info);

/**
 * ubi_open_volume - open UBI volume.
 * @ubi_num: UBI device number
 * @vol_id: volume ID
 * @mode: open mode
 *
 * The @mode parameter specifies if the volume should be opened in read-only
 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
 * nobody else will be able to open this volume. UBI allows to have many volume
 * readers and one writer at a time.
 *
 * If a static volume is being opened for the first time since boot, it will be
 * checked by this function, which means it will be fully read and the CRC
 * checksum of each logical eraseblock will be checked.
 *
 * This function returns volume descriptor in case of success and a negative
 * error code in case of failure.
 */
struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
{
	int err;
	struct ubi_volume_desc *desc;
128
	struct ubi_device *ubi;
A
Artem B. Bityutskiy 已提交
129 130
	struct ubi_volume *vol;

131
	dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
A
Artem B. Bityutskiy 已提交
132

133 134
	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
		return ERR_PTR(-EINVAL);
135

136 137 138
	if (mode != UBI_READONLY && mode != UBI_READWRITE &&
	    mode != UBI_EXCLUSIVE)
		return ERR_PTR(-EINVAL);
A
Artem B. Bityutskiy 已提交
139

140 141 142 143
	/*
	 * First of all, we have to get the UBI device to prevent its removal.
	 */
	ubi = ubi_get_device(ubi_num);
144 145
	if (!ubi)
		return ERR_PTR(-ENODEV);
A
Artem B. Bityutskiy 已提交
146

147 148 149 150
	if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
		err = -EINVAL;
		goto out_put_ubi;
	}
A
Artem B. Bityutskiy 已提交
151 152

	desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
153 154 155 156
	if (!desc) {
		err = -ENOMEM;
		goto out_put_ubi;
	}
157 158 159 160

	err = -ENODEV;
	if (!try_module_get(THIS_MODULE))
		goto out_free;
A
Artem B. Bityutskiy 已提交
161 162 163

	spin_lock(&ubi->volumes_lock);
	vol = ubi->volumes[vol_id];
164
	if (!vol)
A
Artem B. Bityutskiy 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
		goto out_unlock;

	err = -EBUSY;
	switch (mode) {
	case UBI_READONLY:
		if (vol->exclusive)
			goto out_unlock;
		vol->readers += 1;
		break;

	case UBI_READWRITE:
		if (vol->exclusive || vol->writers > 0)
			goto out_unlock;
		vol->writers += 1;
		break;

	case UBI_EXCLUSIVE:
		if (vol->exclusive || vol->writers || vol->readers)
			goto out_unlock;
		vol->exclusive = 1;
		break;
	}
187
	get_device(&vol->dev);
188
	vol->ref_count += 1;
A
Artem B. Bityutskiy 已提交
189 190 191 192 193
	spin_unlock(&ubi->volumes_lock);

	desc->vol = vol;
	desc->mode = mode;

194
	mutex_lock(&ubi->ckvol_mutex);
A
Artem B. Bityutskiy 已提交
195 196 197 198
	if (!vol->checked) {
		/* This is the first open - check the volume */
		err = ubi_check_volume(ubi, vol_id);
		if (err < 0) {
199
			mutex_unlock(&ubi->ckvol_mutex);
A
Artem B. Bityutskiy 已提交
200 201 202 203 204 205 206 207 208 209
			ubi_close_volume(desc);
			return ERR_PTR(err);
		}
		if (err == 1) {
			ubi_warn("volume %d on UBI device %d is corrupted",
				 vol_id, ubi->ubi_num);
			vol->corrupted = 1;
		}
		vol->checked = 1;
	}
210
	mutex_unlock(&ubi->ckvol_mutex);
211

A
Artem B. Bityutskiy 已提交
212 213 214 215 216
	return desc;

out_unlock:
	spin_unlock(&ubi->volumes_lock);
	module_put(THIS_MODULE);
217 218
out_free:
	kfree(desc);
219 220
out_put_ubi:
	ubi_put_device(ubi);
221 222
	dbg_err("cannot open device %d, volume %d, error %d",
		ubi_num, vol_id, err);
A
Artem B. Bityutskiy 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	return ERR_PTR(err);
}
EXPORT_SYMBOL_GPL(ubi_open_volume);

/**
 * ubi_open_volume_nm - open UBI volume by name.
 * @ubi_num: UBI device number
 * @name: volume name
 * @mode: open mode
 *
 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
 */
struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
					   int mode)
{
	int i, vol_id = -1, len;
	struct ubi_device *ubi;
240
	struct ubi_volume_desc *ret;
A
Artem B. Bityutskiy 已提交
241

242
	dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
A
Artem B. Bityutskiy 已提交
243 244 245 246 247 248 249 250

	if (!name)
		return ERR_PTR(-EINVAL);

	len = strnlen(name, UBI_VOL_NAME_MAX + 1);
	if (len > UBI_VOL_NAME_MAX)
		return ERR_PTR(-EINVAL);

251 252
	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
		return ERR_PTR(-EINVAL);
A
Artem B. Bityutskiy 已提交
253

254
	ubi = ubi_get_device(ubi_num);
255 256
	if (!ubi)
		return ERR_PTR(-ENODEV);
A
Artem B. Bityutskiy 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269

	spin_lock(&ubi->volumes_lock);
	/* Walk all volumes of this UBI device */
	for (i = 0; i < ubi->vtbl_slots; i++) {
		struct ubi_volume *vol = ubi->volumes[i];

		if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
			vol_id = i;
			break;
		}
	}
	spin_unlock(&ubi->volumes_lock);

270 271 272 273
	if (vol_id >= 0)
		ret = ubi_open_volume(ubi_num, vol_id, mode);
	else
		ret = ERR_PTR(-ENODEV);
A
Artem B. Bityutskiy 已提交
274

275 276 277 278 279 280
	/*
	 * We should put the UBI device even in case of success, because
	 * 'ubi_open_volume()' took a reference as well.
	 */
	ubi_put_device(ubi);
	return ret;
A
Artem B. Bityutskiy 已提交
281 282 283
}
EXPORT_SYMBOL_GPL(ubi_open_volume_nm);

C
Corentin Chary 已提交
284 285 286 287 288 289 290 291 292 293
/**
 * ubi_open_volume_path - open UBI volume by its character device node path.
 * @pathname: volume character device node path
 * @mode: open mode
 *
 * This function is similar to 'ubi_open_volume()', but opens a volume the path
 * to its character device node.
 */
struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
{
294
	int error, ubi_num, vol_id, mod;
C
Corentin Chary 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307
	struct inode *inode;
	struct path path;

	dbg_gen("open volume %s, mode %d", pathname, mode);

	if (!pathname || !*pathname)
		return ERR_PTR(-EINVAL);

	error = kern_path(pathname, LOOKUP_FOLLOW, &path);
	if (error)
		return ERR_PTR(error);

	inode = path.dentry->d_inode;
308
	mod = inode->i_mode;
C
Corentin Chary 已提交
309 310
	ubi_num = ubi_major2num(imajor(inode));
	vol_id = iminor(inode) - 1;
311
	path_put(&path);
C
Corentin Chary 已提交
312

313 314
	if (!S_ISCHR(mod))
		return ERR_PTR(-EINVAL);
C
Corentin Chary 已提交
315
	if (vol_id >= 0 && ubi_num >= 0)
316 317
		return ubi_open_volume(ubi_num, vol_id, mode);
	return ERR_PTR(-ENODEV);
C
Corentin Chary 已提交
318 319 320
}
EXPORT_SYMBOL_GPL(ubi_open_volume_path);

A
Artem B. Bityutskiy 已提交
321 322 323 324 325 326 327
/**
 * ubi_close_volume - close UBI volume.
 * @desc: volume descriptor
 */
void ubi_close_volume(struct ubi_volume_desc *desc)
{
	struct ubi_volume *vol = desc->vol;
328
	struct ubi_device *ubi = vol->ubi;
A
Artem B. Bityutskiy 已提交
329

330 331
	dbg_gen("close device %d, volume %d, mode %d",
		ubi->ubi_num, vol->vol_id, desc->mode);
A
Artem B. Bityutskiy 已提交
332

333
	spin_lock(&ubi->volumes_lock);
A
Artem B. Bityutskiy 已提交
334 335 336 337 338 339 340 341 342 343
	switch (desc->mode) {
	case UBI_READONLY:
		vol->readers -= 1;
		break;
	case UBI_READWRITE:
		vol->writers -= 1;
		break;
	case UBI_EXCLUSIVE:
		vol->exclusive = 0;
	}
344
	vol->ref_count -= 1;
345
	spin_unlock(&ubi->volumes_lock);
A
Artem B. Bityutskiy 已提交
346

347
	kfree(desc);
348 349
	put_device(&vol->dev);
	ubi_put_device(ubi);
A
Artem B. Bityutskiy 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	module_put(THIS_MODULE);
}
EXPORT_SYMBOL_GPL(ubi_close_volume);

/**
 * ubi_leb_read - read data.
 * @desc: volume descriptor
 * @lnum: logical eraseblock number to read from
 * @buf: buffer where to store the read data
 * @offset: offset within the logical eraseblock to read from
 * @len: how many bytes to read
 * @check: whether UBI has to check the read data's CRC or not.
 *
 * This function reads data from offset @offset of logical eraseblock @lnum and
 * stores the data at @buf. When reading from static volumes, @check specifies
 * whether the data has to be checked or not. If yes, the whole logical
 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
 * checksum is per-eraseblock). So checking may substantially slow down the
 * read speed. The @check argument is ignored for dynamic volumes.
 *
 * In case of success, this function returns zero. In case of failure, this
 * function returns a negative error code.
 *
 * %-EBADMSG error code is returned:
 * o for both static and dynamic volumes if MTD driver has detected a data
 *   integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
 * o for static volumes in case of data CRC mismatch.
 *
 * If the volume is damaged because of an interrupted update this function just
 * returns immediately with %-EBADF error code.
 */
int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
		 int len, int check)
{
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;
	int err, vol_id = vol->vol_id;

388
	dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
A
Artem B. Bityutskiy 已提交
389 390 391 392 393 394

	if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
	    lnum >= vol->used_ebs || offset < 0 || len < 0 ||
	    offset + len > vol->usable_leb_size)
		return -EINVAL;

395 396 397 398 399 400 401 402
	if (vol->vol_type == UBI_STATIC_VOLUME) {
		if (vol->used_ebs == 0)
			/* Empty static UBI volume */
			return 0;
		if (lnum == vol->used_ebs - 1 &&
		    offset + len > vol->last_eb_bytes)
			return -EINVAL;
	}
A
Artem B. Bityutskiy 已提交
403 404 405 406 407 408

	if (vol->upd_marker)
		return -EBADF;
	if (len == 0)
		return 0;

409
	err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
A
Artem B. Bityutskiy 已提交
410 411 412 413 414 415 416 417 418 419 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 447 448 449 450 451 452
	if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
		ubi_warn("mark volume %d as corrupted", vol_id);
		vol->corrupted = 1;
	}

	return err;
}
EXPORT_SYMBOL_GPL(ubi_leb_read);

/**
 * ubi_leb_write - write data.
 * @desc: volume descriptor
 * @lnum: logical eraseblock number to write to
 * @buf: data to write
 * @offset: offset within the logical eraseblock where to write
 * @len: how many bytes to write
 * @dtype: expected data type
 *
 * This function writes @len bytes of data from @buf to offset @offset of
 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
 * the data.
 *
 * This function takes care of physical eraseblock write failures. If write to
 * the physical eraseblock write operation fails, the logical eraseblock is
 * re-mapped to another physical eraseblock, the data is recovered, and the
 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
 *
 * If all the data were successfully written, zero is returned. If an error
 * occurred and UBI has not been able to recover from it, this function returns
 * a negative error code. Note, in case of an error, it is possible that
 * something was still written to the flash media, but that may be some
 * garbage.
 *
 * If the volume is damaged because of an interrupted update this function just
 * returns immediately with %-EBADF code.
 */
int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
		  int offset, int len, int dtype)
{
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;
	int vol_id = vol->vol_id;

453
	dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
A
Artem B. Bityutskiy 已提交
454 455 456 457 458 459 460 461

	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
		return -EINVAL;

	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
		return -EROFS;

	if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
462 463
	    offset + len > vol->usable_leb_size ||
	    offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
A
Artem B. Bityutskiy 已提交
464 465 466 467 468 469 470 471 472 473 474 475
		return -EINVAL;

	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
	    dtype != UBI_UNKNOWN)
		return -EINVAL;

	if (vol->upd_marker)
		return -EBADF;

	if (len == 0)
		return 0;

476
	return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
A
Artem B. Bityutskiy 已提交
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
}
EXPORT_SYMBOL_GPL(ubi_leb_write);

/*
 * ubi_leb_change - change logical eraseblock atomically.
 * @desc: volume descriptor
 * @lnum: logical eraseblock number to change
 * @buf: data to write
 * @len: how many bytes to write
 * @dtype: expected 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. The length may be shorter then the logical
 * eraseblock size, ant the logical eraseblock may be appended to more times
 * later on. 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.
 */
int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
		   int len, int dtype)
{
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;
	int vol_id = vol->vol_id;

503
	dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
A
Artem B. Bityutskiy 已提交
504 505 506 507 508 509 510 511

	if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
		return -EINVAL;

	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
		return -EROFS;

	if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
512
	    len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
A
Artem B. Bityutskiy 已提交
513 514 515 516 517 518 519 520 521 522 523 524
		return -EINVAL;

	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
	    dtype != UBI_UNKNOWN)
		return -EINVAL;

	if (vol->upd_marker)
		return -EBADF;

	if (len == 0)
		return 0;

525
	return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
A
Artem B. Bityutskiy 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
}
EXPORT_SYMBOL_GPL(ubi_leb_change);

/**
 * ubi_leb_erase - erase logical eraseblock.
 * @desc: volume descriptor
 * @lnum: logical eraseblock number
 *
 * This function un-maps logical eraseblock @lnum and synchronously erases the
 * correspondent physical eraseblock. Returns zero in case of success and a
 * negative error code in case of failure.
 *
 * If the volume is damaged because of an interrupted update this function just
 * returns immediately with %-EBADF code.
 */
int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
{
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;
A
Artem Bityutskiy 已提交
545
	int err;
A
Artem B. Bityutskiy 已提交
546

547
	dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
A
Artem B. Bityutskiy 已提交
548 549 550 551 552 553 554 555 556 557

	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
		return -EROFS;

	if (lnum < 0 || lnum >= vol->reserved_pebs)
		return -EINVAL;

	if (vol->upd_marker)
		return -EBADF;

558
	err = ubi_eba_unmap_leb(ubi, vol, lnum);
A
Artem B. Bityutskiy 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
	if (err)
		return err;

	return ubi_wl_flush(ubi);
}
EXPORT_SYMBOL_GPL(ubi_leb_erase);

/**
 * ubi_leb_unmap - un-map logical eraseblock.
 * @desc: volume descriptor
 * @lnum: logical eraseblock number
 *
 * This function un-maps logical eraseblock @lnum and schedules the
 * corresponding physical eraseblock for erasure, so that it will eventually be
 * physically erased in background. This operation is much faster then the
 * erase operation.
 *
 * Unlike erase, the un-map operation does not guarantee that the logical
 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
 * happens after this, the logical eraseblocks will not necessarily be
 * un-mapped again when this MTD device is attached. They may actually be
 * mapped to the same physical eraseblocks again. So, this function has to be
 * used with care.
 *
 * In other words, when un-mapping a logical eraseblock, UBI does not store
 * any information about this on the flash media, it just marks the logical
 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
 * eraseblock is physically erased, it will be mapped again to the same logical
 * eraseblock when the MTD device is attached again.
 *
 * The main and obvious use-case of this function is when the contents of a
 * logical eraseblock has to be re-written. Then it is much more efficient to
 * first un-map it, then write new data, rather then first erase it, then write
 * new data. Note, once new data has been written to the logical eraseblock,
 * UBI guarantees that the old contents has gone forever. In other words, if an
 * unclean reboot happens after the logical eraseblock has been un-mapped and
 * then written to, it will contain the last written data.
 *
 * This function returns zero in case of success and a negative error code in
 * case of failure. If the volume is damaged because of an interrupted update
 * this function just returns immediately with %-EBADF code.
 */
int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
{
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;

607
	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
A
Artem B. Bityutskiy 已提交
608 609 610 611 612 613 614 615 616 617

	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
		return -EROFS;

	if (lnum < 0 || lnum >= vol->reserved_pebs)
		return -EINVAL;

	if (vol->upd_marker)
		return -EBADF;

618
	return ubi_eba_unmap_leb(ubi, vol, lnum);
A
Artem B. Bityutskiy 已提交
619 620
}
EXPORT_SYMBOL_GPL(ubi_leb_unmap);
A
Artem Bityutskiy 已提交
621 622

/**
D
Dmitry Pervushin 已提交
623
 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
A
Artem Bityutskiy 已提交
624 625 626 627 628
 * @desc: volume descriptor
 * @lnum: logical eraseblock number
 * @dtype: expected data type
 *
 * This function maps an un-mapped logical eraseblock @lnum to a physical
C
Coly Li 已提交
629
 * eraseblock. This means, that after a successful invocation of this
A
Artem Bityutskiy 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643
 * function the logical eraseblock @lnum will be empty (contain only %0xFF
 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
 * happens.
 *
 * This function returns zero in case of success, %-EBADF if the volume is
 * damaged because of an interrupted update, %-EBADMSG if the logical
 * eraseblock is already mapped, and other negative error codes in case of
 * other failures.
 */
int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
{
	struct ubi_volume *vol = desc->vol;
	struct ubi_device *ubi = vol->ubi;

644
	dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
A
Artem Bityutskiy 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

	if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
		return -EROFS;

	if (lnum < 0 || lnum >= vol->reserved_pebs)
		return -EINVAL;

	if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
	    dtype != UBI_UNKNOWN)
		return -EINVAL;

	if (vol->upd_marker)
		return -EBADF;

	if (vol->eba_tbl[lnum] >= 0)
		return -EBADMSG;

662
	return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
A
Artem Bityutskiy 已提交
663 664
}
EXPORT_SYMBOL_GPL(ubi_leb_map);
A
Artem B. Bityutskiy 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685

/**
 * ubi_is_mapped - check if logical eraseblock is mapped.
 * @desc: volume descriptor
 * @lnum: logical eraseblock number
 *
 * This function checks if logical eraseblock @lnum is mapped to a physical
 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
 * mean it will still be un-mapped after the UBI device is re-attached. The
 * logical eraseblock may become mapped to the physical eraseblock it was last
 * mapped to.
 *
 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
 * error code in case of failure. If the volume is damaged because of an
 * interrupted update this function just returns immediately with %-EBADF error
 * code.
 */
int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
{
	struct ubi_volume *vol = desc->vol;

686
	dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
A
Artem B. Bityutskiy 已提交
687 688 689 690 691 692 693 694 695 696

	if (lnum < 0 || lnum >= vol->reserved_pebs)
		return -EINVAL;

	if (vol->upd_marker)
		return -EBADF;

	return vol->eba_tbl[lnum] >= 0;
}
EXPORT_SYMBOL_GPL(ubi_is_mapped);
A
Artem Bityutskiy 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720

/**
 * ubi_sync - synchronize UBI device buffers.
 * @ubi_num: UBI device to synchronize
 *
 * The underlying MTD device may cache data in hardware or in software. This
 * function ensures the caches are flushed. Returns zero in case of success and
 * a negative error code in case of failure.
 */
int ubi_sync(int ubi_num)
{
	struct ubi_device *ubi;

	ubi = ubi_get_device(ubi_num);
	if (!ubi)
		return -ENODEV;

	if (ubi->mtd->sync)
		ubi->mtd->sync(ubi->mtd);

	ubi_put_device(ubi);
	return 0;
}
EXPORT_SYMBOL_GPL(ubi_sync);
D
Dmitry Pervushin 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

BLOCKING_NOTIFIER_HEAD(ubi_notifiers);

/**
 * ubi_register_volume_notifier - register a volume notifier.
 * @nb: the notifier description object
 * @ignore_existing: if non-zero, do not send "added" notification for all
 *                   already existing volumes
 *
 * This function registers a volume notifier, which means that
 * 'nb->notifier_call()' will be invoked when an UBI  volume is created,
 * removed, re-sized, re-named, or updated. The first argument of the function
 * is the notification type. The second argument is pointer to a
 * &struct ubi_notification object which describes the notification event.
 * Using UBI API from the volume notifier is prohibited.
 *
 * This function returns zero in case of success and a negative error code
 * in case of failure.
 */
int ubi_register_volume_notifier(struct notifier_block *nb,
				 int ignore_existing)
{
	int err;

	err = blocking_notifier_chain_register(&ubi_notifiers, nb);
	if (err != 0)
		return err;
	if (ignore_existing)
		return 0;

	/*
	 * We are going to walk all UBI devices and all volumes, and
	 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
	 * event. We have to lock the @ubi_devices_mutex to make sure UBI
	 * devices do not disappear.
	 */
	mutex_lock(&ubi_devices_mutex);
	ubi_enumerate_volumes(nb);
	mutex_unlock(&ubi_devices_mutex);

	return err;
}
EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);

/**
 * ubi_unregister_volume_notifier - unregister the volume notifier.
 * @nb: the notifier description object
 *
 * This function unregisters volume notifier @nm and returns zero in case of
 * success and a negative error code in case of failure.
 */
int ubi_unregister_volume_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
}
EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);