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