150acfb2bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dbb7d2a8SRichard Weinberger /*
3dbb7d2a8SRichard Weinberger * Copyright (c) 2012 Linutronix GmbH
4daef3dd1SRichard Weinberger * Copyright (c) 2014 sigma star gmbh
5dbb7d2a8SRichard Weinberger * Author: Richard Weinberger <richard@nod.at>
6dbb7d2a8SRichard Weinberger */
7dbb7d2a8SRichard Weinberger
8dbb7d2a8SRichard Weinberger #include <linux/crc32.h>
95d71afb0SRichard Weinberger #include <linux/bitmap.h>
10dbb7d2a8SRichard Weinberger #include "ubi.h"
11dbb7d2a8SRichard Weinberger
12dbb7d2a8SRichard Weinberger /**
13daef3dd1SRichard Weinberger * init_seen - allocate memory for used for debugging.
14daef3dd1SRichard Weinberger * @ubi: UBI device description object
15daef3dd1SRichard Weinberger */
init_seen(struct ubi_device * ubi)165d71afb0SRichard Weinberger static inline unsigned long *init_seen(struct ubi_device *ubi)
17daef3dd1SRichard Weinberger {
185d71afb0SRichard Weinberger unsigned long *ret;
19daef3dd1SRichard Weinberger
20daef3dd1SRichard Weinberger if (!ubi_dbg_chk_fastmap(ubi))
21daef3dd1SRichard Weinberger return NULL;
22daef3dd1SRichard Weinberger
23e7f35da2SChristophe JAILLET ret = bitmap_zalloc(ubi->peb_count, GFP_KERNEL);
24daef3dd1SRichard Weinberger if (!ret)
25daef3dd1SRichard Weinberger return ERR_PTR(-ENOMEM);
26daef3dd1SRichard Weinberger
27daef3dd1SRichard Weinberger return ret;
28daef3dd1SRichard Weinberger }
29daef3dd1SRichard Weinberger
30daef3dd1SRichard Weinberger /**
31daef3dd1SRichard Weinberger * free_seen - free the seen logic integer array.
32daef3dd1SRichard Weinberger * @seen: integer array of @ubi->peb_count size
33daef3dd1SRichard Weinberger */
free_seen(unsigned long * seen)345d71afb0SRichard Weinberger static inline void free_seen(unsigned long *seen)
35daef3dd1SRichard Weinberger {
36e7f35da2SChristophe JAILLET bitmap_free(seen);
37daef3dd1SRichard Weinberger }
38daef3dd1SRichard Weinberger
39daef3dd1SRichard Weinberger /**
40daef3dd1SRichard Weinberger * set_seen - mark a PEB as seen.
41daef3dd1SRichard Weinberger * @ubi: UBI device description object
42daef3dd1SRichard Weinberger * @pnum: The PEB to be makred as seen
43daef3dd1SRichard Weinberger * @seen: integer array of @ubi->peb_count size
44daef3dd1SRichard Weinberger */
set_seen(struct ubi_device * ubi,int pnum,unsigned long * seen)455d71afb0SRichard Weinberger static inline void set_seen(struct ubi_device *ubi, int pnum, unsigned long *seen)
46daef3dd1SRichard Weinberger {
47daef3dd1SRichard Weinberger if (!ubi_dbg_chk_fastmap(ubi) || !seen)
48daef3dd1SRichard Weinberger return;
49daef3dd1SRichard Weinberger
505d71afb0SRichard Weinberger set_bit(pnum, seen);
51daef3dd1SRichard Weinberger }
52daef3dd1SRichard Weinberger
53daef3dd1SRichard Weinberger /**
54daef3dd1SRichard Weinberger * self_check_seen - check whether all PEB have been seen by fastmap.
55daef3dd1SRichard Weinberger * @ubi: UBI device description object
56daef3dd1SRichard Weinberger * @seen: integer array of @ubi->peb_count size
57daef3dd1SRichard Weinberger */
self_check_seen(struct ubi_device * ubi,unsigned long * seen)585d71afb0SRichard Weinberger static int self_check_seen(struct ubi_device *ubi, unsigned long *seen)
59daef3dd1SRichard Weinberger {
60daef3dd1SRichard Weinberger int pnum, ret = 0;
61daef3dd1SRichard Weinberger
62daef3dd1SRichard Weinberger if (!ubi_dbg_chk_fastmap(ubi) || !seen)
63daef3dd1SRichard Weinberger return 0;
64daef3dd1SRichard Weinberger
65daef3dd1SRichard Weinberger for (pnum = 0; pnum < ubi->peb_count; pnum++) {
66ef5aafb6SSascha Hauer if (!test_bit(pnum, seen) && ubi->lookuptbl[pnum]) {
67daef3dd1SRichard Weinberger ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum);
68daef3dd1SRichard Weinberger ret = -EINVAL;
69daef3dd1SRichard Weinberger }
70daef3dd1SRichard Weinberger }
71daef3dd1SRichard Weinberger
72daef3dd1SRichard Weinberger return ret;
73daef3dd1SRichard Weinberger }
74daef3dd1SRichard Weinberger
75daef3dd1SRichard Weinberger /**
76dbb7d2a8SRichard Weinberger * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
77dbb7d2a8SRichard Weinberger * @ubi: UBI device description object
78dbb7d2a8SRichard Weinberger */
ubi_calc_fm_size(struct ubi_device * ubi)79dbb7d2a8SRichard Weinberger size_t ubi_calc_fm_size(struct ubi_device *ubi)
80dbb7d2a8SRichard Weinberger {
81dbb7d2a8SRichard Weinberger size_t size;
82dbb7d2a8SRichard Weinberger
83a18fd672Sshengyong size = sizeof(struct ubi_fm_sb) +
84a18fd672Sshengyong sizeof(struct ubi_fm_hdr) +
85a18fd672Sshengyong sizeof(struct ubi_fm_scan_pool) +
86a18fd672Sshengyong sizeof(struct ubi_fm_scan_pool) +
87a18fd672Sshengyong (ubi->peb_count * sizeof(struct ubi_fm_ec)) +
88*1a3487cdSZhang Yi ((sizeof(struct ubi_fm_eba) +
89*1a3487cdSZhang Yi sizeof(struct ubi_fm_volhdr)) *
90*1a3487cdSZhang Yi (UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT)) +
91*1a3487cdSZhang Yi (ubi->peb_count * sizeof(__be32));
92dbb7d2a8SRichard Weinberger return roundup(size, ubi->leb_size);
93dbb7d2a8SRichard Weinberger }
94dbb7d2a8SRichard Weinberger
95dbb7d2a8SRichard Weinberger
96dbb7d2a8SRichard Weinberger /**
97b5dd034fSJiapeng Chong * new_fm_vbuf() - allocate a new volume header for fastmap usage.
98dbb7d2a8SRichard Weinberger * @ubi: UBI device description object
99dbb7d2a8SRichard Weinberger * @vol_id: the VID of the new header
100dbb7d2a8SRichard Weinberger *
101dbb7d2a8SRichard Weinberger * Returns a new struct ubi_vid_hdr on success.
102dbb7d2a8SRichard Weinberger * NULL indicates out of memory.
103dbb7d2a8SRichard Weinberger */
new_fm_vbuf(struct ubi_device * ubi,int vol_id)1043291b52fSBoris Brezillon static struct ubi_vid_io_buf *new_fm_vbuf(struct ubi_device *ubi, int vol_id)
105dbb7d2a8SRichard Weinberger {
1063291b52fSBoris Brezillon struct ubi_vid_io_buf *new;
1073291b52fSBoris Brezillon struct ubi_vid_hdr *vh;
108dbb7d2a8SRichard Weinberger
1093291b52fSBoris Brezillon new = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
110dbb7d2a8SRichard Weinberger if (!new)
111dbb7d2a8SRichard Weinberger goto out;
112dbb7d2a8SRichard Weinberger
1133291b52fSBoris Brezillon vh = ubi_get_vid_hdr(new);
1143291b52fSBoris Brezillon vh->vol_type = UBI_VID_DYNAMIC;
1153291b52fSBoris Brezillon vh->vol_id = cpu_to_be32(vol_id);
116dbb7d2a8SRichard Weinberger
117dbb7d2a8SRichard Weinberger /* UBI implementations without fastmap support have to delete the
118dbb7d2a8SRichard Weinberger * fastmap.
119dbb7d2a8SRichard Weinberger */
1203291b52fSBoris Brezillon vh->compat = UBI_COMPAT_DELETE;
121dbb7d2a8SRichard Weinberger
122dbb7d2a8SRichard Weinberger out:
123dbb7d2a8SRichard Weinberger return new;
124dbb7d2a8SRichard Weinberger }
125dbb7d2a8SRichard Weinberger
126dbb7d2a8SRichard Weinberger /**
127dbb7d2a8SRichard Weinberger * add_aeb - create and add a attach erase block to a given list.
128dbb7d2a8SRichard Weinberger * @ai: UBI attach info object
129dbb7d2a8SRichard Weinberger * @list: the target list
130dbb7d2a8SRichard Weinberger * @pnum: PEB number of the new attach erase block
131dbb7d2a8SRichard Weinberger * @ec: erease counter of the new LEB
132dbb7d2a8SRichard Weinberger * @scrub: scrub this PEB after attaching
133dbb7d2a8SRichard Weinberger *
134dbb7d2a8SRichard Weinberger * Returns 0 on success, < 0 indicates an internal error.
135dbb7d2a8SRichard Weinberger */
add_aeb(struct ubi_attach_info * ai,struct list_head * list,int pnum,int ec,int scrub)136dbb7d2a8SRichard Weinberger static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
137dbb7d2a8SRichard Weinberger int pnum, int ec, int scrub)
138dbb7d2a8SRichard Weinberger {
139dbb7d2a8SRichard Weinberger struct ubi_ainf_peb *aeb;
140dbb7d2a8SRichard Weinberger
14191f4285fSBoris Brezillon aeb = ubi_alloc_aeb(ai, pnum, ec);
142dbb7d2a8SRichard Weinberger if (!aeb)
143dbb7d2a8SRichard Weinberger return -ENOMEM;
144dbb7d2a8SRichard Weinberger
145dbb7d2a8SRichard Weinberger aeb->lnum = -1;
146dbb7d2a8SRichard Weinberger aeb->scrub = scrub;
147dbb7d2a8SRichard Weinberger aeb->copy_flag = aeb->sqnum = 0;
148dbb7d2a8SRichard Weinberger
149dbb7d2a8SRichard Weinberger ai->ec_sum += aeb->ec;
150dbb7d2a8SRichard Weinberger ai->ec_count++;
151dbb7d2a8SRichard Weinberger
152dbb7d2a8SRichard Weinberger if (ai->max_ec < aeb->ec)
153dbb7d2a8SRichard Weinberger ai->max_ec = aeb->ec;
154dbb7d2a8SRichard Weinberger
155dbb7d2a8SRichard Weinberger if (ai->min_ec > aeb->ec)
156dbb7d2a8SRichard Weinberger ai->min_ec = aeb->ec;
157dbb7d2a8SRichard Weinberger
158dbb7d2a8SRichard Weinberger list_add_tail(&aeb->u.list, list);
159dbb7d2a8SRichard Weinberger
160dbb7d2a8SRichard Weinberger return 0;
161dbb7d2a8SRichard Weinberger }
162dbb7d2a8SRichard Weinberger
163dbb7d2a8SRichard Weinberger /**
164dbb7d2a8SRichard Weinberger * add_vol - create and add a new volume to ubi_attach_info.
165dbb7d2a8SRichard Weinberger * @ai: ubi_attach_info object
166dbb7d2a8SRichard Weinberger * @vol_id: VID of the new volume
167dbb7d2a8SRichard Weinberger * @used_ebs: number of used EBS
168dbb7d2a8SRichard Weinberger * @data_pad: data padding value of the new volume
169dbb7d2a8SRichard Weinberger * @vol_type: volume type
170dbb7d2a8SRichard Weinberger * @last_eb_bytes: number of bytes in the last LEB
171dbb7d2a8SRichard Weinberger *
172dbb7d2a8SRichard Weinberger * Returns the new struct ubi_ainf_volume on success.
173dbb7d2a8SRichard Weinberger * NULL indicates an error.
174dbb7d2a8SRichard Weinberger */
add_vol(struct ubi_attach_info * ai,int vol_id,int used_ebs,int data_pad,u8 vol_type,int last_eb_bytes)175dbb7d2a8SRichard Weinberger static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id,
176dbb7d2a8SRichard Weinberger int used_ebs, int data_pad, u8 vol_type,
177dbb7d2a8SRichard Weinberger int last_eb_bytes)
178dbb7d2a8SRichard Weinberger {
179dbb7d2a8SRichard Weinberger struct ubi_ainf_volume *av;
180dbb7d2a8SRichard Weinberger
181de4c455bSBoris Brezillon av = ubi_add_av(ai, vol_id);
182de4c455bSBoris Brezillon if (IS_ERR(av))
183de4c455bSBoris Brezillon return av;
184dbb7d2a8SRichard Weinberger
185dbb7d2a8SRichard Weinberger av->data_pad = data_pad;
186dbb7d2a8SRichard Weinberger av->last_data_size = last_eb_bytes;
187dbb7d2a8SRichard Weinberger av->compat = 0;
188dbb7d2a8SRichard Weinberger av->vol_type = vol_type;
18942dd3cdcSRichard Weinberger if (av->vol_type == UBI_STATIC_VOLUME)
19042dd3cdcSRichard Weinberger av->used_ebs = used_ebs;
191dbb7d2a8SRichard Weinberger
192dbb7d2a8SRichard Weinberger dbg_bld("found volume (ID %i)", vol_id);
193dbb7d2a8SRichard Weinberger return av;
194dbb7d2a8SRichard Weinberger }
195dbb7d2a8SRichard Weinberger
196dbb7d2a8SRichard Weinberger /**
197dbb7d2a8SRichard Weinberger * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
198dbb7d2a8SRichard Weinberger * from it's original list.
199dbb7d2a8SRichard Weinberger * @ai: ubi_attach_info object
200dbb7d2a8SRichard Weinberger * @aeb: the to be assigned SEB
201dbb7d2a8SRichard Weinberger * @av: target scan volume
202dbb7d2a8SRichard Weinberger */
assign_aeb_to_av(struct ubi_attach_info * ai,struct ubi_ainf_peb * aeb,struct ubi_ainf_volume * av)203dbb7d2a8SRichard Weinberger static void assign_aeb_to_av(struct ubi_attach_info *ai,
204dbb7d2a8SRichard Weinberger struct ubi_ainf_peb *aeb,
205dbb7d2a8SRichard Weinberger struct ubi_ainf_volume *av)
206dbb7d2a8SRichard Weinberger {
207dbb7d2a8SRichard Weinberger struct ubi_ainf_peb *tmp_aeb;
208f50629dfSColin Ian King struct rb_node **p = &av->root.rb_node, *parent = NULL;
209dbb7d2a8SRichard Weinberger
210dbb7d2a8SRichard Weinberger while (*p) {
211dbb7d2a8SRichard Weinberger parent = *p;
212dbb7d2a8SRichard Weinberger
213dbb7d2a8SRichard Weinberger tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
214dbb7d2a8SRichard Weinberger if (aeb->lnum != tmp_aeb->lnum) {
215dbb7d2a8SRichard Weinberger if (aeb->lnum < tmp_aeb->lnum)
216dbb7d2a8SRichard Weinberger p = &(*p)->rb_left;
217dbb7d2a8SRichard Weinberger else
218dbb7d2a8SRichard Weinberger p = &(*p)->rb_right;
219dbb7d2a8SRichard Weinberger
220dbb7d2a8SRichard Weinberger continue;
221dbb7d2a8SRichard Weinberger } else
222dbb7d2a8SRichard Weinberger break;
223dbb7d2a8SRichard Weinberger }
224dbb7d2a8SRichard Weinberger
225dbb7d2a8SRichard Weinberger list_del(&aeb->u.list);
226dbb7d2a8SRichard Weinberger av->leb_count++;
227dbb7d2a8SRichard Weinberger
228dbb7d2a8SRichard Weinberger rb_link_node(&aeb->u.rb, parent, p);
229dbb7d2a8SRichard Weinberger rb_insert_color(&aeb->u.rb, &av->root);
230dbb7d2a8SRichard Weinberger }
231dbb7d2a8SRichard Weinberger
232dbb7d2a8SRichard Weinberger /**
233dbb7d2a8SRichard Weinberger * update_vol - inserts or updates a LEB which was found a pool.
234dbb7d2a8SRichard Weinberger * @ubi: the UBI device object
235dbb7d2a8SRichard Weinberger * @ai: attach info object
236dbb7d2a8SRichard Weinberger * @av: the volume this LEB belongs to
237dbb7d2a8SRichard Weinberger * @new_vh: the volume header derived from new_aeb
238dbb7d2a8SRichard Weinberger * @new_aeb: the AEB to be examined
239dbb7d2a8SRichard Weinberger *
240dbb7d2a8SRichard Weinberger * Returns 0 on success, < 0 indicates an internal error.
241dbb7d2a8SRichard Weinberger */
update_vol(struct ubi_device * ubi,struct ubi_attach_info * ai,struct ubi_ainf_volume * av,struct ubi_vid_hdr * new_vh,struct ubi_ainf_peb * new_aeb)242dbb7d2a8SRichard Weinberger static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
243dbb7d2a8SRichard Weinberger struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh,
244dbb7d2a8SRichard Weinberger struct ubi_ainf_peb *new_aeb)
245dbb7d2a8SRichard Weinberger {
246dbb7d2a8SRichard Weinberger struct rb_node **p = &av->root.rb_node, *parent = NULL;
247dbb7d2a8SRichard Weinberger struct ubi_ainf_peb *aeb, *victim;
248dbb7d2a8SRichard Weinberger int cmp_res;
249dbb7d2a8SRichard Weinberger
250dbb7d2a8SRichard Weinberger while (*p) {
251dbb7d2a8SRichard Weinberger parent = *p;
252dbb7d2a8SRichard Weinberger aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
253dbb7d2a8SRichard Weinberger
254dbb7d2a8SRichard Weinberger if (be32_to_cpu(new_vh->lnum) != aeb->lnum) {
255dbb7d2a8SRichard Weinberger if (be32_to_cpu(new_vh->lnum) < aeb->lnum)
256dbb7d2a8SRichard Weinberger p = &(*p)->rb_left;
257dbb7d2a8SRichard Weinberger else
258dbb7d2a8SRichard Weinberger p = &(*p)->rb_right;
259dbb7d2a8SRichard Weinberger
260dbb7d2a8SRichard Weinberger continue;
261dbb7d2a8SRichard Weinberger }
262dbb7d2a8SRichard Weinberger
263dbb7d2a8SRichard Weinberger /* This case can happen if the fastmap gets written
264dbb7d2a8SRichard Weinberger * because of a volume change (creation, deletion, ..).
265dbb7d2a8SRichard Weinberger * Then a PEB can be within the persistent EBA and the pool.
266dbb7d2a8SRichard Weinberger */
267dbb7d2a8SRichard Weinberger if (aeb->pnum == new_aeb->pnum) {
268dbb7d2a8SRichard Weinberger ubi_assert(aeb->lnum == new_aeb->lnum);
26991f4285fSBoris Brezillon ubi_free_aeb(ai, new_aeb);
270dbb7d2a8SRichard Weinberger
271dbb7d2a8SRichard Weinberger return 0;
272dbb7d2a8SRichard Weinberger }
273dbb7d2a8SRichard Weinberger
274dbb7d2a8SRichard Weinberger cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh);
275dbb7d2a8SRichard Weinberger if (cmp_res < 0)
276dbb7d2a8SRichard Weinberger return cmp_res;
277dbb7d2a8SRichard Weinberger
278dbb7d2a8SRichard Weinberger /* new_aeb is newer */
279dbb7d2a8SRichard Weinberger if (cmp_res & 1) {
280a15cf34fSColin Ian King victim = ubi_alloc_aeb(ai, aeb->pnum, aeb->ec);
281dbb7d2a8SRichard Weinberger if (!victim)
282dbb7d2a8SRichard Weinberger return -ENOMEM;
283dbb7d2a8SRichard Weinberger
284dbb7d2a8SRichard Weinberger list_add_tail(&victim->u.list, &ai->erase);
285dbb7d2a8SRichard Weinberger
286dbb7d2a8SRichard Weinberger if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
287669d3d12Sshengyong av->last_data_size =
288dbb7d2a8SRichard Weinberger be32_to_cpu(new_vh->data_size);
289dbb7d2a8SRichard Weinberger
290dbb7d2a8SRichard Weinberger dbg_bld("vol %i: AEB %i's PEB %i is the newer",
291dbb7d2a8SRichard Weinberger av->vol_id, aeb->lnum, new_aeb->pnum);
292dbb7d2a8SRichard Weinberger
293dbb7d2a8SRichard Weinberger aeb->ec = new_aeb->ec;
294dbb7d2a8SRichard Weinberger aeb->pnum = new_aeb->pnum;
295dbb7d2a8SRichard Weinberger aeb->copy_flag = new_vh->copy_flag;
296dbb7d2a8SRichard Weinberger aeb->scrub = new_aeb->scrub;
297f7d11b33SRichard Weinberger aeb->sqnum = new_aeb->sqnum;
29891f4285fSBoris Brezillon ubi_free_aeb(ai, new_aeb);
299dbb7d2a8SRichard Weinberger
300dbb7d2a8SRichard Weinberger /* new_aeb is older */
301dbb7d2a8SRichard Weinberger } else {
302dbb7d2a8SRichard Weinberger dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
303dbb7d2a8SRichard Weinberger av->vol_id, aeb->lnum, new_aeb->pnum);
304dbb7d2a8SRichard Weinberger list_add_tail(&new_aeb->u.list, &ai->erase);
305dbb7d2a8SRichard Weinberger }
306dbb7d2a8SRichard Weinberger
307dbb7d2a8SRichard Weinberger return 0;
308dbb7d2a8SRichard Weinberger }
309dbb7d2a8SRichard Weinberger /* This LEB is new, let's add it to the volume */
310dbb7d2a8SRichard Weinberger
311dbb7d2a8SRichard Weinberger if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
312dbb7d2a8SRichard Weinberger av->highest_lnum = be32_to_cpu(new_vh->lnum);
313dbb7d2a8SRichard Weinberger av->last_data_size = be32_to_cpu(new_vh->data_size);
314dbb7d2a8SRichard Weinberger }
315dbb7d2a8SRichard Weinberger
316dbb7d2a8SRichard Weinberger if (av->vol_type == UBI_STATIC_VOLUME)
317dbb7d2a8SRichard Weinberger av->used_ebs = be32_to_cpu(new_vh->used_ebs);
318dbb7d2a8SRichard Weinberger
319dbb7d2a8SRichard Weinberger av->leb_count++;
320dbb7d2a8SRichard Weinberger
321dbb7d2a8SRichard Weinberger rb_link_node(&new_aeb->u.rb, parent, p);
322dbb7d2a8SRichard Weinberger rb_insert_color(&new_aeb->u.rb, &av->root);
323dbb7d2a8SRichard Weinberger
324dbb7d2a8SRichard Weinberger return 0;
325dbb7d2a8SRichard Weinberger }
326dbb7d2a8SRichard Weinberger
327dbb7d2a8SRichard Weinberger /**
328dbb7d2a8SRichard Weinberger * process_pool_aeb - we found a non-empty PEB in a pool.
329dbb7d2a8SRichard Weinberger * @ubi: UBI device object
330dbb7d2a8SRichard Weinberger * @ai: attach info object
331dbb7d2a8SRichard Weinberger * @new_vh: the volume header derived from new_aeb
332dbb7d2a8SRichard Weinberger * @new_aeb: the AEB to be examined
333dbb7d2a8SRichard Weinberger *
334dbb7d2a8SRichard Weinberger * Returns 0 on success, < 0 indicates an internal error.
335dbb7d2a8SRichard Weinberger */
process_pool_aeb(struct ubi_device * ubi,struct ubi_attach_info * ai,struct ubi_vid_hdr * new_vh,struct ubi_ainf_peb * new_aeb)336dbb7d2a8SRichard Weinberger static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
337dbb7d2a8SRichard Weinberger struct ubi_vid_hdr *new_vh,
338dbb7d2a8SRichard Weinberger struct ubi_ainf_peb *new_aeb)
339dbb7d2a8SRichard Weinberger {
3404c368421SBoris Brezillon int vol_id = be32_to_cpu(new_vh->vol_id);
341f5f9d43fSBoris Brezillon struct ubi_ainf_volume *av;
342dbb7d2a8SRichard Weinberger
3434c368421SBoris Brezillon if (vol_id == UBI_FM_SB_VOLUME_ID || vol_id == UBI_FM_DATA_VOLUME_ID) {
34491f4285fSBoris Brezillon ubi_free_aeb(ai, new_aeb);
345dbb7d2a8SRichard Weinberger
346dbb7d2a8SRichard Weinberger return 0;
347dbb7d2a8SRichard Weinberger }
348dbb7d2a8SRichard Weinberger
349dbb7d2a8SRichard Weinberger /* Find the volume this SEB belongs to */
3504c368421SBoris Brezillon av = ubi_find_av(ai, vol_id);
351f5f9d43fSBoris Brezillon if (!av) {
35232608703STanya Brokhman ubi_err(ubi, "orphaned volume in fastmap pool!");
35391f4285fSBoris Brezillon ubi_free_aeb(ai, new_aeb);
354dbb7d2a8SRichard Weinberger return UBI_BAD_FASTMAP;
355dbb7d2a8SRichard Weinberger }
356dbb7d2a8SRichard Weinberger
3574c368421SBoris Brezillon ubi_assert(vol_id == av->vol_id);
358dbb7d2a8SRichard Weinberger
359dbb7d2a8SRichard Weinberger return update_vol(ubi, ai, av, new_vh, new_aeb);
360dbb7d2a8SRichard Weinberger }
361dbb7d2a8SRichard Weinberger
362dbb7d2a8SRichard Weinberger /**
363dbb7d2a8SRichard Weinberger * unmap_peb - unmap a PEB.
364dbb7d2a8SRichard Weinberger * If fastmap detects a free PEB in the pool it has to check whether
365dbb7d2a8SRichard Weinberger * this PEB has been unmapped after writing the fastmap.
366dbb7d2a8SRichard Weinberger *
367dbb7d2a8SRichard Weinberger * @ai: UBI attach info object
368dbb7d2a8SRichard Weinberger * @pnum: The PEB to be unmapped
369dbb7d2a8SRichard Weinberger */
unmap_peb(struct ubi_attach_info * ai,int pnum)370dbb7d2a8SRichard Weinberger static void unmap_peb(struct ubi_attach_info *ai, int pnum)
371dbb7d2a8SRichard Weinberger {
372dbb7d2a8SRichard Weinberger struct ubi_ainf_volume *av;
373dbb7d2a8SRichard Weinberger struct rb_node *node, *node2;
374dbb7d2a8SRichard Weinberger struct ubi_ainf_peb *aeb;
375dbb7d2a8SRichard Weinberger
376f2fb1346SBoris Brezillon ubi_rb_for_each_entry(node, av, &ai->volumes, rb) {
377f2fb1346SBoris Brezillon ubi_rb_for_each_entry(node2, aeb, &av->root, u.rb) {
378dbb7d2a8SRichard Weinberger if (aeb->pnum == pnum) {
379dbb7d2a8SRichard Weinberger rb_erase(&aeb->u.rb, &av->root);
380ad3d6a05SRichard Weinberger av->leb_count--;
38191f4285fSBoris Brezillon ubi_free_aeb(ai, aeb);
382dbb7d2a8SRichard Weinberger return;
383dbb7d2a8SRichard Weinberger }
384dbb7d2a8SRichard Weinberger }
385dbb7d2a8SRichard Weinberger }
386dbb7d2a8SRichard Weinberger }
387dbb7d2a8SRichard Weinberger
388dbb7d2a8SRichard Weinberger /**
389dbb7d2a8SRichard Weinberger * scan_pool - scans a pool for changed (no longer empty PEBs).
390dbb7d2a8SRichard Weinberger * @ubi: UBI device object
391dbb7d2a8SRichard Weinberger * @ai: attach info object
392dbb7d2a8SRichard Weinberger * @pebs: an array of all PEB numbers in the to be scanned pool
393dbb7d2a8SRichard Weinberger * @pool_size: size of the pool (number of entries in @pebs)
394dbb7d2a8SRichard Weinberger * @max_sqnum: pointer to the maximal sequence number
395dbb7d2a8SRichard Weinberger * @free: list of PEBs which are most likely free (and go into @ai->free)
396dbb7d2a8SRichard Weinberger *
397dbb7d2a8SRichard Weinberger * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
398dbb7d2a8SRichard Weinberger * < 0 indicates an internal error.
399dbb7d2a8SRichard Weinberger */
scan_pool(struct ubi_device * ubi,struct ubi_attach_info * ai,__be32 * pebs,int pool_size,unsigned long long * max_sqnum,struct list_head * free)400dbb7d2a8SRichard Weinberger static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
4012a130f1aSEzequiel García __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
402d141a8efSRichard Weinberger struct list_head *free)
403dbb7d2a8SRichard Weinberger {
4043291b52fSBoris Brezillon struct ubi_vid_io_buf *vb;
405dbb7d2a8SRichard Weinberger struct ubi_vid_hdr *vh;
406dbb7d2a8SRichard Weinberger struct ubi_ec_hdr *ech;
407d141a8efSRichard Weinberger struct ubi_ainf_peb *new_aeb;
408d141a8efSRichard Weinberger int i, pnum, err, ret = 0;
409dbb7d2a8SRichard Weinberger
410dbb7d2a8SRichard Weinberger ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
411dbb7d2a8SRichard Weinberger if (!ech)
412dbb7d2a8SRichard Weinberger return -ENOMEM;
413dbb7d2a8SRichard Weinberger
4143291b52fSBoris Brezillon vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
4153291b52fSBoris Brezillon if (!vb) {
416dbb7d2a8SRichard Weinberger kfree(ech);
417dbb7d2a8SRichard Weinberger return -ENOMEM;
418dbb7d2a8SRichard Weinberger }
419dbb7d2a8SRichard Weinberger
4203291b52fSBoris Brezillon vh = ubi_get_vid_hdr(vb);
4213291b52fSBoris Brezillon
422dbb7d2a8SRichard Weinberger dbg_bld("scanning fastmap pool: size = %i", pool_size);
423dbb7d2a8SRichard Weinberger
424dbb7d2a8SRichard Weinberger /*
425dbb7d2a8SRichard Weinberger * Now scan all PEBs in the pool to find changes which have been made
426dbb7d2a8SRichard Weinberger * after the creation of the fastmap
427dbb7d2a8SRichard Weinberger */
428dbb7d2a8SRichard Weinberger for (i = 0; i < pool_size; i++) {
429dbb7d2a8SRichard Weinberger int scrub = 0;
430c22301adSRichard Genoud int image_seq;
431dbb7d2a8SRichard Weinberger
432dbb7d2a8SRichard Weinberger pnum = be32_to_cpu(pebs[i]);
433dbb7d2a8SRichard Weinberger
434dbb7d2a8SRichard Weinberger if (ubi_io_is_bad(ubi, pnum)) {
43532608703STanya Brokhman ubi_err(ubi, "bad PEB in fastmap pool!");
436dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
437dbb7d2a8SRichard Weinberger goto out;
438dbb7d2a8SRichard Weinberger }
439dbb7d2a8SRichard Weinberger
440dbb7d2a8SRichard Weinberger err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
441dbb7d2a8SRichard Weinberger if (err && err != UBI_IO_BITFLIPS) {
44232608703STanya Brokhman ubi_err(ubi, "unable to read EC header! PEB:%i err:%i",
443dbb7d2a8SRichard Weinberger pnum, err);
444dbb7d2a8SRichard Weinberger ret = err > 0 ? UBI_BAD_FASTMAP : err;
445dbb7d2a8SRichard Weinberger goto out;
44644305ebdSBrian Norris } else if (err == UBI_IO_BITFLIPS)
447dbb7d2a8SRichard Weinberger scrub = 1;
448dbb7d2a8SRichard Weinberger
449c22301adSRichard Genoud /*
450c22301adSRichard Genoud * Older UBI implementations have image_seq set to zero, so
451c22301adSRichard Genoud * we shouldn't fail if image_seq == 0.
452c22301adSRichard Genoud */
453c22301adSRichard Genoud image_seq = be32_to_cpu(ech->image_seq);
454c22301adSRichard Genoud
455c22301adSRichard Genoud if (image_seq && (image_seq != ubi->image_seq)) {
45632608703STanya Brokhman ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x",
457dbb7d2a8SRichard Weinberger be32_to_cpu(ech->image_seq), ubi->image_seq);
458f240dca8SRichard Weinberger ret = UBI_BAD_FASTMAP;
459dbb7d2a8SRichard Weinberger goto out;
460dbb7d2a8SRichard Weinberger }
461dbb7d2a8SRichard Weinberger
4623291b52fSBoris Brezillon err = ubi_io_read_vid_hdr(ubi, pnum, vb, 0);
463dbb7d2a8SRichard Weinberger if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
464dbb7d2a8SRichard Weinberger unsigned long long ec = be64_to_cpu(ech->ec);
465dbb7d2a8SRichard Weinberger unmap_peb(ai, pnum);
466dbb7d2a8SRichard Weinberger dbg_bld("Adding PEB to free: %i", pnum);
467ecbfa8eaSBoris Brezillon
468dbb7d2a8SRichard Weinberger if (err == UBI_IO_FF_BITFLIPS)
469ecbfa8eaSBoris Brezillon scrub = 1;
470ecbfa8eaSBoris Brezillon
471c3c07fc2SZhihao Cheng ret = add_aeb(ai, free, pnum, ec, scrub);
472c3c07fc2SZhihao Cheng if (ret)
473c3c07fc2SZhihao Cheng goto out;
474dbb7d2a8SRichard Weinberger continue;
475dbb7d2a8SRichard Weinberger } else if (err == 0 || err == UBI_IO_BITFLIPS) {
476dbb7d2a8SRichard Weinberger dbg_bld("Found non empty PEB:%i in pool", pnum);
477dbb7d2a8SRichard Weinberger
478dbb7d2a8SRichard Weinberger if (err == UBI_IO_BITFLIPS)
479dbb7d2a8SRichard Weinberger scrub = 1;
480dbb7d2a8SRichard Weinberger
48191f4285fSBoris Brezillon new_aeb = ubi_alloc_aeb(ai, pnum, be64_to_cpu(ech->ec));
482dbb7d2a8SRichard Weinberger if (!new_aeb) {
483dbb7d2a8SRichard Weinberger ret = -ENOMEM;
484dbb7d2a8SRichard Weinberger goto out;
485dbb7d2a8SRichard Weinberger }
486dbb7d2a8SRichard Weinberger
487dbb7d2a8SRichard Weinberger new_aeb->lnum = be32_to_cpu(vh->lnum);
488dbb7d2a8SRichard Weinberger new_aeb->sqnum = be64_to_cpu(vh->sqnum);
489dbb7d2a8SRichard Weinberger new_aeb->copy_flag = vh->copy_flag;
490dbb7d2a8SRichard Weinberger new_aeb->scrub = scrub;
491dbb7d2a8SRichard Weinberger
492dbb7d2a8SRichard Weinberger if (*max_sqnum < new_aeb->sqnum)
493dbb7d2a8SRichard Weinberger *max_sqnum = new_aeb->sqnum;
494dbb7d2a8SRichard Weinberger
495dbb7d2a8SRichard Weinberger err = process_pool_aeb(ubi, ai, vh, new_aeb);
496dbb7d2a8SRichard Weinberger if (err) {
497dbb7d2a8SRichard Weinberger ret = err > 0 ? UBI_BAD_FASTMAP : err;
498dbb7d2a8SRichard Weinberger goto out;
499dbb7d2a8SRichard Weinberger }
500dbb7d2a8SRichard Weinberger } else {
501dbb7d2a8SRichard Weinberger /* We are paranoid and fall back to scanning mode */
50232608703STanya Brokhman ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!");
503dbb7d2a8SRichard Weinberger ret = err > 0 ? UBI_BAD_FASTMAP : err;
504dbb7d2a8SRichard Weinberger goto out;
505dbb7d2a8SRichard Weinberger }
506dbb7d2a8SRichard Weinberger
507dbb7d2a8SRichard Weinberger }
508dbb7d2a8SRichard Weinberger
509dbb7d2a8SRichard Weinberger out:
5103291b52fSBoris Brezillon ubi_free_vid_buf(vb);
511dbb7d2a8SRichard Weinberger kfree(ech);
512dbb7d2a8SRichard Weinberger return ret;
513dbb7d2a8SRichard Weinberger }
514dbb7d2a8SRichard Weinberger
515dbb7d2a8SRichard Weinberger /**
516dbb7d2a8SRichard Weinberger * count_fastmap_pebs - Counts the PEBs found by fastmap.
517dbb7d2a8SRichard Weinberger * @ai: The UBI attach info object
518dbb7d2a8SRichard Weinberger */
count_fastmap_pebs(struct ubi_attach_info * ai)519dbb7d2a8SRichard Weinberger static int count_fastmap_pebs(struct ubi_attach_info *ai)
520dbb7d2a8SRichard Weinberger {
521dbb7d2a8SRichard Weinberger struct ubi_ainf_peb *aeb;
522dbb7d2a8SRichard Weinberger struct ubi_ainf_volume *av;
523dbb7d2a8SRichard Weinberger struct rb_node *rb1, *rb2;
524dbb7d2a8SRichard Weinberger int n = 0;
525dbb7d2a8SRichard Weinberger
526dbb7d2a8SRichard Weinberger list_for_each_entry(aeb, &ai->erase, u.list)
527dbb7d2a8SRichard Weinberger n++;
528dbb7d2a8SRichard Weinberger
529dbb7d2a8SRichard Weinberger list_for_each_entry(aeb, &ai->free, u.list)
530dbb7d2a8SRichard Weinberger n++;
531dbb7d2a8SRichard Weinberger
532dbb7d2a8SRichard Weinberger ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
533dbb7d2a8SRichard Weinberger ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
534dbb7d2a8SRichard Weinberger n++;
535dbb7d2a8SRichard Weinberger
536dbb7d2a8SRichard Weinberger return n;
537dbb7d2a8SRichard Weinberger }
538dbb7d2a8SRichard Weinberger
539dbb7d2a8SRichard Weinberger /**
540dbb7d2a8SRichard Weinberger * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
541dbb7d2a8SRichard Weinberger * @ubi: UBI device object
542dbb7d2a8SRichard Weinberger * @ai: UBI attach info object
543dbb7d2a8SRichard Weinberger * @fm: the fastmap to be attached
544dbb7d2a8SRichard Weinberger *
545dbb7d2a8SRichard Weinberger * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
546dbb7d2a8SRichard Weinberger * < 0 indicates an internal error.
547dbb7d2a8SRichard Weinberger */
ubi_attach_fastmap(struct ubi_device * ubi,struct ubi_attach_info * ai,struct ubi_fastmap_layout * fm)548dbb7d2a8SRichard Weinberger static int ubi_attach_fastmap(struct ubi_device *ubi,
549dbb7d2a8SRichard Weinberger struct ubi_attach_info *ai,
550dbb7d2a8SRichard Weinberger struct ubi_fastmap_layout *fm)
551dbb7d2a8SRichard Weinberger {
552d141a8efSRichard Weinberger struct list_head used, free;
553dbb7d2a8SRichard Weinberger struct ubi_ainf_volume *av;
554dbb7d2a8SRichard Weinberger struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
555dbb7d2a8SRichard Weinberger struct ubi_fm_sb *fmsb;
556dbb7d2a8SRichard Weinberger struct ubi_fm_hdr *fmhdr;
557f6e951afSshengyong struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
558dbb7d2a8SRichard Weinberger struct ubi_fm_ec *fmec;
559dbb7d2a8SRichard Weinberger struct ubi_fm_volhdr *fmvhdr;
560dbb7d2a8SRichard Weinberger struct ubi_fm_eba *fm_eba;
561dbb7d2a8SRichard Weinberger int ret, i, j, pool_size, wl_pool_size;
562dbb7d2a8SRichard Weinberger size_t fm_pos = 0, fm_size = ubi->fm_size;
563dbb7d2a8SRichard Weinberger unsigned long long max_sqnum = 0;
564dbb7d2a8SRichard Weinberger void *fm_raw = ubi->fm_buf;
565dbb7d2a8SRichard Weinberger
566dbb7d2a8SRichard Weinberger INIT_LIST_HEAD(&used);
567dbb7d2a8SRichard Weinberger INIT_LIST_HEAD(&free);
568dbb7d2a8SRichard Weinberger ai->min_ec = UBI_MAX_ERASECOUNTER;
569dbb7d2a8SRichard Weinberger
570dbb7d2a8SRichard Weinberger fmsb = (struct ubi_fm_sb *)(fm_raw);
571dbb7d2a8SRichard Weinberger ai->max_sqnum = fmsb->sqnum;
572dbb7d2a8SRichard Weinberger fm_pos += sizeof(struct ubi_fm_sb);
573dbb7d2a8SRichard Weinberger if (fm_pos >= fm_size)
574dbb7d2a8SRichard Weinberger goto fail_bad;
575dbb7d2a8SRichard Weinberger
576dbb7d2a8SRichard Weinberger fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
577dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fmhdr);
578dbb7d2a8SRichard Weinberger if (fm_pos >= fm_size)
579dbb7d2a8SRichard Weinberger goto fail_bad;
580dbb7d2a8SRichard Weinberger
581dbb7d2a8SRichard Weinberger if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
58232608703STanya Brokhman ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x",
583dbb7d2a8SRichard Weinberger be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
584dbb7d2a8SRichard Weinberger goto fail_bad;
585dbb7d2a8SRichard Weinberger }
586dbb7d2a8SRichard Weinberger
587f6e951afSshengyong fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
588f6e951afSshengyong fm_pos += sizeof(*fmpl);
589dbb7d2a8SRichard Weinberger if (fm_pos >= fm_size)
590dbb7d2a8SRichard Weinberger goto fail_bad;
591f6e951afSshengyong if (be32_to_cpu(fmpl->magic) != UBI_FM_POOL_MAGIC) {
59232608703STanya Brokhman ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x",
593f6e951afSshengyong be32_to_cpu(fmpl->magic), UBI_FM_POOL_MAGIC);
594dbb7d2a8SRichard Weinberger goto fail_bad;
595dbb7d2a8SRichard Weinberger }
596dbb7d2a8SRichard Weinberger
597f6e951afSshengyong fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
598f6e951afSshengyong fm_pos += sizeof(*fmpl_wl);
599dbb7d2a8SRichard Weinberger if (fm_pos >= fm_size)
600dbb7d2a8SRichard Weinberger goto fail_bad;
601f6e951afSshengyong if (be32_to_cpu(fmpl_wl->magic) != UBI_FM_POOL_MAGIC) {
602f6e951afSshengyong ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x",
603f6e951afSshengyong be32_to_cpu(fmpl_wl->magic), UBI_FM_POOL_MAGIC);
604dbb7d2a8SRichard Weinberger goto fail_bad;
605dbb7d2a8SRichard Weinberger }
606dbb7d2a8SRichard Weinberger
607f6e951afSshengyong pool_size = be16_to_cpu(fmpl->size);
608f6e951afSshengyong wl_pool_size = be16_to_cpu(fmpl_wl->size);
609f6e951afSshengyong fm->max_pool_size = be16_to_cpu(fmpl->max_size);
610f6e951afSshengyong fm->max_wl_pool_size = be16_to_cpu(fmpl_wl->max_size);
611dbb7d2a8SRichard Weinberger
612dbb7d2a8SRichard Weinberger if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
61332608703STanya Brokhman ubi_err(ubi, "bad pool size: %i", pool_size);
614dbb7d2a8SRichard Weinberger goto fail_bad;
615dbb7d2a8SRichard Weinberger }
616dbb7d2a8SRichard Weinberger
617dbb7d2a8SRichard Weinberger if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
61832608703STanya Brokhman ubi_err(ubi, "bad WL pool size: %i", wl_pool_size);
619dbb7d2a8SRichard Weinberger goto fail_bad;
620dbb7d2a8SRichard Weinberger }
621dbb7d2a8SRichard Weinberger
622dbb7d2a8SRichard Weinberger
623dbb7d2a8SRichard Weinberger if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
624dbb7d2a8SRichard Weinberger fm->max_pool_size < 0) {
62532608703STanya Brokhman ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size);
626dbb7d2a8SRichard Weinberger goto fail_bad;
627dbb7d2a8SRichard Weinberger }
628dbb7d2a8SRichard Weinberger
629dbb7d2a8SRichard Weinberger if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
630dbb7d2a8SRichard Weinberger fm->max_wl_pool_size < 0) {
63132608703STanya Brokhman ubi_err(ubi, "bad maximal WL pool size: %i",
63232608703STanya Brokhman fm->max_wl_pool_size);
633dbb7d2a8SRichard Weinberger goto fail_bad;
634dbb7d2a8SRichard Weinberger }
635dbb7d2a8SRichard Weinberger
636dbb7d2a8SRichard Weinberger /* read EC values from free list */
637dbb7d2a8SRichard Weinberger for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
638dbb7d2a8SRichard Weinberger fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
639dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fmec);
640dbb7d2a8SRichard Weinberger if (fm_pos >= fm_size)
641dbb7d2a8SRichard Weinberger goto fail_bad;
642dbb7d2a8SRichard Weinberger
643c3c07fc2SZhihao Cheng ret = add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
644dbb7d2a8SRichard Weinberger be32_to_cpu(fmec->ec), 0);
645c3c07fc2SZhihao Cheng if (ret)
646c3c07fc2SZhihao Cheng goto fail;
647dbb7d2a8SRichard Weinberger }
648dbb7d2a8SRichard Weinberger
649dbb7d2a8SRichard Weinberger /* read EC values from used list */
650dbb7d2a8SRichard Weinberger for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
651dbb7d2a8SRichard Weinberger fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
652dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fmec);
653dbb7d2a8SRichard Weinberger if (fm_pos >= fm_size)
654dbb7d2a8SRichard Weinberger goto fail_bad;
655dbb7d2a8SRichard Weinberger
656c3c07fc2SZhihao Cheng ret = add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
657dbb7d2a8SRichard Weinberger be32_to_cpu(fmec->ec), 0);
658c3c07fc2SZhihao Cheng if (ret)
659c3c07fc2SZhihao Cheng goto fail;
660dbb7d2a8SRichard Weinberger }
661dbb7d2a8SRichard Weinberger
662dbb7d2a8SRichard Weinberger /* read EC values from scrub list */
663dbb7d2a8SRichard Weinberger for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
664dbb7d2a8SRichard Weinberger fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
665dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fmec);
666dbb7d2a8SRichard Weinberger if (fm_pos >= fm_size)
667dbb7d2a8SRichard Weinberger goto fail_bad;
668dbb7d2a8SRichard Weinberger
669c3c07fc2SZhihao Cheng ret = add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
670dbb7d2a8SRichard Weinberger be32_to_cpu(fmec->ec), 1);
671c3c07fc2SZhihao Cheng if (ret)
672c3c07fc2SZhihao Cheng goto fail;
673dbb7d2a8SRichard Weinberger }
674dbb7d2a8SRichard Weinberger
675dbb7d2a8SRichard Weinberger /* read EC values from erase list */
676dbb7d2a8SRichard Weinberger for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
677dbb7d2a8SRichard Weinberger fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
678dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fmec);
679dbb7d2a8SRichard Weinberger if (fm_pos >= fm_size)
680dbb7d2a8SRichard Weinberger goto fail_bad;
681dbb7d2a8SRichard Weinberger
682c3c07fc2SZhihao Cheng ret = add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
683dbb7d2a8SRichard Weinberger be32_to_cpu(fmec->ec), 1);
684c3c07fc2SZhihao Cheng if (ret)
685c3c07fc2SZhihao Cheng goto fail;
686dbb7d2a8SRichard Weinberger }
687dbb7d2a8SRichard Weinberger
688dbb7d2a8SRichard Weinberger ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
689dbb7d2a8SRichard Weinberger ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
690dbb7d2a8SRichard Weinberger
691dbb7d2a8SRichard Weinberger /* Iterate over all volumes and read their EBA table */
692dbb7d2a8SRichard Weinberger for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
693dbb7d2a8SRichard Weinberger fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
694dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fmvhdr);
695dbb7d2a8SRichard Weinberger if (fm_pos >= fm_size)
696dbb7d2a8SRichard Weinberger goto fail_bad;
697dbb7d2a8SRichard Weinberger
698dbb7d2a8SRichard Weinberger if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
69932608703STanya Brokhman ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x",
700dbb7d2a8SRichard Weinberger be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
701dbb7d2a8SRichard Weinberger goto fail_bad;
702dbb7d2a8SRichard Weinberger }
703dbb7d2a8SRichard Weinberger
704dbb7d2a8SRichard Weinberger av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
705dbb7d2a8SRichard Weinberger be32_to_cpu(fmvhdr->used_ebs),
706dbb7d2a8SRichard Weinberger be32_to_cpu(fmvhdr->data_pad),
707dbb7d2a8SRichard Weinberger fmvhdr->vol_type,
708dbb7d2a8SRichard Weinberger be32_to_cpu(fmvhdr->last_eb_bytes));
709dbb7d2a8SRichard Weinberger
71040b6e61aSBoris Brezillon if (IS_ERR(av)) {
71140b6e61aSBoris Brezillon if (PTR_ERR(av) == -EEXIST)
712e96a8a3bSshengyong ubi_err(ubi, "volume (ID %i) already exists",
713e96a8a3bSshengyong fmvhdr->vol_id);
71440b6e61aSBoris Brezillon
715e96a8a3bSshengyong goto fail_bad;
716e96a8a3bSshengyong }
717dbb7d2a8SRichard Weinberger
718dbb7d2a8SRichard Weinberger ai->vols_found++;
719dbb7d2a8SRichard Weinberger if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
720dbb7d2a8SRichard Weinberger ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
721dbb7d2a8SRichard Weinberger
722dbb7d2a8SRichard Weinberger fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
723dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fm_eba);
724dbb7d2a8SRichard Weinberger fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
725dbb7d2a8SRichard Weinberger if (fm_pos >= fm_size)
726dbb7d2a8SRichard Weinberger goto fail_bad;
727dbb7d2a8SRichard Weinberger
728dbb7d2a8SRichard Weinberger if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
72932608703STanya Brokhman ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x",
730dbb7d2a8SRichard Weinberger be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
731dbb7d2a8SRichard Weinberger goto fail_bad;
732dbb7d2a8SRichard Weinberger }
733dbb7d2a8SRichard Weinberger
734dbb7d2a8SRichard Weinberger for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
735dbb7d2a8SRichard Weinberger int pnum = be32_to_cpu(fm_eba->pnum[j]);
736dbb7d2a8SRichard Weinberger
737876f9a34SRichard Weinberger if (pnum < 0)
738dbb7d2a8SRichard Weinberger continue;
739dbb7d2a8SRichard Weinberger
740dbb7d2a8SRichard Weinberger aeb = NULL;
741dbb7d2a8SRichard Weinberger list_for_each_entry(tmp_aeb, &used, u.list) {
742584d4623SBrian Pomerantz if (tmp_aeb->pnum == pnum) {
743dbb7d2a8SRichard Weinberger aeb = tmp_aeb;
744584d4623SBrian Pomerantz break;
745584d4623SBrian Pomerantz }
746dbb7d2a8SRichard Weinberger }
747dbb7d2a8SRichard Weinberger
748dbb7d2a8SRichard Weinberger if (!aeb) {
749d141a8efSRichard Weinberger ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum);
750d141a8efSRichard Weinberger goto fail_bad;
751dbb7d2a8SRichard Weinberger }
752dbb7d2a8SRichard Weinberger
753dbb7d2a8SRichard Weinberger aeb->lnum = j;
754dbb7d2a8SRichard Weinberger
755dbb7d2a8SRichard Weinberger if (av->highest_lnum <= aeb->lnum)
756dbb7d2a8SRichard Weinberger av->highest_lnum = aeb->lnum;
757dbb7d2a8SRichard Weinberger
758dbb7d2a8SRichard Weinberger assign_aeb_to_av(ai, aeb, av);
759dbb7d2a8SRichard Weinberger
760dbb7d2a8SRichard Weinberger dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
761dbb7d2a8SRichard Weinberger aeb->pnum, aeb->lnum, av->vol_id);
762dbb7d2a8SRichard Weinberger }
763dbb7d2a8SRichard Weinberger }
764dbb7d2a8SRichard Weinberger
765f6e951afSshengyong ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free);
766dbb7d2a8SRichard Weinberger if (ret)
767dbb7d2a8SRichard Weinberger goto fail;
768dbb7d2a8SRichard Weinberger
769f6e951afSshengyong ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free);
770dbb7d2a8SRichard Weinberger if (ret)
771dbb7d2a8SRichard Weinberger goto fail;
772dbb7d2a8SRichard Weinberger
773dbb7d2a8SRichard Weinberger if (max_sqnum > ai->max_sqnum)
774dbb7d2a8SRichard Weinberger ai->max_sqnum = max_sqnum;
775dbb7d2a8SRichard Weinberger
7766a059abdSWei Yongjun list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
7776a059abdSWei Yongjun list_move_tail(&tmp_aeb->u.list, &ai->free);
778dbb7d2a8SRichard Weinberger
779a83832a7SRichard Weinberger list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list)
780a83832a7SRichard Weinberger list_move_tail(&tmp_aeb->u.list, &ai->erase);
781a83832a7SRichard Weinberger
782ae0d1469SRichard Weinberger ubi_assert(list_empty(&free));
783ae0d1469SRichard Weinberger
784dbb7d2a8SRichard Weinberger /*
785dbb7d2a8SRichard Weinberger * If fastmap is leaking PEBs (must not happen), raise a
786dbb7d2a8SRichard Weinberger * fat warning and fall back to scanning mode.
787dbb7d2a8SRichard Weinberger * We do this here because in ubi_wl_init() it's too late
788dbb7d2a8SRichard Weinberger * and we cannot fall back to scanning.
789dbb7d2a8SRichard Weinberger */
790dbb7d2a8SRichard Weinberger if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
791dbb7d2a8SRichard Weinberger ai->bad_peb_count - fm->used_blocks))
792dbb7d2a8SRichard Weinberger goto fail_bad;
793dbb7d2a8SRichard Weinberger
794dbb7d2a8SRichard Weinberger return 0;
795dbb7d2a8SRichard Weinberger
796dbb7d2a8SRichard Weinberger fail_bad:
797dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
798dbb7d2a8SRichard Weinberger fail:
799fe24c6e5SRichard Weinberger list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
800fe24c6e5SRichard Weinberger list_del(&tmp_aeb->u.list);
80191f4285fSBoris Brezillon ubi_free_aeb(ai, tmp_aeb);
802fe24c6e5SRichard Weinberger }
803fe24c6e5SRichard Weinberger list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
804fe24c6e5SRichard Weinberger list_del(&tmp_aeb->u.list);
80591f4285fSBoris Brezillon ubi_free_aeb(ai, tmp_aeb);
806fe24c6e5SRichard Weinberger }
807fe24c6e5SRichard Weinberger
808dbb7d2a8SRichard Weinberger return ret;
809dbb7d2a8SRichard Weinberger }
810dbb7d2a8SRichard Weinberger
811dbb7d2a8SRichard Weinberger /**
812fdf10ed7SRichard Weinberger * find_fm_anchor - find the most recent Fastmap superblock (anchor)
813fdf10ed7SRichard Weinberger * @ai: UBI attach info to be filled
814fdf10ed7SRichard Weinberger */
find_fm_anchor(struct ubi_attach_info * ai)815fdf10ed7SRichard Weinberger static int find_fm_anchor(struct ubi_attach_info *ai)
816fdf10ed7SRichard Weinberger {
817fdf10ed7SRichard Weinberger int ret = -1;
818fdf10ed7SRichard Weinberger struct ubi_ainf_peb *aeb;
819fdf10ed7SRichard Weinberger unsigned long long max_sqnum = 0;
820fdf10ed7SRichard Weinberger
821fdf10ed7SRichard Weinberger list_for_each_entry(aeb, &ai->fastmap, u.list) {
822fdf10ed7SRichard Weinberger if (aeb->vol_id == UBI_FM_SB_VOLUME_ID && aeb->sqnum > max_sqnum) {
823fdf10ed7SRichard Weinberger max_sqnum = aeb->sqnum;
824fdf10ed7SRichard Weinberger ret = aeb->pnum;
825fdf10ed7SRichard Weinberger }
826fdf10ed7SRichard Weinberger }
827fdf10ed7SRichard Weinberger
828fdf10ed7SRichard Weinberger return ret;
829fdf10ed7SRichard Weinberger }
830fdf10ed7SRichard Weinberger
clone_aeb(struct ubi_attach_info * ai,struct ubi_ainf_peb * old)8318a143588SRabin Vincent static struct ubi_ainf_peb *clone_aeb(struct ubi_attach_info *ai,
8328a143588SRabin Vincent struct ubi_ainf_peb *old)
8338a143588SRabin Vincent {
8348a143588SRabin Vincent struct ubi_ainf_peb *new;
8358a143588SRabin Vincent
8368a143588SRabin Vincent new = ubi_alloc_aeb(ai, old->pnum, old->ec);
8378a143588SRabin Vincent if (!new)
8388a143588SRabin Vincent return NULL;
8398a143588SRabin Vincent
8408a143588SRabin Vincent new->vol_id = old->vol_id;
8418a143588SRabin Vincent new->sqnum = old->sqnum;
8428a143588SRabin Vincent new->lnum = old->lnum;
8438a143588SRabin Vincent new->scrub = old->scrub;
8448a143588SRabin Vincent new->copy_flag = old->copy_flag;
8458a143588SRabin Vincent
8468a143588SRabin Vincent return new;
8478a143588SRabin Vincent }
8488a143588SRabin Vincent
849fdf10ed7SRichard Weinberger /**
850dbb7d2a8SRichard Weinberger * ubi_scan_fastmap - scan the fastmap.
851dbb7d2a8SRichard Weinberger * @ubi: UBI device object
852dbb7d2a8SRichard Weinberger * @ai: UBI attach info to be filled
853fdf10ed7SRichard Weinberger * @scan_ai: UBI attach info from the first 64 PEBs,
854fdf10ed7SRichard Weinberger * used to find the most recent Fastmap data structure
855dbb7d2a8SRichard Weinberger *
856dbb7d2a8SRichard Weinberger * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
857dbb7d2a8SRichard Weinberger * UBI_BAD_FASTMAP if one was found but is not usable.
858dbb7d2a8SRichard Weinberger * < 0 indicates an internal error.
859dbb7d2a8SRichard Weinberger */
ubi_scan_fastmap(struct ubi_device * ubi,struct ubi_attach_info * ai,struct ubi_attach_info * scan_ai)860dbb7d2a8SRichard Weinberger int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
861fdf10ed7SRichard Weinberger struct ubi_attach_info *scan_ai)
862dbb7d2a8SRichard Weinberger {
863dbb7d2a8SRichard Weinberger struct ubi_fm_sb *fmsb, *fmsb2;
8643291b52fSBoris Brezillon struct ubi_vid_io_buf *vb;
865dbb7d2a8SRichard Weinberger struct ubi_vid_hdr *vh;
866dbb7d2a8SRichard Weinberger struct ubi_ec_hdr *ech;
867dbb7d2a8SRichard Weinberger struct ubi_fastmap_layout *fm;
8688a143588SRabin Vincent struct ubi_ainf_peb *aeb;
869fdf10ed7SRichard Weinberger int i, used_blocks, pnum, fm_anchor, ret = 0;
870dbb7d2a8SRichard Weinberger size_t fm_size;
871dbb7d2a8SRichard Weinberger __be32 crc, tmp_crc;
872dbb7d2a8SRichard Weinberger unsigned long long sqnum = 0;
873dbb7d2a8SRichard Weinberger
874fdf10ed7SRichard Weinberger fm_anchor = find_fm_anchor(scan_ai);
875fdf10ed7SRichard Weinberger if (fm_anchor < 0)
876fdf10ed7SRichard Weinberger return UBI_NO_FASTMAP;
877fdf10ed7SRichard Weinberger
8788a143588SRabin Vincent /* Copy all (possible) fastmap blocks into our new attach structure. */
8798a143588SRabin Vincent list_for_each_entry(aeb, &scan_ai->fastmap, u.list) {
8808a143588SRabin Vincent struct ubi_ainf_peb *new;
8818a143588SRabin Vincent
8828a143588SRabin Vincent new = clone_aeb(ai, aeb);
8838a143588SRabin Vincent if (!new)
8848a143588SRabin Vincent return -ENOMEM;
8858a143588SRabin Vincent
8868a143588SRabin Vincent list_add(&new->u.list, &ai->fastmap);
8878a143588SRabin Vincent }
888fdf10ed7SRichard Weinberger
889111ab0b2SRichard Weinberger down_write(&ubi->fm_protect);
890dbb7d2a8SRichard Weinberger memset(ubi->fm_buf, 0, ubi->fm_size);
891dbb7d2a8SRichard Weinberger
892dbb7d2a8SRichard Weinberger fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
893dbb7d2a8SRichard Weinberger if (!fmsb) {
894dbb7d2a8SRichard Weinberger ret = -ENOMEM;
895dbb7d2a8SRichard Weinberger goto out;
896dbb7d2a8SRichard Weinberger }
897dbb7d2a8SRichard Weinberger
898dbb7d2a8SRichard Weinberger fm = kzalloc(sizeof(*fm), GFP_KERNEL);
899dbb7d2a8SRichard Weinberger if (!fm) {
900dbb7d2a8SRichard Weinberger ret = -ENOMEM;
901dbb7d2a8SRichard Weinberger kfree(fmsb);
902dbb7d2a8SRichard Weinberger goto out;
903dbb7d2a8SRichard Weinberger }
904dbb7d2a8SRichard Weinberger
905fcbb6af1SBoris Brezillon ret = ubi_io_read_data(ubi, fmsb, fm_anchor, 0, sizeof(*fmsb));
906dbb7d2a8SRichard Weinberger if (ret && ret != UBI_IO_BITFLIPS)
907dbb7d2a8SRichard Weinberger goto free_fm_sb;
908dbb7d2a8SRichard Weinberger else if (ret == UBI_IO_BITFLIPS)
909dbb7d2a8SRichard Weinberger fm->to_be_tortured[0] = 1;
910dbb7d2a8SRichard Weinberger
911dbb7d2a8SRichard Weinberger if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
91232608703STanya Brokhman ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x",
913dbb7d2a8SRichard Weinberger be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
914dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
915dbb7d2a8SRichard Weinberger goto free_fm_sb;
916dbb7d2a8SRichard Weinberger }
917dbb7d2a8SRichard Weinberger
918dbb7d2a8SRichard Weinberger if (fmsb->version != UBI_FM_FMT_VERSION) {
91932608703STanya Brokhman ubi_err(ubi, "bad fastmap version: %i, expected: %i",
920dbb7d2a8SRichard Weinberger fmsb->version, UBI_FM_FMT_VERSION);
921dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
922dbb7d2a8SRichard Weinberger goto free_fm_sb;
923dbb7d2a8SRichard Weinberger }
924dbb7d2a8SRichard Weinberger
925dbb7d2a8SRichard Weinberger used_blocks = be32_to_cpu(fmsb->used_blocks);
926dbb7d2a8SRichard Weinberger if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
92732608703STanya Brokhman ubi_err(ubi, "number of fastmap blocks is invalid: %i",
92832608703STanya Brokhman used_blocks);
929dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
930dbb7d2a8SRichard Weinberger goto free_fm_sb;
931dbb7d2a8SRichard Weinberger }
932dbb7d2a8SRichard Weinberger
933dbb7d2a8SRichard Weinberger fm_size = ubi->leb_size * used_blocks;
934dbb7d2a8SRichard Weinberger if (fm_size != ubi->fm_size) {
93532608703STanya Brokhman ubi_err(ubi, "bad fastmap size: %zi, expected: %zi",
93632608703STanya Brokhman fm_size, ubi->fm_size);
937dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
938dbb7d2a8SRichard Weinberger goto free_fm_sb;
939dbb7d2a8SRichard Weinberger }
940dbb7d2a8SRichard Weinberger
941dbb7d2a8SRichard Weinberger ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
942dbb7d2a8SRichard Weinberger if (!ech) {
943dbb7d2a8SRichard Weinberger ret = -ENOMEM;
944dbb7d2a8SRichard Weinberger goto free_fm_sb;
945dbb7d2a8SRichard Weinberger }
946dbb7d2a8SRichard Weinberger
9473291b52fSBoris Brezillon vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
9483291b52fSBoris Brezillon if (!vb) {
949dbb7d2a8SRichard Weinberger ret = -ENOMEM;
950dbb7d2a8SRichard Weinberger goto free_hdr;
951dbb7d2a8SRichard Weinberger }
952dbb7d2a8SRichard Weinberger
9533291b52fSBoris Brezillon vh = ubi_get_vid_hdr(vb);
9543291b52fSBoris Brezillon
955dbb7d2a8SRichard Weinberger for (i = 0; i < used_blocks; i++) {
956c22301adSRichard Genoud int image_seq;
957c22301adSRichard Genoud
958dbb7d2a8SRichard Weinberger pnum = be32_to_cpu(fmsb->block_loc[i]);
959dbb7d2a8SRichard Weinberger
960dbb7d2a8SRichard Weinberger if (ubi_io_is_bad(ubi, pnum)) {
961dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
962dbb7d2a8SRichard Weinberger goto free_hdr;
963dbb7d2a8SRichard Weinberger }
964dbb7d2a8SRichard Weinberger
9655283ec72SRichard Weinberger if (i == 0 && pnum != fm_anchor) {
9665283ec72SRichard Weinberger ubi_err(ubi, "Fastmap anchor PEB mismatch: PEB: %i vs. %i",
9675283ec72SRichard Weinberger pnum, fm_anchor);
9685283ec72SRichard Weinberger ret = UBI_BAD_FASTMAP;
9695283ec72SRichard Weinberger goto free_hdr;
9705283ec72SRichard Weinberger }
9715283ec72SRichard Weinberger
972dbb7d2a8SRichard Weinberger ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
973dbb7d2a8SRichard Weinberger if (ret && ret != UBI_IO_BITFLIPS) {
97432608703STanya Brokhman ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)",
975dbb7d2a8SRichard Weinberger i, pnum);
976dbb7d2a8SRichard Weinberger if (ret > 0)
977dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
978dbb7d2a8SRichard Weinberger goto free_hdr;
979dbb7d2a8SRichard Weinberger } else if (ret == UBI_IO_BITFLIPS)
980dbb7d2a8SRichard Weinberger fm->to_be_tortured[i] = 1;
981dbb7d2a8SRichard Weinberger
982c22301adSRichard Genoud image_seq = be32_to_cpu(ech->image_seq);
983dbb7d2a8SRichard Weinberger if (!ubi->image_seq)
984c22301adSRichard Genoud ubi->image_seq = image_seq;
985dbb7d2a8SRichard Weinberger
986c22301adSRichard Genoud /*
987c22301adSRichard Genoud * Older UBI implementations have image_seq set to zero, so
988c22301adSRichard Genoud * we shouldn't fail if image_seq == 0.
989c22301adSRichard Genoud */
990c22301adSRichard Genoud if (image_seq && (image_seq != ubi->image_seq)) {
99132608703STanya Brokhman ubi_err(ubi, "wrong image seq:%d instead of %d",
992c22301adSRichard Genoud be32_to_cpu(ech->image_seq), ubi->image_seq);
993dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
994dbb7d2a8SRichard Weinberger goto free_hdr;
995dbb7d2a8SRichard Weinberger }
996dbb7d2a8SRichard Weinberger
9973291b52fSBoris Brezillon ret = ubi_io_read_vid_hdr(ubi, pnum, vb, 0);
998dbb7d2a8SRichard Weinberger if (ret && ret != UBI_IO_BITFLIPS) {
99932608703STanya Brokhman ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)",
1000dbb7d2a8SRichard Weinberger i, pnum);
1001dbb7d2a8SRichard Weinberger goto free_hdr;
1002dbb7d2a8SRichard Weinberger }
1003dbb7d2a8SRichard Weinberger
1004dbb7d2a8SRichard Weinberger if (i == 0) {
1005dbb7d2a8SRichard Weinberger if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
100632608703STanya Brokhman ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x",
1007dbb7d2a8SRichard Weinberger be32_to_cpu(vh->vol_id),
1008dbb7d2a8SRichard Weinberger UBI_FM_SB_VOLUME_ID);
1009dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
1010dbb7d2a8SRichard Weinberger goto free_hdr;
1011dbb7d2a8SRichard Weinberger }
1012dbb7d2a8SRichard Weinberger } else {
1013dbb7d2a8SRichard Weinberger if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
101432608703STanya Brokhman ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x",
1015dbb7d2a8SRichard Weinberger be32_to_cpu(vh->vol_id),
1016dbb7d2a8SRichard Weinberger UBI_FM_DATA_VOLUME_ID);
1017dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
1018dbb7d2a8SRichard Weinberger goto free_hdr;
1019dbb7d2a8SRichard Weinberger }
1020dbb7d2a8SRichard Weinberger }
1021dbb7d2a8SRichard Weinberger
1022dbb7d2a8SRichard Weinberger if (sqnum < be64_to_cpu(vh->sqnum))
1023dbb7d2a8SRichard Weinberger sqnum = be64_to_cpu(vh->sqnum);
1024dbb7d2a8SRichard Weinberger
1025fcbb6af1SBoris Brezillon ret = ubi_io_read_data(ubi, ubi->fm_buf + (ubi->leb_size * i),
1026fcbb6af1SBoris Brezillon pnum, 0, ubi->leb_size);
1027dbb7d2a8SRichard Weinberger if (ret && ret != UBI_IO_BITFLIPS) {
102832608703STanya Brokhman ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, "
1029dbb7d2a8SRichard Weinberger "err: %i)", i, pnum, ret);
1030dbb7d2a8SRichard Weinberger goto free_hdr;
1031dbb7d2a8SRichard Weinberger }
1032dbb7d2a8SRichard Weinberger }
1033dbb7d2a8SRichard Weinberger
1034dbb7d2a8SRichard Weinberger kfree(fmsb);
1035dbb7d2a8SRichard Weinberger fmsb = NULL;
1036dbb7d2a8SRichard Weinberger
1037dbb7d2a8SRichard Weinberger fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
1038dbb7d2a8SRichard Weinberger tmp_crc = be32_to_cpu(fmsb2->data_crc);
1039dbb7d2a8SRichard Weinberger fmsb2->data_crc = 0;
1040dbb7d2a8SRichard Weinberger crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
1041dbb7d2a8SRichard Weinberger if (crc != tmp_crc) {
104232608703STanya Brokhman ubi_err(ubi, "fastmap data CRC is invalid");
104332608703STanya Brokhman ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x",
104432608703STanya Brokhman tmp_crc, crc);
1045dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
1046dbb7d2a8SRichard Weinberger goto free_hdr;
1047dbb7d2a8SRichard Weinberger }
1048dbb7d2a8SRichard Weinberger
1049dbb7d2a8SRichard Weinberger fmsb2->sqnum = sqnum;
1050dbb7d2a8SRichard Weinberger
1051dbb7d2a8SRichard Weinberger fm->used_blocks = used_blocks;
1052dbb7d2a8SRichard Weinberger
1053dbb7d2a8SRichard Weinberger ret = ubi_attach_fastmap(ubi, ai, fm);
1054dbb7d2a8SRichard Weinberger if (ret) {
1055dbb7d2a8SRichard Weinberger if (ret > 0)
1056dbb7d2a8SRichard Weinberger ret = UBI_BAD_FASTMAP;
1057dbb7d2a8SRichard Weinberger goto free_hdr;
1058dbb7d2a8SRichard Weinberger }
1059dbb7d2a8SRichard Weinberger
1060dbb7d2a8SRichard Weinberger for (i = 0; i < used_blocks; i++) {
1061dbb7d2a8SRichard Weinberger struct ubi_wl_entry *e;
1062dbb7d2a8SRichard Weinberger
1063dbb7d2a8SRichard Weinberger e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1064dbb7d2a8SRichard Weinberger if (!e) {
1065dbb7d2a8SRichard Weinberger while (i--)
1066af7bcee2SPan Bian kmem_cache_free(ubi_wl_entry_slab, fm->e[i]);
1067dbb7d2a8SRichard Weinberger
1068dbb7d2a8SRichard Weinberger ret = -ENOMEM;
1069dbb7d2a8SRichard Weinberger goto free_hdr;
1070dbb7d2a8SRichard Weinberger }
1071dbb7d2a8SRichard Weinberger
1072dbb7d2a8SRichard Weinberger e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
1073dbb7d2a8SRichard Weinberger e->ec = be32_to_cpu(fmsb2->block_ec[i]);
1074dbb7d2a8SRichard Weinberger fm->e[i] = e;
1075dbb7d2a8SRichard Weinberger }
1076dbb7d2a8SRichard Weinberger
1077dbb7d2a8SRichard Weinberger ubi->fm = fm;
1078dbb7d2a8SRichard Weinberger ubi->fm_pool.max_size = ubi->fm->max_pool_size;
1079dbb7d2a8SRichard Weinberger ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
108032608703STanya Brokhman ubi_msg(ubi, "attached by fastmap");
108132608703STanya Brokhman ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size);
108232608703STanya Brokhman ubi_msg(ubi, "fastmap WL pool size: %d",
108332608703STanya Brokhman ubi->fm_wl_pool.max_size);
1084dbb7d2a8SRichard Weinberger ubi->fm_disabled = 0;
10851900149cSRichard Weinberger ubi->fast_attach = 1;
1086dbb7d2a8SRichard Weinberger
10873291b52fSBoris Brezillon ubi_free_vid_buf(vb);
1088dbb7d2a8SRichard Weinberger kfree(ech);
1089dbb7d2a8SRichard Weinberger out:
1090111ab0b2SRichard Weinberger up_write(&ubi->fm_protect);
1091dbb7d2a8SRichard Weinberger if (ret == UBI_BAD_FASTMAP)
109232608703STanya Brokhman ubi_err(ubi, "Attach by fastmap failed, doing a full scan!");
1093dbb7d2a8SRichard Weinberger return ret;
1094dbb7d2a8SRichard Weinberger
1095dbb7d2a8SRichard Weinberger free_hdr:
10963291b52fSBoris Brezillon ubi_free_vid_buf(vb);
1097dbb7d2a8SRichard Weinberger kfree(ech);
1098dbb7d2a8SRichard Weinberger free_fm_sb:
1099dbb7d2a8SRichard Weinberger kfree(fmsb);
1100dbb7d2a8SRichard Weinberger kfree(fm);
1101dbb7d2a8SRichard Weinberger goto out;
1102dbb7d2a8SRichard Weinberger }
1103dbb7d2a8SRichard Weinberger
ubi_fastmap_init_checkmap(struct ubi_volume * vol,int leb_count)110434653fd8SRichard Weinberger int ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count)
110534653fd8SRichard Weinberger {
110634653fd8SRichard Weinberger struct ubi_device *ubi = vol->ubi;
110734653fd8SRichard Weinberger
110834653fd8SRichard Weinberger if (!ubi->fast_attach)
110934653fd8SRichard Weinberger return 0;
111034653fd8SRichard Weinberger
1111e7f35da2SChristophe JAILLET vol->checkmap = bitmap_zalloc(leb_count, GFP_KERNEL);
111234653fd8SRichard Weinberger if (!vol->checkmap)
111334653fd8SRichard Weinberger return -ENOMEM;
111434653fd8SRichard Weinberger
111534653fd8SRichard Weinberger return 0;
111634653fd8SRichard Weinberger }
111734653fd8SRichard Weinberger
ubi_fastmap_destroy_checkmap(struct ubi_volume * vol)111834653fd8SRichard Weinberger void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol)
111934653fd8SRichard Weinberger {
1120e7f35da2SChristophe JAILLET bitmap_free(vol->checkmap);
112134653fd8SRichard Weinberger }
112234653fd8SRichard Weinberger
1123dbb7d2a8SRichard Weinberger /**
1124dbb7d2a8SRichard Weinberger * ubi_write_fastmap - writes a fastmap.
1125dbb7d2a8SRichard Weinberger * @ubi: UBI device object
1126dbb7d2a8SRichard Weinberger * @new_fm: the to be written fastmap
1127dbb7d2a8SRichard Weinberger *
1128dbb7d2a8SRichard Weinberger * Returns 0 on success, < 0 indicates an internal error.
1129dbb7d2a8SRichard Weinberger */
ubi_write_fastmap(struct ubi_device * ubi,struct ubi_fastmap_layout * new_fm)1130dbb7d2a8SRichard Weinberger static int ubi_write_fastmap(struct ubi_device *ubi,
1131dbb7d2a8SRichard Weinberger struct ubi_fastmap_layout *new_fm)
1132dbb7d2a8SRichard Weinberger {
1133dbb7d2a8SRichard Weinberger size_t fm_pos = 0;
1134dbb7d2a8SRichard Weinberger void *fm_raw;
1135dbb7d2a8SRichard Weinberger struct ubi_fm_sb *fmsb;
1136dbb7d2a8SRichard Weinberger struct ubi_fm_hdr *fmh;
1137f6e951afSshengyong struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
1138dbb7d2a8SRichard Weinberger struct ubi_fm_ec *fec;
1139dbb7d2a8SRichard Weinberger struct ubi_fm_volhdr *fvh;
1140dbb7d2a8SRichard Weinberger struct ubi_fm_eba *feba;
1141dbb7d2a8SRichard Weinberger struct ubi_wl_entry *wl_e;
1142dbb7d2a8SRichard Weinberger struct ubi_volume *vol;
11433291b52fSBoris Brezillon struct ubi_vid_io_buf *avbuf, *dvbuf;
1144dbb7d2a8SRichard Weinberger struct ubi_vid_hdr *avhdr, *dvhdr;
1145dbb7d2a8SRichard Weinberger struct ubi_work *ubi_wrk;
1146c5c3f3cfSRichard Weinberger struct rb_node *tmp_rb;
1147dbb7d2a8SRichard Weinberger int ret, i, j, free_peb_count, used_peb_count, vol_count;
1148dbb7d2a8SRichard Weinberger int scrub_peb_count, erase_peb_count;
11495d3805afSDan Carpenter unsigned long *seen_pebs;
1150dbb7d2a8SRichard Weinberger
1151dbb7d2a8SRichard Weinberger fm_raw = ubi->fm_buf;
1152dbb7d2a8SRichard Weinberger memset(ubi->fm_buf, 0, ubi->fm_size);
1153dbb7d2a8SRichard Weinberger
11543291b52fSBoris Brezillon avbuf = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID);
11553291b52fSBoris Brezillon if (!avbuf) {
1156dbb7d2a8SRichard Weinberger ret = -ENOMEM;
1157dbb7d2a8SRichard Weinberger goto out;
1158dbb7d2a8SRichard Weinberger }
1159dbb7d2a8SRichard Weinberger
11603291b52fSBoris Brezillon dvbuf = new_fm_vbuf(ubi, UBI_FM_DATA_VOLUME_ID);
11613291b52fSBoris Brezillon if (!dvbuf) {
1162dbb7d2a8SRichard Weinberger ret = -ENOMEM;
11635d3805afSDan Carpenter goto out_free_avbuf;
1164dbb7d2a8SRichard Weinberger }
1165dbb7d2a8SRichard Weinberger
11663291b52fSBoris Brezillon avhdr = ubi_get_vid_hdr(avbuf);
11673291b52fSBoris Brezillon dvhdr = ubi_get_vid_hdr(dvbuf);
11683291b52fSBoris Brezillon
1169daef3dd1SRichard Weinberger seen_pebs = init_seen(ubi);
1170daef3dd1SRichard Weinberger if (IS_ERR(seen_pebs)) {
1171daef3dd1SRichard Weinberger ret = PTR_ERR(seen_pebs);
11725d3805afSDan Carpenter goto out_free_dvbuf;
1173daef3dd1SRichard Weinberger }
1174daef3dd1SRichard Weinberger
1175dbb7d2a8SRichard Weinberger spin_lock(&ubi->volumes_lock);
1176dbb7d2a8SRichard Weinberger spin_lock(&ubi->wl_lock);
1177dbb7d2a8SRichard Weinberger
1178dbb7d2a8SRichard Weinberger fmsb = (struct ubi_fm_sb *)fm_raw;
1179dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fmsb);
1180dbb7d2a8SRichard Weinberger ubi_assert(fm_pos <= ubi->fm_size);
1181dbb7d2a8SRichard Weinberger
1182dbb7d2a8SRichard Weinberger fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
1183dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fmh);
1184dbb7d2a8SRichard Weinberger ubi_assert(fm_pos <= ubi->fm_size);
1185dbb7d2a8SRichard Weinberger
1186dbb7d2a8SRichard Weinberger fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
1187dbb7d2a8SRichard Weinberger fmsb->version = UBI_FM_FMT_VERSION;
1188dbb7d2a8SRichard Weinberger fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
1189dbb7d2a8SRichard Weinberger /* the max sqnum will be filled in while *reading* the fastmap */
1190dbb7d2a8SRichard Weinberger fmsb->sqnum = 0;
1191dbb7d2a8SRichard Weinberger
1192dbb7d2a8SRichard Weinberger fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
1193dbb7d2a8SRichard Weinberger free_peb_count = 0;
1194dbb7d2a8SRichard Weinberger used_peb_count = 0;
1195dbb7d2a8SRichard Weinberger scrub_peb_count = 0;
1196dbb7d2a8SRichard Weinberger erase_peb_count = 0;
1197dbb7d2a8SRichard Weinberger vol_count = 0;
1198dbb7d2a8SRichard Weinberger
1199f6e951afSshengyong fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1200f6e951afSshengyong fm_pos += sizeof(*fmpl);
1201f6e951afSshengyong fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1202f6e951afSshengyong fmpl->size = cpu_to_be16(ubi->fm_pool.size);
1203f6e951afSshengyong fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size);
1204dbb7d2a8SRichard Weinberger
1205daef3dd1SRichard Weinberger for (i = 0; i < ubi->fm_pool.size; i++) {
1206f6e951afSshengyong fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
1207daef3dd1SRichard Weinberger set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs);
1208daef3dd1SRichard Weinberger }
1209dbb7d2a8SRichard Weinberger
1210f6e951afSshengyong fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1211f6e951afSshengyong fm_pos += sizeof(*fmpl_wl);
1212f6e951afSshengyong fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1213f6e951afSshengyong fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size);
1214f6e951afSshengyong fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
1215dbb7d2a8SRichard Weinberger
1216daef3dd1SRichard Weinberger for (i = 0; i < ubi->fm_wl_pool.size; i++) {
1217f6e951afSshengyong fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
1218daef3dd1SRichard Weinberger set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs);
1219daef3dd1SRichard Weinberger }
1220dbb7d2a8SRichard Weinberger
1221c5c3f3cfSRichard Weinberger ubi_for_each_free_peb(ubi, wl_e, tmp_rb) {
1222dbb7d2a8SRichard Weinberger fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1223dbb7d2a8SRichard Weinberger
1224dbb7d2a8SRichard Weinberger fec->pnum = cpu_to_be32(wl_e->pnum);
1225daef3dd1SRichard Weinberger set_seen(ubi, wl_e->pnum, seen_pebs);
1226dbb7d2a8SRichard Weinberger fec->ec = cpu_to_be32(wl_e->ec);
1227dbb7d2a8SRichard Weinberger
1228dbb7d2a8SRichard Weinberger free_peb_count++;
1229dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fec);
1230dbb7d2a8SRichard Weinberger ubi_assert(fm_pos <= ubi->fm_size);
1231dbb7d2a8SRichard Weinberger }
1232dbb7d2a8SRichard Weinberger fmh->free_peb_count = cpu_to_be32(free_peb_count);
1233dbb7d2a8SRichard Weinberger
1234c5c3f3cfSRichard Weinberger ubi_for_each_used_peb(ubi, wl_e, tmp_rb) {
1235dbb7d2a8SRichard Weinberger fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1236dbb7d2a8SRichard Weinberger
1237dbb7d2a8SRichard Weinberger fec->pnum = cpu_to_be32(wl_e->pnum);
1238daef3dd1SRichard Weinberger set_seen(ubi, wl_e->pnum, seen_pebs);
1239dbb7d2a8SRichard Weinberger fec->ec = cpu_to_be32(wl_e->ec);
1240dbb7d2a8SRichard Weinberger
1241dbb7d2a8SRichard Weinberger used_peb_count++;
1242dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fec);
1243dbb7d2a8SRichard Weinberger ubi_assert(fm_pos <= ubi->fm_size);
1244dbb7d2a8SRichard Weinberger }
12454f5e3b6fSRichard Weinberger
1246c5c3f3cfSRichard Weinberger ubi_for_each_protected_peb(ubi, i, wl_e) {
12474f5e3b6fSRichard Weinberger fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
12484f5e3b6fSRichard Weinberger
12494f5e3b6fSRichard Weinberger fec->pnum = cpu_to_be32(wl_e->pnum);
1250c5c3f3cfSRichard Weinberger set_seen(ubi, wl_e->pnum, seen_pebs);
12514f5e3b6fSRichard Weinberger fec->ec = cpu_to_be32(wl_e->ec);
12524f5e3b6fSRichard Weinberger
12534f5e3b6fSRichard Weinberger used_peb_count++;
12544f5e3b6fSRichard Weinberger fm_pos += sizeof(*fec);
12554f5e3b6fSRichard Weinberger ubi_assert(fm_pos <= ubi->fm_size);
12564f5e3b6fSRichard Weinberger }
1257dbb7d2a8SRichard Weinberger fmh->used_peb_count = cpu_to_be32(used_peb_count);
1258dbb7d2a8SRichard Weinberger
1259c5c3f3cfSRichard Weinberger ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) {
1260dbb7d2a8SRichard Weinberger fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1261dbb7d2a8SRichard Weinberger
1262dbb7d2a8SRichard Weinberger fec->pnum = cpu_to_be32(wl_e->pnum);
1263daef3dd1SRichard Weinberger set_seen(ubi, wl_e->pnum, seen_pebs);
1264dbb7d2a8SRichard Weinberger fec->ec = cpu_to_be32(wl_e->ec);
1265dbb7d2a8SRichard Weinberger
1266dbb7d2a8SRichard Weinberger scrub_peb_count++;
1267dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fec);
1268dbb7d2a8SRichard Weinberger ubi_assert(fm_pos <= ubi->fm_size);
1269dbb7d2a8SRichard Weinberger }
1270dbb7d2a8SRichard Weinberger fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
1271dbb7d2a8SRichard Weinberger
1272dbb7d2a8SRichard Weinberger
1273dbb7d2a8SRichard Weinberger list_for_each_entry(ubi_wrk, &ubi->works, list) {
1274dbb7d2a8SRichard Weinberger if (ubi_is_erase_work(ubi_wrk)) {
1275dbb7d2a8SRichard Weinberger wl_e = ubi_wrk->e;
1276dbb7d2a8SRichard Weinberger ubi_assert(wl_e);
1277dbb7d2a8SRichard Weinberger
1278dbb7d2a8SRichard Weinberger fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1279dbb7d2a8SRichard Weinberger
1280dbb7d2a8SRichard Weinberger fec->pnum = cpu_to_be32(wl_e->pnum);
1281daef3dd1SRichard Weinberger set_seen(ubi, wl_e->pnum, seen_pebs);
1282dbb7d2a8SRichard Weinberger fec->ec = cpu_to_be32(wl_e->ec);
1283dbb7d2a8SRichard Weinberger
1284dbb7d2a8SRichard Weinberger erase_peb_count++;
1285dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fec);
1286dbb7d2a8SRichard Weinberger ubi_assert(fm_pos <= ubi->fm_size);
1287dbb7d2a8SRichard Weinberger }
1288dbb7d2a8SRichard Weinberger }
1289dbb7d2a8SRichard Weinberger fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
1290dbb7d2a8SRichard Weinberger
1291dbb7d2a8SRichard Weinberger for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
1292dbb7d2a8SRichard Weinberger vol = ubi->volumes[i];
1293dbb7d2a8SRichard Weinberger
1294dbb7d2a8SRichard Weinberger if (!vol)
1295dbb7d2a8SRichard Weinberger continue;
1296dbb7d2a8SRichard Weinberger
1297dbb7d2a8SRichard Weinberger vol_count++;
1298dbb7d2a8SRichard Weinberger
1299dbb7d2a8SRichard Weinberger fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
1300dbb7d2a8SRichard Weinberger fm_pos += sizeof(*fvh);
1301dbb7d2a8SRichard Weinberger ubi_assert(fm_pos <= ubi->fm_size);
1302dbb7d2a8SRichard Weinberger
1303dbb7d2a8SRichard Weinberger fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
1304dbb7d2a8SRichard Weinberger fvh->vol_id = cpu_to_be32(vol->vol_id);
1305dbb7d2a8SRichard Weinberger fvh->vol_type = vol->vol_type;
1306dbb7d2a8SRichard Weinberger fvh->used_ebs = cpu_to_be32(vol->used_ebs);
1307dbb7d2a8SRichard Weinberger fvh->data_pad = cpu_to_be32(vol->data_pad);
1308dbb7d2a8SRichard Weinberger fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
1309dbb7d2a8SRichard Weinberger
1310dbb7d2a8SRichard Weinberger ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
1311dbb7d2a8SRichard Weinberger vol->vol_type == UBI_STATIC_VOLUME);
1312dbb7d2a8SRichard Weinberger
1313dbb7d2a8SRichard Weinberger feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
1314dbb7d2a8SRichard Weinberger fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
1315dbb7d2a8SRichard Weinberger ubi_assert(fm_pos <= ubi->fm_size);
1316dbb7d2a8SRichard Weinberger
13171f81a5ccSBoris Brezillon for (j = 0; j < vol->reserved_pebs; j++) {
13181f81a5ccSBoris Brezillon struct ubi_eba_leb_desc ldesc;
13191f81a5ccSBoris Brezillon
13201f81a5ccSBoris Brezillon ubi_eba_get_ldesc(vol, j, &ldesc);
13211f81a5ccSBoris Brezillon feba->pnum[j] = cpu_to_be32(ldesc.pnum);
13221f81a5ccSBoris Brezillon }
1323dbb7d2a8SRichard Weinberger
1324dbb7d2a8SRichard Weinberger feba->reserved_pebs = cpu_to_be32(j);
1325dbb7d2a8SRichard Weinberger feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
1326dbb7d2a8SRichard Weinberger }
1327dbb7d2a8SRichard Weinberger fmh->vol_count = cpu_to_be32(vol_count);
1328dbb7d2a8SRichard Weinberger fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
1329dbb7d2a8SRichard Weinberger
1330dbb7d2a8SRichard Weinberger avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1331dbb7d2a8SRichard Weinberger avhdr->lnum = 0;
1332dbb7d2a8SRichard Weinberger
1333dbb7d2a8SRichard Weinberger spin_unlock(&ubi->wl_lock);
1334dbb7d2a8SRichard Weinberger spin_unlock(&ubi->volumes_lock);
1335dbb7d2a8SRichard Weinberger
1336dbb7d2a8SRichard Weinberger dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
13373291b52fSBoris Brezillon ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avbuf);
1338dbb7d2a8SRichard Weinberger if (ret) {
133932608703STanya Brokhman ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
13405d3805afSDan Carpenter goto out_free_seen;
1341dbb7d2a8SRichard Weinberger }
1342dbb7d2a8SRichard Weinberger
1343dbb7d2a8SRichard Weinberger for (i = 0; i < new_fm->used_blocks; i++) {
1344dbb7d2a8SRichard Weinberger fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
1345daef3dd1SRichard Weinberger set_seen(ubi, new_fm->e[i]->pnum, seen_pebs);
1346dbb7d2a8SRichard Weinberger fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
1347dbb7d2a8SRichard Weinberger }
1348dbb7d2a8SRichard Weinberger
1349dbb7d2a8SRichard Weinberger fmsb->data_crc = 0;
1350dbb7d2a8SRichard Weinberger fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
1351dbb7d2a8SRichard Weinberger ubi->fm_size));
1352dbb7d2a8SRichard Weinberger
1353dbb7d2a8SRichard Weinberger for (i = 1; i < new_fm->used_blocks; i++) {
1354dbb7d2a8SRichard Weinberger dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1355dbb7d2a8SRichard Weinberger dvhdr->lnum = cpu_to_be32(i);
1356dbb7d2a8SRichard Weinberger dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1357dbb7d2a8SRichard Weinberger new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
13583291b52fSBoris Brezillon ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvbuf);
1359dbb7d2a8SRichard Weinberger if (ret) {
136032608703STanya Brokhman ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
1361dbb7d2a8SRichard Weinberger new_fm->e[i]->pnum);
13625d3805afSDan Carpenter goto out_free_seen;
1363dbb7d2a8SRichard Weinberger }
1364dbb7d2a8SRichard Weinberger }
1365dbb7d2a8SRichard Weinberger
1366dbb7d2a8SRichard Weinberger for (i = 0; i < new_fm->used_blocks; i++) {
1367fcbb6af1SBoris Brezillon ret = ubi_io_write_data(ubi, fm_raw + (i * ubi->leb_size),
1368fcbb6af1SBoris Brezillon new_fm->e[i]->pnum, 0, ubi->leb_size);
1369dbb7d2a8SRichard Weinberger if (ret) {
137032608703STanya Brokhman ubi_err(ubi, "unable to write fastmap to PEB %i!",
1371dbb7d2a8SRichard Weinberger new_fm->e[i]->pnum);
13725d3805afSDan Carpenter goto out_free_seen;
1373dbb7d2a8SRichard Weinberger }
1374dbb7d2a8SRichard Weinberger }
1375dbb7d2a8SRichard Weinberger
1376dbb7d2a8SRichard Weinberger ubi_assert(new_fm);
1377dbb7d2a8SRichard Weinberger ubi->fm = new_fm;
1378dbb7d2a8SRichard Weinberger
1379daef3dd1SRichard Weinberger ret = self_check_seen(ubi, seen_pebs);
1380dbb7d2a8SRichard Weinberger dbg_bld("fastmap written!");
1381dbb7d2a8SRichard Weinberger
13825d3805afSDan Carpenter out_free_seen:
1383daef3dd1SRichard Weinberger free_seen(seen_pebs);
13845d3805afSDan Carpenter out_free_dvbuf:
13855d3805afSDan Carpenter ubi_free_vid_buf(dvbuf);
13865d3805afSDan Carpenter out_free_avbuf:
13875d3805afSDan Carpenter ubi_free_vid_buf(avbuf);
13885d3805afSDan Carpenter
1389dbb7d2a8SRichard Weinberger out:
1390dbb7d2a8SRichard Weinberger return ret;
1391dbb7d2a8SRichard Weinberger }
1392dbb7d2a8SRichard Weinberger
1393dbb7d2a8SRichard Weinberger /**
1394dbb7d2a8SRichard Weinberger * erase_block - Manually erase a PEB.
1395dbb7d2a8SRichard Weinberger * @ubi: UBI device object
1396dbb7d2a8SRichard Weinberger * @pnum: PEB to be erased
1397dbb7d2a8SRichard Weinberger *
1398dbb7d2a8SRichard Weinberger * Returns the new EC value on success, < 0 indicates an internal error.
1399dbb7d2a8SRichard Weinberger */
erase_block(struct ubi_device * ubi,int pnum)1400dbb7d2a8SRichard Weinberger static int erase_block(struct ubi_device *ubi, int pnum)
1401dbb7d2a8SRichard Weinberger {
1402dbb7d2a8SRichard Weinberger int ret;
1403dbb7d2a8SRichard Weinberger struct ubi_ec_hdr *ec_hdr;
1404dbb7d2a8SRichard Weinberger long long ec;
1405dbb7d2a8SRichard Weinberger
1406dbb7d2a8SRichard Weinberger ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1407dbb7d2a8SRichard Weinberger if (!ec_hdr)
1408dbb7d2a8SRichard Weinberger return -ENOMEM;
1409dbb7d2a8SRichard Weinberger
1410dbb7d2a8SRichard Weinberger ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1411dbb7d2a8SRichard Weinberger if (ret < 0)
1412dbb7d2a8SRichard Weinberger goto out;
1413dbb7d2a8SRichard Weinberger else if (ret && ret != UBI_IO_BITFLIPS) {
1414dbb7d2a8SRichard Weinberger ret = -EINVAL;
1415dbb7d2a8SRichard Weinberger goto out;
1416dbb7d2a8SRichard Weinberger }
1417dbb7d2a8SRichard Weinberger
1418dbb7d2a8SRichard Weinberger ret = ubi_io_sync_erase(ubi, pnum, 0);
1419dbb7d2a8SRichard Weinberger if (ret < 0)
1420dbb7d2a8SRichard Weinberger goto out;
1421dbb7d2a8SRichard Weinberger
1422dbb7d2a8SRichard Weinberger ec = be64_to_cpu(ec_hdr->ec);
1423dbb7d2a8SRichard Weinberger ec += ret;
1424dbb7d2a8SRichard Weinberger if (ec > UBI_MAX_ERASECOUNTER) {
1425dbb7d2a8SRichard Weinberger ret = -EINVAL;
1426dbb7d2a8SRichard Weinberger goto out;
1427dbb7d2a8SRichard Weinberger }
1428dbb7d2a8SRichard Weinberger
1429dbb7d2a8SRichard Weinberger ec_hdr->ec = cpu_to_be64(ec);
1430dbb7d2a8SRichard Weinberger ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
1431dbb7d2a8SRichard Weinberger if (ret < 0)
1432dbb7d2a8SRichard Weinberger goto out;
1433dbb7d2a8SRichard Weinberger
1434dbb7d2a8SRichard Weinberger ret = ec;
1435dbb7d2a8SRichard Weinberger out:
1436dbb7d2a8SRichard Weinberger kfree(ec_hdr);
1437dbb7d2a8SRichard Weinberger return ret;
1438dbb7d2a8SRichard Weinberger }
1439dbb7d2a8SRichard Weinberger
1440dbb7d2a8SRichard Weinberger /**
1441dbb7d2a8SRichard Weinberger * invalidate_fastmap - destroys a fastmap.
1442dbb7d2a8SRichard Weinberger * @ubi: UBI device object
1443dbb7d2a8SRichard Weinberger *
14445ca97ad8SRichard Weinberger * This function ensures that upon next UBI attach a full scan
14455ca97ad8SRichard Weinberger * is issued. We need this if UBI is about to write a new fastmap
14465ca97ad8SRichard Weinberger * but is unable to do so. In this case we have two options:
14475ca97ad8SRichard Weinberger * a) Make sure that the current fastmap will not be usued upon
14485ca97ad8SRichard Weinberger * attach time and contine or b) fall back to RO mode to have the
14495ca97ad8SRichard Weinberger * current fastmap in a valid state.
1450dbb7d2a8SRichard Weinberger * Returns 0 on success, < 0 indicates an internal error.
1451dbb7d2a8SRichard Weinberger */
invalidate_fastmap(struct ubi_device * ubi)14525ca97ad8SRichard Weinberger static int invalidate_fastmap(struct ubi_device *ubi)
1453dbb7d2a8SRichard Weinberger {
14548930fa50SRichard Weinberger int ret;
14555ca97ad8SRichard Weinberger struct ubi_fastmap_layout *fm;
14565ca97ad8SRichard Weinberger struct ubi_wl_entry *e;
14573291b52fSBoris Brezillon struct ubi_vid_io_buf *vb = NULL;
14583291b52fSBoris Brezillon struct ubi_vid_hdr *vh;
1459dbb7d2a8SRichard Weinberger
14605ca97ad8SRichard Weinberger if (!ubi->fm)
14615ca97ad8SRichard Weinberger return 0;
14625ca97ad8SRichard Weinberger
14635ca97ad8SRichard Weinberger ubi->fm = NULL;
14645ca97ad8SRichard Weinberger
14655ca97ad8SRichard Weinberger ret = -ENOMEM;
14665ca97ad8SRichard Weinberger fm = kzalloc(sizeof(*fm), GFP_KERNEL);
14675ca97ad8SRichard Weinberger if (!fm)
14685ca97ad8SRichard Weinberger goto out;
1469dbb7d2a8SRichard Weinberger
14703291b52fSBoris Brezillon vb = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID);
14713291b52fSBoris Brezillon if (!vb)
14725ca97ad8SRichard Weinberger goto out_free_fm;
1473dbb7d2a8SRichard Weinberger
14743291b52fSBoris Brezillon vh = ubi_get_vid_hdr(vb);
14753291b52fSBoris Brezillon
14765ca97ad8SRichard Weinberger ret = -ENOSPC;
14775ca97ad8SRichard Weinberger e = ubi_wl_get_fm_peb(ubi, 1);
14785ca97ad8SRichard Weinberger if (!e)
14795ca97ad8SRichard Weinberger goto out_free_fm;
14805ca97ad8SRichard Weinberger
14815ca97ad8SRichard Weinberger /*
14825ca97ad8SRichard Weinberger * Create fake fastmap such that UBI will fall back
14835ca97ad8SRichard Weinberger * to scanning mode.
14845ca97ad8SRichard Weinberger */
1485dbb7d2a8SRichard Weinberger vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
14863291b52fSBoris Brezillon ret = ubi_io_write_vid_hdr(ubi, e->pnum, vb);
14875ca97ad8SRichard Weinberger if (ret < 0) {
14885ca97ad8SRichard Weinberger ubi_wl_put_fm_peb(ubi, e, 0, 0);
14895ca97ad8SRichard Weinberger goto out_free_fm;
14905ca97ad8SRichard Weinberger }
1491dbb7d2a8SRichard Weinberger
14925ca97ad8SRichard Weinberger fm->used_blocks = 1;
14935ca97ad8SRichard Weinberger fm->e[0] = e;
14945ca97ad8SRichard Weinberger
14955ca97ad8SRichard Weinberger ubi->fm = fm;
14965ca97ad8SRichard Weinberger
14975ca97ad8SRichard Weinberger out:
14983291b52fSBoris Brezillon ubi_free_vid_buf(vb);
1499dbb7d2a8SRichard Weinberger return ret;
15005ca97ad8SRichard Weinberger
15015ca97ad8SRichard Weinberger out_free_fm:
15025ca97ad8SRichard Weinberger kfree(fm);
15035ca97ad8SRichard Weinberger goto out;
15045ca97ad8SRichard Weinberger }
15055ca97ad8SRichard Weinberger
15065ca97ad8SRichard Weinberger /**
15075ca97ad8SRichard Weinberger * return_fm_pebs - returns all PEBs used by a fastmap back to the
15085ca97ad8SRichard Weinberger * WL sub-system.
15095ca97ad8SRichard Weinberger * @ubi: UBI device object
15105ca97ad8SRichard Weinberger * @fm: fastmap layout object
15115ca97ad8SRichard Weinberger */
return_fm_pebs(struct ubi_device * ubi,struct ubi_fastmap_layout * fm)15125ca97ad8SRichard Weinberger static void return_fm_pebs(struct ubi_device *ubi,
15135ca97ad8SRichard Weinberger struct ubi_fastmap_layout *fm)
15145ca97ad8SRichard Weinberger {
15155ca97ad8SRichard Weinberger int i;
15165ca97ad8SRichard Weinberger
15175ca97ad8SRichard Weinberger if (!fm)
15185ca97ad8SRichard Weinberger return;
15195ca97ad8SRichard Weinberger
15205ca97ad8SRichard Weinberger for (i = 0; i < fm->used_blocks; i++) {
15215ca97ad8SRichard Weinberger if (fm->e[i]) {
15225ca97ad8SRichard Weinberger ubi_wl_put_fm_peb(ubi, fm->e[i], i,
15235ca97ad8SRichard Weinberger fm->to_be_tortured[i]);
15245ca97ad8SRichard Weinberger fm->e[i] = NULL;
15255ca97ad8SRichard Weinberger }
15265ca97ad8SRichard Weinberger }
1527dbb7d2a8SRichard Weinberger }
1528dbb7d2a8SRichard Weinberger
1529dbb7d2a8SRichard Weinberger /**
1530dbb7d2a8SRichard Weinberger * ubi_update_fastmap - will be called by UBI if a volume changes or
1531dbb7d2a8SRichard Weinberger * a fastmap pool becomes full.
1532dbb7d2a8SRichard Weinberger * @ubi: UBI device object
1533dbb7d2a8SRichard Weinberger *
1534dbb7d2a8SRichard Weinberger * Returns 0 on success, < 0 indicates an internal error.
1535dbb7d2a8SRichard Weinberger */
ubi_update_fastmap(struct ubi_device * ubi)1536dbb7d2a8SRichard Weinberger int ubi_update_fastmap(struct ubi_device *ubi)
1537dbb7d2a8SRichard Weinberger {
15385ca97ad8SRichard Weinberger int ret, i, j;
1539dbb7d2a8SRichard Weinberger struct ubi_fastmap_layout *new_fm, *old_fm;
1540dbb7d2a8SRichard Weinberger struct ubi_wl_entry *tmp_e;
1541dbb7d2a8SRichard Weinberger
1542111ab0b2SRichard Weinberger down_write(&ubi->fm_protect);
15432e8f08deSRichard Weinberger down_write(&ubi->work_sem);
15442e8f08deSRichard Weinberger down_write(&ubi->fm_eba_sem);
1545dbb7d2a8SRichard Weinberger
1546dbb7d2a8SRichard Weinberger ubi_refill_pools(ubi);
1547dbb7d2a8SRichard Weinberger
1548dbb7d2a8SRichard Weinberger if (ubi->ro_mode || ubi->fm_disabled) {
15492e8f08deSRichard Weinberger up_write(&ubi->fm_eba_sem);
15502e8f08deSRichard Weinberger up_write(&ubi->work_sem);
1551111ab0b2SRichard Weinberger up_write(&ubi->fm_protect);
1552dbb7d2a8SRichard Weinberger return 0;
1553dbb7d2a8SRichard Weinberger }
1554dbb7d2a8SRichard Weinberger
1555dbb7d2a8SRichard Weinberger new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
1556dbb7d2a8SRichard Weinberger if (!new_fm) {
15572e8f08deSRichard Weinberger up_write(&ubi->fm_eba_sem);
15582e8f08deSRichard Weinberger up_write(&ubi->work_sem);
1559111ab0b2SRichard Weinberger up_write(&ubi->fm_protect);
1560dbb7d2a8SRichard Weinberger return -ENOMEM;
1561dbb7d2a8SRichard Weinberger }
1562dbb7d2a8SRichard Weinberger
1563dbb7d2a8SRichard Weinberger new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
1564dbb7d2a8SRichard Weinberger old_fm = ubi->fm;
1565dbb7d2a8SRichard Weinberger ubi->fm = NULL;
1566dbb7d2a8SRichard Weinberger
1567dbb7d2a8SRichard Weinberger if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
156832608703STanya Brokhman ubi_err(ubi, "fastmap too large");
1569dbb7d2a8SRichard Weinberger ret = -ENOSPC;
1570dbb7d2a8SRichard Weinberger goto err;
1571dbb7d2a8SRichard Weinberger }
1572dbb7d2a8SRichard Weinberger
1573dbb7d2a8SRichard Weinberger for (i = 1; i < new_fm->used_blocks; i++) {
1574dbb7d2a8SRichard Weinberger spin_lock(&ubi->wl_lock);
1575dbb7d2a8SRichard Weinberger tmp_e = ubi_wl_get_fm_peb(ubi, 0);
1576dbb7d2a8SRichard Weinberger spin_unlock(&ubi->wl_lock);
1577dbb7d2a8SRichard Weinberger
15785ca97ad8SRichard Weinberger if (!tmp_e) {
15795ca97ad8SRichard Weinberger if (old_fm && old_fm->e[i]) {
1580dbb7d2a8SRichard Weinberger ret = erase_block(ubi, old_fm->e[i]->pnum);
1581dbb7d2a8SRichard Weinberger if (ret < 0) {
15825ca97ad8SRichard Weinberger ubi_err(ubi, "could not erase old fastmap PEB");
1583dbb7d2a8SRichard Weinberger
15845ca97ad8SRichard Weinberger for (j = 1; j < i; j++) {
1585dbb7d2a8SRichard Weinberger ubi_wl_put_fm_peb(ubi, new_fm->e[j],
1586dbb7d2a8SRichard Weinberger j, 0);
15875ca97ad8SRichard Weinberger new_fm->e[j] = NULL;
15885ca97ad8SRichard Weinberger }
1589dbb7d2a8SRichard Weinberger goto err;
1590dbb7d2a8SRichard Weinberger }
1591c4ca6be9SRichard Weinberger new_fm->e[i] = old_fm->e[i];
15925ca97ad8SRichard Weinberger old_fm->e[i] = NULL;
15935ca97ad8SRichard Weinberger } else {
15945ca97ad8SRichard Weinberger ubi_err(ubi, "could not get any free erase block");
15955ca97ad8SRichard Weinberger
15965ca97ad8SRichard Weinberger for (j = 1; j < i; j++) {
15975ca97ad8SRichard Weinberger ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
15985ca97ad8SRichard Weinberger new_fm->e[j] = NULL;
15995ca97ad8SRichard Weinberger }
16005ca97ad8SRichard Weinberger
16015ca97ad8SRichard Weinberger ret = -ENOSPC;
16025ca97ad8SRichard Weinberger goto err;
16035ca97ad8SRichard Weinberger }
1604dbb7d2a8SRichard Weinberger } else {
1605c4ca6be9SRichard Weinberger new_fm->e[i] = tmp_e;
1606dbb7d2a8SRichard Weinberger
16075ca97ad8SRichard Weinberger if (old_fm && old_fm->e[i]) {
160861de74ceSRichard Weinberger ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
160961de74ceSRichard Weinberger old_fm->to_be_tortured[i]);
16105ca97ad8SRichard Weinberger old_fm->e[i] = NULL;
16115ca97ad8SRichard Weinberger }
161261de74ceSRichard Weinberger }
161361de74ceSRichard Weinberger }
161461de74ceSRichard Weinberger
161561de74ceSRichard Weinberger /* Old fastmap is larger than the new one */
161661de74ceSRichard Weinberger if (old_fm && new_fm->used_blocks < old_fm->used_blocks) {
161761de74ceSRichard Weinberger for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) {
1618dbb7d2a8SRichard Weinberger ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1619dbb7d2a8SRichard Weinberger old_fm->to_be_tortured[i]);
16205ca97ad8SRichard Weinberger old_fm->e[i] = NULL;
1621dbb7d2a8SRichard Weinberger }
1622dbb7d2a8SRichard Weinberger }
1623dbb7d2a8SRichard Weinberger
1624dbb7d2a8SRichard Weinberger spin_lock(&ubi->wl_lock);
1625f9c34bb5SSascha Hauer tmp_e = ubi->fm_anchor;
1626f9c34bb5SSascha Hauer ubi->fm_anchor = NULL;
1627dbb7d2a8SRichard Weinberger spin_unlock(&ubi->wl_lock);
1628dbb7d2a8SRichard Weinberger
1629dbb7d2a8SRichard Weinberger if (old_fm) {
1630dbb7d2a8SRichard Weinberger /* no fresh anchor PEB was found, reuse the old one */
1631dbb7d2a8SRichard Weinberger if (!tmp_e) {
1632dbb7d2a8SRichard Weinberger ret = erase_block(ubi, old_fm->e[0]->pnum);
1633dbb7d2a8SRichard Weinberger if (ret < 0) {
163432608703STanya Brokhman ubi_err(ubi, "could not erase old anchor PEB");
1635dbb7d2a8SRichard Weinberger
16365ca97ad8SRichard Weinberger for (i = 1; i < new_fm->used_blocks; i++) {
1637dbb7d2a8SRichard Weinberger ubi_wl_put_fm_peb(ubi, new_fm->e[i],
1638dbb7d2a8SRichard Weinberger i, 0);
16395ca97ad8SRichard Weinberger new_fm->e[i] = NULL;
16405ca97ad8SRichard Weinberger }
1641dbb7d2a8SRichard Weinberger goto err;
1642dbb7d2a8SRichard Weinberger }
1643c4ca6be9SRichard Weinberger new_fm->e[0] = old_fm->e[0];
1644dbb7d2a8SRichard Weinberger new_fm->e[0]->ec = ret;
16455ca97ad8SRichard Weinberger old_fm->e[0] = NULL;
1646dbb7d2a8SRichard Weinberger } else {
1647dbb7d2a8SRichard Weinberger /* we've got a new anchor PEB, return the old one */
1648dbb7d2a8SRichard Weinberger ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
1649dbb7d2a8SRichard Weinberger old_fm->to_be_tortured[0]);
1650c4ca6be9SRichard Weinberger new_fm->e[0] = tmp_e;
16515ca97ad8SRichard Weinberger old_fm->e[0] = NULL;
1652dbb7d2a8SRichard Weinberger }
1653dbb7d2a8SRichard Weinberger } else {
1654dbb7d2a8SRichard Weinberger if (!tmp_e) {
165532608703STanya Brokhman ubi_err(ubi, "could not find any anchor PEB");
1656dbb7d2a8SRichard Weinberger
16575ca97ad8SRichard Weinberger for (i = 1; i < new_fm->used_blocks; i++) {
1658dbb7d2a8SRichard Weinberger ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
16595ca97ad8SRichard Weinberger new_fm->e[i] = NULL;
16605ca97ad8SRichard Weinberger }
1661dbb7d2a8SRichard Weinberger
1662dbb7d2a8SRichard Weinberger ret = -ENOSPC;
1663dbb7d2a8SRichard Weinberger goto err;
1664dbb7d2a8SRichard Weinberger }
1665c4ca6be9SRichard Weinberger new_fm->e[0] = tmp_e;
1666dbb7d2a8SRichard Weinberger }
1667dbb7d2a8SRichard Weinberger
1668dbb7d2a8SRichard Weinberger ret = ubi_write_fastmap(ubi, new_fm);
1669dbb7d2a8SRichard Weinberger
1670dbb7d2a8SRichard Weinberger if (ret)
1671dbb7d2a8SRichard Weinberger goto err;
1672dbb7d2a8SRichard Weinberger
1673dbb7d2a8SRichard Weinberger out_unlock:
16742e8f08deSRichard Weinberger up_write(&ubi->fm_eba_sem);
16752e8f08deSRichard Weinberger up_write(&ubi->work_sem);
1676111ab0b2SRichard Weinberger up_write(&ubi->fm_protect);
1677dbb7d2a8SRichard Weinberger kfree(old_fm);
1678f9c34bb5SSascha Hauer
1679f9c34bb5SSascha Hauer ubi_ensure_anchor_pebs(ubi);
1680f9c34bb5SSascha Hauer
1681dbb7d2a8SRichard Weinberger return ret;
1682dbb7d2a8SRichard Weinberger
1683dbb7d2a8SRichard Weinberger err:
168432608703STanya Brokhman ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret);
1685dbb7d2a8SRichard Weinberger
16865ca97ad8SRichard Weinberger ret = invalidate_fastmap(ubi);
16872d93fb36SRichard Weinberger if (ret < 0) {
1688d2e43d19SColin Ian King ubi_err(ubi, "Unable to invalidate current fastmap!");
16892d93fb36SRichard Weinberger ubi_ro_mode(ubi);
16905ca97ad8SRichard Weinberger } else {
16915ca97ad8SRichard Weinberger return_fm_pebs(ubi, old_fm);
16925ca97ad8SRichard Weinberger return_fm_pebs(ubi, new_fm);
1693dbb7d2a8SRichard Weinberger ret = 0;
1694dbb7d2a8SRichard Weinberger }
16955ca97ad8SRichard Weinberger
16965ca97ad8SRichard Weinberger kfree(new_fm);
1697dbb7d2a8SRichard Weinberger goto out_unlock;
1698dbb7d2a8SRichard Weinberger }
1699