18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2012 Linutronix GmbH 48c2ecf20Sopenharmony_ci * Copyright (c) 2014 sigma star gmbh 58c2ecf20Sopenharmony_ci * Author: Richard Weinberger <richard@nod.at> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/crc32.h> 98c2ecf20Sopenharmony_ci#include <linux/bitmap.h> 108c2ecf20Sopenharmony_ci#include "ubi.h" 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci/** 138c2ecf20Sopenharmony_ci * init_seen - allocate memory for used for debugging. 148c2ecf20Sopenharmony_ci * @ubi: UBI device description object 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_cistatic inline unsigned long *init_seen(struct ubi_device *ubi) 178c2ecf20Sopenharmony_ci{ 188c2ecf20Sopenharmony_ci unsigned long *ret; 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci if (!ubi_dbg_chk_fastmap(ubi)) 218c2ecf20Sopenharmony_ci return NULL; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci ret = kcalloc(BITS_TO_LONGS(ubi->peb_count), sizeof(unsigned long), 248c2ecf20Sopenharmony_ci GFP_KERNEL); 258c2ecf20Sopenharmony_ci if (!ret) 268c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci return ret; 298c2ecf20Sopenharmony_ci} 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci/** 328c2ecf20Sopenharmony_ci * free_seen - free the seen logic integer array. 338c2ecf20Sopenharmony_ci * @seen: integer array of @ubi->peb_count size 348c2ecf20Sopenharmony_ci */ 358c2ecf20Sopenharmony_cistatic inline void free_seen(unsigned long *seen) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci kfree(seen); 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci/** 418c2ecf20Sopenharmony_ci * set_seen - mark a PEB as seen. 428c2ecf20Sopenharmony_ci * @ubi: UBI device description object 438c2ecf20Sopenharmony_ci * @pnum: The PEB to be makred as seen 448c2ecf20Sopenharmony_ci * @seen: integer array of @ubi->peb_count size 458c2ecf20Sopenharmony_ci */ 468c2ecf20Sopenharmony_cistatic inline void set_seen(struct ubi_device *ubi, int pnum, unsigned long *seen) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci if (!ubi_dbg_chk_fastmap(ubi) || !seen) 498c2ecf20Sopenharmony_ci return; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci set_bit(pnum, seen); 528c2ecf20Sopenharmony_ci} 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/** 558c2ecf20Sopenharmony_ci * self_check_seen - check whether all PEB have been seen by fastmap. 568c2ecf20Sopenharmony_ci * @ubi: UBI device description object 578c2ecf20Sopenharmony_ci * @seen: integer array of @ubi->peb_count size 588c2ecf20Sopenharmony_ci */ 598c2ecf20Sopenharmony_cistatic int self_check_seen(struct ubi_device *ubi, unsigned long *seen) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci int pnum, ret = 0; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci if (!ubi_dbg_chk_fastmap(ubi) || !seen) 648c2ecf20Sopenharmony_ci return 0; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci for (pnum = 0; pnum < ubi->peb_count; pnum++) { 678c2ecf20Sopenharmony_ci if (!test_bit(pnum, seen) && ubi->lookuptbl[pnum]) { 688c2ecf20Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum); 698c2ecf20Sopenharmony_ci ret = -EINVAL; 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci } 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci return ret; 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci/** 778c2ecf20Sopenharmony_ci * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device. 788c2ecf20Sopenharmony_ci * @ubi: UBI device description object 798c2ecf20Sopenharmony_ci */ 808c2ecf20Sopenharmony_cisize_t ubi_calc_fm_size(struct ubi_device *ubi) 818c2ecf20Sopenharmony_ci{ 828c2ecf20Sopenharmony_ci size_t size; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci size = sizeof(struct ubi_fm_sb) + 858c2ecf20Sopenharmony_ci sizeof(struct ubi_fm_hdr) + 868c2ecf20Sopenharmony_ci sizeof(struct ubi_fm_scan_pool) + 878c2ecf20Sopenharmony_ci sizeof(struct ubi_fm_scan_pool) + 888c2ecf20Sopenharmony_ci (ubi->peb_count * sizeof(struct ubi_fm_ec)) + 898c2ecf20Sopenharmony_ci (sizeof(struct ubi_fm_eba) + 908c2ecf20Sopenharmony_ci (ubi->peb_count * sizeof(__be32))) + 918c2ecf20Sopenharmony_ci sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES; 928c2ecf20Sopenharmony_ci return roundup(size, ubi->leb_size); 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci/** 978c2ecf20Sopenharmony_ci * new_fm_vhdr - allocate a new volume header for fastmap usage. 988c2ecf20Sopenharmony_ci * @ubi: UBI device description object 998c2ecf20Sopenharmony_ci * @vol_id: the VID of the new header 1008c2ecf20Sopenharmony_ci * 1018c2ecf20Sopenharmony_ci * Returns a new struct ubi_vid_hdr on success. 1028c2ecf20Sopenharmony_ci * NULL indicates out of memory. 1038c2ecf20Sopenharmony_ci */ 1048c2ecf20Sopenharmony_cistatic struct ubi_vid_io_buf *new_fm_vbuf(struct ubi_device *ubi, int vol_id) 1058c2ecf20Sopenharmony_ci{ 1068c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *new; 1078c2ecf20Sopenharmony_ci struct ubi_vid_hdr *vh; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci new = ubi_alloc_vid_buf(ubi, GFP_KERNEL); 1108c2ecf20Sopenharmony_ci if (!new) 1118c2ecf20Sopenharmony_ci goto out; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci vh = ubi_get_vid_hdr(new); 1148c2ecf20Sopenharmony_ci vh->vol_type = UBI_VID_DYNAMIC; 1158c2ecf20Sopenharmony_ci vh->vol_id = cpu_to_be32(vol_id); 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci /* UBI implementations without fastmap support have to delete the 1188c2ecf20Sopenharmony_ci * fastmap. 1198c2ecf20Sopenharmony_ci */ 1208c2ecf20Sopenharmony_ci vh->compat = UBI_COMPAT_DELETE; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ciout: 1238c2ecf20Sopenharmony_ci return new; 1248c2ecf20Sopenharmony_ci} 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci/** 1278c2ecf20Sopenharmony_ci * add_aeb - create and add a attach erase block to a given list. 1288c2ecf20Sopenharmony_ci * @ai: UBI attach info object 1298c2ecf20Sopenharmony_ci * @list: the target list 1308c2ecf20Sopenharmony_ci * @pnum: PEB number of the new attach erase block 1318c2ecf20Sopenharmony_ci * @ec: erease counter of the new LEB 1328c2ecf20Sopenharmony_ci * @scrub: scrub this PEB after attaching 1338c2ecf20Sopenharmony_ci * 1348c2ecf20Sopenharmony_ci * Returns 0 on success, < 0 indicates an internal error. 1358c2ecf20Sopenharmony_ci */ 1368c2ecf20Sopenharmony_cistatic int add_aeb(struct ubi_attach_info *ai, struct list_head *list, 1378c2ecf20Sopenharmony_ci int pnum, int ec, int scrub) 1388c2ecf20Sopenharmony_ci{ 1398c2ecf20Sopenharmony_ci struct ubi_ainf_peb *aeb; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci aeb = ubi_alloc_aeb(ai, pnum, ec); 1428c2ecf20Sopenharmony_ci if (!aeb) 1438c2ecf20Sopenharmony_ci return -ENOMEM; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci aeb->lnum = -1; 1468c2ecf20Sopenharmony_ci aeb->scrub = scrub; 1478c2ecf20Sopenharmony_ci aeb->copy_flag = aeb->sqnum = 0; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci ai->ec_sum += aeb->ec; 1508c2ecf20Sopenharmony_ci ai->ec_count++; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci if (ai->max_ec < aeb->ec) 1538c2ecf20Sopenharmony_ci ai->max_ec = aeb->ec; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci if (ai->min_ec > aeb->ec) 1568c2ecf20Sopenharmony_ci ai->min_ec = aeb->ec; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci list_add_tail(&aeb->u.list, list); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci return 0; 1618c2ecf20Sopenharmony_ci} 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci/** 1648c2ecf20Sopenharmony_ci * add_vol - create and add a new volume to ubi_attach_info. 1658c2ecf20Sopenharmony_ci * @ai: ubi_attach_info object 1668c2ecf20Sopenharmony_ci * @vol_id: VID of the new volume 1678c2ecf20Sopenharmony_ci * @used_ebs: number of used EBS 1688c2ecf20Sopenharmony_ci * @data_pad: data padding value of the new volume 1698c2ecf20Sopenharmony_ci * @vol_type: volume type 1708c2ecf20Sopenharmony_ci * @last_eb_bytes: number of bytes in the last LEB 1718c2ecf20Sopenharmony_ci * 1728c2ecf20Sopenharmony_ci * Returns the new struct ubi_ainf_volume on success. 1738c2ecf20Sopenharmony_ci * NULL indicates an error. 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_cistatic struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id, 1768c2ecf20Sopenharmony_ci int used_ebs, int data_pad, u8 vol_type, 1778c2ecf20Sopenharmony_ci int last_eb_bytes) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci struct ubi_ainf_volume *av; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci av = ubi_add_av(ai, vol_id); 1828c2ecf20Sopenharmony_ci if (IS_ERR(av)) 1838c2ecf20Sopenharmony_ci return av; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci av->data_pad = data_pad; 1868c2ecf20Sopenharmony_ci av->last_data_size = last_eb_bytes; 1878c2ecf20Sopenharmony_ci av->compat = 0; 1888c2ecf20Sopenharmony_ci av->vol_type = vol_type; 1898c2ecf20Sopenharmony_ci if (av->vol_type == UBI_STATIC_VOLUME) 1908c2ecf20Sopenharmony_ci av->used_ebs = used_ebs; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci dbg_bld("found volume (ID %i)", vol_id); 1938c2ecf20Sopenharmony_ci return av; 1948c2ecf20Sopenharmony_ci} 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci/** 1978c2ecf20Sopenharmony_ci * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it 1988c2ecf20Sopenharmony_ci * from it's original list. 1998c2ecf20Sopenharmony_ci * @ai: ubi_attach_info object 2008c2ecf20Sopenharmony_ci * @aeb: the to be assigned SEB 2018c2ecf20Sopenharmony_ci * @av: target scan volume 2028c2ecf20Sopenharmony_ci */ 2038c2ecf20Sopenharmony_cistatic void assign_aeb_to_av(struct ubi_attach_info *ai, 2048c2ecf20Sopenharmony_ci struct ubi_ainf_peb *aeb, 2058c2ecf20Sopenharmony_ci struct ubi_ainf_volume *av) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci struct ubi_ainf_peb *tmp_aeb; 2088c2ecf20Sopenharmony_ci struct rb_node **p = &av->root.rb_node, *parent = NULL; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci while (*p) { 2118c2ecf20Sopenharmony_ci parent = *p; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb); 2148c2ecf20Sopenharmony_ci if (aeb->lnum != tmp_aeb->lnum) { 2158c2ecf20Sopenharmony_ci if (aeb->lnum < tmp_aeb->lnum) 2168c2ecf20Sopenharmony_ci p = &(*p)->rb_left; 2178c2ecf20Sopenharmony_ci else 2188c2ecf20Sopenharmony_ci p = &(*p)->rb_right; 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci continue; 2218c2ecf20Sopenharmony_ci } else 2228c2ecf20Sopenharmony_ci break; 2238c2ecf20Sopenharmony_ci } 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci list_del(&aeb->u.list); 2268c2ecf20Sopenharmony_ci av->leb_count++; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci rb_link_node(&aeb->u.rb, parent, p); 2298c2ecf20Sopenharmony_ci rb_insert_color(&aeb->u.rb, &av->root); 2308c2ecf20Sopenharmony_ci} 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci/** 2338c2ecf20Sopenharmony_ci * update_vol - inserts or updates a LEB which was found a pool. 2348c2ecf20Sopenharmony_ci * @ubi: the UBI device object 2358c2ecf20Sopenharmony_ci * @ai: attach info object 2368c2ecf20Sopenharmony_ci * @av: the volume this LEB belongs to 2378c2ecf20Sopenharmony_ci * @new_vh: the volume header derived from new_aeb 2388c2ecf20Sopenharmony_ci * @new_aeb: the AEB to be examined 2398c2ecf20Sopenharmony_ci * 2408c2ecf20Sopenharmony_ci * Returns 0 on success, < 0 indicates an internal error. 2418c2ecf20Sopenharmony_ci */ 2428c2ecf20Sopenharmony_cistatic int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai, 2438c2ecf20Sopenharmony_ci struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh, 2448c2ecf20Sopenharmony_ci struct ubi_ainf_peb *new_aeb) 2458c2ecf20Sopenharmony_ci{ 2468c2ecf20Sopenharmony_ci struct rb_node **p = &av->root.rb_node, *parent = NULL; 2478c2ecf20Sopenharmony_ci struct ubi_ainf_peb *aeb, *victim; 2488c2ecf20Sopenharmony_ci int cmp_res; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci while (*p) { 2518c2ecf20Sopenharmony_ci parent = *p; 2528c2ecf20Sopenharmony_ci aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci if (be32_to_cpu(new_vh->lnum) != aeb->lnum) { 2558c2ecf20Sopenharmony_ci if (be32_to_cpu(new_vh->lnum) < aeb->lnum) 2568c2ecf20Sopenharmony_ci p = &(*p)->rb_left; 2578c2ecf20Sopenharmony_ci else 2588c2ecf20Sopenharmony_ci p = &(*p)->rb_right; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci continue; 2618c2ecf20Sopenharmony_ci } 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci /* This case can happen if the fastmap gets written 2648c2ecf20Sopenharmony_ci * because of a volume change (creation, deletion, ..). 2658c2ecf20Sopenharmony_ci * Then a PEB can be within the persistent EBA and the pool. 2668c2ecf20Sopenharmony_ci */ 2678c2ecf20Sopenharmony_ci if (aeb->pnum == new_aeb->pnum) { 2688c2ecf20Sopenharmony_ci ubi_assert(aeb->lnum == new_aeb->lnum); 2698c2ecf20Sopenharmony_ci ubi_free_aeb(ai, new_aeb); 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci return 0; 2728c2ecf20Sopenharmony_ci } 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh); 2758c2ecf20Sopenharmony_ci if (cmp_res < 0) 2768c2ecf20Sopenharmony_ci return cmp_res; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci /* new_aeb is newer */ 2798c2ecf20Sopenharmony_ci if (cmp_res & 1) { 2808c2ecf20Sopenharmony_ci victim = ubi_alloc_aeb(ai, aeb->pnum, aeb->ec); 2818c2ecf20Sopenharmony_ci if (!victim) 2828c2ecf20Sopenharmony_ci return -ENOMEM; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci list_add_tail(&victim->u.list, &ai->erase); 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci if (av->highest_lnum == be32_to_cpu(new_vh->lnum)) 2878c2ecf20Sopenharmony_ci av->last_data_size = 2888c2ecf20Sopenharmony_ci be32_to_cpu(new_vh->data_size); 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci dbg_bld("vol %i: AEB %i's PEB %i is the newer", 2918c2ecf20Sopenharmony_ci av->vol_id, aeb->lnum, new_aeb->pnum); 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci aeb->ec = new_aeb->ec; 2948c2ecf20Sopenharmony_ci aeb->pnum = new_aeb->pnum; 2958c2ecf20Sopenharmony_ci aeb->copy_flag = new_vh->copy_flag; 2968c2ecf20Sopenharmony_ci aeb->scrub = new_aeb->scrub; 2978c2ecf20Sopenharmony_ci aeb->sqnum = new_aeb->sqnum; 2988c2ecf20Sopenharmony_ci ubi_free_aeb(ai, new_aeb); 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci /* new_aeb is older */ 3018c2ecf20Sopenharmony_ci } else { 3028c2ecf20Sopenharmony_ci dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it", 3038c2ecf20Sopenharmony_ci av->vol_id, aeb->lnum, new_aeb->pnum); 3048c2ecf20Sopenharmony_ci list_add_tail(&new_aeb->u.list, &ai->erase); 3058c2ecf20Sopenharmony_ci } 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci return 0; 3088c2ecf20Sopenharmony_ci } 3098c2ecf20Sopenharmony_ci /* This LEB is new, let's add it to the volume */ 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) { 3128c2ecf20Sopenharmony_ci av->highest_lnum = be32_to_cpu(new_vh->lnum); 3138c2ecf20Sopenharmony_ci av->last_data_size = be32_to_cpu(new_vh->data_size); 3148c2ecf20Sopenharmony_ci } 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci if (av->vol_type == UBI_STATIC_VOLUME) 3178c2ecf20Sopenharmony_ci av->used_ebs = be32_to_cpu(new_vh->used_ebs); 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci av->leb_count++; 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci rb_link_node(&new_aeb->u.rb, parent, p); 3228c2ecf20Sopenharmony_ci rb_insert_color(&new_aeb->u.rb, &av->root); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci return 0; 3258c2ecf20Sopenharmony_ci} 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci/** 3288c2ecf20Sopenharmony_ci * process_pool_aeb - we found a non-empty PEB in a pool. 3298c2ecf20Sopenharmony_ci * @ubi: UBI device object 3308c2ecf20Sopenharmony_ci * @ai: attach info object 3318c2ecf20Sopenharmony_ci * @new_vh: the volume header derived from new_aeb 3328c2ecf20Sopenharmony_ci * @new_aeb: the AEB to be examined 3338c2ecf20Sopenharmony_ci * 3348c2ecf20Sopenharmony_ci * Returns 0 on success, < 0 indicates an internal error. 3358c2ecf20Sopenharmony_ci */ 3368c2ecf20Sopenharmony_cistatic int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai, 3378c2ecf20Sopenharmony_ci struct ubi_vid_hdr *new_vh, 3388c2ecf20Sopenharmony_ci struct ubi_ainf_peb *new_aeb) 3398c2ecf20Sopenharmony_ci{ 3408c2ecf20Sopenharmony_ci int vol_id = be32_to_cpu(new_vh->vol_id); 3418c2ecf20Sopenharmony_ci struct ubi_ainf_volume *av; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci if (vol_id == UBI_FM_SB_VOLUME_ID || vol_id == UBI_FM_DATA_VOLUME_ID) { 3448c2ecf20Sopenharmony_ci ubi_free_aeb(ai, new_aeb); 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci return 0; 3478c2ecf20Sopenharmony_ci } 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci /* Find the volume this SEB belongs to */ 3508c2ecf20Sopenharmony_ci av = ubi_find_av(ai, vol_id); 3518c2ecf20Sopenharmony_ci if (!av) { 3528c2ecf20Sopenharmony_ci ubi_err(ubi, "orphaned volume in fastmap pool!"); 3538c2ecf20Sopenharmony_ci ubi_free_aeb(ai, new_aeb); 3548c2ecf20Sopenharmony_ci return UBI_BAD_FASTMAP; 3558c2ecf20Sopenharmony_ci } 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci ubi_assert(vol_id == av->vol_id); 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci return update_vol(ubi, ai, av, new_vh, new_aeb); 3608c2ecf20Sopenharmony_ci} 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci/** 3638c2ecf20Sopenharmony_ci * unmap_peb - unmap a PEB. 3648c2ecf20Sopenharmony_ci * If fastmap detects a free PEB in the pool it has to check whether 3658c2ecf20Sopenharmony_ci * this PEB has been unmapped after writing the fastmap. 3668c2ecf20Sopenharmony_ci * 3678c2ecf20Sopenharmony_ci * @ai: UBI attach info object 3688c2ecf20Sopenharmony_ci * @pnum: The PEB to be unmapped 3698c2ecf20Sopenharmony_ci */ 3708c2ecf20Sopenharmony_cistatic void unmap_peb(struct ubi_attach_info *ai, int pnum) 3718c2ecf20Sopenharmony_ci{ 3728c2ecf20Sopenharmony_ci struct ubi_ainf_volume *av; 3738c2ecf20Sopenharmony_ci struct rb_node *node, *node2; 3748c2ecf20Sopenharmony_ci struct ubi_ainf_peb *aeb; 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci ubi_rb_for_each_entry(node, av, &ai->volumes, rb) { 3778c2ecf20Sopenharmony_ci ubi_rb_for_each_entry(node2, aeb, &av->root, u.rb) { 3788c2ecf20Sopenharmony_ci if (aeb->pnum == pnum) { 3798c2ecf20Sopenharmony_ci rb_erase(&aeb->u.rb, &av->root); 3808c2ecf20Sopenharmony_ci av->leb_count--; 3818c2ecf20Sopenharmony_ci ubi_free_aeb(ai, aeb); 3828c2ecf20Sopenharmony_ci return; 3838c2ecf20Sopenharmony_ci } 3848c2ecf20Sopenharmony_ci } 3858c2ecf20Sopenharmony_ci } 3868c2ecf20Sopenharmony_ci} 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci/** 3898c2ecf20Sopenharmony_ci * scan_pool - scans a pool for changed (no longer empty PEBs). 3908c2ecf20Sopenharmony_ci * @ubi: UBI device object 3918c2ecf20Sopenharmony_ci * @ai: attach info object 3928c2ecf20Sopenharmony_ci * @pebs: an array of all PEB numbers in the to be scanned pool 3938c2ecf20Sopenharmony_ci * @pool_size: size of the pool (number of entries in @pebs) 3948c2ecf20Sopenharmony_ci * @max_sqnum: pointer to the maximal sequence number 3958c2ecf20Sopenharmony_ci * @free: list of PEBs which are most likely free (and go into @ai->free) 3968c2ecf20Sopenharmony_ci * 3978c2ecf20Sopenharmony_ci * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned. 3988c2ecf20Sopenharmony_ci * < 0 indicates an internal error. 3998c2ecf20Sopenharmony_ci */ 4008c2ecf20Sopenharmony_cistatic int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai, 4018c2ecf20Sopenharmony_ci __be32 *pebs, int pool_size, unsigned long long *max_sqnum, 4028c2ecf20Sopenharmony_ci struct list_head *free) 4038c2ecf20Sopenharmony_ci{ 4048c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vb; 4058c2ecf20Sopenharmony_ci struct ubi_vid_hdr *vh; 4068c2ecf20Sopenharmony_ci struct ubi_ec_hdr *ech; 4078c2ecf20Sopenharmony_ci struct ubi_ainf_peb *new_aeb; 4088c2ecf20Sopenharmony_ci int i, pnum, err, ret = 0; 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 4118c2ecf20Sopenharmony_ci if (!ech) 4128c2ecf20Sopenharmony_ci return -ENOMEM; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); 4158c2ecf20Sopenharmony_ci if (!vb) { 4168c2ecf20Sopenharmony_ci kfree(ech); 4178c2ecf20Sopenharmony_ci return -ENOMEM; 4188c2ecf20Sopenharmony_ci } 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci vh = ubi_get_vid_hdr(vb); 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci dbg_bld("scanning fastmap pool: size = %i", pool_size); 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci /* 4258c2ecf20Sopenharmony_ci * Now scan all PEBs in the pool to find changes which have been made 4268c2ecf20Sopenharmony_ci * after the creation of the fastmap 4278c2ecf20Sopenharmony_ci */ 4288c2ecf20Sopenharmony_ci for (i = 0; i < pool_size; i++) { 4298c2ecf20Sopenharmony_ci int scrub = 0; 4308c2ecf20Sopenharmony_ci int image_seq; 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci pnum = be32_to_cpu(pebs[i]); 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci if (ubi_io_is_bad(ubi, pnum)) { 4358c2ecf20Sopenharmony_ci ubi_err(ubi, "bad PEB in fastmap pool!"); 4368c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 4378c2ecf20Sopenharmony_ci goto out; 4388c2ecf20Sopenharmony_ci } 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_ci err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); 4418c2ecf20Sopenharmony_ci if (err && err != UBI_IO_BITFLIPS) { 4428c2ecf20Sopenharmony_ci ubi_err(ubi, "unable to read EC header! PEB:%i err:%i", 4438c2ecf20Sopenharmony_ci pnum, err); 4448c2ecf20Sopenharmony_ci ret = err > 0 ? UBI_BAD_FASTMAP : err; 4458c2ecf20Sopenharmony_ci goto out; 4468c2ecf20Sopenharmony_ci } else if (err == UBI_IO_BITFLIPS) 4478c2ecf20Sopenharmony_ci scrub = 1; 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci /* 4508c2ecf20Sopenharmony_ci * Older UBI implementations have image_seq set to zero, so 4518c2ecf20Sopenharmony_ci * we shouldn't fail if image_seq == 0. 4528c2ecf20Sopenharmony_ci */ 4538c2ecf20Sopenharmony_ci image_seq = be32_to_cpu(ech->image_seq); 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci if (image_seq && (image_seq != ubi->image_seq)) { 4568c2ecf20Sopenharmony_ci ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x", 4578c2ecf20Sopenharmony_ci be32_to_cpu(ech->image_seq), ubi->image_seq); 4588c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 4598c2ecf20Sopenharmony_ci goto out; 4608c2ecf20Sopenharmony_ci } 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ci err = ubi_io_read_vid_hdr(ubi, pnum, vb, 0); 4638c2ecf20Sopenharmony_ci if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) { 4648c2ecf20Sopenharmony_ci unsigned long long ec = be64_to_cpu(ech->ec); 4658c2ecf20Sopenharmony_ci unmap_peb(ai, pnum); 4668c2ecf20Sopenharmony_ci dbg_bld("Adding PEB to free: %i", pnum); 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci if (err == UBI_IO_FF_BITFLIPS) 4698c2ecf20Sopenharmony_ci scrub = 1; 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci ret = add_aeb(ai, free, pnum, ec, scrub); 4728c2ecf20Sopenharmony_ci if (ret) 4738c2ecf20Sopenharmony_ci goto out; 4748c2ecf20Sopenharmony_ci continue; 4758c2ecf20Sopenharmony_ci } else if (err == 0 || err == UBI_IO_BITFLIPS) { 4768c2ecf20Sopenharmony_ci dbg_bld("Found non empty PEB:%i in pool", pnum); 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci if (err == UBI_IO_BITFLIPS) 4798c2ecf20Sopenharmony_ci scrub = 1; 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci new_aeb = ubi_alloc_aeb(ai, pnum, be64_to_cpu(ech->ec)); 4828c2ecf20Sopenharmony_ci if (!new_aeb) { 4838c2ecf20Sopenharmony_ci ret = -ENOMEM; 4848c2ecf20Sopenharmony_ci goto out; 4858c2ecf20Sopenharmony_ci } 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_ci new_aeb->lnum = be32_to_cpu(vh->lnum); 4888c2ecf20Sopenharmony_ci new_aeb->sqnum = be64_to_cpu(vh->sqnum); 4898c2ecf20Sopenharmony_ci new_aeb->copy_flag = vh->copy_flag; 4908c2ecf20Sopenharmony_ci new_aeb->scrub = scrub; 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci if (*max_sqnum < new_aeb->sqnum) 4938c2ecf20Sopenharmony_ci *max_sqnum = new_aeb->sqnum; 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci err = process_pool_aeb(ubi, ai, vh, new_aeb); 4968c2ecf20Sopenharmony_ci if (err) { 4978c2ecf20Sopenharmony_ci ret = err > 0 ? UBI_BAD_FASTMAP : err; 4988c2ecf20Sopenharmony_ci goto out; 4998c2ecf20Sopenharmony_ci } 5008c2ecf20Sopenharmony_ci } else { 5018c2ecf20Sopenharmony_ci /* We are paranoid and fall back to scanning mode */ 5028c2ecf20Sopenharmony_ci ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!"); 5038c2ecf20Sopenharmony_ci ret = err > 0 ? UBI_BAD_FASTMAP : err; 5048c2ecf20Sopenharmony_ci goto out; 5058c2ecf20Sopenharmony_ci } 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci } 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ciout: 5108c2ecf20Sopenharmony_ci ubi_free_vid_buf(vb); 5118c2ecf20Sopenharmony_ci kfree(ech); 5128c2ecf20Sopenharmony_ci return ret; 5138c2ecf20Sopenharmony_ci} 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci/** 5168c2ecf20Sopenharmony_ci * count_fastmap_pebs - Counts the PEBs found by fastmap. 5178c2ecf20Sopenharmony_ci * @ai: The UBI attach info object 5188c2ecf20Sopenharmony_ci */ 5198c2ecf20Sopenharmony_cistatic int count_fastmap_pebs(struct ubi_attach_info *ai) 5208c2ecf20Sopenharmony_ci{ 5218c2ecf20Sopenharmony_ci struct ubi_ainf_peb *aeb; 5228c2ecf20Sopenharmony_ci struct ubi_ainf_volume *av; 5238c2ecf20Sopenharmony_ci struct rb_node *rb1, *rb2; 5248c2ecf20Sopenharmony_ci int n = 0; 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci list_for_each_entry(aeb, &ai->erase, u.list) 5278c2ecf20Sopenharmony_ci n++; 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci list_for_each_entry(aeb, &ai->free, u.list) 5308c2ecf20Sopenharmony_ci n++; 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) 5338c2ecf20Sopenharmony_ci ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) 5348c2ecf20Sopenharmony_ci n++; 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci return n; 5378c2ecf20Sopenharmony_ci} 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci/** 5408c2ecf20Sopenharmony_ci * ubi_attach_fastmap - creates ubi_attach_info from a fastmap. 5418c2ecf20Sopenharmony_ci * @ubi: UBI device object 5428c2ecf20Sopenharmony_ci * @ai: UBI attach info object 5438c2ecf20Sopenharmony_ci * @fm: the fastmap to be attached 5448c2ecf20Sopenharmony_ci * 5458c2ecf20Sopenharmony_ci * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable. 5468c2ecf20Sopenharmony_ci * < 0 indicates an internal error. 5478c2ecf20Sopenharmony_ci */ 5488c2ecf20Sopenharmony_cistatic int ubi_attach_fastmap(struct ubi_device *ubi, 5498c2ecf20Sopenharmony_ci struct ubi_attach_info *ai, 5508c2ecf20Sopenharmony_ci struct ubi_fastmap_layout *fm) 5518c2ecf20Sopenharmony_ci{ 5528c2ecf20Sopenharmony_ci struct list_head used, free; 5538c2ecf20Sopenharmony_ci struct ubi_ainf_volume *av; 5548c2ecf20Sopenharmony_ci struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb; 5558c2ecf20Sopenharmony_ci struct ubi_fm_sb *fmsb; 5568c2ecf20Sopenharmony_ci struct ubi_fm_hdr *fmhdr; 5578c2ecf20Sopenharmony_ci struct ubi_fm_scan_pool *fmpl, *fmpl_wl; 5588c2ecf20Sopenharmony_ci struct ubi_fm_ec *fmec; 5598c2ecf20Sopenharmony_ci struct ubi_fm_volhdr *fmvhdr; 5608c2ecf20Sopenharmony_ci struct ubi_fm_eba *fm_eba; 5618c2ecf20Sopenharmony_ci int ret, i, j, pool_size, wl_pool_size; 5628c2ecf20Sopenharmony_ci size_t fm_pos = 0, fm_size = ubi->fm_size; 5638c2ecf20Sopenharmony_ci unsigned long long max_sqnum = 0; 5648c2ecf20Sopenharmony_ci void *fm_raw = ubi->fm_buf; 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&used); 5678c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&free); 5688c2ecf20Sopenharmony_ci ai->min_ec = UBI_MAX_ERASECOUNTER; 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_ci fmsb = (struct ubi_fm_sb *)(fm_raw); 5718c2ecf20Sopenharmony_ci ai->max_sqnum = fmsb->sqnum; 5728c2ecf20Sopenharmony_ci fm_pos += sizeof(struct ubi_fm_sb); 5738c2ecf20Sopenharmony_ci if (fm_pos >= fm_size) 5748c2ecf20Sopenharmony_ci goto fail_bad; 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_ci fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos); 5778c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmhdr); 5788c2ecf20Sopenharmony_ci if (fm_pos >= fm_size) 5798c2ecf20Sopenharmony_ci goto fail_bad; 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) { 5828c2ecf20Sopenharmony_ci ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x", 5838c2ecf20Sopenharmony_ci be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC); 5848c2ecf20Sopenharmony_ci goto fail_bad; 5858c2ecf20Sopenharmony_ci } 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_ci fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); 5888c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmpl); 5898c2ecf20Sopenharmony_ci if (fm_pos >= fm_size) 5908c2ecf20Sopenharmony_ci goto fail_bad; 5918c2ecf20Sopenharmony_ci if (be32_to_cpu(fmpl->magic) != UBI_FM_POOL_MAGIC) { 5928c2ecf20Sopenharmony_ci ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x", 5938c2ecf20Sopenharmony_ci be32_to_cpu(fmpl->magic), UBI_FM_POOL_MAGIC); 5948c2ecf20Sopenharmony_ci goto fail_bad; 5958c2ecf20Sopenharmony_ci } 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); 5988c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmpl_wl); 5998c2ecf20Sopenharmony_ci if (fm_pos >= fm_size) 6008c2ecf20Sopenharmony_ci goto fail_bad; 6018c2ecf20Sopenharmony_ci if (be32_to_cpu(fmpl_wl->magic) != UBI_FM_POOL_MAGIC) { 6028c2ecf20Sopenharmony_ci ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x", 6038c2ecf20Sopenharmony_ci be32_to_cpu(fmpl_wl->magic), UBI_FM_POOL_MAGIC); 6048c2ecf20Sopenharmony_ci goto fail_bad; 6058c2ecf20Sopenharmony_ci } 6068c2ecf20Sopenharmony_ci 6078c2ecf20Sopenharmony_ci pool_size = be16_to_cpu(fmpl->size); 6088c2ecf20Sopenharmony_ci wl_pool_size = be16_to_cpu(fmpl_wl->size); 6098c2ecf20Sopenharmony_ci fm->max_pool_size = be16_to_cpu(fmpl->max_size); 6108c2ecf20Sopenharmony_ci fm->max_wl_pool_size = be16_to_cpu(fmpl_wl->max_size); 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ci if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) { 6138c2ecf20Sopenharmony_ci ubi_err(ubi, "bad pool size: %i", pool_size); 6148c2ecf20Sopenharmony_ci goto fail_bad; 6158c2ecf20Sopenharmony_ci } 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) { 6188c2ecf20Sopenharmony_ci ubi_err(ubi, "bad WL pool size: %i", wl_pool_size); 6198c2ecf20Sopenharmony_ci goto fail_bad; 6208c2ecf20Sopenharmony_ci } 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE || 6248c2ecf20Sopenharmony_ci fm->max_pool_size < 0) { 6258c2ecf20Sopenharmony_ci ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size); 6268c2ecf20Sopenharmony_ci goto fail_bad; 6278c2ecf20Sopenharmony_ci } 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE || 6308c2ecf20Sopenharmony_ci fm->max_wl_pool_size < 0) { 6318c2ecf20Sopenharmony_ci ubi_err(ubi, "bad maximal WL pool size: %i", 6328c2ecf20Sopenharmony_ci fm->max_wl_pool_size); 6338c2ecf20Sopenharmony_ci goto fail_bad; 6348c2ecf20Sopenharmony_ci } 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_ci /* read EC values from free list */ 6378c2ecf20Sopenharmony_ci for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) { 6388c2ecf20Sopenharmony_ci fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); 6398c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmec); 6408c2ecf20Sopenharmony_ci if (fm_pos >= fm_size) 6418c2ecf20Sopenharmony_ci goto fail_bad; 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci ret = add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum), 6448c2ecf20Sopenharmony_ci be32_to_cpu(fmec->ec), 0); 6458c2ecf20Sopenharmony_ci if (ret) 6468c2ecf20Sopenharmony_ci goto fail; 6478c2ecf20Sopenharmony_ci } 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci /* read EC values from used list */ 6508c2ecf20Sopenharmony_ci for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) { 6518c2ecf20Sopenharmony_ci fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); 6528c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmec); 6538c2ecf20Sopenharmony_ci if (fm_pos >= fm_size) 6548c2ecf20Sopenharmony_ci goto fail_bad; 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci ret = add_aeb(ai, &used, be32_to_cpu(fmec->pnum), 6578c2ecf20Sopenharmony_ci be32_to_cpu(fmec->ec), 0); 6588c2ecf20Sopenharmony_ci if (ret) 6598c2ecf20Sopenharmony_ci goto fail; 6608c2ecf20Sopenharmony_ci } 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_ci /* read EC values from scrub list */ 6638c2ecf20Sopenharmony_ci for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) { 6648c2ecf20Sopenharmony_ci fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); 6658c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmec); 6668c2ecf20Sopenharmony_ci if (fm_pos >= fm_size) 6678c2ecf20Sopenharmony_ci goto fail_bad; 6688c2ecf20Sopenharmony_ci 6698c2ecf20Sopenharmony_ci ret = add_aeb(ai, &used, be32_to_cpu(fmec->pnum), 6708c2ecf20Sopenharmony_ci be32_to_cpu(fmec->ec), 1); 6718c2ecf20Sopenharmony_ci if (ret) 6728c2ecf20Sopenharmony_ci goto fail; 6738c2ecf20Sopenharmony_ci } 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci /* read EC values from erase list */ 6768c2ecf20Sopenharmony_ci for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) { 6778c2ecf20Sopenharmony_ci fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); 6788c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmec); 6798c2ecf20Sopenharmony_ci if (fm_pos >= fm_size) 6808c2ecf20Sopenharmony_ci goto fail_bad; 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_ci ret = add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum), 6838c2ecf20Sopenharmony_ci be32_to_cpu(fmec->ec), 1); 6848c2ecf20Sopenharmony_ci if (ret) 6858c2ecf20Sopenharmony_ci goto fail; 6868c2ecf20Sopenharmony_ci } 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count); 6898c2ecf20Sopenharmony_ci ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count); 6908c2ecf20Sopenharmony_ci 6918c2ecf20Sopenharmony_ci /* Iterate over all volumes and read their EBA table */ 6928c2ecf20Sopenharmony_ci for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) { 6938c2ecf20Sopenharmony_ci fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos); 6948c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmvhdr); 6958c2ecf20Sopenharmony_ci if (fm_pos >= fm_size) 6968c2ecf20Sopenharmony_ci goto fail_bad; 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) { 6998c2ecf20Sopenharmony_ci ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x", 7008c2ecf20Sopenharmony_ci be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC); 7018c2ecf20Sopenharmony_ci goto fail_bad; 7028c2ecf20Sopenharmony_ci } 7038c2ecf20Sopenharmony_ci 7048c2ecf20Sopenharmony_ci av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id), 7058c2ecf20Sopenharmony_ci be32_to_cpu(fmvhdr->used_ebs), 7068c2ecf20Sopenharmony_ci be32_to_cpu(fmvhdr->data_pad), 7078c2ecf20Sopenharmony_ci fmvhdr->vol_type, 7088c2ecf20Sopenharmony_ci be32_to_cpu(fmvhdr->last_eb_bytes)); 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci if (IS_ERR(av)) { 7118c2ecf20Sopenharmony_ci if (PTR_ERR(av) == -EEXIST) 7128c2ecf20Sopenharmony_ci ubi_err(ubi, "volume (ID %i) already exists", 7138c2ecf20Sopenharmony_ci fmvhdr->vol_id); 7148c2ecf20Sopenharmony_ci 7158c2ecf20Sopenharmony_ci goto fail_bad; 7168c2ecf20Sopenharmony_ci } 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci ai->vols_found++; 7198c2ecf20Sopenharmony_ci if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id)) 7208c2ecf20Sopenharmony_ci ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id); 7218c2ecf20Sopenharmony_ci 7228c2ecf20Sopenharmony_ci fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos); 7238c2ecf20Sopenharmony_ci fm_pos += sizeof(*fm_eba); 7248c2ecf20Sopenharmony_ci fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs)); 7258c2ecf20Sopenharmony_ci if (fm_pos >= fm_size) 7268c2ecf20Sopenharmony_ci goto fail_bad; 7278c2ecf20Sopenharmony_ci 7288c2ecf20Sopenharmony_ci if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) { 7298c2ecf20Sopenharmony_ci ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x", 7308c2ecf20Sopenharmony_ci be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC); 7318c2ecf20Sopenharmony_ci goto fail_bad; 7328c2ecf20Sopenharmony_ci } 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_ci for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) { 7358c2ecf20Sopenharmony_ci int pnum = be32_to_cpu(fm_eba->pnum[j]); 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci if (pnum < 0) 7388c2ecf20Sopenharmony_ci continue; 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_ci aeb = NULL; 7418c2ecf20Sopenharmony_ci list_for_each_entry(tmp_aeb, &used, u.list) { 7428c2ecf20Sopenharmony_ci if (tmp_aeb->pnum == pnum) { 7438c2ecf20Sopenharmony_ci aeb = tmp_aeb; 7448c2ecf20Sopenharmony_ci break; 7458c2ecf20Sopenharmony_ci } 7468c2ecf20Sopenharmony_ci } 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci if (!aeb) { 7498c2ecf20Sopenharmony_ci ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum); 7508c2ecf20Sopenharmony_ci goto fail_bad; 7518c2ecf20Sopenharmony_ci } 7528c2ecf20Sopenharmony_ci 7538c2ecf20Sopenharmony_ci aeb->lnum = j; 7548c2ecf20Sopenharmony_ci 7558c2ecf20Sopenharmony_ci if (av->highest_lnum <= aeb->lnum) 7568c2ecf20Sopenharmony_ci av->highest_lnum = aeb->lnum; 7578c2ecf20Sopenharmony_ci 7588c2ecf20Sopenharmony_ci assign_aeb_to_av(ai, aeb, av); 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_ci dbg_bld("inserting PEB:%i (LEB %i) to vol %i", 7618c2ecf20Sopenharmony_ci aeb->pnum, aeb->lnum, av->vol_id); 7628c2ecf20Sopenharmony_ci } 7638c2ecf20Sopenharmony_ci } 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_ci ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free); 7668c2ecf20Sopenharmony_ci if (ret) 7678c2ecf20Sopenharmony_ci goto fail; 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_ci ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free); 7708c2ecf20Sopenharmony_ci if (ret) 7718c2ecf20Sopenharmony_ci goto fail; 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ci if (max_sqnum > ai->max_sqnum) 7748c2ecf20Sopenharmony_ci ai->max_sqnum = max_sqnum; 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ci list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) 7778c2ecf20Sopenharmony_ci list_move_tail(&tmp_aeb->u.list, &ai->free); 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_ci list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) 7808c2ecf20Sopenharmony_ci list_move_tail(&tmp_aeb->u.list, &ai->erase); 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci ubi_assert(list_empty(&free)); 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci /* 7858c2ecf20Sopenharmony_ci * If fastmap is leaking PEBs (must not happen), raise a 7868c2ecf20Sopenharmony_ci * fat warning and fall back to scanning mode. 7878c2ecf20Sopenharmony_ci * We do this here because in ubi_wl_init() it's too late 7888c2ecf20Sopenharmony_ci * and we cannot fall back to scanning. 7898c2ecf20Sopenharmony_ci */ 7908c2ecf20Sopenharmony_ci if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count - 7918c2ecf20Sopenharmony_ci ai->bad_peb_count - fm->used_blocks)) 7928c2ecf20Sopenharmony_ci goto fail_bad; 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_ci return 0; 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_cifail_bad: 7978c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 7988c2ecf20Sopenharmony_cifail: 7998c2ecf20Sopenharmony_ci list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) { 8008c2ecf20Sopenharmony_ci list_del(&tmp_aeb->u.list); 8018c2ecf20Sopenharmony_ci ubi_free_aeb(ai, tmp_aeb); 8028c2ecf20Sopenharmony_ci } 8038c2ecf20Sopenharmony_ci list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) { 8048c2ecf20Sopenharmony_ci list_del(&tmp_aeb->u.list); 8058c2ecf20Sopenharmony_ci ubi_free_aeb(ai, tmp_aeb); 8068c2ecf20Sopenharmony_ci } 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_ci return ret; 8098c2ecf20Sopenharmony_ci} 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_ci/** 8128c2ecf20Sopenharmony_ci * find_fm_anchor - find the most recent Fastmap superblock (anchor) 8138c2ecf20Sopenharmony_ci * @ai: UBI attach info to be filled 8148c2ecf20Sopenharmony_ci */ 8158c2ecf20Sopenharmony_cistatic int find_fm_anchor(struct ubi_attach_info *ai) 8168c2ecf20Sopenharmony_ci{ 8178c2ecf20Sopenharmony_ci int ret = -1; 8188c2ecf20Sopenharmony_ci struct ubi_ainf_peb *aeb; 8198c2ecf20Sopenharmony_ci unsigned long long max_sqnum = 0; 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci list_for_each_entry(aeb, &ai->fastmap, u.list) { 8228c2ecf20Sopenharmony_ci if (aeb->vol_id == UBI_FM_SB_VOLUME_ID && aeb->sqnum > max_sqnum) { 8238c2ecf20Sopenharmony_ci max_sqnum = aeb->sqnum; 8248c2ecf20Sopenharmony_ci ret = aeb->pnum; 8258c2ecf20Sopenharmony_ci } 8268c2ecf20Sopenharmony_ci } 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci return ret; 8298c2ecf20Sopenharmony_ci} 8308c2ecf20Sopenharmony_ci 8318c2ecf20Sopenharmony_ci/** 8328c2ecf20Sopenharmony_ci * ubi_scan_fastmap - scan the fastmap. 8338c2ecf20Sopenharmony_ci * @ubi: UBI device object 8348c2ecf20Sopenharmony_ci * @ai: UBI attach info to be filled 8358c2ecf20Sopenharmony_ci * @scan_ai: UBI attach info from the first 64 PEBs, 8368c2ecf20Sopenharmony_ci * used to find the most recent Fastmap data structure 8378c2ecf20Sopenharmony_ci * 8388c2ecf20Sopenharmony_ci * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found, 8398c2ecf20Sopenharmony_ci * UBI_BAD_FASTMAP if one was found but is not usable. 8408c2ecf20Sopenharmony_ci * < 0 indicates an internal error. 8418c2ecf20Sopenharmony_ci */ 8428c2ecf20Sopenharmony_ciint ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai, 8438c2ecf20Sopenharmony_ci struct ubi_attach_info *scan_ai) 8448c2ecf20Sopenharmony_ci{ 8458c2ecf20Sopenharmony_ci struct ubi_fm_sb *fmsb, *fmsb2; 8468c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vb; 8478c2ecf20Sopenharmony_ci struct ubi_vid_hdr *vh; 8488c2ecf20Sopenharmony_ci struct ubi_ec_hdr *ech; 8498c2ecf20Sopenharmony_ci struct ubi_fastmap_layout *fm; 8508c2ecf20Sopenharmony_ci struct ubi_ainf_peb *aeb; 8518c2ecf20Sopenharmony_ci int i, used_blocks, pnum, fm_anchor, ret = 0; 8528c2ecf20Sopenharmony_ci size_t fm_size; 8538c2ecf20Sopenharmony_ci __be32 crc, tmp_crc; 8548c2ecf20Sopenharmony_ci unsigned long long sqnum = 0; 8558c2ecf20Sopenharmony_ci 8568c2ecf20Sopenharmony_ci fm_anchor = find_fm_anchor(scan_ai); 8578c2ecf20Sopenharmony_ci if (fm_anchor < 0) 8588c2ecf20Sopenharmony_ci return UBI_NO_FASTMAP; 8598c2ecf20Sopenharmony_ci 8608c2ecf20Sopenharmony_ci /* Add fastmap blocks(pnum < UBI_FM_MAX_START) into attach structure. */ 8618c2ecf20Sopenharmony_ci list_for_each_entry(aeb, &scan_ai->fastmap, u.list) { 8628c2ecf20Sopenharmony_ci ret = add_aeb(ai, &ai->fastmap, aeb->pnum, aeb->ec, 0); 8638c2ecf20Sopenharmony_ci if (ret) 8648c2ecf20Sopenharmony_ci return ret; 8658c2ecf20Sopenharmony_ci } 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_ci down_write(&ubi->fm_protect); 8688c2ecf20Sopenharmony_ci memset(ubi->fm_buf, 0, ubi->fm_size); 8698c2ecf20Sopenharmony_ci 8708c2ecf20Sopenharmony_ci fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL); 8718c2ecf20Sopenharmony_ci if (!fmsb) { 8728c2ecf20Sopenharmony_ci ret = -ENOMEM; 8738c2ecf20Sopenharmony_ci goto out; 8748c2ecf20Sopenharmony_ci } 8758c2ecf20Sopenharmony_ci 8768c2ecf20Sopenharmony_ci fm = kzalloc(sizeof(*fm), GFP_KERNEL); 8778c2ecf20Sopenharmony_ci if (!fm) { 8788c2ecf20Sopenharmony_ci ret = -ENOMEM; 8798c2ecf20Sopenharmony_ci kfree(fmsb); 8808c2ecf20Sopenharmony_ci goto out; 8818c2ecf20Sopenharmony_ci } 8828c2ecf20Sopenharmony_ci 8838c2ecf20Sopenharmony_ci ret = ubi_io_read_data(ubi, fmsb, fm_anchor, 0, sizeof(*fmsb)); 8848c2ecf20Sopenharmony_ci if (ret && ret != UBI_IO_BITFLIPS) 8858c2ecf20Sopenharmony_ci goto free_fm_sb; 8868c2ecf20Sopenharmony_ci else if (ret == UBI_IO_BITFLIPS) 8878c2ecf20Sopenharmony_ci fm->to_be_tortured[0] = 1; 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ci if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) { 8908c2ecf20Sopenharmony_ci ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x", 8918c2ecf20Sopenharmony_ci be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC); 8928c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 8938c2ecf20Sopenharmony_ci goto free_fm_sb; 8948c2ecf20Sopenharmony_ci } 8958c2ecf20Sopenharmony_ci 8968c2ecf20Sopenharmony_ci if (fmsb->version != UBI_FM_FMT_VERSION) { 8978c2ecf20Sopenharmony_ci ubi_err(ubi, "bad fastmap version: %i, expected: %i", 8988c2ecf20Sopenharmony_ci fmsb->version, UBI_FM_FMT_VERSION); 8998c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 9008c2ecf20Sopenharmony_ci goto free_fm_sb; 9018c2ecf20Sopenharmony_ci } 9028c2ecf20Sopenharmony_ci 9038c2ecf20Sopenharmony_ci used_blocks = be32_to_cpu(fmsb->used_blocks); 9048c2ecf20Sopenharmony_ci if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) { 9058c2ecf20Sopenharmony_ci ubi_err(ubi, "number of fastmap blocks is invalid: %i", 9068c2ecf20Sopenharmony_ci used_blocks); 9078c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 9088c2ecf20Sopenharmony_ci goto free_fm_sb; 9098c2ecf20Sopenharmony_ci } 9108c2ecf20Sopenharmony_ci 9118c2ecf20Sopenharmony_ci fm_size = ubi->leb_size * used_blocks; 9128c2ecf20Sopenharmony_ci if (fm_size != ubi->fm_size) { 9138c2ecf20Sopenharmony_ci ubi_err(ubi, "bad fastmap size: %zi, expected: %zi", 9148c2ecf20Sopenharmony_ci fm_size, ubi->fm_size); 9158c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 9168c2ecf20Sopenharmony_ci goto free_fm_sb; 9178c2ecf20Sopenharmony_ci } 9188c2ecf20Sopenharmony_ci 9198c2ecf20Sopenharmony_ci ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 9208c2ecf20Sopenharmony_ci if (!ech) { 9218c2ecf20Sopenharmony_ci ret = -ENOMEM; 9228c2ecf20Sopenharmony_ci goto free_fm_sb; 9238c2ecf20Sopenharmony_ci } 9248c2ecf20Sopenharmony_ci 9258c2ecf20Sopenharmony_ci vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); 9268c2ecf20Sopenharmony_ci if (!vb) { 9278c2ecf20Sopenharmony_ci ret = -ENOMEM; 9288c2ecf20Sopenharmony_ci goto free_hdr; 9298c2ecf20Sopenharmony_ci } 9308c2ecf20Sopenharmony_ci 9318c2ecf20Sopenharmony_ci vh = ubi_get_vid_hdr(vb); 9328c2ecf20Sopenharmony_ci 9338c2ecf20Sopenharmony_ci for (i = 0; i < used_blocks; i++) { 9348c2ecf20Sopenharmony_ci int image_seq; 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci pnum = be32_to_cpu(fmsb->block_loc[i]); 9378c2ecf20Sopenharmony_ci 9388c2ecf20Sopenharmony_ci if (ubi_io_is_bad(ubi, pnum)) { 9398c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 9408c2ecf20Sopenharmony_ci goto free_hdr; 9418c2ecf20Sopenharmony_ci } 9428c2ecf20Sopenharmony_ci 9438c2ecf20Sopenharmony_ci if (i == 0 && pnum != fm_anchor) { 9448c2ecf20Sopenharmony_ci ubi_err(ubi, "Fastmap anchor PEB mismatch: PEB: %i vs. %i", 9458c2ecf20Sopenharmony_ci pnum, fm_anchor); 9468c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 9478c2ecf20Sopenharmony_ci goto free_hdr; 9488c2ecf20Sopenharmony_ci } 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_ci ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); 9518c2ecf20Sopenharmony_ci if (ret && ret != UBI_IO_BITFLIPS) { 9528c2ecf20Sopenharmony_ci ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)", 9538c2ecf20Sopenharmony_ci i, pnum); 9548c2ecf20Sopenharmony_ci if (ret > 0) 9558c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 9568c2ecf20Sopenharmony_ci goto free_hdr; 9578c2ecf20Sopenharmony_ci } else if (ret == UBI_IO_BITFLIPS) 9588c2ecf20Sopenharmony_ci fm->to_be_tortured[i] = 1; 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci image_seq = be32_to_cpu(ech->image_seq); 9618c2ecf20Sopenharmony_ci if (!ubi->image_seq) 9628c2ecf20Sopenharmony_ci ubi->image_seq = image_seq; 9638c2ecf20Sopenharmony_ci 9648c2ecf20Sopenharmony_ci /* 9658c2ecf20Sopenharmony_ci * Older UBI implementations have image_seq set to zero, so 9668c2ecf20Sopenharmony_ci * we shouldn't fail if image_seq == 0. 9678c2ecf20Sopenharmony_ci */ 9688c2ecf20Sopenharmony_ci if (image_seq && (image_seq != ubi->image_seq)) { 9698c2ecf20Sopenharmony_ci ubi_err(ubi, "wrong image seq:%d instead of %d", 9708c2ecf20Sopenharmony_ci be32_to_cpu(ech->image_seq), ubi->image_seq); 9718c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 9728c2ecf20Sopenharmony_ci goto free_hdr; 9738c2ecf20Sopenharmony_ci } 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_ci ret = ubi_io_read_vid_hdr(ubi, pnum, vb, 0); 9768c2ecf20Sopenharmony_ci if (ret && ret != UBI_IO_BITFLIPS) { 9778c2ecf20Sopenharmony_ci ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)", 9788c2ecf20Sopenharmony_ci i, pnum); 9798c2ecf20Sopenharmony_ci goto free_hdr; 9808c2ecf20Sopenharmony_ci } 9818c2ecf20Sopenharmony_ci 9828c2ecf20Sopenharmony_ci if (i == 0) { 9838c2ecf20Sopenharmony_ci if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) { 9848c2ecf20Sopenharmony_ci ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x", 9858c2ecf20Sopenharmony_ci be32_to_cpu(vh->vol_id), 9868c2ecf20Sopenharmony_ci UBI_FM_SB_VOLUME_ID); 9878c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 9888c2ecf20Sopenharmony_ci goto free_hdr; 9898c2ecf20Sopenharmony_ci } 9908c2ecf20Sopenharmony_ci } else { 9918c2ecf20Sopenharmony_ci if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) { 9928c2ecf20Sopenharmony_ci ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x", 9938c2ecf20Sopenharmony_ci be32_to_cpu(vh->vol_id), 9948c2ecf20Sopenharmony_ci UBI_FM_DATA_VOLUME_ID); 9958c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 9968c2ecf20Sopenharmony_ci goto free_hdr; 9978c2ecf20Sopenharmony_ci } 9988c2ecf20Sopenharmony_ci } 9998c2ecf20Sopenharmony_ci 10008c2ecf20Sopenharmony_ci if (sqnum < be64_to_cpu(vh->sqnum)) 10018c2ecf20Sopenharmony_ci sqnum = be64_to_cpu(vh->sqnum); 10028c2ecf20Sopenharmony_ci 10038c2ecf20Sopenharmony_ci ret = ubi_io_read_data(ubi, ubi->fm_buf + (ubi->leb_size * i), 10048c2ecf20Sopenharmony_ci pnum, 0, ubi->leb_size); 10058c2ecf20Sopenharmony_ci if (ret && ret != UBI_IO_BITFLIPS) { 10068c2ecf20Sopenharmony_ci ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, " 10078c2ecf20Sopenharmony_ci "err: %i)", i, pnum, ret); 10088c2ecf20Sopenharmony_ci goto free_hdr; 10098c2ecf20Sopenharmony_ci } 10108c2ecf20Sopenharmony_ci 10118c2ecf20Sopenharmony_ci /* 10128c2ecf20Sopenharmony_ci * Add left fastmap blocks (pnum >= UBI_FM_MAX_START) into 10138c2ecf20Sopenharmony_ci * attach structure. 10148c2ecf20Sopenharmony_ci */ 10158c2ecf20Sopenharmony_ci if (pnum >= UBI_FM_MAX_START) { 10168c2ecf20Sopenharmony_ci ret = add_aeb(ai, &ai->fastmap, pnum, 10178c2ecf20Sopenharmony_ci be64_to_cpu(ech->ec), 0); 10188c2ecf20Sopenharmony_ci if (ret) 10198c2ecf20Sopenharmony_ci goto free_hdr; 10208c2ecf20Sopenharmony_ci } 10218c2ecf20Sopenharmony_ci } 10228c2ecf20Sopenharmony_ci 10238c2ecf20Sopenharmony_ci kfree(fmsb); 10248c2ecf20Sopenharmony_ci fmsb = NULL; 10258c2ecf20Sopenharmony_ci 10268c2ecf20Sopenharmony_ci fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf); 10278c2ecf20Sopenharmony_ci tmp_crc = be32_to_cpu(fmsb2->data_crc); 10288c2ecf20Sopenharmony_ci fmsb2->data_crc = 0; 10298c2ecf20Sopenharmony_ci crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size); 10308c2ecf20Sopenharmony_ci if (crc != tmp_crc) { 10318c2ecf20Sopenharmony_ci ubi_err(ubi, "fastmap data CRC is invalid"); 10328c2ecf20Sopenharmony_ci ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x", 10338c2ecf20Sopenharmony_ci tmp_crc, crc); 10348c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 10358c2ecf20Sopenharmony_ci goto free_hdr; 10368c2ecf20Sopenharmony_ci } 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci fmsb2->sqnum = sqnum; 10398c2ecf20Sopenharmony_ci 10408c2ecf20Sopenharmony_ci fm->used_blocks = used_blocks; 10418c2ecf20Sopenharmony_ci 10428c2ecf20Sopenharmony_ci ret = ubi_attach_fastmap(ubi, ai, fm); 10438c2ecf20Sopenharmony_ci if (ret) { 10448c2ecf20Sopenharmony_ci if (ret > 0) 10458c2ecf20Sopenharmony_ci ret = UBI_BAD_FASTMAP; 10468c2ecf20Sopenharmony_ci goto free_hdr; 10478c2ecf20Sopenharmony_ci } 10488c2ecf20Sopenharmony_ci 10498c2ecf20Sopenharmony_ci for (i = 0; i < used_blocks; i++) { 10508c2ecf20Sopenharmony_ci struct ubi_wl_entry *e; 10518c2ecf20Sopenharmony_ci 10528c2ecf20Sopenharmony_ci e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); 10538c2ecf20Sopenharmony_ci if (!e) { 10548c2ecf20Sopenharmony_ci while (i--) 10558c2ecf20Sopenharmony_ci kmem_cache_free(ubi_wl_entry_slab, fm->e[i]); 10568c2ecf20Sopenharmony_ci 10578c2ecf20Sopenharmony_ci ret = -ENOMEM; 10588c2ecf20Sopenharmony_ci goto free_hdr; 10598c2ecf20Sopenharmony_ci } 10608c2ecf20Sopenharmony_ci 10618c2ecf20Sopenharmony_ci e->pnum = be32_to_cpu(fmsb2->block_loc[i]); 10628c2ecf20Sopenharmony_ci e->ec = be32_to_cpu(fmsb2->block_ec[i]); 10638c2ecf20Sopenharmony_ci fm->e[i] = e; 10648c2ecf20Sopenharmony_ci } 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_ci ubi->fm = fm; 10678c2ecf20Sopenharmony_ci ubi->fm_pool.max_size = ubi->fm->max_pool_size; 10688c2ecf20Sopenharmony_ci ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size; 10698c2ecf20Sopenharmony_ci ubi_msg(ubi, "attached by fastmap"); 10708c2ecf20Sopenharmony_ci ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size); 10718c2ecf20Sopenharmony_ci ubi_msg(ubi, "fastmap WL pool size: %d", 10728c2ecf20Sopenharmony_ci ubi->fm_wl_pool.max_size); 10738c2ecf20Sopenharmony_ci ubi->fm_disabled = 0; 10748c2ecf20Sopenharmony_ci ubi->fast_attach = 1; 10758c2ecf20Sopenharmony_ci 10768c2ecf20Sopenharmony_ci ubi_free_vid_buf(vb); 10778c2ecf20Sopenharmony_ci kfree(ech); 10788c2ecf20Sopenharmony_ciout: 10798c2ecf20Sopenharmony_ci up_write(&ubi->fm_protect); 10808c2ecf20Sopenharmony_ci if (ret == UBI_BAD_FASTMAP) 10818c2ecf20Sopenharmony_ci ubi_err(ubi, "Attach by fastmap failed, doing a full scan!"); 10828c2ecf20Sopenharmony_ci return ret; 10838c2ecf20Sopenharmony_ci 10848c2ecf20Sopenharmony_cifree_hdr: 10858c2ecf20Sopenharmony_ci ubi_free_vid_buf(vb); 10868c2ecf20Sopenharmony_ci kfree(ech); 10878c2ecf20Sopenharmony_cifree_fm_sb: 10888c2ecf20Sopenharmony_ci kfree(fmsb); 10898c2ecf20Sopenharmony_ci kfree(fm); 10908c2ecf20Sopenharmony_ci goto out; 10918c2ecf20Sopenharmony_ci} 10928c2ecf20Sopenharmony_ci 10938c2ecf20Sopenharmony_ciint ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count) 10948c2ecf20Sopenharmony_ci{ 10958c2ecf20Sopenharmony_ci struct ubi_device *ubi = vol->ubi; 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_ci if (!ubi->fast_attach) 10988c2ecf20Sopenharmony_ci return 0; 10998c2ecf20Sopenharmony_ci 11008c2ecf20Sopenharmony_ci vol->checkmap = kcalloc(BITS_TO_LONGS(leb_count), sizeof(unsigned long), 11018c2ecf20Sopenharmony_ci GFP_KERNEL); 11028c2ecf20Sopenharmony_ci if (!vol->checkmap) 11038c2ecf20Sopenharmony_ci return -ENOMEM; 11048c2ecf20Sopenharmony_ci 11058c2ecf20Sopenharmony_ci return 0; 11068c2ecf20Sopenharmony_ci} 11078c2ecf20Sopenharmony_ci 11088c2ecf20Sopenharmony_civoid ubi_fastmap_destroy_checkmap(struct ubi_volume *vol) 11098c2ecf20Sopenharmony_ci{ 11108c2ecf20Sopenharmony_ci kfree(vol->checkmap); 11118c2ecf20Sopenharmony_ci} 11128c2ecf20Sopenharmony_ci 11138c2ecf20Sopenharmony_ci/** 11148c2ecf20Sopenharmony_ci * ubi_write_fastmap - writes a fastmap. 11158c2ecf20Sopenharmony_ci * @ubi: UBI device object 11168c2ecf20Sopenharmony_ci * @new_fm: the to be written fastmap 11178c2ecf20Sopenharmony_ci * 11188c2ecf20Sopenharmony_ci * Returns 0 on success, < 0 indicates an internal error. 11198c2ecf20Sopenharmony_ci */ 11208c2ecf20Sopenharmony_cistatic int ubi_write_fastmap(struct ubi_device *ubi, 11218c2ecf20Sopenharmony_ci struct ubi_fastmap_layout *new_fm) 11228c2ecf20Sopenharmony_ci{ 11238c2ecf20Sopenharmony_ci size_t fm_pos = 0; 11248c2ecf20Sopenharmony_ci void *fm_raw; 11258c2ecf20Sopenharmony_ci struct ubi_fm_sb *fmsb; 11268c2ecf20Sopenharmony_ci struct ubi_fm_hdr *fmh; 11278c2ecf20Sopenharmony_ci struct ubi_fm_scan_pool *fmpl, *fmpl_wl; 11288c2ecf20Sopenharmony_ci struct ubi_fm_ec *fec; 11298c2ecf20Sopenharmony_ci struct ubi_fm_volhdr *fvh; 11308c2ecf20Sopenharmony_ci struct ubi_fm_eba *feba; 11318c2ecf20Sopenharmony_ci struct ubi_wl_entry *wl_e; 11328c2ecf20Sopenharmony_ci struct ubi_volume *vol; 11338c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *avbuf, *dvbuf; 11348c2ecf20Sopenharmony_ci struct ubi_vid_hdr *avhdr, *dvhdr; 11358c2ecf20Sopenharmony_ci struct ubi_work *ubi_wrk; 11368c2ecf20Sopenharmony_ci struct rb_node *tmp_rb; 11378c2ecf20Sopenharmony_ci int ret, i, j, free_peb_count, used_peb_count, vol_count; 11388c2ecf20Sopenharmony_ci int scrub_peb_count, erase_peb_count; 11398c2ecf20Sopenharmony_ci unsigned long *seen_pebs; 11408c2ecf20Sopenharmony_ci 11418c2ecf20Sopenharmony_ci fm_raw = ubi->fm_buf; 11428c2ecf20Sopenharmony_ci memset(ubi->fm_buf, 0, ubi->fm_size); 11438c2ecf20Sopenharmony_ci 11448c2ecf20Sopenharmony_ci avbuf = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID); 11458c2ecf20Sopenharmony_ci if (!avbuf) { 11468c2ecf20Sopenharmony_ci ret = -ENOMEM; 11478c2ecf20Sopenharmony_ci goto out; 11488c2ecf20Sopenharmony_ci } 11498c2ecf20Sopenharmony_ci 11508c2ecf20Sopenharmony_ci dvbuf = new_fm_vbuf(ubi, UBI_FM_DATA_VOLUME_ID); 11518c2ecf20Sopenharmony_ci if (!dvbuf) { 11528c2ecf20Sopenharmony_ci ret = -ENOMEM; 11538c2ecf20Sopenharmony_ci goto out_free_avbuf; 11548c2ecf20Sopenharmony_ci } 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_ci avhdr = ubi_get_vid_hdr(avbuf); 11578c2ecf20Sopenharmony_ci dvhdr = ubi_get_vid_hdr(dvbuf); 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_ci seen_pebs = init_seen(ubi); 11608c2ecf20Sopenharmony_ci if (IS_ERR(seen_pebs)) { 11618c2ecf20Sopenharmony_ci ret = PTR_ERR(seen_pebs); 11628c2ecf20Sopenharmony_ci goto out_free_dvbuf; 11638c2ecf20Sopenharmony_ci } 11648c2ecf20Sopenharmony_ci 11658c2ecf20Sopenharmony_ci spin_lock(&ubi->volumes_lock); 11668c2ecf20Sopenharmony_ci spin_lock(&ubi->wl_lock); 11678c2ecf20Sopenharmony_ci 11688c2ecf20Sopenharmony_ci fmsb = (struct ubi_fm_sb *)fm_raw; 11698c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmsb); 11708c2ecf20Sopenharmony_ci ubi_assert(fm_pos <= ubi->fm_size); 11718c2ecf20Sopenharmony_ci 11728c2ecf20Sopenharmony_ci fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos); 11738c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmh); 11748c2ecf20Sopenharmony_ci ubi_assert(fm_pos <= ubi->fm_size); 11758c2ecf20Sopenharmony_ci 11768c2ecf20Sopenharmony_ci fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC); 11778c2ecf20Sopenharmony_ci fmsb->version = UBI_FM_FMT_VERSION; 11788c2ecf20Sopenharmony_ci fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks); 11798c2ecf20Sopenharmony_ci /* the max sqnum will be filled in while *reading* the fastmap */ 11808c2ecf20Sopenharmony_ci fmsb->sqnum = 0; 11818c2ecf20Sopenharmony_ci 11828c2ecf20Sopenharmony_ci fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC); 11838c2ecf20Sopenharmony_ci free_peb_count = 0; 11848c2ecf20Sopenharmony_ci used_peb_count = 0; 11858c2ecf20Sopenharmony_ci scrub_peb_count = 0; 11868c2ecf20Sopenharmony_ci erase_peb_count = 0; 11878c2ecf20Sopenharmony_ci vol_count = 0; 11888c2ecf20Sopenharmony_ci 11898c2ecf20Sopenharmony_ci fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); 11908c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmpl); 11918c2ecf20Sopenharmony_ci fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC); 11928c2ecf20Sopenharmony_ci fmpl->size = cpu_to_be16(ubi->fm_pool.size); 11938c2ecf20Sopenharmony_ci fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size); 11948c2ecf20Sopenharmony_ci 11958c2ecf20Sopenharmony_ci for (i = 0; i < ubi->fm_pool.size; i++) { 11968c2ecf20Sopenharmony_ci fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]); 11978c2ecf20Sopenharmony_ci set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs); 11988c2ecf20Sopenharmony_ci } 11998c2ecf20Sopenharmony_ci 12008c2ecf20Sopenharmony_ci fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); 12018c2ecf20Sopenharmony_ci fm_pos += sizeof(*fmpl_wl); 12028c2ecf20Sopenharmony_ci fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC); 12038c2ecf20Sopenharmony_ci fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size); 12048c2ecf20Sopenharmony_ci fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size); 12058c2ecf20Sopenharmony_ci 12068c2ecf20Sopenharmony_ci for (i = 0; i < ubi->fm_wl_pool.size; i++) { 12078c2ecf20Sopenharmony_ci fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]); 12088c2ecf20Sopenharmony_ci set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs); 12098c2ecf20Sopenharmony_ci } 12108c2ecf20Sopenharmony_ci 12118c2ecf20Sopenharmony_ci ubi_for_each_free_peb(ubi, wl_e, tmp_rb) { 12128c2ecf20Sopenharmony_ci fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); 12138c2ecf20Sopenharmony_ci 12148c2ecf20Sopenharmony_ci fec->pnum = cpu_to_be32(wl_e->pnum); 12158c2ecf20Sopenharmony_ci set_seen(ubi, wl_e->pnum, seen_pebs); 12168c2ecf20Sopenharmony_ci fec->ec = cpu_to_be32(wl_e->ec); 12178c2ecf20Sopenharmony_ci 12188c2ecf20Sopenharmony_ci free_peb_count++; 12198c2ecf20Sopenharmony_ci fm_pos += sizeof(*fec); 12208c2ecf20Sopenharmony_ci ubi_assert(fm_pos <= ubi->fm_size); 12218c2ecf20Sopenharmony_ci } 12228c2ecf20Sopenharmony_ci fmh->free_peb_count = cpu_to_be32(free_peb_count); 12238c2ecf20Sopenharmony_ci 12248c2ecf20Sopenharmony_ci ubi_for_each_used_peb(ubi, wl_e, tmp_rb) { 12258c2ecf20Sopenharmony_ci fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); 12268c2ecf20Sopenharmony_ci 12278c2ecf20Sopenharmony_ci fec->pnum = cpu_to_be32(wl_e->pnum); 12288c2ecf20Sopenharmony_ci set_seen(ubi, wl_e->pnum, seen_pebs); 12298c2ecf20Sopenharmony_ci fec->ec = cpu_to_be32(wl_e->ec); 12308c2ecf20Sopenharmony_ci 12318c2ecf20Sopenharmony_ci used_peb_count++; 12328c2ecf20Sopenharmony_ci fm_pos += sizeof(*fec); 12338c2ecf20Sopenharmony_ci ubi_assert(fm_pos <= ubi->fm_size); 12348c2ecf20Sopenharmony_ci } 12358c2ecf20Sopenharmony_ci 12368c2ecf20Sopenharmony_ci ubi_for_each_protected_peb(ubi, i, wl_e) { 12378c2ecf20Sopenharmony_ci fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); 12388c2ecf20Sopenharmony_ci 12398c2ecf20Sopenharmony_ci fec->pnum = cpu_to_be32(wl_e->pnum); 12408c2ecf20Sopenharmony_ci set_seen(ubi, wl_e->pnum, seen_pebs); 12418c2ecf20Sopenharmony_ci fec->ec = cpu_to_be32(wl_e->ec); 12428c2ecf20Sopenharmony_ci 12438c2ecf20Sopenharmony_ci used_peb_count++; 12448c2ecf20Sopenharmony_ci fm_pos += sizeof(*fec); 12458c2ecf20Sopenharmony_ci ubi_assert(fm_pos <= ubi->fm_size); 12468c2ecf20Sopenharmony_ci } 12478c2ecf20Sopenharmony_ci fmh->used_peb_count = cpu_to_be32(used_peb_count); 12488c2ecf20Sopenharmony_ci 12498c2ecf20Sopenharmony_ci ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) { 12508c2ecf20Sopenharmony_ci fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); 12518c2ecf20Sopenharmony_ci 12528c2ecf20Sopenharmony_ci fec->pnum = cpu_to_be32(wl_e->pnum); 12538c2ecf20Sopenharmony_ci set_seen(ubi, wl_e->pnum, seen_pebs); 12548c2ecf20Sopenharmony_ci fec->ec = cpu_to_be32(wl_e->ec); 12558c2ecf20Sopenharmony_ci 12568c2ecf20Sopenharmony_ci scrub_peb_count++; 12578c2ecf20Sopenharmony_ci fm_pos += sizeof(*fec); 12588c2ecf20Sopenharmony_ci ubi_assert(fm_pos <= ubi->fm_size); 12598c2ecf20Sopenharmony_ci } 12608c2ecf20Sopenharmony_ci fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count); 12618c2ecf20Sopenharmony_ci 12628c2ecf20Sopenharmony_ci 12638c2ecf20Sopenharmony_ci list_for_each_entry(ubi_wrk, &ubi->works, list) { 12648c2ecf20Sopenharmony_ci if (ubi_is_erase_work(ubi_wrk)) { 12658c2ecf20Sopenharmony_ci wl_e = ubi_wrk->e; 12668c2ecf20Sopenharmony_ci ubi_assert(wl_e); 12678c2ecf20Sopenharmony_ci 12688c2ecf20Sopenharmony_ci fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); 12698c2ecf20Sopenharmony_ci 12708c2ecf20Sopenharmony_ci fec->pnum = cpu_to_be32(wl_e->pnum); 12718c2ecf20Sopenharmony_ci set_seen(ubi, wl_e->pnum, seen_pebs); 12728c2ecf20Sopenharmony_ci fec->ec = cpu_to_be32(wl_e->ec); 12738c2ecf20Sopenharmony_ci 12748c2ecf20Sopenharmony_ci erase_peb_count++; 12758c2ecf20Sopenharmony_ci fm_pos += sizeof(*fec); 12768c2ecf20Sopenharmony_ci ubi_assert(fm_pos <= ubi->fm_size); 12778c2ecf20Sopenharmony_ci } 12788c2ecf20Sopenharmony_ci } 12798c2ecf20Sopenharmony_ci fmh->erase_peb_count = cpu_to_be32(erase_peb_count); 12808c2ecf20Sopenharmony_ci 12818c2ecf20Sopenharmony_ci for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) { 12828c2ecf20Sopenharmony_ci vol = ubi->volumes[i]; 12838c2ecf20Sopenharmony_ci 12848c2ecf20Sopenharmony_ci if (!vol) 12858c2ecf20Sopenharmony_ci continue; 12868c2ecf20Sopenharmony_ci 12878c2ecf20Sopenharmony_ci vol_count++; 12888c2ecf20Sopenharmony_ci 12898c2ecf20Sopenharmony_ci fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos); 12908c2ecf20Sopenharmony_ci fm_pos += sizeof(*fvh); 12918c2ecf20Sopenharmony_ci ubi_assert(fm_pos <= ubi->fm_size); 12928c2ecf20Sopenharmony_ci 12938c2ecf20Sopenharmony_ci fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC); 12948c2ecf20Sopenharmony_ci fvh->vol_id = cpu_to_be32(vol->vol_id); 12958c2ecf20Sopenharmony_ci fvh->vol_type = vol->vol_type; 12968c2ecf20Sopenharmony_ci fvh->used_ebs = cpu_to_be32(vol->used_ebs); 12978c2ecf20Sopenharmony_ci fvh->data_pad = cpu_to_be32(vol->data_pad); 12988c2ecf20Sopenharmony_ci fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes); 12998c2ecf20Sopenharmony_ci 13008c2ecf20Sopenharmony_ci ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME || 13018c2ecf20Sopenharmony_ci vol->vol_type == UBI_STATIC_VOLUME); 13028c2ecf20Sopenharmony_ci 13038c2ecf20Sopenharmony_ci feba = (struct ubi_fm_eba *)(fm_raw + fm_pos); 13048c2ecf20Sopenharmony_ci fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs); 13058c2ecf20Sopenharmony_ci ubi_assert(fm_pos <= ubi->fm_size); 13068c2ecf20Sopenharmony_ci 13078c2ecf20Sopenharmony_ci for (j = 0; j < vol->reserved_pebs; j++) { 13088c2ecf20Sopenharmony_ci struct ubi_eba_leb_desc ldesc; 13098c2ecf20Sopenharmony_ci 13108c2ecf20Sopenharmony_ci ubi_eba_get_ldesc(vol, j, &ldesc); 13118c2ecf20Sopenharmony_ci feba->pnum[j] = cpu_to_be32(ldesc.pnum); 13128c2ecf20Sopenharmony_ci } 13138c2ecf20Sopenharmony_ci 13148c2ecf20Sopenharmony_ci feba->reserved_pebs = cpu_to_be32(j); 13158c2ecf20Sopenharmony_ci feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC); 13168c2ecf20Sopenharmony_ci } 13178c2ecf20Sopenharmony_ci fmh->vol_count = cpu_to_be32(vol_count); 13188c2ecf20Sopenharmony_ci fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count); 13198c2ecf20Sopenharmony_ci 13208c2ecf20Sopenharmony_ci avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 13218c2ecf20Sopenharmony_ci avhdr->lnum = 0; 13228c2ecf20Sopenharmony_ci 13238c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 13248c2ecf20Sopenharmony_ci spin_unlock(&ubi->volumes_lock); 13258c2ecf20Sopenharmony_ci 13268c2ecf20Sopenharmony_ci dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum); 13278c2ecf20Sopenharmony_ci ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avbuf); 13288c2ecf20Sopenharmony_ci if (ret) { 13298c2ecf20Sopenharmony_ci ubi_err(ubi, "unable to write vid_hdr to fastmap SB!"); 13308c2ecf20Sopenharmony_ci goto out_free_seen; 13318c2ecf20Sopenharmony_ci } 13328c2ecf20Sopenharmony_ci 13338c2ecf20Sopenharmony_ci for (i = 0; i < new_fm->used_blocks; i++) { 13348c2ecf20Sopenharmony_ci fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum); 13358c2ecf20Sopenharmony_ci set_seen(ubi, new_fm->e[i]->pnum, seen_pebs); 13368c2ecf20Sopenharmony_ci fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec); 13378c2ecf20Sopenharmony_ci } 13388c2ecf20Sopenharmony_ci 13398c2ecf20Sopenharmony_ci fmsb->data_crc = 0; 13408c2ecf20Sopenharmony_ci fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw, 13418c2ecf20Sopenharmony_ci ubi->fm_size)); 13428c2ecf20Sopenharmony_ci 13438c2ecf20Sopenharmony_ci for (i = 1; i < new_fm->used_blocks; i++) { 13448c2ecf20Sopenharmony_ci dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 13458c2ecf20Sopenharmony_ci dvhdr->lnum = cpu_to_be32(i); 13468c2ecf20Sopenharmony_ci dbg_bld("writing fastmap data to PEB %i sqnum %llu", 13478c2ecf20Sopenharmony_ci new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum)); 13488c2ecf20Sopenharmony_ci ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvbuf); 13498c2ecf20Sopenharmony_ci if (ret) { 13508c2ecf20Sopenharmony_ci ubi_err(ubi, "unable to write vid_hdr to PEB %i!", 13518c2ecf20Sopenharmony_ci new_fm->e[i]->pnum); 13528c2ecf20Sopenharmony_ci goto out_free_seen; 13538c2ecf20Sopenharmony_ci } 13548c2ecf20Sopenharmony_ci } 13558c2ecf20Sopenharmony_ci 13568c2ecf20Sopenharmony_ci for (i = 0; i < new_fm->used_blocks; i++) { 13578c2ecf20Sopenharmony_ci ret = ubi_io_write_data(ubi, fm_raw + (i * ubi->leb_size), 13588c2ecf20Sopenharmony_ci new_fm->e[i]->pnum, 0, ubi->leb_size); 13598c2ecf20Sopenharmony_ci if (ret) { 13608c2ecf20Sopenharmony_ci ubi_err(ubi, "unable to write fastmap to PEB %i!", 13618c2ecf20Sopenharmony_ci new_fm->e[i]->pnum); 13628c2ecf20Sopenharmony_ci goto out_free_seen; 13638c2ecf20Sopenharmony_ci } 13648c2ecf20Sopenharmony_ci } 13658c2ecf20Sopenharmony_ci 13668c2ecf20Sopenharmony_ci ubi_assert(new_fm); 13678c2ecf20Sopenharmony_ci ubi->fm = new_fm; 13688c2ecf20Sopenharmony_ci 13698c2ecf20Sopenharmony_ci ret = self_check_seen(ubi, seen_pebs); 13708c2ecf20Sopenharmony_ci dbg_bld("fastmap written!"); 13718c2ecf20Sopenharmony_ci 13728c2ecf20Sopenharmony_ciout_free_seen: 13738c2ecf20Sopenharmony_ci free_seen(seen_pebs); 13748c2ecf20Sopenharmony_ciout_free_dvbuf: 13758c2ecf20Sopenharmony_ci ubi_free_vid_buf(dvbuf); 13768c2ecf20Sopenharmony_ciout_free_avbuf: 13778c2ecf20Sopenharmony_ci ubi_free_vid_buf(avbuf); 13788c2ecf20Sopenharmony_ci 13798c2ecf20Sopenharmony_ciout: 13808c2ecf20Sopenharmony_ci return ret; 13818c2ecf20Sopenharmony_ci} 13828c2ecf20Sopenharmony_ci 13838c2ecf20Sopenharmony_ci/** 13848c2ecf20Sopenharmony_ci * erase_block - Manually erase a PEB. 13858c2ecf20Sopenharmony_ci * @ubi: UBI device object 13868c2ecf20Sopenharmony_ci * @pnum: PEB to be erased 13878c2ecf20Sopenharmony_ci * 13888c2ecf20Sopenharmony_ci * Returns the new EC value on success, < 0 indicates an internal error. 13898c2ecf20Sopenharmony_ci */ 13908c2ecf20Sopenharmony_cistatic int erase_block(struct ubi_device *ubi, int pnum) 13918c2ecf20Sopenharmony_ci{ 13928c2ecf20Sopenharmony_ci int ret; 13938c2ecf20Sopenharmony_ci struct ubi_ec_hdr *ec_hdr; 13948c2ecf20Sopenharmony_ci long long ec; 13958c2ecf20Sopenharmony_ci 13968c2ecf20Sopenharmony_ci ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 13978c2ecf20Sopenharmony_ci if (!ec_hdr) 13988c2ecf20Sopenharmony_ci return -ENOMEM; 13998c2ecf20Sopenharmony_ci 14008c2ecf20Sopenharmony_ci ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0); 14018c2ecf20Sopenharmony_ci if (ret < 0) 14028c2ecf20Sopenharmony_ci goto out; 14038c2ecf20Sopenharmony_ci else if (ret && ret != UBI_IO_BITFLIPS) { 14048c2ecf20Sopenharmony_ci ret = -EINVAL; 14058c2ecf20Sopenharmony_ci goto out; 14068c2ecf20Sopenharmony_ci } 14078c2ecf20Sopenharmony_ci 14088c2ecf20Sopenharmony_ci ret = ubi_io_sync_erase(ubi, pnum, 0); 14098c2ecf20Sopenharmony_ci if (ret < 0) 14108c2ecf20Sopenharmony_ci goto out; 14118c2ecf20Sopenharmony_ci 14128c2ecf20Sopenharmony_ci ec = be64_to_cpu(ec_hdr->ec); 14138c2ecf20Sopenharmony_ci ec += ret; 14148c2ecf20Sopenharmony_ci if (ec > UBI_MAX_ERASECOUNTER) { 14158c2ecf20Sopenharmony_ci ret = -EINVAL; 14168c2ecf20Sopenharmony_ci goto out; 14178c2ecf20Sopenharmony_ci } 14188c2ecf20Sopenharmony_ci 14198c2ecf20Sopenharmony_ci ec_hdr->ec = cpu_to_be64(ec); 14208c2ecf20Sopenharmony_ci ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); 14218c2ecf20Sopenharmony_ci if (ret < 0) 14228c2ecf20Sopenharmony_ci goto out; 14238c2ecf20Sopenharmony_ci 14248c2ecf20Sopenharmony_ci ret = ec; 14258c2ecf20Sopenharmony_ciout: 14268c2ecf20Sopenharmony_ci kfree(ec_hdr); 14278c2ecf20Sopenharmony_ci return ret; 14288c2ecf20Sopenharmony_ci} 14298c2ecf20Sopenharmony_ci 14308c2ecf20Sopenharmony_ci/** 14318c2ecf20Sopenharmony_ci * invalidate_fastmap - destroys a fastmap. 14328c2ecf20Sopenharmony_ci * @ubi: UBI device object 14338c2ecf20Sopenharmony_ci * 14348c2ecf20Sopenharmony_ci * This function ensures that upon next UBI attach a full scan 14358c2ecf20Sopenharmony_ci * is issued. We need this if UBI is about to write a new fastmap 14368c2ecf20Sopenharmony_ci * but is unable to do so. In this case we have two options: 14378c2ecf20Sopenharmony_ci * a) Make sure that the current fastmap will not be usued upon 14388c2ecf20Sopenharmony_ci * attach time and contine or b) fall back to RO mode to have the 14398c2ecf20Sopenharmony_ci * current fastmap in a valid state. 14408c2ecf20Sopenharmony_ci * Returns 0 on success, < 0 indicates an internal error. 14418c2ecf20Sopenharmony_ci */ 14428c2ecf20Sopenharmony_cistatic int invalidate_fastmap(struct ubi_device *ubi) 14438c2ecf20Sopenharmony_ci{ 14448c2ecf20Sopenharmony_ci int ret; 14458c2ecf20Sopenharmony_ci struct ubi_fastmap_layout *fm; 14468c2ecf20Sopenharmony_ci struct ubi_wl_entry *e; 14478c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vb = NULL; 14488c2ecf20Sopenharmony_ci struct ubi_vid_hdr *vh; 14498c2ecf20Sopenharmony_ci 14508c2ecf20Sopenharmony_ci if (!ubi->fm) 14518c2ecf20Sopenharmony_ci return 0; 14528c2ecf20Sopenharmony_ci 14538c2ecf20Sopenharmony_ci ubi->fm = NULL; 14548c2ecf20Sopenharmony_ci 14558c2ecf20Sopenharmony_ci ret = -ENOMEM; 14568c2ecf20Sopenharmony_ci fm = kzalloc(sizeof(*fm), GFP_KERNEL); 14578c2ecf20Sopenharmony_ci if (!fm) 14588c2ecf20Sopenharmony_ci goto out; 14598c2ecf20Sopenharmony_ci 14608c2ecf20Sopenharmony_ci vb = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID); 14618c2ecf20Sopenharmony_ci if (!vb) 14628c2ecf20Sopenharmony_ci goto out_free_fm; 14638c2ecf20Sopenharmony_ci 14648c2ecf20Sopenharmony_ci vh = ubi_get_vid_hdr(vb); 14658c2ecf20Sopenharmony_ci 14668c2ecf20Sopenharmony_ci ret = -ENOSPC; 14678c2ecf20Sopenharmony_ci e = ubi_wl_get_fm_peb(ubi, 1); 14688c2ecf20Sopenharmony_ci if (!e) 14698c2ecf20Sopenharmony_ci goto out_free_fm; 14708c2ecf20Sopenharmony_ci 14718c2ecf20Sopenharmony_ci /* 14728c2ecf20Sopenharmony_ci * Create fake fastmap such that UBI will fall back 14738c2ecf20Sopenharmony_ci * to scanning mode. 14748c2ecf20Sopenharmony_ci */ 14758c2ecf20Sopenharmony_ci vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 14768c2ecf20Sopenharmony_ci ret = ubi_io_write_vid_hdr(ubi, e->pnum, vb); 14778c2ecf20Sopenharmony_ci if (ret < 0) { 14788c2ecf20Sopenharmony_ci ubi_wl_put_fm_peb(ubi, e, 0, 0); 14798c2ecf20Sopenharmony_ci goto out_free_fm; 14808c2ecf20Sopenharmony_ci } 14818c2ecf20Sopenharmony_ci 14828c2ecf20Sopenharmony_ci fm->used_blocks = 1; 14838c2ecf20Sopenharmony_ci fm->e[0] = e; 14848c2ecf20Sopenharmony_ci 14858c2ecf20Sopenharmony_ci ubi->fm = fm; 14868c2ecf20Sopenharmony_ci 14878c2ecf20Sopenharmony_ciout: 14888c2ecf20Sopenharmony_ci ubi_free_vid_buf(vb); 14898c2ecf20Sopenharmony_ci return ret; 14908c2ecf20Sopenharmony_ci 14918c2ecf20Sopenharmony_ciout_free_fm: 14928c2ecf20Sopenharmony_ci kfree(fm); 14938c2ecf20Sopenharmony_ci goto out; 14948c2ecf20Sopenharmony_ci} 14958c2ecf20Sopenharmony_ci 14968c2ecf20Sopenharmony_ci/** 14978c2ecf20Sopenharmony_ci * return_fm_pebs - returns all PEBs used by a fastmap back to the 14988c2ecf20Sopenharmony_ci * WL sub-system. 14998c2ecf20Sopenharmony_ci * @ubi: UBI device object 15008c2ecf20Sopenharmony_ci * @fm: fastmap layout object 15018c2ecf20Sopenharmony_ci */ 15028c2ecf20Sopenharmony_cistatic void return_fm_pebs(struct ubi_device *ubi, 15038c2ecf20Sopenharmony_ci struct ubi_fastmap_layout *fm) 15048c2ecf20Sopenharmony_ci{ 15058c2ecf20Sopenharmony_ci int i; 15068c2ecf20Sopenharmony_ci 15078c2ecf20Sopenharmony_ci if (!fm) 15088c2ecf20Sopenharmony_ci return; 15098c2ecf20Sopenharmony_ci 15108c2ecf20Sopenharmony_ci for (i = 0; i < fm->used_blocks; i++) { 15118c2ecf20Sopenharmony_ci if (fm->e[i]) { 15128c2ecf20Sopenharmony_ci ubi_wl_put_fm_peb(ubi, fm->e[i], i, 15138c2ecf20Sopenharmony_ci fm->to_be_tortured[i]); 15148c2ecf20Sopenharmony_ci fm->e[i] = NULL; 15158c2ecf20Sopenharmony_ci } 15168c2ecf20Sopenharmony_ci } 15178c2ecf20Sopenharmony_ci} 15188c2ecf20Sopenharmony_ci 15198c2ecf20Sopenharmony_ci/** 15208c2ecf20Sopenharmony_ci * ubi_update_fastmap - will be called by UBI if a volume changes or 15218c2ecf20Sopenharmony_ci * a fastmap pool becomes full. 15228c2ecf20Sopenharmony_ci * @ubi: UBI device object 15238c2ecf20Sopenharmony_ci * 15248c2ecf20Sopenharmony_ci * Returns 0 on success, < 0 indicates an internal error. 15258c2ecf20Sopenharmony_ci */ 15268c2ecf20Sopenharmony_ciint ubi_update_fastmap(struct ubi_device *ubi) 15278c2ecf20Sopenharmony_ci{ 15288c2ecf20Sopenharmony_ci int ret, i, j; 15298c2ecf20Sopenharmony_ci struct ubi_fastmap_layout *new_fm, *old_fm; 15308c2ecf20Sopenharmony_ci struct ubi_wl_entry *tmp_e; 15318c2ecf20Sopenharmony_ci 15328c2ecf20Sopenharmony_ci down_write(&ubi->fm_protect); 15338c2ecf20Sopenharmony_ci down_write(&ubi->work_sem); 15348c2ecf20Sopenharmony_ci down_write(&ubi->fm_eba_sem); 15358c2ecf20Sopenharmony_ci 15368c2ecf20Sopenharmony_ci ubi_refill_pools(ubi); 15378c2ecf20Sopenharmony_ci 15388c2ecf20Sopenharmony_ci if (ubi->ro_mode || ubi->fm_disabled) { 15398c2ecf20Sopenharmony_ci up_write(&ubi->fm_eba_sem); 15408c2ecf20Sopenharmony_ci up_write(&ubi->work_sem); 15418c2ecf20Sopenharmony_ci up_write(&ubi->fm_protect); 15428c2ecf20Sopenharmony_ci return 0; 15438c2ecf20Sopenharmony_ci } 15448c2ecf20Sopenharmony_ci 15458c2ecf20Sopenharmony_ci new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL); 15468c2ecf20Sopenharmony_ci if (!new_fm) { 15478c2ecf20Sopenharmony_ci up_write(&ubi->fm_eba_sem); 15488c2ecf20Sopenharmony_ci up_write(&ubi->work_sem); 15498c2ecf20Sopenharmony_ci up_write(&ubi->fm_protect); 15508c2ecf20Sopenharmony_ci return -ENOMEM; 15518c2ecf20Sopenharmony_ci } 15528c2ecf20Sopenharmony_ci 15538c2ecf20Sopenharmony_ci new_fm->used_blocks = ubi->fm_size / ubi->leb_size; 15548c2ecf20Sopenharmony_ci old_fm = ubi->fm; 15558c2ecf20Sopenharmony_ci ubi->fm = NULL; 15568c2ecf20Sopenharmony_ci 15578c2ecf20Sopenharmony_ci if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) { 15588c2ecf20Sopenharmony_ci ubi_err(ubi, "fastmap too large"); 15598c2ecf20Sopenharmony_ci ret = -ENOSPC; 15608c2ecf20Sopenharmony_ci goto err; 15618c2ecf20Sopenharmony_ci } 15628c2ecf20Sopenharmony_ci 15638c2ecf20Sopenharmony_ci for (i = 1; i < new_fm->used_blocks; i++) { 15648c2ecf20Sopenharmony_ci spin_lock(&ubi->wl_lock); 15658c2ecf20Sopenharmony_ci tmp_e = ubi_wl_get_fm_peb(ubi, 0); 15668c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 15678c2ecf20Sopenharmony_ci 15688c2ecf20Sopenharmony_ci if (!tmp_e) { 15698c2ecf20Sopenharmony_ci if (old_fm && old_fm->e[i]) { 15708c2ecf20Sopenharmony_ci ret = erase_block(ubi, old_fm->e[i]->pnum); 15718c2ecf20Sopenharmony_ci if (ret < 0) { 15728c2ecf20Sopenharmony_ci ubi_err(ubi, "could not erase old fastmap PEB"); 15738c2ecf20Sopenharmony_ci 15748c2ecf20Sopenharmony_ci for (j = 1; j < i; j++) { 15758c2ecf20Sopenharmony_ci ubi_wl_put_fm_peb(ubi, new_fm->e[j], 15768c2ecf20Sopenharmony_ci j, 0); 15778c2ecf20Sopenharmony_ci new_fm->e[j] = NULL; 15788c2ecf20Sopenharmony_ci } 15798c2ecf20Sopenharmony_ci goto err; 15808c2ecf20Sopenharmony_ci } 15818c2ecf20Sopenharmony_ci new_fm->e[i] = old_fm->e[i]; 15828c2ecf20Sopenharmony_ci old_fm->e[i] = NULL; 15838c2ecf20Sopenharmony_ci } else { 15848c2ecf20Sopenharmony_ci ubi_err(ubi, "could not get any free erase block"); 15858c2ecf20Sopenharmony_ci 15868c2ecf20Sopenharmony_ci for (j = 1; j < i; j++) { 15878c2ecf20Sopenharmony_ci ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0); 15888c2ecf20Sopenharmony_ci new_fm->e[j] = NULL; 15898c2ecf20Sopenharmony_ci } 15908c2ecf20Sopenharmony_ci 15918c2ecf20Sopenharmony_ci ret = -ENOSPC; 15928c2ecf20Sopenharmony_ci goto err; 15938c2ecf20Sopenharmony_ci } 15948c2ecf20Sopenharmony_ci } else { 15958c2ecf20Sopenharmony_ci new_fm->e[i] = tmp_e; 15968c2ecf20Sopenharmony_ci 15978c2ecf20Sopenharmony_ci if (old_fm && old_fm->e[i]) { 15988c2ecf20Sopenharmony_ci ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, 15998c2ecf20Sopenharmony_ci old_fm->to_be_tortured[i]); 16008c2ecf20Sopenharmony_ci old_fm->e[i] = NULL; 16018c2ecf20Sopenharmony_ci } 16028c2ecf20Sopenharmony_ci } 16038c2ecf20Sopenharmony_ci } 16048c2ecf20Sopenharmony_ci 16058c2ecf20Sopenharmony_ci /* Old fastmap is larger than the new one */ 16068c2ecf20Sopenharmony_ci if (old_fm && new_fm->used_blocks < old_fm->used_blocks) { 16078c2ecf20Sopenharmony_ci for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) { 16088c2ecf20Sopenharmony_ci ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, 16098c2ecf20Sopenharmony_ci old_fm->to_be_tortured[i]); 16108c2ecf20Sopenharmony_ci old_fm->e[i] = NULL; 16118c2ecf20Sopenharmony_ci } 16128c2ecf20Sopenharmony_ci } 16138c2ecf20Sopenharmony_ci 16148c2ecf20Sopenharmony_ci spin_lock(&ubi->wl_lock); 16158c2ecf20Sopenharmony_ci tmp_e = ubi->fm_anchor; 16168c2ecf20Sopenharmony_ci ubi->fm_anchor = NULL; 16178c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 16188c2ecf20Sopenharmony_ci 16198c2ecf20Sopenharmony_ci if (old_fm) { 16208c2ecf20Sopenharmony_ci /* no fresh anchor PEB was found, reuse the old one */ 16218c2ecf20Sopenharmony_ci if (!tmp_e) { 16228c2ecf20Sopenharmony_ci ret = erase_block(ubi, old_fm->e[0]->pnum); 16238c2ecf20Sopenharmony_ci if (ret < 0) { 16248c2ecf20Sopenharmony_ci ubi_err(ubi, "could not erase old anchor PEB"); 16258c2ecf20Sopenharmony_ci 16268c2ecf20Sopenharmony_ci for (i = 1; i < new_fm->used_blocks; i++) { 16278c2ecf20Sopenharmony_ci ubi_wl_put_fm_peb(ubi, new_fm->e[i], 16288c2ecf20Sopenharmony_ci i, 0); 16298c2ecf20Sopenharmony_ci new_fm->e[i] = NULL; 16308c2ecf20Sopenharmony_ci } 16318c2ecf20Sopenharmony_ci goto err; 16328c2ecf20Sopenharmony_ci } 16338c2ecf20Sopenharmony_ci new_fm->e[0] = old_fm->e[0]; 16348c2ecf20Sopenharmony_ci new_fm->e[0]->ec = ret; 16358c2ecf20Sopenharmony_ci old_fm->e[0] = NULL; 16368c2ecf20Sopenharmony_ci } else { 16378c2ecf20Sopenharmony_ci /* we've got a new anchor PEB, return the old one */ 16388c2ecf20Sopenharmony_ci ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0, 16398c2ecf20Sopenharmony_ci old_fm->to_be_tortured[0]); 16408c2ecf20Sopenharmony_ci new_fm->e[0] = tmp_e; 16418c2ecf20Sopenharmony_ci old_fm->e[0] = NULL; 16428c2ecf20Sopenharmony_ci } 16438c2ecf20Sopenharmony_ci } else { 16448c2ecf20Sopenharmony_ci if (!tmp_e) { 16458c2ecf20Sopenharmony_ci ubi_err(ubi, "could not find any anchor PEB"); 16468c2ecf20Sopenharmony_ci 16478c2ecf20Sopenharmony_ci for (i = 1; i < new_fm->used_blocks; i++) { 16488c2ecf20Sopenharmony_ci ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0); 16498c2ecf20Sopenharmony_ci new_fm->e[i] = NULL; 16508c2ecf20Sopenharmony_ci } 16518c2ecf20Sopenharmony_ci 16528c2ecf20Sopenharmony_ci ret = -ENOSPC; 16538c2ecf20Sopenharmony_ci goto err; 16548c2ecf20Sopenharmony_ci } 16558c2ecf20Sopenharmony_ci new_fm->e[0] = tmp_e; 16568c2ecf20Sopenharmony_ci } 16578c2ecf20Sopenharmony_ci 16588c2ecf20Sopenharmony_ci ret = ubi_write_fastmap(ubi, new_fm); 16598c2ecf20Sopenharmony_ci 16608c2ecf20Sopenharmony_ci if (ret) 16618c2ecf20Sopenharmony_ci goto err; 16628c2ecf20Sopenharmony_ci 16638c2ecf20Sopenharmony_ciout_unlock: 16648c2ecf20Sopenharmony_ci up_write(&ubi->fm_eba_sem); 16658c2ecf20Sopenharmony_ci up_write(&ubi->work_sem); 16668c2ecf20Sopenharmony_ci up_write(&ubi->fm_protect); 16678c2ecf20Sopenharmony_ci kfree(old_fm); 16688c2ecf20Sopenharmony_ci 16698c2ecf20Sopenharmony_ci ubi_ensure_anchor_pebs(ubi); 16708c2ecf20Sopenharmony_ci 16718c2ecf20Sopenharmony_ci return ret; 16728c2ecf20Sopenharmony_ci 16738c2ecf20Sopenharmony_cierr: 16748c2ecf20Sopenharmony_ci ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret); 16758c2ecf20Sopenharmony_ci 16768c2ecf20Sopenharmony_ci ret = invalidate_fastmap(ubi); 16778c2ecf20Sopenharmony_ci if (ret < 0) { 16788c2ecf20Sopenharmony_ci ubi_err(ubi, "Unable to invalidate current fastmap!"); 16798c2ecf20Sopenharmony_ci ubi_ro_mode(ubi); 16808c2ecf20Sopenharmony_ci } else { 16818c2ecf20Sopenharmony_ci return_fm_pebs(ubi, old_fm); 16828c2ecf20Sopenharmony_ci return_fm_pebs(ubi, new_fm); 16838c2ecf20Sopenharmony_ci ret = 0; 16848c2ecf20Sopenharmony_ci } 16858c2ecf20Sopenharmony_ci 16868c2ecf20Sopenharmony_ci kfree(new_fm); 16878c2ecf20Sopenharmony_ci goto out_unlock; 16888c2ecf20Sopenharmony_ci} 1689