0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef __UBI_UBI_H__
0010 #define __UBI_UBI_H__
0011
0012 #include <linux/types.h>
0013 #include <linux/list.h>
0014 #include <linux/rbtree.h>
0015 #include <linux/sched.h>
0016 #include <linux/wait.h>
0017 #include <linux/mutex.h>
0018 #include <linux/rwsem.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/fs.h>
0021 #include <linux/cdev.h>
0022 #include <linux/device.h>
0023 #include <linux/slab.h>
0024 #include <linux/string.h>
0025 #include <linux/vmalloc.h>
0026 #include <linux/notifier.h>
0027 #include <linux/mtd/mtd.h>
0028 #include <linux/mtd/ubi.h>
0029 #include <linux/pgtable.h>
0030
0031 #include "ubi-media.h"
0032
0033
0034 #define UBI_MAX_DEVICES 32
0035
0036
0037 #define UBI_NAME_STR "ubi"
0038
0039 struct ubi_device;
0040
0041
0042 __printf(2, 3)
0043 void ubi_msg(const struct ubi_device *ubi, const char *fmt, ...);
0044
0045
0046 __printf(2, 3)
0047 void ubi_warn(const struct ubi_device *ubi, const char *fmt, ...);
0048
0049
0050 __printf(2, 3)
0051 void ubi_err(const struct ubi_device *ubi, const char *fmt, ...);
0052
0053
0054 #define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
0055
0056
0057
0058
0059
0060 #define UBI_LEB_UNMAPPED -1
0061
0062
0063
0064
0065
0066 #define UBI_IO_RETRIES 3
0067
0068
0069
0070
0071
0072
0073 #define UBI_PROT_QUEUE_LEN 10
0074
0075
0076 #define UBI_UNKNOWN -1
0077
0078
0079
0080
0081
0082 #define UBI_DFS_DIR_NAME "ubi%d"
0083 #define UBI_DFS_DIR_LEN (3 + 2 + 1)
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 enum {
0103 UBI_IO_FF = 1,
0104 UBI_IO_FF_BITFLIPS,
0105 UBI_IO_BAD_HDR,
0106 UBI_IO_BAD_HDR_EBADMSG,
0107 UBI_IO_BITFLIPS,
0108 };
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 enum {
0126 MOVE_CANCEL_RACE = 1,
0127 MOVE_SOURCE_RD_ERR,
0128 MOVE_TARGET_RD_ERR,
0129 MOVE_TARGET_WR_ERR,
0130 MOVE_TARGET_BITFLIPS,
0131 MOVE_RETRY,
0132 };
0133
0134
0135
0136
0137
0138
0139
0140 enum {
0141 UBI_NO_FASTMAP = 1,
0142 UBI_BAD_FASTMAP,
0143 };
0144
0145
0146
0147
0148
0149
0150
0151 enum {
0152 POWER_CUT_EC_WRITE = 0x01,
0153 POWER_CUT_VID_WRITE = 0x02,
0154 };
0155
0156
0157
0158
0159
0160
0161
0162 struct ubi_vid_io_buf {
0163 struct ubi_vid_hdr *hdr;
0164 void *buffer;
0165 };
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 struct ubi_wl_entry {
0179 union {
0180 struct rb_node rb;
0181 struct list_head list;
0182 } u;
0183 int ec;
0184 int pnum;
0185 };
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201 struct ubi_ltree_entry {
0202 struct rb_node rb;
0203 int vol_id;
0204 int lnum;
0205 int users;
0206 struct rw_semaphore mutex;
0207 };
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222 struct ubi_rename_entry {
0223 int new_name_len;
0224 char new_name[UBI_VOL_NAME_MAX + 1];
0225 int remove;
0226 struct ubi_volume_desc *desc;
0227 struct list_head list;
0228 };
0229
0230 struct ubi_volume_desc;
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240 struct ubi_fastmap_layout {
0241 struct ubi_wl_entry *e[UBI_FM_MAX_BLOCKS];
0242 int to_be_tortured[UBI_FM_MAX_BLOCKS];
0243 int used_blocks;
0244 int max_pool_size;
0245 int max_wl_pool_size;
0246 };
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260 struct ubi_fm_pool {
0261 int pebs[UBI_FM_MAX_POOL_SIZE];
0262 int used;
0263 int size;
0264 int max_size;
0265 };
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277 struct ubi_eba_leb_desc {
0278 int lnum;
0279 int pnum;
0280 };
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338 struct ubi_volume {
0339 struct device dev;
0340 struct cdev cdev;
0341 struct ubi_device *ubi;
0342 int vol_id;
0343 int ref_count;
0344 int readers;
0345 int writers;
0346 int exclusive;
0347 int metaonly;
0348
0349 int reserved_pebs;
0350 int vol_type;
0351 int usable_leb_size;
0352 int used_ebs;
0353 int last_eb_bytes;
0354 long long used_bytes;
0355 int alignment;
0356 int data_pad;
0357 int name_len;
0358 char name[UBI_VOL_NAME_MAX + 1];
0359
0360 int upd_ebs;
0361 int ch_lnum;
0362 long long upd_bytes;
0363 long long upd_received;
0364 void *upd_buf;
0365
0366 struct ubi_eba_table *eba_tbl;
0367 unsigned int skip_check:1;
0368 unsigned int checked:1;
0369 unsigned int corrupted:1;
0370 unsigned int upd_marker:1;
0371 unsigned int updating:1;
0372 unsigned int changing_leb:1;
0373 unsigned int direct_writes:1;
0374
0375 #ifdef CONFIG_MTD_UBI_FASTMAP
0376 unsigned long *checkmap;
0377 #endif
0378 };
0379
0380
0381
0382
0383
0384
0385
0386 struct ubi_volume_desc {
0387 struct ubi_volume *vol;
0388 int mode;
0389 };
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416 struct ubi_debug_info {
0417 unsigned int chk_gen:1;
0418 unsigned int chk_io:1;
0419 unsigned int chk_fastmap:1;
0420 unsigned int disable_bgt:1;
0421 unsigned int emulate_bitflips:1;
0422 unsigned int emulate_io_failures:1;
0423 unsigned int emulate_power_cut:2;
0424 unsigned int power_cut_counter;
0425 unsigned int power_cut_min;
0426 unsigned int power_cut_max;
0427 char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
0428 struct dentry *dfs_dir;
0429 struct dentry *dfs_chk_gen;
0430 struct dentry *dfs_chk_io;
0431 struct dentry *dfs_chk_fastmap;
0432 struct dentry *dfs_disable_bgt;
0433 struct dentry *dfs_emulate_bitflips;
0434 struct dentry *dfs_emulate_io_failures;
0435 struct dentry *dfs_emulate_power_cut;
0436 struct dentry *dfs_power_cut_min;
0437 struct dentry *dfs_power_cut_max;
0438 };
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
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
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557 struct ubi_device {
0558 struct cdev cdev;
0559 struct device dev;
0560 int ubi_num;
0561 char ubi_name[sizeof(UBI_NAME_STR)+5];
0562 int vol_count;
0563 struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
0564 spinlock_t volumes_lock;
0565 int ref_count;
0566 int image_seq;
0567
0568 int rsvd_pebs;
0569 int avail_pebs;
0570 int beb_rsvd_pebs;
0571 int beb_rsvd_level;
0572 int bad_peb_limit;
0573
0574 int autoresize_vol_id;
0575 int vtbl_slots;
0576 int vtbl_size;
0577 struct ubi_vtbl_record *vtbl;
0578 struct mutex device_mutex;
0579
0580 int max_ec;
0581
0582 int mean_ec;
0583
0584
0585 unsigned long long global_sqnum;
0586 spinlock_t ltree_lock;
0587 struct rb_root ltree;
0588 struct mutex alc_mutex;
0589
0590
0591 int fm_disabled;
0592 struct ubi_fastmap_layout *fm;
0593 struct ubi_fm_pool fm_pool;
0594 struct ubi_fm_pool fm_wl_pool;
0595 struct rw_semaphore fm_eba_sem;
0596 struct rw_semaphore fm_protect;
0597 void *fm_buf;
0598 size_t fm_size;
0599 struct work_struct fm_work;
0600 int fm_work_scheduled;
0601 int fast_attach;
0602 struct ubi_wl_entry *fm_anchor;
0603 int fm_do_produce_anchor;
0604
0605
0606 struct rb_root used;
0607 struct rb_root erroneous;
0608 struct rb_root free;
0609 int free_count;
0610 struct rb_root scrub;
0611 struct list_head pq[UBI_PROT_QUEUE_LEN];
0612 int pq_head;
0613 spinlock_t wl_lock;
0614 struct mutex move_mutex;
0615 struct rw_semaphore work_sem;
0616 int wl_scheduled;
0617 struct ubi_wl_entry **lookuptbl;
0618 struct ubi_wl_entry *move_from;
0619 struct ubi_wl_entry *move_to;
0620 int move_to_put;
0621 struct list_head works;
0622 int works_count;
0623 struct task_struct *bgt_thread;
0624 int thread_enabled;
0625 char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
0626
0627
0628 long long flash_size;
0629 int peb_count;
0630 int peb_size;
0631 int bad_peb_count;
0632 int good_peb_count;
0633 int corr_peb_count;
0634 int erroneous_peb_count;
0635 int max_erroneous;
0636 int min_io_size;
0637 int hdrs_min_io_size;
0638 int ro_mode;
0639 int leb_size;
0640 int leb_start;
0641 int ec_hdr_alsize;
0642 int vid_hdr_alsize;
0643 int vid_hdr_offset;
0644 int vid_hdr_aloffset;
0645 int vid_hdr_shift;
0646 unsigned int bad_allowed:1;
0647 unsigned int nor_flash:1;
0648 int max_write_size;
0649 struct mtd_info *mtd;
0650
0651 void *peb_buf;
0652 struct mutex buf_mutex;
0653 struct mutex ckvol_mutex;
0654
0655 struct ubi_debug_info dbg;
0656 };
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675 struct ubi_ainf_peb {
0676 int ec;
0677 int pnum;
0678 int vol_id;
0679 int lnum;
0680 unsigned int scrub:1;
0681 unsigned int copy_flag:1;
0682 unsigned long long sqnum;
0683 union {
0684 struct rb_node rb;
0685 struct list_head list;
0686 } u;
0687 };
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710 struct ubi_ainf_volume {
0711 int vol_id;
0712 int highest_lnum;
0713 int leb_count;
0714 int vol_type;
0715 int used_ebs;
0716 int last_data_size;
0717 int data_pad;
0718 int compat;
0719 struct rb_node rb;
0720 struct rb_root root;
0721 };
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746
0747
0748
0749
0750
0751
0752
0753
0754
0755
0756
0757
0758
0759 struct ubi_attach_info {
0760 struct rb_root volumes;
0761 struct list_head corr;
0762 struct list_head free;
0763 struct list_head erase;
0764 struct list_head alien;
0765 struct list_head fastmap;
0766 int corr_peb_count;
0767 int empty_peb_count;
0768 int alien_peb_count;
0769 int bad_peb_count;
0770 int maybe_bad_peb_count;
0771 int vols_found;
0772 int highest_vol_id;
0773 int is_empty;
0774 int force_full_scan;
0775 int min_ec;
0776 int max_ec;
0777 unsigned long long max_sqnum;
0778 int mean_ec;
0779 uint64_t ec_sum;
0780 int ec_count;
0781 struct kmem_cache *aeb_slab_cache;
0782 struct ubi_ec_hdr *ech;
0783 struct ubi_vid_io_buf *vidb;
0784 };
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800
0801 struct ubi_work {
0802 struct list_head list;
0803 int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int shutdown);
0804
0805 struct ubi_wl_entry *e;
0806 int vol_id;
0807 int lnum;
0808 int torture;
0809 };
0810
0811 #include "debug.h"
0812
0813 extern struct kmem_cache *ubi_wl_entry_slab;
0814 extern const struct file_operations ubi_ctrl_cdev_operations;
0815 extern const struct file_operations ubi_cdev_operations;
0816 extern const struct file_operations ubi_vol_cdev_operations;
0817 extern struct class ubi_class;
0818 extern struct mutex ubi_devices_mutex;
0819 extern struct blocking_notifier_head ubi_notifiers;
0820
0821
0822 struct ubi_ainf_peb *ubi_alloc_aeb(struct ubi_attach_info *ai, int pnum,
0823 int ec);
0824 void ubi_free_aeb(struct ubi_attach_info *ai, struct ubi_ainf_peb *aeb);
0825 int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
0826 int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips);
0827 struct ubi_ainf_volume *ubi_add_av(struct ubi_attach_info *ai, int vol_id);
0828 struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
0829 int vol_id);
0830 void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av);
0831 struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
0832 struct ubi_attach_info *ai);
0833 int ubi_attach(struct ubi_device *ubi, int force_scan);
0834 void ubi_destroy_ai(struct ubi_attach_info *ai);
0835
0836
0837 int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
0838 struct ubi_vtbl_record *vtbl_rec);
0839 int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
0840 struct list_head *rename_list);
0841 int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai);
0842
0843
0844 int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
0845 int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
0846 int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
0847 int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
0848 int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
0849 void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
0850
0851
0852 int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
0853 long long bytes);
0854 int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
0855 const void __user *buf, int count);
0856 int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
0857 const struct ubi_leb_change_req *req);
0858 int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
0859 const void __user *buf, int count);
0860
0861
0862 int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
0863 int length);
0864 int ubi_check_volume(struct ubi_device *ubi, int vol_id);
0865 void ubi_update_reserved(struct ubi_device *ubi);
0866 void ubi_calculate_reserved(struct ubi_device *ubi);
0867 int ubi_check_pattern(const void *buf, uint8_t patt, int size);
0868
0869 static inline bool ubi_leb_valid(struct ubi_volume *vol, int lnum)
0870 {
0871 return lnum >= 0 && lnum < vol->reserved_pebs;
0872 }
0873
0874
0875 struct ubi_eba_table *ubi_eba_create_table(struct ubi_volume *vol,
0876 int nentries);
0877 void ubi_eba_destroy_table(struct ubi_eba_table *tbl);
0878 void ubi_eba_copy_table(struct ubi_volume *vol, struct ubi_eba_table *dst,
0879 int nentries);
0880 void ubi_eba_replace_table(struct ubi_volume *vol, struct ubi_eba_table *tbl);
0881 void ubi_eba_get_ldesc(struct ubi_volume *vol, int lnum,
0882 struct ubi_eba_leb_desc *ldesc);
0883 bool ubi_eba_is_mapped(struct ubi_volume *vol, int lnum);
0884 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
0885 int lnum);
0886 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
0887 void *buf, int offset, int len, int check);
0888 int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
0889 struct ubi_sgl *sgl, int lnum, int offset, int len,
0890 int check);
0891 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
0892 const void *buf, int offset, int len);
0893 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
0894 int lnum, const void *buf, int len, int used_ebs);
0895 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
0896 int lnum, const void *buf, int len);
0897 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
0898 struct ubi_vid_io_buf *vidb);
0899 int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
0900 unsigned long long ubi_next_sqnum(struct ubi_device *ubi);
0901 int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
0902 struct ubi_attach_info *ai_scan);
0903
0904
0905 int ubi_wl_get_peb(struct ubi_device *ubi);
0906 int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
0907 int pnum, int torture);
0908 int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum);
0909 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
0910 int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
0911 void ubi_wl_close(struct ubi_device *ubi);
0912 int ubi_thread(void *u);
0913 struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor);
0914 int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *used_e,
0915 int lnum, int torture);
0916 int ubi_is_erase_work(struct ubi_work *wrk);
0917 void ubi_refill_pools(struct ubi_device *ubi);
0918 int ubi_ensure_anchor_pebs(struct ubi_device *ubi);
0919 int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force_scrub);
0920
0921
0922 int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
0923 int len);
0924 int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
0925 int len);
0926 int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
0927 int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
0928 int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
0929 int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
0930 struct ubi_ec_hdr *ec_hdr, int verbose);
0931 int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
0932 struct ubi_ec_hdr *ec_hdr);
0933 int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
0934 struct ubi_vid_io_buf *vidb, int verbose);
0935 int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
0936 struct ubi_vid_io_buf *vidb);
0937
0938
0939 int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
0940 int vid_hdr_offset, int max_beb_per1024);
0941 int ubi_detach_mtd_dev(int ubi_num, int anyway);
0942 struct ubi_device *ubi_get_device(int ubi_num);
0943 void ubi_put_device(struct ubi_device *ubi);
0944 struct ubi_device *ubi_get_by_major(int major);
0945 int ubi_major2num(int major);
0946 int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
0947 int ntype);
0948 int ubi_notify_all(struct ubi_device *ubi, int ntype,
0949 struct notifier_block *nb);
0950 int ubi_enumerate_volumes(struct notifier_block *nb);
0951 void ubi_free_all_volumes(struct ubi_device *ubi);
0952 void ubi_free_internal_volumes(struct ubi_device *ubi);
0953
0954
0955 void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
0956 void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
0957 struct ubi_volume_info *vi);
0958
0959 int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
0960 int pnum, const struct ubi_vid_hdr *vid_hdr);
0961
0962
0963 #ifdef CONFIG_MTD_UBI_FASTMAP
0964 size_t ubi_calc_fm_size(struct ubi_device *ubi);
0965 int ubi_update_fastmap(struct ubi_device *ubi);
0966 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
0967 struct ubi_attach_info *scan_ai);
0968 int ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count);
0969 void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol);
0970 #else
0971 static inline int ubi_update_fastmap(struct ubi_device *ubi) { return 0; }
0972 static inline int ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count) { return 0; }
0973 static inline void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol) {}
0974 #endif
0975
0976
0977 #ifdef CONFIG_MTD_UBI_BLOCK
0978 int ubiblock_init(void);
0979 void ubiblock_exit(void);
0980 int ubiblock_create(struct ubi_volume_info *vi);
0981 int ubiblock_remove(struct ubi_volume_info *vi);
0982 #else
0983 static inline int ubiblock_init(void) { return 0; }
0984 static inline void ubiblock_exit(void) {}
0985 static inline int ubiblock_create(struct ubi_volume_info *vi)
0986 {
0987 return -ENOSYS;
0988 }
0989 static inline int ubiblock_remove(struct ubi_volume_info *vi)
0990 {
0991 return -ENOSYS;
0992 }
0993 #endif
0994
0995
0996
0997
0998
0999
1000
1001 #define ubi_for_each_free_peb(ubi, e, tmp_rb) \
1002 ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->free, u.rb)
1003
1004
1005
1006
1007
1008
1009
1010 #define ubi_for_each_used_peb(ubi, e, tmp_rb) \
1011 ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->used, u.rb)
1012
1013
1014
1015
1016
1017
1018
1019 #define ubi_for_each_scrub_peb(ubi, e, tmp_rb) \
1020 ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->scrub, u.rb)
1021
1022
1023
1024
1025
1026
1027
1028 #define ubi_for_each_protected_peb(ubi, i, e) \
1029 for ((i) = 0; (i) < UBI_PROT_QUEUE_LEN; (i)++) \
1030 list_for_each_entry((e), &(ubi->pq[(i)]), u.list)
1031
1032
1033
1034
1035
1036
1037
1038
1039 #define ubi_rb_for_each_entry(rb, pos, root, member) \
1040 for (rb = rb_first(root), \
1041 pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \
1042 rb; \
1043 rb = rb_next(rb), \
1044 pos = (rb ? container_of(rb, typeof(*pos), member) : NULL))
1045
1046
1047
1048
1049
1050
1051
1052
1053 static inline void ubi_move_aeb_to_list(struct ubi_ainf_volume *av,
1054 struct ubi_ainf_peb *aeb,
1055 struct list_head *list)
1056 {
1057 rb_erase(&aeb->u.rb, &av->root);
1058 list_add_tail(&aeb->u.list, list);
1059 }
1060
1061
1062
1063
1064
1065
1066
1067 static inline void ubi_init_vid_buf(const struct ubi_device *ubi,
1068 struct ubi_vid_io_buf *vidb,
1069 void *buf)
1070 {
1071 if (buf)
1072 memset(buf, 0, ubi->vid_hdr_alsize);
1073
1074 vidb->buffer = buf;
1075 vidb->hdr = buf + ubi->vid_hdr_shift;
1076 }
1077
1078
1079
1080
1081
1082
1083 static inline struct ubi_vid_io_buf *
1084 ubi_alloc_vid_buf(const struct ubi_device *ubi, gfp_t gfp_flags)
1085 {
1086 struct ubi_vid_io_buf *vidb;
1087 void *buf;
1088
1089 vidb = kzalloc(sizeof(*vidb), gfp_flags);
1090 if (!vidb)
1091 return NULL;
1092
1093 buf = kmalloc(ubi->vid_hdr_alsize, gfp_flags);
1094 if (!buf) {
1095 kfree(vidb);
1096 return NULL;
1097 }
1098
1099 ubi_init_vid_buf(ubi, vidb, buf);
1100
1101 return vidb;
1102 }
1103
1104
1105
1106
1107
1108 static inline void ubi_free_vid_buf(struct ubi_vid_io_buf *vidb)
1109 {
1110 if (!vidb)
1111 return;
1112
1113 kfree(vidb->buffer);
1114 kfree(vidb);
1115 }
1116
1117
1118
1119
1120
1121 static inline struct ubi_vid_hdr *ubi_get_vid_hdr(struct ubi_vid_io_buf *vidb)
1122 {
1123 return vidb->hdr;
1124 }
1125
1126
1127
1128
1129
1130
1131 static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
1132 int pnum, int offset, int len)
1133 {
1134 ubi_assert(offset >= 0);
1135 return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
1136 }
1137
1138
1139
1140
1141
1142
1143 static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
1144 int pnum, int offset, int len)
1145 {
1146 ubi_assert(offset >= 0);
1147 return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
1148 }
1149
1150
1151
1152
1153
1154 static inline void ubi_ro_mode(struct ubi_device *ubi)
1155 {
1156 if (!ubi->ro_mode) {
1157 ubi->ro_mode = 1;
1158 ubi_warn(ubi, "switch to read-only mode");
1159 dump_stack();
1160 }
1161 }
1162
1163
1164
1165
1166
1167
1168 static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
1169 {
1170 if (vol_id >= UBI_INTERNAL_VOL_START)
1171 return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
1172 else
1173 return vol_id;
1174 }
1175
1176
1177
1178
1179
1180
1181 static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
1182 {
1183 if (idx >= ubi->vtbl_slots)
1184 return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
1185 else
1186 return idx;
1187 }
1188
1189
1190
1191
1192
1193 static inline bool ubi_is_fm_vol(int vol_id)
1194 {
1195 switch (vol_id) {
1196 case UBI_FM_SB_VOLUME_ID:
1197 case UBI_FM_DATA_VOLUME_ID:
1198 return true;
1199 }
1200
1201 return false;
1202 }
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212 static inline struct ubi_wl_entry *ubi_find_fm_block(const struct ubi_device *ubi,
1213 int pnum)
1214 {
1215 int i;
1216
1217 if (ubi->fm) {
1218 for (i = 0; i < ubi->fm->used_blocks; i++) {
1219 if (ubi->fm->e[i]->pnum == pnum)
1220 return ubi->fm->e[i];
1221 }
1222 }
1223
1224 return NULL;
1225 }
1226
1227 #endif