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