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