xref: /openbmc/linux/drivers/mtd/ubi/fastmap.c (revision 44305ebde243a7cce2c592cc89afe5041d8bf884)
1dbb7d2a8SRichard Weinberger /*
2dbb7d2a8SRichard Weinberger  * Copyright (c) 2012 Linutronix GmbH
3dbb7d2a8SRichard Weinberger  * Author: Richard Weinberger <richard@nod.at>
4dbb7d2a8SRichard Weinberger  *
5dbb7d2a8SRichard Weinberger  * This program is free software; you can redistribute it and/or modify
6dbb7d2a8SRichard Weinberger  * it under the terms of the GNU General Public License as published by
7dbb7d2a8SRichard Weinberger  * the Free Software Foundation; version 2.
8dbb7d2a8SRichard Weinberger  *
9dbb7d2a8SRichard Weinberger  * This program is distributed in the hope that it will be useful,
10dbb7d2a8SRichard Weinberger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11dbb7d2a8SRichard Weinberger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12dbb7d2a8SRichard Weinberger  * the GNU General Public License for more details.
13dbb7d2a8SRichard Weinberger  *
14dbb7d2a8SRichard Weinberger  */
15dbb7d2a8SRichard Weinberger 
16dbb7d2a8SRichard Weinberger #include <linux/crc32.h>
17dbb7d2a8SRichard Weinberger #include "ubi.h"
18dbb7d2a8SRichard Weinberger 
19dbb7d2a8SRichard Weinberger /**
20dbb7d2a8SRichard Weinberger  * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
21dbb7d2a8SRichard Weinberger  * @ubi: UBI device description object
22dbb7d2a8SRichard Weinberger  */
23dbb7d2a8SRichard Weinberger size_t ubi_calc_fm_size(struct ubi_device *ubi)
24dbb7d2a8SRichard Weinberger {
25dbb7d2a8SRichard Weinberger 	size_t size;
26dbb7d2a8SRichard Weinberger 
27dbb7d2a8SRichard Weinberger 	size = sizeof(struct ubi_fm_hdr) + \
28dbb7d2a8SRichard Weinberger 		sizeof(struct ubi_fm_scan_pool) + \
29dbb7d2a8SRichard Weinberger 		sizeof(struct ubi_fm_scan_pool) + \
30dbb7d2a8SRichard Weinberger 		(ubi->peb_count * sizeof(struct ubi_fm_ec)) + \
31dbb7d2a8SRichard Weinberger 		(sizeof(struct ubi_fm_eba) + \
32dbb7d2a8SRichard Weinberger 		(ubi->peb_count * sizeof(__be32))) + \
33dbb7d2a8SRichard Weinberger 		sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
34dbb7d2a8SRichard Weinberger 	return roundup(size, ubi->leb_size);
35dbb7d2a8SRichard Weinberger }
36dbb7d2a8SRichard Weinberger 
37dbb7d2a8SRichard Weinberger 
38dbb7d2a8SRichard Weinberger /**
39dbb7d2a8SRichard Weinberger  * new_fm_vhdr - allocate a new volume header for fastmap usage.
40dbb7d2a8SRichard Weinberger  * @ubi: UBI device description object
41dbb7d2a8SRichard Weinberger  * @vol_id: the VID of the new header
42dbb7d2a8SRichard Weinberger  *
43dbb7d2a8SRichard Weinberger  * Returns a new struct ubi_vid_hdr on success.
44dbb7d2a8SRichard Weinberger  * NULL indicates out of memory.
45dbb7d2a8SRichard Weinberger  */
46dbb7d2a8SRichard Weinberger static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id)
47dbb7d2a8SRichard Weinberger {
48dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *new;
49dbb7d2a8SRichard Weinberger 
50dbb7d2a8SRichard Weinberger 	new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
51dbb7d2a8SRichard Weinberger 	if (!new)
52dbb7d2a8SRichard Weinberger 		goto out;
53dbb7d2a8SRichard Weinberger 
54dbb7d2a8SRichard Weinberger 	new->vol_type = UBI_VID_DYNAMIC;
55dbb7d2a8SRichard Weinberger 	new->vol_id = cpu_to_be32(vol_id);
56dbb7d2a8SRichard Weinberger 
57dbb7d2a8SRichard Weinberger 	/* UBI implementations without fastmap support have to delete the
58dbb7d2a8SRichard Weinberger 	 * fastmap.
59dbb7d2a8SRichard Weinberger 	 */
60dbb7d2a8SRichard Weinberger 	new->compat = UBI_COMPAT_DELETE;
61dbb7d2a8SRichard Weinberger 
62dbb7d2a8SRichard Weinberger out:
63dbb7d2a8SRichard Weinberger 	return new;
64dbb7d2a8SRichard Weinberger }
65dbb7d2a8SRichard Weinberger 
66dbb7d2a8SRichard Weinberger /**
67dbb7d2a8SRichard Weinberger  * add_aeb - create and add a attach erase block to a given list.
68dbb7d2a8SRichard Weinberger  * @ai: UBI attach info object
69dbb7d2a8SRichard Weinberger  * @list: the target list
70dbb7d2a8SRichard Weinberger  * @pnum: PEB number of the new attach erase block
71dbb7d2a8SRichard Weinberger  * @ec: erease counter of the new LEB
72dbb7d2a8SRichard Weinberger  * @scrub: scrub this PEB after attaching
73dbb7d2a8SRichard Weinberger  *
74dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
75dbb7d2a8SRichard Weinberger  */
76dbb7d2a8SRichard Weinberger static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
77dbb7d2a8SRichard Weinberger 		   int pnum, int ec, int scrub)
78dbb7d2a8SRichard Weinberger {
79dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb;
80dbb7d2a8SRichard Weinberger 
81dbb7d2a8SRichard Weinberger 	aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
82dbb7d2a8SRichard Weinberger 	if (!aeb)
83dbb7d2a8SRichard Weinberger 		return -ENOMEM;
84dbb7d2a8SRichard Weinberger 
85dbb7d2a8SRichard Weinberger 	aeb->pnum = pnum;
86dbb7d2a8SRichard Weinberger 	aeb->ec = ec;
87dbb7d2a8SRichard Weinberger 	aeb->lnum = -1;
88dbb7d2a8SRichard Weinberger 	aeb->scrub = scrub;
89dbb7d2a8SRichard Weinberger 	aeb->copy_flag = aeb->sqnum = 0;
90dbb7d2a8SRichard Weinberger 
91dbb7d2a8SRichard Weinberger 	ai->ec_sum += aeb->ec;
92dbb7d2a8SRichard Weinberger 	ai->ec_count++;
93dbb7d2a8SRichard Weinberger 
94dbb7d2a8SRichard Weinberger 	if (ai->max_ec < aeb->ec)
95dbb7d2a8SRichard Weinberger 		ai->max_ec = aeb->ec;
96dbb7d2a8SRichard Weinberger 
97dbb7d2a8SRichard Weinberger 	if (ai->min_ec > aeb->ec)
98dbb7d2a8SRichard Weinberger 		ai->min_ec = aeb->ec;
99dbb7d2a8SRichard Weinberger 
100dbb7d2a8SRichard Weinberger 	list_add_tail(&aeb->u.list, list);
101dbb7d2a8SRichard Weinberger 
102dbb7d2a8SRichard Weinberger 	return 0;
103dbb7d2a8SRichard Weinberger }
104dbb7d2a8SRichard Weinberger 
105dbb7d2a8SRichard Weinberger /**
106dbb7d2a8SRichard Weinberger  * add_vol - create and add a new volume to ubi_attach_info.
107dbb7d2a8SRichard Weinberger  * @ai: ubi_attach_info object
108dbb7d2a8SRichard Weinberger  * @vol_id: VID of the new volume
109dbb7d2a8SRichard Weinberger  * @used_ebs: number of used EBS
110dbb7d2a8SRichard Weinberger  * @data_pad: data padding value of the new volume
111dbb7d2a8SRichard Weinberger  * @vol_type: volume type
112dbb7d2a8SRichard Weinberger  * @last_eb_bytes: number of bytes in the last LEB
113dbb7d2a8SRichard Weinberger  *
114dbb7d2a8SRichard Weinberger  * Returns the new struct ubi_ainf_volume on success.
115dbb7d2a8SRichard Weinberger  * NULL indicates an error.
116dbb7d2a8SRichard Weinberger  */
117dbb7d2a8SRichard Weinberger static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id,
118dbb7d2a8SRichard Weinberger 				       int used_ebs, int data_pad, u8 vol_type,
119dbb7d2a8SRichard Weinberger 				       int last_eb_bytes)
120dbb7d2a8SRichard Weinberger {
121dbb7d2a8SRichard Weinberger 	struct ubi_ainf_volume *av;
122dbb7d2a8SRichard Weinberger 	struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
123dbb7d2a8SRichard Weinberger 
124dbb7d2a8SRichard Weinberger 	while (*p) {
125dbb7d2a8SRichard Weinberger 		parent = *p;
126dbb7d2a8SRichard Weinberger 		av = rb_entry(parent, struct ubi_ainf_volume, rb);
127dbb7d2a8SRichard Weinberger 
128e9110361SHeiko Schocher 		if (vol_id > av->vol_id)
129dbb7d2a8SRichard Weinberger 			p = &(*p)->rb_left;
130604b592eSMike Snitzer 		else
131dbb7d2a8SRichard Weinberger 			p = &(*p)->rb_right;
132dbb7d2a8SRichard Weinberger 	}
133dbb7d2a8SRichard Weinberger 
134dbb7d2a8SRichard Weinberger 	av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL);
135dbb7d2a8SRichard Weinberger 	if (!av)
136dbb7d2a8SRichard Weinberger 		goto out;
137dbb7d2a8SRichard Weinberger 
138dbb7d2a8SRichard Weinberger 	av->highest_lnum = av->leb_count = 0;
139dbb7d2a8SRichard Weinberger 	av->vol_id = vol_id;
140dbb7d2a8SRichard Weinberger 	av->used_ebs = used_ebs;
141dbb7d2a8SRichard Weinberger 	av->data_pad = data_pad;
142dbb7d2a8SRichard Weinberger 	av->last_data_size = last_eb_bytes;
143dbb7d2a8SRichard Weinberger 	av->compat = 0;
144dbb7d2a8SRichard Weinberger 	av->vol_type = vol_type;
145dbb7d2a8SRichard Weinberger 	av->root = RB_ROOT;
146dbb7d2a8SRichard Weinberger 
147dbb7d2a8SRichard Weinberger 	dbg_bld("found volume (ID %i)", vol_id);
148dbb7d2a8SRichard Weinberger 
149dbb7d2a8SRichard Weinberger 	rb_link_node(&av->rb, parent, p);
150dbb7d2a8SRichard Weinberger 	rb_insert_color(&av->rb, &ai->volumes);
151dbb7d2a8SRichard Weinberger 
152dbb7d2a8SRichard Weinberger out:
153dbb7d2a8SRichard Weinberger 	return av;
154dbb7d2a8SRichard Weinberger }
155dbb7d2a8SRichard Weinberger 
156dbb7d2a8SRichard Weinberger /**
157dbb7d2a8SRichard Weinberger  * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
158dbb7d2a8SRichard Weinberger  * from it's original list.
159dbb7d2a8SRichard Weinberger  * @ai: ubi_attach_info object
160dbb7d2a8SRichard Weinberger  * @aeb: the to be assigned SEB
161dbb7d2a8SRichard Weinberger  * @av: target scan volume
162dbb7d2a8SRichard Weinberger  */
163dbb7d2a8SRichard Weinberger static void assign_aeb_to_av(struct ubi_attach_info *ai,
164dbb7d2a8SRichard Weinberger 			     struct ubi_ainf_peb *aeb,
165dbb7d2a8SRichard Weinberger 			     struct ubi_ainf_volume *av)
166dbb7d2a8SRichard Weinberger {
167dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *tmp_aeb;
168dbb7d2a8SRichard Weinberger 	struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
169dbb7d2a8SRichard Weinberger 
170dbb7d2a8SRichard Weinberger 	p = &av->root.rb_node;
171dbb7d2a8SRichard Weinberger 	while (*p) {
172dbb7d2a8SRichard Weinberger 		parent = *p;
173dbb7d2a8SRichard Weinberger 
174dbb7d2a8SRichard Weinberger 		tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
175dbb7d2a8SRichard Weinberger 		if (aeb->lnum != tmp_aeb->lnum) {
176dbb7d2a8SRichard Weinberger 			if (aeb->lnum < tmp_aeb->lnum)
177dbb7d2a8SRichard Weinberger 				p = &(*p)->rb_left;
178dbb7d2a8SRichard Weinberger 			else
179dbb7d2a8SRichard Weinberger 				p = &(*p)->rb_right;
180dbb7d2a8SRichard Weinberger 
181dbb7d2a8SRichard Weinberger 			continue;
182dbb7d2a8SRichard Weinberger 		} else
183dbb7d2a8SRichard Weinberger 			break;
184dbb7d2a8SRichard Weinberger 	}
185dbb7d2a8SRichard Weinberger 
186dbb7d2a8SRichard Weinberger 	list_del(&aeb->u.list);
187dbb7d2a8SRichard Weinberger 	av->leb_count++;
188dbb7d2a8SRichard Weinberger 
189dbb7d2a8SRichard Weinberger 	rb_link_node(&aeb->u.rb, parent, p);
190dbb7d2a8SRichard Weinberger 	rb_insert_color(&aeb->u.rb, &av->root);
191dbb7d2a8SRichard Weinberger }
192dbb7d2a8SRichard Weinberger 
193dbb7d2a8SRichard Weinberger /**
194dbb7d2a8SRichard Weinberger  * update_vol - inserts or updates a LEB which was found a pool.
195dbb7d2a8SRichard Weinberger  * @ubi: the UBI device object
196dbb7d2a8SRichard Weinberger  * @ai: attach info object
197dbb7d2a8SRichard Weinberger  * @av: the volume this LEB belongs to
198dbb7d2a8SRichard Weinberger  * @new_vh: the volume header derived from new_aeb
199dbb7d2a8SRichard Weinberger  * @new_aeb: the AEB to be examined
200dbb7d2a8SRichard Weinberger  *
201dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
202dbb7d2a8SRichard Weinberger  */
203dbb7d2a8SRichard Weinberger static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
204dbb7d2a8SRichard Weinberger 		      struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh,
205dbb7d2a8SRichard Weinberger 		      struct ubi_ainf_peb *new_aeb)
206dbb7d2a8SRichard Weinberger {
207dbb7d2a8SRichard Weinberger 	struct rb_node **p = &av->root.rb_node, *parent = NULL;
208dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb, *victim;
209dbb7d2a8SRichard Weinberger 	int cmp_res;
210dbb7d2a8SRichard Weinberger 
211dbb7d2a8SRichard Weinberger 	while (*p) {
212dbb7d2a8SRichard Weinberger 		parent = *p;
213dbb7d2a8SRichard Weinberger 		aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
214dbb7d2a8SRichard Weinberger 
215dbb7d2a8SRichard Weinberger 		if (be32_to_cpu(new_vh->lnum) != aeb->lnum) {
216dbb7d2a8SRichard Weinberger 			if (be32_to_cpu(new_vh->lnum) < aeb->lnum)
217dbb7d2a8SRichard Weinberger 				p = &(*p)->rb_left;
218dbb7d2a8SRichard Weinberger 			else
219dbb7d2a8SRichard Weinberger 				p = &(*p)->rb_right;
220dbb7d2a8SRichard Weinberger 
221dbb7d2a8SRichard Weinberger 			continue;
222dbb7d2a8SRichard Weinberger 		}
223dbb7d2a8SRichard Weinberger 
224dbb7d2a8SRichard Weinberger 		/* This case can happen if the fastmap gets written
225dbb7d2a8SRichard Weinberger 		 * because of a volume change (creation, deletion, ..).
226dbb7d2a8SRichard Weinberger 		 * Then a PEB can be within the persistent EBA and the pool.
227dbb7d2a8SRichard Weinberger 		 */
228dbb7d2a8SRichard Weinberger 		if (aeb->pnum == new_aeb->pnum) {
229dbb7d2a8SRichard Weinberger 			ubi_assert(aeb->lnum == new_aeb->lnum);
230dbb7d2a8SRichard Weinberger 			kmem_cache_free(ai->aeb_slab_cache, new_aeb);
231dbb7d2a8SRichard Weinberger 
232dbb7d2a8SRichard Weinberger 			return 0;
233dbb7d2a8SRichard Weinberger 		}
234dbb7d2a8SRichard Weinberger 
235dbb7d2a8SRichard Weinberger 		cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh);
236dbb7d2a8SRichard Weinberger 		if (cmp_res < 0)
237dbb7d2a8SRichard Weinberger 			return cmp_res;
238dbb7d2a8SRichard Weinberger 
239dbb7d2a8SRichard Weinberger 		/* new_aeb is newer */
240dbb7d2a8SRichard Weinberger 		if (cmp_res & 1) {
241dbb7d2a8SRichard Weinberger 			victim = kmem_cache_alloc(ai->aeb_slab_cache,
242dbb7d2a8SRichard Weinberger 				GFP_KERNEL);
243dbb7d2a8SRichard Weinberger 			if (!victim)
244dbb7d2a8SRichard Weinberger 				return -ENOMEM;
245dbb7d2a8SRichard Weinberger 
246dbb7d2a8SRichard Weinberger 			victim->ec = aeb->ec;
247dbb7d2a8SRichard Weinberger 			victim->pnum = aeb->pnum;
248dbb7d2a8SRichard Weinberger 			list_add_tail(&victim->u.list, &ai->erase);
249dbb7d2a8SRichard Weinberger 
250dbb7d2a8SRichard Weinberger 			if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
251dbb7d2a8SRichard Weinberger 				av->last_data_size = \
252dbb7d2a8SRichard Weinberger 					be32_to_cpu(new_vh->data_size);
253dbb7d2a8SRichard Weinberger 
254dbb7d2a8SRichard Weinberger 			dbg_bld("vol %i: AEB %i's PEB %i is the newer",
255dbb7d2a8SRichard Weinberger 				av->vol_id, aeb->lnum, new_aeb->pnum);
256dbb7d2a8SRichard Weinberger 
257dbb7d2a8SRichard Weinberger 			aeb->ec = new_aeb->ec;
258dbb7d2a8SRichard Weinberger 			aeb->pnum = new_aeb->pnum;
259dbb7d2a8SRichard Weinberger 			aeb->copy_flag = new_vh->copy_flag;
260dbb7d2a8SRichard Weinberger 			aeb->scrub = new_aeb->scrub;
261dbb7d2a8SRichard Weinberger 			kmem_cache_free(ai->aeb_slab_cache, new_aeb);
262dbb7d2a8SRichard Weinberger 
263dbb7d2a8SRichard Weinberger 		/* new_aeb is older */
264dbb7d2a8SRichard Weinberger 		} else {
265dbb7d2a8SRichard Weinberger 			dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
266dbb7d2a8SRichard Weinberger 				av->vol_id, aeb->lnum, new_aeb->pnum);
267dbb7d2a8SRichard Weinberger 			list_add_tail(&new_aeb->u.list, &ai->erase);
268dbb7d2a8SRichard Weinberger 		}
269dbb7d2a8SRichard Weinberger 
270dbb7d2a8SRichard Weinberger 		return 0;
271dbb7d2a8SRichard Weinberger 	}
272dbb7d2a8SRichard Weinberger 	/* This LEB is new, let's add it to the volume */
273dbb7d2a8SRichard Weinberger 
274dbb7d2a8SRichard Weinberger 	if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
275dbb7d2a8SRichard Weinberger 		av->highest_lnum = be32_to_cpu(new_vh->lnum);
276dbb7d2a8SRichard Weinberger 		av->last_data_size = be32_to_cpu(new_vh->data_size);
277dbb7d2a8SRichard Weinberger 	}
278dbb7d2a8SRichard Weinberger 
279dbb7d2a8SRichard Weinberger 	if (av->vol_type == UBI_STATIC_VOLUME)
280dbb7d2a8SRichard Weinberger 		av->used_ebs = be32_to_cpu(new_vh->used_ebs);
281dbb7d2a8SRichard Weinberger 
282dbb7d2a8SRichard Weinberger 	av->leb_count++;
283dbb7d2a8SRichard Weinberger 
284dbb7d2a8SRichard Weinberger 	rb_link_node(&new_aeb->u.rb, parent, p);
285dbb7d2a8SRichard Weinberger 	rb_insert_color(&new_aeb->u.rb, &av->root);
286dbb7d2a8SRichard Weinberger 
287dbb7d2a8SRichard Weinberger 	return 0;
288dbb7d2a8SRichard Weinberger }
289dbb7d2a8SRichard Weinberger 
290dbb7d2a8SRichard Weinberger /**
291dbb7d2a8SRichard Weinberger  * process_pool_aeb - we found a non-empty PEB in a pool.
292dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
293dbb7d2a8SRichard Weinberger  * @ai: attach info object
294dbb7d2a8SRichard Weinberger  * @new_vh: the volume header derived from new_aeb
295dbb7d2a8SRichard Weinberger  * @new_aeb: the AEB to be examined
296dbb7d2a8SRichard Weinberger  *
297dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
298dbb7d2a8SRichard Weinberger  */
299dbb7d2a8SRichard Weinberger static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
300dbb7d2a8SRichard Weinberger 			    struct ubi_vid_hdr *new_vh,
301dbb7d2a8SRichard Weinberger 			    struct ubi_ainf_peb *new_aeb)
302dbb7d2a8SRichard Weinberger {
303dbb7d2a8SRichard Weinberger 	struct ubi_ainf_volume *av, *tmp_av = NULL;
304dbb7d2a8SRichard Weinberger 	struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
305dbb7d2a8SRichard Weinberger 	int found = 0;
306dbb7d2a8SRichard Weinberger 
307dbb7d2a8SRichard Weinberger 	if (be32_to_cpu(new_vh->vol_id) == UBI_FM_SB_VOLUME_ID ||
308dbb7d2a8SRichard Weinberger 		be32_to_cpu(new_vh->vol_id) == UBI_FM_DATA_VOLUME_ID) {
309dbb7d2a8SRichard Weinberger 		kmem_cache_free(ai->aeb_slab_cache, new_aeb);
310dbb7d2a8SRichard Weinberger 
311dbb7d2a8SRichard Weinberger 		return 0;
312dbb7d2a8SRichard Weinberger 	}
313dbb7d2a8SRichard Weinberger 
314dbb7d2a8SRichard Weinberger 	/* Find the volume this SEB belongs to */
315dbb7d2a8SRichard Weinberger 	while (*p) {
316dbb7d2a8SRichard Weinberger 		parent = *p;
317dbb7d2a8SRichard Weinberger 		tmp_av = rb_entry(parent, struct ubi_ainf_volume, rb);
318dbb7d2a8SRichard Weinberger 
319dbb7d2a8SRichard Weinberger 		if (be32_to_cpu(new_vh->vol_id) > tmp_av->vol_id)
320dbb7d2a8SRichard Weinberger 			p = &(*p)->rb_left;
321dbb7d2a8SRichard Weinberger 		else if (be32_to_cpu(new_vh->vol_id) < tmp_av->vol_id)
322dbb7d2a8SRichard Weinberger 			p = &(*p)->rb_right;
323dbb7d2a8SRichard Weinberger 		else {
324dbb7d2a8SRichard Weinberger 			found = 1;
325dbb7d2a8SRichard Weinberger 			break;
326dbb7d2a8SRichard Weinberger 		}
327dbb7d2a8SRichard Weinberger 	}
328dbb7d2a8SRichard Weinberger 
329dbb7d2a8SRichard Weinberger 	if (found)
330dbb7d2a8SRichard Weinberger 		av = tmp_av;
331dbb7d2a8SRichard Weinberger 	else {
332dbb7d2a8SRichard Weinberger 		ubi_err("orphaned volume in fastmap pool!");
333dbb7d2a8SRichard Weinberger 		return UBI_BAD_FASTMAP;
334dbb7d2a8SRichard Weinberger 	}
335dbb7d2a8SRichard Weinberger 
336dbb7d2a8SRichard Weinberger 	ubi_assert(be32_to_cpu(new_vh->vol_id) == av->vol_id);
337dbb7d2a8SRichard Weinberger 
338dbb7d2a8SRichard Weinberger 	return update_vol(ubi, ai, av, new_vh, new_aeb);
339dbb7d2a8SRichard Weinberger }
340dbb7d2a8SRichard Weinberger 
341dbb7d2a8SRichard Weinberger /**
342dbb7d2a8SRichard Weinberger  * unmap_peb - unmap a PEB.
343dbb7d2a8SRichard Weinberger  * If fastmap detects a free PEB in the pool it has to check whether
344dbb7d2a8SRichard Weinberger  * this PEB has been unmapped after writing the fastmap.
345dbb7d2a8SRichard Weinberger  *
346dbb7d2a8SRichard Weinberger  * @ai: UBI attach info object
347dbb7d2a8SRichard Weinberger  * @pnum: The PEB to be unmapped
348dbb7d2a8SRichard Weinberger  */
349dbb7d2a8SRichard Weinberger static void unmap_peb(struct ubi_attach_info *ai, int pnum)
350dbb7d2a8SRichard Weinberger {
351dbb7d2a8SRichard Weinberger 	struct ubi_ainf_volume *av;
352dbb7d2a8SRichard Weinberger 	struct rb_node *node, *node2;
353dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb;
354dbb7d2a8SRichard Weinberger 
355dbb7d2a8SRichard Weinberger 	for (node = rb_first(&ai->volumes); node; node = rb_next(node)) {
356dbb7d2a8SRichard Weinberger 		av = rb_entry(node, struct ubi_ainf_volume, rb);
357dbb7d2a8SRichard Weinberger 
358dbb7d2a8SRichard Weinberger 		for (node2 = rb_first(&av->root); node2;
359dbb7d2a8SRichard Weinberger 		     node2 = rb_next(node2)) {
360dbb7d2a8SRichard Weinberger 			aeb = rb_entry(node2, struct ubi_ainf_peb, u.rb);
361dbb7d2a8SRichard Weinberger 			if (aeb->pnum == pnum) {
362dbb7d2a8SRichard Weinberger 				rb_erase(&aeb->u.rb, &av->root);
363dbb7d2a8SRichard Weinberger 				kmem_cache_free(ai->aeb_slab_cache, aeb);
364dbb7d2a8SRichard Weinberger 				return;
365dbb7d2a8SRichard Weinberger 			}
366dbb7d2a8SRichard Weinberger 		}
367dbb7d2a8SRichard Weinberger 	}
368dbb7d2a8SRichard Weinberger }
369dbb7d2a8SRichard Weinberger 
370dbb7d2a8SRichard Weinberger /**
371dbb7d2a8SRichard Weinberger  * scan_pool - scans a pool for changed (no longer empty PEBs).
372dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
373dbb7d2a8SRichard Weinberger  * @ai: attach info object
374dbb7d2a8SRichard Weinberger  * @pebs: an array of all PEB numbers in the to be scanned pool
375dbb7d2a8SRichard Weinberger  * @pool_size: size of the pool (number of entries in @pebs)
376dbb7d2a8SRichard Weinberger  * @max_sqnum: pointer to the maximal sequence number
377dbb7d2a8SRichard Weinberger  * @eba_orphans: list of PEBs which need to be scanned
378dbb7d2a8SRichard Weinberger  * @free: list of PEBs which are most likely free (and go into @ai->free)
379dbb7d2a8SRichard Weinberger  *
380dbb7d2a8SRichard Weinberger  * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
381dbb7d2a8SRichard Weinberger  * < 0 indicates an internal error.
382dbb7d2a8SRichard Weinberger  */
383dbb7d2a8SRichard Weinberger static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
384dbb7d2a8SRichard Weinberger 		     int *pebs, int pool_size, unsigned long long *max_sqnum,
385dbb7d2a8SRichard Weinberger 		     struct list_head *eba_orphans, struct list_head *free)
386dbb7d2a8SRichard Weinberger {
387dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *vh;
388dbb7d2a8SRichard Weinberger 	struct ubi_ec_hdr *ech;
389dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *new_aeb, *tmp_aeb;
390dbb7d2a8SRichard Weinberger 	int i, pnum, err, found_orphan, ret = 0;
391dbb7d2a8SRichard Weinberger 
392dbb7d2a8SRichard Weinberger 	ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
393dbb7d2a8SRichard Weinberger 	if (!ech)
394dbb7d2a8SRichard Weinberger 		return -ENOMEM;
395dbb7d2a8SRichard Weinberger 
396dbb7d2a8SRichard Weinberger 	vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
397dbb7d2a8SRichard Weinberger 	if (!vh) {
398dbb7d2a8SRichard Weinberger 		kfree(ech);
399dbb7d2a8SRichard Weinberger 		return -ENOMEM;
400dbb7d2a8SRichard Weinberger 	}
401dbb7d2a8SRichard Weinberger 
402dbb7d2a8SRichard Weinberger 	dbg_bld("scanning fastmap pool: size = %i", pool_size);
403dbb7d2a8SRichard Weinberger 
404dbb7d2a8SRichard Weinberger 	/*
405dbb7d2a8SRichard Weinberger 	 * Now scan all PEBs in the pool to find changes which have been made
406dbb7d2a8SRichard Weinberger 	 * after the creation of the fastmap
407dbb7d2a8SRichard Weinberger 	 */
408dbb7d2a8SRichard Weinberger 	for (i = 0; i < pool_size; i++) {
409dbb7d2a8SRichard Weinberger 		int scrub = 0;
410c22301adSRichard Genoud 		int image_seq;
411dbb7d2a8SRichard Weinberger 
412dbb7d2a8SRichard Weinberger 		pnum = be32_to_cpu(pebs[i]);
413dbb7d2a8SRichard Weinberger 
414dbb7d2a8SRichard Weinberger 		if (ubi_io_is_bad(ubi, pnum)) {
415dbb7d2a8SRichard Weinberger 			ubi_err("bad PEB in fastmap pool!");
416dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
417dbb7d2a8SRichard Weinberger 			goto out;
418dbb7d2a8SRichard Weinberger 		}
419dbb7d2a8SRichard Weinberger 
420dbb7d2a8SRichard Weinberger 		err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
421dbb7d2a8SRichard Weinberger 		if (err && err != UBI_IO_BITFLIPS) {
422dbb7d2a8SRichard Weinberger 			ubi_err("unable to read EC header! PEB:%i err:%i",
423dbb7d2a8SRichard Weinberger 				pnum, err);
424dbb7d2a8SRichard Weinberger 			ret = err > 0 ? UBI_BAD_FASTMAP : err;
425dbb7d2a8SRichard Weinberger 			goto out;
426*44305ebdSBrian Norris 		} else if (err == UBI_IO_BITFLIPS)
427dbb7d2a8SRichard Weinberger 			scrub = 1;
428dbb7d2a8SRichard Weinberger 
429c22301adSRichard Genoud 		/*
430c22301adSRichard Genoud 		 * Older UBI implementations have image_seq set to zero, so
431c22301adSRichard Genoud 		 * we shouldn't fail if image_seq == 0.
432c22301adSRichard Genoud 		 */
433c22301adSRichard Genoud 		image_seq = be32_to_cpu(ech->image_seq);
434c22301adSRichard Genoud 
435c22301adSRichard Genoud 		if (image_seq && (image_seq != ubi->image_seq)) {
436dbb7d2a8SRichard Weinberger 			ubi_err("bad image seq: 0x%x, expected: 0x%x",
437dbb7d2a8SRichard Weinberger 				be32_to_cpu(ech->image_seq), ubi->image_seq);
438f240dca8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
439dbb7d2a8SRichard Weinberger 			goto out;
440dbb7d2a8SRichard Weinberger 		}
441dbb7d2a8SRichard Weinberger 
442dbb7d2a8SRichard Weinberger 		err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
443dbb7d2a8SRichard Weinberger 		if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
444dbb7d2a8SRichard Weinberger 			unsigned long long ec = be64_to_cpu(ech->ec);
445dbb7d2a8SRichard Weinberger 			unmap_peb(ai, pnum);
446dbb7d2a8SRichard Weinberger 			dbg_bld("Adding PEB to free: %i", pnum);
447dbb7d2a8SRichard Weinberger 			if (err == UBI_IO_FF_BITFLIPS)
448dbb7d2a8SRichard Weinberger 				add_aeb(ai, free, pnum, ec, 1);
449dbb7d2a8SRichard Weinberger 			else
450dbb7d2a8SRichard Weinberger 				add_aeb(ai, free, pnum, ec, 0);
451dbb7d2a8SRichard Weinberger 			continue;
452dbb7d2a8SRichard Weinberger 		} else if (err == 0 || err == UBI_IO_BITFLIPS) {
453dbb7d2a8SRichard Weinberger 			dbg_bld("Found non empty PEB:%i in pool", pnum);
454dbb7d2a8SRichard Weinberger 
455dbb7d2a8SRichard Weinberger 			if (err == UBI_IO_BITFLIPS)
456dbb7d2a8SRichard Weinberger 				scrub = 1;
457dbb7d2a8SRichard Weinberger 
458dbb7d2a8SRichard Weinberger 			found_orphan = 0;
459dbb7d2a8SRichard Weinberger 			list_for_each_entry(tmp_aeb, eba_orphans, u.list) {
460dbb7d2a8SRichard Weinberger 				if (tmp_aeb->pnum == pnum) {
461dbb7d2a8SRichard Weinberger 					found_orphan = 1;
462dbb7d2a8SRichard Weinberger 					break;
463dbb7d2a8SRichard Weinberger 				}
464dbb7d2a8SRichard Weinberger 			}
465dbb7d2a8SRichard Weinberger 			if (found_orphan) {
466dbb7d2a8SRichard Weinberger 				list_del(&tmp_aeb->u.list);
4675547fec7SDan Carpenter 				kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
468dbb7d2a8SRichard Weinberger 			}
469dbb7d2a8SRichard Weinberger 
470dbb7d2a8SRichard Weinberger 			new_aeb = kmem_cache_alloc(ai->aeb_slab_cache,
471dbb7d2a8SRichard Weinberger 						   GFP_KERNEL);
472dbb7d2a8SRichard Weinberger 			if (!new_aeb) {
473dbb7d2a8SRichard Weinberger 				ret = -ENOMEM;
474dbb7d2a8SRichard Weinberger 				goto out;
475dbb7d2a8SRichard Weinberger 			}
476dbb7d2a8SRichard Weinberger 
477dbb7d2a8SRichard Weinberger 			new_aeb->ec = be64_to_cpu(ech->ec);
478dbb7d2a8SRichard Weinberger 			new_aeb->pnum = pnum;
479dbb7d2a8SRichard Weinberger 			new_aeb->lnum = be32_to_cpu(vh->lnum);
480dbb7d2a8SRichard Weinberger 			new_aeb->sqnum = be64_to_cpu(vh->sqnum);
481dbb7d2a8SRichard Weinberger 			new_aeb->copy_flag = vh->copy_flag;
482dbb7d2a8SRichard Weinberger 			new_aeb->scrub = scrub;
483dbb7d2a8SRichard Weinberger 
484dbb7d2a8SRichard Weinberger 			if (*max_sqnum < new_aeb->sqnum)
485dbb7d2a8SRichard Weinberger 				*max_sqnum = new_aeb->sqnum;
486dbb7d2a8SRichard Weinberger 
487dbb7d2a8SRichard Weinberger 			err = process_pool_aeb(ubi, ai, vh, new_aeb);
488dbb7d2a8SRichard Weinberger 			if (err) {
489dbb7d2a8SRichard Weinberger 				ret = err > 0 ? UBI_BAD_FASTMAP : err;
490dbb7d2a8SRichard Weinberger 				goto out;
491dbb7d2a8SRichard Weinberger 			}
492dbb7d2a8SRichard Weinberger 		} else {
493dbb7d2a8SRichard Weinberger 			/* We are paranoid and fall back to scanning mode */
494dbb7d2a8SRichard Weinberger 			ubi_err("fastmap pool PEBs contains damaged PEBs!");
495dbb7d2a8SRichard Weinberger 			ret = err > 0 ? UBI_BAD_FASTMAP : err;
496dbb7d2a8SRichard Weinberger 			goto out;
497dbb7d2a8SRichard Weinberger 		}
498dbb7d2a8SRichard Weinberger 
499dbb7d2a8SRichard Weinberger 	}
500dbb7d2a8SRichard Weinberger 
501dbb7d2a8SRichard Weinberger out:
502dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, vh);
503dbb7d2a8SRichard Weinberger 	kfree(ech);
504dbb7d2a8SRichard Weinberger 	return ret;
505dbb7d2a8SRichard Weinberger }
506dbb7d2a8SRichard Weinberger 
507dbb7d2a8SRichard Weinberger /**
508dbb7d2a8SRichard Weinberger  * count_fastmap_pebs - Counts the PEBs found by fastmap.
509dbb7d2a8SRichard Weinberger  * @ai: The UBI attach info object
510dbb7d2a8SRichard Weinberger  */
511dbb7d2a8SRichard Weinberger static int count_fastmap_pebs(struct ubi_attach_info *ai)
512dbb7d2a8SRichard Weinberger {
513dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb;
514dbb7d2a8SRichard Weinberger 	struct ubi_ainf_volume *av;
515dbb7d2a8SRichard Weinberger 	struct rb_node *rb1, *rb2;
516dbb7d2a8SRichard Weinberger 	int n = 0;
517dbb7d2a8SRichard Weinberger 
518dbb7d2a8SRichard Weinberger 	list_for_each_entry(aeb, &ai->erase, u.list)
519dbb7d2a8SRichard Weinberger 		n++;
520dbb7d2a8SRichard Weinberger 
521dbb7d2a8SRichard Weinberger 	list_for_each_entry(aeb, &ai->free, u.list)
522dbb7d2a8SRichard Weinberger 		n++;
523dbb7d2a8SRichard Weinberger 
524dbb7d2a8SRichard Weinberger 	 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
525dbb7d2a8SRichard Weinberger 		ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
526dbb7d2a8SRichard Weinberger 			n++;
527dbb7d2a8SRichard Weinberger 
528dbb7d2a8SRichard Weinberger 	return n;
529dbb7d2a8SRichard Weinberger }
530dbb7d2a8SRichard Weinberger 
531dbb7d2a8SRichard Weinberger /**
532dbb7d2a8SRichard Weinberger  * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
533dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
534dbb7d2a8SRichard Weinberger  * @ai: UBI attach info object
535dbb7d2a8SRichard Weinberger  * @fm: the fastmap to be attached
536dbb7d2a8SRichard Weinberger  *
537dbb7d2a8SRichard Weinberger  * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
538dbb7d2a8SRichard Weinberger  * < 0 indicates an internal error.
539dbb7d2a8SRichard Weinberger  */
540dbb7d2a8SRichard Weinberger static int ubi_attach_fastmap(struct ubi_device *ubi,
541dbb7d2a8SRichard Weinberger 			      struct ubi_attach_info *ai,
542dbb7d2a8SRichard Weinberger 			      struct ubi_fastmap_layout *fm)
543dbb7d2a8SRichard Weinberger {
544dbb7d2a8SRichard Weinberger 	struct list_head used, eba_orphans, free;
545dbb7d2a8SRichard Weinberger 	struct ubi_ainf_volume *av;
546dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
547dbb7d2a8SRichard Weinberger 	struct ubi_ec_hdr *ech;
548dbb7d2a8SRichard Weinberger 	struct ubi_fm_sb *fmsb;
549dbb7d2a8SRichard Weinberger 	struct ubi_fm_hdr *fmhdr;
550dbb7d2a8SRichard Weinberger 	struct ubi_fm_scan_pool *fmpl1, *fmpl2;
551dbb7d2a8SRichard Weinberger 	struct ubi_fm_ec *fmec;
552dbb7d2a8SRichard Weinberger 	struct ubi_fm_volhdr *fmvhdr;
553dbb7d2a8SRichard Weinberger 	struct ubi_fm_eba *fm_eba;
554dbb7d2a8SRichard Weinberger 	int ret, i, j, pool_size, wl_pool_size;
555dbb7d2a8SRichard Weinberger 	size_t fm_pos = 0, fm_size = ubi->fm_size;
556dbb7d2a8SRichard Weinberger 	unsigned long long max_sqnum = 0;
557dbb7d2a8SRichard Weinberger 	void *fm_raw = ubi->fm_buf;
558dbb7d2a8SRichard Weinberger 
559dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&used);
560dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&free);
561dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&eba_orphans);
562dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&ai->corr);
563dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&ai->free);
564dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&ai->erase);
565dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&ai->alien);
566dbb7d2a8SRichard Weinberger 	ai->volumes = RB_ROOT;
567dbb7d2a8SRichard Weinberger 	ai->min_ec = UBI_MAX_ERASECOUNTER;
568dbb7d2a8SRichard Weinberger 
569dbb7d2a8SRichard Weinberger 	ai->aeb_slab_cache = kmem_cache_create("ubi_ainf_peb_slab",
570dbb7d2a8SRichard Weinberger 					       sizeof(struct ubi_ainf_peb),
571dbb7d2a8SRichard Weinberger 					       0, 0, NULL);
572dbb7d2a8SRichard Weinberger 	if (!ai->aeb_slab_cache) {
573dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
574dbb7d2a8SRichard Weinberger 		goto fail;
575dbb7d2a8SRichard Weinberger 	}
576dbb7d2a8SRichard Weinberger 
577dbb7d2a8SRichard Weinberger 	fmsb = (struct ubi_fm_sb *)(fm_raw);
578dbb7d2a8SRichard Weinberger 	ai->max_sqnum = fmsb->sqnum;
579dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(struct ubi_fm_sb);
580dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
581dbb7d2a8SRichard Weinberger 		goto fail_bad;
582dbb7d2a8SRichard Weinberger 
583dbb7d2a8SRichard Weinberger 	fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
584dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmhdr);
585dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
586dbb7d2a8SRichard Weinberger 		goto fail_bad;
587dbb7d2a8SRichard Weinberger 
588dbb7d2a8SRichard Weinberger 	if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
589dbb7d2a8SRichard Weinberger 		ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
590dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
591dbb7d2a8SRichard Weinberger 		goto fail_bad;
592dbb7d2a8SRichard Weinberger 	}
593dbb7d2a8SRichard Weinberger 
594dbb7d2a8SRichard Weinberger 	fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
595dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmpl1);
596dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
597dbb7d2a8SRichard Weinberger 		goto fail_bad;
598dbb7d2a8SRichard Weinberger 	if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
599dbb7d2a8SRichard Weinberger 		ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
600dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
601dbb7d2a8SRichard Weinberger 		goto fail_bad;
602dbb7d2a8SRichard Weinberger 	}
603dbb7d2a8SRichard Weinberger 
604dbb7d2a8SRichard Weinberger 	fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
605dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmpl2);
606dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
607dbb7d2a8SRichard Weinberger 		goto fail_bad;
608dbb7d2a8SRichard Weinberger 	if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
609dbb7d2a8SRichard Weinberger 		ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
610dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
611dbb7d2a8SRichard Weinberger 		goto fail_bad;
612dbb7d2a8SRichard Weinberger 	}
613dbb7d2a8SRichard Weinberger 
614dbb7d2a8SRichard Weinberger 	pool_size = be16_to_cpu(fmpl1->size);
615dbb7d2a8SRichard Weinberger 	wl_pool_size = be16_to_cpu(fmpl2->size);
616dbb7d2a8SRichard Weinberger 	fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
617dbb7d2a8SRichard Weinberger 	fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
618dbb7d2a8SRichard Weinberger 
619dbb7d2a8SRichard Weinberger 	if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
620dbb7d2a8SRichard Weinberger 		ubi_err("bad pool size: %i", pool_size);
621dbb7d2a8SRichard Weinberger 		goto fail_bad;
622dbb7d2a8SRichard Weinberger 	}
623dbb7d2a8SRichard Weinberger 
624dbb7d2a8SRichard Weinberger 	if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
625dbb7d2a8SRichard Weinberger 		ubi_err("bad WL pool size: %i", wl_pool_size);
626dbb7d2a8SRichard Weinberger 		goto fail_bad;
627dbb7d2a8SRichard Weinberger 	}
628dbb7d2a8SRichard Weinberger 
629dbb7d2a8SRichard Weinberger 
630dbb7d2a8SRichard Weinberger 	if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
631dbb7d2a8SRichard Weinberger 	    fm->max_pool_size < 0) {
632dbb7d2a8SRichard Weinberger 		ubi_err("bad maximal pool size: %i", fm->max_pool_size);
633dbb7d2a8SRichard Weinberger 		goto fail_bad;
634dbb7d2a8SRichard Weinberger 	}
635dbb7d2a8SRichard Weinberger 
636dbb7d2a8SRichard Weinberger 	if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
637dbb7d2a8SRichard Weinberger 	    fm->max_wl_pool_size < 0) {
638dbb7d2a8SRichard Weinberger 		ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
639dbb7d2a8SRichard Weinberger 		goto fail_bad;
640dbb7d2a8SRichard Weinberger 	}
641dbb7d2a8SRichard Weinberger 
642dbb7d2a8SRichard Weinberger 	/* read EC values from free list */
643dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
644dbb7d2a8SRichard Weinberger 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
645dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmec);
646dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
647dbb7d2a8SRichard Weinberger 			goto fail_bad;
648dbb7d2a8SRichard Weinberger 
649dbb7d2a8SRichard Weinberger 		add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
650dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmec->ec), 0);
651dbb7d2a8SRichard Weinberger 	}
652dbb7d2a8SRichard Weinberger 
653dbb7d2a8SRichard Weinberger 	/* read EC values from used list */
654dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
655dbb7d2a8SRichard Weinberger 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
656dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmec);
657dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
658dbb7d2a8SRichard Weinberger 			goto fail_bad;
659dbb7d2a8SRichard Weinberger 
660dbb7d2a8SRichard Weinberger 		add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
661dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmec->ec), 0);
662dbb7d2a8SRichard Weinberger 	}
663dbb7d2a8SRichard Weinberger 
664dbb7d2a8SRichard Weinberger 	/* read EC values from scrub list */
665dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
666dbb7d2a8SRichard Weinberger 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
667dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmec);
668dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
669dbb7d2a8SRichard Weinberger 			goto fail_bad;
670dbb7d2a8SRichard Weinberger 
671dbb7d2a8SRichard Weinberger 		add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
672dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmec->ec), 1);
673dbb7d2a8SRichard Weinberger 	}
674dbb7d2a8SRichard Weinberger 
675dbb7d2a8SRichard Weinberger 	/* read EC values from erase list */
676dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
677dbb7d2a8SRichard Weinberger 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
678dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmec);
679dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
680dbb7d2a8SRichard Weinberger 			goto fail_bad;
681dbb7d2a8SRichard Weinberger 
682dbb7d2a8SRichard Weinberger 		add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
683dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmec->ec), 1);
684dbb7d2a8SRichard Weinberger 	}
685dbb7d2a8SRichard Weinberger 
686dbb7d2a8SRichard Weinberger 	ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
687dbb7d2a8SRichard Weinberger 	ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
688dbb7d2a8SRichard Weinberger 
689dbb7d2a8SRichard Weinberger 	/* Iterate over all volumes and read their EBA table */
690dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
691dbb7d2a8SRichard Weinberger 		fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
692dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmvhdr);
693dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
694dbb7d2a8SRichard Weinberger 			goto fail_bad;
695dbb7d2a8SRichard Weinberger 
696dbb7d2a8SRichard Weinberger 		if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
697dbb7d2a8SRichard Weinberger 			ubi_err("bad fastmap vol header magic: 0x%x, " \
698dbb7d2a8SRichard Weinberger 				"expected: 0x%x",
699dbb7d2a8SRichard Weinberger 				be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
700dbb7d2a8SRichard Weinberger 			goto fail_bad;
701dbb7d2a8SRichard Weinberger 		}
702dbb7d2a8SRichard Weinberger 
703dbb7d2a8SRichard Weinberger 		av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
704dbb7d2a8SRichard Weinberger 			     be32_to_cpu(fmvhdr->used_ebs),
705dbb7d2a8SRichard Weinberger 			     be32_to_cpu(fmvhdr->data_pad),
706dbb7d2a8SRichard Weinberger 			     fmvhdr->vol_type,
707dbb7d2a8SRichard Weinberger 			     be32_to_cpu(fmvhdr->last_eb_bytes));
708dbb7d2a8SRichard Weinberger 
709dbb7d2a8SRichard Weinberger 		if (!av)
710dbb7d2a8SRichard Weinberger 			goto fail_bad;
711dbb7d2a8SRichard Weinberger 
712dbb7d2a8SRichard Weinberger 		ai->vols_found++;
713dbb7d2a8SRichard Weinberger 		if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
714dbb7d2a8SRichard Weinberger 			ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
715dbb7d2a8SRichard Weinberger 
716dbb7d2a8SRichard Weinberger 		fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
717dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fm_eba);
718dbb7d2a8SRichard Weinberger 		fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
719dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
720dbb7d2a8SRichard Weinberger 			goto fail_bad;
721dbb7d2a8SRichard Weinberger 
722dbb7d2a8SRichard Weinberger 		if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
723dbb7d2a8SRichard Weinberger 			ubi_err("bad fastmap EBA header magic: 0x%x, " \
724dbb7d2a8SRichard Weinberger 				"expected: 0x%x",
725dbb7d2a8SRichard Weinberger 				be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
726dbb7d2a8SRichard Weinberger 			goto fail_bad;
727dbb7d2a8SRichard Weinberger 		}
728dbb7d2a8SRichard Weinberger 
729dbb7d2a8SRichard Weinberger 		for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
730dbb7d2a8SRichard Weinberger 			int pnum = be32_to_cpu(fm_eba->pnum[j]);
731dbb7d2a8SRichard Weinberger 
732dbb7d2a8SRichard Weinberger 			if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
733dbb7d2a8SRichard Weinberger 				continue;
734dbb7d2a8SRichard Weinberger 
735dbb7d2a8SRichard Weinberger 			aeb = NULL;
736dbb7d2a8SRichard Weinberger 			list_for_each_entry(tmp_aeb, &used, u.list) {
737584d4623SBrian Pomerantz 				if (tmp_aeb->pnum == pnum) {
738dbb7d2a8SRichard Weinberger 					aeb = tmp_aeb;
739584d4623SBrian Pomerantz 					break;
740584d4623SBrian Pomerantz 				}
741dbb7d2a8SRichard Weinberger 			}
742dbb7d2a8SRichard Weinberger 
743dbb7d2a8SRichard Weinberger 			/* This can happen if a PEB is already in an EBA known
744dbb7d2a8SRichard Weinberger 			 * by this fastmap but the PEB itself is not in the used
745dbb7d2a8SRichard Weinberger 			 * list.
746dbb7d2a8SRichard Weinberger 			 * In this case the PEB can be within the fastmap pool
747dbb7d2a8SRichard Weinberger 			 * or while writing the fastmap it was in the protection
748dbb7d2a8SRichard Weinberger 			 * queue.
749dbb7d2a8SRichard Weinberger 			 */
750dbb7d2a8SRichard Weinberger 			if (!aeb) {
751dbb7d2a8SRichard Weinberger 				aeb = kmem_cache_alloc(ai->aeb_slab_cache,
752dbb7d2a8SRichard Weinberger 						       GFP_KERNEL);
753dbb7d2a8SRichard Weinberger 				if (!aeb) {
754dbb7d2a8SRichard Weinberger 					ret = -ENOMEM;
755dbb7d2a8SRichard Weinberger 
756dbb7d2a8SRichard Weinberger 					goto fail;
757dbb7d2a8SRichard Weinberger 				}
758dbb7d2a8SRichard Weinberger 
759dbb7d2a8SRichard Weinberger 				aeb->lnum = j;
760dbb7d2a8SRichard Weinberger 				aeb->pnum = be32_to_cpu(fm_eba->pnum[j]);
761dbb7d2a8SRichard Weinberger 				aeb->ec = -1;
762dbb7d2a8SRichard Weinberger 				aeb->scrub = aeb->copy_flag = aeb->sqnum = 0;
763dbb7d2a8SRichard Weinberger 				list_add_tail(&aeb->u.list, &eba_orphans);
764dbb7d2a8SRichard Weinberger 				continue;
765dbb7d2a8SRichard Weinberger 			}
766dbb7d2a8SRichard Weinberger 
767dbb7d2a8SRichard Weinberger 			aeb->lnum = j;
768dbb7d2a8SRichard Weinberger 
769dbb7d2a8SRichard Weinberger 			if (av->highest_lnum <= aeb->lnum)
770dbb7d2a8SRichard Weinberger 				av->highest_lnum = aeb->lnum;
771dbb7d2a8SRichard Weinberger 
772dbb7d2a8SRichard Weinberger 			assign_aeb_to_av(ai, aeb, av);
773dbb7d2a8SRichard Weinberger 
774dbb7d2a8SRichard Weinberger 			dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
775dbb7d2a8SRichard Weinberger 				aeb->pnum, aeb->lnum, av->vol_id);
776dbb7d2a8SRichard Weinberger 		}
777dbb7d2a8SRichard Weinberger 
778dbb7d2a8SRichard Weinberger 		ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
779dbb7d2a8SRichard Weinberger 		if (!ech) {
780dbb7d2a8SRichard Weinberger 			ret = -ENOMEM;
781dbb7d2a8SRichard Weinberger 			goto fail;
782dbb7d2a8SRichard Weinberger 		}
783dbb7d2a8SRichard Weinberger 
784dbb7d2a8SRichard Weinberger 		list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &eba_orphans,
785dbb7d2a8SRichard Weinberger 					 u.list) {
786dbb7d2a8SRichard Weinberger 			int err;
787dbb7d2a8SRichard Weinberger 
788dbb7d2a8SRichard Weinberger 			if (ubi_io_is_bad(ubi, tmp_aeb->pnum)) {
789dbb7d2a8SRichard Weinberger 				ubi_err("bad PEB in fastmap EBA orphan list");
790dbb7d2a8SRichard Weinberger 				ret = UBI_BAD_FASTMAP;
791dbb7d2a8SRichard Weinberger 				kfree(ech);
792dbb7d2a8SRichard Weinberger 				goto fail;
793dbb7d2a8SRichard Weinberger 			}
794dbb7d2a8SRichard Weinberger 
795dbb7d2a8SRichard Weinberger 			err = ubi_io_read_ec_hdr(ubi, tmp_aeb->pnum, ech, 0);
796dbb7d2a8SRichard Weinberger 			if (err && err != UBI_IO_BITFLIPS) {
797dbb7d2a8SRichard Weinberger 				ubi_err("unable to read EC header! PEB:%i " \
798dbb7d2a8SRichard Weinberger 					"err:%i", tmp_aeb->pnum, err);
799dbb7d2a8SRichard Weinberger 				ret = err > 0 ? UBI_BAD_FASTMAP : err;
800dbb7d2a8SRichard Weinberger 				kfree(ech);
801dbb7d2a8SRichard Weinberger 
802dbb7d2a8SRichard Weinberger 				goto fail;
803dbb7d2a8SRichard Weinberger 			} else if (err == UBI_IO_BITFLIPS)
804dbb7d2a8SRichard Weinberger 				tmp_aeb->scrub = 1;
805dbb7d2a8SRichard Weinberger 
806dbb7d2a8SRichard Weinberger 			tmp_aeb->ec = be64_to_cpu(ech->ec);
807dbb7d2a8SRichard Weinberger 			assign_aeb_to_av(ai, tmp_aeb, av);
808dbb7d2a8SRichard Weinberger 		}
809dbb7d2a8SRichard Weinberger 
810dbb7d2a8SRichard Weinberger 		kfree(ech);
811dbb7d2a8SRichard Weinberger 	}
812dbb7d2a8SRichard Weinberger 
813dbb7d2a8SRichard Weinberger 	ret = scan_pool(ubi, ai, fmpl1->pebs, pool_size, &max_sqnum,
814dbb7d2a8SRichard Weinberger 			&eba_orphans, &free);
815dbb7d2a8SRichard Weinberger 	if (ret)
816dbb7d2a8SRichard Weinberger 		goto fail;
817dbb7d2a8SRichard Weinberger 
818dbb7d2a8SRichard Weinberger 	ret = scan_pool(ubi, ai, fmpl2->pebs, wl_pool_size, &max_sqnum,
819dbb7d2a8SRichard Weinberger 			&eba_orphans, &free);
820dbb7d2a8SRichard Weinberger 	if (ret)
821dbb7d2a8SRichard Weinberger 		goto fail;
822dbb7d2a8SRichard Weinberger 
823dbb7d2a8SRichard Weinberger 	if (max_sqnum > ai->max_sqnum)
824dbb7d2a8SRichard Weinberger 		ai->max_sqnum = max_sqnum;
825dbb7d2a8SRichard Weinberger 
8266a059abdSWei Yongjun 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
8276a059abdSWei Yongjun 		list_move_tail(&tmp_aeb->u.list, &ai->free);
828dbb7d2a8SRichard Weinberger 
829ae0d1469SRichard Weinberger 	ubi_assert(list_empty(&used));
830ae0d1469SRichard Weinberger 	ubi_assert(list_empty(&eba_orphans));
831ae0d1469SRichard Weinberger 	ubi_assert(list_empty(&free));
832ae0d1469SRichard Weinberger 
833dbb7d2a8SRichard Weinberger 	/*
834dbb7d2a8SRichard Weinberger 	 * If fastmap is leaking PEBs (must not happen), raise a
835dbb7d2a8SRichard Weinberger 	 * fat warning and fall back to scanning mode.
836dbb7d2a8SRichard Weinberger 	 * We do this here because in ubi_wl_init() it's too late
837dbb7d2a8SRichard Weinberger 	 * and we cannot fall back to scanning.
838dbb7d2a8SRichard Weinberger 	 */
839dbb7d2a8SRichard Weinberger 	if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
840dbb7d2a8SRichard Weinberger 		    ai->bad_peb_count - fm->used_blocks))
841dbb7d2a8SRichard Weinberger 		goto fail_bad;
842dbb7d2a8SRichard Weinberger 
843dbb7d2a8SRichard Weinberger 	return 0;
844dbb7d2a8SRichard Weinberger 
845dbb7d2a8SRichard Weinberger fail_bad:
846dbb7d2a8SRichard Weinberger 	ret = UBI_BAD_FASTMAP;
847dbb7d2a8SRichard Weinberger fail:
848fe24c6e5SRichard Weinberger 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
849fe24c6e5SRichard Weinberger 		list_del(&tmp_aeb->u.list);
8505547fec7SDan Carpenter 		kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
851fe24c6e5SRichard Weinberger 	}
852fe24c6e5SRichard Weinberger 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &eba_orphans, u.list) {
853fe24c6e5SRichard Weinberger 		list_del(&tmp_aeb->u.list);
8545547fec7SDan Carpenter 		kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
855fe24c6e5SRichard Weinberger 	}
856fe24c6e5SRichard Weinberger 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
857fe24c6e5SRichard Weinberger 		list_del(&tmp_aeb->u.list);
8585547fec7SDan Carpenter 		kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
859fe24c6e5SRichard Weinberger 	}
860fe24c6e5SRichard Weinberger 
861dbb7d2a8SRichard Weinberger 	return ret;
862dbb7d2a8SRichard Weinberger }
863dbb7d2a8SRichard Weinberger 
864dbb7d2a8SRichard Weinberger /**
865dbb7d2a8SRichard Weinberger  * ubi_scan_fastmap - scan the fastmap.
866dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
867dbb7d2a8SRichard Weinberger  * @ai: UBI attach info to be filled
868dbb7d2a8SRichard Weinberger  * @fm_anchor: The fastmap starts at this PEB
869dbb7d2a8SRichard Weinberger  *
870dbb7d2a8SRichard Weinberger  * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
871dbb7d2a8SRichard Weinberger  * UBI_BAD_FASTMAP if one was found but is not usable.
872dbb7d2a8SRichard Weinberger  * < 0 indicates an internal error.
873dbb7d2a8SRichard Weinberger  */
874dbb7d2a8SRichard Weinberger int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
875dbb7d2a8SRichard Weinberger 		     int fm_anchor)
876dbb7d2a8SRichard Weinberger {
877dbb7d2a8SRichard Weinberger 	struct ubi_fm_sb *fmsb, *fmsb2;
878dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *vh;
879dbb7d2a8SRichard Weinberger 	struct ubi_ec_hdr *ech;
880dbb7d2a8SRichard Weinberger 	struct ubi_fastmap_layout *fm;
881dbb7d2a8SRichard Weinberger 	int i, used_blocks, pnum, ret = 0;
882dbb7d2a8SRichard Weinberger 	size_t fm_size;
883dbb7d2a8SRichard Weinberger 	__be32 crc, tmp_crc;
884dbb7d2a8SRichard Weinberger 	unsigned long long sqnum = 0;
885dbb7d2a8SRichard Weinberger 
886dbb7d2a8SRichard Weinberger 	mutex_lock(&ubi->fm_mutex);
887dbb7d2a8SRichard Weinberger 	memset(ubi->fm_buf, 0, ubi->fm_size);
888dbb7d2a8SRichard Weinberger 
889dbb7d2a8SRichard Weinberger 	fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
890dbb7d2a8SRichard Weinberger 	if (!fmsb) {
891dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
892dbb7d2a8SRichard Weinberger 		goto out;
893dbb7d2a8SRichard Weinberger 	}
894dbb7d2a8SRichard Weinberger 
895dbb7d2a8SRichard Weinberger 	fm = kzalloc(sizeof(*fm), GFP_KERNEL);
896dbb7d2a8SRichard Weinberger 	if (!fm) {
897dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
898dbb7d2a8SRichard Weinberger 		kfree(fmsb);
899dbb7d2a8SRichard Weinberger 		goto out;
900dbb7d2a8SRichard Weinberger 	}
901dbb7d2a8SRichard Weinberger 
902dbb7d2a8SRichard Weinberger 	ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
903dbb7d2a8SRichard Weinberger 	if (ret && ret != UBI_IO_BITFLIPS)
904dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
905dbb7d2a8SRichard Weinberger 	else if (ret == UBI_IO_BITFLIPS)
906dbb7d2a8SRichard Weinberger 		fm->to_be_tortured[0] = 1;
907dbb7d2a8SRichard Weinberger 
908dbb7d2a8SRichard Weinberger 	if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
909dbb7d2a8SRichard Weinberger 		ubi_err("bad super block magic: 0x%x, expected: 0x%x",
910dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
911dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
912dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
913dbb7d2a8SRichard Weinberger 	}
914dbb7d2a8SRichard Weinberger 
915dbb7d2a8SRichard Weinberger 	if (fmsb->version != UBI_FM_FMT_VERSION) {
916dbb7d2a8SRichard Weinberger 		ubi_err("bad fastmap version: %i, expected: %i",
917dbb7d2a8SRichard Weinberger 			fmsb->version, UBI_FM_FMT_VERSION);
918dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
919dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
920dbb7d2a8SRichard Weinberger 	}
921dbb7d2a8SRichard Weinberger 
922dbb7d2a8SRichard Weinberger 	used_blocks = be32_to_cpu(fmsb->used_blocks);
923dbb7d2a8SRichard Weinberger 	if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
924dbb7d2a8SRichard Weinberger 		ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
925dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
926dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
927dbb7d2a8SRichard Weinberger 	}
928dbb7d2a8SRichard Weinberger 
929dbb7d2a8SRichard Weinberger 	fm_size = ubi->leb_size * used_blocks;
930dbb7d2a8SRichard Weinberger 	if (fm_size != ubi->fm_size) {
931dbb7d2a8SRichard Weinberger 		ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
932dbb7d2a8SRichard Weinberger 			ubi->fm_size);
933dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
934dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
935dbb7d2a8SRichard Weinberger 	}
936dbb7d2a8SRichard Weinberger 
937dbb7d2a8SRichard Weinberger 	ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
938dbb7d2a8SRichard Weinberger 	if (!ech) {
939dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
940dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
941dbb7d2a8SRichard Weinberger 	}
942dbb7d2a8SRichard Weinberger 
943dbb7d2a8SRichard Weinberger 	vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
944dbb7d2a8SRichard Weinberger 	if (!vh) {
945dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
946dbb7d2a8SRichard Weinberger 		goto free_hdr;
947dbb7d2a8SRichard Weinberger 	}
948dbb7d2a8SRichard Weinberger 
949dbb7d2a8SRichard Weinberger 	for (i = 0; i < used_blocks; i++) {
950c22301adSRichard Genoud 		int image_seq;
951c22301adSRichard Genoud 
952dbb7d2a8SRichard Weinberger 		pnum = be32_to_cpu(fmsb->block_loc[i]);
953dbb7d2a8SRichard Weinberger 
954dbb7d2a8SRichard Weinberger 		if (ubi_io_is_bad(ubi, pnum)) {
955dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
956dbb7d2a8SRichard Weinberger 			goto free_hdr;
957dbb7d2a8SRichard Weinberger 		}
958dbb7d2a8SRichard Weinberger 
959dbb7d2a8SRichard Weinberger 		ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
960dbb7d2a8SRichard Weinberger 		if (ret && ret != UBI_IO_BITFLIPS) {
961dbb7d2a8SRichard Weinberger 			ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
962dbb7d2a8SRichard Weinberger 				i, pnum);
963dbb7d2a8SRichard Weinberger 			if (ret > 0)
964dbb7d2a8SRichard Weinberger 				ret = UBI_BAD_FASTMAP;
965dbb7d2a8SRichard Weinberger 			goto free_hdr;
966dbb7d2a8SRichard Weinberger 		} else if (ret == UBI_IO_BITFLIPS)
967dbb7d2a8SRichard Weinberger 			fm->to_be_tortured[i] = 1;
968dbb7d2a8SRichard Weinberger 
969c22301adSRichard Genoud 		image_seq = be32_to_cpu(ech->image_seq);
970dbb7d2a8SRichard Weinberger 		if (!ubi->image_seq)
971c22301adSRichard Genoud 			ubi->image_seq = image_seq;
972dbb7d2a8SRichard Weinberger 
973c22301adSRichard Genoud 		/*
974c22301adSRichard Genoud 		 * Older UBI implementations have image_seq set to zero, so
975c22301adSRichard Genoud 		 * we shouldn't fail if image_seq == 0.
976c22301adSRichard Genoud 		 */
977c22301adSRichard Genoud 		if (image_seq && (image_seq != ubi->image_seq)) {
978c22301adSRichard Genoud 			ubi_err("wrong image seq:%d instead of %d",
979c22301adSRichard Genoud 				be32_to_cpu(ech->image_seq), ubi->image_seq);
980dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
981dbb7d2a8SRichard Weinberger 			goto free_hdr;
982dbb7d2a8SRichard Weinberger 		}
983dbb7d2a8SRichard Weinberger 
984dbb7d2a8SRichard Weinberger 		ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
985dbb7d2a8SRichard Weinberger 		if (ret && ret != UBI_IO_BITFLIPS) {
986dbb7d2a8SRichard Weinberger 			ubi_err("unable to read fastmap block# %i (PEB: %i)",
987dbb7d2a8SRichard Weinberger 				i, pnum);
988dbb7d2a8SRichard Weinberger 			goto free_hdr;
989dbb7d2a8SRichard Weinberger 		}
990dbb7d2a8SRichard Weinberger 
991dbb7d2a8SRichard Weinberger 		if (i == 0) {
992dbb7d2a8SRichard Weinberger 			if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
993dbb7d2a8SRichard Weinberger 				ubi_err("bad fastmap anchor vol_id: 0x%x," \
994dbb7d2a8SRichard Weinberger 					" expected: 0x%x",
995dbb7d2a8SRichard Weinberger 					be32_to_cpu(vh->vol_id),
996dbb7d2a8SRichard Weinberger 					UBI_FM_SB_VOLUME_ID);
997dbb7d2a8SRichard Weinberger 				ret = UBI_BAD_FASTMAP;
998dbb7d2a8SRichard Weinberger 				goto free_hdr;
999dbb7d2a8SRichard Weinberger 			}
1000dbb7d2a8SRichard Weinberger 		} else {
1001dbb7d2a8SRichard Weinberger 			if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
1002dbb7d2a8SRichard Weinberger 				ubi_err("bad fastmap data vol_id: 0x%x," \
1003dbb7d2a8SRichard Weinberger 					" expected: 0x%x",
1004dbb7d2a8SRichard Weinberger 					be32_to_cpu(vh->vol_id),
1005dbb7d2a8SRichard Weinberger 					UBI_FM_DATA_VOLUME_ID);
1006dbb7d2a8SRichard Weinberger 				ret = UBI_BAD_FASTMAP;
1007dbb7d2a8SRichard Weinberger 				goto free_hdr;
1008dbb7d2a8SRichard Weinberger 			}
1009dbb7d2a8SRichard Weinberger 		}
1010dbb7d2a8SRichard Weinberger 
1011dbb7d2a8SRichard Weinberger 		if (sqnum < be64_to_cpu(vh->sqnum))
1012dbb7d2a8SRichard Weinberger 			sqnum = be64_to_cpu(vh->sqnum);
1013dbb7d2a8SRichard Weinberger 
1014dbb7d2a8SRichard Weinberger 		ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
1015dbb7d2a8SRichard Weinberger 				  ubi->leb_start, ubi->leb_size);
1016dbb7d2a8SRichard Weinberger 		if (ret && ret != UBI_IO_BITFLIPS) {
1017dbb7d2a8SRichard Weinberger 			ubi_err("unable to read fastmap block# %i (PEB: %i, " \
1018dbb7d2a8SRichard Weinberger 				"err: %i)", i, pnum, ret);
1019dbb7d2a8SRichard Weinberger 			goto free_hdr;
1020dbb7d2a8SRichard Weinberger 		}
1021dbb7d2a8SRichard Weinberger 	}
1022dbb7d2a8SRichard Weinberger 
1023dbb7d2a8SRichard Weinberger 	kfree(fmsb);
1024dbb7d2a8SRichard Weinberger 	fmsb = NULL;
1025dbb7d2a8SRichard Weinberger 
1026dbb7d2a8SRichard Weinberger 	fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
1027dbb7d2a8SRichard Weinberger 	tmp_crc = be32_to_cpu(fmsb2->data_crc);
1028dbb7d2a8SRichard Weinberger 	fmsb2->data_crc = 0;
1029dbb7d2a8SRichard Weinberger 	crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
1030dbb7d2a8SRichard Weinberger 	if (crc != tmp_crc) {
1031dbb7d2a8SRichard Weinberger 		ubi_err("fastmap data CRC is invalid");
1032dbb7d2a8SRichard Weinberger 		ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
1033dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
1034dbb7d2a8SRichard Weinberger 		goto free_hdr;
1035dbb7d2a8SRichard Weinberger 	}
1036dbb7d2a8SRichard Weinberger 
1037dbb7d2a8SRichard Weinberger 	fmsb2->sqnum = sqnum;
1038dbb7d2a8SRichard Weinberger 
1039dbb7d2a8SRichard Weinberger 	fm->used_blocks = used_blocks;
1040dbb7d2a8SRichard Weinberger 
1041dbb7d2a8SRichard Weinberger 	ret = ubi_attach_fastmap(ubi, ai, fm);
1042dbb7d2a8SRichard Weinberger 	if (ret) {
1043dbb7d2a8SRichard Weinberger 		if (ret > 0)
1044dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
1045dbb7d2a8SRichard Weinberger 		goto free_hdr;
1046dbb7d2a8SRichard Weinberger 	}
1047dbb7d2a8SRichard Weinberger 
1048dbb7d2a8SRichard Weinberger 	for (i = 0; i < used_blocks; i++) {
1049dbb7d2a8SRichard Weinberger 		struct ubi_wl_entry *e;
1050dbb7d2a8SRichard Weinberger 
1051dbb7d2a8SRichard Weinberger 		e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1052dbb7d2a8SRichard Weinberger 		if (!e) {
1053dbb7d2a8SRichard Weinberger 			while (i--)
1054dbb7d2a8SRichard Weinberger 				kfree(fm->e[i]);
1055dbb7d2a8SRichard Weinberger 
1056dbb7d2a8SRichard Weinberger 			ret = -ENOMEM;
1057dbb7d2a8SRichard Weinberger 			goto free_hdr;
1058dbb7d2a8SRichard Weinberger 		}
1059dbb7d2a8SRichard Weinberger 
1060dbb7d2a8SRichard Weinberger 		e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
1061dbb7d2a8SRichard Weinberger 		e->ec = be32_to_cpu(fmsb2->block_ec[i]);
1062dbb7d2a8SRichard Weinberger 		fm->e[i] = e;
1063dbb7d2a8SRichard Weinberger 	}
1064dbb7d2a8SRichard Weinberger 
1065dbb7d2a8SRichard Weinberger 	ubi->fm = fm;
1066dbb7d2a8SRichard Weinberger 	ubi->fm_pool.max_size = ubi->fm->max_pool_size;
1067dbb7d2a8SRichard Weinberger 	ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
1068dbb7d2a8SRichard Weinberger 	ubi_msg("attached by fastmap");
1069dbb7d2a8SRichard Weinberger 	ubi_msg("fastmap pool size: %d", ubi->fm_pool.max_size);
1070dbb7d2a8SRichard Weinberger 	ubi_msg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
1071dbb7d2a8SRichard Weinberger 	ubi->fm_disabled = 0;
1072dbb7d2a8SRichard Weinberger 
1073dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, vh);
1074dbb7d2a8SRichard Weinberger 	kfree(ech);
1075dbb7d2a8SRichard Weinberger out:
1076dbb7d2a8SRichard Weinberger 	mutex_unlock(&ubi->fm_mutex);
1077dbb7d2a8SRichard Weinberger 	if (ret == UBI_BAD_FASTMAP)
1078dbb7d2a8SRichard Weinberger 		ubi_err("Attach by fastmap failed, doing a full scan!");
1079dbb7d2a8SRichard Weinberger 	return ret;
1080dbb7d2a8SRichard Weinberger 
1081dbb7d2a8SRichard Weinberger free_hdr:
1082dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, vh);
1083dbb7d2a8SRichard Weinberger 	kfree(ech);
1084dbb7d2a8SRichard Weinberger free_fm_sb:
1085dbb7d2a8SRichard Weinberger 	kfree(fmsb);
1086dbb7d2a8SRichard Weinberger 	kfree(fm);
1087dbb7d2a8SRichard Weinberger 	goto out;
1088dbb7d2a8SRichard Weinberger }
1089dbb7d2a8SRichard Weinberger 
1090dbb7d2a8SRichard Weinberger /**
1091dbb7d2a8SRichard Weinberger  * ubi_write_fastmap - writes a fastmap.
1092dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1093dbb7d2a8SRichard Weinberger  * @new_fm: the to be written fastmap
1094dbb7d2a8SRichard Weinberger  *
1095dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
1096dbb7d2a8SRichard Weinberger  */
1097dbb7d2a8SRichard Weinberger static int ubi_write_fastmap(struct ubi_device *ubi,
1098dbb7d2a8SRichard Weinberger 			     struct ubi_fastmap_layout *new_fm)
1099dbb7d2a8SRichard Weinberger {
1100dbb7d2a8SRichard Weinberger 	size_t fm_pos = 0;
1101dbb7d2a8SRichard Weinberger 	void *fm_raw;
1102dbb7d2a8SRichard Weinberger 	struct ubi_fm_sb *fmsb;
1103dbb7d2a8SRichard Weinberger 	struct ubi_fm_hdr *fmh;
1104dbb7d2a8SRichard Weinberger 	struct ubi_fm_scan_pool *fmpl1, *fmpl2;
1105dbb7d2a8SRichard Weinberger 	struct ubi_fm_ec *fec;
1106dbb7d2a8SRichard Weinberger 	struct ubi_fm_volhdr *fvh;
1107dbb7d2a8SRichard Weinberger 	struct ubi_fm_eba *feba;
1108dbb7d2a8SRichard Weinberger 	struct rb_node *node;
1109dbb7d2a8SRichard Weinberger 	struct ubi_wl_entry *wl_e;
1110dbb7d2a8SRichard Weinberger 	struct ubi_volume *vol;
1111dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *avhdr, *dvhdr;
1112dbb7d2a8SRichard Weinberger 	struct ubi_work *ubi_wrk;
1113dbb7d2a8SRichard Weinberger 	int ret, i, j, free_peb_count, used_peb_count, vol_count;
1114dbb7d2a8SRichard Weinberger 	int scrub_peb_count, erase_peb_count;
1115dbb7d2a8SRichard Weinberger 
1116dbb7d2a8SRichard Weinberger 	fm_raw = ubi->fm_buf;
1117dbb7d2a8SRichard Weinberger 	memset(ubi->fm_buf, 0, ubi->fm_size);
1118dbb7d2a8SRichard Weinberger 
1119dbb7d2a8SRichard Weinberger 	avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1120dbb7d2a8SRichard Weinberger 	if (!avhdr) {
1121dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
1122dbb7d2a8SRichard Weinberger 		goto out;
1123dbb7d2a8SRichard Weinberger 	}
1124dbb7d2a8SRichard Weinberger 
1125dbb7d2a8SRichard Weinberger 	dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
1126dbb7d2a8SRichard Weinberger 	if (!dvhdr) {
1127dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
1128dbb7d2a8SRichard Weinberger 		goto out_kfree;
1129dbb7d2a8SRichard Weinberger 	}
1130dbb7d2a8SRichard Weinberger 
1131dbb7d2a8SRichard Weinberger 	spin_lock(&ubi->volumes_lock);
1132dbb7d2a8SRichard Weinberger 	spin_lock(&ubi->wl_lock);
1133dbb7d2a8SRichard Weinberger 
1134dbb7d2a8SRichard Weinberger 	fmsb = (struct ubi_fm_sb *)fm_raw;
1135dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmsb);
1136dbb7d2a8SRichard Weinberger 	ubi_assert(fm_pos <= ubi->fm_size);
1137dbb7d2a8SRichard Weinberger 
1138dbb7d2a8SRichard Weinberger 	fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
1139dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmh);
1140dbb7d2a8SRichard Weinberger 	ubi_assert(fm_pos <= ubi->fm_size);
1141dbb7d2a8SRichard Weinberger 
1142dbb7d2a8SRichard Weinberger 	fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
1143dbb7d2a8SRichard Weinberger 	fmsb->version = UBI_FM_FMT_VERSION;
1144dbb7d2a8SRichard Weinberger 	fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
1145dbb7d2a8SRichard Weinberger 	/* the max sqnum will be filled in while *reading* the fastmap */
1146dbb7d2a8SRichard Weinberger 	fmsb->sqnum = 0;
1147dbb7d2a8SRichard Weinberger 
1148dbb7d2a8SRichard Weinberger 	fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
1149dbb7d2a8SRichard Weinberger 	free_peb_count = 0;
1150dbb7d2a8SRichard Weinberger 	used_peb_count = 0;
1151dbb7d2a8SRichard Weinberger 	scrub_peb_count = 0;
1152dbb7d2a8SRichard Weinberger 	erase_peb_count = 0;
1153dbb7d2a8SRichard Weinberger 	vol_count = 0;
1154dbb7d2a8SRichard Weinberger 
1155dbb7d2a8SRichard Weinberger 	fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1156dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmpl1);
1157dbb7d2a8SRichard Weinberger 	fmpl1->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1158dbb7d2a8SRichard Weinberger 	fmpl1->size = cpu_to_be16(ubi->fm_pool.size);
1159dbb7d2a8SRichard Weinberger 	fmpl1->max_size = cpu_to_be16(ubi->fm_pool.max_size);
1160dbb7d2a8SRichard Weinberger 
1161dbb7d2a8SRichard Weinberger 	for (i = 0; i < ubi->fm_pool.size; i++)
1162dbb7d2a8SRichard Weinberger 		fmpl1->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
1163dbb7d2a8SRichard Weinberger 
1164dbb7d2a8SRichard Weinberger 	fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1165dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmpl2);
1166dbb7d2a8SRichard Weinberger 	fmpl2->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1167dbb7d2a8SRichard Weinberger 	fmpl2->size = cpu_to_be16(ubi->fm_wl_pool.size);
1168dbb7d2a8SRichard Weinberger 	fmpl2->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
1169dbb7d2a8SRichard Weinberger 
1170dbb7d2a8SRichard Weinberger 	for (i = 0; i < ubi->fm_wl_pool.size; i++)
1171dbb7d2a8SRichard Weinberger 		fmpl2->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
1172dbb7d2a8SRichard Weinberger 
1173dbb7d2a8SRichard Weinberger 	for (node = rb_first(&ubi->free); node; node = rb_next(node)) {
1174dbb7d2a8SRichard Weinberger 		wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1175dbb7d2a8SRichard Weinberger 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1176dbb7d2a8SRichard Weinberger 
1177dbb7d2a8SRichard Weinberger 		fec->pnum = cpu_to_be32(wl_e->pnum);
1178dbb7d2a8SRichard Weinberger 		fec->ec = cpu_to_be32(wl_e->ec);
1179dbb7d2a8SRichard Weinberger 
1180dbb7d2a8SRichard Weinberger 		free_peb_count++;
1181dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fec);
1182dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1183dbb7d2a8SRichard Weinberger 	}
1184dbb7d2a8SRichard Weinberger 	fmh->free_peb_count = cpu_to_be32(free_peb_count);
1185dbb7d2a8SRichard Weinberger 
1186dbb7d2a8SRichard Weinberger 	for (node = rb_first(&ubi->used); node; node = rb_next(node)) {
1187dbb7d2a8SRichard Weinberger 		wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1188dbb7d2a8SRichard Weinberger 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1189dbb7d2a8SRichard Weinberger 
1190dbb7d2a8SRichard Weinberger 		fec->pnum = cpu_to_be32(wl_e->pnum);
1191dbb7d2a8SRichard Weinberger 		fec->ec = cpu_to_be32(wl_e->ec);
1192dbb7d2a8SRichard Weinberger 
1193dbb7d2a8SRichard Weinberger 		used_peb_count++;
1194dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fec);
1195dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1196dbb7d2a8SRichard Weinberger 	}
1197dbb7d2a8SRichard Weinberger 	fmh->used_peb_count = cpu_to_be32(used_peb_count);
1198dbb7d2a8SRichard Weinberger 
1199dbb7d2a8SRichard Weinberger 	for (node = rb_first(&ubi->scrub); node; node = rb_next(node)) {
1200dbb7d2a8SRichard Weinberger 		wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1201dbb7d2a8SRichard Weinberger 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1202dbb7d2a8SRichard Weinberger 
1203dbb7d2a8SRichard Weinberger 		fec->pnum = cpu_to_be32(wl_e->pnum);
1204dbb7d2a8SRichard Weinberger 		fec->ec = cpu_to_be32(wl_e->ec);
1205dbb7d2a8SRichard Weinberger 
1206dbb7d2a8SRichard Weinberger 		scrub_peb_count++;
1207dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fec);
1208dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1209dbb7d2a8SRichard Weinberger 	}
1210dbb7d2a8SRichard Weinberger 	fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
1211dbb7d2a8SRichard Weinberger 
1212dbb7d2a8SRichard Weinberger 
1213dbb7d2a8SRichard Weinberger 	list_for_each_entry(ubi_wrk, &ubi->works, list) {
1214dbb7d2a8SRichard Weinberger 		if (ubi_is_erase_work(ubi_wrk)) {
1215dbb7d2a8SRichard Weinberger 			wl_e = ubi_wrk->e;
1216dbb7d2a8SRichard Weinberger 			ubi_assert(wl_e);
1217dbb7d2a8SRichard Weinberger 
1218dbb7d2a8SRichard Weinberger 			fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1219dbb7d2a8SRichard Weinberger 
1220dbb7d2a8SRichard Weinberger 			fec->pnum = cpu_to_be32(wl_e->pnum);
1221dbb7d2a8SRichard Weinberger 			fec->ec = cpu_to_be32(wl_e->ec);
1222dbb7d2a8SRichard Weinberger 
1223dbb7d2a8SRichard Weinberger 			erase_peb_count++;
1224dbb7d2a8SRichard Weinberger 			fm_pos += sizeof(*fec);
1225dbb7d2a8SRichard Weinberger 			ubi_assert(fm_pos <= ubi->fm_size);
1226dbb7d2a8SRichard Weinberger 		}
1227dbb7d2a8SRichard Weinberger 	}
1228dbb7d2a8SRichard Weinberger 	fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
1229dbb7d2a8SRichard Weinberger 
1230dbb7d2a8SRichard Weinberger 	for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
1231dbb7d2a8SRichard Weinberger 		vol = ubi->volumes[i];
1232dbb7d2a8SRichard Weinberger 
1233dbb7d2a8SRichard Weinberger 		if (!vol)
1234dbb7d2a8SRichard Weinberger 			continue;
1235dbb7d2a8SRichard Weinberger 
1236dbb7d2a8SRichard Weinberger 		vol_count++;
1237dbb7d2a8SRichard Weinberger 
1238dbb7d2a8SRichard Weinberger 		fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
1239dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fvh);
1240dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1241dbb7d2a8SRichard Weinberger 
1242dbb7d2a8SRichard Weinberger 		fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
1243dbb7d2a8SRichard Weinberger 		fvh->vol_id = cpu_to_be32(vol->vol_id);
1244dbb7d2a8SRichard Weinberger 		fvh->vol_type = vol->vol_type;
1245dbb7d2a8SRichard Weinberger 		fvh->used_ebs = cpu_to_be32(vol->used_ebs);
1246dbb7d2a8SRichard Weinberger 		fvh->data_pad = cpu_to_be32(vol->data_pad);
1247dbb7d2a8SRichard Weinberger 		fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
1248dbb7d2a8SRichard Weinberger 
1249dbb7d2a8SRichard Weinberger 		ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
1250dbb7d2a8SRichard Weinberger 			vol->vol_type == UBI_STATIC_VOLUME);
1251dbb7d2a8SRichard Weinberger 
1252dbb7d2a8SRichard Weinberger 		feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
1253dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
1254dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1255dbb7d2a8SRichard Weinberger 
1256dbb7d2a8SRichard Weinberger 		for (j = 0; j < vol->reserved_pebs; j++)
1257dbb7d2a8SRichard Weinberger 			feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]);
1258dbb7d2a8SRichard Weinberger 
1259dbb7d2a8SRichard Weinberger 		feba->reserved_pebs = cpu_to_be32(j);
1260dbb7d2a8SRichard Weinberger 		feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
1261dbb7d2a8SRichard Weinberger 	}
1262dbb7d2a8SRichard Weinberger 	fmh->vol_count = cpu_to_be32(vol_count);
1263dbb7d2a8SRichard Weinberger 	fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
1264dbb7d2a8SRichard Weinberger 
1265dbb7d2a8SRichard Weinberger 	avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1266dbb7d2a8SRichard Weinberger 	avhdr->lnum = 0;
1267dbb7d2a8SRichard Weinberger 
1268dbb7d2a8SRichard Weinberger 	spin_unlock(&ubi->wl_lock);
1269dbb7d2a8SRichard Weinberger 	spin_unlock(&ubi->volumes_lock);
1270dbb7d2a8SRichard Weinberger 
1271dbb7d2a8SRichard Weinberger 	dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
1272dbb7d2a8SRichard Weinberger 	ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
1273dbb7d2a8SRichard Weinberger 	if (ret) {
1274dbb7d2a8SRichard Weinberger 		ubi_err("unable to write vid_hdr to fastmap SB!");
1275dbb7d2a8SRichard Weinberger 		goto out_kfree;
1276dbb7d2a8SRichard Weinberger 	}
1277dbb7d2a8SRichard Weinberger 
1278dbb7d2a8SRichard Weinberger 	for (i = 0; i < new_fm->used_blocks; i++) {
1279dbb7d2a8SRichard Weinberger 		fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
1280dbb7d2a8SRichard Weinberger 		fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
1281dbb7d2a8SRichard Weinberger 	}
1282dbb7d2a8SRichard Weinberger 
1283dbb7d2a8SRichard Weinberger 	fmsb->data_crc = 0;
1284dbb7d2a8SRichard Weinberger 	fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
1285dbb7d2a8SRichard Weinberger 					   ubi->fm_size));
1286dbb7d2a8SRichard Weinberger 
1287dbb7d2a8SRichard Weinberger 	for (i = 1; i < new_fm->used_blocks; i++) {
1288dbb7d2a8SRichard Weinberger 		dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1289dbb7d2a8SRichard Weinberger 		dvhdr->lnum = cpu_to_be32(i);
1290dbb7d2a8SRichard Weinberger 		dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1291dbb7d2a8SRichard Weinberger 			new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
1292dbb7d2a8SRichard Weinberger 		ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
1293dbb7d2a8SRichard Weinberger 		if (ret) {
1294dbb7d2a8SRichard Weinberger 			ubi_err("unable to write vid_hdr to PEB %i!",
1295dbb7d2a8SRichard Weinberger 				new_fm->e[i]->pnum);
1296dbb7d2a8SRichard Weinberger 			goto out_kfree;
1297dbb7d2a8SRichard Weinberger 		}
1298dbb7d2a8SRichard Weinberger 	}
1299dbb7d2a8SRichard Weinberger 
1300dbb7d2a8SRichard Weinberger 	for (i = 0; i < new_fm->used_blocks; i++) {
1301dbb7d2a8SRichard Weinberger 		ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size),
1302dbb7d2a8SRichard Weinberger 			new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size);
1303dbb7d2a8SRichard Weinberger 		if (ret) {
1304dbb7d2a8SRichard Weinberger 			ubi_err("unable to write fastmap to PEB %i!",
1305dbb7d2a8SRichard Weinberger 				new_fm->e[i]->pnum);
1306dbb7d2a8SRichard Weinberger 			goto out_kfree;
1307dbb7d2a8SRichard Weinberger 		}
1308dbb7d2a8SRichard Weinberger 	}
1309dbb7d2a8SRichard Weinberger 
1310dbb7d2a8SRichard Weinberger 	ubi_assert(new_fm);
1311dbb7d2a8SRichard Weinberger 	ubi->fm = new_fm;
1312dbb7d2a8SRichard Weinberger 
1313dbb7d2a8SRichard Weinberger 	dbg_bld("fastmap written!");
1314dbb7d2a8SRichard Weinberger 
1315dbb7d2a8SRichard Weinberger out_kfree:
1316dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, avhdr);
1317dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, dvhdr);
1318dbb7d2a8SRichard Weinberger out:
1319dbb7d2a8SRichard Weinberger 	return ret;
1320dbb7d2a8SRichard Weinberger }
1321dbb7d2a8SRichard Weinberger 
1322dbb7d2a8SRichard Weinberger /**
1323dbb7d2a8SRichard Weinberger  * erase_block - Manually erase a PEB.
1324dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1325dbb7d2a8SRichard Weinberger  * @pnum: PEB to be erased
1326dbb7d2a8SRichard Weinberger  *
1327dbb7d2a8SRichard Weinberger  * Returns the new EC value on success, < 0 indicates an internal error.
1328dbb7d2a8SRichard Weinberger  */
1329dbb7d2a8SRichard Weinberger static int erase_block(struct ubi_device *ubi, int pnum)
1330dbb7d2a8SRichard Weinberger {
1331dbb7d2a8SRichard Weinberger 	int ret;
1332dbb7d2a8SRichard Weinberger 	struct ubi_ec_hdr *ec_hdr;
1333dbb7d2a8SRichard Weinberger 	long long ec;
1334dbb7d2a8SRichard Weinberger 
1335dbb7d2a8SRichard Weinberger 	ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1336dbb7d2a8SRichard Weinberger 	if (!ec_hdr)
1337dbb7d2a8SRichard Weinberger 		return -ENOMEM;
1338dbb7d2a8SRichard Weinberger 
1339dbb7d2a8SRichard Weinberger 	ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1340dbb7d2a8SRichard Weinberger 	if (ret < 0)
1341dbb7d2a8SRichard Weinberger 		goto out;
1342dbb7d2a8SRichard Weinberger 	else if (ret && ret != UBI_IO_BITFLIPS) {
1343dbb7d2a8SRichard Weinberger 		ret = -EINVAL;
1344dbb7d2a8SRichard Weinberger 		goto out;
1345dbb7d2a8SRichard Weinberger 	}
1346dbb7d2a8SRichard Weinberger 
1347dbb7d2a8SRichard Weinberger 	ret = ubi_io_sync_erase(ubi, pnum, 0);
1348dbb7d2a8SRichard Weinberger 	if (ret < 0)
1349dbb7d2a8SRichard Weinberger 		goto out;
1350dbb7d2a8SRichard Weinberger 
1351dbb7d2a8SRichard Weinberger 	ec = be64_to_cpu(ec_hdr->ec);
1352dbb7d2a8SRichard Weinberger 	ec += ret;
1353dbb7d2a8SRichard Weinberger 	if (ec > UBI_MAX_ERASECOUNTER) {
1354dbb7d2a8SRichard Weinberger 		ret = -EINVAL;
1355dbb7d2a8SRichard Weinberger 		goto out;
1356dbb7d2a8SRichard Weinberger 	}
1357dbb7d2a8SRichard Weinberger 
1358dbb7d2a8SRichard Weinberger 	ec_hdr->ec = cpu_to_be64(ec);
1359dbb7d2a8SRichard Weinberger 	ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
1360dbb7d2a8SRichard Weinberger 	if (ret < 0)
1361dbb7d2a8SRichard Weinberger 		goto out;
1362dbb7d2a8SRichard Weinberger 
1363dbb7d2a8SRichard Weinberger 	ret = ec;
1364dbb7d2a8SRichard Weinberger out:
1365dbb7d2a8SRichard Weinberger 	kfree(ec_hdr);
1366dbb7d2a8SRichard Weinberger 	return ret;
1367dbb7d2a8SRichard Weinberger }
1368dbb7d2a8SRichard Weinberger 
1369dbb7d2a8SRichard Weinberger /**
1370dbb7d2a8SRichard Weinberger  * invalidate_fastmap - destroys a fastmap.
1371dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1372dbb7d2a8SRichard Weinberger  * @fm: the fastmap to be destroyed
1373dbb7d2a8SRichard Weinberger  *
1374dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
1375dbb7d2a8SRichard Weinberger  */
1376dbb7d2a8SRichard Weinberger static int invalidate_fastmap(struct ubi_device *ubi,
1377dbb7d2a8SRichard Weinberger 			      struct ubi_fastmap_layout *fm)
1378dbb7d2a8SRichard Weinberger {
13798930fa50SRichard Weinberger 	int ret;
1380dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *vh;
1381dbb7d2a8SRichard Weinberger 
1382dbb7d2a8SRichard Weinberger 	ret = erase_block(ubi, fm->e[0]->pnum);
1383dbb7d2a8SRichard Weinberger 	if (ret < 0)
1384dbb7d2a8SRichard Weinberger 		return ret;
1385dbb7d2a8SRichard Weinberger 
1386dbb7d2a8SRichard Weinberger 	vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1387dbb7d2a8SRichard Weinberger 	if (!vh)
1388dbb7d2a8SRichard Weinberger 		return -ENOMEM;
1389dbb7d2a8SRichard Weinberger 
1390dbb7d2a8SRichard Weinberger 	/* deleting the current fastmap SB is not enough, an old SB may exist,
1391dbb7d2a8SRichard Weinberger 	 * so create a (corrupted) SB such that fastmap will find it and fall
1392dbb7d2a8SRichard Weinberger 	 * back to scanning mode in any case */
1393dbb7d2a8SRichard Weinberger 	vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1394dbb7d2a8SRichard Weinberger 	ret = ubi_io_write_vid_hdr(ubi, fm->e[0]->pnum, vh);
1395dbb7d2a8SRichard Weinberger 
1396dbb7d2a8SRichard Weinberger 	return ret;
1397dbb7d2a8SRichard Weinberger }
1398dbb7d2a8SRichard Weinberger 
1399dbb7d2a8SRichard Weinberger /**
1400dbb7d2a8SRichard Weinberger  * ubi_update_fastmap - will be called by UBI if a volume changes or
1401dbb7d2a8SRichard Weinberger  * a fastmap pool becomes full.
1402dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1403dbb7d2a8SRichard Weinberger  *
1404dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
1405dbb7d2a8SRichard Weinberger  */
1406dbb7d2a8SRichard Weinberger int ubi_update_fastmap(struct ubi_device *ubi)
1407dbb7d2a8SRichard Weinberger {
1408dbb7d2a8SRichard Weinberger 	int ret, i;
1409dbb7d2a8SRichard Weinberger 	struct ubi_fastmap_layout *new_fm, *old_fm;
1410dbb7d2a8SRichard Weinberger 	struct ubi_wl_entry *tmp_e;
1411dbb7d2a8SRichard Weinberger 
1412dbb7d2a8SRichard Weinberger 	mutex_lock(&ubi->fm_mutex);
1413dbb7d2a8SRichard Weinberger 
1414dbb7d2a8SRichard Weinberger 	ubi_refill_pools(ubi);
1415dbb7d2a8SRichard Weinberger 
1416dbb7d2a8SRichard Weinberger 	if (ubi->ro_mode || ubi->fm_disabled) {
1417dbb7d2a8SRichard Weinberger 		mutex_unlock(&ubi->fm_mutex);
1418dbb7d2a8SRichard Weinberger 		return 0;
1419dbb7d2a8SRichard Weinberger 	}
1420dbb7d2a8SRichard Weinberger 
1421dbb7d2a8SRichard Weinberger 	ret = ubi_ensure_anchor_pebs(ubi);
1422dbb7d2a8SRichard Weinberger 	if (ret) {
1423dbb7d2a8SRichard Weinberger 		mutex_unlock(&ubi->fm_mutex);
1424dbb7d2a8SRichard Weinberger 		return ret;
1425dbb7d2a8SRichard Weinberger 	}
1426dbb7d2a8SRichard Weinberger 
1427dbb7d2a8SRichard Weinberger 	new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
1428dbb7d2a8SRichard Weinberger 	if (!new_fm) {
1429dbb7d2a8SRichard Weinberger 		mutex_unlock(&ubi->fm_mutex);
1430dbb7d2a8SRichard Weinberger 		return -ENOMEM;
1431dbb7d2a8SRichard Weinberger 	}
1432dbb7d2a8SRichard Weinberger 
1433dbb7d2a8SRichard Weinberger 	new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
1434dbb7d2a8SRichard Weinberger 
1435dbb7d2a8SRichard Weinberger 	for (i = 0; i < new_fm->used_blocks; i++) {
1436dbb7d2a8SRichard Weinberger 		new_fm->e[i] = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1437dbb7d2a8SRichard Weinberger 		if (!new_fm->e[i]) {
1438dbb7d2a8SRichard Weinberger 			while (i--)
1439dbb7d2a8SRichard Weinberger 				kfree(new_fm->e[i]);
1440dbb7d2a8SRichard Weinberger 
1441dbb7d2a8SRichard Weinberger 			kfree(new_fm);
1442dbb7d2a8SRichard Weinberger 			mutex_unlock(&ubi->fm_mutex);
1443dbb7d2a8SRichard Weinberger 			return -ENOMEM;
1444dbb7d2a8SRichard Weinberger 		}
1445dbb7d2a8SRichard Weinberger 	}
1446dbb7d2a8SRichard Weinberger 
1447dbb7d2a8SRichard Weinberger 	old_fm = ubi->fm;
1448dbb7d2a8SRichard Weinberger 	ubi->fm = NULL;
1449dbb7d2a8SRichard Weinberger 
1450dbb7d2a8SRichard Weinberger 	if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
1451dbb7d2a8SRichard Weinberger 		ubi_err("fastmap too large");
1452dbb7d2a8SRichard Weinberger 		ret = -ENOSPC;
1453dbb7d2a8SRichard Weinberger 		goto err;
1454dbb7d2a8SRichard Weinberger 	}
1455dbb7d2a8SRichard Weinberger 
1456dbb7d2a8SRichard Weinberger 	for (i = 1; i < new_fm->used_blocks; i++) {
1457dbb7d2a8SRichard Weinberger 		spin_lock(&ubi->wl_lock);
1458dbb7d2a8SRichard Weinberger 		tmp_e = ubi_wl_get_fm_peb(ubi, 0);
1459dbb7d2a8SRichard Weinberger 		spin_unlock(&ubi->wl_lock);
1460dbb7d2a8SRichard Weinberger 
1461dbb7d2a8SRichard Weinberger 		if (!tmp_e && !old_fm) {
1462dbb7d2a8SRichard Weinberger 			int j;
1463dbb7d2a8SRichard Weinberger 			ubi_err("could not get any free erase block");
1464dbb7d2a8SRichard Weinberger 
1465dbb7d2a8SRichard Weinberger 			for (j = 1; j < i; j++)
1466dbb7d2a8SRichard Weinberger 				ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
1467dbb7d2a8SRichard Weinberger 
1468dbb7d2a8SRichard Weinberger 			ret = -ENOSPC;
1469dbb7d2a8SRichard Weinberger 			goto err;
1470dbb7d2a8SRichard Weinberger 		} else if (!tmp_e && old_fm) {
1471dbb7d2a8SRichard Weinberger 			ret = erase_block(ubi, old_fm->e[i]->pnum);
1472dbb7d2a8SRichard Weinberger 			if (ret < 0) {
1473dbb7d2a8SRichard Weinberger 				int j;
1474dbb7d2a8SRichard Weinberger 
1475dbb7d2a8SRichard Weinberger 				for (j = 1; j < i; j++)
1476dbb7d2a8SRichard Weinberger 					ubi_wl_put_fm_peb(ubi, new_fm->e[j],
1477dbb7d2a8SRichard Weinberger 							  j, 0);
1478dbb7d2a8SRichard Weinberger 
1479dbb7d2a8SRichard Weinberger 				ubi_err("could not erase old fastmap PEB");
1480dbb7d2a8SRichard Weinberger 				goto err;
1481dbb7d2a8SRichard Weinberger 			}
1482dbb7d2a8SRichard Weinberger 
1483dbb7d2a8SRichard Weinberger 			new_fm->e[i]->pnum = old_fm->e[i]->pnum;
1484dbb7d2a8SRichard Weinberger 			new_fm->e[i]->ec = old_fm->e[i]->ec;
1485dbb7d2a8SRichard Weinberger 		} else {
1486dbb7d2a8SRichard Weinberger 			new_fm->e[i]->pnum = tmp_e->pnum;
1487dbb7d2a8SRichard Weinberger 			new_fm->e[i]->ec = tmp_e->ec;
1488dbb7d2a8SRichard Weinberger 
1489dbb7d2a8SRichard Weinberger 			if (old_fm)
1490dbb7d2a8SRichard Weinberger 				ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1491dbb7d2a8SRichard Weinberger 						  old_fm->to_be_tortured[i]);
1492dbb7d2a8SRichard Weinberger 		}
1493dbb7d2a8SRichard Weinberger 	}
1494dbb7d2a8SRichard Weinberger 
1495dbb7d2a8SRichard Weinberger 	spin_lock(&ubi->wl_lock);
1496dbb7d2a8SRichard Weinberger 	tmp_e = ubi_wl_get_fm_peb(ubi, 1);
1497dbb7d2a8SRichard Weinberger 	spin_unlock(&ubi->wl_lock);
1498dbb7d2a8SRichard Weinberger 
1499dbb7d2a8SRichard Weinberger 	if (old_fm) {
1500dbb7d2a8SRichard Weinberger 		/* no fresh anchor PEB was found, reuse the old one */
1501dbb7d2a8SRichard Weinberger 		if (!tmp_e) {
1502dbb7d2a8SRichard Weinberger 			ret = erase_block(ubi, old_fm->e[0]->pnum);
1503dbb7d2a8SRichard Weinberger 			if (ret < 0) {
1504dbb7d2a8SRichard Weinberger 				int i;
1505dbb7d2a8SRichard Weinberger 				ubi_err("could not erase old anchor PEB");
1506dbb7d2a8SRichard Weinberger 
1507dbb7d2a8SRichard Weinberger 				for (i = 1; i < new_fm->used_blocks; i++)
1508dbb7d2a8SRichard Weinberger 					ubi_wl_put_fm_peb(ubi, new_fm->e[i],
1509dbb7d2a8SRichard Weinberger 							  i, 0);
1510dbb7d2a8SRichard Weinberger 				goto err;
1511dbb7d2a8SRichard Weinberger 			}
1512dbb7d2a8SRichard Weinberger 
1513dbb7d2a8SRichard Weinberger 			new_fm->e[0]->pnum = old_fm->e[0]->pnum;
1514dbb7d2a8SRichard Weinberger 			new_fm->e[0]->ec = ret;
1515dbb7d2a8SRichard Weinberger 		} else {
1516dbb7d2a8SRichard Weinberger 			/* we've got a new anchor PEB, return the old one */
1517dbb7d2a8SRichard Weinberger 			ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
1518dbb7d2a8SRichard Weinberger 					  old_fm->to_be_tortured[0]);
1519dbb7d2a8SRichard Weinberger 
1520dbb7d2a8SRichard Weinberger 			new_fm->e[0]->pnum = tmp_e->pnum;
1521dbb7d2a8SRichard Weinberger 			new_fm->e[0]->ec = tmp_e->ec;
1522dbb7d2a8SRichard Weinberger 		}
1523dbb7d2a8SRichard Weinberger 	} else {
1524dbb7d2a8SRichard Weinberger 		if (!tmp_e) {
1525dbb7d2a8SRichard Weinberger 			int i;
1526dbb7d2a8SRichard Weinberger 			ubi_err("could not find any anchor PEB");
1527dbb7d2a8SRichard Weinberger 
1528dbb7d2a8SRichard Weinberger 			for (i = 1; i < new_fm->used_blocks; i++)
1529dbb7d2a8SRichard Weinberger 				ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
1530dbb7d2a8SRichard Weinberger 
1531dbb7d2a8SRichard Weinberger 			ret = -ENOSPC;
1532dbb7d2a8SRichard Weinberger 			goto err;
1533dbb7d2a8SRichard Weinberger 		}
1534dbb7d2a8SRichard Weinberger 
1535dbb7d2a8SRichard Weinberger 		new_fm->e[0]->pnum = tmp_e->pnum;
1536dbb7d2a8SRichard Weinberger 		new_fm->e[0]->ec = tmp_e->ec;
1537dbb7d2a8SRichard Weinberger 	}
1538dbb7d2a8SRichard Weinberger 
1539dbb7d2a8SRichard Weinberger 	down_write(&ubi->work_sem);
1540dbb7d2a8SRichard Weinberger 	down_write(&ubi->fm_sem);
1541dbb7d2a8SRichard Weinberger 	ret = ubi_write_fastmap(ubi, new_fm);
1542dbb7d2a8SRichard Weinberger 	up_write(&ubi->fm_sem);
1543dbb7d2a8SRichard Weinberger 	up_write(&ubi->work_sem);
1544dbb7d2a8SRichard Weinberger 
1545dbb7d2a8SRichard Weinberger 	if (ret)
1546dbb7d2a8SRichard Weinberger 		goto err;
1547dbb7d2a8SRichard Weinberger 
1548dbb7d2a8SRichard Weinberger out_unlock:
1549dbb7d2a8SRichard Weinberger 	mutex_unlock(&ubi->fm_mutex);
1550dbb7d2a8SRichard Weinberger 	kfree(old_fm);
1551dbb7d2a8SRichard Weinberger 	return ret;
1552dbb7d2a8SRichard Weinberger 
1553dbb7d2a8SRichard Weinberger err:
1554dbb7d2a8SRichard Weinberger 	kfree(new_fm);
1555dbb7d2a8SRichard Weinberger 
1556dbb7d2a8SRichard Weinberger 	ubi_warn("Unable to write new fastmap, err=%i", ret);
1557dbb7d2a8SRichard Weinberger 
1558dbb7d2a8SRichard Weinberger 	ret = 0;
1559dbb7d2a8SRichard Weinberger 	if (old_fm) {
1560dbb7d2a8SRichard Weinberger 		ret = invalidate_fastmap(ubi, old_fm);
1561dbb7d2a8SRichard Weinberger 		if (ret < 0)
1562dbb7d2a8SRichard Weinberger 			ubi_err("Unable to invalidiate current fastmap!");
1563dbb7d2a8SRichard Weinberger 		else if (ret)
1564dbb7d2a8SRichard Weinberger 			ret = 0;
1565dbb7d2a8SRichard Weinberger 	}
1566dbb7d2a8SRichard Weinberger 	goto out_unlock;
1567dbb7d2a8SRichard Weinberger }
1568