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 */
23 #include <linux/module.h>
24 #include <linux/err.h>
25 #include <asm/div64.h>
29 * ubi_get_device_info - get information about UBI device.
30 * @ubi_num: UBI device number
31 * @di: the information is stored here
33 * This function returns %0 in case of success and a %-ENODEV if there is no
36 int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
38 const struct ubi_device *ubi;
40 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES ||
41 !ubi_devices[ubi_num])
44 ubi = ubi_devices[ubi_num];
45 di->ubi_num = ubi->ubi_num;
46 di->leb_size = ubi->leb_size;
47 di->min_io_size = ubi->min_io_size;
48 di->ro_mode = ubi->ro_mode;
49 di->cdev = MKDEV(ubi->major, 0);
52 EXPORT_SYMBOL_GPL(ubi_get_device_info);
55 * ubi_get_volume_info - get information about UBI volume.
56 * @desc: volume descriptor
57 * @vi: the information is stored here
59 void ubi_get_volume_info(struct ubi_volume_desc *desc,
60 struct ubi_volume_info *vi)
62 const struct ubi_volume *vol = desc->vol;
63 const struct ubi_device *ubi = vol->ubi;
65 vi->vol_id = vol->vol_id;
66 vi->ubi_num = ubi->ubi_num;
67 vi->size = vol->reserved_pebs;
68 vi->used_bytes = vol->used_bytes;
69 vi->vol_type = vol->vol_type;
70 vi->corrupted = vol->corrupted;
71 vi->upd_marker = vol->upd_marker;
72 vi->alignment = vol->alignment;
73 vi->usable_leb_size = vol->usable_leb_size;
74 vi->name_len = vol->name_len;
76 vi->cdev = MKDEV(ubi->major, vi->vol_id + 1);
78 EXPORT_SYMBOL_GPL(ubi_get_volume_info);
81 * ubi_open_volume - open UBI volume.
82 * @ubi_num: UBI device number
86 * The @mode parameter specifies if the volume should be opened in read-only
87 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
88 * nobody else will be able to open this volume. UBI allows to have many volume
89 * readers and one writer at a time.
91 * If a static volume is being opened for the first time since boot, it will be
92 * checked by this function, which means it will be fully read and the CRC
93 * checksum of each logical eraseblock will be checked.
95 * This function returns volume descriptor in case of success and a negative
96 * error code in case of failure.
98 struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
101 struct ubi_volume_desc *desc;
102 struct ubi_device *ubi = ubi_devices[ubi_num];
103 struct ubi_volume *vol;
105 dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
108 if (!try_module_get(THIS_MODULE))
111 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi)
115 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
117 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
118 mode != UBI_EXCLUSIVE)
121 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
127 spin_lock(&ubi->volumes_lock);
128 vol = ubi->volumes[vol_id];
143 if (vol->exclusive || vol->writers > 0)
149 if (vol->exclusive || vol->writers || vol->readers)
154 spin_unlock(&ubi->volumes_lock);
160 * To prevent simultaneous checks of the same volume we use @vtbl_mutex,
161 * although it is not the purpose it was introduced for.
163 mutex_lock(&ubi->vtbl_mutex);
165 /* This is the first open - check the volume */
166 err = ubi_check_volume(ubi, vol_id);
168 mutex_unlock(&ubi->vtbl_mutex);
169 ubi_close_volume(desc);
173 ubi_warn("volume %d on UBI device %d is corrupted",
174 vol_id, ubi->ubi_num);
179 mutex_unlock(&ubi->vtbl_mutex);
183 spin_unlock(&ubi->volumes_lock);
186 module_put(THIS_MODULE);
189 EXPORT_SYMBOL_GPL(ubi_open_volume);
192 * ubi_open_volume_nm - open UBI volume by name.
193 * @ubi_num: UBI device number
197 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
199 struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
202 int i, vol_id = -1, len;
203 struct ubi_volume_desc *ret;
204 struct ubi_device *ubi;
206 dbg_msg("open volume %s, mode %d", name, mode);
209 return ERR_PTR(-EINVAL);
211 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
212 if (len > UBI_VOL_NAME_MAX)
213 return ERR_PTR(-EINVAL);
215 ret = ERR_PTR(-ENODEV);
216 if (!try_module_get(THIS_MODULE))
219 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi_devices[ubi_num])
222 ubi = ubi_devices[ubi_num];
224 spin_lock(&ubi->volumes_lock);
225 /* Walk all volumes of this UBI device */
226 for (i = 0; i < ubi->vtbl_slots; i++) {
227 struct ubi_volume *vol = ubi->volumes[i];
229 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
234 spin_unlock(&ubi->volumes_lock);
239 ret = ubi_open_volume(ubi_num, vol_id, mode);
242 module_put(THIS_MODULE);
245 EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
248 * ubi_close_volume - close UBI volume.
249 * @desc: volume descriptor
251 void ubi_close_volume(struct ubi_volume_desc *desc)
253 struct ubi_volume *vol = desc->vol;
255 dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode);
257 spin_lock(&vol->ubi->volumes_lock);
258 switch (desc->mode) {
268 spin_unlock(&vol->ubi->volumes_lock);
271 module_put(THIS_MODULE);
273 EXPORT_SYMBOL_GPL(ubi_close_volume);
276 * ubi_leb_read - read data.
277 * @desc: volume descriptor
278 * @lnum: logical eraseblock number to read from
279 * @buf: buffer where to store the read data
280 * @offset: offset within the logical eraseblock to read from
281 * @len: how many bytes to read
282 * @check: whether UBI has to check the read data's CRC or not.
284 * This function reads data from offset @offset of logical eraseblock @lnum and
285 * stores the data at @buf. When reading from static volumes, @check specifies
286 * whether the data has to be checked or not. If yes, the whole logical
287 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
288 * checksum is per-eraseblock). So checking may substantially slow down the
289 * read speed. The @check argument is ignored for dynamic volumes.
291 * In case of success, this function returns zero. In case of failure, this
292 * function returns a negative error code.
294 * %-EBADMSG error code is returned:
295 * o for both static and dynamic volumes if MTD driver has detected a data
296 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
297 * o for static volumes in case of data CRC mismatch.
299 * If the volume is damaged because of an interrupted update this function just
300 * returns immediately with %-EBADF error code.
302 int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
305 struct ubi_volume *vol = desc->vol;
306 struct ubi_device *ubi = vol->ubi;
307 int err, vol_id = vol->vol_id;
309 dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
311 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
312 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
313 offset + len > vol->usable_leb_size)
316 if (vol->vol_type == UBI_STATIC_VOLUME) {
317 if (vol->used_ebs == 0)
318 /* Empty static UBI volume */
320 if (lnum == vol->used_ebs - 1 &&
321 offset + len > vol->last_eb_bytes)
330 err = ubi_eba_read_leb(ubi, vol_id, lnum, buf, offset, len, check);
331 if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
332 ubi_warn("mark volume %d as corrupted", vol_id);
338 EXPORT_SYMBOL_GPL(ubi_leb_read);
341 * ubi_leb_write - write data.
342 * @desc: volume descriptor
343 * @lnum: logical eraseblock number to write to
344 * @buf: data to write
345 * @offset: offset within the logical eraseblock where to write
346 * @len: how many bytes to write
347 * @dtype: expected data type
349 * This function writes @len bytes of data from @buf to offset @offset of
350 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
353 * This function takes care of physical eraseblock write failures. If write to
354 * the physical eraseblock write operation fails, the logical eraseblock is
355 * re-mapped to another physical eraseblock, the data is recovered, and the
356 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
358 * If all the data were successfully written, zero is returned. If an error
359 * occurred and UBI has not been able to recover from it, this function returns
360 * a negative error code. Note, in case of an error, it is possible that
361 * something was still written to the flash media, but that may be some
364 * If the volume is damaged because of an interrupted update this function just
365 * returns immediately with %-EBADF code.
367 int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
368 int offset, int len, int dtype)
370 struct ubi_volume *vol = desc->vol;
371 struct ubi_device *ubi = vol->ubi;
372 int vol_id = vol->vol_id;
374 dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
376 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
379 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
382 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
383 offset + len > vol->usable_leb_size || offset % ubi->min_io_size ||
384 len % ubi->min_io_size)
387 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
388 dtype != UBI_UNKNOWN)
397 return ubi_eba_write_leb(ubi, vol_id, lnum, buf, offset, len, dtype);
399 EXPORT_SYMBOL_GPL(ubi_leb_write);
402 * ubi_leb_change - change logical eraseblock atomically.
403 * @desc: volume descriptor
404 * @lnum: logical eraseblock number to change
405 * @buf: data to write
406 * @len: how many bytes to write
407 * @dtype: expected data type
409 * This function changes the contents of a logical eraseblock atomically. @buf
410 * has to contain new logical eraseblock data, and @len - the length of the
411 * data, which has to be aligned. The length may be shorter then the logical
412 * eraseblock size, ant the logical eraseblock may be appended to more times
413 * later on. This function guarantees that in case of an unclean reboot the old
414 * contents is preserved. Returns zero in case of success and a negative error
415 * code in case of failure.
417 int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
420 struct ubi_volume *vol = desc->vol;
421 struct ubi_device *ubi = vol->ubi;
422 int vol_id = vol->vol_id;
424 dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
426 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
429 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
432 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
433 len > vol->usable_leb_size || len % ubi->min_io_size)
436 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
437 dtype != UBI_UNKNOWN)
446 return ubi_eba_atomic_leb_change(ubi, vol_id, lnum, buf, len, dtype);
448 EXPORT_SYMBOL_GPL(ubi_leb_change);
451 * ubi_leb_erase - erase logical eraseblock.
452 * @desc: volume descriptor
453 * @lnum: logical eraseblock number
455 * This function un-maps logical eraseblock @lnum and synchronously erases the
456 * correspondent physical eraseblock. Returns zero in case of success and a
457 * negative error code in case of failure.
459 * If the volume is damaged because of an interrupted update this function just
460 * returns immediately with %-EBADF code.
462 int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
464 struct ubi_volume *vol = desc->vol;
465 struct ubi_device *ubi = vol->ubi;
466 int err, vol_id = vol->vol_id;
468 dbg_msg("erase LEB %d:%d", vol_id, lnum);
470 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
473 if (lnum < 0 || lnum >= vol->reserved_pebs)
479 err = ubi_eba_unmap_leb(ubi, vol_id, lnum);
483 return ubi_wl_flush(ubi);
485 EXPORT_SYMBOL_GPL(ubi_leb_erase);
488 * ubi_leb_unmap - un-map logical eraseblock.
489 * @desc: volume descriptor
490 * @lnum: logical eraseblock number
492 * This function un-maps logical eraseblock @lnum and schedules the
493 * corresponding physical eraseblock for erasure, so that it will eventually be
494 * physically erased in background. This operation is much faster then the
497 * Unlike erase, the un-map operation does not guarantee that the logical
498 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
499 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
500 * happens after this, the logical eraseblocks will not necessarily be
501 * un-mapped again when this MTD device is attached. They may actually be
502 * mapped to the same physical eraseblocks again. So, this function has to be
505 * In other words, when un-mapping a logical eraseblock, UBI does not store
506 * any information about this on the flash media, it just marks the logical
507 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
508 * eraseblock is physically erased, it will be mapped again to the same logical
509 * eraseblock when the MTD device is attached again.
511 * The main and obvious use-case of this function is when the contents of a
512 * logical eraseblock has to be re-written. Then it is much more efficient to
513 * first un-map it, then write new data, rather then first erase it, then write
514 * new data. Note, once new data has been written to the logical eraseblock,
515 * UBI guarantees that the old contents has gone forever. In other words, if an
516 * unclean reboot happens after the logical eraseblock has been un-mapped and
517 * then written to, it will contain the last written data.
519 * This function returns zero in case of success and a negative error code in
520 * case of failure. If the volume is damaged because of an interrupted update
521 * this function just returns immediately with %-EBADF code.
523 int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
525 struct ubi_volume *vol = desc->vol;
526 struct ubi_device *ubi = vol->ubi;
527 int vol_id = vol->vol_id;
529 dbg_msg("unmap LEB %d:%d", vol_id, lnum);
531 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
534 if (lnum < 0 || lnum >= vol->reserved_pebs)
540 return ubi_eba_unmap_leb(ubi, vol_id, lnum);
542 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
545 * ubi_is_mapped - check if logical eraseblock is mapped.
546 * @desc: volume descriptor
547 * @lnum: logical eraseblock number
549 * This function checks if logical eraseblock @lnum is mapped to a physical
550 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
551 * mean it will still be un-mapped after the UBI device is re-attached. The
552 * logical eraseblock may become mapped to the physical eraseblock it was last
555 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
556 * error code in case of failure. If the volume is damaged because of an
557 * interrupted update this function just returns immediately with %-EBADF error
560 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
562 struct ubi_volume *vol = desc->vol;
564 dbg_msg("test LEB %d:%d", vol->vol_id, lnum);
566 if (lnum < 0 || lnum >= vol->reserved_pebs)
572 return vol->eba_tbl[lnum] >= 0;
574 EXPORT_SYMBOL_GPL(ubi_is_mapped);