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 */
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/namei.h>
17 #include <asm/div64.h>
19 #include <ubi_uboot.h>
21 #include <linux/err.h>
26 * ubi_do_get_device_info - get information about UBI device.
27 * @ubi: UBI device description object
28 * @di: the information is stored here
30 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
31 * device is locked and cannot disappear.
33 void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
35 di->ubi_num = ubi->ubi_num;
36 di->leb_size = ubi->leb_size;
37 di->leb_start = ubi->leb_start;
38 di->min_io_size = ubi->min_io_size;
39 di->max_write_size = ubi->max_write_size;
40 di->ro_mode = ubi->ro_mode;
42 di->cdev = ubi->cdev.dev;
45 EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
48 * ubi_get_device_info - get information about UBI device.
49 * @ubi_num: UBI device number
50 * @di: the information is stored here
52 * This function returns %0 in case of success, %-EINVAL if the UBI device
53 * number is invalid, and %-ENODEV if there is no such UBI device.
55 int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
57 struct ubi_device *ubi;
59 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
61 ubi = ubi_get_device(ubi_num);
64 ubi_do_get_device_info(ubi, di);
68 EXPORT_SYMBOL_GPL(ubi_get_device_info);
71 * ubi_do_get_volume_info - get information about UBI volume.
72 * @ubi: UBI device description object
73 * @vol: volume description object
74 * @vi: the information is stored here
76 void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
77 struct ubi_volume_info *vi)
79 vi->vol_id = vol->vol_id;
80 vi->ubi_num = ubi->ubi_num;
81 vi->size = vol->reserved_pebs;
82 vi->used_bytes = vol->used_bytes;
83 vi->vol_type = vol->vol_type;
84 vi->corrupted = vol->corrupted;
85 vi->upd_marker = vol->upd_marker;
86 vi->alignment = vol->alignment;
87 vi->usable_leb_size = vol->usable_leb_size;
88 vi->name_len = vol->name_len;
90 vi->cdev = vol->cdev.dev;
94 * ubi_get_volume_info - get information about UBI volume.
95 * @desc: volume descriptor
96 * @vi: the information is stored here
98 void ubi_get_volume_info(struct ubi_volume_desc *desc,
99 struct ubi_volume_info *vi)
101 ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
103 EXPORT_SYMBOL_GPL(ubi_get_volume_info);
106 * ubi_open_volume - open UBI volume.
107 * @ubi_num: UBI device number
111 * The @mode parameter specifies if the volume should be opened in read-only
112 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
113 * nobody else will be able to open this volume. UBI allows to have many volume
114 * readers and one writer at a time.
116 * If a static volume is being opened for the first time since boot, it will be
117 * checked by this function, which means it will be fully read and the CRC
118 * checksum of each logical eraseblock will be checked.
120 * This function returns volume descriptor in case of success and a negative
121 * error code in case of failure.
123 struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
126 struct ubi_volume_desc *desc;
127 struct ubi_device *ubi;
128 struct ubi_volume *vol;
130 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
132 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
133 return ERR_PTR(-EINVAL);
135 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
136 mode != UBI_EXCLUSIVE)
137 return ERR_PTR(-EINVAL);
140 * First of all, we have to get the UBI device to prevent its removal.
142 ubi = ubi_get_device(ubi_num);
144 return ERR_PTR(-ENODEV);
146 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
151 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
158 if (!try_module_get(THIS_MODULE))
161 spin_lock(&ubi->volumes_lock);
162 vol = ubi->volumes[vol_id];
175 if (vol->exclusive || vol->writers > 0)
181 if (vol->exclusive || vol->writers || vol->readers)
186 get_device(&vol->dev);
188 spin_unlock(&ubi->volumes_lock);
193 mutex_lock(&ubi->ckvol_mutex);
195 /* This is the first open - check the volume */
196 err = ubi_check_volume(ubi, vol_id);
198 mutex_unlock(&ubi->ckvol_mutex);
199 ubi_close_volume(desc);
203 ubi_warn("volume %d on UBI device %d is corrupted",
204 vol_id, ubi->ubi_num);
209 mutex_unlock(&ubi->ckvol_mutex);
214 spin_unlock(&ubi->volumes_lock);
215 module_put(THIS_MODULE);
220 ubi_err("cannot open device %d, volume %d, error %d",
221 ubi_num, vol_id, err);
224 EXPORT_SYMBOL_GPL(ubi_open_volume);
227 * ubi_open_volume_nm - open UBI volume by name.
228 * @ubi_num: UBI device number
232 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
234 struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
237 int i, vol_id = -1, len;
238 struct ubi_device *ubi;
239 struct ubi_volume_desc *ret;
241 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
244 return ERR_PTR(-EINVAL);
246 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
247 if (len > UBI_VOL_NAME_MAX)
248 return ERR_PTR(-EINVAL);
250 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
251 return ERR_PTR(-EINVAL);
253 ubi = ubi_get_device(ubi_num);
255 return ERR_PTR(-ENODEV);
257 spin_lock(&ubi->volumes_lock);
258 /* Walk all volumes of this UBI device */
259 for (i = 0; i < ubi->vtbl_slots; i++) {
260 struct ubi_volume *vol = ubi->volumes[i];
262 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
267 spin_unlock(&ubi->volumes_lock);
270 ret = ubi_open_volume(ubi_num, vol_id, mode);
272 ret = ERR_PTR(-ENODEV);
275 * We should put the UBI device even in case of success, because
276 * 'ubi_open_volume()' took a reference as well.
281 EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
285 * ubi_open_volume_path - open UBI volume by its character device node path.
286 * @pathname: volume character device node path
289 * This function is similar to 'ubi_open_volume()', but opens a volume the path
290 * to its character device node.
292 struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
294 int error, ubi_num, vol_id, mod;
298 dbg_gen("open volume %s, mode %d", pathname, mode);
300 if (!pathname || !*pathname)
301 return ERR_PTR(-EINVAL);
303 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
305 return ERR_PTR(error);
307 inode = path.dentry->d_inode;
309 ubi_num = ubi_major2num(imajor(inode));
310 vol_id = iminor(inode) - 1;
314 return ERR_PTR(-EINVAL);
315 if (vol_id >= 0 && ubi_num >= 0)
316 return ubi_open_volume(ubi_num, vol_id, mode);
317 return ERR_PTR(-ENODEV);
319 EXPORT_SYMBOL_GPL(ubi_open_volume_path);
323 * ubi_close_volume - close UBI volume.
324 * @desc: volume descriptor
326 void ubi_close_volume(struct ubi_volume_desc *desc)
328 struct ubi_volume *vol = desc->vol;
329 struct ubi_device *ubi = vol->ubi;
331 dbg_gen("close device %d, volume %d, mode %d",
332 ubi->ubi_num, vol->vol_id, desc->mode);
334 spin_lock(&ubi->volumes_lock);
335 switch (desc->mode) {
346 spin_unlock(&ubi->volumes_lock);
349 put_device(&vol->dev);
351 module_put(THIS_MODULE);
353 EXPORT_SYMBOL_GPL(ubi_close_volume);
356 * ubi_leb_read - read data.
357 * @desc: volume descriptor
358 * @lnum: logical eraseblock number to read from
359 * @buf: buffer where to store the read data
360 * @offset: offset within the logical eraseblock to read from
361 * @len: how many bytes to read
362 * @check: whether UBI has to check the read data's CRC or not.
364 * This function reads data from offset @offset of logical eraseblock @lnum and
365 * stores the data at @buf. When reading from static volumes, @check specifies
366 * whether the data has to be checked or not. If yes, the whole logical
367 * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
368 * checksum is per-eraseblock). So checking may substantially slow down the
369 * read speed. The @check argument is ignored for dynamic volumes.
371 * In case of success, this function returns zero. In case of failure, this
372 * function returns a negative error code.
374 * %-EBADMSG error code is returned:
375 * o for both static and dynamic volumes if MTD driver has detected a data
376 * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
377 * o for static volumes in case of data CRC mismatch.
379 * If the volume is damaged because of an interrupted update this function just
380 * returns immediately with %-EBADF error code.
382 int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
385 struct ubi_volume *vol = desc->vol;
386 struct ubi_device *ubi = vol->ubi;
387 int err, vol_id = vol->vol_id;
389 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
391 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
392 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
393 offset + len > vol->usable_leb_size)
396 if (vol->vol_type == UBI_STATIC_VOLUME) {
397 if (vol->used_ebs == 0)
398 /* Empty static UBI volume */
400 if (lnum == vol->used_ebs - 1 &&
401 offset + len > vol->last_eb_bytes)
410 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
411 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
412 ubi_warn("mark volume %d as corrupted", vol_id);
418 EXPORT_SYMBOL_GPL(ubi_leb_read);
421 * ubi_leb_write - write data.
422 * @desc: volume descriptor
423 * @lnum: logical eraseblock number to write to
424 * @buf: data to write
425 * @offset: offset within the logical eraseblock where to write
426 * @len: how many bytes to write
428 * This function writes @len bytes of data from @buf to offset @offset of
429 * logical eraseblock @lnum.
431 * This function takes care of physical eraseblock write failures. If write to
432 * the physical eraseblock write operation fails, the logical eraseblock is
433 * re-mapped to another physical eraseblock, the data is recovered, and the
434 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
436 * If all the data were successfully written, zero is returned. If an error
437 * occurred and UBI has not been able to recover from it, this function returns
438 * a negative error code. Note, in case of an error, it is possible that
439 * something was still written to the flash media, but that may be some
442 * If the volume is damaged because of an interrupted update this function just
443 * returns immediately with %-EBADF code.
445 int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
448 struct ubi_volume *vol = desc->vol;
449 struct ubi_device *ubi = vol->ubi;
450 int vol_id = vol->vol_id;
452 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
454 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
457 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
460 if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
461 offset + len > vol->usable_leb_size ||
462 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
471 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
473 EXPORT_SYMBOL_GPL(ubi_leb_write);
476 * ubi_leb_change - change logical eraseblock atomically.
477 * @desc: volume descriptor
478 * @lnum: logical eraseblock number to change
479 * @buf: data to write
480 * @len: how many bytes to write
482 * This function changes the contents of a logical eraseblock atomically. @buf
483 * has to contain new logical eraseblock data, and @len - the length of the
484 * data, which has to be aligned. The length may be shorter than the logical
485 * eraseblock size, ant the logical eraseblock may be appended to more times
486 * later on. This function guarantees that in case of an unclean reboot the old
487 * contents is preserved. Returns zero in case of success and a negative error
488 * code in case of failure.
490 int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
493 struct ubi_volume *vol = desc->vol;
494 struct ubi_device *ubi = vol->ubi;
495 int vol_id = vol->vol_id;
497 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
499 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
502 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
505 if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
506 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
515 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
517 EXPORT_SYMBOL_GPL(ubi_leb_change);
520 * ubi_leb_erase - erase logical eraseblock.
521 * @desc: volume descriptor
522 * @lnum: logical eraseblock number
524 * This function un-maps logical eraseblock @lnum and synchronously erases the
525 * correspondent physical eraseblock. Returns zero in case of success and a
526 * negative error code in case of failure.
528 * If the volume is damaged because of an interrupted update this function just
529 * returns immediately with %-EBADF code.
531 int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
533 struct ubi_volume *vol = desc->vol;
534 struct ubi_device *ubi = vol->ubi;
537 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
539 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
542 if (lnum < 0 || lnum >= vol->reserved_pebs)
548 err = ubi_eba_unmap_leb(ubi, vol, lnum);
552 return ubi_wl_flush(ubi, vol->vol_id, lnum);
554 EXPORT_SYMBOL_GPL(ubi_leb_erase);
557 * ubi_leb_unmap - un-map logical eraseblock.
558 * @desc: volume descriptor
559 * @lnum: logical eraseblock number
561 * This function un-maps logical eraseblock @lnum and schedules the
562 * corresponding physical eraseblock for erasure, so that it will eventually be
563 * physically erased in background. This operation is much faster than the
566 * Unlike erase, the un-map operation does not guarantee that the logical
567 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
568 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
569 * happens after this, the logical eraseblocks will not necessarily be
570 * un-mapped again when this MTD device is attached. They may actually be
571 * mapped to the same physical eraseblocks again. So, this function has to be
574 * In other words, when un-mapping a logical eraseblock, UBI does not store
575 * any information about this on the flash media, it just marks the logical
576 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
577 * eraseblock is physically erased, it will be mapped again to the same logical
578 * eraseblock when the MTD device is attached again.
580 * The main and obvious use-case of this function is when the contents of a
581 * logical eraseblock has to be re-written. Then it is much more efficient to
582 * first un-map it, then write new data, rather than first erase it, then write
583 * new data. Note, once new data has been written to the logical eraseblock,
584 * UBI guarantees that the old contents has gone forever. In other words, if an
585 * unclean reboot happens after the logical eraseblock has been un-mapped and
586 * then written to, it will contain the last written data.
588 * This function returns zero in case of success and a negative error code in
589 * case of failure. If the volume is damaged because of an interrupted update
590 * this function just returns immediately with %-EBADF code.
592 int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
594 struct ubi_volume *vol = desc->vol;
595 struct ubi_device *ubi = vol->ubi;
597 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
599 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
602 if (lnum < 0 || lnum >= vol->reserved_pebs)
608 return ubi_eba_unmap_leb(ubi, vol, lnum);
610 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
613 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
614 * @desc: volume descriptor
615 * @lnum: logical eraseblock number
617 * This function maps an un-mapped logical eraseblock @lnum to a physical
618 * eraseblock. This means, that after a successful invocation of this
619 * function the logical eraseblock @lnum will be empty (contain only %0xFF
620 * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
623 * This function returns zero in case of success, %-EBADF if the volume is
624 * damaged because of an interrupted update, %-EBADMSG if the logical
625 * eraseblock is already mapped, and other negative error codes in case of
628 int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
630 struct ubi_volume *vol = desc->vol;
631 struct ubi_device *ubi = vol->ubi;
633 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
635 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
638 if (lnum < 0 || lnum >= vol->reserved_pebs)
644 if (vol->eba_tbl[lnum] >= 0)
647 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
649 EXPORT_SYMBOL_GPL(ubi_leb_map);
652 * ubi_is_mapped - check if logical eraseblock is mapped.
653 * @desc: volume descriptor
654 * @lnum: logical eraseblock number
656 * This function checks if logical eraseblock @lnum is mapped to a physical
657 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
658 * mean it will still be un-mapped after the UBI device is re-attached. The
659 * logical eraseblock may become mapped to the physical eraseblock it was last
662 * This function returns %1 if the LEB is mapped, %0 if not, and a negative
663 * error code in case of failure. If the volume is damaged because of an
664 * interrupted update this function just returns immediately with %-EBADF error
667 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
669 struct ubi_volume *vol = desc->vol;
671 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
673 if (lnum < 0 || lnum >= vol->reserved_pebs)
679 return vol->eba_tbl[lnum] >= 0;
681 EXPORT_SYMBOL_GPL(ubi_is_mapped);
684 * ubi_sync - synchronize UBI device buffers.
685 * @ubi_num: UBI device to synchronize
687 * The underlying MTD device may cache data in hardware or in software. This
688 * function ensures the caches are flushed. Returns zero in case of success and
689 * a negative error code in case of failure.
691 int ubi_sync(int ubi_num)
693 struct ubi_device *ubi;
695 ubi = ubi_get_device(ubi_num);
703 EXPORT_SYMBOL_GPL(ubi_sync);
706 * ubi_flush - flush UBI work queue.
707 * @ubi_num: UBI device to flush work queue
708 * @vol_id: volume id to flush for
709 * @lnum: logical eraseblock number to flush for
711 * This function executes all pending works for a particular volume id / logical
712 * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
713 * a wildcard for all of the corresponding volume numbers or logical
714 * eraseblock numbers. It returns zero in case of success and a negative error
715 * code in case of failure.
717 int ubi_flush(int ubi_num, int vol_id, int lnum)
719 struct ubi_device *ubi;
722 ubi = ubi_get_device(ubi_num);
726 err = ubi_wl_flush(ubi, vol_id, lnum);
730 EXPORT_SYMBOL_GPL(ubi_flush);
733 BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
736 * ubi_register_volume_notifier - register a volume notifier.
737 * @nb: the notifier description object
738 * @ignore_existing: if non-zero, do not send "added" notification for all
739 * already existing volumes
741 * This function registers a volume notifier, which means that
742 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
743 * removed, re-sized, re-named, or updated. The first argument of the function
744 * is the notification type. The second argument is pointer to a
745 * &struct ubi_notification object which describes the notification event.
746 * Using UBI API from the volume notifier is prohibited.
748 * This function returns zero in case of success and a negative error code
749 * in case of failure.
751 int ubi_register_volume_notifier(struct notifier_block *nb,
756 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
763 * We are going to walk all UBI devices and all volumes, and
764 * notify the user about existing volumes by the %UBI_VOLUME_ADDED
765 * event. We have to lock the @ubi_devices_mutex to make sure UBI
766 * devices do not disappear.
768 mutex_lock(&ubi_devices_mutex);
769 ubi_enumerate_volumes(nb);
770 mutex_unlock(&ubi_devices_mutex);
774 EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
777 * ubi_unregister_volume_notifier - unregister the volume notifier.
778 * @nb: the notifier description object
780 * This function unregisters volume notifier @nm and returns zero in case of
781 * success and a negative error code in case of failure.
783 int ubi_unregister_volume_notifier(struct notifier_block *nb)
785 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
787 EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);