Lines Matching +full:- +full:av

6  * SPDX-License-Identifier:	GPL-2.0+
23 * init_seen - allocate memory for used for debugging.
33 ret = kcalloc(ubi->peb_count, sizeof(int), GFP_KERNEL); in init_seen()
35 return ERR_PTR(-ENOMEM); in init_seen()
41 * free_seen - free the seen logic integer array.
42 * @seen: integer array of @ubi->peb_count size
50 * set_seen - mark a PEB as seen.
53 * @seen: integer array of @ubi->peb_count size
64 * self_check_seen - check whether all PEB have been seen by fastmap.
66 * @seen: integer array of @ubi->peb_count size
75 for (pnum = 0; pnum < ubi->peb_count; pnum++) { in self_check_seen()
76 if (!seen[pnum] && ubi->lookuptbl[pnum]) { in self_check_seen()
77 ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum); in self_check_seen()
78 ret = -EINVAL; in self_check_seen()
86 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
97 (ubi->peb_count * sizeof(struct ubi_fm_ec)) + in ubi_calc_fm_size()
99 (ubi->peb_count * sizeof(__be32))) + in ubi_calc_fm_size()
101 return roundup(size, ubi->leb_size); in ubi_calc_fm_size()
106 * new_fm_vhdr - allocate a new volume header for fastmap usage.
121 new->vol_type = UBI_VID_DYNAMIC; in new_fm_vhdr()
122 new->vol_id = cpu_to_be32(vol_id); in new_fm_vhdr()
127 new->compat = UBI_COMPAT_DELETE; in new_fm_vhdr()
134 * add_aeb - create and add a attach erase block to a given list.
148 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL); in add_aeb()
150 return -ENOMEM; in add_aeb()
152 aeb->pnum = pnum; in add_aeb()
153 aeb->ec = ec; in add_aeb()
154 aeb->lnum = -1; in add_aeb()
155 aeb->scrub = scrub; in add_aeb()
156 aeb->copy_flag = aeb->sqnum = 0; in add_aeb()
158 ai->ec_sum += aeb->ec; in add_aeb()
159 ai->ec_count++; in add_aeb()
161 if (ai->max_ec < aeb->ec) in add_aeb()
162 ai->max_ec = aeb->ec; in add_aeb()
164 if (ai->min_ec > aeb->ec) in add_aeb()
165 ai->min_ec = aeb->ec; in add_aeb()
167 list_add_tail(&aeb->u.list, list); in add_aeb()
173 * add_vol - create and add a new volume to ubi_attach_info.
188 struct ubi_ainf_volume *av; in add_vol() local
189 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; in add_vol()
193 av = rb_entry(parent, struct ubi_ainf_volume, rb); in add_vol()
195 if (vol_id > av->vol_id) in add_vol()
196 p = &(*p)->rb_left; in add_vol()
197 else if (vol_id < av->vol_id) in add_vol()
198 p = &(*p)->rb_right; in add_vol()
200 return ERR_PTR(-EINVAL); in add_vol()
203 av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL); in add_vol()
204 if (!av) in add_vol()
207 av->highest_lnum = av->leb_count = av->used_ebs = 0; in add_vol()
208 av->vol_id = vol_id; in add_vol()
209 av->data_pad = data_pad; in add_vol()
210 av->last_data_size = last_eb_bytes; in add_vol()
211 av->compat = 0; in add_vol()
212 av->vol_type = vol_type; in add_vol()
213 av->root = RB_ROOT; in add_vol()
214 if (av->vol_type == UBI_STATIC_VOLUME) in add_vol()
215 av->used_ebs = used_ebs; in add_vol()
219 rb_link_node(&av->rb, parent, p); in add_vol()
220 rb_insert_color(&av->rb, &ai->volumes); in add_vol()
223 return av; in add_vol()
227 * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it
231 * @av: target scan volume
235 struct ubi_ainf_volume *av) in assign_aeb_to_av() argument
238 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; in assign_aeb_to_av()
240 p = &av->root.rb_node; in assign_aeb_to_av()
245 if (aeb->lnum != tmp_aeb->lnum) { in assign_aeb_to_av()
246 if (aeb->lnum < tmp_aeb->lnum) in assign_aeb_to_av()
247 p = &(*p)->rb_left; in assign_aeb_to_av()
249 p = &(*p)->rb_right; in assign_aeb_to_av()
256 list_del(&aeb->u.list); in assign_aeb_to_av()
257 av->leb_count++; in assign_aeb_to_av()
259 rb_link_node(&aeb->u.rb, parent, p); in assign_aeb_to_av()
260 rb_insert_color(&aeb->u.rb, &av->root); in assign_aeb_to_av()
264 * update_vol - inserts or updates a LEB which was found a pool.
267 * @av: the volume this LEB belongs to
274 struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh, in update_vol() argument
277 struct rb_node **p = &av->root.rb_node, *parent = NULL; in update_vol()
285 if (be32_to_cpu(new_vh->lnum) != aeb->lnum) { in update_vol()
286 if (be32_to_cpu(new_vh->lnum) < aeb->lnum) in update_vol()
287 p = &(*p)->rb_left; in update_vol()
289 p = &(*p)->rb_right; in update_vol()
298 if (aeb->pnum == new_aeb->pnum) { in update_vol()
299 ubi_assert(aeb->lnum == new_aeb->lnum); in update_vol()
300 kmem_cache_free(ai->aeb_slab_cache, new_aeb); in update_vol()
305 cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh); in update_vol()
311 victim = kmem_cache_alloc(ai->aeb_slab_cache, in update_vol()
314 return -ENOMEM; in update_vol()
316 victim->ec = aeb->ec; in update_vol()
317 victim->pnum = aeb->pnum; in update_vol()
318 list_add_tail(&victim->u.list, &ai->erase); in update_vol()
320 if (av->highest_lnum == be32_to_cpu(new_vh->lnum)) in update_vol()
321 av->last_data_size = in update_vol()
322 be32_to_cpu(new_vh->data_size); in update_vol()
325 av->vol_id, aeb->lnum, new_aeb->pnum); in update_vol()
327 aeb->ec = new_aeb->ec; in update_vol()
328 aeb->pnum = new_aeb->pnum; in update_vol()
329 aeb->copy_flag = new_vh->copy_flag; in update_vol()
330 aeb->scrub = new_aeb->scrub; in update_vol()
331 kmem_cache_free(ai->aeb_slab_cache, new_aeb); in update_vol()
336 av->vol_id, aeb->lnum, new_aeb->pnum); in update_vol()
337 list_add_tail(&new_aeb->u.list, &ai->erase); in update_vol()
344 if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) { in update_vol()
345 av->highest_lnum = be32_to_cpu(new_vh->lnum); in update_vol()
346 av->last_data_size = be32_to_cpu(new_vh->data_size); in update_vol()
349 if (av->vol_type == UBI_STATIC_VOLUME) in update_vol()
350 av->used_ebs = be32_to_cpu(new_vh->used_ebs); in update_vol()
352 av->leb_count++; in update_vol()
354 rb_link_node(&new_aeb->u.rb, parent, p); in update_vol()
355 rb_insert_color(&new_aeb->u.rb, &av->root); in update_vol()
361 * process_pool_aeb - we found a non-empty PEB in a pool.
373 struct ubi_ainf_volume *av, *tmp_av = NULL; in process_pool_aeb() local
374 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; in process_pool_aeb()
377 if (be32_to_cpu(new_vh->vol_id) == UBI_FM_SB_VOLUME_ID || in process_pool_aeb()
378 be32_to_cpu(new_vh->vol_id) == UBI_FM_DATA_VOLUME_ID) { in process_pool_aeb()
379 kmem_cache_free(ai->aeb_slab_cache, new_aeb); in process_pool_aeb()
389 if (be32_to_cpu(new_vh->vol_id) > tmp_av->vol_id) in process_pool_aeb()
390 p = &(*p)->rb_left; in process_pool_aeb()
391 else if (be32_to_cpu(new_vh->vol_id) < tmp_av->vol_id) in process_pool_aeb()
392 p = &(*p)->rb_right; in process_pool_aeb()
400 av = tmp_av; in process_pool_aeb()
403 kmem_cache_free(ai->aeb_slab_cache, new_aeb); in process_pool_aeb()
407 ubi_assert(be32_to_cpu(new_vh->vol_id) == av->vol_id); in process_pool_aeb()
409 return update_vol(ubi, ai, av, new_vh, new_aeb); in process_pool_aeb()
413 * unmap_peb - unmap a PEB.
422 struct ubi_ainf_volume *av; in unmap_peb() local
426 for (node = rb_first(&ai->volumes); node; node = rb_next(node)) { in unmap_peb()
427 av = rb_entry(node, struct ubi_ainf_volume, rb); in unmap_peb()
429 for (node2 = rb_first(&av->root); node2; in unmap_peb()
432 if (aeb->pnum == pnum) { in unmap_peb()
433 rb_erase(&aeb->u.rb, &av->root); in unmap_peb()
434 av->leb_count--; in unmap_peb()
435 kmem_cache_free(ai->aeb_slab_cache, aeb); in unmap_peb()
443 * scan_pool - scans a pool for changed (no longer empty PEBs).
449 * @free: list of PEBs which are most likely free (and go into @ai->free)
469 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in scan_pool()
471 return -ENOMEM; in scan_pool()
476 return -ENOMEM; in scan_pool()
510 image_seq = be32_to_cpu(ech->image_seq); in scan_pool()
512 if (image_seq && (image_seq != ubi->image_seq)) { in scan_pool()
514 be32_to_cpu(ech->image_seq), ubi->image_seq); in scan_pool()
521 unsigned long long ec = be64_to_cpu(ech->ec); in scan_pool()
535 new_aeb = kmem_cache_alloc(ai->aeb_slab_cache, in scan_pool()
538 ret = -ENOMEM; in scan_pool()
542 new_aeb->ec = be64_to_cpu(ech->ec); in scan_pool()
543 new_aeb->pnum = pnum; in scan_pool()
544 new_aeb->lnum = be32_to_cpu(vh->lnum); in scan_pool()
545 new_aeb->sqnum = be64_to_cpu(vh->sqnum); in scan_pool()
546 new_aeb->copy_flag = vh->copy_flag; in scan_pool()
547 new_aeb->scrub = scrub; in scan_pool()
549 if (*max_sqnum < new_aeb->sqnum) in scan_pool()
550 *max_sqnum = new_aeb->sqnum; in scan_pool()
573 * count_fastmap_pebs - Counts the PEBs found by fastmap.
579 struct ubi_ainf_volume *av; in count_fastmap_pebs() local
583 list_for_each_entry(aeb, &ai->erase, u.list) in count_fastmap_pebs()
586 list_for_each_entry(aeb, &ai->free, u.list) in count_fastmap_pebs()
589 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) in count_fastmap_pebs()
590 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) in count_fastmap_pebs()
597 * ubi_attach_fastmap - creates ubi_attach_info from a fastmap.
610 struct ubi_ainf_volume *av; in ubi_attach_fastmap() local
619 size_t fm_pos = 0, fm_size = ubi->fm_size; in ubi_attach_fastmap()
621 void *fm_raw = ubi->fm_buf; in ubi_attach_fastmap()
625 ai->min_ec = UBI_MAX_ERASECOUNTER; in ubi_attach_fastmap()
628 ai->max_sqnum = fmsb->sqnum; in ubi_attach_fastmap()
638 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) { in ubi_attach_fastmap()
640 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC); in ubi_attach_fastmap()
648 if (be32_to_cpu(fmpl->magic) != UBI_FM_POOL_MAGIC) { in ubi_attach_fastmap()
650 be32_to_cpu(fmpl->magic), UBI_FM_POOL_MAGIC); in ubi_attach_fastmap()
658 if (be32_to_cpu(fmpl_wl->magic) != UBI_FM_POOL_MAGIC) { in ubi_attach_fastmap()
660 be32_to_cpu(fmpl_wl->magic), UBI_FM_POOL_MAGIC); in ubi_attach_fastmap()
664 pool_size = be16_to_cpu(fmpl->size); in ubi_attach_fastmap()
665 wl_pool_size = be16_to_cpu(fmpl_wl->size); in ubi_attach_fastmap()
666 fm->max_pool_size = be16_to_cpu(fmpl->max_size); in ubi_attach_fastmap()
667 fm->max_wl_pool_size = be16_to_cpu(fmpl_wl->max_size); in ubi_attach_fastmap()
680 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE || in ubi_attach_fastmap()
681 fm->max_pool_size < 0) { in ubi_attach_fastmap()
682 ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size); in ubi_attach_fastmap()
686 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE || in ubi_attach_fastmap()
687 fm->max_wl_pool_size < 0) { in ubi_attach_fastmap()
689 fm->max_wl_pool_size); in ubi_attach_fastmap()
694 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) { in ubi_attach_fastmap()
700 add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum), in ubi_attach_fastmap()
701 be32_to_cpu(fmec->ec), 0); in ubi_attach_fastmap()
705 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) { in ubi_attach_fastmap()
711 add_aeb(ai, &used, be32_to_cpu(fmec->pnum), in ubi_attach_fastmap()
712 be32_to_cpu(fmec->ec), 0); in ubi_attach_fastmap()
716 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) { in ubi_attach_fastmap()
722 add_aeb(ai, &used, be32_to_cpu(fmec->pnum), in ubi_attach_fastmap()
723 be32_to_cpu(fmec->ec), 1); in ubi_attach_fastmap()
727 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) { in ubi_attach_fastmap()
733 add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum), in ubi_attach_fastmap()
734 be32_to_cpu(fmec->ec), 1); in ubi_attach_fastmap()
737 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count); in ubi_attach_fastmap()
738 ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count); in ubi_attach_fastmap()
741 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) { in ubi_attach_fastmap()
747 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) { in ubi_attach_fastmap()
749 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC); in ubi_attach_fastmap()
753 av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id), in ubi_attach_fastmap()
754 be32_to_cpu(fmvhdr->used_ebs), in ubi_attach_fastmap()
755 be32_to_cpu(fmvhdr->data_pad), in ubi_attach_fastmap()
756 fmvhdr->vol_type, in ubi_attach_fastmap()
757 be32_to_cpu(fmvhdr->last_eb_bytes)); in ubi_attach_fastmap()
759 if (!av) in ubi_attach_fastmap()
761 if (PTR_ERR(av) == -EINVAL) { in ubi_attach_fastmap()
763 fmvhdr->vol_id); in ubi_attach_fastmap()
767 ai->vols_found++; in ubi_attach_fastmap()
768 if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id)) in ubi_attach_fastmap()
769 ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id); in ubi_attach_fastmap()
773 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs)); in ubi_attach_fastmap()
777 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) { in ubi_attach_fastmap()
779 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC); in ubi_attach_fastmap()
783 for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) { in ubi_attach_fastmap()
784 int pnum = be32_to_cpu(fm_eba->pnum[j]); in ubi_attach_fastmap()
786 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0) in ubi_attach_fastmap()
791 if (tmp_aeb->pnum == pnum) { in ubi_attach_fastmap()
802 aeb->lnum = j; in ubi_attach_fastmap()
804 if (av->highest_lnum <= aeb->lnum) in ubi_attach_fastmap()
805 av->highest_lnum = aeb->lnum; in ubi_attach_fastmap()
807 assign_aeb_to_av(ai, aeb, av); in ubi_attach_fastmap()
810 aeb->pnum, aeb->lnum, av->vol_id); in ubi_attach_fastmap()
814 ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free); in ubi_attach_fastmap()
818 ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free); in ubi_attach_fastmap()
822 if (max_sqnum > ai->max_sqnum) in ubi_attach_fastmap()
823 ai->max_sqnum = max_sqnum; in ubi_attach_fastmap()
826 list_move_tail(&tmp_aeb->u.list, &ai->free); in ubi_attach_fastmap()
829 list_move_tail(&tmp_aeb->u.list, &ai->erase); in ubi_attach_fastmap()
840 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count - in ubi_attach_fastmap()
841 ai->bad_peb_count - fm->used_blocks)) in ubi_attach_fastmap()
844 if (count_fastmap_pebs(ai) != ubi->peb_count - in ubi_attach_fastmap()
845 ai->bad_peb_count - fm->used_blocks) { in ubi_attach_fastmap()
857 list_del(&tmp_aeb->u.list); in ubi_attach_fastmap()
858 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb); in ubi_attach_fastmap()
861 list_del(&tmp_aeb->u.list); in ubi_attach_fastmap()
862 kmem_cache_free(ai->aeb_slab_cache, tmp_aeb); in ubi_attach_fastmap()
869 * ubi_scan_fastmap - scan the fastmap.
890 down_write(&ubi->fm_protect); in ubi_scan_fastmap()
891 memset(ubi->fm_buf, 0, ubi->fm_size); in ubi_scan_fastmap()
895 ret = -ENOMEM; in ubi_scan_fastmap()
901 ret = -ENOMEM; in ubi_scan_fastmap()
906 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb)); in ubi_scan_fastmap()
910 fm->to_be_tortured[0] = 1; in ubi_scan_fastmap()
912 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) { in ubi_scan_fastmap()
914 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC); in ubi_scan_fastmap()
919 if (fmsb->version != UBI_FM_FMT_VERSION) { in ubi_scan_fastmap()
921 fmsb->version, UBI_FM_FMT_VERSION); in ubi_scan_fastmap()
926 used_blocks = be32_to_cpu(fmsb->used_blocks); in ubi_scan_fastmap()
934 fm_size = ubi->leb_size * used_blocks; in ubi_scan_fastmap()
935 if (fm_size != ubi->fm_size) { in ubi_scan_fastmap()
937 fm_size, ubi->fm_size); in ubi_scan_fastmap()
942 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in ubi_scan_fastmap()
944 ret = -ENOMEM; in ubi_scan_fastmap()
950 ret = -ENOMEM; in ubi_scan_fastmap()
957 pnum = be32_to_cpu(fmsb->block_loc[i]); in ubi_scan_fastmap()
972 fm->to_be_tortured[i] = 1; in ubi_scan_fastmap()
974 image_seq = be32_to_cpu(ech->image_seq); in ubi_scan_fastmap()
975 if (!ubi->image_seq) in ubi_scan_fastmap()
976 ubi->image_seq = image_seq; in ubi_scan_fastmap()
982 if (image_seq && (image_seq != ubi->image_seq)) { in ubi_scan_fastmap()
984 be32_to_cpu(ech->image_seq), ubi->image_seq); in ubi_scan_fastmap()
997 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) { in ubi_scan_fastmap()
999 be32_to_cpu(vh->vol_id), in ubi_scan_fastmap()
1005 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) { in ubi_scan_fastmap()
1007 be32_to_cpu(vh->vol_id), in ubi_scan_fastmap()
1014 if (sqnum < be64_to_cpu(vh->sqnum)) in ubi_scan_fastmap()
1015 sqnum = be64_to_cpu(vh->sqnum); in ubi_scan_fastmap()
1017 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum, in ubi_scan_fastmap()
1018 ubi->leb_start, ubi->leb_size); in ubi_scan_fastmap()
1029 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf); in ubi_scan_fastmap()
1030 tmp_crc = be32_to_cpu(fmsb2->data_crc); in ubi_scan_fastmap()
1031 fmsb2->data_crc = 0; in ubi_scan_fastmap()
1032 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size); in ubi_scan_fastmap()
1041 fmsb2->sqnum = sqnum; in ubi_scan_fastmap()
1043 fm->used_blocks = used_blocks; in ubi_scan_fastmap()
1057 while (i--) in ubi_scan_fastmap()
1058 kfree(fm->e[i]); in ubi_scan_fastmap()
1060 ret = -ENOMEM; in ubi_scan_fastmap()
1064 e->pnum = be32_to_cpu(fmsb2->block_loc[i]); in ubi_scan_fastmap()
1065 e->ec = be32_to_cpu(fmsb2->block_ec[i]); in ubi_scan_fastmap()
1066 fm->e[i] = e; in ubi_scan_fastmap()
1069 ubi->fm = fm; in ubi_scan_fastmap()
1070 ubi->fm_pool.max_size = ubi->fm->max_pool_size; in ubi_scan_fastmap()
1071 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size; in ubi_scan_fastmap()
1073 ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size); in ubi_scan_fastmap()
1075 ubi->fm_wl_pool.max_size); in ubi_scan_fastmap()
1076 ubi->fm_disabled = 0; in ubi_scan_fastmap()
1081 up_write(&ubi->fm_protect); in ubi_scan_fastmap()
1096 * ubi_write_fastmap - writes a fastmap.
1122 fm_raw = ubi->fm_buf; in ubi_write_fastmap()
1123 memset(ubi->fm_buf, 0, ubi->fm_size); in ubi_write_fastmap()
1127 ret = -ENOMEM; in ubi_write_fastmap()
1133 ret = -ENOMEM; in ubi_write_fastmap()
1143 spin_lock(&ubi->volumes_lock); in ubi_write_fastmap()
1144 spin_lock(&ubi->wl_lock); in ubi_write_fastmap()
1148 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1152 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1154 fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC); in ubi_write_fastmap()
1155 fmsb->version = UBI_FM_FMT_VERSION; in ubi_write_fastmap()
1156 fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks); in ubi_write_fastmap()
1158 fmsb->sqnum = 0; in ubi_write_fastmap()
1160 fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC); in ubi_write_fastmap()
1169 fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC); in ubi_write_fastmap()
1170 fmpl->size = cpu_to_be16(ubi->fm_pool.size); in ubi_write_fastmap()
1171 fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size); in ubi_write_fastmap()
1173 for (i = 0; i < ubi->fm_pool.size; i++) { in ubi_write_fastmap()
1174 fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]); in ubi_write_fastmap()
1175 set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs); in ubi_write_fastmap()
1180 fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC); in ubi_write_fastmap()
1181 fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size); in ubi_write_fastmap()
1182 fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size); in ubi_write_fastmap()
1184 for (i = 0; i < ubi->fm_wl_pool.size; i++) { in ubi_write_fastmap()
1185 fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]); in ubi_write_fastmap()
1186 set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs); in ubi_write_fastmap()
1192 fec->pnum = cpu_to_be32(wl_e->pnum); in ubi_write_fastmap()
1193 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1194 fec->ec = cpu_to_be32(wl_e->ec); in ubi_write_fastmap()
1198 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1200 fmh->free_peb_count = cpu_to_be32(free_peb_count); in ubi_write_fastmap()
1205 fec->pnum = cpu_to_be32(wl_e->pnum); in ubi_write_fastmap()
1206 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1207 fec->ec = cpu_to_be32(wl_e->ec); in ubi_write_fastmap()
1211 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1217 fec->pnum = cpu_to_be32(wl_e->pnum); in ubi_write_fastmap()
1218 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1219 fec->ec = cpu_to_be32(wl_e->ec); in ubi_write_fastmap()
1223 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1225 fmh->used_peb_count = cpu_to_be32(used_peb_count); in ubi_write_fastmap()
1230 fec->pnum = cpu_to_be32(wl_e->pnum); in ubi_write_fastmap()
1231 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1232 fec->ec = cpu_to_be32(wl_e->ec); in ubi_write_fastmap()
1236 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1238 fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count); in ubi_write_fastmap()
1241 list_for_each_entry(ubi_wrk, &ubi->works, list) { in ubi_write_fastmap()
1243 wl_e = ubi_wrk->e; in ubi_write_fastmap()
1248 fec->pnum = cpu_to_be32(wl_e->pnum); in ubi_write_fastmap()
1249 set_seen(ubi, wl_e->pnum, seen_pebs); in ubi_write_fastmap()
1250 fec->ec = cpu_to_be32(wl_e->ec); in ubi_write_fastmap()
1254 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1257 fmh->erase_peb_count = cpu_to_be32(erase_peb_count); in ubi_write_fastmap()
1260 vol = ubi->volumes[i]; in ubi_write_fastmap()
1269 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1271 fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC); in ubi_write_fastmap()
1272 fvh->vol_id = cpu_to_be32(vol->vol_id); in ubi_write_fastmap()
1273 fvh->vol_type = vol->vol_type; in ubi_write_fastmap()
1274 fvh->used_ebs = cpu_to_be32(vol->used_ebs); in ubi_write_fastmap()
1275 fvh->data_pad = cpu_to_be32(vol->data_pad); in ubi_write_fastmap()
1276 fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes); in ubi_write_fastmap()
1278 ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME || in ubi_write_fastmap()
1279 vol->vol_type == UBI_STATIC_VOLUME); in ubi_write_fastmap()
1282 fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs); in ubi_write_fastmap()
1283 ubi_assert(fm_pos <= ubi->fm_size); in ubi_write_fastmap()
1285 for (j = 0; j < vol->reserved_pebs; j++) in ubi_write_fastmap()
1286 feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]); in ubi_write_fastmap()
1288 feba->reserved_pebs = cpu_to_be32(j); in ubi_write_fastmap()
1289 feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC); in ubi_write_fastmap()
1291 fmh->vol_count = cpu_to_be32(vol_count); in ubi_write_fastmap()
1292 fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count); in ubi_write_fastmap()
1294 avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_write_fastmap()
1295 avhdr->lnum = 0; in ubi_write_fastmap()
1297 spin_unlock(&ubi->wl_lock); in ubi_write_fastmap()
1298 spin_unlock(&ubi->volumes_lock); in ubi_write_fastmap()
1300 dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum); in ubi_write_fastmap()
1301 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr); in ubi_write_fastmap()
1307 for (i = 0; i < new_fm->used_blocks; i++) { in ubi_write_fastmap()
1308 fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum); in ubi_write_fastmap()
1309 set_seen(ubi, new_fm->e[i]->pnum, seen_pebs); in ubi_write_fastmap()
1310 fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec); in ubi_write_fastmap()
1313 fmsb->data_crc = 0; in ubi_write_fastmap()
1314 fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw, in ubi_write_fastmap()
1315 ubi->fm_size)); in ubi_write_fastmap()
1317 for (i = 1; i < new_fm->used_blocks; i++) { in ubi_write_fastmap()
1318 dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in ubi_write_fastmap()
1319 dvhdr->lnum = cpu_to_be32(i); in ubi_write_fastmap()
1321 new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum)); in ubi_write_fastmap()
1322 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr); in ubi_write_fastmap()
1325 new_fm->e[i]->pnum); in ubi_write_fastmap()
1330 for (i = 0; i < new_fm->used_blocks; i++) { in ubi_write_fastmap()
1331 ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size), in ubi_write_fastmap()
1332 new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size); in ubi_write_fastmap()
1335 new_fm->e[i]->pnum); in ubi_write_fastmap()
1341 ubi->fm = new_fm; in ubi_write_fastmap()
1355 * erase_block - Manually erase a PEB.
1367 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in erase_block()
1369 return -ENOMEM; in erase_block()
1375 ret = -EINVAL; in erase_block()
1383 ec = be64_to_cpu(ec_hdr->ec); in erase_block()
1386 ret = -EINVAL; in erase_block()
1390 ec_hdr->ec = cpu_to_be64(ec); in erase_block()
1402 * invalidate_fastmap - destroys a fastmap.
1420 if (!ubi->fm) in invalidate_fastmap()
1423 ubi->fm = NULL; in invalidate_fastmap()
1425 ret = -ENOMEM; in invalidate_fastmap()
1434 ret = -ENOSPC; in invalidate_fastmap()
1443 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); in invalidate_fastmap()
1444 ret = ubi_io_write_vid_hdr(ubi, e->pnum, vh); in invalidate_fastmap()
1450 fm->used_blocks = 1; in invalidate_fastmap()
1451 fm->e[0] = e; in invalidate_fastmap()
1453 ubi->fm = fm; in invalidate_fastmap()
1465 * return_fm_pebs - returns all PEBs used by a fastmap back to the
1466 * WL sub-system.
1478 for (i = 0; i < fm->used_blocks; i++) { in return_fm_pebs()
1479 if (fm->e[i]) { in return_fm_pebs()
1480 ubi_wl_put_fm_peb(ubi, fm->e[i], i, in return_fm_pebs()
1481 fm->to_be_tortured[i]); in return_fm_pebs()
1482 fm->e[i] = NULL; in return_fm_pebs()
1488 * ubi_update_fastmap - will be called by UBI if a volume changes or
1500 down_write(&ubi->fm_protect); in ubi_update_fastmap()
1504 if (ubi->ro_mode || ubi->fm_disabled) { in ubi_update_fastmap()
1505 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1511 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1517 up_write(&ubi->fm_protect); in ubi_update_fastmap()
1518 return -ENOMEM; in ubi_update_fastmap()
1521 new_fm->used_blocks = ubi->fm_size / ubi->leb_size; in ubi_update_fastmap()
1522 old_fm = ubi->fm; in ubi_update_fastmap()
1523 ubi->fm = NULL; in ubi_update_fastmap()
1525 if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) { in ubi_update_fastmap()
1527 ret = -ENOSPC; in ubi_update_fastmap()
1531 for (i = 1; i < new_fm->used_blocks; i++) { in ubi_update_fastmap()
1532 spin_lock(&ubi->wl_lock); in ubi_update_fastmap()
1534 spin_unlock(&ubi->wl_lock); in ubi_update_fastmap()
1537 if (old_fm && old_fm->e[i]) { in ubi_update_fastmap()
1538 ret = erase_block(ubi, old_fm->e[i]->pnum); in ubi_update_fastmap()
1543 ubi_wl_put_fm_peb(ubi, new_fm->e[j], in ubi_update_fastmap()
1545 new_fm->e[j] = NULL; in ubi_update_fastmap()
1549 new_fm->e[i] = old_fm->e[i]; in ubi_update_fastmap()
1550 old_fm->e[i] = NULL; in ubi_update_fastmap()
1555 ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0); in ubi_update_fastmap()
1556 new_fm->e[j] = NULL; in ubi_update_fastmap()
1559 ret = -ENOSPC; in ubi_update_fastmap()
1563 new_fm->e[i] = tmp_e; in ubi_update_fastmap()
1565 if (old_fm && old_fm->e[i]) { in ubi_update_fastmap()
1566 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, in ubi_update_fastmap()
1567 old_fm->to_be_tortured[i]); in ubi_update_fastmap()
1568 old_fm->e[i] = NULL; in ubi_update_fastmap()
1574 if (old_fm && new_fm->used_blocks < old_fm->used_blocks) { in ubi_update_fastmap()
1575 for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) { in ubi_update_fastmap()
1576 ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, in ubi_update_fastmap()
1577 old_fm->to_be_tortured[i]); in ubi_update_fastmap()
1578 old_fm->e[i] = NULL; in ubi_update_fastmap()
1582 spin_lock(&ubi->wl_lock); in ubi_update_fastmap()
1584 spin_unlock(&ubi->wl_lock); in ubi_update_fastmap()
1589 ret = erase_block(ubi, old_fm->e[0]->pnum); in ubi_update_fastmap()
1593 for (i = 1; i < new_fm->used_blocks; i++) { in ubi_update_fastmap()
1594 ubi_wl_put_fm_peb(ubi, new_fm->e[i], in ubi_update_fastmap()
1596 new_fm->e[i] = NULL; in ubi_update_fastmap()
1600 new_fm->e[0] = old_fm->e[0]; in ubi_update_fastmap()
1601 new_fm->e[0]->ec = ret; in ubi_update_fastmap()
1602 old_fm->e[0] = NULL; in ubi_update_fastmap()
1605 ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0, in ubi_update_fastmap()
1606 old_fm->to_be_tortured[0]); in ubi_update_fastmap()
1607 new_fm->e[0] = tmp_e; in ubi_update_fastmap()
1608 old_fm->e[0] = NULL; in ubi_update_fastmap()
1614 for (i = 1; i < new_fm->used_blocks; i++) { in ubi_update_fastmap()
1615 ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0); in ubi_update_fastmap()
1616 new_fm->e[i] = NULL; in ubi_update_fastmap()
1619 ret = -ENOSPC; in ubi_update_fastmap()
1622 new_fm->e[0] = tmp_e; in ubi_update_fastmap()
1625 down_write(&ubi->work_sem); in ubi_update_fastmap()
1626 down_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1628 up_write(&ubi->fm_eba_sem); in ubi_update_fastmap()
1629 up_write(&ubi->work_sem); in ubi_update_fastmap()
1635 up_write(&ubi->fm_protect); in ubi_update_fastmap()