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