1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef __UBOOT__
15static void update_fastmap_work_fn(struct work_struct *wrk)
16#else
17void update_fastmap_work_fn(struct ubi_device *ubi)
18#endif
19{
20#ifndef __UBOOT__
21 struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
22#endif
23
24 ubi_update_fastmap(ubi);
25 spin_lock(&ubi->wl_lock);
26 ubi->fm_work_scheduled = 0;
27 spin_unlock(&ubi->wl_lock);
28}
29
30
31
32
33
34static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
35{
36 struct rb_node *p;
37 struct ubi_wl_entry *e, *victim = NULL;
38 int max_ec = UBI_MAX_ERASECOUNTER;
39
40 ubi_rb_for_each_entry(p, e, root, u.rb) {
41 if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
42 victim = e;
43 max_ec = e->ec;
44 }
45 }
46
47 return victim;
48}
49
50
51
52
53
54
55static void return_unused_pool_pebs(struct ubi_device *ubi,
56 struct ubi_fm_pool *pool)
57{
58 int i;
59 struct ubi_wl_entry *e;
60
61 for (i = pool->used; i < pool->size; i++) {
62 e = ubi->lookuptbl[pool->pebs[i]];
63 wl_tree_add(e, &ubi->free);
64 ubi->free_count++;
65 }
66}
67
68static int anchor_pebs_avalible(struct rb_root *root)
69{
70 struct rb_node *p;
71 struct ubi_wl_entry *e;
72
73 ubi_rb_for_each_entry(p, e, root, u.rb)
74 if (e->pnum < UBI_FM_MAX_START)
75 return 1;
76
77 return 0;
78}
79
80
81
82
83
84
85
86
87
88
89struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
90{
91 struct ubi_wl_entry *e = NULL;
92
93 if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
94 goto out;
95
96 if (anchor)
97 e = find_anchor_wl_entry(&ubi->free);
98 else
99 e = find_mean_wl_entry(ubi, &ubi->free);
100
101 if (!e)
102 goto out;
103
104 self_check_in_wl_tree(ubi, e, &ubi->free);
105
106
107
108 rb_erase(&e->u.rb, &ubi->free);
109 ubi->free_count--;
110out:
111 return e;
112}
113
114
115
116
117
118void ubi_refill_pools(struct ubi_device *ubi)
119{
120 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
121 struct ubi_fm_pool *pool = &ubi->fm_pool;
122 struct ubi_wl_entry *e;
123 int enough;
124
125 spin_lock(&ubi->wl_lock);
126
127 return_unused_pool_pebs(ubi, wl_pool);
128 return_unused_pool_pebs(ubi, pool);
129
130 wl_pool->size = 0;
131 pool->size = 0;
132
133 for (;;) {
134 enough = 0;
135 if (pool->size < pool->max_size) {
136 if (!ubi->free.rb_node)
137 break;
138
139 e = wl_get_wle(ubi);
140 if (!e)
141 break;
142
143 pool->pebs[pool->size] = e->pnum;
144 pool->size++;
145 } else
146 enough++;
147
148 if (wl_pool->size < wl_pool->max_size) {
149 if (!ubi->free.rb_node ||
150 (ubi->free_count - ubi->beb_rsvd_pebs < 5))
151 break;
152
153 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
154 self_check_in_wl_tree(ubi, e, &ubi->free);
155 rb_erase(&e->u.rb, &ubi->free);
156 ubi->free_count--;
157
158 wl_pool->pebs[wl_pool->size] = e->pnum;
159 wl_pool->size++;
160 } else
161 enough++;
162
163 if (enough == 2)
164 break;
165 }
166
167 wl_pool->used = 0;
168 pool->used = 0;
169
170 spin_unlock(&ubi->wl_lock);
171}
172
173
174
175
176
177
178
179
180
181int ubi_wl_get_peb(struct ubi_device *ubi)
182{
183 int ret, retried = 0;
184 struct ubi_fm_pool *pool = &ubi->fm_pool;
185 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
186
187again:
188 down_read(&ubi->fm_eba_sem);
189 spin_lock(&ubi->wl_lock);
190
191
192
193 if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
194 spin_unlock(&ubi->wl_lock);
195 up_read(&ubi->fm_eba_sem);
196 ret = ubi_update_fastmap(ubi);
197 if (ret) {
198 ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
199 down_read(&ubi->fm_eba_sem);
200 return -ENOSPC;
201 }
202 down_read(&ubi->fm_eba_sem);
203 spin_lock(&ubi->wl_lock);
204 }
205
206 if (pool->used == pool->size) {
207 spin_unlock(&ubi->wl_lock);
208 if (retried) {
209 ubi_err(ubi, "Unable to get a free PEB from user WL pool");
210 ret = -ENOSPC;
211 goto out;
212 }
213 retried = 1;
214 up_read(&ubi->fm_eba_sem);
215 goto again;
216 }
217
218 ubi_assert(pool->used < pool->size);
219 ret = pool->pebs[pool->used++];
220 prot_queue_add(ubi, ubi->lookuptbl[ret]);
221 spin_unlock(&ubi->wl_lock);
222out:
223 return ret;
224}
225
226
227
228
229
230static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
231{
232 struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
233 int pnum;
234
235 if (pool->used == pool->size) {
236#ifndef __UBOOT__
237
238
239
240 if (!ubi->fm_work_scheduled) {
241 ubi->fm_work_scheduled = 1;
242 schedule_work(&ubi->fm_work);
243 }
244 return NULL;
245#else
246
247
248
249 update_fastmap_work_fn(ubi);
250#endif
251 }
252
253 pnum = pool->pebs[pool->used++];
254 return ubi->lookuptbl[pnum];
255}
256
257
258
259
260
261int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
262{
263 struct ubi_work *wrk;
264
265 spin_lock(&ubi->wl_lock);
266 if (ubi->wl_scheduled) {
267 spin_unlock(&ubi->wl_lock);
268 return 0;
269 }
270 ubi->wl_scheduled = 1;
271 spin_unlock(&ubi->wl_lock);
272
273 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
274 if (!wrk) {
275 spin_lock(&ubi->wl_lock);
276 ubi->wl_scheduled = 0;
277 spin_unlock(&ubi->wl_lock);
278 return -ENOMEM;
279 }
280
281 wrk->anchor = 1;
282 wrk->func = &wear_leveling_worker;
283 schedule_ubi_work(ubi, wrk);
284 return 0;
285}
286
287
288
289
290
291
292
293
294
295
296
297int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
298 int lnum, int torture)
299{
300 struct ubi_wl_entry *e;
301 int vol_id, pnum = fm_e->pnum;
302
303 dbg_wl("PEB %d", pnum);
304
305 ubi_assert(pnum >= 0);
306 ubi_assert(pnum < ubi->peb_count);
307
308 spin_lock(&ubi->wl_lock);
309 e = ubi->lookuptbl[pnum];
310
311
312
313
314
315 if (!e) {
316 e = fm_e;
317 ubi_assert(e->ec >= 0);
318 ubi->lookuptbl[pnum] = e;
319 }
320
321 spin_unlock(&ubi->wl_lock);
322
323 vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
324 return schedule_erase(ubi, e, vol_id, lnum, torture);
325}
326
327
328
329
330
331int ubi_is_erase_work(struct ubi_work *wrk)
332{
333 return wrk->func == erase_worker;
334}
335
336static void ubi_fastmap_close(struct ubi_device *ubi)
337{
338 int i;
339
340#ifndef __UBOOT__
341 flush_work(&ubi->fm_work);
342#else
343 update_fastmap_work_fn(ubi);
344#endif
345 return_unused_pool_pebs(ubi, &ubi->fm_pool);
346 return_unused_pool_pebs(ubi, &ubi->fm_wl_pool);
347
348 if (ubi->fm) {
349 for (i = 0; i < ubi->fm->used_blocks; i++)
350 kfree(ubi->fm->e[i]);
351 }
352 kfree(ubi->fm);
353}
354
355
356
357
358
359
360
361
362
363static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
364 struct ubi_wl_entry *e,
365 struct rb_root *root) {
366 if (e && !ubi->fm_disabled && !ubi->fm &&
367 e->pnum < UBI_FM_MAX_START)
368 e = rb_entry(rb_next(root->rb_node),
369 struct ubi_wl_entry, u.rb);
370
371 return e;
372}
373