2 * Copyright (c) 2012 Linutronix GmbH
3 * Author: Richard Weinberger <richard@nod.at>
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/crc32.h>
14 #include <ubi_uboot.h>
17 #include <linux/compat.h>
18 #include <linux/math64.h>
22 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
23 * @ubi: UBI device description object
25 size_t ubi_calc_fm_size(struct ubi_device *ubi)
29 size = sizeof(struct ubi_fm_hdr) + \
30 sizeof(struct ubi_fm_scan_pool) + \
31 sizeof(struct ubi_fm_scan_pool) + \
32 (ubi->peb_count * sizeof(struct ubi_fm_ec)) + \
33 (sizeof(struct ubi_fm_eba) + \
34 (ubi->peb_count * sizeof(__be32))) + \
35 sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
36 return roundup(size, ubi->leb_size);
41 * new_fm_vhdr - allocate a new volume header for fastmap usage.
42 * @ubi: UBI device description object
43 * @vol_id: the VID of the new header
45 * Returns a new struct ubi_vid_hdr on success.
46 * NULL indicates out of memory.
48 static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id)
50 struct ubi_vid_hdr *new;
52 new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
56 new->vol_type = UBI_VID_DYNAMIC;
57 new->vol_id = cpu_to_be32(vol_id);
59 /* UBI implementations without fastmap support have to delete the
62 new->compat = UBI_COMPAT_DELETE;
69 * add_aeb - create and add a attach erase block to a given list.
70 * @ai: UBI attach info object
71 * @list: the target list
72 * @pnum: PEB number of the new attach erase block
73 * @ec: erease counter of the new LEB
74 * @scrub: scrub this PEB after attaching
76 * Returns 0 on success, < 0 indicates an internal error.
78 static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
79 int pnum, int ec, int scrub)
81 struct ubi_ainf_peb *aeb;
83 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
91 aeb->copy_flag = aeb->sqnum = 0;
93 ai->ec_sum += aeb->ec;
96 if (ai->max_ec < aeb->ec)
99 if (ai->min_ec > aeb->ec)
100 ai->min_ec = aeb->ec;
102 list_add_tail(&aeb->u.list, list);
108 * add_vol - create and add a new volume to ubi_attach_info.
109 * @ai: ubi_attach_info object
110 * @vol_id: VID of the new volume
111 * @used_ebs: number of used EBS
112 * @data_pad: data padding value of the new volume
113 * @vol_type: volume type
114 * @last_eb_bytes: number of bytes in the last LEB
116 * Returns the new struct ubi_ainf_volume on success.
117 * NULL indicates an error.
119 static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id,
120 int used_ebs, int data_pad, u8 vol_type,
123 struct ubi_ainf_volume *av;
124 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
128 av = rb_entry(parent, struct ubi_ainf_volume, rb);
130 if (vol_id > av->vol_id)
132 else if (vol_id > av->vol_id)
136 av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL);
140 av->highest_lnum = av->leb_count = 0;
142 av->used_ebs = used_ebs;
143 av->data_pad = data_pad;
144 av->last_data_size = last_eb_bytes;
146 av->vol_type = vol_type;
149 dbg_bld("found volume (ID %i)", vol_id);
151 rb_link_node(&av->rb, parent, p);
152 rb_insert_color(&av->rb, &ai->volumes);
159 * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
160 * from it's original list.
161 * @ai: ubi_attach_info object
162 * @aeb: the to be assigned SEB
163 * @av: target scan volume
165 static void assign_aeb_to_av(struct ubi_attach_info *ai,
166 struct ubi_ainf_peb *aeb,
167 struct ubi_ainf_volume *av)
169 struct ubi_ainf_peb *tmp_aeb;
170 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
172 p = &av->root.rb_node;
176 tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
177 if (aeb->lnum != tmp_aeb->lnum) {
178 if (aeb->lnum < tmp_aeb->lnum)
188 list_del(&aeb->u.list);
191 rb_link_node(&aeb->u.rb, parent, p);
192 rb_insert_color(&aeb->u.rb, &av->root);
196 * update_vol - inserts or updates a LEB which was found a pool.
197 * @ubi: the UBI device object
198 * @ai: attach info object
199 * @av: the volume this LEB belongs to
200 * @new_vh: the volume header derived from new_aeb
201 * @new_aeb: the AEB to be examined
203 * Returns 0 on success, < 0 indicates an internal error.
205 static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
206 struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh,
207 struct ubi_ainf_peb *new_aeb)
209 struct rb_node **p = &av->root.rb_node, *parent = NULL;
210 struct ubi_ainf_peb *aeb, *victim;
215 aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
217 if (be32_to_cpu(new_vh->lnum) != aeb->lnum) {
218 if (be32_to_cpu(new_vh->lnum) < aeb->lnum)
226 /* This case can happen if the fastmap gets written
227 * because of a volume change (creation, deletion, ..).
228 * Then a PEB can be within the persistent EBA and the pool.
230 if (aeb->pnum == new_aeb->pnum) {
231 ubi_assert(aeb->lnum == new_aeb->lnum);
232 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
237 cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh);
241 /* new_aeb is newer */
243 victim = kmem_cache_alloc(ai->aeb_slab_cache,
248 victim->ec = aeb->ec;
249 victim->pnum = aeb->pnum;
250 list_add_tail(&victim->u.list, &ai->erase);
252 if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
253 av->last_data_size = \
254 be32_to_cpu(new_vh->data_size);
256 dbg_bld("vol %i: AEB %i's PEB %i is the newer",
257 av->vol_id, aeb->lnum, new_aeb->pnum);
259 aeb->ec = new_aeb->ec;
260 aeb->pnum = new_aeb->pnum;
261 aeb->copy_flag = new_vh->copy_flag;
262 aeb->scrub = new_aeb->scrub;
263 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
265 /* new_aeb is older */
267 dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
268 av->vol_id, aeb->lnum, new_aeb->pnum);
269 list_add_tail(&new_aeb->u.list, &ai->erase);
274 /* This LEB is new, let's add it to the volume */
276 if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
277 av->highest_lnum = be32_to_cpu(new_vh->lnum);
278 av->last_data_size = be32_to_cpu(new_vh->data_size);
281 if (av->vol_type == UBI_STATIC_VOLUME)
282 av->used_ebs = be32_to_cpu(new_vh->used_ebs);
286 rb_link_node(&new_aeb->u.rb, parent, p);
287 rb_insert_color(&new_aeb->u.rb, &av->root);
293 * process_pool_aeb - we found a non-empty PEB in a pool.
294 * @ubi: UBI device object
295 * @ai: attach info object
296 * @new_vh: the volume header derived from new_aeb
297 * @new_aeb: the AEB to be examined
299 * Returns 0 on success, < 0 indicates an internal error.
301 static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
302 struct ubi_vid_hdr *new_vh,
303 struct ubi_ainf_peb *new_aeb)
305 struct ubi_ainf_volume *av, *tmp_av = NULL;
306 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
309 if (be32_to_cpu(new_vh->vol_id) == UBI_FM_SB_VOLUME_ID ||
310 be32_to_cpu(new_vh->vol_id) == UBI_FM_DATA_VOLUME_ID) {
311 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
316 /* Find the volume this SEB belongs to */
319 tmp_av = rb_entry(parent, struct ubi_ainf_volume, rb);
321 if (be32_to_cpu(new_vh->vol_id) > tmp_av->vol_id)
323 else if (be32_to_cpu(new_vh->vol_id) < tmp_av->vol_id)
334 ubi_err("orphaned volume in fastmap pool!");
335 return UBI_BAD_FASTMAP;
338 ubi_assert(be32_to_cpu(new_vh->vol_id) == av->vol_id);
340 return update_vol(ubi, ai, av, new_vh, new_aeb);
344 * unmap_peb - unmap a PEB.
345 * If fastmap detects a free PEB in the pool it has to check whether
346 * this PEB has been unmapped after writing the fastmap.
348 * @ai: UBI attach info object
349 * @pnum: The PEB to be unmapped
351 static void unmap_peb(struct ubi_attach_info *ai, int pnum)
353 struct ubi_ainf_volume *av;
354 struct rb_node *node, *node2;
355 struct ubi_ainf_peb *aeb;
357 for (node = rb_first(&ai->volumes); node; node = rb_next(node)) {
358 av = rb_entry(node, struct ubi_ainf_volume, rb);
360 for (node2 = rb_first(&av->root); node2;
361 node2 = rb_next(node2)) {
362 aeb = rb_entry(node2, struct ubi_ainf_peb, u.rb);
363 if (aeb->pnum == pnum) {
364 rb_erase(&aeb->u.rb, &av->root);
365 kmem_cache_free(ai->aeb_slab_cache, aeb);
373 * scan_pool - scans a pool for changed (no longer empty PEBs).
374 * @ubi: UBI device object
375 * @ai: attach info object
376 * @pebs: an array of all PEB numbers in the to be scanned pool
377 * @pool_size: size of the pool (number of entries in @pebs)
378 * @max_sqnum: pointer to the maximal sequence number
379 * @eba_orphans: list of PEBs which need to be scanned
380 * @free: list of PEBs which are most likely free (and go into @ai->free)
382 * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
383 * < 0 indicates an internal error.
386 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
387 int *pebs, int pool_size, unsigned long long *max_sqnum,
388 struct list_head *eba_orphans, struct list_head *freef)
390 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
391 __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
392 struct list_head *eba_orphans, struct list_head *freef)
395 struct ubi_vid_hdr *vh;
396 struct ubi_ec_hdr *ech;
397 struct ubi_ainf_peb *new_aeb, *tmp_aeb;
398 int i, pnum, err, found_orphan, ret = 0;
400 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
404 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
410 dbg_bld("scanning fastmap pool: size = %i", pool_size);
413 * Now scan all PEBs in the pool to find changes which have been made
414 * after the creation of the fastmap
416 for (i = 0; i < pool_size; i++) {
420 pnum = be32_to_cpu(pebs[i]);
422 if (ubi_io_is_bad(ubi, pnum)) {
423 ubi_err("bad PEB in fastmap pool!");
424 ret = UBI_BAD_FASTMAP;
428 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
429 if (err && err != UBI_IO_BITFLIPS) {
430 ubi_err("unable to read EC header! PEB:%i err:%i",
432 ret = err > 0 ? UBI_BAD_FASTMAP : err;
434 } else if (ret == UBI_IO_BITFLIPS)
438 * Older UBI implementations have image_seq set to zero, so
439 * we shouldn't fail if image_seq == 0.
441 image_seq = be32_to_cpu(ech->image_seq);
443 if (image_seq && (image_seq != ubi->image_seq)) {
444 ubi_err("bad image seq: 0x%x, expected: 0x%x",
445 be32_to_cpu(ech->image_seq), ubi->image_seq);
446 ret = UBI_BAD_FASTMAP;
450 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
451 if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
452 unsigned long long ec = be64_to_cpu(ech->ec);
454 dbg_bld("Adding PEB to free: %i", pnum);
455 if (err == UBI_IO_FF_BITFLIPS)
456 add_aeb(ai, freef, pnum, ec, 1);
458 add_aeb(ai, freef, pnum, ec, 0);
460 } else if (err == 0 || err == UBI_IO_BITFLIPS) {
461 dbg_bld("Found non empty PEB:%i in pool", pnum);
463 if (err == UBI_IO_BITFLIPS)
467 list_for_each_entry(tmp_aeb, eba_orphans, u.list) {
468 if (tmp_aeb->pnum == pnum) {
474 list_del(&tmp_aeb->u.list);
475 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
478 new_aeb = kmem_cache_alloc(ai->aeb_slab_cache,
485 new_aeb->ec = be64_to_cpu(ech->ec);
486 new_aeb->pnum = pnum;
487 new_aeb->lnum = be32_to_cpu(vh->lnum);
488 new_aeb->sqnum = be64_to_cpu(vh->sqnum);
489 new_aeb->copy_flag = vh->copy_flag;
490 new_aeb->scrub = scrub;
492 if (*max_sqnum < new_aeb->sqnum)
493 *max_sqnum = new_aeb->sqnum;
495 err = process_pool_aeb(ubi, ai, vh, new_aeb);
497 ret = err > 0 ? UBI_BAD_FASTMAP : err;
501 /* We are paranoid and fall back to scanning mode */
502 ubi_err("fastmap pool PEBs contains damaged PEBs!");
503 ret = err > 0 ? UBI_BAD_FASTMAP : err;
510 ubi_free_vid_hdr(ubi, vh);
516 * count_fastmap_pebs - Counts the PEBs found by fastmap.
517 * @ai: The UBI attach info object
519 static int count_fastmap_pebs(struct ubi_attach_info *ai)
521 struct ubi_ainf_peb *aeb;
522 struct ubi_ainf_volume *av;
523 struct rb_node *rb1, *rb2;
526 list_for_each_entry(aeb, &ai->erase, u.list)
529 list_for_each_entry(aeb, &ai->free, u.list)
532 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
533 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
540 * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
541 * @ubi: UBI device object
542 * @ai: UBI attach info object
543 * @fm: the fastmap to be attached
545 * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
546 * < 0 indicates an internal error.
548 static int ubi_attach_fastmap(struct ubi_device *ubi,
549 struct ubi_attach_info *ai,
550 struct ubi_fastmap_layout *fm)
552 struct list_head used, eba_orphans, freef;
553 struct ubi_ainf_volume *av;
554 struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
555 struct ubi_ec_hdr *ech;
556 struct ubi_fm_sb *fmsb;
557 struct ubi_fm_hdr *fmhdr;
558 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
559 struct ubi_fm_ec *fmec;
560 struct ubi_fm_volhdr *fmvhdr;
561 struct ubi_fm_eba *fm_eba;
562 int ret, i, j, pool_size, wl_pool_size;
563 size_t fm_pos = 0, fm_size = ubi->fm_size;
564 unsigned long long max_sqnum = 0;
565 void *fm_raw = ubi->fm_buf;
567 INIT_LIST_HEAD(&used);
568 INIT_LIST_HEAD(&freef);
569 INIT_LIST_HEAD(&eba_orphans);
570 INIT_LIST_HEAD(&ai->corr);
571 INIT_LIST_HEAD(&ai->free);
572 INIT_LIST_HEAD(&ai->erase);
573 INIT_LIST_HEAD(&ai->alien);
574 ai->volumes = RB_ROOT;
575 ai->min_ec = UBI_MAX_ERASECOUNTER;
577 ai->aeb_slab_cache = kmem_cache_create("ubi_ainf_peb_slab",
578 sizeof(struct ubi_ainf_peb),
580 if (!ai->aeb_slab_cache) {
585 fmsb = (struct ubi_fm_sb *)(fm_raw);
586 ai->max_sqnum = fmsb->sqnum;
587 fm_pos += sizeof(struct ubi_fm_sb);
588 if (fm_pos >= fm_size)
591 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
592 fm_pos += sizeof(*fmhdr);
593 if (fm_pos >= fm_size)
596 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
597 ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
598 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
602 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
603 fm_pos += sizeof(*fmpl1);
604 if (fm_pos >= fm_size)
606 if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
607 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
608 be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
612 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
613 fm_pos += sizeof(*fmpl2);
614 if (fm_pos >= fm_size)
616 if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
617 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
618 be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
622 pool_size = be16_to_cpu(fmpl1->size);
623 wl_pool_size = be16_to_cpu(fmpl2->size);
624 fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
625 fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
627 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
628 ubi_err("bad pool size: %i", pool_size);
632 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
633 ubi_err("bad WL pool size: %i", wl_pool_size);
638 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
639 fm->max_pool_size < 0) {
640 ubi_err("bad maximal pool size: %i", fm->max_pool_size);
644 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
645 fm->max_wl_pool_size < 0) {
646 ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
650 /* read EC values from free list */
651 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
652 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
653 fm_pos += sizeof(*fmec);
654 if (fm_pos >= fm_size)
657 add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
658 be32_to_cpu(fmec->ec), 0);
661 /* read EC values from used list */
662 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
663 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
664 fm_pos += sizeof(*fmec);
665 if (fm_pos >= fm_size)
668 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
669 be32_to_cpu(fmec->ec), 0);
672 /* read EC values from scrub list */
673 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
674 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
675 fm_pos += sizeof(*fmec);
676 if (fm_pos >= fm_size)
679 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
680 be32_to_cpu(fmec->ec), 1);
683 /* read EC values from erase list */
684 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
685 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
686 fm_pos += sizeof(*fmec);
687 if (fm_pos >= fm_size)
690 add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
691 be32_to_cpu(fmec->ec), 1);
694 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
695 ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
697 /* Iterate over all volumes and read their EBA table */
698 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
699 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
700 fm_pos += sizeof(*fmvhdr);
701 if (fm_pos >= fm_size)
704 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
705 ubi_err("bad fastmap vol header magic: 0x%x, " \
707 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
711 av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
712 be32_to_cpu(fmvhdr->used_ebs),
713 be32_to_cpu(fmvhdr->data_pad),
715 be32_to_cpu(fmvhdr->last_eb_bytes));
721 if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
722 ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
724 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
725 fm_pos += sizeof(*fm_eba);
726 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
727 if (fm_pos >= fm_size)
730 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
731 ubi_err("bad fastmap EBA header magic: 0x%x, " \
733 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
737 for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
738 int pnum = be32_to_cpu(fm_eba->pnum[j]);
740 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
744 list_for_each_entry(tmp_aeb, &used, u.list) {
745 if (tmp_aeb->pnum == pnum) {
751 /* This can happen if a PEB is already in an EBA known
752 * by this fastmap but the PEB itself is not in the used
754 * In this case the PEB can be within the fastmap pool
755 * or while writing the fastmap it was in the protection
759 aeb = kmem_cache_alloc(ai->aeb_slab_cache,
768 aeb->pnum = be32_to_cpu(fm_eba->pnum[j]);
770 aeb->scrub = aeb->copy_flag = aeb->sqnum = 0;
771 list_add_tail(&aeb->u.list, &eba_orphans);
777 if (av->highest_lnum <= aeb->lnum)
778 av->highest_lnum = aeb->lnum;
780 assign_aeb_to_av(ai, aeb, av);
782 dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
783 aeb->pnum, aeb->lnum, av->vol_id);
786 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
792 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &eba_orphans,
796 if (ubi_io_is_bad(ubi, tmp_aeb->pnum)) {
797 ubi_err("bad PEB in fastmap EBA orphan list");
798 ret = UBI_BAD_FASTMAP;
803 err = ubi_io_read_ec_hdr(ubi, tmp_aeb->pnum, ech, 0);
804 if (err && err != UBI_IO_BITFLIPS) {
805 ubi_err("unable to read EC header! PEB:%i " \
806 "err:%i", tmp_aeb->pnum, err);
807 ret = err > 0 ? UBI_BAD_FASTMAP : err;
811 } else if (err == UBI_IO_BITFLIPS)
814 tmp_aeb->ec = be64_to_cpu(ech->ec);
815 assign_aeb_to_av(ai, tmp_aeb, av);
821 ret = scan_pool(ubi, ai, fmpl1->pebs, pool_size, &max_sqnum,
822 &eba_orphans, &freef);
826 ret = scan_pool(ubi, ai, fmpl2->pebs, wl_pool_size, &max_sqnum,
827 &eba_orphans, &freef);
831 if (max_sqnum > ai->max_sqnum)
832 ai->max_sqnum = max_sqnum;
834 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &freef, u.list)
835 list_move_tail(&tmp_aeb->u.list, &ai->free);
837 ubi_assert(list_empty(&used));
838 ubi_assert(list_empty(&eba_orphans));
839 ubi_assert(list_empty(&freef));
842 * If fastmap is leaking PEBs (must not happen), raise a
843 * fat warning and fall back to scanning mode.
844 * We do this here because in ubi_wl_init() it's too late
845 * and we cannot fall back to scanning.
848 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
849 ai->bad_peb_count - fm->used_blocks))
852 if (count_fastmap_pebs(ai) != ubi->peb_count -
853 ai->bad_peb_count - fm->used_blocks) {
862 ret = UBI_BAD_FASTMAP;
864 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
865 list_del(&tmp_aeb->u.list);
866 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
868 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &eba_orphans, u.list) {
869 list_del(&tmp_aeb->u.list);
870 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
872 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &freef, u.list) {
873 list_del(&tmp_aeb->u.list);
874 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
881 * ubi_scan_fastmap - scan the fastmap.
882 * @ubi: UBI device object
883 * @ai: UBI attach info to be filled
884 * @fm_anchor: The fastmap starts at this PEB
886 * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
887 * UBI_BAD_FASTMAP if one was found but is not usable.
888 * < 0 indicates an internal error.
890 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
893 struct ubi_fm_sb *fmsb, *fmsb2;
894 struct ubi_vid_hdr *vh;
895 struct ubi_ec_hdr *ech;
896 struct ubi_fastmap_layout *fm;
897 int i, used_blocks, pnum, ret = 0;
900 unsigned long long sqnum = 0;
902 mutex_lock(&ubi->fm_mutex);
903 memset(ubi->fm_buf, 0, ubi->fm_size);
905 fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
911 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
918 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
919 if (ret && ret != UBI_IO_BITFLIPS)
921 else if (ret == UBI_IO_BITFLIPS)
922 fm->to_be_tortured[0] = 1;
924 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
925 ubi_err("bad super block magic: 0x%x, expected: 0x%x",
926 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
927 ret = UBI_BAD_FASTMAP;
931 if (fmsb->version != UBI_FM_FMT_VERSION) {
932 ubi_err("bad fastmap version: %i, expected: %i",
933 fmsb->version, UBI_FM_FMT_VERSION);
934 ret = UBI_BAD_FASTMAP;
938 used_blocks = be32_to_cpu(fmsb->used_blocks);
939 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
940 ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
941 ret = UBI_BAD_FASTMAP;
945 fm_size = ubi->leb_size * used_blocks;
946 if (fm_size != ubi->fm_size) {
947 ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
949 ret = UBI_BAD_FASTMAP;
953 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
959 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
965 for (i = 0; i < used_blocks; i++) {
968 pnum = be32_to_cpu(fmsb->block_loc[i]);
970 if (ubi_io_is_bad(ubi, pnum)) {
971 ret = UBI_BAD_FASTMAP;
975 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
976 if (ret && ret != UBI_IO_BITFLIPS) {
977 ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
980 ret = UBI_BAD_FASTMAP;
982 } else if (ret == UBI_IO_BITFLIPS)
983 fm->to_be_tortured[i] = 1;
985 image_seq = be32_to_cpu(ech->image_seq);
987 ubi->image_seq = image_seq;
990 * Older UBI implementations have image_seq set to zero, so
991 * we shouldn't fail if image_seq == 0.
993 if (image_seq && (image_seq != ubi->image_seq)) {
994 ubi_err("wrong image seq:%d instead of %d",
995 be32_to_cpu(ech->image_seq), ubi->image_seq);
996 ret = UBI_BAD_FASTMAP;
1000 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
1001 if (ret && ret != UBI_IO_BITFLIPS) {
1002 ubi_err("unable to read fastmap block# %i (PEB: %i)",
1008 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
1009 ubi_err("bad fastmap anchor vol_id: 0x%x," \
1011 be32_to_cpu(vh->vol_id),
1012 UBI_FM_SB_VOLUME_ID);
1013 ret = UBI_BAD_FASTMAP;
1017 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
1018 ubi_err("bad fastmap data vol_id: 0x%x," \
1020 be32_to_cpu(vh->vol_id),
1021 UBI_FM_DATA_VOLUME_ID);
1022 ret = UBI_BAD_FASTMAP;
1027 if (sqnum < be64_to_cpu(vh->sqnum))
1028 sqnum = be64_to_cpu(vh->sqnum);
1030 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
1031 ubi->leb_start, ubi->leb_size);
1032 if (ret && ret != UBI_IO_BITFLIPS) {
1033 ubi_err("unable to read fastmap block# %i (PEB: %i, " \
1034 "err: %i)", i, pnum, ret);
1042 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
1043 tmp_crc = be32_to_cpu(fmsb2->data_crc);
1044 fmsb2->data_crc = 0;
1045 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
1046 if (crc != tmp_crc) {
1047 ubi_err("fastmap data CRC is invalid");
1048 ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
1049 ret = UBI_BAD_FASTMAP;
1053 fmsb2->sqnum = sqnum;
1055 fm->used_blocks = used_blocks;
1057 ret = ubi_attach_fastmap(ubi, ai, fm);
1060 ret = UBI_BAD_FASTMAP;
1064 for (i = 0; i < used_blocks; i++) {
1065 struct ubi_wl_entry *e;
1067 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1076 e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
1077 e->ec = be32_to_cpu(fmsb2->block_ec[i]);
1082 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
1083 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
1084 ubi_msg("attached by fastmap");
1085 ubi_msg("fastmap pool size: %d", ubi->fm_pool.max_size);
1086 ubi_msg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
1087 ubi->fm_disabled = 0;
1089 ubi_free_vid_hdr(ubi, vh);
1092 mutex_unlock(&ubi->fm_mutex);
1093 if (ret == UBI_BAD_FASTMAP)
1094 ubi_err("Attach by fastmap failed, doing a full scan!");
1098 ubi_free_vid_hdr(ubi, vh);
1107 * ubi_write_fastmap - writes a fastmap.
1108 * @ubi: UBI device object
1109 * @new_fm: the to be written fastmap
1111 * Returns 0 on success, < 0 indicates an internal error.
1113 static int ubi_write_fastmap(struct ubi_device *ubi,
1114 struct ubi_fastmap_layout *new_fm)
1118 struct ubi_fm_sb *fmsb;
1119 struct ubi_fm_hdr *fmh;
1120 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
1121 struct ubi_fm_ec *fec;
1122 struct ubi_fm_volhdr *fvh;
1123 struct ubi_fm_eba *feba;
1124 struct rb_node *node;
1125 struct ubi_wl_entry *wl_e;
1126 struct ubi_volume *vol;
1127 struct ubi_vid_hdr *avhdr, *dvhdr;
1128 struct ubi_work *ubi_wrk;
1129 int ret, i, j, free_peb_count, used_peb_count, vol_count;
1130 int scrub_peb_count, erase_peb_count;
1132 fm_raw = ubi->fm_buf;
1133 memset(ubi->fm_buf, 0, ubi->fm_size);
1135 avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1141 dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
1147 spin_lock(&ubi->volumes_lock);
1148 spin_lock(&ubi->wl_lock);
1150 fmsb = (struct ubi_fm_sb *)fm_raw;
1151 fm_pos += sizeof(*fmsb);
1152 ubi_assert(fm_pos <= ubi->fm_size);
1154 fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
1155 fm_pos += sizeof(*fmh);
1156 ubi_assert(fm_pos <= ubi->fm_size);
1158 fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
1159 fmsb->version = UBI_FM_FMT_VERSION;
1160 fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
1161 /* the max sqnum will be filled in while *reading* the fastmap */
1164 fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
1167 scrub_peb_count = 0;
1168 erase_peb_count = 0;
1171 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1172 fm_pos += sizeof(*fmpl1);
1173 fmpl1->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1174 fmpl1->size = cpu_to_be16(ubi->fm_pool.size);
1175 fmpl1->max_size = cpu_to_be16(ubi->fm_pool.max_size);
1177 for (i = 0; i < ubi->fm_pool.size; i++)
1178 fmpl1->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
1180 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1181 fm_pos += sizeof(*fmpl2);
1182 fmpl2->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1183 fmpl2->size = cpu_to_be16(ubi->fm_wl_pool.size);
1184 fmpl2->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
1186 for (i = 0; i < ubi->fm_wl_pool.size; i++)
1187 fmpl2->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
1189 for (node = rb_first(&ubi->free); node; node = rb_next(node)) {
1190 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1191 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1193 fec->pnum = cpu_to_be32(wl_e->pnum);
1194 fec->ec = cpu_to_be32(wl_e->ec);
1197 fm_pos += sizeof(*fec);
1198 ubi_assert(fm_pos <= ubi->fm_size);
1200 fmh->free_peb_count = cpu_to_be32(free_peb_count);
1202 for (node = rb_first(&ubi->used); node; node = rb_next(node)) {
1203 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1204 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1206 fec->pnum = cpu_to_be32(wl_e->pnum);
1207 fec->ec = cpu_to_be32(wl_e->ec);
1210 fm_pos += sizeof(*fec);
1211 ubi_assert(fm_pos <= ubi->fm_size);
1213 fmh->used_peb_count = cpu_to_be32(used_peb_count);
1215 for (node = rb_first(&ubi->scrub); node; node = rb_next(node)) {
1216 wl_e = rb_entry(node, struct ubi_wl_entry, u.rb);
1217 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1219 fec->pnum = cpu_to_be32(wl_e->pnum);
1220 fec->ec = cpu_to_be32(wl_e->ec);
1223 fm_pos += sizeof(*fec);
1224 ubi_assert(fm_pos <= ubi->fm_size);
1226 fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
1229 list_for_each_entry(ubi_wrk, &ubi->works, list) {
1230 if (ubi_is_erase_work(ubi_wrk)) {
1234 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1236 fec->pnum = cpu_to_be32(wl_e->pnum);
1237 fec->ec = cpu_to_be32(wl_e->ec);
1240 fm_pos += sizeof(*fec);
1241 ubi_assert(fm_pos <= ubi->fm_size);
1244 fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
1246 for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
1247 vol = ubi->volumes[i];
1254 fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
1255 fm_pos += sizeof(*fvh);
1256 ubi_assert(fm_pos <= ubi->fm_size);
1258 fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
1259 fvh->vol_id = cpu_to_be32(vol->vol_id);
1260 fvh->vol_type = vol->vol_type;
1261 fvh->used_ebs = cpu_to_be32(vol->used_ebs);
1262 fvh->data_pad = cpu_to_be32(vol->data_pad);
1263 fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
1265 ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
1266 vol->vol_type == UBI_STATIC_VOLUME);
1268 feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
1269 fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
1270 ubi_assert(fm_pos <= ubi->fm_size);
1272 for (j = 0; j < vol->reserved_pebs; j++)
1273 feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]);
1275 feba->reserved_pebs = cpu_to_be32(j);
1276 feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
1278 fmh->vol_count = cpu_to_be32(vol_count);
1279 fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
1281 avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1284 spin_unlock(&ubi->wl_lock);
1285 spin_unlock(&ubi->volumes_lock);
1287 dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
1288 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
1290 ubi_err("unable to write vid_hdr to fastmap SB!");
1294 for (i = 0; i < new_fm->used_blocks; i++) {
1295 fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
1296 fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
1300 fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
1303 for (i = 1; i < new_fm->used_blocks; i++) {
1304 dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1305 dvhdr->lnum = cpu_to_be32(i);
1306 dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1307 new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
1308 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
1310 ubi_err("unable to write vid_hdr to PEB %i!",
1311 new_fm->e[i]->pnum);
1316 for (i = 0; i < new_fm->used_blocks; i++) {
1317 ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size),
1318 new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size);
1320 ubi_err("unable to write fastmap to PEB %i!",
1321 new_fm->e[i]->pnum);
1329 dbg_bld("fastmap written!");
1332 ubi_free_vid_hdr(ubi, avhdr);
1333 ubi_free_vid_hdr(ubi, dvhdr);
1339 * erase_block - Manually erase a PEB.
1340 * @ubi: UBI device object
1341 * @pnum: PEB to be erased
1343 * Returns the new EC value on success, < 0 indicates an internal error.
1345 static int erase_block(struct ubi_device *ubi, int pnum)
1348 struct ubi_ec_hdr *ec_hdr;
1351 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1355 ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1358 else if (ret && ret != UBI_IO_BITFLIPS) {
1363 ret = ubi_io_sync_erase(ubi, pnum, 0);
1367 ec = be64_to_cpu(ec_hdr->ec);
1369 if (ec > UBI_MAX_ERASECOUNTER) {
1374 ec_hdr->ec = cpu_to_be64(ec);
1375 ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
1386 * invalidate_fastmap - destroys a fastmap.
1387 * @ubi: UBI device object
1388 * @fm: the fastmap to be destroyed
1390 * Returns 0 on success, < 0 indicates an internal error.
1392 static int invalidate_fastmap(struct ubi_device *ubi,
1393 struct ubi_fastmap_layout *fm)
1396 struct ubi_vid_hdr *vh;
1398 ret = erase_block(ubi, fm->e[0]->pnum);
1402 vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1406 /* deleting the current fastmap SB is not enough, an old SB may exist,
1407 * so create a (corrupted) SB such that fastmap will find it and fall
1408 * back to scanning mode in any case */
1409 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1410 ret = ubi_io_write_vid_hdr(ubi, fm->e[0]->pnum, vh);
1416 * ubi_update_fastmap - will be called by UBI if a volume changes or
1417 * a fastmap pool becomes full.
1418 * @ubi: UBI device object
1420 * Returns 0 on success, < 0 indicates an internal error.
1422 int ubi_update_fastmap(struct ubi_device *ubi)
1425 struct ubi_fastmap_layout *new_fm, *old_fm;
1426 struct ubi_wl_entry *tmp_e;
1428 mutex_lock(&ubi->fm_mutex);
1430 ubi_refill_pools(ubi);
1432 if (ubi->ro_mode || ubi->fm_disabled) {
1433 mutex_unlock(&ubi->fm_mutex);
1437 ret = ubi_ensure_anchor_pebs(ubi);
1439 mutex_unlock(&ubi->fm_mutex);
1443 new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
1445 mutex_unlock(&ubi->fm_mutex);
1449 new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
1451 for (i = 0; i < new_fm->used_blocks; i++) {
1452 new_fm->e[i] = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1453 if (!new_fm->e[i]) {
1455 kfree(new_fm->e[i]);
1458 mutex_unlock(&ubi->fm_mutex);
1466 if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
1467 ubi_err("fastmap too large");
1472 for (i = 1; i < new_fm->used_blocks; i++) {
1473 spin_lock(&ubi->wl_lock);
1474 tmp_e = ubi_wl_get_fm_peb(ubi, 0);
1475 spin_unlock(&ubi->wl_lock);
1477 if (!tmp_e && !old_fm) {
1479 ubi_err("could not get any free erase block");
1481 for (j = 1; j < i; j++)
1482 ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
1486 } else if (!tmp_e && old_fm) {
1487 ret = erase_block(ubi, old_fm->e[i]->pnum);
1491 for (j = 1; j < i; j++)
1492 ubi_wl_put_fm_peb(ubi, new_fm->e[j],
1495 ubi_err("could not erase old fastmap PEB");
1499 new_fm->e[i]->pnum = old_fm->e[i]->pnum;
1500 new_fm->e[i]->ec = old_fm->e[i]->ec;
1502 new_fm->e[i]->pnum = tmp_e->pnum;
1503 new_fm->e[i]->ec = tmp_e->ec;
1506 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1507 old_fm->to_be_tortured[i]);
1511 spin_lock(&ubi->wl_lock);
1512 tmp_e = ubi_wl_get_fm_peb(ubi, 1);
1513 spin_unlock(&ubi->wl_lock);
1516 /* no fresh anchor PEB was found, reuse the old one */
1518 ret = erase_block(ubi, old_fm->e[0]->pnum);
1521 ubi_err("could not erase old anchor PEB");
1523 for (i = 1; i < new_fm->used_blocks; i++)
1524 ubi_wl_put_fm_peb(ubi, new_fm->e[i],
1529 new_fm->e[0]->pnum = old_fm->e[0]->pnum;
1530 new_fm->e[0]->ec = ret;
1532 /* we've got a new anchor PEB, return the old one */
1533 ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
1534 old_fm->to_be_tortured[0]);
1536 new_fm->e[0]->pnum = tmp_e->pnum;
1537 new_fm->e[0]->ec = tmp_e->ec;
1542 ubi_err("could not find any anchor PEB");
1544 for (i = 1; i < new_fm->used_blocks; i++)
1545 ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
1551 new_fm->e[0]->pnum = tmp_e->pnum;
1552 new_fm->e[0]->ec = tmp_e->ec;
1555 down_write(&ubi->work_sem);
1556 down_write(&ubi->fm_sem);
1557 ret = ubi_write_fastmap(ubi, new_fm);
1558 up_write(&ubi->fm_sem);
1559 up_write(&ubi->work_sem);
1565 mutex_unlock(&ubi->fm_mutex);
1572 ubi_warn("Unable to write new fastmap, err=%i", ret);
1576 ret = invalidate_fastmap(ubi, old_fm);
1578 ubi_err("Unable to invalidiate current fastmap!");