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