2 * Copyright (c) 2012 Linutronix GmbH
3 * Author: Richard Weinberger <richard@nod.at>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/crc32.h>
15 #include <ubi_uboot.h>
18 #include <linux/compat.h>
19 #include <linux/math64.h>
23 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
24 * @ubi: UBI device description object
26 size_t ubi_calc_fm_size(struct ubi_device *ubi)
30 size = sizeof(struct ubi_fm_hdr) + \
31 sizeof(struct ubi_fm_scan_pool) + \
32 sizeof(struct ubi_fm_scan_pool) + \
33 (ubi->peb_count * sizeof(struct ubi_fm_ec)) + \
34 (sizeof(struct ubi_fm_eba) + \
35 (ubi->peb_count * sizeof(__be32))) + \
36 sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
37 return roundup(size, ubi->leb_size);
42 * new_fm_vhdr - allocate a new volume header for fastmap usage.
43 * @ubi: UBI device description object
44 * @vol_id: the VID of the new header
46 * Returns a new struct ubi_vid_hdr on success.
47 * NULL indicates out of memory.
49 static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id)
51 struct ubi_vid_hdr *new;
53 new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
57 new->vol_type = UBI_VID_DYNAMIC;
58 new->vol_id = cpu_to_be32(vol_id);
60 /* UBI implementations without fastmap support have to delete the
63 new->compat = UBI_COMPAT_DELETE;
70 * add_aeb - create and add a attach erase block to a given list.
71 * @ai: UBI attach info object
72 * @list: the target list
73 * @pnum: PEB number of the new attach erase block
74 * @ec: erease counter of the new LEB
75 * @scrub: scrub this PEB after attaching
77 * Returns 0 on success, < 0 indicates an internal error.
79 static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
80 int pnum, int ec, int scrub)
82 struct ubi_ainf_peb *aeb;
84 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
92 aeb->copy_flag = aeb->sqnum = 0;
94 ai->ec_sum += aeb->ec;
97 if (ai->max_ec < aeb->ec)
100 if (ai->min_ec > aeb->ec)
101 ai->min_ec = aeb->ec;
103 list_add_tail(&aeb->u.list, list);
109 * add_vol - create and add a new volume to ubi_attach_info.
110 * @ai: ubi_attach_info object
111 * @vol_id: VID of the new volume
112 * @used_ebs: number of used EBS
113 * @data_pad: data padding value of the new volume
114 * @vol_type: volume type
115 * @last_eb_bytes: number of bytes in the last LEB
117 * Returns the new struct ubi_ainf_volume on success.
118 * NULL indicates an error.
120 static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id,
121 int used_ebs, int data_pad, u8 vol_type,
124 struct ubi_ainf_volume *av;
125 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
129 av = rb_entry(parent, struct ubi_ainf_volume, rb);
131 if (vol_id > av->vol_id)
133 else if (vol_id > av->vol_id)
137 av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL);
141 av->highest_lnum = av->leb_count = 0;
143 av->used_ebs = used_ebs;
144 av->data_pad = data_pad;
145 av->last_data_size = last_eb_bytes;
147 av->vol_type = vol_type;
150 dbg_bld("found volume (ID %i)", vol_id);
152 rb_link_node(&av->rb, parent, p);
153 rb_insert_color(&av->rb, &ai->volumes);
160 * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
161 * from it's original list.
162 * @ai: ubi_attach_info object
163 * @aeb: the to be assigned SEB
164 * @av: target scan volume
166 static void assign_aeb_to_av(struct ubi_attach_info *ai,
167 struct ubi_ainf_peb *aeb,
168 struct ubi_ainf_volume *av)
170 struct ubi_ainf_peb *tmp_aeb;
171 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
173 p = &av->root.rb_node;
177 tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
178 if (aeb->lnum != tmp_aeb->lnum) {
179 if (aeb->lnum < tmp_aeb->lnum)
189 list_del(&aeb->u.list);
192 rb_link_node(&aeb->u.rb, parent, p);
193 rb_insert_color(&aeb->u.rb, &av->root);
197 * update_vol - inserts or updates a LEB which was found a pool.
198 * @ubi: the UBI device object
199 * @ai: attach info object
200 * @av: the volume this LEB belongs to
201 * @new_vh: the volume header derived from new_aeb
202 * @new_aeb: the AEB to be examined
204 * Returns 0 on success, < 0 indicates an internal error.
206 static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
207 struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh,
208 struct ubi_ainf_peb *new_aeb)
210 struct rb_node **p = &av->root.rb_node, *parent = NULL;
211 struct ubi_ainf_peb *aeb, *victim;
216 aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
218 if (be32_to_cpu(new_vh->lnum) != aeb->lnum) {
219 if (be32_to_cpu(new_vh->lnum) < aeb->lnum)
227 /* This case can happen if the fastmap gets written
228 * because of a volume change (creation, deletion, ..).
229 * Then a PEB can be within the persistent EBA and the pool.
231 if (aeb->pnum == new_aeb->pnum) {
232 ubi_assert(aeb->lnum == new_aeb->lnum);
233 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
238 cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh);
242 /* new_aeb is newer */
244 victim = kmem_cache_alloc(ai->aeb_slab_cache,
249 victim->ec = aeb->ec;
250 victim->pnum = aeb->pnum;
251 list_add_tail(&victim->u.list, &ai->erase);
253 if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
254 av->last_data_size = \
255 be32_to_cpu(new_vh->data_size);
257 dbg_bld("vol %i: AEB %i's PEB %i is the newer",
258 av->vol_id, aeb->lnum, new_aeb->pnum);
260 aeb->ec = new_aeb->ec;
261 aeb->pnum = new_aeb->pnum;
262 aeb->copy_flag = new_vh->copy_flag;
263 aeb->scrub = new_aeb->scrub;
264 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
266 /* new_aeb is older */
268 dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
269 av->vol_id, aeb->lnum, new_aeb->pnum);
270 list_add_tail(&new_aeb->u.list, &ai->erase);
275 /* This LEB is new, let's add it to the volume */
277 if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
278 av->highest_lnum = be32_to_cpu(new_vh->lnum);
279 av->last_data_size = be32_to_cpu(new_vh->data_size);
282 if (av->vol_type == UBI_STATIC_VOLUME)
283 av->used_ebs = be32_to_cpu(new_vh->used_ebs);
287 rb_link_node(&new_aeb->u.rb, parent, p);
288 rb_insert_color(&new_aeb->u.rb, &av->root);
294 * process_pool_aeb - we found a non-empty PEB in a pool.
295 * @ubi: UBI device object
296 * @ai: attach info object
297 * @new_vh: the volume header derived from new_aeb
298 * @new_aeb: the AEB to be examined
300 * Returns 0 on success, < 0 indicates an internal error.
302 static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
303 struct ubi_vid_hdr *new_vh,
304 struct ubi_ainf_peb *new_aeb)
306 struct ubi_ainf_volume *av, *tmp_av = NULL;
307 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
310 if (be32_to_cpu(new_vh->vol_id) == UBI_FM_SB_VOLUME_ID ||
311 be32_to_cpu(new_vh->vol_id) == UBI_FM_DATA_VOLUME_ID) {
312 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
317 /* Find the volume this SEB belongs to */
320 tmp_av = rb_entry(parent, struct ubi_ainf_volume, rb);
322 if (be32_to_cpu(new_vh->vol_id) > tmp_av->vol_id)
324 else if (be32_to_cpu(new_vh->vol_id) < tmp_av->vol_id)
335 ubi_err("orphaned volume in fastmap pool!");
336 return UBI_BAD_FASTMAP;
339 ubi_assert(be32_to_cpu(new_vh->vol_id) == av->vol_id);
341 return update_vol(ubi, ai, av, new_vh, new_aeb);
345 * unmap_peb - unmap a PEB.
346 * If fastmap detects a free PEB in the pool it has to check whether
347 * this PEB has been unmapped after writing the fastmap.
349 * @ai: UBI attach info object
350 * @pnum: The PEB to be unmapped
352 static void unmap_peb(struct ubi_attach_info *ai, int pnum)
354 struct ubi_ainf_volume *av;
355 struct rb_node *node, *node2;
356 struct ubi_ainf_peb *aeb;
358 for (node = rb_first(&ai->volumes); node; node = rb_next(node)) {
359 av = rb_entry(node, struct ubi_ainf_volume, rb);
361 for (node2 = rb_first(&av->root); node2;
362 node2 = rb_next(node2)) {
363 aeb = rb_entry(node2, struct ubi_ainf_peb, u.rb);
364 if (aeb->pnum == pnum) {
365 rb_erase(&aeb->u.rb, &av->root);
366 kmem_cache_free(ai->aeb_slab_cache, aeb);
374 * scan_pool - scans a pool for changed (no longer empty PEBs).
375 * @ubi: UBI device object
376 * @ai: attach info object
377 * @pebs: an array of all PEB numbers in the to be scanned pool
378 * @pool_size: size of the pool (number of entries in @pebs)
379 * @max_sqnum: pointer to the maximal sequence number
380 * @eba_orphans: list of PEBs which need to be scanned
381 * @free: list of PEBs which are most likely free (and go into @ai->free)
383 * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
384 * < 0 indicates an internal error.
387 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
388 int *pebs, int pool_size, unsigned long long *max_sqnum,
389 struct list_head *eba_orphans, struct list_head *freef)
391 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
392 __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
393 struct list_head *eba_orphans, struct list_head *freef)
396 struct ubi_vid_hdr *vh;
397 struct ubi_ec_hdr *ech;
398 struct ubi_ainf_peb *new_aeb, *tmp_aeb;
399 int i, pnum, err, found_orphan, ret = 0;
401 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
405 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
411 dbg_bld("scanning fastmap pool: size = %i", pool_size);
414 * Now scan all PEBs in the pool to find changes which have been made
415 * after the creation of the fastmap
417 for (i = 0; i < pool_size; i++) {
421 pnum = be32_to_cpu(pebs[i]);
423 if (ubi_io_is_bad(ubi, pnum)) {
424 ubi_err("bad PEB in fastmap pool!");
425 ret = UBI_BAD_FASTMAP;
429 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
430 if (err && err != UBI_IO_BITFLIPS) {
431 ubi_err("unable to read EC header! PEB:%i err:%i",
433 ret = err > 0 ? UBI_BAD_FASTMAP : err;
435 } else if (ret == UBI_IO_BITFLIPS)
439 * Older UBI implementations have image_seq set to zero, so
440 * we shouldn't fail if image_seq == 0.
442 image_seq = be32_to_cpu(ech->image_seq);
444 if (image_seq && (image_seq != ubi->image_seq)) {
445 ubi_err("bad image seq: 0x%x, expected: 0x%x",
446 be32_to_cpu(ech->image_seq), ubi->image_seq);
447 ret = UBI_BAD_FASTMAP;
451 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
452 if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
453 unsigned long long ec = be64_to_cpu(ech->ec);
455 dbg_bld("Adding PEB to free: %i", pnum);
456 if (err == UBI_IO_FF_BITFLIPS)
457 add_aeb(ai, freef, pnum, ec, 1);
459 add_aeb(ai, freef, pnum, ec, 0);
461 } else if (err == 0 || err == UBI_IO_BITFLIPS) {
462 dbg_bld("Found non empty PEB:%i in pool", pnum);
464 if (err == UBI_IO_BITFLIPS)
468 list_for_each_entry(tmp_aeb, eba_orphans, u.list) {
469 if (tmp_aeb->pnum == pnum) {
475 list_del(&tmp_aeb->u.list);
476 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
479 new_aeb = kmem_cache_alloc(ai->aeb_slab_cache,
486 new_aeb->ec = be64_to_cpu(ech->ec);
487 new_aeb->pnum = pnum;
488 new_aeb->lnum = be32_to_cpu(vh->lnum);
489 new_aeb->sqnum = be64_to_cpu(vh->sqnum);
490 new_aeb->copy_flag = vh->copy_flag;
491 new_aeb->scrub = scrub;
493 if (*max_sqnum < new_aeb->sqnum)
494 *max_sqnum = new_aeb->sqnum;
496 err = process_pool_aeb(ubi, ai, vh, new_aeb);
498 ret = err > 0 ? UBI_BAD_FASTMAP : err;
502 /* We are paranoid and fall back to scanning mode */
503 ubi_err("fastmap pool PEBs contains damaged PEBs!");
504 ret = err > 0 ? UBI_BAD_FASTMAP : err;
511 ubi_free_vid_hdr(ubi, vh);
517 * count_fastmap_pebs - Counts the PEBs found by fastmap.
518 * @ai: The UBI attach info object
520 static int count_fastmap_pebs(struct ubi_attach_info *ai)
522 struct ubi_ainf_peb *aeb;
523 struct ubi_ainf_volume *av;
524 struct rb_node *rb1, *rb2;
527 list_for_each_entry(aeb, &ai->erase, u.list)
530 list_for_each_entry(aeb, &ai->free, u.list)
533 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
534 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
541 * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
542 * @ubi: UBI device object
543 * @ai: UBI attach info object
544 * @fm: the fastmap to be attached
546 * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
547 * < 0 indicates an internal error.
549 static int ubi_attach_fastmap(struct ubi_device *ubi,
550 struct ubi_attach_info *ai,
551 struct ubi_fastmap_layout *fm)
553 struct list_head used, eba_orphans, freef;
554 struct ubi_ainf_volume *av;
555 struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
556 struct ubi_ec_hdr *ech;
557 struct ubi_fm_sb *fmsb;
558 struct ubi_fm_hdr *fmhdr;
559 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
560 struct ubi_fm_ec *fmec;
561 struct ubi_fm_volhdr *fmvhdr;
562 struct ubi_fm_eba *fm_eba;
563 int ret, i, j, pool_size, wl_pool_size;
564 size_t fm_pos = 0, fm_size = ubi->fm_size;
565 unsigned long long max_sqnum = 0;
566 void *fm_raw = ubi->fm_buf;
568 INIT_LIST_HEAD(&used);
569 INIT_LIST_HEAD(&freef);
570 INIT_LIST_HEAD(&eba_orphans);
571 INIT_LIST_HEAD(&ai->corr);
572 INIT_LIST_HEAD(&ai->free);
573 INIT_LIST_HEAD(&ai->erase);
574 INIT_LIST_HEAD(&ai->alien);
575 ai->volumes = RB_ROOT;
576 ai->min_ec = UBI_MAX_ERASECOUNTER;
578 ai->aeb_slab_cache = kmem_cache_create("ubi_ainf_peb_slab",
579 sizeof(struct ubi_ainf_peb),
581 if (!ai->aeb_slab_cache) {
586 fmsb = (struct ubi_fm_sb *)(fm_raw);
587 ai->max_sqnum = fmsb->sqnum;
588 fm_pos += sizeof(struct ubi_fm_sb);
589 if (fm_pos >= fm_size)
592 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
593 fm_pos += sizeof(*fmhdr);
594 if (fm_pos >= fm_size)
597 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
598 ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
599 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
603 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
604 fm_pos += sizeof(*fmpl1);
605 if (fm_pos >= fm_size)
607 if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
608 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
609 be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
613 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
614 fm_pos += sizeof(*fmpl2);
615 if (fm_pos >= fm_size)
617 if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
618 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
619 be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
623 pool_size = be16_to_cpu(fmpl1->size);
624 wl_pool_size = be16_to_cpu(fmpl2->size);
625 fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
626 fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
628 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
629 ubi_err("bad pool size: %i", pool_size);
633 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
634 ubi_err("bad WL pool size: %i", wl_pool_size);
639 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
640 fm->max_pool_size < 0) {
641 ubi_err("bad maximal pool size: %i", fm->max_pool_size);
645 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
646 fm->max_wl_pool_size < 0) {
647 ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
651 /* read EC values from free list */
652 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
653 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
654 fm_pos += sizeof(*fmec);
655 if (fm_pos >= fm_size)
658 add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
659 be32_to_cpu(fmec->ec), 0);
662 /* read EC values from used list */
663 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
664 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
665 fm_pos += sizeof(*fmec);
666 if (fm_pos >= fm_size)
669 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
670 be32_to_cpu(fmec->ec), 0);
673 /* read EC values from scrub list */
674 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
675 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
676 fm_pos += sizeof(*fmec);
677 if (fm_pos >= fm_size)
680 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
681 be32_to_cpu(fmec->ec), 1);
684 /* read EC values from erase list */
685 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
686 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
687 fm_pos += sizeof(*fmec);
688 if (fm_pos >= fm_size)
691 add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
692 be32_to_cpu(fmec->ec), 1);
695 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
696 ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
698 /* Iterate over all volumes and read their EBA table */
699 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
700 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
701 fm_pos += sizeof(*fmvhdr);
702 if (fm_pos >= fm_size)
705 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
706 ubi_err("bad fastmap vol header magic: 0x%x, " \
708 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
712 av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
713 be32_to_cpu(fmvhdr->used_ebs),
714 be32_to_cpu(fmvhdr->data_pad),
716 be32_to_cpu(fmvhdr->last_eb_bytes));
722 if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
723 ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
725 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
726 fm_pos += sizeof(*fm_eba);
727 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
728 if (fm_pos >= fm_size)
731 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
732 ubi_err("bad fastmap EBA header magic: 0x%x, " \
734 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
738 for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
739 int pnum = be32_to_cpu(fm_eba->pnum[j]);
741 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
745 list_for_each_entry(tmp_aeb, &used, u.list) {
746 if (tmp_aeb->pnum == pnum) {
752 /* This can happen if a PEB is already in an EBA known
753 * by this fastmap but the PEB itself is not in the used
755 * In this case the PEB can be within the fastmap pool
756 * or while writing the fastmap it was in the protection
760 aeb = kmem_cache_alloc(ai->aeb_slab_cache,
769 aeb->pnum = be32_to_cpu(fm_eba->pnum[j]);
771 aeb->scrub = aeb->copy_flag = aeb->sqnum = 0;
772 list_add_tail(&aeb->u.list, &eba_orphans);
778 if (av->highest_lnum <= aeb->lnum)
779 av->highest_lnum = aeb->lnum;
781 assign_aeb_to_av(ai, aeb, av);
783 dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
784 aeb->pnum, aeb->lnum, av->vol_id);
787 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
793 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &eba_orphans,
797 if (ubi_io_is_bad(ubi, tmp_aeb->pnum)) {
798 ubi_err("bad PEB in fastmap EBA orphan list");
799 ret = UBI_BAD_FASTMAP;
804 err = ubi_io_read_ec_hdr(ubi, tmp_aeb->pnum, ech, 0);
805 if (err && err != UBI_IO_BITFLIPS) {
806 ubi_err("unable to read EC header! PEB:%i " \
807 "err:%i", tmp_aeb->pnum, err);
808 ret = err > 0 ? UBI_BAD_FASTMAP : err;
812 } else if (err == UBI_IO_BITFLIPS)
815 tmp_aeb->ec = be64_to_cpu(ech->ec);
816 assign_aeb_to_av(ai, tmp_aeb, av);
822 ret = scan_pool(ubi, ai, fmpl1->pebs, pool_size, &max_sqnum,
823 &eba_orphans, &freef);
827 ret = scan_pool(ubi, ai, fmpl2->pebs, wl_pool_size, &max_sqnum,
828 &eba_orphans, &freef);
832 if (max_sqnum > ai->max_sqnum)
833 ai->max_sqnum = max_sqnum;
835 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &freef, u.list)
836 list_move_tail(&tmp_aeb->u.list, &ai->free);
838 ubi_assert(list_empty(&used));
839 ubi_assert(list_empty(&eba_orphans));
840 ubi_assert(list_empty(&freef));
843 * If fastmap is leaking PEBs (must not happen), raise a
844 * fat warning and fall back to scanning mode.
845 * We do this here because in ubi_wl_init() it's too late
846 * and we cannot fall back to scanning.
849 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
850 ai->bad_peb_count - fm->used_blocks))
853 if (count_fastmap_pebs(ai) != ubi->peb_count -
854 ai->bad_peb_count - fm->used_blocks) {
863 ret = UBI_BAD_FASTMAP;
865 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
866 list_del(&tmp_aeb->u.list);
867 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
869 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &eba_orphans, u.list) {
870 list_del(&tmp_aeb->u.list);
871 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
873 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &freef, u.list) {
874 list_del(&tmp_aeb->u.list);
875 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
882 * ubi_scan_fastmap - scan the fastmap.
883 * @ubi: UBI device object
884 * @ai: UBI attach info to be filled
885 * @fm_anchor: The fastmap starts at this PEB
887 * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
888 * UBI_BAD_FASTMAP if one was found but is not usable.
889 * < 0 indicates an internal error.
891 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
894 struct ubi_fm_sb *fmsb, *fmsb2;
895 struct ubi_vid_hdr *vh;
896 struct ubi_ec_hdr *ech;
897 struct ubi_fastmap_layout *fm;
898 int i, used_blocks, pnum, ret = 0;
901 unsigned long long sqnum = 0;
903 mutex_lock(&ubi->fm_mutex);
904 memset(ubi->fm_buf, 0, ubi->fm_size);
906 fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
912 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
919 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
920 if (ret && ret != UBI_IO_BITFLIPS)
922 else if (ret == UBI_IO_BITFLIPS)
923 fm->to_be_tortured[0] = 1;
925 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
926 ubi_err("bad super block magic: 0x%x, expected: 0x%x",
927 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
928 ret = UBI_BAD_FASTMAP;
932 if (fmsb->version != UBI_FM_FMT_VERSION) {
933 ubi_err("bad fastmap version: %i, expected: %i",
934 fmsb->version, UBI_FM_FMT_VERSION);
935 ret = UBI_BAD_FASTMAP;
939 used_blocks = be32_to_cpu(fmsb->used_blocks);
940 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
941 ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
942 ret = UBI_BAD_FASTMAP;
946 fm_size = ubi->leb_size * used_blocks;
947 if (fm_size != ubi->fm_size) {
948 ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
950 ret = UBI_BAD_FASTMAP;
954 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
960 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
966 for (i = 0; i < used_blocks; i++) {
969 pnum = be32_to_cpu(fmsb->block_loc[i]);
971 if (ubi_io_is_bad(ubi, pnum)) {
972 ret = UBI_BAD_FASTMAP;
976 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
977 if (ret && ret != UBI_IO_BITFLIPS) {
978 ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
981 ret = UBI_BAD_FASTMAP;
983 } else if (ret == UBI_IO_BITFLIPS)
984 fm->to_be_tortured[i] = 1;
986 image_seq = be32_to_cpu(ech->image_seq);
988 ubi->image_seq = image_seq;
991 * Older UBI implementations have image_seq set to zero, so
992 * we shouldn't fail if image_seq == 0.
994 if (image_seq && (image_seq != ubi->image_seq)) {
995 ubi_err("wrong image seq:%d instead of %d",
996 be32_to_cpu(ech->image_seq), ubi->image_seq);
997 ret = UBI_BAD_FASTMAP;
1001 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
1002 if (ret && ret != UBI_IO_BITFLIPS) {
1003 ubi_err("unable to read fastmap block# %i (PEB: %i)",
1009 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
1010 ubi_err("bad fastmap anchor vol_id: 0x%x," \
1012 be32_to_cpu(vh->vol_id),
1013 UBI_FM_SB_VOLUME_ID);
1014 ret = UBI_BAD_FASTMAP;
1018 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
1019 ubi_err("bad fastmap data vol_id: 0x%x," \
1021 be32_to_cpu(vh->vol_id),
1022 UBI_FM_DATA_VOLUME_ID);
1023 ret = UBI_BAD_FASTMAP;
1028 if (sqnum < be64_to_cpu(vh->sqnum))
1029 sqnum = be64_to_cpu(vh->sqnum);
1031 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
1032 ubi->leb_start, ubi->leb_size);
1033 if (ret && ret != UBI_IO_BITFLIPS) {
1034 ubi_err("unable to read fastmap block# %i (PEB: %i, " \
1035 "err: %i)", i, pnum, ret);
1043 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
1044 tmp_crc = be32_to_cpu(fmsb2->data_crc);
1045 fmsb2->data_crc = 0;
1046 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
1047 if (crc != tmp_crc) {
1048 ubi_err("fastmap data CRC is invalid");
1049 ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
1050 ret = UBI_BAD_FASTMAP;
1054 fmsb2->sqnum = sqnum;
1056 fm->used_blocks = used_blocks;
1058 ret = ubi_attach_fastmap(ubi, ai, fm);
1061 ret = UBI_BAD_FASTMAP;
1065 for (i = 0; i < used_blocks; i++) {
1066 struct ubi_wl_entry *e;
1068 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1077 e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
1078 e->ec = be32_to_cpu(fmsb2->block_ec[i]);
1083 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
1084 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
1085 ubi_msg("attached by fastmap");
1086 ubi_msg("fastmap pool size: %d", ubi->fm_pool.max_size);
1087 ubi_msg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
1088 ubi->fm_disabled = 0;
1090 ubi_free_vid_hdr(ubi, vh);
1093 mutex_unlock(&ubi->fm_mutex);
1094 if (ret == UBI_BAD_FASTMAP)
1095 ubi_err("Attach by fastmap failed, doing a full scan!");
1099 ubi_free_vid_hdr(ubi, vh);
1108 * ubi_write_fastmap - writes a fastmap.
1109 * @ubi: UBI device object
1110 * @new_fm: the to be written fastmap
1112 * Returns 0 on success, < 0 indicates an internal error.
1114 static int ubi_write_fastmap(struct ubi_device *ubi,
1115 struct ubi_fastmap_layout *new_fm)
1119 struct ubi_fm_sb *fmsb;
1120 struct ubi_fm_hdr *fmh;
1121 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
1122 struct ubi_fm_ec *fec;
1123 struct ubi_fm_volhdr *fvh;
1124 struct ubi_fm_eba *feba;
1125 struct rb_node *node;
1126 struct ubi_wl_entry *wl_e;
1127 struct ubi_volume *vol;
1128 struct ubi_vid_hdr *avhdr, *dvhdr;
1129 struct ubi_work *ubi_wrk;
1130 int ret, i, j, free_peb_count, used_peb_count, vol_count;
1131 int scrub_peb_count, erase_peb_count;
1133 fm_raw = ubi->fm_buf;
1134 memset(ubi->fm_buf, 0, ubi->fm_size);
1136 avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1142 dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
1148 spin_lock(&ubi->volumes_lock);
1149 spin_lock(&ubi->wl_lock);
1151 fmsb = (struct ubi_fm_sb *)fm_raw;
1152 fm_pos += sizeof(*fmsb);
1153 ubi_assert(fm_pos <= ubi->fm_size);
1155 fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
1156 fm_pos += sizeof(*fmh);
1157 ubi_assert(fm_pos <= ubi->fm_size);
1159 fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
1160 fmsb->version = UBI_FM_FMT_VERSION;
1161 fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
1162 /* the max sqnum will be filled in while *reading* the fastmap */
1165 fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
1168 scrub_peb_count = 0;
1169 erase_peb_count = 0;
1172 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1173 fm_pos += sizeof(*fmpl1);
1174 fmpl1->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1175 fmpl1->size = cpu_to_be16(ubi->fm_pool.size);
1176 fmpl1->max_size = cpu_to_be16(ubi->fm_pool.max_size);
1178 for (i = 0; i < ubi->fm_pool.size; i++)
1179 fmpl1->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
1181 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1182 fm_pos += sizeof(*fmpl2);
1183 fmpl2->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1184 fmpl2->size = cpu_to_be16(ubi->fm_wl_pool.size);
1185 fmpl2->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
1187 for (i = 0; i < ubi->fm_wl_pool.size; i++)
1188 fmpl2->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
1190 for (node = rb_first(&ubi->free); node; node = rb_next(node)) {
1191 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1192 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1194 fec->pnum = cpu_to_be32(wl_e->pnum);
1195 fec->ec = cpu_to_be32(wl_e->ec);
1198 fm_pos += sizeof(*fec);
1199 ubi_assert(fm_pos <= ubi->fm_size);
1201 fmh->free_peb_count = cpu_to_be32(free_peb_count);
1203 for (node = rb_first(&ubi->used); node; node = rb_next(node)) {
1204 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1205 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1207 fec->pnum = cpu_to_be32(wl_e->pnum);
1208 fec->ec = cpu_to_be32(wl_e->ec);
1211 fm_pos += sizeof(*fec);
1212 ubi_assert(fm_pos <= ubi->fm_size);
1214 fmh->used_peb_count = cpu_to_be32(used_peb_count);
1216 for (node = rb_first(&ubi->scrub); node; node = rb_next(node)) {
1217 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1218 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1220 fec->pnum = cpu_to_be32(wl_e->pnum);
1221 fec->ec = cpu_to_be32(wl_e->ec);
1224 fm_pos += sizeof(*fec);
1225 ubi_assert(fm_pos <= ubi->fm_size);
1227 fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
1230 list_for_each_entry(ubi_wrk, &ubi->works, list) {
1231 if (ubi_is_erase_work(ubi_wrk)) {
1235 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1237 fec->pnum = cpu_to_be32(wl_e->pnum);
1238 fec->ec = cpu_to_be32(wl_e->ec);
1241 fm_pos += sizeof(*fec);
1242 ubi_assert(fm_pos <= ubi->fm_size);
1245 fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
1247 for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
1248 vol = ubi->volumes[i];
1255 fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
1256 fm_pos += sizeof(*fvh);
1257 ubi_assert(fm_pos <= ubi->fm_size);
1259 fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
1260 fvh->vol_id = cpu_to_be32(vol->vol_id);
1261 fvh->vol_type = vol->vol_type;
1262 fvh->used_ebs = cpu_to_be32(vol->used_ebs);
1263 fvh->data_pad = cpu_to_be32(vol->data_pad);
1264 fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
1266 ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
1267 vol->vol_type == UBI_STATIC_VOLUME);
1269 feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
1270 fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
1271 ubi_assert(fm_pos <= ubi->fm_size);
1273 for (j = 0; j < vol->reserved_pebs; j++)
1274 feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]);
1276 feba->reserved_pebs = cpu_to_be32(j);
1277 feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
1279 fmh->vol_count = cpu_to_be32(vol_count);
1280 fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
1282 avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1285 spin_unlock(&ubi->wl_lock);
1286 spin_unlock(&ubi->volumes_lock);
1288 dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
1289 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
1291 ubi_err("unable to write vid_hdr to fastmap SB!");
1295 for (i = 0; i < new_fm->used_blocks; i++) {
1296 fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
1297 fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
1301 fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
1304 for (i = 1; i < new_fm->used_blocks; i++) {
1305 dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1306 dvhdr->lnum = cpu_to_be32(i);
1307 dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1308 new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
1309 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
1311 ubi_err("unable to write vid_hdr to PEB %i!",
1312 new_fm->e[i]->pnum);
1317 for (i = 0; i < new_fm->used_blocks; i++) {
1318 ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size),
1319 new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size);
1321 ubi_err("unable to write fastmap to PEB %i!",
1322 new_fm->e[i]->pnum);
1330 dbg_bld("fastmap written!");
1333 ubi_free_vid_hdr(ubi, avhdr);
1334 ubi_free_vid_hdr(ubi, dvhdr);
1340 * erase_block - Manually erase a PEB.
1341 * @ubi: UBI device object
1342 * @pnum: PEB to be erased
1344 * Returns the new EC value on success, < 0 indicates an internal error.
1346 static int erase_block(struct ubi_device *ubi, int pnum)
1349 struct ubi_ec_hdr *ec_hdr;
1352 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1356 ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1359 else if (ret && ret != UBI_IO_BITFLIPS) {
1364 ret = ubi_io_sync_erase(ubi, pnum, 0);
1368 ec = be64_to_cpu(ec_hdr->ec);
1370 if (ec > UBI_MAX_ERASECOUNTER) {
1375 ec_hdr->ec = cpu_to_be64(ec);
1376 ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
1387 * invalidate_fastmap - destroys a fastmap.
1388 * @ubi: UBI device object
1389 * @fm: the fastmap to be destroyed
1391 * Returns 0 on success, < 0 indicates an internal error.
1393 static int invalidate_fastmap(struct ubi_device *ubi,
1394 struct ubi_fastmap_layout *fm)
1397 struct ubi_vid_hdr *vh;
1399 ret = erase_block(ubi, fm->e[0]->pnum);
1403 vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1407 /* deleting the current fastmap SB is not enough, an old SB may exist,
1408 * so create a (corrupted) SB such that fastmap will find it and fall
1409 * back to scanning mode in any case */
1410 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1411 ret = ubi_io_write_vid_hdr(ubi, fm->e[0]->pnum, vh);
1417 * ubi_update_fastmap - will be called by UBI if a volume changes or
1418 * a fastmap pool becomes full.
1419 * @ubi: UBI device object
1421 * Returns 0 on success, < 0 indicates an internal error.
1423 int ubi_update_fastmap(struct ubi_device *ubi)
1426 struct ubi_fastmap_layout *new_fm, *old_fm;
1427 struct ubi_wl_entry *tmp_e;
1429 mutex_lock(&ubi->fm_mutex);
1431 ubi_refill_pools(ubi);
1433 if (ubi->ro_mode || ubi->fm_disabled) {
1434 mutex_unlock(&ubi->fm_mutex);
1438 ret = ubi_ensure_anchor_pebs(ubi);
1440 mutex_unlock(&ubi->fm_mutex);
1444 new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
1446 mutex_unlock(&ubi->fm_mutex);
1450 new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
1452 for (i = 0; i < new_fm->used_blocks; i++) {
1453 new_fm->e[i] = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1454 if (!new_fm->e[i]) {
1456 kfree(new_fm->e[i]);
1459 mutex_unlock(&ubi->fm_mutex);
1467 if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
1468 ubi_err("fastmap too large");
1473 for (i = 1; i < new_fm->used_blocks; i++) {
1474 spin_lock(&ubi->wl_lock);
1475 tmp_e = ubi_wl_get_fm_peb(ubi, 0);
1476 spin_unlock(&ubi->wl_lock);
1478 if (!tmp_e && !old_fm) {
1480 ubi_err("could not get any free erase block");
1482 for (j = 1; j < i; j++)
1483 ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
1487 } else if (!tmp_e && old_fm) {
1488 ret = erase_block(ubi, old_fm->e[i]->pnum);
1492 for (j = 1; j < i; j++)
1493 ubi_wl_put_fm_peb(ubi, new_fm->e[j],
1496 ubi_err("could not erase old fastmap PEB");
1500 new_fm->e[i]->pnum = old_fm->e[i]->pnum;
1501 new_fm->e[i]->ec = old_fm->e[i]->ec;
1503 new_fm->e[i]->pnum = tmp_e->pnum;
1504 new_fm->e[i]->ec = tmp_e->ec;
1507 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1508 old_fm->to_be_tortured[i]);
1512 spin_lock(&ubi->wl_lock);
1513 tmp_e = ubi_wl_get_fm_peb(ubi, 1);
1514 spin_unlock(&ubi->wl_lock);
1517 /* no fresh anchor PEB was found, reuse the old one */
1519 ret = erase_block(ubi, old_fm->e[0]->pnum);
1522 ubi_err("could not erase old anchor PEB");
1524 for (i = 1; i < new_fm->used_blocks; i++)
1525 ubi_wl_put_fm_peb(ubi, new_fm->e[i],
1530 new_fm->e[0]->pnum = old_fm->e[0]->pnum;
1531 new_fm->e[0]->ec = ret;
1533 /* we've got a new anchor PEB, return the old one */
1534 ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
1535 old_fm->to_be_tortured[0]);
1537 new_fm->e[0]->pnum = tmp_e->pnum;
1538 new_fm->e[0]->ec = tmp_e->ec;
1543 ubi_err("could not find any anchor PEB");
1545 for (i = 1; i < new_fm->used_blocks; i++)
1546 ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
1552 new_fm->e[0]->pnum = tmp_e->pnum;
1553 new_fm->e[0]->ec = tmp_e->ec;
1556 down_write(&ubi->work_sem);
1557 down_write(&ubi->fm_sem);
1558 ret = ubi_write_fastmap(ubi, new_fm);
1559 up_write(&ubi->fm_sem);
1560 up_write(&ubi->work_sem);
1566 mutex_unlock(&ubi->fm_mutex);
1573 ubi_warn("Unable to write new fastmap, err=%i", ret);
1577 ret = invalidate_fastmap(ubi, old_fm);
1579 ubi_err("Unable to invalidiate current fastmap!");