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