1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#ifndef __UBI_UBI_H__
23#define __UBI_UBI_H__
24
25#ifdef UBI_LINUX
26#include <linux/init.h>
27#include <linux/types.h>
28#include <linux/list.h>
29#include <linux/rbtree.h>
30#include <linux/sched.h>
31#include <linux/wait.h>
32#include <linux/mutex.h>
33#include <linux/rwsem.h>
34#include <linux/spinlock.h>
35#include <linux/fs.h>
36#include <linux/cdev.h>
37#include <linux/device.h>
38#include <linux/string.h>
39#include <linux/vmalloc.h>
40#include <linux/mtd/mtd.h>
41#include <linux/mtd/ubi.h>
42#endif
43
44#include <linux/types.h>
45#include <linux/list.h>
46#include <linux/rbtree.h>
47#include <linux/string.h>
48#include <linux/mtd/mtd.h>
49#include <linux/mtd/ubi.h>
50
51#include "ubi-media.h"
52#include "scan.h"
53#include "debug.h"
54
55
56#define UBI_MAX_DEVICES 32
57
58
59#define UBI_NAME_STR "ubi"
60
61
62#define ubi_msg(fmt, ...) printk(KERN_NOTICE "UBI: " fmt "\n", ##__VA_ARGS__)
63
64#define ubi_warn(fmt, ...) printk(KERN_WARNING "UBI warning: %s: " fmt "\n", \
65 __func__, ##__VA_ARGS__)
66
67#define ubi_err(fmt, ...) printk(KERN_ERR "UBI error: %s: " fmt "\n", \
68 __func__, ##__VA_ARGS__)
69
70
71#define MIN_RESEVED_PEBS 2
72
73
74#define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
75
76
77#define UBI_LEB_UNMAPPED -1
78
79
80
81
82
83#define UBI_IO_RETRIES 3
84
85
86
87
88
89
90
91
92
93
94
95
96
97enum {
98 UBI_IO_PEB_EMPTY = 1,
99 UBI_IO_PEB_FREE,
100 UBI_IO_BAD_EC_HDR,
101 UBI_IO_BAD_VID_HDR,
102 UBI_IO_BITFLIPS
103};
104
105
106
107
108
109
110
111
112
113
114
115struct ubi_wl_entry {
116 struct rb_node rb;
117 int ec;
118 int pnum;
119};
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135struct ubi_ltree_entry {
136 struct rb_node rb;
137 int vol_id;
138 int lnum;
139 int users;
140 struct rw_semaphore mutex;
141};
142
143struct ubi_volume_desc;
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199struct ubi_volume {
200 struct device dev;
201 struct cdev cdev;
202 struct ubi_device *ubi;
203 int vol_id;
204 int ref_count;
205 int readers;
206 int writers;
207 int exclusive;
208
209 int reserved_pebs;
210 int vol_type;
211 int usable_leb_size;
212 int used_ebs;
213 int last_eb_bytes;
214 long long used_bytes;
215 int alignment;
216 int data_pad;
217 int name_len;
218 char name[UBI_VOL_NAME_MAX+1];
219
220 int upd_ebs;
221 int ch_lnum;
222 int ch_dtype;
223 long long upd_bytes;
224 long long upd_received;
225 void *upd_buf;
226
227 int *eba_tbl;
228 unsigned int checked:1;
229 unsigned int corrupted:1;
230 unsigned int upd_marker:1;
231 unsigned int updating:1;
232 unsigned int changing_leb:1;
233
234#ifdef CONFIG_MTD_UBI_GLUEBI
235
236
237
238
239
240 struct ubi_volume_desc *gluebi_desc;
241 int gluebi_refcount;
242 struct mtd_info gluebi_mtd;
243#endif
244};
245
246
247
248
249
250
251
252struct ubi_volume_desc {
253 struct ubi_volume *vol;
254 int mode;
255};
256
257struct ubi_wl_entry;
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346struct ubi_device {
347 struct cdev cdev;
348 struct device dev;
349 int ubi_num;
350 char ubi_name[sizeof(UBI_NAME_STR)+5];
351 int vol_count;
352 struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
353 spinlock_t volumes_lock;
354 int ref_count;
355
356 int rsvd_pebs;
357 int avail_pebs;
358 int beb_rsvd_pebs;
359 int beb_rsvd_level;
360
361 int autoresize_vol_id;
362 int vtbl_slots;
363 int vtbl_size;
364 struct ubi_vtbl_record *vtbl;
365 struct mutex volumes_mutex;
366
367 int max_ec;
368
369 int mean_ec;
370
371
372 unsigned long long global_sqnum;
373 spinlock_t ltree_lock;
374 struct rb_root ltree;
375 struct mutex alc_mutex;
376
377
378 struct rb_root used;
379 struct rb_root free;
380 struct rb_root scrub;
381 struct {
382 struct rb_root pnum;
383 struct rb_root aec;
384 } prot;
385 spinlock_t wl_lock;
386 struct mutex move_mutex;
387 struct rw_semaphore work_sem;
388 int wl_scheduled;
389 struct ubi_wl_entry **lookuptbl;
390 unsigned long long abs_ec;
391 struct ubi_wl_entry *move_from;
392 struct ubi_wl_entry *move_to;
393 int move_to_put;
394 struct list_head works;
395 int works_count;
396 struct task_struct *bgt_thread;
397 int thread_enabled;
398 char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
399
400
401 long long flash_size;
402 int peb_count;
403 int peb_size;
404 int bad_peb_count;
405 int good_peb_count;
406 int min_io_size;
407 int hdrs_min_io_size;
408 int ro_mode;
409 int leb_size;
410 int leb_start;
411 int ec_hdr_alsize;
412 int vid_hdr_alsize;
413 int vid_hdr_offset;
414 int vid_hdr_aloffset;
415 int vid_hdr_shift;
416 int bad_allowed;
417 struct mtd_info *mtd;
418
419 void *peb_buf1;
420 void *peb_buf2;
421 struct mutex buf_mutex;
422 struct mutex ckvol_mutex;
423#ifdef CONFIG_MTD_UBI_DEBUG
424 void *dbg_peb_buf;
425 struct mutex dbg_buf_mutex;
426#endif
427};
428
429extern struct kmem_cache *ubi_wl_entry_slab;
430extern struct file_operations ubi_ctrl_cdev_operations;
431extern struct file_operations ubi_cdev_operations;
432extern struct file_operations ubi_vol_cdev_operations;
433extern struct class *ubi_class;
434extern struct mutex ubi_devices_mutex;
435
436
437int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
438 struct ubi_vtbl_record *vtbl_rec);
439int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si);
440
441
442int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
443int ubi_remove_volume(struct ubi_volume_desc *desc);
444int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
445int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
446void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
447
448
449int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
450 long long bytes);
451int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
452 const void __user *buf, int count);
453int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
454 const struct ubi_leb_change_req *req);
455int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
456 const void __user *buf, int count);
457
458
459int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf, int length);
460int ubi_check_volume(struct ubi_device *ubi, int vol_id);
461void ubi_calculate_reserved(struct ubi_device *ubi);
462
463
464#ifdef CONFIG_MTD_UBI_GLUEBI
465int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol);
466int ubi_destroy_gluebi(struct ubi_volume *vol);
467void ubi_gluebi_updated(struct ubi_volume *vol);
468#else
469#define ubi_create_gluebi(ubi, vol) 0
470
471static inline int ubi_destroy_gluebi(struct ubi_volume *vol)
472{
473 return 0;
474}
475
476#define ubi_gluebi_updated(vol)
477#endif
478
479
480int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
481 int lnum);
482int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
483 void *buf, int offset, int len, int check);
484int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
485 const void *buf, int offset, int len, int dtype);
486int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
487 int lnum, const void *buf, int len, int dtype,
488 int used_ebs);
489int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
490 int lnum, const void *buf, int len, int dtype);
491int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
492 struct ubi_vid_hdr *vid_hdr);
493int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
494void ubi_eba_close(const struct ubi_device *ubi);
495
496
497int ubi_wl_get_peb(struct ubi_device *ubi, int dtype);
498int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture);
499int ubi_wl_flush(struct ubi_device *ubi);
500int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
501int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
502void ubi_wl_close(struct ubi_device *ubi);
503int ubi_thread(void *u);
504
505
506int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
507 int len);
508int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
509 int len);
510int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
511int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
512int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
513int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
514 struct ubi_ec_hdr *ec_hdr, int verbose);
515int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
516 struct ubi_ec_hdr *ec_hdr);
517int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
518 struct ubi_vid_hdr *vid_hdr, int verbose);
519int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
520 struct ubi_vid_hdr *vid_hdr);
521
522
523int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset);
524int ubi_detach_mtd_dev(int ubi_num, int anyway);
525struct ubi_device *ubi_get_device(int ubi_num);
526void ubi_put_device(struct ubi_device *ubi);
527struct ubi_device *ubi_get_by_major(int major);
528int ubi_major2num(int major);
529
530
531
532
533
534
535
536
537#define ubi_rb_for_each_entry(rb, pos, root, member) \
538 for (rb = rb_first(root), \
539 pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \
540 rb; \
541 rb = rb_next(rb), pos = container_of(rb, typeof(*pos), member))
542
543
544
545
546
547
548
549
550
551
552static inline struct ubi_vid_hdr *
553ubi_zalloc_vid_hdr(const struct ubi_device *ubi, gfp_t gfp_flags)
554{
555 void *vid_hdr;
556
557 vid_hdr = kzalloc(ubi->vid_hdr_alsize, gfp_flags);
558 if (!vid_hdr)
559 return NULL;
560
561
562
563
564
565 return vid_hdr + ubi->vid_hdr_shift;
566}
567
568
569
570
571
572
573static inline void ubi_free_vid_hdr(const struct ubi_device *ubi,
574 struct ubi_vid_hdr *vid_hdr)
575{
576 void *p = vid_hdr;
577
578 if (!p)
579 return;
580
581 kfree(p - ubi->vid_hdr_shift);
582}
583
584
585
586
587
588
589static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
590 int pnum, int offset, int len)
591{
592 ubi_assert(offset >= 0);
593 return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
594}
595
596
597
598
599
600
601static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
602 int pnum, int offset, int len)
603{
604 ubi_assert(offset >= 0);
605 return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
606}
607
608
609
610
611
612static inline void ubi_ro_mode(struct ubi_device *ubi)
613{
614 if (!ubi->ro_mode) {
615 ubi->ro_mode = 1;
616 ubi_warn("switch to read-only mode");
617 }
618}
619
620
621
622
623
624
625static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
626{
627 if (vol_id >= UBI_INTERNAL_VOL_START)
628 return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
629 else
630 return vol_id;
631}
632
633
634
635
636
637
638static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
639{
640 if (idx >= ubi->vtbl_slots)
641 return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
642 else
643 return idx;
644}
645
646#endif
647