0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/module.h>
0011 #include <linux/err.h>
0012 #include <linux/slab.h>
0013 #include <linux/namei.h>
0014 #include <linux/fs.h>
0015 #include <asm/div64.h>
0016 #include "ubi.h"
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
0027 {
0028 di->ubi_num = ubi->ubi_num;
0029 di->leb_size = ubi->leb_size;
0030 di->leb_start = ubi->leb_start;
0031 di->min_io_size = ubi->min_io_size;
0032 di->max_write_size = ubi->max_write_size;
0033 di->ro_mode = ubi->ro_mode;
0034 di->cdev = ubi->cdev.dev;
0035 }
0036 EXPORT_SYMBOL_GPL(ubi_do_get_device_info);
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
0047 {
0048 struct ubi_device *ubi;
0049
0050 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
0051 return -EINVAL;
0052 ubi = ubi_get_device(ubi_num);
0053 if (!ubi)
0054 return -ENODEV;
0055 ubi_do_get_device_info(ubi, di);
0056 ubi_put_device(ubi);
0057 return 0;
0058 }
0059 EXPORT_SYMBOL_GPL(ubi_get_device_info);
0060
0061
0062
0063
0064
0065
0066
0067 void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
0068 struct ubi_volume_info *vi)
0069 {
0070 vi->vol_id = vol->vol_id;
0071 vi->ubi_num = ubi->ubi_num;
0072 vi->size = vol->reserved_pebs;
0073 vi->used_bytes = vol->used_bytes;
0074 vi->vol_type = vol->vol_type;
0075 vi->corrupted = vol->corrupted;
0076 vi->upd_marker = vol->upd_marker;
0077 vi->alignment = vol->alignment;
0078 vi->usable_leb_size = vol->usable_leb_size;
0079 vi->name_len = vol->name_len;
0080 vi->name = vol->name;
0081 vi->cdev = vol->cdev.dev;
0082 }
0083
0084
0085
0086
0087
0088
0089 void ubi_get_volume_info(struct ubi_volume_desc *desc,
0090 struct ubi_volume_info *vi)
0091 {
0092 ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi);
0093 }
0094 EXPORT_SYMBOL_GPL(ubi_get_volume_info);
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114 struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
0115 {
0116 int err;
0117 struct ubi_volume_desc *desc;
0118 struct ubi_device *ubi;
0119 struct ubi_volume *vol;
0120
0121 dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode);
0122
0123 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
0124 return ERR_PTR(-EINVAL);
0125
0126 if (mode != UBI_READONLY && mode != UBI_READWRITE &&
0127 mode != UBI_EXCLUSIVE && mode != UBI_METAONLY)
0128 return ERR_PTR(-EINVAL);
0129
0130
0131
0132
0133 ubi = ubi_get_device(ubi_num);
0134 if (!ubi)
0135 return ERR_PTR(-ENODEV);
0136
0137 if (vol_id < 0 || vol_id >= ubi->vtbl_slots) {
0138 err = -EINVAL;
0139 goto out_put_ubi;
0140 }
0141
0142 desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
0143 if (!desc) {
0144 err = -ENOMEM;
0145 goto out_put_ubi;
0146 }
0147
0148 err = -ENODEV;
0149 if (!try_module_get(THIS_MODULE))
0150 goto out_free;
0151
0152 spin_lock(&ubi->volumes_lock);
0153 vol = ubi->volumes[vol_id];
0154 if (!vol)
0155 goto out_unlock;
0156
0157 err = -EBUSY;
0158 switch (mode) {
0159 case UBI_READONLY:
0160 if (vol->exclusive)
0161 goto out_unlock;
0162 vol->readers += 1;
0163 break;
0164
0165 case UBI_READWRITE:
0166 if (vol->exclusive || vol->writers > 0)
0167 goto out_unlock;
0168 vol->writers += 1;
0169 break;
0170
0171 case UBI_EXCLUSIVE:
0172 if (vol->exclusive || vol->writers || vol->readers ||
0173 vol->metaonly)
0174 goto out_unlock;
0175 vol->exclusive = 1;
0176 break;
0177
0178 case UBI_METAONLY:
0179 if (vol->metaonly || vol->exclusive)
0180 goto out_unlock;
0181 vol->metaonly = 1;
0182 break;
0183 }
0184 get_device(&vol->dev);
0185 vol->ref_count += 1;
0186 spin_unlock(&ubi->volumes_lock);
0187
0188 desc->vol = vol;
0189 desc->mode = mode;
0190
0191 mutex_lock(&ubi->ckvol_mutex);
0192 if (!vol->checked && !vol->skip_check) {
0193
0194 err = ubi_check_volume(ubi, vol_id);
0195 if (err < 0) {
0196 mutex_unlock(&ubi->ckvol_mutex);
0197 ubi_close_volume(desc);
0198 return ERR_PTR(err);
0199 }
0200 if (err == 1) {
0201 ubi_warn(ubi, "volume %d on UBI device %d is corrupted",
0202 vol_id, ubi->ubi_num);
0203 vol->corrupted = 1;
0204 }
0205 vol->checked = 1;
0206 }
0207 mutex_unlock(&ubi->ckvol_mutex);
0208
0209 return desc;
0210
0211 out_unlock:
0212 spin_unlock(&ubi->volumes_lock);
0213 module_put(THIS_MODULE);
0214 out_free:
0215 kfree(desc);
0216 out_put_ubi:
0217 ubi_err(ubi, "cannot open device %d, volume %d, error %d",
0218 ubi_num, vol_id, err);
0219 ubi_put_device(ubi);
0220 return ERR_PTR(err);
0221 }
0222 EXPORT_SYMBOL_GPL(ubi_open_volume);
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232 struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
0233 int mode)
0234 {
0235 int i, vol_id = -1, len;
0236 struct ubi_device *ubi;
0237 struct ubi_volume_desc *ret;
0238
0239 dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode);
0240
0241 if (!name)
0242 return ERR_PTR(-EINVAL);
0243
0244 len = strnlen(name, UBI_VOL_NAME_MAX + 1);
0245 if (len > UBI_VOL_NAME_MAX)
0246 return ERR_PTR(-EINVAL);
0247
0248 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
0249 return ERR_PTR(-EINVAL);
0250
0251 ubi = ubi_get_device(ubi_num);
0252 if (!ubi)
0253 return ERR_PTR(-ENODEV);
0254
0255 spin_lock(&ubi->volumes_lock);
0256
0257 for (i = 0; i < ubi->vtbl_slots; i++) {
0258 struct ubi_volume *vol = ubi->volumes[i];
0259
0260 if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
0261 vol_id = i;
0262 break;
0263 }
0264 }
0265 spin_unlock(&ubi->volumes_lock);
0266
0267 if (vol_id >= 0)
0268 ret = ubi_open_volume(ubi_num, vol_id, mode);
0269 else
0270 ret = ERR_PTR(-ENODEV);
0271
0272
0273
0274
0275
0276 ubi_put_device(ubi);
0277 return ret;
0278 }
0279 EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289 struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
0290 {
0291 int error, ubi_num, vol_id;
0292 struct path path;
0293 struct kstat stat;
0294
0295 dbg_gen("open volume %s, mode %d", pathname, mode);
0296
0297 if (!pathname || !*pathname)
0298 return ERR_PTR(-EINVAL);
0299
0300 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
0301 if (error)
0302 return ERR_PTR(error);
0303
0304 error = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
0305 path_put(&path);
0306 if (error)
0307 return ERR_PTR(error);
0308
0309 if (!S_ISCHR(stat.mode))
0310 return ERR_PTR(-EINVAL);
0311
0312 ubi_num = ubi_major2num(MAJOR(stat.rdev));
0313 vol_id = MINOR(stat.rdev) - 1;
0314
0315 if (vol_id >= 0 && ubi_num >= 0)
0316 return ubi_open_volume(ubi_num, vol_id, mode);
0317 return ERR_PTR(-ENODEV);
0318 }
0319 EXPORT_SYMBOL_GPL(ubi_open_volume_path);
0320
0321
0322
0323
0324
0325 void ubi_close_volume(struct ubi_volume_desc *desc)
0326 {
0327 struct ubi_volume *vol = desc->vol;
0328 struct ubi_device *ubi = vol->ubi;
0329
0330 dbg_gen("close device %d, volume %d, mode %d",
0331 ubi->ubi_num, vol->vol_id, desc->mode);
0332
0333 spin_lock(&ubi->volumes_lock);
0334 switch (desc->mode) {
0335 case UBI_READONLY:
0336 vol->readers -= 1;
0337 break;
0338 case UBI_READWRITE:
0339 vol->writers -= 1;
0340 break;
0341 case UBI_EXCLUSIVE:
0342 vol->exclusive = 0;
0343 break;
0344 case UBI_METAONLY:
0345 vol->metaonly = 0;
0346 break;
0347 }
0348 vol->ref_count -= 1;
0349 spin_unlock(&ubi->volumes_lock);
0350
0351 kfree(desc);
0352 put_device(&vol->dev);
0353 ubi_put_device(ubi);
0354 module_put(THIS_MODULE);
0355 }
0356 EXPORT_SYMBOL_GPL(ubi_close_volume);
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368 static int leb_read_sanity_check(struct ubi_volume_desc *desc, int lnum,
0369 int offset, int len)
0370 {
0371 struct ubi_volume *vol = desc->vol;
0372 struct ubi_device *ubi = vol->ubi;
0373 int vol_id = vol->vol_id;
0374
0375 if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
0376 lnum >= vol->used_ebs || offset < 0 || len < 0 ||
0377 offset + len > vol->usable_leb_size)
0378 return -EINVAL;
0379
0380 if (vol->vol_type == UBI_STATIC_VOLUME) {
0381 if (vol->used_ebs == 0)
0382
0383 return 0;
0384 if (lnum == vol->used_ebs - 1 &&
0385 offset + len > vol->last_eb_bytes)
0386 return -EINVAL;
0387 }
0388
0389 if (vol->upd_marker)
0390 return -EBADF;
0391
0392 return 0;
0393 }
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422 int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
0423 int len, int check)
0424 {
0425 struct ubi_volume *vol = desc->vol;
0426 struct ubi_device *ubi = vol->ubi;
0427 int err, vol_id = vol->vol_id;
0428
0429 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
0430
0431 err = leb_read_sanity_check(desc, lnum, offset, len);
0432 if (err < 0)
0433 return err;
0434
0435 if (len == 0)
0436 return 0;
0437
0438 err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
0439 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
0440 ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
0441 vol->corrupted = 1;
0442 }
0443
0444 return err;
0445 }
0446 EXPORT_SYMBOL_GPL(ubi_leb_read);
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462 int ubi_leb_read_sg(struct ubi_volume_desc *desc, int lnum, struct ubi_sgl *sgl,
0463 int offset, int len, int check)
0464 {
0465 struct ubi_volume *vol = desc->vol;
0466 struct ubi_device *ubi = vol->ubi;
0467 int err, vol_id = vol->vol_id;
0468
0469 dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
0470
0471 err = leb_read_sanity_check(desc, lnum, offset, len);
0472 if (err < 0)
0473 return err;
0474
0475 if (len == 0)
0476 return 0;
0477
0478 err = ubi_eba_read_leb_sg(ubi, vol, sgl, lnum, offset, len, check);
0479 if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) {
0480 ubi_warn(ubi, "mark volume %d as corrupted", vol_id);
0481 vol->corrupted = 1;
0482 }
0483
0484 return err;
0485 }
0486 EXPORT_SYMBOL_GPL(ubi_leb_read_sg);
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513 int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
0514 int offset, int len)
0515 {
0516 struct ubi_volume *vol = desc->vol;
0517 struct ubi_device *ubi = vol->ubi;
0518 int vol_id = vol->vol_id;
0519
0520 dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
0521
0522 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
0523 return -EINVAL;
0524
0525 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
0526 return -EROFS;
0527
0528 if (!ubi_leb_valid(vol, lnum) || offset < 0 || len < 0 ||
0529 offset + len > vol->usable_leb_size ||
0530 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
0531 return -EINVAL;
0532
0533 if (vol->upd_marker)
0534 return -EBADF;
0535
0536 if (len == 0)
0537 return 0;
0538
0539 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
0540 }
0541 EXPORT_SYMBOL_GPL(ubi_leb_write);
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558 int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
0559 int len)
0560 {
0561 struct ubi_volume *vol = desc->vol;
0562 struct ubi_device *ubi = vol->ubi;
0563 int vol_id = vol->vol_id;
0564
0565 dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
0566
0567 if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
0568 return -EINVAL;
0569
0570 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
0571 return -EROFS;
0572
0573 if (!ubi_leb_valid(vol, lnum) || len < 0 ||
0574 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
0575 return -EINVAL;
0576
0577 if (vol->upd_marker)
0578 return -EBADF;
0579
0580 if (len == 0)
0581 return 0;
0582
0583 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
0584 }
0585 EXPORT_SYMBOL_GPL(ubi_leb_change);
0586
0587
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599 int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
0600 {
0601 struct ubi_volume *vol = desc->vol;
0602 struct ubi_device *ubi = vol->ubi;
0603 int err;
0604
0605 dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
0606
0607 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
0608 return -EROFS;
0609
0610 if (!ubi_leb_valid(vol, lnum))
0611 return -EINVAL;
0612
0613 if (vol->upd_marker)
0614 return -EBADF;
0615
0616 err = ubi_eba_unmap_leb(ubi, vol, lnum);
0617 if (err)
0618 return err;
0619
0620 return ubi_wl_flush(ubi, vol->vol_id, lnum);
0621 }
0622 EXPORT_SYMBOL_GPL(ubi_leb_erase);
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660 int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
0661 {
0662 struct ubi_volume *vol = desc->vol;
0663 struct ubi_device *ubi = vol->ubi;
0664
0665 dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
0666
0667 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
0668 return -EROFS;
0669
0670 if (!ubi_leb_valid(vol, lnum))
0671 return -EINVAL;
0672
0673 if (vol->upd_marker)
0674 return -EBADF;
0675
0676 return ubi_eba_unmap_leb(ubi, vol, lnum);
0677 }
0678 EXPORT_SYMBOL_GPL(ubi_leb_unmap);
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696 int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
0697 {
0698 struct ubi_volume *vol = desc->vol;
0699 struct ubi_device *ubi = vol->ubi;
0700
0701 dbg_gen("map LEB %d:%d", vol->vol_id, lnum);
0702
0703 if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
0704 return -EROFS;
0705
0706 if (!ubi_leb_valid(vol, lnum))
0707 return -EINVAL;
0708
0709 if (vol->upd_marker)
0710 return -EBADF;
0711
0712 if (ubi_eba_is_mapped(vol, lnum))
0713 return -EBADMSG;
0714
0715 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
0716 }
0717 EXPORT_SYMBOL_GPL(ubi_leb_map);
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735 int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
0736 {
0737 struct ubi_volume *vol = desc->vol;
0738
0739 dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
0740
0741 if (!ubi_leb_valid(vol, lnum))
0742 return -EINVAL;
0743
0744 if (vol->upd_marker)
0745 return -EBADF;
0746
0747 return ubi_eba_is_mapped(vol, lnum);
0748 }
0749 EXPORT_SYMBOL_GPL(ubi_is_mapped);
0750
0751
0752
0753
0754
0755
0756
0757
0758
0759 int ubi_sync(int ubi_num)
0760 {
0761 struct ubi_device *ubi;
0762
0763 ubi = ubi_get_device(ubi_num);
0764 if (!ubi)
0765 return -ENODEV;
0766
0767 mtd_sync(ubi->mtd);
0768 ubi_put_device(ubi);
0769 return 0;
0770 }
0771 EXPORT_SYMBOL_GPL(ubi_sync);
0772
0773
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785 int ubi_flush(int ubi_num, int vol_id, int lnum)
0786 {
0787 struct ubi_device *ubi;
0788 int err = 0;
0789
0790 ubi = ubi_get_device(ubi_num);
0791 if (!ubi)
0792 return -ENODEV;
0793
0794 err = ubi_wl_flush(ubi, vol_id, lnum);
0795 ubi_put_device(ubi);
0796 return err;
0797 }
0798 EXPORT_SYMBOL_GPL(ubi_flush);
0799
0800 BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818 int ubi_register_volume_notifier(struct notifier_block *nb,
0819 int ignore_existing)
0820 {
0821 int err;
0822
0823 err = blocking_notifier_chain_register(&ubi_notifiers, nb);
0824 if (err != 0)
0825 return err;
0826 if (ignore_existing)
0827 return 0;
0828
0829
0830
0831
0832
0833
0834
0835 mutex_lock(&ubi_devices_mutex);
0836 ubi_enumerate_volumes(nb);
0837 mutex_unlock(&ubi_devices_mutex);
0838
0839 return err;
0840 }
0841 EXPORT_SYMBOL_GPL(ubi_register_volume_notifier);
0842
0843
0844
0845
0846
0847
0848
0849
0850 int ubi_unregister_volume_notifier(struct notifier_block *nb)
0851 {
0852 return blocking_notifier_chain_unregister(&ubi_notifiers, nb);
0853 }
0854 EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier);