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 = ubi->cdev.dev;
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 = vol->cdev.dev;
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;
103 struct ubi_volume *vol;
105 dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
107 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
108 return ERR_PTR(-EINVAL);
110 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
111 mode != UBI_EXCLUSIVE)
112 return ERR_PTR(-EINVAL);
114 ubi = ubi_devices[ubi_num];
116 return ERR_PTR(-ENODEV);
118 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
119 return ERR_PTR(-EINVAL);
121 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
123 return ERR_PTR(-ENOMEM);
126 if (!try_module_get(THIS_MODULE))
129 spin_lock(&ubi->volumes_lock);
130 vol = ubi->volumes[vol_id];
143 if (vol->exclusive || vol->writers > 0)
149 if (vol->exclusive || vol->writers || vol->readers)
154 get_device(&vol->dev);
156 spin_unlock(&ubi->volumes_lock);
162 * To prevent simultaneous checks of the same volume we use
163 * @volumes_mutex, although it is not the purpose it was introduced
166 mutex_lock(&ubi->volumes_mutex);
168 /* This is the first open - check the volume */
169 err = ubi_check_volume(ubi, vol_id);
171 mutex_unlock(&ubi->volumes_mutex);
172 ubi_close_volume(desc);
176 ubi_warn("volume %d on UBI device %d is corrupted",
177 vol_id, ubi->ubi_num);
182 mutex_unlock(&ubi->volumes_mutex);
187 spin_unlock(&ubi->volumes_lock);
188 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;
209 dbg_msg("open volume %s, mode %d", name, mode);
212 return ERR_PTR(-EINVAL);
214 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
215 if (len > UBI_VOL_NAME_MAX)
216 return ERR_PTR(-EINVAL);
218 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
219 return ERR_PTR(-EINVAL);
221 ubi = ubi_devices[ubi_num];
223 return ERR_PTR(-ENODEV);
225 spin_lock(&ubi->volumes_lock);
226 /* Walk all volumes of this UBI device */
227 for (i = 0; i < ubi->vtbl_slots; i++) {
228 struct ubi_volume *vol = ubi->volumes[i];
230 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
235 spin_unlock(&ubi->volumes_lock);
238 return ERR_PTR(-ENODEV);
240 return ubi_open_volume(ubi_num, vol_id, mode);
242 EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
245 * ubi_close_volume - close UBI volume.
246 * @desc: volume descriptor
248 void ubi_close_volume(struct ubi_volume_desc *desc)
250 struct ubi_volume *vol = desc->vol;
252 dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode);
254 spin_lock(&vol->ubi->volumes_lock);
255 switch (desc->mode) {
266 spin_unlock(&vol->ubi->volumes_lock);
268 put_device(&vol->dev);
270 module_put(THIS_MODULE);
272 EXPORT_SYMBOL_GPL(ubi_close_volume);
275 * ubi_leb_read - read data.
276 * @desc: volume descriptor
277 * @lnum: logical eraseblock number to read from
278 * @buf: buffer where to store the read data
279 * @offset: offset within the logical eraseblock to read from
280 * @len: how many bytes to read
281 * @check: whether UBI has to check the read data's CRC or not.
283 * This function reads data from offset @offset of logical eraseblock @lnum and
284 * stores the data at @buf. When reading from static volumes, @check specifies
285 * whether the data has to be checked or not. If yes, the whole logical
286 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
287 * checksum is per-eraseblock). So checking may substantially slow down the
288 * read speed. The @check argument is ignored for dynamic volumes.
290 * In case of success, this function returns zero. In case of failure, this
291 * function returns a negative error code.
293 * %-EBADMSG error code is returned:
294 * o for both static and dynamic volumes if MTD driver has detected a data
295 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
296 * o for static volumes in case of data CRC mismatch.
298 * If the volume is damaged because of an interrupted update this function just
299 * returns immediately with %-EBADF error code.
301 int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
304 struct ubi_volume *vol = desc->vol;
305 struct ubi_device *ubi = vol->ubi;
306 int err, vol_id = vol->vol_id;
308 dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
310 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
311 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
312 offset + len > vol->usable_leb_size)
315 if (vol->vol_type == UBI_STATIC_VOLUME) {
316 if (vol->used_ebs == 0)
317 /* Empty static UBI volume */
319 if (lnum == vol->used_ebs - 1 &&
320 offset + len > vol->last_eb_bytes)
329 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
330 if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
331 ubi_warn("mark volume %d as corrupted", vol_id);
337 EXPORT_SYMBOL_GPL(ubi_leb_read);
340 * ubi_leb_write - write data.
341 * @desc: volume descriptor
342 * @lnum: logical eraseblock number to write to
343 * @buf: data to write
344 * @offset: offset within the logical eraseblock where to write
345 * @len: how many bytes to write
346 * @dtype: expected data type
348 * This function writes @len bytes of data from @buf to offset @offset of
349 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
352 * This function takes care of physical eraseblock write failures. If write to
353 * the physical eraseblock write operation fails, the logical eraseblock is
354 * re-mapped to another physical eraseblock, the data is recovered, and the
355 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
357 * If all the data were successfully written, zero is returned. If an error
358 * occurred and UBI has not been able to recover from it, this function returns
359 * a negative error code. Note, in case of an error, it is possible that
360 * something was still written to the flash media, but that may be some
363 * If the volume is damaged because of an interrupted update this function just
364 * returns immediately with %-EBADF code.
366 int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
367 int offset, int len, int dtype)
369 struct ubi_volume *vol = desc->vol;
370 struct ubi_device *ubi = vol->ubi;
371 int vol_id = vol->vol_id;
373 dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
375 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
378 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
381 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
382 offset + len > vol->usable_leb_size || offset % ubi->min_io_size ||
383 len % ubi->min_io_size)
386 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
387 dtype != UBI_UNKNOWN)
396 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
398 EXPORT_SYMBOL_GPL(ubi_leb_write);
401 * ubi_leb_change - change logical eraseblock atomically.
402 * @desc: volume descriptor
403 * @lnum: logical eraseblock number to change
404 * @buf: data to write
405 * @len: how many bytes to write
406 * @dtype: expected data type
408 * This function changes the contents of a logical eraseblock atomically. @buf
409 * has to contain new logical eraseblock data, and @len - the length of the
410 * data, which has to be aligned. The length may be shorter then the logical
411 * eraseblock size, ant the logical eraseblock may be appended to more times
412 * later on. This function guarantees that in case of an unclean reboot the old
413 * contents is preserved. Returns zero in case of success and a negative error
414 * code in case of failure.
416 int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
419 struct ubi_volume *vol = desc->vol;
420 struct ubi_device *ubi = vol->ubi;
421 int vol_id = vol->vol_id;
423 dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
425 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
428 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
431 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
432 len > vol->usable_leb_size || len % ubi->min_io_size)
435 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
436 dtype != UBI_UNKNOWN)
445 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
447 EXPORT_SYMBOL_GPL(ubi_leb_change);
450 * ubi_leb_erase - erase logical eraseblock.
451 * @desc: volume descriptor
452 * @lnum: logical eraseblock number
454 * This function un-maps logical eraseblock @lnum and synchronously erases the
455 * correspondent physical eraseblock. Returns zero in case of success and a
456 * negative error code in case of failure.
458 * If the volume is damaged because of an interrupted update this function just
459 * returns immediately with %-EBADF code.
461 int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
463 struct ubi_volume *vol = desc->vol;
464 struct ubi_device *ubi = vol->ubi;
465 int err, vol_id = vol->vol_id;
467 dbg_msg("erase LEB %d:%d", vol_id, lnum);
469 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
472 if (lnum < 0 || lnum >= vol->reserved_pebs)
478 err = ubi_eba_unmap_leb(ubi, vol, lnum);
482 return ubi_wl_flush(ubi);
484 EXPORT_SYMBOL_GPL(ubi_leb_erase);
487 * ubi_leb_unmap - un-map logical eraseblock.
488 * @desc: volume descriptor
489 * @lnum: logical eraseblock number
491 * This function un-maps logical eraseblock @lnum and schedules the
492 * corresponding physical eraseblock for erasure, so that it will eventually be
493 * physically erased in background. This operation is much faster then the
496 * Unlike erase, the un-map operation does not guarantee that the logical
497 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
498 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
499 * happens after this, the logical eraseblocks will not necessarily be
500 * un-mapped again when this MTD device is attached. They may actually be
501 * mapped to the same physical eraseblocks again. So, this function has to be
504 * In other words, when un-mapping a logical eraseblock, UBI does not store
505 * any information about this on the flash media, it just marks the logical
506 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
507 * eraseblock is physically erased, it will be mapped again to the same logical
508 * eraseblock when the MTD device is attached again.
510 * The main and obvious use-case of this function is when the contents of a
511 * logical eraseblock has to be re-written. Then it is much more efficient to
512 * first un-map it, then write new data, rather then first erase it, then write
513 * new data. Note, once new data has been written to the logical eraseblock,
514 * UBI guarantees that the old contents has gone forever. In other words, if an
515 * unclean reboot happens after the logical eraseblock has been un-mapped and
516 * then written to, it will contain the last written data.
518 * This function returns zero in case of success and a negative error code in
519 * case of failure. If the volume is damaged because of an interrupted update
520 * this function just returns immediately with %-EBADF code.
522 int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
524 struct ubi_volume *vol = desc->vol;
525 struct ubi_device *ubi = vol->ubi;
526 int vol_id = vol->vol_id;
528 dbg_msg("unmap LEB %d:%d", vol_id, lnum);
530 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
533 if (lnum < 0 || lnum >= vol->reserved_pebs)
539 return ubi_eba_unmap_leb(ubi, vol, lnum);
541 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
544 * ubi_leb_map - map logical erasblock to a physical eraseblock.
545 * @desc: volume descriptor
546 * @lnum: logical eraseblock number
547 * @dtype: expected data type
549 * This function maps an un-mapped logical eraseblock @lnum to a physical
550 * eraseblock. This means, that after a successfull invocation of this
551 * function the logical eraseblock @lnum will be empty (contain only %0xFF
552 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
555 * This function returns zero in case of success, %-EBADF if the volume is
556 * damaged because of an interrupted update, %-EBADMSG if the logical
557 * eraseblock is already mapped, and other negative error codes in case of
560 int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
562 struct ubi_volume *vol = desc->vol;
563 struct ubi_device *ubi = vol->ubi;
564 int vol_id = vol->vol_id;
566 dbg_msg("unmap LEB %d:%d", vol_id, lnum);
568 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
571 if (lnum < 0 || lnum >= vol->reserved_pebs)
574 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
575 dtype != UBI_UNKNOWN)
581 if (vol->eba_tbl[lnum] >= 0)
584 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
586 EXPORT_SYMBOL_GPL(ubi_leb_map);
589 * ubi_is_mapped - check if logical eraseblock is mapped.
590 * @desc: volume descriptor
591 * @lnum: logical eraseblock number
593 * This function checks if logical eraseblock @lnum is mapped to a physical
594 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
595 * mean it will still be un-mapped after the UBI device is re-attached. The
596 * logical eraseblock may become mapped to the physical eraseblock it was last
599 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
600 * error code in case of failure. If the volume is damaged because of an
601 * interrupted update this function just returns immediately with %-EBADF error
604 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
606 struct ubi_volume *vol = desc->vol;
608 dbg_msg("test LEB %d:%d", vol->vol_id, lnum);
610 if (lnum < 0 || lnum >= vol->reserved_pebs)
616 return vol->eba_tbl[lnum] >= 0;
618 EXPORT_SYMBOL_GPL(ubi_is_mapped);