2 * Copyright (c) 2012 Linutronix GmbH
3 * Copyright (c) 2014 sigma star gmbh
4 * Author: Richard Weinberger <richard@nod.at>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
17 #include <linux/crc32.h>
18 #include <linux/bitmap.h>
22 * init_seen - allocate memory for used for debugging.
23 * @ubi: UBI device description object
25 static inline unsigned long *init_seen(struct ubi_device *ubi)
29 if (!ubi_dbg_chk_fastmap(ubi))
32 ret = kcalloc(BITS_TO_LONGS(ubi->peb_count), sizeof(unsigned long),
35 return ERR_PTR(-ENOMEM);
41 * free_seen - free the seen logic integer array.
42 * @seen: integer array of @ubi->peb_count size
44 static inline void free_seen(unsigned long *seen)
50 * set_seen - mark a PEB as seen.
51 * @ubi: UBI device description object
52 * @pnum: The PEB to be makred as seen
53 * @seen: integer array of @ubi->peb_count size
55 static inline void set_seen(struct ubi_device *ubi, int pnum, unsigned long *seen)
57 if (!ubi_dbg_chk_fastmap(ubi) || !seen)
64 * self_check_seen - check whether all PEB have been seen by fastmap.
65 * @ubi: UBI device description object
66 * @seen: integer array of @ubi->peb_count size
68 static int self_check_seen(struct ubi_device *ubi, unsigned long *seen)
72 if (!ubi_dbg_chk_fastmap(ubi) || !seen)
75 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
76 if (test_bit(pnum, seen) && ubi->lookuptbl[pnum]) {
77 ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum);
86 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
87 * @ubi: UBI device description object
89 size_t ubi_calc_fm_size(struct ubi_device *ubi)
93 size = sizeof(struct ubi_fm_sb) +
94 sizeof(struct ubi_fm_hdr) +
95 sizeof(struct ubi_fm_scan_pool) +
96 sizeof(struct ubi_fm_scan_pool) +
97 (ubi->peb_count * sizeof(struct ubi_fm_ec)) +
98 (sizeof(struct ubi_fm_eba) +
99 (ubi->peb_count * sizeof(__be32))) +
100 sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
101 return roundup(size, ubi->leb_size);
106 * new_fm_vhdr - allocate a new volume header for fastmap usage.
107 * @ubi: UBI device description object
108 * @vol_id: the VID of the new header
110 * Returns a new struct ubi_vid_hdr on success.
111 * NULL indicates out of memory.
113 static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id)
115 struct ubi_vid_hdr *new;
117 new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
121 new->vol_type = UBI_VID_DYNAMIC;
122 new->vol_id = cpu_to_be32(vol_id);
124 /* UBI implementations without fastmap support have to delete the
127 new->compat = UBI_COMPAT_DELETE;
134 * add_aeb - create and add a attach erase block to a given list.
135 * @ai: UBI attach info object
136 * @list: the target list
137 * @pnum: PEB number of the new attach erase block
138 * @ec: erease counter of the new LEB
139 * @scrub: scrub this PEB after attaching
141 * Returns 0 on success, < 0 indicates an internal error.
143 static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
144 int pnum, int ec, int scrub)
146 struct ubi_ainf_peb *aeb;
148 aeb = ubi_alloc_aeb(ai, pnum, ec);
154 aeb->copy_flag = aeb->sqnum = 0;
156 ai->ec_sum += aeb->ec;
159 if (ai->max_ec < aeb->ec)
160 ai->max_ec = aeb->ec;
162 if (ai->min_ec > aeb->ec)
163 ai->min_ec = aeb->ec;
165 list_add_tail(&aeb->u.list, list);
171 * add_vol - create and add a new volume to ubi_attach_info.
172 * @ai: ubi_attach_info object
173 * @vol_id: VID of the new volume
174 * @used_ebs: number of used EBS
175 * @data_pad: data padding value of the new volume
176 * @vol_type: volume type
177 * @last_eb_bytes: number of bytes in the last LEB
179 * Returns the new struct ubi_ainf_volume on success.
180 * NULL indicates an error.
182 static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id,
183 int used_ebs, int data_pad, u8 vol_type,
186 struct ubi_ainf_volume *av;
188 av = ubi_add_av(ai, vol_id);
192 av->data_pad = data_pad;
193 av->last_data_size = last_eb_bytes;
195 av->vol_type = vol_type;
196 if (av->vol_type == UBI_STATIC_VOLUME)
197 av->used_ebs = used_ebs;
199 dbg_bld("found volume (ID %i)", vol_id);
204 * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
205 * from it's original list.
206 * @ai: ubi_attach_info object
207 * @aeb: the to be assigned SEB
208 * @av: target scan volume
210 static void assign_aeb_to_av(struct ubi_attach_info *ai,
211 struct ubi_ainf_peb *aeb,
212 struct ubi_ainf_volume *av)
214 struct ubi_ainf_peb *tmp_aeb;
215 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
217 p = &av->root.rb_node;
221 tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
222 if (aeb->lnum != tmp_aeb->lnum) {
223 if (aeb->lnum < tmp_aeb->lnum)
233 list_del(&aeb->u.list);
236 rb_link_node(&aeb->u.rb, parent, p);
237 rb_insert_color(&aeb->u.rb, &av->root);
241 * update_vol - inserts or updates a LEB which was found a pool.
242 * @ubi: the UBI device object
243 * @ai: attach info object
244 * @av: the volume this LEB belongs to
245 * @new_vh: the volume header derived from new_aeb
246 * @new_aeb: the AEB to be examined
248 * Returns 0 on success, < 0 indicates an internal error.
250 static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
251 struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh,
252 struct ubi_ainf_peb *new_aeb)
254 struct rb_node **p = &av->root.rb_node, *parent = NULL;
255 struct ubi_ainf_peb *aeb, *victim;
260 aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
262 if (be32_to_cpu(new_vh->lnum) != aeb->lnum) {
263 if (be32_to_cpu(new_vh->lnum) < aeb->lnum)
271 /* This case can happen if the fastmap gets written
272 * because of a volume change (creation, deletion, ..).
273 * Then a PEB can be within the persistent EBA and the pool.
275 if (aeb->pnum == new_aeb->pnum) {
276 ubi_assert(aeb->lnum == new_aeb->lnum);
277 ubi_free_aeb(ai, new_aeb);
282 cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh);
286 /* new_aeb is newer */
288 victim = ubi_alloc_aeb(ai, aeb->ec, aeb->pnum);
292 list_add_tail(&victim->u.list, &ai->erase);
294 if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
296 be32_to_cpu(new_vh->data_size);
298 dbg_bld("vol %i: AEB %i's PEB %i is the newer",
299 av->vol_id, aeb->lnum, new_aeb->pnum);
301 aeb->ec = new_aeb->ec;
302 aeb->pnum = new_aeb->pnum;
303 aeb->copy_flag = new_vh->copy_flag;
304 aeb->scrub = new_aeb->scrub;
305 ubi_free_aeb(ai, new_aeb);
307 /* new_aeb is older */
309 dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it",
310 av->vol_id, aeb->lnum, new_aeb->pnum);
311 list_add_tail(&new_aeb->u.list, &ai->erase);
316 /* This LEB is new, let's add it to the volume */
318 if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) {
319 av->highest_lnum = be32_to_cpu(new_vh->lnum);
320 av->last_data_size = be32_to_cpu(new_vh->data_size);
323 if (av->vol_type == UBI_STATIC_VOLUME)
324 av->used_ebs = be32_to_cpu(new_vh->used_ebs);
328 rb_link_node(&new_aeb->u.rb, parent, p);
329 rb_insert_color(&new_aeb->u.rb, &av->root);
335 * process_pool_aeb - we found a non-empty PEB in a pool.
336 * @ubi: UBI device object
337 * @ai: attach info object
338 * @new_vh: the volume header derived from new_aeb
339 * @new_aeb: the AEB to be examined
341 * Returns 0 on success, < 0 indicates an internal error.
343 static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
344 struct ubi_vid_hdr *new_vh,
345 struct ubi_ainf_peb *new_aeb)
347 int vol_id = be32_to_cpu(new_vh->vol_id);
348 struct ubi_ainf_volume *av;
350 if (vol_id == UBI_FM_SB_VOLUME_ID || vol_id == UBI_FM_DATA_VOLUME_ID) {
351 ubi_free_aeb(ai, new_aeb);
356 /* Find the volume this SEB belongs to */
357 av = ubi_find_av(ai, vol_id);
359 ubi_err(ubi, "orphaned volume in fastmap pool!");
360 ubi_free_aeb(ai, new_aeb);
361 return UBI_BAD_FASTMAP;
364 ubi_assert(vol_id == av->vol_id);
366 return update_vol(ubi, ai, av, new_vh, new_aeb);
370 * unmap_peb - unmap a PEB.
371 * If fastmap detects a free PEB in the pool it has to check whether
372 * this PEB has been unmapped after writing the fastmap.
374 * @ai: UBI attach info object
375 * @pnum: The PEB to be unmapped
377 static void unmap_peb(struct ubi_attach_info *ai, int pnum)
379 struct ubi_ainf_volume *av;
380 struct rb_node *node, *node2;
381 struct ubi_ainf_peb *aeb;
383 ubi_rb_for_each_entry(node, av, &ai->volumes, rb) {
384 ubi_rb_for_each_entry(node2, aeb, &av->root, u.rb) {
385 if (aeb->pnum == pnum) {
386 rb_erase(&aeb->u.rb, &av->root);
388 ubi_free_aeb(ai, aeb);
396 * scan_pool - scans a pool for changed (no longer empty PEBs).
397 * @ubi: UBI device object
398 * @ai: attach info object
399 * @pebs: an array of all PEB numbers in the to be scanned pool
400 * @pool_size: size of the pool (number of entries in @pebs)
401 * @max_sqnum: pointer to the maximal sequence number
402 * @free: list of PEBs which are most likely free (and go into @ai->free)
404 * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned.
405 * < 0 indicates an internal error.
407 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
408 __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
409 struct list_head *free)
411 struct ubi_vid_hdr *vh;
412 struct ubi_ec_hdr *ech;
413 struct ubi_ainf_peb *new_aeb;
414 int i, pnum, err, ret = 0;
416 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
420 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
426 dbg_bld("scanning fastmap pool: size = %i", pool_size);
429 * Now scan all PEBs in the pool to find changes which have been made
430 * after the creation of the fastmap
432 for (i = 0; i < pool_size; i++) {
436 pnum = be32_to_cpu(pebs[i]);
438 if (ubi_io_is_bad(ubi, pnum)) {
439 ubi_err(ubi, "bad PEB in fastmap pool!");
440 ret = UBI_BAD_FASTMAP;
444 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
445 if (err && err != UBI_IO_BITFLIPS) {
446 ubi_err(ubi, "unable to read EC header! PEB:%i err:%i",
448 ret = err > 0 ? UBI_BAD_FASTMAP : err;
450 } else if (err == UBI_IO_BITFLIPS)
454 * Older UBI implementations have image_seq set to zero, so
455 * we shouldn't fail if image_seq == 0.
457 image_seq = be32_to_cpu(ech->image_seq);
459 if (image_seq && (image_seq != ubi->image_seq)) {
460 ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x",
461 be32_to_cpu(ech->image_seq), ubi->image_seq);
462 ret = UBI_BAD_FASTMAP;
466 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
467 if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
468 unsigned long long ec = be64_to_cpu(ech->ec);
470 dbg_bld("Adding PEB to free: %i", pnum);
472 if (err == UBI_IO_FF_BITFLIPS)
475 add_aeb(ai, free, pnum, ec, scrub);
477 } else if (err == 0 || err == UBI_IO_BITFLIPS) {
478 dbg_bld("Found non empty PEB:%i in pool", pnum);
480 if (err == UBI_IO_BITFLIPS)
483 new_aeb = ubi_alloc_aeb(ai, pnum, be64_to_cpu(ech->ec));
489 new_aeb->lnum = be32_to_cpu(vh->lnum);
490 new_aeb->sqnum = be64_to_cpu(vh->sqnum);
491 new_aeb->copy_flag = vh->copy_flag;
492 new_aeb->scrub = scrub;
494 if (*max_sqnum < new_aeb->sqnum)
495 *max_sqnum = new_aeb->sqnum;
497 err = process_pool_aeb(ubi, ai, vh, new_aeb);
499 ret = err > 0 ? UBI_BAD_FASTMAP : err;
503 /* We are paranoid and fall back to scanning mode */
504 ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!");
505 ret = err > 0 ? UBI_BAD_FASTMAP : err;
512 ubi_free_vid_hdr(ubi, vh);
518 * count_fastmap_pebs - Counts the PEBs found by fastmap.
519 * @ai: The UBI attach info object
521 static int count_fastmap_pebs(struct ubi_attach_info *ai)
523 struct ubi_ainf_peb *aeb;
524 struct ubi_ainf_volume *av;
525 struct rb_node *rb1, *rb2;
528 list_for_each_entry(aeb, &ai->erase, u.list)
531 list_for_each_entry(aeb, &ai->free, u.list)
534 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
535 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
542 * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
543 * @ubi: UBI device object
544 * @ai: UBI attach info object
545 * @fm: the fastmap to be attached
547 * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable.
548 * < 0 indicates an internal error.
550 static int ubi_attach_fastmap(struct ubi_device *ubi,
551 struct ubi_attach_info *ai,
552 struct ubi_fastmap_layout *fm)
554 struct list_head used, free;
555 struct ubi_ainf_volume *av;
556 struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb;
557 struct ubi_fm_sb *fmsb;
558 struct ubi_fm_hdr *fmhdr;
559 struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
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(&free);
570 ai->min_ec = UBI_MAX_ERASECOUNTER;
572 fmsb = (struct ubi_fm_sb *)(fm_raw);
573 ai->max_sqnum = fmsb->sqnum;
574 fm_pos += sizeof(struct ubi_fm_sb);
575 if (fm_pos >= fm_size)
578 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
579 fm_pos += sizeof(*fmhdr);
580 if (fm_pos >= fm_size)
583 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
584 ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x",
585 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
589 fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
590 fm_pos += sizeof(*fmpl);
591 if (fm_pos >= fm_size)
593 if (be32_to_cpu(fmpl->magic) != UBI_FM_POOL_MAGIC) {
594 ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x",
595 be32_to_cpu(fmpl->magic), UBI_FM_POOL_MAGIC);
599 fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
600 fm_pos += sizeof(*fmpl_wl);
601 if (fm_pos >= fm_size)
603 if (be32_to_cpu(fmpl_wl->magic) != UBI_FM_POOL_MAGIC) {
604 ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x",
605 be32_to_cpu(fmpl_wl->magic), UBI_FM_POOL_MAGIC);
609 pool_size = be16_to_cpu(fmpl->size);
610 wl_pool_size = be16_to_cpu(fmpl_wl->size);
611 fm->max_pool_size = be16_to_cpu(fmpl->max_size);
612 fm->max_wl_pool_size = be16_to_cpu(fmpl_wl->max_size);
614 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
615 ubi_err(ubi, "bad pool size: %i", pool_size);
619 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
620 ubi_err(ubi, "bad WL pool size: %i", wl_pool_size);
625 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
626 fm->max_pool_size < 0) {
627 ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size);
631 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
632 fm->max_wl_pool_size < 0) {
633 ubi_err(ubi, "bad maximal WL pool size: %i",
634 fm->max_wl_pool_size);
638 /* read EC values from free list */
639 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
640 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
641 fm_pos += sizeof(*fmec);
642 if (fm_pos >= fm_size)
645 add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum),
646 be32_to_cpu(fmec->ec), 0);
649 /* read EC values from used list */
650 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
651 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
652 fm_pos += sizeof(*fmec);
653 if (fm_pos >= fm_size)
656 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
657 be32_to_cpu(fmec->ec), 0);
660 /* read EC values from scrub list */
661 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
662 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
663 fm_pos += sizeof(*fmec);
664 if (fm_pos >= fm_size)
667 add_aeb(ai, &used, be32_to_cpu(fmec->pnum),
668 be32_to_cpu(fmec->ec), 1);
671 /* read EC values from erase list */
672 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
673 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
674 fm_pos += sizeof(*fmec);
675 if (fm_pos >= fm_size)
678 add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum),
679 be32_to_cpu(fmec->ec), 1);
682 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
683 ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count);
685 /* Iterate over all volumes and read their EBA table */
686 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
687 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
688 fm_pos += sizeof(*fmvhdr);
689 if (fm_pos >= fm_size)
692 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
693 ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x",
694 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
698 av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id),
699 be32_to_cpu(fmvhdr->used_ebs),
700 be32_to_cpu(fmvhdr->data_pad),
702 be32_to_cpu(fmvhdr->last_eb_bytes));
706 if (PTR_ERR(av) == -EINVAL) {
707 ubi_err(ubi, "volume (ID %i) already exists",
713 if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id))
714 ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id);
716 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
717 fm_pos += sizeof(*fm_eba);
718 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
719 if (fm_pos >= fm_size)
722 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
723 ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x",
724 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
728 for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) {
729 int pnum = be32_to_cpu(fm_eba->pnum[j]);
735 list_for_each_entry(tmp_aeb, &used, u.list) {
736 if (tmp_aeb->pnum == pnum) {
743 ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum);
749 if (av->highest_lnum <= aeb->lnum)
750 av->highest_lnum = aeb->lnum;
752 assign_aeb_to_av(ai, aeb, av);
754 dbg_bld("inserting PEB:%i (LEB %i) to vol %i",
755 aeb->pnum, aeb->lnum, av->vol_id);
759 ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free);
763 ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free);
767 if (max_sqnum > ai->max_sqnum)
768 ai->max_sqnum = max_sqnum;
770 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
771 list_move_tail(&tmp_aeb->u.list, &ai->free);
773 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list)
774 list_move_tail(&tmp_aeb->u.list, &ai->erase);
776 ubi_assert(list_empty(&free));
779 * If fastmap is leaking PEBs (must not happen), raise a
780 * fat warning and fall back to scanning mode.
781 * We do this here because in ubi_wl_init() it's too late
782 * and we cannot fall back to scanning.
784 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
785 ai->bad_peb_count - fm->used_blocks))
791 ret = UBI_BAD_FASTMAP;
793 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
794 list_del(&tmp_aeb->u.list);
795 ubi_free_aeb(ai, tmp_aeb);
797 list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
798 list_del(&tmp_aeb->u.list);
799 ubi_free_aeb(ai, tmp_aeb);
806 * find_fm_anchor - find the most recent Fastmap superblock (anchor)
807 * @ai: UBI attach info to be filled
809 static int find_fm_anchor(struct ubi_attach_info *ai)
812 struct ubi_ainf_peb *aeb;
813 unsigned long long max_sqnum = 0;
815 list_for_each_entry(aeb, &ai->fastmap, u.list) {
816 if (aeb->vol_id == UBI_FM_SB_VOLUME_ID && aeb->sqnum > max_sqnum) {
817 max_sqnum = aeb->sqnum;
826 * ubi_scan_fastmap - scan the fastmap.
827 * @ubi: UBI device object
828 * @ai: UBI attach info to be filled
829 * @scan_ai: UBI attach info from the first 64 PEBs,
830 * used to find the most recent Fastmap data structure
832 * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found,
833 * UBI_BAD_FASTMAP if one was found but is not usable.
834 * < 0 indicates an internal error.
836 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
837 struct ubi_attach_info *scan_ai)
839 struct ubi_fm_sb *fmsb, *fmsb2;
840 struct ubi_vid_hdr *vh;
841 struct ubi_ec_hdr *ech;
842 struct ubi_fastmap_layout *fm;
843 struct ubi_ainf_peb *tmp_aeb, *aeb;
844 int i, used_blocks, pnum, fm_anchor, ret = 0;
847 unsigned long long sqnum = 0;
849 fm_anchor = find_fm_anchor(scan_ai);
851 return UBI_NO_FASTMAP;
853 /* Move all (possible) fastmap blocks into our new attach structure. */
854 list_for_each_entry_safe(aeb, tmp_aeb, &scan_ai->fastmap, u.list)
855 list_move_tail(&aeb->u.list, &ai->fastmap);
857 down_write(&ubi->fm_protect);
858 memset(ubi->fm_buf, 0, ubi->fm_size);
860 fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL);
866 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
873 ret = ubi_io_read_data(ubi, fmsb, fm_anchor, 0, sizeof(*fmsb));
874 if (ret && ret != UBI_IO_BITFLIPS)
876 else if (ret == UBI_IO_BITFLIPS)
877 fm->to_be_tortured[0] = 1;
879 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
880 ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x",
881 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
882 ret = UBI_BAD_FASTMAP;
886 if (fmsb->version != UBI_FM_FMT_VERSION) {
887 ubi_err(ubi, "bad fastmap version: %i, expected: %i",
888 fmsb->version, UBI_FM_FMT_VERSION);
889 ret = UBI_BAD_FASTMAP;
893 used_blocks = be32_to_cpu(fmsb->used_blocks);
894 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
895 ubi_err(ubi, "number of fastmap blocks is invalid: %i",
897 ret = UBI_BAD_FASTMAP;
901 fm_size = ubi->leb_size * used_blocks;
902 if (fm_size != ubi->fm_size) {
903 ubi_err(ubi, "bad fastmap size: %zi, expected: %zi",
904 fm_size, ubi->fm_size);
905 ret = UBI_BAD_FASTMAP;
909 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
915 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
921 for (i = 0; i < used_blocks; i++) {
924 pnum = be32_to_cpu(fmsb->block_loc[i]);
926 if (ubi_io_is_bad(ubi, pnum)) {
927 ret = UBI_BAD_FASTMAP;
931 if (i == 0 && pnum != fm_anchor) {
932 ubi_err(ubi, "Fastmap anchor PEB mismatch: PEB: %i vs. %i",
934 ret = UBI_BAD_FASTMAP;
938 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
939 if (ret && ret != UBI_IO_BITFLIPS) {
940 ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)",
943 ret = UBI_BAD_FASTMAP;
945 } else if (ret == UBI_IO_BITFLIPS)
946 fm->to_be_tortured[i] = 1;
948 image_seq = be32_to_cpu(ech->image_seq);
950 ubi->image_seq = image_seq;
953 * Older UBI implementations have image_seq set to zero, so
954 * we shouldn't fail if image_seq == 0.
956 if (image_seq && (image_seq != ubi->image_seq)) {
957 ubi_err(ubi, "wrong image seq:%d instead of %d",
958 be32_to_cpu(ech->image_seq), ubi->image_seq);
959 ret = UBI_BAD_FASTMAP;
963 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
964 if (ret && ret != UBI_IO_BITFLIPS) {
965 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)",
971 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
972 ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x",
973 be32_to_cpu(vh->vol_id),
974 UBI_FM_SB_VOLUME_ID);
975 ret = UBI_BAD_FASTMAP;
979 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
980 ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x",
981 be32_to_cpu(vh->vol_id),
982 UBI_FM_DATA_VOLUME_ID);
983 ret = UBI_BAD_FASTMAP;
988 if (sqnum < be64_to_cpu(vh->sqnum))
989 sqnum = be64_to_cpu(vh->sqnum);
991 ret = ubi_io_read_data(ubi, ubi->fm_buf + (ubi->leb_size * i),
992 pnum, 0, ubi->leb_size);
993 if (ret && ret != UBI_IO_BITFLIPS) {
994 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, "
995 "err: %i)", i, pnum, ret);
1003 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
1004 tmp_crc = be32_to_cpu(fmsb2->data_crc);
1005 fmsb2->data_crc = 0;
1006 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
1007 if (crc != tmp_crc) {
1008 ubi_err(ubi, "fastmap data CRC is invalid");
1009 ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x",
1011 ret = UBI_BAD_FASTMAP;
1015 fmsb2->sqnum = sqnum;
1017 fm->used_blocks = used_blocks;
1019 ret = ubi_attach_fastmap(ubi, ai, fm);
1022 ret = UBI_BAD_FASTMAP;
1026 for (i = 0; i < used_blocks; i++) {
1027 struct ubi_wl_entry *e;
1029 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1038 e->pnum = be32_to_cpu(fmsb2->block_loc[i]);
1039 e->ec = be32_to_cpu(fmsb2->block_ec[i]);
1044 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
1045 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
1046 ubi_msg(ubi, "attached by fastmap");
1047 ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size);
1048 ubi_msg(ubi, "fastmap WL pool size: %d",
1049 ubi->fm_wl_pool.max_size);
1050 ubi->fm_disabled = 0;
1051 ubi->fast_attach = 1;
1053 ubi_free_vid_hdr(ubi, vh);
1056 up_write(&ubi->fm_protect);
1057 if (ret == UBI_BAD_FASTMAP)
1058 ubi_err(ubi, "Attach by fastmap failed, doing a full scan!");
1062 ubi_free_vid_hdr(ubi, vh);
1071 * ubi_write_fastmap - writes a fastmap.
1072 * @ubi: UBI device object
1073 * @new_fm: the to be written fastmap
1075 * Returns 0 on success, < 0 indicates an internal error.
1077 static int ubi_write_fastmap(struct ubi_device *ubi,
1078 struct ubi_fastmap_layout *new_fm)
1082 struct ubi_fm_sb *fmsb;
1083 struct ubi_fm_hdr *fmh;
1084 struct ubi_fm_scan_pool *fmpl, *fmpl_wl;
1085 struct ubi_fm_ec *fec;
1086 struct ubi_fm_volhdr *fvh;
1087 struct ubi_fm_eba *feba;
1088 struct ubi_wl_entry *wl_e;
1089 struct ubi_volume *vol;
1090 struct ubi_vid_hdr *avhdr, *dvhdr;
1091 struct ubi_work *ubi_wrk;
1092 struct rb_node *tmp_rb;
1093 int ret, i, j, free_peb_count, used_peb_count, vol_count;
1094 int scrub_peb_count, erase_peb_count;
1095 unsigned long *seen_pebs = NULL;
1097 fm_raw = ubi->fm_buf;
1098 memset(ubi->fm_buf, 0, ubi->fm_size);
1100 avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1106 dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID);
1112 seen_pebs = init_seen(ubi);
1113 if (IS_ERR(seen_pebs)) {
1114 ret = PTR_ERR(seen_pebs);
1118 spin_lock(&ubi->volumes_lock);
1119 spin_lock(&ubi->wl_lock);
1121 fmsb = (struct ubi_fm_sb *)fm_raw;
1122 fm_pos += sizeof(*fmsb);
1123 ubi_assert(fm_pos <= ubi->fm_size);
1125 fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
1126 fm_pos += sizeof(*fmh);
1127 ubi_assert(fm_pos <= ubi->fm_size);
1129 fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC);
1130 fmsb->version = UBI_FM_FMT_VERSION;
1131 fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks);
1132 /* the max sqnum will be filled in while *reading* the fastmap */
1135 fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC);
1138 scrub_peb_count = 0;
1139 erase_peb_count = 0;
1142 fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1143 fm_pos += sizeof(*fmpl);
1144 fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1145 fmpl->size = cpu_to_be16(ubi->fm_pool.size);
1146 fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size);
1148 for (i = 0; i < ubi->fm_pool.size; i++) {
1149 fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]);
1150 set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs);
1153 fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
1154 fm_pos += sizeof(*fmpl_wl);
1155 fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC);
1156 fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size);
1157 fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size);
1159 for (i = 0; i < ubi->fm_wl_pool.size; i++) {
1160 fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]);
1161 set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs);
1164 ubi_for_each_free_peb(ubi, wl_e, tmp_rb) {
1165 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1167 fec->pnum = cpu_to_be32(wl_e->pnum);
1168 set_seen(ubi, wl_e->pnum, seen_pebs);
1169 fec->ec = cpu_to_be32(wl_e->ec);
1172 fm_pos += sizeof(*fec);
1173 ubi_assert(fm_pos <= ubi->fm_size);
1175 fmh->free_peb_count = cpu_to_be32(free_peb_count);
1177 ubi_for_each_used_peb(ubi, wl_e, tmp_rb) {
1178 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1180 fec->pnum = cpu_to_be32(wl_e->pnum);
1181 set_seen(ubi, wl_e->pnum, seen_pebs);
1182 fec->ec = cpu_to_be32(wl_e->ec);
1185 fm_pos += sizeof(*fec);
1186 ubi_assert(fm_pos <= ubi->fm_size);
1189 ubi_for_each_protected_peb(ubi, i, wl_e) {
1190 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1192 fec->pnum = cpu_to_be32(wl_e->pnum);
1193 set_seen(ubi, wl_e->pnum, seen_pebs);
1194 fec->ec = cpu_to_be32(wl_e->ec);
1197 fm_pos += sizeof(*fec);
1198 ubi_assert(fm_pos <= ubi->fm_size);
1200 fmh->used_peb_count = cpu_to_be32(used_peb_count);
1202 ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) {
1203 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1205 fec->pnum = cpu_to_be32(wl_e->pnum);
1206 set_seen(ubi, wl_e->pnum, seen_pebs);
1207 fec->ec = cpu_to_be32(wl_e->ec);
1210 fm_pos += sizeof(*fec);
1211 ubi_assert(fm_pos <= ubi->fm_size);
1213 fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count);
1216 list_for_each_entry(ubi_wrk, &ubi->works, list) {
1217 if (ubi_is_erase_work(ubi_wrk)) {
1221 fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1223 fec->pnum = cpu_to_be32(wl_e->pnum);
1224 set_seen(ubi, wl_e->pnum, seen_pebs);
1225 fec->ec = cpu_to_be32(wl_e->ec);
1228 fm_pos += sizeof(*fec);
1229 ubi_assert(fm_pos <= ubi->fm_size);
1232 fmh->erase_peb_count = cpu_to_be32(erase_peb_count);
1234 for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) {
1235 vol = ubi->volumes[i];
1242 fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
1243 fm_pos += sizeof(*fvh);
1244 ubi_assert(fm_pos <= ubi->fm_size);
1246 fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC);
1247 fvh->vol_id = cpu_to_be32(vol->vol_id);
1248 fvh->vol_type = vol->vol_type;
1249 fvh->used_ebs = cpu_to_be32(vol->used_ebs);
1250 fvh->data_pad = cpu_to_be32(vol->data_pad);
1251 fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes);
1253 ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME ||
1254 vol->vol_type == UBI_STATIC_VOLUME);
1256 feba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
1257 fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs);
1258 ubi_assert(fm_pos <= ubi->fm_size);
1260 for (j = 0; j < vol->reserved_pebs; j++) {
1261 struct ubi_eba_leb_desc ldesc;
1263 ubi_eba_get_ldesc(vol, j, &ldesc);
1264 feba->pnum[j] = cpu_to_be32(ldesc.pnum);
1267 feba->reserved_pebs = cpu_to_be32(j);
1268 feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC);
1270 fmh->vol_count = cpu_to_be32(vol_count);
1271 fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count);
1273 avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1276 spin_unlock(&ubi->wl_lock);
1277 spin_unlock(&ubi->volumes_lock);
1279 dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
1280 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr);
1282 ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
1286 for (i = 0; i < new_fm->used_blocks; i++) {
1287 fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum);
1288 set_seen(ubi, new_fm->e[i]->pnum, seen_pebs);
1289 fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec);
1293 fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw,
1296 for (i = 1; i < new_fm->used_blocks; i++) {
1297 dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1298 dvhdr->lnum = cpu_to_be32(i);
1299 dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1300 new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
1301 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr);
1303 ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
1304 new_fm->e[i]->pnum);
1309 for (i = 0; i < new_fm->used_blocks; i++) {
1310 ret = ubi_io_write_data(ubi, fm_raw + (i * ubi->leb_size),
1311 new_fm->e[i]->pnum, 0, ubi->leb_size);
1313 ubi_err(ubi, "unable to write fastmap to PEB %i!",
1314 new_fm->e[i]->pnum);
1322 ret = self_check_seen(ubi, seen_pebs);
1323 dbg_bld("fastmap written!");
1326 ubi_free_vid_hdr(ubi, avhdr);
1327 ubi_free_vid_hdr(ubi, dvhdr);
1328 free_seen(seen_pebs);
1334 * erase_block - Manually erase a PEB.
1335 * @ubi: UBI device object
1336 * @pnum: PEB to be erased
1338 * Returns the new EC value on success, < 0 indicates an internal error.
1340 static int erase_block(struct ubi_device *ubi, int pnum)
1343 struct ubi_ec_hdr *ec_hdr;
1346 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1350 ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1353 else if (ret && ret != UBI_IO_BITFLIPS) {
1358 ret = ubi_io_sync_erase(ubi, pnum, 0);
1362 ec = be64_to_cpu(ec_hdr->ec);
1364 if (ec > UBI_MAX_ERASECOUNTER) {
1369 ec_hdr->ec = cpu_to_be64(ec);
1370 ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
1381 * invalidate_fastmap - destroys a fastmap.
1382 * @ubi: UBI device object
1384 * This function ensures that upon next UBI attach a full scan
1385 * is issued. We need this if UBI is about to write a new fastmap
1386 * but is unable to do so. In this case we have two options:
1387 * a) Make sure that the current fastmap will not be usued upon
1388 * attach time and contine or b) fall back to RO mode to have the
1389 * current fastmap in a valid state.
1390 * Returns 0 on success, < 0 indicates an internal error.
1392 static int invalidate_fastmap(struct ubi_device *ubi)
1395 struct ubi_fastmap_layout *fm;
1396 struct ubi_wl_entry *e;
1397 struct ubi_vid_hdr *vh = NULL;
1405 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
1409 vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID);
1414 e = ubi_wl_get_fm_peb(ubi, 1);
1419 * Create fake fastmap such that UBI will fall back
1422 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1423 ret = ubi_io_write_vid_hdr(ubi, e->pnum, vh);
1425 ubi_wl_put_fm_peb(ubi, e, 0, 0);
1429 fm->used_blocks = 1;
1435 ubi_free_vid_hdr(ubi, vh);
1444 * return_fm_pebs - returns all PEBs used by a fastmap back to the
1446 * @ubi: UBI device object
1447 * @fm: fastmap layout object
1449 static void return_fm_pebs(struct ubi_device *ubi,
1450 struct ubi_fastmap_layout *fm)
1457 for (i = 0; i < fm->used_blocks; i++) {
1459 ubi_wl_put_fm_peb(ubi, fm->e[i], i,
1460 fm->to_be_tortured[i]);
1467 * ubi_update_fastmap - will be called by UBI if a volume changes or
1468 * a fastmap pool becomes full.
1469 * @ubi: UBI device object
1471 * Returns 0 on success, < 0 indicates an internal error.
1473 int ubi_update_fastmap(struct ubi_device *ubi)
1476 struct ubi_fastmap_layout *new_fm, *old_fm;
1477 struct ubi_wl_entry *tmp_e;
1479 down_write(&ubi->fm_protect);
1481 ubi_refill_pools(ubi);
1483 if (ubi->ro_mode || ubi->fm_disabled) {
1484 up_write(&ubi->fm_protect);
1488 ret = ubi_ensure_anchor_pebs(ubi);
1490 up_write(&ubi->fm_protect);
1494 new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL);
1496 up_write(&ubi->fm_protect);
1500 new_fm->used_blocks = ubi->fm_size / ubi->leb_size;
1504 if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) {
1505 ubi_err(ubi, "fastmap too large");
1510 for (i = 1; i < new_fm->used_blocks; i++) {
1511 spin_lock(&ubi->wl_lock);
1512 tmp_e = ubi_wl_get_fm_peb(ubi, 0);
1513 spin_unlock(&ubi->wl_lock);
1516 if (old_fm && old_fm->e[i]) {
1517 ret = erase_block(ubi, old_fm->e[i]->pnum);
1519 ubi_err(ubi, "could not erase old fastmap PEB");
1521 for (j = 1; j < i; j++) {
1522 ubi_wl_put_fm_peb(ubi, new_fm->e[j],
1524 new_fm->e[j] = NULL;
1528 new_fm->e[i] = old_fm->e[i];
1529 old_fm->e[i] = NULL;
1531 ubi_err(ubi, "could not get any free erase block");
1533 for (j = 1; j < i; j++) {
1534 ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0);
1535 new_fm->e[j] = NULL;
1542 new_fm->e[i] = tmp_e;
1544 if (old_fm && old_fm->e[i]) {
1545 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1546 old_fm->to_be_tortured[i]);
1547 old_fm->e[i] = NULL;
1552 /* Old fastmap is larger than the new one */
1553 if (old_fm && new_fm->used_blocks < old_fm->used_blocks) {
1554 for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) {
1555 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i,
1556 old_fm->to_be_tortured[i]);
1557 old_fm->e[i] = NULL;
1561 spin_lock(&ubi->wl_lock);
1562 tmp_e = ubi_wl_get_fm_peb(ubi, 1);
1563 spin_unlock(&ubi->wl_lock);
1566 /* no fresh anchor PEB was found, reuse the old one */
1568 ret = erase_block(ubi, old_fm->e[0]->pnum);
1570 ubi_err(ubi, "could not erase old anchor PEB");
1572 for (i = 1; i < new_fm->used_blocks; i++) {
1573 ubi_wl_put_fm_peb(ubi, new_fm->e[i],
1575 new_fm->e[i] = NULL;
1579 new_fm->e[0] = old_fm->e[0];
1580 new_fm->e[0]->ec = ret;
1581 old_fm->e[0] = NULL;
1583 /* we've got a new anchor PEB, return the old one */
1584 ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0,
1585 old_fm->to_be_tortured[0]);
1586 new_fm->e[0] = tmp_e;
1587 old_fm->e[0] = NULL;
1591 ubi_err(ubi, "could not find any anchor PEB");
1593 for (i = 1; i < new_fm->used_blocks; i++) {
1594 ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0);
1595 new_fm->e[i] = NULL;
1601 new_fm->e[0] = tmp_e;
1604 down_write(&ubi->work_sem);
1605 down_write(&ubi->fm_eba_sem);
1606 ret = ubi_write_fastmap(ubi, new_fm);
1607 up_write(&ubi->fm_eba_sem);
1608 up_write(&ubi->work_sem);
1614 up_write(&ubi->fm_protect);
1619 ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret);
1621 ret = invalidate_fastmap(ubi);
1623 ubi_err(ubi, "Unable to invalidiate current fastmap!");
1626 return_fm_pebs(ubi, old_fm);
1627 return_fm_pebs(ubi, new_fm);