xref: /openbmc/u-boot/drivers/mtd/ubi/fastmap.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2ff94bc40SHeiko Schocher /*
3ff94bc40SHeiko Schocher  * Copyright (c) 2012 Linutronix GmbH
40195a7bbSHeiko Schocher  * Copyright (c) 2014 sigma star gmbh
5ff94bc40SHeiko Schocher  * Author: Richard Weinberger <richard@nod.at>
6ff94bc40SHeiko Schocher  *
7ff94bc40SHeiko Schocher  */
8ff94bc40SHeiko Schocher 
9ff94bc40SHeiko Schocher #ifndef __UBOOT__
10ff94bc40SHeiko Schocher #include <linux/crc32.h>
11ff94bc40SHeiko Schocher #else
12ff94bc40SHeiko Schocher #include <div64.h>
13ff94bc40SHeiko Schocher #include <malloc.h>
14ff94bc40SHeiko Schocher #include <ubi_uboot.h>
15ff94bc40SHeiko Schocher #endif
16ff94bc40SHeiko Schocher 
17ff94bc40SHeiko Schocher #include <linux/compat.h>
18ff94bc40SHeiko Schocher #include <linux/math64.h>
19ff94bc40SHeiko Schocher #include "ubi.h"
20ff94bc40SHeiko Schocher 
21ff94bc40SHeiko Schocher /**
220195a7bbSHeiko Schocher  * init_seen - allocate memory for used for debugging.
230195a7bbSHeiko Schocher  * @ubi: UBI device description object
240195a7bbSHeiko Schocher  */
init_seen(struct ubi_device * ubi)250195a7bbSHeiko Schocher static inline int *init_seen(struct ubi_device *ubi)
260195a7bbSHeiko Schocher {
270195a7bbSHeiko Schocher 	int *ret;
280195a7bbSHeiko Schocher 
290195a7bbSHeiko Schocher 	if (!ubi_dbg_chk_fastmap(ubi))
300195a7bbSHeiko Schocher 		return NULL;
310195a7bbSHeiko Schocher 
320195a7bbSHeiko Schocher 	ret = kcalloc(ubi->peb_count, sizeof(int), GFP_KERNEL);
330195a7bbSHeiko Schocher 	if (!ret)
340195a7bbSHeiko Schocher 		return ERR_PTR(-ENOMEM);
350195a7bbSHeiko Schocher 
360195a7bbSHeiko Schocher 	return ret;
370195a7bbSHeiko Schocher }
380195a7bbSHeiko Schocher 
390195a7bbSHeiko Schocher /**
400195a7bbSHeiko Schocher  * free_seen - free the seen logic integer array.
410195a7bbSHeiko Schocher  * @seen: integer array of @ubi->peb_count size
420195a7bbSHeiko Schocher  */
free_seen(int * seen)430195a7bbSHeiko Schocher static inline void free_seen(int *seen)
440195a7bbSHeiko Schocher {
450195a7bbSHeiko Schocher 	kfree(seen);
460195a7bbSHeiko Schocher }
470195a7bbSHeiko Schocher 
480195a7bbSHeiko Schocher /**
490195a7bbSHeiko Schocher  * set_seen - mark a PEB as seen.
500195a7bbSHeiko Schocher  * @ubi: UBI device description object
510195a7bbSHeiko Schocher  * @pnum: The PEB to be makred as seen
520195a7bbSHeiko Schocher  * @seen: integer array of @ubi->peb_count size
530195a7bbSHeiko Schocher  */
set_seen(struct ubi_device * ubi,int pnum,int * seen)540195a7bbSHeiko Schocher static inline void set_seen(struct ubi_device *ubi, int pnum, int *seen)
550195a7bbSHeiko Schocher {
560195a7bbSHeiko Schocher 	if (!ubi_dbg_chk_fastmap(ubi) || !seen)
570195a7bbSHeiko Schocher 		return;
580195a7bbSHeiko Schocher 
590195a7bbSHeiko Schocher 	seen[pnum] = 1;
600195a7bbSHeiko Schocher }
610195a7bbSHeiko Schocher 
620195a7bbSHeiko Schocher /**
630195a7bbSHeiko Schocher  * self_check_seen - check whether all PEB have been seen by fastmap.
640195a7bbSHeiko Schocher  * @ubi: UBI device description object
650195a7bbSHeiko Schocher  * @seen: integer array of @ubi->peb_count size
660195a7bbSHeiko Schocher  */
self_check_seen(struct ubi_device * ubi,int * seen)670195a7bbSHeiko Schocher static int self_check_seen(struct ubi_device *ubi, int *seen)
680195a7bbSHeiko Schocher {
690195a7bbSHeiko Schocher 	int pnum, ret = 0;
700195a7bbSHeiko Schocher 
710195a7bbSHeiko Schocher 	if (!ubi_dbg_chk_fastmap(ubi) || !seen)
720195a7bbSHeiko Schocher 		return 0;
730195a7bbSHeiko Schocher 
740195a7bbSHeiko Schocher 	for (pnum = 0; pnum < ubi->peb_count; pnum++) {
750195a7bbSHeiko Schocher 		if (!seen[pnum] && ubi->lookuptbl[pnum]) {
760195a7bbSHeiko Schocher 			ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum);
770195a7bbSHeiko Schocher 			ret = -EINVAL;
780195a7bbSHeiko Schocher 		}
790195a7bbSHeiko Schocher 	}
800195a7bbSHeiko Schocher 
810195a7bbSHeiko Schocher 	return ret;
820195a7bbSHeiko Schocher }
830195a7bbSHeiko Schocher 
840195a7bbSHeiko Schocher /**
85ff94bc40SHeiko Schocher  * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
86ff94bc40SHeiko Schocher  * @ubi: UBI device description object
87ff94bc40SHeiko Schocher  */
ubi_calc_fm_size(struct ubi_device * ubi)88ff94bc40SHeiko Schocher size_t ubi_calc_fm_size(struct ubi_device *ubi)
89ff94bc40SHeiko Schocher {
90ff94bc40SHeiko Schocher 	size_t size;
91ff94bc40SHeiko Schocher 
920195a7bbSHeiko Schocher 	size = sizeof(struct ubi_fm_sb) +
930195a7bbSHeiko Schocher 		sizeof(struct ubi_fm_hdr) +
940195a7bbSHeiko Schocher 		sizeof(struct ubi_fm_scan_pool) +
950195a7bbSHeiko Schocher 		sizeof(struct ubi_fm_scan_pool) +
960195a7bbSHeiko Schocher 		(ubi->peb_count * sizeof(struct ubi_fm_ec)) +
970195a7bbSHeiko Schocher 		(sizeof(struct ubi_fm_eba) +
980195a7bbSHeiko Schocher 		(ubi->peb_count * sizeof(__be32))) +
99ff94bc40SHeiko Schocher 		sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
100ff94bc40SHeiko Schocher 	return roundup(size, ubi->leb_size);
101ff94bc40SHeiko Schocher }
102ff94bc40SHeiko Schocher 
103ff94bc40SHeiko Schocher 
104ff94bc40SHeiko Schocher /**
105ff94bc40SHeiko Schocher  * new_fm_vhdr - allocate a new volume header for fastmap usage.
106ff94bc40SHeiko Schocher  * @ubi: UBI device description object
107ff94bc40SHeiko Schocher  * @vol_id: the VID of the new header
108ff94bc40SHeiko Schocher  *
109ff94bc40SHeiko Schocher  * Returns a new struct ubi_vid_hdr on success.
110ff94bc40SHeiko Schocher  * NULL indicates out of memory.
111ff94bc40SHeiko Schocher  */
new_fm_vhdr(struct ubi_device * ubi,int vol_id)112ff94bc40SHeiko Schocher static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id)
113ff94bc40SHeiko Schocher {
114ff94bc40SHeiko Schocher 	struct ubi_vid_hdr *new;
115ff94bc40SHeiko Schocher 
116ff94bc40SHeiko Schocher 	new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
117ff94bc40SHeiko Schocher 	if (!new)
118ff94bc40SHeiko Schocher 		goto out;
119ff94bc40SHeiko Schocher 
120ff94bc40SHeiko Schocher 	new->vol_type = UBI_VID_DYNAMIC;
121ff94bc40SHeiko Schocher 	new->vol_id = cpu_to_be32(vol_id);
122ff94bc40SHeiko Schocher 
123ff94bc40SHeiko Schocher 	/* UBI implementations without fastmap support have to delete the
124ff94bc40SHeiko Schocher 	 * fastmap.
125ff94bc40SHeiko Schocher 	 */
126ff94bc40SHeiko Schocher 	new->compat = UBI_COMPAT_DELETE;
127ff94bc40SHeiko Schocher 
128ff94bc40SHeiko Schocher out:
129ff94bc40SHeiko Schocher 	return new;
130ff94bc40SHeiko Schocher }
131ff94bc40SHeiko Schocher 
132ff94bc40SHeiko Schocher /**
133ff94bc40SHeiko Schocher  * add_aeb - create and add a attach erase block to a given list.
134ff94bc40SHeiko Schocher  * @ai: UBI attach info object
135ff94bc40SHeiko Schocher  * @list: the target list
136ff94bc40SHeiko Schocher  * @pnum: PEB number of the new attach erase block
137ff94bc40SHeiko Schocher  * @ec: erease counter of the new LEB
138ff94bc40SHeiko Schocher  * @scrub: scrub this PEB after attaching
139ff94bc40SHeiko Schocher  *
140ff94bc40SHeiko Schocher  * Returns 0 on success, < 0 indicates an internal error.
141ff94bc40SHeiko Schocher  */
add_aeb(struct ubi_attach_info * ai,struct list_head * list,int pnum,int ec,int scrub)142ff94bc40SHeiko Schocher static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
143ff94bc40SHeiko Schocher 		   int pnum, int ec, int scrub)
144ff94bc40SHeiko Schocher {
145ff94bc40SHeiko Schocher 	struct ubi_ainf_peb *aeb;
146ff94bc40SHeiko Schocher 
147ff94bc40SHeiko Schocher 	aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
148ff94bc40SHeiko Schocher 	if (!aeb)
149ff94bc40SHeiko Schocher 		return -ENOMEM;
150ff94bc40SHeiko Schocher 
151ff94bc40SHeiko Schocher 	aeb->pnum = pnum;
152ff94bc40SHeiko Schocher 	aeb->ec = ec;
153ff94bc40SHeiko Schocher 	aeb->lnum = -1;
154ff94bc40SHeiko Schocher 	aeb->scrub = scrub;
155ff94bc40SHeiko Schocher 	aeb->copy_flag = aeb->sqnum = 0;
156ff94bc40SHeiko Schocher 
157ff94bc40SHeiko Schocher 	ai->ec_sum += aeb->ec;
158ff94bc40SHeiko Schocher 	ai->ec_count++;
159ff94bc40SHeiko Schocher 
160ff94bc40SHeiko Schocher 	if (ai->max_ec < aeb->ec)
161ff94bc40SHeiko Schocher 		ai->max_ec = aeb->ec;
162ff94bc40SHeiko Schocher 
163ff94bc40SHeiko Schocher 	if (ai->min_ec > aeb->ec)
164ff94bc40SHeiko Schocher 		ai->min_ec = aeb->ec;
165ff94bc40SHeiko Schocher 
166ff94bc40SHeiko Schocher 	list_add_tail(&aeb->u.list, list);
167ff94bc40SHeiko Schocher 
168ff94bc40SHeiko Schocher 	return 0;
169ff94bc40SHeiko Schocher }
170ff94bc40SHeiko Schocher 
171ff94bc40SHeiko Schocher /**
172ff94bc40SHeiko Schocher  * add_vol - create and add a new volume to ubi_attach_info.
173ff94bc40SHeiko Schocher  * @ai: ubi_attach_info object
174ff94bc40SHeiko Schocher  * @vol_id: VID of the new volume
175ff94bc40SHeiko Schocher  * @used_ebs: number of used EBS
176ff94bc40SHeiko Schocher  * @data_pad: data padding value of the new volume
177ff94bc40SHeiko Schocher  * @vol_type: volume type
178ff94bc40SHeiko Schocher  * @last_eb_bytes: number of bytes in the last LEB
179ff94bc40SHeiko Schocher  *
180ff94bc40SHeiko Schocher  * Returns the new struct ubi_ainf_volume on success.
181ff94bc40SHeiko Schocher  * NULL indicates an error.
182ff94bc40SHeiko Schocher  */
add_vol(struct ubi_attach_info * ai,int vol_id,int used_ebs,int data_pad,u8 vol_type,int last_eb_bytes)183ff94bc40SHeiko Schocher static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id,
184ff94bc40SHeiko Schocher 				       int used_ebs, int data_pad, u8 vol_type,
185ff94bc40SHeiko Schocher 				       int last_eb_bytes)
186ff94bc40SHeiko Schocher {
187ff94bc40SHeiko Schocher 	struct ubi_ainf_volume *av;
188ff94bc40SHeiko Schocher 	struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
189ff94bc40SHeiko Schocher 
190ff94bc40SHeiko Schocher 	while (*p) {
191ff94bc40SHeiko Schocher 		parent = *p;
192ff94bc40SHeiko Schocher 		av = rb_entry(parent, struct ubi_ainf_volume, rb);
193ff94bc40SHeiko Schocher 
194ff94bc40SHeiko Schocher 		if (vol_id > av->vol_id)
195ff94bc40SHeiko Schocher 			p = &(*p)->rb_left;
1960195a7bbSHeiko Schocher 		else if (vol_id < av->vol_id)
197ff94bc40SHeiko Schocher 			p = &(*p)->rb_right;
1980195a7bbSHeiko Schocher 		else
1990195a7bbSHeiko Schocher 			return ERR_PTR(-EINVAL);
200ff94bc40SHeiko Schocher 	}
201ff94bc40SHeiko Schocher 
202ff94bc40SHeiko Schocher 	av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL);
203ff94bc40SHeiko Schocher 	if (!av)
204ff94bc40SHeiko Schocher 		goto out;
205ff94bc40SHeiko Schocher 
2060195a7bbSHeiko Schocher 	av->highest_lnum = av->leb_count = av->used_ebs = 0;
207ff94bc40SHeiko Schocher 	av->vol_id = vol_id;
208ff94bc40SHeiko Schocher 	av->data_pad = data_pad;
209ff94bc40SHeiko Schocher 	av->last_data_size = last_eb_bytes;
210ff94bc40SHeiko Schocher 	av->compat = 0;
211ff94bc40SHeiko Schocher 	av->vol_type = vol_type;
212ff94bc40SHeiko Schocher 	av->root = RB_ROOT;
2130195a7bbSHeiko Schocher 	if (av->vol_type == UBI_STATIC_VOLUME)
2140195a7bbSHeiko Schocher 		av->used_ebs = used_ebs;
215ff94bc40SHeiko Schocher 
216ff94bc40SHeiko Schocher 	dbg_bld("found volume (ID %i)", vol_id);
217ff94bc40SHeiko Schocher 
218ff94bc40SHeiko Schocher 	rb_link_node(&av->rb, parent, p);
219ff94bc40SHeiko Schocher 	rb_insert_color(&av->rb, &ai->volumes);
220ff94bc40SHeiko Schocher 
221ff94bc40SHeiko Schocher out:
222ff94bc40SHeiko Schocher 	return av;
223ff94bc40SHeiko Schocher }
224ff94bc40SHeiko Schocher 
225ff94bc40SHeiko Schocher /**
226ff94bc40SHeiko Schocher  * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
227ff94bc40SHeiko Schocher  * from it's original list.
228ff94bc40SHeiko Schocher  * @ai: ubi_attach_info object
229ff94bc40SHeiko Schocher  * @aeb: the to be assigned SEB
230ff94bc40SHeiko Schocher  * @av: target scan volume
231ff94bc40SHeiko Schocher  */
assign_aeb_to_av(struct ubi_attach_info * ai,struct ubi_ainf_peb * aeb,struct ubi_ainf_volume * av)232ff94bc40SHeiko Schocher static void assign_aeb_to_av(struct ubi_attach_info *ai,
233ff94bc40SHeiko Schocher 			     struct ubi_ainf_peb *aeb,
234ff94bc40SHeiko Schocher 			     struct ubi_ainf_volume *av)
235ff94bc40SHeiko Schocher {
236ff94bc40SHeiko Schocher 	struct ubi_ainf_peb *tmp_aeb;
237ff94bc40SHeiko Schocher 	struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
238ff94bc40SHeiko Schocher 
239ff94bc40SHeiko Schocher 	p = &av->root.rb_node;
240ff94bc40SHeiko Schocher 	while (*p) {
241ff94bc40SHeiko Schocher 		parent = *p;
242ff94bc40SHeiko Schocher 
243ff94bc40SHeiko Schocher 		tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
244ff94bc40SHeiko Schocher 		if (aeb->lnum != tmp_aeb->lnum) {
245ff94bc40SHeiko Schocher 			if (aeb->lnum < tmp_aeb->lnum)
246ff94bc40SHeiko Schocher 				p = &(*p)->rb_left;
247ff94bc40SHeiko Schocher 			else
248ff94bc40SHeiko Schocher 				p = &(*p)->rb_right;
249ff94bc40SHeiko Schocher 
250ff94bc40SHeiko Schocher 			continue;
251ff94bc40SHeiko Schocher 		} else
252ff94bc40SHeiko Schocher 			break;
253ff94bc40SHeiko Schocher 	}
254ff94bc40SHeiko Schocher 
255ff94bc40SHeiko Schocher 	list_del(&aeb->u.list);
256ff94bc40SHeiko Schocher 	av->leb_count++;
257ff94bc40SHeiko Schocher 
258ff94bc40SHeiko Schocher 	rb_link_node(&aeb->u.rb, parent, p);
259ff94bc40SHeiko Schocher 	rb_insert_color(&aeb->u.rb, &av->root);
260ff94bc40SHeiko Schocher }
261ff94bc40SHeiko Schocher 
262ff94bc40SHeiko Schocher /**
263ff94bc40SHeiko Schocher  * update_vol - inserts or updates a LEB which was found a pool.
264ff94bc40SHeiko Schocher  * @ubi: the UBI device object
265ff94bc40SHeiko Schocher  * @ai: attach info object
266ff94bc40SHeiko Schocher  * @av: the volume this LEB belongs to
267ff94bc40SHeiko Schocher  * @new_vh: the volume header derived from new_aeb
268ff94bc40SHeiko Schocher  * @new_aeb: the AEB to be examined
269ff94bc40SHeiko Schocher  *
270ff94bc40SHeiko Schocher  * Returns 0 on success, < 0 indicates an internal error.
271ff94bc40SHeiko Schocher  */
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)272ff94bc40SHeiko Schocher static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
273ff94bc40SHeiko Schocher 		      struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh,
274ff94bc40SHeiko Schocher 		      struct ubi_ainf_peb *new_aeb)
275ff94bc40SHeiko Schocher {
276ff94bc40SHeiko Schocher 	struct rb_node **p = &av->root.rb_node, *parent = NULL;
277ff94bc40SHeiko Schocher 	struct ubi_ainf_peb *aeb, *victim;
278ff94bc40SHeiko Schocher 	int cmp_res;
279ff94bc40SHeiko Schocher 
280ff94bc40SHeiko Schocher 	while (*p) {
281ff94bc40SHeiko Schocher 		parent = *p;
282ff94bc40SHeiko Schocher 		aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
283ff94bc40SHeiko Schocher 
284ff94bc40SHeiko Schocher 		if (be32_to_cpu(new_vh->lnum) != aeb->lnum) {
285ff94bc40SHeiko Schocher 			if (be32_to_cpu(new_vh->lnum) < aeb->lnum)
286ff94bc40SHeiko Schocher 				p = &(*p)->rb_left;
287ff94bc40SHeiko Schocher 			else
288ff94bc40SHeiko Schocher 				p = &(*p)->rb_right;
289ff94bc40SHeiko Schocher 
290ff94bc40SHeiko Schocher 			continue;
291ff94bc40SHeiko Schocher 		}
292ff94bc40SHeiko Schocher 
293ff94bc40SHeiko Schocher 		/* This case can happen if the fastmap gets written
294ff94bc40SHeiko Schocher 		 * because of a volume change (creation, deletion, ..).
295ff94bc40SHeiko Schocher 		 * Then a PEB can be within the persistent EBA and the pool.
296ff94bc40SHeiko Schocher 		 */
297ff94bc40SHeiko Schocher 		if (aeb->pnum == new_aeb->pnum) {
298ff94bc40SHeiko Schocher 			ubi_assert(aeb->lnum == new_aeb->lnum);
299ff94bc40SHeiko Schocher 			kmem_cache_free(ai->aeb_slab_cache, new_aeb);
300ff94bc40SHeiko Schocher 
301ff94bc40SHeiko Schocher 			return 0;
302ff94bc40SHeiko Schocher 		}
303ff94bc40SHeiko Schocher 
304ff94bc40SHeiko Schocher 		cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh);
305ff94bc40SHeiko Schocher 		if (cmp_res < 0)
306ff94bc40SHeiko Schocher 			return cmp_res;
307ff94bc40SHeiko Schocher 
308ff94bc40SHeiko Schocher 		/* new_aeb is newer */
309ff94bc40SHeiko Schocher 		if (cmp_res & 1) {
310ff94bc40SHeiko Schocher 			victim = kmem_cache_alloc(ai->aeb_slab_cache,
311ff94bc40SHeiko Schocher 				GFP_KERNEL);
312ff94bc40SHeiko Schocher 			if (!victim)
313ff94bc40SHeiko Schocher 				return -ENOMEM;
314ff94bc40SHeiko Schocher 
315ff94bc40SHeiko Schocher 			victim->ec = aeb->ec;
316ff94bc40SHeiko Schocher 			victim->pnum = aeb->pnum;
317ff94bc40SHeiko Schocher 			list_add_tail(&victim->u.list, &ai->erase);
318ff94bc40SHeiko Schocher 
319ff94bc40SHeiko Schocher 			if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
3200195a7bbSHeiko Schocher 				av->last_data_size =
321ff94bc40SHeiko Schocher 					be32_to_cpu(new_vh->data_size);
322ff94bc40SHeiko Schocher 
323ff94bc40SHeiko Schocher 			dbg_bld("vol %i: AEB %i's PEB %i is the newer",
324ff94bc40SHeiko Schocher 				av->vol_id, aeb->lnum, new_aeb->pnum);
325ff94bc40SHeiko Schocher 
326ff94bc40SHeiko Schocher 			aeb->ec = new_aeb->ec;
327ff94bc40SHeiko Schocher 			aeb->pnum = new_aeb->pnum;
328ff94bc40SHeiko Schocher 			aeb->copy_flag = new_vh->copy_flag;
329ff94bc40SHeiko Schocher 			aeb->scrub = new_aeb->scrub;
330ff94bc40SHeiko Schocher 			kmem_cache_free(ai->aeb_slab_cache, new_aeb);
331ff94bc40SHeiko Schocher 
332ff94bc40SHeiko Schocher 		/* new_aeb is older */
333ff94bc40SHeiko Schocher 		} else {
334ff94bc40SHeiko Schocher 			dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
335ff94bc40SHeiko Schocher 				av->vol_id, aeb->lnum, new_aeb->pnum);
336ff94bc40SHeiko Schocher 			list_add_tail(&new_aeb->u.list, &ai->erase);
337ff94bc40SHeiko Schocher 		}
338ff94bc40SHeiko Schocher 
339ff94bc40SHeiko Schocher 		return 0;
340ff94bc40SHeiko Schocher 	}
341ff94bc40SHeiko Schocher 	/* This LEB is new, let's add it to the volume */
342ff94bc40SHeiko Schocher 
343ff94bc40SHeiko Schocher 	if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
344ff94bc40SHeiko Schocher 		av->highest_lnum = be32_to_cpu(new_vh->lnum);
345ff94bc40SHeiko Schocher 		av->last_data_size = be32_to_cpu(new_vh->data_size);
346ff94bc40SHeiko Schocher 	}
347ff94bc40SHeiko Schocher 
348ff94bc40SHeiko Schocher 	if (av->vol_type == UBI_STATIC_VOLUME)
349ff94bc40SHeiko Schocher 		av->used_ebs = be32_to_cpu(new_vh->used_ebs);
350ff94bc40SHeiko Schocher 
351ff94bc40SHeiko Schocher 	av->leb_count++;
352ff94bc40SHeiko Schocher 
353ff94bc40SHeiko Schocher 	rb_link_node(&new_aeb->u.rb, parent, p);
354ff94bc40SHeiko Schocher 	rb_insert_color(&new_aeb->u.rb, &av->root);
355ff94bc40SHeiko Schocher 
356ff94bc40SHeiko Schocher 	return 0;
357ff94bc40SHeiko Schocher }
358ff94bc40SHeiko Schocher 
359ff94bc40SHeiko Schocher /**
360ff94bc40SHeiko Schocher  * process_pool_aeb - we found a non-empty PEB in a pool.
361ff94bc40SHeiko Schocher  * @ubi: UBI device object
362ff94bc40SHeiko Schocher  * @ai: attach info object
363ff94bc40SHeiko Schocher  * @new_vh: the volume header derived from new_aeb
364ff94bc40SHeiko Schocher  * @new_aeb: the AEB to be examined
365ff94bc40SHeiko Schocher  *
366ff94bc40SHeiko Schocher  * Returns 0 on success, < 0 indicates an internal error.
367ff94bc40SHeiko Schocher  */
process_pool_aeb(struct ubi_device * ubi,struct ubi_attach_info * ai,struct ubi_vid_hdr * new_vh,struct ubi_ainf_peb * new_aeb)368ff94bc40SHeiko Schocher static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
369ff94bc40SHeiko Schocher 			    struct ubi_vid_hdr *new_vh,
370ff94bc40SHeiko Schocher 			    struct ubi_ainf_peb *new_aeb)
371ff94bc40SHeiko Schocher {
372ff94bc40SHeiko Schocher 	struct ubi_ainf_volume *av, *tmp_av = NULL;
373ff94bc40SHeiko Schocher 	struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
374ff94bc40SHeiko Schocher 	int found = 0;
375ff94bc40SHeiko Schocher 
376ff94bc40SHeiko Schocher 	if (be32_to_cpu(new_vh->vol_id) == UBI_FM_SB_VOLUME_ID ||
377ff94bc40SHeiko Schocher 		be32_to_cpu(new_vh->vol_id) == UBI_FM_DATA_VOLUME_ID) {
378ff94bc40SHeiko Schocher 		kmem_cache_free(ai->aeb_slab_cache, new_aeb);
379ff94bc40SHeiko Schocher 
380ff94bc40SHeiko Schocher 		return 0;
381ff94bc40SHeiko Schocher 	}
382ff94bc40SHeiko Schocher 
383ff94bc40SHeiko Schocher 	/* Find the volume this SEB belongs to */
384ff94bc40SHeiko Schocher 	while (*p) {
385ff94bc40SHeiko Schocher 		parent = *p;
386ff94bc40SHeiko Schocher 		tmp_av = rb_entry(parent, struct ubi_ainf_volume, rb);
387ff94bc40SHeiko Schocher 
388ff94bc40SHeiko Schocher 		if (be32_to_cpu(new_vh->vol_id) > tmp_av->vol_id)
389ff94bc40SHeiko Schocher 			p = &(*p)->rb_left;
390ff94bc40SHeiko Schocher 		else if (be32_to_cpu(new_vh->vol_id) < tmp_av->vol_id)
391ff94bc40SHeiko Schocher 			p = &(*p)->rb_right;
392ff94bc40SHeiko Schocher 		else {
393ff94bc40SHeiko Schocher 			found = 1;
394ff94bc40SHeiko Schocher 			break;
395ff94bc40SHeiko Schocher 		}
396ff94bc40SHeiko Schocher 	}
397ff94bc40SHeiko Schocher 
398ff94bc40SHeiko Schocher 	if (found)
399ff94bc40SHeiko Schocher 		av = tmp_av;
400ff94bc40SHeiko Schocher 	else {
4010195a7bbSHeiko Schocher 		ubi_err(ubi, "orphaned volume in fastmap pool!");
4020195a7bbSHeiko Schocher 		kmem_cache_free(ai->aeb_slab_cache, new_aeb);
403ff94bc40SHeiko Schocher 		return UBI_BAD_FASTMAP;
404ff94bc40SHeiko Schocher 	}
405ff94bc40SHeiko Schocher 
406ff94bc40SHeiko Schocher 	ubi_assert(be32_to_cpu(new_vh->vol_id) == av->vol_id);
407ff94bc40SHeiko Schocher 
408ff94bc40SHeiko Schocher 	return update_vol(ubi, ai, av, new_vh, new_aeb);
409ff94bc40SHeiko Schocher }
410ff94bc40SHeiko Schocher 
411ff94bc40SHeiko Schocher /**
412ff94bc40SHeiko Schocher  * unmap_peb - unmap a PEB.
413ff94bc40SHeiko Schocher  * If fastmap detects a free PEB in the pool it has to check whether
414ff94bc40SHeiko Schocher  * this PEB has been unmapped after writing the fastmap.
415ff94bc40SHeiko Schocher  *
416ff94bc40SHeiko Schocher  * @ai: UBI attach info object
417ff94bc40SHeiko Schocher  * @pnum: The PEB to be unmapped
418ff94bc40SHeiko Schocher  */
unmap_peb(struct ubi_attach_info * ai,int pnum)419ff94bc40SHeiko Schocher static void unmap_peb(struct ubi_attach_info *ai, int pnum)
420ff94bc40SHeiko Schocher {
421ff94bc40SHeiko Schocher 	struct ubi_ainf_volume *av;
422ff94bc40SHeiko Schocher 	struct rb_node *node, *node2;
423ff94bc40SHeiko Schocher 	struct ubi_ainf_peb *aeb;
424ff94bc40SHeiko Schocher 
425ff94bc40SHeiko Schocher 	for (node = rb_first(&ai->volumes); node; node = rb_next(node)) {
426ff94bc40SHeiko Schocher 		av = rb_entry(node, struct ubi_ainf_volume, rb);
427ff94bc40SHeiko Schocher 
428ff94bc40SHeiko Schocher 		for (node2 = rb_first(&av->root); node2;
429ff94bc40SHeiko Schocher 		     node2 = rb_next(node2)) {
430ff94bc40SHeiko Schocher 			aeb = rb_entry(node2, struct ubi_ainf_peb, u.rb);
431ff94bc40SHeiko Schocher 			if (aeb->pnum == pnum) {
432ff94bc40SHeiko Schocher 				rb_erase(&aeb->u.rb, &av->root);
4330195a7bbSHeiko Schocher 				av->leb_count--;
434ff94bc40SHeiko Schocher 				kmem_cache_free(ai->aeb_slab_cache, aeb);
435ff94bc40SHeiko Schocher 				return;
436ff94bc40SHeiko Schocher 			}
437ff94bc40SHeiko Schocher 		}
438ff94bc40SHeiko Schocher 	}
439ff94bc40SHeiko Schocher }
440ff94bc40SHeiko Schocher 
441ff94bc40SHeiko Schocher /**
442ff94bc40SHeiko Schocher  * scan_pool - scans a pool for changed (no longer empty PEBs).
443ff94bc40SHeiko Schocher  * @ubi: UBI device object
444ff94bc40SHeiko Schocher  * @ai: attach info object
445ff94bc40SHeiko Schocher  * @pebs: an array of all PEB numbers in the to be scanned pool
446ff94bc40SHeiko Schocher  * @pool_size: size of the pool (number of entries in @pebs)
447ff94bc40SHeiko Schocher  * @max_sqnum: pointer to the maximal sequence number
448ff94bc40SHeiko Schocher  * @free: list of PEBs which are most likely free (and go into @ai->free)
449ff94bc40SHeiko Schocher  *
450ff94bc40SHeiko Schocher  * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
451ff94bc40SHeiko Schocher  * < 0 indicates an internal error.
452ff94bc40SHeiko Schocher  */
453ff94bc40SHeiko Schocher #ifndef __UBOOT__
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)454ff94bc40SHeiko Schocher static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
455248f260cSHeiko Schocher 		     __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
4560195a7bbSHeiko Schocher 		     struct list_head *free)
457ff94bc40SHeiko Schocher #else
458ff94bc40SHeiko Schocher static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
459248f260cSHeiko Schocher 		     __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
4600195a7bbSHeiko Schocher 		     struct list_head *free)
461ff94bc40SHeiko Schocher #endif
462ff94bc40SHeiko Schocher {
463ff94bc40SHeiko Schocher 	struct ubi_vid_hdr *vh;
464ff94bc40SHeiko Schocher 	struct ubi_ec_hdr *ech;
4650195a7bbSHeiko Schocher 	struct ubi_ainf_peb *new_aeb;
4660195a7bbSHeiko Schocher 	int i, pnum, err, ret = 0;
467ff94bc40SHeiko Schocher 
468ff94bc40SHeiko Schocher 	ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
469ff94bc40SHeiko Schocher 	if (!ech)
470ff94bc40SHeiko Schocher 		return -ENOMEM;
471ff94bc40SHeiko Schocher 
472ff94bc40SHeiko Schocher 	vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
473ff94bc40SHeiko Schocher 	if (!vh) {
474ff94bc40SHeiko Schocher 		kfree(ech);
475ff94bc40SHeiko Schocher 		return -ENOMEM;
476ff94bc40SHeiko Schocher 	}
477ff94bc40SHeiko Schocher 
478ff94bc40SHeiko Schocher 	dbg_bld("scanning fastmap pool: size = %i", pool_size);
479ff94bc40SHeiko Schocher 
480ff94bc40SHeiko Schocher 	/*
481ff94bc40SHeiko Schocher 	 * Now scan all PEBs in the pool to find changes which have been made
482ff94bc40SHeiko Schocher 	 * after the creation of the fastmap
483ff94bc40SHeiko Schocher 	 */
484ff94bc40SHeiko Schocher 	for (i = 0; i < pool_size; i++) {
485ff94bc40SHeiko Schocher 		int scrub = 0;
486ff94bc40SHeiko Schocher 		int image_seq;
487ff94bc40SHeiko Schocher 
488ff94bc40SHeiko Schocher 		pnum = be32_to_cpu(pebs[i]);
489ff94bc40SHeiko Schocher 
490ff94bc40SHeiko Schocher 		if (ubi_io_is_bad(ubi, pnum)) {
4910195a7bbSHeiko Schocher 			ubi_err(ubi, "bad PEB in fastmap pool!");
492ff94bc40SHeiko Schocher 			ret = UBI_BAD_FASTMAP;
493ff94bc40SHeiko Schocher 			goto out;
494ff94bc40SHeiko Schocher 		}
495ff94bc40SHeiko Schocher 
496ff94bc40SHeiko Schocher 		err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
497ff94bc40SHeiko Schocher 		if (err && err != UBI_IO_BITFLIPS) {
4980195a7bbSHeiko Schocher 			ubi_err(ubi, "unable to read EC header! PEB:%i err:%i",
499ff94bc40SHeiko Schocher 				pnum, err);
500ff94bc40SHeiko Schocher 			ret = err > 0 ? UBI_BAD_FASTMAP : err;
501ff94bc40SHeiko Schocher 			goto out;
5020195a7bbSHeiko Schocher 		} else if (err == UBI_IO_BITFLIPS)
503ff94bc40SHeiko Schocher 			scrub = 1;
504ff94bc40SHeiko Schocher 
505ff94bc40SHeiko Schocher 		/*
506ff94bc40SHeiko Schocher 		 * Older UBI implementations have image_seq set to zero, so
507ff94bc40SHeiko Schocher 		 * we shouldn't fail if image_seq == 0.
508ff94bc40SHeiko Schocher 		 */
509ff94bc40SHeiko Schocher 		image_seq = be32_to_cpu(ech->image_seq);
510ff94bc40SHeiko Schocher 
511ff94bc40SHeiko Schocher 		if (image_seq && (image_seq != ubi->image_seq)) {
5120195a7bbSHeiko Schocher 			ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x",
513ff94bc40SHeiko Schocher 				be32_to_cpu(ech->image_seq), ubi->image_seq);
514ff94bc40SHeiko Schocher 			ret = UBI_BAD_FASTMAP;
515ff94bc40SHeiko Schocher 			goto out;
516ff94bc40SHeiko Schocher 		}
517ff94bc40SHeiko Schocher 
518ff94bc40SHeiko Schocher 		err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
519ff94bc40SHeiko Schocher 		if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
520ff94bc40SHeiko Schocher 			unsigned long long ec = be64_to_cpu(ech->ec);
521ff94bc40SHeiko Schocher 			unmap_peb(ai, pnum);
522ff94bc40SHeiko Schocher 			dbg_bld("Adding PEB to free: %i", pnum);
523ff94bc40SHeiko Schocher 			if (err == UBI_IO_FF_BITFLIPS)
5240195a7bbSHeiko Schocher 				add_aeb(ai, free, pnum, ec, 1);
525ff94bc40SHeiko Schocher 			else
5260195a7bbSHeiko Schocher 				add_aeb(ai, free, pnum, ec, 0);
527ff94bc40SHeiko Schocher 			continue;
528ff94bc40SHeiko Schocher 		} else if (err == 0 || err == UBI_IO_BITFLIPS) {
529ff94bc40SHeiko Schocher 			dbg_bld("Found non empty PEB:%i in pool", pnum);
530ff94bc40SHeiko Schocher 
531ff94bc40SHeiko Schocher 			if (err == UBI_IO_BITFLIPS)
532ff94bc40SHeiko Schocher 				scrub = 1;
533ff94bc40SHeiko Schocher 
534ff94bc40SHeiko Schocher 			new_aeb = kmem_cache_alloc(ai->aeb_slab_cache,
535ff94bc40SHeiko Schocher 						   GFP_KERNEL);
536ff94bc40SHeiko Schocher 			if (!new_aeb) {
537ff94bc40SHeiko Schocher 				ret = -ENOMEM;
538ff94bc40SHeiko Schocher 				goto out;
539ff94bc40SHeiko Schocher 			}
540ff94bc40SHeiko Schocher 
541ff94bc40SHeiko Schocher 			new_aeb->ec = be64_to_cpu(ech->ec);
542ff94bc40SHeiko Schocher 			new_aeb->pnum = pnum;
543ff94bc40SHeiko Schocher 			new_aeb->lnum = be32_to_cpu(vh->lnum);
544ff94bc40SHeiko Schocher 			new_aeb->sqnum = be64_to_cpu(vh->sqnum);
545ff94bc40SHeiko Schocher 			new_aeb->copy_flag = vh->copy_flag;
546ff94bc40SHeiko Schocher 			new_aeb->scrub = scrub;
547ff94bc40SHeiko Schocher 
548ff94bc40SHeiko Schocher 			if (*max_sqnum < new_aeb->sqnum)
549ff94bc40SHeiko Schocher 				*max_sqnum = new_aeb->sqnum;
550ff94bc40SHeiko Schocher 
551ff94bc40SHeiko Schocher 			err = process_pool_aeb(ubi, ai, vh, new_aeb);
552ff94bc40SHeiko Schocher 			if (err) {
553ff94bc40SHeiko Schocher 				ret = err > 0 ? UBI_BAD_FASTMAP : err;
554ff94bc40SHeiko Schocher 				goto out;
555ff94bc40SHeiko Schocher 			}
556ff94bc40SHeiko Schocher 		} else {
557ff94bc40SHeiko Schocher 			/* We are paranoid and fall back to scanning mode */
5580195a7bbSHeiko Schocher 			ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!");
559ff94bc40SHeiko Schocher 			ret = err > 0 ? UBI_BAD_FASTMAP : err;
560ff94bc40SHeiko Schocher 			goto out;
561ff94bc40SHeiko Schocher 		}
562ff94bc40SHeiko Schocher 
563ff94bc40SHeiko Schocher 	}
564ff94bc40SHeiko Schocher 
565ff94bc40SHeiko Schocher out:
566ff94bc40SHeiko Schocher 	ubi_free_vid_hdr(ubi, vh);
567ff94bc40SHeiko Schocher 	kfree(ech);
568ff94bc40SHeiko Schocher 	return ret;
569ff94bc40SHeiko Schocher }
570ff94bc40SHeiko Schocher 
571ff94bc40SHeiko Schocher /**
572ff94bc40SHeiko Schocher  * count_fastmap_pebs - Counts the PEBs found by fastmap.
573ff94bc40SHeiko Schocher  * @ai: The UBI attach info object
574ff94bc40SHeiko Schocher  */
count_fastmap_pebs(struct ubi_attach_info * ai)575ff94bc40SHeiko Schocher static int count_fastmap_pebs(struct ubi_attach_info *ai)
576ff94bc40SHeiko Schocher {
577ff94bc40SHeiko Schocher 	struct ubi_ainf_peb *aeb;
578ff94bc40SHeiko Schocher 	struct ubi_ainf_volume *av;
579ff94bc40SHeiko Schocher 	struct rb_node *rb1, *rb2;
580ff94bc40SHeiko Schocher 	int n = 0;
581ff94bc40SHeiko Schocher 
582ff94bc40SHeiko Schocher 	list_for_each_entry(aeb, &ai->erase, u.list)
583ff94bc40SHeiko Schocher 		n++;
584ff94bc40SHeiko Schocher 
585ff94bc40SHeiko Schocher 	list_for_each_entry(aeb, &ai->free, u.list)
586ff94bc40SHeiko Schocher 		n++;
587ff94bc40SHeiko Schocher 
588ff94bc40SHeiko Schocher 	 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
589ff94bc40SHeiko Schocher 		ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
590ff94bc40SHeiko Schocher 			n++;
591ff94bc40SHeiko Schocher 
592ff94bc40SHeiko Schocher 	return n;
593ff94bc40SHeiko Schocher }
594ff94bc40SHeiko Schocher 
595ff94bc40SHeiko Schocher /**
596ff94bc40SHeiko Schocher  * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
597ff94bc40SHeiko Schocher  * @ubi: UBI device object
598ff94bc40SHeiko Schocher  * @ai: UBI attach info object
599ff94bc40SHeiko Schocher  * @fm: the fastmap to be attached
600ff94bc40SHeiko Schocher  *
601ff94bc40SHeiko Schocher  * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
602ff94bc40SHeiko Schocher  * < 0 indicates an internal error.
603ff94bc40SHeiko Schocher  */
ubi_attach_fastmap(struct ubi_device * ubi,struct ubi_attach_info * ai,struct ubi_fastmap_layout * fm)604ff94bc40SHeiko Schocher static int ubi_attach_fastmap(struct ubi_device *ubi,
605ff94bc40SHeiko Schocher 			      struct ubi_attach_info *ai,
606ff94bc40SHeiko Schocher 			      struct ubi_fastmap_layout *fm)
607ff94bc40SHeiko Schocher {
6080195a7bbSHeiko Schocher 	struct list_head used, free;
609ff94bc40SHeiko Schocher 	struct ubi_ainf_volume *av;
610ff94bc40SHeiko Schocher 	struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
611ff94bc40SHeiko Schocher 	struct ubi_fm_sb *fmsb;
612ff94bc40SHeiko Schocher 	struct ubi_fm_hdr *fmhdr;
6130195a7bbSHeiko Schocher 	struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
614ff94bc40SHeiko Schocher 	struct ubi_fm_ec *fmec;
615ff94bc40SHeiko Schocher 	struct ubi_fm_volhdr *fmvhdr;
616ff94bc40SHeiko Schocher 	struct ubi_fm_eba *fm_eba;
617ff94bc40SHeiko Schocher 	int ret, i, j, pool_size, wl_pool_size;
618ff94bc40SHeiko Schocher 	size_t fm_pos = 0, fm_size = ubi->fm_size;
619ff94bc40SHeiko Schocher 	unsigned long long max_sqnum = 0;
620ff94bc40SHeiko Schocher 	void *fm_raw = ubi->fm_buf;
621ff94bc40SHeiko Schocher 
622ff94bc40SHeiko Schocher 	INIT_LIST_HEAD(&used);
6230195a7bbSHeiko Schocher 	INIT_LIST_HEAD(&free);
624ff94bc40SHeiko Schocher 	ai->min_ec = UBI_MAX_ERASECOUNTER;
625ff94bc40SHeiko Schocher 
626ff94bc40SHeiko Schocher 	fmsb = (struct ubi_fm_sb *)(fm_raw);
627ff94bc40SHeiko Schocher 	ai->max_sqnum = fmsb->sqnum;
628ff94bc40SHeiko Schocher 	fm_pos += sizeof(struct ubi_fm_sb);
629ff94bc40SHeiko Schocher 	if (fm_pos >= fm_size)
630ff94bc40SHeiko Schocher 		goto fail_bad;
631ff94bc40SHeiko Schocher 
632ff94bc40SHeiko Schocher 	fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
633ff94bc40SHeiko Schocher 	fm_pos += sizeof(*fmhdr);
634ff94bc40SHeiko Schocher 	if (fm_pos >= fm_size)
635ff94bc40SHeiko Schocher 		goto fail_bad;
636ff94bc40SHeiko Schocher 
637ff94bc40SHeiko Schocher 	if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
6380195a7bbSHeiko Schocher 		ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x",
639ff94bc40SHeiko Schocher 			be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
640ff94bc40SHeiko Schocher 		goto fail_bad;
641ff94bc40SHeiko Schocher 	}
642ff94bc40SHeiko Schocher 
6430195a7bbSHeiko Schocher 	fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
6440195a7bbSHeiko Schocher 	fm_pos += sizeof(*fmpl);
645ff94bc40SHeiko Schocher 	if (fm_pos >= fm_size)
646ff94bc40SHeiko Schocher 		goto fail_bad;
6470195a7bbSHeiko Schocher 	if (be32_to_cpu(fmpl->magic) != UBI_FM_POOL_MAGIC) {
6480195a7bbSHeiko Schocher 		ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x",
6490195a7bbSHeiko Schocher 			be32_to_cpu(fmpl->magic), UBI_FM_POOL_MAGIC);
650ff94bc40SHeiko Schocher 		goto fail_bad;
651ff94bc40SHeiko Schocher 	}
652ff94bc40SHeiko Schocher 
6530195a7bbSHeiko Schocher 	fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
6540195a7bbSHeiko Schocher 	fm_pos += sizeof(*fmpl_wl);
655ff94bc40SHeiko Schocher 	if (fm_pos >= fm_size)
656ff94bc40SHeiko Schocher 		goto fail_bad;
6570195a7bbSHeiko Schocher 	if (be32_to_cpu(fmpl_wl->magic) != UBI_FM_POOL_MAGIC) {
6580195a7bbSHeiko Schocher 		ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x",
6590195a7bbSHeiko Schocher 			be32_to_cpu(fmpl_wl->magic), UBI_FM_POOL_MAGIC);
660ff94bc40SHeiko Schocher 		goto fail_bad;
661ff94bc40SHeiko Schocher 	}
662ff94bc40SHeiko Schocher 
6630195a7bbSHeiko Schocher 	pool_size = be16_to_cpu(fmpl->size);
6640195a7bbSHeiko Schocher 	wl_pool_size = be16_to_cpu(fmpl_wl->size);
6650195a7bbSHeiko Schocher 	fm->max_pool_size = be16_to_cpu(fmpl->max_size);
6660195a7bbSHeiko Schocher 	fm->max_wl_pool_size = be16_to_cpu(fmpl_wl->max_size);
667ff94bc40SHeiko Schocher 
668ff94bc40SHeiko Schocher 	if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
6690195a7bbSHeiko Schocher 		ubi_err(ubi, "bad pool size: %i", pool_size);
670ff94bc40SHeiko Schocher 		goto fail_bad;
671ff94bc40SHeiko Schocher 	}
672ff94bc40SHeiko Schocher 
673ff94bc40SHeiko Schocher 	if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
6740195a7bbSHeiko Schocher 		ubi_err(ubi, "bad WL pool size: %i", wl_pool_size);
675ff94bc40SHeiko Schocher 		goto fail_bad;
676ff94bc40SHeiko Schocher 	}
677ff94bc40SHeiko Schocher 
678ff94bc40SHeiko Schocher 
679ff94bc40SHeiko Schocher 	if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
680ff94bc40SHeiko Schocher 	    fm->max_pool_size < 0) {
6810195a7bbSHeiko Schocher 		ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size);
682ff94bc40SHeiko Schocher 		goto fail_bad;
683ff94bc40SHeiko Schocher 	}
684ff94bc40SHeiko Schocher 
685ff94bc40SHeiko Schocher 	if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
686ff94bc40SHeiko Schocher 	    fm->max_wl_pool_size < 0) {
6870195a7bbSHeiko Schocher 		ubi_err(ubi, "bad maximal WL pool size: %i",
6880195a7bbSHeiko Schocher 			fm->max_wl_pool_size);
689ff94bc40SHeiko Schocher 		goto fail_bad;
690ff94bc40SHeiko Schocher 	}
691ff94bc40SHeiko Schocher 
692ff94bc40SHeiko Schocher 	/* read EC values from free list */
693ff94bc40SHeiko Schocher 	for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
694ff94bc40SHeiko Schocher 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
695ff94bc40SHeiko Schocher 		fm_pos += sizeof(*fmec);
696ff94bc40SHeiko Schocher 		if (fm_pos >= fm_size)
697ff94bc40SHeiko Schocher 			goto fail_bad;
698ff94bc40SHeiko Schocher 
699ff94bc40SHeiko Schocher 		add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
700ff94bc40SHeiko Schocher 			be32_to_cpu(fmec->ec), 0);
701ff94bc40SHeiko Schocher 	}
702ff94bc40SHeiko Schocher 
703ff94bc40SHeiko Schocher 	/* read EC values from used list */
704ff94bc40SHeiko Schocher 	for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
705ff94bc40SHeiko Schocher 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
706ff94bc40SHeiko Schocher 		fm_pos += sizeof(*fmec);
707ff94bc40SHeiko Schocher 		if (fm_pos >= fm_size)
708ff94bc40SHeiko Schocher 			goto fail_bad;
709ff94bc40SHeiko Schocher 
710ff94bc40SHeiko Schocher 		add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
711ff94bc40SHeiko Schocher 			be32_to_cpu(fmec->ec), 0);
712ff94bc40SHeiko Schocher 	}
713ff94bc40SHeiko Schocher 
714ff94bc40SHeiko Schocher 	/* read EC values from scrub list */
715ff94bc40SHeiko Schocher 	for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
716ff94bc40SHeiko Schocher 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
717ff94bc40SHeiko Schocher 		fm_pos += sizeof(*fmec);
718ff94bc40SHeiko Schocher 		if (fm_pos >= fm_size)
719ff94bc40SHeiko Schocher 			goto fail_bad;
720ff94bc40SHeiko Schocher 
721ff94bc40SHeiko Schocher 		add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
722ff94bc40SHeiko Schocher 			be32_to_cpu(fmec->ec), 1);
723ff94bc40SHeiko Schocher 	}
724ff94bc40SHeiko Schocher 
725ff94bc40SHeiko Schocher 	/* read EC values from erase list */
726ff94bc40SHeiko Schocher 	for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
727ff94bc40SHeiko Schocher 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
728ff94bc40SHeiko Schocher 		fm_pos += sizeof(*fmec);
729ff94bc40SHeiko Schocher 		if (fm_pos >= fm_size)
730ff94bc40SHeiko Schocher 			goto fail_bad;
731ff94bc40SHeiko Schocher 
732ff94bc40SHeiko Schocher 		add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
733ff94bc40SHeiko Schocher 			be32_to_cpu(fmec->ec), 1);
734ff94bc40SHeiko Schocher 	}
735ff94bc40SHeiko Schocher 
736ff94bc40SHeiko Schocher 	ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
737ff94bc40SHeiko Schocher 	ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
738ff94bc40SHeiko Schocher 
739ff94bc40SHeiko Schocher 	/* Iterate over all volumes and read their EBA table */
740ff94bc40SHeiko Schocher 	for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
741ff94bc40SHeiko Schocher 		fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
742ff94bc40SHeiko Schocher 		fm_pos += sizeof(*fmvhdr);
743ff94bc40SHeiko Schocher 		if (fm_pos >= fm_size)
744ff94bc40SHeiko Schocher 			goto fail_bad;
745ff94bc40SHeiko Schocher 
746ff94bc40SHeiko Schocher 		if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
7470195a7bbSHeiko Schocher 			ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x",
748ff94bc40SHeiko Schocher 				be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
749ff94bc40SHeiko Schocher 			goto fail_bad;
750ff94bc40SHeiko Schocher 		}
751ff94bc40SHeiko Schocher 
752ff94bc40SHeiko Schocher 		av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
753ff94bc40SHeiko Schocher 			     be32_to_cpu(fmvhdr->used_ebs),
754ff94bc40SHeiko Schocher 			     be32_to_cpu(fmvhdr->data_pad),
755ff94bc40SHeiko Schocher 			     fmvhdr->vol_type,
756ff94bc40SHeiko Schocher 			     be32_to_cpu(fmvhdr->last_eb_bytes));
757ff94bc40SHeiko Schocher 
758ff94bc40SHeiko Schocher 		if (!av)
759ff94bc40SHeiko Schocher 			goto fail_bad;
7600195a7bbSHeiko Schocher 		if (PTR_ERR(av) == -EINVAL) {
7610195a7bbSHeiko Schocher 			ubi_err(ubi, "volume (ID %i) already exists",
7620195a7bbSHeiko Schocher 				fmvhdr->vol_id);
7630195a7bbSHeiko Schocher 			goto fail_bad;
7640195a7bbSHeiko Schocher 		}
765ff94bc40SHeiko Schocher 
766ff94bc40SHeiko Schocher 		ai->vols_found++;
767ff94bc40SHeiko Schocher 		if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
768ff94bc40SHeiko Schocher 			ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
769ff94bc40SHeiko Schocher 
770ff94bc40SHeiko Schocher 		fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
771ff94bc40SHeiko Schocher 		fm_pos += sizeof(*fm_eba);
772ff94bc40SHeiko Schocher 		fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
773ff94bc40SHeiko Schocher 		if (fm_pos >= fm_size)
774ff94bc40SHeiko Schocher 			goto fail_bad;
775ff94bc40SHeiko Schocher 
776ff94bc40SHeiko Schocher 		if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
7770195a7bbSHeiko Schocher 			ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x",
778ff94bc40SHeiko Schocher 				be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
779ff94bc40SHeiko Schocher 			goto fail_bad;
780ff94bc40SHeiko Schocher 		}
781ff94bc40SHeiko Schocher 
782ff94bc40SHeiko Schocher 		for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
783ff94bc40SHeiko Schocher 			int pnum = be32_to_cpu(fm_eba->pnum[j]);
784ff94bc40SHeiko Schocher 
785ff94bc40SHeiko Schocher 			if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
786ff94bc40SHeiko Schocher 				continue;
787ff94bc40SHeiko Schocher 
788ff94bc40SHeiko Schocher 			aeb = NULL;
789ff94bc40SHeiko Schocher 			list_for_each_entry(tmp_aeb, &used, u.list) {
790ff94bc40SHeiko Schocher 				if (tmp_aeb->pnum == pnum) {
791ff94bc40SHeiko Schocher 					aeb = tmp_aeb;
792ff94bc40SHeiko Schocher 					break;
793ff94bc40SHeiko Schocher 				}
794ff94bc40SHeiko Schocher 			}
795ff94bc40SHeiko Schocher 
796ff94bc40SHeiko Schocher 			if (!aeb) {
7970195a7bbSHeiko Schocher 				ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum);
7980195a7bbSHeiko Schocher 				goto fail_bad;
799ff94bc40SHeiko Schocher 			}
800ff94bc40SHeiko Schocher 
801ff94bc40SHeiko Schocher 			aeb->lnum = j;
802ff94bc40SHeiko Schocher 
803ff94bc40SHeiko Schocher 			if (av->highest_lnum <= aeb->lnum)
804ff94bc40SHeiko Schocher 				av->highest_lnum = aeb->lnum;
805ff94bc40SHeiko Schocher 
806ff94bc40SHeiko Schocher 			assign_aeb_to_av(ai, aeb, av);
807ff94bc40SHeiko Schocher 
808ff94bc40SHeiko Schocher 			dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
809ff94bc40SHeiko Schocher 				aeb->pnum, aeb->lnum, av->vol_id);
810ff94bc40SHeiko Schocher 		}
811ff94bc40SHeiko Schocher 	}
812ff94bc40SHeiko Schocher 
8130195a7bbSHeiko Schocher 	ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free);
814ff94bc40SHeiko Schocher 	if (ret)
815ff94bc40SHeiko Schocher 		goto fail;
816ff94bc40SHeiko Schocher 
8170195a7bbSHeiko Schocher 	ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free);
818ff94bc40SHeiko Schocher 	if (ret)
819ff94bc40SHeiko Schocher 		goto fail;
820ff94bc40SHeiko Schocher 
821ff94bc40SHeiko Schocher 	if (max_sqnum > ai->max_sqnum)
822ff94bc40SHeiko Schocher 		ai->max_sqnum = max_sqnum;
823ff94bc40SHeiko Schocher 
8240195a7bbSHeiko Schocher 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
825ff94bc40SHeiko Schocher 		list_move_tail(&tmp_aeb->u.list, &ai->free);
826ff94bc40SHeiko Schocher 
8270195a7bbSHeiko Schocher 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list)
8280195a7bbSHeiko Schocher 		list_move_tail(&tmp_aeb->u.list, &ai->erase);
8290195a7bbSHeiko Schocher 
8300195a7bbSHeiko Schocher 	ubi_assert(list_empty(&free));
831ff94bc40SHeiko Schocher 
832ff94bc40SHeiko Schocher 	/*
833ff94bc40SHeiko Schocher 	 * If fastmap is leaking PEBs (must not happen), raise a
834ff94bc40SHeiko Schocher 	 * fat warning and fall back to scanning mode.
835ff94bc40SHeiko Schocher 	 * We do this here because in ubi_wl_init() it's too late
836ff94bc40SHeiko Schocher 	 * and we cannot fall back to scanning.
837ff94bc40SHeiko Schocher 	 */
838ff94bc40SHeiko Schocher #ifndef __UBOOT__
839ff94bc40SHeiko Schocher 	if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
840ff94bc40SHeiko Schocher 		    ai->bad_peb_count - fm->used_blocks))
841ff94bc40SHeiko Schocher 		goto fail_bad;
842ff94bc40SHeiko Schocher #else
843ff94bc40SHeiko Schocher 	if (count_fastmap_pebs(ai) != ubi->peb_count -
844ff94bc40SHeiko Schocher 		    ai->bad_peb_count - fm->used_blocks) {
845ff94bc40SHeiko Schocher 		WARN_ON(1);
846ff94bc40SHeiko Schocher 		goto fail_bad;
847ff94bc40SHeiko Schocher 	}
848ff94bc40SHeiko Schocher #endif
849ff94bc40SHeiko Schocher 
850ff94bc40SHeiko Schocher 	return 0;
851ff94bc40SHeiko Schocher 
852ff94bc40SHeiko Schocher fail_bad:
853ff94bc40SHeiko Schocher 	ret = UBI_BAD_FASTMAP;
854ff94bc40SHeiko Schocher fail:
855ff94bc40SHeiko Schocher 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
856ff94bc40SHeiko Schocher 		list_del(&tmp_aeb->u.list);
857ff94bc40SHeiko Schocher 		kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
858ff94bc40SHeiko Schocher 	}
8590195a7bbSHeiko Schocher 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
860ff94bc40SHeiko Schocher 		list_del(&tmp_aeb->u.list);
861ff94bc40SHeiko Schocher 		kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
862ff94bc40SHeiko Schocher 	}
863ff94bc40SHeiko Schocher 
864ff94bc40SHeiko Schocher 	return ret;
865ff94bc40SHeiko Schocher }
866ff94bc40SHeiko Schocher 
867ff94bc40SHeiko Schocher /**
868ff94bc40SHeiko Schocher  * ubi_scan_fastmap - scan the fastmap.
869ff94bc40SHeiko Schocher  * @ubi: UBI device object
870ff94bc40SHeiko Schocher  * @ai: UBI attach info to be filled
871ff94bc40SHeiko Schocher  * @fm_anchor: The fastmap starts at this PEB
872ff94bc40SHeiko Schocher  *
873ff94bc40SHeiko Schocher  * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
874ff94bc40SHeiko Schocher  * UBI_BAD_FASTMAP if one was found but is not usable.
875ff94bc40SHeiko Schocher  * < 0 indicates an internal error.
876ff94bc40SHeiko Schocher  */
ubi_scan_fastmap(struct ubi_device * ubi,struct ubi_attach_info * ai,int fm_anchor)877ff94bc40SHeiko Schocher int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
878ff94bc40SHeiko Schocher 		     int fm_anchor)
879ff94bc40SHeiko Schocher {
880ff94bc40SHeiko Schocher 	struct ubi_fm_sb *fmsb, *fmsb2;
881ff94bc40SHeiko Schocher 	struct ubi_vid_hdr *vh;
882ff94bc40SHeiko Schocher 	struct ubi_ec_hdr *ech;
883ff94bc40SHeiko Schocher 	struct ubi_fastmap_layout *fm;
884ff94bc40SHeiko Schocher 	int i, used_blocks, pnum, ret = 0;
885ff94bc40SHeiko Schocher 	size_t fm_size;
886ff94bc40SHeiko Schocher 	__be32 crc, tmp_crc;
887ff94bc40SHeiko Schocher 	unsigned long long sqnum = 0;
888ff94bc40SHeiko Schocher 
8890195a7bbSHeiko Schocher 	down_write(&ubi->fm_protect);
890ff94bc40SHeiko Schocher 	memset(ubi->fm_buf, 0, ubi->fm_size);
891ff94bc40SHeiko Schocher 
892ff94bc40SHeiko Schocher 	fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
893ff94bc40SHeiko Schocher 	if (!fmsb) {
894ff94bc40SHeiko Schocher 		ret = -ENOMEM;
895ff94bc40SHeiko Schocher 		goto out;
896ff94bc40SHeiko Schocher 	}
897ff94bc40SHeiko Schocher 
898ff94bc40SHeiko Schocher 	fm = kzalloc(sizeof(*fm), GFP_KERNEL);
899ff94bc40SHeiko Schocher 	if (!fm) {
900ff94bc40SHeiko Schocher 		ret = -ENOMEM;
901ff94bc40SHeiko Schocher 		kfree(fmsb);
902ff94bc40SHeiko Schocher 		goto out;
903ff94bc40SHeiko Schocher 	}
904ff94bc40SHeiko Schocher 
905ff94bc40SHeiko Schocher 	ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
906ff94bc40SHeiko Schocher 	if (ret && ret != UBI_IO_BITFLIPS)
907ff94bc40SHeiko Schocher 		goto free_fm_sb;
908ff94bc40SHeiko Schocher 	else if (ret == UBI_IO_BITFLIPS)
909ff94bc40SHeiko Schocher 		fm->to_be_tortured[0] = 1;
910ff94bc40SHeiko Schocher 
911ff94bc40SHeiko Schocher 	if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
9120195a7bbSHeiko Schocher 		ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x",
913ff94bc40SHeiko Schocher 			be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
914ff94bc40SHeiko Schocher 		ret = UBI_BAD_FASTMAP;
915ff94bc40SHeiko Schocher 		goto free_fm_sb;
916ff94bc40SHeiko Schocher 	}
917ff94bc40SHeiko Schocher 
918ff94bc40SHeiko Schocher 	if (fmsb->version != UBI_FM_FMT_VERSION) {
9190195a7bbSHeiko Schocher 		ubi_err(ubi, "bad fastmap version: %i, expected: %i",
920ff94bc40SHeiko Schocher 			fmsb->version, UBI_FM_FMT_VERSION);
921ff94bc40SHeiko Schocher 		ret = UBI_BAD_FASTMAP;
922ff94bc40SHeiko Schocher 		goto free_fm_sb;
923ff94bc40SHeiko Schocher 	}
924ff94bc40SHeiko Schocher 
925ff94bc40SHeiko Schocher 	used_blocks = be32_to_cpu(fmsb->used_blocks);
926ff94bc40SHeiko Schocher 	if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
9270195a7bbSHeiko Schocher 		ubi_err(ubi, "number of fastmap blocks is invalid: %i",
9280195a7bbSHeiko Schocher 			used_blocks);
929ff94bc40SHeiko Schocher 		ret = UBI_BAD_FASTMAP;
930ff94bc40SHeiko Schocher 		goto free_fm_sb;
931ff94bc40SHeiko Schocher 	}
932ff94bc40SHeiko Schocher 
933ff94bc40SHeiko Schocher 	fm_size = ubi->leb_size * used_blocks;
934ff94bc40SHeiko Schocher 	if (fm_size != ubi->fm_size) {
9350195a7bbSHeiko Schocher 		ubi_err(ubi, "bad fastmap size: %zi, expected: %zi",
9360195a7bbSHeiko Schocher 			fm_size, ubi->fm_size);
937ff94bc40SHeiko Schocher 		ret = UBI_BAD_FASTMAP;
938ff94bc40SHeiko Schocher 		goto free_fm_sb;
939ff94bc40SHeiko Schocher 	}
940ff94bc40SHeiko Schocher 
941ff94bc40SHeiko Schocher 	ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
942ff94bc40SHeiko Schocher 	if (!ech) {
943ff94bc40SHeiko Schocher 		ret = -ENOMEM;
944ff94bc40SHeiko Schocher 		goto free_fm_sb;
945ff94bc40SHeiko Schocher 	}
946ff94bc40SHeiko Schocher 
947ff94bc40SHeiko Schocher 	vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
948ff94bc40SHeiko Schocher 	if (!vh) {
949ff94bc40SHeiko Schocher 		ret = -ENOMEM;
950ff94bc40SHeiko Schocher 		goto free_hdr;
951ff94bc40SHeiko Schocher 	}
952ff94bc40SHeiko Schocher 
953ff94bc40SHeiko Schocher 	for (i = 0; i < used_blocks; i++) {
954ff94bc40SHeiko Schocher 		int image_seq;
955ff94bc40SHeiko Schocher 
956ff94bc40SHeiko Schocher 		pnum = be32_to_cpu(fmsb->block_loc[i]);
957ff94bc40SHeiko Schocher 
958ff94bc40SHeiko Schocher 		if (ubi_io_is_bad(ubi, pnum)) {
959ff94bc40SHeiko Schocher 			ret = UBI_BAD_FASTMAP;
960ff94bc40SHeiko Schocher 			goto free_hdr;
961ff94bc40SHeiko Schocher 		}
962ff94bc40SHeiko Schocher 
963ff94bc40SHeiko Schocher 		ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
964ff94bc40SHeiko Schocher 		if (ret && ret != UBI_IO_BITFLIPS) {
9650195a7bbSHeiko Schocher 			ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)",
966ff94bc40SHeiko Schocher 				i, pnum);
967ff94bc40SHeiko Schocher 			if (ret > 0)
968ff94bc40SHeiko Schocher 				ret = UBI_BAD_FASTMAP;
969ff94bc40SHeiko Schocher 			goto free_hdr;
970ff94bc40SHeiko Schocher 		} else if (ret == UBI_IO_BITFLIPS)
971ff94bc40SHeiko Schocher 			fm->to_be_tortured[i] = 1;
972ff94bc40SHeiko Schocher 
973ff94bc40SHeiko Schocher 		image_seq = be32_to_cpu(ech->image_seq);
974ff94bc40SHeiko Schocher 		if (!ubi->image_seq)
975ff94bc40SHeiko Schocher 			ubi->image_seq = image_seq;
976ff94bc40SHeiko Schocher 
977ff94bc40SHeiko Schocher 		/*
978ff94bc40SHeiko Schocher 		 * Older UBI implementations have image_seq set to zero, so
979ff94bc40SHeiko Schocher 		 * we shouldn't fail if image_seq == 0.
980ff94bc40SHeiko Schocher 		 */
981ff94bc40SHeiko Schocher 		if (image_seq && (image_seq != ubi->image_seq)) {
9820195a7bbSHeiko Schocher 			ubi_err(ubi, "wrong image seq:%d instead of %d",
983ff94bc40SHeiko Schocher 				be32_to_cpu(ech->image_seq), ubi->image_seq);
984ff94bc40SHeiko Schocher 			ret = UBI_BAD_FASTMAP;
985ff94bc40SHeiko Schocher 			goto free_hdr;
986ff94bc40SHeiko Schocher 		}
987ff94bc40SHeiko Schocher 
988ff94bc40SHeiko Schocher 		ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
989ff94bc40SHeiko Schocher 		if (ret && ret != UBI_IO_BITFLIPS) {
9900195a7bbSHeiko Schocher 			ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)",
991ff94bc40SHeiko Schocher 				i, pnum);
992ff94bc40SHeiko Schocher 			goto free_hdr;
993ff94bc40SHeiko Schocher 		}
994ff94bc40SHeiko Schocher 
995ff94bc40SHeiko Schocher 		if (i == 0) {
996ff94bc40SHeiko Schocher 			if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
9970195a7bbSHeiko Schocher 				ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x",
998ff94bc40SHeiko Schocher 					be32_to_cpu(vh->vol_id),
999ff94bc40SHeiko Schocher 					UBI_FM_SB_VOLUME_ID);
1000ff94bc40SHeiko Schocher 				ret = UBI_BAD_FASTMAP;
1001ff94bc40SHeiko Schocher 				goto free_hdr;
1002ff94bc40SHeiko Schocher 			}
1003ff94bc40SHeiko Schocher 		} else {
1004ff94bc40SHeiko Schocher 			if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
10050195a7bbSHeiko Schocher 				ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x",
1006ff94bc40SHeiko Schocher 					be32_to_cpu(vh->vol_id),
1007ff94bc40SHeiko Schocher 					UBI_FM_DATA_VOLUME_ID);
1008ff94bc40SHeiko Schocher 				ret = UBI_BAD_FASTMAP;
1009ff94bc40SHeiko Schocher 				goto free_hdr;
1010ff94bc40SHeiko Schocher 			}
1011ff94bc40SHeiko Schocher 		}
1012ff94bc40SHeiko Schocher 
1013ff94bc40SHeiko Schocher 		if (sqnum < be64_to_cpu(vh->sqnum))
1014ff94bc40SHeiko Schocher 			sqnum = be64_to_cpu(vh->sqnum);
1015ff94bc40SHeiko Schocher 
1016ff94bc40SHeiko Schocher 		ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
1017ff94bc40SHeiko Schocher 				  ubi->leb_start, ubi->leb_size);
1018ff94bc40SHeiko Schocher 		if (ret && ret != UBI_IO_BITFLIPS) {
10190195a7bbSHeiko Schocher 			ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, "
1020ff94bc40SHeiko Schocher 				"err: %i)", i, pnum, ret);
1021ff94bc40SHeiko Schocher 			goto free_hdr;
1022ff94bc40SHeiko Schocher 		}
1023ff94bc40SHeiko Schocher 	}
1024ff94bc40SHeiko Schocher 
1025ff94bc40SHeiko Schocher 	kfree(fmsb);
1026ff94bc40SHeiko Schocher 	fmsb = NULL;
1027ff94bc40SHeiko Schocher 
1028ff94bc40SHeiko Schocher 	fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
1029ff94bc40SHeiko Schocher 	tmp_crc = be32_to_cpu(fmsb2->data_crc);
1030ff94bc40SHeiko Schocher 	fmsb2->data_crc = 0;
1031ff94bc40SHeiko Schocher 	crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
1032ff94bc40SHeiko Schocher 	if (crc != tmp_crc) {
10330195a7bbSHeiko Schocher 		ubi_err(ubi, "fastmap data CRC is invalid");
10340195a7bbSHeiko Schocher 		ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x",
10350195a7bbSHeiko Schocher 			tmp_crc, crc);
1036ff94bc40SHeiko Schocher 		ret = UBI_BAD_FASTMAP;
1037ff94bc40SHeiko Schocher 		goto free_hdr;
1038ff94bc40SHeiko Schocher 	}
1039ff94bc40SHeiko Schocher 
1040ff94bc40SHeiko Schocher 	fmsb2->sqnum = sqnum;
1041ff94bc40SHeiko Schocher 
1042ff94bc40SHeiko Schocher 	fm->used_blocks = used_blocks;
1043ff94bc40SHeiko Schocher 
1044ff94bc40SHeiko Schocher 	ret = ubi_attach_fastmap(ubi, ai, fm);
1045ff94bc40SHeiko Schocher 	if (ret) {
1046ff94bc40SHeiko Schocher 		if (ret > 0)
1047ff94bc40SHeiko Schocher 			ret = UBI_BAD_FASTMAP;
1048ff94bc40SHeiko Schocher 		goto free_hdr;
1049ff94bc40SHeiko Schocher 	}
1050ff94bc40SHeiko Schocher 
1051ff94bc40SHeiko Schocher 	for (i = 0; i < used_blocks; i++) {
1052ff94bc40SHeiko Schocher 		struct ubi_wl_entry *e;
1053ff94bc40SHeiko Schocher 
1054ff94bc40SHeiko Schocher 		e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1055ff94bc40SHeiko Schocher 		if (!e) {
1056ff94bc40SHeiko Schocher 			while (i--)
1057ff94bc40SHeiko Schocher 				kfree(fm->e[i]);
1058ff94bc40SHeiko Schocher 
1059ff94bc40SHeiko Schocher 			ret = -ENOMEM;
1060ff94bc40SHeiko Schocher 			goto free_hdr;
1061ff94bc40SHeiko Schocher 		}
1062ff94bc40SHeiko Schocher 
1063ff94bc40SHeiko Schocher 		e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
1064ff94bc40SHeiko Schocher 		e->ec = be32_to_cpu(fmsb2->block_ec[i]);
1065ff94bc40SHeiko Schocher 		fm->e[i] = e;
1066ff94bc40SHeiko Schocher 	}
1067ff94bc40SHeiko Schocher 
1068ff94bc40SHeiko Schocher 	ubi->fm = fm;
1069ff94bc40SHeiko Schocher 	ubi->fm_pool.max_size = ubi->fm->max_pool_size;
1070ff94bc40SHeiko Schocher 	ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
10710195a7bbSHeiko Schocher 	ubi_msg(ubi, "attached by fastmap");
10720195a7bbSHeiko Schocher 	ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size);
10730195a7bbSHeiko Schocher 	ubi_msg(ubi, "fastmap WL pool size: %d",
10740195a7bbSHeiko Schocher 		ubi->fm_wl_pool.max_size);
1075ff94bc40SHeiko Schocher 	ubi->fm_disabled = 0;
1076ff94bc40SHeiko Schocher 
1077ff94bc40SHeiko Schocher 	ubi_free_vid_hdr(ubi, vh);
1078ff94bc40SHeiko Schocher 	kfree(ech);
1079ff94bc40SHeiko Schocher out:
10800195a7bbSHeiko Schocher 	up_write(&ubi->fm_protect);
1081ff94bc40SHeiko Schocher 	if (ret == UBI_BAD_FASTMAP)
10820195a7bbSHeiko Schocher 		ubi_err(ubi, "Attach by fastmap failed, doing a full scan!");
1083ff94bc40SHeiko Schocher 	return ret;
1084ff94bc40SHeiko Schocher 
1085ff94bc40SHeiko Schocher free_hdr:
1086ff94bc40SHeiko Schocher 	ubi_free_vid_hdr(ubi, vh);
1087ff94bc40SHeiko Schocher 	kfree(ech);
1088ff94bc40SHeiko Schocher free_fm_sb:
1089ff94bc40SHeiko Schocher 	kfree(fmsb);
1090ff94bc40SHeiko Schocher 	kfree(fm);
1091ff94bc40SHeiko Schocher 	goto out;
1092ff94bc40SHeiko Schocher }
1093ff94bc40SHeiko Schocher 
1094ff94bc40SHeiko Schocher /**
1095ff94bc40SHeiko Schocher  * ubi_write_fastmap - writes a fastmap.
1096ff94bc40SHeiko Schocher  * @ubi: UBI device object
1097ff94bc40SHeiko Schocher  * @new_fm: the to be written fastmap
1098ff94bc40SHeiko Schocher  *
1099ff94bc40SHeiko Schocher  * Returns 0 on success, < 0 indicates an internal error.
1100ff94bc40SHeiko Schocher  */
ubi_write_fastmap(struct ubi_device * ubi,struct ubi_fastmap_layout * new_fm)1101ff94bc40SHeiko Schocher static int ubi_write_fastmap(struct ubi_device *ubi,
1102ff94bc40SHeiko Schocher 			     struct ubi_fastmap_layout *new_fm)
1103ff94bc40SHeiko Schocher {
1104ff94bc40SHeiko Schocher 	size_t fm_pos = 0;
1105ff94bc40SHeiko Schocher 	void *fm_raw;
1106ff94bc40SHeiko Schocher 	struct ubi_fm_sb *fmsb;
1107ff94bc40SHeiko Schocher 	struct ubi_fm_hdr *fmh;
11080195a7bbSHeiko Schocher 	struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
1109ff94bc40SHeiko Schocher 	struct ubi_fm_ec *fec;
1110ff94bc40SHeiko Schocher 	struct ubi_fm_volhdr *fvh;
1111ff94bc40SHeiko Schocher 	struct ubi_fm_eba *feba;
1112ff94bc40SHeiko Schocher 	struct ubi_wl_entry *wl_e;
1113ff94bc40SHeiko Schocher 	struct ubi_volume *vol;
1114ff94bc40SHeiko Schocher 	struct ubi_vid_hdr *avhdr, *dvhdr;
1115ff94bc40SHeiko Schocher 	struct ubi_work *ubi_wrk;
11160195a7bbSHeiko Schocher 	struct rb_node *tmp_rb;
1117ff94bc40SHeiko Schocher 	int ret, i, j, free_peb_count, used_peb_count, vol_count;
1118ff94bc40SHeiko Schocher 	int scrub_peb_count, erase_peb_count;
11190195a7bbSHeiko Schocher 	int *seen_pebs = NULL;
1120ff94bc40SHeiko Schocher 
1121ff94bc40SHeiko Schocher 	fm_raw = ubi->fm_buf;
1122ff94bc40SHeiko Schocher 	memset(ubi->fm_buf, 0, ubi->fm_size);
1123ff94bc40SHeiko Schocher 
1124ff94bc40SHeiko Schocher 	avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1125ff94bc40SHeiko Schocher 	if (!avhdr) {
1126ff94bc40SHeiko Schocher 		ret = -ENOMEM;
1127ff94bc40SHeiko Schocher 		goto out;
1128ff94bc40SHeiko Schocher 	}
1129ff94bc40SHeiko Schocher 
1130ff94bc40SHeiko Schocher 	dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
1131ff94bc40SHeiko Schocher 	if (!dvhdr) {
1132ff94bc40SHeiko Schocher 		ret = -ENOMEM;
1133ff94bc40SHeiko Schocher 		goto out_kfree;
1134ff94bc40SHeiko Schocher 	}
1135ff94bc40SHeiko Schocher 
11360195a7bbSHeiko Schocher 	seen_pebs = init_seen(ubi);
11370195a7bbSHeiko Schocher 	if (IS_ERR(seen_pebs)) {
11380195a7bbSHeiko Schocher 		ret = PTR_ERR(seen_pebs);
11390195a7bbSHeiko Schocher 		goto out_kfree;
11400195a7bbSHeiko Schocher 	}
11410195a7bbSHeiko Schocher 
1142ff94bc40SHeiko Schocher 	spin_lock(&ubi->volumes_lock);
1143ff94bc40SHeiko Schocher 	spin_lock(&ubi->wl_lock);
1144ff94bc40SHeiko Schocher 
1145ff94bc40SHeiko Schocher 	fmsb = (struct ubi_fm_sb *)fm_raw;
1146ff94bc40SHeiko Schocher 	fm_pos += sizeof(*fmsb);
1147ff94bc40SHeiko Schocher 	ubi_assert(fm_pos <= ubi->fm_size);
1148ff94bc40SHeiko Schocher 
1149ff94bc40SHeiko Schocher 	fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
1150ff94bc40SHeiko Schocher 	fm_pos += sizeof(*fmh);
1151ff94bc40SHeiko Schocher 	ubi_assert(fm_pos <= ubi->fm_size);
1152ff94bc40SHeiko Schocher 
1153ff94bc40SHeiko Schocher 	fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
1154ff94bc40SHeiko Schocher 	fmsb->version = UBI_FM_FMT_VERSION;
1155ff94bc40SHeiko Schocher 	fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
1156ff94bc40SHeiko Schocher 	/* the max sqnum will be filled in while *reading* the fastmap */
1157ff94bc40SHeiko Schocher 	fmsb->sqnum = 0;
1158ff94bc40SHeiko Schocher 
1159ff94bc40SHeiko Schocher 	fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
1160ff94bc40SHeiko Schocher 	free_peb_count = 0;
1161ff94bc40SHeiko Schocher 	used_peb_count = 0;
1162ff94bc40SHeiko Schocher 	scrub_peb_count = 0;
1163ff94bc40SHeiko Schocher 	erase_peb_count = 0;
1164ff94bc40SHeiko Schocher 	vol_count = 0;
1165ff94bc40SHeiko Schocher 
11660195a7bbSHeiko Schocher 	fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
11670195a7bbSHeiko Schocher 	fm_pos += sizeof(*fmpl);
11680195a7bbSHeiko Schocher 	fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
11690195a7bbSHeiko Schocher 	fmpl->size = cpu_to_be16(ubi->fm_pool.size);
11700195a7bbSHeiko Schocher 	fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size);
1171ff94bc40SHeiko Schocher 
11720195a7bbSHeiko Schocher 	for (i = 0; i < ubi->fm_pool.size; i++) {
11730195a7bbSHeiko Schocher 		fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
11740195a7bbSHeiko Schocher 		set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs);
11750195a7bbSHeiko Schocher 	}
1176ff94bc40SHeiko Schocher 
11770195a7bbSHeiko Schocher 	fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
11780195a7bbSHeiko Schocher 	fm_pos += sizeof(*fmpl_wl);
11790195a7bbSHeiko Schocher 	fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
11800195a7bbSHeiko Schocher 	fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size);
11810195a7bbSHeiko Schocher 	fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
1182ff94bc40SHeiko Schocher 
11830195a7bbSHeiko Schocher 	for (i = 0; i < ubi->fm_wl_pool.size; i++) {
11840195a7bbSHeiko Schocher 		fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
11850195a7bbSHeiko Schocher 		set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs);
11860195a7bbSHeiko Schocher 	}
1187ff94bc40SHeiko Schocher 
11880195a7bbSHeiko Schocher 	ubi_for_each_free_peb(ubi, wl_e, tmp_rb) {
1189ff94bc40SHeiko Schocher 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1190ff94bc40SHeiko Schocher 
1191ff94bc40SHeiko Schocher 		fec->pnum = cpu_to_be32(wl_e->pnum);
11920195a7bbSHeiko Schocher 		set_seen(ubi, wl_e->pnum, seen_pebs);
1193ff94bc40SHeiko Schocher 		fec->ec = cpu_to_be32(wl_e->ec);
1194ff94bc40SHeiko Schocher 
1195ff94bc40SHeiko Schocher 		free_peb_count++;
1196ff94bc40SHeiko Schocher 		fm_pos += sizeof(*fec);
1197ff94bc40SHeiko Schocher 		ubi_assert(fm_pos <= ubi->fm_size);
1198ff94bc40SHeiko Schocher 	}
1199ff94bc40SHeiko Schocher 	fmh->free_peb_count = cpu_to_be32(free_peb_count);
1200ff94bc40SHeiko Schocher 
12010195a7bbSHeiko Schocher 	ubi_for_each_used_peb(ubi, wl_e, tmp_rb) {
1202ff94bc40SHeiko Schocher 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1203ff94bc40SHeiko Schocher 
1204ff94bc40SHeiko Schocher 		fec->pnum = cpu_to_be32(wl_e->pnum);
12050195a7bbSHeiko Schocher 		set_seen(ubi, wl_e->pnum, seen_pebs);
12060195a7bbSHeiko Schocher 		fec->ec = cpu_to_be32(wl_e->ec);
12070195a7bbSHeiko Schocher 
12080195a7bbSHeiko Schocher 		used_peb_count++;
12090195a7bbSHeiko Schocher 		fm_pos += sizeof(*fec);
12100195a7bbSHeiko Schocher 		ubi_assert(fm_pos <= ubi->fm_size);
12110195a7bbSHeiko Schocher 	}
12120195a7bbSHeiko Schocher 
12130195a7bbSHeiko Schocher 	ubi_for_each_protected_peb(ubi, i, wl_e) {
12140195a7bbSHeiko Schocher 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
12150195a7bbSHeiko Schocher 
12160195a7bbSHeiko Schocher 		fec->pnum = cpu_to_be32(wl_e->pnum);
12170195a7bbSHeiko Schocher 		set_seen(ubi, wl_e->pnum, seen_pebs);
1218ff94bc40SHeiko Schocher 		fec->ec = cpu_to_be32(wl_e->ec);
1219ff94bc40SHeiko Schocher 
1220ff94bc40SHeiko Schocher 		used_peb_count++;
1221ff94bc40SHeiko Schocher 		fm_pos += sizeof(*fec);
1222ff94bc40SHeiko Schocher 		ubi_assert(fm_pos <= ubi->fm_size);
1223ff94bc40SHeiko Schocher 	}
1224ff94bc40SHeiko Schocher 	fmh->used_peb_count = cpu_to_be32(used_peb_count);
1225ff94bc40SHeiko Schocher 
12260195a7bbSHeiko Schocher 	ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) {
1227ff94bc40SHeiko Schocher 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1228ff94bc40SHeiko Schocher 
1229ff94bc40SHeiko Schocher 		fec->pnum = cpu_to_be32(wl_e->pnum);
12300195a7bbSHeiko Schocher 		set_seen(ubi, wl_e->pnum, seen_pebs);
1231ff94bc40SHeiko Schocher 		fec->ec = cpu_to_be32(wl_e->ec);
1232ff94bc40SHeiko Schocher 
1233ff94bc40SHeiko Schocher 		scrub_peb_count++;
1234ff94bc40SHeiko Schocher 		fm_pos += sizeof(*fec);
1235ff94bc40SHeiko Schocher 		ubi_assert(fm_pos <= ubi->fm_size);
1236ff94bc40SHeiko Schocher 	}
1237ff94bc40SHeiko Schocher 	fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
1238ff94bc40SHeiko Schocher 
1239ff94bc40SHeiko Schocher 
1240ff94bc40SHeiko Schocher 	list_for_each_entry(ubi_wrk, &ubi->works, list) {
1241ff94bc40SHeiko Schocher 		if (ubi_is_erase_work(ubi_wrk)) {
1242ff94bc40SHeiko Schocher 			wl_e = ubi_wrk->e;
1243ff94bc40SHeiko Schocher 			ubi_assert(wl_e);
1244ff94bc40SHeiko Schocher 
1245ff94bc40SHeiko Schocher 			fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1246ff94bc40SHeiko Schocher 
1247ff94bc40SHeiko Schocher 			fec->pnum = cpu_to_be32(wl_e->pnum);
12480195a7bbSHeiko Schocher 			set_seen(ubi, wl_e->pnum, seen_pebs);
1249ff94bc40SHeiko Schocher 			fec->ec = cpu_to_be32(wl_e->ec);
1250ff94bc40SHeiko Schocher 
1251ff94bc40SHeiko Schocher 			erase_peb_count++;
1252ff94bc40SHeiko Schocher 			fm_pos += sizeof(*fec);
1253ff94bc40SHeiko Schocher 			ubi_assert(fm_pos <= ubi->fm_size);
1254ff94bc40SHeiko Schocher 		}
1255ff94bc40SHeiko Schocher 	}
1256ff94bc40SHeiko Schocher 	fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
1257ff94bc40SHeiko Schocher 
1258ff94bc40SHeiko Schocher 	for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
1259ff94bc40SHeiko Schocher 		vol = ubi->volumes[i];
1260ff94bc40SHeiko Schocher 
1261ff94bc40SHeiko Schocher 		if (!vol)
1262ff94bc40SHeiko Schocher 			continue;
1263ff94bc40SHeiko Schocher 
1264ff94bc40SHeiko Schocher 		vol_count++;
1265ff94bc40SHeiko Schocher 
1266ff94bc40SHeiko Schocher 		fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
1267ff94bc40SHeiko Schocher 		fm_pos += sizeof(*fvh);
1268ff94bc40SHeiko Schocher 		ubi_assert(fm_pos <= ubi->fm_size);
1269ff94bc40SHeiko Schocher 
1270ff94bc40SHeiko Schocher 		fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
1271ff94bc40SHeiko Schocher 		fvh->vol_id = cpu_to_be32(vol->vol_id);
1272ff94bc40SHeiko Schocher 		fvh->vol_type = vol->vol_type;
1273ff94bc40SHeiko Schocher 		fvh->used_ebs = cpu_to_be32(vol->used_ebs);
1274ff94bc40SHeiko Schocher 		fvh->data_pad = cpu_to_be32(vol->data_pad);
1275ff94bc40SHeiko Schocher 		fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
1276ff94bc40SHeiko Schocher 
1277ff94bc40SHeiko Schocher 		ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
1278ff94bc40SHeiko Schocher 			vol->vol_type == UBI_STATIC_VOLUME);
1279ff94bc40SHeiko Schocher 
1280ff94bc40SHeiko Schocher 		feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
1281ff94bc40SHeiko Schocher 		fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
1282ff94bc40SHeiko Schocher 		ubi_assert(fm_pos <= ubi->fm_size);
1283ff94bc40SHeiko Schocher 
1284ff94bc40SHeiko Schocher 		for (j = 0; j < vol->reserved_pebs; j++)
1285ff94bc40SHeiko Schocher 			feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]);
1286ff94bc40SHeiko Schocher 
1287ff94bc40SHeiko Schocher 		feba->reserved_pebs = cpu_to_be32(j);
1288ff94bc40SHeiko Schocher 		feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
1289ff94bc40SHeiko Schocher 	}
1290ff94bc40SHeiko Schocher 	fmh->vol_count = cpu_to_be32(vol_count);
1291ff94bc40SHeiko Schocher 	fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
1292ff94bc40SHeiko Schocher 
1293ff94bc40SHeiko Schocher 	avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1294ff94bc40SHeiko Schocher 	avhdr->lnum = 0;
1295ff94bc40SHeiko Schocher 
1296ff94bc40SHeiko Schocher 	spin_unlock(&ubi->wl_lock);
1297ff94bc40SHeiko Schocher 	spin_unlock(&ubi->volumes_lock);
1298ff94bc40SHeiko Schocher 
1299ff94bc40SHeiko Schocher 	dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
1300ff94bc40SHeiko Schocher 	ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
1301ff94bc40SHeiko Schocher 	if (ret) {
13020195a7bbSHeiko Schocher 		ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
1303ff94bc40SHeiko Schocher 		goto out_kfree;
1304ff94bc40SHeiko Schocher 	}
1305ff94bc40SHeiko Schocher 
1306ff94bc40SHeiko Schocher 	for (i = 0; i < new_fm->used_blocks; i++) {
1307ff94bc40SHeiko Schocher 		fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
13080195a7bbSHeiko Schocher 		set_seen(ubi, new_fm->e[i]->pnum, seen_pebs);
1309ff94bc40SHeiko Schocher 		fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
1310ff94bc40SHeiko Schocher 	}
1311ff94bc40SHeiko Schocher 
1312ff94bc40SHeiko Schocher 	fmsb->data_crc = 0;
1313ff94bc40SHeiko Schocher 	fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
1314ff94bc40SHeiko Schocher 					   ubi->fm_size));
1315ff94bc40SHeiko Schocher 
1316ff94bc40SHeiko Schocher 	for (i = 1; i < new_fm->used_blocks; i++) {
1317ff94bc40SHeiko Schocher 		dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1318ff94bc40SHeiko Schocher 		dvhdr->lnum = cpu_to_be32(i);
1319ff94bc40SHeiko Schocher 		dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1320ff94bc40SHeiko Schocher 			new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
1321ff94bc40SHeiko Schocher 		ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
1322ff94bc40SHeiko Schocher 		if (ret) {
13230195a7bbSHeiko Schocher 			ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
1324ff94bc40SHeiko Schocher 				new_fm->e[i]->pnum);
1325ff94bc40SHeiko Schocher 			goto out_kfree;
1326ff94bc40SHeiko Schocher 		}
1327ff94bc40SHeiko Schocher 	}
1328ff94bc40SHeiko Schocher 
1329ff94bc40SHeiko Schocher 	for (i = 0; i < new_fm->used_blocks; i++) {
1330ff94bc40SHeiko Schocher 		ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size),
1331ff94bc40SHeiko Schocher 			new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size);
1332ff94bc40SHeiko Schocher 		if (ret) {
13330195a7bbSHeiko Schocher 			ubi_err(ubi, "unable to write fastmap to PEB %i!",
1334ff94bc40SHeiko Schocher 				new_fm->e[i]->pnum);
1335ff94bc40SHeiko Schocher 			goto out_kfree;
1336ff94bc40SHeiko Schocher 		}
1337ff94bc40SHeiko Schocher 	}
1338ff94bc40SHeiko Schocher 
1339ff94bc40SHeiko Schocher 	ubi_assert(new_fm);
1340ff94bc40SHeiko Schocher 	ubi->fm = new_fm;
1341ff94bc40SHeiko Schocher 
13420195a7bbSHeiko Schocher 	ret = self_check_seen(ubi, seen_pebs);
1343ff94bc40SHeiko Schocher 	dbg_bld("fastmap written!");
1344ff94bc40SHeiko Schocher 
1345ff94bc40SHeiko Schocher out_kfree:
1346ff94bc40SHeiko Schocher 	ubi_free_vid_hdr(ubi, avhdr);
1347ff94bc40SHeiko Schocher 	ubi_free_vid_hdr(ubi, dvhdr);
13480195a7bbSHeiko Schocher 	free_seen(seen_pebs);
1349ff94bc40SHeiko Schocher out:
1350ff94bc40SHeiko Schocher 	return ret;
1351ff94bc40SHeiko Schocher }
1352ff94bc40SHeiko Schocher 
1353ff94bc40SHeiko Schocher /**
1354ff94bc40SHeiko Schocher  * erase_block - Manually erase a PEB.
1355ff94bc40SHeiko Schocher  * @ubi: UBI device object
1356ff94bc40SHeiko Schocher  * @pnum: PEB to be erased
1357ff94bc40SHeiko Schocher  *
1358ff94bc40SHeiko Schocher  * Returns the new EC value on success, < 0 indicates an internal error.
1359ff94bc40SHeiko Schocher  */
erase_block(struct ubi_device * ubi,int pnum)1360ff94bc40SHeiko Schocher static int erase_block(struct ubi_device *ubi, int pnum)
1361ff94bc40SHeiko Schocher {
1362ff94bc40SHeiko Schocher 	int ret;
1363ff94bc40SHeiko Schocher 	struct ubi_ec_hdr *ec_hdr;
1364ff94bc40SHeiko Schocher 	long long ec;
1365ff94bc40SHeiko Schocher 
1366ff94bc40SHeiko Schocher 	ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1367ff94bc40SHeiko Schocher 	if (!ec_hdr)
1368ff94bc40SHeiko Schocher 		return -ENOMEM;
1369ff94bc40SHeiko Schocher 
1370ff94bc40SHeiko Schocher 	ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1371ff94bc40SHeiko Schocher 	if (ret < 0)
1372ff94bc40SHeiko Schocher 		goto out;
1373ff94bc40SHeiko Schocher 	else if (ret && ret != UBI_IO_BITFLIPS) {
1374ff94bc40SHeiko Schocher 		ret = -EINVAL;
1375ff94bc40SHeiko Schocher 		goto out;
1376ff94bc40SHeiko Schocher 	}
1377ff94bc40SHeiko Schocher 
1378ff94bc40SHeiko Schocher 	ret = ubi_io_sync_erase(ubi, pnum, 0);
1379ff94bc40SHeiko Schocher 	if (ret < 0)
1380ff94bc40SHeiko Schocher 		goto out;
1381ff94bc40SHeiko Schocher 
1382ff94bc40SHeiko Schocher 	ec = be64_to_cpu(ec_hdr->ec);
1383ff94bc40SHeiko Schocher 	ec += ret;
1384ff94bc40SHeiko Schocher 	if (ec > UBI_MAX_ERASECOUNTER) {
1385ff94bc40SHeiko Schocher 		ret = -EINVAL;
1386ff94bc40SHeiko Schocher 		goto out;
1387ff94bc40SHeiko Schocher 	}
1388ff94bc40SHeiko Schocher 
1389ff94bc40SHeiko Schocher 	ec_hdr->ec = cpu_to_be64(ec);
1390ff94bc40SHeiko Schocher 	ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
1391ff94bc40SHeiko Schocher 	if (ret < 0)
1392ff94bc40SHeiko Schocher 		goto out;
1393ff94bc40SHeiko Schocher 
1394ff94bc40SHeiko Schocher 	ret = ec;
1395ff94bc40SHeiko Schocher out:
1396ff94bc40SHeiko Schocher 	kfree(ec_hdr);
1397ff94bc40SHeiko Schocher 	return ret;
1398ff94bc40SHeiko Schocher }
1399ff94bc40SHeiko Schocher 
1400ff94bc40SHeiko Schocher /**
1401ff94bc40SHeiko Schocher  * invalidate_fastmap - destroys a fastmap.
1402ff94bc40SHeiko Schocher  * @ubi: UBI device object
1403ff94bc40SHeiko Schocher  *
14040195a7bbSHeiko Schocher  * This function ensures that upon next UBI attach a full scan
14050195a7bbSHeiko Schocher  * is issued. We need this if UBI is about to write a new fastmap
14060195a7bbSHeiko Schocher  * but is unable to do so. In this case we have two options:
14070195a7bbSHeiko Schocher  * a) Make sure that the current fastmap will not be usued upon
14080195a7bbSHeiko Schocher  * attach time and contine or b) fall back to RO mode to have the
14090195a7bbSHeiko Schocher  * current fastmap in a valid state.
1410ff94bc40SHeiko Schocher  * Returns 0 on success, < 0 indicates an internal error.
1411ff94bc40SHeiko Schocher  */
invalidate_fastmap(struct ubi_device * ubi)14120195a7bbSHeiko Schocher static int invalidate_fastmap(struct ubi_device *ubi)
1413ff94bc40SHeiko Schocher {
1414ff94bc40SHeiko Schocher 	int ret;
14150195a7bbSHeiko Schocher 	struct ubi_fastmap_layout *fm;
14160195a7bbSHeiko Schocher 	struct ubi_wl_entry *e;
14170195a7bbSHeiko Schocher 	struct ubi_vid_hdr *vh = NULL;
1418ff94bc40SHeiko Schocher 
14190195a7bbSHeiko Schocher 	if (!ubi->fm)
14200195a7bbSHeiko Schocher 		return 0;
14210195a7bbSHeiko Schocher 
14220195a7bbSHeiko Schocher 	ubi->fm = NULL;
14230195a7bbSHeiko Schocher 
14240195a7bbSHeiko Schocher 	ret = -ENOMEM;
14250195a7bbSHeiko Schocher 	fm = kzalloc(sizeof(*fm), GFP_KERNEL);
14260195a7bbSHeiko Schocher 	if (!fm)
14270195a7bbSHeiko Schocher 		goto out;
1428ff94bc40SHeiko Schocher 
1429ff94bc40SHeiko Schocher 	vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1430ff94bc40SHeiko Schocher 	if (!vh)
14310195a7bbSHeiko Schocher 		goto out_free_fm;
1432ff94bc40SHeiko Schocher 
14330195a7bbSHeiko Schocher 	ret = -ENOSPC;
14340195a7bbSHeiko Schocher 	e = ubi_wl_get_fm_peb(ubi, 1);
14350195a7bbSHeiko Schocher 	if (!e)
14360195a7bbSHeiko Schocher 		goto out_free_fm;
14370195a7bbSHeiko Schocher 
14380195a7bbSHeiko Schocher 	/*
14390195a7bbSHeiko Schocher 	 * Create fake fastmap such that UBI will fall back
14400195a7bbSHeiko Schocher 	 * to scanning mode.
14410195a7bbSHeiko Schocher 	 */
1442ff94bc40SHeiko Schocher 	vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
14430195a7bbSHeiko Schocher 	ret = ubi_io_write_vid_hdr(ubi, e->pnum, vh);
14440195a7bbSHeiko Schocher 	if (ret < 0) {
14450195a7bbSHeiko Schocher 		ubi_wl_put_fm_peb(ubi, e, 0, 0);
14460195a7bbSHeiko Schocher 		goto out_free_fm;
14470195a7bbSHeiko Schocher 	}
1448ff94bc40SHeiko Schocher 
14490195a7bbSHeiko Schocher 	fm->used_blocks = 1;
14500195a7bbSHeiko Schocher 	fm->e[0] = e;
14510195a7bbSHeiko Schocher 
14520195a7bbSHeiko Schocher 	ubi->fm = fm;
14530195a7bbSHeiko Schocher 
14540195a7bbSHeiko Schocher out:
14550195a7bbSHeiko Schocher 	ubi_free_vid_hdr(ubi, vh);
1456ff94bc40SHeiko Schocher 	return ret;
14570195a7bbSHeiko Schocher 
14580195a7bbSHeiko Schocher out_free_fm:
14590195a7bbSHeiko Schocher 	kfree(fm);
14600195a7bbSHeiko Schocher 	goto out;
14610195a7bbSHeiko Schocher }
14620195a7bbSHeiko Schocher 
14630195a7bbSHeiko Schocher /**
14640195a7bbSHeiko Schocher  * return_fm_pebs - returns all PEBs used by a fastmap back to the
14650195a7bbSHeiko Schocher  * WL sub-system.
14660195a7bbSHeiko Schocher  * @ubi: UBI device object
14670195a7bbSHeiko Schocher  * @fm: fastmap layout object
14680195a7bbSHeiko Schocher  */
return_fm_pebs(struct ubi_device * ubi,struct ubi_fastmap_layout * fm)14690195a7bbSHeiko Schocher static void return_fm_pebs(struct ubi_device *ubi,
14700195a7bbSHeiko Schocher 			   struct ubi_fastmap_layout *fm)
14710195a7bbSHeiko Schocher {
14720195a7bbSHeiko Schocher 	int i;
14730195a7bbSHeiko Schocher 
14740195a7bbSHeiko Schocher 	if (!fm)
14750195a7bbSHeiko Schocher 		return;
14760195a7bbSHeiko Schocher 
14770195a7bbSHeiko Schocher 	for (i = 0; i < fm->used_blocks; i++) {
14780195a7bbSHeiko Schocher 		if (fm->e[i]) {
14790195a7bbSHeiko Schocher 			ubi_wl_put_fm_peb(ubi, fm->e[i], i,
14800195a7bbSHeiko Schocher 					  fm->to_be_tortured[i]);
14810195a7bbSHeiko Schocher 			fm->e[i] = NULL;
14820195a7bbSHeiko Schocher 		}
14830195a7bbSHeiko Schocher 	}
1484ff94bc40SHeiko Schocher }
1485ff94bc40SHeiko Schocher 
1486ff94bc40SHeiko Schocher /**
1487ff94bc40SHeiko Schocher  * ubi_update_fastmap - will be called by UBI if a volume changes or
1488ff94bc40SHeiko Schocher  * a fastmap pool becomes full.
1489ff94bc40SHeiko Schocher  * @ubi: UBI device object
1490ff94bc40SHeiko Schocher  *
1491ff94bc40SHeiko Schocher  * Returns 0 on success, < 0 indicates an internal error.
1492ff94bc40SHeiko Schocher  */
ubi_update_fastmap(struct ubi_device * ubi)1493ff94bc40SHeiko Schocher int ubi_update_fastmap(struct ubi_device *ubi)
1494ff94bc40SHeiko Schocher {
14950195a7bbSHeiko Schocher 	int ret, i, j;
1496ff94bc40SHeiko Schocher 	struct ubi_fastmap_layout *new_fm, *old_fm;
1497ff94bc40SHeiko Schocher 	struct ubi_wl_entry *tmp_e;
1498ff94bc40SHeiko Schocher 
14990195a7bbSHeiko Schocher 	down_write(&ubi->fm_protect);
1500ff94bc40SHeiko Schocher 
1501ff94bc40SHeiko Schocher 	ubi_refill_pools(ubi);
1502ff94bc40SHeiko Schocher 
1503ff94bc40SHeiko Schocher 	if (ubi->ro_mode || ubi->fm_disabled) {
15040195a7bbSHeiko Schocher 		up_write(&ubi->fm_protect);
1505ff94bc40SHeiko Schocher 		return 0;
1506ff94bc40SHeiko Schocher 	}
1507ff94bc40SHeiko Schocher 
1508ff94bc40SHeiko Schocher 	ret = ubi_ensure_anchor_pebs(ubi);
1509ff94bc40SHeiko Schocher 	if (ret) {
15100195a7bbSHeiko Schocher 		up_write(&ubi->fm_protect);
1511ff94bc40SHeiko Schocher 		return ret;
1512ff94bc40SHeiko Schocher 	}
1513ff94bc40SHeiko Schocher 
1514ff94bc40SHeiko Schocher 	new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
1515ff94bc40SHeiko Schocher 	if (!new_fm) {
15160195a7bbSHeiko Schocher 		up_write(&ubi->fm_protect);
1517ff94bc40SHeiko Schocher 		return -ENOMEM;
1518ff94bc40SHeiko Schocher 	}
1519ff94bc40SHeiko Schocher 
1520ff94bc40SHeiko Schocher 	new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
1521ff94bc40SHeiko Schocher 	old_fm = ubi->fm;
1522ff94bc40SHeiko Schocher 	ubi->fm = NULL;
1523ff94bc40SHeiko Schocher 
1524ff94bc40SHeiko Schocher 	if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
15250195a7bbSHeiko Schocher 		ubi_err(ubi, "fastmap too large");
1526ff94bc40SHeiko Schocher 		ret = -ENOSPC;
1527ff94bc40SHeiko Schocher 		goto err;
1528ff94bc40SHeiko Schocher 	}
1529ff94bc40SHeiko Schocher 
1530ff94bc40SHeiko Schocher 	for (i = 1; i < new_fm->used_blocks; i++) {
1531ff94bc40SHeiko Schocher 		spin_lock(&ubi->wl_lock);
1532ff94bc40SHeiko Schocher 		tmp_e = ubi_wl_get_fm_peb(ubi, 0);
1533ff94bc40SHeiko Schocher 		spin_unlock(&ubi->wl_lock);
1534ff94bc40SHeiko Schocher 
15350195a7bbSHeiko Schocher 		if (!tmp_e) {
15360195a7bbSHeiko Schocher 			if (old_fm && old_fm->e[i]) {
15370195a7bbSHeiko Schocher 				ret = erase_block(ubi, old_fm->e[i]->pnum);
15380195a7bbSHeiko Schocher 				if (ret < 0) {
15390195a7bbSHeiko Schocher 					ubi_err(ubi, "could not erase old fastmap PEB");
1540ff94bc40SHeiko Schocher 
15410195a7bbSHeiko Schocher 					for (j = 1; j < i; j++) {
15420195a7bbSHeiko Schocher 						ubi_wl_put_fm_peb(ubi, new_fm->e[j],
15430195a7bbSHeiko Schocher 								  j, 0);
15440195a7bbSHeiko Schocher 						new_fm->e[j] = NULL;
15450195a7bbSHeiko Schocher 					}
15460195a7bbSHeiko Schocher 					goto err;
15470195a7bbSHeiko Schocher 				}
15480195a7bbSHeiko Schocher 				new_fm->e[i] = old_fm->e[i];
15490195a7bbSHeiko Schocher 				old_fm->e[i] = NULL;
15500195a7bbSHeiko Schocher 			} else {
15510195a7bbSHeiko Schocher 				ubi_err(ubi, "could not get any free erase block");
15520195a7bbSHeiko Schocher 
15530195a7bbSHeiko Schocher 				for (j = 1; j < i; j++) {
1554ff94bc40SHeiko Schocher 					ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
15550195a7bbSHeiko Schocher 					new_fm->e[j] = NULL;
15560195a7bbSHeiko Schocher 				}
1557ff94bc40SHeiko Schocher 
1558ff94bc40SHeiko Schocher 				ret = -ENOSPC;
1559ff94bc40SHeiko Schocher 				goto err;
1560ff94bc40SHeiko Schocher 			}
1561ff94bc40SHeiko Schocher 		} else {
15620195a7bbSHeiko Schocher 			new_fm->e[i] = tmp_e;
1563ff94bc40SHeiko Schocher 
15640195a7bbSHeiko Schocher 			if (old_fm && old_fm->e[i]) {
1565ff94bc40SHeiko Schocher 				ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1566ff94bc40SHeiko Schocher 						  old_fm->to_be_tortured[i]);
15670195a7bbSHeiko Schocher 				old_fm->e[i] = NULL;
15680195a7bbSHeiko Schocher 			}
15690195a7bbSHeiko Schocher 		}
15700195a7bbSHeiko Schocher 	}
15710195a7bbSHeiko Schocher 
15720195a7bbSHeiko Schocher 	/* Old fastmap is larger than the new one */
15730195a7bbSHeiko Schocher 	if (old_fm && new_fm->used_blocks < old_fm->used_blocks) {
15740195a7bbSHeiko Schocher 		for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) {
15750195a7bbSHeiko Schocher 			ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
15760195a7bbSHeiko Schocher 					  old_fm->to_be_tortured[i]);
15770195a7bbSHeiko Schocher 			old_fm->e[i] = NULL;
1578ff94bc40SHeiko Schocher 		}
1579ff94bc40SHeiko Schocher 	}
1580ff94bc40SHeiko Schocher 
1581ff94bc40SHeiko Schocher 	spin_lock(&ubi->wl_lock);
1582ff94bc40SHeiko Schocher 	tmp_e = ubi_wl_get_fm_peb(ubi, 1);
1583ff94bc40SHeiko Schocher 	spin_unlock(&ubi->wl_lock);
1584ff94bc40SHeiko Schocher 
1585ff94bc40SHeiko Schocher 	if (old_fm) {
1586ff94bc40SHeiko Schocher 		/* no fresh anchor PEB was found, reuse the old one */
1587ff94bc40SHeiko Schocher 		if (!tmp_e) {
1588ff94bc40SHeiko Schocher 			ret = erase_block(ubi, old_fm->e[0]->pnum);
1589ff94bc40SHeiko Schocher 			if (ret < 0) {
15900195a7bbSHeiko Schocher 				ubi_err(ubi, "could not erase old anchor PEB");
1591ff94bc40SHeiko Schocher 
15920195a7bbSHeiko Schocher 				for (i = 1; i < new_fm->used_blocks; i++) {
1593ff94bc40SHeiko Schocher 					ubi_wl_put_fm_peb(ubi, new_fm->e[i],
1594ff94bc40SHeiko Schocher 							  i, 0);
15950195a7bbSHeiko Schocher 					new_fm->e[i] = NULL;
15960195a7bbSHeiko Schocher 				}
1597ff94bc40SHeiko Schocher 				goto err;
1598ff94bc40SHeiko Schocher 			}
15990195a7bbSHeiko Schocher 			new_fm->e[0] = old_fm->e[0];
1600ff94bc40SHeiko Schocher 			new_fm->e[0]->ec = ret;
16010195a7bbSHeiko Schocher 			old_fm->e[0] = NULL;
1602ff94bc40SHeiko Schocher 		} else {
1603ff94bc40SHeiko Schocher 			/* we've got a new anchor PEB, return the old one */
1604ff94bc40SHeiko Schocher 			ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
1605ff94bc40SHeiko Schocher 					  old_fm->to_be_tortured[0]);
16060195a7bbSHeiko Schocher 			new_fm->e[0] = tmp_e;
16070195a7bbSHeiko Schocher 			old_fm->e[0] = NULL;
1608ff94bc40SHeiko Schocher 		}
1609ff94bc40SHeiko Schocher 	} else {
1610ff94bc40SHeiko Schocher 		if (!tmp_e) {
16110195a7bbSHeiko Schocher 			ubi_err(ubi, "could not find any anchor PEB");
1612ff94bc40SHeiko Schocher 
16130195a7bbSHeiko Schocher 			for (i = 1; i < new_fm->used_blocks; i++) {
1614ff94bc40SHeiko Schocher 				ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
16150195a7bbSHeiko Schocher 				new_fm->e[i] = NULL;
16160195a7bbSHeiko Schocher 			}
1617ff94bc40SHeiko Schocher 
1618ff94bc40SHeiko Schocher 			ret = -ENOSPC;
1619ff94bc40SHeiko Schocher 			goto err;
1620ff94bc40SHeiko Schocher 		}
16210195a7bbSHeiko Schocher 		new_fm->e[0] = tmp_e;
1622ff94bc40SHeiko Schocher 	}
1623ff94bc40SHeiko Schocher 
1624ff94bc40SHeiko Schocher 	down_write(&ubi->work_sem);
16250195a7bbSHeiko Schocher 	down_write(&ubi->fm_eba_sem);
1626ff94bc40SHeiko Schocher 	ret = ubi_write_fastmap(ubi, new_fm);
16270195a7bbSHeiko Schocher 	up_write(&ubi->fm_eba_sem);
1628ff94bc40SHeiko Schocher 	up_write(&ubi->work_sem);
1629ff94bc40SHeiko Schocher 
1630ff94bc40SHeiko Schocher 	if (ret)
1631ff94bc40SHeiko Schocher 		goto err;
1632ff94bc40SHeiko Schocher 
1633ff94bc40SHeiko Schocher out_unlock:
16340195a7bbSHeiko Schocher 	up_write(&ubi->fm_protect);
1635ff94bc40SHeiko Schocher 	kfree(old_fm);
1636ff94bc40SHeiko Schocher 	return ret;
1637ff94bc40SHeiko Schocher 
1638ff94bc40SHeiko Schocher err:
16390195a7bbSHeiko Schocher 	ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret);
1640ff94bc40SHeiko Schocher 
16410195a7bbSHeiko Schocher 	ret = invalidate_fastmap(ubi);
16420195a7bbSHeiko Schocher 	if (ret < 0) {
16430195a7bbSHeiko Schocher 		ubi_err(ubi, "Unable to invalidiate current fastmap!");
16440195a7bbSHeiko Schocher 		ubi_ro_mode(ubi);
16450195a7bbSHeiko Schocher 	} else {
16460195a7bbSHeiko Schocher 		return_fm_pebs(ubi, old_fm);
16470195a7bbSHeiko Schocher 		return_fm_pebs(ubi, new_fm);
1648ff94bc40SHeiko Schocher 		ret = 0;
1649ff94bc40SHeiko Schocher 	}
16500195a7bbSHeiko Schocher 
16510195a7bbSHeiko Schocher 	kfree(new_fm);
1652ff94bc40SHeiko Schocher 	goto out_unlock;
1653ff94bc40SHeiko Schocher }
1654