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