Lines Matching +full:ubi +full:- +full:volume +full:-

1 // SPDX-License-Identifier: GPL-2.0-or-later
8 /* This file mostly implements UBI kernel API functions */
16 #include "ubi.h"
19 * ubi_do_get_device_info - get information about UBI device.
20 * @ubi: UBI device description object
23 * This function is the same as 'ubi_get_device_info()', but it assumes the UBI
26 void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di) in ubi_do_get_device_info() argument
28 di->ubi_num = ubi->ubi_num; in ubi_do_get_device_info()
29 di->leb_size = ubi->leb_size; in ubi_do_get_device_info()
30 di->leb_start = ubi->leb_start; in ubi_do_get_device_info()
31 di->min_io_size = ubi->min_io_size; in ubi_do_get_device_info()
32 di->max_write_size = ubi->max_write_size; in ubi_do_get_device_info()
33 di->ro_mode = ubi->ro_mode; in ubi_do_get_device_info()
34 di->cdev = ubi->cdev.dev; in ubi_do_get_device_info()
39 * ubi_get_device_info - get information about UBI device.
40 * @ubi_num: UBI device number
43 * This function returns %0 in case of success, %-EINVAL if the UBI device
44 * number is invalid, and %-ENODEV if there is no such UBI device.
48 struct ubi_device *ubi; in ubi_get_device_info() local
51 return -EINVAL; in ubi_get_device_info()
52 ubi = ubi_get_device(ubi_num); in ubi_get_device_info()
53 if (!ubi) in ubi_get_device_info()
54 return -ENODEV; in ubi_get_device_info()
55 ubi_do_get_device_info(ubi, di); in ubi_get_device_info()
56 ubi_put_device(ubi); in ubi_get_device_info()
62 * ubi_do_get_volume_info - get information about UBI volume.
63 * @ubi: UBI device description object
64 * @vol: volume description object
67 void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_do_get_volume_info() argument
70 vi->vol_id = vol->vol_id; in ubi_do_get_volume_info()
71 vi->ubi_num = ubi->ubi_num; in ubi_do_get_volume_info()
72 vi->size = vol->reserved_pebs; in ubi_do_get_volume_info()
73 vi->used_bytes = vol->used_bytes; in ubi_do_get_volume_info()
74 vi->vol_type = vol->vol_type; in ubi_do_get_volume_info()
75 vi->corrupted = vol->corrupted; in ubi_do_get_volume_info()
76 vi->upd_marker = vol->upd_marker; in ubi_do_get_volume_info()
77 vi->alignment = vol->alignment; in ubi_do_get_volume_info()
78 vi->usable_leb_size = vol->usable_leb_size; in ubi_do_get_volume_info()
79 vi->name_len = vol->name_len; in ubi_do_get_volume_info()
80 vi->name = vol->name; in ubi_do_get_volume_info()
81 vi->cdev = vol->cdev.dev; in ubi_do_get_volume_info()
82 vi->dev = &vol->dev; in ubi_do_get_volume_info()
86 * ubi_get_volume_info - get information about UBI volume.
87 * @desc: volume descriptor
93 ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi); in ubi_get_volume_info()
98 * ubi_open_volume - open UBI volume.
99 * @ubi_num: UBI device number
100 * @vol_id: volume ID
103 * The @mode parameter specifies if the volume should be opened in read-only
104 * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
105 * nobody else will be able to open this volume. UBI allows to have many volume
108 * If a static volume is being opened for the first time since boot, it will be
112 * This function returns volume descriptor in case of success and a negative
119 struct ubi_device *ubi; in ubi_open_volume() local
122 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode); in ubi_open_volume()
125 return ERR_PTR(-EINVAL); in ubi_open_volume()
129 return ERR_PTR(-EINVAL); in ubi_open_volume()
132 * First of all, we have to get the UBI device to prevent its removal. in ubi_open_volume()
134 ubi = ubi_get_device(ubi_num); in ubi_open_volume()
135 if (!ubi) in ubi_open_volume()
136 return ERR_PTR(-ENODEV); in ubi_open_volume()
138 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) { in ubi_open_volume()
139 err = -EINVAL; in ubi_open_volume()
145 err = -ENOMEM; in ubi_open_volume()
149 err = -ENODEV; in ubi_open_volume()
153 spin_lock(&ubi->volumes_lock); in ubi_open_volume()
154 vol = ubi->volumes[vol_id]; in ubi_open_volume()
155 if (!vol || vol->is_dead) in ubi_open_volume()
158 err = -EBUSY; in ubi_open_volume()
161 if (vol->exclusive) in ubi_open_volume()
163 vol->readers += 1; in ubi_open_volume()
167 if (vol->exclusive || vol->writers > 0) in ubi_open_volume()
169 vol->writers += 1; in ubi_open_volume()
173 if (vol->exclusive || vol->writers || vol->readers || in ubi_open_volume()
174 vol->metaonly) in ubi_open_volume()
176 vol->exclusive = 1; in ubi_open_volume()
180 if (vol->metaonly || vol->exclusive) in ubi_open_volume()
182 vol->metaonly = 1; in ubi_open_volume()
185 get_device(&vol->dev); in ubi_open_volume()
186 vol->ref_count += 1; in ubi_open_volume()
187 spin_unlock(&ubi->volumes_lock); in ubi_open_volume()
189 desc->vol = vol; in ubi_open_volume()
190 desc->mode = mode; in ubi_open_volume()
192 mutex_lock(&ubi->ckvol_mutex); in ubi_open_volume()
193 if (!vol->checked && !vol->skip_check) { in ubi_open_volume()
194 /* This is the first open - check the volume */ in ubi_open_volume()
195 err = ubi_check_volume(ubi, vol_id); in ubi_open_volume()
197 mutex_unlock(&ubi->ckvol_mutex); in ubi_open_volume()
202 ubi_warn(ubi, "volume %d on UBI device %d is corrupted", in ubi_open_volume()
203 vol_id, ubi->ubi_num); in ubi_open_volume()
204 vol->corrupted = 1; in ubi_open_volume()
206 vol->checked = 1; in ubi_open_volume()
208 mutex_unlock(&ubi->ckvol_mutex); in ubi_open_volume()
213 spin_unlock(&ubi->volumes_lock); in ubi_open_volume()
218 ubi_err(ubi, "cannot open device %d, volume %d, error %d", in ubi_open_volume()
220 ubi_put_device(ubi); in ubi_open_volume()
226 * ubi_open_volume_nm - open UBI volume by name.
227 * @ubi_num: UBI device number
228 * @name: volume name
231 * This function is similar to 'ubi_open_volume()', but opens a volume by name.
236 int i, vol_id = -1, len; in ubi_open_volume_nm()
237 struct ubi_device *ubi; in ubi_open_volume_nm() local
240 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode); in ubi_open_volume_nm()
243 return ERR_PTR(-EINVAL); in ubi_open_volume_nm()
247 return ERR_PTR(-EINVAL); in ubi_open_volume_nm()
250 return ERR_PTR(-EINVAL); in ubi_open_volume_nm()
252 ubi = ubi_get_device(ubi_num); in ubi_open_volume_nm()
253 if (!ubi) in ubi_open_volume_nm()
254 return ERR_PTR(-ENODEV); in ubi_open_volume_nm()
256 spin_lock(&ubi->volumes_lock); in ubi_open_volume_nm()
257 /* Walk all volumes of this UBI device */ in ubi_open_volume_nm()
258 for (i = 0; i < ubi->vtbl_slots; i++) { in ubi_open_volume_nm()
259 struct ubi_volume *vol = ubi->volumes[i]; in ubi_open_volume_nm()
261 if (vol && len == vol->name_len && !strcmp(name, vol->name)) { in ubi_open_volume_nm()
266 spin_unlock(&ubi->volumes_lock); in ubi_open_volume_nm()
271 ret = ERR_PTR(-ENODEV); in ubi_open_volume_nm()
274 * We should put the UBI device even in case of success, because in ubi_open_volume_nm()
277 ubi_put_device(ubi); in ubi_open_volume_nm()
283 * ubi_get_num_by_path - get UBI device and volume number from device path
284 * @pathname: volume character device node path
285 * @ubi_num: pointer to UBI device number to be set
286 * @vol_id: pointer to UBI volume ID to be set
306 return -EINVAL; in ubi_get_num_by_path()
309 *vol_id = MINOR(stat.rdev) - 1; in ubi_get_num_by_path()
312 return -ENODEV; in ubi_get_num_by_path()
318 * ubi_open_volume_path - open UBI volume by its character device node path.
319 * @pathname: volume character device node path
322 * This function is similar to 'ubi_open_volume()', but opens a volume the path
329 dbg_gen("open volume %s, mode %d", pathname, mode); in ubi_open_volume_path()
332 return ERR_PTR(-EINVAL); in ubi_open_volume_path()
343 * ubi_close_volume - close UBI volume.
344 * @desc: volume descriptor
348 struct ubi_volume *vol = desc->vol; in ubi_close_volume()
349 struct ubi_device *ubi = vol->ubi; in ubi_close_volume() local
351 dbg_gen("close device %d, volume %d, mode %d", in ubi_close_volume()
352 ubi->ubi_num, vol->vol_id, desc->mode); in ubi_close_volume()
354 spin_lock(&ubi->volumes_lock); in ubi_close_volume()
355 switch (desc->mode) { in ubi_close_volume()
357 vol->readers -= 1; in ubi_close_volume()
360 vol->writers -= 1; in ubi_close_volume()
363 vol->exclusive = 0; in ubi_close_volume()
366 vol->metaonly = 0; in ubi_close_volume()
369 vol->ref_count -= 1; in ubi_close_volume()
370 spin_unlock(&ubi->volumes_lock); in ubi_close_volume()
373 put_device(&vol->dev); in ubi_close_volume()
374 ubi_put_device(ubi); in ubi_close_volume()
380 * leb_read_sanity_check - does sanity checks on read requests.
381 * @desc: volume descriptor
392 struct ubi_volume *vol = desc->vol; in leb_read_sanity_check()
393 struct ubi_device *ubi = vol->ubi; in leb_read_sanity_check() local
394 int vol_id = vol->vol_id; in leb_read_sanity_check()
396 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || in leb_read_sanity_check()
397 lnum >= vol->used_ebs || offset < 0 || len < 0 || in leb_read_sanity_check()
398 offset + len > vol->usable_leb_size) in leb_read_sanity_check()
399 return -EINVAL; in leb_read_sanity_check()
401 if (vol->vol_type == UBI_STATIC_VOLUME) { in leb_read_sanity_check()
402 if (vol->used_ebs == 0) in leb_read_sanity_check()
403 /* Empty static UBI volume */ in leb_read_sanity_check()
405 if (lnum == vol->used_ebs - 1 && in leb_read_sanity_check()
406 offset + len > vol->last_eb_bytes) in leb_read_sanity_check()
407 return -EINVAL; in leb_read_sanity_check()
410 if (vol->upd_marker) in leb_read_sanity_check()
411 return -EBADF; in leb_read_sanity_check()
417 * ubi_leb_read - read data.
418 * @desc: volume descriptor
423 * @check: whether UBI has to check the read data's CRC or not.
429 * checksum is per-eraseblock). So checking may substantially slow down the
435 * %-EBADMSG error code is returned:
440 * If the volume is damaged because of an interrupted update this function just
441 * returns immediately with %-EBADF error code.
446 struct ubi_volume *vol = desc->vol; in ubi_leb_read()
447 struct ubi_device *ubi = vol->ubi; in ubi_leb_read() local
448 int err, vol_id = vol->vol_id; in ubi_leb_read()
459 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check); in ubi_leb_read()
460 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) { in ubi_leb_read()
461 ubi_warn(ubi, "mark volume %d as corrupted", vol_id); in ubi_leb_read()
462 vol->corrupted = 1; in ubi_leb_read()
471 * ubi_leb_read_sg - read data into a scatter gather list.
472 * @desc: volume descriptor
474 * @sgl: UBI scatter gather list to store the read data
477 * @check: whether UBI has to check the read data's CRC or not.
480 * storing the read data into a buffer it writes to an UBI scatter gather
486 struct ubi_volume *vol = desc->vol; in ubi_leb_read_sg()
487 struct ubi_device *ubi = vol->ubi; in ubi_leb_read_sg() local
488 int err, vol_id = vol->vol_id; in ubi_leb_read_sg()
499 err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check); in ubi_leb_read_sg()
500 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) { in ubi_leb_read_sg()
501 ubi_warn(ubi, "mark volume %d as corrupted", vol_id); in ubi_leb_read_sg()
502 vol->corrupted = 1; in ubi_leb_read_sg()
510 * ubi_leb_write - write data.
511 * @desc: volume descriptor
522 * re-mapped to another physical eraseblock, the data is recovered, and the
523 * write finishes. UBI has a pool of reserved physical eraseblocks for this.
526 * occurred and UBI has not been able to recover from it, this function returns
531 * If the volume is damaged because of an interrupted update this function just
532 * returns immediately with %-EBADF code.
537 struct ubi_volume *vol = desc->vol; in ubi_leb_write()
538 struct ubi_device *ubi = vol->ubi; in ubi_leb_write() local
539 int vol_id = vol->vol_id; in ubi_leb_write()
543 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) in ubi_leb_write()
544 return -EINVAL; in ubi_leb_write()
546 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) in ubi_leb_write()
547 return -EROFS; in ubi_leb_write()
550 offset + len > vol->usable_leb_size || in ubi_leb_write()
551 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1)) in ubi_leb_write()
552 return -EINVAL; in ubi_leb_write()
554 if (vol->upd_marker) in ubi_leb_write()
555 return -EBADF; in ubi_leb_write()
560 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len); in ubi_leb_write()
565 * ubi_leb_change - change logical eraseblock atomically.
566 * @desc: volume descriptor
572 * has to contain new logical eraseblock data, and @len - the length of the
582 struct ubi_volume *vol = desc->vol; in ubi_leb_change()
583 struct ubi_device *ubi = vol->ubi; in ubi_leb_change() local
584 int vol_id = vol->vol_id; in ubi_leb_change()
588 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) in ubi_leb_change()
589 return -EINVAL; in ubi_leb_change()
591 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) in ubi_leb_change()
592 return -EROFS; in ubi_leb_change()
595 len > vol->usable_leb_size || len & (ubi->min_io_size - 1)) in ubi_leb_change()
596 return -EINVAL; in ubi_leb_change()
598 if (vol->upd_marker) in ubi_leb_change()
599 return -EBADF; in ubi_leb_change()
604 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len); in ubi_leb_change()
609 * ubi_leb_erase - erase logical eraseblock.
610 * @desc: volume descriptor
613 * This function un-maps logical eraseblock @lnum and synchronously erases the
617 * If the volume is damaged because of an interrupted update this function just
618 * returns immediately with %-EBADF code.
622 struct ubi_volume *vol = desc->vol; in ubi_leb_erase()
623 struct ubi_device *ubi = vol->ubi; in ubi_leb_erase() local
626 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum); in ubi_leb_erase()
628 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) in ubi_leb_erase()
629 return -EROFS; in ubi_leb_erase()
632 return -EINVAL; in ubi_leb_erase()
634 if (vol->upd_marker) in ubi_leb_erase()
635 return -EBADF; in ubi_leb_erase()
637 err = ubi_eba_unmap_leb(ubi, vol, lnum); in ubi_leb_erase()
641 return ubi_wl_flush(ubi, vol->vol_id, lnum); in ubi_leb_erase()
646 * ubi_leb_unmap - un-map logical eraseblock.
647 * @desc: volume descriptor
650 * This function un-maps logical eraseblock @lnum and schedules the
655 * Unlike erase, the un-map operation does not guarantee that the logical
656 * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
657 * example, if several logical eraseblocks are un-mapped, and an unclean reboot
659 * un-mapped again when this MTD device is attached. They may actually be
663 * In other words, when un-mapping a logical eraseblock, UBI does not store
665 * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
669 * The main and obvious use-case of this function is when the contents of a
670 * logical eraseblock has to be re-written. Then it is much more efficient to
671 * first un-map it, then write new data, rather than first erase it, then write
673 * UBI guarantees that the old contents has gone forever. In other words, if an
674 * unclean reboot happens after the logical eraseblock has been un-mapped and
678 * case of failure. If the volume is damaged because of an interrupted update
679 * this function just returns immediately with %-EBADF code.
683 struct ubi_volume *vol = desc->vol; in ubi_leb_unmap()
684 struct ubi_device *ubi = vol->ubi; in ubi_leb_unmap() local
686 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum); in ubi_leb_unmap()
688 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) in ubi_leb_unmap()
689 return -EROFS; in ubi_leb_unmap()
692 return -EINVAL; in ubi_leb_unmap()
694 if (vol->upd_marker) in ubi_leb_unmap()
695 return -EBADF; in ubi_leb_unmap()
697 return ubi_eba_unmap_leb(ubi, vol, lnum); in ubi_leb_unmap()
702 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
703 * @desc: volume descriptor
706 * This function maps an un-mapped logical eraseblock @lnum to a physical
712 * This function returns zero in case of success, %-EBADF if the volume is
713 * damaged because of an interrupted update, %-EBADMSG if the logical
719 struct ubi_volume *vol = desc->vol; in ubi_leb_map()
720 struct ubi_device *ubi = vol->ubi; in ubi_leb_map() local
722 dbg_gen("map LEB %d:%d", vol->vol_id, lnum); in ubi_leb_map()
724 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) in ubi_leb_map()
725 return -EROFS; in ubi_leb_map()
728 return -EINVAL; in ubi_leb_map()
730 if (vol->upd_marker) in ubi_leb_map()
731 return -EBADF; in ubi_leb_map()
734 return -EBADMSG; in ubi_leb_map()
736 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0); in ubi_leb_map()
741 * ubi_is_mapped - check if logical eraseblock is mapped.
742 * @desc: volume descriptor
746 * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
747 * mean it will still be un-mapped after the UBI device is re-attached. The
752 * error code in case of failure. If the volume is damaged because of an
753 * interrupted update this function just returns immediately with %-EBADF error
758 struct ubi_volume *vol = desc->vol; in ubi_is_mapped()
760 dbg_gen("test LEB %d:%d", vol->vol_id, lnum); in ubi_is_mapped()
763 return -EINVAL; in ubi_is_mapped()
765 if (vol->upd_marker) in ubi_is_mapped()
766 return -EBADF; in ubi_is_mapped()
773 * ubi_sync - synchronize UBI device buffers.
774 * @ubi_num: UBI device to synchronize
782 struct ubi_device *ubi; in ubi_sync() local
784 ubi = ubi_get_device(ubi_num); in ubi_sync()
785 if (!ubi) in ubi_sync()
786 return -ENODEV; in ubi_sync()
788 mtd_sync(ubi->mtd); in ubi_sync()
789 ubi_put_device(ubi); in ubi_sync()
795 * ubi_flush - flush UBI work queue.
796 * @ubi_num: UBI device to flush work queue
797 * @vol_id: volume id to flush for
800 * This function executes all pending works for a particular volume id / logical
802 * a wildcard for all of the corresponding volume numbers or logical
808 struct ubi_device *ubi; in ubi_flush() local
811 ubi = ubi_get_device(ubi_num); in ubi_flush()
812 if (!ubi) in ubi_flush()
813 return -ENODEV; in ubi_flush()
815 err = ubi_wl_flush(ubi, vol_id, lnum); in ubi_flush()
816 ubi_put_device(ubi); in ubi_flush()
824 * ubi_register_volume_notifier - register a volume notifier.
826 * @ignore_existing: if non-zero, do not send "added" notification for all
829 * This function registers a volume notifier, which means that
830 * 'nb->notifier_call()' will be invoked when an UBI volume is created,
831 * removed, re-sized, re-named, or updated. The first argument of the function
834 * Using UBI API from the volume notifier is prohibited.
851 * We are going to walk all UBI devices and all volumes, and in ubi_register_volume_notifier()
853 * event. We have to lock the @ubi_devices_mutex to make sure UBI in ubi_register_volume_notifier()
865 * ubi_unregister_volume_notifier - unregister the volume notifier.
868 * This function unregisters volume notifier @nm and returns zero in case of