2 * Copyright (c) International Business Machines Corp., 2006
4 * SPDX-License-Identifier: GPL-2.0+
6 * Author: Artem Bityutskiy (Битюцкий Артём)
9 /* This file mostly implements UBI kernel API functions */
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <asm/div64.h>
17 #include <ubi_uboot.h>
21 * ubi_get_device_info - get information about UBI device.
22 * @ubi_num: UBI device number
23 * @di: the information is stored here
25 * This function returns %0 in case of success, %-EINVAL if the UBI device
26 * number is invalid, and %-ENODEV if there is no such UBI device.
28 int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
30 struct ubi_device *ubi;
32 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
35 ubi = ubi_get_device(ubi_num);
39 di->ubi_num = ubi->ubi_num;
40 di->leb_size = ubi->leb_size;
41 di->min_io_size = ubi->min_io_size;
42 di->ro_mode = ubi->ro_mode;
43 di->cdev = ubi->cdev.dev;
48 EXPORT_SYMBOL_GPL(ubi_get_device_info);
51 * ubi_get_volume_info - get information about UBI volume.
52 * @desc: volume descriptor
53 * @vi: the information is stored here
55 void ubi_get_volume_info(struct ubi_volume_desc *desc,
56 struct ubi_volume_info *vi)
58 const struct ubi_volume *vol = desc->vol;
59 const struct ubi_device *ubi = vol->ubi;
61 vi->vol_id = vol->vol_id;
62 vi->ubi_num = ubi->ubi_num;
63 vi->size = vol->reserved_pebs;
64 vi->used_bytes = vol->used_bytes;
65 vi->vol_type = vol->vol_type;
66 vi->corrupted = vol->corrupted;
67 vi->upd_marker = vol->upd_marker;
68 vi->alignment = vol->alignment;
69 vi->usable_leb_size = vol->usable_leb_size;
70 vi->name_len = vol->name_len;
72 vi->cdev = vol->cdev.dev;
74 EXPORT_SYMBOL_GPL(ubi_get_volume_info);
77 * ubi_open_volume - open UBI volume.
78 * @ubi_num: UBI device number
82 * The @mode parameter specifies if the volume should be opened in read-only
83 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
84 * nobody else will be able to open this volume. UBI allows to have many volume
85 * readers and one writer at a time.
87 * If a static volume is being opened for the first time since boot, it will be
88 * checked by this function, which means it will be fully read and the CRC
89 * checksum of each logical eraseblock will be checked.
91 * This function returns volume descriptor in case of success and a negative
92 * error code in case of failure.
94 struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
97 struct ubi_volume_desc *desc;
98 struct ubi_device *ubi;
99 struct ubi_volume *vol;
101 dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
103 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
104 return ERR_PTR(-EINVAL);
106 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
107 mode != UBI_EXCLUSIVE)
108 return ERR_PTR(-EINVAL);
111 * First of all, we have to get the UBI device to prevent its removal.
113 ubi = ubi_get_device(ubi_num);
115 return ERR_PTR(-ENODEV);
117 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
122 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
129 if (!try_module_get(THIS_MODULE))
132 spin_lock(&ubi->volumes_lock);
133 vol = ubi->volumes[vol_id];
146 if (vol->exclusive || vol->writers > 0)
152 if (vol->exclusive || vol->writers || vol->readers)
157 get_device(&vol->dev);
159 spin_unlock(&ubi->volumes_lock);
164 mutex_lock(&ubi->ckvol_mutex);
166 /* This is the first open - check the volume */
167 err = ubi_check_volume(ubi, vol_id);
169 mutex_unlock(&ubi->ckvol_mutex);
170 ubi_close_volume(desc);
174 ubi_warn("volume %d on UBI device %d is corrupted",
175 vol_id, ubi->ubi_num);
180 mutex_unlock(&ubi->ckvol_mutex);
185 spin_unlock(&ubi->volumes_lock);
186 module_put(THIS_MODULE);
193 EXPORT_SYMBOL_GPL(ubi_open_volume);
196 * ubi_open_volume_nm - open UBI volume by name.
197 * @ubi_num: UBI device number
201 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
203 struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
206 int i, vol_id = -1, len;
207 struct ubi_device *ubi;
208 struct ubi_volume_desc *ret;
210 dbg_msg("open volume %s, mode %d", name, mode);
213 return ERR_PTR(-EINVAL);
215 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
216 if (len > UBI_VOL_NAME_MAX)
217 return ERR_PTR(-EINVAL);
219 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
220 return ERR_PTR(-EINVAL);
222 ubi = ubi_get_device(ubi_num);
224 return ERR_PTR(-ENODEV);
226 spin_lock(&ubi->volumes_lock);
227 /* Walk all volumes of this UBI device */
228 for (i = 0; i < ubi->vtbl_slots; i++) {
229 struct ubi_volume *vol = ubi->volumes[i];
231 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
236 spin_unlock(&ubi->volumes_lock);
239 ret = ubi_open_volume(ubi_num, vol_id, mode);
241 ret = ERR_PTR(-ENODEV);
244 * We should put the UBI device even in case of success, because
245 * 'ubi_open_volume()' took a reference as well.
250 EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
253 * ubi_close_volume - close UBI volume.
254 * @desc: volume descriptor
256 void ubi_close_volume(struct ubi_volume_desc *desc)
258 struct ubi_volume *vol = desc->vol;
259 struct ubi_device *ubi = vol->ubi;
261 dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode);
263 spin_lock(&ubi->volumes_lock);
264 switch (desc->mode) {
275 spin_unlock(&ubi->volumes_lock);
278 put_device(&vol->dev);
280 module_put(THIS_MODULE);
282 EXPORT_SYMBOL_GPL(ubi_close_volume);
285 * ubi_leb_read - read data.
286 * @desc: volume descriptor
287 * @lnum: logical eraseblock number to read from
288 * @buf: buffer where to store the read data
289 * @offset: offset within the logical eraseblock to read from
290 * @len: how many bytes to read
291 * @check: whether UBI has to check the read data's CRC or not.
293 * This function reads data from offset @offset of logical eraseblock @lnum and
294 * stores the data at @buf. When reading from static volumes, @check specifies
295 * whether the data has to be checked or not. If yes, the whole logical
296 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
297 * checksum is per-eraseblock). So checking may substantially slow down the
298 * read speed. The @check argument is ignored for dynamic volumes.
300 * In case of success, this function returns zero. In case of failure, this
301 * function returns a negative error code.
303 * %-EBADMSG error code is returned:
304 * o for both static and dynamic volumes if MTD driver has detected a data
305 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
306 * o for static volumes in case of data CRC mismatch.
308 * If the volume is damaged because of an interrupted update this function just
309 * returns immediately with %-EBADF error code.
311 int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
314 struct ubi_volume *vol = desc->vol;
315 struct ubi_device *ubi = vol->ubi;
316 int err, vol_id = vol->vol_id;
318 dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
320 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
321 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
322 offset + len > vol->usable_leb_size)
325 if (vol->vol_type == UBI_STATIC_VOLUME) {
326 if (vol->used_ebs == 0)
327 /* Empty static UBI volume */
329 if (lnum == vol->used_ebs - 1 &&
330 offset + len > vol->last_eb_bytes)
339 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
340 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
341 ubi_warn("mark volume %d as corrupted", vol_id);
347 EXPORT_SYMBOL_GPL(ubi_leb_read);
350 * ubi_leb_write - write data.
351 * @desc: volume descriptor
352 * @lnum: logical eraseblock number to write to
353 * @buf: data to write
354 * @offset: offset within the logical eraseblock where to write
355 * @len: how many bytes to write
356 * @dtype: expected data type
358 * This function writes @len bytes of data from @buf to offset @offset of
359 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
362 * This function takes care of physical eraseblock write failures. If write to
363 * the physical eraseblock write operation fails, the logical eraseblock is
364 * re-mapped to another physical eraseblock, the data is recovered, and the
365 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
367 * If all the data were successfully written, zero is returned. If an error
368 * occurred and UBI has not been able to recover from it, this function returns
369 * a negative error code. Note, in case of an error, it is possible that
370 * something was still written to the flash media, but that may be some
373 * If the volume is damaged because of an interrupted update this function just
374 * returns immediately with %-EBADF code.
376 int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
377 int offset, int len, int dtype)
379 struct ubi_volume *vol = desc->vol;
380 struct ubi_device *ubi = vol->ubi;
381 int vol_id = vol->vol_id;
383 dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
385 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
388 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
391 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
392 offset + len > vol->usable_leb_size ||
393 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
396 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
397 dtype != UBI_UNKNOWN)
406 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
408 EXPORT_SYMBOL_GPL(ubi_leb_write);
411 * ubi_leb_change - change logical eraseblock atomically.
412 * @desc: volume descriptor
413 * @lnum: logical eraseblock number to change
414 * @buf: data to write
415 * @len: how many bytes to write
416 * @dtype: expected data type
418 * This function changes the contents of a logical eraseblock atomically. @buf
419 * has to contain new logical eraseblock data, and @len - the length of the
420 * data, which has to be aligned. The length may be shorter then the logical
421 * eraseblock size, ant the logical eraseblock may be appended to more times
422 * later on. This function guarantees that in case of an unclean reboot the old
423 * contents is preserved. Returns zero in case of success and a negative error
424 * code in case of failure.
426 int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
429 struct ubi_volume *vol = desc->vol;
430 struct ubi_device *ubi = vol->ubi;
431 int vol_id = vol->vol_id;
433 dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
435 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
438 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
441 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
442 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
445 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
446 dtype != UBI_UNKNOWN)
455 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
457 EXPORT_SYMBOL_GPL(ubi_leb_change);
460 * ubi_leb_erase - erase logical eraseblock.
461 * @desc: volume descriptor
462 * @lnum: logical eraseblock number
464 * This function un-maps logical eraseblock @lnum and synchronously erases the
465 * correspondent physical eraseblock. Returns zero in case of success and a
466 * negative error code in case of failure.
468 * If the volume is damaged because of an interrupted update this function just
469 * returns immediately with %-EBADF code.
471 int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
473 struct ubi_volume *vol = desc->vol;
474 struct ubi_device *ubi = vol->ubi;
477 dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
479 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
482 if (lnum < 0 || lnum >= vol->reserved_pebs)
488 err = ubi_eba_unmap_leb(ubi, vol, lnum);
492 return ubi_wl_flush(ubi);
494 EXPORT_SYMBOL_GPL(ubi_leb_erase);
497 * ubi_leb_unmap - un-map logical eraseblock.
498 * @desc: volume descriptor
499 * @lnum: logical eraseblock number
501 * This function un-maps logical eraseblock @lnum and schedules the
502 * corresponding physical eraseblock for erasure, so that it will eventually be
503 * physically erased in background. This operation is much faster then the
506 * Unlike erase, the un-map operation does not guarantee that the logical
507 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
508 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
509 * happens after this, the logical eraseblocks will not necessarily be
510 * un-mapped again when this MTD device is attached. They may actually be
511 * mapped to the same physical eraseblocks again. So, this function has to be
514 * In other words, when un-mapping a logical eraseblock, UBI does not store
515 * any information about this on the flash media, it just marks the logical
516 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
517 * eraseblock is physically erased, it will be mapped again to the same logical
518 * eraseblock when the MTD device is attached again.
520 * The main and obvious use-case of this function is when the contents of a
521 * logical eraseblock has to be re-written. Then it is much more efficient to
522 * first un-map it, then write new data, rather then first erase it, then write
523 * new data. Note, once new data has been written to the logical eraseblock,
524 * UBI guarantees that the old contents has gone forever. In other words, if an
525 * unclean reboot happens after the logical eraseblock has been un-mapped and
526 * then written to, it will contain the last written data.
528 * This function returns zero in case of success and a negative error code in
529 * case of failure. If the volume is damaged because of an interrupted update
530 * this function just returns immediately with %-EBADF code.
532 int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
534 struct ubi_volume *vol = desc->vol;
535 struct ubi_device *ubi = vol->ubi;
537 dbg_msg("unmap LEB %d:%d", vol->vol_id, lnum);
539 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
542 if (lnum < 0 || lnum >= vol->reserved_pebs)
548 return ubi_eba_unmap_leb(ubi, vol, lnum);
550 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
553 * ubi_leb_map - map logical erasblock to a physical eraseblock.
554 * @desc: volume descriptor
555 * @lnum: logical eraseblock number
556 * @dtype: expected data type
558 * This function maps an un-mapped logical eraseblock @lnum to a physical
559 * eraseblock. This means, that after a successfull invocation of this
560 * function the logical eraseblock @lnum will be empty (contain only %0xFF
561 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
564 * This function returns zero in case of success, %-EBADF if the volume is
565 * damaged because of an interrupted update, %-EBADMSG if the logical
566 * eraseblock is already mapped, and other negative error codes in case of
569 int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
571 struct ubi_volume *vol = desc->vol;
572 struct ubi_device *ubi = vol->ubi;
574 dbg_msg("unmap LEB %d:%d", vol->vol_id, lnum);
576 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
579 if (lnum < 0 || lnum >= vol->reserved_pebs)
582 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
583 dtype != UBI_UNKNOWN)
589 if (vol->eba_tbl[lnum] >= 0)
592 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
594 EXPORT_SYMBOL_GPL(ubi_leb_map);
597 * ubi_is_mapped - check if logical eraseblock is mapped.
598 * @desc: volume descriptor
599 * @lnum: logical eraseblock number
601 * This function checks if logical eraseblock @lnum is mapped to a physical
602 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
603 * mean it will still be un-mapped after the UBI device is re-attached. The
604 * logical eraseblock may become mapped to the physical eraseblock it was last
607 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
608 * error code in case of failure. If the volume is damaged because of an
609 * interrupted update this function just returns immediately with %-EBADF error
612 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
614 struct ubi_volume *vol = desc->vol;
616 dbg_msg("test LEB %d:%d", vol->vol_id, lnum);
618 if (lnum < 0 || lnum >= vol->reserved_pebs)
624 return vol->eba_tbl[lnum] >= 0;
626 EXPORT_SYMBOL_GPL(ubi_is_mapped);