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