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/slab.h>
14 #include <linux/namei.h>
16 #include <asm/div64.h>
18 #include <ubi_uboot.h>
20 #include <linux/err.h>
25 * ubi_do_get_device_info - get information about UBI device.
26 * @ubi: UBI device description object
27 * @di: the information is stored here
29 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
30 * device is locked and cannot disappear.
32 void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
34 di->ubi_num = ubi->ubi_num;
35 di->leb_size = ubi->leb_size;
36 di->leb_start = ubi->leb_start;
37 di->min_io_size = ubi->min_io_size;
38 di->max_write_size = ubi->max_write_size;
39 di->ro_mode = ubi->ro_mode;
41 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 ubi_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);
284 * ubi_open_volume_path - open UBI volume by its character device node path.
285 * @pathname: volume character device node path
288 * This function is similar to 'ubi_open_volume()', but opens a volume the path
289 * to its character device node.
291 struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
293 int error, ubi_num, vol_id, mod;
297 dbg_gen("open volume %s, mode %d", pathname, mode);
299 if (!pathname || !*pathname)
300 return ERR_PTR(-EINVAL);
302 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
304 return ERR_PTR(error);
306 inode = path.dentry->d_inode;
308 ubi_num = ubi_major2num(imajor(inode));
309 vol_id = iminor(inode) - 1;
313 return ERR_PTR(-EINVAL);
314 if (vol_id >= 0 && ubi_num >= 0)
315 return ubi_open_volume(ubi_num, vol_id, mode);
316 return ERR_PTR(-ENODEV);
318 EXPORT_SYMBOL_GPL(ubi_open_volume_path);
322 * ubi_close_volume - close UBI volume.
323 * @desc: volume descriptor
325 void ubi_close_volume(struct ubi_volume_desc *desc)
327 struct ubi_volume *vol = desc->vol;
328 struct ubi_device *ubi = vol->ubi;
330 dbg_gen("close device %d, volume %d, mode %d",
331 ubi->ubi_num, vol->vol_id, desc->mode);
333 spin_lock(&ubi->volumes_lock);
334 switch (desc->mode) {
345 spin_unlock(&ubi->volumes_lock);
348 put_device(&vol->dev);
350 module_put(THIS_MODULE);
352 EXPORT_SYMBOL_GPL(ubi_close_volume);
355 * ubi_leb_read - read data.
356 * @desc: volume descriptor
357 * @lnum: logical eraseblock number to read from
358 * @buf: buffer where to store the read data
359 * @offset: offset within the logical eraseblock to read from
360 * @len: how many bytes to read
361 * @check: whether UBI has to check the read data's CRC or not.
363 * This function reads data from offset @offset of logical eraseblock @lnum and
364 * stores the data at @buf. When reading from static volumes, @check specifies
365 * whether the data has to be checked or not. If yes, the whole logical
366 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
367 * checksum is per-eraseblock). So checking may substantially slow down the
368 * read speed. The @check argument is ignored for dynamic volumes.
370 * In case of success, this function returns zero. In case of failure, this
371 * function returns a negative error code.
373 * %-EBADMSG error code is returned:
374 * o for both static and dynamic volumes if MTD driver has detected a data
375 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
376 * o for static volumes in case of data CRC mismatch.
378 * If the volume is damaged because of an interrupted update this function just
379 * returns immediately with %-EBADF error code.
381 int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
384 struct ubi_volume *vol = desc->vol;
385 struct ubi_device *ubi = vol->ubi;
386 int err, vol_id = vol->vol_id;
388 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
390 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
391 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
392 offset + len > vol->usable_leb_size)
395 if (vol->vol_type == UBI_STATIC_VOLUME) {
396 if (vol->used_ebs == 0)
397 /* Empty static UBI volume */
399 if (lnum == vol->used_ebs - 1 &&
400 offset + len > vol->last_eb_bytes)
409 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
410 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
411 ubi_warn("mark volume %d as corrupted", vol_id);
417 EXPORT_SYMBOL_GPL(ubi_leb_read);
420 * ubi_leb_write - write data.
421 * @desc: volume descriptor
422 * @lnum: logical eraseblock number to write to
423 * @buf: data to write
424 * @offset: offset within the logical eraseblock where to write
425 * @len: how many bytes to write
427 * This function writes @len bytes of data from @buf to offset @offset of
428 * logical eraseblock @lnum.
430 * This function takes care of physical eraseblock write failures. If write to
431 * the physical eraseblock write operation fails, the logical eraseblock is
432 * re-mapped to another physical eraseblock, the data is recovered, and the
433 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
435 * If all the data were successfully written, zero is returned. If an error
436 * occurred and UBI has not been able to recover from it, this function returns
437 * a negative error code. Note, in case of an error, it is possible that
438 * something was still written to the flash media, but that may be some
441 * If the volume is damaged because of an interrupted update this function just
442 * returns immediately with %-EBADF code.
444 int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
447 struct ubi_volume *vol = desc->vol;
448 struct ubi_device *ubi = vol->ubi;
449 int vol_id = vol->vol_id;
451 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
453 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
456 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
459 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
460 offset + len > vol->usable_leb_size ||
461 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
470 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
472 EXPORT_SYMBOL_GPL(ubi_leb_write);
475 * ubi_leb_change - change logical eraseblock atomically.
476 * @desc: volume descriptor
477 * @lnum: logical eraseblock number to change
478 * @buf: data to write
479 * @len: how many bytes to write
481 * This function changes the contents of a logical eraseblock atomically. @buf
482 * has to contain new logical eraseblock data, and @len - the length of the
483 * data, which has to be aligned. The length may be shorter than the logical
484 * eraseblock size, ant the logical eraseblock may be appended to more times
485 * later on. This function guarantees that in case of an unclean reboot the old
486 * contents is preserved. Returns zero in case of success and a negative error
487 * code in case of failure.
489 int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
492 struct ubi_volume *vol = desc->vol;
493 struct ubi_device *ubi = vol->ubi;
494 int vol_id = vol->vol_id;
496 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
498 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
501 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
504 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
505 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
514 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
516 EXPORT_SYMBOL_GPL(ubi_leb_change);
519 * ubi_leb_erase - erase logical eraseblock.
520 * @desc: volume descriptor
521 * @lnum: logical eraseblock number
523 * This function un-maps logical eraseblock @lnum and synchronously erases the
524 * correspondent physical eraseblock. Returns zero in case of success and a
525 * negative error code in case of failure.
527 * If the volume is damaged because of an interrupted update this function just
528 * returns immediately with %-EBADF code.
530 int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
532 struct ubi_volume *vol = desc->vol;
533 struct ubi_device *ubi = vol->ubi;
536 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
538 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
541 if (lnum < 0 || lnum >= vol->reserved_pebs)
547 err = ubi_eba_unmap_leb(ubi, vol, lnum);
551 return ubi_wl_flush(ubi, vol->vol_id, lnum);
553 EXPORT_SYMBOL_GPL(ubi_leb_erase);
556 * ubi_leb_unmap - un-map logical eraseblock.
557 * @desc: volume descriptor
558 * @lnum: logical eraseblock number
560 * This function un-maps logical eraseblock @lnum and schedules the
561 * corresponding physical eraseblock for erasure, so that it will eventually be
562 * physically erased in background. This operation is much faster than the
565 * Unlike erase, the un-map operation does not guarantee that the logical
566 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
567 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
568 * happens after this, the logical eraseblocks will not necessarily be
569 * un-mapped again when this MTD device is attached. They may actually be
570 * mapped to the same physical eraseblocks again. So, this function has to be
573 * In other words, when un-mapping a logical eraseblock, UBI does not store
574 * any information about this on the flash media, it just marks the logical
575 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
576 * eraseblock is physically erased, it will be mapped again to the same logical
577 * eraseblock when the MTD device is attached again.
579 * The main and obvious use-case of this function is when the contents of a
580 * logical eraseblock has to be re-written. Then it is much more efficient to
581 * first un-map it, then write new data, rather than first erase it, then write
582 * new data. Note, once new data has been written to the logical eraseblock,
583 * UBI guarantees that the old contents has gone forever. In other words, if an
584 * unclean reboot happens after the logical eraseblock has been un-mapped and
585 * then written to, it will contain the last written data.
587 * This function returns zero in case of success and a negative error code in
588 * case of failure. If the volume is damaged because of an interrupted update
589 * this function just returns immediately with %-EBADF code.
591 int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
593 struct ubi_volume *vol = desc->vol;
594 struct ubi_device *ubi = vol->ubi;
596 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
598 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
601 if (lnum < 0 || lnum >= vol->reserved_pebs)
607 return ubi_eba_unmap_leb(ubi, vol, lnum);
609 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
612 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
613 * @desc: volume descriptor
614 * @lnum: logical eraseblock number
616 * This function maps an un-mapped logical eraseblock @lnum to a physical
617 * eraseblock. This means, that after a successful invocation of this
618 * function the logical eraseblock @lnum will be empty (contain only %0xFF
619 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
622 * This function returns zero in case of success, %-EBADF if the volume is
623 * damaged because of an interrupted update, %-EBADMSG if the logical
624 * eraseblock is already mapped, and other negative error codes in case of
627 int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
629 struct ubi_volume *vol = desc->vol;
630 struct ubi_device *ubi = vol->ubi;
632 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
634 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
637 if (lnum < 0 || lnum >= vol->reserved_pebs)
643 if (vol->eba_tbl[lnum] >= 0)
646 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
648 EXPORT_SYMBOL_GPL(ubi_leb_map);
651 * ubi_is_mapped - check if logical eraseblock is mapped.
652 * @desc: volume descriptor
653 * @lnum: logical eraseblock number
655 * This function checks if logical eraseblock @lnum is mapped to a physical
656 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
657 * mean it will still be un-mapped after the UBI device is re-attached. The
658 * logical eraseblock may become mapped to the physical eraseblock it was last
661 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
662 * error code in case of failure. If the volume is damaged because of an
663 * interrupted update this function just returns immediately with %-EBADF error
666 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
668 struct ubi_volume *vol = desc->vol;
670 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
672 if (lnum < 0 || lnum >= vol->reserved_pebs)
678 return vol->eba_tbl[lnum] >= 0;
680 EXPORT_SYMBOL_GPL(ubi_is_mapped);
683 * ubi_sync - synchronize UBI device buffers.
684 * @ubi_num: UBI device to synchronize
686 * The underlying MTD device may cache data in hardware or in software. This
687 * function ensures the caches are flushed. Returns zero in case of success and
688 * a negative error code in case of failure.
690 int ubi_sync(int ubi_num)
692 struct ubi_device *ubi;
694 ubi = ubi_get_device(ubi_num);
702 EXPORT_SYMBOL_GPL(ubi_sync);
705 * ubi_flush - flush UBI work queue.
706 * @ubi_num: UBI device to flush work queue
707 * @vol_id: volume id to flush for
708 * @lnum: logical eraseblock number to flush for
710 * This function executes all pending works for a particular volume id / logical
711 * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
712 * a wildcard for all of the corresponding volume numbers or logical
713 * eraseblock numbers. It returns zero in case of success and a negative error
714 * code in case of failure.
716 int ubi_flush(int ubi_num, int vol_id, int lnum)
718 struct ubi_device *ubi;
721 ubi = ubi_get_device(ubi_num);
725 err = ubi_wl_flush(ubi, vol_id, lnum);
729 EXPORT_SYMBOL_GPL(ubi_flush);
732 BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
735 * ubi_register_volume_notifier - register a volume notifier.
736 * @nb: the notifier description object
737 * @ignore_existing: if non-zero, do not send "added" notification for all
738 * already existing volumes
740 * This function registers a volume notifier, which means that
741 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
742 * removed, re-sized, re-named, or updated. The first argument of the function
743 * is the notification type. The second argument is pointer to a
744 * &struct ubi_notification object which describes the notification event.
745 * Using UBI API from the volume notifier is prohibited.
747 * This function returns zero in case of success and a negative error code
748 * in case of failure.
750 int ubi_register_volume_notifier(struct notifier_block *nb,
755 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
762 * We are going to walk all UBI devices and all volumes, and
763 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
764 * event. We have to lock the @ubi_devices_mutex to make sure UBI
765 * devices do not disappear.
767 mutex_lock(&ubi_devices_mutex);
768 ubi_enumerate_volumes(nb);
769 mutex_unlock(&ubi_devices_mutex);
773 EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
776 * ubi_unregister_volume_notifier - unregister the volume notifier.
777 * @nb: the notifier description object
779 * This function unregisters volume notifier @nm and returns zero in case of
780 * success and a negative error code in case of failure.
782 int ubi_unregister_volume_notifier(struct notifier_block *nb)
784 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
786 EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);