Lines Matching +full:ubi +full:- +full:volume +full:-
1 // SPDX-License-Identifier: GPL-2.0-or-later
11 * This file includes UBI initialization and building of UBI devices.
13 * When UBI is initialized, it attaches all the MTD devices specified as the
15 * specified, UBI does not attach any MTD device, but it is possible to do
16 * later using the "UBI control device".
33 #include "ubi.h"
38 /* Maximum number of comma-separated items in the 'mtd=' parameter */
51 * struct mtd_dev_param - MTD device parameter description data structure.
54 * @ubi_num: UBI number
57 * @enable_fm: enable fastmap when value is non-zero
58 * @need_resv_pool: reserve pool->max_size pebs when value is none-zero
75 /* UBI module parameter to enable fastmap automatically on non-fastmap images */
80 /* Slab cache for wear-leveling entries */
83 /* UBI control character device */
90 /* All UBI devices in system */
93 /* Serializes UBI devices creations and removals */
96 /* Protects @ubi_devices, @ubi->ref_count and @ubi->is_dead */
99 /* "Show" method for files in '/<sysfs>/class/ubi/' */
100 /* UBI version attribute ('/<sysfs>/class/ubi/version') */
114 /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
123 /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
150 * ubi_volume_notify - send a volume change notification.
151 * @ubi: UBI device description object
152 * @vol: volume description object of the changed volume
155 * This is a helper function which notifies all subscribers about a volume
156 * change event (creation, removal, re-sizing, re-naming, updating). Returns
159 int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol, int ntype) in ubi_volume_notify() argument
164 ubi_do_get_device_info(ubi, &nt.di); in ubi_volume_notify()
165 ubi_do_get_volume_info(ubi, vol, &nt.vi); in ubi_volume_notify()
172 ret = ubi_update_fastmap(ubi); in ubi_volume_notify()
174 ubi_msg(ubi, "Unable to write a new fastmap: %i", ret); in ubi_volume_notify()
181 * ubi_notify_all - send a notification to all volumes.
182 * @ubi: UBI device description object
186 * This function walks all volumes of UBI device @ubi and sends the @ntype
187 * notification for each volume. If @nb is %NULL, then all registered notifiers
191 int ubi_notify_all(struct ubi_device *ubi, int ntype, struct notifier_block *nb) in ubi_notify_all() argument
196 ubi_do_get_device_info(ubi, &nt.di); in ubi_notify_all()
198 mutex_lock(&ubi->device_mutex); in ubi_notify_all()
199 for (i = 0; i < ubi->vtbl_slots; i++) { in ubi_notify_all()
201 * Since the @ubi->device is locked, and we are not going to in ubi_notify_all()
202 * change @ubi->volumes, we do not have to lock in ubi_notify_all()
203 * @ubi->volumes_lock. in ubi_notify_all()
205 if (!ubi->volumes[i]) in ubi_notify_all()
208 ubi_do_get_volume_info(ubi, ubi->volumes[i], &nt.vi); in ubi_notify_all()
210 nb->notifier_call(nb, ntype, &nt); in ubi_notify_all()
216 mutex_unlock(&ubi->device_mutex); in ubi_notify_all()
222 * ubi_enumerate_volumes - send "add" notification for all existing volumes.
225 * This function walks all UBI devices and volumes and sends the
226 * %UBI_VOLUME_ADDED notification for each volume. If @nb is %NULL, then all
239 struct ubi_device *ubi = ubi_devices[i]; in ubi_enumerate_volumes() local
241 if (!ubi) in ubi_enumerate_volumes()
243 count += ubi_notify_all(ubi, UBI_VOLUME_ADDED, nb); in ubi_enumerate_volumes()
250 * ubi_get_device - get UBI device.
251 * @ubi_num: UBI device number
253 * This function returns UBI device description object for UBI device number
260 struct ubi_device *ubi; in ubi_get_device() local
263 ubi = ubi_devices[ubi_num]; in ubi_get_device()
264 if (ubi && ubi->is_dead) in ubi_get_device()
265 ubi = NULL; in ubi_get_device()
267 if (ubi) { in ubi_get_device()
268 ubi_assert(ubi->ref_count >= 0); in ubi_get_device()
269 ubi->ref_count += 1; in ubi_get_device()
270 get_device(&ubi->dev); in ubi_get_device()
274 return ubi; in ubi_get_device()
278 * ubi_put_device - drop an UBI device reference.
279 * @ubi: UBI device description object
281 void ubi_put_device(struct ubi_device *ubi) in ubi_put_device() argument
284 ubi->ref_count -= 1; in ubi_put_device()
285 put_device(&ubi->dev); in ubi_put_device()
290 * ubi_get_by_major - get UBI device by character device major number.
299 struct ubi_device *ubi; in ubi_get_by_major() local
303 ubi = ubi_devices[i]; in ubi_get_by_major()
304 if (ubi && !ubi->is_dead && MAJOR(ubi->cdev.dev) == major) { in ubi_get_by_major()
305 ubi_assert(ubi->ref_count >= 0); in ubi_get_by_major()
306 ubi->ref_count += 1; in ubi_get_by_major()
307 get_device(&ubi->dev); in ubi_get_by_major()
309 return ubi; in ubi_get_by_major()
318 * ubi_major2num - get UBI device number by character device major number.
321 * This function searches UBI device number object by its major number. If UBI
322 * device was not found, this function returns -ENODEV, otherwise the UBI device
327 int i, ubi_num = -ENODEV; in ubi_major2num()
331 struct ubi_device *ubi = ubi_devices[i]; in ubi_major2num() local
333 if (ubi && !ubi->is_dead && MAJOR(ubi->cdev.dev) == major) { in ubi_major2num()
334 ubi_num = ubi->ubi_num; in ubi_major2num()
343 /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
348 struct ubi_device *ubi; in dev_attribute_show() local
352 * UBI device reference from the contained 'struct ubi_device'. But it in dev_attribute_show()
355 * 'ubi_get_device()' will return -ENODEV and we fail. in dev_attribute_show()
358 * we still can use 'ubi->ubi_num'. in dev_attribute_show()
360 ubi = container_of(dev, struct ubi_device, dev); in dev_attribute_show()
363 ret = sprintf(buf, "%d\n", ubi->leb_size); in dev_attribute_show()
365 ret = sprintf(buf, "%d\n", ubi->avail_pebs); in dev_attribute_show()
367 ret = sprintf(buf, "%d\n", ubi->good_peb_count); in dev_attribute_show()
369 ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT); in dev_attribute_show()
371 ret = sprintf(buf, "%d\n", ubi->max_ec); in dev_attribute_show()
373 ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs); in dev_attribute_show()
375 ret = sprintf(buf, "%d\n", ubi->bad_peb_count); in dev_attribute_show()
377 ret = sprintf(buf, "%d\n", ubi->vtbl_slots); in dev_attribute_show()
379 ret = sprintf(buf, "%d\n", ubi->min_io_size); in dev_attribute_show()
381 ret = sprintf(buf, "%d\n", ubi->thread_enabled); in dev_attribute_show()
383 ret = sprintf(buf, "%d\n", ubi->mtd->index); in dev_attribute_show()
385 ret = sprintf(buf, "%d\n", ubi->ro_mode); in dev_attribute_show()
387 ret = -EINVAL; in dev_attribute_show()
411 struct ubi_device *ubi = container_of(dev, struct ubi_device, dev); in dev_release() local
413 kfree(ubi); in dev_release()
417 * kill_volumes - destroy all user volumes.
418 * @ubi: UBI device description object
420 static void kill_volumes(struct ubi_device *ubi) in kill_volumes() argument
424 for (i = 0; i < ubi->vtbl_slots; i++) in kill_volumes()
425 if (ubi->volumes[i]) in kill_volumes()
426 ubi_free_volume(ubi, ubi->volumes[i]); in kill_volumes()
430 * uif_init - initialize user interfaces for an UBI device.
431 * @ubi: UBI device description object
433 * This function initializes various user interfaces for an UBI device. If the
440 static int uif_init(struct ubi_device *ubi) in uif_init() argument
445 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num); in uif_init()
448 * Major numbers for the UBI character devices are allocated in uif_init()
449 * dynamically. Major numbers of volume character devices are in uif_init()
450 * equivalent to ones of the corresponding UBI character device. Minor in uif_init()
451 * numbers of UBI character devices are 0, while minor numbers of in uif_init()
452 * volume character devices start from 1. Thus, we allocate one major in uif_init()
453 * number and ubi->vtbl_slots + 1 minor numbers. in uif_init()
455 err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name); in uif_init()
457 ubi_err(ubi, "cannot register UBI character devices"); in uif_init()
461 ubi->dev.devt = dev; in uif_init()
464 cdev_init(&ubi->cdev, &ubi_cdev_operations); in uif_init()
465 dbg_gen("%s major is %u", ubi->ubi_name, MAJOR(dev)); in uif_init()
466 ubi->cdev.owner = THIS_MODULE; in uif_init()
468 dev_set_name(&ubi->dev, UBI_NAME_STR "%d", ubi->ubi_num); in uif_init()
469 err = cdev_device_add(&ubi->cdev, &ubi->dev); in uif_init()
473 for (i = 0; i < ubi->vtbl_slots; i++) in uif_init()
474 if (ubi->volumes[i]) { in uif_init()
475 err = ubi_add_volume(ubi, ubi->volumes[i]); in uif_init()
477 ubi_err(ubi, "cannot add volume %d", i); in uif_init()
478 ubi->volumes[i] = NULL; in uif_init()
486 kill_volumes(ubi); in uif_init()
487 cdev_device_del(&ubi->cdev, &ubi->dev); in uif_init()
489 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1); in uif_init()
490 ubi_err(ubi, "cannot initialize UBI %s, error %d", in uif_init()
491 ubi->ubi_name, err); in uif_init()
496 * uif_close - close user interfaces for an UBI device.
497 * @ubi: UBI device description object
499 * Note, since this function un-registers UBI volume device objects (@vol->dev),
503 static void uif_close(struct ubi_device *ubi) in uif_close() argument
505 kill_volumes(ubi); in uif_close()
506 cdev_device_del(&ubi->cdev, &ubi->dev); in uif_close()
507 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1); in uif_close()
511 * ubi_free_volumes_from - free volumes from specific index.
512 * @ubi: UBI device description object
513 * @from: the start index used for volume free.
515 static void ubi_free_volumes_from(struct ubi_device *ubi, int from) in ubi_free_volumes_from() argument
519 for (i = from; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) { in ubi_free_volumes_from()
520 if (!ubi->volumes[i] || ubi->volumes[i]->is_dead) in ubi_free_volumes_from()
522 ubi_eba_replace_table(ubi->volumes[i], NULL); in ubi_free_volumes_from()
523 ubi_fastmap_destroy_checkmap(ubi->volumes[i]); in ubi_free_volumes_from()
524 kfree(ubi->volumes[i]); in ubi_free_volumes_from()
525 ubi->volumes[i] = NULL; in ubi_free_volumes_from()
530 * ubi_free_all_volumes - free all volumes.
531 * @ubi: UBI device description object
533 void ubi_free_all_volumes(struct ubi_device *ubi) in ubi_free_all_volumes() argument
535 ubi_free_volumes_from(ubi, 0); in ubi_free_all_volumes()
539 * ubi_free_internal_volumes - free internal volumes.
540 * @ubi: UBI device description object
542 void ubi_free_internal_volumes(struct ubi_device *ubi) in ubi_free_internal_volumes() argument
544 ubi_free_volumes_from(ubi, ubi->vtbl_slots); in ubi_free_internal_volumes()
547 static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024) in get_bad_peb_limit() argument
558 limit = mtd_max_bad_blocks(ubi->mtd, 0, ubi->mtd->size); in get_bad_peb_limit()
571 * the MTD partition we are attaching (ubi->mtd). in get_bad_peb_limit()
573 device_size = mtd_get_device_size(ubi->mtd); in get_bad_peb_limit()
574 device_pebs = mtd_div_by_eb(device_size, ubi->mtd); in get_bad_peb_limit()
585 * io_init - initialize I/O sub-system for a given UBI device.
586 * @ubi: UBI device description object
589 * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
591 * o EC header is always at offset zero - this cannot be changed;
593 * aligned to @io->hdrs_min_io_size;
595 * @io->min_io_size
600 static int io_init(struct ubi_device *ubi, int max_beb_per1024) in io_init() argument
605 if (ubi->mtd->numeraseregions != 0) { in io_init()
609 * characteristics. It looks like mostly multi-region flashes in io_init()
615 ubi_err(ubi, "multiple regions, not implemented"); in io_init()
616 return -EINVAL; in io_init()
619 if (ubi->vid_hdr_offset < 0) in io_init()
620 return -EINVAL; in io_init()
627 ubi->peb_size = ubi->mtd->erasesize; in io_init()
628 ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd); in io_init()
629 ubi->flash_size = ubi->mtd->size; in io_init()
631 if (mtd_can_have_bb(ubi->mtd)) { in io_init()
632 ubi->bad_allowed = 1; in io_init()
633 ubi->bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024); in io_init()
636 if (ubi->mtd->type == MTD_NORFLASH) in io_init()
637 ubi->nor_flash = 1; in io_init()
639 ubi->min_io_size = ubi->mtd->writesize; in io_init()
640 ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft; in io_init()
647 if (!is_power_of_2(ubi->min_io_size)) { in io_init()
648 ubi_err(ubi, "min. I/O unit (%d) is not power of 2", in io_init()
649 ubi->min_io_size); in io_init()
650 return -EINVAL; in io_init()
653 ubi_assert(ubi->hdrs_min_io_size > 0); in io_init()
654 ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size); in io_init()
655 ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0); in io_init()
657 ubi->max_write_size = ubi->mtd->writebufsize; in io_init()
662 if (ubi->max_write_size < ubi->min_io_size || in io_init()
663 ubi->max_write_size % ubi->min_io_size || in io_init()
664 !is_power_of_2(ubi->max_write_size)) { in io_init()
665 ubi_err(ubi, "bad write buffer size %d for %d min. I/O unit", in io_init()
666 ubi->max_write_size, ubi->min_io_size); in io_init()
667 return -EINVAL; in io_init()
671 ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size); in io_init()
672 ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size); in io_init()
674 dbg_gen("min_io_size %d", ubi->min_io_size); in io_init()
675 dbg_gen("max_write_size %d", ubi->max_write_size); in io_init()
676 dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size); in io_init()
677 dbg_gen("ec_hdr_alsize %d", ubi->ec_hdr_alsize); in io_init()
678 dbg_gen("vid_hdr_alsize %d", ubi->vid_hdr_alsize); in io_init()
680 if (ubi->vid_hdr_offset == 0) in io_init()
682 ubi->vid_hdr_offset = ubi->vid_hdr_aloffset = in io_init()
683 ubi->ec_hdr_alsize; in io_init()
685 ubi->vid_hdr_aloffset = ubi->vid_hdr_offset & in io_init()
686 ~(ubi->hdrs_min_io_size - 1); in io_init()
687 ubi->vid_hdr_shift = ubi->vid_hdr_offset - in io_init()
688 ubi->vid_hdr_aloffset; in io_init()
692 * Memory allocation for VID header is ubi->vid_hdr_alsize in io_init()
695 * ubi->vid_hdr_alsize, so that all vid header operations in io_init()
698 if ((ubi->vid_hdr_shift + UBI_VID_HDR_SIZE) > ubi->vid_hdr_alsize) { in io_init()
699 ubi_err(ubi, "Invalid VID header offset %d, VID header shift(%d)" in io_init()
701 ubi->vid_hdr_offset, ubi->vid_hdr_shift, in io_init()
702 UBI_VID_HDR_SIZE, ubi->vid_hdr_alsize); in io_init()
703 return -EINVAL; in io_init()
707 ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE; in io_init()
708 ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size); in io_init()
710 dbg_gen("vid_hdr_offset %d", ubi->vid_hdr_offset); in io_init()
711 dbg_gen("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset); in io_init()
712 dbg_gen("vid_hdr_shift %d", ubi->vid_hdr_shift); in io_init()
713 dbg_gen("leb_start %d", ubi->leb_start); in io_init()
715 /* The shift must be aligned to 32-bit boundary */ in io_init()
716 if (ubi->vid_hdr_shift % 4) { in io_init()
717 ubi_err(ubi, "unaligned VID header shift %d", in io_init()
718 ubi->vid_hdr_shift); in io_init()
719 return -EINVAL; in io_init()
723 if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE || in io_init()
724 ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE || in io_init()
725 ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE || in io_init()
726 ubi->leb_start & (ubi->min_io_size - 1)) { in io_init()
727 ubi_err(ubi, "bad VID header (%d) or data offsets (%d)", in io_init()
728 ubi->vid_hdr_offset, ubi->leb_start); in io_init()
729 return -EINVAL; in io_init()
736 ubi->max_erroneous = ubi->peb_count / 10; in io_init()
737 if (ubi->max_erroneous < 16) in io_init()
738 ubi->max_erroneous = 16; in io_init()
739 dbg_gen("max_erroneous %d", ubi->max_erroneous); in io_init()
743 * I/O unit. In this case we can only accept this UBI image in in io_init()
744 * read-only mode. in io_init()
746 if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) { in io_init()
747 ubi_warn(ubi, "EC and VID headers are in the same minimal I/O unit, switch to read-only mode"); in io_init()
748 ubi->ro_mode = 1; in io_init()
751 ubi->leb_size = ubi->peb_size - ubi->leb_start; in io_init()
753 if (!(ubi->mtd->flags & MTD_WRITEABLE)) { in io_init()
754 ubi_msg(ubi, "MTD device %d is write-protected, attach in read-only mode", in io_init()
755 ubi->mtd->index); in io_init()
756 ubi->ro_mode = 1; in io_init()
760 * Note, ideally, we have to initialize @ubi->bad_peb_count here. But in io_init()
762 * over all physical eraseblocks and invoke mtd->block_is_bad() for in io_init()
763 * each physical eraseblock. So, we leave @ubi->bad_peb_count in io_init()
771 * autoresize - re-size the volume which has the "auto-resize" flag set.
772 * @ubi: UBI device description object
773 * @vol_id: ID of the volume to re-size
775 * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
776 * the volume table to the largest possible size. See comments in ubi-header.h
780 static int autoresize(struct ubi_device *ubi, int vol_id) in autoresize() argument
783 struct ubi_volume *vol = ubi->volumes[vol_id]; in autoresize()
784 int err, old_reserved_pebs = vol->reserved_pebs; in autoresize()
786 if (ubi->ro_mode) { in autoresize()
787 ubi_warn(ubi, "skip auto-resize because of R/O mode"); in autoresize()
792 * Clear the auto-resize flag in the volume in-memory copy of the in autoresize()
793 * volume table, and 'ubi_resize_volume()' will propagate this change in autoresize()
796 ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG; in autoresize()
798 if (ubi->avail_pebs == 0) { in autoresize()
802 * No available PEBs to re-size the volume, clear the flag on in autoresize()
805 vtbl_rec = ubi->vtbl[vol_id]; in autoresize()
806 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); in autoresize()
808 ubi_err(ubi, "cannot clean auto-resize flag for volume %d", in autoresize()
813 old_reserved_pebs + ubi->avail_pebs); in autoresize()
815 ubi_err(ubi, "cannot auto-resize volume %d", in autoresize()
822 ubi_msg(ubi, "volume %d (\"%s\") re-sized from %d to %d LEBs", in autoresize()
823 vol_id, vol->name, old_reserved_pebs, vol->reserved_pebs); in autoresize()
828 * ubi_attach_mtd_dev - attach an MTD device.
830 * @ubi_num: number to assign to the new UBI device
836 * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
837 * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
839 * automatically. Returns the new UBI device number in case of success and a
842 * If @disable_fm is true, ubi doesn't create new fastmap even the module param
853 struct ubi_device *ubi; in ubi_attach_mtd_dev() local
857 return -EINVAL; in ubi_attach_mtd_dev()
865 * Note, this function assumes that UBI devices creations and deletions in ubi_attach_mtd_dev()
869 ubi = ubi_devices[i]; in ubi_attach_mtd_dev()
870 if (ubi && mtd->index == ubi->mtd->index) { in ubi_attach_mtd_dev()
871 pr_err("ubi: mtd%d is already attached to ubi%d\n", in ubi_attach_mtd_dev()
872 mtd->index, i); in ubi_attach_mtd_dev()
873 return -EEXIST; in ubi_attach_mtd_dev()
878 * Make sure this MTD device is not emulated on top of an UBI volume in ubi_attach_mtd_dev()
880 * different problems like the UBI module takes a reference to itself in ubi_attach_mtd_dev()
885 if (mtd->type == MTD_UBIVOLUME) { in ubi_attach_mtd_dev()
886 pr_err("ubi: refuse attaching mtd%d - it is already emulated on top of UBI\n", in ubi_attach_mtd_dev()
887 mtd->index); in ubi_attach_mtd_dev()
888 return -EINVAL; in ubi_attach_mtd_dev()
892 * Both UBI and UBIFS have been designed for SLC NAND and NOR flashes. in ubi_attach_mtd_dev()
893 * MLC NAND is different and needs special care, otherwise UBI or UBIFS in ubi_attach_mtd_dev()
898 if (mtd->type == MTD_MLCNANDFLASH && in ubi_attach_mtd_dev()
899 !(mtd->flags & MTD_SLC_ON_MLC_EMULATION)) { in ubi_attach_mtd_dev()
900 pr_err("ubi: refuse attaching mtd%d - MLC NAND is not supported\n", in ubi_attach_mtd_dev()
901 mtd->index); in ubi_attach_mtd_dev()
902 return -EINVAL; in ubi_attach_mtd_dev()
905 /* UBI cannot work on flashes with zero erasesize. */ in ubi_attach_mtd_dev()
906 if (!mtd->erasesize) { in ubi_attach_mtd_dev()
907 pr_err("ubi: refuse attaching mtd%d - zero erasesize flash is not supported\n", in ubi_attach_mtd_dev()
908 mtd->index); in ubi_attach_mtd_dev()
909 return -EINVAL; in ubi_attach_mtd_dev()
918 pr_err("ubi: only %d UBI devices may be created\n", in ubi_attach_mtd_dev()
920 return -ENFILE; in ubi_attach_mtd_dev()
924 return -EINVAL; in ubi_attach_mtd_dev()
928 pr_err("ubi: ubi%i already exists\n", ubi_num); in ubi_attach_mtd_dev()
929 return -EEXIST; in ubi_attach_mtd_dev()
933 ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL); in ubi_attach_mtd_dev()
934 if (!ubi) in ubi_attach_mtd_dev()
935 return -ENOMEM; in ubi_attach_mtd_dev()
937 device_initialize(&ubi->dev); in ubi_attach_mtd_dev()
938 ubi->dev.release = dev_release; in ubi_attach_mtd_dev()
939 ubi->dev.class = &ubi_class; in ubi_attach_mtd_dev()
940 ubi->dev.groups = ubi_dev_groups; in ubi_attach_mtd_dev()
941 ubi->dev.parent = &mtd->dev; in ubi_attach_mtd_dev()
943 ubi->mtd = mtd; in ubi_attach_mtd_dev()
944 ubi->ubi_num = ubi_num; in ubi_attach_mtd_dev()
945 ubi->vid_hdr_offset = vid_hdr_offset; in ubi_attach_mtd_dev()
946 ubi->autoresize_vol_id = -1; in ubi_attach_mtd_dev()
949 ubi->fm_pool.used = ubi->fm_pool.size = 0; in ubi_attach_mtd_dev()
950 ubi->fm_wl_pool.used = ubi->fm_wl_pool.size = 0; in ubi_attach_mtd_dev()
956 ubi->fm_pool.max_size = min(((int)mtd_div_by_eb(ubi->mtd->size, in ubi_attach_mtd_dev()
957 ubi->mtd) / 100) * 5, UBI_FM_MAX_POOL_SIZE); in ubi_attach_mtd_dev()
958 ubi->fm_pool.max_size = max(ubi->fm_pool.max_size, in ubi_attach_mtd_dev()
961 ubi->fm_wl_pool.max_size = ubi->fm_pool.max_size / 2; in ubi_attach_mtd_dev()
962 ubi->fm_pool_rsv_cnt = need_resv_pool ? ubi->fm_pool.max_size : 0; in ubi_attach_mtd_dev()
963 ubi->fm_disabled = (!fm_autoconvert || disable_fm) ? 1 : 0; in ubi_attach_mtd_dev()
965 ubi_enable_dbg_chk_fastmap(ubi); in ubi_attach_mtd_dev()
967 if (!ubi->fm_disabled && (int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd) in ubi_attach_mtd_dev()
969 ubi_err(ubi, "More than %i PEBs are needed for fastmap, sorry.", in ubi_attach_mtd_dev()
971 ubi->fm_disabled = 1; in ubi_attach_mtd_dev()
974 ubi_msg(ubi, "default fastmap pool size: %d", ubi->fm_pool.max_size); in ubi_attach_mtd_dev()
975 ubi_msg(ubi, "default fastmap WL pool size: %d", in ubi_attach_mtd_dev()
976 ubi->fm_wl_pool.max_size); in ubi_attach_mtd_dev()
978 ubi->fm_disabled = 1; in ubi_attach_mtd_dev()
980 mutex_init(&ubi->buf_mutex); in ubi_attach_mtd_dev()
981 mutex_init(&ubi->ckvol_mutex); in ubi_attach_mtd_dev()
982 mutex_init(&ubi->device_mutex); in ubi_attach_mtd_dev()
983 spin_lock_init(&ubi->volumes_lock); in ubi_attach_mtd_dev()
984 init_rwsem(&ubi->fm_protect); in ubi_attach_mtd_dev()
985 init_rwsem(&ubi->fm_eba_sem); in ubi_attach_mtd_dev()
987 ubi_msg(ubi, "attaching mtd%d", mtd->index); in ubi_attach_mtd_dev()
989 err = io_init(ubi, max_beb_per1024); in ubi_attach_mtd_dev()
993 err = -ENOMEM; in ubi_attach_mtd_dev()
994 ubi->peb_buf = vmalloc(ubi->peb_size); in ubi_attach_mtd_dev()
995 if (!ubi->peb_buf) in ubi_attach_mtd_dev()
999 ubi->fm_size = ubi_calc_fm_size(ubi); in ubi_attach_mtd_dev()
1000 ubi->fm_buf = vzalloc(ubi->fm_size); in ubi_attach_mtd_dev()
1001 if (!ubi->fm_buf) in ubi_attach_mtd_dev()
1004 err = ubi_attach(ubi, disable_fm ? 1 : 0); in ubi_attach_mtd_dev()
1006 ubi_err(ubi, "failed to attach mtd%d, error %d", in ubi_attach_mtd_dev()
1007 mtd->index, err); in ubi_attach_mtd_dev()
1011 if (ubi->autoresize_vol_id != -1) { in ubi_attach_mtd_dev()
1012 err = autoresize(ubi, ubi->autoresize_vol_id); in ubi_attach_mtd_dev()
1017 err = uif_init(ubi); in ubi_attach_mtd_dev()
1021 err = ubi_debugfs_init_dev(ubi); in ubi_attach_mtd_dev()
1025 ubi->bgt_thread = kthread_create(ubi_thread, ubi, "%s", ubi->bgt_name); in ubi_attach_mtd_dev()
1026 if (IS_ERR(ubi->bgt_thread)) { in ubi_attach_mtd_dev()
1027 err = PTR_ERR(ubi->bgt_thread); in ubi_attach_mtd_dev()
1028 ubi_err(ubi, "cannot spawn \"%s\", error %d", in ubi_attach_mtd_dev()
1029 ubi->bgt_name, err); in ubi_attach_mtd_dev()
1033 ubi_msg(ubi, "attached mtd%d (name \"%s\", size %llu MiB)", in ubi_attach_mtd_dev()
1034 mtd->index, mtd->name, ubi->flash_size >> 20); in ubi_attach_mtd_dev()
1035 ubi_msg(ubi, "PEB size: %d bytes (%d KiB), LEB size: %d bytes", in ubi_attach_mtd_dev()
1036 ubi->peb_size, ubi->peb_size >> 10, ubi->leb_size); in ubi_attach_mtd_dev()
1037 ubi_msg(ubi, "min./max. I/O unit sizes: %d/%d, sub-page size %d", in ubi_attach_mtd_dev()
1038 ubi->min_io_size, ubi->max_write_size, ubi->hdrs_min_io_size); in ubi_attach_mtd_dev()
1039 ubi_msg(ubi, "VID header offset: %d (aligned %d), data offset: %d", in ubi_attach_mtd_dev()
1040 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset, ubi->leb_start); in ubi_attach_mtd_dev()
1041 ubi_msg(ubi, "good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d", in ubi_attach_mtd_dev()
1042 ubi->good_peb_count, ubi->bad_peb_count, ubi->corr_peb_count); in ubi_attach_mtd_dev()
1043 ubi_msg(ubi, "user volume: %d, internal volumes: %d, max. volumes count: %d", in ubi_attach_mtd_dev()
1044 ubi->vol_count - UBI_INT_VOL_COUNT, UBI_INT_VOL_COUNT, in ubi_attach_mtd_dev()
1045 ubi->vtbl_slots); in ubi_attach_mtd_dev()
1046 ubi_msg(ubi, "max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u", in ubi_attach_mtd_dev()
1047 ubi->max_ec, ubi->mean_ec, CONFIG_MTD_UBI_WL_THRESHOLD, in ubi_attach_mtd_dev()
1048 ubi->image_seq); in ubi_attach_mtd_dev()
1049 ubi_msg(ubi, "available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d", in ubi_attach_mtd_dev()
1050 ubi->avail_pebs, ubi->rsvd_pebs, ubi->beb_rsvd_pebs); in ubi_attach_mtd_dev()
1054 * checks @ubi->thread_enabled. Otherwise we may fail to wake it up. in ubi_attach_mtd_dev()
1056 spin_lock(&ubi->wl_lock); in ubi_attach_mtd_dev()
1057 ubi->thread_enabled = 1; in ubi_attach_mtd_dev()
1058 wake_up_process(ubi->bgt_thread); in ubi_attach_mtd_dev()
1059 spin_unlock(&ubi->wl_lock); in ubi_attach_mtd_dev()
1061 ubi_devices[ubi_num] = ubi; in ubi_attach_mtd_dev()
1062 ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL); in ubi_attach_mtd_dev()
1066 ubi_debugfs_exit_dev(ubi); in ubi_attach_mtd_dev()
1068 uif_close(ubi); in ubi_attach_mtd_dev()
1070 ubi_wl_close(ubi); in ubi_attach_mtd_dev()
1071 ubi_free_all_volumes(ubi); in ubi_attach_mtd_dev()
1072 vfree(ubi->vtbl); in ubi_attach_mtd_dev()
1074 vfree(ubi->peb_buf); in ubi_attach_mtd_dev()
1075 vfree(ubi->fm_buf); in ubi_attach_mtd_dev()
1076 put_device(&ubi->dev); in ubi_attach_mtd_dev()
1081 * ubi_detach_mtd_dev - detach an MTD device.
1082 * @ubi_num: UBI device number to detach from
1085 * This function destroys an UBI device number @ubi_num and detaches the
1086 * underlying MTD device. Returns zero in case of success and %-EBUSY if the
1087 * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
1095 struct ubi_device *ubi; in ubi_detach_mtd_dev() local
1098 return -EINVAL; in ubi_detach_mtd_dev()
1100 ubi = ubi_get_device(ubi_num); in ubi_detach_mtd_dev()
1101 if (!ubi) in ubi_detach_mtd_dev()
1102 return -EINVAL; in ubi_detach_mtd_dev()
1105 ubi->ref_count -= 1; in ubi_detach_mtd_dev()
1106 if (ubi->ref_count) { in ubi_detach_mtd_dev()
1109 return -EBUSY; in ubi_detach_mtd_dev()
1112 ubi_err(ubi, "%s reference count %d, destroy anyway", in ubi_detach_mtd_dev()
1113 ubi->ubi_name, ubi->ref_count); in ubi_detach_mtd_dev()
1115 ubi->is_dead = true; in ubi_detach_mtd_dev()
1118 ubi_notify_all(ubi, UBI_VOLUME_SHUTDOWN, NULL); in ubi_detach_mtd_dev()
1121 put_device(&ubi->dev); in ubi_detach_mtd_dev()
1125 ubi_assert(ubi_num == ubi->ubi_num); in ubi_detach_mtd_dev()
1126 ubi_notify_all(ubi, UBI_VOLUME_REMOVED, NULL); in ubi_detach_mtd_dev()
1127 ubi_msg(ubi, "detaching mtd%d", ubi->mtd->index); in ubi_detach_mtd_dev()
1133 if (!ubi_dbg_chk_fastmap(ubi)) in ubi_detach_mtd_dev()
1134 ubi_update_fastmap(ubi); in ubi_detach_mtd_dev()
1140 if (ubi->bgt_thread) in ubi_detach_mtd_dev()
1141 kthread_stop(ubi->bgt_thread); in ubi_detach_mtd_dev()
1144 cancel_work_sync(&ubi->fm_work); in ubi_detach_mtd_dev()
1146 ubi_debugfs_exit_dev(ubi); in ubi_detach_mtd_dev()
1147 uif_close(ubi); in ubi_detach_mtd_dev()
1149 ubi_wl_close(ubi); in ubi_detach_mtd_dev()
1150 ubi_free_internal_volumes(ubi); in ubi_detach_mtd_dev()
1151 vfree(ubi->vtbl); in ubi_detach_mtd_dev()
1152 vfree(ubi->peb_buf); in ubi_detach_mtd_dev()
1153 vfree(ubi->fm_buf); in ubi_detach_mtd_dev()
1154 ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index); in ubi_detach_mtd_dev()
1155 put_mtd_device(ubi->mtd); in ubi_detach_mtd_dev()
1156 put_device(&ubi->dev); in ubi_detach_mtd_dev()
1161 * open_mtd_by_chdev - open an MTD device by its character device node path.
1186 return ERR_PTR(-EINVAL); in open_mtd_by_chdev()
1195 return ERR_PTR(-EINVAL); in open_mtd_by_chdev()
1201 * open_mtd_device - open MTD device by name, character device path, or number.
1223 if (PTR_ERR(mtd) == -ENODEV) in open_mtd_device()
1237 if (!of_device_is_compatible(np, "linux,ubi")) in ubi_notify_add()
1277 mtd = open_mtd_device(p->name); in ubi_init_attach()
1280 pr_err("UBI error: cannot open mtd %s, error %d\n", in ubi_init_attach()
1281 p->name, err); in ubi_init_attach()
1282 /* See comment below re-ubi_is_module(). */ in ubi_init_attach()
1289 err = ubi_attach_mtd_dev(mtd, p->ubi_num, in ubi_init_attach()
1290 p->vid_hdr_offs, p->max_beb_per1024, in ubi_init_attach()
1291 p->enable_fm == 0, in ubi_init_attach()
1292 p->need_resv_pool != 0); in ubi_init_attach()
1295 pr_err("UBI error: cannot attach mtd%d\n", in ubi_init_attach()
1296 mtd->index); in ubi_init_attach()
1300 * Originally UBI stopped initializing on any error. in ubi_init_attach()
1302 * behavior is not very good when UBI is compiled into in ubi_init_attach()
1304 * through the command line. Indeed, UBI failure in ubi_init_attach()
1308 * non-module case, but preserved the old behavior for in ubi_init_attach()
1323 ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1); in ubi_init_attach()
1341 pr_err("UBI error: too many MTD devices, maximum is %d\n", in ubi_init()
1343 return -EINVAL; in ubi_init()
1353 pr_err("UBI error: cannot register device\n"); in ubi_init()
1361 err = -ENOMEM; in ubi_init()
1371 pr_err("UBI error: block: cannot initialize, error %d\n", err); in ubi_init()
1373 /* See comment above re-ubi_is_module(). */ in ubi_init()
1399 pr_err("UBI error: cannot initialize UBI, error %d\n", err); in ubi_init()
1415 ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1); in ubi_exit()
1426 * bytes_str_to_int - convert a number of bytes string into an integer.
1439 pr_err("UBI error: incorrect bytes count: \"%s\"\n", str); in bytes_str_to_int()
1440 return -EINVAL; in bytes_str_to_int()
1456 pr_err("UBI error: incorrect bytes count: \"%s\"\n", str); in bytes_str_to_int()
1457 return -EINVAL; in bytes_str_to_int()
1464 * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
1480 return -EINVAL; in ubi_mtd_param_parse()
1483 pr_err("UBI error: too many parameters, max. is %d\n", in ubi_mtd_param_parse()
1485 return -EINVAL; in ubi_mtd_param_parse()
1490 pr_err("UBI error: parameter \"%s\" is too long, max. is %d\n", in ubi_mtd_param_parse()
1492 return -EINVAL; in ubi_mtd_param_parse()
1496 pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n"); in ubi_mtd_param_parse()
1503 if (buf[len - 1] == '\n') in ubi_mtd_param_parse()
1504 buf[len - 1] = '\0'; in ubi_mtd_param_parse()
1510 pr_err("UBI error: too many arguments at \"%s\"\n", val); in ubi_mtd_param_parse()
1511 return -EINVAL; in ubi_mtd_param_parse()
1515 strcpy(&p->name[0], tokens[0]); in ubi_mtd_param_parse()
1519 p->vid_hdr_offs = bytes_str_to_int(token); in ubi_mtd_param_parse()
1521 if (p->vid_hdr_offs < 0) in ubi_mtd_param_parse()
1522 return p->vid_hdr_offs; in ubi_mtd_param_parse()
1527 int err = kstrtoint(token, 10, &p->max_beb_per1024); in ubi_mtd_param_parse()
1530 pr_err("UBI error: bad value for max_beb_per1024 parameter: %s\n", in ubi_mtd_param_parse()
1532 return -EINVAL; in ubi_mtd_param_parse()
1538 int err = kstrtoint(token, 10, &p->ubi_num); in ubi_mtd_param_parse()
1541 pr_err("UBI error: bad value for ubi_num parameter: %s\n", in ubi_mtd_param_parse()
1543 return -EINVAL; in ubi_mtd_param_parse()
1546 p->ubi_num = UBI_DEV_NUM_AUTO; in ubi_mtd_param_parse()
1550 int err = kstrtoint(token, 10, &p->enable_fm); in ubi_mtd_param_parse()
1553 pr_err("UBI error: bad value for enable_fm parameter: %s\n", in ubi_mtd_param_parse()
1555 return -EINVAL; in ubi_mtd_param_parse()
1558 p->enable_fm = 0; in ubi_mtd_param_parse()
1562 int err = kstrtoint(token, 10, &p->need_resv_pool); in ubi_mtd_param_parse()
1565 pr_err("UBI error: bad value for need_resv_pool parameter: %s\n", in ubi_mtd_param_parse()
1567 return -EINVAL; in ubi_mtd_param_parse()
1570 p->need_resv_pool = 0; in ubi_mtd_param_parse()
1580 …"Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default…
1583 …"Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly…
1584 …eter determines whether to enable fastmap during attach. If the value is non-zero, fastmap is enab…
1585 …pool\" parameter determines whether to reserve pool->max_size pebs during attach. If the value is …
1587 "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
1588 …"Example 2: mtd=content,1984 mtd=4 - attach MTD device with name \"content\" using VID header offs…
1589 …"Example 3: mtd=/dev/mtd1,0,25 - attach MTD device /dev/mtd1 using default VID header offset and r…
1590 …"Example 4: mtd=/dev/mtd1,0,0,5 - attach MTD device /dev/mtd1 to UBI 5 and using default values fo…
1591 …5: mtd=1,0,0,5 mtd=2,0,0,6,1 - attach MTD device /dev/mtd1 to UBI 5 and disable fastmap; attach MT…
1592 "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
1600 MODULE_DESCRIPTION("UBI - Unsorted Block Images");