0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/err.h>
0014 #include <linux/math64.h>
0015 #include <linux/slab.h>
0016 #include <linux/export.h>
0017 #include "ubi.h"
0018
0019 static int self_check_volumes(struct ubi_device *ubi);
0020
0021 static ssize_t vol_attribute_show(struct device *dev,
0022 struct device_attribute *attr, char *buf);
0023
0024
0025 static struct device_attribute attr_vol_reserved_ebs =
0026 __ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
0027 static struct device_attribute attr_vol_type =
0028 __ATTR(type, S_IRUGO, vol_attribute_show, NULL);
0029 static struct device_attribute attr_vol_name =
0030 __ATTR(name, S_IRUGO, vol_attribute_show, NULL);
0031 static struct device_attribute attr_vol_corrupted =
0032 __ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
0033 static struct device_attribute attr_vol_alignment =
0034 __ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
0035 static struct device_attribute attr_vol_usable_eb_size =
0036 __ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
0037 static struct device_attribute attr_vol_data_bytes =
0038 __ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
0039 static struct device_attribute attr_vol_upd_marker =
0040 __ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL);
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 static ssize_t vol_attribute_show(struct device *dev,
0055 struct device_attribute *attr, char *buf)
0056 {
0057 int ret;
0058 struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
0059 struct ubi_device *ubi = vol->ubi;
0060
0061 spin_lock(&ubi->volumes_lock);
0062 if (!ubi->volumes[vol->vol_id]) {
0063 spin_unlock(&ubi->volumes_lock);
0064 return -ENODEV;
0065 }
0066
0067 vol->ref_count += 1;
0068 spin_unlock(&ubi->volumes_lock);
0069
0070 if (attr == &attr_vol_reserved_ebs)
0071 ret = sprintf(buf, "%d\n", vol->reserved_pebs);
0072 else if (attr == &attr_vol_type) {
0073 const char *tp;
0074
0075 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
0076 tp = "dynamic";
0077 else
0078 tp = "static";
0079 ret = sprintf(buf, "%s\n", tp);
0080 } else if (attr == &attr_vol_name)
0081 ret = sprintf(buf, "%s\n", vol->name);
0082 else if (attr == &attr_vol_corrupted)
0083 ret = sprintf(buf, "%d\n", vol->corrupted);
0084 else if (attr == &attr_vol_alignment)
0085 ret = sprintf(buf, "%d\n", vol->alignment);
0086 else if (attr == &attr_vol_usable_eb_size)
0087 ret = sprintf(buf, "%d\n", vol->usable_leb_size);
0088 else if (attr == &attr_vol_data_bytes)
0089 ret = sprintf(buf, "%lld\n", vol->used_bytes);
0090 else if (attr == &attr_vol_upd_marker)
0091 ret = sprintf(buf, "%d\n", vol->upd_marker);
0092 else
0093
0094 ret = -EINVAL;
0095
0096
0097 spin_lock(&ubi->volumes_lock);
0098 vol->ref_count -= 1;
0099 ubi_assert(vol->ref_count >= 0);
0100 spin_unlock(&ubi->volumes_lock);
0101 return ret;
0102 }
0103
0104 static struct attribute *volume_dev_attrs[] = {
0105 &attr_vol_reserved_ebs.attr,
0106 &attr_vol_type.attr,
0107 &attr_vol_name.attr,
0108 &attr_vol_corrupted.attr,
0109 &attr_vol_alignment.attr,
0110 &attr_vol_usable_eb_size.attr,
0111 &attr_vol_data_bytes.attr,
0112 &attr_vol_upd_marker.attr,
0113 NULL
0114 };
0115 ATTRIBUTE_GROUPS(volume_dev);
0116
0117
0118 static void vol_release(struct device *dev)
0119 {
0120 struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
0121
0122 ubi_eba_replace_table(vol, NULL);
0123 ubi_fastmap_destroy_checkmap(vol);
0124 kfree(vol);
0125 }
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
0139 {
0140 int i, err, vol_id = req->vol_id;
0141 struct ubi_volume *vol;
0142 struct ubi_vtbl_record vtbl_rec;
0143 struct ubi_eba_table *eba_tbl = NULL;
0144
0145 if (ubi->ro_mode)
0146 return -EROFS;
0147
0148 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
0149 if (!vol)
0150 return -ENOMEM;
0151
0152 device_initialize(&vol->dev);
0153 vol->dev.release = vol_release;
0154 vol->dev.parent = &ubi->dev;
0155 vol->dev.class = &ubi_class;
0156 vol->dev.groups = volume_dev_groups;
0157
0158 if (req->flags & UBI_VOL_SKIP_CRC_CHECK_FLG)
0159 vol->skip_check = 1;
0160
0161 spin_lock(&ubi->volumes_lock);
0162 if (vol_id == UBI_VOL_NUM_AUTO) {
0163
0164 dbg_gen("search for vacant volume ID");
0165 for (i = 0; i < ubi->vtbl_slots; i++)
0166 if (!ubi->volumes[i]) {
0167 vol_id = i;
0168 break;
0169 }
0170
0171 if (vol_id == UBI_VOL_NUM_AUTO) {
0172 ubi_err(ubi, "out of volume IDs");
0173 err = -ENFILE;
0174 goto out_unlock;
0175 }
0176 req->vol_id = vol_id;
0177 }
0178
0179 dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s",
0180 ubi->ubi_num, vol_id, (unsigned long long)req->bytes,
0181 (int)req->vol_type, req->name);
0182
0183
0184 err = -EEXIST;
0185 if (ubi->volumes[vol_id]) {
0186 ubi_err(ubi, "volume %d already exists", vol_id);
0187 goto out_unlock;
0188 }
0189
0190
0191 for (i = 0; i < ubi->vtbl_slots; i++)
0192 if (ubi->volumes[i] &&
0193 ubi->volumes[i]->name_len == req->name_len &&
0194 !strcmp(ubi->volumes[i]->name, req->name)) {
0195 ubi_err(ubi, "volume \"%s\" exists (ID %d)",
0196 req->name, i);
0197 goto out_unlock;
0198 }
0199
0200
0201 vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
0202 vol->reserved_pebs = div_u64(req->bytes + vol->usable_leb_size - 1,
0203 vol->usable_leb_size);
0204
0205
0206 if (vol->reserved_pebs > ubi->avail_pebs) {
0207 ubi_err(ubi, "not enough PEBs, only %d available",
0208 ubi->avail_pebs);
0209 if (ubi->corr_peb_count)
0210 ubi_err(ubi, "%d PEBs are corrupted and not used",
0211 ubi->corr_peb_count);
0212 err = -ENOSPC;
0213 goto out_unlock;
0214 }
0215 ubi->avail_pebs -= vol->reserved_pebs;
0216 ubi->rsvd_pebs += vol->reserved_pebs;
0217 spin_unlock(&ubi->volumes_lock);
0218
0219 vol->vol_id = vol_id;
0220 vol->alignment = req->alignment;
0221 vol->data_pad = ubi->leb_size % vol->alignment;
0222 vol->vol_type = req->vol_type;
0223 vol->name_len = req->name_len;
0224 memcpy(vol->name, req->name, vol->name_len);
0225 vol->ubi = ubi;
0226
0227
0228
0229
0230
0231 err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
0232 if (err)
0233 goto out_acc;
0234
0235 eba_tbl = ubi_eba_create_table(vol, vol->reserved_pebs);
0236 if (IS_ERR(eba_tbl)) {
0237 err = PTR_ERR(eba_tbl);
0238 goto out_acc;
0239 }
0240
0241 ubi_eba_replace_table(vol, eba_tbl);
0242
0243 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
0244 vol->used_ebs = vol->reserved_pebs;
0245 vol->last_eb_bytes = vol->usable_leb_size;
0246 vol->used_bytes =
0247 (long long)vol->used_ebs * vol->usable_leb_size;
0248 } else {
0249 vol->used_ebs = div_u64_rem(vol->used_bytes,
0250 vol->usable_leb_size,
0251 &vol->last_eb_bytes);
0252 if (vol->last_eb_bytes != 0)
0253 vol->used_ebs += 1;
0254 else
0255 vol->last_eb_bytes = vol->usable_leb_size;
0256 }
0257
0258
0259 spin_lock(&ubi->volumes_lock);
0260 ubi->volumes[vol_id] = vol;
0261 ubi->vol_count += 1;
0262 spin_unlock(&ubi->volumes_lock);
0263
0264
0265 cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
0266 vol->cdev.owner = THIS_MODULE;
0267
0268 vol->dev.devt = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
0269 dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
0270 err = cdev_device_add(&vol->cdev, &vol->dev);
0271 if (err) {
0272 ubi_err(ubi, "cannot add device");
0273 goto out_mapping;
0274 }
0275
0276
0277 memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
0278 vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
0279 vtbl_rec.alignment = cpu_to_be32(vol->alignment);
0280 vtbl_rec.data_pad = cpu_to_be32(vol->data_pad);
0281 vtbl_rec.name_len = cpu_to_be16(vol->name_len);
0282 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
0283 vtbl_rec.vol_type = UBI_VID_DYNAMIC;
0284 else
0285 vtbl_rec.vol_type = UBI_VID_STATIC;
0286
0287 if (vol->skip_check)
0288 vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
0289
0290 memcpy(vtbl_rec.name, vol->name, vol->name_len);
0291
0292 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
0293 if (err)
0294 goto out_sysfs;
0295
0296 ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
0297 self_check_volumes(ubi);
0298 return err;
0299
0300 out_sysfs:
0301
0302
0303
0304
0305
0306 cdev_device_del(&vol->cdev, &vol->dev);
0307 out_mapping:
0308 spin_lock(&ubi->volumes_lock);
0309 ubi->volumes[vol_id] = NULL;
0310 ubi->vol_count -= 1;
0311 spin_unlock(&ubi->volumes_lock);
0312 out_acc:
0313 spin_lock(&ubi->volumes_lock);
0314 ubi->rsvd_pebs -= vol->reserved_pebs;
0315 ubi->avail_pebs += vol->reserved_pebs;
0316 out_unlock:
0317 spin_unlock(&ubi->volumes_lock);
0318 put_device(&vol->dev);
0319 ubi_err(ubi, "cannot create volume %d, error %d", vol_id, err);
0320 return err;
0321 }
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333 int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
0334 {
0335 struct ubi_volume *vol = desc->vol;
0336 struct ubi_device *ubi = vol->ubi;
0337 int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
0338
0339 dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
0340 ubi_assert(desc->mode == UBI_EXCLUSIVE);
0341 ubi_assert(vol == ubi->volumes[vol_id]);
0342
0343 if (ubi->ro_mode)
0344 return -EROFS;
0345
0346 spin_lock(&ubi->volumes_lock);
0347 if (vol->ref_count > 1) {
0348
0349
0350
0351
0352 err = -EBUSY;
0353 goto out_unlock;
0354 }
0355 ubi->volumes[vol_id] = NULL;
0356 spin_unlock(&ubi->volumes_lock);
0357
0358 if (!no_vtbl) {
0359 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
0360 if (err)
0361 goto out_err;
0362 }
0363
0364 for (i = 0; i < vol->reserved_pebs; i++) {
0365 err = ubi_eba_unmap_leb(ubi, vol, i);
0366 if (err)
0367 goto out_err;
0368 }
0369
0370 cdev_device_del(&vol->cdev, &vol->dev);
0371 put_device(&vol->dev);
0372
0373 spin_lock(&ubi->volumes_lock);
0374 ubi->rsvd_pebs -= reserved_pebs;
0375 ubi->avail_pebs += reserved_pebs;
0376 ubi_update_reserved(ubi);
0377 ubi->vol_count -= 1;
0378 spin_unlock(&ubi->volumes_lock);
0379
0380 ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
0381 if (!no_vtbl)
0382 self_check_volumes(ubi);
0383
0384 return 0;
0385
0386 out_err:
0387 ubi_err(ubi, "cannot remove volume %d, error %d", vol_id, err);
0388 spin_lock(&ubi->volumes_lock);
0389 ubi->volumes[vol_id] = vol;
0390 out_unlock:
0391 spin_unlock(&ubi->volumes_lock);
0392 return err;
0393 }
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404 int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
0405 {
0406 int i, err, pebs;
0407 struct ubi_volume *vol = desc->vol;
0408 struct ubi_device *ubi = vol->ubi;
0409 struct ubi_vtbl_record vtbl_rec;
0410 struct ubi_eba_table *new_eba_tbl = NULL;
0411 int vol_id = vol->vol_id;
0412
0413 if (ubi->ro_mode)
0414 return -EROFS;
0415
0416 dbg_gen("re-size device %d, volume %d to from %d to %d PEBs",
0417 ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs);
0418
0419 if (vol->vol_type == UBI_STATIC_VOLUME &&
0420 reserved_pebs < vol->used_ebs) {
0421 ubi_err(ubi, "too small size %d, %d LEBs contain data",
0422 reserved_pebs, vol->used_ebs);
0423 return -EINVAL;
0424 }
0425
0426
0427 if (reserved_pebs == vol->reserved_pebs)
0428 return 0;
0429
0430 new_eba_tbl = ubi_eba_create_table(vol, reserved_pebs);
0431 if (IS_ERR(new_eba_tbl))
0432 return PTR_ERR(new_eba_tbl);
0433
0434 spin_lock(&ubi->volumes_lock);
0435 if (vol->ref_count > 1) {
0436 spin_unlock(&ubi->volumes_lock);
0437 err = -EBUSY;
0438 goto out_free;
0439 }
0440 spin_unlock(&ubi->volumes_lock);
0441
0442
0443 pebs = reserved_pebs - vol->reserved_pebs;
0444 if (pebs > 0) {
0445 spin_lock(&ubi->volumes_lock);
0446 if (pebs > ubi->avail_pebs) {
0447 ubi_err(ubi, "not enough PEBs: requested %d, available %d",
0448 pebs, ubi->avail_pebs);
0449 if (ubi->corr_peb_count)
0450 ubi_err(ubi, "%d PEBs are corrupted and not used",
0451 ubi->corr_peb_count);
0452 spin_unlock(&ubi->volumes_lock);
0453 err = -ENOSPC;
0454 goto out_free;
0455 }
0456 ubi->avail_pebs -= pebs;
0457 ubi->rsvd_pebs += pebs;
0458 ubi_eba_copy_table(vol, new_eba_tbl, vol->reserved_pebs);
0459 ubi_eba_replace_table(vol, new_eba_tbl);
0460 spin_unlock(&ubi->volumes_lock);
0461 }
0462
0463 if (pebs < 0) {
0464 for (i = 0; i < -pebs; i++) {
0465 err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
0466 if (err)
0467 goto out_acc;
0468 }
0469 spin_lock(&ubi->volumes_lock);
0470 ubi->rsvd_pebs += pebs;
0471 ubi->avail_pebs -= pebs;
0472 ubi_update_reserved(ubi);
0473 ubi_eba_copy_table(vol, new_eba_tbl, reserved_pebs);
0474 ubi_eba_replace_table(vol, new_eba_tbl);
0475 spin_unlock(&ubi->volumes_lock);
0476 }
0477
0478
0479
0480
0481
0482
0483 if (pebs < 0) {
0484 err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
0485 if (err)
0486 goto out_acc;
0487 }
0488
0489
0490 vtbl_rec = ubi->vtbl[vol_id];
0491 vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
0492 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
0493 if (err)
0494 goto out_acc;
0495
0496 vol->reserved_pebs = reserved_pebs;
0497 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
0498 vol->used_ebs = reserved_pebs;
0499 vol->last_eb_bytes = vol->usable_leb_size;
0500 vol->used_bytes =
0501 (long long)vol->used_ebs * vol->usable_leb_size;
0502 }
0503
0504 ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
0505 self_check_volumes(ubi);
0506 return err;
0507
0508 out_acc:
0509 if (pebs > 0) {
0510 spin_lock(&ubi->volumes_lock);
0511 ubi->rsvd_pebs -= pebs;
0512 ubi->avail_pebs += pebs;
0513 spin_unlock(&ubi->volumes_lock);
0514 }
0515 out_free:
0516 kfree(new_eba_tbl);
0517 return err;
0518 }
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529 int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
0530 {
0531 int err;
0532 struct ubi_rename_entry *re;
0533
0534 err = ubi_vtbl_rename_volumes(ubi, rename_list);
0535 if (err)
0536 return err;
0537
0538 list_for_each_entry(re, rename_list, list) {
0539 if (re->remove) {
0540 err = ubi_remove_volume(re->desc, 1);
0541 if (err)
0542 break;
0543 } else {
0544 struct ubi_volume *vol = re->desc->vol;
0545
0546 spin_lock(&ubi->volumes_lock);
0547 vol->name_len = re->new_name_len;
0548 memcpy(vol->name, re->new_name, re->new_name_len + 1);
0549 spin_unlock(&ubi->volumes_lock);
0550 ubi_volume_notify(ubi, vol, UBI_VOLUME_RENAMED);
0551 }
0552 }
0553
0554 if (!err)
0555 self_check_volumes(ubi);
0556 return err;
0557 }
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568 int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
0569 {
0570 int err, vol_id = vol->vol_id;
0571 dev_t dev;
0572
0573 dbg_gen("add volume %d", vol_id);
0574
0575
0576 cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
0577 vol->cdev.owner = THIS_MODULE;
0578 dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
0579 err = cdev_add(&vol->cdev, dev, 1);
0580 if (err) {
0581 ubi_err(ubi, "cannot add character device for volume %d, error %d",
0582 vol_id, err);
0583 return err;
0584 }
0585
0586 vol->dev.release = vol_release;
0587 vol->dev.parent = &ubi->dev;
0588 vol->dev.devt = dev;
0589 vol->dev.class = &ubi_class;
0590 vol->dev.groups = volume_dev_groups;
0591 dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
0592 err = device_register(&vol->dev);
0593 if (err)
0594 goto out_cdev;
0595
0596 self_check_volumes(ubi);
0597 return err;
0598
0599 out_cdev:
0600 cdev_del(&vol->cdev);
0601 return err;
0602 }
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612 void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
0613 {
0614 dbg_gen("free volume %d", vol->vol_id);
0615
0616 ubi->volumes[vol->vol_id] = NULL;
0617 cdev_del(&vol->cdev);
0618 device_unregister(&vol->dev);
0619 }
0620
0621
0622
0623
0624
0625
0626
0627
0628 static int self_check_volume(struct ubi_device *ubi, int vol_id)
0629 {
0630 int idx = vol_id2idx(ubi, vol_id);
0631 int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
0632 const struct ubi_volume *vol;
0633 long long n;
0634 const char *name;
0635
0636 spin_lock(&ubi->volumes_lock);
0637 reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
0638 vol = ubi->volumes[idx];
0639
0640 if (!vol) {
0641 if (reserved_pebs) {
0642 ubi_err(ubi, "no volume info, but volume exists");
0643 goto fail;
0644 }
0645 spin_unlock(&ubi->volumes_lock);
0646 return 0;
0647 }
0648
0649 if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
0650 vol->name_len < 0) {
0651 ubi_err(ubi, "negative values");
0652 goto fail;
0653 }
0654 if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
0655 ubi_err(ubi, "bad alignment");
0656 goto fail;
0657 }
0658
0659 n = vol->alignment & (ubi->min_io_size - 1);
0660 if (vol->alignment != 1 && n) {
0661 ubi_err(ubi, "alignment is not multiple of min I/O unit");
0662 goto fail;
0663 }
0664
0665 n = ubi->leb_size % vol->alignment;
0666 if (vol->data_pad != n) {
0667 ubi_err(ubi, "bad data_pad, has to be %lld", n);
0668 goto fail;
0669 }
0670
0671 if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
0672 vol->vol_type != UBI_STATIC_VOLUME) {
0673 ubi_err(ubi, "bad vol_type");
0674 goto fail;
0675 }
0676
0677 if (vol->upd_marker && vol->corrupted) {
0678 ubi_err(ubi, "update marker and corrupted simultaneously");
0679 goto fail;
0680 }
0681
0682 if (vol->reserved_pebs > ubi->good_peb_count) {
0683 ubi_err(ubi, "too large reserved_pebs");
0684 goto fail;
0685 }
0686
0687 n = ubi->leb_size - vol->data_pad;
0688 if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
0689 ubi_err(ubi, "bad usable_leb_size, has to be %lld", n);
0690 goto fail;
0691 }
0692
0693 if (vol->name_len > UBI_VOL_NAME_MAX) {
0694 ubi_err(ubi, "too long volume name, max is %d",
0695 UBI_VOL_NAME_MAX);
0696 goto fail;
0697 }
0698
0699 n = strnlen(vol->name, vol->name_len + 1);
0700 if (n != vol->name_len) {
0701 ubi_err(ubi, "bad name_len %lld", n);
0702 goto fail;
0703 }
0704
0705 n = (long long)vol->used_ebs * vol->usable_leb_size;
0706 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
0707 if (vol->corrupted) {
0708 ubi_err(ubi, "corrupted dynamic volume");
0709 goto fail;
0710 }
0711 if (vol->used_ebs != vol->reserved_pebs) {
0712 ubi_err(ubi, "bad used_ebs");
0713 goto fail;
0714 }
0715 if (vol->last_eb_bytes != vol->usable_leb_size) {
0716 ubi_err(ubi, "bad last_eb_bytes");
0717 goto fail;
0718 }
0719 if (vol->used_bytes != n) {
0720 ubi_err(ubi, "bad used_bytes");
0721 goto fail;
0722 }
0723
0724 if (vol->skip_check) {
0725 ubi_err(ubi, "bad skip_check");
0726 goto fail;
0727 }
0728 } else {
0729 if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
0730 ubi_err(ubi, "bad used_ebs");
0731 goto fail;
0732 }
0733 if (vol->last_eb_bytes < 0 ||
0734 vol->last_eb_bytes > vol->usable_leb_size) {
0735 ubi_err(ubi, "bad last_eb_bytes");
0736 goto fail;
0737 }
0738 if (vol->used_bytes < 0 || vol->used_bytes > n ||
0739 vol->used_bytes < n - vol->usable_leb_size) {
0740 ubi_err(ubi, "bad used_bytes");
0741 goto fail;
0742 }
0743 }
0744
0745 alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
0746 data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
0747 name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
0748 upd_marker = ubi->vtbl[vol_id].upd_marker;
0749 name = &ubi->vtbl[vol_id].name[0];
0750 if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
0751 vol_type = UBI_DYNAMIC_VOLUME;
0752 else
0753 vol_type = UBI_STATIC_VOLUME;
0754
0755 if (alignment != vol->alignment || data_pad != vol->data_pad ||
0756 upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
0757 name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
0758 ubi_err(ubi, "volume info is different");
0759 goto fail;
0760 }
0761
0762 spin_unlock(&ubi->volumes_lock);
0763 return 0;
0764
0765 fail:
0766 ubi_err(ubi, "self-check failed for volume %d", vol_id);
0767 if (vol)
0768 ubi_dump_vol_info(vol);
0769 ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
0770 dump_stack();
0771 spin_unlock(&ubi->volumes_lock);
0772 return -EINVAL;
0773 }
0774
0775
0776
0777
0778
0779
0780
0781 static int self_check_volumes(struct ubi_device *ubi)
0782 {
0783 int i, err = 0;
0784
0785 if (!ubi_dbg_chk_gen(ubi))
0786 return 0;
0787
0788 for (i = 0; i < ubi->vtbl_slots; i++) {
0789 err = self_check_volume(ubi, i);
0790 if (err)
0791 break;
0792 }
0793
0794 return err;
0795 }