xref: /openbmc/linux/drivers/mtd/ubi/fastmap.c (revision 6a059abdbae7b6b35ddc69f6336f7afc1ce79bac)
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 
128dbb7d2a8SRichard Weinberger 		if (vol_id > av->vol_id)
129dbb7d2a8SRichard Weinberger 			p = &(*p)->rb_left;
130dbb7d2a8SRichard Weinberger 		else if (vol_id > av->vol_id)
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;
410dbb7d2a8SRichard Weinberger 
411dbb7d2a8SRichard Weinberger 		pnum = be32_to_cpu(pebs[i]);
412dbb7d2a8SRichard Weinberger 
413dbb7d2a8SRichard Weinberger 		if (ubi_io_is_bad(ubi, pnum)) {
414dbb7d2a8SRichard Weinberger 			ubi_err("bad PEB in fastmap pool!");
415dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
416dbb7d2a8SRichard Weinberger 			goto out;
417dbb7d2a8SRichard Weinberger 		}
418dbb7d2a8SRichard Weinberger 
419dbb7d2a8SRichard Weinberger 		err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
420dbb7d2a8SRichard Weinberger 		if (err && err != UBI_IO_BITFLIPS) {
421dbb7d2a8SRichard Weinberger 			ubi_err("unable to read EC header! PEB:%i err:%i",
422dbb7d2a8SRichard Weinberger 				pnum, err);
423dbb7d2a8SRichard Weinberger 			ret = err > 0 ? UBI_BAD_FASTMAP : err;
424dbb7d2a8SRichard Weinberger 			goto out;
425dbb7d2a8SRichard Weinberger 		} else if (ret == UBI_IO_BITFLIPS)
426dbb7d2a8SRichard Weinberger 			scrub = 1;
427dbb7d2a8SRichard Weinberger 
428dbb7d2a8SRichard Weinberger 		if (be32_to_cpu(ech->image_seq) != ubi->image_seq) {
429dbb7d2a8SRichard Weinberger 			ubi_err("bad image seq: 0x%x, expected: 0x%x",
430dbb7d2a8SRichard Weinberger 				be32_to_cpu(ech->image_seq), ubi->image_seq);
431dbb7d2a8SRichard Weinberger 			err = UBI_BAD_FASTMAP;
432dbb7d2a8SRichard Weinberger 			goto out;
433dbb7d2a8SRichard Weinberger 		}
434dbb7d2a8SRichard Weinberger 
435dbb7d2a8SRichard Weinberger 		err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
436dbb7d2a8SRichard Weinberger 		if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
437dbb7d2a8SRichard Weinberger 			unsigned long long ec = be64_to_cpu(ech->ec);
438dbb7d2a8SRichard Weinberger 			unmap_peb(ai, pnum);
439dbb7d2a8SRichard Weinberger 			dbg_bld("Adding PEB to free: %i", pnum);
440dbb7d2a8SRichard Weinberger 			if (err == UBI_IO_FF_BITFLIPS)
441dbb7d2a8SRichard Weinberger 				add_aeb(ai, free, pnum, ec, 1);
442dbb7d2a8SRichard Weinberger 			else
443dbb7d2a8SRichard Weinberger 				add_aeb(ai, free, pnum, ec, 0);
444dbb7d2a8SRichard Weinberger 			continue;
445dbb7d2a8SRichard Weinberger 		} else if (err == 0 || err == UBI_IO_BITFLIPS) {
446dbb7d2a8SRichard Weinberger 			dbg_bld("Found non empty PEB:%i in pool", pnum);
447dbb7d2a8SRichard Weinberger 
448dbb7d2a8SRichard Weinberger 			if (err == UBI_IO_BITFLIPS)
449dbb7d2a8SRichard Weinberger 				scrub = 1;
450dbb7d2a8SRichard Weinberger 
451dbb7d2a8SRichard Weinberger 			found_orphan = 0;
452dbb7d2a8SRichard Weinberger 			list_for_each_entry(tmp_aeb, eba_orphans, u.list) {
453dbb7d2a8SRichard Weinberger 				if (tmp_aeb->pnum == pnum) {
454dbb7d2a8SRichard Weinberger 					found_orphan = 1;
455dbb7d2a8SRichard Weinberger 					break;
456dbb7d2a8SRichard Weinberger 				}
457dbb7d2a8SRichard Weinberger 			}
458dbb7d2a8SRichard Weinberger 			if (found_orphan) {
459dbb7d2a8SRichard Weinberger 				kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
460dbb7d2a8SRichard Weinberger 				list_del(&tmp_aeb->u.list);
461dbb7d2a8SRichard Weinberger 			}
462dbb7d2a8SRichard Weinberger 
463dbb7d2a8SRichard Weinberger 			new_aeb = kmem_cache_alloc(ai->aeb_slab_cache,
464dbb7d2a8SRichard Weinberger 						   GFP_KERNEL);
465dbb7d2a8SRichard Weinberger 			if (!new_aeb) {
466dbb7d2a8SRichard Weinberger 				ret = -ENOMEM;
467dbb7d2a8SRichard Weinberger 				goto out;
468dbb7d2a8SRichard Weinberger 			}
469dbb7d2a8SRichard Weinberger 
470dbb7d2a8SRichard Weinberger 			new_aeb->ec = be64_to_cpu(ech->ec);
471dbb7d2a8SRichard Weinberger 			new_aeb->pnum = pnum;
472dbb7d2a8SRichard Weinberger 			new_aeb->lnum = be32_to_cpu(vh->lnum);
473dbb7d2a8SRichard Weinberger 			new_aeb->sqnum = be64_to_cpu(vh->sqnum);
474dbb7d2a8SRichard Weinberger 			new_aeb->copy_flag = vh->copy_flag;
475dbb7d2a8SRichard Weinberger 			new_aeb->scrub = scrub;
476dbb7d2a8SRichard Weinberger 
477dbb7d2a8SRichard Weinberger 			if (*max_sqnum < new_aeb->sqnum)
478dbb7d2a8SRichard Weinberger 				*max_sqnum = new_aeb->sqnum;
479dbb7d2a8SRichard Weinberger 
480dbb7d2a8SRichard Weinberger 			err = process_pool_aeb(ubi, ai, vh, new_aeb);
481dbb7d2a8SRichard Weinberger 			if (err) {
482dbb7d2a8SRichard Weinberger 				ret = err > 0 ? UBI_BAD_FASTMAP : err;
483dbb7d2a8SRichard Weinberger 				goto out;
484dbb7d2a8SRichard Weinberger 			}
485dbb7d2a8SRichard Weinberger 		} else {
486dbb7d2a8SRichard Weinberger 			/* We are paranoid and fall back to scanning mode */
487dbb7d2a8SRichard Weinberger 			ubi_err("fastmap pool PEBs contains damaged PEBs!");
488dbb7d2a8SRichard Weinberger 			ret = err > 0 ? UBI_BAD_FASTMAP : err;
489dbb7d2a8SRichard Weinberger 			goto out;
490dbb7d2a8SRichard Weinberger 		}
491dbb7d2a8SRichard Weinberger 
492dbb7d2a8SRichard Weinberger 	}
493dbb7d2a8SRichard Weinberger 
494dbb7d2a8SRichard Weinberger out:
495dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, vh);
496dbb7d2a8SRichard Weinberger 	kfree(ech);
497dbb7d2a8SRichard Weinberger 	return ret;
498dbb7d2a8SRichard Weinberger }
499dbb7d2a8SRichard Weinberger 
500dbb7d2a8SRichard Weinberger /**
501dbb7d2a8SRichard Weinberger  * count_fastmap_pebs - Counts the PEBs found by fastmap.
502dbb7d2a8SRichard Weinberger  * @ai: The UBI attach info object
503dbb7d2a8SRichard Weinberger  */
504dbb7d2a8SRichard Weinberger static int count_fastmap_pebs(struct ubi_attach_info *ai)
505dbb7d2a8SRichard Weinberger {
506dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb;
507dbb7d2a8SRichard Weinberger 	struct ubi_ainf_volume *av;
508dbb7d2a8SRichard Weinberger 	struct rb_node *rb1, *rb2;
509dbb7d2a8SRichard Weinberger 	int n = 0;
510dbb7d2a8SRichard Weinberger 
511dbb7d2a8SRichard Weinberger 	list_for_each_entry(aeb, &ai->erase, u.list)
512dbb7d2a8SRichard Weinberger 		n++;
513dbb7d2a8SRichard Weinberger 
514dbb7d2a8SRichard Weinberger 	list_for_each_entry(aeb, &ai->free, u.list)
515dbb7d2a8SRichard Weinberger 		n++;
516dbb7d2a8SRichard Weinberger 
517dbb7d2a8SRichard Weinberger 	 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
518dbb7d2a8SRichard Weinberger 		ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
519dbb7d2a8SRichard Weinberger 			n++;
520dbb7d2a8SRichard Weinberger 
521dbb7d2a8SRichard Weinberger 	return n;
522dbb7d2a8SRichard Weinberger }
523dbb7d2a8SRichard Weinberger 
524dbb7d2a8SRichard Weinberger /**
525dbb7d2a8SRichard Weinberger  * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
526dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
527dbb7d2a8SRichard Weinberger  * @ai: UBI attach info object
528dbb7d2a8SRichard Weinberger  * @fm: the fastmap to be attached
529dbb7d2a8SRichard Weinberger  *
530dbb7d2a8SRichard Weinberger  * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
531dbb7d2a8SRichard Weinberger  * < 0 indicates an internal error.
532dbb7d2a8SRichard Weinberger  */
533dbb7d2a8SRichard Weinberger static int ubi_attach_fastmap(struct ubi_device *ubi,
534dbb7d2a8SRichard Weinberger 			      struct ubi_attach_info *ai,
535dbb7d2a8SRichard Weinberger 			      struct ubi_fastmap_layout *fm)
536dbb7d2a8SRichard Weinberger {
537dbb7d2a8SRichard Weinberger 	struct list_head used, eba_orphans, free;
538dbb7d2a8SRichard Weinberger 	struct ubi_ainf_volume *av;
539dbb7d2a8SRichard Weinberger 	struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
540dbb7d2a8SRichard Weinberger 	struct ubi_ec_hdr *ech;
541dbb7d2a8SRichard Weinberger 	struct ubi_fm_sb *fmsb;
542dbb7d2a8SRichard Weinberger 	struct ubi_fm_hdr *fmhdr;
543dbb7d2a8SRichard Weinberger 	struct ubi_fm_scan_pool *fmpl1, *fmpl2;
544dbb7d2a8SRichard Weinberger 	struct ubi_fm_ec *fmec;
545dbb7d2a8SRichard Weinberger 	struct ubi_fm_volhdr *fmvhdr;
546dbb7d2a8SRichard Weinberger 	struct ubi_fm_eba *fm_eba;
547dbb7d2a8SRichard Weinberger 	int ret, i, j, pool_size, wl_pool_size;
548dbb7d2a8SRichard Weinberger 	size_t fm_pos = 0, fm_size = ubi->fm_size;
549dbb7d2a8SRichard Weinberger 	unsigned long long max_sqnum = 0;
550dbb7d2a8SRichard Weinberger 	void *fm_raw = ubi->fm_buf;
551dbb7d2a8SRichard Weinberger 
552dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&used);
553dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&free);
554dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&eba_orphans);
555dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&ai->corr);
556dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&ai->free);
557dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&ai->erase);
558dbb7d2a8SRichard Weinberger 	INIT_LIST_HEAD(&ai->alien);
559dbb7d2a8SRichard Weinberger 	ai->volumes = RB_ROOT;
560dbb7d2a8SRichard Weinberger 	ai->min_ec = UBI_MAX_ERASECOUNTER;
561dbb7d2a8SRichard Weinberger 
562dbb7d2a8SRichard Weinberger 	ai->aeb_slab_cache = kmem_cache_create("ubi_ainf_peb_slab",
563dbb7d2a8SRichard Weinberger 					       sizeof(struct ubi_ainf_peb),
564dbb7d2a8SRichard Weinberger 					       0, 0, NULL);
565dbb7d2a8SRichard Weinberger 	if (!ai->aeb_slab_cache) {
566dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
567dbb7d2a8SRichard Weinberger 		goto fail;
568dbb7d2a8SRichard Weinberger 	}
569dbb7d2a8SRichard Weinberger 
570dbb7d2a8SRichard Weinberger 	fmsb = (struct ubi_fm_sb *)(fm_raw);
571dbb7d2a8SRichard Weinberger 	ai->max_sqnum = fmsb->sqnum;
572dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(struct ubi_fm_sb);
573dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
574dbb7d2a8SRichard Weinberger 		goto fail_bad;
575dbb7d2a8SRichard Weinberger 
576dbb7d2a8SRichard Weinberger 	fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
577dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmhdr);
578dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
579dbb7d2a8SRichard Weinberger 		goto fail_bad;
580dbb7d2a8SRichard Weinberger 
581dbb7d2a8SRichard Weinberger 	if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
582dbb7d2a8SRichard Weinberger 		ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
583dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
584dbb7d2a8SRichard Weinberger 		goto fail_bad;
585dbb7d2a8SRichard Weinberger 	}
586dbb7d2a8SRichard Weinberger 
587dbb7d2a8SRichard Weinberger 	fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
588dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmpl1);
589dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
590dbb7d2a8SRichard Weinberger 		goto fail_bad;
591dbb7d2a8SRichard Weinberger 	if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
592dbb7d2a8SRichard Weinberger 		ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
593dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
594dbb7d2a8SRichard Weinberger 		goto fail_bad;
595dbb7d2a8SRichard Weinberger 	}
596dbb7d2a8SRichard Weinberger 
597dbb7d2a8SRichard Weinberger 	fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
598dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmpl2);
599dbb7d2a8SRichard Weinberger 	if (fm_pos >= fm_size)
600dbb7d2a8SRichard Weinberger 		goto fail_bad;
601dbb7d2a8SRichard Weinberger 	if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
602dbb7d2a8SRichard Weinberger 		ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
603dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
604dbb7d2a8SRichard Weinberger 		goto fail_bad;
605dbb7d2a8SRichard Weinberger 	}
606dbb7d2a8SRichard Weinberger 
607dbb7d2a8SRichard Weinberger 	pool_size = be16_to_cpu(fmpl1->size);
608dbb7d2a8SRichard Weinberger 	wl_pool_size = be16_to_cpu(fmpl2->size);
609dbb7d2a8SRichard Weinberger 	fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
610dbb7d2a8SRichard Weinberger 	fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
611dbb7d2a8SRichard Weinberger 
612dbb7d2a8SRichard Weinberger 	if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
613dbb7d2a8SRichard Weinberger 		ubi_err("bad pool size: %i", pool_size);
614dbb7d2a8SRichard Weinberger 		goto fail_bad;
615dbb7d2a8SRichard Weinberger 	}
616dbb7d2a8SRichard Weinberger 
617dbb7d2a8SRichard Weinberger 	if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
618dbb7d2a8SRichard Weinberger 		ubi_err("bad WL pool size: %i", wl_pool_size);
619dbb7d2a8SRichard Weinberger 		goto fail_bad;
620dbb7d2a8SRichard Weinberger 	}
621dbb7d2a8SRichard Weinberger 
622dbb7d2a8SRichard Weinberger 
623dbb7d2a8SRichard Weinberger 	if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
624dbb7d2a8SRichard Weinberger 	    fm->max_pool_size < 0) {
625dbb7d2a8SRichard Weinberger 		ubi_err("bad maximal pool size: %i", fm->max_pool_size);
626dbb7d2a8SRichard Weinberger 		goto fail_bad;
627dbb7d2a8SRichard Weinberger 	}
628dbb7d2a8SRichard Weinberger 
629dbb7d2a8SRichard Weinberger 	if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
630dbb7d2a8SRichard Weinberger 	    fm->max_wl_pool_size < 0) {
631dbb7d2a8SRichard Weinberger 		ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
632dbb7d2a8SRichard Weinberger 		goto fail_bad;
633dbb7d2a8SRichard Weinberger 	}
634dbb7d2a8SRichard Weinberger 
635dbb7d2a8SRichard Weinberger 	/* read EC values from free list */
636dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
637dbb7d2a8SRichard Weinberger 		fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
638dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmec);
639dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
640dbb7d2a8SRichard Weinberger 			goto fail_bad;
641dbb7d2a8SRichard Weinberger 
642dbb7d2a8SRichard Weinberger 		add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
643dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmec->ec), 0);
644dbb7d2a8SRichard Weinberger 	}
645dbb7d2a8SRichard Weinberger 
646dbb7d2a8SRichard Weinberger 	/* read EC values from used list */
647dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->used_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, &used, be32_to_cpu(fmec->pnum),
654dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmec->ec), 0);
655dbb7d2a8SRichard Weinberger 	}
656dbb7d2a8SRichard Weinberger 
657dbb7d2a8SRichard Weinberger 	/* read EC values from scrub list */
658dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->scrub_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), 1);
666dbb7d2a8SRichard Weinberger 	}
667dbb7d2a8SRichard Weinberger 
668dbb7d2a8SRichard Weinberger 	/* read EC values from erase list */
669dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->erase_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, &ai->erase, be32_to_cpu(fmec->pnum),
676dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmec->ec), 1);
677dbb7d2a8SRichard Weinberger 	}
678dbb7d2a8SRichard Weinberger 
679dbb7d2a8SRichard Weinberger 	ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
680dbb7d2a8SRichard Weinberger 	ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
681dbb7d2a8SRichard Weinberger 
682dbb7d2a8SRichard Weinberger 	/* Iterate over all volumes and read their EBA table */
683dbb7d2a8SRichard Weinberger 	for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
684dbb7d2a8SRichard Weinberger 		fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
685dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fmvhdr);
686dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
687dbb7d2a8SRichard Weinberger 			goto fail_bad;
688dbb7d2a8SRichard Weinberger 
689dbb7d2a8SRichard Weinberger 		if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
690dbb7d2a8SRichard Weinberger 			ubi_err("bad fastmap vol header magic: 0x%x, " \
691dbb7d2a8SRichard Weinberger 				"expected: 0x%x",
692dbb7d2a8SRichard Weinberger 				be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
693dbb7d2a8SRichard Weinberger 			goto fail_bad;
694dbb7d2a8SRichard Weinberger 		}
695dbb7d2a8SRichard Weinberger 
696dbb7d2a8SRichard Weinberger 		av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
697dbb7d2a8SRichard Weinberger 			     be32_to_cpu(fmvhdr->used_ebs),
698dbb7d2a8SRichard Weinberger 			     be32_to_cpu(fmvhdr->data_pad),
699dbb7d2a8SRichard Weinberger 			     fmvhdr->vol_type,
700dbb7d2a8SRichard Weinberger 			     be32_to_cpu(fmvhdr->last_eb_bytes));
701dbb7d2a8SRichard Weinberger 
702dbb7d2a8SRichard Weinberger 		if (!av)
703dbb7d2a8SRichard Weinberger 			goto fail_bad;
704dbb7d2a8SRichard Weinberger 
705dbb7d2a8SRichard Weinberger 		ai->vols_found++;
706dbb7d2a8SRichard Weinberger 		if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
707dbb7d2a8SRichard Weinberger 			ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
708dbb7d2a8SRichard Weinberger 
709dbb7d2a8SRichard Weinberger 		fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
710dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fm_eba);
711dbb7d2a8SRichard Weinberger 		fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
712dbb7d2a8SRichard Weinberger 		if (fm_pos >= fm_size)
713dbb7d2a8SRichard Weinberger 			goto fail_bad;
714dbb7d2a8SRichard Weinberger 
715dbb7d2a8SRichard Weinberger 		if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
716dbb7d2a8SRichard Weinberger 			ubi_err("bad fastmap EBA header magic: 0x%x, " \
717dbb7d2a8SRichard Weinberger 				"expected: 0x%x",
718dbb7d2a8SRichard Weinberger 				be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
719dbb7d2a8SRichard Weinberger 			goto fail_bad;
720dbb7d2a8SRichard Weinberger 		}
721dbb7d2a8SRichard Weinberger 
722dbb7d2a8SRichard Weinberger 		for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
723dbb7d2a8SRichard Weinberger 			int pnum = be32_to_cpu(fm_eba->pnum[j]);
724dbb7d2a8SRichard Weinberger 
725dbb7d2a8SRichard Weinberger 			if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
726dbb7d2a8SRichard Weinberger 				continue;
727dbb7d2a8SRichard Weinberger 
728dbb7d2a8SRichard Weinberger 			aeb = NULL;
729dbb7d2a8SRichard Weinberger 			list_for_each_entry(tmp_aeb, &used, u.list) {
730dbb7d2a8SRichard Weinberger 				if (tmp_aeb->pnum == pnum)
731dbb7d2a8SRichard Weinberger 					aeb = tmp_aeb;
732dbb7d2a8SRichard Weinberger 			}
733dbb7d2a8SRichard Weinberger 
734dbb7d2a8SRichard Weinberger 			/* This can happen if a PEB is already in an EBA known
735dbb7d2a8SRichard Weinberger 			 * by this fastmap but the PEB itself is not in the used
736dbb7d2a8SRichard Weinberger 			 * list.
737dbb7d2a8SRichard Weinberger 			 * In this case the PEB can be within the fastmap pool
738dbb7d2a8SRichard Weinberger 			 * or while writing the fastmap it was in the protection
739dbb7d2a8SRichard Weinberger 			 * queue.
740dbb7d2a8SRichard Weinberger 			 */
741dbb7d2a8SRichard Weinberger 			if (!aeb) {
742dbb7d2a8SRichard Weinberger 				aeb = kmem_cache_alloc(ai->aeb_slab_cache,
743dbb7d2a8SRichard Weinberger 						       GFP_KERNEL);
744dbb7d2a8SRichard Weinberger 				if (!aeb) {
745dbb7d2a8SRichard Weinberger 					ret = -ENOMEM;
746dbb7d2a8SRichard Weinberger 
747dbb7d2a8SRichard Weinberger 					goto fail;
748dbb7d2a8SRichard Weinberger 				}
749dbb7d2a8SRichard Weinberger 
750dbb7d2a8SRichard Weinberger 				aeb->lnum = j;
751dbb7d2a8SRichard Weinberger 				aeb->pnum = be32_to_cpu(fm_eba->pnum[j]);
752dbb7d2a8SRichard Weinberger 				aeb->ec = -1;
753dbb7d2a8SRichard Weinberger 				aeb->scrub = aeb->copy_flag = aeb->sqnum = 0;
754dbb7d2a8SRichard Weinberger 				list_add_tail(&aeb->u.list, &eba_orphans);
755dbb7d2a8SRichard Weinberger 				continue;
756dbb7d2a8SRichard Weinberger 			}
757dbb7d2a8SRichard Weinberger 
758dbb7d2a8SRichard Weinberger 			aeb->lnum = j;
759dbb7d2a8SRichard Weinberger 
760dbb7d2a8SRichard Weinberger 			if (av->highest_lnum <= aeb->lnum)
761dbb7d2a8SRichard Weinberger 				av->highest_lnum = aeb->lnum;
762dbb7d2a8SRichard Weinberger 
763dbb7d2a8SRichard Weinberger 			assign_aeb_to_av(ai, aeb, av);
764dbb7d2a8SRichard Weinberger 
765dbb7d2a8SRichard Weinberger 			dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
766dbb7d2a8SRichard Weinberger 				aeb->pnum, aeb->lnum, av->vol_id);
767dbb7d2a8SRichard Weinberger 		}
768dbb7d2a8SRichard Weinberger 
769dbb7d2a8SRichard Weinberger 		ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
770dbb7d2a8SRichard Weinberger 		if (!ech) {
771dbb7d2a8SRichard Weinberger 			ret = -ENOMEM;
772dbb7d2a8SRichard Weinberger 			goto fail;
773dbb7d2a8SRichard Weinberger 		}
774dbb7d2a8SRichard Weinberger 
775dbb7d2a8SRichard Weinberger 		list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &eba_orphans,
776dbb7d2a8SRichard Weinberger 					 u.list) {
777dbb7d2a8SRichard Weinberger 			int err;
778dbb7d2a8SRichard Weinberger 
779dbb7d2a8SRichard Weinberger 			if (ubi_io_is_bad(ubi, tmp_aeb->pnum)) {
780dbb7d2a8SRichard Weinberger 				ubi_err("bad PEB in fastmap EBA orphan list");
781dbb7d2a8SRichard Weinberger 				ret = UBI_BAD_FASTMAP;
782dbb7d2a8SRichard Weinberger 				kfree(ech);
783dbb7d2a8SRichard Weinberger 				goto fail;
784dbb7d2a8SRichard Weinberger 			}
785dbb7d2a8SRichard Weinberger 
786dbb7d2a8SRichard Weinberger 			err = ubi_io_read_ec_hdr(ubi, tmp_aeb->pnum, ech, 0);
787dbb7d2a8SRichard Weinberger 			if (err && err != UBI_IO_BITFLIPS) {
788dbb7d2a8SRichard Weinberger 				ubi_err("unable to read EC header! PEB:%i " \
789dbb7d2a8SRichard Weinberger 					"err:%i", tmp_aeb->pnum, err);
790dbb7d2a8SRichard Weinberger 				ret = err > 0 ? UBI_BAD_FASTMAP : err;
791dbb7d2a8SRichard Weinberger 				kfree(ech);
792dbb7d2a8SRichard Weinberger 
793dbb7d2a8SRichard Weinberger 				goto fail;
794dbb7d2a8SRichard Weinberger 			} else if (err == UBI_IO_BITFLIPS)
795dbb7d2a8SRichard Weinberger 				tmp_aeb->scrub = 1;
796dbb7d2a8SRichard Weinberger 
797dbb7d2a8SRichard Weinberger 			tmp_aeb->ec = be64_to_cpu(ech->ec);
798dbb7d2a8SRichard Weinberger 			assign_aeb_to_av(ai, tmp_aeb, av);
799dbb7d2a8SRichard Weinberger 		}
800dbb7d2a8SRichard Weinberger 
801dbb7d2a8SRichard Weinberger 		kfree(ech);
802dbb7d2a8SRichard Weinberger 	}
803dbb7d2a8SRichard Weinberger 
804dbb7d2a8SRichard Weinberger 	ret = scan_pool(ubi, ai, fmpl1->pebs, pool_size, &max_sqnum,
805dbb7d2a8SRichard Weinberger 			&eba_orphans, &free);
806dbb7d2a8SRichard Weinberger 	if (ret)
807dbb7d2a8SRichard Weinberger 		goto fail;
808dbb7d2a8SRichard Weinberger 
809dbb7d2a8SRichard Weinberger 	ret = scan_pool(ubi, ai, fmpl2->pebs, wl_pool_size, &max_sqnum,
810dbb7d2a8SRichard Weinberger 			&eba_orphans, &free);
811dbb7d2a8SRichard Weinberger 	if (ret)
812dbb7d2a8SRichard Weinberger 		goto fail;
813dbb7d2a8SRichard Weinberger 
814dbb7d2a8SRichard Weinberger 	if (max_sqnum > ai->max_sqnum)
815dbb7d2a8SRichard Weinberger 		ai->max_sqnum = max_sqnum;
816dbb7d2a8SRichard Weinberger 
817*6a059abdSWei Yongjun 	list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
818*6a059abdSWei Yongjun 		list_move_tail(&tmp_aeb->u.list, &ai->free);
819dbb7d2a8SRichard Weinberger 
820dbb7d2a8SRichard Weinberger 	/*
821dbb7d2a8SRichard Weinberger 	 * If fastmap is leaking PEBs (must not happen), raise a
822dbb7d2a8SRichard Weinberger 	 * fat warning and fall back to scanning mode.
823dbb7d2a8SRichard Weinberger 	 * We do this here because in ubi_wl_init() it's too late
824dbb7d2a8SRichard Weinberger 	 * and we cannot fall back to scanning.
825dbb7d2a8SRichard Weinberger 	 */
826dbb7d2a8SRichard Weinberger 	if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
827dbb7d2a8SRichard Weinberger 		    ai->bad_peb_count - fm->used_blocks))
828dbb7d2a8SRichard Weinberger 		goto fail_bad;
829dbb7d2a8SRichard Weinberger 
830dbb7d2a8SRichard Weinberger 	return 0;
831dbb7d2a8SRichard Weinberger 
832dbb7d2a8SRichard Weinberger fail_bad:
833dbb7d2a8SRichard Weinberger 	ret = UBI_BAD_FASTMAP;
834dbb7d2a8SRichard Weinberger fail:
835dbb7d2a8SRichard Weinberger 	return ret;
836dbb7d2a8SRichard Weinberger }
837dbb7d2a8SRichard Weinberger 
838dbb7d2a8SRichard Weinberger /**
839dbb7d2a8SRichard Weinberger  * ubi_scan_fastmap - scan the fastmap.
840dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
841dbb7d2a8SRichard Weinberger  * @ai: UBI attach info to be filled
842dbb7d2a8SRichard Weinberger  * @fm_anchor: The fastmap starts at this PEB
843dbb7d2a8SRichard Weinberger  *
844dbb7d2a8SRichard Weinberger  * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
845dbb7d2a8SRichard Weinberger  * UBI_BAD_FASTMAP if one was found but is not usable.
846dbb7d2a8SRichard Weinberger  * < 0 indicates an internal error.
847dbb7d2a8SRichard Weinberger  */
848dbb7d2a8SRichard Weinberger int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
849dbb7d2a8SRichard Weinberger 		     int fm_anchor)
850dbb7d2a8SRichard Weinberger {
851dbb7d2a8SRichard Weinberger 	struct ubi_fm_sb *fmsb, *fmsb2;
852dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *vh;
853dbb7d2a8SRichard Weinberger 	struct ubi_ec_hdr *ech;
854dbb7d2a8SRichard Weinberger 	struct ubi_fastmap_layout *fm;
855dbb7d2a8SRichard Weinberger 	int i, used_blocks, pnum, ret = 0;
856dbb7d2a8SRichard Weinberger 	size_t fm_size;
857dbb7d2a8SRichard Weinberger 	__be32 crc, tmp_crc;
858dbb7d2a8SRichard Weinberger 	unsigned long long sqnum = 0;
859dbb7d2a8SRichard Weinberger 
860dbb7d2a8SRichard Weinberger 	mutex_lock(&ubi->fm_mutex);
861dbb7d2a8SRichard Weinberger 	memset(ubi->fm_buf, 0, ubi->fm_size);
862dbb7d2a8SRichard Weinberger 
863dbb7d2a8SRichard Weinberger 	fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
864dbb7d2a8SRichard Weinberger 	if (!fmsb) {
865dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
866dbb7d2a8SRichard Weinberger 		goto out;
867dbb7d2a8SRichard Weinberger 	}
868dbb7d2a8SRichard Weinberger 
869dbb7d2a8SRichard Weinberger 	fm = kzalloc(sizeof(*fm), GFP_KERNEL);
870dbb7d2a8SRichard Weinberger 	if (!fm) {
871dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
872dbb7d2a8SRichard Weinberger 		kfree(fmsb);
873dbb7d2a8SRichard Weinberger 		goto out;
874dbb7d2a8SRichard Weinberger 	}
875dbb7d2a8SRichard Weinberger 
876dbb7d2a8SRichard Weinberger 	ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
877dbb7d2a8SRichard Weinberger 	if (ret && ret != UBI_IO_BITFLIPS)
878dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
879dbb7d2a8SRichard Weinberger 	else if (ret == UBI_IO_BITFLIPS)
880dbb7d2a8SRichard Weinberger 		fm->to_be_tortured[0] = 1;
881dbb7d2a8SRichard Weinberger 
882dbb7d2a8SRichard Weinberger 	if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
883dbb7d2a8SRichard Weinberger 		ubi_err("bad super block magic: 0x%x, expected: 0x%x",
884dbb7d2a8SRichard Weinberger 			be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
885dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
886dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
887dbb7d2a8SRichard Weinberger 	}
888dbb7d2a8SRichard Weinberger 
889dbb7d2a8SRichard Weinberger 	if (fmsb->version != UBI_FM_FMT_VERSION) {
890dbb7d2a8SRichard Weinberger 		ubi_err("bad fastmap version: %i, expected: %i",
891dbb7d2a8SRichard Weinberger 			fmsb->version, UBI_FM_FMT_VERSION);
892dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
893dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
894dbb7d2a8SRichard Weinberger 	}
895dbb7d2a8SRichard Weinberger 
896dbb7d2a8SRichard Weinberger 	used_blocks = be32_to_cpu(fmsb->used_blocks);
897dbb7d2a8SRichard Weinberger 	if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
898dbb7d2a8SRichard Weinberger 		ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
899dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
900dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
901dbb7d2a8SRichard Weinberger 	}
902dbb7d2a8SRichard Weinberger 
903dbb7d2a8SRichard Weinberger 	fm_size = ubi->leb_size * used_blocks;
904dbb7d2a8SRichard Weinberger 	if (fm_size != ubi->fm_size) {
905dbb7d2a8SRichard Weinberger 		ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
906dbb7d2a8SRichard Weinberger 			ubi->fm_size);
907dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
908dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
909dbb7d2a8SRichard Weinberger 	}
910dbb7d2a8SRichard Weinberger 
911dbb7d2a8SRichard Weinberger 	ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
912dbb7d2a8SRichard Weinberger 	if (!ech) {
913dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
914dbb7d2a8SRichard Weinberger 		goto free_fm_sb;
915dbb7d2a8SRichard Weinberger 	}
916dbb7d2a8SRichard Weinberger 
917dbb7d2a8SRichard Weinberger 	vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
918dbb7d2a8SRichard Weinberger 	if (!vh) {
919dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
920dbb7d2a8SRichard Weinberger 		goto free_hdr;
921dbb7d2a8SRichard Weinberger 	}
922dbb7d2a8SRichard Weinberger 
923dbb7d2a8SRichard Weinberger 	for (i = 0; i < used_blocks; i++) {
924dbb7d2a8SRichard Weinberger 		pnum = be32_to_cpu(fmsb->block_loc[i]);
925dbb7d2a8SRichard Weinberger 
926dbb7d2a8SRichard Weinberger 		if (ubi_io_is_bad(ubi, pnum)) {
927dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
928dbb7d2a8SRichard Weinberger 			goto free_hdr;
929dbb7d2a8SRichard Weinberger 		}
930dbb7d2a8SRichard Weinberger 
931dbb7d2a8SRichard Weinberger 		ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
932dbb7d2a8SRichard Weinberger 		if (ret && ret != UBI_IO_BITFLIPS) {
933dbb7d2a8SRichard Weinberger 			ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
934dbb7d2a8SRichard Weinberger 				i, pnum);
935dbb7d2a8SRichard Weinberger 			if (ret > 0)
936dbb7d2a8SRichard Weinberger 				ret = UBI_BAD_FASTMAP;
937dbb7d2a8SRichard Weinberger 			goto free_hdr;
938dbb7d2a8SRichard Weinberger 		} else if (ret == UBI_IO_BITFLIPS)
939dbb7d2a8SRichard Weinberger 			fm->to_be_tortured[i] = 1;
940dbb7d2a8SRichard Weinberger 
941dbb7d2a8SRichard Weinberger 		if (!ubi->image_seq)
942dbb7d2a8SRichard Weinberger 			ubi->image_seq = be32_to_cpu(ech->image_seq);
943dbb7d2a8SRichard Weinberger 
944dbb7d2a8SRichard Weinberger 		if (be32_to_cpu(ech->image_seq) != ubi->image_seq) {
945dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
946dbb7d2a8SRichard Weinberger 			goto free_hdr;
947dbb7d2a8SRichard Weinberger 		}
948dbb7d2a8SRichard Weinberger 
949dbb7d2a8SRichard Weinberger 		ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
950dbb7d2a8SRichard Weinberger 		if (ret && ret != UBI_IO_BITFLIPS) {
951dbb7d2a8SRichard Weinberger 			ubi_err("unable to read fastmap block# %i (PEB: %i)",
952dbb7d2a8SRichard Weinberger 				i, pnum);
953dbb7d2a8SRichard Weinberger 			goto free_hdr;
954dbb7d2a8SRichard Weinberger 		}
955dbb7d2a8SRichard Weinberger 
956dbb7d2a8SRichard Weinberger 		if (i == 0) {
957dbb7d2a8SRichard Weinberger 			if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
958dbb7d2a8SRichard Weinberger 				ubi_err("bad fastmap anchor vol_id: 0x%x," \
959dbb7d2a8SRichard Weinberger 					" expected: 0x%x",
960dbb7d2a8SRichard Weinberger 					be32_to_cpu(vh->vol_id),
961dbb7d2a8SRichard Weinberger 					UBI_FM_SB_VOLUME_ID);
962dbb7d2a8SRichard Weinberger 				ret = UBI_BAD_FASTMAP;
963dbb7d2a8SRichard Weinberger 				goto free_hdr;
964dbb7d2a8SRichard Weinberger 			}
965dbb7d2a8SRichard Weinberger 		} else {
966dbb7d2a8SRichard Weinberger 			if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
967dbb7d2a8SRichard Weinberger 				ubi_err("bad fastmap data vol_id: 0x%x," \
968dbb7d2a8SRichard Weinberger 					" expected: 0x%x",
969dbb7d2a8SRichard Weinberger 					be32_to_cpu(vh->vol_id),
970dbb7d2a8SRichard Weinberger 					UBI_FM_DATA_VOLUME_ID);
971dbb7d2a8SRichard Weinberger 				ret = UBI_BAD_FASTMAP;
972dbb7d2a8SRichard Weinberger 				goto free_hdr;
973dbb7d2a8SRichard Weinberger 			}
974dbb7d2a8SRichard Weinberger 		}
975dbb7d2a8SRichard Weinberger 
976dbb7d2a8SRichard Weinberger 		if (sqnum < be64_to_cpu(vh->sqnum))
977dbb7d2a8SRichard Weinberger 			sqnum = be64_to_cpu(vh->sqnum);
978dbb7d2a8SRichard Weinberger 
979dbb7d2a8SRichard Weinberger 		ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
980dbb7d2a8SRichard Weinberger 				  ubi->leb_start, ubi->leb_size);
981dbb7d2a8SRichard Weinberger 		if (ret && ret != UBI_IO_BITFLIPS) {
982dbb7d2a8SRichard Weinberger 			ubi_err("unable to read fastmap block# %i (PEB: %i, " \
983dbb7d2a8SRichard Weinberger 				"err: %i)", i, pnum, ret);
984dbb7d2a8SRichard Weinberger 			goto free_hdr;
985dbb7d2a8SRichard Weinberger 		}
986dbb7d2a8SRichard Weinberger 	}
987dbb7d2a8SRichard Weinberger 
988dbb7d2a8SRichard Weinberger 	kfree(fmsb);
989dbb7d2a8SRichard Weinberger 	fmsb = NULL;
990dbb7d2a8SRichard Weinberger 
991dbb7d2a8SRichard Weinberger 	fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
992dbb7d2a8SRichard Weinberger 	tmp_crc = be32_to_cpu(fmsb2->data_crc);
993dbb7d2a8SRichard Weinberger 	fmsb2->data_crc = 0;
994dbb7d2a8SRichard Weinberger 	crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
995dbb7d2a8SRichard Weinberger 	if (crc != tmp_crc) {
996dbb7d2a8SRichard Weinberger 		ubi_err("fastmap data CRC is invalid");
997dbb7d2a8SRichard Weinberger 		ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
998dbb7d2a8SRichard Weinberger 		ret = UBI_BAD_FASTMAP;
999dbb7d2a8SRichard Weinberger 		goto free_hdr;
1000dbb7d2a8SRichard Weinberger 	}
1001dbb7d2a8SRichard Weinberger 
1002dbb7d2a8SRichard Weinberger 	fmsb2->sqnum = sqnum;
1003dbb7d2a8SRichard Weinberger 
1004dbb7d2a8SRichard Weinberger 	fm->used_blocks = used_blocks;
1005dbb7d2a8SRichard Weinberger 
1006dbb7d2a8SRichard Weinberger 	ret = ubi_attach_fastmap(ubi, ai, fm);
1007dbb7d2a8SRichard Weinberger 	if (ret) {
1008dbb7d2a8SRichard Weinberger 		if (ret > 0)
1009dbb7d2a8SRichard Weinberger 			ret = UBI_BAD_FASTMAP;
1010dbb7d2a8SRichard Weinberger 		goto free_hdr;
1011dbb7d2a8SRichard Weinberger 	}
1012dbb7d2a8SRichard Weinberger 
1013dbb7d2a8SRichard Weinberger 	for (i = 0; i < used_blocks; i++) {
1014dbb7d2a8SRichard Weinberger 		struct ubi_wl_entry *e;
1015dbb7d2a8SRichard Weinberger 
1016dbb7d2a8SRichard Weinberger 		e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1017dbb7d2a8SRichard Weinberger 		if (!e) {
1018dbb7d2a8SRichard Weinberger 			while (i--)
1019dbb7d2a8SRichard Weinberger 				kfree(fm->e[i]);
1020dbb7d2a8SRichard Weinberger 
1021dbb7d2a8SRichard Weinberger 			ret = -ENOMEM;
1022dbb7d2a8SRichard Weinberger 			goto free_hdr;
1023dbb7d2a8SRichard Weinberger 		}
1024dbb7d2a8SRichard Weinberger 
1025dbb7d2a8SRichard Weinberger 		e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
1026dbb7d2a8SRichard Weinberger 		e->ec = be32_to_cpu(fmsb2->block_ec[i]);
1027dbb7d2a8SRichard Weinberger 		fm->e[i] = e;
1028dbb7d2a8SRichard Weinberger 	}
1029dbb7d2a8SRichard Weinberger 
1030dbb7d2a8SRichard Weinberger 	ubi->fm = fm;
1031dbb7d2a8SRichard Weinberger 	ubi->fm_pool.max_size = ubi->fm->max_pool_size;
1032dbb7d2a8SRichard Weinberger 	ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
1033dbb7d2a8SRichard Weinberger 	ubi_msg("attached by fastmap");
1034dbb7d2a8SRichard Weinberger 	ubi_msg("fastmap pool size: %d", ubi->fm_pool.max_size);
1035dbb7d2a8SRichard Weinberger 	ubi_msg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
1036dbb7d2a8SRichard Weinberger 	ubi->fm_disabled = 0;
1037dbb7d2a8SRichard Weinberger 
1038dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, vh);
1039dbb7d2a8SRichard Weinberger 	kfree(ech);
1040dbb7d2a8SRichard Weinberger out:
1041dbb7d2a8SRichard Weinberger 	mutex_unlock(&ubi->fm_mutex);
1042dbb7d2a8SRichard Weinberger 	if (ret == UBI_BAD_FASTMAP)
1043dbb7d2a8SRichard Weinberger 		ubi_err("Attach by fastmap failed, doing a full scan!");
1044dbb7d2a8SRichard Weinberger 	return ret;
1045dbb7d2a8SRichard Weinberger 
1046dbb7d2a8SRichard Weinberger free_hdr:
1047dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, vh);
1048dbb7d2a8SRichard Weinberger 	kfree(ech);
1049dbb7d2a8SRichard Weinberger free_fm_sb:
1050dbb7d2a8SRichard Weinberger 	kfree(fmsb);
1051dbb7d2a8SRichard Weinberger 	kfree(fm);
1052dbb7d2a8SRichard Weinberger 	goto out;
1053dbb7d2a8SRichard Weinberger }
1054dbb7d2a8SRichard Weinberger 
1055dbb7d2a8SRichard Weinberger /**
1056dbb7d2a8SRichard Weinberger  * ubi_write_fastmap - writes a fastmap.
1057dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1058dbb7d2a8SRichard Weinberger  * @new_fm: the to be written fastmap
1059dbb7d2a8SRichard Weinberger  *
1060dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
1061dbb7d2a8SRichard Weinberger  */
1062dbb7d2a8SRichard Weinberger static int ubi_write_fastmap(struct ubi_device *ubi,
1063dbb7d2a8SRichard Weinberger 			     struct ubi_fastmap_layout *new_fm)
1064dbb7d2a8SRichard Weinberger {
1065dbb7d2a8SRichard Weinberger 	size_t fm_pos = 0;
1066dbb7d2a8SRichard Weinberger 	void *fm_raw;
1067dbb7d2a8SRichard Weinberger 	struct ubi_fm_sb *fmsb;
1068dbb7d2a8SRichard Weinberger 	struct ubi_fm_hdr *fmh;
1069dbb7d2a8SRichard Weinberger 	struct ubi_fm_scan_pool *fmpl1, *fmpl2;
1070dbb7d2a8SRichard Weinberger 	struct ubi_fm_ec *fec;
1071dbb7d2a8SRichard Weinberger 	struct ubi_fm_volhdr *fvh;
1072dbb7d2a8SRichard Weinberger 	struct ubi_fm_eba *feba;
1073dbb7d2a8SRichard Weinberger 	struct rb_node *node;
1074dbb7d2a8SRichard Weinberger 	struct ubi_wl_entry *wl_e;
1075dbb7d2a8SRichard Weinberger 	struct ubi_volume *vol;
1076dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *avhdr, *dvhdr;
1077dbb7d2a8SRichard Weinberger 	struct ubi_work *ubi_wrk;
1078dbb7d2a8SRichard Weinberger 	int ret, i, j, free_peb_count, used_peb_count, vol_count;
1079dbb7d2a8SRichard Weinberger 	int scrub_peb_count, erase_peb_count;
1080dbb7d2a8SRichard Weinberger 
1081dbb7d2a8SRichard Weinberger 	fm_raw = ubi->fm_buf;
1082dbb7d2a8SRichard Weinberger 	memset(ubi->fm_buf, 0, ubi->fm_size);
1083dbb7d2a8SRichard Weinberger 
1084dbb7d2a8SRichard Weinberger 	avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1085dbb7d2a8SRichard Weinberger 	if (!avhdr) {
1086dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
1087dbb7d2a8SRichard Weinberger 		goto out;
1088dbb7d2a8SRichard Weinberger 	}
1089dbb7d2a8SRichard Weinberger 
1090dbb7d2a8SRichard Weinberger 	dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
1091dbb7d2a8SRichard Weinberger 	if (!dvhdr) {
1092dbb7d2a8SRichard Weinberger 		ret = -ENOMEM;
1093dbb7d2a8SRichard Weinberger 		goto out_kfree;
1094dbb7d2a8SRichard Weinberger 	}
1095dbb7d2a8SRichard Weinberger 
1096dbb7d2a8SRichard Weinberger 	spin_lock(&ubi->volumes_lock);
1097dbb7d2a8SRichard Weinberger 	spin_lock(&ubi->wl_lock);
1098dbb7d2a8SRichard Weinberger 
1099dbb7d2a8SRichard Weinberger 	fmsb = (struct ubi_fm_sb *)fm_raw;
1100dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmsb);
1101dbb7d2a8SRichard Weinberger 	ubi_assert(fm_pos <= ubi->fm_size);
1102dbb7d2a8SRichard Weinberger 
1103dbb7d2a8SRichard Weinberger 	fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
1104dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmh);
1105dbb7d2a8SRichard Weinberger 	ubi_assert(fm_pos <= ubi->fm_size);
1106dbb7d2a8SRichard Weinberger 
1107dbb7d2a8SRichard Weinberger 	fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
1108dbb7d2a8SRichard Weinberger 	fmsb->version = UBI_FM_FMT_VERSION;
1109dbb7d2a8SRichard Weinberger 	fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
1110dbb7d2a8SRichard Weinberger 	/* the max sqnum will be filled in while *reading* the fastmap */
1111dbb7d2a8SRichard Weinberger 	fmsb->sqnum = 0;
1112dbb7d2a8SRichard Weinberger 
1113dbb7d2a8SRichard Weinberger 	fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
1114dbb7d2a8SRichard Weinberger 	free_peb_count = 0;
1115dbb7d2a8SRichard Weinberger 	used_peb_count = 0;
1116dbb7d2a8SRichard Weinberger 	scrub_peb_count = 0;
1117dbb7d2a8SRichard Weinberger 	erase_peb_count = 0;
1118dbb7d2a8SRichard Weinberger 	vol_count = 0;
1119dbb7d2a8SRichard Weinberger 
1120dbb7d2a8SRichard Weinberger 	fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1121dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmpl1);
1122dbb7d2a8SRichard Weinberger 	fmpl1->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1123dbb7d2a8SRichard Weinberger 	fmpl1->size = cpu_to_be16(ubi->fm_pool.size);
1124dbb7d2a8SRichard Weinberger 	fmpl1->max_size = cpu_to_be16(ubi->fm_pool.max_size);
1125dbb7d2a8SRichard Weinberger 
1126dbb7d2a8SRichard Weinberger 	for (i = 0; i < ubi->fm_pool.size; i++)
1127dbb7d2a8SRichard Weinberger 		fmpl1->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
1128dbb7d2a8SRichard Weinberger 
1129dbb7d2a8SRichard Weinberger 	fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1130dbb7d2a8SRichard Weinberger 	fm_pos += sizeof(*fmpl2);
1131dbb7d2a8SRichard Weinberger 	fmpl2->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1132dbb7d2a8SRichard Weinberger 	fmpl2->size = cpu_to_be16(ubi->fm_wl_pool.size);
1133dbb7d2a8SRichard Weinberger 	fmpl2->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
1134dbb7d2a8SRichard Weinberger 
1135dbb7d2a8SRichard Weinberger 	for (i = 0; i < ubi->fm_wl_pool.size; i++)
1136dbb7d2a8SRichard Weinberger 		fmpl2->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
1137dbb7d2a8SRichard Weinberger 
1138dbb7d2a8SRichard Weinberger 	for (node = rb_first(&ubi->free); node; node = rb_next(node)) {
1139dbb7d2a8SRichard Weinberger 		wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1140dbb7d2a8SRichard Weinberger 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1141dbb7d2a8SRichard Weinberger 
1142dbb7d2a8SRichard Weinberger 		fec->pnum = cpu_to_be32(wl_e->pnum);
1143dbb7d2a8SRichard Weinberger 		fec->ec = cpu_to_be32(wl_e->ec);
1144dbb7d2a8SRichard Weinberger 
1145dbb7d2a8SRichard Weinberger 		free_peb_count++;
1146dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fec);
1147dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1148dbb7d2a8SRichard Weinberger 	}
1149dbb7d2a8SRichard Weinberger 	fmh->free_peb_count = cpu_to_be32(free_peb_count);
1150dbb7d2a8SRichard Weinberger 
1151dbb7d2a8SRichard Weinberger 	for (node = rb_first(&ubi->used); node; node = rb_next(node)) {
1152dbb7d2a8SRichard Weinberger 		wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1153dbb7d2a8SRichard Weinberger 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1154dbb7d2a8SRichard Weinberger 
1155dbb7d2a8SRichard Weinberger 		fec->pnum = cpu_to_be32(wl_e->pnum);
1156dbb7d2a8SRichard Weinberger 		fec->ec = cpu_to_be32(wl_e->ec);
1157dbb7d2a8SRichard Weinberger 
1158dbb7d2a8SRichard Weinberger 		used_peb_count++;
1159dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fec);
1160dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1161dbb7d2a8SRichard Weinberger 	}
1162dbb7d2a8SRichard Weinberger 	fmh->used_peb_count = cpu_to_be32(used_peb_count);
1163dbb7d2a8SRichard Weinberger 
1164dbb7d2a8SRichard Weinberger 	for (node = rb_first(&ubi->scrub); node; node = rb_next(node)) {
1165dbb7d2a8SRichard Weinberger 		wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1166dbb7d2a8SRichard Weinberger 		fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1167dbb7d2a8SRichard Weinberger 
1168dbb7d2a8SRichard Weinberger 		fec->pnum = cpu_to_be32(wl_e->pnum);
1169dbb7d2a8SRichard Weinberger 		fec->ec = cpu_to_be32(wl_e->ec);
1170dbb7d2a8SRichard Weinberger 
1171dbb7d2a8SRichard Weinberger 		scrub_peb_count++;
1172dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fec);
1173dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1174dbb7d2a8SRichard Weinberger 	}
1175dbb7d2a8SRichard Weinberger 	fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
1176dbb7d2a8SRichard Weinberger 
1177dbb7d2a8SRichard Weinberger 
1178dbb7d2a8SRichard Weinberger 	list_for_each_entry(ubi_wrk, &ubi->works, list) {
1179dbb7d2a8SRichard Weinberger 		if (ubi_is_erase_work(ubi_wrk)) {
1180dbb7d2a8SRichard Weinberger 			wl_e = ubi_wrk->e;
1181dbb7d2a8SRichard Weinberger 			ubi_assert(wl_e);
1182dbb7d2a8SRichard Weinberger 
1183dbb7d2a8SRichard Weinberger 			fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1184dbb7d2a8SRichard Weinberger 
1185dbb7d2a8SRichard Weinberger 			fec->pnum = cpu_to_be32(wl_e->pnum);
1186dbb7d2a8SRichard Weinberger 			fec->ec = cpu_to_be32(wl_e->ec);
1187dbb7d2a8SRichard Weinberger 
1188dbb7d2a8SRichard Weinberger 			erase_peb_count++;
1189dbb7d2a8SRichard Weinberger 			fm_pos += sizeof(*fec);
1190dbb7d2a8SRichard Weinberger 			ubi_assert(fm_pos <= ubi->fm_size);
1191dbb7d2a8SRichard Weinberger 		}
1192dbb7d2a8SRichard Weinberger 	}
1193dbb7d2a8SRichard Weinberger 	fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
1194dbb7d2a8SRichard Weinberger 
1195dbb7d2a8SRichard Weinberger 	for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
1196dbb7d2a8SRichard Weinberger 		vol = ubi->volumes[i];
1197dbb7d2a8SRichard Weinberger 
1198dbb7d2a8SRichard Weinberger 		if (!vol)
1199dbb7d2a8SRichard Weinberger 			continue;
1200dbb7d2a8SRichard Weinberger 
1201dbb7d2a8SRichard Weinberger 		vol_count++;
1202dbb7d2a8SRichard Weinberger 
1203dbb7d2a8SRichard Weinberger 		fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
1204dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*fvh);
1205dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1206dbb7d2a8SRichard Weinberger 
1207dbb7d2a8SRichard Weinberger 		fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
1208dbb7d2a8SRichard Weinberger 		fvh->vol_id = cpu_to_be32(vol->vol_id);
1209dbb7d2a8SRichard Weinberger 		fvh->vol_type = vol->vol_type;
1210dbb7d2a8SRichard Weinberger 		fvh->used_ebs = cpu_to_be32(vol->used_ebs);
1211dbb7d2a8SRichard Weinberger 		fvh->data_pad = cpu_to_be32(vol->data_pad);
1212dbb7d2a8SRichard Weinberger 		fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
1213dbb7d2a8SRichard Weinberger 
1214dbb7d2a8SRichard Weinberger 		ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
1215dbb7d2a8SRichard Weinberger 			vol->vol_type == UBI_STATIC_VOLUME);
1216dbb7d2a8SRichard Weinberger 
1217dbb7d2a8SRichard Weinberger 		feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
1218dbb7d2a8SRichard Weinberger 		fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
1219dbb7d2a8SRichard Weinberger 		ubi_assert(fm_pos <= ubi->fm_size);
1220dbb7d2a8SRichard Weinberger 
1221dbb7d2a8SRichard Weinberger 		for (j = 0; j < vol->reserved_pebs; j++)
1222dbb7d2a8SRichard Weinberger 			feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]);
1223dbb7d2a8SRichard Weinberger 
1224dbb7d2a8SRichard Weinberger 		feba->reserved_pebs = cpu_to_be32(j);
1225dbb7d2a8SRichard Weinberger 		feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
1226dbb7d2a8SRichard Weinberger 	}
1227dbb7d2a8SRichard Weinberger 	fmh->vol_count = cpu_to_be32(vol_count);
1228dbb7d2a8SRichard Weinberger 	fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
1229dbb7d2a8SRichard Weinberger 
1230dbb7d2a8SRichard Weinberger 	avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1231dbb7d2a8SRichard Weinberger 	avhdr->lnum = 0;
1232dbb7d2a8SRichard Weinberger 
1233dbb7d2a8SRichard Weinberger 	spin_unlock(&ubi->wl_lock);
1234dbb7d2a8SRichard Weinberger 	spin_unlock(&ubi->volumes_lock);
1235dbb7d2a8SRichard Weinberger 
1236dbb7d2a8SRichard Weinberger 	dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
1237dbb7d2a8SRichard Weinberger 	ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
1238dbb7d2a8SRichard Weinberger 	if (ret) {
1239dbb7d2a8SRichard Weinberger 		ubi_err("unable to write vid_hdr to fastmap SB!");
1240dbb7d2a8SRichard Weinberger 		goto out_kfree;
1241dbb7d2a8SRichard Weinberger 	}
1242dbb7d2a8SRichard Weinberger 
1243dbb7d2a8SRichard Weinberger 	for (i = 0; i < new_fm->used_blocks; i++) {
1244dbb7d2a8SRichard Weinberger 		fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
1245dbb7d2a8SRichard Weinberger 		fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
1246dbb7d2a8SRichard Weinberger 	}
1247dbb7d2a8SRichard Weinberger 
1248dbb7d2a8SRichard Weinberger 	fmsb->data_crc = 0;
1249dbb7d2a8SRichard Weinberger 	fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
1250dbb7d2a8SRichard Weinberger 					   ubi->fm_size));
1251dbb7d2a8SRichard Weinberger 
1252dbb7d2a8SRichard Weinberger 	for (i = 1; i < new_fm->used_blocks; i++) {
1253dbb7d2a8SRichard Weinberger 		dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1254dbb7d2a8SRichard Weinberger 		dvhdr->lnum = cpu_to_be32(i);
1255dbb7d2a8SRichard Weinberger 		dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1256dbb7d2a8SRichard Weinberger 			new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
1257dbb7d2a8SRichard Weinberger 		ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
1258dbb7d2a8SRichard Weinberger 		if (ret) {
1259dbb7d2a8SRichard Weinberger 			ubi_err("unable to write vid_hdr to PEB %i!",
1260dbb7d2a8SRichard Weinberger 				new_fm->e[i]->pnum);
1261dbb7d2a8SRichard Weinberger 			goto out_kfree;
1262dbb7d2a8SRichard Weinberger 		}
1263dbb7d2a8SRichard Weinberger 	}
1264dbb7d2a8SRichard Weinberger 
1265dbb7d2a8SRichard Weinberger 	for (i = 0; i < new_fm->used_blocks; i++) {
1266dbb7d2a8SRichard Weinberger 		ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size),
1267dbb7d2a8SRichard Weinberger 			new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size);
1268dbb7d2a8SRichard Weinberger 		if (ret) {
1269dbb7d2a8SRichard Weinberger 			ubi_err("unable to write fastmap to PEB %i!",
1270dbb7d2a8SRichard Weinberger 				new_fm->e[i]->pnum);
1271dbb7d2a8SRichard Weinberger 			goto out_kfree;
1272dbb7d2a8SRichard Weinberger 		}
1273dbb7d2a8SRichard Weinberger 	}
1274dbb7d2a8SRichard Weinberger 
1275dbb7d2a8SRichard Weinberger 	ubi_assert(new_fm);
1276dbb7d2a8SRichard Weinberger 	ubi->fm = new_fm;
1277dbb7d2a8SRichard Weinberger 
1278dbb7d2a8SRichard Weinberger 	dbg_bld("fastmap written!");
1279dbb7d2a8SRichard Weinberger 
1280dbb7d2a8SRichard Weinberger out_kfree:
1281dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, avhdr);
1282dbb7d2a8SRichard Weinberger 	ubi_free_vid_hdr(ubi, dvhdr);
1283dbb7d2a8SRichard Weinberger out:
1284dbb7d2a8SRichard Weinberger 	return ret;
1285dbb7d2a8SRichard Weinberger }
1286dbb7d2a8SRichard Weinberger 
1287dbb7d2a8SRichard Weinberger /**
1288dbb7d2a8SRichard Weinberger  * erase_block - Manually erase a PEB.
1289dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1290dbb7d2a8SRichard Weinberger  * @pnum: PEB to be erased
1291dbb7d2a8SRichard Weinberger  *
1292dbb7d2a8SRichard Weinberger  * Returns the new EC value on success, < 0 indicates an internal error.
1293dbb7d2a8SRichard Weinberger  */
1294dbb7d2a8SRichard Weinberger static int erase_block(struct ubi_device *ubi, int pnum)
1295dbb7d2a8SRichard Weinberger {
1296dbb7d2a8SRichard Weinberger 	int ret;
1297dbb7d2a8SRichard Weinberger 	struct ubi_ec_hdr *ec_hdr;
1298dbb7d2a8SRichard Weinberger 	long long ec;
1299dbb7d2a8SRichard Weinberger 
1300dbb7d2a8SRichard Weinberger 	ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1301dbb7d2a8SRichard Weinberger 	if (!ec_hdr)
1302dbb7d2a8SRichard Weinberger 		return -ENOMEM;
1303dbb7d2a8SRichard Weinberger 
1304dbb7d2a8SRichard Weinberger 	ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1305dbb7d2a8SRichard Weinberger 	if (ret < 0)
1306dbb7d2a8SRichard Weinberger 		goto out;
1307dbb7d2a8SRichard Weinberger 	else if (ret && ret != UBI_IO_BITFLIPS) {
1308dbb7d2a8SRichard Weinberger 		ret = -EINVAL;
1309dbb7d2a8SRichard Weinberger 		goto out;
1310dbb7d2a8SRichard Weinberger 	}
1311dbb7d2a8SRichard Weinberger 
1312dbb7d2a8SRichard Weinberger 	ret = ubi_io_sync_erase(ubi, pnum, 0);
1313dbb7d2a8SRichard Weinberger 	if (ret < 0)
1314dbb7d2a8SRichard Weinberger 		goto out;
1315dbb7d2a8SRichard Weinberger 
1316dbb7d2a8SRichard Weinberger 	ec = be64_to_cpu(ec_hdr->ec);
1317dbb7d2a8SRichard Weinberger 	ec += ret;
1318dbb7d2a8SRichard Weinberger 	if (ec > UBI_MAX_ERASECOUNTER) {
1319dbb7d2a8SRichard Weinberger 		ret = -EINVAL;
1320dbb7d2a8SRichard Weinberger 		goto out;
1321dbb7d2a8SRichard Weinberger 	}
1322dbb7d2a8SRichard Weinberger 
1323dbb7d2a8SRichard Weinberger 	ec_hdr->ec = cpu_to_be64(ec);
1324dbb7d2a8SRichard Weinberger 	ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
1325dbb7d2a8SRichard Weinberger 	if (ret < 0)
1326dbb7d2a8SRichard Weinberger 		goto out;
1327dbb7d2a8SRichard Weinberger 
1328dbb7d2a8SRichard Weinberger 	ret = ec;
1329dbb7d2a8SRichard Weinberger out:
1330dbb7d2a8SRichard Weinberger 	kfree(ec_hdr);
1331dbb7d2a8SRichard Weinberger 	return ret;
1332dbb7d2a8SRichard Weinberger }
1333dbb7d2a8SRichard Weinberger 
1334dbb7d2a8SRichard Weinberger /**
1335dbb7d2a8SRichard Weinberger  * invalidate_fastmap - destroys a fastmap.
1336dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1337dbb7d2a8SRichard Weinberger  * @fm: the fastmap to be destroyed
1338dbb7d2a8SRichard Weinberger  *
1339dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
1340dbb7d2a8SRichard Weinberger  */
1341dbb7d2a8SRichard Weinberger static int invalidate_fastmap(struct ubi_device *ubi,
1342dbb7d2a8SRichard Weinberger 			      struct ubi_fastmap_layout *fm)
1343dbb7d2a8SRichard Weinberger {
1344dbb7d2a8SRichard Weinberger 	int ret, i;
1345dbb7d2a8SRichard Weinberger 	struct ubi_vid_hdr *vh;
1346dbb7d2a8SRichard Weinberger 
1347dbb7d2a8SRichard Weinberger 	ret = erase_block(ubi, fm->e[0]->pnum);
1348dbb7d2a8SRichard Weinberger 	if (ret < 0)
1349dbb7d2a8SRichard Weinberger 		return ret;
1350dbb7d2a8SRichard Weinberger 
1351dbb7d2a8SRichard Weinberger 	vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1352dbb7d2a8SRichard Weinberger 	if (!vh)
1353dbb7d2a8SRichard Weinberger 		return -ENOMEM;
1354dbb7d2a8SRichard Weinberger 
1355dbb7d2a8SRichard Weinberger 	/* deleting the current fastmap SB is not enough, an old SB may exist,
1356dbb7d2a8SRichard Weinberger 	 * so create a (corrupted) SB such that fastmap will find it and fall
1357dbb7d2a8SRichard Weinberger 	 * back to scanning mode in any case */
1358dbb7d2a8SRichard Weinberger 	vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1359dbb7d2a8SRichard Weinberger 	ret = ubi_io_write_vid_hdr(ubi, fm->e[0]->pnum, vh);
1360dbb7d2a8SRichard Weinberger 
1361dbb7d2a8SRichard Weinberger 	for (i = 0; i < fm->used_blocks; i++)
1362dbb7d2a8SRichard Weinberger 		ubi_wl_put_fm_peb(ubi, fm->e[i], i, fm->to_be_tortured[i]);
1363dbb7d2a8SRichard Weinberger 
1364dbb7d2a8SRichard Weinberger 	return ret;
1365dbb7d2a8SRichard Weinberger }
1366dbb7d2a8SRichard Weinberger 
1367dbb7d2a8SRichard Weinberger /**
1368dbb7d2a8SRichard Weinberger  * ubi_update_fastmap - will be called by UBI if a volume changes or
1369dbb7d2a8SRichard Weinberger  * a fastmap pool becomes full.
1370dbb7d2a8SRichard Weinberger  * @ubi: UBI device object
1371dbb7d2a8SRichard Weinberger  *
1372dbb7d2a8SRichard Weinberger  * Returns 0 on success, < 0 indicates an internal error.
1373dbb7d2a8SRichard Weinberger  */
1374dbb7d2a8SRichard Weinberger int ubi_update_fastmap(struct ubi_device *ubi)
1375dbb7d2a8SRichard Weinberger {
1376dbb7d2a8SRichard Weinberger 	int ret, i;
1377dbb7d2a8SRichard Weinberger 	struct ubi_fastmap_layout *new_fm, *old_fm;
1378dbb7d2a8SRichard Weinberger 	struct ubi_wl_entry *tmp_e;
1379dbb7d2a8SRichard Weinberger 
1380dbb7d2a8SRichard Weinberger 	mutex_lock(&ubi->fm_mutex);
1381dbb7d2a8SRichard Weinberger 
1382dbb7d2a8SRichard Weinberger 	ubi_refill_pools(ubi);
1383dbb7d2a8SRichard Weinberger 
1384dbb7d2a8SRichard Weinberger 	if (ubi->ro_mode || ubi->fm_disabled) {
1385dbb7d2a8SRichard Weinberger 		mutex_unlock(&ubi->fm_mutex);
1386dbb7d2a8SRichard Weinberger 		return 0;
1387dbb7d2a8SRichard Weinberger 	}
1388dbb7d2a8SRichard Weinberger 
1389dbb7d2a8SRichard Weinberger 	ret = ubi_ensure_anchor_pebs(ubi);
1390dbb7d2a8SRichard Weinberger 	if (ret) {
1391dbb7d2a8SRichard Weinberger 		mutex_unlock(&ubi->fm_mutex);
1392dbb7d2a8SRichard Weinberger 		return ret;
1393dbb7d2a8SRichard Weinberger 	}
1394dbb7d2a8SRichard Weinberger 
1395dbb7d2a8SRichard Weinberger 	new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
1396dbb7d2a8SRichard Weinberger 	if (!new_fm) {
1397dbb7d2a8SRichard Weinberger 		mutex_unlock(&ubi->fm_mutex);
1398dbb7d2a8SRichard Weinberger 		return -ENOMEM;
1399dbb7d2a8SRichard Weinberger 	}
1400dbb7d2a8SRichard Weinberger 
1401dbb7d2a8SRichard Weinberger 	new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
1402dbb7d2a8SRichard Weinberger 
1403dbb7d2a8SRichard Weinberger 	for (i = 0; i < new_fm->used_blocks; i++) {
1404dbb7d2a8SRichard Weinberger 		new_fm->e[i] = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1405dbb7d2a8SRichard Weinberger 		if (!new_fm->e[i]) {
1406dbb7d2a8SRichard Weinberger 			while (i--)
1407dbb7d2a8SRichard Weinberger 				kfree(new_fm->e[i]);
1408dbb7d2a8SRichard Weinberger 
1409dbb7d2a8SRichard Weinberger 			kfree(new_fm);
1410dbb7d2a8SRichard Weinberger 			mutex_unlock(&ubi->fm_mutex);
1411dbb7d2a8SRichard Weinberger 			return -ENOMEM;
1412dbb7d2a8SRichard Weinberger 		}
1413dbb7d2a8SRichard Weinberger 	}
1414dbb7d2a8SRichard Weinberger 
1415dbb7d2a8SRichard Weinberger 	old_fm = ubi->fm;
1416dbb7d2a8SRichard Weinberger 	ubi->fm = NULL;
1417dbb7d2a8SRichard Weinberger 
1418dbb7d2a8SRichard Weinberger 	if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
1419dbb7d2a8SRichard Weinberger 		ubi_err("fastmap too large");
1420dbb7d2a8SRichard Weinberger 		ret = -ENOSPC;
1421dbb7d2a8SRichard Weinberger 		goto err;
1422dbb7d2a8SRichard Weinberger 	}
1423dbb7d2a8SRichard Weinberger 
1424dbb7d2a8SRichard Weinberger 	for (i = 1; i < new_fm->used_blocks; i++) {
1425dbb7d2a8SRichard Weinberger 		spin_lock(&ubi->wl_lock);
1426dbb7d2a8SRichard Weinberger 		tmp_e = ubi_wl_get_fm_peb(ubi, 0);
1427dbb7d2a8SRichard Weinberger 		spin_unlock(&ubi->wl_lock);
1428dbb7d2a8SRichard Weinberger 
1429dbb7d2a8SRichard Weinberger 		if (!tmp_e && !old_fm) {
1430dbb7d2a8SRichard Weinberger 			int j;
1431dbb7d2a8SRichard Weinberger 			ubi_err("could not get any free erase block");
1432dbb7d2a8SRichard Weinberger 
1433dbb7d2a8SRichard Weinberger 			for (j = 1; j < i; j++)
1434dbb7d2a8SRichard Weinberger 				ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
1435dbb7d2a8SRichard Weinberger 
1436dbb7d2a8SRichard Weinberger 			ret = -ENOSPC;
1437dbb7d2a8SRichard Weinberger 			goto err;
1438dbb7d2a8SRichard Weinberger 		} else if (!tmp_e && old_fm) {
1439dbb7d2a8SRichard Weinberger 			ret = erase_block(ubi, old_fm->e[i]->pnum);
1440dbb7d2a8SRichard Weinberger 			if (ret < 0) {
1441dbb7d2a8SRichard Weinberger 				int j;
1442dbb7d2a8SRichard Weinberger 
1443dbb7d2a8SRichard Weinberger 				for (j = 1; j < i; j++)
1444dbb7d2a8SRichard Weinberger 					ubi_wl_put_fm_peb(ubi, new_fm->e[j],
1445dbb7d2a8SRichard Weinberger 							  j, 0);
1446dbb7d2a8SRichard Weinberger 
1447dbb7d2a8SRichard Weinberger 				ubi_err("could not erase old fastmap PEB");
1448dbb7d2a8SRichard Weinberger 				goto err;
1449dbb7d2a8SRichard Weinberger 			}
1450dbb7d2a8SRichard Weinberger 
1451dbb7d2a8SRichard Weinberger 			new_fm->e[i]->pnum = old_fm->e[i]->pnum;
1452dbb7d2a8SRichard Weinberger 			new_fm->e[i]->ec = old_fm->e[i]->ec;
1453dbb7d2a8SRichard Weinberger 		} else {
1454dbb7d2a8SRichard Weinberger 			new_fm->e[i]->pnum = tmp_e->pnum;
1455dbb7d2a8SRichard Weinberger 			new_fm->e[i]->ec = tmp_e->ec;
1456dbb7d2a8SRichard Weinberger 
1457dbb7d2a8SRichard Weinberger 			if (old_fm)
1458dbb7d2a8SRichard Weinberger 				ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1459dbb7d2a8SRichard Weinberger 						  old_fm->to_be_tortured[i]);
1460dbb7d2a8SRichard Weinberger 		}
1461dbb7d2a8SRichard Weinberger 	}
1462dbb7d2a8SRichard Weinberger 
1463dbb7d2a8SRichard Weinberger 	spin_lock(&ubi->wl_lock);
1464dbb7d2a8SRichard Weinberger 	tmp_e = ubi_wl_get_fm_peb(ubi, 1);
1465dbb7d2a8SRichard Weinberger 	spin_unlock(&ubi->wl_lock);
1466dbb7d2a8SRichard Weinberger 
1467dbb7d2a8SRichard Weinberger 	if (old_fm) {
1468dbb7d2a8SRichard Weinberger 		/* no fresh anchor PEB was found, reuse the old one */
1469dbb7d2a8SRichard Weinberger 		if (!tmp_e) {
1470dbb7d2a8SRichard Weinberger 			ret = erase_block(ubi, old_fm->e[0]->pnum);
1471dbb7d2a8SRichard Weinberger 			if (ret < 0) {
1472dbb7d2a8SRichard Weinberger 				int i;
1473dbb7d2a8SRichard Weinberger 				ubi_err("could not erase old anchor PEB");
1474dbb7d2a8SRichard Weinberger 
1475dbb7d2a8SRichard Weinberger 				for (i = 1; i < new_fm->used_blocks; i++)
1476dbb7d2a8SRichard Weinberger 					ubi_wl_put_fm_peb(ubi, new_fm->e[i],
1477dbb7d2a8SRichard Weinberger 							  i, 0);
1478dbb7d2a8SRichard Weinberger 				goto err;
1479dbb7d2a8SRichard Weinberger 			}
1480dbb7d2a8SRichard Weinberger 
1481dbb7d2a8SRichard Weinberger 			new_fm->e[0]->pnum = old_fm->e[0]->pnum;
1482dbb7d2a8SRichard Weinberger 			new_fm->e[0]->ec = ret;
1483dbb7d2a8SRichard Weinberger 		} else {
1484dbb7d2a8SRichard Weinberger 			/* we've got a new anchor PEB, return the old one */
1485dbb7d2a8SRichard Weinberger 			ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
1486dbb7d2a8SRichard Weinberger 					  old_fm->to_be_tortured[0]);
1487dbb7d2a8SRichard Weinberger 
1488dbb7d2a8SRichard Weinberger 			new_fm->e[0]->pnum = tmp_e->pnum;
1489dbb7d2a8SRichard Weinberger 			new_fm->e[0]->ec = tmp_e->ec;
1490dbb7d2a8SRichard Weinberger 		}
1491dbb7d2a8SRichard Weinberger 	} else {
1492dbb7d2a8SRichard Weinberger 		if (!tmp_e) {
1493dbb7d2a8SRichard Weinberger 			int i;
1494dbb7d2a8SRichard Weinberger 			ubi_err("could not find any anchor PEB");
1495dbb7d2a8SRichard Weinberger 
1496dbb7d2a8SRichard Weinberger 			for (i = 1; i < new_fm->used_blocks; i++)
1497dbb7d2a8SRichard Weinberger 				ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
1498dbb7d2a8SRichard Weinberger 
1499dbb7d2a8SRichard Weinberger 			ret = -ENOSPC;
1500dbb7d2a8SRichard Weinberger 			goto err;
1501dbb7d2a8SRichard Weinberger 		}
1502dbb7d2a8SRichard Weinberger 
1503dbb7d2a8SRichard Weinberger 		new_fm->e[0]->pnum = tmp_e->pnum;
1504dbb7d2a8SRichard Weinberger 		new_fm->e[0]->ec = tmp_e->ec;
1505dbb7d2a8SRichard Weinberger 	}
1506dbb7d2a8SRichard Weinberger 
1507dbb7d2a8SRichard Weinberger 	down_write(&ubi->work_sem);
1508dbb7d2a8SRichard Weinberger 	down_write(&ubi->fm_sem);
1509dbb7d2a8SRichard Weinberger 	ret = ubi_write_fastmap(ubi, new_fm);
1510dbb7d2a8SRichard Weinberger 	up_write(&ubi->fm_sem);
1511dbb7d2a8SRichard Weinberger 	up_write(&ubi->work_sem);
1512dbb7d2a8SRichard Weinberger 
1513dbb7d2a8SRichard Weinberger 	if (ret)
1514dbb7d2a8SRichard Weinberger 		goto err;
1515dbb7d2a8SRichard Weinberger 
1516dbb7d2a8SRichard Weinberger out_unlock:
1517dbb7d2a8SRichard Weinberger 	mutex_unlock(&ubi->fm_mutex);
1518dbb7d2a8SRichard Weinberger 	kfree(old_fm);
1519dbb7d2a8SRichard Weinberger 	return ret;
1520dbb7d2a8SRichard Weinberger 
1521dbb7d2a8SRichard Weinberger err:
1522dbb7d2a8SRichard Weinberger 	kfree(new_fm);
1523dbb7d2a8SRichard Weinberger 
1524dbb7d2a8SRichard Weinberger 	ubi_warn("Unable to write new fastmap, err=%i", ret);
1525dbb7d2a8SRichard Weinberger 
1526dbb7d2a8SRichard Weinberger 	ret = 0;
1527dbb7d2a8SRichard Weinberger 	if (old_fm) {
1528dbb7d2a8SRichard Weinberger 		ret = invalidate_fastmap(ubi, old_fm);
1529dbb7d2a8SRichard Weinberger 		if (ret < 0)
1530dbb7d2a8SRichard Weinberger 			ubi_err("Unable to invalidiate current fastmap!");
1531dbb7d2a8SRichard Weinberger 		else if (ret)
1532dbb7d2a8SRichard Weinberger 			ret = 0;
1533dbb7d2a8SRichard Weinberger 	}
1534dbb7d2a8SRichard Weinberger 	goto out_unlock;
1535dbb7d2a8SRichard Weinberger }
1536