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