33 #include <linux/module.h>
37 #include <linux/stat.h>
42 #include <linux/kernel.h>
43 #include <linux/slab.h>
47 #define MTD_PARAM_LEN_MAX 64
50 #define MTD_PARAM_MAX_COUNT 3
53 #define MAX_MTD_UBI_BEB_LIMIT 768
55 #ifdef CONFIG_MTD_UBI_MODULE
56 #define ubi_is_module() 1
58 #define ubi_is_module() 0
79 #ifdef CONFIG_MTD_UBI_FASTMAP
81 static bool fm_autoconvert;
106 static ssize_t ubi_version_show(
struct class *
class,
160 #ifdef CONFIG_MTD_UBI_FASTMAP
167 ubi_err(
"Unable to update fastmap!");
257 spin_lock(&ubi_devices_lock);
264 spin_unlock(&ubi_devices_lock);
275 spin_lock(&ubi_devices_lock);
278 spin_unlock(&ubi_devices_lock);
293 spin_lock(&ubi_devices_lock);
295 ubi = ubi_devices[
i];
296 if (ubi &&
MAJOR(ubi->
cdev.dev) == major) {
300 spin_unlock(&ubi_devices_lock);
304 spin_unlock(&ubi_devices_lock);
321 spin_lock(&ubi_devices_lock);
325 if (ubi &&
MAJOR(ubi->
cdev.dev) == major) {
330 spin_unlock(&ubi_devices_lock);
357 if (attr == &dev_eraseblock_size)
359 else if (attr == &dev_avail_eraseblocks)
361 else if (attr == &dev_total_eraseblocks)
363 else if (attr == &dev_volumes_count)
365 else if (attr == &dev_max_ec)
367 else if (attr == &dev_reserved_for_bad)
369 else if (attr == &dev_bad_peb_count)
371 else if (attr == &dev_max_vol_count)
373 else if (attr == &dev_min_io_size)
375 else if (attr == &dev_bgt_enabled)
377 else if (attr == &dev_mtd_num)
386 static void dev_release(
struct device *dev)
402 static int ubi_sysfs_init(
struct ubi_device *ubi,
int *ref)
406 ubi->
dev.release = dev_release;
453 static void ubi_sysfs_close(
struct ubi_device *ubi)
473 static void kill_volumes(
struct ubi_device *ubi)
500 static int uif_init(
struct ubi_device *ubi,
int *ref)
518 ubi_err(
"cannot register UBI character devices");
529 ubi_err(
"cannot add character device");
533 err = ubi_sysfs_init(ubi, ref);
541 ubi_err(
"cannot add volume %d", i);
553 ubi_sysfs_close(ubi);
572 ubi_sysfs_close(ubi);
592 static int get_bad_peb_limit(
const struct ubi_device *ubi,
int max_beb_per1024)
594 int limit, device_pebs;
597 if (!max_beb_per1024)
610 device_pebs = mtd_div_by_eb(device_size, ubi->
mtd);
611 limit =
mult_frac(device_pebs, max_beb_per1024, 1024);
614 if (
mult_frac(limit, 1024, max_beb_per1024) < device_pebs)
636 static int io_init(
struct ubi_device *ubi,
int max_beb_per1024)
641 if (ubi->
mtd->numeraseregions != 0) {
651 ubi_err(
"multiple regions, not implemented");
667 if (mtd_can_have_bb(ubi->
mtd)) {
669 ubi->
bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024);
686 ubi_err(
"min. I/O unit (%d) is not power of 2",
703 ubi_err(
"bad write buffer size %d for %d min. I/O unit",
740 ubi_err(
"unaligned VID header shift %d",
750 ubi_err(
"bad VID header (%d) or data offsets (%d)",
770 ubi_warn(
"EC and VID headers are in the same minimal I/O unit, switch to read-only mode");
777 ubi_msg(
"MTD device %d is write-protected, attach in read-only mode",
810 ubi_warn(
"skip auto-resize because of R/O mode");
832 ubi_err(
"cannot clean auto-resize flag for volume %d",
839 ubi_err(
"cannot auto-resize volume %d", vol_id);
845 ubi_msg(
"volume %d (\"%s\") re-sized from %d to %d LEBs", vol_id,
867 int vid_hdr_offset,
int max_beb_per1024)
875 if (!max_beb_per1024)
876 max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
885 ubi = ubi_devices[
i];
886 if (ubi && mtd->
index == ubi->
mtd->index) {
887 ubi_err(
"mtd%d is already attached to ubi%d",
902 ubi_err(
"refuse attaching mtd%d - it is already emulated on top of UBI",
910 if (!ubi_devices[ubi_num])
912 if (ubi_num == UBI_MAX_DEVICES) {
913 ubi_err(
"only %d UBI devices may be created",
918 if (ubi_num >= UBI_MAX_DEVICES)
922 if (ubi_devices[ubi_num]) {
923 ubi_err(
"ubi%d already exists", ubi_num);
937 #ifdef CONFIG_MTD_UBI_FASTMAP
945 ubi->
fm_pool.max_size =
min(((
int)mtd_div_by_eb(ubi->
mtd->size,
955 ubi_err(
"More than %i PEBs are needed for fastmap, sorry.",
972 ubi_msg(
"attaching mtd%d to ubi%d", mtd->
index, ubi_num);
974 err = io_init(ubi, max_beb_per1024);
983 #ifdef CONFIG_MTD_UBI_FASTMAP
995 ubi_err(
"failed to attach mtd%d, error %d", mtd->
index, err);
1005 err = uif_init(ubi, &ref);
1021 ubi_msg(
"attached mtd%d (name \"%s\", size %llu MiB) to ubi%d",
1023 ubi_msg(
"PEB size: %d bytes (%d KiB), LEB size: %d bytes",
1025 ubi_msg(
"min./max. I/O unit sizes: %d/%d, sub-page size %d",
1027 ubi_msg(
"VID header offset: %d (aligned %d), data offset: %d",
1029 ubi_msg(
"good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
1031 ubi_msg(
"user volume: %d, internal volumes: %d, max. volumes count: %d",
1034 ubi_msg(
"max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
1037 ubi_msg(
"available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
1099 spin_lock(&ubi_devices_lock);
1104 spin_unlock(&ubi_devices_lock);
1108 ubi_err(
"%s reference count %d, destroy anyway",
1112 spin_unlock(&ubi_devices_lock);
1116 ubi_msg(
"detaching mtd%d from ubi%d", ubi->
mtd->index, ubi_num);
1117 #ifdef CONFIG_MTD_UBI_FASTMAP
1158 static struct mtd_info *
__init open_mtd_by_chdev(
const char *mtd_dev)
1166 return ERR_PTR(err);
1196 static struct mtd_info *
__init open_mtd_device(
const char *mtd_dev)
1203 if (*endp !=
'\0' || mtd_dev == endp) {
1209 if (IS_ERR(mtd) && PTR_ERR(mtd) == -
ENODEV)
1211 mtd = open_mtd_by_chdev(mtd_dev);
1218 static int __init ubi_init(
void)
1233 if (IS_ERR(ubi_class)) {
1234 err = PTR_ERR(ubi_class);
1235 ubi_err(
"cannot create UBI class");
1241 ubi_err(
"cannot create sysfs file");
1247 ubi_err(
"cannot register device");
1254 if (!ubi_wl_entry_slab)
1263 for (i = 0; i < mtd_devs; i++) {
1269 mtd = open_mtd_device(p->
name);
1304 for (k = 0; k <
i; k++)
1305 if (ubi_devices[k]) {
1320 ubi_err(
"UBI error: cannot initialize UBI, error %d", err);
1325 static void __exit ubi_exit(
void)
1330 if (ubi_devices[i]) {
1350 static int __init bytes_str_to_int(
const char *
str)
1356 if (str == endp || result >=
INT_MAX) {
1357 ubi_err(
"UBI error: incorrect bytes count: \"%s\"\n", str);
1368 if (endp[1] ==
'i' && endp[2] ==
'B')
1373 ubi_err(
"UBI error: incorrect bytes count: \"%s\"\n", str);
1393 char *pbuf = &buf[0];
1399 if (mtd_devs == UBI_MAX_DEVICES) {
1400 ubi_err(
"UBI error: too many parameters, max. is %d\n",
1407 ubi_err(
"UBI error: parameter \"%s\" is too long, max. is %d\n",
1413 pr_warn(
"UBI warning: empty 'mtd=' parameter - ignored\n");
1420 if (buf[len - 1] ==
'\n')
1421 buf[len - 1] =
'\0';
1424 tokens[i] =
strsep(&pbuf,
",");
1427 ubi_err(
"UBI error: too many arguments at \"%s\"\n", val);
1444 ubi_err(
"UBI error: bad value for max_beb_per1024 parameter: %s",
1455 MODULE_PARM_DESC(mtd,
"MTD devices to attach. Parameter format: mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024]].\n"
1456 "Multiple \"mtd\" parameters may be specified.\n"
1457 "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
1458 "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
1459 "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
1462 "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
1463 "Example 2: mtd=content,1984 mtd=4 - attach MTD device with name \"content\" using VID header offset 1984, and MTD device number 4 with default VID header offset.\n"
1464 "Example 3: mtd=/dev/mtd1,0,25 - attach MTD device /dev/mtd1 using default VID header offset and reserve 25*nand_size_in_blocks/1024 erase blocks for bad block handling.\n"
1465 "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
1466 #ifdef CONFIG_MTD_UBI_FASTMAP
1468 MODULE_PARM_DESC(fm_autoconvert,
"Set this parameter to enable fastmap automatically on images without a fastmap.");