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_do_get_device_info - get information about UBI device.
30 * @ubi: UBI device description object
31 * @di: the information is stored here
33 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
34 * device is locked and cannot disappear.
36 void ubi_do_get_device_info(struct ubi_device
*ubi
, struct ubi_device_info
*di
)
38 di
->ubi_num
= ubi
->ubi_num
;
39 di
->leb_size
= ubi
->leb_size
;
40 di
->min_io_size
= ubi
->min_io_size
;
41 di
->ro_mode
= ubi
->ro_mode
;
42 di
->cdev
= ubi
->cdev
.dev
;
44 EXPORT_SYMBOL_GPL(ubi_do_get_device_info
);
47 * ubi_get_device_info - get information about UBI device.
48 * @ubi_num: UBI device number
49 * @di: the information is stored here
51 * This function returns %0 in case of success, %-EINVAL if the UBI device
52 * number is invalid, and %-ENODEV if there is no such UBI device.
54 int ubi_get_device_info(int ubi_num
, struct ubi_device_info
*di
)
56 struct ubi_device
*ubi
;
58 if (ubi_num
< 0 || ubi_num
>= UBI_MAX_DEVICES
)
60 ubi
= ubi_get_device(ubi_num
);
63 ubi_do_get_device_info(ubi
, di
);
67 EXPORT_SYMBOL_GPL(ubi_get_device_info
);
70 * ubi_do_get_volume_info - get information about UBI volume.
71 * @ubi: UBI device description object
72 * @vol: volume description object
73 * @vi: the information is stored here
75 void ubi_do_get_volume_info(struct ubi_device
*ubi
, struct ubi_volume
*vol
,
76 struct ubi_volume_info
*vi
)
78 vi
->vol_id
= vol
->vol_id
;
79 vi
->ubi_num
= ubi
->ubi_num
;
80 vi
->size
= vol
->reserved_pebs
;
81 vi
->used_bytes
= vol
->used_bytes
;
82 vi
->vol_type
= vol
->vol_type
;
83 vi
->corrupted
= vol
->corrupted
;
84 vi
->upd_marker
= vol
->upd_marker
;
85 vi
->alignment
= vol
->alignment
;
86 vi
->usable_leb_size
= vol
->usable_leb_size
;
87 vi
->name_len
= vol
->name_len
;
89 vi
->cdev
= vol
->cdev
.dev
;
93 * ubi_get_volume_info - get information about UBI volume.
94 * @desc: volume descriptor
95 * @vi: the information is stored here
97 void ubi_get_volume_info(struct ubi_volume_desc
*desc
,
98 struct ubi_volume_info
*vi
)
100 ubi_do_get_volume_info(desc
->vol
->ubi
, desc
->vol
, vi
);
102 EXPORT_SYMBOL_GPL(ubi_get_volume_info
);
105 * ubi_open_volume - open UBI volume.
106 * @ubi_num: UBI device number
110 * The @mode parameter specifies if the volume should be opened in read-only
111 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
112 * nobody else will be able to open this volume. UBI allows to have many volume
113 * readers and one writer at a time.
115 * If a static volume is being opened for the first time since boot, it will be
116 * checked by this function, which means it will be fully read and the CRC
117 * checksum of each logical eraseblock will be checked.
119 * This function returns volume descriptor in case of success and a negative
120 * error code in case of failure.
122 struct ubi_volume_desc
*ubi_open_volume(int ubi_num
, int vol_id
, int mode
)
125 struct ubi_volume_desc
*desc
;
126 struct ubi_device
*ubi
;
127 struct ubi_volume
*vol
;
129 dbg_gen("open device %d, volume %d, mode %d", ubi_num
, vol_id
, mode
);
131 if (ubi_num
< 0 || ubi_num
>= UBI_MAX_DEVICES
)
132 return ERR_PTR(-EINVAL
);
134 if (mode
!= UBI_READONLY
&& mode
!= UBI_READWRITE
&&
135 mode
!= UBI_EXCLUSIVE
)
136 return ERR_PTR(-EINVAL
);
139 * First of all, we have to get the UBI device to prevent its removal.
141 ubi
= ubi_get_device(ubi_num
);
143 return ERR_PTR(-ENODEV
);
145 if (vol_id
< 0 || vol_id
>= ubi
->vtbl_slots
) {
150 desc
= kmalloc(sizeof(struct ubi_volume_desc
), GFP_KERNEL
);
157 if (!try_module_get(THIS_MODULE
))
160 spin_lock(&ubi
->volumes_lock
);
161 vol
= ubi
->volumes
[vol_id
];
174 if (vol
->exclusive
|| vol
->writers
> 0)
180 if (vol
->exclusive
|| vol
->writers
|| vol
->readers
)
185 get_device(&vol
->dev
);
187 spin_unlock(&ubi
->volumes_lock
);
192 mutex_lock(&ubi
->ckvol_mutex
);
194 /* This is the first open - check the volume */
195 err
= ubi_check_volume(ubi
, vol_id
);
197 mutex_unlock(&ubi
->ckvol_mutex
);
198 ubi_close_volume(desc
);
202 ubi_warn("volume %d on UBI device %d is corrupted",
203 vol_id
, ubi
->ubi_num
);
208 mutex_unlock(&ubi
->ckvol_mutex
);
213 spin_unlock(&ubi
->volumes_lock
);
214 module_put(THIS_MODULE
);
219 dbg_err("cannot open device %d, volume %d, error %d",
220 ubi_num
, vol_id
, err
);
223 EXPORT_SYMBOL_GPL(ubi_open_volume
);
226 * ubi_open_volume_nm - open UBI volume by name.
227 * @ubi_num: UBI device number
231 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
233 struct ubi_volume_desc
*ubi_open_volume_nm(int ubi_num
, const char *name
,
236 int i
, vol_id
= -1, len
;
237 struct ubi_device
*ubi
;
238 struct ubi_volume_desc
*ret
;
240 dbg_gen("open device %d, volume %s, mode %d", ubi_num
, name
, mode
);
243 return ERR_PTR(-EINVAL
);
245 len
= strnlen(name
, UBI_VOL_NAME_MAX
+ 1);
246 if (len
> UBI_VOL_NAME_MAX
)
247 return ERR_PTR(-EINVAL
);
249 if (ubi_num
< 0 || ubi_num
>= UBI_MAX_DEVICES
)
250 return ERR_PTR(-EINVAL
);
252 ubi
= ubi_get_device(ubi_num
);
254 return ERR_PTR(-ENODEV
);
256 spin_lock(&ubi
->volumes_lock
);
257 /* Walk all volumes of this UBI device */
258 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
259 struct ubi_volume
*vol
= ubi
->volumes
[i
];
261 if (vol
&& len
== vol
->name_len
&& !strcmp(name
, vol
->name
)) {
266 spin_unlock(&ubi
->volumes_lock
);
269 ret
= ubi_open_volume(ubi_num
, vol_id
, mode
);
271 ret
= ERR_PTR(-ENODEV
);
274 * We should put the UBI device even in case of success, because
275 * 'ubi_open_volume()' took a reference as well.
280 EXPORT_SYMBOL_GPL(ubi_open_volume_nm
);
283 * ubi_close_volume - close UBI volume.
284 * @desc: volume descriptor
286 void ubi_close_volume(struct ubi_volume_desc
*desc
)
288 struct ubi_volume
*vol
= desc
->vol
;
289 struct ubi_device
*ubi
= vol
->ubi
;
291 dbg_gen("close device %d, volume %d, mode %d",
292 ubi
->ubi_num
, vol
->vol_id
, desc
->mode
);
294 spin_lock(&ubi
->volumes_lock
);
295 switch (desc
->mode
) {
306 spin_unlock(&ubi
->volumes_lock
);
309 put_device(&vol
->dev
);
311 module_put(THIS_MODULE
);
313 EXPORT_SYMBOL_GPL(ubi_close_volume
);
316 * ubi_leb_read - read data.
317 * @desc: volume descriptor
318 * @lnum: logical eraseblock number to read from
319 * @buf: buffer where to store the read data
320 * @offset: offset within the logical eraseblock to read from
321 * @len: how many bytes to read
322 * @check: whether UBI has to check the read data's CRC or not.
324 * This function reads data from offset @offset of logical eraseblock @lnum and
325 * stores the data at @buf. When reading from static volumes, @check specifies
326 * whether the data has to be checked or not. If yes, the whole logical
327 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
328 * checksum is per-eraseblock). So checking may substantially slow down the
329 * read speed. The @check argument is ignored for dynamic volumes.
331 * In case of success, this function returns zero. In case of failure, this
332 * function returns a negative error code.
334 * %-EBADMSG error code is returned:
335 * o for both static and dynamic volumes if MTD driver has detected a data
336 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
337 * o for static volumes in case of data CRC mismatch.
339 * If the volume is damaged because of an interrupted update this function just
340 * returns immediately with %-EBADF error code.
342 int ubi_leb_read(struct ubi_volume_desc
*desc
, int lnum
, char *buf
, int offset
,
345 struct ubi_volume
*vol
= desc
->vol
;
346 struct ubi_device
*ubi
= vol
->ubi
;
347 int err
, vol_id
= vol
->vol_id
;
349 dbg_gen("read %d bytes from LEB %d:%d:%d", len
, vol_id
, lnum
, offset
);
351 if (vol_id
< 0 || vol_id
>= ubi
->vtbl_slots
|| lnum
< 0 ||
352 lnum
>= vol
->used_ebs
|| offset
< 0 || len
< 0 ||
353 offset
+ len
> vol
->usable_leb_size
)
356 if (vol
->vol_type
== UBI_STATIC_VOLUME
) {
357 if (vol
->used_ebs
== 0)
358 /* Empty static UBI volume */
360 if (lnum
== vol
->used_ebs
- 1 &&
361 offset
+ len
> vol
->last_eb_bytes
)
370 err
= ubi_eba_read_leb(ubi
, vol
, lnum
, buf
, offset
, len
, check
);
371 if (err
&& err
== -EBADMSG
&& vol
->vol_type
== UBI_STATIC_VOLUME
) {
372 ubi_warn("mark volume %d as corrupted", vol_id
);
378 EXPORT_SYMBOL_GPL(ubi_leb_read
);
381 * ubi_leb_write - write data.
382 * @desc: volume descriptor
383 * @lnum: logical eraseblock number to write to
384 * @buf: data to write
385 * @offset: offset within the logical eraseblock where to write
386 * @len: how many bytes to write
387 * @dtype: expected data type
389 * This function writes @len bytes of data from @buf to offset @offset of
390 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
393 * This function takes care of physical eraseblock write failures. If write to
394 * the physical eraseblock write operation fails, the logical eraseblock is
395 * re-mapped to another physical eraseblock, the data is recovered, and the
396 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
398 * If all the data were successfully written, zero is returned. If an error
399 * occurred and UBI has not been able to recover from it, this function returns
400 * a negative error code. Note, in case of an error, it is possible that
401 * something was still written to the flash media, but that may be some
404 * If the volume is damaged because of an interrupted update this function just
405 * returns immediately with %-EBADF code.
407 int ubi_leb_write(struct ubi_volume_desc
*desc
, int lnum
, const void *buf
,
408 int offset
, int len
, int dtype
)
410 struct ubi_volume
*vol
= desc
->vol
;
411 struct ubi_device
*ubi
= vol
->ubi
;
412 int vol_id
= vol
->vol_id
;
414 dbg_gen("write %d bytes to LEB %d:%d:%d", len
, vol_id
, lnum
, offset
);
416 if (vol_id
< 0 || vol_id
>= ubi
->vtbl_slots
)
419 if (desc
->mode
== UBI_READONLY
|| vol
->vol_type
== UBI_STATIC_VOLUME
)
422 if (lnum
< 0 || lnum
>= vol
->reserved_pebs
|| offset
< 0 || len
< 0 ||
423 offset
+ len
> vol
->usable_leb_size
||
424 offset
& (ubi
->min_io_size
- 1) || len
& (ubi
->min_io_size
- 1))
427 if (dtype
!= UBI_LONGTERM
&& dtype
!= UBI_SHORTTERM
&&
428 dtype
!= UBI_UNKNOWN
)
437 return ubi_eba_write_leb(ubi
, vol
, lnum
, buf
, offset
, len
, dtype
);
439 EXPORT_SYMBOL_GPL(ubi_leb_write
);
442 * ubi_leb_change - change logical eraseblock atomically.
443 * @desc: volume descriptor
444 * @lnum: logical eraseblock number to change
445 * @buf: data to write
446 * @len: how many bytes to write
447 * @dtype: expected data type
449 * This function changes the contents of a logical eraseblock atomically. @buf
450 * has to contain new logical eraseblock data, and @len - the length of the
451 * data, which has to be aligned. The length may be shorter then the logical
452 * eraseblock size, ant the logical eraseblock may be appended to more times
453 * later on. This function guarantees that in case of an unclean reboot the old
454 * contents is preserved. Returns zero in case of success and a negative error
455 * code in case of failure.
457 int ubi_leb_change(struct ubi_volume_desc
*desc
, int lnum
, const void *buf
,
460 struct ubi_volume
*vol
= desc
->vol
;
461 struct ubi_device
*ubi
= vol
->ubi
;
462 int vol_id
= vol
->vol_id
;
464 dbg_gen("atomically write %d bytes to LEB %d:%d", len
, vol_id
, lnum
);
466 if (vol_id
< 0 || vol_id
>= ubi
->vtbl_slots
)
469 if (desc
->mode
== UBI_READONLY
|| vol
->vol_type
== UBI_STATIC_VOLUME
)
472 if (lnum
< 0 || lnum
>= vol
->reserved_pebs
|| len
< 0 ||
473 len
> vol
->usable_leb_size
|| len
& (ubi
->min_io_size
- 1))
476 if (dtype
!= UBI_LONGTERM
&& dtype
!= UBI_SHORTTERM
&&
477 dtype
!= UBI_UNKNOWN
)
486 return ubi_eba_atomic_leb_change(ubi
, vol
, lnum
, buf
, len
, dtype
);
488 EXPORT_SYMBOL_GPL(ubi_leb_change
);
491 * ubi_leb_erase - erase logical eraseblock.
492 * @desc: volume descriptor
493 * @lnum: logical eraseblock number
495 * This function un-maps logical eraseblock @lnum and synchronously erases the
496 * correspondent physical eraseblock. Returns zero in case of success and a
497 * negative error code in case of failure.
499 * If the volume is damaged because of an interrupted update this function just
500 * returns immediately with %-EBADF code.
502 int ubi_leb_erase(struct ubi_volume_desc
*desc
, int lnum
)
504 struct ubi_volume
*vol
= desc
->vol
;
505 struct ubi_device
*ubi
= vol
->ubi
;
508 dbg_gen("erase LEB %d:%d", vol
->vol_id
, lnum
);
510 if (desc
->mode
== UBI_READONLY
|| vol
->vol_type
== UBI_STATIC_VOLUME
)
513 if (lnum
< 0 || lnum
>= vol
->reserved_pebs
)
519 err
= ubi_eba_unmap_leb(ubi
, vol
, lnum
);
523 return ubi_wl_flush(ubi
);
525 EXPORT_SYMBOL_GPL(ubi_leb_erase
);
528 * ubi_leb_unmap - un-map logical eraseblock.
529 * @desc: volume descriptor
530 * @lnum: logical eraseblock number
532 * This function un-maps logical eraseblock @lnum and schedules the
533 * corresponding physical eraseblock for erasure, so that it will eventually be
534 * physically erased in background. This operation is much faster then the
537 * Unlike erase, the un-map operation does not guarantee that the logical
538 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
539 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
540 * happens after this, the logical eraseblocks will not necessarily be
541 * un-mapped again when this MTD device is attached. They may actually be
542 * mapped to the same physical eraseblocks again. So, this function has to be
545 * In other words, when un-mapping a logical eraseblock, UBI does not store
546 * any information about this on the flash media, it just marks the logical
547 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
548 * eraseblock is physically erased, it will be mapped again to the same logical
549 * eraseblock when the MTD device is attached again.
551 * The main and obvious use-case of this function is when the contents of a
552 * logical eraseblock has to be re-written. Then it is much more efficient to
553 * first un-map it, then write new data, rather then first erase it, then write
554 * new data. Note, once new data has been written to the logical eraseblock,
555 * UBI guarantees that the old contents has gone forever. In other words, if an
556 * unclean reboot happens after the logical eraseblock has been un-mapped and
557 * then written to, it will contain the last written data.
559 * This function returns zero in case of success and a negative error code in
560 * case of failure. If the volume is damaged because of an interrupted update
561 * this function just returns immediately with %-EBADF code.
563 int ubi_leb_unmap(struct ubi_volume_desc
*desc
, int lnum
)
565 struct ubi_volume
*vol
= desc
->vol
;
566 struct ubi_device
*ubi
= vol
->ubi
;
568 dbg_gen("unmap LEB %d:%d", vol
->vol_id
, lnum
);
570 if (desc
->mode
== UBI_READONLY
|| vol
->vol_type
== UBI_STATIC_VOLUME
)
573 if (lnum
< 0 || lnum
>= vol
->reserved_pebs
)
579 return ubi_eba_unmap_leb(ubi
, vol
, lnum
);
581 EXPORT_SYMBOL_GPL(ubi_leb_unmap
);
584 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
585 * @desc: volume descriptor
586 * @lnum: logical eraseblock number
587 * @dtype: expected data type
589 * This function maps an un-mapped logical eraseblock @lnum to a physical
590 * eraseblock. This means, that after a successful invocation of this
591 * function the logical eraseblock @lnum will be empty (contain only %0xFF
592 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
595 * This function returns zero in case of success, %-EBADF if the volume is
596 * damaged because of an interrupted update, %-EBADMSG if the logical
597 * eraseblock is already mapped, and other negative error codes in case of
600 int ubi_leb_map(struct ubi_volume_desc
*desc
, int lnum
, int dtype
)
602 struct ubi_volume
*vol
= desc
->vol
;
603 struct ubi_device
*ubi
= vol
->ubi
;
605 dbg_gen("unmap LEB %d:%d", vol
->vol_id
, lnum
);
607 if (desc
->mode
== UBI_READONLY
|| vol
->vol_type
== UBI_STATIC_VOLUME
)
610 if (lnum
< 0 || lnum
>= vol
->reserved_pebs
)
613 if (dtype
!= UBI_LONGTERM
&& dtype
!= UBI_SHORTTERM
&&
614 dtype
!= UBI_UNKNOWN
)
620 if (vol
->eba_tbl
[lnum
] >= 0)
623 return ubi_eba_write_leb(ubi
, vol
, lnum
, NULL
, 0, 0, dtype
);
625 EXPORT_SYMBOL_GPL(ubi_leb_map
);
628 * ubi_is_mapped - check if logical eraseblock is mapped.
629 * @desc: volume descriptor
630 * @lnum: logical eraseblock number
632 * This function checks if logical eraseblock @lnum is mapped to a physical
633 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
634 * mean it will still be un-mapped after the UBI device is re-attached. The
635 * logical eraseblock may become mapped to the physical eraseblock it was last
638 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
639 * error code in case of failure. If the volume is damaged because of an
640 * interrupted update this function just returns immediately with %-EBADF error
643 int ubi_is_mapped(struct ubi_volume_desc
*desc
, int lnum
)
645 struct ubi_volume
*vol
= desc
->vol
;
647 dbg_gen("test LEB %d:%d", vol
->vol_id
, lnum
);
649 if (lnum
< 0 || lnum
>= vol
->reserved_pebs
)
655 return vol
->eba_tbl
[lnum
] >= 0;
657 EXPORT_SYMBOL_GPL(ubi_is_mapped
);
660 * ubi_sync - synchronize UBI device buffers.
661 * @ubi_num: UBI device to synchronize
663 * The underlying MTD device may cache data in hardware or in software. This
664 * function ensures the caches are flushed. Returns zero in case of success and
665 * a negative error code in case of failure.
667 int ubi_sync(int ubi_num
)
669 struct ubi_device
*ubi
;
671 ubi
= ubi_get_device(ubi_num
);
676 ubi
->mtd
->sync(ubi
->mtd
);
681 EXPORT_SYMBOL_GPL(ubi_sync
);
683 BLOCKING_NOTIFIER_HEAD(ubi_notifiers
);
686 * ubi_register_volume_notifier - register a volume notifier.
687 * @nb: the notifier description object
688 * @ignore_existing: if non-zero, do not send "added" notification for all
689 * already existing volumes
691 * This function registers a volume notifier, which means that
692 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
693 * removed, re-sized, re-named, or updated. The first argument of the function
694 * is the notification type. The second argument is pointer to a
695 * &struct ubi_notification object which describes the notification event.
696 * Using UBI API from the volume notifier is prohibited.
698 * This function returns zero in case of success and a negative error code
699 * in case of failure.
701 int ubi_register_volume_notifier(struct notifier_block
*nb
,
706 err
= blocking_notifier_chain_register(&ubi_notifiers
, nb
);
713 * We are going to walk all UBI devices and all volumes, and
714 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
715 * event. We have to lock the @ubi_devices_mutex to make sure UBI
716 * devices do not disappear.
718 mutex_lock(&ubi_devices_mutex
);
719 ubi_enumerate_volumes(nb
);
720 mutex_unlock(&ubi_devices_mutex
);
724 EXPORT_SYMBOL_GPL(ubi_register_volume_notifier
);
727 * ubi_unregister_volume_notifier - unregister the volume notifier.
728 * @nb: the notifier description object
730 * This function unregisters volume notifier @nm and returns zero in case of
731 * success and a negative error code in case of failure.
733 int ubi_unregister_volume_notifier(struct notifier_block
*nb
)
735 return blocking_notifier_chain_unregister(&ubi_notifiers
, nb
);
737 EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier
);