18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) International Business Machines Corp., 2006 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Author: Artem Bityutskiy (Битюцкий Артём) 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci/* 98c2ecf20Sopenharmony_ci * The UBI Eraseblock Association (EBA) sub-system. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * This sub-system is responsible for I/O to/from logical eraseblock. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * Although in this implementation the EBA table is fully kept and managed in 148c2ecf20Sopenharmony_ci * RAM, which assumes poor scalability, it might be (partially) maintained on 158c2ecf20Sopenharmony_ci * flash in future implementations. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * The EBA sub-system implements per-logical eraseblock locking. Before 188c2ecf20Sopenharmony_ci * accessing a logical eraseblock it is locked for reading or writing. The 198c2ecf20Sopenharmony_ci * per-logical eraseblock locking is implemented by means of the lock tree. The 208c2ecf20Sopenharmony_ci * lock tree is an RB-tree which refers all the currently locked logical 218c2ecf20Sopenharmony_ci * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects. 228c2ecf20Sopenharmony_ci * They are indexed by (@vol_id, @lnum) pairs. 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * EBA also maintains the global sequence counter which is incremented each 258c2ecf20Sopenharmony_ci * time a logical eraseblock is mapped to a physical eraseblock and it is 268c2ecf20Sopenharmony_ci * stored in the volume identifier header. This means that each VID header has 278c2ecf20Sopenharmony_ci * a unique sequence number. The sequence number is only increased an we assume 288c2ecf20Sopenharmony_ci * 64 bits is enough to never overflow. 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#include <linux/slab.h> 328c2ecf20Sopenharmony_ci#include <linux/crc32.h> 338c2ecf20Sopenharmony_ci#include <linux/err.h> 348c2ecf20Sopenharmony_ci#include "ubi.h" 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci/* Number of physical eraseblocks reserved for atomic LEB change operation */ 378c2ecf20Sopenharmony_ci#define EBA_RESERVED_PEBS 1 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/** 408c2ecf20Sopenharmony_ci * struct ubi_eba_entry - structure encoding a single LEB -> PEB association 418c2ecf20Sopenharmony_ci * @pnum: the physical eraseblock number attached to the LEB 428c2ecf20Sopenharmony_ci * 438c2ecf20Sopenharmony_ci * This structure is encoding a LEB -> PEB association. Note that the LEB 448c2ecf20Sopenharmony_ci * number is not stored here, because it is the index used to access the 458c2ecf20Sopenharmony_ci * entries table. 468c2ecf20Sopenharmony_ci */ 478c2ecf20Sopenharmony_cistruct ubi_eba_entry { 488c2ecf20Sopenharmony_ci int pnum; 498c2ecf20Sopenharmony_ci}; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci/** 528c2ecf20Sopenharmony_ci * struct ubi_eba_table - LEB -> PEB association information 538c2ecf20Sopenharmony_ci * @entries: the LEB to PEB mapping (one entry per LEB). 548c2ecf20Sopenharmony_ci * 558c2ecf20Sopenharmony_ci * This structure is private to the EBA logic and should be kept here. 568c2ecf20Sopenharmony_ci * It is encoding the LEB to PEB association table, and is subject to 578c2ecf20Sopenharmony_ci * changes. 588c2ecf20Sopenharmony_ci */ 598c2ecf20Sopenharmony_cistruct ubi_eba_table { 608c2ecf20Sopenharmony_ci struct ubi_eba_entry *entries; 618c2ecf20Sopenharmony_ci}; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci/** 648c2ecf20Sopenharmony_ci * next_sqnum - get next sequence number. 658c2ecf20Sopenharmony_ci * @ubi: UBI device description object 668c2ecf20Sopenharmony_ci * 678c2ecf20Sopenharmony_ci * This function returns next sequence number to use, which is just the current 688c2ecf20Sopenharmony_ci * global sequence counter value. It also increases the global sequence 698c2ecf20Sopenharmony_ci * counter. 708c2ecf20Sopenharmony_ci */ 718c2ecf20Sopenharmony_ciunsigned long long ubi_next_sqnum(struct ubi_device *ubi) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci unsigned long long sqnum; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci spin_lock(&ubi->ltree_lock); 768c2ecf20Sopenharmony_ci sqnum = ubi->global_sqnum++; 778c2ecf20Sopenharmony_ci spin_unlock(&ubi->ltree_lock); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci return sqnum; 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci/** 838c2ecf20Sopenharmony_ci * ubi_get_compat - get compatibility flags of a volume. 848c2ecf20Sopenharmony_ci * @ubi: UBI device description object 858c2ecf20Sopenharmony_ci * @vol_id: volume ID 868c2ecf20Sopenharmony_ci * 878c2ecf20Sopenharmony_ci * This function returns compatibility flags for an internal volume. User 888c2ecf20Sopenharmony_ci * volumes have no compatibility flags, so %0 is returned. 898c2ecf20Sopenharmony_ci */ 908c2ecf20Sopenharmony_cistatic int ubi_get_compat(const struct ubi_device *ubi, int vol_id) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci if (vol_id == UBI_LAYOUT_VOLUME_ID) 938c2ecf20Sopenharmony_ci return UBI_LAYOUT_VOLUME_COMPAT; 948c2ecf20Sopenharmony_ci return 0; 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/** 988c2ecf20Sopenharmony_ci * ubi_eba_get_ldesc - get information about a LEB 998c2ecf20Sopenharmony_ci * @vol: volume description object 1008c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 1018c2ecf20Sopenharmony_ci * @ldesc: the LEB descriptor to fill 1028c2ecf20Sopenharmony_ci * 1038c2ecf20Sopenharmony_ci * Used to query information about a specific LEB. 1048c2ecf20Sopenharmony_ci * It is currently only returning the physical position of the LEB, but will be 1058c2ecf20Sopenharmony_ci * extended to provide more information. 1068c2ecf20Sopenharmony_ci */ 1078c2ecf20Sopenharmony_civoid ubi_eba_get_ldesc(struct ubi_volume *vol, int lnum, 1088c2ecf20Sopenharmony_ci struct ubi_eba_leb_desc *ldesc) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci ldesc->lnum = lnum; 1118c2ecf20Sopenharmony_ci ldesc->pnum = vol->eba_tbl->entries[lnum].pnum; 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci/** 1158c2ecf20Sopenharmony_ci * ubi_eba_create_table - allocate a new EBA table and initialize it with all 1168c2ecf20Sopenharmony_ci * LEBs unmapped 1178c2ecf20Sopenharmony_ci * @vol: volume containing the EBA table to copy 1188c2ecf20Sopenharmony_ci * @nentries: number of entries in the table 1198c2ecf20Sopenharmony_ci * 1208c2ecf20Sopenharmony_ci * Allocate a new EBA table and initialize it with all LEBs unmapped. 1218c2ecf20Sopenharmony_ci * Returns a valid pointer if it succeed, an ERR_PTR() otherwise. 1228c2ecf20Sopenharmony_ci */ 1238c2ecf20Sopenharmony_cistruct ubi_eba_table *ubi_eba_create_table(struct ubi_volume *vol, 1248c2ecf20Sopenharmony_ci int nentries) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci struct ubi_eba_table *tbl; 1278c2ecf20Sopenharmony_ci int err = -ENOMEM; 1288c2ecf20Sopenharmony_ci int i; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci tbl = kzalloc(sizeof(*tbl), GFP_KERNEL); 1318c2ecf20Sopenharmony_ci if (!tbl) 1328c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci tbl->entries = kmalloc_array(nentries, sizeof(*tbl->entries), 1358c2ecf20Sopenharmony_ci GFP_KERNEL); 1368c2ecf20Sopenharmony_ci if (!tbl->entries) 1378c2ecf20Sopenharmony_ci goto err; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci for (i = 0; i < nentries; i++) 1408c2ecf20Sopenharmony_ci tbl->entries[i].pnum = UBI_LEB_UNMAPPED; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci return tbl; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cierr: 1458c2ecf20Sopenharmony_ci kfree(tbl->entries); 1468c2ecf20Sopenharmony_ci kfree(tbl); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return ERR_PTR(err); 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci/** 1528c2ecf20Sopenharmony_ci * ubi_eba_destroy_table - destroy an EBA table 1538c2ecf20Sopenharmony_ci * @tbl: the table to destroy 1548c2ecf20Sopenharmony_ci * 1558c2ecf20Sopenharmony_ci * Destroy an EBA table. 1568c2ecf20Sopenharmony_ci */ 1578c2ecf20Sopenharmony_civoid ubi_eba_destroy_table(struct ubi_eba_table *tbl) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci if (!tbl) 1608c2ecf20Sopenharmony_ci return; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci kfree(tbl->entries); 1638c2ecf20Sopenharmony_ci kfree(tbl); 1648c2ecf20Sopenharmony_ci} 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci/** 1678c2ecf20Sopenharmony_ci * ubi_eba_copy_table - copy the EBA table attached to vol into another table 1688c2ecf20Sopenharmony_ci * @vol: volume containing the EBA table to copy 1698c2ecf20Sopenharmony_ci * @dst: destination 1708c2ecf20Sopenharmony_ci * @nentries: number of entries to copy 1718c2ecf20Sopenharmony_ci * 1728c2ecf20Sopenharmony_ci * Copy the EBA table stored in vol into the one pointed by dst. 1738c2ecf20Sopenharmony_ci */ 1748c2ecf20Sopenharmony_civoid ubi_eba_copy_table(struct ubi_volume *vol, struct ubi_eba_table *dst, 1758c2ecf20Sopenharmony_ci int nentries) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci struct ubi_eba_table *src; 1788c2ecf20Sopenharmony_ci int i; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci ubi_assert(dst && vol && vol->eba_tbl); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci src = vol->eba_tbl; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci for (i = 0; i < nentries; i++) 1858c2ecf20Sopenharmony_ci dst->entries[i].pnum = src->entries[i].pnum; 1868c2ecf20Sopenharmony_ci} 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci/** 1898c2ecf20Sopenharmony_ci * ubi_eba_replace_table - assign a new EBA table to a volume 1908c2ecf20Sopenharmony_ci * @vol: volume containing the EBA table to copy 1918c2ecf20Sopenharmony_ci * @tbl: new EBA table 1928c2ecf20Sopenharmony_ci * 1938c2ecf20Sopenharmony_ci * Assign a new EBA table to the volume and release the old one. 1948c2ecf20Sopenharmony_ci */ 1958c2ecf20Sopenharmony_civoid ubi_eba_replace_table(struct ubi_volume *vol, struct ubi_eba_table *tbl) 1968c2ecf20Sopenharmony_ci{ 1978c2ecf20Sopenharmony_ci ubi_eba_destroy_table(vol->eba_tbl); 1988c2ecf20Sopenharmony_ci vol->eba_tbl = tbl; 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci/** 2028c2ecf20Sopenharmony_ci * ltree_lookup - look up the lock tree. 2038c2ecf20Sopenharmony_ci * @ubi: UBI device description object 2048c2ecf20Sopenharmony_ci * @vol_id: volume ID 2058c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 2068c2ecf20Sopenharmony_ci * 2078c2ecf20Sopenharmony_ci * This function returns a pointer to the corresponding &struct ubi_ltree_entry 2088c2ecf20Sopenharmony_ci * object if the logical eraseblock is locked and %NULL if it is not. 2098c2ecf20Sopenharmony_ci * @ubi->ltree_lock has to be locked. 2108c2ecf20Sopenharmony_ci */ 2118c2ecf20Sopenharmony_cistatic struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id, 2128c2ecf20Sopenharmony_ci int lnum) 2138c2ecf20Sopenharmony_ci{ 2148c2ecf20Sopenharmony_ci struct rb_node *p; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci p = ubi->ltree.rb_node; 2178c2ecf20Sopenharmony_ci while (p) { 2188c2ecf20Sopenharmony_ci struct ubi_ltree_entry *le; 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci le = rb_entry(p, struct ubi_ltree_entry, rb); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci if (vol_id < le->vol_id) 2238c2ecf20Sopenharmony_ci p = p->rb_left; 2248c2ecf20Sopenharmony_ci else if (vol_id > le->vol_id) 2258c2ecf20Sopenharmony_ci p = p->rb_right; 2268c2ecf20Sopenharmony_ci else { 2278c2ecf20Sopenharmony_ci if (lnum < le->lnum) 2288c2ecf20Sopenharmony_ci p = p->rb_left; 2298c2ecf20Sopenharmony_ci else if (lnum > le->lnum) 2308c2ecf20Sopenharmony_ci p = p->rb_right; 2318c2ecf20Sopenharmony_ci else 2328c2ecf20Sopenharmony_ci return le; 2338c2ecf20Sopenharmony_ci } 2348c2ecf20Sopenharmony_ci } 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci return NULL; 2378c2ecf20Sopenharmony_ci} 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci/** 2408c2ecf20Sopenharmony_ci * ltree_add_entry - add new entry to the lock tree. 2418c2ecf20Sopenharmony_ci * @ubi: UBI device description object 2428c2ecf20Sopenharmony_ci * @vol_id: volume ID 2438c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 2448c2ecf20Sopenharmony_ci * 2458c2ecf20Sopenharmony_ci * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the 2468c2ecf20Sopenharmony_ci * lock tree. If such entry is already there, its usage counter is increased. 2478c2ecf20Sopenharmony_ci * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation 2488c2ecf20Sopenharmony_ci * failed. 2498c2ecf20Sopenharmony_ci */ 2508c2ecf20Sopenharmony_cistatic struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi, 2518c2ecf20Sopenharmony_ci int vol_id, int lnum) 2528c2ecf20Sopenharmony_ci{ 2538c2ecf20Sopenharmony_ci struct ubi_ltree_entry *le, *le1, *le_free; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS); 2568c2ecf20Sopenharmony_ci if (!le) 2578c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci le->users = 0; 2608c2ecf20Sopenharmony_ci init_rwsem(&le->mutex); 2618c2ecf20Sopenharmony_ci le->vol_id = vol_id; 2628c2ecf20Sopenharmony_ci le->lnum = lnum; 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci spin_lock(&ubi->ltree_lock); 2658c2ecf20Sopenharmony_ci le1 = ltree_lookup(ubi, vol_id, lnum); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci if (le1) { 2688c2ecf20Sopenharmony_ci /* 2698c2ecf20Sopenharmony_ci * This logical eraseblock is already locked. The newly 2708c2ecf20Sopenharmony_ci * allocated lock entry is not needed. 2718c2ecf20Sopenharmony_ci */ 2728c2ecf20Sopenharmony_ci le_free = le; 2738c2ecf20Sopenharmony_ci le = le1; 2748c2ecf20Sopenharmony_ci } else { 2758c2ecf20Sopenharmony_ci struct rb_node **p, *parent = NULL; 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci /* 2788c2ecf20Sopenharmony_ci * No lock entry, add the newly allocated one to the 2798c2ecf20Sopenharmony_ci * @ubi->ltree RB-tree. 2808c2ecf20Sopenharmony_ci */ 2818c2ecf20Sopenharmony_ci le_free = NULL; 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci p = &ubi->ltree.rb_node; 2848c2ecf20Sopenharmony_ci while (*p) { 2858c2ecf20Sopenharmony_ci parent = *p; 2868c2ecf20Sopenharmony_ci le1 = rb_entry(parent, struct ubi_ltree_entry, rb); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci if (vol_id < le1->vol_id) 2898c2ecf20Sopenharmony_ci p = &(*p)->rb_left; 2908c2ecf20Sopenharmony_ci else if (vol_id > le1->vol_id) 2918c2ecf20Sopenharmony_ci p = &(*p)->rb_right; 2928c2ecf20Sopenharmony_ci else { 2938c2ecf20Sopenharmony_ci ubi_assert(lnum != le1->lnum); 2948c2ecf20Sopenharmony_ci if (lnum < le1->lnum) 2958c2ecf20Sopenharmony_ci p = &(*p)->rb_left; 2968c2ecf20Sopenharmony_ci else 2978c2ecf20Sopenharmony_ci p = &(*p)->rb_right; 2988c2ecf20Sopenharmony_ci } 2998c2ecf20Sopenharmony_ci } 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci rb_link_node(&le->rb, parent, p); 3028c2ecf20Sopenharmony_ci rb_insert_color(&le->rb, &ubi->ltree); 3038c2ecf20Sopenharmony_ci } 3048c2ecf20Sopenharmony_ci le->users += 1; 3058c2ecf20Sopenharmony_ci spin_unlock(&ubi->ltree_lock); 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci kfree(le_free); 3088c2ecf20Sopenharmony_ci return le; 3098c2ecf20Sopenharmony_ci} 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci/** 3128c2ecf20Sopenharmony_ci * leb_read_lock - lock logical eraseblock for reading. 3138c2ecf20Sopenharmony_ci * @ubi: UBI device description object 3148c2ecf20Sopenharmony_ci * @vol_id: volume ID 3158c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 3168c2ecf20Sopenharmony_ci * 3178c2ecf20Sopenharmony_ci * This function locks a logical eraseblock for reading. Returns zero in case 3188c2ecf20Sopenharmony_ci * of success and a negative error code in case of failure. 3198c2ecf20Sopenharmony_ci */ 3208c2ecf20Sopenharmony_cistatic int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum) 3218c2ecf20Sopenharmony_ci{ 3228c2ecf20Sopenharmony_ci struct ubi_ltree_entry *le; 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci le = ltree_add_entry(ubi, vol_id, lnum); 3258c2ecf20Sopenharmony_ci if (IS_ERR(le)) 3268c2ecf20Sopenharmony_ci return PTR_ERR(le); 3278c2ecf20Sopenharmony_ci down_read(&le->mutex); 3288c2ecf20Sopenharmony_ci return 0; 3298c2ecf20Sopenharmony_ci} 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci/** 3328c2ecf20Sopenharmony_ci * leb_read_unlock - unlock logical eraseblock. 3338c2ecf20Sopenharmony_ci * @ubi: UBI device description object 3348c2ecf20Sopenharmony_ci * @vol_id: volume ID 3358c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 3368c2ecf20Sopenharmony_ci */ 3378c2ecf20Sopenharmony_cistatic void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum) 3388c2ecf20Sopenharmony_ci{ 3398c2ecf20Sopenharmony_ci struct ubi_ltree_entry *le; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci spin_lock(&ubi->ltree_lock); 3428c2ecf20Sopenharmony_ci le = ltree_lookup(ubi, vol_id, lnum); 3438c2ecf20Sopenharmony_ci le->users -= 1; 3448c2ecf20Sopenharmony_ci ubi_assert(le->users >= 0); 3458c2ecf20Sopenharmony_ci up_read(&le->mutex); 3468c2ecf20Sopenharmony_ci if (le->users == 0) { 3478c2ecf20Sopenharmony_ci rb_erase(&le->rb, &ubi->ltree); 3488c2ecf20Sopenharmony_ci kfree(le); 3498c2ecf20Sopenharmony_ci } 3508c2ecf20Sopenharmony_ci spin_unlock(&ubi->ltree_lock); 3518c2ecf20Sopenharmony_ci} 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci/** 3548c2ecf20Sopenharmony_ci * leb_write_lock - lock logical eraseblock for writing. 3558c2ecf20Sopenharmony_ci * @ubi: UBI device description object 3568c2ecf20Sopenharmony_ci * @vol_id: volume ID 3578c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 3588c2ecf20Sopenharmony_ci * 3598c2ecf20Sopenharmony_ci * This function locks a logical eraseblock for writing. Returns zero in case 3608c2ecf20Sopenharmony_ci * of success and a negative error code in case of failure. 3618c2ecf20Sopenharmony_ci */ 3628c2ecf20Sopenharmony_cistatic int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum) 3638c2ecf20Sopenharmony_ci{ 3648c2ecf20Sopenharmony_ci struct ubi_ltree_entry *le; 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci le = ltree_add_entry(ubi, vol_id, lnum); 3678c2ecf20Sopenharmony_ci if (IS_ERR(le)) 3688c2ecf20Sopenharmony_ci return PTR_ERR(le); 3698c2ecf20Sopenharmony_ci down_write(&le->mutex); 3708c2ecf20Sopenharmony_ci return 0; 3718c2ecf20Sopenharmony_ci} 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci/** 3748c2ecf20Sopenharmony_ci * leb_write_trylock - try to lock logical eraseblock for writing. 3758c2ecf20Sopenharmony_ci * @ubi: UBI device description object 3768c2ecf20Sopenharmony_ci * @vol_id: volume ID 3778c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 3788c2ecf20Sopenharmony_ci * 3798c2ecf20Sopenharmony_ci * This function locks a logical eraseblock for writing if there is no 3808c2ecf20Sopenharmony_ci * contention and does nothing if there is contention. Returns %0 in case of 3818c2ecf20Sopenharmony_ci * success, %1 in case of contention, and and a negative error code in case of 3828c2ecf20Sopenharmony_ci * failure. 3838c2ecf20Sopenharmony_ci */ 3848c2ecf20Sopenharmony_cistatic int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum) 3858c2ecf20Sopenharmony_ci{ 3868c2ecf20Sopenharmony_ci struct ubi_ltree_entry *le; 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci le = ltree_add_entry(ubi, vol_id, lnum); 3898c2ecf20Sopenharmony_ci if (IS_ERR(le)) 3908c2ecf20Sopenharmony_ci return PTR_ERR(le); 3918c2ecf20Sopenharmony_ci if (down_write_trylock(&le->mutex)) 3928c2ecf20Sopenharmony_ci return 0; 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci /* Contention, cancel */ 3958c2ecf20Sopenharmony_ci spin_lock(&ubi->ltree_lock); 3968c2ecf20Sopenharmony_ci le->users -= 1; 3978c2ecf20Sopenharmony_ci ubi_assert(le->users >= 0); 3988c2ecf20Sopenharmony_ci if (le->users == 0) { 3998c2ecf20Sopenharmony_ci rb_erase(&le->rb, &ubi->ltree); 4008c2ecf20Sopenharmony_ci kfree(le); 4018c2ecf20Sopenharmony_ci } 4028c2ecf20Sopenharmony_ci spin_unlock(&ubi->ltree_lock); 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci return 1; 4058c2ecf20Sopenharmony_ci} 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci/** 4088c2ecf20Sopenharmony_ci * leb_write_unlock - unlock logical eraseblock. 4098c2ecf20Sopenharmony_ci * @ubi: UBI device description object 4108c2ecf20Sopenharmony_ci * @vol_id: volume ID 4118c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 4128c2ecf20Sopenharmony_ci */ 4138c2ecf20Sopenharmony_cistatic void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum) 4148c2ecf20Sopenharmony_ci{ 4158c2ecf20Sopenharmony_ci struct ubi_ltree_entry *le; 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci spin_lock(&ubi->ltree_lock); 4188c2ecf20Sopenharmony_ci le = ltree_lookup(ubi, vol_id, lnum); 4198c2ecf20Sopenharmony_ci le->users -= 1; 4208c2ecf20Sopenharmony_ci ubi_assert(le->users >= 0); 4218c2ecf20Sopenharmony_ci up_write(&le->mutex); 4228c2ecf20Sopenharmony_ci if (le->users == 0) { 4238c2ecf20Sopenharmony_ci rb_erase(&le->rb, &ubi->ltree); 4248c2ecf20Sopenharmony_ci kfree(le); 4258c2ecf20Sopenharmony_ci } 4268c2ecf20Sopenharmony_ci spin_unlock(&ubi->ltree_lock); 4278c2ecf20Sopenharmony_ci} 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci/** 4308c2ecf20Sopenharmony_ci * ubi_eba_is_mapped - check if a LEB is mapped. 4318c2ecf20Sopenharmony_ci * @vol: volume description object 4328c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 4338c2ecf20Sopenharmony_ci * 4348c2ecf20Sopenharmony_ci * This function returns true if the LEB is mapped, false otherwise. 4358c2ecf20Sopenharmony_ci */ 4368c2ecf20Sopenharmony_cibool ubi_eba_is_mapped(struct ubi_volume *vol, int lnum) 4378c2ecf20Sopenharmony_ci{ 4388c2ecf20Sopenharmony_ci return vol->eba_tbl->entries[lnum].pnum >= 0; 4398c2ecf20Sopenharmony_ci} 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci/** 4428c2ecf20Sopenharmony_ci * ubi_eba_unmap_leb - un-map logical eraseblock. 4438c2ecf20Sopenharmony_ci * @ubi: UBI device description object 4448c2ecf20Sopenharmony_ci * @vol: volume description object 4458c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 4468c2ecf20Sopenharmony_ci * 4478c2ecf20Sopenharmony_ci * This function un-maps logical eraseblock @lnum and schedules corresponding 4488c2ecf20Sopenharmony_ci * physical eraseblock for erasure. Returns zero in case of success and a 4498c2ecf20Sopenharmony_ci * negative error code in case of failure. 4508c2ecf20Sopenharmony_ci */ 4518c2ecf20Sopenharmony_ciint ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol, 4528c2ecf20Sopenharmony_ci int lnum) 4538c2ecf20Sopenharmony_ci{ 4548c2ecf20Sopenharmony_ci int err, pnum, vol_id = vol->vol_id; 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci if (ubi->ro_mode) 4578c2ecf20Sopenharmony_ci return -EROFS; 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci err = leb_write_lock(ubi, vol_id, lnum); 4608c2ecf20Sopenharmony_ci if (err) 4618c2ecf20Sopenharmony_ci return err; 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci pnum = vol->eba_tbl->entries[lnum].pnum; 4648c2ecf20Sopenharmony_ci if (pnum < 0) 4658c2ecf20Sopenharmony_ci /* This logical eraseblock is already unmapped */ 4668c2ecf20Sopenharmony_ci goto out_unlock; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum); 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ci down_read(&ubi->fm_eba_sem); 4718c2ecf20Sopenharmony_ci vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED; 4728c2ecf20Sopenharmony_ci up_read(&ubi->fm_eba_sem); 4738c2ecf20Sopenharmony_ci err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0); 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ciout_unlock: 4768c2ecf20Sopenharmony_ci leb_write_unlock(ubi, vol_id, lnum); 4778c2ecf20Sopenharmony_ci return err; 4788c2ecf20Sopenharmony_ci} 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci#ifdef CONFIG_MTD_UBI_FASTMAP 4818c2ecf20Sopenharmony_ci/** 4828c2ecf20Sopenharmony_ci * check_mapping - check and fixup a mapping 4838c2ecf20Sopenharmony_ci * @ubi: UBI device description object 4848c2ecf20Sopenharmony_ci * @vol: volume description object 4858c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 4868c2ecf20Sopenharmony_ci * @pnum: physical eraseblock number 4878c2ecf20Sopenharmony_ci * 4888c2ecf20Sopenharmony_ci * Checks whether a given mapping is valid. Fastmap cannot track LEB unmap 4898c2ecf20Sopenharmony_ci * operations, if such an operation is interrupted the mapping still looks 4908c2ecf20Sopenharmony_ci * good, but upon first read an ECC is reported to the upper layer. 4918c2ecf20Sopenharmony_ci * Normaly during the full-scan at attach time this is fixed, for Fastmap 4928c2ecf20Sopenharmony_ci * we have to deal with it while reading. 4938c2ecf20Sopenharmony_ci * If the PEB behind a LEB shows this symthom we change the mapping to 4948c2ecf20Sopenharmony_ci * %UBI_LEB_UNMAPPED and schedule the PEB for erasure. 4958c2ecf20Sopenharmony_ci * 4968c2ecf20Sopenharmony_ci * Returns 0 on success, negative error code in case of failure. 4978c2ecf20Sopenharmony_ci */ 4988c2ecf20Sopenharmony_cistatic int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, 4998c2ecf20Sopenharmony_ci int *pnum) 5008c2ecf20Sopenharmony_ci{ 5018c2ecf20Sopenharmony_ci int err; 5028c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vidb; 5038c2ecf20Sopenharmony_ci struct ubi_vid_hdr *vid_hdr; 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci if (!ubi->fast_attach) 5068c2ecf20Sopenharmony_ci return 0; 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci if (!vol->checkmap || test_bit(lnum, vol->checkmap)) 5098c2ecf20Sopenharmony_ci return 0; 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_ci vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 5128c2ecf20Sopenharmony_ci if (!vidb) 5138c2ecf20Sopenharmony_ci return -ENOMEM; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci err = ubi_io_read_vid_hdr(ubi, *pnum, vidb, 0); 5168c2ecf20Sopenharmony_ci if (err > 0 && err != UBI_IO_BITFLIPS) { 5178c2ecf20Sopenharmony_ci int torture = 0; 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci switch (err) { 5208c2ecf20Sopenharmony_ci case UBI_IO_FF: 5218c2ecf20Sopenharmony_ci case UBI_IO_FF_BITFLIPS: 5228c2ecf20Sopenharmony_ci case UBI_IO_BAD_HDR: 5238c2ecf20Sopenharmony_ci case UBI_IO_BAD_HDR_EBADMSG: 5248c2ecf20Sopenharmony_ci break; 5258c2ecf20Sopenharmony_ci default: 5268c2ecf20Sopenharmony_ci ubi_assert(0); 5278c2ecf20Sopenharmony_ci } 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci if (err == UBI_IO_BAD_HDR_EBADMSG || err == UBI_IO_FF_BITFLIPS) 5308c2ecf20Sopenharmony_ci torture = 1; 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci down_read(&ubi->fm_eba_sem); 5338c2ecf20Sopenharmony_ci vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED; 5348c2ecf20Sopenharmony_ci up_read(&ubi->fm_eba_sem); 5358c2ecf20Sopenharmony_ci ubi_wl_put_peb(ubi, vol->vol_id, lnum, *pnum, torture); 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci *pnum = UBI_LEB_UNMAPPED; 5388c2ecf20Sopenharmony_ci } else if (err < 0) { 5398c2ecf20Sopenharmony_ci ubi_err(ubi, "unable to read VID header back from PEB %i: %i", 5408c2ecf20Sopenharmony_ci *pnum, err); 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci goto out_free; 5438c2ecf20Sopenharmony_ci } else { 5448c2ecf20Sopenharmony_ci int found_vol_id, found_lnum; 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci ubi_assert(err == 0 || err == UBI_IO_BITFLIPS); 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci vid_hdr = ubi_get_vid_hdr(vidb); 5498c2ecf20Sopenharmony_ci found_vol_id = be32_to_cpu(vid_hdr->vol_id); 5508c2ecf20Sopenharmony_ci found_lnum = be32_to_cpu(vid_hdr->lnum); 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci if (found_lnum != lnum || found_vol_id != vol->vol_id) { 5538c2ecf20Sopenharmony_ci ubi_err(ubi, "EBA mismatch! PEB %i is LEB %i:%i instead of LEB %i:%i", 5548c2ecf20Sopenharmony_ci *pnum, found_vol_id, found_lnum, vol->vol_id, lnum); 5558c2ecf20Sopenharmony_ci ubi_ro_mode(ubi); 5568c2ecf20Sopenharmony_ci err = -EINVAL; 5578c2ecf20Sopenharmony_ci goto out_free; 5588c2ecf20Sopenharmony_ci } 5598c2ecf20Sopenharmony_ci } 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci set_bit(lnum, vol->checkmap); 5628c2ecf20Sopenharmony_ci err = 0; 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ciout_free: 5658c2ecf20Sopenharmony_ci ubi_free_vid_buf(vidb); 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci return err; 5688c2ecf20Sopenharmony_ci} 5698c2ecf20Sopenharmony_ci#else 5708c2ecf20Sopenharmony_cistatic int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, 5718c2ecf20Sopenharmony_ci int *pnum) 5728c2ecf20Sopenharmony_ci{ 5738c2ecf20Sopenharmony_ci return 0; 5748c2ecf20Sopenharmony_ci} 5758c2ecf20Sopenharmony_ci#endif 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci/** 5788c2ecf20Sopenharmony_ci * ubi_eba_read_leb - read data. 5798c2ecf20Sopenharmony_ci * @ubi: UBI device description object 5808c2ecf20Sopenharmony_ci * @vol: volume description object 5818c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 5828c2ecf20Sopenharmony_ci * @buf: buffer to store the read data 5838c2ecf20Sopenharmony_ci * @offset: offset from where to read 5848c2ecf20Sopenharmony_ci * @len: how many bytes to read 5858c2ecf20Sopenharmony_ci * @check: data CRC check flag 5868c2ecf20Sopenharmony_ci * 5878c2ecf20Sopenharmony_ci * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF 5888c2ecf20Sopenharmony_ci * bytes. The @check flag only makes sense for static volumes and forces 5898c2ecf20Sopenharmony_ci * eraseblock data CRC checking. 5908c2ecf20Sopenharmony_ci * 5918c2ecf20Sopenharmony_ci * In case of success this function returns zero. In case of a static volume, 5928c2ecf20Sopenharmony_ci * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be 5938c2ecf20Sopenharmony_ci * returned for any volume type if an ECC error was detected by the MTD device 5948c2ecf20Sopenharmony_ci * driver. Other negative error cored may be returned in case of other errors. 5958c2ecf20Sopenharmony_ci */ 5968c2ecf20Sopenharmony_ciint ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, 5978c2ecf20Sopenharmony_ci void *buf, int offset, int len, int check) 5988c2ecf20Sopenharmony_ci{ 5998c2ecf20Sopenharmony_ci int err, pnum, scrub = 0, vol_id = vol->vol_id; 6008c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vidb; 6018c2ecf20Sopenharmony_ci struct ubi_vid_hdr *vid_hdr; 6028c2ecf20Sopenharmony_ci uint32_t crc; 6038c2ecf20Sopenharmony_ci 6048c2ecf20Sopenharmony_ci err = leb_read_lock(ubi, vol_id, lnum); 6058c2ecf20Sopenharmony_ci if (err) 6068c2ecf20Sopenharmony_ci return err; 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_ci pnum = vol->eba_tbl->entries[lnum].pnum; 6098c2ecf20Sopenharmony_ci if (pnum >= 0) { 6108c2ecf20Sopenharmony_ci err = check_mapping(ubi, vol, lnum, &pnum); 6118c2ecf20Sopenharmony_ci if (err < 0) 6128c2ecf20Sopenharmony_ci goto out_unlock; 6138c2ecf20Sopenharmony_ci } 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci if (pnum == UBI_LEB_UNMAPPED) { 6168c2ecf20Sopenharmony_ci /* 6178c2ecf20Sopenharmony_ci * The logical eraseblock is not mapped, fill the whole buffer 6188c2ecf20Sopenharmony_ci * with 0xFF bytes. The exception is static volumes for which 6198c2ecf20Sopenharmony_ci * it is an error to read unmapped logical eraseblocks. 6208c2ecf20Sopenharmony_ci */ 6218c2ecf20Sopenharmony_ci dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)", 6228c2ecf20Sopenharmony_ci len, offset, vol_id, lnum); 6238c2ecf20Sopenharmony_ci leb_read_unlock(ubi, vol_id, lnum); 6248c2ecf20Sopenharmony_ci ubi_assert(vol->vol_type != UBI_STATIC_VOLUME); 6258c2ecf20Sopenharmony_ci memset(buf, 0xFF, len); 6268c2ecf20Sopenharmony_ci return 0; 6278c2ecf20Sopenharmony_ci } 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d", 6308c2ecf20Sopenharmony_ci len, offset, vol_id, lnum, pnum); 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ci if (vol->vol_type == UBI_DYNAMIC_VOLUME) 6338c2ecf20Sopenharmony_ci check = 0; 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ciretry: 6368c2ecf20Sopenharmony_ci if (check) { 6378c2ecf20Sopenharmony_ci vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 6388c2ecf20Sopenharmony_ci if (!vidb) { 6398c2ecf20Sopenharmony_ci err = -ENOMEM; 6408c2ecf20Sopenharmony_ci goto out_unlock; 6418c2ecf20Sopenharmony_ci } 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci vid_hdr = ubi_get_vid_hdr(vidb); 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_ci err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1); 6468c2ecf20Sopenharmony_ci if (err && err != UBI_IO_BITFLIPS) { 6478c2ecf20Sopenharmony_ci if (err > 0) { 6488c2ecf20Sopenharmony_ci /* 6498c2ecf20Sopenharmony_ci * The header is either absent or corrupted. 6508c2ecf20Sopenharmony_ci * The former case means there is a bug - 6518c2ecf20Sopenharmony_ci * switch to read-only mode just in case. 6528c2ecf20Sopenharmony_ci * The latter case means a real corruption - we 6538c2ecf20Sopenharmony_ci * may try to recover data. FIXME: but this is 6548c2ecf20Sopenharmony_ci * not implemented. 6558c2ecf20Sopenharmony_ci */ 6568c2ecf20Sopenharmony_ci if (err == UBI_IO_BAD_HDR_EBADMSG || 6578c2ecf20Sopenharmony_ci err == UBI_IO_BAD_HDR) { 6588c2ecf20Sopenharmony_ci ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d", 6598c2ecf20Sopenharmony_ci pnum, vol_id, lnum); 6608c2ecf20Sopenharmony_ci err = -EBADMSG; 6618c2ecf20Sopenharmony_ci } else { 6628c2ecf20Sopenharmony_ci /* 6638c2ecf20Sopenharmony_ci * Ending up here in the non-Fastmap case 6648c2ecf20Sopenharmony_ci * is a clear bug as the VID header had to 6658c2ecf20Sopenharmony_ci * be present at scan time to have it referenced. 6668c2ecf20Sopenharmony_ci * With fastmap the story is more complicated. 6678c2ecf20Sopenharmony_ci * Fastmap has the mapping info without the need 6688c2ecf20Sopenharmony_ci * of a full scan. So the LEB could have been 6698c2ecf20Sopenharmony_ci * unmapped, Fastmap cannot know this and keeps 6708c2ecf20Sopenharmony_ci * the LEB referenced. 6718c2ecf20Sopenharmony_ci * This is valid and works as the layer above UBI 6728c2ecf20Sopenharmony_ci * has to do bookkeeping about used/referenced 6738c2ecf20Sopenharmony_ci * LEBs in any case. 6748c2ecf20Sopenharmony_ci */ 6758c2ecf20Sopenharmony_ci if (ubi->fast_attach) { 6768c2ecf20Sopenharmony_ci err = -EBADMSG; 6778c2ecf20Sopenharmony_ci } else { 6788c2ecf20Sopenharmony_ci err = -EINVAL; 6798c2ecf20Sopenharmony_ci ubi_ro_mode(ubi); 6808c2ecf20Sopenharmony_ci } 6818c2ecf20Sopenharmony_ci } 6828c2ecf20Sopenharmony_ci } 6838c2ecf20Sopenharmony_ci goto out_free; 6848c2ecf20Sopenharmony_ci } else if (err == UBI_IO_BITFLIPS) 6858c2ecf20Sopenharmony_ci scrub = 1; 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_ci ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs)); 6888c2ecf20Sopenharmony_ci ubi_assert(len == be32_to_cpu(vid_hdr->data_size)); 6898c2ecf20Sopenharmony_ci 6908c2ecf20Sopenharmony_ci crc = be32_to_cpu(vid_hdr->data_crc); 6918c2ecf20Sopenharmony_ci ubi_free_vid_buf(vidb); 6928c2ecf20Sopenharmony_ci } 6938c2ecf20Sopenharmony_ci 6948c2ecf20Sopenharmony_ci err = ubi_io_read_data(ubi, buf, pnum, offset, len); 6958c2ecf20Sopenharmony_ci if (err) { 6968c2ecf20Sopenharmony_ci if (err == UBI_IO_BITFLIPS) 6978c2ecf20Sopenharmony_ci scrub = 1; 6988c2ecf20Sopenharmony_ci else if (mtd_is_eccerr(err)) { 6998c2ecf20Sopenharmony_ci if (vol->vol_type == UBI_DYNAMIC_VOLUME) 7008c2ecf20Sopenharmony_ci goto out_unlock; 7018c2ecf20Sopenharmony_ci scrub = 1; 7028c2ecf20Sopenharmony_ci if (!check) { 7038c2ecf20Sopenharmony_ci ubi_msg(ubi, "force data checking"); 7048c2ecf20Sopenharmony_ci check = 1; 7058c2ecf20Sopenharmony_ci goto retry; 7068c2ecf20Sopenharmony_ci } 7078c2ecf20Sopenharmony_ci } else 7088c2ecf20Sopenharmony_ci goto out_unlock; 7098c2ecf20Sopenharmony_ci } 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci if (check) { 7128c2ecf20Sopenharmony_ci uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len); 7138c2ecf20Sopenharmony_ci if (crc1 != crc) { 7148c2ecf20Sopenharmony_ci ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x", 7158c2ecf20Sopenharmony_ci crc1, crc); 7168c2ecf20Sopenharmony_ci err = -EBADMSG; 7178c2ecf20Sopenharmony_ci goto out_unlock; 7188c2ecf20Sopenharmony_ci } 7198c2ecf20Sopenharmony_ci } 7208c2ecf20Sopenharmony_ci 7218c2ecf20Sopenharmony_ci if (scrub) 7228c2ecf20Sopenharmony_ci err = ubi_wl_scrub_peb(ubi, pnum); 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci leb_read_unlock(ubi, vol_id, lnum); 7258c2ecf20Sopenharmony_ci return err; 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ciout_free: 7288c2ecf20Sopenharmony_ci ubi_free_vid_buf(vidb); 7298c2ecf20Sopenharmony_ciout_unlock: 7308c2ecf20Sopenharmony_ci leb_read_unlock(ubi, vol_id, lnum); 7318c2ecf20Sopenharmony_ci return err; 7328c2ecf20Sopenharmony_ci} 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_ci/** 7358c2ecf20Sopenharmony_ci * ubi_eba_read_leb_sg - read data into a scatter gather list. 7368c2ecf20Sopenharmony_ci * @ubi: UBI device description object 7378c2ecf20Sopenharmony_ci * @vol: volume description object 7388c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 7398c2ecf20Sopenharmony_ci * @sgl: UBI scatter gather list to store the read data 7408c2ecf20Sopenharmony_ci * @offset: offset from where to read 7418c2ecf20Sopenharmony_ci * @len: how many bytes to read 7428c2ecf20Sopenharmony_ci * @check: data CRC check flag 7438c2ecf20Sopenharmony_ci * 7448c2ecf20Sopenharmony_ci * This function works exactly like ubi_eba_read_leb(). But instead of 7458c2ecf20Sopenharmony_ci * storing the read data into a buffer it writes to an UBI scatter gather 7468c2ecf20Sopenharmony_ci * list. 7478c2ecf20Sopenharmony_ci */ 7488c2ecf20Sopenharmony_ciint ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol, 7498c2ecf20Sopenharmony_ci struct ubi_sgl *sgl, int lnum, int offset, int len, 7508c2ecf20Sopenharmony_ci int check) 7518c2ecf20Sopenharmony_ci{ 7528c2ecf20Sopenharmony_ci int to_read; 7538c2ecf20Sopenharmony_ci int ret; 7548c2ecf20Sopenharmony_ci struct scatterlist *sg; 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_ci for (;;) { 7578c2ecf20Sopenharmony_ci ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT); 7588c2ecf20Sopenharmony_ci sg = &sgl->sg[sgl->list_pos]; 7598c2ecf20Sopenharmony_ci if (len < sg->length - sgl->page_pos) 7608c2ecf20Sopenharmony_ci to_read = len; 7618c2ecf20Sopenharmony_ci else 7628c2ecf20Sopenharmony_ci to_read = sg->length - sgl->page_pos; 7638c2ecf20Sopenharmony_ci 7648c2ecf20Sopenharmony_ci ret = ubi_eba_read_leb(ubi, vol, lnum, 7658c2ecf20Sopenharmony_ci sg_virt(sg) + sgl->page_pos, offset, 7668c2ecf20Sopenharmony_ci to_read, check); 7678c2ecf20Sopenharmony_ci if (ret < 0) 7688c2ecf20Sopenharmony_ci return ret; 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci offset += to_read; 7718c2ecf20Sopenharmony_ci len -= to_read; 7728c2ecf20Sopenharmony_ci if (!len) { 7738c2ecf20Sopenharmony_ci sgl->page_pos += to_read; 7748c2ecf20Sopenharmony_ci if (sgl->page_pos == sg->length) { 7758c2ecf20Sopenharmony_ci sgl->list_pos++; 7768c2ecf20Sopenharmony_ci sgl->page_pos = 0; 7778c2ecf20Sopenharmony_ci } 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_ci break; 7808c2ecf20Sopenharmony_ci } 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci sgl->list_pos++; 7838c2ecf20Sopenharmony_ci sgl->page_pos = 0; 7848c2ecf20Sopenharmony_ci } 7858c2ecf20Sopenharmony_ci 7868c2ecf20Sopenharmony_ci return ret; 7878c2ecf20Sopenharmony_ci} 7888c2ecf20Sopenharmony_ci 7898c2ecf20Sopenharmony_ci/** 7908c2ecf20Sopenharmony_ci * try_recover_peb - try to recover from write failure. 7918c2ecf20Sopenharmony_ci * @vol: volume description object 7928c2ecf20Sopenharmony_ci * @pnum: the physical eraseblock to recover 7938c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 7948c2ecf20Sopenharmony_ci * @buf: data which was not written because of the write failure 7958c2ecf20Sopenharmony_ci * @offset: offset of the failed write 7968c2ecf20Sopenharmony_ci * @len: how many bytes should have been written 7978c2ecf20Sopenharmony_ci * @vidb: VID buffer 7988c2ecf20Sopenharmony_ci * @retry: whether the caller should retry in case of failure 7998c2ecf20Sopenharmony_ci * 8008c2ecf20Sopenharmony_ci * This function is called in case of a write failure and moves all good data 8018c2ecf20Sopenharmony_ci * from the potentially bad physical eraseblock to a good physical eraseblock. 8028c2ecf20Sopenharmony_ci * This function also writes the data which was not written due to the failure. 8038c2ecf20Sopenharmony_ci * Returns 0 in case of success, and a negative error code in case of failure. 8048c2ecf20Sopenharmony_ci * In case of failure, the %retry parameter is set to false if this is a fatal 8058c2ecf20Sopenharmony_ci * error (retrying won't help), and true otherwise. 8068c2ecf20Sopenharmony_ci */ 8078c2ecf20Sopenharmony_cistatic int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum, 8088c2ecf20Sopenharmony_ci const void *buf, int offset, int len, 8098c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vidb, bool *retry) 8108c2ecf20Sopenharmony_ci{ 8118c2ecf20Sopenharmony_ci struct ubi_device *ubi = vol->ubi; 8128c2ecf20Sopenharmony_ci struct ubi_vid_hdr *vid_hdr; 8138c2ecf20Sopenharmony_ci int new_pnum, err, vol_id = vol->vol_id, data_size; 8148c2ecf20Sopenharmony_ci uint32_t crc; 8158c2ecf20Sopenharmony_ci 8168c2ecf20Sopenharmony_ci *retry = false; 8178c2ecf20Sopenharmony_ci 8188c2ecf20Sopenharmony_ci new_pnum = ubi_wl_get_peb(ubi); 8198c2ecf20Sopenharmony_ci if (new_pnum < 0) { 8208c2ecf20Sopenharmony_ci err = new_pnum; 8218c2ecf20Sopenharmony_ci goto out_put; 8228c2ecf20Sopenharmony_ci } 8238c2ecf20Sopenharmony_ci 8248c2ecf20Sopenharmony_ci ubi_msg(ubi, "recover PEB %d, move data to PEB %d", 8258c2ecf20Sopenharmony_ci pnum, new_pnum); 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1); 8288c2ecf20Sopenharmony_ci if (err && err != UBI_IO_BITFLIPS) { 8298c2ecf20Sopenharmony_ci if (err > 0) 8308c2ecf20Sopenharmony_ci err = -EIO; 8318c2ecf20Sopenharmony_ci goto out_put; 8328c2ecf20Sopenharmony_ci } 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_ci vid_hdr = ubi_get_vid_hdr(vidb); 8358c2ecf20Sopenharmony_ci ubi_assert(vid_hdr->vol_type == UBI_VID_DYNAMIC); 8368c2ecf20Sopenharmony_ci 8378c2ecf20Sopenharmony_ci mutex_lock(&ubi->buf_mutex); 8388c2ecf20Sopenharmony_ci memset(ubi->peb_buf + offset, 0xFF, len); 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ci /* Read everything before the area where the write failure happened */ 8418c2ecf20Sopenharmony_ci if (offset > 0) { 8428c2ecf20Sopenharmony_ci err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset); 8438c2ecf20Sopenharmony_ci if (err && err != UBI_IO_BITFLIPS) 8448c2ecf20Sopenharmony_ci goto out_unlock; 8458c2ecf20Sopenharmony_ci } 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_ci *retry = true; 8488c2ecf20Sopenharmony_ci 8498c2ecf20Sopenharmony_ci memcpy(ubi->peb_buf + offset, buf, len); 8508c2ecf20Sopenharmony_ci 8518c2ecf20Sopenharmony_ci data_size = offset + len; 8528c2ecf20Sopenharmony_ci crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size); 8538c2ecf20Sopenharmony_ci vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 8548c2ecf20Sopenharmony_ci vid_hdr->copy_flag = 1; 8558c2ecf20Sopenharmony_ci vid_hdr->data_size = cpu_to_be32(data_size); 8568c2ecf20Sopenharmony_ci vid_hdr->data_crc = cpu_to_be32(crc); 8578c2ecf20Sopenharmony_ci err = ubi_io_write_vid_hdr(ubi, new_pnum, vidb); 8588c2ecf20Sopenharmony_ci if (err) 8598c2ecf20Sopenharmony_ci goto out_unlock; 8608c2ecf20Sopenharmony_ci 8618c2ecf20Sopenharmony_ci err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size); 8628c2ecf20Sopenharmony_ci 8638c2ecf20Sopenharmony_ciout_unlock: 8648c2ecf20Sopenharmony_ci mutex_unlock(&ubi->buf_mutex); 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci if (!err) 8678c2ecf20Sopenharmony_ci vol->eba_tbl->entries[lnum].pnum = new_pnum; 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_ciout_put: 8708c2ecf20Sopenharmony_ci up_read(&ubi->fm_eba_sem); 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_ci if (!err) { 8738c2ecf20Sopenharmony_ci ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); 8748c2ecf20Sopenharmony_ci ubi_msg(ubi, "data was successfully recovered"); 8758c2ecf20Sopenharmony_ci } else if (new_pnum >= 0) { 8768c2ecf20Sopenharmony_ci /* 8778c2ecf20Sopenharmony_ci * Bad luck? This physical eraseblock is bad too? Crud. Let's 8788c2ecf20Sopenharmony_ci * try to get another one. 8798c2ecf20Sopenharmony_ci */ 8808c2ecf20Sopenharmony_ci ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1); 8818c2ecf20Sopenharmony_ci ubi_warn(ubi, "failed to write to PEB %d", new_pnum); 8828c2ecf20Sopenharmony_ci } 8838c2ecf20Sopenharmony_ci 8848c2ecf20Sopenharmony_ci return err; 8858c2ecf20Sopenharmony_ci} 8868c2ecf20Sopenharmony_ci 8878c2ecf20Sopenharmony_ci/** 8888c2ecf20Sopenharmony_ci * recover_peb - recover from write failure. 8898c2ecf20Sopenharmony_ci * @ubi: UBI device description object 8908c2ecf20Sopenharmony_ci * @pnum: the physical eraseblock to recover 8918c2ecf20Sopenharmony_ci * @vol_id: volume ID 8928c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 8938c2ecf20Sopenharmony_ci * @buf: data which was not written because of the write failure 8948c2ecf20Sopenharmony_ci * @offset: offset of the failed write 8958c2ecf20Sopenharmony_ci * @len: how many bytes should have been written 8968c2ecf20Sopenharmony_ci * 8978c2ecf20Sopenharmony_ci * This function is called in case of a write failure and moves all good data 8988c2ecf20Sopenharmony_ci * from the potentially bad physical eraseblock to a good physical eraseblock. 8998c2ecf20Sopenharmony_ci * This function also writes the data which was not written due to the failure. 9008c2ecf20Sopenharmony_ci * Returns 0 in case of success, and a negative error code in case of failure. 9018c2ecf20Sopenharmony_ci * This function tries %UBI_IO_RETRIES before giving up. 9028c2ecf20Sopenharmony_ci */ 9038c2ecf20Sopenharmony_cistatic int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum, 9048c2ecf20Sopenharmony_ci const void *buf, int offset, int len) 9058c2ecf20Sopenharmony_ci{ 9068c2ecf20Sopenharmony_ci int err, idx = vol_id2idx(ubi, vol_id), tries; 9078c2ecf20Sopenharmony_ci struct ubi_volume *vol = ubi->volumes[idx]; 9088c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vidb; 9098c2ecf20Sopenharmony_ci 9108c2ecf20Sopenharmony_ci vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 9118c2ecf20Sopenharmony_ci if (!vidb) 9128c2ecf20Sopenharmony_ci return -ENOMEM; 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 9158c2ecf20Sopenharmony_ci bool retry; 9168c2ecf20Sopenharmony_ci 9178c2ecf20Sopenharmony_ci err = try_recover_peb(vol, pnum, lnum, buf, offset, len, vidb, 9188c2ecf20Sopenharmony_ci &retry); 9198c2ecf20Sopenharmony_ci if (!err || !retry) 9208c2ecf20Sopenharmony_ci break; 9218c2ecf20Sopenharmony_ci 9228c2ecf20Sopenharmony_ci ubi_msg(ubi, "try again"); 9238c2ecf20Sopenharmony_ci } 9248c2ecf20Sopenharmony_ci 9258c2ecf20Sopenharmony_ci ubi_free_vid_buf(vidb); 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_ci return err; 9288c2ecf20Sopenharmony_ci} 9298c2ecf20Sopenharmony_ci 9308c2ecf20Sopenharmony_ci/** 9318c2ecf20Sopenharmony_ci * try_write_vid_and_data - try to write VID header and data to a new PEB. 9328c2ecf20Sopenharmony_ci * @vol: volume description object 9338c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 9348c2ecf20Sopenharmony_ci * @vidb: the VID buffer to write 9358c2ecf20Sopenharmony_ci * @buf: buffer containing the data 9368c2ecf20Sopenharmony_ci * @offset: where to start writing data 9378c2ecf20Sopenharmony_ci * @len: how many bytes should be written 9388c2ecf20Sopenharmony_ci * 9398c2ecf20Sopenharmony_ci * This function tries to write VID header and data belonging to logical 9408c2ecf20Sopenharmony_ci * eraseblock @lnum of volume @vol to a new physical eraseblock. Returns zero 9418c2ecf20Sopenharmony_ci * in case of success and a negative error code in case of failure. 9428c2ecf20Sopenharmony_ci * In case of error, it is possible that something was still written to the 9438c2ecf20Sopenharmony_ci * flash media, but may be some garbage. 9448c2ecf20Sopenharmony_ci */ 9458c2ecf20Sopenharmony_cistatic int try_write_vid_and_data(struct ubi_volume *vol, int lnum, 9468c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vidb, const void *buf, 9478c2ecf20Sopenharmony_ci int offset, int len) 9488c2ecf20Sopenharmony_ci{ 9498c2ecf20Sopenharmony_ci struct ubi_device *ubi = vol->ubi; 9508c2ecf20Sopenharmony_ci int pnum, opnum, err, err2, vol_id = vol->vol_id; 9518c2ecf20Sopenharmony_ci 9528c2ecf20Sopenharmony_ci pnum = ubi_wl_get_peb(ubi); 9538c2ecf20Sopenharmony_ci if (pnum < 0) { 9548c2ecf20Sopenharmony_ci err = pnum; 9558c2ecf20Sopenharmony_ci goto out_put; 9568c2ecf20Sopenharmony_ci } 9578c2ecf20Sopenharmony_ci 9588c2ecf20Sopenharmony_ci opnum = vol->eba_tbl->entries[lnum].pnum; 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d", 9618c2ecf20Sopenharmony_ci len, offset, vol_id, lnum, pnum); 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci err = ubi_io_write_vid_hdr(ubi, pnum, vidb); 9648c2ecf20Sopenharmony_ci if (err) { 9658c2ecf20Sopenharmony_ci ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d", 9668c2ecf20Sopenharmony_ci vol_id, lnum, pnum); 9678c2ecf20Sopenharmony_ci goto out_put; 9688c2ecf20Sopenharmony_ci } 9698c2ecf20Sopenharmony_ci 9708c2ecf20Sopenharmony_ci if (len) { 9718c2ecf20Sopenharmony_ci err = ubi_io_write_data(ubi, buf, pnum, offset, len); 9728c2ecf20Sopenharmony_ci if (err) { 9738c2ecf20Sopenharmony_ci ubi_warn(ubi, 9748c2ecf20Sopenharmony_ci "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d", 9758c2ecf20Sopenharmony_ci len, offset, vol_id, lnum, pnum); 9768c2ecf20Sopenharmony_ci goto out_put; 9778c2ecf20Sopenharmony_ci } 9788c2ecf20Sopenharmony_ci } 9798c2ecf20Sopenharmony_ci 9808c2ecf20Sopenharmony_ci vol->eba_tbl->entries[lnum].pnum = pnum; 9818c2ecf20Sopenharmony_ci 9828c2ecf20Sopenharmony_ciout_put: 9838c2ecf20Sopenharmony_ci up_read(&ubi->fm_eba_sem); 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ci if (err && pnum >= 0) { 9868c2ecf20Sopenharmony_ci err2 = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); 9878c2ecf20Sopenharmony_ci if (err2) { 9888c2ecf20Sopenharmony_ci ubi_warn(ubi, "failed to return physical eraseblock %d, error %d", 9898c2ecf20Sopenharmony_ci pnum, err2); 9908c2ecf20Sopenharmony_ci } 9918c2ecf20Sopenharmony_ci } else if (!err && opnum >= 0) { 9928c2ecf20Sopenharmony_ci err2 = ubi_wl_put_peb(ubi, vol_id, lnum, opnum, 0); 9938c2ecf20Sopenharmony_ci if (err2) { 9948c2ecf20Sopenharmony_ci ubi_warn(ubi, "failed to return physical eraseblock %d, error %d", 9958c2ecf20Sopenharmony_ci opnum, err2); 9968c2ecf20Sopenharmony_ci } 9978c2ecf20Sopenharmony_ci } 9988c2ecf20Sopenharmony_ci 9998c2ecf20Sopenharmony_ci return err; 10008c2ecf20Sopenharmony_ci} 10018c2ecf20Sopenharmony_ci 10028c2ecf20Sopenharmony_ci/** 10038c2ecf20Sopenharmony_ci * ubi_eba_write_leb - write data to dynamic volume. 10048c2ecf20Sopenharmony_ci * @ubi: UBI device description object 10058c2ecf20Sopenharmony_ci * @vol: volume description object 10068c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 10078c2ecf20Sopenharmony_ci * @buf: the data to write 10088c2ecf20Sopenharmony_ci * @offset: offset within the logical eraseblock where to write 10098c2ecf20Sopenharmony_ci * @len: how many bytes to write 10108c2ecf20Sopenharmony_ci * 10118c2ecf20Sopenharmony_ci * This function writes data to logical eraseblock @lnum of a dynamic volume 10128c2ecf20Sopenharmony_ci * @vol. Returns zero in case of success and a negative error code in case 10138c2ecf20Sopenharmony_ci * of failure. In case of error, it is possible that something was still 10148c2ecf20Sopenharmony_ci * written to the flash media, but may be some garbage. 10158c2ecf20Sopenharmony_ci * This function retries %UBI_IO_RETRIES times before giving up. 10168c2ecf20Sopenharmony_ci */ 10178c2ecf20Sopenharmony_ciint ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, 10188c2ecf20Sopenharmony_ci const void *buf, int offset, int len) 10198c2ecf20Sopenharmony_ci{ 10208c2ecf20Sopenharmony_ci int err, pnum, tries, vol_id = vol->vol_id; 10218c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vidb; 10228c2ecf20Sopenharmony_ci struct ubi_vid_hdr *vid_hdr; 10238c2ecf20Sopenharmony_ci 10248c2ecf20Sopenharmony_ci if (ubi->ro_mode) 10258c2ecf20Sopenharmony_ci return -EROFS; 10268c2ecf20Sopenharmony_ci 10278c2ecf20Sopenharmony_ci err = leb_write_lock(ubi, vol_id, lnum); 10288c2ecf20Sopenharmony_ci if (err) 10298c2ecf20Sopenharmony_ci return err; 10308c2ecf20Sopenharmony_ci 10318c2ecf20Sopenharmony_ci pnum = vol->eba_tbl->entries[lnum].pnum; 10328c2ecf20Sopenharmony_ci if (pnum >= 0) { 10338c2ecf20Sopenharmony_ci err = check_mapping(ubi, vol, lnum, &pnum); 10348c2ecf20Sopenharmony_ci if (err < 0) 10358c2ecf20Sopenharmony_ci goto out; 10368c2ecf20Sopenharmony_ci } 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci if (pnum >= 0) { 10398c2ecf20Sopenharmony_ci dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d", 10408c2ecf20Sopenharmony_ci len, offset, vol_id, lnum, pnum); 10418c2ecf20Sopenharmony_ci 10428c2ecf20Sopenharmony_ci err = ubi_io_write_data(ubi, buf, pnum, offset, len); 10438c2ecf20Sopenharmony_ci if (err) { 10448c2ecf20Sopenharmony_ci ubi_warn(ubi, "failed to write data to PEB %d", pnum); 10458c2ecf20Sopenharmony_ci if (err == -EIO && ubi->bad_allowed) 10468c2ecf20Sopenharmony_ci err = recover_peb(ubi, pnum, vol_id, lnum, buf, 10478c2ecf20Sopenharmony_ci offset, len); 10488c2ecf20Sopenharmony_ci } 10498c2ecf20Sopenharmony_ci 10508c2ecf20Sopenharmony_ci goto out; 10518c2ecf20Sopenharmony_ci } 10528c2ecf20Sopenharmony_ci 10538c2ecf20Sopenharmony_ci /* 10548c2ecf20Sopenharmony_ci * The logical eraseblock is not mapped. We have to get a free physical 10558c2ecf20Sopenharmony_ci * eraseblock and write the volume identifier header there first. 10568c2ecf20Sopenharmony_ci */ 10578c2ecf20Sopenharmony_ci vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 10588c2ecf20Sopenharmony_ci if (!vidb) { 10598c2ecf20Sopenharmony_ci leb_write_unlock(ubi, vol_id, lnum); 10608c2ecf20Sopenharmony_ci return -ENOMEM; 10618c2ecf20Sopenharmony_ci } 10628c2ecf20Sopenharmony_ci 10638c2ecf20Sopenharmony_ci vid_hdr = ubi_get_vid_hdr(vidb); 10648c2ecf20Sopenharmony_ci 10658c2ecf20Sopenharmony_ci vid_hdr->vol_type = UBI_VID_DYNAMIC; 10668c2ecf20Sopenharmony_ci vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 10678c2ecf20Sopenharmony_ci vid_hdr->vol_id = cpu_to_be32(vol_id); 10688c2ecf20Sopenharmony_ci vid_hdr->lnum = cpu_to_be32(lnum); 10698c2ecf20Sopenharmony_ci vid_hdr->compat = ubi_get_compat(ubi, vol_id); 10708c2ecf20Sopenharmony_ci vid_hdr->data_pad = cpu_to_be32(vol->data_pad); 10718c2ecf20Sopenharmony_ci 10728c2ecf20Sopenharmony_ci for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 10738c2ecf20Sopenharmony_ci err = try_write_vid_and_data(vol, lnum, vidb, buf, offset, len); 10748c2ecf20Sopenharmony_ci if (err != -EIO || !ubi->bad_allowed) 10758c2ecf20Sopenharmony_ci break; 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_ci /* 10788c2ecf20Sopenharmony_ci * Fortunately, this is the first write operation to this 10798c2ecf20Sopenharmony_ci * physical eraseblock, so just put it and request a new one. 10808c2ecf20Sopenharmony_ci * We assume that if this physical eraseblock went bad, the 10818c2ecf20Sopenharmony_ci * erase code will handle that. 10828c2ecf20Sopenharmony_ci */ 10838c2ecf20Sopenharmony_ci vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 10848c2ecf20Sopenharmony_ci ubi_msg(ubi, "try another PEB"); 10858c2ecf20Sopenharmony_ci } 10868c2ecf20Sopenharmony_ci 10878c2ecf20Sopenharmony_ci ubi_free_vid_buf(vidb); 10888c2ecf20Sopenharmony_ci 10898c2ecf20Sopenharmony_ciout: 10908c2ecf20Sopenharmony_ci if (err) 10918c2ecf20Sopenharmony_ci ubi_ro_mode(ubi); 10928c2ecf20Sopenharmony_ci 10938c2ecf20Sopenharmony_ci leb_write_unlock(ubi, vol_id, lnum); 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_ci return err; 10968c2ecf20Sopenharmony_ci} 10978c2ecf20Sopenharmony_ci 10988c2ecf20Sopenharmony_ci/** 10998c2ecf20Sopenharmony_ci * ubi_eba_write_leb_st - write data to static volume. 11008c2ecf20Sopenharmony_ci * @ubi: UBI device description object 11018c2ecf20Sopenharmony_ci * @vol: volume description object 11028c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 11038c2ecf20Sopenharmony_ci * @buf: data to write 11048c2ecf20Sopenharmony_ci * @len: how many bytes to write 11058c2ecf20Sopenharmony_ci * @used_ebs: how many logical eraseblocks will this volume contain 11068c2ecf20Sopenharmony_ci * 11078c2ecf20Sopenharmony_ci * This function writes data to logical eraseblock @lnum of static volume 11088c2ecf20Sopenharmony_ci * @vol. The @used_ebs argument should contain total number of logical 11098c2ecf20Sopenharmony_ci * eraseblock in this static volume. 11108c2ecf20Sopenharmony_ci * 11118c2ecf20Sopenharmony_ci * When writing to the last logical eraseblock, the @len argument doesn't have 11128c2ecf20Sopenharmony_ci * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent 11138c2ecf20Sopenharmony_ci * to the real data size, although the @buf buffer has to contain the 11148c2ecf20Sopenharmony_ci * alignment. In all other cases, @len has to be aligned. 11158c2ecf20Sopenharmony_ci * 11168c2ecf20Sopenharmony_ci * It is prohibited to write more than once to logical eraseblocks of static 11178c2ecf20Sopenharmony_ci * volumes. This function returns zero in case of success and a negative error 11188c2ecf20Sopenharmony_ci * code in case of failure. 11198c2ecf20Sopenharmony_ci */ 11208c2ecf20Sopenharmony_ciint ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol, 11218c2ecf20Sopenharmony_ci int lnum, const void *buf, int len, int used_ebs) 11228c2ecf20Sopenharmony_ci{ 11238c2ecf20Sopenharmony_ci int err, tries, data_size = len, vol_id = vol->vol_id; 11248c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vidb; 11258c2ecf20Sopenharmony_ci struct ubi_vid_hdr *vid_hdr; 11268c2ecf20Sopenharmony_ci uint32_t crc; 11278c2ecf20Sopenharmony_ci 11288c2ecf20Sopenharmony_ci if (ubi->ro_mode) 11298c2ecf20Sopenharmony_ci return -EROFS; 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_ci if (lnum == used_ebs - 1) 11328c2ecf20Sopenharmony_ci /* If this is the last LEB @len may be unaligned */ 11338c2ecf20Sopenharmony_ci len = ALIGN(data_size, ubi->min_io_size); 11348c2ecf20Sopenharmony_ci else 11358c2ecf20Sopenharmony_ci ubi_assert(!(len & (ubi->min_io_size - 1))); 11368c2ecf20Sopenharmony_ci 11378c2ecf20Sopenharmony_ci vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 11388c2ecf20Sopenharmony_ci if (!vidb) 11398c2ecf20Sopenharmony_ci return -ENOMEM; 11408c2ecf20Sopenharmony_ci 11418c2ecf20Sopenharmony_ci vid_hdr = ubi_get_vid_hdr(vidb); 11428c2ecf20Sopenharmony_ci 11438c2ecf20Sopenharmony_ci err = leb_write_lock(ubi, vol_id, lnum); 11448c2ecf20Sopenharmony_ci if (err) 11458c2ecf20Sopenharmony_ci goto out; 11468c2ecf20Sopenharmony_ci 11478c2ecf20Sopenharmony_ci vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 11488c2ecf20Sopenharmony_ci vid_hdr->vol_id = cpu_to_be32(vol_id); 11498c2ecf20Sopenharmony_ci vid_hdr->lnum = cpu_to_be32(lnum); 11508c2ecf20Sopenharmony_ci vid_hdr->compat = ubi_get_compat(ubi, vol_id); 11518c2ecf20Sopenharmony_ci vid_hdr->data_pad = cpu_to_be32(vol->data_pad); 11528c2ecf20Sopenharmony_ci 11538c2ecf20Sopenharmony_ci crc = crc32(UBI_CRC32_INIT, buf, data_size); 11548c2ecf20Sopenharmony_ci vid_hdr->vol_type = UBI_VID_STATIC; 11558c2ecf20Sopenharmony_ci vid_hdr->data_size = cpu_to_be32(data_size); 11568c2ecf20Sopenharmony_ci vid_hdr->used_ebs = cpu_to_be32(used_ebs); 11578c2ecf20Sopenharmony_ci vid_hdr->data_crc = cpu_to_be32(crc); 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_ci ubi_assert(vol->eba_tbl->entries[lnum].pnum < 0); 11608c2ecf20Sopenharmony_ci 11618c2ecf20Sopenharmony_ci for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 11628c2ecf20Sopenharmony_ci err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len); 11638c2ecf20Sopenharmony_ci if (err != -EIO || !ubi->bad_allowed) 11648c2ecf20Sopenharmony_ci break; 11658c2ecf20Sopenharmony_ci 11668c2ecf20Sopenharmony_ci vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 11678c2ecf20Sopenharmony_ci ubi_msg(ubi, "try another PEB"); 11688c2ecf20Sopenharmony_ci } 11698c2ecf20Sopenharmony_ci 11708c2ecf20Sopenharmony_ci if (err) 11718c2ecf20Sopenharmony_ci ubi_ro_mode(ubi); 11728c2ecf20Sopenharmony_ci 11738c2ecf20Sopenharmony_ci leb_write_unlock(ubi, vol_id, lnum); 11748c2ecf20Sopenharmony_ci 11758c2ecf20Sopenharmony_ciout: 11768c2ecf20Sopenharmony_ci ubi_free_vid_buf(vidb); 11778c2ecf20Sopenharmony_ci 11788c2ecf20Sopenharmony_ci return err; 11798c2ecf20Sopenharmony_ci} 11808c2ecf20Sopenharmony_ci 11818c2ecf20Sopenharmony_ci/* 11828c2ecf20Sopenharmony_ci * ubi_eba_atomic_leb_change - change logical eraseblock atomically. 11838c2ecf20Sopenharmony_ci * @ubi: UBI device description object 11848c2ecf20Sopenharmony_ci * @vol: volume description object 11858c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number 11868c2ecf20Sopenharmony_ci * @buf: data to write 11878c2ecf20Sopenharmony_ci * @len: how many bytes to write 11888c2ecf20Sopenharmony_ci * 11898c2ecf20Sopenharmony_ci * This function changes the contents of a logical eraseblock atomically. @buf 11908c2ecf20Sopenharmony_ci * has to contain new logical eraseblock data, and @len - the length of the 11918c2ecf20Sopenharmony_ci * data, which has to be aligned. This function guarantees that in case of an 11928c2ecf20Sopenharmony_ci * unclean reboot the old contents is preserved. Returns zero in case of 11938c2ecf20Sopenharmony_ci * success and a negative error code in case of failure. 11948c2ecf20Sopenharmony_ci * 11958c2ecf20Sopenharmony_ci * UBI reserves one LEB for the "atomic LEB change" operation, so only one 11968c2ecf20Sopenharmony_ci * LEB change may be done at a time. This is ensured by @ubi->alc_mutex. 11978c2ecf20Sopenharmony_ci */ 11988c2ecf20Sopenharmony_ciint ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol, 11998c2ecf20Sopenharmony_ci int lnum, const void *buf, int len) 12008c2ecf20Sopenharmony_ci{ 12018c2ecf20Sopenharmony_ci int err, tries, vol_id = vol->vol_id; 12028c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vidb; 12038c2ecf20Sopenharmony_ci struct ubi_vid_hdr *vid_hdr; 12048c2ecf20Sopenharmony_ci uint32_t crc; 12058c2ecf20Sopenharmony_ci 12068c2ecf20Sopenharmony_ci if (ubi->ro_mode) 12078c2ecf20Sopenharmony_ci return -EROFS; 12088c2ecf20Sopenharmony_ci 12098c2ecf20Sopenharmony_ci if (len == 0) { 12108c2ecf20Sopenharmony_ci /* 12118c2ecf20Sopenharmony_ci * Special case when data length is zero. In this case the LEB 12128c2ecf20Sopenharmony_ci * has to be unmapped and mapped somewhere else. 12138c2ecf20Sopenharmony_ci */ 12148c2ecf20Sopenharmony_ci err = ubi_eba_unmap_leb(ubi, vol, lnum); 12158c2ecf20Sopenharmony_ci if (err) 12168c2ecf20Sopenharmony_ci return err; 12178c2ecf20Sopenharmony_ci return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0); 12188c2ecf20Sopenharmony_ci } 12198c2ecf20Sopenharmony_ci 12208c2ecf20Sopenharmony_ci vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 12218c2ecf20Sopenharmony_ci if (!vidb) 12228c2ecf20Sopenharmony_ci return -ENOMEM; 12238c2ecf20Sopenharmony_ci 12248c2ecf20Sopenharmony_ci vid_hdr = ubi_get_vid_hdr(vidb); 12258c2ecf20Sopenharmony_ci 12268c2ecf20Sopenharmony_ci mutex_lock(&ubi->alc_mutex); 12278c2ecf20Sopenharmony_ci err = leb_write_lock(ubi, vol_id, lnum); 12288c2ecf20Sopenharmony_ci if (err) 12298c2ecf20Sopenharmony_ci goto out_mutex; 12308c2ecf20Sopenharmony_ci 12318c2ecf20Sopenharmony_ci vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 12328c2ecf20Sopenharmony_ci vid_hdr->vol_id = cpu_to_be32(vol_id); 12338c2ecf20Sopenharmony_ci vid_hdr->lnum = cpu_to_be32(lnum); 12348c2ecf20Sopenharmony_ci vid_hdr->compat = ubi_get_compat(ubi, vol_id); 12358c2ecf20Sopenharmony_ci vid_hdr->data_pad = cpu_to_be32(vol->data_pad); 12368c2ecf20Sopenharmony_ci 12378c2ecf20Sopenharmony_ci crc = crc32(UBI_CRC32_INIT, buf, len); 12388c2ecf20Sopenharmony_ci vid_hdr->vol_type = UBI_VID_DYNAMIC; 12398c2ecf20Sopenharmony_ci vid_hdr->data_size = cpu_to_be32(len); 12408c2ecf20Sopenharmony_ci vid_hdr->copy_flag = 1; 12418c2ecf20Sopenharmony_ci vid_hdr->data_crc = cpu_to_be32(crc); 12428c2ecf20Sopenharmony_ci 12438c2ecf20Sopenharmony_ci dbg_eba("change LEB %d:%d", vol_id, lnum); 12448c2ecf20Sopenharmony_ci 12458c2ecf20Sopenharmony_ci for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 12468c2ecf20Sopenharmony_ci err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len); 12478c2ecf20Sopenharmony_ci if (err != -EIO || !ubi->bad_allowed) 12488c2ecf20Sopenharmony_ci break; 12498c2ecf20Sopenharmony_ci 12508c2ecf20Sopenharmony_ci vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 12518c2ecf20Sopenharmony_ci ubi_msg(ubi, "try another PEB"); 12528c2ecf20Sopenharmony_ci } 12538c2ecf20Sopenharmony_ci 12548c2ecf20Sopenharmony_ci /* 12558c2ecf20Sopenharmony_ci * This flash device does not admit of bad eraseblocks or 12568c2ecf20Sopenharmony_ci * something nasty and unexpected happened. Switch to read-only 12578c2ecf20Sopenharmony_ci * mode just in case. 12588c2ecf20Sopenharmony_ci */ 12598c2ecf20Sopenharmony_ci if (err) 12608c2ecf20Sopenharmony_ci ubi_ro_mode(ubi); 12618c2ecf20Sopenharmony_ci 12628c2ecf20Sopenharmony_ci leb_write_unlock(ubi, vol_id, lnum); 12638c2ecf20Sopenharmony_ci 12648c2ecf20Sopenharmony_ciout_mutex: 12658c2ecf20Sopenharmony_ci mutex_unlock(&ubi->alc_mutex); 12668c2ecf20Sopenharmony_ci ubi_free_vid_buf(vidb); 12678c2ecf20Sopenharmony_ci return err; 12688c2ecf20Sopenharmony_ci} 12698c2ecf20Sopenharmony_ci 12708c2ecf20Sopenharmony_ci/** 12718c2ecf20Sopenharmony_ci * is_error_sane - check whether a read error is sane. 12728c2ecf20Sopenharmony_ci * @err: code of the error happened during reading 12738c2ecf20Sopenharmony_ci * 12748c2ecf20Sopenharmony_ci * This is a helper function for 'ubi_eba_copy_leb()' which is called when we 12758c2ecf20Sopenharmony_ci * cannot read data from the target PEB (an error @err happened). If the error 12768c2ecf20Sopenharmony_ci * code is sane, then we treat this error as non-fatal. Otherwise the error is 12778c2ecf20Sopenharmony_ci * fatal and UBI will be switched to R/O mode later. 12788c2ecf20Sopenharmony_ci * 12798c2ecf20Sopenharmony_ci * The idea is that we try not to switch to R/O mode if the read error is 12808c2ecf20Sopenharmony_ci * something which suggests there was a real read problem. E.g., %-EIO. Or a 12818c2ecf20Sopenharmony_ci * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O 12828c2ecf20Sopenharmony_ci * mode, simply because we do not know what happened at the MTD level, and we 12838c2ecf20Sopenharmony_ci * cannot handle this. E.g., the underlying driver may have become crazy, and 12848c2ecf20Sopenharmony_ci * it is safer to switch to R/O mode to preserve the data. 12858c2ecf20Sopenharmony_ci * 12868c2ecf20Sopenharmony_ci * And bear in mind, this is about reading from the target PEB, i.e. the PEB 12878c2ecf20Sopenharmony_ci * which we have just written. 12888c2ecf20Sopenharmony_ci */ 12898c2ecf20Sopenharmony_cistatic int is_error_sane(int err) 12908c2ecf20Sopenharmony_ci{ 12918c2ecf20Sopenharmony_ci if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR || 12928c2ecf20Sopenharmony_ci err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT) 12938c2ecf20Sopenharmony_ci return 0; 12948c2ecf20Sopenharmony_ci return 1; 12958c2ecf20Sopenharmony_ci} 12968c2ecf20Sopenharmony_ci 12978c2ecf20Sopenharmony_ci/** 12988c2ecf20Sopenharmony_ci * ubi_eba_copy_leb - copy logical eraseblock. 12998c2ecf20Sopenharmony_ci * @ubi: UBI device description object 13008c2ecf20Sopenharmony_ci * @from: physical eraseblock number from where to copy 13018c2ecf20Sopenharmony_ci * @to: physical eraseblock number where to copy 13028c2ecf20Sopenharmony_ci * @vid_hdr: VID header of the @from physical eraseblock 13038c2ecf20Sopenharmony_ci * 13048c2ecf20Sopenharmony_ci * This function copies logical eraseblock from physical eraseblock @from to 13058c2ecf20Sopenharmony_ci * physical eraseblock @to. The @vid_hdr buffer may be changed by this 13068c2ecf20Sopenharmony_ci * function. Returns: 13078c2ecf20Sopenharmony_ci * o %0 in case of success; 13088c2ecf20Sopenharmony_ci * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc; 13098c2ecf20Sopenharmony_ci * o a negative error code in case of failure. 13108c2ecf20Sopenharmony_ci */ 13118c2ecf20Sopenharmony_ciint ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, 13128c2ecf20Sopenharmony_ci struct ubi_vid_io_buf *vidb) 13138c2ecf20Sopenharmony_ci{ 13148c2ecf20Sopenharmony_ci int err, vol_id, lnum, data_size, aldata_size, idx; 13158c2ecf20Sopenharmony_ci struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb); 13168c2ecf20Sopenharmony_ci struct ubi_volume *vol; 13178c2ecf20Sopenharmony_ci uint32_t crc; 13188c2ecf20Sopenharmony_ci 13198c2ecf20Sopenharmony_ci ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem)); 13208c2ecf20Sopenharmony_ci 13218c2ecf20Sopenharmony_ci vol_id = be32_to_cpu(vid_hdr->vol_id); 13228c2ecf20Sopenharmony_ci lnum = be32_to_cpu(vid_hdr->lnum); 13238c2ecf20Sopenharmony_ci 13248c2ecf20Sopenharmony_ci dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to); 13258c2ecf20Sopenharmony_ci 13268c2ecf20Sopenharmony_ci if (vid_hdr->vol_type == UBI_VID_STATIC) { 13278c2ecf20Sopenharmony_ci data_size = be32_to_cpu(vid_hdr->data_size); 13288c2ecf20Sopenharmony_ci aldata_size = ALIGN(data_size, ubi->min_io_size); 13298c2ecf20Sopenharmony_ci } else 13308c2ecf20Sopenharmony_ci data_size = aldata_size = 13318c2ecf20Sopenharmony_ci ubi->leb_size - be32_to_cpu(vid_hdr->data_pad); 13328c2ecf20Sopenharmony_ci 13338c2ecf20Sopenharmony_ci idx = vol_id2idx(ubi, vol_id); 13348c2ecf20Sopenharmony_ci spin_lock(&ubi->volumes_lock); 13358c2ecf20Sopenharmony_ci /* 13368c2ecf20Sopenharmony_ci * Note, we may race with volume deletion, which means that the volume 13378c2ecf20Sopenharmony_ci * this logical eraseblock belongs to might be being deleted. Since the 13388c2ecf20Sopenharmony_ci * volume deletion un-maps all the volume's logical eraseblocks, it will 13398c2ecf20Sopenharmony_ci * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish. 13408c2ecf20Sopenharmony_ci */ 13418c2ecf20Sopenharmony_ci vol = ubi->volumes[idx]; 13428c2ecf20Sopenharmony_ci spin_unlock(&ubi->volumes_lock); 13438c2ecf20Sopenharmony_ci if (!vol) { 13448c2ecf20Sopenharmony_ci /* No need to do further work, cancel */ 13458c2ecf20Sopenharmony_ci dbg_wl("volume %d is being removed, cancel", vol_id); 13468c2ecf20Sopenharmony_ci return MOVE_CANCEL_RACE; 13478c2ecf20Sopenharmony_ci } 13488c2ecf20Sopenharmony_ci 13498c2ecf20Sopenharmony_ci /* 13508c2ecf20Sopenharmony_ci * We do not want anybody to write to this logical eraseblock while we 13518c2ecf20Sopenharmony_ci * are moving it, so lock it. 13528c2ecf20Sopenharmony_ci * 13538c2ecf20Sopenharmony_ci * Note, we are using non-waiting locking here, because we cannot sleep 13548c2ecf20Sopenharmony_ci * on the LEB, since it may cause deadlocks. Indeed, imagine a task is 13558c2ecf20Sopenharmony_ci * unmapping the LEB which is mapped to the PEB we are going to move 13568c2ecf20Sopenharmony_ci * (@from). This task locks the LEB and goes sleep in the 13578c2ecf20Sopenharmony_ci * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are 13588c2ecf20Sopenharmony_ci * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the 13598c2ecf20Sopenharmony_ci * LEB is already locked, we just do not move it and return 13608c2ecf20Sopenharmony_ci * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because 13618c2ecf20Sopenharmony_ci * we do not know the reasons of the contention - it may be just a 13628c2ecf20Sopenharmony_ci * normal I/O on this LEB, so we want to re-try. 13638c2ecf20Sopenharmony_ci */ 13648c2ecf20Sopenharmony_ci err = leb_write_trylock(ubi, vol_id, lnum); 13658c2ecf20Sopenharmony_ci if (err) { 13668c2ecf20Sopenharmony_ci dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum); 13678c2ecf20Sopenharmony_ci return MOVE_RETRY; 13688c2ecf20Sopenharmony_ci } 13698c2ecf20Sopenharmony_ci 13708c2ecf20Sopenharmony_ci /* 13718c2ecf20Sopenharmony_ci * The LEB might have been put meanwhile, and the task which put it is 13728c2ecf20Sopenharmony_ci * probably waiting on @ubi->move_mutex. No need to continue the work, 13738c2ecf20Sopenharmony_ci * cancel it. 13748c2ecf20Sopenharmony_ci */ 13758c2ecf20Sopenharmony_ci if (vol->eba_tbl->entries[lnum].pnum != from) { 13768c2ecf20Sopenharmony_ci dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel", 13778c2ecf20Sopenharmony_ci vol_id, lnum, from, vol->eba_tbl->entries[lnum].pnum); 13788c2ecf20Sopenharmony_ci err = MOVE_CANCEL_RACE; 13798c2ecf20Sopenharmony_ci goto out_unlock_leb; 13808c2ecf20Sopenharmony_ci } 13818c2ecf20Sopenharmony_ci 13828c2ecf20Sopenharmony_ci /* 13838c2ecf20Sopenharmony_ci * OK, now the LEB is locked and we can safely start moving it. Since 13848c2ecf20Sopenharmony_ci * this function utilizes the @ubi->peb_buf buffer which is shared 13858c2ecf20Sopenharmony_ci * with some other functions - we lock the buffer by taking the 13868c2ecf20Sopenharmony_ci * @ubi->buf_mutex. 13878c2ecf20Sopenharmony_ci */ 13888c2ecf20Sopenharmony_ci mutex_lock(&ubi->buf_mutex); 13898c2ecf20Sopenharmony_ci dbg_wl("read %d bytes of data", aldata_size); 13908c2ecf20Sopenharmony_ci err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size); 13918c2ecf20Sopenharmony_ci if (err && err != UBI_IO_BITFLIPS) { 13928c2ecf20Sopenharmony_ci ubi_warn(ubi, "error %d while reading data from PEB %d", 13938c2ecf20Sopenharmony_ci err, from); 13948c2ecf20Sopenharmony_ci err = MOVE_SOURCE_RD_ERR; 13958c2ecf20Sopenharmony_ci goto out_unlock_buf; 13968c2ecf20Sopenharmony_ci } 13978c2ecf20Sopenharmony_ci 13988c2ecf20Sopenharmony_ci /* 13998c2ecf20Sopenharmony_ci * Now we have got to calculate how much data we have to copy. In 14008c2ecf20Sopenharmony_ci * case of a static volume it is fairly easy - the VID header contains 14018c2ecf20Sopenharmony_ci * the data size. In case of a dynamic volume it is more difficult - we 14028c2ecf20Sopenharmony_ci * have to read the contents, cut 0xFF bytes from the end and copy only 14038c2ecf20Sopenharmony_ci * the first part. We must do this to avoid writing 0xFF bytes as it 14048c2ecf20Sopenharmony_ci * may have some side-effects. And not only this. It is important not 14058c2ecf20Sopenharmony_ci * to include those 0xFFs to CRC because later the they may be filled 14068c2ecf20Sopenharmony_ci * by data. 14078c2ecf20Sopenharmony_ci */ 14088c2ecf20Sopenharmony_ci if (vid_hdr->vol_type == UBI_VID_DYNAMIC) 14098c2ecf20Sopenharmony_ci aldata_size = data_size = 14108c2ecf20Sopenharmony_ci ubi_calc_data_len(ubi, ubi->peb_buf, data_size); 14118c2ecf20Sopenharmony_ci 14128c2ecf20Sopenharmony_ci cond_resched(); 14138c2ecf20Sopenharmony_ci crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size); 14148c2ecf20Sopenharmony_ci cond_resched(); 14158c2ecf20Sopenharmony_ci 14168c2ecf20Sopenharmony_ci /* 14178c2ecf20Sopenharmony_ci * It may turn out to be that the whole @from physical eraseblock 14188c2ecf20Sopenharmony_ci * contains only 0xFF bytes. Then we have to only write the VID header 14198c2ecf20Sopenharmony_ci * and do not write any data. This also means we should not set 14208c2ecf20Sopenharmony_ci * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc. 14218c2ecf20Sopenharmony_ci */ 14228c2ecf20Sopenharmony_ci if (data_size > 0) { 14238c2ecf20Sopenharmony_ci vid_hdr->copy_flag = 1; 14248c2ecf20Sopenharmony_ci vid_hdr->data_size = cpu_to_be32(data_size); 14258c2ecf20Sopenharmony_ci vid_hdr->data_crc = cpu_to_be32(crc); 14268c2ecf20Sopenharmony_ci } 14278c2ecf20Sopenharmony_ci vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 14288c2ecf20Sopenharmony_ci 14298c2ecf20Sopenharmony_ci err = ubi_io_write_vid_hdr(ubi, to, vidb); 14308c2ecf20Sopenharmony_ci if (err) { 14318c2ecf20Sopenharmony_ci if (err == -EIO) 14328c2ecf20Sopenharmony_ci err = MOVE_TARGET_WR_ERR; 14338c2ecf20Sopenharmony_ci goto out_unlock_buf; 14348c2ecf20Sopenharmony_ci } 14358c2ecf20Sopenharmony_ci 14368c2ecf20Sopenharmony_ci cond_resched(); 14378c2ecf20Sopenharmony_ci 14388c2ecf20Sopenharmony_ci /* Read the VID header back and check if it was written correctly */ 14398c2ecf20Sopenharmony_ci err = ubi_io_read_vid_hdr(ubi, to, vidb, 1); 14408c2ecf20Sopenharmony_ci if (err) { 14418c2ecf20Sopenharmony_ci if (err != UBI_IO_BITFLIPS) { 14428c2ecf20Sopenharmony_ci ubi_warn(ubi, "error %d while reading VID header back from PEB %d", 14438c2ecf20Sopenharmony_ci err, to); 14448c2ecf20Sopenharmony_ci if (is_error_sane(err)) 14458c2ecf20Sopenharmony_ci err = MOVE_TARGET_RD_ERR; 14468c2ecf20Sopenharmony_ci } else 14478c2ecf20Sopenharmony_ci err = MOVE_TARGET_BITFLIPS; 14488c2ecf20Sopenharmony_ci goto out_unlock_buf; 14498c2ecf20Sopenharmony_ci } 14508c2ecf20Sopenharmony_ci 14518c2ecf20Sopenharmony_ci if (data_size > 0) { 14528c2ecf20Sopenharmony_ci err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size); 14538c2ecf20Sopenharmony_ci if (err) { 14548c2ecf20Sopenharmony_ci if (err == -EIO) 14558c2ecf20Sopenharmony_ci err = MOVE_TARGET_WR_ERR; 14568c2ecf20Sopenharmony_ci goto out_unlock_buf; 14578c2ecf20Sopenharmony_ci } 14588c2ecf20Sopenharmony_ci 14598c2ecf20Sopenharmony_ci cond_resched(); 14608c2ecf20Sopenharmony_ci } 14618c2ecf20Sopenharmony_ci 14628c2ecf20Sopenharmony_ci ubi_assert(vol->eba_tbl->entries[lnum].pnum == from); 14638c2ecf20Sopenharmony_ci vol->eba_tbl->entries[lnum].pnum = to; 14648c2ecf20Sopenharmony_ci 14658c2ecf20Sopenharmony_ciout_unlock_buf: 14668c2ecf20Sopenharmony_ci mutex_unlock(&ubi->buf_mutex); 14678c2ecf20Sopenharmony_ciout_unlock_leb: 14688c2ecf20Sopenharmony_ci leb_write_unlock(ubi, vol_id, lnum); 14698c2ecf20Sopenharmony_ci return err; 14708c2ecf20Sopenharmony_ci} 14718c2ecf20Sopenharmony_ci 14728c2ecf20Sopenharmony_ci/** 14738c2ecf20Sopenharmony_ci * print_rsvd_warning - warn about not having enough reserved PEBs. 14748c2ecf20Sopenharmony_ci * @ubi: UBI device description object 14758c2ecf20Sopenharmony_ci * 14768c2ecf20Sopenharmony_ci * This is a helper function for 'ubi_eba_init()' which is called when UBI 14778c2ecf20Sopenharmony_ci * cannot reserve enough PEBs for bad block handling. This function makes a 14788c2ecf20Sopenharmony_ci * decision whether we have to print a warning or not. The algorithm is as 14798c2ecf20Sopenharmony_ci * follows: 14808c2ecf20Sopenharmony_ci * o if this is a new UBI image, then just print the warning 14818c2ecf20Sopenharmony_ci * o if this is an UBI image which has already been used for some time, print 14828c2ecf20Sopenharmony_ci * a warning only if we can reserve less than 10% of the expected amount of 14838c2ecf20Sopenharmony_ci * the reserved PEB. 14848c2ecf20Sopenharmony_ci * 14858c2ecf20Sopenharmony_ci * The idea is that when UBI is used, PEBs become bad, and the reserved pool 14868c2ecf20Sopenharmony_ci * of PEBs becomes smaller, which is normal and we do not want to scare users 14878c2ecf20Sopenharmony_ci * with a warning every time they attach the MTD device. This was an issue 14888c2ecf20Sopenharmony_ci * reported by real users. 14898c2ecf20Sopenharmony_ci */ 14908c2ecf20Sopenharmony_cistatic void print_rsvd_warning(struct ubi_device *ubi, 14918c2ecf20Sopenharmony_ci struct ubi_attach_info *ai) 14928c2ecf20Sopenharmony_ci{ 14938c2ecf20Sopenharmony_ci /* 14948c2ecf20Sopenharmony_ci * The 1 << 18 (256KiB) number is picked randomly, just a reasonably 14958c2ecf20Sopenharmony_ci * large number to distinguish between newly flashed and used images. 14968c2ecf20Sopenharmony_ci */ 14978c2ecf20Sopenharmony_ci if (ai->max_sqnum > (1 << 18)) { 14988c2ecf20Sopenharmony_ci int min = ubi->beb_rsvd_level / 10; 14998c2ecf20Sopenharmony_ci 15008c2ecf20Sopenharmony_ci if (!min) 15018c2ecf20Sopenharmony_ci min = 1; 15028c2ecf20Sopenharmony_ci if (ubi->beb_rsvd_pebs > min) 15038c2ecf20Sopenharmony_ci return; 15048c2ecf20Sopenharmony_ci } 15058c2ecf20Sopenharmony_ci 15068c2ecf20Sopenharmony_ci ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d", 15078c2ecf20Sopenharmony_ci ubi->beb_rsvd_pebs, ubi->beb_rsvd_level); 15088c2ecf20Sopenharmony_ci if (ubi->corr_peb_count) 15098c2ecf20Sopenharmony_ci ubi_warn(ubi, "%d PEBs are corrupted and not used", 15108c2ecf20Sopenharmony_ci ubi->corr_peb_count); 15118c2ecf20Sopenharmony_ci} 15128c2ecf20Sopenharmony_ci 15138c2ecf20Sopenharmony_ci/** 15148c2ecf20Sopenharmony_ci * self_check_eba - run a self check on the EBA table constructed by fastmap. 15158c2ecf20Sopenharmony_ci * @ubi: UBI device description object 15168c2ecf20Sopenharmony_ci * @ai_fastmap: UBI attach info object created by fastmap 15178c2ecf20Sopenharmony_ci * @ai_scan: UBI attach info object created by scanning 15188c2ecf20Sopenharmony_ci * 15198c2ecf20Sopenharmony_ci * Returns < 0 in case of an internal error, 0 otherwise. 15208c2ecf20Sopenharmony_ci * If a bad EBA table entry was found it will be printed out and 15218c2ecf20Sopenharmony_ci * ubi_assert() triggers. 15228c2ecf20Sopenharmony_ci */ 15238c2ecf20Sopenharmony_ciint self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap, 15248c2ecf20Sopenharmony_ci struct ubi_attach_info *ai_scan) 15258c2ecf20Sopenharmony_ci{ 15268c2ecf20Sopenharmony_ci int i, j, num_volumes, ret = 0; 15278c2ecf20Sopenharmony_ci int **scan_eba, **fm_eba; 15288c2ecf20Sopenharmony_ci struct ubi_ainf_volume *av; 15298c2ecf20Sopenharmony_ci struct ubi_volume *vol; 15308c2ecf20Sopenharmony_ci struct ubi_ainf_peb *aeb; 15318c2ecf20Sopenharmony_ci struct rb_node *rb; 15328c2ecf20Sopenharmony_ci 15338c2ecf20Sopenharmony_ci num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; 15348c2ecf20Sopenharmony_ci 15358c2ecf20Sopenharmony_ci scan_eba = kmalloc_array(num_volumes, sizeof(*scan_eba), GFP_KERNEL); 15368c2ecf20Sopenharmony_ci if (!scan_eba) 15378c2ecf20Sopenharmony_ci return -ENOMEM; 15388c2ecf20Sopenharmony_ci 15398c2ecf20Sopenharmony_ci fm_eba = kmalloc_array(num_volumes, sizeof(*fm_eba), GFP_KERNEL); 15408c2ecf20Sopenharmony_ci if (!fm_eba) { 15418c2ecf20Sopenharmony_ci kfree(scan_eba); 15428c2ecf20Sopenharmony_ci return -ENOMEM; 15438c2ecf20Sopenharmony_ci } 15448c2ecf20Sopenharmony_ci 15458c2ecf20Sopenharmony_ci for (i = 0; i < num_volumes; i++) { 15468c2ecf20Sopenharmony_ci vol = ubi->volumes[i]; 15478c2ecf20Sopenharmony_ci if (!vol) 15488c2ecf20Sopenharmony_ci continue; 15498c2ecf20Sopenharmony_ci 15508c2ecf20Sopenharmony_ci scan_eba[i] = kmalloc_array(vol->reserved_pebs, 15518c2ecf20Sopenharmony_ci sizeof(**scan_eba), 15528c2ecf20Sopenharmony_ci GFP_KERNEL); 15538c2ecf20Sopenharmony_ci if (!scan_eba[i]) { 15548c2ecf20Sopenharmony_ci ret = -ENOMEM; 15558c2ecf20Sopenharmony_ci goto out_free; 15568c2ecf20Sopenharmony_ci } 15578c2ecf20Sopenharmony_ci 15588c2ecf20Sopenharmony_ci fm_eba[i] = kmalloc_array(vol->reserved_pebs, 15598c2ecf20Sopenharmony_ci sizeof(**fm_eba), 15608c2ecf20Sopenharmony_ci GFP_KERNEL); 15618c2ecf20Sopenharmony_ci if (!fm_eba[i]) { 15628c2ecf20Sopenharmony_ci ret = -ENOMEM; 15638c2ecf20Sopenharmony_ci goto out_free; 15648c2ecf20Sopenharmony_ci } 15658c2ecf20Sopenharmony_ci 15668c2ecf20Sopenharmony_ci for (j = 0; j < vol->reserved_pebs; j++) 15678c2ecf20Sopenharmony_ci scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED; 15688c2ecf20Sopenharmony_ci 15698c2ecf20Sopenharmony_ci av = ubi_find_av(ai_scan, idx2vol_id(ubi, i)); 15708c2ecf20Sopenharmony_ci if (!av) 15718c2ecf20Sopenharmony_ci continue; 15728c2ecf20Sopenharmony_ci 15738c2ecf20Sopenharmony_ci ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) 15748c2ecf20Sopenharmony_ci scan_eba[i][aeb->lnum] = aeb->pnum; 15758c2ecf20Sopenharmony_ci 15768c2ecf20Sopenharmony_ci av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i)); 15778c2ecf20Sopenharmony_ci if (!av) 15788c2ecf20Sopenharmony_ci continue; 15798c2ecf20Sopenharmony_ci 15808c2ecf20Sopenharmony_ci ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) 15818c2ecf20Sopenharmony_ci fm_eba[i][aeb->lnum] = aeb->pnum; 15828c2ecf20Sopenharmony_ci 15838c2ecf20Sopenharmony_ci for (j = 0; j < vol->reserved_pebs; j++) { 15848c2ecf20Sopenharmony_ci if (scan_eba[i][j] != fm_eba[i][j]) { 15858c2ecf20Sopenharmony_ci if (scan_eba[i][j] == UBI_LEB_UNMAPPED || 15868c2ecf20Sopenharmony_ci fm_eba[i][j] == UBI_LEB_UNMAPPED) 15878c2ecf20Sopenharmony_ci continue; 15888c2ecf20Sopenharmony_ci 15898c2ecf20Sopenharmony_ci ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!", 15908c2ecf20Sopenharmony_ci vol->vol_id, j, fm_eba[i][j], 15918c2ecf20Sopenharmony_ci scan_eba[i][j]); 15928c2ecf20Sopenharmony_ci ubi_assert(0); 15938c2ecf20Sopenharmony_ci } 15948c2ecf20Sopenharmony_ci } 15958c2ecf20Sopenharmony_ci } 15968c2ecf20Sopenharmony_ci 15978c2ecf20Sopenharmony_ciout_free: 15988c2ecf20Sopenharmony_ci for (i = 0; i < num_volumes; i++) { 15998c2ecf20Sopenharmony_ci if (!ubi->volumes[i]) 16008c2ecf20Sopenharmony_ci continue; 16018c2ecf20Sopenharmony_ci 16028c2ecf20Sopenharmony_ci kfree(scan_eba[i]); 16038c2ecf20Sopenharmony_ci kfree(fm_eba[i]); 16048c2ecf20Sopenharmony_ci } 16058c2ecf20Sopenharmony_ci 16068c2ecf20Sopenharmony_ci kfree(scan_eba); 16078c2ecf20Sopenharmony_ci kfree(fm_eba); 16088c2ecf20Sopenharmony_ci return ret; 16098c2ecf20Sopenharmony_ci} 16108c2ecf20Sopenharmony_ci 16118c2ecf20Sopenharmony_ci/** 16128c2ecf20Sopenharmony_ci * ubi_eba_init - initialize the EBA sub-system using attaching information. 16138c2ecf20Sopenharmony_ci * @ubi: UBI device description object 16148c2ecf20Sopenharmony_ci * @ai: attaching information 16158c2ecf20Sopenharmony_ci * 16168c2ecf20Sopenharmony_ci * This function returns zero in case of success and a negative error code in 16178c2ecf20Sopenharmony_ci * case of failure. 16188c2ecf20Sopenharmony_ci */ 16198c2ecf20Sopenharmony_ciint ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai) 16208c2ecf20Sopenharmony_ci{ 16218c2ecf20Sopenharmony_ci int i, err, num_volumes; 16228c2ecf20Sopenharmony_ci struct ubi_ainf_volume *av; 16238c2ecf20Sopenharmony_ci struct ubi_volume *vol; 16248c2ecf20Sopenharmony_ci struct ubi_ainf_peb *aeb; 16258c2ecf20Sopenharmony_ci struct rb_node *rb; 16268c2ecf20Sopenharmony_ci 16278c2ecf20Sopenharmony_ci dbg_eba("initialize EBA sub-system"); 16288c2ecf20Sopenharmony_ci 16298c2ecf20Sopenharmony_ci spin_lock_init(&ubi->ltree_lock); 16308c2ecf20Sopenharmony_ci mutex_init(&ubi->alc_mutex); 16318c2ecf20Sopenharmony_ci ubi->ltree = RB_ROOT; 16328c2ecf20Sopenharmony_ci 16338c2ecf20Sopenharmony_ci ubi->global_sqnum = ai->max_sqnum + 1; 16348c2ecf20Sopenharmony_ci num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; 16358c2ecf20Sopenharmony_ci 16368c2ecf20Sopenharmony_ci for (i = 0; i < num_volumes; i++) { 16378c2ecf20Sopenharmony_ci struct ubi_eba_table *tbl; 16388c2ecf20Sopenharmony_ci 16398c2ecf20Sopenharmony_ci vol = ubi->volumes[i]; 16408c2ecf20Sopenharmony_ci if (!vol) 16418c2ecf20Sopenharmony_ci continue; 16428c2ecf20Sopenharmony_ci 16438c2ecf20Sopenharmony_ci cond_resched(); 16448c2ecf20Sopenharmony_ci 16458c2ecf20Sopenharmony_ci tbl = ubi_eba_create_table(vol, vol->reserved_pebs); 16468c2ecf20Sopenharmony_ci if (IS_ERR(tbl)) { 16478c2ecf20Sopenharmony_ci err = PTR_ERR(tbl); 16488c2ecf20Sopenharmony_ci goto out_free; 16498c2ecf20Sopenharmony_ci } 16508c2ecf20Sopenharmony_ci 16518c2ecf20Sopenharmony_ci ubi_eba_replace_table(vol, tbl); 16528c2ecf20Sopenharmony_ci 16538c2ecf20Sopenharmony_ci av = ubi_find_av(ai, idx2vol_id(ubi, i)); 16548c2ecf20Sopenharmony_ci if (!av) 16558c2ecf20Sopenharmony_ci continue; 16568c2ecf20Sopenharmony_ci 16578c2ecf20Sopenharmony_ci ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) { 16588c2ecf20Sopenharmony_ci if (aeb->lnum >= vol->reserved_pebs) { 16598c2ecf20Sopenharmony_ci /* 16608c2ecf20Sopenharmony_ci * This may happen in case of an unclean reboot 16618c2ecf20Sopenharmony_ci * during re-size. 16628c2ecf20Sopenharmony_ci */ 16638c2ecf20Sopenharmony_ci ubi_move_aeb_to_list(av, aeb, &ai->erase); 16648c2ecf20Sopenharmony_ci } else { 16658c2ecf20Sopenharmony_ci struct ubi_eba_entry *entry; 16668c2ecf20Sopenharmony_ci 16678c2ecf20Sopenharmony_ci entry = &vol->eba_tbl->entries[aeb->lnum]; 16688c2ecf20Sopenharmony_ci entry->pnum = aeb->pnum; 16698c2ecf20Sopenharmony_ci } 16708c2ecf20Sopenharmony_ci } 16718c2ecf20Sopenharmony_ci } 16728c2ecf20Sopenharmony_ci 16738c2ecf20Sopenharmony_ci if (ubi->avail_pebs < EBA_RESERVED_PEBS) { 16748c2ecf20Sopenharmony_ci ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)", 16758c2ecf20Sopenharmony_ci ubi->avail_pebs, EBA_RESERVED_PEBS); 16768c2ecf20Sopenharmony_ci if (ubi->corr_peb_count) 16778c2ecf20Sopenharmony_ci ubi_err(ubi, "%d PEBs are corrupted and not used", 16788c2ecf20Sopenharmony_ci ubi->corr_peb_count); 16798c2ecf20Sopenharmony_ci err = -ENOSPC; 16808c2ecf20Sopenharmony_ci goto out_free; 16818c2ecf20Sopenharmony_ci } 16828c2ecf20Sopenharmony_ci ubi->avail_pebs -= EBA_RESERVED_PEBS; 16838c2ecf20Sopenharmony_ci ubi->rsvd_pebs += EBA_RESERVED_PEBS; 16848c2ecf20Sopenharmony_ci 16858c2ecf20Sopenharmony_ci if (ubi->bad_allowed) { 16868c2ecf20Sopenharmony_ci ubi_calculate_reserved(ubi); 16878c2ecf20Sopenharmony_ci 16888c2ecf20Sopenharmony_ci if (ubi->avail_pebs < ubi->beb_rsvd_level) { 16898c2ecf20Sopenharmony_ci /* No enough free physical eraseblocks */ 16908c2ecf20Sopenharmony_ci ubi->beb_rsvd_pebs = ubi->avail_pebs; 16918c2ecf20Sopenharmony_ci print_rsvd_warning(ubi, ai); 16928c2ecf20Sopenharmony_ci } else 16938c2ecf20Sopenharmony_ci ubi->beb_rsvd_pebs = ubi->beb_rsvd_level; 16948c2ecf20Sopenharmony_ci 16958c2ecf20Sopenharmony_ci ubi->avail_pebs -= ubi->beb_rsvd_pebs; 16968c2ecf20Sopenharmony_ci ubi->rsvd_pebs += ubi->beb_rsvd_pebs; 16978c2ecf20Sopenharmony_ci } 16988c2ecf20Sopenharmony_ci 16998c2ecf20Sopenharmony_ci dbg_eba("EBA sub-system is initialized"); 17008c2ecf20Sopenharmony_ci return 0; 17018c2ecf20Sopenharmony_ci 17028c2ecf20Sopenharmony_ciout_free: 17038c2ecf20Sopenharmony_ci for (i = 0; i < num_volumes; i++) { 17048c2ecf20Sopenharmony_ci if (!ubi->volumes[i]) 17058c2ecf20Sopenharmony_ci continue; 17068c2ecf20Sopenharmony_ci ubi_eba_replace_table(ubi->volumes[i], NULL); 17078c2ecf20Sopenharmony_ci } 17088c2ecf20Sopenharmony_ci return err; 17098c2ecf20Sopenharmony_ci} 1710