Commit | Line | Data |
---|---|---|
78d6d497 RW |
1 | /* |
2 | * Copyright (c) 2012 Linutronix GmbH | |
3 | * Copyright (c) 2014 sigma star gmbh | |
4 | * Author: Richard Weinberger <richard@nod.at> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; version 2. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | |
13 | * the GNU General Public License for more details. | |
14 | * | |
15 | */ | |
16 | ||
17 | /** | |
18 | * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue | |
19 | * @wrk: the work description object | |
20 | */ | |
21 | static void update_fastmap_work_fn(struct work_struct *wrk) | |
22 | { | |
23 | struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work); | |
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 | * is_fm_block - returns 1 if a PEB is currently used in a fastmap. | |
32 | * @ubi: UBI device description object | |
33 | * @pnum: the to be checked PEB | |
34 | */ | |
35 | static int is_fm_block(struct ubi_device *ubi, int pnum) | |
36 | { | |
37 | int i; | |
38 | ||
39 | if (!ubi->fm) | |
40 | return 0; | |
41 | ||
42 | for (i = 0; i < ubi->fm->used_blocks; i++) | |
43 | if (ubi->fm->e[i]->pnum == pnum) | |
44 | return 1; | |
45 | ||
46 | return 0; | |
47 | } | |
48 | ||
49 | /** | |
50 | * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB. | |
51 | * @root: the RB-tree where to look for | |
52 | */ | |
53 | static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root) | |
54 | { | |
55 | struct rb_node *p; | |
56 | struct ubi_wl_entry *e, *victim = NULL; | |
57 | int max_ec = UBI_MAX_ERASECOUNTER; | |
58 | ||
59 | ubi_rb_for_each_entry(p, e, root, u.rb) { | |
60 | if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) { | |
61 | victim = e; | |
62 | max_ec = e->ec; | |
63 | } | |
64 | } | |
65 | ||
66 | return victim; | |
67 | } | |
68 | ||
69 | /** | |
70 | * return_unused_pool_pebs - returns unused PEB to the free tree. | |
71 | * @ubi: UBI device description object | |
72 | * @pool: fastmap pool description object | |
73 | */ | |
74 | static void return_unused_pool_pebs(struct ubi_device *ubi, | |
75 | struct ubi_fm_pool *pool) | |
76 | { | |
77 | int i; | |
78 | struct ubi_wl_entry *e; | |
79 | ||
80 | for (i = pool->used; i < pool->size; i++) { | |
81 | e = ubi->lookuptbl[pool->pebs[i]]; | |
82 | wl_tree_add(e, &ubi->free); | |
83 | ubi->free_count++; | |
84 | } | |
85 | } | |
86 | ||
87 | static int anchor_pebs_avalible(struct rb_root *root) | |
88 | { | |
89 | struct rb_node *p; | |
90 | struct ubi_wl_entry *e; | |
91 | ||
92 | ubi_rb_for_each_entry(p, e, root, u.rb) | |
93 | if (e->pnum < UBI_FM_MAX_START) | |
94 | return 1; | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | /** | |
100 | * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number. | |
101 | * @ubi: UBI device description object | |
102 | * @anchor: This PEB will be used as anchor PEB by fastmap | |
103 | * | |
104 | * The function returns a physical erase block with a given maximal number | |
105 | * and removes it from the wl subsystem. | |
106 | * Must be called with wl_lock held! | |
107 | */ | |
108 | struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor) | |
109 | { | |
110 | struct ubi_wl_entry *e = NULL; | |
111 | ||
112 | if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1)) | |
113 | goto out; | |
114 | ||
115 | if (anchor) | |
116 | e = find_anchor_wl_entry(&ubi->free); | |
117 | else | |
118 | e = find_mean_wl_entry(ubi, &ubi->free); | |
119 | ||
120 | if (!e) | |
121 | goto out; | |
122 | ||
123 | self_check_in_wl_tree(ubi, e, &ubi->free); | |
124 | ||
125 | /* remove it from the free list, | |
126 | * the wl subsystem does no longer know this erase block */ | |
127 | rb_erase(&e->u.rb, &ubi->free); | |
128 | ubi->free_count--; | |
129 | out: | |
130 | return e; | |
131 | } | |
132 | ||
133 | /** | |
134 | * ubi_refill_pools - refills all fastmap PEB pools. | |
135 | * @ubi: UBI device description object | |
136 | */ | |
137 | void ubi_refill_pools(struct ubi_device *ubi) | |
138 | { | |
139 | struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; | |
140 | struct ubi_fm_pool *pool = &ubi->fm_pool; | |
141 | struct ubi_wl_entry *e; | |
142 | int enough; | |
143 | ||
144 | spin_lock(&ubi->wl_lock); | |
145 | ||
146 | return_unused_pool_pebs(ubi, wl_pool); | |
147 | return_unused_pool_pebs(ubi, pool); | |
148 | ||
149 | wl_pool->size = 0; | |
150 | pool->size = 0; | |
151 | ||
152 | for (;;) { | |
153 | enough = 0; | |
154 | if (pool->size < pool->max_size) { | |
155 | if (!ubi->free.rb_node) | |
156 | break; | |
157 | ||
158 | e = wl_get_wle(ubi); | |
159 | if (!e) | |
160 | break; | |
161 | ||
162 | pool->pebs[pool->size] = e->pnum; | |
163 | pool->size++; | |
164 | } else | |
165 | enough++; | |
166 | ||
167 | if (wl_pool->size < wl_pool->max_size) { | |
168 | if (!ubi->free.rb_node || | |
169 | (ubi->free_count - ubi->beb_rsvd_pebs < 5)) | |
170 | break; | |
171 | ||
172 | e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF); | |
173 | self_check_in_wl_tree(ubi, e, &ubi->free); | |
174 | rb_erase(&e->u.rb, &ubi->free); | |
175 | ubi->free_count--; | |
176 | ||
177 | wl_pool->pebs[wl_pool->size] = e->pnum; | |
178 | wl_pool->size++; | |
179 | } else | |
180 | enough++; | |
181 | ||
182 | if (enough == 2) | |
183 | break; | |
184 | } | |
185 | ||
186 | wl_pool->used = 0; | |
187 | pool->used = 0; | |
188 | ||
189 | spin_unlock(&ubi->wl_lock); | |
190 | } | |
191 | ||
192 | /** | |
193 | * ubi_wl_get_peb - get a physical eraseblock. | |
194 | * @ubi: UBI device description object | |
195 | * | |
196 | * This function returns a physical eraseblock in case of success and a | |
197 | * negative error code in case of failure. | |
198 | * Returns with ubi->fm_eba_sem held in read mode! | |
199 | */ | |
200 | int ubi_wl_get_peb(struct ubi_device *ubi) | |
201 | { | |
202 | int ret, retried = 0; | |
203 | struct ubi_fm_pool *pool = &ubi->fm_pool; | |
204 | struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; | |
205 | ||
206 | again: | |
207 | down_read(&ubi->fm_eba_sem); | |
208 | spin_lock(&ubi->wl_lock); | |
209 | ||
210 | /* We check here also for the WL pool because at this point we can | |
211 | * refill the WL pool synchronous. */ | |
212 | if (pool->used == pool->size || wl_pool->used == wl_pool->size) { | |
213 | spin_unlock(&ubi->wl_lock); | |
214 | up_read(&ubi->fm_eba_sem); | |
215 | ret = ubi_update_fastmap(ubi); | |
216 | if (ret) { | |
217 | ubi_msg(ubi, "Unable to write a new fastmap: %i", ret); | |
218 | down_read(&ubi->fm_eba_sem); | |
219 | return -ENOSPC; | |
220 | } | |
221 | down_read(&ubi->fm_eba_sem); | |
222 | spin_lock(&ubi->wl_lock); | |
223 | } | |
224 | ||
225 | if (pool->used == pool->size) { | |
226 | spin_unlock(&ubi->wl_lock); | |
227 | if (retried) { | |
228 | ubi_err(ubi, "Unable to get a free PEB from user WL pool"); | |
229 | ret = -ENOSPC; | |
230 | goto out; | |
231 | } | |
232 | retried = 1; | |
233 | up_read(&ubi->fm_eba_sem); | |
234 | goto again; | |
235 | } | |
236 | ||
237 | ubi_assert(pool->used < pool->size); | |
238 | ret = pool->pebs[pool->used++]; | |
239 | prot_queue_add(ubi, ubi->lookuptbl[ret]); | |
240 | spin_unlock(&ubi->wl_lock); | |
241 | out: | |
242 | return ret; | |
243 | } | |
244 | ||
245 | /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system. | |
246 | * | |
247 | * @ubi: UBI device description object | |
248 | */ | |
249 | static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi) | |
250 | { | |
251 | struct ubi_fm_pool *pool = &ubi->fm_wl_pool; | |
252 | int pnum; | |
253 | ||
254 | if (pool->used == pool->size) { | |
255 | /* We cannot update the fastmap here because this | |
256 | * function is called in atomic context. | |
257 | * Let's fail here and refill/update it as soon as possible. */ | |
258 | if (!ubi->fm_work_scheduled) { | |
259 | ubi->fm_work_scheduled = 1; | |
260 | schedule_work(&ubi->fm_work); | |
261 | } | |
262 | return NULL; | |
78d6d497 | 263 | } |
e1bc37ce RW |
264 | |
265 | pnum = pool->pebs[pool->used++]; | |
266 | return ubi->lookuptbl[pnum]; | |
78d6d497 RW |
267 | } |
268 | ||
269 | /** | |
270 | * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB. | |
271 | * @ubi: UBI device description object | |
272 | */ | |
273 | int ubi_ensure_anchor_pebs(struct ubi_device *ubi) | |
274 | { | |
275 | struct ubi_work *wrk; | |
276 | ||
277 | spin_lock(&ubi->wl_lock); | |
278 | if (ubi->wl_scheduled) { | |
279 | spin_unlock(&ubi->wl_lock); | |
280 | return 0; | |
281 | } | |
282 | ubi->wl_scheduled = 1; | |
283 | spin_unlock(&ubi->wl_lock); | |
284 | ||
285 | wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS); | |
286 | if (!wrk) { | |
287 | spin_lock(&ubi->wl_lock); | |
288 | ubi->wl_scheduled = 0; | |
289 | spin_unlock(&ubi->wl_lock); | |
290 | return -ENOMEM; | |
291 | } | |
292 | ||
293 | wrk->anchor = 1; | |
294 | wrk->func = &wear_leveling_worker; | |
295 | schedule_ubi_work(ubi, wrk); | |
296 | return 0; | |
297 | } | |
298 | ||
299 | /** | |
300 | * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling | |
301 | * sub-system. | |
302 | * see: ubi_wl_put_peb() | |
303 | * | |
304 | * @ubi: UBI device description object | |
305 | * @fm_e: physical eraseblock to return | |
306 | * @lnum: the last used logical eraseblock number for the PEB | |
307 | * @torture: if this physical eraseblock has to be tortured | |
308 | */ | |
309 | int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e, | |
310 | int lnum, int torture) | |
311 | { | |
312 | struct ubi_wl_entry *e; | |
313 | int vol_id, pnum = fm_e->pnum; | |
314 | ||
315 | dbg_wl("PEB %d", pnum); | |
316 | ||
317 | ubi_assert(pnum >= 0); | |
318 | ubi_assert(pnum < ubi->peb_count); | |
319 | ||
320 | spin_lock(&ubi->wl_lock); | |
321 | e = ubi->lookuptbl[pnum]; | |
322 | ||
323 | /* This can happen if we recovered from a fastmap the very | |
324 | * first time and writing now a new one. In this case the wl system | |
325 | * has never seen any PEB used by the original fastmap. | |
326 | */ | |
327 | if (!e) { | |
328 | e = fm_e; | |
329 | ubi_assert(e->ec >= 0); | |
330 | ubi->lookuptbl[pnum] = e; | |
331 | } | |
332 | ||
333 | spin_unlock(&ubi->wl_lock); | |
334 | ||
335 | vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID; | |
336 | return schedule_erase(ubi, e, vol_id, lnum, torture); | |
337 | } | |
338 | ||
339 | /** | |
340 | * ubi_is_erase_work - checks whether a work is erase work. | |
341 | * @wrk: The work object to be checked | |
342 | */ | |
343 | int ubi_is_erase_work(struct ubi_work *wrk) | |
344 | { | |
345 | return wrk->func == erase_worker; | |
346 | } | |
347 | ||
348 | static void ubi_fastmap_close(struct ubi_device *ubi) | |
349 | { | |
350 | int i; | |
351 | ||
352 | flush_work(&ubi->fm_work); | |
353 | return_unused_pool_pebs(ubi, &ubi->fm_pool); | |
354 | return_unused_pool_pebs(ubi, &ubi->fm_wl_pool); | |
355 | ||
356 | if (ubi->fm) { | |
357 | for (i = 0; i < ubi->fm->used_blocks; i++) | |
358 | kfree(ubi->fm->e[i]); | |
359 | } | |
360 | kfree(ubi->fm); | |
361 | } | |
2f84c246 RW |
362 | |
363 | /** | |
364 | * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap. | |
365 | * See find_mean_wl_entry() | |
366 | * | |
367 | * @ubi: UBI device description object | |
368 | * @e: physical eraseblock to return | |
369 | * @root: RB tree to test against. | |
370 | */ | |
371 | static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi, | |
372 | struct ubi_wl_entry *e, | |
373 | struct rb_root *root) { | |
374 | if (e && !ubi->fm_disabled && !ubi->fm && | |
375 | e->pnum < UBI_FM_MAX_START) | |
376 | e = rb_entry(rb_next(root->rb_node), | |
377 | struct ubi_wl_entry, u.rb); | |
378 | ||
379 | return e; | |
380 | } |