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