Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (c) International Business Machines Corp., 2006
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Author: Artem Bityutskiy (Битюцкий Артём)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * The UBI Eraseblock Association (EBA) sub-system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * This sub-system is responsible for I/O to/from logical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * Although in this implementation the EBA table is fully kept and managed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * RAM, which assumes poor scalability, it might be (partially) maintained on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * flash in future implementations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * The EBA sub-system implements per-logical eraseblock locking. Before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  * accessing a logical eraseblock it is locked for reading or writing. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * per-logical eraseblock locking is implemented by means of the lock tree. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * lock tree is an RB-tree which refers all the currently locked logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * They are indexed by (@vol_id, @lnum) pairs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * EBA also maintains the global sequence counter which is incremented each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  * time a logical eraseblock is mapped to a physical eraseblock and it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * stored in the volume identifier header. This means that each VID header has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * a unique sequence number. The sequence number is only increased an we assume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  * 64 bits is enough to never overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include "ubi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) /* Number of physical eraseblocks reserved for atomic LEB change operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define EBA_RESERVED_PEBS 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * struct ubi_eba_entry - structure encoding a single LEB -> PEB association
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * @pnum: the physical eraseblock number attached to the LEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * This structure is encoding a LEB -> PEB association. Note that the LEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * number is not stored here, because it is the index used to access the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * entries table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) struct ubi_eba_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	int pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * struct ubi_eba_table - LEB -> PEB association information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * @entries: the LEB to PEB mapping (one entry per LEB).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * This structure is private to the EBA logic and should be kept here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * It is encoding the LEB to PEB association table, and is subject to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) struct ubi_eba_table {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	struct ubi_eba_entry *entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  * next_sqnum - get next sequence number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  * This function returns next sequence number to use, which is just the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  * global sequence counter value. It also increases the global sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) unsigned long long ubi_next_sqnum(struct ubi_device *ubi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	unsigned long long sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	spin_lock(&ubi->ltree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	sqnum = ubi->global_sqnum++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	spin_unlock(&ubi->ltree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	return sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  * ubi_get_compat - get compatibility flags of a volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  * @vol_id: volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  * This function returns compatibility flags for an internal volume. User
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * volumes have no compatibility flags, so %0 is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	if (vol_id == UBI_LAYOUT_VOLUME_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		return UBI_LAYOUT_VOLUME_COMPAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  * ubi_eba_get_ldesc - get information about a LEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * @ldesc: the LEB descriptor to fill
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * Used to query information about a specific LEB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  * It is currently only returning the physical position of the LEB, but will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  * extended to provide more information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) void ubi_eba_get_ldesc(struct ubi_volume *vol, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		       struct ubi_eba_leb_desc *ldesc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	ldesc->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	ldesc->pnum = vol->eba_tbl->entries[lnum].pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  * ubi_eba_create_table - allocate a new EBA table and initialize it with all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  *			  LEBs unmapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  * @vol: volume containing the EBA table to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  * @nentries: number of entries in the table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  * Allocate a new EBA table and initialize it with all LEBs unmapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  * Returns a valid pointer if it succeed, an ERR_PTR() otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) struct ubi_eba_table *ubi_eba_create_table(struct ubi_volume *vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 					   int nentries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	struct ubi_eba_table *tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	tbl = kzalloc(sizeof(*tbl), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	if (!tbl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	tbl->entries = kmalloc_array(nentries, sizeof(*tbl->entries),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 				     GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	if (!tbl->entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	for (i = 0; i < nentries; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		tbl->entries[i].pnum = UBI_LEB_UNMAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	return tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	kfree(tbl->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	kfree(tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  * ubi_eba_destroy_table - destroy an EBA table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  * @tbl: the table to destroy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155)  * Destroy an EBA table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) void ubi_eba_destroy_table(struct ubi_eba_table *tbl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	if (!tbl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	kfree(tbl->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	kfree(tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  * ubi_eba_copy_table - copy the EBA table attached to vol into another table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  * @vol: volume containing the EBA table to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  * @dst: destination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  * @nentries: number of entries to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172)  * Copy the EBA table stored in vol into the one pointed by dst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) void ubi_eba_copy_table(struct ubi_volume *vol, struct ubi_eba_table *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 			int nentries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	struct ubi_eba_table *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	ubi_assert(dst && vol && vol->eba_tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	src = vol->eba_tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	for (i = 0; i < nentries; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		dst->entries[i].pnum = src->entries[i].pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189)  * ubi_eba_replace_table - assign a new EBA table to a volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190)  * @vol: volume containing the EBA table to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191)  * @tbl: new EBA table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  * Assign a new EBA table to the volume and release the old one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) void ubi_eba_replace_table(struct ubi_volume *vol, struct ubi_eba_table *tbl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	ubi_eba_destroy_table(vol->eba_tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	vol->eba_tbl = tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  * ltree_lookup - look up the lock tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  * @vol_id: volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  * This function returns a pointer to the corresponding &struct ubi_ltree_entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  * object if the logical eraseblock is locked and %NULL if it is not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  * @ubi->ltree_lock has to be locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 					    int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	struct rb_node *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	p = ubi->ltree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	while (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		struct ubi_ltree_entry *le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		le = rb_entry(p, struct ubi_ltree_entry, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		if (vol_id < le->vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 			p = p->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		else if (vol_id > le->vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 			p = p->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 			if (lnum < le->lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 				p = p->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			else if (lnum > le->lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 				p = p->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 				return le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240)  * ltree_add_entry - add new entry to the lock tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242)  * @vol_id: volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245)  * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246)  * lock tree. If such entry is already there, its usage counter is increased.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247)  * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248)  * failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 					       int vol_id, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	struct ubi_ltree_entry *le, *le1, *le_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	if (!le)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	le->users = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	init_rwsem(&le->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	le->vol_id = vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	le->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	spin_lock(&ubi->ltree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	le1 = ltree_lookup(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	if (le1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		 * This logical eraseblock is already locked. The newly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		 * allocated lock entry is not needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		le_free = le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		le = le1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		struct rb_node **p, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		 * No lock entry, add the newly allocated one to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		 * @ubi->ltree RB-tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		le_free = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		p = &ubi->ltree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 			le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 			if (vol_id < le1->vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 				p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 			else if (vol_id > le1->vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 				p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 				ubi_assert(lnum != le1->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 				if (lnum < le1->lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 					p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 					p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		rb_link_node(&le->rb, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		rb_insert_color(&le->rb, &ubi->ltree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	le->users += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	spin_unlock(&ubi->ltree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	kfree(le_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	return le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312)  * leb_read_lock - lock logical eraseblock for reading.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314)  * @vol_id: volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317)  * This function locks a logical eraseblock for reading. Returns zero in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318)  * of success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	struct ubi_ltree_entry *le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	le = ltree_add_entry(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	if (IS_ERR(le))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		return PTR_ERR(le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	down_read(&le->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332)  * leb_read_unlock - unlock logical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334)  * @vol_id: volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	struct ubi_ltree_entry *le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	spin_lock(&ubi->ltree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	le = ltree_lookup(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	le->users -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	ubi_assert(le->users >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	up_read(&le->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	if (le->users == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		rb_erase(&le->rb, &ubi->ltree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		kfree(le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	spin_unlock(&ubi->ltree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354)  * leb_write_lock - lock logical eraseblock for writing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356)  * @vol_id: volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359)  * This function locks a logical eraseblock for writing. Returns zero in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360)  * of success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	struct ubi_ltree_entry *le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	le = ltree_add_entry(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	if (IS_ERR(le))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		return PTR_ERR(le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	down_write(&le->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374)  * leb_write_trylock - try to lock logical eraseblock for writing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376)  * @vol_id: volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379)  * This function locks a logical eraseblock for writing if there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380)  * contention and does nothing if there is contention. Returns %0 in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381)  * success, %1 in case of contention, and and a negative error code in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382)  * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	struct ubi_ltree_entry *le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	le = ltree_add_entry(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	if (IS_ERR(le))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		return PTR_ERR(le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	if (down_write_trylock(&le->mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	/* Contention, cancel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	spin_lock(&ubi->ltree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	le->users -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	ubi_assert(le->users >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	if (le->users == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		rb_erase(&le->rb, &ubi->ltree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		kfree(le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	spin_unlock(&ubi->ltree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408)  * leb_write_unlock - unlock logical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410)  * @vol_id: volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	struct ubi_ltree_entry *le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	spin_lock(&ubi->ltree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	le = ltree_lookup(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	le->users -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	ubi_assert(le->users >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	up_write(&le->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	if (le->users == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		rb_erase(&le->rb, &ubi->ltree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		kfree(le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	spin_unlock(&ubi->ltree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  * ubi_eba_is_mapped - check if a LEB is mapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434)  * This function returns true if the LEB is mapped, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) bool ubi_eba_is_mapped(struct ubi_volume *vol, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	return vol->eba_tbl->entries[lnum].pnum >= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442)  * ubi_eba_unmap_leb - un-map logical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447)  * This function un-maps logical eraseblock @lnum and schedules corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448)  * physical eraseblock for erasure. Returns zero in case of success and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  * negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		      int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	int err, pnum, vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	if (ubi->ro_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	err = leb_write_lock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	pnum = vol->eba_tbl->entries[lnum].pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	if (pnum < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		/* This logical eraseblock is already unmapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	down_read(&ubi->fm_eba_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	up_read(&ubi->fm_eba_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	leb_write_unlock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) #ifdef CONFIG_MTD_UBI_FASTMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482)  * check_mapping - check and fixup a mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486)  * @pnum: physical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488)  * Checks whether a given mapping is valid. Fastmap cannot track LEB unmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489)  * operations, if such an operation is interrupted the mapping still looks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  * good, but upon first read an ECC is reported to the upper layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491)  * Normaly during the full-scan at attach time this is fixed, for Fastmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492)  * we have to deal with it while reading.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493)  * If the PEB behind a LEB shows this symthom we change the mapping to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494)  * %UBI_LEB_UNMAPPED and schedule the PEB for erasure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  * Returns 0 on success, negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			 int *pnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	struct ubi_vid_io_buf *vidb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	struct ubi_vid_hdr *vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	if (!ubi->fast_attach)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	if (!vol->checkmap || test_bit(lnum, vol->checkmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	if (!vidb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	err = ubi_io_read_vid_hdr(ubi, *pnum, vidb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	if (err > 0 && err != UBI_IO_BITFLIPS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		int torture = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		switch (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			case UBI_IO_FF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 			case UBI_IO_FF_BITFLIPS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			case UBI_IO_BAD_HDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 			case UBI_IO_BAD_HDR_EBADMSG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 				ubi_assert(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		if (err == UBI_IO_BAD_HDR_EBADMSG || err == UBI_IO_FF_BITFLIPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 			torture = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		down_read(&ubi->fm_eba_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		up_read(&ubi->fm_eba_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		ubi_wl_put_peb(ubi, vol->vol_id, lnum, *pnum, torture);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		*pnum = UBI_LEB_UNMAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	} else if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		ubi_err(ubi, "unable to read VID header back from PEB %i: %i",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 			*pnum, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		int found_vol_id, found_lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		ubi_assert(err == 0 || err == UBI_IO_BITFLIPS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		vid_hdr = ubi_get_vid_hdr(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		found_vol_id = be32_to_cpu(vid_hdr->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		found_lnum = be32_to_cpu(vid_hdr->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		if (found_lnum != lnum || found_vol_id != vol->vol_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 			ubi_err(ubi, "EBA mismatch! PEB %i is LEB %i:%i instead of LEB %i:%i",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 				*pnum, found_vol_id, found_lnum, vol->vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			ubi_ro_mode(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	set_bit(lnum, vol->checkmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	ubi_free_vid_buf(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		  int *pnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578)  * ubi_eba_read_leb - read data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582)  * @buf: buffer to store the read data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583)  * @offset: offset from where to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584)  * @len: how many bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585)  * @check: data CRC check flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587)  * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588)  * bytes. The @check flag only makes sense for static volumes and forces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589)  * eraseblock data CRC checking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591)  * In case of success this function returns zero. In case of a static volume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592)  * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593)  * returned for any volume type if an ECC error was detected by the MTD device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594)  * driver. Other negative error cored may be returned in case of other errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		     void *buf, int offset, int len, int check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	int err, pnum, scrub = 0, vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	struct ubi_vid_io_buf *vidb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	struct ubi_vid_hdr *vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	uint32_t crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	err = leb_read_lock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	pnum = vol->eba_tbl->entries[lnum].pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	if (pnum >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		err = check_mapping(ubi, vol, lnum, &pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	if (pnum == UBI_LEB_UNMAPPED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		 * The logical eraseblock is not mapped, fill the whole buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		 * with 0xFF bytes. The exception is static volumes for which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		 * it is an error to read unmapped logical eraseblocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 			len, offset, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		leb_read_unlock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		memset(buf, 0xFF, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		len, offset, vol_id, lnum, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	if (vol->vol_type == UBI_DYNAMIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		check = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	if (check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		if (!vidb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		vid_hdr = ubi_get_vid_hdr(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		if (err && err != UBI_IO_BITFLIPS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 			if (err > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 				 * The header is either absent or corrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 				 * The former case means there is a bug -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 				 * switch to read-only mode just in case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 				 * The latter case means a real corruption - we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 				 * may try to recover data. FIXME: but this is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 				 * not implemented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 				if (err == UBI_IO_BAD_HDR_EBADMSG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 				    err == UBI_IO_BAD_HDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 					ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 						 pnum, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 					err = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 					/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 					 * Ending up here in the non-Fastmap case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 					 * is a clear bug as the VID header had to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 					 * be present at scan time to have it referenced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 					 * With fastmap the story is more complicated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 					 * Fastmap has the mapping info without the need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 					 * of a full scan. So the LEB could have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 					 * unmapped, Fastmap cannot know this and keeps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 					 * the LEB referenced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 					 * This is valid and works as the layer above UBI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 					 * has to do bookkeeping about used/referenced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 					 * LEBs in any case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 					if (ubi->fast_attach) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 						err = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 					} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 						err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 						ubi_ro_mode(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		} else if (err == UBI_IO_BITFLIPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			scrub = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		crc = be32_to_cpu(vid_hdr->data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		ubi_free_vid_buf(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	err = ubi_io_read_data(ubi, buf, pnum, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		if (err == UBI_IO_BITFLIPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 			scrub = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		else if (mtd_is_eccerr(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			if (vol->vol_type == UBI_DYNAMIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 				goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 			scrub = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			if (!check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 				ubi_msg(ubi, "force data checking");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 				check = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 				goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	if (check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		if (crc1 != crc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 			ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 				 crc1, crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			err = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (scrub)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		err = ubi_wl_scrub_peb(ubi, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	leb_read_unlock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	ubi_free_vid_buf(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	leb_read_unlock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735)  * ubi_eba_read_leb_sg - read data into a scatter gather list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739)  * @sgl: UBI scatter gather list to store the read data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740)  * @offset: offset from where to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741)  * @len: how many bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742)  * @check: data CRC check flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744)  * This function works exactly like ubi_eba_read_leb(). But instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745)  * storing the read data into a buffer it writes to an UBI scatter gather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746)  * list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			struct ubi_sgl *sgl, int lnum, int offset, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			int check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	int to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		sg = &sgl->sg[sgl->list_pos];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		if (len < sg->length - sgl->page_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 			to_read = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 			to_read = sg->length - sgl->page_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		ret = ubi_eba_read_leb(ubi, vol, lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 				       sg_virt(sg) + sgl->page_pos, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 				       to_read, check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		offset += to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		len -= to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		if (!len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 			sgl->page_pos += to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 			if (sgl->page_pos == sg->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 				sgl->list_pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 				sgl->page_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		sgl->list_pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		sgl->page_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790)  * try_recover_peb - try to recover from write failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792)  * @pnum: the physical eraseblock to recover
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794)  * @buf: data which was not written because of the write failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795)  * @offset: offset of the failed write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796)  * @len: how many bytes should have been written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797)  * @vidb: VID buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798)  * @retry: whether the caller should retry in case of failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800)  * This function is called in case of a write failure and moves all good data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801)  * from the potentially bad physical eraseblock to a good physical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802)  * This function also writes the data which was not written due to the failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803)  * Returns 0 in case of success, and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804)  * In case of failure, the %retry parameter is set to false if this is a fatal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805)  * error (retrying won't help), and true otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) static int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			   const void *buf, int offset, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 			   struct ubi_vid_io_buf *vidb, bool *retry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	struct ubi_device *ubi = vol->ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	struct ubi_vid_hdr *vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	int new_pnum, err, vol_id = vol->vol_id, data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	uint32_t crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	*retry = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	new_pnum = ubi_wl_get_peb(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	if (new_pnum < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		err = new_pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		pnum, new_pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	if (err && err != UBI_IO_BITFLIPS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	vid_hdr = ubi_get_vid_hdr(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	ubi_assert(vid_hdr->vol_type == UBI_VID_DYNAMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	mutex_lock(&ubi->buf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	memset(ubi->peb_buf + offset, 0xFF, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	/* Read everything before the area where the write failure happened */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	if (offset > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		if (err && err != UBI_IO_BITFLIPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	*retry = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	memcpy(ubi->peb_buf + offset, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	data_size = offset + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	vid_hdr->copy_flag = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	vid_hdr->data_size = cpu_to_be32(data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	vid_hdr->data_crc = cpu_to_be32(crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	err = ubi_io_write_vid_hdr(ubi, new_pnum, vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	mutex_unlock(&ubi->buf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		vol->eba_tbl->entries[lnum].pnum = new_pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	up_read(&ubi->fm_eba_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		ubi_msg(ubi, "data was successfully recovered");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	} else if (new_pnum >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		 * Bad luck? This physical eraseblock is bad too? Crud. Let's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		 * try to get another one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		ubi_warn(ubi, "failed to write to PEB %d", new_pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888)  * recover_peb - recover from write failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890)  * @pnum: the physical eraseblock to recover
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891)  * @vol_id: volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893)  * @buf: data which was not written because of the write failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894)  * @offset: offset of the failed write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895)  * @len: how many bytes should have been written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897)  * This function is called in case of a write failure and moves all good data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898)  * from the potentially bad physical eraseblock to a good physical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899)  * This function also writes the data which was not written due to the failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900)  * Returns 0 in case of success, and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901)  * This function tries %UBI_IO_RETRIES before giving up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		       const void *buf, int offset, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	int err, idx = vol_id2idx(ubi, vol_id), tries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	struct ubi_volume *vol = ubi->volumes[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	struct ubi_vid_io_buf *vidb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	if (!vidb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		bool retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		err = try_recover_peb(vol, pnum, lnum, buf, offset, len, vidb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 				      &retry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		if (!err || !retry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		ubi_msg(ubi, "try again");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	ubi_free_vid_buf(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931)  * try_write_vid_and_data - try to write VID header and data to a new PEB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934)  * @vidb: the VID buffer to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935)  * @buf: buffer containing the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936)  * @offset: where to start writing data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937)  * @len: how many bytes should be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939)  * This function tries to write VID header and data belonging to logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940)  * eraseblock @lnum of volume @vol to a new physical eraseblock. Returns zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941)  * in case of success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942)  * In case of error, it is possible that something was still written to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943)  * flash media, but may be some garbage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) static int try_write_vid_and_data(struct ubi_volume *vol, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 				  struct ubi_vid_io_buf *vidb, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 				  int offset, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	struct ubi_device *ubi = vol->ubi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	int pnum, opnum, err, vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	pnum = ubi_wl_get_peb(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	if (pnum < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		err = pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	opnum = vol->eba_tbl->entries[lnum].pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		len, offset, vol_id, lnum, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	err = ubi_io_write_vid_hdr(ubi, pnum, vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 			 vol_id, lnum, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		err = ubi_io_write_data(ubi, buf, pnum, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			ubi_warn(ubi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 				 "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 				 len, offset, vol_id, lnum, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	vol->eba_tbl->entries[lnum].pnum = pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	up_read(&ubi->fm_eba_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	if (err && pnum >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	else if (!err && opnum >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		err = ubi_wl_put_peb(ubi, vol_id, lnum, opnum, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994)  * ubi_eba_write_leb - write data to dynamic volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  * @buf: the data to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  * @offset: offset within the logical eraseblock where to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)  * @len: how many bytes to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)  * This function writes data to logical eraseblock @lnum of a dynamic volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)  * @vol. Returns zero in case of success and a negative error code in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)  * of failure. In case of error, it is possible that something was still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)  * written to the flash media, but may be some garbage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)  * This function retries %UBI_IO_RETRIES times before giving up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		      const void *buf, int offset, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	int err, pnum, tries, vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	struct ubi_vid_io_buf *vidb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	struct ubi_vid_hdr *vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	if (ubi->ro_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	err = leb_write_lock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	pnum = vol->eba_tbl->entries[lnum].pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	if (pnum >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		err = check_mapping(ubi, vol, lnum, &pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	if (pnum >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 			len, offset, vol_id, lnum, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		err = ubi_io_write_data(ubi, buf, pnum, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 			ubi_warn(ubi, "failed to write data to PEB %d", pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 			if (err == -EIO && ubi->bad_allowed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 				err = recover_peb(ubi, pnum, vol_id, lnum, buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 						  offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	 * The logical eraseblock is not mapped. We have to get a free physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	 * eraseblock and write the volume identifier header there first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	if (!vidb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		leb_write_unlock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	vid_hdr = ubi_get_vid_hdr(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	vid_hdr->vol_type = UBI_VID_DYNAMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	vid_hdr->vol_id = cpu_to_be32(vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	vid_hdr->lnum = cpu_to_be32(lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		err = try_write_vid_and_data(vol, lnum, vidb, buf, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		if (err != -EIO || !ubi->bad_allowed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		 * Fortunately, this is the first write operation to this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		 * physical eraseblock, so just put it and request a new one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		 * We assume that if this physical eraseblock went bad, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		 * erase code will handle that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		ubi_msg(ubi, "try another PEB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	ubi_free_vid_buf(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		ubi_ro_mode(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	leb_write_unlock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)  * ubi_eba_write_leb_st - write data to static volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)  * @buf: data to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)  * @len: how many bytes to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)  * @used_ebs: how many logical eraseblocks will this volume contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)  * This function writes data to logical eraseblock @lnum of static volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)  * @vol. The @used_ebs argument should contain total number of logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)  * eraseblock in this static volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)  * When writing to the last logical eraseblock, the @len argument doesn't have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)  * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104)  * to the real data size, although the @buf buffer has to contain the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)  * alignment. In all other cases, @len has to be aligned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)  * It is prohibited to write more than once to logical eraseblocks of static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)  * volumes. This function returns zero in case of success and a negative error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)  * code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			 int lnum, const void *buf, int len, int used_ebs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	int err, tries, data_size = len, vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	struct ubi_vid_io_buf *vidb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	struct ubi_vid_hdr *vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	uint32_t crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	if (ubi->ro_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	if (lnum == used_ebs - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		/* If this is the last LEB @len may be unaligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		len = ALIGN(data_size, ubi->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		ubi_assert(!(len & (ubi->min_io_size - 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	if (!vidb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	vid_hdr = ubi_get_vid_hdr(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	err = leb_write_lock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	vid_hdr->vol_id = cpu_to_be32(vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	vid_hdr->lnum = cpu_to_be32(lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	crc = crc32(UBI_CRC32_INIT, buf, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	vid_hdr->vol_type = UBI_VID_STATIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	vid_hdr->data_size = cpu_to_be32(data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	vid_hdr->used_ebs = cpu_to_be32(used_ebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	vid_hdr->data_crc = cpu_to_be32(crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	ubi_assert(vol->eba_tbl->entries[lnum].pnum < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		if (err != -EIO || !ubi->bad_allowed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		ubi_msg(ubi, "try another PEB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		ubi_ro_mode(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	leb_write_unlock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	ubi_free_vid_buf(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)  * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175)  * @vol: volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)  * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)  * @buf: data to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)  * @len: how many bytes to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)  * This function changes the contents of a logical eraseblock atomically. @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)  * has to contain new logical eraseblock data, and @len - the length of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)  * data, which has to be aligned. This function guarantees that in case of an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)  * unclean reboot the old contents is preserved. Returns zero in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)  * success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)  * UBI reserves one LEB for the "atomic LEB change" operation, so only one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)  * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			      int lnum, const void *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	int err, tries, vol_id = vol->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	struct ubi_vid_io_buf *vidb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	struct ubi_vid_hdr *vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	uint32_t crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	if (ubi->ro_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	if (len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		 * Special case when data length is zero. In this case the LEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		 * has to be unmapped and mapped somewhere else.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		err = ubi_eba_unmap_leb(ubi, vol, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	if (!vidb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	vid_hdr = ubi_get_vid_hdr(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	mutex_lock(&ubi->alc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	err = leb_write_lock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		goto out_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	vid_hdr->vol_id = cpu_to_be32(vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	vid_hdr->lnum = cpu_to_be32(lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	crc = crc32(UBI_CRC32_INIT, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	vid_hdr->vol_type = UBI_VID_DYNAMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	vid_hdr->data_size = cpu_to_be32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	vid_hdr->copy_flag = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	vid_hdr->data_crc = cpu_to_be32(crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	dbg_eba("change LEB %d:%d", vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		if (err != -EIO || !ubi->bad_allowed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		ubi_msg(ubi, "try another PEB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	 * This flash device does not admit of bad eraseblocks or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	 * something nasty and unexpected happened. Switch to read-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	 * mode just in case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		ubi_ro_mode(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	leb_write_unlock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) out_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	mutex_unlock(&ubi->alc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	ubi_free_vid_buf(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)  * is_error_sane - check whether a read error is sane.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)  * @err: code of the error happened during reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265)  * This is a helper function for 'ubi_eba_copy_leb()' which is called when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)  * cannot read data from the target PEB (an error @err happened). If the error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)  * code is sane, then we treat this error as non-fatal. Otherwise the error is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)  * fatal and UBI will be switched to R/O mode later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)  * The idea is that we try not to switch to R/O mode if the read error is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)  * something which suggests there was a real read problem. E.g., %-EIO. Or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)  * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)  * mode, simply because we do not know what happened at the MTD level, and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)  * cannot handle this. E.g., the underlying driver may have become crazy, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)  * it is safer to switch to R/O mode to preserve the data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)  * And bear in mind, this is about reading from the target PEB, i.e. the PEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)  * which we have just written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) static int is_error_sane(int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	    err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)  * ubi_eba_copy_leb - copy logical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)  * @from: physical eraseblock number from where to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)  * @to: physical eraseblock number where to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293)  * @vid_hdr: VID header of the @from physical eraseblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)  * This function copies logical eraseblock from physical eraseblock @from to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)  * physical eraseblock @to. The @vid_hdr buffer may be changed by this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)  * function. Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)  *   o %0 in case of success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299)  *   o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)  *   o a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		     struct ubi_vid_io_buf *vidb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	int err, vol_id, lnum, data_size, aldata_size, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	struct ubi_volume *vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	uint32_t crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	vol_id = be32_to_cpu(vid_hdr->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	lnum = be32_to_cpu(vid_hdr->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	if (vid_hdr->vol_type == UBI_VID_STATIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		data_size = be32_to_cpu(vid_hdr->data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		aldata_size = ALIGN(data_size, ubi->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		data_size = aldata_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 			    ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	idx = vol_id2idx(ubi, vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	spin_lock(&ubi->volumes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	 * Note, we may race with volume deletion, which means that the volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	 * this logical eraseblock belongs to might be being deleted. Since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	 * volume deletion un-maps all the volume's logical eraseblocks, it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	vol = ubi->volumes[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	spin_unlock(&ubi->volumes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	if (!vol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		/* No need to do further work, cancel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		dbg_wl("volume %d is being removed, cancel", vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		return MOVE_CANCEL_RACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	 * We do not want anybody to write to this logical eraseblock while we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	 * are moving it, so lock it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	 * Note, we are using non-waiting locking here, because we cannot sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	 * unmapping the LEB which is mapped to the PEB we are going to move
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	 * (@from). This task locks the LEB and goes sleep in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	 * LEB is already locked, we just do not move it and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	 * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	 * we do not know the reasons of the contention - it may be just a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	 * normal I/O on this LEB, so we want to re-try.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	err = leb_write_trylock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		return MOVE_RETRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	 * The LEB might have been put meanwhile, and the task which put it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	 * probably waiting on @ubi->move_mutex. No need to continue the work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	 * cancel it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	if (vol->eba_tbl->entries[lnum].pnum != from) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		       vol_id, lnum, from, vol->eba_tbl->entries[lnum].pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		err = MOVE_CANCEL_RACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		goto out_unlock_leb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	 * OK, now the LEB is locked and we can safely start moving it. Since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	 * this function utilizes the @ubi->peb_buf buffer which is shared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	 * with some other functions - we lock the buffer by taking the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	 * @ubi->buf_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	mutex_lock(&ubi->buf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	dbg_wl("read %d bytes of data", aldata_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	if (err && err != UBI_IO_BITFLIPS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		ubi_warn(ubi, "error %d while reading data from PEB %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 			 err, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		err = MOVE_SOURCE_RD_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		goto out_unlock_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	 * Now we have got to calculate how much data we have to copy. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	 * case of a static volume it is fairly easy - the VID header contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	 * the data size. In case of a dynamic volume it is more difficult - we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	 * have to read the contents, cut 0xFF bytes from the end and copy only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	 * the first part. We must do this to avoid writing 0xFF bytes as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	 * may have some side-effects. And not only this. It is important not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	 * to include those 0xFFs to CRC because later the they may be filled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	 * by data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		aldata_size = data_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 			ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	 * It may turn out to be that the whole @from physical eraseblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	 * contains only 0xFF bytes. Then we have to only write the VID header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	 * and do not write any data. This also means we should not set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	if (data_size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		vid_hdr->copy_flag = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		vid_hdr->data_size = cpu_to_be32(data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		vid_hdr->data_crc = cpu_to_be32(crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	err = ubi_io_write_vid_hdr(ubi, to, vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		if (err == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			err = MOVE_TARGET_WR_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		goto out_unlock_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	/* Read the VID header back and check if it was written correctly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	err = ubi_io_read_vid_hdr(ubi, to, vidb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		if (err != UBI_IO_BITFLIPS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 			ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 				 err, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 			if (is_error_sane(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 				err = MOVE_TARGET_RD_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 			err = MOVE_TARGET_BITFLIPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		goto out_unlock_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	if (data_size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 			if (err == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 				err = MOVE_TARGET_WR_ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 			goto out_unlock_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	ubi_assert(vol->eba_tbl->entries[lnum].pnum == from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	vol->eba_tbl->entries[lnum].pnum = to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) out_unlock_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	mutex_unlock(&ubi->buf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) out_unlock_leb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	leb_write_unlock(ubi, vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464)  * print_rsvd_warning - warn about not having enough reserved PEBs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)  * This is a helper function for 'ubi_eba_init()' which is called when UBI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468)  * cannot reserve enough PEBs for bad block handling. This function makes a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)  * decision whether we have to print a warning or not. The algorithm is as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)  * follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)  *   o if this is a new UBI image, then just print the warning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)  *   o if this is an UBI image which has already been used for some time, print
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473)  *     a warning only if we can reserve less than 10% of the expected amount of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)  *     the reserved PEB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)  * The idea is that when UBI is used, PEBs become bad, and the reserved pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477)  * of PEBs becomes smaller, which is normal and we do not want to scare users
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478)  * with a warning every time they attach the MTD device. This was an issue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)  * reported by real users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) static void print_rsvd_warning(struct ubi_device *ubi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 			       struct ubi_attach_info *ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	 * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	 * large number to distinguish between newly flashed and used images.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	if (ai->max_sqnum > (1 << 18)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		int min = ubi->beb_rsvd_level / 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 		if (!min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 			min = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 		if (ubi->beb_rsvd_pebs > min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	if (ubi->corr_peb_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		ubi_warn(ubi, "%d PEBs are corrupted and not used",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 			 ubi->corr_peb_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)  * self_check_eba - run a self check on the EBA table constructed by fastmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)  * @ai_fastmap: UBI attach info object created by fastmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)  * @ai_scan: UBI attach info object created by scanning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)  * Returns < 0 in case of an internal error, 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)  * If a bad EBA table entry was found it will be printed out and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512)  * ubi_assert() triggers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		   struct ubi_attach_info *ai_scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	int i, j, num_volumes, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	int **scan_eba, **fm_eba;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	struct ubi_ainf_volume *av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	struct ubi_volume *vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	struct ubi_ainf_peb *aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	struct rb_node *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	scan_eba = kmalloc_array(num_volumes, sizeof(*scan_eba), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	if (!scan_eba)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	fm_eba = kmalloc_array(num_volumes, sizeof(*fm_eba), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	if (!fm_eba) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		kfree(scan_eba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	for (i = 0; i < num_volumes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		vol = ubi->volumes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		if (!vol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		scan_eba[i] = kmalloc_array(vol->reserved_pebs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 					    sizeof(**scan_eba),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 					    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		if (!scan_eba[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		fm_eba[i] = kmalloc_array(vol->reserved_pebs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 					  sizeof(**fm_eba),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 					  GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		if (!fm_eba[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		for (j = 0; j < vol->reserved_pebs; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 			scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		av = ubi_find_av(ai_scan, idx2vol_id(ubi, i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		if (!av)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 			scan_eba[i][aeb->lnum] = aeb->pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		if (!av)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 			fm_eba[i][aeb->lnum] = aeb->pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		for (j = 0; j < vol->reserved_pebs; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 			if (scan_eba[i][j] != fm_eba[i][j]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 				if (scan_eba[i][j] == UBI_LEB_UNMAPPED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 					fm_eba[i][j] == UBI_LEB_UNMAPPED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 				ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 					vol->vol_id, j, fm_eba[i][j],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 					scan_eba[i][j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 				ubi_assert(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	for (i = 0; i < num_volumes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		if (!ubi->volumes[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		kfree(scan_eba[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		kfree(fm_eba[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	kfree(scan_eba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	kfree(fm_eba);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)  * ubi_eba_init - initialize the EBA sub-system using attaching information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604)  * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605)  * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)  * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608)  * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	int i, err, num_volumes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	struct ubi_ainf_volume *av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	struct ubi_volume *vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	struct ubi_ainf_peb *aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	struct rb_node *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	dbg_eba("initialize EBA sub-system");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	spin_lock_init(&ubi->ltree_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	mutex_init(&ubi->alc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	ubi->ltree = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	ubi->global_sqnum = ai->max_sqnum + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	for (i = 0; i < num_volumes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		struct ubi_eba_table *tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		vol = ubi->volumes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		if (!vol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		tbl = ubi_eba_create_table(vol, vol->reserved_pebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		if (IS_ERR(tbl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 			err = PTR_ERR(tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		ubi_eba_replace_table(vol, tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 		av = ubi_find_av(ai, idx2vol_id(ubi, i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		if (!av)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 		ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 			if (aeb->lnum >= vol->reserved_pebs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 				 * This may happen in case of an unclean reboot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 				 * during re-size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 				ubi_move_aeb_to_list(av, aeb, &ai->erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 				struct ubi_eba_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 				entry = &vol->eba_tbl->entries[aeb->lnum];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 				entry->pnum = aeb->pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 			ubi->avail_pebs, EBA_RESERVED_PEBS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		if (ubi->corr_peb_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 			ubi_err(ubi, "%d PEBs are corrupted and not used",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 				ubi->corr_peb_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 		err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	ubi->avail_pebs -= EBA_RESERVED_PEBS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	ubi->rsvd_pebs += EBA_RESERVED_PEBS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	if (ubi->bad_allowed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		ubi_calculate_reserved(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		if (ubi->avail_pebs < ubi->beb_rsvd_level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 			/* No enough free physical eraseblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 			ubi->beb_rsvd_pebs = ubi->avail_pebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 			print_rsvd_warning(ubi, ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 			ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		ubi->avail_pebs -= ubi->beb_rsvd_pebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 		ubi->rsvd_pebs  += ubi->beb_rsvd_pebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	dbg_eba("EBA sub-system is initialized");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	for (i = 0; i < num_volumes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 		if (!ubi->volumes[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		ubi_eba_replace_table(ubi->volumes[i], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) }