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#ifdef CONFIG_UBI_SILENCE_MSG
63#define ubi_msg(fmt, ...)
64#else
65#define ubi_msg(fmt, ...) printk(KERN_NOTICE "UBI: " fmt "\n", ##__VA_ARGS__)
66#endif
67
68#define ubi_warn(fmt, ...) printk(KERN_WARNING "UBI warning: %s: " fmt "\n", \
69 __func__, ##__VA_ARGS__)
70
71#define ubi_err(fmt, ...) printk(KERN_ERR "UBI error: %s: " fmt "\n", \
72 __func__, ##__VA_ARGS__)
73
74
75#define MIN_RESEVED_PEBS 2
76
77
78#define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
79
80
81#define UBI_LEB_UNMAPPED -1
82
83
84
85
86
87#define UBI_IO_RETRIES 3
88
89
90
91
92
93
94
95
96
97
98
99
100
101enum {
102 UBI_IO_PEB_EMPTY = 1,
103 UBI_IO_PEB_FREE,
104 UBI_IO_BAD_EC_HDR,
105 UBI_IO_BAD_VID_HDR,
106 UBI_IO_BITFLIPS
107};
108
109
110
111
112
113
114
115
116
117
118
119struct ubi_wl_entry {
120 struct rb_node rb;
121 int ec;
122 int pnum;
123};
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139struct ubi_ltree_entry {
140 struct rb_node rb;
141 int vol_id;
142 int lnum;
143 int users;
144 struct rw_semaphore mutex;
145};
146
147struct ubi_volume_desc;
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
199
200
201
202
203struct ubi_volume {
204 struct device dev;
205 struct cdev cdev;
206 struct ubi_device *ubi;
207 int vol_id;
208 int ref_count;
209 int readers;
210 int writers;
211 int exclusive;
212
213 int reserved_pebs;
214 int vol_type;
215 int usable_leb_size;
216 int used_ebs;
217 int last_eb_bytes;
218 long long used_bytes;
219 int alignment;
220 int data_pad;
221 int name_len;
222 char name[UBI_VOL_NAME_MAX+1];
223
224 int upd_ebs;
225 int ch_lnum;
226 int ch_dtype;
227 long long upd_bytes;
228 long long upd_received;
229 void *upd_buf;
230
231 int *eba_tbl;
232 unsigned int checked:1;
233 unsigned int corrupted:1;
234 unsigned int upd_marker:1;
235 unsigned int updating:1;
236 unsigned int changing_leb:1;
237
238#ifdef CONFIG_MTD_UBI_GLUEBI
239
240
241
242
243
244 struct ubi_volume_desc *gluebi_desc;
245 int gluebi_refcount;
246 struct mtd_info gluebi_mtd;
247#endif
248};
249
250
251
252
253
254
255
256struct ubi_volume_desc {
257 struct ubi_volume *vol;
258 int mode;
259};
260
261struct ubi_wl_entry;
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
346
347
348
349
350struct ubi_device {
351 struct cdev cdev;
352 struct device dev;
353 int ubi_num;
354 char ubi_name[sizeof(UBI_NAME_STR)+5];
355 int vol_count;
356 struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
357 spinlock_t volumes_lock;
358 int ref_count;
359
360 int rsvd_pebs;
361 int avail_pebs;
362 int beb_rsvd_pebs;
363 int beb_rsvd_level;
364
365 int autoresize_vol_id;
366 int vtbl_slots;
367 int vtbl_size;
368 struct ubi_vtbl_record *vtbl;
369 struct mutex volumes_mutex;
370
371 int max_ec;
372
373 int mean_ec;
374
375
376 unsigned long long global_sqnum;
377 spinlock_t ltree_lock;
378 struct rb_root ltree;
379 struct mutex alc_mutex;
380
381
382 struct rb_root used;
383 struct rb_root free;
384 struct rb_root scrub;
385 struct {
386 struct rb_root pnum;
387 struct rb_root aec;
388 } prot;
389 spinlock_t wl_lock;
390 struct mutex move_mutex;
391 struct rw_semaphore work_sem;
392 int wl_scheduled;
393 struct ubi_wl_entry **lookuptbl;
394 unsigned long long abs_ec;
395 struct ubi_wl_entry *move_from;
396 struct ubi_wl_entry *move_to;
397 int move_to_put;
398 struct list_head works;
399 int works_count;
400 struct task_struct *bgt_thread;
401 int thread_enabled;
402 char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
403
404
405 long long flash_size;
406 int peb_count;
407 int peb_size;
408 int bad_peb_count;
409 int good_peb_count;
410 int min_io_size;
411 int hdrs_min_io_size;
412 int ro_mode;
413 int leb_size;
414 int leb_start;
415 int ec_hdr_alsize;
416 int vid_hdr_alsize;
417 int vid_hdr_offset;
418 int vid_hdr_aloffset;
419 int vid_hdr_shift;
420 int bad_allowed;
421 struct mtd_info *mtd;
422
423 void *peb_buf1;
424 void *peb_buf2;
425 struct mutex buf_mutex;
426 struct mutex ckvol_mutex;
427#ifdef CONFIG_MTD_UBI_DEBUG
428 void *dbg_peb_buf;
429 struct mutex dbg_buf_mutex;
430#endif
431};
432
433extern struct kmem_cache *ubi_wl_entry_slab;
434extern struct file_operations ubi_ctrl_cdev_operations;
435extern struct file_operations ubi_cdev_operations;
436extern struct file_operations ubi_vol_cdev_operations;
437extern struct class *ubi_class;
438extern struct mutex ubi_devices_mutex;
439
440
441int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
442 struct ubi_vtbl_record *vtbl_rec);
443int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si);
444
445
446int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
447int ubi_remove_volume(struct ubi_volume_desc *desc);
448int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
449int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
450void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
451
452
453int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
454 long long bytes);
455int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
456 const void __user *buf, int count);
457int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
458 const struct ubi_leb_change_req *req);
459int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
460 const void __user *buf, int count);
461
462
463int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf, int length);
464int ubi_check_volume(struct ubi_device *ubi, int vol_id);
465void ubi_calculate_reserved(struct ubi_device *ubi);
466
467
468#ifdef CONFIG_MTD_UBI_GLUEBI
469int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol);
470int ubi_destroy_gluebi(struct ubi_volume *vol);
471void ubi_gluebi_updated(struct ubi_volume *vol);
472#else
473#define ubi_create_gluebi(ubi, vol) 0
474
475static inline int ubi_destroy_gluebi(struct ubi_volume *vol)
476{
477 return 0;
478}
479
480#define ubi_gluebi_updated(vol)
481#endif
482
483
484int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
485 int lnum);
486int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
487 void *buf, int offset, int len, int check);
488int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
489 const void *buf, int offset, int len, int dtype);
490int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
491 int lnum, const void *buf, int len, int dtype,
492 int used_ebs);
493int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
494 int lnum, const void *buf, int len, int dtype);
495int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
496 struct ubi_vid_hdr *vid_hdr);
497int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
498void ubi_eba_close(const struct ubi_device *ubi);
499
500
501int ubi_wl_get_peb(struct ubi_device *ubi, int dtype);
502int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture);
503int ubi_wl_flush(struct ubi_device *ubi);
504int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
505int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
506void ubi_wl_close(struct ubi_device *ubi);
507int ubi_thread(void *u);
508
509
510int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
511 int len);
512int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
513 int len);
514int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
515int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
516int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
517int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
518 struct ubi_ec_hdr *ec_hdr, int verbose);
519int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
520 struct ubi_ec_hdr *ec_hdr);
521int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
522 struct ubi_vid_hdr *vid_hdr, int verbose);
523int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
524 struct ubi_vid_hdr *vid_hdr);
525
526
527int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset);
528int ubi_detach_mtd_dev(int ubi_num, int anyway);
529struct ubi_device *ubi_get_device(int ubi_num);
530void ubi_put_device(struct ubi_device *ubi);
531struct ubi_device *ubi_get_by_major(int major);
532int ubi_major2num(int major);
533
534
535
536
537
538
539
540
541#define ubi_rb_for_each_entry(rb, pos, root, member) \
542 for (rb = rb_first(root), \
543 pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \
544 rb; \
545 rb = rb_next(rb), pos = container_of(rb, typeof(*pos), member))
546
547
548
549
550
551
552
553
554
555
556static inline struct ubi_vid_hdr *
557ubi_zalloc_vid_hdr(const struct ubi_device *ubi, gfp_t gfp_flags)
558{
559 void *vid_hdr;
560
561 vid_hdr = kzalloc(ubi->vid_hdr_alsize, gfp_flags);
562 if (!vid_hdr)
563 return NULL;
564
565
566
567
568
569 return vid_hdr + ubi->vid_hdr_shift;
570}
571
572
573
574
575
576
577static inline void ubi_free_vid_hdr(const struct ubi_device *ubi,
578 struct ubi_vid_hdr *vid_hdr)
579{
580 void *p = vid_hdr;
581
582 if (!p)
583 return;
584
585 kfree(p - ubi->vid_hdr_shift);
586}
587
588
589
590
591
592
593static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
594 int pnum, int offset, int len)
595{
596 ubi_assert(offset >= 0);
597 return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
598}
599
600
601
602
603
604
605static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
606 int pnum, int offset, int len)
607{
608 ubi_assert(offset >= 0);
609 return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
610}
611
612
613
614
615
616static inline void ubi_ro_mode(struct ubi_device *ubi)
617{
618 if (!ubi->ro_mode) {
619 ubi->ro_mode = 1;
620 ubi_warn("switch to read-only mode");
621 }
622}
623
624
625
626
627
628
629static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
630{
631 if (vol_id >= UBI_INTERNAL_VOL_START)
632 return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
633 else
634 return vol_id;
635}
636
637
638
639
640
641
642static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
643{
644 if (idx >= ubi->vtbl_slots)
645 return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
646 else
647 return idx;
648}
649
650#endif
651