2 * Copyright (c) 2012 Linutronix GmbH
3 * Copyright (c) 2014 sigma star gmbh
4 * Author: Richard Weinberger <richard@nod.at>
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.
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.
18 * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
19 * @wrk: the work description object
21 static void update_fastmap_work_fn(struct work_struct
*wrk
)
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
);
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
35 static int is_fm_block(struct ubi_device
*ubi
, int pnum
)
42 for (i
= 0; i
< ubi
->fm
->used_blocks
; i
++)
43 if (ubi
->fm
->e
[i
]->pnum
== pnum
)
50 * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
51 * @root: the RB-tree where to look for
53 static struct ubi_wl_entry
*find_anchor_wl_entry(struct rb_root
*root
)
56 struct ubi_wl_entry
*e
, *victim
= NULL
;
57 int max_ec
= UBI_MAX_ERASECOUNTER
;
59 ubi_rb_for_each_entry(p
, e
, root
, u
.rb
) {
60 if (e
->pnum
< UBI_FM_MAX_START
&& e
->ec
< max_ec
) {
70 * return_unused_pool_pebs - returns unused PEB to the free tree.
71 * @ubi: UBI device description object
72 * @pool: fastmap pool description object
74 static void return_unused_pool_pebs(struct ubi_device
*ubi
,
75 struct ubi_fm_pool
*pool
)
78 struct ubi_wl_entry
*e
;
80 for (i
= pool
->used
; i
< pool
->size
; i
++) {
81 e
= ubi
->lookuptbl
[pool
->pebs
[i
]];
82 wl_tree_add(e
, &ubi
->free
);
87 static int anchor_pebs_avalible(struct rb_root
*root
)
90 struct ubi_wl_entry
*e
;
92 ubi_rb_for_each_entry(p
, e
, root
, u
.rb
)
93 if (e
->pnum
< UBI_FM_MAX_START
)
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
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!
108 struct ubi_wl_entry
*ubi_wl_get_fm_peb(struct ubi_device
*ubi
, int anchor
)
110 struct ubi_wl_entry
*e
= NULL
;
112 if (!ubi
->free
.rb_node
|| (ubi
->free_count
- ubi
->beb_rsvd_pebs
< 1))
116 e
= find_anchor_wl_entry(&ubi
->free
);
118 e
= find_mean_wl_entry(ubi
, &ubi
->free
);
123 self_check_in_wl_tree(ubi
, e
, &ubi
->free
);
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
);
134 * ubi_refill_pools - refills all fastmap PEB pools.
135 * @ubi: UBI device description object
137 void ubi_refill_pools(struct ubi_device
*ubi
)
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
;
144 spin_lock(&ubi
->wl_lock
);
146 return_unused_pool_pebs(ubi
, wl_pool
);
147 return_unused_pool_pebs(ubi
, pool
);
154 if (pool
->size
< pool
->max_size
) {
155 if (!ubi
->free
.rb_node
)
162 pool
->pebs
[pool
->size
] = e
->pnum
;
167 if (wl_pool
->size
< wl_pool
->max_size
) {
168 if (!ubi
->free
.rb_node
||
169 (ubi
->free_count
- ubi
->beb_rsvd_pebs
< 5))
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
);
177 wl_pool
->pebs
[wl_pool
->size
] = e
->pnum
;
189 spin_unlock(&ubi
->wl_lock
);
193 * ubi_wl_get_peb - get a physical eraseblock.
194 * @ubi: UBI device description object
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!
200 int ubi_wl_get_peb(struct ubi_device
*ubi
)
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
;
207 down_read(&ubi
->fm_eba_sem
);
208 spin_lock(&ubi
->wl_lock
);
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
);
217 ubi_msg(ubi
, "Unable to write a new fastmap: %i", ret
);
218 down_read(&ubi
->fm_eba_sem
);
221 down_read(&ubi
->fm_eba_sem
);
222 spin_lock(&ubi
->wl_lock
);
225 if (pool
->used
== pool
->size
) {
226 spin_unlock(&ubi
->wl_lock
);
228 ubi_err(ubi
, "Unable to get a free PEB from user WL pool");
233 up_read(&ubi
->fm_eba_sem
);
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
);
245 /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
247 * @ubi: UBI device description object
249 static struct ubi_wl_entry
*get_peb_for_wl(struct ubi_device
*ubi
)
251 struct ubi_fm_pool
*pool
= &ubi
->fm_wl_pool
;
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
);
265 pnum
= pool
->pebs
[pool
->used
++];
266 return ubi
->lookuptbl
[pnum
];
270 * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
271 * @ubi: UBI device description object
273 int ubi_ensure_anchor_pebs(struct ubi_device
*ubi
)
275 struct ubi_work
*wrk
;
277 spin_lock(&ubi
->wl_lock
);
278 if (ubi
->wl_scheduled
) {
279 spin_unlock(&ubi
->wl_lock
);
282 ubi
->wl_scheduled
= 1;
283 spin_unlock(&ubi
->wl_lock
);
285 wrk
= kmalloc(sizeof(struct ubi_work
), GFP_NOFS
);
287 spin_lock(&ubi
->wl_lock
);
288 ubi
->wl_scheduled
= 0;
289 spin_unlock(&ubi
->wl_lock
);
294 wrk
->func
= &wear_leveling_worker
;
295 schedule_ubi_work(ubi
, wrk
);
300 * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
302 * see: ubi_wl_put_peb()
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
309 int ubi_wl_put_fm_peb(struct ubi_device
*ubi
, struct ubi_wl_entry
*fm_e
,
310 int lnum
, int torture
)
312 struct ubi_wl_entry
*e
;
313 int vol_id
, pnum
= fm_e
->pnum
;
315 dbg_wl("PEB %d", pnum
);
317 ubi_assert(pnum
>= 0);
318 ubi_assert(pnum
< ubi
->peb_count
);
320 spin_lock(&ubi
->wl_lock
);
321 e
= ubi
->lookuptbl
[pnum
];
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.
329 ubi_assert(e
->ec
>= 0);
330 ubi
->lookuptbl
[pnum
] = e
;
333 spin_unlock(&ubi
->wl_lock
);
335 vol_id
= lnum
? UBI_FM_DATA_VOLUME_ID
: UBI_FM_SB_VOLUME_ID
;
336 return schedule_erase(ubi
, e
, vol_id
, lnum
, torture
);
340 * ubi_is_erase_work - checks whether a work is erase work.
341 * @wrk: The work object to be checked
343 int ubi_is_erase_work(struct ubi_work
*wrk
)
345 return wrk
->func
== erase_worker
;
348 static void ubi_fastmap_close(struct ubi_device
*ubi
)
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
);
357 for (i
= 0; i
< ubi
->fm
->used_blocks
; i
++)
358 kfree(ubi
->fm
->e
[i
]);
364 * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
365 * See find_mean_wl_entry()
367 * @ubi: UBI device description object
368 * @e: physical eraseblock to return
369 * @root: RB tree to test against.
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
);
This page took 0.03898 seconds and 6 git commands to generate.