xref: /openbmc/linux/drivers/mtd/ubi/fastmap.c (revision fcbb6af17bda4b3856a1f4c302da5d0d7bf9a0f9)
1dbb7d2a8SRichard Weinberger /*
2dbb7d2a8SRichard Weinberger  * Copyright (c) 2012 Linutronix GmbH
3daef3dd1SRichard Weinberger  * Copyright (c) 2014 sigma star gmbh
4dbb7d2a8SRichard Weinberger  * Author: Richard Weinberger <richard@nod.at>
5dbb7d2a8SRichard Weinberger  *
6dbb7d2a8SRichard Weinberger  * This program is free software; you can redistribute it and/or modify
7dbb7d2a8SRichard Weinberger  * it under the terms of the GNU General Public License as published by
8dbb7d2a8SRichard Weinberger  * the Free Software Foundation; version 2.
9dbb7d2a8SRichard Weinberger  *
10dbb7d2a8SRichard Weinberger  * This program is distributed in the hope that it will be useful,
11dbb7d2a8SRichard Weinberger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12dbb7d2a8SRichard Weinberger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13dbb7d2a8SRichard Weinberger  * the GNU General Public License for more details.
14dbb7d2a8SRichard Weinberger  *
15dbb7d2a8SRichard Weinberger  */
16dbb7d2a8SRichard Weinberger 
17dbb7d2a8SRichard Weinberger #include <linux/crc32.h>
185d71afb0SRichard Weinberger #include <linux/bitmap.h>
19dbb7d2a8SRichard Weinberger #include "ubi.h"
20dbb7d2a8SRichard Weinberger 
21dbb7d2a8SRichard Weinberger /**
22daef3dd1SRichard Weinberger  * init_seen - allocate memory for used for debugging.
23daef3dd1SRichard Weinberger  * @ubi: UBI device description object
24daef3dd1SRichard Weinberger  */
255d71afb0SRichard Weinberger static inline unsigned long *init_seen(struct ubi_device *ubi)
26daef3dd1SRichard Weinberger {
275d71afb0SRichard Weinberger 	unsigned long *ret;
28daef3dd1SRichard Weinberger 
29daef3dd1SRichard Weinberger 	if (!ubi_dbg_chk_fastmap(ubi))
30daef3dd1SRichard Weinberger 		return NULL;
31daef3dd1SRichard Weinberger 
325d71afb0SRichard Weinberger 	ret = kcalloc(BITS_TO_LONGS(ubi->peb_count), sizeof(unsigned long),
335d71afb0SRichard Weinberger 		      GFP_KERNEL);
34daef3dd1SRichard Weinberger 	if (!ret)
35daef3dd1SRichard Weinberger 		return ERR_PTR(-ENOMEM);
36daef3dd1SRichard Weinberger 
37daef3dd1SRichard Weinberger 	return ret;
38daef3dd1SRichard Weinberger }
39daef3dd1SRichard Weinberger 
40daef3dd1SRichard Weinberger /**
41daef3dd1SRichard Weinberger  * free_seen - free the seen logic integer array.
42daef3dd1SRichard Weinberger  * @seen: integer array of @ubi->peb_count size
43daef3dd1SRichard Weinberger  */
445d71afb0SRichard Weinberger static inline void free_seen(unsigned long *seen)
45daef3dd1SRichard Weinberger {
46daef3dd1SRichard Weinberger 	kfree(seen);
47daef3dd1SRichard Weinberger }
48daef3dd1SRichard Weinberger 
49daef3dd1SRichard Weinberger /**
50daef3dd1SRichard Weinberger  * set_seen - mark a PEB as seen.
51daef3dd1SRichard Weinberger  * @ubi: UBI device description object
52daef3dd1SRichard Weinberger  * @pnum: The PEB to be makred as seen
53daef3dd1SRichard Weinberger  * @seen: integer array of @ubi->peb_count size
54daef3dd1SRichard Weinberger  */
555d71afb0SRichard Weinberger static inline void set_seen(struct ubi_device *ubi, int pnum, unsigned long *seen)
56daef3dd1SRichard Weinberger {
57daef3dd1SRichard Weinberger 	if (!ubi_dbg_chk_fastmap(ubi) || !seen)
58daef3dd1SRichard Weinberger 		return;
59daef3dd1SRichard Weinberger 
605d71afb0SRichard Weinberger 	set_bit(pnum, seen);
61daef3dd1SRichard Weinberger }
62daef3dd1SRichard Weinberger 
63daef3dd1SRichard Weinberger /**
64daef3dd1SRichard Weinberger  * self_check_seen - check whether all PEB have been seen by fastmap.
65daef3dd1SRichard Weinberger  * @ubi: UBI device description object
66daef3dd1SRichard Weinberger  * @seen: integer array of @ubi->peb_count size
67daef3dd1SRichard Weinberger  */
685d71afb0SRichard Weinberger static int self_check_seen(struct ubi_device *ubi, unsigned long *seen)
69daef3dd1SRichard Weinberger {
70daef3dd1SRichard Weinberger 	int pnum, ret = 0;
71daef3dd1SRichard Weinberger 
72daef3dd1SRichard Weinberger 	if (!ubi_dbg_chk_fastmap(ubi) || !seen)
73daef3dd1SRichard Weinberger 		return 0;
74daef3dd1SRichard Weinberger 
75daef3dd1SRichard Weinberger 	for (pnum = 0; pnum < ubi->peb_count; pnum++) {
765d71afb0SRichard Weinberger 		if (test_bit(pnum, seen) && ubi->lookuptbl[pnum]) {
77daef3dd1SRichard Weinberger 			ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum);
78daef3dd1SRichard Weinberger 			ret = -EINVAL;
79daef3dd1SRichard Weinberger 		}
80daef3dd1SRichard Weinberger 	}
81daef3dd1SRichard Weinberger 
82daef3dd1SRichard Weinberger 	return ret;
83daef3dd1SRichard Weinberger }
84daef3dd1SRichard Weinberger 
85daef3dd1SRichard Weinberger /**
86dbb7d2a8SRichard Weinberger  * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
87dbb7d2a8SRichard Weinberger  * @ubi: UBI device description object
88dbb7d2a8SRichard Weinberger  */
89dbb7d2a8SRichard Weinberger size_t ubi_calc_fm_size(struct ubi_device *ubi)
90dbb7d2a8SRichard Weinberger {
91dbb7d2a8SRichard Weinberger 	size_t size;
92dbb7d2a8SRichard Weinberger 
93a18fd672Sshengyong 	size = sizeof(struct ubi_fm_sb) +
94a18fd672Sshengyong 		sizeof(struct ubi_fm_hdr) +
95a18fd672Sshengyong 		sizeof(struct ubi_fm_scan_pool) +
96a18fd672Sshengyong 		sizeof(struct ubi_fm_scan_pool) +
97a18fd672Sshengyong 		(ubi->peb_count * sizeof(struct ubi_fm_ec)) +
98a18fd672Sshengyong 		(sizeof(struct ubi_fm_eba) +
99a18fd672Sshengyong 		(ubi->peb_count * sizeof(__be32))) +
100dbb7d2a8SRichard Weinberger 		sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
101dbb7d2a8SRichard Weinberger 	return roundup(size, ubi->leb_size);
102dbb7d2a8SRichard Weinberger }
103dbb7d2a8SRichard Weinberger 
104dbb7d2a8SRichard Weinberger 
105dbb7d2a8SRichard Weinberger /**
106dbb7d2a8SRichard Weinberger  * new_fm_vhdr - allocate a new volume header for fastmap usage.
107dbb7d2a8SRichard Weinberger  * @ubi: UBI device description object
108dbb7d2a8SRichard Weinberger  * @vol_id: the VID of the new header
109dbb7d2a8SRichard Weinberger  *
110dbb7d2a8SRichard Weinberger  * Returns a new struct ubi_vid_hdr on success.
111dbb7d2a8SRichard Weinberger  * NULL indicates out of memory.
112dbb7d2a8SRichard Weinberger  */
113dbb7d2a8SRichard Weinberger static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id)
114dbb7d2a8SRichard Weinberger {
115dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *new;
116dbb7d2a8SRichard Weinberger 
117dbb7d2a8SRichard Weinberger 	new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
118dbb7d2a8SRichard Weinberger 	if (!new)
119dbb7d2a8SRichard Weinberger 		goto out;
120dbb7d2a8SRichard Weinberger 
121dbb7d2a8SRichard Weinberger 	new->vol_type = UBI_VID_DYNAMIC;
122dbb7d2a8SRichard Weinberger 	new->vol_id = cpu_to_be32(vol_id);
123dbb7d2a8SRichard Weinberger 
124dbb7d2a8SRichard Weinberger 	/* UBI implementations without fastmap support have to delete the
125dbb7d2a8SRichard Weinberger 	 * fastmap.
126dbb7d2a8SRichard Weinberger 	 */
127dbb7d2a8SRichard Weinberger 	new->compat = UBI_COMPAT_DELETE;
128dbb7d2a8SRichard Weinberger 
129dbb7d2a8SRichard Weinberger out:
130dbb7d2a8SRichard Weinberger 	return new;
131dbb7d2a8SRichard Weinberger }
132dbb7d2a8SRichard Weinberger 
133dbb7d2a8SRichard Weinberger /**
134dbb7d2a8SRichard Weinberger  * add_aeb - create and add a attach erase block to a given list.
135dbb7d2a8SRichard Weinberger  * @ai: UBI attach info object
136dbb7d2a8SRichard Weinberger  * @list: the target list
137dbb7d2a8SRichard Weinberger  * @pnum: PEB number of the new attach erase block
138dbb7d2a8SRichard Weinberger  * @ec: erease counter of the new LEB
139dbb7d2a8SRichard Weinberger  * @scrub: scrub this PEB after attaching
140dbb7d2a8SRichard Weinberger  *
141dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
142dbb7d2a8SRichard Weinberger  */
143dbb7d2a8SRichard Weinberger static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
144dbb7d2a8SRichard Weinberger 		   int pnum, int ec, int scrub)
145dbb7d2a8SRichard Weinberger {
146dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb;
147dbb7d2a8SRichard Weinberger 
148dbb7d2a8SRichard Weinberger 	aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
149dbb7d2a8SRichard Weinberger 	if (!aeb)
150dbb7d2a8SRichard Weinberger 		return -ENOMEM;
151dbb7d2a8SRichard Weinberger 
152dbb7d2a8SRichard Weinberger 	aeb->pnum = pnum;
153dbb7d2a8SRichard Weinberger 	aeb->ec = ec;
154dbb7d2a8SRichard Weinberger 	aeb->lnum = -1;
155dbb7d2a8SRichard Weinberger 	aeb->scrub = scrub;
156dbb7d2a8SRichard Weinberger 	aeb->copy_flag = aeb->sqnum = 0;
157dbb7d2a8SRichard Weinberger 
158dbb7d2a8SRichard Weinberger 	ai->ec_sum += aeb->ec;
159dbb7d2a8SRichard Weinberger 	ai->ec_count++;
160dbb7d2a8SRichard Weinberger 
161dbb7d2a8SRichard Weinberger 	if (ai->max_ec < aeb->ec)
162dbb7d2a8SRichard Weinberger 		ai->max_ec = aeb->ec;
163dbb7d2a8SRichard Weinberger 
164dbb7d2a8SRichard Weinberger 	if (ai->min_ec > aeb->ec)
165dbb7d2a8SRichard Weinberger 		ai->min_ec = aeb->ec;
166dbb7d2a8SRichard Weinberger 
167dbb7d2a8SRichard Weinberger 	list_add_tail(&aeb->u.list, list);
168dbb7d2a8SRichard Weinberger 
169dbb7d2a8SRichard Weinberger 	return 0;
170dbb7d2a8SRichard Weinberger }
171dbb7d2a8SRichard Weinberger 
172dbb7d2a8SRichard Weinberger /**
173dbb7d2a8SRichard Weinberger  * add_vol - create and add a new volume to ubi_attach_info.
174dbb7d2a8SRichard Weinberger  * @ai: ubi_attach_info object
175dbb7d2a8SRichard Weinberger  * @vol_id: VID of the new volume
176dbb7d2a8SRichard Weinberger  * @used_ebs: number of used EBS
177dbb7d2a8SRichard Weinberger  * @data_pad: data padding value of the new volume
178dbb7d2a8SRichard Weinberger  * @vol_type: volume type
179dbb7d2a8SRichard Weinberger  * @last_eb_bytes: number of bytes in the last LEB
180dbb7d2a8SRichard Weinberger  *
181dbb7d2a8SRichard Weinberger  * Returns the new struct ubi_ainf_volume on success.
182dbb7d2a8SRichard Weinberger  * NULL indicates an error.
183dbb7d2a8SRichard Weinberger  */
184dbb7d2a8SRichard Weinberger static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id,
185dbb7d2a8SRichard Weinberger 				       int used_ebs, int data_pad, u8 vol_type,
186dbb7d2a8SRichard Weinberger 				       int last_eb_bytes)
187dbb7d2a8SRichard Weinberger {
188dbb7d2a8SRichard Weinberger 	struct ubi_ainf_volume *av;
189dbb7d2a8SRichard Weinberger 
190de4c455bSBoris Brezillon 	av = ubi_add_av(ai, vol_id);
191de4c455bSBoris Brezillon 	if (IS_ERR(av))
192de4c455bSBoris Brezillon 		return av;
193dbb7d2a8SRichard Weinberger 
194dbb7d2a8SRichard Weinberger 	av->data_pad = data_pad;
195dbb7d2a8SRichard Weinberger 	av->last_data_size = last_eb_bytes;
196dbb7d2a8SRichard Weinberger 	av->compat = 0;
197dbb7d2a8SRichard Weinberger 	av->vol_type = vol_type;
19842dd3cdcSRichard Weinberger 	if (av->vol_type == UBI_STATIC_VOLUME)
19942dd3cdcSRichard Weinberger 		av->used_ebs = used_ebs;
200dbb7d2a8SRichard Weinberger 
201dbb7d2a8SRichard Weinberger 	dbg_bld("found volume (ID %i)", vol_id);
202dbb7d2a8SRichard Weinberger 	return av;
203dbb7d2a8SRichard Weinberger }
204dbb7d2a8SRichard Weinberger 
205dbb7d2a8SRichard Weinberger /**
206dbb7d2a8SRichard Weinberger  * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
207dbb7d2a8SRichard Weinberger  * from it's original list.
208dbb7d2a8SRichard Weinberger  * @ai: ubi_attach_info object
209dbb7d2a8SRichard Weinberger  * @aeb: the to be assigned SEB
210dbb7d2a8SRichard Weinberger  * @av: target scan volume
211dbb7d2a8SRichard Weinberger  */
212dbb7d2a8SRichard Weinberger static void assign_aeb_to_av(struct ubi_attach_info *ai,
213dbb7d2a8SRichard Weinberger 			     struct ubi_ainf_peb *aeb,
214dbb7d2a8SRichard Weinberger 			     struct ubi_ainf_volume *av)
215dbb7d2a8SRichard Weinberger {
216dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *tmp_aeb;
217dbb7d2a8SRichard Weinberger 	struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
218dbb7d2a8SRichard Weinberger 
219dbb7d2a8SRichard Weinberger 	p = &av->root.rb_node;
220dbb7d2a8SRichard Weinberger 	while (*p) {
221dbb7d2a8SRichard Weinberger 		parent = *p;
222dbb7d2a8SRichard Weinberger 
223dbb7d2a8SRichard Weinberger 		tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
224dbb7d2a8SRichard Weinberger 		if (aeb->lnum != tmp_aeb->lnum) {
225dbb7d2a8SRichard Weinberger 			if (aeb->lnum < tmp_aeb->lnum)
226dbb7d2a8SRichard Weinberger 				p = &(*p)->rb_left;
227dbb7d2a8SRichard Weinberger 			else
228dbb7d2a8SRichard Weinberger 				p = &(*p)->rb_right;
229dbb7d2a8SRichard Weinberger 
230dbb7d2a8SRichard Weinberger 			continue;
231dbb7d2a8SRichard Weinberger 		} else
232dbb7d2a8SRichard Weinberger 			break;
233dbb7d2a8SRichard Weinberger 	}
234dbb7d2a8SRichard Weinberger 
235dbb7d2a8SRichard Weinberger 	list_del(&aeb->u.list);
236dbb7d2a8SRichard Weinberger 	av->leb_count++;
237dbb7d2a8SRichard Weinberger 
238dbb7d2a8SRichard Weinberger 	rb_link_node(&aeb->u.rb, parent, p);
239dbb7d2a8SRichard Weinberger 	rb_insert_color(&aeb->u.rb, &av->root);
240dbb7d2a8SRichard Weinberger }
241dbb7d2a8SRichard Weinberger 
242dbb7d2a8SRichard Weinberger /**
243dbb7d2a8SRichard Weinberger  * update_vol - inserts or updates a LEB which was found a pool.
244dbb7d2a8SRichard Weinberger  * @ubi: the UBI device object
245dbb7d2a8SRichard Weinberger  * @ai: attach info object
246dbb7d2a8SRichard Weinberger  * @av: the volume this LEB belongs to
247dbb7d2a8SRichard Weinberger  * @new_vh: the volume header derived from new_aeb
248dbb7d2a8SRichard Weinberger  * @new_aeb: the AEB to be examined
249dbb7d2a8SRichard Weinberger  *
250dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
251dbb7d2a8SRichard Weinberger  */
252dbb7d2a8SRichard Weinberger static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
253dbb7d2a8SRichard Weinberger 		      struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh,
254dbb7d2a8SRichard Weinberger 		      struct ubi_ainf_peb *new_aeb)
255dbb7d2a8SRichard Weinberger {
256dbb7d2a8SRichard Weinberger 	struct rb_node **p = &av->root.rb_node, *parent = NULL;
257dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb, *victim;
258dbb7d2a8SRichard Weinberger 	int cmp_res;
259dbb7d2a8SRichard Weinberger 
260dbb7d2a8SRichard Weinberger 	while (*p) {
261dbb7d2a8SRichard Weinberger 		parent = *p;
262dbb7d2a8SRichard Weinberger 		aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
263dbb7d2a8SRichard Weinberger 
264dbb7d2a8SRichard Weinberger 		if (be32_to_cpu(new_vh->lnum) != aeb->lnum) {
265dbb7d2a8SRichard Weinberger 			if (be32_to_cpu(new_vh->lnum) < aeb->lnum)
266dbb7d2a8SRichard Weinberger 				p = &(*p)->rb_left;
267dbb7d2a8SRichard Weinberger 			else
268dbb7d2a8SRichard Weinberger 				p = &(*p)->rb_right;
269dbb7d2a8SRichard Weinberger 
270dbb7d2a8SRichard Weinberger 			continue;
271dbb7d2a8SRichard Weinberger 		}
272dbb7d2a8SRichard Weinberger 
273dbb7d2a8SRichard Weinberger 		/* This case can happen if the fastmap gets written
274dbb7d2a8SRichard Weinberger 		 * because of a volume change (creation, deletion, ..).
275dbb7d2a8SRichard Weinberger 		 * Then a PEB can be within the persistent EBA and the pool.
276dbb7d2a8SRichard Weinberger 		 */
277dbb7d2a8SRichard Weinberger 		if (aeb->pnum == new_aeb->pnum) {
278dbb7d2a8SRichard Weinberger 			ubi_assert(aeb->lnum == new_aeb->lnum);
279dbb7d2a8SRichard Weinberger 			kmem_cache_free(ai->aeb_slab_cache, new_aeb);
280dbb7d2a8SRichard Weinberger 
281dbb7d2a8SRichard Weinberger 			return 0;
282dbb7d2a8SRichard Weinberger 		}
283dbb7d2a8SRichard Weinberger 
284dbb7d2a8SRichard Weinberger 		cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh);
285dbb7d2a8SRichard Weinberger 		if (cmp_res < 0)
286dbb7d2a8SRichard Weinberger 			return cmp_res;
287dbb7d2a8SRichard Weinberger 
288dbb7d2a8SRichard Weinberger 		/* new_aeb is newer */
289dbb7d2a8SRichard Weinberger 		if (cmp_res & 1) {
290dbb7d2a8SRichard Weinberger 			victim = kmem_cache_alloc(ai->aeb_slab_cache,
291dbb7d2a8SRichard Weinberger 				GFP_KERNEL);
292dbb7d2a8SRichard Weinberger 			if (!victim)
293dbb7d2a8SRichard Weinberger 				return -ENOMEM;
294dbb7d2a8SRichard Weinberger 
295dbb7d2a8SRichard Weinberger 			victim->ec = aeb->ec;
296dbb7d2a8SRichard Weinberger 			victim->pnum = aeb->pnum;
297dbb7d2a8SRichard Weinberger 			list_add_tail(&victim->u.list, &ai->erase);
298dbb7d2a8SRichard Weinberger 
299dbb7d2a8SRichard Weinberger 			if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
300669d3d12Sshengyong 				av->last_data_size =
301dbb7d2a8SRichard Weinberger 					be32_to_cpu(new_vh->data_size);
302dbb7d2a8SRichard Weinberger 
303dbb7d2a8SRichard Weinberger 			dbg_bld("vol %i: AEB %i's PEB %i is the newer",
304dbb7d2a8SRichard Weinberger 				av->vol_id, aeb->lnum, new_aeb->pnum);
305dbb7d2a8SRichard Weinberger 
306dbb7d2a8SRichard Weinberger 			aeb->ec = new_aeb->ec;
307dbb7d2a8SRichard Weinberger 			aeb->pnum = new_aeb->pnum;
308dbb7d2a8SRichard Weinberger 			aeb->copy_flag = new_vh->copy_flag;
309dbb7d2a8SRichard Weinberger 			aeb->scrub = new_aeb->scrub;
310dbb7d2a8SRichard Weinberger 			kmem_cache_free(ai->aeb_slab_cache, new_aeb);
311dbb7d2a8SRichard Weinberger 
312dbb7d2a8SRichard Weinberger 		/* new_aeb is older */
313dbb7d2a8SRichard Weinberger 		} else {
314dbb7d2a8SRichard Weinberger 			dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
315dbb7d2a8SRichard Weinberger 				av->vol_id, aeb->lnum, new_aeb->pnum);
316dbb7d2a8SRichard Weinberger 			list_add_tail(&new_aeb->u.list, &ai->erase);
317dbb7d2a8SRichard Weinberger 		}
318dbb7d2a8SRichard Weinberger 
319dbb7d2a8SRichard Weinberger 		return 0;
320dbb7d2a8SRichard Weinberger 	}
321dbb7d2a8SRichard Weinberger 	/* This LEB is new, let's add it to the volume */
322dbb7d2a8SRichard Weinberger 
323dbb7d2a8SRichard Weinberger 	if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
324dbb7d2a8SRichard Weinberger 		av->highest_lnum = be32_to_cpu(new_vh->lnum);
325dbb7d2a8SRichard Weinberger 		av->last_data_size = be32_to_cpu(new_vh->data_size);
326dbb7d2a8SRichard Weinberger 	}
327dbb7d2a8SRichard Weinberger 
328dbb7d2a8SRichard Weinberger 	if (av->vol_type == UBI_STATIC_VOLUME)
329dbb7d2a8SRichard Weinberger 		av->used_ebs = be32_to_cpu(new_vh->used_ebs);
330dbb7d2a8SRichard Weinberger 
331dbb7d2a8SRichard Weinberger 	av->leb_count++;
332dbb7d2a8SRichard Weinberger 
333dbb7d2a8SRichard Weinberger 	rb_link_node(&new_aeb->u.rb, parent, p);
334dbb7d2a8SRichard Weinberger 	rb_insert_color(&new_aeb->u.rb, &av->root);
335dbb7d2a8SRichard Weinberger 
336dbb7d2a8SRichard Weinberger 	return 0;
337dbb7d2a8SRichard Weinberger }
338dbb7d2a8SRichard Weinberger 
339dbb7d2a8SRichard Weinberger /**
340dbb7d2a8SRichard Weinberger  * process_pool_aeb - we found a non-empty PEB in a pool.
341dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
342dbb7d2a8SRichard Weinberger  * @ai: attach info object
343dbb7d2a8SRichard Weinberger  * @new_vh: the volume header derived from new_aeb
344dbb7d2a8SRichard Weinberger  * @new_aeb: the AEB to be examined
345dbb7d2a8SRichard Weinberger  *
346dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
347dbb7d2a8SRichard Weinberger  */
348dbb7d2a8SRichard Weinberger static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
349dbb7d2a8SRichard Weinberger 			    struct ubi_vid_hdr *new_vh,
350dbb7d2a8SRichard Weinberger 			    struct ubi_ainf_peb *new_aeb)
351dbb7d2a8SRichard Weinberger {
3524c368421SBoris Brezillon 	int vol_id = be32_to_cpu(new_vh->vol_id);
353f5f9d43fSBoris Brezillon 	struct ubi_ainf_volume *av;
354dbb7d2a8SRichard Weinberger 
3554c368421SBoris Brezillon 	if (vol_id == UBI_FM_SB_VOLUME_ID || vol_id == UBI_FM_DATA_VOLUME_ID) {
356dbb7d2a8SRichard Weinberger 		kmem_cache_free(ai->aeb_slab_cache, new_aeb);
357dbb7d2a8SRichard Weinberger 
358dbb7d2a8SRichard Weinberger 		return 0;
359dbb7d2a8SRichard Weinberger 	}
360dbb7d2a8SRichard Weinberger 
361dbb7d2a8SRichard Weinberger 	/* Find the volume this SEB belongs to */
3624c368421SBoris Brezillon 	av = ubi_find_av(ai, vol_id);
363f5f9d43fSBoris Brezillon 	if (!av) {
36432608703STanya Brokhman 		ubi_err(ubi, "orphaned volume in fastmap pool!");
3651bf1890eSRichard Genoud 		kmem_cache_free(ai->aeb_slab_cache, new_aeb);
366dbb7d2a8SRichard Weinberger 		return UBI_BAD_FASTMAP;
367dbb7d2a8SRichard Weinberger 	}
368dbb7d2a8SRichard Weinberger 
3694c368421SBoris Brezillon 	ubi_assert(vol_id == av->vol_id);
370dbb7d2a8SRichard Weinberger 
371dbb7d2a8SRichard Weinberger 	return update_vol(ubi, ai, av, new_vh, new_aeb);
372dbb7d2a8SRichard Weinberger }
373dbb7d2a8SRichard Weinberger 
374dbb7d2a8SRichard Weinberger /**
375dbb7d2a8SRichard Weinberger  * unmap_peb - unmap a PEB.
376dbb7d2a8SRichard Weinberger  * If fastmap detects a free PEB in the pool it has to check whether
377dbb7d2a8SRichard Weinberger  * this PEB has been unmapped after writing the fastmap.
378dbb7d2a8SRichard Weinberger  *
379dbb7d2a8SRichard Weinberger  * @ai: UBI attach info object
380dbb7d2a8SRichard Weinberger  * @pnum: The PEB to be unmapped
381dbb7d2a8SRichard Weinberger  */
382dbb7d2a8SRichard Weinberger static void unmap_peb(struct ubi_attach_info *ai, int pnum)
383dbb7d2a8SRichard Weinberger {
384dbb7d2a8SRichard Weinberger 	struct ubi_ainf_volume *av;
385dbb7d2a8SRichard Weinberger 	struct rb_node *node, *node2;
386dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb;
387dbb7d2a8SRichard Weinberger 
388f2fb1346SBoris Brezillon 	ubi_rb_for_each_entry(node, av, &ai->volumes, rb) {
389f2fb1346SBoris Brezillon 		ubi_rb_for_each_entry(node2, aeb, &av->root, u.rb) {
390dbb7d2a8SRichard Weinberger 			if (aeb->pnum == pnum) {
391dbb7d2a8SRichard Weinberger 				rb_erase(&aeb->u.rb, &av->root);
392ad3d6a05SRichard Weinberger 				av->leb_count--;
393dbb7d2a8SRichard Weinberger 				kmem_cache_free(ai->aeb_slab_cache, aeb);
394dbb7d2a8SRichard Weinberger 				return;
395dbb7d2a8SRichard Weinberger 			}
396dbb7d2a8SRichard Weinberger 		}
397dbb7d2a8SRichard Weinberger 	}
398dbb7d2a8SRichard Weinberger }
399dbb7d2a8SRichard Weinberger 
400dbb7d2a8SRichard Weinberger /**
401dbb7d2a8SRichard Weinberger  * scan_pool - scans a pool for changed (no longer empty PEBs).
402dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
403dbb7d2a8SRichard Weinberger  * @ai: attach info object
404dbb7d2a8SRichard Weinberger  * @pebs: an array of all PEB numbers in the to be scanned pool
405dbb7d2a8SRichard Weinberger  * @pool_size: size of the pool (number of entries in @pebs)
406dbb7d2a8SRichard Weinberger  * @max_sqnum: pointer to the maximal sequence number
407dbb7d2a8SRichard Weinberger  * @free: list of PEBs which are most likely free (and go into @ai->free)
408dbb7d2a8SRichard Weinberger  *
409dbb7d2a8SRichard Weinberger  * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
410dbb7d2a8SRichard Weinberger  * < 0 indicates an internal error.
411dbb7d2a8SRichard Weinberger  */
412dbb7d2a8SRichard Weinberger static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
4132a130f1aSEzequiel García 		     __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
414d141a8efSRichard Weinberger 		     struct list_head *free)
415dbb7d2a8SRichard Weinberger {
416dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *vh;
417dbb7d2a8SRichard Weinberger 	struct ubi_ec_hdr *ech;
418d141a8efSRichard Weinberger 	struct ubi_ainf_peb *new_aeb;
419d141a8efSRichard Weinberger 	int i, pnum, err, ret = 0;
420dbb7d2a8SRichard Weinberger 
421dbb7d2a8SRichard Weinberger 	ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
422dbb7d2a8SRichard Weinberger 	if (!ech)
423dbb7d2a8SRichard Weinberger 		return -ENOMEM;
424dbb7d2a8SRichard Weinberger 
425dbb7d2a8SRichard Weinberger 	vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
426dbb7d2a8SRichard Weinberger 	if (!vh) {
427dbb7d2a8SRichard Weinberger 		kfree(ech);
428dbb7d2a8SRichard Weinberger 		return -ENOMEM;
429dbb7d2a8SRichard Weinberger 	}
430dbb7d2a8SRichard Weinberger 
431dbb7d2a8SRichard Weinberger 	dbg_bld("scanning fastmap pool: size = %i", pool_size);
432dbb7d2a8SRichard Weinberger 
433dbb7d2a8SRichard Weinberger 	/*
434dbb7d2a8SRichard Weinberger 	 * Now scan all PEBs in the pool to find changes which have been made
435dbb7d2a8SRichard Weinberger 	 * after the creation of the fastmap
436dbb7d2a8SRichard Weinberger 	 */
437dbb7d2a8SRichard Weinberger 	for (i = 0; i < pool_size; i++) {
438dbb7d2a8SRichard Weinberger 		int scrub = 0;
439c22301adSRichard Genoud 		int image_seq;
440dbb7d2a8SRichard Weinberger 
441dbb7d2a8SRichard Weinberger 		pnum = be32_to_cpu(pebs[i]);
442dbb7d2a8SRichard Weinberger 
443dbb7d2a8SRichard Weinberger 		if (ubi_io_is_bad(ubi, pnum)) {
44432608703STanya Brokhman 			ubi_err(ubi, "bad PEB in fastmap pool!");
445dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
446dbb7d2a8SRichard Weinberger 			goto out;
447dbb7d2a8SRichard Weinberger 		}
448dbb7d2a8SRichard Weinberger 
449dbb7d2a8SRichard Weinberger 		err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
450dbb7d2a8SRichard Weinberger 		if (err && err != UBI_IO_BITFLIPS) {
45132608703STanya Brokhman 			ubi_err(ubi, "unable to read EC header! PEB:%i err:%i",
452dbb7d2a8SRichard Weinberger 				pnum, err);
453dbb7d2a8SRichard Weinberger 			ret = err > 0 ? UBI_BAD_FASTMAP : err;
454dbb7d2a8SRichard Weinberger 			goto out;
45544305ebdSBrian Norris 		} else if (err == UBI_IO_BITFLIPS)
456dbb7d2a8SRichard Weinberger 			scrub = 1;
457dbb7d2a8SRichard Weinberger 
458c22301adSRichard Genoud 		/*
459c22301adSRichard Genoud 		 * Older UBI implementations have image_seq set to zero, so
460c22301adSRichard Genoud 		 * we shouldn't fail if image_seq == 0.
461c22301adSRichard Genoud 		 */
462c22301adSRichard Genoud 		image_seq = be32_to_cpu(ech->image_seq);
463c22301adSRichard Genoud 
464c22301adSRichard Genoud 		if (image_seq && (image_seq != ubi->image_seq)) {
46532608703STanya Brokhman 			ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x",
466dbb7d2a8SRichard Weinberger 				be32_to_cpu(ech->image_seq), ubi->image_seq);
467f240dca8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
468dbb7d2a8SRichard Weinberger 			goto out;
469dbb7d2a8SRichard Weinberger 		}
470dbb7d2a8SRichard Weinberger 
471dbb7d2a8SRichard Weinberger 		err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
472dbb7d2a8SRichard Weinberger 		if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
473dbb7d2a8SRichard Weinberger 			unsigned long long ec = be64_to_cpu(ech->ec);
474dbb7d2a8SRichard Weinberger 			unmap_peb(ai, pnum);
475dbb7d2a8SRichard Weinberger 			dbg_bld("Adding PEB to free: %i", pnum);
476ecbfa8eaSBoris Brezillon 
477dbb7d2a8SRichard Weinberger 			if (err == UBI_IO_FF_BITFLIPS)
478ecbfa8eaSBoris Brezillon 				scrub = 1;
479ecbfa8eaSBoris Brezillon 
480ecbfa8eaSBoris Brezillon 			add_aeb(ai, free, pnum, ec, scrub);
481dbb7d2a8SRichard Weinberger 			continue;
482dbb7d2a8SRichard Weinberger 		} else if (err == 0 || err == UBI_IO_BITFLIPS) {
483dbb7d2a8SRichard Weinberger 			dbg_bld("Found non empty PEB:%i in pool", pnum);
484dbb7d2a8SRichard Weinberger 
485dbb7d2a8SRichard Weinberger 			if (err == UBI_IO_BITFLIPS)
486dbb7d2a8SRichard Weinberger 				scrub = 1;
487dbb7d2a8SRichard Weinberger 
488dbb7d2a8SRichard Weinberger 			new_aeb = kmem_cache_alloc(ai->aeb_slab_cache,
489dbb7d2a8SRichard Weinberger 						   GFP_KERNEL);
490dbb7d2a8SRichard Weinberger 			if (!new_aeb) {
491dbb7d2a8SRichard Weinberger 				ret = -ENOMEM;
492dbb7d2a8SRichard Weinberger 				goto out;
493dbb7d2a8SRichard Weinberger 			}
494dbb7d2a8SRichard Weinberger 
495dbb7d2a8SRichard Weinberger 			new_aeb->ec = be64_to_cpu(ech->ec);
496dbb7d2a8SRichard Weinberger 			new_aeb->pnum = pnum;
497dbb7d2a8SRichard Weinberger 			new_aeb->lnum = be32_to_cpu(vh->lnum);
498dbb7d2a8SRichard Weinberger 			new_aeb->sqnum = be64_to_cpu(vh->sqnum);
499dbb7d2a8SRichard Weinberger 			new_aeb->copy_flag = vh->copy_flag;
500dbb7d2a8SRichard Weinberger 			new_aeb->scrub = scrub;
501dbb7d2a8SRichard Weinberger 
502dbb7d2a8SRichard Weinberger 			if (*max_sqnum < new_aeb->sqnum)
503dbb7d2a8SRichard Weinberger 				*max_sqnum = new_aeb->sqnum;
504dbb7d2a8SRichard Weinberger 
505dbb7d2a8SRichard Weinberger 			err = process_pool_aeb(ubi, ai, vh, new_aeb);
506dbb7d2a8SRichard Weinberger 			if (err) {
507dbb7d2a8SRichard Weinberger 				ret = err > 0 ? UBI_BAD_FASTMAP : err;
508dbb7d2a8SRichard Weinberger 				goto out;
509dbb7d2a8SRichard Weinberger 			}
510dbb7d2a8SRichard Weinberger 		} else {
511dbb7d2a8SRichard Weinberger 			/* We are paranoid and fall back to scanning mode */
51232608703STanya Brokhman 			ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!");
513dbb7d2a8SRichard Weinberger 			ret = err > 0 ? UBI_BAD_FASTMAP : err;
514dbb7d2a8SRichard Weinberger 			goto out;
515dbb7d2a8SRichard Weinberger 		}
516dbb7d2a8SRichard Weinberger 
517dbb7d2a8SRichard Weinberger 	}
518dbb7d2a8SRichard Weinberger 
519dbb7d2a8SRichard Weinberger out:
520dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, vh);
521dbb7d2a8SRichard Weinberger 	kfree(ech);
522dbb7d2a8SRichard Weinberger 	return ret;
523dbb7d2a8SRichard Weinberger }
524dbb7d2a8SRichard Weinberger 
525dbb7d2a8SRichard Weinberger /**
526dbb7d2a8SRichard Weinberger  * count_fastmap_pebs - Counts the PEBs found by fastmap.
527dbb7d2a8SRichard Weinberger  * @ai: The UBI attach info object
528dbb7d2a8SRichard Weinberger  */
529dbb7d2a8SRichard Weinberger static int count_fastmap_pebs(struct ubi_attach_info *ai)
530dbb7d2a8SRichard Weinberger {
531dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb;
532dbb7d2a8SRichard Weinberger 	struct ubi_ainf_volume *av;
533dbb7d2a8SRichard Weinberger 	struct rb_node *rb1, *rb2;
534dbb7d2a8SRichard Weinberger 	int n = 0;
535dbb7d2a8SRichard Weinberger 
536dbb7d2a8SRichard Weinberger 	list_for_each_entry(aeb, &ai->erase, u.list)
537dbb7d2a8SRichard Weinberger 		n++;
538dbb7d2a8SRichard Weinberger 
539dbb7d2a8SRichard Weinberger 	list_for_each_entry(aeb, &ai->free, u.list)
540dbb7d2a8SRichard Weinberger 		n++;
541dbb7d2a8SRichard Weinberger 
542dbb7d2a8SRichard Weinberger 	ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
543dbb7d2a8SRichard Weinberger 		ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
544dbb7d2a8SRichard Weinberger 			n++;
545dbb7d2a8SRichard Weinberger 
546dbb7d2a8SRichard Weinberger 	return n;
547dbb7d2a8SRichard Weinberger }
548dbb7d2a8SRichard Weinberger 
549dbb7d2a8SRichard Weinberger /**
550dbb7d2a8SRichard Weinberger  * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
551dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
552dbb7d2a8SRichard Weinberger  * @ai: UBI attach info object
553dbb7d2a8SRichard Weinberger  * @fm: the fastmap to be attached
554dbb7d2a8SRichard Weinberger  *
555dbb7d2a8SRichard Weinberger  * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
556dbb7d2a8SRichard Weinberger  * < 0 indicates an internal error.
557dbb7d2a8SRichard Weinberger  */
558dbb7d2a8SRichard Weinberger static int ubi_attach_fastmap(struct ubi_device *ubi,
559dbb7d2a8SRichard Weinberger 			      struct ubi_attach_info *ai,
560dbb7d2a8SRichard Weinberger 			      struct ubi_fastmap_layout *fm)
561dbb7d2a8SRichard Weinberger {
562d141a8efSRichard Weinberger 	struct list_head used, free;
563dbb7d2a8SRichard Weinberger 	struct ubi_ainf_volume *av;
564dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
565dbb7d2a8SRichard Weinberger 	struct ubi_fm_sb *fmsb;
566dbb7d2a8SRichard Weinberger 	struct ubi_fm_hdr *fmhdr;
567f6e951afSshengyong 	struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
568dbb7d2a8SRichard Weinberger 	struct ubi_fm_ec *fmec;
569dbb7d2a8SRichard Weinberger 	struct ubi_fm_volhdr *fmvhdr;
570dbb7d2a8SRichard Weinberger 	struct ubi_fm_eba *fm_eba;
571dbb7d2a8SRichard Weinberger 	int ret, i, j, pool_size, wl_pool_size;
572dbb7d2a8SRichard Weinberger 	size_t fm_pos = 0, fm_size = ubi->fm_size;
573dbb7d2a8SRichard Weinberger 	unsigned long long max_sqnum = 0;
574dbb7d2a8SRichard Weinberger 	void *fm_raw = ubi->fm_buf;
575dbb7d2a8SRichard Weinberger 
576dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&used);
577dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&free);
578dbb7d2a8SRichard Weinberger 	ai->min_ec = UBI_MAX_ERASECOUNTER;
579dbb7d2a8SRichard Weinberger 
580dbb7d2a8SRichard Weinberger 	fmsb = (struct ubi_fm_sb *)(fm_raw);
581dbb7d2a8SRichard Weinberger 	ai->max_sqnum = fmsb->sqnum;
582dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(struct ubi_fm_sb);
583dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
584dbb7d2a8SRichard Weinberger 		goto fail_bad;
585dbb7d2a8SRichard Weinberger 
586dbb7d2a8SRichard Weinberger 	fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
587dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmhdr);
588dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
589dbb7d2a8SRichard Weinberger 		goto fail_bad;
590dbb7d2a8SRichard Weinberger 
591dbb7d2a8SRichard Weinberger 	if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
59232608703STanya Brokhman 		ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x",
593dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
594dbb7d2a8SRichard Weinberger 		goto fail_bad;
595dbb7d2a8SRichard Weinberger 	}
596dbb7d2a8SRichard Weinberger 
597f6e951afSshengyong 	fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
598f6e951afSshengyong 	fm_pos += sizeof(*fmpl);
599dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
600dbb7d2a8SRichard Weinberger 		goto fail_bad;
601f6e951afSshengyong 	if (be32_to_cpu(fmpl->magic) != UBI_FM_POOL_MAGIC) {
60232608703STanya Brokhman 		ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x",
603f6e951afSshengyong 			be32_to_cpu(fmpl->magic), UBI_FM_POOL_MAGIC);
604dbb7d2a8SRichard Weinberger 		goto fail_bad;
605dbb7d2a8SRichard Weinberger 	}
606dbb7d2a8SRichard Weinberger 
607f6e951afSshengyong 	fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
608f6e951afSshengyong 	fm_pos += sizeof(*fmpl_wl);
609dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
610dbb7d2a8SRichard Weinberger 		goto fail_bad;
611f6e951afSshengyong 	if (be32_to_cpu(fmpl_wl->magic) != UBI_FM_POOL_MAGIC) {
612f6e951afSshengyong 		ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x",
613f6e951afSshengyong 			be32_to_cpu(fmpl_wl->magic), UBI_FM_POOL_MAGIC);
614dbb7d2a8SRichard Weinberger 		goto fail_bad;
615dbb7d2a8SRichard Weinberger 	}
616dbb7d2a8SRichard Weinberger 
617f6e951afSshengyong 	pool_size = be16_to_cpu(fmpl->size);
618f6e951afSshengyong 	wl_pool_size = be16_to_cpu(fmpl_wl->size);
619f6e951afSshengyong 	fm->max_pool_size = be16_to_cpu(fmpl->max_size);
620f6e951afSshengyong 	fm->max_wl_pool_size = be16_to_cpu(fmpl_wl->max_size);
621dbb7d2a8SRichard Weinberger 
622dbb7d2a8SRichard Weinberger 	if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
62332608703STanya Brokhman 		ubi_err(ubi, "bad pool size: %i", pool_size);
624dbb7d2a8SRichard Weinberger 		goto fail_bad;
625dbb7d2a8SRichard Weinberger 	}
626dbb7d2a8SRichard Weinberger 
627dbb7d2a8SRichard Weinberger 	if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
62832608703STanya Brokhman 		ubi_err(ubi, "bad WL pool size: %i", wl_pool_size);
629dbb7d2a8SRichard Weinberger 		goto fail_bad;
630dbb7d2a8SRichard Weinberger 	}
631dbb7d2a8SRichard Weinberger 
632dbb7d2a8SRichard Weinberger 
633dbb7d2a8SRichard Weinberger 	if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
634dbb7d2a8SRichard Weinberger 	    fm->max_pool_size < 0) {
63532608703STanya Brokhman 		ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size);
636dbb7d2a8SRichard Weinberger 		goto fail_bad;
637dbb7d2a8SRichard Weinberger 	}
638dbb7d2a8SRichard Weinberger 
639dbb7d2a8SRichard Weinberger 	if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
640dbb7d2a8SRichard Weinberger 	    fm->max_wl_pool_size < 0) {
64132608703STanya Brokhman 		ubi_err(ubi, "bad maximal WL pool size: %i",
64232608703STanya Brokhman 			fm->max_wl_pool_size);
643dbb7d2a8SRichard Weinberger 		goto fail_bad;
644dbb7d2a8SRichard Weinberger 	}
645dbb7d2a8SRichard Weinberger 
646dbb7d2a8SRichard Weinberger 	/* read EC values from free list */
647dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
648dbb7d2a8SRichard Weinberger 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
649dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmec);
650dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
651dbb7d2a8SRichard Weinberger 			goto fail_bad;
652dbb7d2a8SRichard Weinberger 
653dbb7d2a8SRichard Weinberger 		add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
654dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmec->ec), 0);
655dbb7d2a8SRichard Weinberger 	}
656dbb7d2a8SRichard Weinberger 
657dbb7d2a8SRichard Weinberger 	/* read EC values from used list */
658dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
659dbb7d2a8SRichard Weinberger 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
660dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmec);
661dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
662dbb7d2a8SRichard Weinberger 			goto fail_bad;
663dbb7d2a8SRichard Weinberger 
664dbb7d2a8SRichard Weinberger 		add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
665dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmec->ec), 0);
666dbb7d2a8SRichard Weinberger 	}
667dbb7d2a8SRichard Weinberger 
668dbb7d2a8SRichard Weinberger 	/* read EC values from scrub list */
669dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
670dbb7d2a8SRichard Weinberger 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
671dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmec);
672dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
673dbb7d2a8SRichard Weinberger 			goto fail_bad;
674dbb7d2a8SRichard Weinberger 
675dbb7d2a8SRichard Weinberger 		add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
676dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmec->ec), 1);
677dbb7d2a8SRichard Weinberger 	}
678dbb7d2a8SRichard Weinberger 
679dbb7d2a8SRichard Weinberger 	/* read EC values from erase list */
680dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
681dbb7d2a8SRichard Weinberger 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
682dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmec);
683dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
684dbb7d2a8SRichard Weinberger 			goto fail_bad;
685dbb7d2a8SRichard Weinberger 
686dbb7d2a8SRichard Weinberger 		add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
687dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmec->ec), 1);
688dbb7d2a8SRichard Weinberger 	}
689dbb7d2a8SRichard Weinberger 
690dbb7d2a8SRichard Weinberger 	ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
691dbb7d2a8SRichard Weinberger 	ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
692dbb7d2a8SRichard Weinberger 
693dbb7d2a8SRichard Weinberger 	/* Iterate over all volumes and read their EBA table */
694dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
695dbb7d2a8SRichard Weinberger 		fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
696dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmvhdr);
697dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
698dbb7d2a8SRichard Weinberger 			goto fail_bad;
699dbb7d2a8SRichard Weinberger 
700dbb7d2a8SRichard Weinberger 		if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
70132608703STanya Brokhman 			ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x",
702dbb7d2a8SRichard Weinberger 				be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
703dbb7d2a8SRichard Weinberger 			goto fail_bad;
704dbb7d2a8SRichard Weinberger 		}
705dbb7d2a8SRichard Weinberger 
706dbb7d2a8SRichard Weinberger 		av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
707dbb7d2a8SRichard Weinberger 			     be32_to_cpu(fmvhdr->used_ebs),
708dbb7d2a8SRichard Weinberger 			     be32_to_cpu(fmvhdr->data_pad),
709dbb7d2a8SRichard Weinberger 			     fmvhdr->vol_type,
710dbb7d2a8SRichard Weinberger 			     be32_to_cpu(fmvhdr->last_eb_bytes));
711dbb7d2a8SRichard Weinberger 
712dbb7d2a8SRichard Weinberger 		if (!av)
713dbb7d2a8SRichard Weinberger 			goto fail_bad;
714e96a8a3bSshengyong 		if (PTR_ERR(av) == -EINVAL) {
715e96a8a3bSshengyong 			ubi_err(ubi, "volume (ID %i) already exists",
716e96a8a3bSshengyong 				fmvhdr->vol_id);
717e96a8a3bSshengyong 			goto fail_bad;
718e96a8a3bSshengyong 		}
719dbb7d2a8SRichard Weinberger 
720dbb7d2a8SRichard Weinberger 		ai->vols_found++;
721dbb7d2a8SRichard Weinberger 		if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
722dbb7d2a8SRichard Weinberger 			ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
723dbb7d2a8SRichard Weinberger 
724dbb7d2a8SRichard Weinberger 		fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
725dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fm_eba);
726dbb7d2a8SRichard Weinberger 		fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
727dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
728dbb7d2a8SRichard Weinberger 			goto fail_bad;
729dbb7d2a8SRichard Weinberger 
730dbb7d2a8SRichard Weinberger 		if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
73132608703STanya Brokhman 			ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x",
732dbb7d2a8SRichard Weinberger 				be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
733dbb7d2a8SRichard Weinberger 			goto fail_bad;
734dbb7d2a8SRichard Weinberger 		}
735dbb7d2a8SRichard Weinberger 
736dbb7d2a8SRichard Weinberger 		for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
737dbb7d2a8SRichard Weinberger 			int pnum = be32_to_cpu(fm_eba->pnum[j]);
738dbb7d2a8SRichard Weinberger 
739876f9a34SRichard Weinberger 			if (pnum < 0)
740dbb7d2a8SRichard Weinberger 				continue;
741dbb7d2a8SRichard Weinberger 
742dbb7d2a8SRichard Weinberger 			aeb = NULL;
743dbb7d2a8SRichard Weinberger 			list_for_each_entry(tmp_aeb, &used, u.list) {
744584d4623SBrian Pomerantz 				if (tmp_aeb->pnum == pnum) {
745dbb7d2a8SRichard Weinberger 					aeb = tmp_aeb;
746584d4623SBrian Pomerantz 					break;
747584d4623SBrian Pomerantz 				}
748dbb7d2a8SRichard Weinberger 			}
749dbb7d2a8SRichard Weinberger 
750dbb7d2a8SRichard Weinberger 			if (!aeb) {
751d141a8efSRichard Weinberger 				ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum);
752d141a8efSRichard Weinberger 				goto fail_bad;
753dbb7d2a8SRichard Weinberger 			}
754dbb7d2a8SRichard Weinberger 
755dbb7d2a8SRichard Weinberger 			aeb->lnum = j;
756dbb7d2a8SRichard Weinberger 
757dbb7d2a8SRichard Weinberger 			if (av->highest_lnum <= aeb->lnum)
758dbb7d2a8SRichard Weinberger 				av->highest_lnum = aeb->lnum;
759dbb7d2a8SRichard Weinberger 
760dbb7d2a8SRichard Weinberger 			assign_aeb_to_av(ai, aeb, av);
761dbb7d2a8SRichard Weinberger 
762dbb7d2a8SRichard Weinberger 			dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
763dbb7d2a8SRichard Weinberger 				aeb->pnum, aeb->lnum, av->vol_id);
764dbb7d2a8SRichard Weinberger 		}
765dbb7d2a8SRichard Weinberger 	}
766dbb7d2a8SRichard Weinberger 
767f6e951afSshengyong 	ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free);
768dbb7d2a8SRichard Weinberger 	if (ret)
769dbb7d2a8SRichard Weinberger 		goto fail;
770dbb7d2a8SRichard Weinberger 
771f6e951afSshengyong 	ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free);
772dbb7d2a8SRichard Weinberger 	if (ret)
773dbb7d2a8SRichard Weinberger 		goto fail;
774dbb7d2a8SRichard Weinberger 
775dbb7d2a8SRichard Weinberger 	if (max_sqnum > ai->max_sqnum)
776dbb7d2a8SRichard Weinberger 		ai->max_sqnum = max_sqnum;
777dbb7d2a8SRichard Weinberger 
7786a059abdSWei Yongjun 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
7796a059abdSWei Yongjun 		list_move_tail(&tmp_aeb->u.list, &ai->free);
780dbb7d2a8SRichard Weinberger 
781a83832a7SRichard Weinberger 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list)
782a83832a7SRichard Weinberger 		list_move_tail(&tmp_aeb->u.list, &ai->erase);
783a83832a7SRichard Weinberger 
784ae0d1469SRichard Weinberger 	ubi_assert(list_empty(&free));
785ae0d1469SRichard Weinberger 
786dbb7d2a8SRichard Weinberger 	/*
787dbb7d2a8SRichard Weinberger 	 * If fastmap is leaking PEBs (must not happen), raise a
788dbb7d2a8SRichard Weinberger 	 * fat warning and fall back to scanning mode.
789dbb7d2a8SRichard Weinberger 	 * We do this here because in ubi_wl_init() it's too late
790dbb7d2a8SRichard Weinberger 	 * and we cannot fall back to scanning.
791dbb7d2a8SRichard Weinberger 	 */
792dbb7d2a8SRichard Weinberger 	if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
793dbb7d2a8SRichard Weinberger 		    ai->bad_peb_count - fm->used_blocks))
794dbb7d2a8SRichard Weinberger 		goto fail_bad;
795dbb7d2a8SRichard Weinberger 
796dbb7d2a8SRichard Weinberger 	return 0;
797dbb7d2a8SRichard Weinberger 
798dbb7d2a8SRichard Weinberger fail_bad:
799dbb7d2a8SRichard Weinberger 	ret = UBI_BAD_FASTMAP;
800dbb7d2a8SRichard Weinberger fail:
801fe24c6e5SRichard Weinberger 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
802fe24c6e5SRichard Weinberger 		list_del(&tmp_aeb->u.list);
8035547fec7SDan Carpenter 		kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
804fe24c6e5SRichard Weinberger 	}
805fe24c6e5SRichard Weinberger 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
806fe24c6e5SRichard Weinberger 		list_del(&tmp_aeb->u.list);
8075547fec7SDan Carpenter 		kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
808fe24c6e5SRichard Weinberger 	}
809fe24c6e5SRichard Weinberger 
810dbb7d2a8SRichard Weinberger 	return ret;
811dbb7d2a8SRichard Weinberger }
812dbb7d2a8SRichard Weinberger 
813dbb7d2a8SRichard Weinberger /**
814fdf10ed7SRichard Weinberger  * find_fm_anchor - find the most recent Fastmap superblock (anchor)
815fdf10ed7SRichard Weinberger  * @ai: UBI attach info to be filled
816fdf10ed7SRichard Weinberger  */
817fdf10ed7SRichard Weinberger static int find_fm_anchor(struct ubi_attach_info *ai)
818fdf10ed7SRichard Weinberger {
819fdf10ed7SRichard Weinberger 	int ret = -1;
820fdf10ed7SRichard Weinberger 	struct ubi_ainf_peb *aeb;
821fdf10ed7SRichard Weinberger 	unsigned long long max_sqnum = 0;
822fdf10ed7SRichard Weinberger 
823fdf10ed7SRichard Weinberger 	list_for_each_entry(aeb, &ai->fastmap, u.list) {
824fdf10ed7SRichard Weinberger 		if (aeb->vol_id == UBI_FM_SB_VOLUME_ID && aeb->sqnum > max_sqnum) {
825fdf10ed7SRichard Weinberger 			max_sqnum = aeb->sqnum;
826fdf10ed7SRichard Weinberger 			ret = aeb->pnum;
827fdf10ed7SRichard Weinberger 		}
828fdf10ed7SRichard Weinberger 	}
829fdf10ed7SRichard Weinberger 
830fdf10ed7SRichard Weinberger 	return ret;
831fdf10ed7SRichard Weinberger }
832fdf10ed7SRichard Weinberger 
833fdf10ed7SRichard Weinberger /**
834dbb7d2a8SRichard Weinberger  * ubi_scan_fastmap - scan the fastmap.
835dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
836dbb7d2a8SRichard Weinberger  * @ai: UBI attach info to be filled
837fdf10ed7SRichard Weinberger  * @scan_ai: UBI attach info from the first 64 PEBs,
838fdf10ed7SRichard Weinberger  *           used to find the most recent Fastmap data structure
839dbb7d2a8SRichard Weinberger  *
840dbb7d2a8SRichard Weinberger  * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
841dbb7d2a8SRichard Weinberger  * UBI_BAD_FASTMAP if one was found but is not usable.
842dbb7d2a8SRichard Weinberger  * < 0 indicates an internal error.
843dbb7d2a8SRichard Weinberger  */
844dbb7d2a8SRichard Weinberger int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
845fdf10ed7SRichard Weinberger 		     struct ubi_attach_info *scan_ai)
846dbb7d2a8SRichard Weinberger {
847dbb7d2a8SRichard Weinberger 	struct ubi_fm_sb *fmsb, *fmsb2;
848dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *vh;
849dbb7d2a8SRichard Weinberger 	struct ubi_ec_hdr *ech;
850dbb7d2a8SRichard Weinberger 	struct ubi_fastmap_layout *fm;
851fdf10ed7SRichard Weinberger 	struct ubi_ainf_peb *tmp_aeb, *aeb;
852fdf10ed7SRichard Weinberger 	int i, used_blocks, pnum, fm_anchor, ret = 0;
853dbb7d2a8SRichard Weinberger 	size_t fm_size;
854dbb7d2a8SRichard Weinberger 	__be32 crc, tmp_crc;
855dbb7d2a8SRichard Weinberger 	unsigned long long sqnum = 0;
856dbb7d2a8SRichard Weinberger 
857fdf10ed7SRichard Weinberger 	fm_anchor = find_fm_anchor(scan_ai);
858fdf10ed7SRichard Weinberger 	if (fm_anchor < 0)
859fdf10ed7SRichard Weinberger 		return UBI_NO_FASTMAP;
860fdf10ed7SRichard Weinberger 
861fdf10ed7SRichard Weinberger 	/* Move all (possible) fastmap blocks into our new attach structure. */
862fdf10ed7SRichard Weinberger 	list_for_each_entry_safe(aeb, tmp_aeb, &scan_ai->fastmap, u.list)
863fdf10ed7SRichard Weinberger 		list_move_tail(&aeb->u.list, &ai->fastmap);
864fdf10ed7SRichard Weinberger 
865111ab0b2SRichard Weinberger 	down_write(&ubi->fm_protect);
866dbb7d2a8SRichard Weinberger 	memset(ubi->fm_buf, 0, ubi->fm_size);
867dbb7d2a8SRichard Weinberger 
868dbb7d2a8SRichard Weinberger 	fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
869dbb7d2a8SRichard Weinberger 	if (!fmsb) {
870dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
871dbb7d2a8SRichard Weinberger 		goto out;
872dbb7d2a8SRichard Weinberger 	}
873dbb7d2a8SRichard Weinberger 
874dbb7d2a8SRichard Weinberger 	fm = kzalloc(sizeof(*fm), GFP_KERNEL);
875dbb7d2a8SRichard Weinberger 	if (!fm) {
876dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
877dbb7d2a8SRichard Weinberger 		kfree(fmsb);
878dbb7d2a8SRichard Weinberger 		goto out;
879dbb7d2a8SRichard Weinberger 	}
880dbb7d2a8SRichard Weinberger 
881*fcbb6af1SBoris Brezillon 	ret = ubi_io_read_data(ubi, fmsb, fm_anchor, 0, sizeof(*fmsb));
882dbb7d2a8SRichard Weinberger 	if (ret && ret != UBI_IO_BITFLIPS)
883dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
884dbb7d2a8SRichard Weinberger 	else if (ret == UBI_IO_BITFLIPS)
885dbb7d2a8SRichard Weinberger 		fm->to_be_tortured[0] = 1;
886dbb7d2a8SRichard Weinberger 
887dbb7d2a8SRichard Weinberger 	if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
88832608703STanya Brokhman 		ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x",
889dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
890dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
891dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
892dbb7d2a8SRichard Weinberger 	}
893dbb7d2a8SRichard Weinberger 
894dbb7d2a8SRichard Weinberger 	if (fmsb->version != UBI_FM_FMT_VERSION) {
89532608703STanya Brokhman 		ubi_err(ubi, "bad fastmap version: %i, expected: %i",
896dbb7d2a8SRichard Weinberger 			fmsb->version, UBI_FM_FMT_VERSION);
897dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
898dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
899dbb7d2a8SRichard Weinberger 	}
900dbb7d2a8SRichard Weinberger 
901dbb7d2a8SRichard Weinberger 	used_blocks = be32_to_cpu(fmsb->used_blocks);
902dbb7d2a8SRichard Weinberger 	if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
90332608703STanya Brokhman 		ubi_err(ubi, "number of fastmap blocks is invalid: %i",
90432608703STanya Brokhman 			used_blocks);
905dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
906dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
907dbb7d2a8SRichard Weinberger 	}
908dbb7d2a8SRichard Weinberger 
909dbb7d2a8SRichard Weinberger 	fm_size = ubi->leb_size * used_blocks;
910dbb7d2a8SRichard Weinberger 	if (fm_size != ubi->fm_size) {
91132608703STanya Brokhman 		ubi_err(ubi, "bad fastmap size: %zi, expected: %zi",
91232608703STanya Brokhman 			fm_size, ubi->fm_size);
913dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
914dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
915dbb7d2a8SRichard Weinberger 	}
916dbb7d2a8SRichard Weinberger 
917dbb7d2a8SRichard Weinberger 	ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
918dbb7d2a8SRichard Weinberger 	if (!ech) {
919dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
920dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
921dbb7d2a8SRichard Weinberger 	}
922dbb7d2a8SRichard Weinberger 
923dbb7d2a8SRichard Weinberger 	vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
924dbb7d2a8SRichard Weinberger 	if (!vh) {
925dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
926dbb7d2a8SRichard Weinberger 		goto free_hdr;
927dbb7d2a8SRichard Weinberger 	}
928dbb7d2a8SRichard Weinberger 
929dbb7d2a8SRichard Weinberger 	for (i = 0; i < used_blocks; i++) {
930c22301adSRichard Genoud 		int image_seq;
931c22301adSRichard Genoud 
932dbb7d2a8SRichard Weinberger 		pnum = be32_to_cpu(fmsb->block_loc[i]);
933dbb7d2a8SRichard Weinberger 
934dbb7d2a8SRichard Weinberger 		if (ubi_io_is_bad(ubi, pnum)) {
935dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
936dbb7d2a8SRichard Weinberger 			goto free_hdr;
937dbb7d2a8SRichard Weinberger 		}
938dbb7d2a8SRichard Weinberger 
9395283ec72SRichard Weinberger 		if (i == 0 && pnum != fm_anchor) {
9405283ec72SRichard Weinberger 			ubi_err(ubi, "Fastmap anchor PEB mismatch: PEB: %i vs. %i",
9415283ec72SRichard Weinberger 				pnum, fm_anchor);
9425283ec72SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
9435283ec72SRichard Weinberger 			goto free_hdr;
9445283ec72SRichard Weinberger 		}
9455283ec72SRichard Weinberger 
946dbb7d2a8SRichard Weinberger 		ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
947dbb7d2a8SRichard Weinberger 		if (ret && ret != UBI_IO_BITFLIPS) {
94832608703STanya Brokhman 			ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)",
949dbb7d2a8SRichard Weinberger 				i, pnum);
950dbb7d2a8SRichard Weinberger 			if (ret > 0)
951dbb7d2a8SRichard Weinberger 				ret = UBI_BAD_FASTMAP;
952dbb7d2a8SRichard Weinberger 			goto free_hdr;
953dbb7d2a8SRichard Weinberger 		} else if (ret == UBI_IO_BITFLIPS)
954dbb7d2a8SRichard Weinberger 			fm->to_be_tortured[i] = 1;
955dbb7d2a8SRichard Weinberger 
956c22301adSRichard Genoud 		image_seq = be32_to_cpu(ech->image_seq);
957dbb7d2a8SRichard Weinberger 		if (!ubi->image_seq)
958c22301adSRichard Genoud 			ubi->image_seq = image_seq;
959dbb7d2a8SRichard Weinberger 
960c22301adSRichard Genoud 		/*
961c22301adSRichard Genoud 		 * Older UBI implementations have image_seq set to zero, so
962c22301adSRichard Genoud 		 * we shouldn't fail if image_seq == 0.
963c22301adSRichard Genoud 		 */
964c22301adSRichard Genoud 		if (image_seq && (image_seq != ubi->image_seq)) {
96532608703STanya Brokhman 			ubi_err(ubi, "wrong image seq:%d instead of %d",
966c22301adSRichard Genoud 				be32_to_cpu(ech->image_seq), ubi->image_seq);
967dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
968dbb7d2a8SRichard Weinberger 			goto free_hdr;
969dbb7d2a8SRichard Weinberger 		}
970dbb7d2a8SRichard Weinberger 
971dbb7d2a8SRichard Weinberger 		ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
972dbb7d2a8SRichard Weinberger 		if (ret && ret != UBI_IO_BITFLIPS) {
97332608703STanya Brokhman 			ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)",
974dbb7d2a8SRichard Weinberger 				i, pnum);
975dbb7d2a8SRichard Weinberger 			goto free_hdr;
976dbb7d2a8SRichard Weinberger 		}
977dbb7d2a8SRichard Weinberger 
978dbb7d2a8SRichard Weinberger 		if (i == 0) {
979dbb7d2a8SRichard Weinberger 			if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
98032608703STanya Brokhman 				ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x",
981dbb7d2a8SRichard Weinberger 					be32_to_cpu(vh->vol_id),
982dbb7d2a8SRichard Weinberger 					UBI_FM_SB_VOLUME_ID);
983dbb7d2a8SRichard Weinberger 				ret = UBI_BAD_FASTMAP;
984dbb7d2a8SRichard Weinberger 				goto free_hdr;
985dbb7d2a8SRichard Weinberger 			}
986dbb7d2a8SRichard Weinberger 		} else {
987dbb7d2a8SRichard Weinberger 			if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
98832608703STanya Brokhman 				ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x",
989dbb7d2a8SRichard Weinberger 					be32_to_cpu(vh->vol_id),
990dbb7d2a8SRichard Weinberger 					UBI_FM_DATA_VOLUME_ID);
991dbb7d2a8SRichard Weinberger 				ret = UBI_BAD_FASTMAP;
992dbb7d2a8SRichard Weinberger 				goto free_hdr;
993dbb7d2a8SRichard Weinberger 			}
994dbb7d2a8SRichard Weinberger 		}
995dbb7d2a8SRichard Weinberger 
996dbb7d2a8SRichard Weinberger 		if (sqnum < be64_to_cpu(vh->sqnum))
997dbb7d2a8SRichard Weinberger 			sqnum = be64_to_cpu(vh->sqnum);
998dbb7d2a8SRichard Weinberger 
999*fcbb6af1SBoris Brezillon 		ret = ubi_io_read_data(ubi, ubi->fm_buf + (ubi->leb_size * i),
1000*fcbb6af1SBoris Brezillon 				       pnum, 0, ubi->leb_size);
1001dbb7d2a8SRichard Weinberger 		if (ret && ret != UBI_IO_BITFLIPS) {
100232608703STanya Brokhman 			ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, "
1003dbb7d2a8SRichard Weinberger 				"err: %i)", i, pnum, ret);
1004dbb7d2a8SRichard Weinberger 			goto free_hdr;
1005dbb7d2a8SRichard Weinberger 		}
1006dbb7d2a8SRichard Weinberger 	}
1007dbb7d2a8SRichard Weinberger 
1008dbb7d2a8SRichard Weinberger 	kfree(fmsb);
1009dbb7d2a8SRichard Weinberger 	fmsb = NULL;
1010dbb7d2a8SRichard Weinberger 
1011dbb7d2a8SRichard Weinberger 	fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
1012dbb7d2a8SRichard Weinberger 	tmp_crc = be32_to_cpu(fmsb2->data_crc);
1013dbb7d2a8SRichard Weinberger 	fmsb2->data_crc = 0;
1014dbb7d2a8SRichard Weinberger 	crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
1015dbb7d2a8SRichard Weinberger 	if (crc != tmp_crc) {
101632608703STanya Brokhman 		ubi_err(ubi, "fastmap data CRC is invalid");
101732608703STanya Brokhman 		ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x",
101832608703STanya Brokhman 			tmp_crc, crc);
1019dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
1020dbb7d2a8SRichard Weinberger 		goto free_hdr;
1021dbb7d2a8SRichard Weinberger 	}
1022dbb7d2a8SRichard Weinberger 
1023dbb7d2a8SRichard Weinberger 	fmsb2->sqnum = sqnum;
1024dbb7d2a8SRichard Weinberger 
1025dbb7d2a8SRichard Weinberger 	fm->used_blocks = used_blocks;
1026dbb7d2a8SRichard Weinberger 
1027dbb7d2a8SRichard Weinberger 	ret = ubi_attach_fastmap(ubi, ai, fm);
1028dbb7d2a8SRichard Weinberger 	if (ret) {
1029dbb7d2a8SRichard Weinberger 		if (ret > 0)
1030dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
1031dbb7d2a8SRichard Weinberger 		goto free_hdr;
1032dbb7d2a8SRichard Weinberger 	}
1033dbb7d2a8SRichard Weinberger 
1034dbb7d2a8SRichard Weinberger 	for (i = 0; i < used_blocks; i++) {
1035dbb7d2a8SRichard Weinberger 		struct ubi_wl_entry *e;
1036dbb7d2a8SRichard Weinberger 
1037dbb7d2a8SRichard Weinberger 		e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1038dbb7d2a8SRichard Weinberger 		if (!e) {
1039dbb7d2a8SRichard Weinberger 			while (i--)
1040dbb7d2a8SRichard Weinberger 				kfree(fm->e[i]);
1041dbb7d2a8SRichard Weinberger 
1042dbb7d2a8SRichard Weinberger 			ret = -ENOMEM;
1043dbb7d2a8SRichard Weinberger 			goto free_hdr;
1044dbb7d2a8SRichard Weinberger 		}
1045dbb7d2a8SRichard Weinberger 
1046dbb7d2a8SRichard Weinberger 		e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
1047dbb7d2a8SRichard Weinberger 		e->ec = be32_to_cpu(fmsb2->block_ec[i]);
1048dbb7d2a8SRichard Weinberger 		fm->e[i] = e;
1049dbb7d2a8SRichard Weinberger 	}
1050dbb7d2a8SRichard Weinberger 
1051dbb7d2a8SRichard Weinberger 	ubi->fm = fm;
1052dbb7d2a8SRichard Weinberger 	ubi->fm_pool.max_size = ubi->fm->max_pool_size;
1053dbb7d2a8SRichard Weinberger 	ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
105432608703STanya Brokhman 	ubi_msg(ubi, "attached by fastmap");
105532608703STanya Brokhman 	ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size);
105632608703STanya Brokhman 	ubi_msg(ubi, "fastmap WL pool size: %d",
105732608703STanya Brokhman 		ubi->fm_wl_pool.max_size);
1058dbb7d2a8SRichard Weinberger 	ubi->fm_disabled = 0;
10591900149cSRichard Weinberger 	ubi->fast_attach = 1;
1060dbb7d2a8SRichard Weinberger 
1061dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, vh);
1062dbb7d2a8SRichard Weinberger 	kfree(ech);
1063dbb7d2a8SRichard Weinberger out:
1064111ab0b2SRichard Weinberger 	up_write(&ubi->fm_protect);
1065dbb7d2a8SRichard Weinberger 	if (ret == UBI_BAD_FASTMAP)
106632608703STanya Brokhman 		ubi_err(ubi, "Attach by fastmap failed, doing a full scan!");
1067dbb7d2a8SRichard Weinberger 	return ret;
1068dbb7d2a8SRichard Weinberger 
1069dbb7d2a8SRichard Weinberger free_hdr:
1070dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, vh);
1071dbb7d2a8SRichard Weinberger 	kfree(ech);
1072dbb7d2a8SRichard Weinberger free_fm_sb:
1073dbb7d2a8SRichard Weinberger 	kfree(fmsb);
1074dbb7d2a8SRichard Weinberger 	kfree(fm);
1075dbb7d2a8SRichard Weinberger 	goto out;
1076dbb7d2a8SRichard Weinberger }
1077dbb7d2a8SRichard Weinberger 
1078dbb7d2a8SRichard Weinberger /**
1079dbb7d2a8SRichard Weinberger  * ubi_write_fastmap - writes a fastmap.
1080dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1081dbb7d2a8SRichard Weinberger  * @new_fm: the to be written fastmap
1082dbb7d2a8SRichard Weinberger  *
1083dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
1084dbb7d2a8SRichard Weinberger  */
1085dbb7d2a8SRichard Weinberger static int ubi_write_fastmap(struct ubi_device *ubi,
1086dbb7d2a8SRichard Weinberger 			     struct ubi_fastmap_layout *new_fm)
1087dbb7d2a8SRichard Weinberger {
1088dbb7d2a8SRichard Weinberger 	size_t fm_pos = 0;
1089dbb7d2a8SRichard Weinberger 	void *fm_raw;
1090dbb7d2a8SRichard Weinberger 	struct ubi_fm_sb *fmsb;
1091dbb7d2a8SRichard Weinberger 	struct ubi_fm_hdr *fmh;
1092f6e951afSshengyong 	struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
1093dbb7d2a8SRichard Weinberger 	struct ubi_fm_ec *fec;
1094dbb7d2a8SRichard Weinberger 	struct ubi_fm_volhdr *fvh;
1095dbb7d2a8SRichard Weinberger 	struct ubi_fm_eba *feba;
1096dbb7d2a8SRichard Weinberger 	struct ubi_wl_entry *wl_e;
1097dbb7d2a8SRichard Weinberger 	struct ubi_volume *vol;
1098dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *avhdr, *dvhdr;
1099dbb7d2a8SRichard Weinberger 	struct ubi_work *ubi_wrk;
1100c5c3f3cfSRichard Weinberger 	struct rb_node *tmp_rb;
1101dbb7d2a8SRichard Weinberger 	int ret, i, j, free_peb_count, used_peb_count, vol_count;
1102dbb7d2a8SRichard Weinberger 	int scrub_peb_count, erase_peb_count;
11035d71afb0SRichard Weinberger 	unsigned long *seen_pebs = NULL;
1104dbb7d2a8SRichard Weinberger 
1105dbb7d2a8SRichard Weinberger 	fm_raw = ubi->fm_buf;
1106dbb7d2a8SRichard Weinberger 	memset(ubi->fm_buf, 0, ubi->fm_size);
1107dbb7d2a8SRichard Weinberger 
1108dbb7d2a8SRichard Weinberger 	avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1109dbb7d2a8SRichard Weinberger 	if (!avhdr) {
1110dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
1111dbb7d2a8SRichard Weinberger 		goto out;
1112dbb7d2a8SRichard Weinberger 	}
1113dbb7d2a8SRichard Weinberger 
1114dbb7d2a8SRichard Weinberger 	dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
1115dbb7d2a8SRichard Weinberger 	if (!dvhdr) {
1116dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
1117dbb7d2a8SRichard Weinberger 		goto out_kfree;
1118dbb7d2a8SRichard Weinberger 	}
1119dbb7d2a8SRichard Weinberger 
1120daef3dd1SRichard Weinberger 	seen_pebs = init_seen(ubi);
1121daef3dd1SRichard Weinberger 	if (IS_ERR(seen_pebs)) {
1122daef3dd1SRichard Weinberger 		ret = PTR_ERR(seen_pebs);
1123daef3dd1SRichard Weinberger 		goto out_kfree;
1124daef3dd1SRichard Weinberger 	}
1125daef3dd1SRichard Weinberger 
1126dbb7d2a8SRichard Weinberger 	spin_lock(&ubi->volumes_lock);
1127dbb7d2a8SRichard Weinberger 	spin_lock(&ubi->wl_lock);
1128dbb7d2a8SRichard Weinberger 
1129dbb7d2a8SRichard Weinberger 	fmsb = (struct ubi_fm_sb *)fm_raw;
1130dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmsb);
1131dbb7d2a8SRichard Weinberger 	ubi_assert(fm_pos <= ubi->fm_size);
1132dbb7d2a8SRichard Weinberger 
1133dbb7d2a8SRichard Weinberger 	fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
1134dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmh);
1135dbb7d2a8SRichard Weinberger 	ubi_assert(fm_pos <= ubi->fm_size);
1136dbb7d2a8SRichard Weinberger 
1137dbb7d2a8SRichard Weinberger 	fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
1138dbb7d2a8SRichard Weinberger 	fmsb->version = UBI_FM_FMT_VERSION;
1139dbb7d2a8SRichard Weinberger 	fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
1140dbb7d2a8SRichard Weinberger 	/* the max sqnum will be filled in while *reading* the fastmap */
1141dbb7d2a8SRichard Weinberger 	fmsb->sqnum = 0;
1142dbb7d2a8SRichard Weinberger 
1143dbb7d2a8SRichard Weinberger 	fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
1144dbb7d2a8SRichard Weinberger 	free_peb_count = 0;
1145dbb7d2a8SRichard Weinberger 	used_peb_count = 0;
1146dbb7d2a8SRichard Weinberger 	scrub_peb_count = 0;
1147dbb7d2a8SRichard Weinberger 	erase_peb_count = 0;
1148dbb7d2a8SRichard Weinberger 	vol_count = 0;
1149dbb7d2a8SRichard Weinberger 
1150f6e951afSshengyong 	fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1151f6e951afSshengyong 	fm_pos += sizeof(*fmpl);
1152f6e951afSshengyong 	fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1153f6e951afSshengyong 	fmpl->size = cpu_to_be16(ubi->fm_pool.size);
1154f6e951afSshengyong 	fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size);
1155dbb7d2a8SRichard Weinberger 
1156daef3dd1SRichard Weinberger 	for (i = 0; i < ubi->fm_pool.size; i++) {
1157f6e951afSshengyong 		fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
1158daef3dd1SRichard Weinberger 		set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs);
1159daef3dd1SRichard Weinberger 	}
1160dbb7d2a8SRichard Weinberger 
1161f6e951afSshengyong 	fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1162f6e951afSshengyong 	fm_pos += sizeof(*fmpl_wl);
1163f6e951afSshengyong 	fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1164f6e951afSshengyong 	fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size);
1165f6e951afSshengyong 	fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
1166dbb7d2a8SRichard Weinberger 
1167daef3dd1SRichard Weinberger 	for (i = 0; i < ubi->fm_wl_pool.size; i++) {
1168f6e951afSshengyong 		fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
1169daef3dd1SRichard Weinberger 		set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs);
1170daef3dd1SRichard Weinberger 	}
1171dbb7d2a8SRichard Weinberger 
1172c5c3f3cfSRichard Weinberger 	ubi_for_each_free_peb(ubi, wl_e, tmp_rb) {
1173dbb7d2a8SRichard Weinberger 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1174dbb7d2a8SRichard Weinberger 
1175dbb7d2a8SRichard Weinberger 		fec->pnum = cpu_to_be32(wl_e->pnum);
1176daef3dd1SRichard Weinberger 		set_seen(ubi, wl_e->pnum, seen_pebs);
1177dbb7d2a8SRichard Weinberger 		fec->ec = cpu_to_be32(wl_e->ec);
1178dbb7d2a8SRichard Weinberger 
1179dbb7d2a8SRichard Weinberger 		free_peb_count++;
1180dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fec);
1181dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1182dbb7d2a8SRichard Weinberger 	}
1183dbb7d2a8SRichard Weinberger 	fmh->free_peb_count = cpu_to_be32(free_peb_count);
1184dbb7d2a8SRichard Weinberger 
1185c5c3f3cfSRichard Weinberger 	ubi_for_each_used_peb(ubi, wl_e, tmp_rb) {
1186dbb7d2a8SRichard Weinberger 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1187dbb7d2a8SRichard Weinberger 
1188dbb7d2a8SRichard Weinberger 		fec->pnum = cpu_to_be32(wl_e->pnum);
1189daef3dd1SRichard Weinberger 		set_seen(ubi, wl_e->pnum, seen_pebs);
1190dbb7d2a8SRichard Weinberger 		fec->ec = cpu_to_be32(wl_e->ec);
1191dbb7d2a8SRichard Weinberger 
1192dbb7d2a8SRichard Weinberger 		used_peb_count++;
1193dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fec);
1194dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1195dbb7d2a8SRichard Weinberger 	}
11964f5e3b6fSRichard Weinberger 
1197c5c3f3cfSRichard Weinberger 	ubi_for_each_protected_peb(ubi, i, wl_e) {
11984f5e3b6fSRichard Weinberger 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
11994f5e3b6fSRichard Weinberger 
12004f5e3b6fSRichard Weinberger 		fec->pnum = cpu_to_be32(wl_e->pnum);
1201c5c3f3cfSRichard Weinberger 		set_seen(ubi, wl_e->pnum, seen_pebs);
12024f5e3b6fSRichard Weinberger 		fec->ec = cpu_to_be32(wl_e->ec);
12034f5e3b6fSRichard Weinberger 
12044f5e3b6fSRichard Weinberger 		used_peb_count++;
12054f5e3b6fSRichard Weinberger 		fm_pos += sizeof(*fec);
12064f5e3b6fSRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
12074f5e3b6fSRichard Weinberger 	}
1208dbb7d2a8SRichard Weinberger 	fmh->used_peb_count = cpu_to_be32(used_peb_count);
1209dbb7d2a8SRichard Weinberger 
1210c5c3f3cfSRichard Weinberger 	ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) {
1211dbb7d2a8SRichard Weinberger 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1212dbb7d2a8SRichard Weinberger 
1213dbb7d2a8SRichard Weinberger 		fec->pnum = cpu_to_be32(wl_e->pnum);
1214daef3dd1SRichard Weinberger 		set_seen(ubi, wl_e->pnum, seen_pebs);
1215dbb7d2a8SRichard Weinberger 		fec->ec = cpu_to_be32(wl_e->ec);
1216dbb7d2a8SRichard Weinberger 
1217dbb7d2a8SRichard Weinberger 		scrub_peb_count++;
1218dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fec);
1219dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1220dbb7d2a8SRichard Weinberger 	}
1221dbb7d2a8SRichard Weinberger 	fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
1222dbb7d2a8SRichard Weinberger 
1223dbb7d2a8SRichard Weinberger 
1224dbb7d2a8SRichard Weinberger 	list_for_each_entry(ubi_wrk, &ubi->works, list) {
1225dbb7d2a8SRichard Weinberger 		if (ubi_is_erase_work(ubi_wrk)) {
1226dbb7d2a8SRichard Weinberger 			wl_e = ubi_wrk->e;
1227dbb7d2a8SRichard Weinberger 			ubi_assert(wl_e);
1228dbb7d2a8SRichard Weinberger 
1229dbb7d2a8SRichard Weinberger 			fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1230dbb7d2a8SRichard Weinberger 
1231dbb7d2a8SRichard Weinberger 			fec->pnum = cpu_to_be32(wl_e->pnum);
1232daef3dd1SRichard Weinberger 			set_seen(ubi, wl_e->pnum, seen_pebs);
1233dbb7d2a8SRichard Weinberger 			fec->ec = cpu_to_be32(wl_e->ec);
1234dbb7d2a8SRichard Weinberger 
1235dbb7d2a8SRichard Weinberger 			erase_peb_count++;
1236dbb7d2a8SRichard Weinberger 			fm_pos += sizeof(*fec);
1237dbb7d2a8SRichard Weinberger 			ubi_assert(fm_pos <= ubi->fm_size);
1238dbb7d2a8SRichard Weinberger 		}
1239dbb7d2a8SRichard Weinberger 	}
1240dbb7d2a8SRichard Weinberger 	fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
1241dbb7d2a8SRichard Weinberger 
1242dbb7d2a8SRichard Weinberger 	for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
1243dbb7d2a8SRichard Weinberger 		vol = ubi->volumes[i];
1244dbb7d2a8SRichard Weinberger 
1245dbb7d2a8SRichard Weinberger 		if (!vol)
1246dbb7d2a8SRichard Weinberger 			continue;
1247dbb7d2a8SRichard Weinberger 
1248dbb7d2a8SRichard Weinberger 		vol_count++;
1249dbb7d2a8SRichard Weinberger 
1250dbb7d2a8SRichard Weinberger 		fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
1251dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fvh);
1252dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1253dbb7d2a8SRichard Weinberger 
1254dbb7d2a8SRichard Weinberger 		fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
1255dbb7d2a8SRichard Weinberger 		fvh->vol_id = cpu_to_be32(vol->vol_id);
1256dbb7d2a8SRichard Weinberger 		fvh->vol_type = vol->vol_type;
1257dbb7d2a8SRichard Weinberger 		fvh->used_ebs = cpu_to_be32(vol->used_ebs);
1258dbb7d2a8SRichard Weinberger 		fvh->data_pad = cpu_to_be32(vol->data_pad);
1259dbb7d2a8SRichard Weinberger 		fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
1260dbb7d2a8SRichard Weinberger 
1261dbb7d2a8SRichard Weinberger 		ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
1262dbb7d2a8SRichard Weinberger 			vol->vol_type == UBI_STATIC_VOLUME);
1263dbb7d2a8SRichard Weinberger 
1264dbb7d2a8SRichard Weinberger 		feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
1265dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
1266dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1267dbb7d2a8SRichard Weinberger 
1268dbb7d2a8SRichard Weinberger 		for (j = 0; j < vol->reserved_pebs; j++)
1269dbb7d2a8SRichard Weinberger 			feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]);
1270dbb7d2a8SRichard Weinberger 
1271dbb7d2a8SRichard Weinberger 		feba->reserved_pebs = cpu_to_be32(j);
1272dbb7d2a8SRichard Weinberger 		feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
1273dbb7d2a8SRichard Weinberger 	}
1274dbb7d2a8SRichard Weinberger 	fmh->vol_count = cpu_to_be32(vol_count);
1275dbb7d2a8SRichard Weinberger 	fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
1276dbb7d2a8SRichard Weinberger 
1277dbb7d2a8SRichard Weinberger 	avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1278dbb7d2a8SRichard Weinberger 	avhdr->lnum = 0;
1279dbb7d2a8SRichard Weinberger 
1280dbb7d2a8SRichard Weinberger 	spin_unlock(&ubi->wl_lock);
1281dbb7d2a8SRichard Weinberger 	spin_unlock(&ubi->volumes_lock);
1282dbb7d2a8SRichard Weinberger 
1283dbb7d2a8SRichard Weinberger 	dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
1284dbb7d2a8SRichard Weinberger 	ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
1285dbb7d2a8SRichard Weinberger 	if (ret) {
128632608703STanya Brokhman 		ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
1287dbb7d2a8SRichard Weinberger 		goto out_kfree;
1288dbb7d2a8SRichard Weinberger 	}
1289dbb7d2a8SRichard Weinberger 
1290dbb7d2a8SRichard Weinberger 	for (i = 0; i < new_fm->used_blocks; i++) {
1291dbb7d2a8SRichard Weinberger 		fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
1292daef3dd1SRichard Weinberger 		set_seen(ubi, new_fm->e[i]->pnum, seen_pebs);
1293dbb7d2a8SRichard Weinberger 		fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
1294dbb7d2a8SRichard Weinberger 	}
1295dbb7d2a8SRichard Weinberger 
1296dbb7d2a8SRichard Weinberger 	fmsb->data_crc = 0;
1297dbb7d2a8SRichard Weinberger 	fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
1298dbb7d2a8SRichard Weinberger 					   ubi->fm_size));
1299dbb7d2a8SRichard Weinberger 
1300dbb7d2a8SRichard Weinberger 	for (i = 1; i < new_fm->used_blocks; i++) {
1301dbb7d2a8SRichard Weinberger 		dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1302dbb7d2a8SRichard Weinberger 		dvhdr->lnum = cpu_to_be32(i);
1303dbb7d2a8SRichard Weinberger 		dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1304dbb7d2a8SRichard Weinberger 			new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
1305dbb7d2a8SRichard Weinberger 		ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
1306dbb7d2a8SRichard Weinberger 		if (ret) {
130732608703STanya Brokhman 			ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
1308dbb7d2a8SRichard Weinberger 				new_fm->e[i]->pnum);
1309dbb7d2a8SRichard Weinberger 			goto out_kfree;
1310dbb7d2a8SRichard Weinberger 		}
1311dbb7d2a8SRichard Weinberger 	}
1312dbb7d2a8SRichard Weinberger 
1313dbb7d2a8SRichard Weinberger 	for (i = 0; i < new_fm->used_blocks; i++) {
1314*fcbb6af1SBoris Brezillon 		ret = ubi_io_write_data(ubi, fm_raw + (i * ubi->leb_size),
1315*fcbb6af1SBoris Brezillon 					new_fm->e[i]->pnum, 0, ubi->leb_size);
1316dbb7d2a8SRichard Weinberger 		if (ret) {
131732608703STanya Brokhman 			ubi_err(ubi, "unable to write fastmap to PEB %i!",
1318dbb7d2a8SRichard Weinberger 				new_fm->e[i]->pnum);
1319dbb7d2a8SRichard Weinberger 			goto out_kfree;
1320dbb7d2a8SRichard Weinberger 		}
1321dbb7d2a8SRichard Weinberger 	}
1322dbb7d2a8SRichard Weinberger 
1323dbb7d2a8SRichard Weinberger 	ubi_assert(new_fm);
1324dbb7d2a8SRichard Weinberger 	ubi->fm = new_fm;
1325dbb7d2a8SRichard Weinberger 
1326daef3dd1SRichard Weinberger 	ret = self_check_seen(ubi, seen_pebs);
1327dbb7d2a8SRichard Weinberger 	dbg_bld("fastmap written!");
1328dbb7d2a8SRichard Weinberger 
1329dbb7d2a8SRichard Weinberger out_kfree:
1330dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, avhdr);
1331dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, dvhdr);
1332daef3dd1SRichard Weinberger 	free_seen(seen_pebs);
1333dbb7d2a8SRichard Weinberger out:
1334dbb7d2a8SRichard Weinberger 	return ret;
1335dbb7d2a8SRichard Weinberger }
1336dbb7d2a8SRichard Weinberger 
1337dbb7d2a8SRichard Weinberger /**
1338dbb7d2a8SRichard Weinberger  * erase_block - Manually erase a PEB.
1339dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1340dbb7d2a8SRichard Weinberger  * @pnum: PEB to be erased
1341dbb7d2a8SRichard Weinberger  *
1342dbb7d2a8SRichard Weinberger  * Returns the new EC value on success, < 0 indicates an internal error.
1343dbb7d2a8SRichard Weinberger  */
1344dbb7d2a8SRichard Weinberger static int erase_block(struct ubi_device *ubi, int pnum)
1345dbb7d2a8SRichard Weinberger {
1346dbb7d2a8SRichard Weinberger 	int ret;
1347dbb7d2a8SRichard Weinberger 	struct ubi_ec_hdr *ec_hdr;
1348dbb7d2a8SRichard Weinberger 	long long ec;
1349dbb7d2a8SRichard Weinberger 
1350dbb7d2a8SRichard Weinberger 	ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1351dbb7d2a8SRichard Weinberger 	if (!ec_hdr)
1352dbb7d2a8SRichard Weinberger 		return -ENOMEM;
1353dbb7d2a8SRichard Weinberger 
1354dbb7d2a8SRichard Weinberger 	ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1355dbb7d2a8SRichard Weinberger 	if (ret < 0)
1356dbb7d2a8SRichard Weinberger 		goto out;
1357dbb7d2a8SRichard Weinberger 	else if (ret && ret != UBI_IO_BITFLIPS) {
1358dbb7d2a8SRichard Weinberger 		ret = -EINVAL;
1359dbb7d2a8SRichard Weinberger 		goto out;
1360dbb7d2a8SRichard Weinberger 	}
1361dbb7d2a8SRichard Weinberger 
1362dbb7d2a8SRichard Weinberger 	ret = ubi_io_sync_erase(ubi, pnum, 0);
1363dbb7d2a8SRichard Weinberger 	if (ret < 0)
1364dbb7d2a8SRichard Weinberger 		goto out;
1365dbb7d2a8SRichard Weinberger 
1366dbb7d2a8SRichard Weinberger 	ec = be64_to_cpu(ec_hdr->ec);
1367dbb7d2a8SRichard Weinberger 	ec += ret;
1368dbb7d2a8SRichard Weinberger 	if (ec > UBI_MAX_ERASECOUNTER) {
1369dbb7d2a8SRichard Weinberger 		ret = -EINVAL;
1370dbb7d2a8SRichard Weinberger 		goto out;
1371dbb7d2a8SRichard Weinberger 	}
1372dbb7d2a8SRichard Weinberger 
1373dbb7d2a8SRichard Weinberger 	ec_hdr->ec = cpu_to_be64(ec);
1374dbb7d2a8SRichard Weinberger 	ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
1375dbb7d2a8SRichard Weinberger 	if (ret < 0)
1376dbb7d2a8SRichard Weinberger 		goto out;
1377dbb7d2a8SRichard Weinberger 
1378dbb7d2a8SRichard Weinberger 	ret = ec;
1379dbb7d2a8SRichard Weinberger out:
1380dbb7d2a8SRichard Weinberger 	kfree(ec_hdr);
1381dbb7d2a8SRichard Weinberger 	return ret;
1382dbb7d2a8SRichard Weinberger }
1383dbb7d2a8SRichard Weinberger 
1384dbb7d2a8SRichard Weinberger /**
1385dbb7d2a8SRichard Weinberger  * invalidate_fastmap - destroys a fastmap.
1386dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1387dbb7d2a8SRichard Weinberger  *
13885ca97ad8SRichard Weinberger  * This function ensures that upon next UBI attach a full scan
13895ca97ad8SRichard Weinberger  * is issued. We need this if UBI is about to write a new fastmap
13905ca97ad8SRichard Weinberger  * but is unable to do so. In this case we have two options:
13915ca97ad8SRichard Weinberger  * a) Make sure that the current fastmap will not be usued upon
13925ca97ad8SRichard Weinberger  * attach time and contine or b) fall back to RO mode to have the
13935ca97ad8SRichard Weinberger  * current fastmap in a valid state.
1394dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
1395dbb7d2a8SRichard Weinberger  */
13965ca97ad8SRichard Weinberger static int invalidate_fastmap(struct ubi_device *ubi)
1397dbb7d2a8SRichard Weinberger {
13988930fa50SRichard Weinberger 	int ret;
13995ca97ad8SRichard Weinberger 	struct ubi_fastmap_layout *fm;
14005ca97ad8SRichard Weinberger 	struct ubi_wl_entry *e;
14015ca97ad8SRichard Weinberger 	struct ubi_vid_hdr *vh = NULL;
1402dbb7d2a8SRichard Weinberger 
14035ca97ad8SRichard Weinberger 	if (!ubi->fm)
14045ca97ad8SRichard Weinberger 		return 0;
14055ca97ad8SRichard Weinberger 
14065ca97ad8SRichard Weinberger 	ubi->fm = NULL;
14075ca97ad8SRichard Weinberger 
14085ca97ad8SRichard Weinberger 	ret = -ENOMEM;
14095ca97ad8SRichard Weinberger 	fm = kzalloc(sizeof(*fm), GFP_KERNEL);
14105ca97ad8SRichard Weinberger 	if (!fm)
14115ca97ad8SRichard Weinberger 		goto out;
1412dbb7d2a8SRichard Weinberger 
1413dbb7d2a8SRichard Weinberger 	vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1414dbb7d2a8SRichard Weinberger 	if (!vh)
14155ca97ad8SRichard Weinberger 		goto out_free_fm;
1416dbb7d2a8SRichard Weinberger 
14175ca97ad8SRichard Weinberger 	ret = -ENOSPC;
14185ca97ad8SRichard Weinberger 	e = ubi_wl_get_fm_peb(ubi, 1);
14195ca97ad8SRichard Weinberger 	if (!e)
14205ca97ad8SRichard Weinberger 		goto out_free_fm;
14215ca97ad8SRichard Weinberger 
14225ca97ad8SRichard Weinberger 	/*
14235ca97ad8SRichard Weinberger 	 * Create fake fastmap such that UBI will fall back
14245ca97ad8SRichard Weinberger 	 * to scanning mode.
14255ca97ad8SRichard Weinberger 	 */
1426dbb7d2a8SRichard Weinberger 	vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
14275ca97ad8SRichard Weinberger 	ret = ubi_io_write_vid_hdr(ubi, e->pnum, vh);
14285ca97ad8SRichard Weinberger 	if (ret < 0) {
14295ca97ad8SRichard Weinberger 		ubi_wl_put_fm_peb(ubi, e, 0, 0);
14305ca97ad8SRichard Weinberger 		goto out_free_fm;
14315ca97ad8SRichard Weinberger 	}
1432dbb7d2a8SRichard Weinberger 
14335ca97ad8SRichard Weinberger 	fm->used_blocks = 1;
14345ca97ad8SRichard Weinberger 	fm->e[0] = e;
14355ca97ad8SRichard Weinberger 
14365ca97ad8SRichard Weinberger 	ubi->fm = fm;
14375ca97ad8SRichard Weinberger 
14385ca97ad8SRichard Weinberger out:
14395ca97ad8SRichard Weinberger 	ubi_free_vid_hdr(ubi, vh);
1440dbb7d2a8SRichard Weinberger 	return ret;
14415ca97ad8SRichard Weinberger 
14425ca97ad8SRichard Weinberger out_free_fm:
14435ca97ad8SRichard Weinberger 	kfree(fm);
14445ca97ad8SRichard Weinberger 	goto out;
14455ca97ad8SRichard Weinberger }
14465ca97ad8SRichard Weinberger 
14475ca97ad8SRichard Weinberger /**
14485ca97ad8SRichard Weinberger  * return_fm_pebs - returns all PEBs used by a fastmap back to the
14495ca97ad8SRichard Weinberger  * WL sub-system.
14505ca97ad8SRichard Weinberger  * @ubi: UBI device object
14515ca97ad8SRichard Weinberger  * @fm: fastmap layout object
14525ca97ad8SRichard Weinberger  */
14535ca97ad8SRichard Weinberger static void return_fm_pebs(struct ubi_device *ubi,
14545ca97ad8SRichard Weinberger 			   struct ubi_fastmap_layout *fm)
14555ca97ad8SRichard Weinberger {
14565ca97ad8SRichard Weinberger 	int i;
14575ca97ad8SRichard Weinberger 
14585ca97ad8SRichard Weinberger 	if (!fm)
14595ca97ad8SRichard Weinberger 		return;
14605ca97ad8SRichard Weinberger 
14615ca97ad8SRichard Weinberger 	for (i = 0; i < fm->used_blocks; i++) {
14625ca97ad8SRichard Weinberger 		if (fm->e[i]) {
14635ca97ad8SRichard Weinberger 			ubi_wl_put_fm_peb(ubi, fm->e[i], i,
14645ca97ad8SRichard Weinberger 					  fm->to_be_tortured[i]);
14655ca97ad8SRichard Weinberger 			fm->e[i] = NULL;
14665ca97ad8SRichard Weinberger 		}
14675ca97ad8SRichard Weinberger 	}
1468dbb7d2a8SRichard Weinberger }
1469dbb7d2a8SRichard Weinberger 
1470dbb7d2a8SRichard Weinberger /**
1471dbb7d2a8SRichard Weinberger  * ubi_update_fastmap - will be called by UBI if a volume changes or
1472dbb7d2a8SRichard Weinberger  * a fastmap pool becomes full.
1473dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1474dbb7d2a8SRichard Weinberger  *
1475dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
1476dbb7d2a8SRichard Weinberger  */
1477dbb7d2a8SRichard Weinberger int ubi_update_fastmap(struct ubi_device *ubi)
1478dbb7d2a8SRichard Weinberger {
14795ca97ad8SRichard Weinberger 	int ret, i, j;
1480dbb7d2a8SRichard Weinberger 	struct ubi_fastmap_layout *new_fm, *old_fm;
1481dbb7d2a8SRichard Weinberger 	struct ubi_wl_entry *tmp_e;
1482dbb7d2a8SRichard Weinberger 
1483111ab0b2SRichard Weinberger 	down_write(&ubi->fm_protect);
1484dbb7d2a8SRichard Weinberger 
1485dbb7d2a8SRichard Weinberger 	ubi_refill_pools(ubi);
1486dbb7d2a8SRichard Weinberger 
1487dbb7d2a8SRichard Weinberger 	if (ubi->ro_mode || ubi->fm_disabled) {
1488111ab0b2SRichard Weinberger 		up_write(&ubi->fm_protect);
1489dbb7d2a8SRichard Weinberger 		return 0;
1490dbb7d2a8SRichard Weinberger 	}
1491dbb7d2a8SRichard Weinberger 
1492dbb7d2a8SRichard Weinberger 	ret = ubi_ensure_anchor_pebs(ubi);
1493dbb7d2a8SRichard Weinberger 	if (ret) {
1494111ab0b2SRichard Weinberger 		up_write(&ubi->fm_protect);
1495dbb7d2a8SRichard Weinberger 		return ret;
1496dbb7d2a8SRichard Weinberger 	}
1497dbb7d2a8SRichard Weinberger 
1498dbb7d2a8SRichard Weinberger 	new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
1499dbb7d2a8SRichard Weinberger 	if (!new_fm) {
1500111ab0b2SRichard Weinberger 		up_write(&ubi->fm_protect);
1501dbb7d2a8SRichard Weinberger 		return -ENOMEM;
1502dbb7d2a8SRichard Weinberger 	}
1503dbb7d2a8SRichard Weinberger 
1504dbb7d2a8SRichard Weinberger 	new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
1505dbb7d2a8SRichard Weinberger 	old_fm = ubi->fm;
1506dbb7d2a8SRichard Weinberger 	ubi->fm = NULL;
1507dbb7d2a8SRichard Weinberger 
1508dbb7d2a8SRichard Weinberger 	if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
150932608703STanya Brokhman 		ubi_err(ubi, "fastmap too large");
1510dbb7d2a8SRichard Weinberger 		ret = -ENOSPC;
1511dbb7d2a8SRichard Weinberger 		goto err;
1512dbb7d2a8SRichard Weinberger 	}
1513dbb7d2a8SRichard Weinberger 
1514dbb7d2a8SRichard Weinberger 	for (i = 1; i < new_fm->used_blocks; i++) {
1515dbb7d2a8SRichard Weinberger 		spin_lock(&ubi->wl_lock);
1516dbb7d2a8SRichard Weinberger 		tmp_e = ubi_wl_get_fm_peb(ubi, 0);
1517dbb7d2a8SRichard Weinberger 		spin_unlock(&ubi->wl_lock);
1518dbb7d2a8SRichard Weinberger 
15195ca97ad8SRichard Weinberger 		if (!tmp_e) {
15205ca97ad8SRichard Weinberger 			if (old_fm && old_fm->e[i]) {
1521dbb7d2a8SRichard Weinberger 				ret = erase_block(ubi, old_fm->e[i]->pnum);
1522dbb7d2a8SRichard Weinberger 				if (ret < 0) {
15235ca97ad8SRichard Weinberger 					ubi_err(ubi, "could not erase old fastmap PEB");
1524dbb7d2a8SRichard Weinberger 
15255ca97ad8SRichard Weinberger 					for (j = 1; j < i; j++) {
1526dbb7d2a8SRichard Weinberger 						ubi_wl_put_fm_peb(ubi, new_fm->e[j],
1527dbb7d2a8SRichard Weinberger 								  j, 0);
15285ca97ad8SRichard Weinberger 						new_fm->e[j] = NULL;
15295ca97ad8SRichard Weinberger 					}
1530dbb7d2a8SRichard Weinberger 					goto err;
1531dbb7d2a8SRichard Weinberger 				}
1532c4ca6be9SRichard Weinberger 				new_fm->e[i] = old_fm->e[i];
15335ca97ad8SRichard Weinberger 				old_fm->e[i] = NULL;
15345ca97ad8SRichard Weinberger 			} else {
15355ca97ad8SRichard Weinberger 				ubi_err(ubi, "could not get any free erase block");
15365ca97ad8SRichard Weinberger 
15375ca97ad8SRichard Weinberger 				for (j = 1; j < i; j++) {
15385ca97ad8SRichard Weinberger 					ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
15395ca97ad8SRichard Weinberger 					new_fm->e[j] = NULL;
15405ca97ad8SRichard Weinberger 				}
15415ca97ad8SRichard Weinberger 
15425ca97ad8SRichard Weinberger 				ret = -ENOSPC;
15435ca97ad8SRichard Weinberger 				goto err;
15445ca97ad8SRichard Weinberger 			}
1545dbb7d2a8SRichard Weinberger 		} else {
1546c4ca6be9SRichard Weinberger 			new_fm->e[i] = tmp_e;
1547dbb7d2a8SRichard Weinberger 
15485ca97ad8SRichard Weinberger 			if (old_fm && old_fm->e[i]) {
154961de74ceSRichard Weinberger 				ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
155061de74ceSRichard Weinberger 						  old_fm->to_be_tortured[i]);
15515ca97ad8SRichard Weinberger 				old_fm->e[i] = NULL;
15525ca97ad8SRichard Weinberger 			}
155361de74ceSRichard Weinberger 		}
155461de74ceSRichard Weinberger 	}
155561de74ceSRichard Weinberger 
155661de74ceSRichard Weinberger 	/* Old fastmap is larger than the new one */
155761de74ceSRichard Weinberger 	if (old_fm && new_fm->used_blocks < old_fm->used_blocks) {
155861de74ceSRichard Weinberger 		for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) {
1559dbb7d2a8SRichard Weinberger 			ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1560dbb7d2a8SRichard Weinberger 					  old_fm->to_be_tortured[i]);
15615ca97ad8SRichard Weinberger 			old_fm->e[i] = NULL;
1562dbb7d2a8SRichard Weinberger 		}
1563dbb7d2a8SRichard Weinberger 	}
1564dbb7d2a8SRichard Weinberger 
1565dbb7d2a8SRichard Weinberger 	spin_lock(&ubi->wl_lock);
1566dbb7d2a8SRichard Weinberger 	tmp_e = ubi_wl_get_fm_peb(ubi, 1);
1567dbb7d2a8SRichard Weinberger 	spin_unlock(&ubi->wl_lock);
1568dbb7d2a8SRichard Weinberger 
1569dbb7d2a8SRichard Weinberger 	if (old_fm) {
1570dbb7d2a8SRichard Weinberger 		/* no fresh anchor PEB was found, reuse the old one */
1571dbb7d2a8SRichard Weinberger 		if (!tmp_e) {
1572dbb7d2a8SRichard Weinberger 			ret = erase_block(ubi, old_fm->e[0]->pnum);
1573dbb7d2a8SRichard Weinberger 			if (ret < 0) {
157432608703STanya Brokhman 				ubi_err(ubi, "could not erase old anchor PEB");
1575dbb7d2a8SRichard Weinberger 
15765ca97ad8SRichard Weinberger 				for (i = 1; i < new_fm->used_blocks; i++) {
1577dbb7d2a8SRichard Weinberger 					ubi_wl_put_fm_peb(ubi, new_fm->e[i],
1578dbb7d2a8SRichard Weinberger 							  i, 0);
15795ca97ad8SRichard Weinberger 					new_fm->e[i] = NULL;
15805ca97ad8SRichard Weinberger 				}
1581dbb7d2a8SRichard Weinberger 				goto err;
1582dbb7d2a8SRichard Weinberger 			}
1583c4ca6be9SRichard Weinberger 			new_fm->e[0] = old_fm->e[0];
1584dbb7d2a8SRichard Weinberger 			new_fm->e[0]->ec = ret;
15855ca97ad8SRichard Weinberger 			old_fm->e[0] = NULL;
1586dbb7d2a8SRichard Weinberger 		} else {
1587dbb7d2a8SRichard Weinberger 			/* we've got a new anchor PEB, return the old one */
1588dbb7d2a8SRichard Weinberger 			ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
1589dbb7d2a8SRichard Weinberger 					  old_fm->to_be_tortured[0]);
1590c4ca6be9SRichard Weinberger 			new_fm->e[0] = tmp_e;
15915ca97ad8SRichard Weinberger 			old_fm->e[0] = NULL;
1592dbb7d2a8SRichard Weinberger 		}
1593dbb7d2a8SRichard Weinberger 	} else {
1594dbb7d2a8SRichard Weinberger 		if (!tmp_e) {
159532608703STanya Brokhman 			ubi_err(ubi, "could not find any anchor PEB");
1596dbb7d2a8SRichard Weinberger 
15975ca97ad8SRichard Weinberger 			for (i = 1; i < new_fm->used_blocks; i++) {
1598dbb7d2a8SRichard Weinberger 				ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
15995ca97ad8SRichard Weinberger 				new_fm->e[i] = NULL;
16005ca97ad8SRichard Weinberger 			}
1601dbb7d2a8SRichard Weinberger 
1602dbb7d2a8SRichard Weinberger 			ret = -ENOSPC;
1603dbb7d2a8SRichard Weinberger 			goto err;
1604dbb7d2a8SRichard Weinberger 		}
1605c4ca6be9SRichard Weinberger 		new_fm->e[0] = tmp_e;
1606dbb7d2a8SRichard Weinberger 	}
1607dbb7d2a8SRichard Weinberger 
1608dbb7d2a8SRichard Weinberger 	down_write(&ubi->work_sem);
1609111ab0b2SRichard Weinberger 	down_write(&ubi->fm_eba_sem);
1610dbb7d2a8SRichard Weinberger 	ret = ubi_write_fastmap(ubi, new_fm);
1611111ab0b2SRichard Weinberger 	up_write(&ubi->fm_eba_sem);
1612dbb7d2a8SRichard Weinberger 	up_write(&ubi->work_sem);
1613dbb7d2a8SRichard Weinberger 
1614dbb7d2a8SRichard Weinberger 	if (ret)
1615dbb7d2a8SRichard Weinberger 		goto err;
1616dbb7d2a8SRichard Weinberger 
1617dbb7d2a8SRichard Weinberger out_unlock:
1618111ab0b2SRichard Weinberger 	up_write(&ubi->fm_protect);
1619dbb7d2a8SRichard Weinberger 	kfree(old_fm);
1620dbb7d2a8SRichard Weinberger 	return ret;
1621dbb7d2a8SRichard Weinberger 
1622dbb7d2a8SRichard Weinberger err:
162332608703STanya Brokhman 	ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret);
1624dbb7d2a8SRichard Weinberger 
16255ca97ad8SRichard Weinberger 	ret = invalidate_fastmap(ubi);
16262d93fb36SRichard Weinberger 	if (ret < 0) {
162732608703STanya Brokhman 		ubi_err(ubi, "Unable to invalidiate current fastmap!");
16282d93fb36SRichard Weinberger 		ubi_ro_mode(ubi);
16295ca97ad8SRichard Weinberger 	} else {
16305ca97ad8SRichard Weinberger 		return_fm_pebs(ubi, old_fm);
16315ca97ad8SRichard Weinberger 		return_fm_pebs(ubi, new_fm);
1632dbb7d2a8SRichard Weinberger 		ret = 0;
1633dbb7d2a8SRichard Weinberger 	}
16345ca97ad8SRichard Weinberger 
16355ca97ad8SRichard Weinberger 	kfree(new_fm);
1636dbb7d2a8SRichard Weinberger 	goto out_unlock;
1637dbb7d2a8SRichard Weinberger }
1638