Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2006 |
| 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 5 | * |
| 6 | * Author: Artem Bityutskiy (Битюцкий Артём) |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * This file contains implementation of volume creation, deletion, updating and |
| 11 | * resizing. |
| 12 | */ |
| 13 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 14 | #ifndef __UBOOT__ |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 15 | #include <linux/err.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/export.h> |
| 18 | #else |
| 19 | #include <div64.h> |
| 20 | #include <ubi_uboot.h> |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 21 | #endif |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 22 | #include <linux/math64.h> |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 23 | |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 24 | #include "ubi.h" |
| 25 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 26 | static int self_check_volumes(struct ubi_device *ubi); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 27 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 28 | #ifndef __UBOOT__ |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 29 | static ssize_t vol_attribute_show(struct device *dev, |
| 30 | struct device_attribute *attr, char *buf); |
| 31 | |
| 32 | /* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */ |
| 33 | static struct device_attribute attr_vol_reserved_ebs = |
| 34 | __ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL); |
| 35 | static struct device_attribute attr_vol_type = |
| 36 | __ATTR(type, S_IRUGO, vol_attribute_show, NULL); |
| 37 | static struct device_attribute attr_vol_name = |
| 38 | __ATTR(name, S_IRUGO, vol_attribute_show, NULL); |
| 39 | static struct device_attribute attr_vol_corrupted = |
| 40 | __ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL); |
| 41 | static struct device_attribute attr_vol_alignment = |
| 42 | __ATTR(alignment, S_IRUGO, vol_attribute_show, NULL); |
| 43 | static struct device_attribute attr_vol_usable_eb_size = |
| 44 | __ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL); |
| 45 | static struct device_attribute attr_vol_data_bytes = |
| 46 | __ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL); |
| 47 | static struct device_attribute attr_vol_upd_marker = |
| 48 | __ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL); |
| 49 | |
| 50 | /* |
| 51 | * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'. |
| 52 | * |
| 53 | * Consider a situation: |
| 54 | * A. process 1 opens a sysfs file related to volume Y, say |
| 55 | * /<sysfs>/class/ubi/ubiX_Y/reserved_ebs; |
| 56 | * B. process 2 removes volume Y; |
| 57 | * C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file; |
| 58 | * |
| 59 | * In this situation, this function will return %-ENODEV because it will find |
| 60 | * out that the volume was removed from the @ubi->volumes array. |
| 61 | */ |
| 62 | static ssize_t vol_attribute_show(struct device *dev, |
| 63 | struct device_attribute *attr, char *buf) |
| 64 | { |
| 65 | int ret; |
| 66 | struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev); |
| 67 | struct ubi_device *ubi; |
| 68 | |
| 69 | ubi = ubi_get_device(vol->ubi->ubi_num); |
| 70 | if (!ubi) |
| 71 | return -ENODEV; |
| 72 | |
| 73 | spin_lock(&ubi->volumes_lock); |
| 74 | if (!ubi->volumes[vol->vol_id]) { |
| 75 | spin_unlock(&ubi->volumes_lock); |
| 76 | ubi_put_device(ubi); |
| 77 | return -ENODEV; |
| 78 | } |
| 79 | /* Take a reference to prevent volume removal */ |
| 80 | vol->ref_count += 1; |
| 81 | spin_unlock(&ubi->volumes_lock); |
| 82 | |
| 83 | if (attr == &attr_vol_reserved_ebs) |
| 84 | ret = sprintf(buf, "%d\n", vol->reserved_pebs); |
| 85 | else if (attr == &attr_vol_type) { |
| 86 | const char *tp; |
| 87 | |
| 88 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) |
| 89 | tp = "dynamic"; |
| 90 | else |
| 91 | tp = "static"; |
| 92 | ret = sprintf(buf, "%s\n", tp); |
| 93 | } else if (attr == &attr_vol_name) |
| 94 | ret = sprintf(buf, "%s\n", vol->name); |
| 95 | else if (attr == &attr_vol_corrupted) |
| 96 | ret = sprintf(buf, "%d\n", vol->corrupted); |
| 97 | else if (attr == &attr_vol_alignment) |
| 98 | ret = sprintf(buf, "%d\n", vol->alignment); |
| 99 | else if (attr == &attr_vol_usable_eb_size) |
| 100 | ret = sprintf(buf, "%d\n", vol->usable_leb_size); |
| 101 | else if (attr == &attr_vol_data_bytes) |
| 102 | ret = sprintf(buf, "%lld\n", vol->used_bytes); |
| 103 | else if (attr == &attr_vol_upd_marker) |
| 104 | ret = sprintf(buf, "%d\n", vol->upd_marker); |
| 105 | else |
| 106 | /* This must be a bug */ |
| 107 | ret = -EINVAL; |
| 108 | |
| 109 | /* We've done the operation, drop volume and UBI device references */ |
| 110 | spin_lock(&ubi->volumes_lock); |
| 111 | vol->ref_count -= 1; |
| 112 | ubi_assert(vol->ref_count >= 0); |
| 113 | spin_unlock(&ubi->volumes_lock); |
| 114 | ubi_put_device(ubi); |
| 115 | return ret; |
| 116 | } |
| 117 | #endif |
| 118 | |
| 119 | /* Release method for volume devices */ |
| 120 | static void vol_release(struct device *dev) |
| 121 | { |
| 122 | struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev); |
| 123 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 124 | kfree(vol->eba_tbl); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 125 | kfree(vol); |
| 126 | } |
| 127 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 128 | #ifndef __UBOOT__ |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 129 | /** |
| 130 | * volume_sysfs_init - initialize sysfs for new volume. |
| 131 | * @ubi: UBI device description object |
| 132 | * @vol: volume description object |
| 133 | * |
| 134 | * This function returns zero in case of success and a negative error code in |
| 135 | * case of failure. |
| 136 | * |
| 137 | * Note, this function does not free allocated resources in case of failure - |
| 138 | * the caller does it. This is because this would cause release() here and the |
| 139 | * caller would oops. |
| 140 | */ |
| 141 | static int volume_sysfs_init(struct ubi_device *ubi, struct ubi_volume *vol) |
| 142 | { |
| 143 | int err; |
| 144 | |
| 145 | err = device_create_file(&vol->dev, &attr_vol_reserved_ebs); |
| 146 | if (err) |
| 147 | return err; |
| 148 | err = device_create_file(&vol->dev, &attr_vol_type); |
| 149 | if (err) |
| 150 | return err; |
| 151 | err = device_create_file(&vol->dev, &attr_vol_name); |
| 152 | if (err) |
| 153 | return err; |
| 154 | err = device_create_file(&vol->dev, &attr_vol_corrupted); |
| 155 | if (err) |
| 156 | return err; |
| 157 | err = device_create_file(&vol->dev, &attr_vol_alignment); |
| 158 | if (err) |
| 159 | return err; |
| 160 | err = device_create_file(&vol->dev, &attr_vol_usable_eb_size); |
| 161 | if (err) |
| 162 | return err; |
| 163 | err = device_create_file(&vol->dev, &attr_vol_data_bytes); |
| 164 | if (err) |
| 165 | return err; |
| 166 | err = device_create_file(&vol->dev, &attr_vol_upd_marker); |
| 167 | return err; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * volume_sysfs_close - close sysfs for a volume. |
| 172 | * @vol: volume description object |
| 173 | */ |
| 174 | static void volume_sysfs_close(struct ubi_volume *vol) |
| 175 | { |
| 176 | device_remove_file(&vol->dev, &attr_vol_upd_marker); |
| 177 | device_remove_file(&vol->dev, &attr_vol_data_bytes); |
| 178 | device_remove_file(&vol->dev, &attr_vol_usable_eb_size); |
| 179 | device_remove_file(&vol->dev, &attr_vol_alignment); |
| 180 | device_remove_file(&vol->dev, &attr_vol_corrupted); |
| 181 | device_remove_file(&vol->dev, &attr_vol_name); |
| 182 | device_remove_file(&vol->dev, &attr_vol_type); |
| 183 | device_remove_file(&vol->dev, &attr_vol_reserved_ebs); |
| 184 | device_unregister(&vol->dev); |
| 185 | } |
| 186 | #endif |
| 187 | |
| 188 | /** |
| 189 | * ubi_create_volume - create volume. |
| 190 | * @ubi: UBI device description object |
| 191 | * @req: volume creation request |
| 192 | * |
| 193 | * This function creates volume described by @req. If @req->vol_id id |
| 194 | * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume |
| 195 | * and saves it in @req->vol_id. Returns zero in case of success and a negative |
| 196 | * error code in case of failure. Note, the caller has to have the |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 197 | * @ubi->device_mutex locked. |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 198 | */ |
| 199 | int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req) |
| 200 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 201 | int i, err, vol_id = req->vol_id, do_free = 1; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 202 | struct ubi_volume *vol; |
| 203 | struct ubi_vtbl_record vtbl_rec; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 204 | dev_t dev; |
| 205 | |
| 206 | if (ubi->ro_mode) |
| 207 | return -EROFS; |
| 208 | |
| 209 | vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); |
| 210 | if (!vol) |
| 211 | return -ENOMEM; |
| 212 | |
| 213 | spin_lock(&ubi->volumes_lock); |
| 214 | if (vol_id == UBI_VOL_NUM_AUTO) { |
| 215 | /* Find unused volume ID */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 216 | dbg_gen("search for vacant volume ID"); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 217 | for (i = 0; i < ubi->vtbl_slots; i++) |
| 218 | if (!ubi->volumes[i]) { |
| 219 | vol_id = i; |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | if (vol_id == UBI_VOL_NUM_AUTO) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 224 | ubi_err("out of volume IDs"); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 225 | err = -ENFILE; |
| 226 | goto out_unlock; |
| 227 | } |
| 228 | req->vol_id = vol_id; |
| 229 | } |
| 230 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 231 | dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s", |
| 232 | ubi->ubi_num, vol_id, (unsigned long long)req->bytes, |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 233 | (int)req->vol_type, req->name); |
| 234 | |
| 235 | /* Ensure that this volume does not exist */ |
| 236 | err = -EEXIST; |
| 237 | if (ubi->volumes[vol_id]) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 238 | ubi_err("volume %d already exists", vol_id); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 239 | goto out_unlock; |
| 240 | } |
| 241 | |
| 242 | /* Ensure that the name is unique */ |
| 243 | for (i = 0; i < ubi->vtbl_slots; i++) |
| 244 | if (ubi->volumes[i] && |
| 245 | ubi->volumes[i]->name_len == req->name_len && |
| 246 | !strcmp(ubi->volumes[i]->name, req->name)) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 247 | ubi_err("volume \"%s\" exists (ID %d)", req->name, i); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 248 | goto out_unlock; |
| 249 | } |
| 250 | |
Wolfgang Denk | 55334c7 | 2008-12-16 01:02:17 +0100 | [diff] [blame] | 251 | /* Calculate how many eraseblocks are requested */ |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 252 | vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 253 | vol->reserved_pebs += div_u64(req->bytes + vol->usable_leb_size - 1, |
| 254 | vol->usable_leb_size); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 255 | |
| 256 | /* Reserve physical eraseblocks */ |
| 257 | if (vol->reserved_pebs > ubi->avail_pebs) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 258 | ubi_err("not enough PEBs, only %d available", ubi->avail_pebs); |
| 259 | if (ubi->corr_peb_count) |
| 260 | ubi_err("%d PEBs are corrupted and not used", |
| 261 | ubi->corr_peb_count); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 262 | err = -ENOSPC; |
| 263 | goto out_unlock; |
| 264 | } |
| 265 | ubi->avail_pebs -= vol->reserved_pebs; |
| 266 | ubi->rsvd_pebs += vol->reserved_pebs; |
| 267 | spin_unlock(&ubi->volumes_lock); |
| 268 | |
| 269 | vol->vol_id = vol_id; |
| 270 | vol->alignment = req->alignment; |
| 271 | vol->data_pad = ubi->leb_size % vol->alignment; |
| 272 | vol->vol_type = req->vol_type; |
| 273 | vol->name_len = req->name_len; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 274 | memcpy(vol->name, req->name, vol->name_len); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 275 | vol->ubi = ubi; |
| 276 | |
| 277 | /* |
| 278 | * Finish all pending erases because there may be some LEBs belonging |
| 279 | * to the same volume ID. |
| 280 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 281 | err = ubi_wl_flush(ubi, vol_id, UBI_ALL); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 282 | if (err) |
| 283 | goto out_acc; |
| 284 | |
| 285 | vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL); |
| 286 | if (!vol->eba_tbl) { |
| 287 | err = -ENOMEM; |
| 288 | goto out_acc; |
| 289 | } |
| 290 | |
| 291 | for (i = 0; i < vol->reserved_pebs; i++) |
| 292 | vol->eba_tbl[i] = UBI_LEB_UNMAPPED; |
| 293 | |
| 294 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { |
| 295 | vol->used_ebs = vol->reserved_pebs; |
| 296 | vol->last_eb_bytes = vol->usable_leb_size; |
| 297 | vol->used_bytes = |
| 298 | (long long)vol->used_ebs * vol->usable_leb_size; |
| 299 | } else { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 300 | vol->used_ebs = div_u64_rem(vol->used_bytes, |
| 301 | vol->usable_leb_size, |
| 302 | &vol->last_eb_bytes); |
| 303 | if (vol->last_eb_bytes != 0) |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 304 | vol->used_ebs += 1; |
| 305 | else |
| 306 | vol->last_eb_bytes = vol->usable_leb_size; |
| 307 | } |
| 308 | |
| 309 | /* Register character device for the volume */ |
| 310 | cdev_init(&vol->cdev, &ubi_vol_cdev_operations); |
| 311 | vol->cdev.owner = THIS_MODULE; |
| 312 | dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1); |
| 313 | err = cdev_add(&vol->cdev, dev, 1); |
| 314 | if (err) { |
| 315 | ubi_err("cannot add character device"); |
| 316 | goto out_mapping; |
| 317 | } |
| 318 | |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 319 | vol->dev.release = vol_release; |
| 320 | vol->dev.parent = &ubi->dev; |
| 321 | vol->dev.devt = dev; |
| 322 | vol->dev.class = ubi_class; |
| 323 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 324 | dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 325 | err = device_register(&vol->dev); |
| 326 | if (err) { |
| 327 | ubi_err("cannot register device"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 328 | goto out_cdev; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | err = volume_sysfs_init(ubi, vol); |
| 332 | if (err) |
| 333 | goto out_sysfs; |
| 334 | |
| 335 | /* Fill volume table record */ |
| 336 | memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record)); |
| 337 | vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs); |
| 338 | vtbl_rec.alignment = cpu_to_be32(vol->alignment); |
| 339 | vtbl_rec.data_pad = cpu_to_be32(vol->data_pad); |
| 340 | vtbl_rec.name_len = cpu_to_be16(vol->name_len); |
| 341 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) |
| 342 | vtbl_rec.vol_type = UBI_VID_DYNAMIC; |
| 343 | else |
| 344 | vtbl_rec.vol_type = UBI_VID_STATIC; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 345 | memcpy(vtbl_rec.name, vol->name, vol->name_len); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 346 | |
| 347 | err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); |
| 348 | if (err) |
| 349 | goto out_sysfs; |
| 350 | |
| 351 | spin_lock(&ubi->volumes_lock); |
| 352 | ubi->volumes[vol_id] = vol; |
| 353 | ubi->vol_count += 1; |
| 354 | spin_unlock(&ubi->volumes_lock); |
| 355 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 356 | ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED); |
| 357 | self_check_volumes(ubi); |
| 358 | return err; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 359 | |
| 360 | out_sysfs: |
| 361 | /* |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 362 | * We have registered our device, we should not free the volume |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 363 | * description object in this function in case of an error - it is |
| 364 | * freed by the release function. |
| 365 | * |
| 366 | * Get device reference to prevent the release function from being |
| 367 | * called just after sysfs has been closed. |
| 368 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 369 | do_free = 0; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 370 | get_device(&vol->dev); |
| 371 | volume_sysfs_close(vol); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 372 | out_cdev: |
| 373 | cdev_del(&vol->cdev); |
| 374 | out_mapping: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 375 | if (do_free) |
| 376 | kfree(vol->eba_tbl); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 377 | out_acc: |
| 378 | spin_lock(&ubi->volumes_lock); |
| 379 | ubi->rsvd_pebs -= vol->reserved_pebs; |
| 380 | ubi->avail_pebs += vol->reserved_pebs; |
| 381 | out_unlock: |
| 382 | spin_unlock(&ubi->volumes_lock); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 383 | if (do_free) |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 384 | kfree(vol); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 385 | else |
| 386 | put_device(&vol->dev); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 387 | ubi_err("cannot create volume %d, error %d", vol_id, err); |
| 388 | return err; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * ubi_remove_volume - remove volume. |
| 393 | * @desc: volume descriptor |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 394 | * @no_vtbl: do not change volume table if not zero |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 395 | * |
| 396 | * This function removes volume described by @desc. The volume has to be opened |
| 397 | * in "exclusive" mode. Returns zero in case of success and a negative error |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 398 | * code in case of failure. The caller has to have the @ubi->device_mutex |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 399 | * locked. |
| 400 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 401 | int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl) |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 402 | { |
| 403 | struct ubi_volume *vol = desc->vol; |
| 404 | struct ubi_device *ubi = vol->ubi; |
| 405 | int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs; |
| 406 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 407 | dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 408 | ubi_assert(desc->mode == UBI_EXCLUSIVE); |
| 409 | ubi_assert(vol == ubi->volumes[vol_id]); |
| 410 | |
| 411 | if (ubi->ro_mode) |
| 412 | return -EROFS; |
| 413 | |
| 414 | spin_lock(&ubi->volumes_lock); |
| 415 | if (vol->ref_count > 1) { |
| 416 | /* |
| 417 | * The volume is busy, probably someone is reading one of its |
| 418 | * sysfs files. |
| 419 | */ |
| 420 | err = -EBUSY; |
| 421 | goto out_unlock; |
| 422 | } |
| 423 | ubi->volumes[vol_id] = NULL; |
| 424 | spin_unlock(&ubi->volumes_lock); |
| 425 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 426 | if (!no_vtbl) { |
| 427 | err = ubi_change_vtbl_record(ubi, vol_id, NULL); |
| 428 | if (err) |
| 429 | goto out_err; |
| 430 | } |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 431 | |
| 432 | for (i = 0; i < vol->reserved_pebs; i++) { |
| 433 | err = ubi_eba_unmap_leb(ubi, vol, i); |
| 434 | if (err) |
| 435 | goto out_err; |
| 436 | } |
| 437 | |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 438 | cdev_del(&vol->cdev); |
| 439 | volume_sysfs_close(vol); |
| 440 | |
| 441 | spin_lock(&ubi->volumes_lock); |
| 442 | ubi->rsvd_pebs -= reserved_pebs; |
| 443 | ubi->avail_pebs += reserved_pebs; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 444 | ubi_update_reserved(ubi); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 445 | ubi->vol_count -= 1; |
| 446 | spin_unlock(&ubi->volumes_lock); |
| 447 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 448 | ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED); |
| 449 | if (!no_vtbl) |
| 450 | self_check_volumes(ubi); |
| 451 | |
| 452 | return err; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 453 | |
| 454 | out_err: |
| 455 | ubi_err("cannot remove volume %d, error %d", vol_id, err); |
| 456 | spin_lock(&ubi->volumes_lock); |
| 457 | ubi->volumes[vol_id] = vol; |
| 458 | out_unlock: |
| 459 | spin_unlock(&ubi->volumes_lock); |
| 460 | return err; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * ubi_resize_volume - re-size volume. |
| 465 | * @desc: volume descriptor |
| 466 | * @reserved_pebs: new size in physical eraseblocks |
| 467 | * |
| 468 | * This function re-sizes the volume and returns zero in case of success, and a |
| 469 | * negative error code in case of failure. The caller has to have the |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 470 | * @ubi->device_mutex locked. |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 471 | */ |
| 472 | int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs) |
| 473 | { |
| 474 | int i, err, pebs, *new_mapping; |
| 475 | struct ubi_volume *vol = desc->vol; |
| 476 | struct ubi_device *ubi = vol->ubi; |
| 477 | struct ubi_vtbl_record vtbl_rec; |
| 478 | int vol_id = vol->vol_id; |
| 479 | |
| 480 | if (ubi->ro_mode) |
| 481 | return -EROFS; |
| 482 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 483 | dbg_gen("re-size device %d, volume %d to from %d to %d PEBs", |
| 484 | ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 485 | |
| 486 | if (vol->vol_type == UBI_STATIC_VOLUME && |
| 487 | reserved_pebs < vol->used_ebs) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 488 | ubi_err("too small size %d, %d LEBs contain data", |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 489 | reserved_pebs, vol->used_ebs); |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
| 493 | /* If the size is the same, we have nothing to do */ |
| 494 | if (reserved_pebs == vol->reserved_pebs) |
| 495 | return 0; |
| 496 | |
| 497 | new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL); |
| 498 | if (!new_mapping) |
| 499 | return -ENOMEM; |
| 500 | |
| 501 | for (i = 0; i < reserved_pebs; i++) |
| 502 | new_mapping[i] = UBI_LEB_UNMAPPED; |
| 503 | |
| 504 | spin_lock(&ubi->volumes_lock); |
| 505 | if (vol->ref_count > 1) { |
| 506 | spin_unlock(&ubi->volumes_lock); |
| 507 | err = -EBUSY; |
| 508 | goto out_free; |
| 509 | } |
| 510 | spin_unlock(&ubi->volumes_lock); |
| 511 | |
| 512 | /* Reserve physical eraseblocks */ |
| 513 | pebs = reserved_pebs - vol->reserved_pebs; |
| 514 | if (pebs > 0) { |
| 515 | spin_lock(&ubi->volumes_lock); |
| 516 | if (pebs > ubi->avail_pebs) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 517 | ubi_err("not enough PEBs: requested %d, available %d", |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 518 | pebs, ubi->avail_pebs); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 519 | if (ubi->corr_peb_count) |
| 520 | ubi_err("%d PEBs are corrupted and not used", |
| 521 | ubi->corr_peb_count); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 522 | spin_unlock(&ubi->volumes_lock); |
| 523 | err = -ENOSPC; |
| 524 | goto out_free; |
| 525 | } |
| 526 | ubi->avail_pebs -= pebs; |
| 527 | ubi->rsvd_pebs += pebs; |
| 528 | for (i = 0; i < vol->reserved_pebs; i++) |
| 529 | new_mapping[i] = vol->eba_tbl[i]; |
| 530 | kfree(vol->eba_tbl); |
| 531 | vol->eba_tbl = new_mapping; |
| 532 | spin_unlock(&ubi->volumes_lock); |
| 533 | } |
| 534 | |
| 535 | /* Change volume table record */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 536 | vtbl_rec = ubi->vtbl[vol_id]; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 537 | vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs); |
| 538 | err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); |
| 539 | if (err) |
| 540 | goto out_acc; |
| 541 | |
| 542 | if (pebs < 0) { |
| 543 | for (i = 0; i < -pebs; i++) { |
| 544 | err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i); |
| 545 | if (err) |
| 546 | goto out_acc; |
| 547 | } |
| 548 | spin_lock(&ubi->volumes_lock); |
| 549 | ubi->rsvd_pebs += pebs; |
| 550 | ubi->avail_pebs -= pebs; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 551 | ubi_update_reserved(ubi); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 552 | for (i = 0; i < reserved_pebs; i++) |
| 553 | new_mapping[i] = vol->eba_tbl[i]; |
| 554 | kfree(vol->eba_tbl); |
| 555 | vol->eba_tbl = new_mapping; |
| 556 | spin_unlock(&ubi->volumes_lock); |
| 557 | } |
| 558 | |
| 559 | vol->reserved_pebs = reserved_pebs; |
| 560 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { |
| 561 | vol->used_ebs = reserved_pebs; |
| 562 | vol->last_eb_bytes = vol->usable_leb_size; |
| 563 | vol->used_bytes = |
| 564 | (long long)vol->used_ebs * vol->usable_leb_size; |
| 565 | } |
| 566 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 567 | ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED); |
| 568 | self_check_volumes(ubi); |
| 569 | return err; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 570 | |
| 571 | out_acc: |
| 572 | if (pebs > 0) { |
| 573 | spin_lock(&ubi->volumes_lock); |
| 574 | ubi->rsvd_pebs -= pebs; |
| 575 | ubi->avail_pebs += pebs; |
| 576 | spin_unlock(&ubi->volumes_lock); |
| 577 | } |
| 578 | out_free: |
| 579 | kfree(new_mapping); |
| 580 | return err; |
| 581 | } |
| 582 | |
| 583 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 584 | * ubi_rename_volumes - re-name UBI volumes. |
| 585 | * @ubi: UBI device description object |
| 586 | * @rename_list: list of &struct ubi_rename_entry objects |
| 587 | * |
| 588 | * This function re-names or removes volumes specified in the re-name list. |
| 589 | * Returns zero in case of success and a negative error code in case of |
| 590 | * failure. |
| 591 | */ |
| 592 | int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list) |
| 593 | { |
| 594 | int err; |
| 595 | struct ubi_rename_entry *re; |
| 596 | |
| 597 | err = ubi_vtbl_rename_volumes(ubi, rename_list); |
| 598 | if (err) |
| 599 | return err; |
| 600 | |
| 601 | list_for_each_entry(re, rename_list, list) { |
| 602 | if (re->remove) { |
| 603 | err = ubi_remove_volume(re->desc, 1); |
| 604 | if (err) |
| 605 | break; |
| 606 | } else { |
| 607 | struct ubi_volume *vol = re->desc->vol; |
| 608 | |
| 609 | spin_lock(&ubi->volumes_lock); |
| 610 | vol->name_len = re->new_name_len; |
| 611 | memcpy(vol->name, re->new_name, re->new_name_len + 1); |
| 612 | spin_unlock(&ubi->volumes_lock); |
| 613 | ubi_volume_notify(ubi, vol, UBI_VOLUME_RENAMED); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if (!err) |
| 618 | self_check_volumes(ubi); |
| 619 | return err; |
| 620 | } |
| 621 | |
| 622 | /** |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 623 | * ubi_add_volume - add volume. |
| 624 | * @ubi: UBI device description object |
| 625 | * @vol: volume description object |
| 626 | * |
| 627 | * This function adds an existing volume and initializes all its data |
| 628 | * structures. Returns zero in case of success and a negative error code in |
| 629 | * case of failure. |
| 630 | */ |
| 631 | int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol) |
| 632 | { |
| 633 | int err, vol_id = vol->vol_id; |
| 634 | dev_t dev; |
| 635 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 636 | dbg_gen("add volume %d", vol_id); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 637 | |
| 638 | /* Register character device for the volume */ |
| 639 | cdev_init(&vol->cdev, &ubi_vol_cdev_operations); |
| 640 | vol->cdev.owner = THIS_MODULE; |
| 641 | dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1); |
| 642 | err = cdev_add(&vol->cdev, dev, 1); |
| 643 | if (err) { |
| 644 | ubi_err("cannot add character device for volume %d, error %d", |
| 645 | vol_id, err); |
| 646 | return err; |
| 647 | } |
| 648 | |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 649 | vol->dev.release = vol_release; |
| 650 | vol->dev.parent = &ubi->dev; |
| 651 | vol->dev.devt = dev; |
| 652 | vol->dev.class = ubi_class; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 653 | dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 654 | err = device_register(&vol->dev); |
| 655 | if (err) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 656 | goto out_cdev; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 657 | |
| 658 | err = volume_sysfs_init(ubi, vol); |
| 659 | if (err) { |
| 660 | cdev_del(&vol->cdev); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 661 | volume_sysfs_close(vol); |
| 662 | return err; |
| 663 | } |
| 664 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 665 | self_check_volumes(ubi); |
| 666 | return err; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 667 | |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 668 | out_cdev: |
| 669 | cdev_del(&vol->cdev); |
| 670 | return err; |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * ubi_free_volume - free volume. |
| 675 | * @ubi: UBI device description object |
| 676 | * @vol: volume description object |
| 677 | * |
| 678 | * This function frees all resources for volume @vol but does not remove it. |
| 679 | * Used only when the UBI device is detached. |
| 680 | */ |
| 681 | void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol) |
| 682 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 683 | dbg_gen("free volume %d", vol->vol_id); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 684 | |
| 685 | ubi->volumes[vol->vol_id] = NULL; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 686 | cdev_del(&vol->cdev); |
| 687 | volume_sysfs_close(vol); |
| 688 | } |
| 689 | |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 690 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 691 | * self_check_volume - check volume information. |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 692 | * @ubi: UBI device description object |
| 693 | * @vol_id: volume ID |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 694 | * |
| 695 | * Returns zero if volume is all right and a a negative error code if not. |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 696 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 697 | static int self_check_volume(struct ubi_device *ubi, int vol_id) |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 698 | { |
| 699 | int idx = vol_id2idx(ubi, vol_id); |
| 700 | int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker; |
| 701 | const struct ubi_volume *vol; |
| 702 | long long n; |
| 703 | const char *name; |
| 704 | |
| 705 | spin_lock(&ubi->volumes_lock); |
| 706 | reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs); |
| 707 | vol = ubi->volumes[idx]; |
| 708 | |
| 709 | if (!vol) { |
| 710 | if (reserved_pebs) { |
| 711 | ubi_err("no volume info, but volume exists"); |
| 712 | goto fail; |
| 713 | } |
| 714 | spin_unlock(&ubi->volumes_lock); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 715 | return 0; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 || |
| 719 | vol->name_len < 0) { |
| 720 | ubi_err("negative values"); |
| 721 | goto fail; |
| 722 | } |
| 723 | if (vol->alignment > ubi->leb_size || vol->alignment == 0) { |
| 724 | ubi_err("bad alignment"); |
| 725 | goto fail; |
| 726 | } |
| 727 | |
| 728 | n = vol->alignment & (ubi->min_io_size - 1); |
| 729 | if (vol->alignment != 1 && n) { |
| 730 | ubi_err("alignment is not multiple of min I/O unit"); |
| 731 | goto fail; |
| 732 | } |
| 733 | |
| 734 | n = ubi->leb_size % vol->alignment; |
| 735 | if (vol->data_pad != n) { |
| 736 | ubi_err("bad data_pad, has to be %lld", n); |
| 737 | goto fail; |
| 738 | } |
| 739 | |
| 740 | if (vol->vol_type != UBI_DYNAMIC_VOLUME && |
| 741 | vol->vol_type != UBI_STATIC_VOLUME) { |
| 742 | ubi_err("bad vol_type"); |
| 743 | goto fail; |
| 744 | } |
| 745 | |
| 746 | if (vol->upd_marker && vol->corrupted) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 747 | ubi_err("update marker and corrupted simultaneously"); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 748 | goto fail; |
| 749 | } |
| 750 | |
| 751 | if (vol->reserved_pebs > ubi->good_peb_count) { |
| 752 | ubi_err("too large reserved_pebs"); |
| 753 | goto fail; |
| 754 | } |
| 755 | |
| 756 | n = ubi->leb_size - vol->data_pad; |
| 757 | if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) { |
| 758 | ubi_err("bad usable_leb_size, has to be %lld", n); |
| 759 | goto fail; |
| 760 | } |
| 761 | |
| 762 | if (vol->name_len > UBI_VOL_NAME_MAX) { |
| 763 | ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX); |
| 764 | goto fail; |
| 765 | } |
| 766 | |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 767 | n = strnlen(vol->name, vol->name_len + 1); |
| 768 | if (n != vol->name_len) { |
| 769 | ubi_err("bad name_len %lld", n); |
| 770 | goto fail; |
| 771 | } |
| 772 | |
| 773 | n = (long long)vol->used_ebs * vol->usable_leb_size; |
| 774 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { |
| 775 | if (vol->corrupted) { |
| 776 | ubi_err("corrupted dynamic volume"); |
| 777 | goto fail; |
| 778 | } |
| 779 | if (vol->used_ebs != vol->reserved_pebs) { |
| 780 | ubi_err("bad used_ebs"); |
| 781 | goto fail; |
| 782 | } |
| 783 | if (vol->last_eb_bytes != vol->usable_leb_size) { |
| 784 | ubi_err("bad last_eb_bytes"); |
| 785 | goto fail; |
| 786 | } |
| 787 | if (vol->used_bytes != n) { |
| 788 | ubi_err("bad used_bytes"); |
| 789 | goto fail; |
| 790 | } |
| 791 | } else { |
| 792 | if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) { |
| 793 | ubi_err("bad used_ebs"); |
| 794 | goto fail; |
| 795 | } |
| 796 | if (vol->last_eb_bytes < 0 || |
| 797 | vol->last_eb_bytes > vol->usable_leb_size) { |
| 798 | ubi_err("bad last_eb_bytes"); |
| 799 | goto fail; |
| 800 | } |
| 801 | if (vol->used_bytes < 0 || vol->used_bytes > n || |
| 802 | vol->used_bytes < n - vol->usable_leb_size) { |
| 803 | ubi_err("bad used_bytes"); |
| 804 | goto fail; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment); |
| 809 | data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad); |
| 810 | name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len); |
| 811 | upd_marker = ubi->vtbl[vol_id].upd_marker; |
| 812 | name = &ubi->vtbl[vol_id].name[0]; |
| 813 | if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC) |
| 814 | vol_type = UBI_DYNAMIC_VOLUME; |
| 815 | else |
| 816 | vol_type = UBI_STATIC_VOLUME; |
| 817 | |
| 818 | if (alignment != vol->alignment || data_pad != vol->data_pad || |
| 819 | upd_marker != vol->upd_marker || vol_type != vol->vol_type || |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 820 | name_len != vol->name_len || strncmp(name, vol->name, name_len)) { |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 821 | ubi_err("volume info is different"); |
| 822 | goto fail; |
| 823 | } |
| 824 | |
| 825 | spin_unlock(&ubi->volumes_lock); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 826 | return 0; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 827 | |
| 828 | fail: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 829 | ubi_err("self-check failed for volume %d", vol_id); |
| 830 | if (vol) |
| 831 | ubi_dump_vol_info(vol); |
| 832 | ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id); |
| 833 | dump_stack(); |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 834 | spin_unlock(&ubi->volumes_lock); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 835 | return -EINVAL; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 839 | * self_check_volumes - check information about all volumes. |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 840 | * @ubi: UBI device description object |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 841 | * |
| 842 | * Returns zero if volumes are all right and a a negative error code if not. |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 843 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 844 | static int self_check_volumes(struct ubi_device *ubi) |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 845 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 846 | int i, err = 0; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 847 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 848 | if (!ubi_dbg_chk_gen(ubi)) |
| 849 | return 0; |
| 850 | |
| 851 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 852 | err = self_check_volume(ubi, i); |
| 853 | if (err) |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | return err; |
Kyungmin Park | 7f88f00 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 858 | } |