| 1 | |
| 2 | |
| 3 | * Copyright (c) International Business Machines Corp., 2006
|
| 4 | * Copyright (c) Nokia Corporation, 2007
|
| 5 | *
|
| 6 | * Author: Artem Bityutskiy (Битюцкий Артём),
|
| 7 | * Frank Haverkamp
|
| 8 |
|
| 9 |
|
| 10 | |
| 11 | * This file includes UBI initialization and building of UBI devices.
|
| 12 | *
|
| 13 | * When UBI is initialized, it attaches all the MTD devices specified as the
|
| 14 | * module load parameters or the kernel boot parameters. If MTD devices were
|
| 15 | * specified, UBI does not attach any MTD device, but it is possible to do
|
| 16 | * later using the "UBI control device".
|
| 17 |
|
| 18 |
|
| 19 | #include <linux/err.h>
|
| 20 | #include <linux/module.h>
|
| 21 | #include <linux/moduleparam.h>
|
| 22 | #include <linux/stringify.h>
|
| 23 | #include <linux/namei.h>
|
| 24 | #include <linux/stat.h>
|
| 25 | #include <linux/miscdevice.h>
|
| 26 | #include <linux/mtd/partitions.h>
|
| 27 | #include <linux/log2.h>
|
| 28 | #include <linux/kthread.h>
|
| 29 | #include <linux/kernel.h>
|
| 30 | #include <linux/of.h>
|
| 31 | #include <linux/slab.h>
|
| 32 | #include <linux/major.h>
|
| 33 | #include "ubi.h"
|
| 34 |
|
| 35 |
|
| 36 | #define MTD_PARAM_LEN_MAX 64
|
| 37 |
|
| 38 |
|
| 39 | #define MTD_PARAM_MAX_COUNT 7
|
| 40 |
|
| 41 |
|
| 42 | #define MAX_MTD_UBI_BEB_LIMIT 768
|
| 43 |
|
| 44 | #ifdef CONFIG_MTD_UBI_MODULE
|
| 45 | #define ubi_is_module() 1
|
| 46 | #else
|
| 47 | #define ubi_is_module() 0
|
| 48 | #endif
|
| 49 |
|
| 50 | |
| 51 | * struct mtd_dev_param - MTD device parameter description data structure.
|
| 52 | * @name: MTD character device node path, MTD device name, or MTD device number
|
| 53 | * string
|
| 54 | * @ubi_num: UBI number
|
| 55 | * @vid_hdr_offs: VID header offset
|
| 56 | * @max_beb_per1024: maximum expected number of bad PEBs per 1024 PEBs
|
| 57 | * @enable_fm: enable fastmap when value is non-zero
|
| 58 | * @need_resv_pool: reserve pool->max_size pebs when value is none-zero
|
| 59 | * @wl_threshold: wear-leveling threshold, 0 means use CONFIG_MTD_UBI_WL_THRESHOLD
|
| 60 |
|
| 61 | struct mtd_dev_param {
|
| 62 | char name[MTD_PARAM_LEN_MAX];
|
| 63 | int ubi_num;
|
| 64 | int vid_hdr_offs;
|
| 65 | int max_beb_per1024;
|
| 66 | int enable_fm;
|
| 67 | int need_resv_pool;
|
| 68 | int wl_threshold;
|
| 69 | };
|
| 70 |
|
| 71 |
|
| 72 | static int mtd_devs;
|
| 73 |
|
| 74 |
|
| 75 | static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES];
|
| 76 | #ifdef CONFIG_MTD_UBI_FASTMAP
|
| 77 |
|
| 78 | static bool fm_autoconvert;
|
| 79 | static bool fm_debug;
|
| 80 | #endif
|
| 81 |
|
| 82 |
|
| 83 | struct kmem_cache *ubi_wl_entry_slab;
|
| 84 |
|
| 85 |
|
| 86 | static struct miscdevice ubi_ctrl_cdev = {
|
| 87 | .minor = MISC_DYNAMIC_MINOR,
|
| 88 | .name = "ubi_ctrl",
|
| 89 | .fops = &ubi_ctrl_cdev_operations,
|
| 90 | };
|
| 91 |
|
| 92 |
|
| 93 | static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
|
| 94 |
|
| 95 |
|
| 96 | DEFINE_MUTEX(ubi_devices_mutex);
|
| 97 |
|
| 98 |
|
| 99 | static DEFINE_SPINLOCK(ubi_devices_lock);
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 | static ssize_t version_show(const struct class *class, const struct class_attribute *attr,
|
| 104 | char *buf)
|
| 105 | {
|
| 106 | return sprintf(buf, "%d\n", UBI_VERSION);
|
| 107 | }
|
| 108 | static CLASS_ATTR_RO(version);
|
| 109 |
|
| 110 | static struct attribute *ubi_class_attrs[] = {
|
| 111 | &class_attr_version.attr,
|
| 112 | NULL,
|
| 113 | };
|
| 114 | ATTRIBUTE_GROUPS(ubi_class);
|
| 115 |
|
| 116 |
|
| 117 | const struct class ubi_class = {
|
| 118 | .name = UBI_NAME_STR,
|
| 119 | .class_groups = ubi_class_groups,
|
| 120 | };
|
| 121 |
|
| 122 | static ssize_t dev_attribute_show(struct device *dev,
|
| 123 | struct device_attribute *attr, char *buf);
|
| 124 |
|
| 125 |
|
| 126 | static struct device_attribute dev_eraseblock_size =
|
| 127 | __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
|
| 128 | static struct device_attribute dev_avail_eraseblocks =
|
| 129 | __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
|
| 130 | static struct device_attribute dev_total_eraseblocks =
|
| 131 | __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
|
| 132 | static struct device_attribute dev_volumes_count =
|
| 133 | __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
|
| 134 | static struct device_attribute dev_max_ec =
|
| 135 | __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
|
| 136 | static struct device_attribute dev_reserved_for_bad =
|
| 137 | __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
|
| 138 | static struct device_attribute dev_bad_peb_count =
|
| 139 | __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
|
| 140 | static struct device_attribute dev_max_vol_count =
|
| 141 | __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
|
| 142 | static struct device_attribute dev_min_io_size =
|
| 143 | __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
|
| 144 | static struct device_attribute dev_bgt_enabled =
|
| 145 | __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
|
| 146 | static struct device_attribute dev_mtd_num =
|
| 147 | __ATTR(mtd_num, S_IRUGO, dev_attribute_show, NULL);
|
| 148 | static struct device_attribute dev_ro_mode =
|
| 149 | __ATTR(ro_mode, S_IRUGO, dev_attribute_show, NULL);
|
| 150 |
|
| 151 | |
| 152 | * ubi_volume_notify - send a volume change notification.
|
| 153 | * @ubi: UBI device description object
|
| 154 | * @vol: volume description object of the changed volume
|
| 155 | * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
|
| 156 | *
|
| 157 | * This is a helper function which notifies all subscribers about a volume
|
| 158 | * change event (creation, removal, re-sizing, re-naming, updating). Returns
|
| 159 | * zero in case of success and a negative error code in case of failure.
|
| 160 |
|
| 161 | int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol, int ntype)
|
| 162 | {
|
| 163 | int ret;
|
| 164 | struct ubi_notification nt;
|
| 165 |
|
| 166 | ubi_do_get_device_info(ubi, &nt.di);
|
| 167 | ubi_do_get_volume_info(ubi, vol, &nt.vi);
|
| 168 |
|
| 169 | switch (ntype) {
|
| 170 | case UBI_VOLUME_ADDED:
|
| 171 | case UBI_VOLUME_REMOVED:
|
| 172 | case UBI_VOLUME_RESIZED:
|
| 173 | case UBI_VOLUME_RENAMED:
|
| 174 | ret = ubi_update_fastmap(ubi);
|
| 175 | if (ret)
|
| 176 | ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
|
| 177 | }
|
| 178 |
|
| 179 | return blocking_notifier_call_chain(&ubi_notifiers, ntype, &nt);
|
| 180 | }
|
| 181 |
|
| 182 | |
| 183 | * ubi_notify_all - send a notification to all volumes.
|
| 184 | * @ubi: UBI device description object
|
| 185 | * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
|
| 186 | * @nb: the notifier to call
|
| 187 | *
|
| 188 | * This function walks all volumes of UBI device @ubi and sends the @ntype
|
| 189 | * notification for each volume. If @nb is %NULL, then all registered notifiers
|
| 190 | * are called, otherwise only the @nb notifier is called. Returns the number of
|
| 191 | * sent notifications.
|
| 192 |
|
| 193 | int ubi_notify_all(struct ubi_device *ubi, int ntype, struct notifier_block *nb)
|
| 194 | {
|
| 195 | struct ubi_notification nt;
|
| 196 | int i, count = 0;
|
| 197 |
|
| 198 | ubi_do_get_device_info(ubi, &nt.di);
|
| 199 |
|
| 200 | mutex_lock(&ubi->device_mutex);
|
| 201 | for (i = 0; i < ubi->vtbl_slots; i++) {
|
| 202 | |
| 203 | * Since the @ubi->device is locked, and we are not going to
|
| 204 | * change @ubi->volumes, we do not have to lock
|
| 205 | * @ubi->volumes_lock.
|
| 206 |
|
| 207 | if (!ubi->volumes[i])
|
| 208 | continue;
|
| 209 |
|
| 210 | ubi_do_get_volume_info(ubi, ubi->volumes[i], &nt.vi);
|
| 211 | if (nb)
|
| 212 | nb->notifier_call(nb, ntype, &nt);
|
| 213 | else
|
| 214 | blocking_notifier_call_chain(&ubi_notifiers, ntype,
|
| 215 | &nt);
|
| 216 | count += 1;
|
| 217 | }
|
| 218 | mutex_unlock(&ubi->device_mutex);
|
| 219 |
|
| 220 | return count;
|
| 221 | }
|
| 222 |
|
| 223 | |
| 224 | * ubi_enumerate_volumes - send "add" notification for all existing volumes.
|
| 225 | * @nb: the notifier to call
|
| 226 | *
|
| 227 | * This function walks all UBI devices and volumes and sends the
|
| 228 | * %UBI_VOLUME_ADDED notification for each volume. If @nb is %NULL, then all
|
| 229 | * registered notifiers are called, otherwise only the @nb notifier is called.
|
| 230 | * Returns the number of sent notifications.
|
| 231 |
|
| 232 | int ubi_enumerate_volumes(struct notifier_block *nb)
|
| 233 | {
|
| 234 | int i, count = 0;
|
| 235 |
|
| 236 | |
| 237 | * Since the @ubi_devices_mutex is locked, and we are not going to
|
| 238 | * change @ubi_devices, we do not have to lock @ubi_devices_lock.
|
| 239 |
|
| 240 | for (i = 0; i < UBI_MAX_DEVICES; i++) {
|
| 241 | struct ubi_device *ubi = ubi_devices[i];
|
| 242 |
|
| 243 | if (!ubi)
|
| 244 | continue;
|
| 245 | count += ubi_notify_all(ubi, UBI_VOLUME_ADDED, nb);
|
| 246 | }
|
| 247 |
|
| 248 | return count;
|
| 249 | }
|
| 250 |
|
| 251 | |
| 252 | * ubi_get_device - get UBI device.
|
| 253 | * @ubi_num: UBI device number
|
| 254 | *
|
| 255 | * This function returns UBI device description object for UBI device number
|
| 256 | * @ubi_num, or %NULL if the device does not exist. This function increases the
|
| 257 | * device reference count to prevent removal of the device. In other words, the
|
| 258 | * device cannot be removed if its reference count is not zero.
|
| 259 |
|
| 260 | struct ubi_device *ubi_get_device(int ubi_num)
|
| 261 | {
|
| 262 | struct ubi_device *ubi;
|
| 263 |
|
| 264 | spin_lock(&ubi_devices_lock);
|
| 265 | ubi = ubi_devices[ubi_num];
|
| 266 | if (ubi && ubi->is_dead)
|
| 267 | ubi = NULL;
|
| 268 |
|
| 269 | if (ubi) {
|
| 270 | ubi_assert(ubi->ref_count >= 0);
|
| 271 | ubi->ref_count += 1;
|
| 272 | get_device(&ubi->dev);
|
| 273 | }
|
| 274 | spin_unlock(&ubi_devices_lock);
|
| 275 |
|
| 276 | return ubi;
|
| 277 | }
|
| 278 |
|
| 279 | |
| 280 | * ubi_put_device - drop an UBI device reference.
|
| 281 | * @ubi: UBI device description object
|
| 282 |
|
| 283 | void ubi_put_device(struct ubi_device *ubi)
|
| 284 | {
|
| 285 | spin_lock(&ubi_devices_lock);
|
| 286 | ubi->ref_count -= 1;
|
| 287 | put_device(&ubi->dev);
|
| 288 | spin_unlock(&ubi_devices_lock);
|
| 289 | }
|
| 290 |
|
| 291 | |
| 292 | * ubi_get_by_major - get UBI device by character device major number.
|
| 293 | * @major: major number
|
| 294 | *
|
| 295 | * This function is similar to 'ubi_get_device()', but it searches the device
|
| 296 | * by its major number.
|
| 297 |
|
| 298 | struct ubi_device *ubi_get_by_major(int major)
|
| 299 | {
|
| 300 | int i;
|
| 301 | struct ubi_device *ubi;
|
| 302 |
|
| 303 | spin_lock(&ubi_devices_lock);
|
| 304 | for (i = 0; i < UBI_MAX_DEVICES; i++) {
|
| 305 | ubi = ubi_devices[i];
|
| 306 | if (ubi && !ubi->is_dead && MAJOR(ubi->cdev.dev) == major) {
|
| 307 | ubi_assert(ubi->ref_count >= 0);
|
| 308 | ubi->ref_count += 1;
|
| 309 | get_device(&ubi->dev);
|
| 310 | spin_unlock(&ubi_devices_lock);
|
| 311 | return ubi;
|
| 312 | }
|
| 313 | }
|
| 314 | spin_unlock(&ubi_devices_lock);
|
| 315 |
|
| 316 | return NULL;
|
| 317 | }
|
| 318 |
|
| 319 | |
| 320 | * ubi_major2num - get UBI device number by character device major number.
|
| 321 | * @major: major number
|
| 322 | *
|
| 323 | * This function searches UBI device number object by its major number. If UBI
|
| 324 | * device was not found, this function returns -ENODEV, otherwise the UBI device
|
| 325 | * number is returned.
|
| 326 |
|
| 327 | int ubi_major2num(int major)
|
| 328 | {
|
| 329 | int i, ubi_num = -ENODEV;
|
| 330 |
|
| 331 | spin_lock(&ubi_devices_lock);
|
| 332 | for (i = 0; i < UBI_MAX_DEVICES; i++) {
|
| 333 | struct ubi_device *ubi = ubi_devices[i];
|
| 334 |
|
| 335 | if (ubi && !ubi->is_dead && MAJOR(ubi->cdev.dev) == major) {
|
| 336 | ubi_num = ubi->ubi_num;
|
| 337 | break;
|
| 338 | }
|
| 339 | }
|
| 340 | spin_unlock(&ubi_devices_lock);
|
| 341 |
|
| 342 | return ubi_num;
|
| 343 | }
|
| 344 |
|
| 345 |
|
| 346 | static ssize_t dev_attribute_show(struct device *dev,
|
| 347 | struct device_attribute *attr, char *buf)
|
| 348 | {
|
| 349 | ssize_t ret;
|
| 350 | struct ubi_device *ubi;
|
| 351 |
|
| 352 | |
| 353 | * The below code looks weird, but it actually makes sense. We get the
|
| 354 | * UBI device reference from the contained 'struct ubi_device'. But it
|
| 355 | * is unclear if the device was removed or not yet. Indeed, if the
|
| 356 | * device was removed before we increased its reference count,
|
| 357 | * 'ubi_get_device()' will return -ENODEV and we fail.
|
| 358 | *
|
| 359 | * Remember, 'struct ubi_device' is freed in the release function, so
|
| 360 | * we still can use 'ubi->ubi_num'.
|
| 361 |
|
| 362 | ubi = container_of(dev, struct ubi_device, dev);
|
| 363 |
|
| 364 | if (attr == &dev_eraseblock_size)
|
| 365 | ret = sprintf(buf, "%d\n", ubi->leb_size);
|
| 366 | else if (attr == &dev_avail_eraseblocks)
|
| 367 | ret = sprintf(buf, "%d\n", ubi->avail_pebs);
|
| 368 | else if (attr == &dev_total_eraseblocks)
|
| 369 | ret = sprintf(buf, "%d\n", ubi->good_peb_count);
|
| 370 | else if (attr == &dev_volumes_count)
|
| 371 | ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
|
| 372 | else if (attr == &dev_max_ec)
|
| 373 | ret = sprintf(buf, "%d\n", ubi->max_ec);
|
| 374 | else if (attr == &dev_reserved_for_bad)
|
| 375 | ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
|
| 376 | else if (attr == &dev_bad_peb_count)
|
| 377 | ret = sprintf(buf, "%d\n", ubi->bad_peb_count);
|
| 378 | else if (attr == &dev_max_vol_count)
|
| 379 | ret = sprintf(buf, "%d\n", ubi->vtbl_slots);
|
| 380 | else if (attr == &dev_min_io_size)
|
| 381 | ret = sprintf(buf, "%d\n", ubi->min_io_size);
|
| 382 | else if (attr == &dev_bgt_enabled)
|
| 383 | ret = sprintf(buf, "%d\n", ubi->thread_enabled);
|
| 384 | else if (attr == &dev_mtd_num)
|
| 385 | ret = sprintf(buf, "%d\n", ubi->mtd->index);
|
| 386 | else if (attr == &dev_ro_mode)
|
| 387 | ret = sprintf(buf, "%d\n", ubi->ro_mode);
|
| 388 | else
|
| 389 | ret = -EINVAL;
|
| 390 |
|
| 391 | return ret;
|
| 392 | }
|
| 393 |
|
| 394 | static struct attribute *ubi_dev_attrs[] = {
|
| 395 | &dev_eraseblock_size.attr,
|
| 396 | &dev_avail_eraseblocks.attr,
|
| 397 | &dev_total_eraseblocks.attr,
|
| 398 | &dev_volumes_count.attr,
|
| 399 | &dev_max_ec.attr,
|
| 400 | &dev_reserved_for_bad.attr,
|
| 401 | &dev_bad_peb_count.attr,
|
| 402 | &dev_max_vol_count.attr,
|
| 403 | &dev_min_io_size.attr,
|
| 404 | &dev_bgt_enabled.attr,
|
| 405 | &dev_mtd_num.attr,
|
| 406 | &dev_ro_mode.attr,
|
| 407 | NULL
|
| 408 | };
|
| 409 | ATTRIBUTE_GROUPS(ubi_dev);
|
| 410 |
|
| 411 | static void dev_release(struct device *dev)
|
| 412 | {
|
| 413 | struct ubi_device *ubi = container_of(dev, struct ubi_device, dev);
|
| 414 |
|
| 415 | kfree(ubi);
|
| 416 | }
|
| 417 |
|
| 418 | |
| 419 | * kill_volumes - destroy all user volumes.
|
| 420 | * @ubi: UBI device description object
|
| 421 |
|
| 422 | static void kill_volumes(struct ubi_device *ubi)
|
| 423 | {
|
| 424 | int i;
|
| 425 |
|
| 426 | for (i = 0; i < ubi->vtbl_slots; i++)
|
| 427 | if (ubi->volumes[i])
|
| 428 | ubi_free_volume(ubi, ubi->volumes[i]);
|
| 429 | }
|
| 430 |
|
| 431 | |
| 432 | * uif_init - initialize user interfaces for an UBI device.
|
| 433 | * @ubi: UBI device description object
|
| 434 | *
|
| 435 | * This function initializes various user interfaces for an UBI device. If the
|
| 436 | * initialization fails at an early stage, this function frees all the
|
| 437 | * resources it allocated, returns an error.
|
| 438 | *
|
| 439 | * This function returns zero in case of success and a negative error code in
|
| 440 | * case of failure.
|
| 441 |
|
| 442 | static int uif_init(struct ubi_device *ubi)
|
| 443 | {
|
| 444 | int i, err;
|
| 445 | dev_t dev;
|
| 446 |
|
| 447 | sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
|
| 448 |
|
| 449 | |
| 450 | * Major numbers for the UBI character devices are allocated
|
| 451 | * dynamically. Major numbers of volume character devices are
|
| 452 | * equivalent to ones of the corresponding UBI character device. Minor
|
| 453 | * numbers of UBI character devices are 0, while minor numbers of
|
| 454 | * volume character devices start from 1. Thus, we allocate one major
|
| 455 | * number and ubi->vtbl_slots + 1 minor numbers.
|
| 456 |
|
| 457 | err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
|
| 458 | if (err) {
|
| 459 | ubi_err(ubi, "cannot register UBI character devices");
|
| 460 | return err;
|
| 461 | }
|
| 462 |
|
| 463 | ubi->dev.devt = dev;
|
| 464 |
|
| 465 | ubi_assert(MINOR(dev) == 0);
|
| 466 | cdev_init(&ubi->cdev, &ubi_cdev_operations);
|
| 467 | dbg_gen("%s major is %u", ubi->ubi_name, MAJOR(dev));
|
| 468 | ubi->cdev.owner = THIS_MODULE;
|
| 469 |
|
| 470 | dev_set_name(&ubi->dev, UBI_NAME_STR "%d", ubi->ubi_num);
|
| 471 | err = cdev_device_add(&ubi->cdev, &ubi->dev);
|
| 472 | if (err)
|
| 473 | goto out_unreg;
|
| 474 |
|
| 475 | for (i = 0; i < ubi->vtbl_slots; i++)
|
| 476 | if (ubi->volumes[i]) {
|
| 477 | err = ubi_add_volume(ubi, ubi->volumes[i]);
|
| 478 | if (err) {
|
| 479 | ubi_err(ubi, "cannot add volume %d", i);
|
| 480 | ubi->volumes[i] = NULL;
|
| 481 | goto out_volumes;
|
| 482 | }
|
| 483 | }
|
| 484 |
|
| 485 | return 0;
|
| 486 |
|
| 487 | out_volumes:
|
| 488 | kill_volumes(ubi);
|
| 489 | cdev_device_del(&ubi->cdev, &ubi->dev);
|
| 490 | out_unreg:
|
| 491 | unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
|
| 492 | ubi_err(ubi, "cannot initialize UBI %s, error %d",
|
| 493 | ubi->ubi_name, err);
|
| 494 | return err;
|
| 495 | }
|
| 496 |
|
| 497 | |
| 498 | * uif_close - close user interfaces for an UBI device.
|
| 499 | * @ubi: UBI device description object
|
| 500 | *
|
| 501 | * Note, since this function un-registers UBI volume device objects (@vol->dev),
|
| 502 | * the memory allocated voe the volumes is freed as well (in the release
|
| 503 | * function).
|
| 504 |
|
| 505 | static void uif_close(struct ubi_device *ubi)
|
| 506 | {
|
| 507 | kill_volumes(ubi);
|
| 508 | cdev_device_del(&ubi->cdev, &ubi->dev);
|
| 509 | unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
|
| 510 | }
|
| 511 |
|
| 512 | |
| 513 | * ubi_free_volumes_from - free volumes from specific index.
|
| 514 | * @ubi: UBI device description object
|
| 515 | * @from: the start index used for volume free.
|
| 516 |
|
| 517 | static void ubi_free_volumes_from(struct ubi_device *ubi, int from)
|
| 518 | {
|
| 519 | int i;
|
| 520 |
|
| 521 | for (i = from; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
|
| 522 | if (!ubi->volumes[i] || ubi->volumes[i]->is_dead)
|
| 523 | continue;
|
| 524 | ubi_eba_replace_table(ubi->volumes[i], NULL);
|
| 525 | ubi_fastmap_destroy_checkmap(ubi->volumes[i]);
|
| 526 | kfree(ubi->volumes[i]);
|
| 527 | ubi->volumes[i] = NULL;
|
| 528 | }
|
| 529 | }
|
| 530 |
|
| 531 | |
| 532 | * ubi_free_all_volumes - free all volumes.
|
| 533 | * @ubi: UBI device description object
|
| 534 |
|
| 535 | void ubi_free_all_volumes(struct ubi_device *ubi)
|
| 536 | {
|
| 537 | ubi_free_volumes_from(ubi, 0);
|
| 538 | }
|
| 539 |
|
| 540 | |
| 541 | * ubi_free_internal_volumes - free internal volumes.
|
| 542 | * @ubi: UBI device description object
|
| 543 |
|
| 544 | void ubi_free_internal_volumes(struct ubi_device *ubi)
|
| 545 | {
|
| 546 | ubi_free_volumes_from(ubi, ubi->vtbl_slots);
|
| 547 | }
|
| 548 |
|
| 549 | static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
|
| 550 | {
|
| 551 | int limit, device_pebs;
|
| 552 | uint64_t device_size;
|
| 553 |
|
| 554 | if (!max_beb_per1024) {
|
| 555 | |
| 556 | * Since max_beb_per1024 has not been set by the user in either
|
| 557 | * the cmdline or Kconfig, use mtd_max_bad_blocks to set the
|
| 558 | * limit if it is supported by the device.
|
| 559 |
|
| 560 | limit = mtd_max_bad_blocks(ubi->mtd, 0, ubi->mtd->size);
|
| 561 | if (limit < 0)
|
| 562 | return 0;
|
| 563 | return limit;
|
| 564 | }
|
| 565 |
|
| 566 | |
| 567 | * Here we are using size of the entire flash chip and
|
| 568 | * not just the MTD partition size because the maximum
|
| 569 | * number of bad eraseblocks is a percentage of the
|
| 570 | * whole device and bad eraseblocks are not fairly
|
| 571 | * distributed over the flash chip. So the worst case
|
| 572 | * is that all the bad eraseblocks of the chip are in
|
| 573 | * the MTD partition we are attaching (ubi->mtd).
|
| 574 |
|
| 575 | device_size = mtd_get_device_size(ubi->mtd);
|
| 576 | device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
|
| 577 | limit = mult_frac(device_pebs, max_beb_per1024, 1024);
|
| 578 |
|
| 579 |
|
| 580 | if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)
|
| 581 | limit += 1;
|
| 582 |
|
| 583 | return limit;
|
| 584 | }
|
| 585 |
|
| 586 | |
| 587 | * io_init - initialize I/O sub-system for a given UBI device.
|
| 588 | * @ubi: UBI device description object
|
| 589 | * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
|
| 590 | *
|
| 591 | * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
|
| 592 | * assumed:
|
| 593 | * o EC header is always at offset zero - this cannot be changed;
|
| 594 | * o VID header starts just after the EC header at the closest address
|
| 595 | * aligned to @io->hdrs_min_io_size;
|
| 596 | * o data starts just after the VID header at the closest address aligned to
|
| 597 | * @io->min_io_size
|
| 598 | *
|
| 599 | * This function returns zero in case of success and a negative error code in
|
| 600 | * case of failure.
|
| 601 |
|
| 602 | static int io_init(struct ubi_device *ubi, int max_beb_per1024)
|
| 603 | {
|
| 604 | dbg_gen("sizeof(struct ubi_ainf_peb) %zu", sizeof(struct ubi_ainf_peb));
|
| 605 | dbg_gen("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry));
|
| 606 |
|
| 607 | if (ubi->mtd->numeraseregions != 0) {
|
| 608 | |
| 609 | * Some flashes have several erase regions. Different regions
|
| 610 | * may have different eraseblock size and other
|
| 611 | * characteristics. It looks like mostly multi-region flashes
|
| 612 | * have one "main" region and one or more small regions to
|
| 613 | * store boot loader code or boot parameters or whatever. I
|
| 614 | * guess we should just pick the largest region. But this is
|
| 615 | * not implemented.
|
| 616 |
|
| 617 | ubi_err(ubi, "multiple regions, not implemented");
|
| 618 | return -EINVAL;
|
| 619 | }
|
| 620 |
|
| 621 | if (ubi->vid_hdr_offset < 0)
|
| 622 | return -EINVAL;
|
| 623 |
|
| 624 | |
| 625 | * Note, in this implementation we support MTD devices with 0x7FFFFFFF
|
| 626 | * physical eraseblocks maximum.
|
| 627 |
|
| 628 |
|
| 629 | ubi->peb_size = ubi->mtd->erasesize;
|
| 630 | ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd);
|
| 631 | ubi->flash_size = ubi->mtd->size;
|
| 632 |
|
| 633 | if (mtd_can_have_bb(ubi->mtd)) {
|
| 634 | ubi->bad_allowed = 1;
|
| 635 | ubi->bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024);
|
| 636 | }
|
| 637 |
|
| 638 | if (ubi->mtd->type == MTD_NORFLASH)
|
| 639 | ubi->nor_flash = 1;
|
| 640 |
|
| 641 | ubi->min_io_size = ubi->mtd->writesize;
|
| 642 | ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
|
| 643 |
|
| 644 | |
| 645 | * Make sure minimal I/O unit is power of 2. Note, there is no
|
| 646 | * fundamental reason for this assumption. It is just an optimization
|
| 647 | * which allows us to avoid costly division operations.
|
| 648 |
|
| 649 | if (!is_power_of_2(ubi->min_io_size)) {
|
| 650 | ubi_err(ubi, "min. I/O unit (%d) is not power of 2",
|
| 651 | ubi->min_io_size);
|
| 652 | return -EINVAL;
|
| 653 | }
|
| 654 |
|
| 655 | ubi_assert(ubi->hdrs_min_io_size > 0);
|
| 656 | ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
|
| 657 | ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
|
| 658 |
|
| 659 | ubi->max_write_size = ubi->mtd->writebufsize;
|
| 660 | |
| 661 | * Maximum write size has to be greater or equivalent to min. I/O
|
| 662 | * size, and be multiple of min. I/O size.
|
| 663 |
|
| 664 | if (ubi->max_write_size < ubi->min_io_size ||
|
| 665 | ubi->max_write_size % ubi->min_io_size ||
|
| 666 | !is_power_of_2(ubi->max_write_size)) {
|
| 667 | ubi_err(ubi, "bad write buffer size %d for %d min. I/O unit",
|
| 668 | ubi->max_write_size, ubi->min_io_size);
|
| 669 | return -EINVAL;
|
| 670 | }
|
| 671 |
|
| 672 |
|
| 673 | ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
|
| 674 | ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
|
| 675 |
|
| 676 | dbg_gen("min_io_size %d", ubi->min_io_size);
|
| 677 | dbg_gen("max_write_size %d", ubi->max_write_size);
|
| 678 | dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
|
| 679 | dbg_gen("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
|
| 680 | dbg_gen("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
|
| 681 |
|
| 682 | if (ubi->vid_hdr_offset == 0)
|
| 683 |
|
| 684 | ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
|
| 685 | ubi->ec_hdr_alsize;
|
| 686 | else {
|
| 687 | ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
|
| 688 | ~(ubi->hdrs_min_io_size - 1);
|
| 689 | ubi->vid_hdr_shift = ubi->vid_hdr_offset -
|
| 690 | ubi->vid_hdr_aloffset;
|
| 691 | }
|
| 692 |
|
| 693 | |
| 694 | * Memory allocation for VID header is ubi->vid_hdr_alsize
|
| 695 | * which is described in comments in io.c.
|
| 696 | * Make sure VID header shift + UBI_VID_HDR_SIZE not exceeds
|
| 697 | * ubi->vid_hdr_alsize, so that all vid header operations
|
| 698 | * won't access memory out of bounds.
|
| 699 |
|
| 700 | if ((ubi->vid_hdr_shift + UBI_VID_HDR_SIZE) > ubi->vid_hdr_alsize) {
|
| 701 | ubi_err(ubi, "Invalid VID header offset %d, VID header shift(%d)"
|
| 702 | " + VID header size(%zu) > VID header aligned size(%d).",
|
| 703 | ubi->vid_hdr_offset, ubi->vid_hdr_shift,
|
| 704 | UBI_VID_HDR_SIZE, ubi->vid_hdr_alsize);
|
| 705 | return -EINVAL;
|
| 706 | }
|
| 707 |
|
| 708 |
|
| 709 | ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
|
| 710 | ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
|
| 711 |
|
| 712 | dbg_gen("vid_hdr_offset %d", ubi->vid_hdr_offset);
|
| 713 | dbg_gen("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
|
| 714 | dbg_gen("vid_hdr_shift %d", ubi->vid_hdr_shift);
|
| 715 | dbg_gen("leb_start %d", ubi->leb_start);
|
| 716 |
|
| 717 |
|
| 718 | if (ubi->vid_hdr_shift % 4) {
|
| 719 | ubi_err(ubi, "unaligned VID header shift %d",
|
| 720 | ubi->vid_hdr_shift);
|
| 721 | return -EINVAL;
|
| 722 | }
|
| 723 |
|
| 724 |
|
| 725 | if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
|
| 726 | ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
|
| 727 | ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
|
| 728 | ubi->leb_start & (ubi->min_io_size - 1)) {
|
| 729 | ubi_err(ubi, "bad VID header (%d) or data offsets (%d)",
|
| 730 | ubi->vid_hdr_offset, ubi->leb_start);
|
| 731 | return -EINVAL;
|
| 732 | }
|
| 733 |
|
| 734 | |
| 735 | * Set maximum amount of physical erroneous eraseblocks to be 10%.
|
| 736 | * Erroneous PEB are those which have read errors.
|
| 737 |
|
| 738 | ubi->max_erroneous = ubi->peb_count / 10;
|
| 739 | if (ubi->max_erroneous < 16)
|
| 740 | ubi->max_erroneous = 16;
|
| 741 | dbg_gen("max_erroneous %d", ubi->max_erroneous);
|
| 742 |
|
| 743 | |
| 744 | * It may happen that EC and VID headers are situated in one minimal
|
| 745 | * I/O unit. In this case we can only accept this UBI image in
|
| 746 | * read-only mode.
|
| 747 |
|
| 748 | if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
|
| 749 | ubi_warn(ubi, "EC and VID headers are in the same minimal I/O unit, switch to read-only mode");
|
| 750 | ubi->ro_mode = 1;
|
| 751 | }
|
| 752 |
|
| 753 | ubi->leb_size = ubi->peb_size - ubi->leb_start;
|
| 754 |
|
| 755 | if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
|
| 756 | ubi_msg(ubi, "MTD device %d is write-protected, attach in read-only mode",
|
| 757 | ubi->mtd->index);
|
| 758 | ubi->ro_mode = 1;
|
| 759 | }
|
| 760 |
|
| 761 | |
| 762 | * Note, ideally, we have to initialize @ubi->bad_peb_count here. But
|
| 763 | * unfortunately, MTD does not provide this information. We should loop
|
| 764 | * over all physical eraseblocks and invoke mtd->block_is_bad() for
|
| 765 | * each physical eraseblock. So, we leave @ubi->bad_peb_count
|
| 766 | * uninitialized so far.
|
| 767 |
|
| 768 |
|
| 769 | return 0;
|
| 770 | }
|
| 771 |
|
| 772 | |
| 773 | * autoresize - re-size the volume which has the "auto-resize" flag set.
|
| 774 | * @ubi: UBI device description object
|
| 775 | * @vol_id: ID of the volume to re-size
|
| 776 | *
|
| 777 | * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
|
| 778 | * the volume table to the largest possible size. See comments in ubi-header.h
|
| 779 | * for more description of the flag. Returns zero in case of success and a
|
| 780 | * negative error code in case of failure.
|
| 781 |
|
| 782 | static int autoresize(struct ubi_device *ubi, int vol_id)
|
| 783 | {
|
| 784 | struct ubi_volume_desc desc;
|
| 785 | struct ubi_volume *vol = ubi->volumes[vol_id];
|
| 786 | int err, old_reserved_pebs = vol->reserved_pebs;
|
| 787 |
|
| 788 | if (ubi->ro_mode) {
|
| 789 | ubi_warn(ubi, "skip auto-resize because of R/O mode");
|
| 790 | return 0;
|
| 791 | }
|
| 792 |
|
| 793 | |
| 794 | * Clear the auto-resize flag in the volume in-memory copy of the
|
| 795 | * volume table, and 'ubi_resize_volume()' will propagate this change
|
| 796 | * to the flash.
|
| 797 |
|
| 798 | ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
|
| 799 |
|
| 800 | if (ubi->avail_pebs == 0) {
|
| 801 | struct ubi_vtbl_record vtbl_rec;
|
| 802 |
|
| 803 | |
| 804 | * No available PEBs to re-size the volume, clear the flag on
|
| 805 | * flash and exit.
|
| 806 |
|
| 807 | vtbl_rec = ubi->vtbl[vol_id];
|
| 808 | err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
|
| 809 | if (err)
|
| 810 | ubi_err(ubi, "cannot clean auto-resize flag for volume %d",
|
| 811 | vol_id);
|
| 812 | } else {
|
| 813 | desc.vol = vol;
|
| 814 | err = ubi_resize_volume(&desc,
|
| 815 | old_reserved_pebs + ubi->avail_pebs);
|
| 816 | if (err)
|
| 817 | ubi_err(ubi, "cannot auto-resize volume %d",
|
| 818 | vol_id);
|
| 819 | }
|
| 820 |
|
| 821 | if (err)
|
| 822 | return err;
|
| 823 |
|
| 824 | ubi_msg(ubi, "volume %d (\"%s\") re-sized from %d to %d LEBs",
|
| 825 | vol_id, vol->name, old_reserved_pebs, vol->reserved_pebs);
|
| 826 | return 0;
|
| 827 | }
|
| 828 |
|
| 829 | |
| 830 | * ubi_attach_mtd_dev - attach an MTD device.
|
| 831 | * @mtd: MTD device description object
|
| 832 | * @ubi_num: number to assign to the new UBI device
|
| 833 | * @vid_hdr_offset: VID header offset
|
| 834 | * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
|
| 835 | * @disable_fm: whether disable fastmap
|
| 836 | * @need_resv_pool: whether reserve pebs to fill fm_pool
|
| 837 | * @wl_threshold: wear-leveling threshold for this UBI device; 0 means use
|
| 838 | * %CONFIG_MTD_UBI_WL_THRESHOLD; accepted range is 2-65536
|
| 839 | *
|
| 840 | * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
|
| 841 | * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
|
| 842 | * which case this function finds a vacant device number and assigns it
|
| 843 | * automatically. Returns the new UBI device number in case of success and a
|
| 844 | * negative error code in case of failure.
|
| 845 | *
|
| 846 | * If @disable_fm is true, ubi doesn't create new fastmap even the module param
|
| 847 | * 'fm_autoconvert' is set, and existed old fastmap will be destroyed after
|
| 848 | * doing full scanning.
|
| 849 | *
|
| 850 | * Note, the invocations of this function has to be serialized by the
|
| 851 | * @ubi_devices_mutex.
|
| 852 |
|
| 853 | int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
|
| 854 | int vid_hdr_offset, int max_beb_per1024, bool disable_fm,
|
| 855 | bool need_resv_pool, int wl_threshold)
|
| 856 | {
|
| 857 | struct ubi_device *ubi;
|
| 858 | int i, err;
|
| 859 |
|
| 860 | if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
|
| 861 | return -EINVAL;
|
| 862 |
|
| 863 | if (!max_beb_per1024)
|
| 864 | max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
|
| 865 |
|
| 866 | if (!wl_threshold)
|
| 867 | wl_threshold = CONFIG_MTD_UBI_WL_THRESHOLD;
|
| 868 |
|
| 869 | if (wl_threshold < 2 || wl_threshold > 65536) {
|
| 870 | pr_err("ubi: bad wear-leveling threshold %d\n",
|
| 871 | wl_threshold);
|
| 872 | return -EINVAL;
|
| 873 | }
|
| 874 |
|
| 875 | |
| 876 | * Check if we already have the same MTD device attached.
|
| 877 | *
|
| 878 | * Note, this function assumes that UBI devices creations and deletions
|
| 879 | * are serialized, so it does not take the &ubi_devices_lock.
|
| 880 |
|
| 881 | for (i = 0; i < UBI_MAX_DEVICES; i++) {
|
| 882 | ubi = ubi_devices[i];
|
| 883 | if (ubi && mtd->index == ubi->mtd->index) {
|
| 884 | pr_err("ubi: mtd%d is already attached to ubi%d\n",
|
| 885 | mtd->index, i);
|
| 886 | return -EEXIST;
|
| 887 | }
|
| 888 | }
|
| 889 |
|
| 890 | |
| 891 | * Make sure this MTD device is not emulated on top of an UBI volume
|
| 892 | * already. Well, generally this recursion works fine, but there are
|
| 893 | * different problems like the UBI module takes a reference to itself
|
| 894 | * by attaching (and thus, opening) the emulated MTD device. This
|
| 895 | * results in inability to unload the module. And in general it makes
|
| 896 | * no sense to attach emulated MTD devices, so we prohibit this.
|
| 897 |
|
| 898 | if (mtd->type == MTD_UBIVOLUME) {
|
| 899 | pr_err("ubi: refuse attaching mtd%d - it is already emulated on top of UBI\n",
|
| 900 | mtd->index);
|
| 901 | return -EINVAL;
|
| 902 | }
|
| 903 |
|
| 904 | |
| 905 | * Both UBI and UBIFS have been designed for SLC NAND and NOR flashes.
|
| 906 | * MLC NAND is different and needs special care, otherwise UBI or UBIFS
|
| 907 | * will die soon and you will lose all your data.
|
| 908 | * Relax this rule if the partition we're attaching to operates in SLC
|
| 909 | * mode.
|
| 910 |
|
| 911 | if (mtd->type == MTD_MLCNANDFLASH &&
|
| 912 | !(mtd->flags & MTD_SLC_ON_MLC_EMULATION)) {
|
| 913 | pr_err("ubi: refuse attaching mtd%d - MLC NAND is not supported\n",
|
| 914 | mtd->index);
|
| 915 | return -EINVAL;
|
| 916 | }
|
| 917 |
|
| 918 |
|
| 919 | if (!mtd->erasesize) {
|
| 920 | pr_err("ubi: refuse attaching mtd%d - zero erasesize flash is not supported\n",
|
| 921 | mtd->index);
|
| 922 | return -EINVAL;
|
| 923 | }
|
| 924 |
|
| 925 | if (ubi_num == UBI_DEV_NUM_AUTO) {
|
| 926 |
|
| 927 | for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++)
|
| 928 | if (!ubi_devices[ubi_num])
|
| 929 | break;
|
| 930 | if (ubi_num == UBI_MAX_DEVICES) {
|
| 931 | pr_err("ubi: only %d UBI devices may be created\n",
|
| 932 | UBI_MAX_DEVICES);
|
| 933 | return -ENFILE;
|
| 934 | }
|
| 935 | } else {
|
| 936 | if (ubi_num >= UBI_MAX_DEVICES)
|
| 937 | return -EINVAL;
|
| 938 |
|
| 939 |
|
| 940 | if (ubi_devices[ubi_num]) {
|
| 941 | pr_err("ubi: ubi%i already exists\n", ubi_num);
|
| 942 | return -EEXIST;
|
| 943 | }
|
| 944 | }
|
| 945 |
|
| 946 | ubi = kzalloc_obj(struct ubi_device);
|
| 947 | if (!ubi)
|
| 948 | return -ENOMEM;
|
| 949 |
|
| 950 | device_initialize(&ubi->dev);
|
| 951 | ubi->dev.release = dev_release;
|
| 952 | ubi->dev.class = &ubi_class;
|
| 953 | ubi->dev.groups = ubi_dev_groups;
|
| 954 | ubi->dev.parent = &mtd->dev;
|
| 955 |
|
| 956 | ubi->mtd = mtd;
|
| 957 | ubi->ubi_num = ubi_num;
|
| 958 | ubi->vid_hdr_offset = vid_hdr_offset;
|
| 959 | ubi->autoresize_vol_id = -1;
|
| 960 | ubi->wl_threshold = wl_threshold;
|
| 961 | ubi->wl_free_max_diff = wl_threshold * 2;
|
| 962 |
|
| 963 | #ifdef CONFIG_MTD_UBI_FASTMAP
|
| 964 | ubi->fm_pool.used = ubi->fm_pool.size = 0;
|
| 965 | ubi->fm_wl_pool.used = ubi->fm_wl_pool.size = 0;
|
| 966 |
|
| 967 | |
| 968 | * fm_pool.max_size is 5% of the total number of PEBs but it's also
|
| 969 | * between UBI_FM_MAX_POOL_SIZE and UBI_FM_MIN_POOL_SIZE.
|
| 970 |
|
| 971 | ubi->fm_pool.max_size = min(((int)mtd_div_by_eb(ubi->mtd->size,
|
| 972 | ubi->mtd) / 100) * 5, UBI_FM_MAX_POOL_SIZE);
|
| 973 | ubi->fm_pool.max_size = max(ubi->fm_pool.max_size,
|
| 974 | UBI_FM_MIN_POOL_SIZE);
|
| 975 |
|
| 976 | ubi->fm_wl_pool.max_size = ubi->fm_pool.max_size / 2;
|
| 977 | ubi->fm_pool_rsv_cnt = need_resv_pool ? ubi->fm_pool.max_size : 0;
|
| 978 | ubi->fm_disabled = (!fm_autoconvert || disable_fm) ? 1 : 0;
|
| 979 | if (fm_debug)
|
| 980 | ubi_enable_dbg_chk_fastmap(ubi);
|
| 981 |
|
| 982 | if (!ubi->fm_disabled && (int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd)
|
| 983 | <= UBI_FM_MAX_START) {
|
| 984 | ubi_err(ubi, "More than %i PEBs are needed for fastmap, sorry.",
|
| 985 | UBI_FM_MAX_START);
|
| 986 | ubi->fm_disabled = 1;
|
| 987 | }
|
| 988 |
|
| 989 | ubi_msg(ubi, "default fastmap pool size: %d", ubi->fm_pool.max_size);
|
| 990 | ubi_msg(ubi, "default fastmap WL pool size: %d",
|
| 991 | ubi->fm_wl_pool.max_size);
|
| 992 | #else
|
| 993 | ubi->fm_disabled = 1;
|
| 994 | #endif
|
| 995 | mutex_init(&ubi->buf_mutex);
|
| 996 | mutex_init(&ubi->ckvol_mutex);
|
| 997 | mutex_init(&ubi->device_mutex);
|
| 998 | spin_lock_init(&ubi->volumes_lock);
|
| 999 | init_rwsem(&ubi->fm_protect);
|
| 1000 | init_rwsem(&ubi->fm_eba_sem);
|
| 1001 |
|
| 1002 | ubi_msg(ubi, "attaching mtd%d", mtd->index);
|
| 1003 |
|
| 1004 | err = io_init(ubi, max_beb_per1024);
|
| 1005 | if (err)
|
| 1006 | goto out_free;
|
| 1007 |
|
| 1008 | err = -ENOMEM;
|
| 1009 | ubi->peb_buf = vmalloc(ubi->peb_size);
|
| 1010 | if (!ubi->peb_buf)
|
| 1011 | goto out_free;
|
| 1012 |
|
| 1013 | #ifdef CONFIG_MTD_UBI_FASTMAP
|
| 1014 | ubi->fm_size = ubi_calc_fm_size(ubi);
|
| 1015 | ubi->fm_buf = vzalloc(ubi->fm_size);
|
| 1016 | if (!ubi->fm_buf)
|
| 1017 | goto out_free;
|
| 1018 | #endif
|
| 1019 | err = ubi_attach(ubi, disable_fm ? 1 : 0);
|
| 1020 | if (err) {
|
| 1021 | ubi_err(ubi, "failed to attach mtd%d, error %d",
|
| 1022 | mtd->index, err);
|
| 1023 | goto out_free;
|
| 1024 | }
|
| 1025 |
|
| 1026 | if (ubi->autoresize_vol_id != -1) {
|
| 1027 | err = autoresize(ubi, ubi->autoresize_vol_id);
|
| 1028 | if (err)
|
| 1029 | goto out_detach;
|
| 1030 | }
|
| 1031 |
|
| 1032 | err = uif_init(ubi);
|
| 1033 | if (err)
|
| 1034 | goto out_detach;
|
| 1035 |
|
| 1036 | err = ubi_debugfs_init_dev(ubi);
|
| 1037 | if (err)
|
| 1038 | goto out_uif;
|
| 1039 |
|
| 1040 | ubi->bgt_thread = kthread_create(ubi_thread, ubi, "%s", ubi->bgt_name);
|
| 1041 | if (IS_ERR(ubi->bgt_thread)) {
|
| 1042 | err = PTR_ERR(ubi->bgt_thread);
|
| 1043 | ubi_err(ubi, "cannot spawn \"%s\", error %d",
|
| 1044 | ubi->bgt_name, err);
|
| 1045 | goto out_debugfs;
|
| 1046 | }
|
| 1047 |
|
| 1048 | ubi_msg(ubi, "attached mtd%d (name \"%s\", size %llu MiB)",
|
| 1049 | mtd->index, mtd->name, ubi->flash_size >> 20);
|
| 1050 | ubi_msg(ubi, "PEB size: %d bytes (%d KiB), LEB size: %d bytes",
|
| 1051 | ubi->peb_size, ubi->peb_size >> 10, ubi->leb_size);
|
| 1052 | ubi_msg(ubi, "min./max. I/O unit sizes: %d/%d, sub-page size %d",
|
| 1053 | ubi->min_io_size, ubi->max_write_size, ubi->hdrs_min_io_size);
|
| 1054 | ubi_msg(ubi, "VID header offset: %d (aligned %d), data offset: %d",
|
| 1055 | ubi->vid_hdr_offset, ubi->vid_hdr_aloffset, ubi->leb_start);
|
| 1056 | ubi_msg(ubi, "good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
|
| 1057 | ubi->good_peb_count, ubi->bad_peb_count, ubi->corr_peb_count);
|
| 1058 | ubi_msg(ubi, "user volume: %d, internal volumes: %d, max. volumes count: %d",
|
| 1059 | ubi->vol_count - UBI_INT_VOL_COUNT, UBI_INT_VOL_COUNT,
|
| 1060 | ubi->vtbl_slots);
|
| 1061 | ubi_msg(ubi, "max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
|
| 1062 | ubi->max_ec, ubi->mean_ec, ubi->wl_threshold,
|
| 1063 | ubi->image_seq);
|
| 1064 | ubi_msg(ubi, "available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
|
| 1065 | ubi->avail_pebs, ubi->rsvd_pebs, ubi->beb_rsvd_pebs);
|
| 1066 |
|
| 1067 | |
| 1068 | * The below lock makes sure we do not race with 'ubi_thread()' which
|
| 1069 | * checks @ubi->thread_enabled. Otherwise we may fail to wake it up.
|
| 1070 |
|
| 1071 | spin_lock(&ubi->wl_lock);
|
| 1072 | ubi->thread_enabled = 1;
|
| 1073 | wake_up_process(ubi->bgt_thread);
|
| 1074 | spin_unlock(&ubi->wl_lock);
|
| 1075 |
|
| 1076 | ubi_devices[ubi_num] = ubi;
|
| 1077 | ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
|
| 1078 | return ubi_num;
|
| 1079 |
|
| 1080 | out_debugfs:
|
| 1081 | ubi_debugfs_exit_dev(ubi);
|
| 1082 | out_uif:
|
| 1083 | uif_close(ubi);
|
| 1084 | out_detach:
|
| 1085 | ubi_wl_close(ubi);
|
| 1086 | ubi_free_all_volumes(ubi);
|
| 1087 | vfree(ubi->vtbl);
|
| 1088 | out_free:
|
| 1089 | vfree(ubi->peb_buf);
|
| 1090 | vfree(ubi->fm_buf);
|
| 1091 | put_device(&ubi->dev);
|
| 1092 | return err;
|
| 1093 | }
|
| 1094 |
|
| 1095 | |
| 1096 | * ubi_detach_mtd_dev - detach an MTD device.
|
| 1097 | * @ubi_num: UBI device number to detach from
|
| 1098 | * @anyway: detach MTD even if device reference count is not zero
|
| 1099 | *
|
| 1100 | * This function destroys an UBI device number @ubi_num and detaches the
|
| 1101 | * underlying MTD device. Returns zero in case of success and %-EBUSY if the
|
| 1102 | * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
|
| 1103 | * exist.
|
| 1104 | *
|
| 1105 | * Note, the invocations of this function has to be serialized by the
|
| 1106 | * @ubi_devices_mutex.
|
| 1107 |
|
| 1108 | int ubi_detach_mtd_dev(int ubi_num, int anyway)
|
| 1109 | {
|
| 1110 | struct ubi_device *ubi;
|
| 1111 |
|
| 1112 | if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
|
| 1113 | return -EINVAL;
|
| 1114 |
|
| 1115 | ubi = ubi_get_device(ubi_num);
|
| 1116 | if (!ubi)
|
| 1117 | return -EINVAL;
|
| 1118 |
|
| 1119 | spin_lock(&ubi_devices_lock);
|
| 1120 | ubi->ref_count -= 1;
|
| 1121 | if (ubi->ref_count) {
|
| 1122 | if (!anyway) {
|
| 1123 | put_device(&ubi->dev);
|
| 1124 | spin_unlock(&ubi_devices_lock);
|
| 1125 | return -EBUSY;
|
| 1126 | }
|
| 1127 |
|
| 1128 | ubi_err(ubi, "%s reference count %d, destroy anyway",
|
| 1129 | ubi->ubi_name, ubi->ref_count);
|
| 1130 | }
|
| 1131 | ubi->is_dead = true;
|
| 1132 | spin_unlock(&ubi_devices_lock);
|
| 1133 |
|
| 1134 | ubi_notify_all(ubi, UBI_VOLUME_SHUTDOWN, NULL);
|
| 1135 |
|
| 1136 | spin_lock(&ubi_devices_lock);
|
| 1137 | put_device(&ubi->dev);
|
| 1138 | ubi_devices[ubi_num] = NULL;
|
| 1139 | spin_unlock(&ubi_devices_lock);
|
| 1140 |
|
| 1141 | ubi_assert(ubi_num == ubi->ubi_num);
|
| 1142 | ubi_notify_all(ubi, UBI_VOLUME_REMOVED, NULL);
|
| 1143 | ubi_msg(ubi, "detaching mtd%d", ubi->mtd->index);
|
| 1144 | #ifdef CONFIG_MTD_UBI_FASTMAP
|
| 1145 | |
| 1146 | * EC updates that have been made since the last written fastmap.
|
| 1147 | * In case of fastmap debugging we omit the update to simulate an
|
| 1148 | * unclean shutdown.
|
| 1149 | if (!ubi_dbg_chk_fastmap(ubi))
|
| 1150 | ubi_update_fastmap(ubi);
|
| 1151 | #endif
|
| 1152 | |
| 1153 | * Before freeing anything, we have to stop the background thread to
|
| 1154 | * prevent it from doing anything on this device while we are freeing.
|
| 1155 |
|
| 1156 | if (ubi->bgt_thread)
|
| 1157 | kthread_stop(ubi->bgt_thread);
|
| 1158 |
|
| 1159 | #ifdef CONFIG_MTD_UBI_FASTMAP
|
| 1160 | cancel_work_sync(&ubi->fm_work);
|
| 1161 | #endif
|
| 1162 | ubi_debugfs_exit_dev(ubi);
|
| 1163 | uif_close(ubi);
|
| 1164 |
|
| 1165 | ubi_wl_close(ubi);
|
| 1166 | ubi_free_internal_volumes(ubi);
|
| 1167 | vfree(ubi->vtbl);
|
| 1168 | vfree(ubi->peb_buf);
|
| 1169 | vfree(ubi->fm_buf);
|
| 1170 | ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index);
|
| 1171 | put_mtd_device(ubi->mtd);
|
| 1172 | put_device(&ubi->dev);
|
| 1173 | return 0;
|
| 1174 | }
|
| 1175 |
|
| 1176 | |
| 1177 | * open_mtd_by_chdev - open an MTD device by its character device node path.
|
| 1178 | * @mtd_dev: MTD character device node path
|
| 1179 | *
|
| 1180 | * This helper function opens an MTD device by its character node device path.
|
| 1181 | * Returns MTD device description object in case of success and a negative
|
| 1182 | * error code in case of failure.
|
| 1183 |
|
| 1184 | static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
|
| 1185 | {
|
| 1186 | int err, minor;
|
| 1187 | struct path path;
|
| 1188 | struct kstat stat;
|
| 1189 |
|
| 1190 |
|
| 1191 | err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
|
| 1192 | if (err)
|
| 1193 | return ERR_PTR(err);
|
| 1194 |
|
| 1195 | err = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
|
| 1196 | path_put(&path);
|
| 1197 | if (err)
|
| 1198 | return ERR_PTR(err);
|
| 1199 |
|
| 1200 |
|
| 1201 | if (MAJOR(stat.rdev) != MTD_CHAR_MAJOR || !S_ISCHR(stat.mode))
|
| 1202 | return ERR_PTR(-EINVAL);
|
| 1203 |
|
| 1204 | minor = MINOR(stat.rdev);
|
| 1205 |
|
| 1206 | if (minor & 1)
|
| 1207 | |
| 1208 | * Just do not think the "/dev/mtdrX" devices support is need,
|
| 1209 | * so do not support them to avoid doing extra work.
|
| 1210 |
|
| 1211 | return ERR_PTR(-EINVAL);
|
| 1212 |
|
| 1213 | return get_mtd_device(NULL, minor / 2);
|
| 1214 | }
|
| 1215 |
|
| 1216 | |
| 1217 | * open_mtd_device - open MTD device by name, character device path, or number.
|
| 1218 | * @mtd_dev: name, character device node path, or MTD device device number
|
| 1219 | *
|
| 1220 | * This function tries to open and MTD device described by @mtd_dev string,
|
| 1221 | * which is first treated as ASCII MTD device number, and if it is not true, it
|
| 1222 | * is treated as MTD device name, and if that is also not true, it is treated
|
| 1223 | * as MTD character device node path. Returns MTD device description object in
|
| 1224 | * case of success and a negative error code in case of failure.
|
| 1225 |
|
| 1226 | static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
|
| 1227 | {
|
| 1228 | struct mtd_info *mtd;
|
| 1229 | int mtd_num;
|
| 1230 | char *endp;
|
| 1231 |
|
| 1232 | mtd_num = simple_strtoul(mtd_dev, &endp, 0);
|
| 1233 | if (*endp != '\0' || mtd_dev == endp) {
|
| 1234 | |
| 1235 | * This does not look like an ASCII integer, probably this is
|
| 1236 | * MTD device name.
|
| 1237 |
|
| 1238 | mtd = get_mtd_device_nm(mtd_dev);
|
| 1239 | if (PTR_ERR(mtd) == -ENODEV)
|
| 1240 |
|
| 1241 | mtd = open_mtd_by_chdev(mtd_dev);
|
| 1242 | } else
|
| 1243 | mtd = get_mtd_device(NULL, mtd_num);
|
| 1244 |
|
| 1245 | return mtd;
|
| 1246 | }
|
| 1247 |
|
| 1248 | static void ubi_notify_add(struct mtd_info *mtd)
|
| 1249 | {
|
| 1250 | struct device_node *np = mtd_get_of_node(mtd);
|
| 1251 | int err;
|
| 1252 |
|
| 1253 | if (!of_device_is_compatible(np, "linux,ubi"))
|
| 1254 | return;
|
| 1255 |
|
| 1256 | |
| 1257 | * we are already holding &mtd_table_mutex, but still need
|
| 1258 | * to bump refcount
|
| 1259 |
|
| 1260 | err = __get_mtd_device(mtd);
|
| 1261 | if (err)
|
| 1262 | return;
|
| 1263 |
|
| 1264 |
|
| 1265 | mutex_lock_nested(&ubi_devices_mutex, SINGLE_DEPTH_NESTING);
|
| 1266 | err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0, false, false, 0);
|
| 1267 | mutex_unlock(&ubi_devices_mutex);
|
| 1268 | if (err < 0)
|
| 1269 | __put_mtd_device(mtd);
|
| 1270 | }
|
| 1271 |
|
| 1272 | static void ubi_notify_remove(struct mtd_info *mtd)
|
| 1273 | {
|
| 1274 |
|
| 1275 | }
|
| 1276 |
|
| 1277 | static struct mtd_notifier ubi_mtd_notifier = {
|
| 1278 | .add = ubi_notify_add,
|
| 1279 | .remove = ubi_notify_remove,
|
| 1280 | };
|
| 1281 |
|
| 1282 | static int __init ubi_init_attach(void)
|
| 1283 | {
|
| 1284 | int err, i, k;
|
| 1285 |
|
| 1286 |
|
| 1287 | for (i = 0; i < mtd_devs; i++) {
|
| 1288 | struct mtd_dev_param *p = &mtd_dev_param[i];
|
| 1289 | struct mtd_info *mtd;
|
| 1290 |
|
| 1291 | cond_resched();
|
| 1292 |
|
| 1293 | mtd = open_mtd_device(p->name);
|
| 1294 | if (IS_ERR(mtd)) {
|
| 1295 | err = PTR_ERR(mtd);
|
| 1296 | pr_err("UBI error: cannot open mtd %s, error %d\n",
|
| 1297 | p->name, err);
|
| 1298 |
|
| 1299 | if (ubi_is_module())
|
| 1300 | goto out_detach;
|
| 1301 | continue;
|
| 1302 | }
|
| 1303 |
|
| 1304 | mutex_lock(&ubi_devices_mutex);
|
| 1305 | err = ubi_attach_mtd_dev(mtd, p->ubi_num,
|
| 1306 | p->vid_hdr_offs, p->max_beb_per1024,
|
| 1307 | p->enable_fm == 0,
|
| 1308 | p->need_resv_pool != 0,
|
| 1309 | p->wl_threshold);
|
| 1310 | mutex_unlock(&ubi_devices_mutex);
|
| 1311 | if (err < 0) {
|
| 1312 | pr_err("UBI error: cannot attach mtd%d\n",
|
| 1313 | mtd->index);
|
| 1314 | put_mtd_device(mtd);
|
| 1315 |
|
| 1316 | |
| 1317 | * Originally UBI stopped initializing on any error.
|
| 1318 | * However, later on it was found out that this
|
| 1319 | * behavior is not very good when UBI is compiled into
|
| 1320 | * the kernel and the MTD devices to attach are passed
|
| 1321 | * through the command line. Indeed, UBI failure
|
| 1322 | * stopped whole boot sequence.
|
| 1323 | *
|
| 1324 | * To fix this, we changed the behavior for the
|
| 1325 | * non-module case, but preserved the old behavior for
|
| 1326 | * the module case, just for compatibility. This is a
|
| 1327 | * little inconsistent, though.
|
| 1328 |
|
| 1329 | if (ubi_is_module())
|
| 1330 | goto out_detach;
|
| 1331 | }
|
| 1332 | }
|
| 1333 |
|
| 1334 | return 0;
|
| 1335 |
|
| 1336 | out_detach:
|
| 1337 | for (k = 0; k < UBI_MAX_DEVICES; k++)
|
| 1338 | if (ubi_devices[k]) {
|
| 1339 | mutex_lock(&ubi_devices_mutex);
|
| 1340 | ubi_detach_mtd_dev(k, 1);
|
| 1341 | mutex_unlock(&ubi_devices_mutex);
|
| 1342 | }
|
| 1343 | return err;
|
| 1344 | }
|
| 1345 | #ifndef CONFIG_MTD_UBI_MODULE
|
| 1346 | late_initcall(ubi_init_attach);
|
| 1347 | #endif
|
| 1348 |
|
| 1349 | static int __init ubi_init(void)
|
| 1350 | {
|
| 1351 | int err;
|
| 1352 |
|
| 1353 |
|
| 1354 | BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
|
| 1355 | BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
|
| 1356 |
|
| 1357 | if (mtd_devs > UBI_MAX_DEVICES) {
|
| 1358 | pr_err("UBI error: too many MTD devices, maximum is %d\n",
|
| 1359 | UBI_MAX_DEVICES);
|
| 1360 | return -EINVAL;
|
| 1361 | }
|
| 1362 |
|
| 1363 |
|
| 1364 | err = class_register(&ubi_class);
|
| 1365 | if (err < 0)
|
| 1366 | return err;
|
| 1367 |
|
| 1368 | err = misc_register(&ubi_ctrl_cdev);
|
| 1369 | if (err) {
|
| 1370 | pr_err("UBI error: cannot register device\n");
|
| 1371 | goto out;
|
| 1372 | }
|
| 1373 |
|
| 1374 | ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
|
| 1375 | sizeof(struct ubi_wl_entry),
|
| 1376 | 0, 0, NULL);
|
| 1377 | if (!ubi_wl_entry_slab) {
|
| 1378 | err = -ENOMEM;
|
| 1379 | goto out_dev_unreg;
|
| 1380 | }
|
| 1381 |
|
| 1382 | err = ubi_debugfs_init();
|
| 1383 | if (err)
|
| 1384 | goto out_slab;
|
| 1385 |
|
| 1386 | err = ubiblock_init();
|
| 1387 | if (err) {
|
| 1388 | pr_err("UBI error: block: cannot initialize, error %d\n", err);
|
| 1389 |
|
| 1390 |
|
| 1391 | if (ubi_is_module())
|
| 1392 | goto out_debugfs;
|
| 1393 | }
|
| 1394 |
|
| 1395 | register_mtd_user(&ubi_mtd_notifier);
|
| 1396 |
|
| 1397 | if (ubi_is_module()) {
|
| 1398 | err = ubi_init_attach();
|
| 1399 | if (err)
|
| 1400 | goto out_mtd_notifier;
|
| 1401 | }
|
| 1402 |
|
| 1403 | return 0;
|
| 1404 |
|
| 1405 | out_mtd_notifier:
|
| 1406 | unregister_mtd_user(&ubi_mtd_notifier);
|
| 1407 | ubiblock_exit();
|
| 1408 | out_debugfs:
|
| 1409 | ubi_debugfs_exit();
|
| 1410 | out_slab:
|
| 1411 | kmem_cache_destroy(ubi_wl_entry_slab);
|
| 1412 | out_dev_unreg:
|
| 1413 | misc_deregister(&ubi_ctrl_cdev);
|
| 1414 | out:
|
| 1415 | class_unregister(&ubi_class);
|
| 1416 | pr_err("UBI error: cannot initialize UBI, error %d\n", err);
|
| 1417 | return err;
|
| 1418 | }
|
| 1419 | device_initcall(ubi_init);
|
| 1420 |
|
| 1421 |
|
| 1422 | static void __exit ubi_exit(void)
|
| 1423 | {
|
| 1424 | int i;
|
| 1425 |
|
| 1426 | ubiblock_exit();
|
| 1427 | unregister_mtd_user(&ubi_mtd_notifier);
|
| 1428 |
|
| 1429 | for (i = 0; i < UBI_MAX_DEVICES; i++)
|
| 1430 | if (ubi_devices[i]) {
|
| 1431 | mutex_lock(&ubi_devices_mutex);
|
| 1432 | ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
|
| 1433 | mutex_unlock(&ubi_devices_mutex);
|
| 1434 | }
|
| 1435 | ubi_debugfs_exit();
|
| 1436 | kmem_cache_destroy(ubi_wl_entry_slab);
|
| 1437 | misc_deregister(&ubi_ctrl_cdev);
|
| 1438 | class_unregister(&ubi_class);
|
| 1439 | }
|
| 1440 | module_exit(ubi_exit);
|
| 1441 |
|
| 1442 | |
| 1443 | * bytes_str_to_int - convert a number of bytes string into an integer.
|
| 1444 | * @str: the string to convert
|
| 1445 | *
|
| 1446 | * This function returns positive resulting integer in case of success and a
|
| 1447 | * negative error code in case of failure.
|
| 1448 |
|
| 1449 | static int bytes_str_to_int(const char *str)
|
| 1450 | {
|
| 1451 | char *endp;
|
| 1452 | unsigned long result;
|
| 1453 |
|
| 1454 | result = simple_strtoul(str, &endp, 0);
|
| 1455 | if (str == endp || result >= INT_MAX) {
|
| 1456 | pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
|
| 1457 | return -EINVAL;
|
| 1458 | }
|
| 1459 |
|
| 1460 | switch (*endp) {
|
| 1461 | case 'G':
|
| 1462 | result *= 1024;
|
| 1463 | fallthrough;
|
| 1464 | case 'M':
|
| 1465 | result *= 1024;
|
| 1466 | fallthrough;
|
| 1467 | case 'K':
|
| 1468 | result *= 1024;
|
| 1469 | break;
|
| 1470 | case '\0':
|
| 1471 | break;
|
| 1472 | default:
|
| 1473 | pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
|
| 1474 | return -EINVAL;
|
| 1475 | }
|
| 1476 |
|
| 1477 | return result;
|
| 1478 | }
|
| 1479 |
|
| 1480 | |
| 1481 | * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
|
| 1482 | * @val: the parameter value to parse
|
| 1483 | * @kp: not used
|
| 1484 | *
|
| 1485 | * This function returns zero in case of success and a negative error code in
|
| 1486 | * case of error.
|
| 1487 |
|
| 1488 | static int ubi_mtd_param_parse(const char *val, const struct kernel_param *kp)
|
| 1489 | {
|
| 1490 | int i, len;
|
| 1491 | struct mtd_dev_param *p;
|
| 1492 | char buf[MTD_PARAM_LEN_MAX];
|
| 1493 | char *pbuf = &buf[0];
|
| 1494 | char *tokens[MTD_PARAM_MAX_COUNT], *token;
|
| 1495 |
|
| 1496 | if (!val)
|
| 1497 | return -EINVAL;
|
| 1498 |
|
| 1499 | if (mtd_devs == UBI_MAX_DEVICES) {
|
| 1500 | pr_err("UBI error: too many parameters, max. is %d\n",
|
| 1501 | UBI_MAX_DEVICES);
|
| 1502 | return -EINVAL;
|
| 1503 | }
|
| 1504 |
|
| 1505 | len = strnlen(val, MTD_PARAM_LEN_MAX);
|
| 1506 | if (len == MTD_PARAM_LEN_MAX) {
|
| 1507 | pr_err("UBI error: parameter \"%s\" is too long, max. is %d\n",
|
| 1508 | val, MTD_PARAM_LEN_MAX);
|
| 1509 | return -EINVAL;
|
| 1510 | }
|
| 1511 |
|
| 1512 | if (len == 0) {
|
| 1513 | pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
|
| 1514 | return 0;
|
| 1515 | }
|
| 1516 |
|
| 1517 | strcpy(buf, val);
|
| 1518 |
|
| 1519 |
|
| 1520 | if (buf[len - 1] == '\n')
|
| 1521 | buf[len - 1] = '\0';
|
| 1522 |
|
| 1523 | for (i = 0; i < MTD_PARAM_MAX_COUNT; i++)
|
| 1524 | tokens[i] = strsep(&pbuf, ",");
|
| 1525 |
|
| 1526 | if (pbuf) {
|
| 1527 | pr_err("UBI error: too many arguments at \"%s\"\n", val);
|
| 1528 | return -EINVAL;
|
| 1529 | }
|
| 1530 |
|
| 1531 | p = &mtd_dev_param[mtd_devs];
|
| 1532 | strcpy(&p->name[0], tokens[0]);
|
| 1533 |
|
| 1534 | token = tokens[1];
|
| 1535 | if (token) {
|
| 1536 | p->vid_hdr_offs = bytes_str_to_int(token);
|
| 1537 |
|
| 1538 | if (p->vid_hdr_offs < 0)
|
| 1539 | return p->vid_hdr_offs;
|
| 1540 | }
|
| 1541 |
|
| 1542 | token = tokens[2];
|
| 1543 | if (token) {
|
| 1544 | int err = kstrtoint(token, 10, &p->max_beb_per1024);
|
| 1545 |
|
| 1546 | if (err) {
|
| 1547 | pr_err("UBI error: bad value for max_beb_per1024 parameter: %s\n",
|
| 1548 | token);
|
| 1549 | return -EINVAL;
|
| 1550 | }
|
| 1551 | }
|
| 1552 |
|
| 1553 | token = tokens[3];
|
| 1554 | if (token) {
|
| 1555 | int err = kstrtoint(token, 10, &p->ubi_num);
|
| 1556 |
|
| 1557 | if (err || p->ubi_num < UBI_DEV_NUM_AUTO) {
|
| 1558 | pr_err("UBI error: bad value for ubi_num parameter: %s\n",
|
| 1559 | token);
|
| 1560 | return -EINVAL;
|
| 1561 | }
|
| 1562 | } else
|
| 1563 | p->ubi_num = UBI_DEV_NUM_AUTO;
|
| 1564 |
|
| 1565 | token = tokens[4];
|
| 1566 | if (token) {
|
| 1567 | int err = kstrtoint(token, 10, &p->enable_fm);
|
| 1568 |
|
| 1569 | if (err) {
|
| 1570 | pr_err("UBI error: bad value for enable_fm parameter: %s\n",
|
| 1571 | token);
|
| 1572 | return -EINVAL;
|
| 1573 | }
|
| 1574 | } else
|
| 1575 | p->enable_fm = 0;
|
| 1576 |
|
| 1577 | token = tokens[5];
|
| 1578 | if (token) {
|
| 1579 | int err = kstrtoint(token, 10, &p->need_resv_pool);
|
| 1580 |
|
| 1581 | if (err) {
|
| 1582 | pr_err("UBI error: bad value for need_resv_pool parameter: %s\n",
|
| 1583 | token);
|
| 1584 | return -EINVAL;
|
| 1585 | }
|
| 1586 | } else
|
| 1587 | p->need_resv_pool = 0;
|
| 1588 |
|
| 1589 | token = tokens[6];
|
| 1590 | if (token) {
|
| 1591 | int err = kstrtoint(token, 10, &p->wl_threshold);
|
| 1592 |
|
| 1593 | if (err) {
|
| 1594 | pr_err("UBI error: bad value for wl_threshold parameter: %s\n",
|
| 1595 | token);
|
| 1596 | return -EINVAL;
|
| 1597 | }
|
| 1598 | } else
|
| 1599 | p->wl_threshold = 0;
|
| 1600 |
|
| 1601 | mtd_devs += 1;
|
| 1602 | return 0;
|
| 1603 | }
|
| 1604 |
|
| 1605 | module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 0400);
|
| 1606 | MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024[,ubi_num[,enable_fm[,need_resv_pool[,wl_threshold]]]]]].\n"
|
| 1607 | "Multiple \"mtd\" parameters may be specified.\n"
|
| 1608 | "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
|
| 1609 | "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
|
| 1610 | "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
|
| 1611 | __stringify(CONFIG_MTD_UBI_BEB_LIMIT) ") if 0)\n"
|
| 1612 | "Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly created UBI device (assigned automatically by default)\n"
|
| 1613 | "Optional \"enable_fm\" parameter determines whether to enable fastmap during attach. If the value is non-zero, fastmap is enabled. Default value is 0.\n"
|
| 1614 | "Optional \"need_resv_pool\" parameter determines whether to reserve pool->max_size pebs during attach. If the value is non-zero, peb reservation is enabled. Default value is 0.\n"
|
| 1615 | "\n"
|
| 1616 | "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
|
| 1617 | "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"
|
| 1618 | "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"
|
| 1619 | "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).\n"
|
| 1620 | "Example 4: mtd=/dev/mtd1,0,0,5 - attach MTD device /dev/mtd1 to UBI 5 and using default values for the other fields.\n"
|
| 1621 | "example 5: mtd=1,0,0,5 mtd=2,0,0,6,1 - attach MTD device /dev/mtd1 to UBI 5 and disable fastmap; attach MTD device /dev/mtd2 to UBI 6 and enable fastmap.(only works when fastmap is enabled and fm_autoconvert=Y).\n"
|
| 1622 | "Example 6: mtd=/dev/mtd0,0,0,0,0,0,256 mtd=/dev/mtd1,0,0,0,0,0,4096 - attach MTD device /dev/mtd0 with wear-leveling threshold 256, and MTD device /dev/mtd1 with threshold 4096.\n");
|
| 1623 | #ifdef CONFIG_MTD_UBI_FASTMAP
|
| 1624 | module_param(fm_autoconvert, bool, 0644);
|
| 1625 | MODULE_PARM_DESC(fm_autoconvert, "Set this parameter to enable fastmap automatically on images without a fastmap.");
|
| 1626 | module_param(fm_debug, bool, 0);
|
| 1627 | MODULE_PARM_DESC(fm_debug, "Set this parameter to enable fastmap debugging by default. Warning, this will make fastmap slow!");
|
| 1628 | #endif
|
| 1629 | MODULE_VERSION(__stringify(UBI_VERSION));
|
| 1630 | MODULE_DESCRIPTION("UBI - Unsorted Block Images");
|
| 1631 | MODULE_AUTHOR("Artem Bityutskiy");
|
| 1632 | MODULE_LICENSE("GPL");
|