diff options
Diffstat (limited to 'drivers/mtd/ubi/kapi.c')
| -rw-r--r-- | drivers/mtd/ubi/kapi.c | 575 |
1 files changed, 575 insertions, 0 deletions
diff --git a/drivers/mtd/ubi/kapi.c b/drivers/mtd/ubi/kapi.c new file mode 100644 index 000000000000..d352c4575c3d --- /dev/null +++ b/drivers/mtd/ubi/kapi.c | |||
| @@ -0,0 +1,575 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) International Business Machines Corp., 2006 | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License as published by | ||
| 6 | * the Free Software Foundation; either version 2 of the License, or | ||
| 7 | * (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | ||
| 12 | * the GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 17 | * | ||
| 18 | * Author: Artem Bityutskiy (Битюцкий Артём) | ||
| 19 | */ | ||
| 20 | |||
| 21 | /* This file mostly implements UBI kernel API functions */ | ||
| 22 | |||
| 23 | #include <linux/module.h> | ||
| 24 | #include <linux/err.h> | ||
| 25 | #include <asm/div64.h> | ||
| 26 | #include "ubi.h" | ||
| 27 | |||
| 28 | /** | ||
| 29 | * ubi_get_device_info - get information about UBI device. | ||
| 30 | * @ubi_num: UBI device number | ||
| 31 | * @di: the information is stored here | ||
| 32 | * | ||
| 33 | * This function returns %0 in case of success and a %-ENODEV if there is no | ||
| 34 | * such UBI device. | ||
| 35 | */ | ||
| 36 | int ubi_get_device_info(int ubi_num, struct ubi_device_info *di) | ||
| 37 | { | ||
| 38 | const struct ubi_device *ubi; | ||
| 39 | |||
| 40 | if (!try_module_get(THIS_MODULE)) | ||
| 41 | return -ENODEV; | ||
| 42 | |||
| 43 | if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || | ||
| 44 | !ubi_devices[ubi_num]) { | ||
| 45 | module_put(THIS_MODULE); | ||
| 46 | return -ENODEV; | ||
| 47 | } | ||
| 48 | |||
| 49 | ubi = ubi_devices[ubi_num]; | ||
| 50 | di->ubi_num = ubi->ubi_num; | ||
| 51 | di->leb_size = ubi->leb_size; | ||
| 52 | di->min_io_size = ubi->min_io_size; | ||
| 53 | di->ro_mode = ubi->ro_mode; | ||
| 54 | di->cdev = MKDEV(ubi->major, 0); | ||
| 55 | module_put(THIS_MODULE); | ||
| 56 | return 0; | ||
| 57 | } | ||
| 58 | EXPORT_SYMBOL_GPL(ubi_get_device_info); | ||
| 59 | |||
| 60 | /** | ||
| 61 | * ubi_get_volume_info - get information about UBI volume. | ||
| 62 | * @desc: volume descriptor | ||
| 63 | * @vi: the information is stored here | ||
| 64 | */ | ||
| 65 | void ubi_get_volume_info(struct ubi_volume_desc *desc, | ||
| 66 | struct ubi_volume_info *vi) | ||
| 67 | { | ||
| 68 | const struct ubi_volume *vol = desc->vol; | ||
| 69 | const struct ubi_device *ubi = vol->ubi; | ||
| 70 | |||
| 71 | vi->vol_id = vol->vol_id; | ||
| 72 | vi->ubi_num = ubi->ubi_num; | ||
| 73 | vi->size = vol->reserved_pebs; | ||
| 74 | vi->used_bytes = vol->used_bytes; | ||
| 75 | vi->vol_type = vol->vol_type; | ||
| 76 | vi->corrupted = vol->corrupted; | ||
| 77 | vi->upd_marker = vol->upd_marker; | ||
| 78 | vi->alignment = vol->alignment; | ||
| 79 | vi->usable_leb_size = vol->usable_leb_size; | ||
| 80 | vi->name_len = vol->name_len; | ||
| 81 | vi->name = vol->name; | ||
| 82 | vi->cdev = MKDEV(ubi->major, vi->vol_id + 1); | ||
| 83 | } | ||
| 84 | EXPORT_SYMBOL_GPL(ubi_get_volume_info); | ||
| 85 | |||
| 86 | /** | ||
| 87 | * ubi_open_volume - open UBI volume. | ||
| 88 | * @ubi_num: UBI device number | ||
| 89 | * @vol_id: volume ID | ||
| 90 | * @mode: open mode | ||
| 91 | * | ||
| 92 | * The @mode parameter specifies if the volume should be opened in read-only | ||
| 93 | * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that | ||
| 94 | * nobody else will be able to open this volume. UBI allows to have many volume | ||
| 95 | * readers and one writer at a time. | ||
| 96 | * | ||
| 97 | * If a static volume is being opened for the first time since boot, it will be | ||
| 98 | * checked by this function, which means it will be fully read and the CRC | ||
| 99 | * checksum of each logical eraseblock will be checked. | ||
| 100 | * | ||
| 101 | * This function returns volume descriptor in case of success and a negative | ||
| 102 | * error code in case of failure. | ||
| 103 | */ | ||
| 104 | struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode) | ||
| 105 | { | ||
| 106 | int err; | ||
| 107 | struct ubi_volume_desc *desc; | ||
| 108 | struct ubi_device *ubi = ubi_devices[ubi_num]; | ||
| 109 | struct ubi_volume *vol; | ||
| 110 | |||
| 111 | dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode); | ||
| 112 | |||
| 113 | err = -ENODEV; | ||
| 114 | if (!try_module_get(THIS_MODULE)) | ||
| 115 | return ERR_PTR(err); | ||
| 116 | |||
| 117 | if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi) | ||
| 118 | goto out_put; | ||
| 119 | |||
| 120 | err = -EINVAL; | ||
| 121 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots) | ||
| 122 | goto out_put; | ||
| 123 | if (mode != UBI_READONLY && mode != UBI_READWRITE && | ||
| 124 | mode != UBI_EXCLUSIVE) | ||
| 125 | goto out_put; | ||
| 126 | |||
| 127 | desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL); | ||
| 128 | if (!desc) { | ||
| 129 | err = -ENOMEM; | ||
| 130 | goto out_put; | ||
| 131 | } | ||
| 132 | |||
| 133 | spin_lock(&ubi->volumes_lock); | ||
| 134 | vol = ubi->volumes[vol_id]; | ||
| 135 | if (!vol) { | ||
| 136 | err = -ENODEV; | ||
| 137 | goto out_unlock; | ||
| 138 | } | ||
| 139 | |||
| 140 | err = -EBUSY; | ||
| 141 | switch (mode) { | ||
| 142 | case UBI_READONLY: | ||
| 143 | if (vol->exclusive) | ||
| 144 | goto out_unlock; | ||
| 145 | vol->readers += 1; | ||
| 146 | break; | ||
| 147 | |||
| 148 | case UBI_READWRITE: | ||
| 149 | if (vol->exclusive || vol->writers > 0) | ||
| 150 | goto out_unlock; | ||
| 151 | vol->writers += 1; | ||
| 152 | break; | ||
| 153 | |||
| 154 | case UBI_EXCLUSIVE: | ||
| 155 | if (vol->exclusive || vol->writers || vol->readers) | ||
| 156 | goto out_unlock; | ||
| 157 | vol->exclusive = 1; | ||
| 158 | break; | ||
| 159 | } | ||
| 160 | spin_unlock(&ubi->volumes_lock); | ||
| 161 | |||
| 162 | desc->vol = vol; | ||
| 163 | desc->mode = mode; | ||
| 164 | |||
| 165 | /* | ||
| 166 | * To prevent simultaneous checks of the same volume we use @vtbl_mutex, | ||
| 167 | * although it is not the purpose it was introduced for. | ||
| 168 | */ | ||
| 169 | mutex_lock(&ubi->vtbl_mutex); | ||
| 170 | if (!vol->checked) { | ||
| 171 | /* This is the first open - check the volume */ | ||
| 172 | err = ubi_check_volume(ubi, vol_id); | ||
| 173 | if (err < 0) { | ||
| 174 | mutex_unlock(&ubi->vtbl_mutex); | ||
| 175 | ubi_close_volume(desc); | ||
| 176 | return ERR_PTR(err); | ||
| 177 | } | ||
| 178 | if (err == 1) { | ||
| 179 | ubi_warn("volume %d on UBI device %d is corrupted", | ||
| 180 | vol_id, ubi->ubi_num); | ||
| 181 | vol->corrupted = 1; | ||
| 182 | } | ||
| 183 | vol->checked = 1; | ||
| 184 | } | ||
| 185 | mutex_unlock(&ubi->vtbl_mutex); | ||
| 186 | return desc; | ||
| 187 | |||
| 188 | out_unlock: | ||
| 189 | spin_unlock(&ubi->volumes_lock); | ||
| 190 | kfree(desc); | ||
| 191 | out_put: | ||
| 192 | module_put(THIS_MODULE); | ||
| 193 | return ERR_PTR(err); | ||
| 194 | } | ||
| 195 | EXPORT_SYMBOL_GPL(ubi_open_volume); | ||
| 196 | |||
| 197 | /** | ||
| 198 | * ubi_open_volume_nm - open UBI volume by name. | ||
| 199 | * @ubi_num: UBI device number | ||
| 200 | * @name: volume name | ||
| 201 | * @mode: open mode | ||
| 202 | * | ||
| 203 | * This function is similar to 'ubi_open_volume()', but opens a volume by name. | ||
| 204 | */ | ||
| 205 | struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, | ||
| 206 | int mode) | ||
| 207 | { | ||
| 208 | int i, vol_id = -1, len; | ||
| 209 | struct ubi_volume_desc *ret; | ||
| 210 | struct ubi_device *ubi; | ||
| 211 | |||
| 212 | dbg_msg("open volume %s, mode %d", name, mode); | ||
| 213 | |||
| 214 | if (!name) | ||
| 215 | return ERR_PTR(-EINVAL); | ||
| 216 | |||
| 217 | len = strnlen(name, UBI_VOL_NAME_MAX + 1); | ||
| 218 | if (len > UBI_VOL_NAME_MAX) | ||
| 219 | return ERR_PTR(-EINVAL); | ||
| 220 | |||
| 221 | ret = ERR_PTR(-ENODEV); | ||
| 222 | if (!try_module_get(THIS_MODULE)) | ||
| 223 | return ret; | ||
| 224 | |||
| 225 | if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi_devices[ubi_num]) | ||
| 226 | goto out_put; | ||
| 227 | |||
| 228 | ubi = ubi_devices[ubi_num]; | ||
| 229 | |||
| 230 | spin_lock(&ubi->volumes_lock); | ||
| 231 | /* Walk all volumes of this UBI device */ | ||
| 232 | for (i = 0; i < ubi->vtbl_slots; i++) { | ||
| 233 | struct ubi_volume *vol = ubi->volumes[i]; | ||
| 234 | |||
| 235 | if (vol && len == vol->name_len && !strcmp(name, vol->name)) { | ||
| 236 | vol_id = i; | ||
| 237 | break; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | spin_unlock(&ubi->volumes_lock); | ||
| 241 | |||
| 242 | if (vol_id < 0) | ||
| 243 | goto out_put; | ||
| 244 | |||
| 245 | ret = ubi_open_volume(ubi_num, vol_id, mode); | ||
| 246 | |||
| 247 | out_put: | ||
| 248 | module_put(THIS_MODULE); | ||
| 249 | return ret; | ||
| 250 | } | ||
| 251 | EXPORT_SYMBOL_GPL(ubi_open_volume_nm); | ||
| 252 | |||
| 253 | /** | ||
| 254 | * ubi_close_volume - close UBI volume. | ||
| 255 | * @desc: volume descriptor | ||
| 256 | */ | ||
| 257 | void ubi_close_volume(struct ubi_volume_desc *desc) | ||
| 258 | { | ||
| 259 | struct ubi_volume *vol = desc->vol; | ||
| 260 | |||
| 261 | dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode); | ||
| 262 | |||
| 263 | spin_lock(&vol->ubi->volumes_lock); | ||
| 264 | switch (desc->mode) { | ||
| 265 | case UBI_READONLY: | ||
| 266 | vol->readers -= 1; | ||
| 267 | break; | ||
| 268 | case UBI_READWRITE: | ||
| 269 | vol->writers -= 1; | ||
| 270 | break; | ||
| 271 | case UBI_EXCLUSIVE: | ||
| 272 | vol->exclusive = 0; | ||
| 273 | } | ||
| 274 | spin_unlock(&vol->ubi->volumes_lock); | ||
| 275 | |||
| 276 | kfree(desc); | ||
| 277 | module_put(THIS_MODULE); | ||
| 278 | } | ||
| 279 | EXPORT_SYMBOL_GPL(ubi_close_volume); | ||
| 280 | |||
| 281 | /** | ||
| 282 | * ubi_leb_read - read data. | ||
| 283 | * @desc: volume descriptor | ||
| 284 | * @lnum: logical eraseblock number to read from | ||
| 285 | * @buf: buffer where to store the read data | ||
| 286 | * @offset: offset within the logical eraseblock to read from | ||
| 287 | * @len: how many bytes to read | ||
| 288 | * @check: whether UBI has to check the read data's CRC or not. | ||
| 289 | * | ||
| 290 | * This function reads data from offset @offset of logical eraseblock @lnum and | ||
| 291 | * stores the data at @buf. When reading from static volumes, @check specifies | ||
| 292 | * whether the data has to be checked or not. If yes, the whole logical | ||
| 293 | * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC | ||
| 294 | * checksum is per-eraseblock). So checking may substantially slow down the | ||
| 295 | * read speed. The @check argument is ignored for dynamic volumes. | ||
| 296 | * | ||
| 297 | * In case of success, this function returns zero. In case of failure, this | ||
| 298 | * function returns a negative error code. | ||
| 299 | * | ||
| 300 | * %-EBADMSG error code is returned: | ||
| 301 | * o for both static and dynamic volumes if MTD driver has detected a data | ||
| 302 | * integrity problem (unrecoverable ECC checksum mismatch in case of NAND); | ||
| 303 | * o for static volumes in case of data CRC mismatch. | ||
| 304 | * | ||
| 305 | * If the volume is damaged because of an interrupted update this function just | ||
| 306 | * returns immediately with %-EBADF error code. | ||
| 307 | */ | ||
| 308 | int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, | ||
| 309 | int len, int check) | ||
| 310 | { | ||
| 311 | struct ubi_volume *vol = desc->vol; | ||
| 312 | struct ubi_device *ubi = vol->ubi; | ||
| 313 | int err, vol_id = vol->vol_id; | ||
| 314 | |||
| 315 | dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset); | ||
| 316 | |||
| 317 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || | ||
| 318 | lnum >= vol->used_ebs || offset < 0 || len < 0 || | ||
| 319 | offset + len > vol->usable_leb_size) | ||
| 320 | return -EINVAL; | ||
| 321 | |||
| 322 | if (vol->vol_type == UBI_STATIC_VOLUME && lnum == vol->used_ebs - 1 && | ||
| 323 | offset + len > vol->last_eb_bytes) | ||
| 324 | return -EINVAL; | ||
| 325 | |||
| 326 | if (vol->upd_marker) | ||
| 327 | return -EBADF; | ||
| 328 | if (len == 0) | ||
| 329 | return 0; | ||
| 330 | |||
| 331 | err = ubi_eba_read_leb(ubi, vol_id, lnum, buf, offset, len, check); | ||
| 332 | if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) { | ||
| 333 | ubi_warn("mark volume %d as corrupted", vol_id); | ||
| 334 | vol->corrupted = 1; | ||
| 335 | } | ||
| 336 | |||
| 337 | return err; | ||
| 338 | } | ||
| 339 | EXPORT_SYMBOL_GPL(ubi_leb_read); | ||
| 340 | |||
| 341 | /** | ||
| 342 | * ubi_leb_write - write data. | ||
| 343 | * @desc: volume descriptor | ||
| 344 | * @lnum: logical eraseblock number to write to | ||
| 345 | * @buf: data to write | ||
| 346 | * @offset: offset within the logical eraseblock where to write | ||
| 347 | * @len: how many bytes to write | ||
| 348 | * @dtype: expected data type | ||
| 349 | * | ||
| 350 | * This function writes @len bytes of data from @buf to offset @offset of | ||
| 351 | * logical eraseblock @lnum. The @dtype argument describes expected lifetime of | ||
| 352 | * the data. | ||
| 353 | * | ||
| 354 | * This function takes care of physical eraseblock write failures. If write to | ||
| 355 | * the physical eraseblock write operation fails, the logical eraseblock is | ||
| 356 | * re-mapped to another physical eraseblock, the data is recovered, and the | ||
| 357 | * write finishes. UBI has a pool of reserved physical eraseblocks for this. | ||
| 358 | * | ||
| 359 | * If all the data were successfully written, zero is returned. If an error | ||
| 360 | * occurred and UBI has not been able to recover from it, this function returns | ||
| 361 | * a negative error code. Note, in case of an error, it is possible that | ||
| 362 | * something was still written to the flash media, but that may be some | ||
| 363 | * garbage. | ||
| 364 | * | ||
| 365 | * If the volume is damaged because of an interrupted update this function just | ||
| 366 | * returns immediately with %-EBADF code. | ||
| 367 | */ | ||
| 368 | int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, | ||
| 369 | int offset, int len, int dtype) | ||
| 370 | { | ||
| 371 | struct ubi_volume *vol = desc->vol; | ||
| 372 | struct ubi_device *ubi = vol->ubi; | ||
| 373 | int vol_id = vol->vol_id; | ||
| 374 | |||
| 375 | dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset); | ||
| 376 | |||
| 377 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots) | ||
| 378 | return -EINVAL; | ||
| 379 | |||
| 380 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | ||
| 381 | return -EROFS; | ||
| 382 | |||
| 383 | if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 || | ||
| 384 | offset + len > vol->usable_leb_size || offset % ubi->min_io_size || | ||
| 385 | len % ubi->min_io_size) | ||
| 386 | return -EINVAL; | ||
| 387 | |||
| 388 | if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && | ||
| 389 | dtype != UBI_UNKNOWN) | ||
| 390 | return -EINVAL; | ||
| 391 | |||
| 392 | if (vol->upd_marker) | ||
| 393 | return -EBADF; | ||
| 394 | |||
| 395 | if (len == 0) | ||
| 396 | return 0; | ||
| 397 | |||
| 398 | return ubi_eba_write_leb(ubi, vol_id, lnum, buf, offset, len, dtype); | ||
| 399 | } | ||
| 400 | EXPORT_SYMBOL_GPL(ubi_leb_write); | ||
| 401 | |||
| 402 | /* | ||
| 403 | * ubi_leb_change - change logical eraseblock atomically. | ||
| 404 | * @desc: volume descriptor | ||
| 405 | * @lnum: logical eraseblock number to change | ||
| 406 | * @buf: data to write | ||
| 407 | * @len: how many bytes to write | ||
| 408 | * @dtype: expected data type | ||
| 409 | * | ||
| 410 | * This function changes the contents of a logical eraseblock atomically. @buf | ||
| 411 | * has to contain new logical eraseblock data, and @len - the length of the | ||
| 412 | * data, which has to be aligned. The length may be shorter then the logical | ||
| 413 | * eraseblock size, ant the logical eraseblock may be appended to more times | ||
| 414 | * later on. This function guarantees that in case of an unclean reboot the old | ||
| 415 | * contents is preserved. Returns zero in case of success and a negative error | ||
| 416 | * code in case of failure. | ||
| 417 | */ | ||
| 418 | int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, | ||
| 419 | int len, int dtype) | ||
| 420 | { | ||
| 421 | struct ubi_volume *vol = desc->vol; | ||
| 422 | struct ubi_device *ubi = vol->ubi; | ||
| 423 | int vol_id = vol->vol_id; | ||
| 424 | |||
| 425 | dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum); | ||
| 426 | |||
| 427 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots) | ||
| 428 | return -EINVAL; | ||
| 429 | |||
| 430 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | ||
| 431 | return -EROFS; | ||
| 432 | |||
| 433 | if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || | ||
| 434 | len > vol->usable_leb_size || len % ubi->min_io_size) | ||
| 435 | return -EINVAL; | ||
| 436 | |||
| 437 | if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && | ||
| 438 | dtype != UBI_UNKNOWN) | ||
| 439 | return -EINVAL; | ||
| 440 | |||
| 441 | if (vol->upd_marker) | ||
| 442 | return -EBADF; | ||
| 443 | |||
| 444 | if (len == 0) | ||
| 445 | return 0; | ||
| 446 | |||
| 447 | return ubi_eba_atomic_leb_change(ubi, vol_id, lnum, buf, len, dtype); | ||
| 448 | } | ||
| 449 | EXPORT_SYMBOL_GPL(ubi_leb_change); | ||
| 450 | |||
| 451 | /** | ||
| 452 | * ubi_leb_erase - erase logical eraseblock. | ||
| 453 | * @desc: volume descriptor | ||
| 454 | * @lnum: logical eraseblock number | ||
| 455 | * | ||
| 456 | * This function un-maps logical eraseblock @lnum and synchronously erases the | ||
| 457 | * correspondent physical eraseblock. Returns zero in case of success and a | ||
| 458 | * negative error code in case of failure. | ||
| 459 | * | ||
| 460 | * If the volume is damaged because of an interrupted update this function just | ||
| 461 | * returns immediately with %-EBADF code. | ||
| 462 | */ | ||
| 463 | int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum) | ||
| 464 | { | ||
| 465 | struct ubi_volume *vol = desc->vol; | ||
| 466 | struct ubi_device *ubi = vol->ubi; | ||
| 467 | int err, vol_id = vol->vol_id; | ||
| 468 | |||
| 469 | dbg_msg("erase LEB %d:%d", vol_id, lnum); | ||
| 470 | |||
| 471 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | ||
| 472 | return -EROFS; | ||
| 473 | |||
| 474 | if (lnum < 0 || lnum >= vol->reserved_pebs) | ||
| 475 | return -EINVAL; | ||
| 476 | |||
| 477 | if (vol->upd_marker) | ||
| 478 | return -EBADF; | ||
| 479 | |||
| 480 | err = ubi_eba_unmap_leb(ubi, vol_id, lnum); | ||
| 481 | if (err) | ||
| 482 | return err; | ||
| 483 | |||
| 484 | return ubi_wl_flush(ubi); | ||
| 485 | } | ||
| 486 | EXPORT_SYMBOL_GPL(ubi_leb_erase); | ||
| 487 | |||
| 488 | /** | ||
| 489 | * ubi_leb_unmap - un-map logical eraseblock. | ||
| 490 | * @desc: volume descriptor | ||
| 491 | * @lnum: logical eraseblock number | ||
| 492 | * | ||
| 493 | * This function un-maps logical eraseblock @lnum and schedules the | ||
| 494 | * corresponding physical eraseblock for erasure, so that it will eventually be | ||
| 495 | * physically erased in background. This operation is much faster then the | ||
| 496 | * erase operation. | ||
| 497 | * | ||
| 498 | * Unlike erase, the un-map operation does not guarantee that the logical | ||
| 499 | * eraseblock will contain all 0xFF bytes when UBI is initialized again. For | ||
| 500 | * example, if several logical eraseblocks are un-mapped, and an unclean reboot | ||
| 501 | * happens after this, the logical eraseblocks will not necessarily be | ||
| 502 | * un-mapped again when this MTD device is attached. They may actually be | ||
| 503 | * mapped to the same physical eraseblocks again. So, this function has to be | ||
| 504 | * used with care. | ||
| 505 | * | ||
| 506 | * In other words, when un-mapping a logical eraseblock, UBI does not store | ||
| 507 | * any information about this on the flash media, it just marks the logical | ||
| 508 | * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical | ||
| 509 | * eraseblock is physically erased, it will be mapped again to the same logical | ||
| 510 | * eraseblock when the MTD device is attached again. | ||
| 511 | * | ||
| 512 | * The main and obvious use-case of this function is when the contents of a | ||
| 513 | * logical eraseblock has to be re-written. Then it is much more efficient to | ||
| 514 | * first un-map it, then write new data, rather then first erase it, then write | ||
| 515 | * new data. Note, once new data has been written to the logical eraseblock, | ||
| 516 | * UBI guarantees that the old contents has gone forever. In other words, if an | ||
| 517 | * unclean reboot happens after the logical eraseblock has been un-mapped and | ||
| 518 | * then written to, it will contain the last written data. | ||
| 519 | * | ||
| 520 | * This function returns zero in case of success and a negative error code in | ||
| 521 | * case of failure. If the volume is damaged because of an interrupted update | ||
| 522 | * this function just returns immediately with %-EBADF code. | ||
| 523 | */ | ||
| 524 | int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum) | ||
| 525 | { | ||
| 526 | struct ubi_volume *vol = desc->vol; | ||
| 527 | struct ubi_device *ubi = vol->ubi; | ||
| 528 | int vol_id = vol->vol_id; | ||
| 529 | |||
| 530 | dbg_msg("unmap LEB %d:%d", vol_id, lnum); | ||
| 531 | |||
| 532 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | ||
| 533 | return -EROFS; | ||
| 534 | |||
| 535 | if (lnum < 0 || lnum >= vol->reserved_pebs) | ||
| 536 | return -EINVAL; | ||
| 537 | |||
| 538 | if (vol->upd_marker) | ||
| 539 | return -EBADF; | ||
| 540 | |||
| 541 | return ubi_eba_unmap_leb(ubi, vol_id, lnum); | ||
| 542 | } | ||
| 543 | EXPORT_SYMBOL_GPL(ubi_leb_unmap); | ||
| 544 | |||
| 545 | /** | ||
| 546 | * ubi_is_mapped - check if logical eraseblock is mapped. | ||
| 547 | * @desc: volume descriptor | ||
| 548 | * @lnum: logical eraseblock number | ||
| 549 | * | ||
| 550 | * This function checks if logical eraseblock @lnum is mapped to a physical | ||
| 551 | * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily | ||
| 552 | * mean it will still be un-mapped after the UBI device is re-attached. The | ||
| 553 | * logical eraseblock may become mapped to the physical eraseblock it was last | ||
| 554 | * mapped to. | ||
| 555 | * | ||
| 556 | * This function returns %1 if the LEB is mapped, %0 if not, and a negative | ||
| 557 | * error code in case of failure. If the volume is damaged because of an | ||
| 558 | * interrupted update this function just returns immediately with %-EBADF error | ||
| 559 | * code. | ||
| 560 | */ | ||
| 561 | int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum) | ||
| 562 | { | ||
| 563 | struct ubi_volume *vol = desc->vol; | ||
| 564 | |||
| 565 | dbg_msg("test LEB %d:%d", vol->vol_id, lnum); | ||
| 566 | |||
| 567 | if (lnum < 0 || lnum >= vol->reserved_pebs) | ||
| 568 | return -EINVAL; | ||
| 569 | |||
| 570 | if (vol->upd_marker) | ||
| 571 | return -EBADF; | ||
| 572 | |||
| 573 | return vol->eba_tbl[lnum] >= 0; | ||
| 574 | } | ||
| 575 | EXPORT_SYMBOL_GPL(ubi_is_mapped); | ||
