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