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