Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2006 |
| 3 | * Copyright (c) Nokia Corporation, 2007 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | * Author: Artem Bityutskiy (Битюцкий Артём), |
| 20 | * Frank Haverkamp |
| 21 | */ |
| 22 | |
| 23 | /* |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 24 | * This file includes UBI initialization and building of UBI devices. |
| 25 | * |
| 26 | * When UBI is initialized, it attaches all the MTD devices specified as the |
| 27 | * module load parameters or the kernel boot parameters. If MTD devices were |
| 28 | * specified, UBI does not attach any MTD device, but it is possible to do |
| 29 | * later using the "UBI control device". |
| 30 | * |
| 31 | * At the moment we only attach UBI devices by scanning, which will become a |
| 32 | * bottleneck when flashes reach certain large size. Then one may improve UBI |
| 33 | * and add other methods, although it does not seem to be easy to do. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #include <linux/err.h> |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/moduleparam.h> |
| 39 | #include <linux/stringify.h> |
| 40 | #include <linux/stat.h> |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 41 | #include <linux/miscdevice.h> |
Vignesh Babu | 7753f16 | 2007-06-12 10:31:05 +0530 | [diff] [blame] | 42 | #include <linux/log2.h> |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 43 | #include "ubi.h" |
| 44 | |
| 45 | /* Maximum length of the 'mtd=' parameter */ |
| 46 | #define MTD_PARAM_LEN_MAX 64 |
| 47 | |
| 48 | /** |
| 49 | * struct mtd_dev_param - MTD device parameter description data structure. |
| 50 | * @name: MTD device name or number string |
| 51 | * @vid_hdr_offs: VID header offset |
| 52 | * @data_offs: data offset |
| 53 | */ |
| 54 | struct mtd_dev_param |
| 55 | { |
| 56 | char name[MTD_PARAM_LEN_MAX]; |
| 57 | int vid_hdr_offs; |
| 58 | int data_offs; |
| 59 | }; |
| 60 | |
| 61 | /* Numbers of elements set in the @mtd_dev_param array */ |
| 62 | static int mtd_devs = 0; |
| 63 | |
| 64 | /* MTD devices specification parameters */ |
| 65 | static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES]; |
| 66 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 67 | /* All UBI devices in system */ |
| 68 | struct ubi_device *ubi_devices[UBI_MAX_DEVICES]; |
| 69 | |
| 70 | /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */ |
| 71 | struct class *ubi_class; |
| 72 | |
Artem Bityutskiy | 3a8d464 | 2007-12-16 12:32:51 +0200 | [diff] [blame] | 73 | /* Slab cache for lock-tree entries */ |
| 74 | struct kmem_cache *ubi_ltree_slab; |
| 75 | |
Artem Bityutskiy | 06b68ba | 2007-12-16 12:49:01 +0200 | [diff] [blame] | 76 | /* Slab cache for wear-leveling entries */ |
| 77 | struct kmem_cache *ubi_wl_entry_slab; |
| 78 | |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 79 | /* UBI control character device */ |
| 80 | static struct miscdevice ubi_ctrl_cdev = { |
| 81 | .minor = MISC_DYNAMIC_MINOR, |
| 82 | .name = "ubi_ctrl", |
| 83 | .fops = &ubi_ctrl_cdev_operations, |
| 84 | }; |
Artem Bityutskiy | 06b68ba | 2007-12-16 12:49:01 +0200 | [diff] [blame] | 85 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 86 | /* "Show" method for files in '/<sysfs>/class/ubi/' */ |
| 87 | static ssize_t ubi_version_show(struct class *class, char *buf) |
| 88 | { |
| 89 | return sprintf(buf, "%d\n", UBI_VERSION); |
| 90 | } |
| 91 | |
| 92 | /* UBI version attribute ('/<sysfs>/class/ubi/version') */ |
| 93 | static struct class_attribute ubi_version = |
| 94 | __ATTR(version, S_IRUGO, ubi_version_show, NULL); |
| 95 | |
| 96 | static ssize_t dev_attribute_show(struct device *dev, |
| 97 | struct device_attribute *attr, char *buf); |
| 98 | |
| 99 | /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */ |
| 100 | static struct device_attribute dev_eraseblock_size = |
| 101 | __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL); |
| 102 | static struct device_attribute dev_avail_eraseblocks = |
| 103 | __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL); |
| 104 | static struct device_attribute dev_total_eraseblocks = |
| 105 | __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL); |
| 106 | static struct device_attribute dev_volumes_count = |
| 107 | __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL); |
| 108 | static struct device_attribute dev_max_ec = |
| 109 | __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL); |
| 110 | static struct device_attribute dev_reserved_for_bad = |
| 111 | __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL); |
| 112 | static struct device_attribute dev_bad_peb_count = |
| 113 | __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL); |
| 114 | static struct device_attribute dev_max_vol_count = |
| 115 | __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL); |
| 116 | static struct device_attribute dev_min_io_size = |
| 117 | __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL); |
| 118 | static struct device_attribute dev_bgt_enabled = |
| 119 | __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL); |
| 120 | |
| 121 | /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */ |
| 122 | static ssize_t dev_attribute_show(struct device *dev, |
| 123 | struct device_attribute *attr, char *buf) |
| 124 | { |
| 125 | const struct ubi_device *ubi; |
| 126 | |
| 127 | ubi = container_of(dev, struct ubi_device, dev); |
| 128 | if (attr == &dev_eraseblock_size) |
| 129 | return sprintf(buf, "%d\n", ubi->leb_size); |
| 130 | else if (attr == &dev_avail_eraseblocks) |
| 131 | return sprintf(buf, "%d\n", ubi->avail_pebs); |
| 132 | else if (attr == &dev_total_eraseblocks) |
| 133 | return sprintf(buf, "%d\n", ubi->good_peb_count); |
| 134 | else if (attr == &dev_volumes_count) |
| 135 | return sprintf(buf, "%d\n", ubi->vol_count); |
| 136 | else if (attr == &dev_max_ec) |
| 137 | return sprintf(buf, "%d\n", ubi->max_ec); |
| 138 | else if (attr == &dev_reserved_for_bad) |
| 139 | return sprintf(buf, "%d\n", ubi->beb_rsvd_pebs); |
| 140 | else if (attr == &dev_bad_peb_count) |
| 141 | return sprintf(buf, "%d\n", ubi->bad_peb_count); |
| 142 | else if (attr == &dev_max_vol_count) |
| 143 | return sprintf(buf, "%d\n", ubi->vtbl_slots); |
| 144 | else if (attr == &dev_min_io_size) |
| 145 | return sprintf(buf, "%d\n", ubi->min_io_size); |
| 146 | else if (attr == &dev_bgt_enabled) |
| 147 | return sprintf(buf, "%d\n", ubi->thread_enabled); |
| 148 | else |
| 149 | BUG(); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /* Fake "release" method for UBI devices */ |
| 155 | static void dev_release(struct device *dev) { } |
| 156 | |
| 157 | /** |
| 158 | * ubi_sysfs_init - initialize sysfs for an UBI device. |
| 159 | * @ubi: UBI device description object |
| 160 | * |
| 161 | * This function returns zero in case of success and a negative error code in |
| 162 | * case of failure. |
| 163 | */ |
| 164 | static int ubi_sysfs_init(struct ubi_device *ubi) |
| 165 | { |
| 166 | int err; |
| 167 | |
| 168 | ubi->dev.release = dev_release; |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 169 | ubi->dev.devt = ubi->cdev.dev; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 170 | ubi->dev.class = ubi_class; |
| 171 | sprintf(&ubi->dev.bus_id[0], UBI_NAME_STR"%d", ubi->ubi_num); |
| 172 | err = device_register(&ubi->dev); |
| 173 | if (err) |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 174 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 175 | |
| 176 | err = device_create_file(&ubi->dev, &dev_eraseblock_size); |
| 177 | if (err) |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 178 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 179 | err = device_create_file(&ubi->dev, &dev_avail_eraseblocks); |
| 180 | if (err) |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 181 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 182 | err = device_create_file(&ubi->dev, &dev_total_eraseblocks); |
| 183 | if (err) |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 184 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 185 | err = device_create_file(&ubi->dev, &dev_volumes_count); |
| 186 | if (err) |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 187 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 188 | err = device_create_file(&ubi->dev, &dev_max_ec); |
| 189 | if (err) |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 190 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 191 | err = device_create_file(&ubi->dev, &dev_reserved_for_bad); |
| 192 | if (err) |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 193 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 194 | err = device_create_file(&ubi->dev, &dev_bad_peb_count); |
| 195 | if (err) |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 196 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 197 | err = device_create_file(&ubi->dev, &dev_max_vol_count); |
| 198 | if (err) |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 199 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 200 | err = device_create_file(&ubi->dev, &dev_min_io_size); |
| 201 | if (err) |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 202 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 203 | err = device_create_file(&ubi->dev, &dev_bgt_enabled); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 204 | return err; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * ubi_sysfs_close - close sysfs for an UBI device. |
| 209 | * @ubi: UBI device description object |
| 210 | */ |
| 211 | static void ubi_sysfs_close(struct ubi_device *ubi) |
| 212 | { |
| 213 | device_remove_file(&ubi->dev, &dev_bgt_enabled); |
| 214 | device_remove_file(&ubi->dev, &dev_min_io_size); |
| 215 | device_remove_file(&ubi->dev, &dev_max_vol_count); |
| 216 | device_remove_file(&ubi->dev, &dev_bad_peb_count); |
| 217 | device_remove_file(&ubi->dev, &dev_reserved_for_bad); |
| 218 | device_remove_file(&ubi->dev, &dev_max_ec); |
| 219 | device_remove_file(&ubi->dev, &dev_volumes_count); |
| 220 | device_remove_file(&ubi->dev, &dev_total_eraseblocks); |
| 221 | device_remove_file(&ubi->dev, &dev_avail_eraseblocks); |
| 222 | device_remove_file(&ubi->dev, &dev_eraseblock_size); |
| 223 | device_unregister(&ubi->dev); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * kill_volumes - destroy all volumes. |
| 228 | * @ubi: UBI device description object |
| 229 | */ |
| 230 | static void kill_volumes(struct ubi_device *ubi) |
| 231 | { |
| 232 | int i; |
| 233 | |
| 234 | for (i = 0; i < ubi->vtbl_slots; i++) |
| 235 | if (ubi->volumes[i]) |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 236 | ubi_free_volume(ubi, ubi->volumes[i]); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * uif_init - initialize user interfaces for an UBI device. |
| 241 | * @ubi: UBI device description object |
| 242 | * |
| 243 | * This function returns zero in case of success and a negative error code in |
| 244 | * case of failure. |
| 245 | */ |
| 246 | static int uif_init(struct ubi_device *ubi) |
| 247 | { |
| 248 | int i, err; |
| 249 | dev_t dev; |
| 250 | |
Artem Bityutskiy | cae0a77 | 2007-12-17 12:46:48 +0200 | [diff] [blame] | 251 | mutex_init(&ubi->volumes_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 252 | spin_lock_init(&ubi->volumes_lock); |
| 253 | |
| 254 | sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num); |
| 255 | |
| 256 | /* |
| 257 | * Major numbers for the UBI character devices are allocated |
| 258 | * dynamically. Major numbers of volume character devices are |
| 259 | * equivalent to ones of the corresponding UBI character device. Minor |
| 260 | * numbers of UBI character devices are 0, while minor numbers of |
| 261 | * volume character devices start from 1. Thus, we allocate one major |
| 262 | * number and ubi->vtbl_slots + 1 minor numbers. |
| 263 | */ |
| 264 | err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name); |
| 265 | if (err) { |
| 266 | ubi_err("cannot register UBI character devices"); |
| 267 | return err; |
| 268 | } |
| 269 | |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 270 | ubi_assert(MINOR(dev) == 0); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 271 | cdev_init(&ubi->cdev, &ubi_cdev_operations); |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 272 | dbg_msg("%s major is %u", ubi->ubi_name, MAJOR(dev)); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 273 | ubi->cdev.owner = THIS_MODULE; |
| 274 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 275 | err = cdev_add(&ubi->cdev, dev, 1); |
| 276 | if (err) { |
Artem Bityutskiy | 01f7b30 | 2007-12-15 19:56:51 +0200 | [diff] [blame] | 277 | ubi_err("cannot add character device"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 278 | goto out_unreg; |
| 279 | } |
| 280 | |
| 281 | err = ubi_sysfs_init(ubi); |
| 282 | if (err) |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 283 | goto out_sysfs; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 284 | |
| 285 | for (i = 0; i < ubi->vtbl_slots; i++) |
| 286 | if (ubi->volumes[i]) { |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 287 | err = ubi_add_volume(ubi, ubi->volumes[i]); |
Artem Bityutskiy | 01f7b30 | 2007-12-15 19:56:51 +0200 | [diff] [blame] | 288 | if (err) { |
| 289 | ubi_err("cannot add volume %d", i); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 290 | goto out_volumes; |
Artem Bityutskiy | 01f7b30 | 2007-12-15 19:56:51 +0200 | [diff] [blame] | 291 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | |
| 296 | out_volumes: |
| 297 | kill_volumes(ubi); |
Artem Bityutskiy | db6e577 | 2007-12-17 15:48:49 +0200 | [diff] [blame] | 298 | out_sysfs: |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 299 | ubi_sysfs_close(ubi); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 300 | cdev_del(&ubi->cdev); |
| 301 | out_unreg: |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 302 | unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1); |
Artem Bityutskiy | 01f7b30 | 2007-12-15 19:56:51 +0200 | [diff] [blame] | 303 | ubi_err("cannot initialize UBI %s, error %d", ubi->ubi_name, err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 304 | return err; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * uif_close - close user interfaces for an UBI device. |
| 309 | * @ubi: UBI device description object |
| 310 | */ |
| 311 | static void uif_close(struct ubi_device *ubi) |
| 312 | { |
| 313 | kill_volumes(ubi); |
| 314 | ubi_sysfs_close(ubi); |
| 315 | cdev_del(&ubi->cdev); |
Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 316 | unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /** |
| 320 | * attach_by_scanning - attach an MTD device using scanning method. |
| 321 | * @ubi: UBI device descriptor |
| 322 | * |
| 323 | * This function returns zero in case of success and a negative error code in |
| 324 | * case of failure. |
| 325 | * |
| 326 | * Note, currently this is the only method to attach UBI devices. Hopefully in |
| 327 | * the future we'll have more scalable attaching methods and avoid full media |
| 328 | * scanning. But even in this case scanning will be needed as a fall-back |
| 329 | * attaching method if there are some on-flash table corruptions. |
| 330 | */ |
| 331 | static int attach_by_scanning(struct ubi_device *ubi) |
| 332 | { |
| 333 | int err; |
| 334 | struct ubi_scan_info *si; |
| 335 | |
| 336 | si = ubi_scan(ubi); |
| 337 | if (IS_ERR(si)) |
| 338 | return PTR_ERR(si); |
| 339 | |
| 340 | ubi->bad_peb_count = si->bad_peb_count; |
| 341 | ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count; |
| 342 | ubi->max_ec = si->max_ec; |
| 343 | ubi->mean_ec = si->mean_ec; |
| 344 | |
| 345 | err = ubi_read_volume_table(ubi, si); |
| 346 | if (err) |
| 347 | goto out_si; |
| 348 | |
| 349 | err = ubi_wl_init_scan(ubi, si); |
| 350 | if (err) |
| 351 | goto out_vtbl; |
| 352 | |
| 353 | err = ubi_eba_init_scan(ubi, si); |
| 354 | if (err) |
| 355 | goto out_wl; |
| 356 | |
| 357 | ubi_scan_destroy_si(si); |
| 358 | return 0; |
| 359 | |
| 360 | out_wl: |
| 361 | ubi_wl_close(ubi); |
| 362 | out_vtbl: |
Vinit Agnihotri | d7f0c4d | 2007-06-15 15:31:22 +0530 | [diff] [blame] | 363 | vfree(ubi->vtbl); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 364 | out_si: |
| 365 | ubi_scan_destroy_si(si); |
| 366 | return err; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * io_init - initialize I/O unit for a given UBI device. |
| 371 | * @ubi: UBI device description object |
| 372 | * |
| 373 | * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are |
| 374 | * assumed: |
| 375 | * o EC header is always at offset zero - this cannot be changed; |
| 376 | * o VID header starts just after the EC header at the closest address |
| 377 | * aligned to @io->@hdrs_min_io_size; |
| 378 | * o data starts just after the VID header at the closest address aligned to |
| 379 | * @io->@min_io_size |
| 380 | * |
| 381 | * This function returns zero in case of success and a negative error code in |
| 382 | * case of failure. |
| 383 | */ |
| 384 | static int io_init(struct ubi_device *ubi) |
| 385 | { |
| 386 | if (ubi->mtd->numeraseregions != 0) { |
| 387 | /* |
| 388 | * Some flashes have several erase regions. Different regions |
| 389 | * may have different eraseblock size and other |
| 390 | * characteristics. It looks like mostly multi-region flashes |
| 391 | * have one "main" region and one or more small regions to |
| 392 | * store boot loader code or boot parameters or whatever. I |
| 393 | * guess we should just pick the largest region. But this is |
| 394 | * not implemented. |
| 395 | */ |
| 396 | ubi_err("multiple regions, not implemented"); |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Note, in this implementation we support MTD devices with 0x7FFFFFFF |
| 402 | * physical eraseblocks maximum. |
| 403 | */ |
| 404 | |
| 405 | ubi->peb_size = ubi->mtd->erasesize; |
| 406 | ubi->peb_count = ubi->mtd->size / ubi->mtd->erasesize; |
| 407 | ubi->flash_size = ubi->mtd->size; |
| 408 | |
| 409 | if (ubi->mtd->block_isbad && ubi->mtd->block_markbad) |
| 410 | ubi->bad_allowed = 1; |
| 411 | |
| 412 | ubi->min_io_size = ubi->mtd->writesize; |
| 413 | ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft; |
| 414 | |
| 415 | /* Make sure minimal I/O unit is power of 2 */ |
Vignesh Babu | 7753f16 | 2007-06-12 10:31:05 +0530 | [diff] [blame] | 416 | if (!is_power_of_2(ubi->min_io_size)) { |
Artem Bityutskiy | 01f7b30 | 2007-12-15 19:56:51 +0200 | [diff] [blame] | 417 | ubi_err("min. I/O unit (%d) is not power of 2", |
| 418 | ubi->min_io_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 419 | return -EINVAL; |
| 420 | } |
| 421 | |
| 422 | ubi_assert(ubi->hdrs_min_io_size > 0); |
| 423 | ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size); |
| 424 | ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0); |
| 425 | |
| 426 | /* Calculate default aligned sizes of EC and VID headers */ |
| 427 | ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size); |
| 428 | ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size); |
| 429 | |
| 430 | dbg_msg("min_io_size %d", ubi->min_io_size); |
| 431 | dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size); |
| 432 | dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize); |
| 433 | dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize); |
| 434 | |
| 435 | if (ubi->vid_hdr_offset == 0) |
| 436 | /* Default offset */ |
| 437 | ubi->vid_hdr_offset = ubi->vid_hdr_aloffset = |
| 438 | ubi->ec_hdr_alsize; |
| 439 | else { |
| 440 | ubi->vid_hdr_aloffset = ubi->vid_hdr_offset & |
| 441 | ~(ubi->hdrs_min_io_size - 1); |
| 442 | ubi->vid_hdr_shift = ubi->vid_hdr_offset - |
| 443 | ubi->vid_hdr_aloffset; |
| 444 | } |
| 445 | |
| 446 | /* Similar for the data offset */ |
| 447 | if (ubi->leb_start == 0) { |
| 448 | ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize; |
| 449 | ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size); |
| 450 | } |
| 451 | |
| 452 | dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset); |
| 453 | dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset); |
| 454 | dbg_msg("vid_hdr_shift %d", ubi->vid_hdr_shift); |
| 455 | dbg_msg("leb_start %d", ubi->leb_start); |
| 456 | |
| 457 | /* The shift must be aligned to 32-bit boundary */ |
| 458 | if (ubi->vid_hdr_shift % 4) { |
| 459 | ubi_err("unaligned VID header shift %d", |
| 460 | ubi->vid_hdr_shift); |
| 461 | return -EINVAL; |
| 462 | } |
| 463 | |
| 464 | /* Check sanity */ |
| 465 | if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE || |
| 466 | ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE || |
| 467 | ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE || |
| 468 | ubi->leb_start % ubi->min_io_size) { |
| 469 | ubi_err("bad VID header (%d) or data offsets (%d)", |
| 470 | ubi->vid_hdr_offset, ubi->leb_start); |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * It may happen that EC and VID headers are situated in one minimal |
| 476 | * I/O unit. In this case we can only accept this UBI image in |
| 477 | * read-only mode. |
| 478 | */ |
| 479 | if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) { |
| 480 | ubi_warn("EC and VID headers are in the same minimal I/O unit, " |
| 481 | "switch to read-only mode"); |
| 482 | ubi->ro_mode = 1; |
| 483 | } |
| 484 | |
| 485 | ubi->leb_size = ubi->peb_size - ubi->leb_start; |
| 486 | |
| 487 | if (!(ubi->mtd->flags & MTD_WRITEABLE)) { |
| 488 | ubi_msg("MTD device %d is write-protected, attach in " |
| 489 | "read-only mode", ubi->mtd->index); |
| 490 | ubi->ro_mode = 1; |
| 491 | } |
| 492 | |
| 493 | dbg_msg("leb_size %d", ubi->leb_size); |
| 494 | dbg_msg("ro_mode %d", ubi->ro_mode); |
| 495 | |
| 496 | /* |
| 497 | * Note, ideally, we have to initialize ubi->bad_peb_count here. But |
| 498 | * unfortunately, MTD does not provide this information. We should loop |
| 499 | * over all physical eraseblocks and invoke mtd->block_is_bad() for |
| 500 | * each physical eraseblock. So, we skip ubi->bad_peb_count |
| 501 | * uninitialized and initialize it after scanning. |
| 502 | */ |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * attach_mtd_dev - attach an MTD device. |
| 509 | * @mtd_dev: MTD device name or number string |
| 510 | * @vid_hdr_offset: VID header offset |
| 511 | * @data_offset: data offset |
| 512 | * |
| 513 | * This function attaches an MTD device to UBI. It first treats @mtd_dev as the |
| 514 | * MTD device name, and tries to open it by this name. If it is unable to open, |
| 515 | * it tries to convert @mtd_dev to an integer and open the MTD device by its |
| 516 | * number. Returns zero in case of success and a negative error code in case of |
| 517 | * failure. |
| 518 | */ |
| 519 | static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset, |
| 520 | int data_offset) |
| 521 | { |
| 522 | struct ubi_device *ubi; |
| 523 | struct mtd_info *mtd; |
| 524 | int i, err; |
| 525 | |
| 526 | mtd = get_mtd_device_nm(mtd_dev); |
| 527 | if (IS_ERR(mtd)) { |
| 528 | int mtd_num; |
| 529 | char *endp; |
| 530 | |
| 531 | if (PTR_ERR(mtd) != -ENODEV) |
| 532 | return PTR_ERR(mtd); |
| 533 | |
| 534 | /* |
| 535 | * Probably this is not MTD device name but MTD device number - |
| 536 | * check this out. |
| 537 | */ |
| 538 | mtd_num = simple_strtoul(mtd_dev, &endp, 0); |
| 539 | if (*endp != '\0' || mtd_dev == endp) { |
| 540 | ubi_err("incorrect MTD device: \"%s\"", mtd_dev); |
| 541 | return -ENODEV; |
| 542 | } |
| 543 | |
| 544 | mtd = get_mtd_device(NULL, mtd_num); |
| 545 | if (IS_ERR(mtd)) |
| 546 | return PTR_ERR(mtd); |
| 547 | } |
| 548 | |
| 549 | /* Check if we already have the same MTD device attached */ |
Artem Bityutskiy | b96bf4c | 2007-12-16 13:01:03 +0200 | [diff] [blame] | 550 | for (i = 0; i < UBI_MAX_DEVICES; i++) |
| 551 | ubi = ubi_devices[i]; |
| 552 | if (ubi && ubi->mtd->index == mtd->index) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 553 | ubi_err("mtd%d is already attached to ubi%d", |
| 554 | mtd->index, i); |
| 555 | err = -EINVAL; |
| 556 | goto out_mtd; |
| 557 | } |
| 558 | |
Artem Bityutskiy | b96bf4c | 2007-12-16 13:01:03 +0200 | [diff] [blame] | 559 | ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 560 | if (!ubi) { |
| 561 | err = -ENOMEM; |
| 562 | goto out_mtd; |
| 563 | } |
| 564 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 565 | ubi->mtd = mtd; |
| 566 | |
Artem Bityutskiy | b96bf4c | 2007-12-16 13:01:03 +0200 | [diff] [blame] | 567 | /* Search for an empty slot in the @ubi_devices array */ |
| 568 | ubi->ubi_num = -1; |
| 569 | for (i = 0; i < UBI_MAX_DEVICES; i++) |
| 570 | if (!ubi_devices[i]) { |
| 571 | ubi->ubi_num = i; |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | if (ubi->ubi_num == -1) { |
| 576 | ubi_err("only %d UBI devices may be created", UBI_MAX_DEVICES); |
| 577 | err = -ENFILE; |
| 578 | goto out_free; |
| 579 | } |
| 580 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 581 | dbg_msg("attaching mtd%d to ubi%d: VID header offset %d data offset %d", |
Artem Bityutskiy | b96bf4c | 2007-12-16 13:01:03 +0200 | [diff] [blame] | 582 | ubi->mtd->index, ubi->ubi_num, vid_hdr_offset, data_offset); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 583 | |
| 584 | ubi->vid_hdr_offset = vid_hdr_offset; |
| 585 | ubi->leb_start = data_offset; |
| 586 | err = io_init(ubi); |
| 587 | if (err) |
| 588 | goto out_free; |
| 589 | |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 590 | mutex_init(&ubi->buf_mutex); |
| 591 | ubi->peb_buf1 = vmalloc(ubi->peb_size); |
| 592 | if (!ubi->peb_buf1) |
| 593 | goto out_free; |
| 594 | |
| 595 | ubi->peb_buf2 = vmalloc(ubi->peb_size); |
| 596 | if (!ubi->peb_buf2) |
| 597 | goto out_free; |
| 598 | |
| 599 | #ifdef CONFIG_MTD_UBI_DEBUG |
| 600 | mutex_init(&ubi->dbg_buf_mutex); |
| 601 | ubi->dbg_peb_buf = vmalloc(ubi->peb_size); |
| 602 | if (!ubi->dbg_peb_buf) |
| 603 | goto out_free; |
| 604 | #endif |
| 605 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 606 | err = attach_by_scanning(ubi); |
| 607 | if (err) { |
| 608 | dbg_err("failed to attach by scanning, error %d", err); |
| 609 | goto out_free; |
| 610 | } |
| 611 | |
| 612 | err = uif_init(ubi); |
| 613 | if (err) |
| 614 | goto out_detach; |
| 615 | |
Artem Bityutskiy | b96bf4c | 2007-12-16 13:01:03 +0200 | [diff] [blame] | 616 | ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi->ubi_num); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 617 | ubi_msg("MTD device name: \"%s\"", ubi->mtd->name); |
| 618 | ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20); |
| 619 | ubi_msg("physical eraseblock size: %d bytes (%d KiB)", |
| 620 | ubi->peb_size, ubi->peb_size >> 10); |
| 621 | ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size); |
| 622 | ubi_msg("number of good PEBs: %d", ubi->good_peb_count); |
| 623 | ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count); |
| 624 | ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size); |
| 625 | ubi_msg("VID header offset: %d (aligned %d)", |
| 626 | ubi->vid_hdr_offset, ubi->vid_hdr_aloffset); |
| 627 | ubi_msg("data offset: %d", ubi->leb_start); |
| 628 | ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots); |
| 629 | ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD); |
| 630 | ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT); |
| 631 | ubi_msg("number of user volumes: %d", |
| 632 | ubi->vol_count - UBI_INT_VOL_COUNT); |
| 633 | ubi_msg("available PEBs: %d", ubi->avail_pebs); |
| 634 | ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs); |
| 635 | ubi_msg("number of PEBs reserved for bad PEB handling: %d", |
| 636 | ubi->beb_rsvd_pebs); |
| 637 | ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec); |
| 638 | |
| 639 | /* Enable the background thread */ |
| 640 | if (!DBG_DISABLE_BGT) { |
| 641 | ubi->thread_enabled = 1; |
| 642 | wake_up_process(ubi->bgt_thread); |
| 643 | } |
| 644 | |
Artem Bityutskiy | b96bf4c | 2007-12-16 13:01:03 +0200 | [diff] [blame] | 645 | ubi_devices[ubi->ubi_num] = ubi; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 646 | return 0; |
| 647 | |
| 648 | out_detach: |
| 649 | ubi_eba_close(ubi); |
| 650 | ubi_wl_close(ubi); |
Vinit Agnihotri | d7f0c4d | 2007-06-15 15:31:22 +0530 | [diff] [blame] | 651 | vfree(ubi->vtbl); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 652 | out_free: |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 653 | vfree(ubi->peb_buf1); |
| 654 | vfree(ubi->peb_buf2); |
| 655 | #ifdef CONFIG_MTD_UBI_DEBUG |
| 656 | vfree(ubi->dbg_peb_buf); |
| 657 | #endif |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 658 | kfree(ubi); |
| 659 | out_mtd: |
| 660 | put_mtd_device(mtd); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 661 | return err; |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * detach_mtd_dev - detach an MTD device. |
| 666 | * @ubi: UBI device description object |
| 667 | */ |
| 668 | static void detach_mtd_dev(struct ubi_device *ubi) |
| 669 | { |
| 670 | int ubi_num = ubi->ubi_num, mtd_num = ubi->mtd->index; |
| 671 | |
| 672 | dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num); |
| 673 | uif_close(ubi); |
| 674 | ubi_eba_close(ubi); |
| 675 | ubi_wl_close(ubi); |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 676 | vfree(ubi->vtbl); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 677 | put_mtd_device(ubi->mtd); |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 678 | vfree(ubi->peb_buf1); |
| 679 | vfree(ubi->peb_buf2); |
| 680 | #ifdef CONFIG_MTD_UBI_DEBUG |
| 681 | vfree(ubi->dbg_peb_buf); |
| 682 | #endif |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 683 | kfree(ubi_devices[ubi_num]); |
| 684 | ubi_devices[ubi_num] = NULL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 685 | ubi_msg("mtd%d is detached from ubi%d", mtd_num, ubi_num); |
| 686 | } |
| 687 | |
Artem Bityutskiy | 3a8d464 | 2007-12-16 12:32:51 +0200 | [diff] [blame] | 688 | /** |
| 689 | * ltree_entry_ctor - lock tree entries slab cache constructor. |
| 690 | * @obj: the lock-tree entry to construct |
| 691 | * @cache: the lock tree entry slab cache |
| 692 | * @flags: constructor flags |
| 693 | */ |
| 694 | static void ltree_entry_ctor(struct kmem_cache *cache, void *obj) |
| 695 | { |
| 696 | struct ubi_ltree_entry *le = obj; |
| 697 | |
| 698 | le->users = 0; |
| 699 | init_rwsem(&le->mutex); |
| 700 | } |
| 701 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 702 | static int __init ubi_init(void) |
| 703 | { |
| 704 | int err, i, k; |
| 705 | |
| 706 | /* Ensure that EC and VID headers have correct size */ |
| 707 | BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64); |
| 708 | BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64); |
| 709 | |
| 710 | if (mtd_devs > UBI_MAX_DEVICES) { |
Artem Bityutskiy | 458dbb3 | 2007-12-19 17:03:42 +0200 | [diff] [blame] | 711 | printk(KERN_ERR "UBI error: too many MTD devices, " |
| 712 | "maximum is %d\n", UBI_MAX_DEVICES); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 713 | return -EINVAL; |
| 714 | } |
| 715 | |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 716 | /* Create base sysfs directory and sysfs files */ |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 717 | ubi_class = class_create(THIS_MODULE, UBI_NAME_STR); |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 718 | if (IS_ERR(ubi_class)) { |
| 719 | err = PTR_ERR(ubi_class); |
| 720 | printk(KERN_ERR "UBI error: cannot create UBI class\n"); |
| 721 | goto out; |
| 722 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 723 | |
| 724 | err = class_create_file(ubi_class, &ubi_version); |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 725 | if (err) { |
| 726 | printk(KERN_ERR "UBI error: cannot create sysfs file\n"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 727 | goto out_class; |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 728 | } |
| 729 | |
| 730 | err = misc_register(&ubi_ctrl_cdev); |
| 731 | if (err) { |
| 732 | printk(KERN_ERR "UBI error: cannot register device\n"); |
| 733 | goto out_version; |
| 734 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 735 | |
Artem Bityutskiy | 3a8d464 | 2007-12-16 12:32:51 +0200 | [diff] [blame] | 736 | ubi_ltree_slab = kmem_cache_create("ubi_ltree_slab", |
| 737 | sizeof(struct ubi_ltree_entry), 0, |
| 738 | 0, <ree_entry_ctor); |
| 739 | if (!ubi_ltree_slab) |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 740 | goto out_dev_unreg; |
Artem Bityutskiy | 3a8d464 | 2007-12-16 12:32:51 +0200 | [diff] [blame] | 741 | |
Artem Bityutskiy | 06b68ba | 2007-12-16 12:49:01 +0200 | [diff] [blame] | 742 | ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab", |
| 743 | sizeof(struct ubi_wl_entry), |
| 744 | 0, 0, NULL); |
| 745 | if (!ubi_wl_entry_slab) |
| 746 | goto out_ltree; |
| 747 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 748 | /* Attach MTD devices */ |
| 749 | for (i = 0; i < mtd_devs; i++) { |
| 750 | struct mtd_dev_param *p = &mtd_dev_param[i]; |
| 751 | |
| 752 | cond_resched(); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 753 | err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs); |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 754 | if (err) { |
| 755 | printk(KERN_ERR "UBI error: cannot attach %s\n", |
| 756 | p->name); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 757 | goto out_detach; |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 758 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | |
| 763 | out_detach: |
| 764 | for (k = 0; k < i; k++) |
| 765 | detach_mtd_dev(ubi_devices[k]); |
Artem Bityutskiy | 06b68ba | 2007-12-16 12:49:01 +0200 | [diff] [blame] | 766 | kmem_cache_destroy(ubi_wl_entry_slab); |
| 767 | out_ltree: |
Artem Bityutskiy | 3a8d464 | 2007-12-16 12:32:51 +0200 | [diff] [blame] | 768 | kmem_cache_destroy(ubi_ltree_slab); |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 769 | out_dev_unreg: |
| 770 | misc_deregister(&ubi_ctrl_cdev); |
Artem Bityutskiy | 3a8d464 | 2007-12-16 12:32:51 +0200 | [diff] [blame] | 771 | out_version: |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 772 | class_remove_file(ubi_class, &ubi_version); |
| 773 | out_class: |
| 774 | class_destroy(ubi_class); |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 775 | out: |
| 776 | printk(KERN_ERR "UBI error: cannot initialize UBI, error %d\n", err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 777 | return err; |
| 778 | } |
| 779 | module_init(ubi_init); |
| 780 | |
| 781 | static void __exit ubi_exit(void) |
| 782 | { |
Artem Bityutskiy | b96bf4c | 2007-12-16 13:01:03 +0200 | [diff] [blame] | 783 | int i; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 784 | |
Artem Bityutskiy | b96bf4c | 2007-12-16 13:01:03 +0200 | [diff] [blame] | 785 | for (i = 0; i < UBI_MAX_DEVICES; i++) |
| 786 | if (ubi_devices[i]) |
| 787 | detach_mtd_dev(ubi_devices[i]); |
Artem Bityutskiy | 06b68ba | 2007-12-16 12:49:01 +0200 | [diff] [blame] | 788 | kmem_cache_destroy(ubi_wl_entry_slab); |
Artem Bityutskiy | 3a8d464 | 2007-12-16 12:32:51 +0200 | [diff] [blame] | 789 | kmem_cache_destroy(ubi_ltree_slab); |
Artem Bityutskiy | 9f961b5 | 2007-12-16 16:59:31 +0200 | [diff] [blame^] | 790 | misc_deregister(&ubi_ctrl_cdev); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 791 | class_remove_file(ubi_class, &ubi_version); |
| 792 | class_destroy(ubi_class); |
| 793 | } |
| 794 | module_exit(ubi_exit); |
| 795 | |
| 796 | /** |
| 797 | * bytes_str_to_int - convert a string representing number of bytes to an |
| 798 | * integer. |
| 799 | * @str: the string to convert |
| 800 | * |
| 801 | * This function returns positive resulting integer in case of success and a |
| 802 | * negative error code in case of failure. |
| 803 | */ |
| 804 | static int __init bytes_str_to_int(const char *str) |
| 805 | { |
| 806 | char *endp; |
| 807 | unsigned long result; |
| 808 | |
| 809 | result = simple_strtoul(str, &endp, 0); |
| 810 | if (str == endp || result < 0) { |
Artem Bityutskiy | 458dbb3 | 2007-12-19 17:03:42 +0200 | [diff] [blame] | 811 | printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n", |
| 812 | str); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 813 | return -EINVAL; |
| 814 | } |
| 815 | |
| 816 | switch (*endp) { |
| 817 | case 'G': |
| 818 | result *= 1024; |
| 819 | case 'M': |
| 820 | result *= 1024; |
| 821 | case 'K': |
| 822 | case 'k': |
| 823 | result *= 1024; |
| 824 | if (endp[1] == 'i' && (endp[2] == '\0' || |
| 825 | endp[2] == 'B' || endp[2] == 'b')) |
| 826 | endp += 2; |
| 827 | case '\0': |
| 828 | break; |
| 829 | default: |
Artem Bityutskiy | 458dbb3 | 2007-12-19 17:03:42 +0200 | [diff] [blame] | 830 | printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n", |
| 831 | str); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 832 | return -EINVAL; |
| 833 | } |
| 834 | |
| 835 | return result; |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter. |
| 840 | * @val: the parameter value to parse |
| 841 | * @kp: not used |
| 842 | * |
| 843 | * This function returns zero in case of success and a negative error code in |
| 844 | * case of error. |
| 845 | */ |
| 846 | static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp) |
| 847 | { |
| 848 | int i, len; |
| 849 | struct mtd_dev_param *p; |
| 850 | char buf[MTD_PARAM_LEN_MAX]; |
| 851 | char *pbuf = &buf[0]; |
| 852 | char *tokens[3] = {NULL, NULL, NULL}; |
| 853 | |
Artem Bityutskiy | 77c722d | 2007-12-16 16:46:57 +0200 | [diff] [blame] | 854 | if (!val) |
| 855 | return -EINVAL; |
| 856 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 857 | if (mtd_devs == UBI_MAX_DEVICES) { |
Artem Bityutskiy | 458dbb3 | 2007-12-19 17:03:42 +0200 | [diff] [blame] | 858 | printk(KERN_ERR "UBI error: too many parameters, max. is %d\n", |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 859 | UBI_MAX_DEVICES); |
| 860 | return -EINVAL; |
| 861 | } |
| 862 | |
| 863 | len = strnlen(val, MTD_PARAM_LEN_MAX); |
| 864 | if (len == MTD_PARAM_LEN_MAX) { |
Artem Bityutskiy | 458dbb3 | 2007-12-19 17:03:42 +0200 | [diff] [blame] | 865 | printk(KERN_ERR "UBI error: parameter \"%s\" is too long, " |
| 866 | "max. is %d\n", val, MTD_PARAM_LEN_MAX); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 867 | return -EINVAL; |
| 868 | } |
| 869 | |
| 870 | if (len == 0) { |
Artem Bityutskiy | 458dbb3 | 2007-12-19 17:03:42 +0200 | [diff] [blame] | 871 | printk(KERN_WARNING "UBI warning: empty 'mtd=' parameter - " |
| 872 | "ignored\n"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | strcpy(buf, val); |
| 877 | |
| 878 | /* Get rid of the final newline */ |
| 879 | if (buf[len - 1] == '\n') |
Artem Bityutskiy | 503990e | 2007-07-11 16:03:29 +0300 | [diff] [blame] | 880 | buf[len - 1] = '\0'; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 881 | |
| 882 | for (i = 0; i < 3; i++) |
| 883 | tokens[i] = strsep(&pbuf, ","); |
| 884 | |
| 885 | if (pbuf) { |
Artem Bityutskiy | 458dbb3 | 2007-12-19 17:03:42 +0200 | [diff] [blame] | 886 | printk(KERN_ERR "UBI error: too many arguments at \"%s\"\n", |
| 887 | val); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 888 | return -EINVAL; |
| 889 | } |
| 890 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 891 | p = &mtd_dev_param[mtd_devs]; |
| 892 | strcpy(&p->name[0], tokens[0]); |
| 893 | |
| 894 | if (tokens[1]) |
| 895 | p->vid_hdr_offs = bytes_str_to_int(tokens[1]); |
| 896 | if (tokens[2]) |
| 897 | p->data_offs = bytes_str_to_int(tokens[2]); |
| 898 | |
| 899 | if (p->vid_hdr_offs < 0) |
| 900 | return p->vid_hdr_offs; |
| 901 | if (p->data_offs < 0) |
| 902 | return p->data_offs; |
| 903 | |
| 904 | mtd_devs += 1; |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000); |
| 909 | MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: " |
| 910 | "mtd=<name|num>[,<vid_hdr_offs>,<data_offs>]. " |
| 911 | "Multiple \"mtd\" parameters may be specified.\n" |
| 912 | "MTD devices may be specified by their number or name. " |
| 913 | "Optional \"vid_hdr_offs\" and \"data_offs\" parameters " |
| 914 | "specify UBI VID header position and data starting " |
| 915 | "position to be used by UBI.\n" |
| 916 | "Example: mtd=content,1984,2048 mtd=4 - attach MTD device" |
| 917 | "with name content using VID header offset 1984 and data " |
| 918 | "start 2048, and MTD device number 4 using default " |
| 919 | "offsets"); |
| 920 | |
| 921 | MODULE_VERSION(__stringify(UBI_VERSION)); |
| 922 | MODULE_DESCRIPTION("UBI - Unsorted Block Images"); |
| 923 | MODULE_AUTHOR("Artem Bityutskiy"); |
| 924 | MODULE_LICENSE("GPL"); |