1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012 Linutronix GmbH
4 * Copyright (c) 2014 sigma star gmbh
11 #include <dm/devres.h>
12 #include <linux/crc32.h>
13 #include <linux/err.h>
14 #include <u-boot/crc.h>
18 #include <ubi_uboot.h>
19 #include <linux/bug.h>
22 #include <linux/compat.h>
23 #include <linux/math64.h>
27 * init_seen - allocate memory for used for debugging.
28 * @ubi: UBI device description object
30 static inline int *init_seen(struct ubi_device *ubi)
34 if (!ubi_dbg_chk_fastmap(ubi))
37 ret = kcalloc(ubi->peb_count, sizeof(int), GFP_KERNEL);
39 return ERR_PTR(-ENOMEM);
45 * free_seen - free the seen logic integer array.
46 * @seen: integer array of @ubi->peb_count size
48 static inline void free_seen(int *seen)
54 * set_seen - mark a PEB as seen.
55 * @ubi: UBI device description object
56 * @pnum: The PEB to be makred as seen
57 * @seen: integer array of @ubi->peb_count size
59 static inline void set_seen(struct ubi_device *ubi, int pnum, int *seen)
61 if (!ubi_dbg_chk_fastmap(ubi) || !seen)
68 * self_check_seen - check whether all PEB have been seen by fastmap.
69 * @ubi: UBI device description object
70 * @seen: integer array of @ubi->peb_count size
72 static int self_check_seen(struct ubi_device *ubi, int *seen)
76 if (!ubi_dbg_chk_fastmap(ubi) || !seen)
79 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
80 if (!seen[pnum] && ubi->lookuptbl[pnum]) {
81 ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum);
90 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
91 * @ubi: UBI device description object
93 size_t ubi_calc_fm_size(struct ubi_device *ubi)
97 size = sizeof(struct ubi_fm_sb) +
98 sizeof(struct ubi_fm_hdr) +
99 sizeof(struct ubi_fm_scan_pool) +
100 sizeof(struct ubi_fm_scan_pool) +
101 (ubi->peb_count * sizeof(struct ubi_fm_ec)) +
102 (sizeof(struct ubi_fm_eba) +
103 (ubi->peb_count * sizeof(__be32))) +
104 sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
105 return roundup(size, ubi->leb_size);
110 * new_fm_vhdr - allocate a new volume header for fastmap usage.
111 * @ubi: UBI device description object
112 * @vol_id: the VID of the new header
114 * Returns a new struct ubi_vid_hdr on success.
115 * NULL indicates out of memory.
117 static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id)
119 struct ubi_vid_hdr *new;
121 new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
125 new->vol_type = UBI_VID_DYNAMIC;
126 new->vol_id = cpu_to_be32(vol_id);
128 /* UBI implementations without fastmap support have to delete the
131 new->compat = UBI_COMPAT_DELETE;
138 * add_aeb - create and add a attach erase block to a given list.
139 * @ai: UBI attach info object
140 * @list: the target list
141 * @pnum: PEB number of the new attach erase block
142 * @ec: erease counter of the new LEB
143 * @scrub: scrub this PEB after attaching
145 * Returns 0 on success, < 0 indicates an internal error.
147 static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
148 int pnum, int ec, int scrub)
150 struct ubi_ainf_peb *aeb;
152 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
160 aeb->copy_flag = aeb->sqnum = 0;
162 ai->ec_sum += aeb->ec;
165 if (ai->max_ec < aeb->ec)
166 ai->max_ec = aeb->ec;
168 if (ai->min_ec > aeb->ec)
169 ai->min_ec = aeb->ec;
171 list_add_tail(&aeb->u.list, list);
177 * add_vol - create and add a new volume to ubi_attach_info.
178 * @ai: ubi_attach_info object
179 * @vol_id: VID of the new volume
180 * @used_ebs: number of used EBS
181 * @data_pad: data padding value of the new volume
182 * @vol_type: volume type
183 * @last_eb_bytes: number of bytes in the last LEB
185 * Returns the new struct ubi_ainf_volume on success.
186 * NULL indicates an error.
188 static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id,
189 int used_ebs, int data_pad, u8 vol_type,
192 struct ubi_ainf_volume *av;
193 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
197 av = rb_entry(parent, struct ubi_ainf_volume, rb);
199 if (vol_id > av->vol_id)
201 else if (vol_id < av->vol_id)
204 return ERR_PTR(-EINVAL);
207 av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL);
211 av->highest_lnum = av->leb_count = av->used_ebs = 0;
213 av->data_pad = data_pad;
214 av->last_data_size = last_eb_bytes;
216 av->vol_type = vol_type;
218 if (av->vol_type == UBI_STATIC_VOLUME)
219 av->used_ebs = used_ebs;
221 dbg_bld("found volume (ID %i)", vol_id);
223 rb_link_node(&av->rb, parent, p);
224 rb_insert_color(&av->rb, &ai->volumes);
231 * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
232 * from it's original list.
233 * @ai: ubi_attach_info object
234 * @aeb: the to be assigned SEB
235 * @av: target scan volume
237 static void assign_aeb_to_av(struct ubi_attach_info *ai,
238 struct ubi_ainf_peb *aeb,
239 struct ubi_ainf_volume *av)
241 struct ubi_ainf_peb *tmp_aeb;
242 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
244 p = &av->root.rb_node;
248 tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
249 if (aeb->lnum != tmp_aeb->lnum) {
250 if (aeb->lnum < tmp_aeb->lnum)
260 list_del(&aeb->u.list);
263 rb_link_node(&aeb->u.rb, parent, p);
264 rb_insert_color(&aeb->u.rb, &av->root);
268 * update_vol - inserts or updates a LEB which was found a pool.
269 * @ubi: the UBI device object
270 * @ai: attach info object
271 * @av: the volume this LEB belongs to
272 * @new_vh: the volume header derived from new_aeb
273 * @new_aeb: the AEB to be examined
275 * Returns 0 on success, < 0 indicates an internal error.
277 static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
278 struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh,
279 struct ubi_ainf_peb *new_aeb)
281 struct rb_node **p = &av->root.rb_node, *parent = NULL;
282 struct ubi_ainf_peb *aeb, *victim;
287 aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
289 if (be32_to_cpu(new_vh->lnum) != aeb->lnum) {
290 if (be32_to_cpu(new_vh->lnum) < aeb->lnum)
298 /* This case can happen if the fastmap gets written
299 * because of a volume change (creation, deletion, ..).
300 * Then a PEB can be within the persistent EBA and the pool.
302 if (aeb->pnum == new_aeb->pnum) {
303 ubi_assert(aeb->lnum == new_aeb->lnum);
304 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
309 cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh);
313 /* new_aeb is newer */
315 victim = kmem_cache_alloc(ai->aeb_slab_cache,
320 victim->ec = aeb->ec;
321 victim->pnum = aeb->pnum;
322 list_add_tail(&victim->u.list, &ai->erase);
324 if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
326 be32_to_cpu(new_vh->data_size);
328 dbg_bld("vol %i: AEB %i's PEB %i is the newer",
329 av->vol_id, aeb->lnum, new_aeb->pnum);
331 aeb->ec = new_aeb->ec;
332 aeb->pnum = new_aeb->pnum;
333 aeb->copy_flag = new_vh->copy_flag;
334 aeb->scrub = new_aeb->scrub;
335 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
337 /* new_aeb is older */
339 dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
340 av->vol_id, aeb->lnum, new_aeb->pnum);
341 list_add_tail(&new_aeb->u.list, &ai->erase);
346 /* This LEB is new, let's add it to the volume */
348 if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
349 av->highest_lnum = be32_to_cpu(new_vh->lnum);
350 av->last_data_size = be32_to_cpu(new_vh->data_size);
353 if (av->vol_type == UBI_STATIC_VOLUME)
354 av->used_ebs = be32_to_cpu(new_vh->used_ebs);
358 rb_link_node(&new_aeb->u.rb, parent, p);
359 rb_insert_color(&new_aeb->u.rb, &av->root);
365 * process_pool_aeb - we found a non-empty PEB in a pool.
366 * @ubi: UBI device object
367 * @ai: attach info object
368 * @new_vh: the volume header derived from new_aeb
369 * @new_aeb: the AEB to be examined
371 * Returns 0 on success, < 0 indicates an internal error.
373 static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
374 struct ubi_vid_hdr *new_vh,
375 struct ubi_ainf_peb *new_aeb)
377 struct ubi_ainf_volume *av, *tmp_av = NULL;
378 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
381 if (be32_to_cpu(new_vh->vol_id) == UBI_FM_SB_VOLUME_ID ||
382 be32_to_cpu(new_vh->vol_id) == UBI_FM_DATA_VOLUME_ID) {
383 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
388 /* Find the volume this SEB belongs to */
391 tmp_av = rb_entry(parent, struct ubi_ainf_volume, rb);
393 if (be32_to_cpu(new_vh->vol_id) > tmp_av->vol_id)
395 else if (be32_to_cpu(new_vh->vol_id) < tmp_av->vol_id)
406 ubi_err(ubi, "orphaned volume in fastmap pool!");
407 kmem_cache_free(ai->aeb_slab_cache, new_aeb);
408 return UBI_BAD_FASTMAP;
411 ubi_assert(be32_to_cpu(new_vh->vol_id) == av->vol_id);
413 return update_vol(ubi, ai, av, new_vh, new_aeb);
417 * unmap_peb - unmap a PEB.
418 * If fastmap detects a free PEB in the pool it has to check whether
419 * this PEB has been unmapped after writing the fastmap.
421 * @ai: UBI attach info object
422 * @pnum: The PEB to be unmapped
424 static void unmap_peb(struct ubi_attach_info *ai, int pnum)
426 struct ubi_ainf_volume *av;
427 struct rb_node *node, *node2;
428 struct ubi_ainf_peb *aeb;
430 for (node = rb_first(&ai->volumes); node; node = rb_next(node)) {
431 av = rb_entry(node, struct ubi_ainf_volume, rb);
433 for (node2 = rb_first(&av->root); node2;
434 node2 = rb_next(node2)) {
435 aeb = rb_entry(node2, struct ubi_ainf_peb, u.rb);
436 if (aeb->pnum == pnum) {
437 rb_erase(&aeb->u.rb, &av->root);
439 kmem_cache_free(ai->aeb_slab_cache, aeb);
447 * scan_pool - scans a pool for changed (no longer empty PEBs).
448 * @ubi: UBI device object
449 * @ai: attach info object
450 * @pebs: an array of all PEB numbers in the to be scanned pool
451 * @pool_size: size of the pool (number of entries in @pebs)
452 * @max_sqnum: pointer to the maximal sequence number
453 * @free: list of PEBs which are most likely free (and go into @ai->free)
455 * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
456 * < 0 indicates an internal error.
459 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
460 __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
461 struct list_head *free)
463 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
464 __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
465 struct list_head *free)
468 struct ubi_vid_hdr *vh;
469 struct ubi_ec_hdr *ech;
470 struct ubi_ainf_peb *new_aeb;
471 int i, pnum, err, ret = 0;
473 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
477 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
483 dbg_bld("scanning fastmap pool: size = %i", pool_size);
486 * Now scan all PEBs in the pool to find changes which have been made
487 * after the creation of the fastmap
489 for (i = 0; i < pool_size; i++) {
493 pnum = be32_to_cpu(pebs[i]);
495 if (ubi_io_is_bad(ubi, pnum)) {
496 ubi_err(ubi, "bad PEB in fastmap pool!");
497 ret = UBI_BAD_FASTMAP;
501 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
502 if (err && err != UBI_IO_BITFLIPS) {
503 ubi_err(ubi, "unable to read EC header! PEB:%i err:%i",
505 ret = err > 0 ? UBI_BAD_FASTMAP : err;
507 } else if (err == UBI_IO_BITFLIPS)
511 * Older UBI implementations have image_seq set to zero, so
512 * we shouldn't fail if image_seq == 0.
514 image_seq = be32_to_cpu(ech->image_seq);
516 if (image_seq && (image_seq != ubi->image_seq)) {
517 ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x",
518 be32_to_cpu(ech->image_seq), ubi->image_seq);
519 ret = UBI_BAD_FASTMAP;
523 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
524 if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
525 unsigned long long ec = be64_to_cpu(ech->ec);
527 dbg_bld("Adding PEB to free: %i", pnum);
528 if (err == UBI_IO_FF_BITFLIPS)
529 add_aeb(ai, free, pnum, ec, 1);
531 add_aeb(ai, free, pnum, ec, 0);
533 } else if (err == 0 || err == UBI_IO_BITFLIPS) {
534 dbg_bld("Found non empty PEB:%i in pool", pnum);
536 if (err == UBI_IO_BITFLIPS)
539 new_aeb = kmem_cache_alloc(ai->aeb_slab_cache,
546 new_aeb->ec = be64_to_cpu(ech->ec);
547 new_aeb->pnum = pnum;
548 new_aeb->lnum = be32_to_cpu(vh->lnum);
549 new_aeb->sqnum = be64_to_cpu(vh->sqnum);
550 new_aeb->copy_flag = vh->copy_flag;
551 new_aeb->scrub = scrub;
553 if (*max_sqnum < new_aeb->sqnum)
554 *max_sqnum = new_aeb->sqnum;
556 err = process_pool_aeb(ubi, ai, vh, new_aeb);
558 ret = err > 0 ? UBI_BAD_FASTMAP : err;
562 /* We are paranoid and fall back to scanning mode */
563 ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!");
564 ret = err > 0 ? UBI_BAD_FASTMAP : err;
571 ubi_free_vid_hdr(ubi, vh);
577 * count_fastmap_pebs - Counts the PEBs found by fastmap.
578 * @ai: The UBI attach info object
580 static int count_fastmap_pebs(struct ubi_attach_info *ai)
582 struct ubi_ainf_peb *aeb;
583 struct ubi_ainf_volume *av;
584 struct rb_node *rb1, *rb2;
587 list_for_each_entry(aeb, &ai->erase, u.list)
590 list_for_each_entry(aeb, &ai->free, u.list)
593 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
594 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
601 * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
602 * @ubi: UBI device object
603 * @ai: UBI attach info object
604 * @fm: the fastmap to be attached
606 * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
607 * < 0 indicates an internal error.
609 static int ubi_attach_fastmap(struct ubi_device *ubi,
610 struct ubi_attach_info *ai,
611 struct ubi_fastmap_layout *fm)
613 struct list_head used, free;
614 struct ubi_ainf_volume *av;
615 struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
616 struct ubi_fm_sb *fmsb;
617 struct ubi_fm_hdr *fmhdr;
618 struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
619 struct ubi_fm_ec *fmec;
620 struct ubi_fm_volhdr *fmvhdr;
621 struct ubi_fm_eba *fm_eba;
622 int ret, i, j, pool_size, wl_pool_size;
623 size_t fm_pos = 0, fm_size = ubi->fm_size;
624 unsigned long long max_sqnum = 0;
625 void *fm_raw = ubi->fm_buf;
627 INIT_LIST_HEAD(&used);
628 INIT_LIST_HEAD(&free);
629 ai->min_ec = UBI_MAX_ERASECOUNTER;
631 fmsb = (struct ubi_fm_sb *)(fm_raw);
632 ai->max_sqnum = fmsb->sqnum;
633 fm_pos += sizeof(struct ubi_fm_sb);
634 if (fm_pos >= fm_size)
637 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
638 fm_pos += sizeof(*fmhdr);
639 if (fm_pos >= fm_size)
642 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
643 ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x",
644 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
648 fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
649 fm_pos += sizeof(*fmpl);
650 if (fm_pos >= fm_size)
652 if (be32_to_cpu(fmpl->magic) != UBI_FM_POOL_MAGIC) {
653 ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x",
654 be32_to_cpu(fmpl->magic), UBI_FM_POOL_MAGIC);
658 fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
659 fm_pos += sizeof(*fmpl_wl);
660 if (fm_pos >= fm_size)
662 if (be32_to_cpu(fmpl_wl->magic) != UBI_FM_POOL_MAGIC) {
663 ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x",
664 be32_to_cpu(fmpl_wl->magic), UBI_FM_POOL_MAGIC);
668 pool_size = be16_to_cpu(fmpl->size);
669 wl_pool_size = be16_to_cpu(fmpl_wl->size);
670 fm->max_pool_size = be16_to_cpu(fmpl->max_size);
671 fm->max_wl_pool_size = be16_to_cpu(fmpl_wl->max_size);
673 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
674 ubi_err(ubi, "bad pool size: %i", pool_size);
678 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
679 ubi_err(ubi, "bad WL pool size: %i", wl_pool_size);
684 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
685 fm->max_pool_size < 0) {
686 ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size);
690 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
691 fm->max_wl_pool_size < 0) {
692 ubi_err(ubi, "bad maximal WL pool size: %i",
693 fm->max_wl_pool_size);
697 /* read EC values from free list */
698 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
699 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
700 fm_pos += sizeof(*fmec);
701 if (fm_pos >= fm_size)
704 add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
705 be32_to_cpu(fmec->ec), 0);
708 /* read EC values from used list */
709 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
710 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
711 fm_pos += sizeof(*fmec);
712 if (fm_pos >= fm_size)
715 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
716 be32_to_cpu(fmec->ec), 0);
719 /* read EC values from scrub list */
720 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
721 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
722 fm_pos += sizeof(*fmec);
723 if (fm_pos >= fm_size)
726 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
727 be32_to_cpu(fmec->ec), 1);
730 /* read EC values from erase list */
731 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
732 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
733 fm_pos += sizeof(*fmec);
734 if (fm_pos >= fm_size)
737 add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
738 be32_to_cpu(fmec->ec), 1);
741 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
742 ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
744 /* Iterate over all volumes and read their EBA table */
745 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
746 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
747 fm_pos += sizeof(*fmvhdr);
748 if (fm_pos >= fm_size)
751 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
752 ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x",
753 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
757 av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
758 be32_to_cpu(fmvhdr->used_ebs),
759 be32_to_cpu(fmvhdr->data_pad),
761 be32_to_cpu(fmvhdr->last_eb_bytes));
765 if (PTR_ERR(av) == -EINVAL) {
766 ubi_err(ubi, "volume (ID %i) already exists",
772 if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
773 ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
775 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
776 fm_pos += sizeof(*fm_eba);
777 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
778 if (fm_pos >= fm_size)
781 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
782 ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x",
783 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
787 for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
788 int pnum = be32_to_cpu(fm_eba->pnum[j]);
790 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
794 list_for_each_entry(tmp_aeb, &used, u.list) {
795 if (tmp_aeb->pnum == pnum) {
802 ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum);
808 if (av->highest_lnum <= aeb->lnum)
809 av->highest_lnum = aeb->lnum;
811 assign_aeb_to_av(ai, aeb, av);
813 dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
814 aeb->pnum, aeb->lnum, av->vol_id);
818 ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free);
822 ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free);
826 if (max_sqnum > ai->max_sqnum)
827 ai->max_sqnum = max_sqnum;
829 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
830 list_move_tail(&tmp_aeb->u.list, &ai->free);
832 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list)
833 list_move_tail(&tmp_aeb->u.list, &ai->erase);
835 ubi_assert(list_empty(&free));
838 * If fastmap is leaking PEBs (must not happen), raise a
839 * fat warning and fall back to scanning mode.
840 * We do this here because in ubi_wl_init() it's too late
841 * and we cannot fall back to scanning.
844 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
845 ai->bad_peb_count - fm->used_blocks))
848 if (count_fastmap_pebs(ai) != ubi->peb_count -
849 ai->bad_peb_count - fm->used_blocks) {
858 ret = UBI_BAD_FASTMAP;
860 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
861 list_del(&tmp_aeb->u.list);
862 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
864 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
865 list_del(&tmp_aeb->u.list);
866 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
873 * ubi_scan_fastmap - scan the fastmap.
874 * @ubi: UBI device object
875 * @ai: UBI attach info to be filled
876 * @fm_anchor: The fastmap starts at this PEB
878 * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
879 * UBI_BAD_FASTMAP if one was found but is not usable.
880 * < 0 indicates an internal error.
882 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
885 struct ubi_fm_sb *fmsb, *fmsb2;
886 struct ubi_vid_hdr *vh;
887 struct ubi_ec_hdr *ech;
888 struct ubi_fastmap_layout *fm;
889 int i, used_blocks, pnum, ret = 0;
892 unsigned long long sqnum = 0;
894 down_write(&ubi->fm_protect);
895 memset(ubi->fm_buf, 0, ubi->fm_size);
897 fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
903 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
910 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
911 if (ret && ret != UBI_IO_BITFLIPS)
913 else if (ret == UBI_IO_BITFLIPS)
914 fm->to_be_tortured[0] = 1;
916 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
917 ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x",
918 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
919 ret = UBI_BAD_FASTMAP;
923 if (fmsb->version != UBI_FM_FMT_VERSION) {
924 ubi_err(ubi, "bad fastmap version: %i, expected: %i",
925 fmsb->version, UBI_FM_FMT_VERSION);
926 ret = UBI_BAD_FASTMAP;
930 used_blocks = be32_to_cpu(fmsb->used_blocks);
931 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
932 ubi_err(ubi, "number of fastmap blocks is invalid: %i",
934 ret = UBI_BAD_FASTMAP;
938 fm_size = ubi->leb_size * used_blocks;
939 if (fm_size != ubi->fm_size) {
940 ubi_err(ubi, "bad fastmap size: %zi, expected: %zi",
941 fm_size, ubi->fm_size);
942 ret = UBI_BAD_FASTMAP;
946 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
952 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
958 for (i = 0; i < used_blocks; i++) {
961 pnum = be32_to_cpu(fmsb->block_loc[i]);
963 if (ubi_io_is_bad(ubi, pnum)) {
964 ret = UBI_BAD_FASTMAP;
968 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
969 if (ret && ret != UBI_IO_BITFLIPS) {
970 ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)",
973 ret = UBI_BAD_FASTMAP;
975 } else if (ret == UBI_IO_BITFLIPS)
976 fm->to_be_tortured[i] = 1;
978 image_seq = be32_to_cpu(ech->image_seq);
980 ubi->image_seq = image_seq;
983 * Older UBI implementations have image_seq set to zero, so
984 * we shouldn't fail if image_seq == 0.
986 if (image_seq && (image_seq != ubi->image_seq)) {
987 ubi_err(ubi, "wrong image seq:%d instead of %d",
988 be32_to_cpu(ech->image_seq), ubi->image_seq);
989 ret = UBI_BAD_FASTMAP;
993 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
994 if (ret && ret != UBI_IO_BITFLIPS) {
995 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)",
1001 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
1002 ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x",
1003 be32_to_cpu(vh->vol_id),
1004 UBI_FM_SB_VOLUME_ID);
1005 ret = UBI_BAD_FASTMAP;
1009 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
1010 ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x",
1011 be32_to_cpu(vh->vol_id),
1012 UBI_FM_DATA_VOLUME_ID);
1013 ret = UBI_BAD_FASTMAP;
1018 if (sqnum < be64_to_cpu(vh->sqnum))
1019 sqnum = be64_to_cpu(vh->sqnum);
1021 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
1022 ubi->leb_start, ubi->leb_size);
1023 if (ret && ret != UBI_IO_BITFLIPS) {
1024 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, "
1025 "err: %i)", i, pnum, ret);
1033 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
1034 tmp_crc = be32_to_cpu(fmsb2->data_crc);
1035 fmsb2->data_crc = 0;
1036 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
1037 if (crc != tmp_crc) {
1038 ubi_err(ubi, "fastmap data CRC is invalid");
1039 ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x",
1041 ret = UBI_BAD_FASTMAP;
1045 fmsb2->sqnum = sqnum;
1047 fm->used_blocks = used_blocks;
1049 ret = ubi_attach_fastmap(ubi, ai, fm);
1052 ret = UBI_BAD_FASTMAP;
1056 for (i = 0; i < used_blocks; i++) {
1057 struct ubi_wl_entry *e;
1059 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1068 e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
1069 e->ec = be32_to_cpu(fmsb2->block_ec[i]);
1074 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
1075 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
1076 ubi_msg(ubi, "attached by fastmap");
1077 ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size);
1078 ubi_msg(ubi, "fastmap WL pool size: %d",
1079 ubi->fm_wl_pool.max_size);
1080 ubi->fm_disabled = 0;
1082 ubi_free_vid_hdr(ubi, vh);
1085 up_write(&ubi->fm_protect);
1086 if (ret == UBI_BAD_FASTMAP)
1087 ubi_err(ubi, "Attach by fastmap failed, doing a full scan!");
1091 ubi_free_vid_hdr(ubi, vh);
1100 * ubi_write_fastmap - writes a fastmap.
1101 * @ubi: UBI device object
1102 * @new_fm: the to be written fastmap
1104 * Returns 0 on success, < 0 indicates an internal error.
1106 static int ubi_write_fastmap(struct ubi_device *ubi,
1107 struct ubi_fastmap_layout *new_fm)
1111 struct ubi_fm_sb *fmsb;
1112 struct ubi_fm_hdr *fmh;
1113 struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
1114 struct ubi_fm_ec *fec;
1115 struct ubi_fm_volhdr *fvh;
1116 struct ubi_fm_eba *feba;
1117 struct ubi_wl_entry *wl_e;
1118 struct ubi_volume *vol;
1119 struct ubi_vid_hdr *avhdr, *dvhdr;
1120 struct ubi_work *ubi_wrk;
1121 struct rb_node *tmp_rb;
1122 int ret, i, j, free_peb_count, used_peb_count, vol_count;
1123 int scrub_peb_count, erase_peb_count;
1124 int *seen_pebs = NULL;
1126 fm_raw = ubi->fm_buf;
1127 memset(ubi->fm_buf, 0, ubi->fm_size);
1129 avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1135 dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
1141 seen_pebs = init_seen(ubi);
1142 if (IS_ERR(seen_pebs)) {
1143 ret = PTR_ERR(seen_pebs);
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 fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1172 fm_pos += sizeof(*fmpl);
1173 fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1174 fmpl->size = cpu_to_be16(ubi->fm_pool.size);
1175 fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size);
1177 for (i = 0; i < ubi->fm_pool.size; i++) {
1178 fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
1179 set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs);
1182 fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1183 fm_pos += sizeof(*fmpl_wl);
1184 fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1185 fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size);
1186 fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
1188 for (i = 0; i < ubi->fm_wl_pool.size; i++) {
1189 fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
1190 set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs);
1193 ubi_for_each_free_peb(ubi, wl_e, tmp_rb) {
1194 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1196 fec->pnum = cpu_to_be32(wl_e->pnum);
1197 set_seen(ubi, wl_e->pnum, seen_pebs);
1198 fec->ec = cpu_to_be32(wl_e->ec);
1201 fm_pos += sizeof(*fec);
1202 ubi_assert(fm_pos <= ubi->fm_size);
1204 fmh->free_peb_count = cpu_to_be32(free_peb_count);
1206 ubi_for_each_used_peb(ubi, wl_e, tmp_rb) {
1207 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1209 fec->pnum = cpu_to_be32(wl_e->pnum);
1210 set_seen(ubi, wl_e->pnum, seen_pebs);
1211 fec->ec = cpu_to_be32(wl_e->ec);
1214 fm_pos += sizeof(*fec);
1215 ubi_assert(fm_pos <= ubi->fm_size);
1218 ubi_for_each_protected_peb(ubi, i, wl_e) {
1219 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1221 fec->pnum = cpu_to_be32(wl_e->pnum);
1222 set_seen(ubi, wl_e->pnum, seen_pebs);
1223 fec->ec = cpu_to_be32(wl_e->ec);
1226 fm_pos += sizeof(*fec);
1227 ubi_assert(fm_pos <= ubi->fm_size);
1229 fmh->used_peb_count = cpu_to_be32(used_peb_count);
1231 ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) {
1232 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1234 fec->pnum = cpu_to_be32(wl_e->pnum);
1235 set_seen(ubi, wl_e->pnum, seen_pebs);
1236 fec->ec = cpu_to_be32(wl_e->ec);
1239 fm_pos += sizeof(*fec);
1240 ubi_assert(fm_pos <= ubi->fm_size);
1242 fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
1245 list_for_each_entry(ubi_wrk, &ubi->works, list) {
1246 if (ubi_is_erase_work(ubi_wrk)) {
1250 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1252 fec->pnum = cpu_to_be32(wl_e->pnum);
1253 set_seen(ubi, wl_e->pnum, seen_pebs);
1254 fec->ec = cpu_to_be32(wl_e->ec);
1257 fm_pos += sizeof(*fec);
1258 ubi_assert(fm_pos <= ubi->fm_size);
1261 fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
1263 for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
1264 vol = ubi->volumes[i];
1271 fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
1272 fm_pos += sizeof(*fvh);
1273 ubi_assert(fm_pos <= ubi->fm_size);
1275 fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
1276 fvh->vol_id = cpu_to_be32(vol->vol_id);
1277 fvh->vol_type = vol->vol_type;
1278 fvh->used_ebs = cpu_to_be32(vol->used_ebs);
1279 fvh->data_pad = cpu_to_be32(vol->data_pad);
1280 fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
1282 ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
1283 vol->vol_type == UBI_STATIC_VOLUME);
1285 feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
1286 fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
1287 ubi_assert(fm_pos <= ubi->fm_size);
1289 for (j = 0; j < vol->reserved_pebs; j++)
1290 feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]);
1292 feba->reserved_pebs = cpu_to_be32(j);
1293 feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
1295 fmh->vol_count = cpu_to_be32(vol_count);
1296 fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
1298 avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1301 spin_unlock(&ubi->wl_lock);
1302 spin_unlock(&ubi->volumes_lock);
1304 dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
1305 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
1307 ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
1311 for (i = 0; i < new_fm->used_blocks; i++) {
1312 fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
1313 set_seen(ubi, new_fm->e[i]->pnum, seen_pebs);
1314 fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
1318 fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
1321 for (i = 1; i < new_fm->used_blocks; i++) {
1322 dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1323 dvhdr->lnum = cpu_to_be32(i);
1324 dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1325 new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
1326 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
1328 ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
1329 new_fm->e[i]->pnum);
1334 for (i = 0; i < new_fm->used_blocks; i++) {
1335 ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size),
1336 new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size);
1338 ubi_err(ubi, "unable to write fastmap to PEB %i!",
1339 new_fm->e[i]->pnum);
1347 ret = self_check_seen(ubi, seen_pebs);
1348 dbg_bld("fastmap written!");
1351 ubi_free_vid_hdr(ubi, avhdr);
1352 ubi_free_vid_hdr(ubi, dvhdr);
1353 free_seen(seen_pebs);
1359 * erase_block - Manually erase a PEB.
1360 * @ubi: UBI device object
1361 * @pnum: PEB to be erased
1363 * Returns the new EC value on success, < 0 indicates an internal error.
1365 static int erase_block(struct ubi_device *ubi, int pnum)
1368 struct ubi_ec_hdr *ec_hdr;
1371 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1375 ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1378 else if (ret && ret != UBI_IO_BITFLIPS) {
1383 ret = ubi_io_sync_erase(ubi, pnum, 0);
1387 ec = be64_to_cpu(ec_hdr->ec);
1389 if (ec > UBI_MAX_ERASECOUNTER) {
1394 ec_hdr->ec = cpu_to_be64(ec);
1395 ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
1406 * invalidate_fastmap - destroys a fastmap.
1407 * @ubi: UBI device object
1409 * This function ensures that upon next UBI attach a full scan
1410 * is issued. We need this if UBI is about to write a new fastmap
1411 * but is unable to do so. In this case we have two options:
1412 * a) Make sure that the current fastmap will not be usued upon
1413 * attach time and contine or b) fall back to RO mode to have the
1414 * current fastmap in a valid state.
1415 * Returns 0 on success, < 0 indicates an internal error.
1417 static int invalidate_fastmap(struct ubi_device *ubi)
1420 struct ubi_fastmap_layout *fm;
1421 struct ubi_wl_entry *e;
1422 struct ubi_vid_hdr *vh = NULL;
1430 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
1434 vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1439 e = ubi_wl_get_fm_peb(ubi, 1);
1444 * Create fake fastmap such that UBI will fall back
1447 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1448 ret = ubi_io_write_vid_hdr(ubi, e->pnum, vh);
1450 ubi_wl_put_fm_peb(ubi, e, 0, 0);
1454 fm->used_blocks = 1;
1460 ubi_free_vid_hdr(ubi, vh);
1469 * return_fm_pebs - returns all PEBs used by a fastmap back to the
1471 * @ubi: UBI device object
1472 * @fm: fastmap layout object
1474 static void return_fm_pebs(struct ubi_device *ubi,
1475 struct ubi_fastmap_layout *fm)
1482 for (i = 0; i < fm->used_blocks; i++) {
1484 ubi_wl_put_fm_peb(ubi, fm->e[i], i,
1485 fm->to_be_tortured[i]);
1492 * ubi_update_fastmap - will be called by UBI if a volume changes or
1493 * a fastmap pool becomes full.
1494 * @ubi: UBI device object
1496 * Returns 0 on success, < 0 indicates an internal error.
1498 int ubi_update_fastmap(struct ubi_device *ubi)
1501 struct ubi_fastmap_layout *new_fm, *old_fm;
1502 struct ubi_wl_entry *tmp_e;
1504 down_write(&ubi->fm_protect);
1506 ubi_refill_pools(ubi);
1508 if (ubi->ro_mode || ubi->fm_disabled) {
1509 up_write(&ubi->fm_protect);
1513 ret = ubi_ensure_anchor_pebs(ubi);
1515 up_write(&ubi->fm_protect);
1519 new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
1521 up_write(&ubi->fm_protect);
1525 new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
1529 if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
1530 ubi_err(ubi, "fastmap too large");
1535 for (i = 1; i < new_fm->used_blocks; i++) {
1536 spin_lock(&ubi->wl_lock);
1537 tmp_e = ubi_wl_get_fm_peb(ubi, 0);
1538 spin_unlock(&ubi->wl_lock);
1541 if (old_fm && old_fm->e[i]) {
1542 ret = erase_block(ubi, old_fm->e[i]->pnum);
1544 ubi_err(ubi, "could not erase old fastmap PEB");
1546 for (j = 1; j < i; j++) {
1547 ubi_wl_put_fm_peb(ubi, new_fm->e[j],
1549 new_fm->e[j] = NULL;
1553 new_fm->e[i] = old_fm->e[i];
1554 old_fm->e[i] = NULL;
1556 ubi_err(ubi, "could not get any free erase block");
1558 for (j = 1; j < i; j++) {
1559 ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
1560 new_fm->e[j] = NULL;
1567 new_fm->e[i] = tmp_e;
1569 if (old_fm && old_fm->e[i]) {
1570 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1571 old_fm->to_be_tortured[i]);
1572 old_fm->e[i] = NULL;
1577 /* Old fastmap is larger than the new one */
1578 if (old_fm && new_fm->used_blocks < old_fm->used_blocks) {
1579 for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) {
1580 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1581 old_fm->to_be_tortured[i]);
1582 old_fm->e[i] = NULL;
1586 spin_lock(&ubi->wl_lock);
1587 tmp_e = ubi_wl_get_fm_peb(ubi, 1);
1588 spin_unlock(&ubi->wl_lock);
1591 /* no fresh anchor PEB was found, reuse the old one */
1593 ret = erase_block(ubi, old_fm->e[0]->pnum);
1595 ubi_err(ubi, "could not erase old anchor PEB");
1597 for (i = 1; i < new_fm->used_blocks; i++) {
1598 ubi_wl_put_fm_peb(ubi, new_fm->e[i],
1600 new_fm->e[i] = NULL;
1604 new_fm->e[0] = old_fm->e[0];
1605 new_fm->e[0]->ec = ret;
1606 old_fm->e[0] = NULL;
1608 /* we've got a new anchor PEB, return the old one */
1609 ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
1610 old_fm->to_be_tortured[0]);
1611 new_fm->e[0] = tmp_e;
1612 old_fm->e[0] = NULL;
1616 ubi_err(ubi, "could not find any anchor PEB");
1618 for (i = 1; i < new_fm->used_blocks; i++) {
1619 ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
1620 new_fm->e[i] = NULL;
1626 new_fm->e[0] = tmp_e;
1629 down_write(&ubi->work_sem);
1630 down_write(&ubi->fm_eba_sem);
1631 ret = ubi_write_fastmap(ubi, new_fm);
1632 up_write(&ubi->fm_eba_sem);
1633 up_write(&ubi->work_sem);
1639 up_write(&ubi->fm_protect);
1644 ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret);
1646 ret = invalidate_fastmap(ubi);
1648 ubi_err(ubi, "Unable to invalidiate current fastmap!");
1651 return_fm_pebs(ubi, old_fm);
1652 return_fm_pebs(ubi, new_fm);