Lines Matching +full:ubi +full:- +full:volume +full:-

1 // SPDX-License-Identifier: GPL-2.0-only
10 #include "ubi.h"
13 * init_seen - allocate memory for used for debugging.
14 * @ubi: UBI device description object
16 static inline unsigned long *init_seen(struct ubi_device *ubi) in init_seen() argument
20 if (!ubi_dbg_chk_fastmap(ubi)) in init_seen()
23 ret = bitmap_zalloc(ubi->peb_count, GFP_NOFS); in init_seen()
25 return ERR_PTR(-ENOMEM); in init_seen()
31 * free_seen - free the seen logic integer array.
32 * @seen: integer array of @ubi->peb_count size
40 * set_seen - mark a PEB as seen.
41 * @ubi: UBI device description object
43 * @seen: integer array of @ubi->peb_count size
45 static inline void set_seen(struct ubi_device *ubi, int pnum, unsigned long *seen) in set_seen() argument
47 if (!ubi_dbg_chk_fastmap(ubi) || !seen) in set_seen()
54 * self_check_seen - check whether all PEB have been seen by fastmap.
55 * @ubi: UBI device description object
56 * @seen: integer array of @ubi->peb_count size
58 static int self_check_seen(struct ubi_device *ubi, unsigned long *seen) in self_check_seen() argument
62 if (!ubi_dbg_chk_fastmap(ubi) || !seen) in self_check_seen()
65 for (pnum = 0; pnum < ubi->peb_count; pnum++) { in self_check_seen()
66 if (!test_bit(pnum, seen) && ubi->lookuptbl[pnum]) { in self_check_seen()
67 ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum); in self_check_seen()
68 ret = -EINVAL; in self_check_seen()
76 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
77 * @ubi: UBI device description object
79 size_t ubi_calc_fm_size(struct ubi_device *ubi) in ubi_calc_fm_size() argument
87 (ubi->peb_count * sizeof(struct ubi_fm_ec)) + in ubi_calc_fm_size()
91 (ubi->peb_count * sizeof(__be32)); in ubi_calc_fm_size()
92 return roundup(size, ubi->leb_size); in ubi_calc_fm_size()
97 * new_fm_vbuf() - allocate a new volume header for fastmap usage.
98 * @ubi: UBI device description object
104 static struct ubi_vid_io_buf *new_fm_vbuf(struct ubi_device *ubi, int vol_id) in new_fm_vbuf() argument
109 new = ubi_alloc_vid_buf(ubi, GFP_NOFS); in new_fm_vbuf()
114 vh->vol_type = UBI_VID_DYNAMIC; in new_fm_vbuf()
115 vh->vol_id = cpu_to_be32(vol_id); in new_fm_vbuf()
117 /* UBI implementations without fastmap support have to delete the in new_fm_vbuf()
120 vh->compat = UBI_COMPAT_DELETE; in new_fm_vbuf()
127 * add_aeb - create and add a attach erase block to a given list.
128 * @ai: UBI attach info object
143 return -ENOMEM; in add_aeb()
145 aeb->lnum = -1; in add_aeb()
146 aeb->scrub = scrub; in add_aeb()
147 aeb->copy_flag = aeb->sqnum = 0; in add_aeb()
149 ai->ec_sum += aeb->ec; in add_aeb()
150 ai->ec_count++; in add_aeb()
152 if (ai->max_ec < aeb->ec) in add_aeb()
153 ai->max_ec = aeb->ec; in add_aeb()
155 if (ai->min_ec > aeb->ec) in add_aeb()
156 ai->min_ec = aeb->ec; in add_aeb()
158 list_add_tail(&aeb->u.list, list); in add_aeb()
164 * add_vol - create and add a new volume to ubi_attach_info.
166 * @vol_id: VID of the new volume
168 * @data_pad: data padding value of the new volume
169 * @vol_type: volume type
185 av->data_pad = data_pad; in add_vol()
186 av->last_data_size = last_eb_bytes; in add_vol()
187 av->compat = 0; in add_vol()
188 av->vol_type = vol_type; in add_vol()
189 if (av->vol_type == UBI_STATIC_VOLUME) in add_vol()
190 av->used_ebs = used_ebs; in add_vol()
192 dbg_bld("found volume (ID %i)", vol_id); in add_vol()
197 * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
201 * @av: target scan volume
208 struct rb_node **p = &av->root.rb_node, *parent = NULL; in assign_aeb_to_av()
214 if (aeb->lnum != tmp_aeb->lnum) { in assign_aeb_to_av()
215 if (aeb->lnum < tmp_aeb->lnum) in assign_aeb_to_av()
216 p = &(*p)->rb_left; in assign_aeb_to_av()
218 p = &(*p)->rb_right; in assign_aeb_to_av()
225 list_del(&aeb->u.list); in assign_aeb_to_av()
226 av->leb_count++; in assign_aeb_to_av()
228 rb_link_node(&aeb->u.rb, parent, p); in assign_aeb_to_av()
229 rb_insert_color(&aeb->u.rb, &av->root); in assign_aeb_to_av()
233 * update_vol - inserts or updates a LEB which was found a pool.
234 * @ubi: the UBI device object
236 * @av: the volume this LEB belongs to
237 * @new_vh: the volume header derived from new_aeb
242 static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai, in update_vol() argument
246 struct rb_node **p = &av->root.rb_node, *parent = NULL; in update_vol()
254 if (be32_to_cpu(new_vh->lnum) != aeb->lnum) { in update_vol()
255 if (be32_to_cpu(new_vh->lnum) < aeb->lnum) in update_vol()
256 p = &(*p)->rb_left; in update_vol()
258 p = &(*p)->rb_right; in update_vol()
264 * because of a volume change (creation, deletion, ..). in update_vol()
267 if (aeb->pnum == new_aeb->pnum) { in update_vol()
268 ubi_assert(aeb->lnum == new_aeb->lnum); in update_vol()
274 cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh); in update_vol()
280 victim = ubi_alloc_aeb(ai, aeb->pnum, aeb->ec); in update_vol()
282 return -ENOMEM; in update_vol()
284 list_add_tail(&victim->u.list, &ai->erase); in update_vol()
286 if (av->highest_lnum == be32_to_cpu(new_vh->lnum)) in update_vol()
287 av->last_data_size = in update_vol()
288 be32_to_cpu(new_vh->data_size); in update_vol()
291 av->vol_id, aeb->lnum, new_aeb->pnum); in update_vol()
293 aeb->ec = new_aeb->ec; in update_vol()
294 aeb->pnum = new_aeb->pnum; in update_vol()
295 aeb->copy_flag = new_vh->copy_flag; in update_vol()
296 aeb->scrub = new_aeb->scrub; in update_vol()
297 aeb->sqnum = new_aeb->sqnum; in update_vol()
303 av->vol_id, aeb->lnum, new_aeb->pnum); in update_vol()
304 list_add_tail(&new_aeb->u.list, &ai->erase); in update_vol()
309 /* This LEB is new, let's add it to the volume */ in update_vol()
311 if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) { in update_vol()
312 av->highest_lnum = be32_to_cpu(new_vh->lnum); in update_vol()
313 av->last_data_size = be32_to_cpu(new_vh->data_size); in update_vol()
316 if (av->vol_type == UBI_STATIC_VOLUME) in update_vol()
317 av->used_ebs = be32_to_cpu(new_vh->used_ebs); in update_vol()
319 av->leb_count++; in update_vol()
321 rb_link_node(&new_aeb->u.rb, parent, p); in update_vol()
322 rb_insert_color(&new_aeb->u.rb, &av->root); in update_vol()
328 * process_pool_aeb - we found a non-empty PEB in a pool.
329 * @ubi: UBI device object
331 * @new_vh: the volume header derived from new_aeb
336 static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai, in process_pool_aeb() argument
340 int vol_id = be32_to_cpu(new_vh->vol_id); in process_pool_aeb()
349 /* Find the volume this SEB belongs to */ in process_pool_aeb()
352 ubi_err(ubi, "orphaned volume in fastmap pool!"); in process_pool_aeb()
357 ubi_assert(vol_id == av->vol_id); in process_pool_aeb()
359 return update_vol(ubi, ai, av, new_vh, new_aeb); in process_pool_aeb()
363 * unmap_peb - unmap a PEB.
367 * @ai: UBI attach info object
376 ubi_rb_for_each_entry(node, av, &ai->volumes, rb) { in unmap_peb()
377 ubi_rb_for_each_entry(node2, aeb, &av->root, u.rb) { in unmap_peb()
378 if (aeb->pnum == pnum) { in unmap_peb()
379 rb_erase(&aeb->u.rb, &av->root); in unmap_peb()
380 av->leb_count--; in unmap_peb()
389 * scan_pool - scans a pool for changed (no longer empty PEBs).
390 * @ubi: UBI device object
395 * @free: list of PEBs which are most likely free (and go into @ai->free)
400 static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai, in scan_pool() argument
410 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in scan_pool()
412 return -ENOMEM; in scan_pool()
414 vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in scan_pool()
417 return -ENOMEM; in scan_pool()
434 if (ubi_io_is_bad(ubi, pnum)) { in scan_pool()
435 ubi_err(ubi, "bad PEB in fastmap pool!"); in scan_pool()
440 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); in scan_pool()
442 ubi_err(ubi, "unable to read EC header! PEB:%i err:%i", in scan_pool()
450 * Older UBI implementations have image_seq set to zero, so in scan_pool()
453 image_seq = be32_to_cpu(ech->image_seq); in scan_pool()
455 if (image_seq && (image_seq != ubi->image_seq)) { in scan_pool()
456 ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x", in scan_pool()
457 be32_to_cpu(ech->image_seq), ubi->image_seq); in scan_pool()
462 err = ubi_io_read_vid_hdr(ubi, pnum, vb, 0); in scan_pool()
464 unsigned long long ec = be64_to_cpu(ech->ec); in scan_pool()
481 new_aeb = ubi_alloc_aeb(ai, pnum, be64_to_cpu(ech->ec)); in scan_pool()
483 ret = -ENOMEM; in scan_pool()
487 new_aeb->lnum = be32_to_cpu(vh->lnum); in scan_pool()
488 new_aeb->sqnum = be64_to_cpu(vh->sqnum); in scan_pool()
489 new_aeb->copy_flag = vh->copy_flag; in scan_pool()
490 new_aeb->scrub = scrub; in scan_pool()
492 if (*max_sqnum < new_aeb->sqnum) in scan_pool()
493 *max_sqnum = new_aeb->sqnum; in scan_pool()
495 err = process_pool_aeb(ubi, ai, vh, new_aeb); in scan_pool()
502 ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!"); in scan_pool()
516 * count_fastmap_pebs - Counts the PEBs found by fastmap.
517 * @ai: The UBI attach info object
526 list_for_each_entry(aeb, &ai->erase, u.list) in count_fastmap_pebs()
529 list_for_each_entry(aeb, &ai->free, u.list) in count_fastmap_pebs()
532 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) in count_fastmap_pebs()
533 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) in count_fastmap_pebs()
540 * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
541 * @ubi: UBI device object
542 * @ai: UBI attach info object
548 static int ubi_attach_fastmap(struct ubi_device *ubi, in ubi_attach_fastmap() argument
562 size_t fm_pos = 0, fm_size = ubi->fm_size; in ubi_attach_fastmap()
564 void *fm_raw = ubi->fm_buf; in ubi_attach_fastmap()
568 ai->min_ec = UBI_MAX_ERASECOUNTER; in ubi_attach_fastmap()
571 ai->max_sqnum = fmsb->sqnum; in ubi_attach_fastmap()
581 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) { in ubi_attach_fastmap()
582 ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
583 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC); in ubi_attach_fastmap()
591 if (be32_to_cpu(fmpl->magic) != UBI_FM_POOL_MAGIC) { in ubi_attach_fastmap()
592 ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
593 be32_to_cpu(fmpl->magic), UBI_FM_POOL_MAGIC); in ubi_attach_fastmap()
601 if (be32_to_cpu(fmpl_wl->magic) != UBI_FM_POOL_MAGIC) { in ubi_attach_fastmap()
602 ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
603 be32_to_cpu(fmpl_wl->magic), UBI_FM_POOL_MAGIC); in ubi_attach_fastmap()
607 pool_size = be16_to_cpu(fmpl->size); in ubi_attach_fastmap()
608 wl_pool_size = be16_to_cpu(fmpl_wl->size); in ubi_attach_fastmap()
609 fm->max_pool_size = be16_to_cpu(fmpl->max_size); in ubi_attach_fastmap()
610 fm->max_wl_pool_size = be16_to_cpu(fmpl_wl->max_size); in ubi_attach_fastmap()
613 ubi_err(ubi, "bad pool size: %i", pool_size); in ubi_attach_fastmap()
618 ubi_err(ubi, "bad WL pool size: %i", wl_pool_size); in ubi_attach_fastmap()
623 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE || in ubi_attach_fastmap()
624 fm->max_pool_size < 0) { in ubi_attach_fastmap()
625 ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size); in ubi_attach_fastmap()
629 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE || in ubi_attach_fastmap()
630 fm->max_wl_pool_size < 0) { in ubi_attach_fastmap()
631 ubi_err(ubi, "bad maximal WL pool size: %i", in ubi_attach_fastmap()
632 fm->max_wl_pool_size); in ubi_attach_fastmap()
637 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) { in ubi_attach_fastmap()
643 ret = add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum), in ubi_attach_fastmap()
644 be32_to_cpu(fmec->ec), 0); in ubi_attach_fastmap()
650 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) { in ubi_attach_fastmap()
656 ret = add_aeb(ai, &used, be32_to_cpu(fmec->pnum), in ubi_attach_fastmap()
657 be32_to_cpu(fmec->ec), 0); in ubi_attach_fastmap()
663 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) { in ubi_attach_fastmap()
669 ret = add_aeb(ai, &used, be32_to_cpu(fmec->pnum), in ubi_attach_fastmap()
670 be32_to_cpu(fmec->ec), 1); in ubi_attach_fastmap()
676 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) { in ubi_attach_fastmap()
682 ret = add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum), in ubi_attach_fastmap()
683 be32_to_cpu(fmec->ec), 1); in ubi_attach_fastmap()
688 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count); in ubi_attach_fastmap()
689 ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count); in ubi_attach_fastmap()
692 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) { in ubi_attach_fastmap()
698 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) { in ubi_attach_fastmap()
699 ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
700 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC); in ubi_attach_fastmap()
704 av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id), in ubi_attach_fastmap()
705 be32_to_cpu(fmvhdr->used_ebs), in ubi_attach_fastmap()
706 be32_to_cpu(fmvhdr->data_pad), in ubi_attach_fastmap()
707 fmvhdr->vol_type, in ubi_attach_fastmap()
708 be32_to_cpu(fmvhdr->last_eb_bytes)); in ubi_attach_fastmap()
711 if (PTR_ERR(av) == -EEXIST) in ubi_attach_fastmap()
712 ubi_err(ubi, "volume (ID %i) already exists", in ubi_attach_fastmap()
713 fmvhdr->vol_id); in ubi_attach_fastmap()
718 ai->vols_found++; in ubi_attach_fastmap()
719 if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id)) in ubi_attach_fastmap()
720 ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id); in ubi_attach_fastmap()
724 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs)); in ubi_attach_fastmap()
728 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) { in ubi_attach_fastmap()
729 ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x", in ubi_attach_fastmap()
730 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC); in ubi_attach_fastmap()
734 for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) { in ubi_attach_fastmap()
735 int pnum = be32_to_cpu(fm_eba->pnum[j]); in ubi_attach_fastmap()
742 if (tmp_aeb->pnum == pnum) { in ubi_attach_fastmap()
749 ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum); in ubi_attach_fastmap()
753 aeb->lnum = j; in ubi_attach_fastmap()
755 if (av->highest_lnum <= aeb->lnum) in ubi_attach_fastmap()
756 av->highest_lnum = aeb->lnum; in ubi_attach_fastmap()
761 aeb->pnum, aeb->lnum, av->vol_id); in ubi_attach_fastmap()
765 ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free); in ubi_attach_fastmap()
769 ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free); in ubi_attach_fastmap()
773 if (max_sqnum > ai->max_sqnum) in ubi_attach_fastmap()
774 ai->max_sqnum = max_sqnum; in ubi_attach_fastmap()
777 list_move_tail(&tmp_aeb->u.list, &ai->free); in ubi_attach_fastmap()
780 list_move_tail(&tmp_aeb->u.list, &ai->erase); in ubi_attach_fastmap()
790 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count - in ubi_attach_fastmap()
791 ai->bad_peb_count - fm->used_blocks)) in ubi_attach_fastmap()
800 list_del(&tmp_aeb->u.list); in ubi_attach_fastmap()
804 list_del(&tmp_aeb->u.list); in ubi_attach_fastmap()
812 * find_fm_anchor - find the most recent Fastmap superblock (anchor)
813 * @ai: UBI attach info to be filled
817 int ret = -1; in find_fm_anchor()
821 list_for_each_entry(aeb, &ai->fastmap, u.list) { in find_fm_anchor()
822 if (aeb->vol_id == UBI_FM_SB_VOLUME_ID && aeb->sqnum > max_sqnum) { in find_fm_anchor()
823 max_sqnum = aeb->sqnum; in find_fm_anchor()
824 ret = aeb->pnum; in find_fm_anchor()
836 new = ubi_alloc_aeb(ai, old->pnum, old->ec); in clone_aeb()
840 new->vol_id = old->vol_id; in clone_aeb()
841 new->sqnum = old->sqnum; in clone_aeb()
842 new->lnum = old->lnum; in clone_aeb()
843 new->scrub = old->scrub; in clone_aeb()
844 new->copy_flag = old->copy_flag; in clone_aeb()
850 * ubi_scan_fastmap - scan the fastmap.
851 * @ubi: UBI device object
852 * @ai: UBI attach info to be filled
853 * @scan_ai: UBI attach info from the first 64 PEBs,
860 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai, in ubi_scan_fastmap() argument
879 list_for_each_entry(aeb, &scan_ai->fastmap, u.list) { in ubi_scan_fastmap()
884 return -ENOMEM; in ubi_scan_fastmap()
886 list_add(&new->u.list, &ai->fastmap); in ubi_scan_fastmap()
889 down_write(&ubi->fm_protect); in ubi_scan_fastmap()
890 memset(ubi->fm_buf, 0, ubi->fm_size); in ubi_scan_fastmap()
894 ret = -ENOMEM; in ubi_scan_fastmap()
900 ret = -ENOMEM; in ubi_scan_fastmap()
905 ret = ubi_io_read_data(ubi, fmsb, fm_anchor, 0, sizeof(*fmsb)); in ubi_scan_fastmap()
909 fm->to_be_tortured[0] = 1; in ubi_scan_fastmap()
911 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) { in ubi_scan_fastmap()
912 ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
913 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC); in ubi_scan_fastmap()
918 if (fmsb->version != UBI_FM_FMT_VERSION) { in ubi_scan_fastmap()
919 ubi_err(ubi, "bad fastmap version: %i, expected: %i", in ubi_scan_fastmap()
920 fmsb->version, UBI_FM_FMT_VERSION); in ubi_scan_fastmap()
925 used_blocks = be32_to_cpu(fmsb->used_blocks); in ubi_scan_fastmap()
927 ubi_err(ubi, "number of fastmap blocks is invalid: %i", in ubi_scan_fastmap()
933 fm_size = ubi->leb_size * used_blocks; in ubi_scan_fastmap()
934 if (fm_size != ubi->fm_size) { in ubi_scan_fastmap()
935 ubi_err(ubi, "bad fastmap size: %zi, expected: %zi", in ubi_scan_fastmap()
936 fm_size, ubi->fm_size); in ubi_scan_fastmap()
941 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in ubi_scan_fastmap()
943 ret = -ENOMEM; in ubi_scan_fastmap()
947 vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in ubi_scan_fastmap()
949 ret = -ENOMEM; in ubi_scan_fastmap()
958 pnum = be32_to_cpu(fmsb->block_loc[i]); in ubi_scan_fastmap()
960 if (ubi_io_is_bad(ubi, pnum)) { in ubi_scan_fastmap()
966 ubi_err(ubi, "Fastmap anchor PEB mismatch: PEB: %i vs. %i", in ubi_scan_fastmap()
972 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); in ubi_scan_fastmap()
974 ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)", in ubi_scan_fastmap()
980 fm->to_be_tortured[i] = 1; in ubi_scan_fastmap()
982 image_seq = be32_to_cpu(ech->image_seq); in ubi_scan_fastmap()
983 if (!ubi->image_seq) in ubi_scan_fastmap()
984 ubi->image_seq = image_seq; in ubi_scan_fastmap()
987 * Older UBI implementations have image_seq set to zero, so in ubi_scan_fastmap()
990 if (image_seq && (image_seq != ubi->image_seq)) { in ubi_scan_fastmap()
991 ubi_err(ubi, "wrong image seq:%d instead of %d", in ubi_scan_fastmap()
992 be32_to_cpu(ech->image_seq), ubi->image_seq); in ubi_scan_fastmap()
997 ret = ubi_io_read_vid_hdr(ubi, pnum, vb, 0); in ubi_scan_fastmap()
999 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)", in ubi_scan_fastmap()
1005 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) { in ubi_scan_fastmap()
1006 ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
1007 be32_to_cpu(vh->vol_id), in ubi_scan_fastmap()
1013 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) { in ubi_scan_fastmap()
1014 ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x", in ubi_scan_fastmap()
1015 be32_to_cpu(vh->vol_id), in ubi_scan_fastmap()
1022 if (sqnum < be64_to_cpu(vh->sqnum)) in ubi_scan_fastmap()
1023 sqnum = be64_to_cpu(vh->sqnum); in ubi_scan_fastmap()
1025 ret = ubi_io_read_data(ubi, ubi->fm_buf + (ubi->leb_size * i), in ubi_scan_fastmap()
1026 pnum, 0, ubi->leb_size); in ubi_scan_fastmap()
1028 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, " in ubi_scan_fastmap()
1037 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf); in ubi_scan_fastmap()
1038 tmp_crc = be32_to_cpu(fmsb2->data_crc); in ubi_scan_fastmap()
1039 fmsb2->data_crc = 0; in ubi_scan_fastmap()
1040 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size); in ubi_scan_fastmap()
1042 ubi_err(ubi, "fastmap data CRC is invalid"); in ubi_scan_fastmap()
1043 ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x", in ubi_scan_fastmap()
1049 fmsb2->sqnum = sqnum; in ubi_scan_fastmap()
1051 fm->used_blocks = used_blocks; in ubi_scan_fastmap()
1053 ret = ubi_attach_fastmap(ubi, ai, fm); in ubi_scan_fastmap()
1065 while (i--) in ubi_scan_fastmap()
1066 kmem_cache_free(ubi_wl_entry_slab, fm->e[i]); in ubi_scan_fastmap()
1068 ret = -ENOMEM; in ubi_scan_fastmap()
1072 e->pnum = be32_to_cpu(fmsb2->block_loc[i]); in ubi_scan_fastmap()
1073 e->ec = be32_to_cpu(fmsb2->block_ec[i]); in ubi_scan_fastmap()
1074 fm->e[i] = e; in ubi_scan_fastmap()
1077 ubi->fm = fm; in ubi_scan_fastmap()
1078 ubi->fm_pool.max_size = ubi->fm->max_pool_size; in ubi_scan_fastmap()
1079 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size; in ubi_scan_fastmap()
1080 ubi_msg(ubi, "attached by fastmap"); in ubi_scan_fastmap()
1081 ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size); in ubi_scan_fastmap()
1082 ubi_msg(ubi, "fastmap WL pool size: %d", in ubi_scan_fastmap()
1083 ubi->fm_wl_pool.max_size); in ubi_scan_fastmap()
1084 ubi->fm_disabled = 0; in ubi_scan_fastmap()
1085 ubi->fast_attach = 1; in ubi_scan_fastmap()
1090 up_write(&ubi->fm_protect); in ubi_scan_fastmap()
1092 ubi_err(ubi, "Attach by fastmap failed, doing a full scan!"); in ubi_scan_fastmap()
1106 struct ubi_device *ubi = vol->ubi; in ubi_fastmap_init_checkmap() local
1108 if (!ubi->fast_attach) in ubi_fastmap_init_checkmap()
1111 vol->checkmap = bitmap_zalloc(leb_count, GFP_KERNEL); in ubi_fastmap_init_checkmap()
1112 if (!vol->checkmap) in ubi_fastmap_init_checkmap()
1113 return -ENOMEM; in ubi_fastmap_init_checkmap()
1120 bitmap_free(vol->checkmap); in ubi_fastmap_destroy_checkmap()
1124 * ubi_write_fastmap - writes a fastmap.
1125 * @ubi: UBI device object
1130 static int ubi_write_fastmap(struct ubi_device *ubi, in ubi_write_fastmap() argument
1151 fm_raw = ubi->fm_buf; in ubi_write_fastmap()
1152 memset(ubi->fm_buf, 0, ubi->fm_size); in ubi_write_fastmap()
1154 avbuf = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID); in ubi_write_fastmap()
1156 ret = -ENOMEM; in ubi_write_fastmap()
1160 dvbuf = new_fm_vbuf(ubi, UBI_FM_DATA_VOLUME_ID); in ubi_write_fastmap()
1162 ret = -ENOMEM; in ubi_write_fastmap()
1169 seen_pebs = init_seen(ubi); in ubi_write_fastmap()
1175 spin_lock(&ubi->volumes_lock); in ubi_write_fastmap()
1176 spin_lock(&ubi->wl_lock); in ubi_write_fastmap()
1180 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1184 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1186 fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC); in ubi_write_fastmap()
1187 fmsb->version = UBI_FM_FMT_VERSION; in ubi_write_fastmap()
1188 fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks); in ubi_write_fastmap()
1190 fmsb->sqnum = 0; in ubi_write_fastmap()
1192 fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC); in ubi_write_fastmap()
1201 fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC); in ubi_write_fastmap()
1202 fmpl->size = cpu_to_be16(ubi->fm_pool.size); in ubi_write_fastmap()
1203 fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size); in ubi_write_fastmap()
1205 for (i = 0; i < ubi->fm_pool.size; i++) { in ubi_write_fastmap()
1206 fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]); in ubi_write_fastmap()
1207 set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs); in ubi_write_fastmap()
1212 fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC); in ubi_write_fastmap()
1213 fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size); in ubi_write_fastmap()
1214 fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size); in ubi_write_fastmap()
1216 for (i = 0; i < ubi->fm_wl_pool.size; i++) { in ubi_write_fastmap()
1217 fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]); in ubi_write_fastmap()
1218 set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs); in ubi_write_fastmap()
1221 ubi_for_each_free_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1224 fec->pnum = cpu_to_be32(wl_e->pnum); in ubi_write_fastmap()
1225 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1226 fec->ec = cpu_to_be32(wl_e->ec); in ubi_write_fastmap()
1230 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1232 fmh->free_peb_count = cpu_to_be32(free_peb_count); in ubi_write_fastmap()
1234 ubi_for_each_used_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1237 fec->pnum = cpu_to_be32(wl_e->pnum); in ubi_write_fastmap()
1238 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1239 fec->ec = cpu_to_be32(wl_e->ec); in ubi_write_fastmap()
1243 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1246 ubi_for_each_protected_peb(ubi, i, wl_e) { in ubi_write_fastmap()
1249 fec->pnum = cpu_to_be32(wl_e->pnum); in ubi_write_fastmap()
1250 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1251 fec->ec = cpu_to_be32(wl_e->ec); in ubi_write_fastmap()
1255 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1257 fmh->used_peb_count = cpu_to_be32(used_peb_count); in ubi_write_fastmap()
1259 ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) { in ubi_write_fastmap()
1262 fec->pnum = cpu_to_be32(wl_e->pnum); in ubi_write_fastmap()
1263 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1264 fec->ec = cpu_to_be32(wl_e->ec); in ubi_write_fastmap()
1268 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1270 fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count); in ubi_write_fastmap()
1273 list_for_each_entry(ubi_wrk, &ubi->works, list) { in ubi_write_fastmap()
1275 wl_e = ubi_wrk->e; in ubi_write_fastmap()
1280 fec->pnum = cpu_to_be32(wl_e->pnum); in ubi_write_fastmap()
1281 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1282 fec->ec = cpu_to_be32(wl_e->ec); in ubi_write_fastmap()
1286 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1289 fmh->erase_peb_count = cpu_to_be32(erase_peb_count); in ubi_write_fastmap()
1292 vol = ubi->volumes[i]; in ubi_write_fastmap()
1301 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1303 fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC); in ubi_write_fastmap()
1304 fvh->vol_id = cpu_to_be32(vol->vol_id); in ubi_write_fastmap()
1305 fvh->vol_type = vol->vol_type; in ubi_write_fastmap()
1306 fvh->used_ebs = cpu_to_be32(vol->used_ebs); in ubi_write_fastmap()
1307 fvh->data_pad = cpu_to_be32(vol->data_pad); in ubi_write_fastmap()
1308 fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes); in ubi_write_fastmap()
1310 ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME || in ubi_write_fastmap()
1311 vol->vol_type == UBI_STATIC_VOLUME); in ubi_write_fastmap()
1314 fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs); in ubi_write_fastmap()
1315 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1317 for (j = 0; j < vol->reserved_pebs; j++) { in ubi_write_fastmap()
1321 feba->pnum[j] = cpu_to_be32(ldesc.pnum); in ubi_write_fastmap()
1324 feba->reserved_pebs = cpu_to_be32(j); in ubi_write_fastmap()
1325 feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC); in ubi_write_fastmap()
1327 fmh->vol_count = cpu_to_be32(vol_count); in ubi_write_fastmap()
1328 fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count); in ubi_write_fastmap()
1330 avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_write_fastmap()
1331 avhdr->lnum = 0; in ubi_write_fastmap()
1333 spin_unlock(&ubi->wl_lock); in ubi_write_fastmap()
1334 spin_unlock(&ubi->volumes_lock); in ubi_write_fastmap()
1336 dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum); in ubi_write_fastmap()
1337 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avbuf); in ubi_write_fastmap()
1339 ubi_err(ubi, "unable to write vid_hdr to fastmap SB!"); in ubi_write_fastmap()
1343 for (i = 0; i < new_fm->used_blocks; i++) { in ubi_write_fastmap()
1344 fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum); in ubi_write_fastmap()
1345 set_seen(ubi, new_fm->e[i]->pnum, seen_pebs); in ubi_write_fastmap()
1346 fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec); in ubi_write_fastmap()
1349 fmsb->data_crc = 0; in ubi_write_fastmap()
1350 fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw, in ubi_write_fastmap()
1351 ubi->fm_size)); in ubi_write_fastmap()
1353 for (i = 1; i < new_fm->used_blocks; i++) { in ubi_write_fastmap()
1354 dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_write_fastmap()
1355 dvhdr->lnum = cpu_to_be32(i); in ubi_write_fastmap()
1357 new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum)); in ubi_write_fastmap()
1358 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvbuf); in ubi_write_fastmap()
1360 ubi_err(ubi, "unable to write vid_hdr to PEB %i!", in ubi_write_fastmap()
1361 new_fm->e[i]->pnum); in ubi_write_fastmap()
1366 for (i = 0; i < new_fm->used_blocks; i++) { in ubi_write_fastmap()
1367 ret = ubi_io_write_data(ubi, fm_raw + (i * ubi->leb_size), in ubi_write_fastmap()
1368 new_fm->e[i]->pnum, 0, ubi->leb_size); in ubi_write_fastmap()
1370 ubi_err(ubi, "unable to write fastmap to PEB %i!", in ubi_write_fastmap()
1371 new_fm->e[i]->pnum); in ubi_write_fastmap()
1377 ubi->fm = new_fm; in ubi_write_fastmap()
1379 ret = self_check_seen(ubi, seen_pebs); in ubi_write_fastmap()
1394 * invalidate_fastmap - destroys a fastmap.
1395 * @ubi: UBI device object
1397 * This function ensures that upon next UBI attach a full scan
1398 * is issued. We need this if UBI is about to write a new fastmap
1405 static int invalidate_fastmap(struct ubi_device *ubi) in invalidate_fastmap() argument
1413 if (!ubi->fm) in invalidate_fastmap()
1416 ubi->fm = NULL; in invalidate_fastmap()
1418 ret = -ENOMEM; in invalidate_fastmap()
1423 vb = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID); in invalidate_fastmap()
1429 ret = -ENOSPC; in invalidate_fastmap()
1430 e = ubi_wl_get_fm_peb(ubi, 1); in invalidate_fastmap()
1435 * Create fake fastmap such that UBI will fall back in invalidate_fastmap()
1438 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in invalidate_fastmap()
1439 ret = ubi_io_write_vid_hdr(ubi, e->pnum, vb); in invalidate_fastmap()
1441 ubi_wl_put_fm_peb(ubi, e, 0, 0); in invalidate_fastmap()
1445 fm->used_blocks = 1; in invalidate_fastmap()
1446 fm->e[0] = e; in invalidate_fastmap()
1448 ubi->fm = fm; in invalidate_fastmap()
1460 * return_fm_pebs - returns all PEBs used by a fastmap back to the
1461 * WL sub-system.
1462 * @ubi: UBI device object
1465 static void return_fm_pebs(struct ubi_device *ubi, in return_fm_pebs() argument
1473 for (i = 0; i < fm->used_blocks; i++) { in return_fm_pebs()
1474 if (fm->e[i]) { in return_fm_pebs()
1475 ubi_wl_put_fm_peb(ubi, fm->e[i], i, in return_fm_pebs()
1476 fm->to_be_tortured[i]); in return_fm_pebs()
1477 fm->e[i] = NULL; in return_fm_pebs()
1483 * ubi_update_fastmap - will be called by UBI if a volume changes or
1485 * @ubi: UBI device object
1489 int ubi_update_fastmap(struct ubi_device *ubi) in ubi_update_fastmap() argument
1495 ubi_refill_pools_and_lock(ubi); in ubi_update_fastmap()
1497 if (ubi->ro_mode || ubi->fm_disabled) { in ubi_update_fastmap()
1498 up_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1499 up_write(&ubi->work_sem); in ubi_update_fastmap()
1500 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1506 up_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1507 up_write(&ubi->work_sem); in ubi_update_fastmap()
1508 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1509 return -ENOMEM; in ubi_update_fastmap()
1512 new_fm->used_blocks = ubi->fm_size / ubi->leb_size; in ubi_update_fastmap()
1513 old_fm = ubi->fm; in ubi_update_fastmap()
1514 ubi->fm = NULL; in ubi_update_fastmap()
1516 if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) { in ubi_update_fastmap()
1517 ubi_err(ubi, "fastmap too large"); in ubi_update_fastmap()
1518 ret = -ENOSPC; in ubi_update_fastmap()
1522 for (i = 1; i < new_fm->used_blocks; i++) { in ubi_update_fastmap()
1523 spin_lock(&ubi->wl_lock); in ubi_update_fastmap()
1524 tmp_e = ubi_wl_get_fm_peb(ubi, 0); in ubi_update_fastmap()
1525 spin_unlock(&ubi->wl_lock); in ubi_update_fastmap()
1528 if (old_fm && old_fm->e[i]) { in ubi_update_fastmap()
1529 ret = ubi_sync_erase(ubi, old_fm->e[i], 0); in ubi_update_fastmap()
1531 ubi_err(ubi, "could not erase old fastmap PEB"); in ubi_update_fastmap()
1534 ubi_wl_put_fm_peb(ubi, new_fm->e[j], in ubi_update_fastmap()
1536 new_fm->e[j] = NULL; in ubi_update_fastmap()
1540 new_fm->e[i] = old_fm->e[i]; in ubi_update_fastmap()
1541 old_fm->e[i] = NULL; in ubi_update_fastmap()
1543 ubi_err(ubi, "could not get any free erase block"); in ubi_update_fastmap()
1546 ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0); in ubi_update_fastmap()
1547 new_fm->e[j] = NULL; in ubi_update_fastmap()
1550 ret = -ENOSPC; in ubi_update_fastmap()
1554 new_fm->e[i] = tmp_e; in ubi_update_fastmap()
1556 if (old_fm && old_fm->e[i]) { in ubi_update_fastmap()
1557 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, in ubi_update_fastmap()
1558 old_fm->to_be_tortured[i]); in ubi_update_fastmap()
1559 old_fm->e[i] = NULL; in ubi_update_fastmap()
1565 if (old_fm && new_fm->used_blocks < old_fm->used_blocks) { in ubi_update_fastmap()
1566 for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) { in ubi_update_fastmap()
1567 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, in ubi_update_fastmap()
1568 old_fm->to_be_tortured[i]); in ubi_update_fastmap()
1569 old_fm->e[i] = NULL; in ubi_update_fastmap()
1573 spin_lock(&ubi->wl_lock); in ubi_update_fastmap()
1574 tmp_e = ubi->fm_anchor; in ubi_update_fastmap()
1575 ubi->fm_anchor = NULL; in ubi_update_fastmap()
1576 spin_unlock(&ubi->wl_lock); in ubi_update_fastmap()
1581 ret = ubi_sync_erase(ubi, old_fm->e[0], 0); in ubi_update_fastmap()
1583 ubi_err(ubi, "could not erase old anchor PEB"); in ubi_update_fastmap()
1585 for (i = 1; i < new_fm->used_blocks; i++) { in ubi_update_fastmap()
1586 ubi_wl_put_fm_peb(ubi, new_fm->e[i], in ubi_update_fastmap()
1588 new_fm->e[i] = NULL; in ubi_update_fastmap()
1592 new_fm->e[0] = old_fm->e[0]; in ubi_update_fastmap()
1593 old_fm->e[0] = NULL; in ubi_update_fastmap()
1596 ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0, in ubi_update_fastmap()
1597 old_fm->to_be_tortured[0]); in ubi_update_fastmap()
1598 new_fm->e[0] = tmp_e; in ubi_update_fastmap()
1599 old_fm->e[0] = NULL; in ubi_update_fastmap()
1603 ubi_err(ubi, "could not find any anchor PEB"); in ubi_update_fastmap()
1605 for (i = 1; i < new_fm->used_blocks; i++) { in ubi_update_fastmap()
1606 ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0); in ubi_update_fastmap()
1607 new_fm->e[i] = NULL; in ubi_update_fastmap()
1610 ret = -ENOSPC; in ubi_update_fastmap()
1613 new_fm->e[0] = tmp_e; in ubi_update_fastmap()
1616 ret = ubi_write_fastmap(ubi, new_fm); in ubi_update_fastmap()
1622 up_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1623 up_write(&ubi->work_sem); in ubi_update_fastmap()
1624 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1627 ubi_ensure_anchor_pebs(ubi); in ubi_update_fastmap()
1632 ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret); in ubi_update_fastmap()
1634 ret = invalidate_fastmap(ubi); in ubi_update_fastmap()
1636 ubi_err(ubi, "Unable to invalidate current fastmap!"); in ubi_update_fastmap()
1637 ubi_ro_mode(ubi); in ubi_update_fastmap()
1639 return_fm_pebs(ubi, old_fm); in ubi_update_fastmap()
1640 return_fm_pebs(ubi, new_fm); in ubi_update_fastmap()