2 * Copyright (c) International Business Machines Corp., 2006
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.
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.
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
18 * Author: Artem Bityutskiy (Битюцкий Артём)
21 /* This file mostly implements UBI kernel API functions */
24 #include <linux/module.h>
25 #include <linux/err.h>
26 #include <asm/div64.h>
29 #include <ubi_uboot.h>
33 * ubi_get_device_info - get information about UBI device.
34 * @ubi_num: UBI device number
35 * @di: the information is stored here
37 * This function returns %0 in case of success, %-EINVAL if the UBI device
38 * number is invalid, and %-ENODEV if there is no such UBI device.
40 int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
42 struct ubi_device *ubi;
44 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
47 ubi = ubi_get_device(ubi_num);
51 di->ubi_num = ubi->ubi_num;
52 di->leb_size = ubi->leb_size;
53 di->min_io_size = ubi->min_io_size;
54 di->ro_mode = ubi->ro_mode;
55 di->cdev = ubi->cdev.dev;
60 EXPORT_SYMBOL_GPL(ubi_get_device_info);
63 * ubi_get_volume_info - get information about UBI volume.
64 * @desc: volume descriptor
65 * @vi: the information is stored here
67 void ubi_get_volume_info(struct ubi_volume_desc *desc,
68 struct ubi_volume_info *vi)
70 const struct ubi_volume *vol = desc->vol;
71 const struct ubi_device *ubi = vol->ubi;
73 vi->vol_id = vol->vol_id;
74 vi->ubi_num = ubi->ubi_num;
75 vi->size = vol->reserved_pebs;
76 vi->used_bytes = vol->used_bytes;
77 vi->vol_type = vol->vol_type;
78 vi->corrupted = vol->corrupted;
79 vi->upd_marker = vol->upd_marker;
80 vi->alignment = vol->alignment;
81 vi->usable_leb_size = vol->usable_leb_size;
82 vi->name_len = vol->name_len;
84 vi->cdev = vol->cdev.dev;
86 EXPORT_SYMBOL_GPL(ubi_get_volume_info);
89 * ubi_open_volume - open UBI volume.
90 * @ubi_num: UBI device number
94 * The @mode parameter specifies if the volume should be opened in read-only
95 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
96 * nobody else will be able to open this volume. UBI allows to have many volume
97 * readers and one writer at a time.
99 * If a static volume is being opened for the first time since boot, it will be
100 * checked by this function, which means it will be fully read and the CRC
101 * checksum of each logical eraseblock will be checked.
103 * This function returns volume descriptor in case of success and a negative
104 * error code in case of failure.
106 struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
109 struct ubi_volume_desc *desc;
110 struct ubi_device *ubi;
111 struct ubi_volume *vol;
113 dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
115 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
116 return ERR_PTR(-EINVAL);
118 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
119 mode != UBI_EXCLUSIVE)
120 return ERR_PTR(-EINVAL);
123 * First of all, we have to get the UBI device to prevent its removal.
125 ubi = ubi_get_device(ubi_num);
127 return ERR_PTR(-ENODEV);
129 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
134 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
141 if (!try_module_get(THIS_MODULE))
144 spin_lock(&ubi->volumes_lock);
145 vol = ubi->volumes[vol_id];
158 if (vol->exclusive || vol->writers > 0)
164 if (vol->exclusive || vol->writers || vol->readers)
169 get_device(&vol->dev);
171 spin_unlock(&ubi->volumes_lock);
176 mutex_lock(&ubi->ckvol_mutex);
178 /* This is the first open - check the volume */
179 err = ubi_check_volume(ubi, vol_id);
181 mutex_unlock(&ubi->ckvol_mutex);
182 ubi_close_volume(desc);
186 ubi_warn("volume %d on UBI device %d is corrupted",
187 vol_id, ubi->ubi_num);
192 mutex_unlock(&ubi->ckvol_mutex);
197 spin_unlock(&ubi->volumes_lock);
198 module_put(THIS_MODULE);
205 EXPORT_SYMBOL_GPL(ubi_open_volume);
208 * ubi_open_volume_nm - open UBI volume by name.
209 * @ubi_num: UBI device number
213 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
215 struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
218 int i, vol_id = -1, len;
219 struct ubi_device *ubi;
220 struct ubi_volume_desc *ret;
222 dbg_msg("open volume %s, mode %d", name, mode);
225 return ERR_PTR(-EINVAL);
227 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
228 if (len > UBI_VOL_NAME_MAX)
229 return ERR_PTR(-EINVAL);
231 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
232 return ERR_PTR(-EINVAL);
234 ubi = ubi_get_device(ubi_num);
236 return ERR_PTR(-ENODEV);
238 spin_lock(&ubi->volumes_lock);
239 /* Walk all volumes of this UBI device */
240 for (i = 0; i < ubi->vtbl_slots; i++) {
241 struct ubi_volume *vol = ubi->volumes[i];
243 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
248 spin_unlock(&ubi->volumes_lock);
251 ret = ubi_open_volume(ubi_num, vol_id, mode);
253 ret = ERR_PTR(-ENODEV);
256 * We should put the UBI device even in case of success, because
257 * 'ubi_open_volume()' took a reference as well.
262 EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
265 * ubi_close_volume - close UBI volume.
266 * @desc: volume descriptor
268 void ubi_close_volume(struct ubi_volume_desc *desc)
270 struct ubi_volume *vol = desc->vol;
271 struct ubi_device *ubi = vol->ubi;
273 dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode);
275 spin_lock(&ubi->volumes_lock);
276 switch (desc->mode) {
287 spin_unlock(&ubi->volumes_lock);
290 put_device(&vol->dev);
292 module_put(THIS_MODULE);
294 EXPORT_SYMBOL_GPL(ubi_close_volume);
297 * ubi_leb_read - read data.
298 * @desc: volume descriptor
299 * @lnum: logical eraseblock number to read from
300 * @buf: buffer where to store the read data
301 * @offset: offset within the logical eraseblock to read from
302 * @len: how many bytes to read
303 * @check: whether UBI has to check the read data's CRC or not.
305 * This function reads data from offset @offset of logical eraseblock @lnum and
306 * stores the data at @buf. When reading from static volumes, @check specifies
307 * whether the data has to be checked or not. If yes, the whole logical
308 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
309 * checksum is per-eraseblock). So checking may substantially slow down the
310 * read speed. The @check argument is ignored for dynamic volumes.
312 * In case of success, this function returns zero. In case of failure, this
313 * function returns a negative error code.
315 * %-EBADMSG error code is returned:
316 * o for both static and dynamic volumes if MTD driver has detected a data
317 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
318 * o for static volumes in case of data CRC mismatch.
320 * If the volume is damaged because of an interrupted update this function just
321 * returns immediately with %-EBADF error code.
323 int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
326 struct ubi_volume *vol = desc->vol;
327 struct ubi_device *ubi = vol->ubi;
328 int err, vol_id = vol->vol_id;
330 dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
332 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
333 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
334 offset + len > vol->usable_leb_size)
337 if (vol->vol_type == UBI_STATIC_VOLUME) {
338 if (vol->used_ebs == 0)
339 /* Empty static UBI volume */
341 if (lnum == vol->used_ebs - 1 &&
342 offset + len > vol->last_eb_bytes)
351 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
352 if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
353 ubi_warn("mark volume %d as corrupted", vol_id);
359 EXPORT_SYMBOL_GPL(ubi_leb_read);
362 * ubi_leb_write - write data.
363 * @desc: volume descriptor
364 * @lnum: logical eraseblock number to write to
365 * @buf: data to write
366 * @offset: offset within the logical eraseblock where to write
367 * @len: how many bytes to write
368 * @dtype: expected data type
370 * This function writes @len bytes of data from @buf to offset @offset of
371 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
374 * This function takes care of physical eraseblock write failures. If write to
375 * the physical eraseblock write operation fails, the logical eraseblock is
376 * re-mapped to another physical eraseblock, the data is recovered, and the
377 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
379 * If all the data were successfully written, zero is returned. If an error
380 * occurred and UBI has not been able to recover from it, this function returns
381 * a negative error code. Note, in case of an error, it is possible that
382 * something was still written to the flash media, but that may be some
385 * If the volume is damaged because of an interrupted update this function just
386 * returns immediately with %-EBADF code.
388 int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
389 int offset, int len, int dtype)
391 struct ubi_volume *vol = desc->vol;
392 struct ubi_device *ubi = vol->ubi;
393 int vol_id = vol->vol_id;
395 dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
397 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
400 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
403 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
404 offset + len > vol->usable_leb_size ||
405 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
408 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
409 dtype != UBI_UNKNOWN)
418 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
420 EXPORT_SYMBOL_GPL(ubi_leb_write);
423 * ubi_leb_change - change logical eraseblock atomically.
424 * @desc: volume descriptor
425 * @lnum: logical eraseblock number to change
426 * @buf: data to write
427 * @len: how many bytes to write
428 * @dtype: expected data type
430 * This function changes the contents of a logical eraseblock atomically. @buf
431 * has to contain new logical eraseblock data, and @len - the length of the
432 * data, which has to be aligned. The length may be shorter then the logical
433 * eraseblock size, ant the logical eraseblock may be appended to more times
434 * later on. This function guarantees that in case of an unclean reboot the old
435 * contents is preserved. Returns zero in case of success and a negative error
436 * code in case of failure.
438 int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
441 struct ubi_volume *vol = desc->vol;
442 struct ubi_device *ubi = vol->ubi;
443 int vol_id = vol->vol_id;
445 dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
447 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
450 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
453 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
454 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
457 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
458 dtype != UBI_UNKNOWN)
467 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
469 EXPORT_SYMBOL_GPL(ubi_leb_change);
472 * ubi_leb_erase - erase logical eraseblock.
473 * @desc: volume descriptor
474 * @lnum: logical eraseblock number
476 * This function un-maps logical eraseblock @lnum and synchronously erases the
477 * correspondent physical eraseblock. Returns zero in case of success and a
478 * negative error code in case of failure.
480 * If the volume is damaged because of an interrupted update this function just
481 * returns immediately with %-EBADF code.
483 int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
485 struct ubi_volume *vol = desc->vol;
486 struct ubi_device *ubi = vol->ubi;
489 dbg_msg("erase LEB %d:%d", vol->vol_id, lnum);
491 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
494 if (lnum < 0 || lnum >= vol->reserved_pebs)
500 err = ubi_eba_unmap_leb(ubi, vol, lnum);
504 return ubi_wl_flush(ubi);
506 EXPORT_SYMBOL_GPL(ubi_leb_erase);
509 * ubi_leb_unmap - un-map logical eraseblock.
510 * @desc: volume descriptor
511 * @lnum: logical eraseblock number
513 * This function un-maps logical eraseblock @lnum and schedules the
514 * corresponding physical eraseblock for erasure, so that it will eventually be
515 * physically erased in background. This operation is much faster then the
518 * Unlike erase, the un-map operation does not guarantee that the logical
519 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
520 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
521 * happens after this, the logical eraseblocks will not necessarily be
522 * un-mapped again when this MTD device is attached. They may actually be
523 * mapped to the same physical eraseblocks again. So, this function has to be
526 * In other words, when un-mapping a logical eraseblock, UBI does not store
527 * any information about this on the flash media, it just marks the logical
528 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
529 * eraseblock is physically erased, it will be mapped again to the same logical
530 * eraseblock when the MTD device is attached again.
532 * The main and obvious use-case of this function is when the contents of a
533 * logical eraseblock has to be re-written. Then it is much more efficient to
534 * first un-map it, then write new data, rather then first erase it, then write
535 * new data. Note, once new data has been written to the logical eraseblock,
536 * UBI guarantees that the old contents has gone forever. In other words, if an
537 * unclean reboot happens after the logical eraseblock has been un-mapped and
538 * then written to, it will contain the last written data.
540 * This function returns zero in case of success and a negative error code in
541 * case of failure. If the volume is damaged because of an interrupted update
542 * this function just returns immediately with %-EBADF code.
544 int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
546 struct ubi_volume *vol = desc->vol;
547 struct ubi_device *ubi = vol->ubi;
549 dbg_msg("unmap LEB %d:%d", vol->vol_id, lnum);
551 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
554 if (lnum < 0 || lnum >= vol->reserved_pebs)
560 return ubi_eba_unmap_leb(ubi, vol, lnum);
562 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
565 * ubi_leb_map - map logical erasblock to a physical eraseblock.
566 * @desc: volume descriptor
567 * @lnum: logical eraseblock number
568 * @dtype: expected data type
570 * This function maps an un-mapped logical eraseblock @lnum to a physical
571 * eraseblock. This means, that after a successfull invocation of this
572 * function the logical eraseblock @lnum will be empty (contain only %0xFF
573 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
576 * This function returns zero in case of success, %-EBADF if the volume is
577 * damaged because of an interrupted update, %-EBADMSG if the logical
578 * eraseblock is already mapped, and other negative error codes in case of
581 int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
583 struct ubi_volume *vol = desc->vol;
584 struct ubi_device *ubi = vol->ubi;
586 dbg_msg("unmap LEB %d:%d", vol->vol_id, lnum);
588 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
591 if (lnum < 0 || lnum >= vol->reserved_pebs)
594 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
595 dtype != UBI_UNKNOWN)
601 if (vol->eba_tbl[lnum] >= 0)
604 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
606 EXPORT_SYMBOL_GPL(ubi_leb_map);
609 * ubi_is_mapped - check if logical eraseblock is mapped.
610 * @desc: volume descriptor
611 * @lnum: logical eraseblock number
613 * This function checks if logical eraseblock @lnum is mapped to a physical
614 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
615 * mean it will still be un-mapped after the UBI device is re-attached. The
616 * logical eraseblock may become mapped to the physical eraseblock it was last
619 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
620 * error code in case of failure. If the volume is damaged because of an
621 * interrupted update this function just returns immediately with %-EBADF error
624 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
626 struct ubi_volume *vol = desc->vol;
628 dbg_msg("test LEB %d:%d", vol->vol_id, lnum);
630 if (lnum < 0 || lnum >= vol->reserved_pebs)
636 return vol->eba_tbl[lnum] >= 0;
638 EXPORT_SYMBOL_GPL(ubi_is_mapped);