^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) * UBI attaching 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 attaching MTD devices and it also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * implements flash media scanning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * The attaching information is represented by a &struct ubi_attach_info'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * object. Information about volumes is represented by &struct ubi_ainf_volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * objects which are kept in volume RB-tree with root at the @volumes field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * The RB-tree is indexed by the volume ID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Logical eraseblocks are represented by &struct ubi_ainf_peb objects. These
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * objects are kept in per-volume RB-trees with the root at the corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * &struct ubi_ainf_volume object. To put it differently, we keep an RB-tree of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * per-volume objects and each of these objects is the root of RB-tree of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * per-LEB objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Corrupted physical eraseblocks are put to the @corr list, free physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * eraseblocks are put to the @free list and the physical eraseblock to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * erased are put to the @erase list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * About corruptions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * ~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * UBI protects EC and VID headers with CRC-32 checksums, so it can detect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * whether the headers are corrupted or not. Sometimes UBI also protects the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * data with CRC-32, e.g., when it executes the atomic LEB change operation, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * when it moves the contents of a PEB for wear-leveling purposes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * UBI tries to distinguish between 2 types of corruptions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * tries to handle them gracefully, without printing too many warnings and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * error messages. The idea is that we do not lose important data in these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * cases - we may lose only the data which were being written to the media just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * before the power cut happened, and the upper layers (e.g., UBIFS) are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * supposed to handle such data losses (e.g., by using the FS journal).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * the reason is a power cut, UBI puts this PEB to the @erase list, and all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * PEBs in the @erase list are scheduled for erasure later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * 2. Unexpected corruptions which are not caused by power cuts. During
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * attaching, such PEBs are put to the @corr list and UBI preserves them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Obviously, this lessens the amount of available PEBs, and if at some point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * about such PEBs every time the MTD device is attached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * However, it is difficult to reliably distinguish between these types of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * corruptions and UBI's strategy is as follows (in case of attaching by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * scanning). UBI assumes corruption type 2 if the VID header is corrupted and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * the data area does not contain all 0xFFs, and there were no bit-flips or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * integrity errors (e.g., ECC errors in case of NAND) while reading the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * area. Otherwise UBI assumes corruption type 1. So the decision criteria
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * are as follows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * o If the data area contains only 0xFFs, there are no data, and it is safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * to just erase this PEB - this is corruption type 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * o If the data area has bit-flips or data integrity errors (ECC errors on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * NAND), it is probably a PEB which was being erased when power cut
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * happened, so this is corruption type 1. However, this is just a guess,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * which might be wrong.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * o Otherwise this is corruption type 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #include <linux/math64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #include "ubi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define AV_FIND BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define AV_ADD BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define AV_FIND_OR_ADD (AV_FIND | AV_ADD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * find_or_add_av - internal function to find a volume, add a volume or do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * both (find and add if missing).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * @vol_id: the requested volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * @flags: a combination of the %AV_FIND and %AV_ADD flags describing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * expected operation. If only %AV_ADD is set, -EEXIST is returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * if the volume already exists. If only %AV_FIND is set, NULL is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * returned if the volume does not exist. And if both flags are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * set, the helper first tries to find an existing volume, and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * it does not exist it creates a new one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * @created: in value used to inform the caller whether it"s a newly created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * volume or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * This function returns a pointer to a volume description or an ERR_PTR if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * the operation failed. It can also return NULL if only %AV_FIND is set and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * the volume does not exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static struct ubi_ainf_volume *find_or_add_av(struct ubi_attach_info *ai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int vol_id, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) bool *created)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct ubi_ainf_volume *av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* Walk the volume RB-tree to look if this volume is already present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) av = rb_entry(parent, struct ubi_ainf_volume, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (vol_id == av->vol_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *created = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!(flags & AV_FIND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return ERR_PTR(-EEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (vol_id > av->vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (!(flags & AV_ADD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /* The volume is absent - add it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) av = kzalloc(sizeof(*av), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (!av)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) av->vol_id = vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (vol_id > ai->highest_vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) ai->highest_vol_id = vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) rb_link_node(&av->rb, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) rb_insert_color(&av->rb, &ai->volumes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) ai->vols_found += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) *created = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) dbg_bld("added volume %d", vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return av;
^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_find_or_add_av - search for a volume in the attaching information and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * add one if it does not exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * @vol_id: the requested volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * @created: whether the volume has been created or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * This function returns a pointer to the new volume description or an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * ERR_PTR if the operation failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static struct ubi_ainf_volume *ubi_find_or_add_av(struct ubi_attach_info *ai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int vol_id, bool *created)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return find_or_add_av(ai, vol_id, AV_FIND_OR_ADD, created);
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * ubi_alloc_aeb - allocate an aeb element
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * @pnum: physical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * @ec: erase counter of the physical eraseblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * Allocate an aeb object and initialize the pnum and ec information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * vol_id and lnum are set to UBI_UNKNOWN, and the other fields are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * initialized to zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * Note that the element is not added in any list or RB tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct ubi_ainf_peb *ubi_alloc_aeb(struct ubi_attach_info *ai, int pnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int ec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct ubi_ainf_peb *aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) aeb = kmem_cache_zalloc(ai->aeb_slab_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!aeb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) aeb->pnum = pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) aeb->ec = ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) aeb->vol_id = UBI_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) aeb->lnum = UBI_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * ubi_free_aeb - free an aeb element
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @aeb: the element to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * Free an aeb object. The caller must have removed the element from any list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * or RB tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) void ubi_free_aeb(struct ubi_attach_info *ai, struct ubi_ainf_peb *aeb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) kmem_cache_free(ai->aeb_slab_cache, aeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * add_to_list - add physical eraseblock to a list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * @pnum: physical eraseblock number to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * @vol_id: the last used volume id for the PEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * @lnum: the last used LEB number for the PEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * @ec: erase counter of the physical eraseblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * @to_head: if not zero, add to the head of the list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * @list: the list to add to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * This function allocates a 'struct ubi_ainf_peb' object for physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * eraseblock @pnum and adds it to the "free", "erase", or "alien" lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * It stores the @lnum and @vol_id alongside, which can both be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * %UBI_UNKNOWN if they are not available, not readable, or not assigned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * If @to_head is not zero, PEB will be added to the head of the list, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * basically means it will be processed first later. E.g., we add corrupted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * PEBs (corrupted due to power cuts) to the head of the erase list to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * sure we erase them first and get rid of corruptions ASAP. This function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * returns zero in case of success and a negative error code in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static int add_to_list(struct ubi_attach_info *ai, int pnum, int vol_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int lnum, int ec, int to_head, struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct ubi_ainf_peb *aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (list == &ai->free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) } else if (list == &ai->erase) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) } else if (list == &ai->alien) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) ai->alien_peb_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) aeb = ubi_alloc_aeb(ai, pnum, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!aeb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) aeb->vol_id = vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) aeb->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (to_head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) list_add(&aeb->u.list, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) list_add_tail(&aeb->u.list, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * add_corrupted - add a corrupted physical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * @pnum: physical eraseblock number to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * @ec: erase counter of the physical eraseblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * This function allocates a 'struct ubi_ainf_peb' object for a corrupted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * physical eraseblock @pnum and adds it to the 'corr' list. The corruption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * was presumably not caused by a power cut. Returns zero in case of success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int add_corrupted(struct ubi_attach_info *ai, int pnum, int ec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) struct ubi_ainf_peb *aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) aeb = ubi_alloc_aeb(ai, pnum, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (!aeb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ai->corr_peb_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) list_add(&aeb->u.list, &ai->corr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * add_fastmap - add a Fastmap related physical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * @pnum: physical eraseblock number the VID header came from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * @vid_hdr: the volume identifier header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * @ec: erase counter of the physical eraseblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * This function allocates a 'struct ubi_ainf_peb' object for a Fastamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * physical eraseblock @pnum and adds it to the 'fastmap' list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * Such blocks can be Fastmap super and data blocks from both the most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * recent Fastmap we're attaching from or from old Fastmaps which will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * be erased.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static int add_fastmap(struct ubi_attach_info *ai, int pnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct ubi_vid_hdr *vid_hdr, int ec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct ubi_ainf_peb *aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) aeb = ubi_alloc_aeb(ai, pnum, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (!aeb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) aeb->vol_id = be32_to_cpu(vid_hdr->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) aeb->sqnum = be64_to_cpu(vid_hdr->sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) list_add(&aeb->u.list, &ai->fastmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) dbg_bld("add to fastmap list: PEB %d, vol_id %d, sqnum: %llu", pnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) aeb->vol_id, aeb->sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * validate_vid_hdr - check volume identifier header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * @vid_hdr: the volume identifier header to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * @av: information about the volume this logical eraseblock belongs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * @pnum: physical eraseblock number the VID header came from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * This function checks that data stored in @vid_hdr is consistent. Returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * non-zero if an inconsistency was found and zero if not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * Note, UBI does sanity check of everything it reads from the flash media.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * Most of the checks are done in the I/O sub-system. Here we check that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * information in the VID header is consistent to the information in other VID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * headers of the same volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static int validate_vid_hdr(const struct ubi_device *ubi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) const struct ubi_vid_hdr *vid_hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) const struct ubi_ainf_volume *av, int pnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int vol_type = vid_hdr->vol_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int vol_id = be32_to_cpu(vid_hdr->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int data_pad = be32_to_cpu(vid_hdr->data_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (av->leb_count != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) int av_vol_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * This is not the first logical eraseblock belonging to this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * volume. Ensure that the data in its VID header is consistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * to the data in previous logical eraseblock headers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (vol_id != av->vol_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) ubi_err(ubi, "inconsistent vol_id");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) goto bad;
^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) if (av->vol_type == UBI_STATIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) av_vol_type = UBI_VID_STATIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) av_vol_type = UBI_VID_DYNAMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (vol_type != av_vol_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ubi_err(ubi, "inconsistent vol_type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (used_ebs != av->used_ebs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) ubi_err(ubi, "inconsistent used_ebs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (data_pad != av->data_pad) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) ubi_err(ubi, "inconsistent data_pad");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) goto bad;
^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) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ubi_err(ubi, "inconsistent VID header at PEB %d", pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) ubi_dump_vid_hdr(vid_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) ubi_dump_av(av);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * add_volume - add volume to the attaching information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * @vol_id: ID of the volume to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * @pnum: physical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * @vid_hdr: volume identifier header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * If the volume corresponding to the @vid_hdr logical eraseblock is already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * present in the attaching information, this function does nothing. Otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * it adds corresponding volume to the attaching information. Returns a pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * to the allocated "av" object in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *ai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int vol_id, int pnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) const struct ubi_vid_hdr *vid_hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) struct ubi_ainf_volume *av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) bool created;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) av = ubi_find_or_add_av(ai, vol_id, &created);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (IS_ERR(av) || !created)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) av->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) av->data_pad = be32_to_cpu(vid_hdr->data_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) av->compat = vid_hdr->compat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) av->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) : UBI_STATIC_VOLUME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * ubi_compare_lebs - find out which logical eraseblock is newer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * @aeb: first logical eraseblock to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * @pnum: physical eraseblock number of the second logical eraseblock to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * @vid_hdr: volume identifier header of the second logical eraseblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * This function compares 2 copies of a LEB and informs which one is newer. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * case of success this function returns a positive value, in case of failure, a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * negative error code is returned. The success return codes use the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * bits:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * o bit 0 is cleared: the first PEB (described by @aeb) is newer than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * second PEB (described by @pnum and @vid_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * o bit 0 is set: the second PEB is newer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * o bit 1 is set: bit-flips were detected in the newer LEB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * o bit 2 is cleared: the older LEB is not corrupted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * o bit 2 is set: the older LEB is corrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) int pnum, const struct ubi_vid_hdr *vid_hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) int len, err, second_is_newer, bitflips = 0, corrupted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) uint32_t data_crc, crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) struct ubi_vid_io_buf *vidb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (sqnum2 == aeb->sqnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * This must be a really ancient UBI image which has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * created before sequence numbers support has been added. At
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * that times we used 32-bit LEB versions stored in logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * eraseblocks. That was before UBI got into mainline. We do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * support these images anymore. Well, those images still work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * but only if no unclean reboots happened.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) ubi_err(ubi, "unsupported on-flash UBI format");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /* Obviously the LEB with lower sequence counter is older */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) second_is_newer = (sqnum2 > aeb->sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * Now we know which copy is newer. If the copy flag of the PEB with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * newer version is not set, then we just return, otherwise we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * check data CRC. For the second PEB we already have the VID header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * for the first one - we'll need to re-read it from flash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * Note: this may be optimized so that we wouldn't read twice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (second_is_newer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (!vid_hdr->copy_flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /* It is not a copy, so it is newer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) dbg_bld("second PEB %d is newer, copy_flag is unset",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (!aeb->copy_flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /* It is not a copy, so it is newer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) dbg_bld("first PEB %d is newer, copy_flag is unset",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) return bitflips << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (!vidb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) pnum = aeb->pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (err == UBI_IO_BITFLIPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) bitflips = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) ubi_err(ubi, "VID of PEB %d header is bad, but it was OK earlier, err %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) pnum, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) goto out_free_vidh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) vid_hdr = ubi_get_vid_hdr(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) /* Read the data of the copy and check the CRC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) len = be32_to_cpu(vid_hdr->data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) mutex_lock(&ubi->buf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) data_crc = be32_to_cpu(vid_hdr->data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (crc != data_crc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) pnum, crc, data_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) corrupted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) bitflips = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) second_is_newer = !second_is_newer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) dbg_bld("PEB %d CRC is OK", pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) bitflips |= !!err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) mutex_unlock(&ubi->buf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) ubi_free_vid_buf(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (second_is_newer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return second_is_newer | (bitflips << 1) | (corrupted << 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) mutex_unlock(&ubi->buf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) out_free_vidh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) ubi_free_vid_buf(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * ubi_add_to_av - add used physical eraseblock to the attaching information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * @pnum: the physical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * @ec: erase counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * @vid_hdr: the volume identifier header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * @bitflips: if bit-flips were detected when this physical eraseblock was read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * This function adds information about a used physical eraseblock to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * 'used' tree of the corresponding volume. The function is rather complex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * because it has to handle cases when this is not the first physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * eraseblock belonging to the same logical eraseblock, and the newer one has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * to be picked, while the older one has to be dropped. This function returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * zero in case of success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) int err, vol_id, lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) unsigned long long sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct ubi_ainf_volume *av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) struct ubi_ainf_peb *aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) struct rb_node **p, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) vol_id = be32_to_cpu(vid_hdr->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) lnum = be32_to_cpu(vid_hdr->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) sqnum = be64_to_cpu(vid_hdr->sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) pnum, vol_id, lnum, ec, sqnum, bitflips);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) av = add_volume(ai, vol_id, pnum, vid_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (IS_ERR(av))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) return PTR_ERR(av);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (ai->max_sqnum < sqnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) ai->max_sqnum = sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * if this is the first instance of this logical eraseblock or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) p = &av->root.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) int cmp_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (lnum != aeb->lnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (lnum < aeb->lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * There is already a physical eraseblock describing the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * logical eraseblock present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) dbg_bld("this LEB already exists: PEB %d, sqnum %llu, EC %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) aeb->pnum, aeb->sqnum, aeb->ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * Make sure that the logical eraseblocks have different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * sequence numbers. Otherwise the image is bad.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * However, if the sequence number is zero, we assume it must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * be an ancient UBI image from the era when UBI did not have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * sequence numbers. We still can attach these images, unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * there is a need to distinguish between old and new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) * eraseblocks, in which case we'll refuse the image in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * 'ubi_compare_lebs()'. In other words, we attach old clean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * images, but refuse attaching old images with duplicated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * logical eraseblocks because there was an unclean reboot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (aeb->sqnum == sqnum && sqnum != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) ubi_err(ubi, "two LEBs with same sequence number %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) ubi_dump_aeb(aeb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) ubi_dump_vid_hdr(vid_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) * Now we have to drop the older one and preserve the newer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) * one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) cmp_res = ubi_compare_lebs(ubi, aeb, pnum, vid_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (cmp_res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) return cmp_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (cmp_res & 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * This logical eraseblock is newer than the one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * found earlier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) err = validate_vid_hdr(ubi, vid_hdr, av, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) err = add_to_list(ai, aeb->pnum, aeb->vol_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) aeb->lnum, aeb->ec, cmp_res & 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) &ai->erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) aeb->ec = ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) aeb->pnum = pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) aeb->vol_id = vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) aeb->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) aeb->scrub = ((cmp_res & 2) || bitflips);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) aeb->copy_flag = vid_hdr->copy_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) aeb->sqnum = sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (av->highest_lnum == lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) av->last_data_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) be32_to_cpu(vid_hdr->data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) * This logical eraseblock is older than the one found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * previously.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) return add_to_list(ai, pnum, vol_id, lnum, ec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) cmp_res & 4, &ai->erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * We've met this logical eraseblock for the first time, add it to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) * attaching information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) err = validate_vid_hdr(ubi, vid_hdr, av, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) aeb = ubi_alloc_aeb(ai, pnum, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) if (!aeb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) aeb->vol_id = vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) aeb->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) aeb->scrub = bitflips;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) aeb->copy_flag = vid_hdr->copy_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) aeb->sqnum = sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (av->highest_lnum <= lnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) av->highest_lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) av->last_data_size = be32_to_cpu(vid_hdr->data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) av->leb_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) rb_link_node(&aeb->u.rb, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) rb_insert_color(&aeb->u.rb, &av->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * ubi_add_av - add volume to the attaching information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * @vol_id: the requested volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * This function returns a pointer to the new volume description or an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * ERR_PTR if the operation failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) struct ubi_ainf_volume *ubi_add_av(struct ubi_attach_info *ai, int vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) bool created;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) return find_or_add_av(ai, vol_id, AV_ADD, &created);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * ubi_find_av - find volume in the attaching information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * @vol_id: the requested volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * This function returns a pointer to the volume description or %NULL if there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * are no data about this volume in the attaching information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) int vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) bool created;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) return find_or_add_av((struct ubi_attach_info *)ai, vol_id, AV_FIND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) &created);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct list_head *list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) * ubi_remove_av - delete attaching information about a volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) * @av: the volume attaching information to delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) dbg_bld("remove attaching information about volume %d", av->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) rb_erase(&av->rb, &ai->volumes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) destroy_av(ai, av, &ai->erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) ai->vols_found -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) * early_erase_peb - erase a physical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) * @pnum: physical eraseblock number to erase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) * @ec: erase counter value to write (%UBI_UNKNOWN if it is unknown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) * This function erases physical eraseblock 'pnum', and writes the erase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * counter header to it. This function should only be used on UBI device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * initialization stages, when the EBA sub-system had not been yet initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) static int early_erase_peb(struct ubi_device *ubi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) const struct ubi_attach_info *ai, int pnum, int ec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) struct ubi_ec_hdr *ec_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) * Erase counter overflow. Upgrade UBI and use 64-bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) * erase counters internally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) ubi_err(ubi, "erase counter overflow at PEB %d, EC %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) pnum, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (!ec_hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) ec_hdr->ec = cpu_to_be64(ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) err = ubi_io_sync_erase(ubi, pnum, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) kfree(ec_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * ubi_early_get_peb - get a free physical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * This function returns a free physical eraseblock. It is supposed to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) * called on the UBI initialization stages when the wear-leveling sub-system is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) * not initialized yet. This function picks a physical eraseblocks from one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) * the lists, writes the EC header if it is needed, and removes it from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) * list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) * This function returns a pointer to the "aeb" of the found free PEB in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) * of success and an error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) struct ubi_attach_info *ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) struct ubi_ainf_peb *aeb, *tmp_aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) if (!list_empty(&ai->free)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) aeb = list_entry(ai->free.next, struct ubi_ainf_peb, u.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) list_del(&aeb->u.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) return aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * We try to erase the first physical eraseblock from the erase list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * and pick it if we succeed, or try to erase the next one if not. And
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * so forth. We don't want to take care about bad eraseblocks here -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * they'll be handled later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) list_for_each_entry_safe(aeb, tmp_aeb, &ai->erase, u.list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (aeb->ec == UBI_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) aeb->ec = ai->mean_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) aeb->ec += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) list_del(&aeb->u.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) return aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) ubi_err(ubi, "no free eraseblocks");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) return ERR_PTR(-ENOSPC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * check_corruption - check the data area of PEB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * @vid_hdr: the (corrupted) VID header of this PEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) * @pnum: the physical eraseblock number to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) * This is a helper function which is used to distinguish between VID header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) * corruptions caused by power cuts and other reasons. If the PEB contains only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * 0xFF bytes in the data area, the VID header is most probably corrupted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * because of a power cut (%0 is returned in this case). Otherwise, it was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * probably corrupted for some other reasons (%1 is returned in this case). A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) * negative error code is returned if a read error occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) * If the corruption reason was a power cut, UBI can safely erase this PEB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) * Otherwise, it should preserve it to avoid possibly destroying important
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) * information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) int pnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) mutex_lock(&ubi->buf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) memset(ubi->peb_buf, 0x00, ubi->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) ubi->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) * Bit-flips or integrity errors while reading the data area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) * It is difficult to say for sure what type of corruption is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * this, but presumably a power cut happened while this PEB was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * erased, so it became unstable and corrupted, and should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * erased.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) ubi_err(ubi, "PEB %d contains corrupted VID header, and the data does not contain all 0xFF",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) ubi_err(ubi, "this may be a non-UBI PEB or a severe VID header corruption which requires manual inspection");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) ubi_dump_vid_hdr(vid_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) pr_err("hexdump of PEB %d offset %d, length %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) pnum, ubi->leb_start, ubi->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) ubi->peb_buf, ubi->leb_size, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) err = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) mutex_unlock(&ubi->buf_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) static bool vol_ignored(int vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) switch (vol_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) case UBI_LAYOUT_VOLUME_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) #ifdef CONFIG_MTD_UBI_FASTMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) return ubi_is_fm_vol(vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) }
^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) * scan_peb - scan and process UBI headers of a PEB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) * @pnum: the physical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * @fast: true if we're scanning for a Fastmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) * This function reads UBI headers of PEB @pnum, checks them, and adds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) * information about this PEB to the corresponding list or RB-tree in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) * "attaching info" structure. Returns zero if the physical eraseblock was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * successfully handled and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) int pnum, bool fast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) struct ubi_ec_hdr *ech = ai->ech;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) struct ubi_vid_io_buf *vidb = ai->vidb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) struct ubi_vid_hdr *vidh = ubi_get_vid_hdr(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) long long ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) int err, bitflips = 0, vol_id = -1, ec_err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) dbg_bld("scan PEB %d", pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) /* Skip bad physical eraseblocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) err = ubi_io_is_bad(ubi, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) else if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) ai->bad_peb_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) switch (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) case UBI_IO_BITFLIPS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) bitflips = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) case UBI_IO_FF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) ai->empty_peb_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) UBI_UNKNOWN, 0, &ai->erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) case UBI_IO_FF_BITFLIPS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) ai->empty_peb_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) UBI_UNKNOWN, 1, &ai->erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) case UBI_IO_BAD_HDR_EBADMSG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) case UBI_IO_BAD_HDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * We have to also look at the VID header, possibly it is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) * corrupted. Set %bitflips flag in order to make this PEB be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) * moved and EC be re-created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) ec_err = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) ec = UBI_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) bitflips = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) ubi_err(ubi, "'ubi_io_read_ec_hdr()' returned unknown code %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) if (!ec_err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) int image_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) /* Make sure UBI version is OK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) if (ech->version != UBI_VERSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) ubi_err(ubi, "this UBI version is %d, image version is %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) UBI_VERSION, (int)ech->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) ec = be64_to_cpu(ech->ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) if (ec > UBI_MAX_ERASECOUNTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * Erase counter overflow. The EC headers have 64 bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * reserved, but we anyway make use of only 31 bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) * values, as this seems to be enough for any existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) * flash. Upgrade UBI and use 64-bit erase counters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) * internally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) ubi_err(ubi, "erase counter overflow, max is %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) UBI_MAX_ERASECOUNTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) ubi_dump_ec_hdr(ech);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) * Make sure that all PEBs have the same image sequence number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) * This allows us to detect situations when users flash UBI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) * images incorrectly, so that the flash has the new UBI image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * and leftovers from the old one. This feature was added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) * relatively recently, and the sequence number was always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * zero, because old UBI implementations always set it to zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) * For this reasons, we do not panic if some PEBs have zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * sequence number, while other PEBs have non-zero sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) * number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) image_seq = be32_to_cpu(ech->image_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) if (!ubi->image_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) ubi->image_seq = image_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) if (image_seq && ubi->image_seq != image_seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) ubi_err(ubi, "bad image sequence number %d in PEB %d, expected %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) image_seq, pnum, ubi->image_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) ubi_dump_ec_hdr(ech);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) return -EINVAL;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) /* OK, we've done with the EC header, let's look at the VID header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) switch (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) case UBI_IO_BITFLIPS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) bitflips = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) case UBI_IO_BAD_HDR_EBADMSG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) * Both EC and VID headers are corrupted and were read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) * with data integrity error, probably this is a bad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) * PEB, bit it is not marked as bad yet. This may also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) * be a result of power cut during erasure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) ai->maybe_bad_peb_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) case UBI_IO_BAD_HDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) * If we're facing a bad VID header we have to drop *all*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) * Fastmap data structures we find. The most recent Fastmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) * could be bad and therefore there is a chance that we attach
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) * from an old one. On a fine MTD stack a PEB must not render
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) * bad all of a sudden, but the reality is different.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) * So, let's be paranoid and help finding the root cause by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) * falling back to scanning mode instead of attaching with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) * bad EBA table and cause data corruption which is hard to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) * analyze.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) if (fast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) ai->force_full_scan = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) if (ec_err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) * Both headers are corrupted. There is a possibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) * that this a valid UBI PEB which has corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) * LEB, but the headers are corrupted. However, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) * impossible to distinguish it from a PEB which just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) * contains garbage because of a power cut during erase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) * operation. So we just schedule this PEB for erasure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) * Besides, in case of NOR flash, we deliberately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) * corrupt both headers because NOR flash erasure is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) * slow and can start from the end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) * The EC was OK, but the VID header is corrupted. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) * have to check what is in the data area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) err = check_corruption(ubi, vidh, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) else if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) /* This corruption is caused by a power cut */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) err = add_to_list(ai, pnum, UBI_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) UBI_UNKNOWN, ec, 1, &ai->erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) /* This is an unexpected corruption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) err = add_corrupted(ai, pnum, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) goto adjust_mean_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) case UBI_IO_FF_BITFLIPS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) err = add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) ec, 1, &ai->erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) goto adjust_mean_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) case UBI_IO_FF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) if (ec_err || bitflips)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) err = add_to_list(ai, pnum, UBI_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) UBI_UNKNOWN, ec, 1, &ai->erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) err = add_to_list(ai, pnum, UBI_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) UBI_UNKNOWN, ec, 0, &ai->free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) goto adjust_mean_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) ubi_err(ubi, "'ubi_io_read_vid_hdr()' returned unknown code %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) vol_id = be32_to_cpu(vidh->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) if (vol_id > UBI_MAX_VOLUMES && !vol_ignored(vol_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) int lnum = be32_to_cpu(vidh->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) /* Unsupported internal volume */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) switch (vidh->compat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) case UBI_COMPAT_DELETE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) ubi_msg(ubi, "\"delete\" compatible internal volume %d:%d found, will remove it",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) err = add_to_list(ai, pnum, vol_id, lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) ec, 1, &ai->erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) case UBI_COMPAT_RO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) ubi_msg(ubi, "read-only compatible internal volume %d:%d found, switch to read-only mode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) ubi->ro_mode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) case UBI_COMPAT_PRESERVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) ubi_msg(ubi, "\"preserve\" compatible internal volume %d:%d found",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) err = add_to_list(ai, pnum, vol_id, lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) ec, 0, &ai->alien);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) case UBI_COMPAT_REJECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) ubi_err(ubi, "incompatible internal volume %d:%d found",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) vol_id, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) if (ec_err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) ubi_warn(ubi, "valid VID header but corrupted EC header at PEB %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) if (ubi_is_fm_vol(vol_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) err = add_fastmap(ai, pnum, vidh, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) err = ubi_add_to_av(ubi, ai, pnum, ec, vidh, bitflips);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) adjust_mean_ec:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) if (!ec_err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) ai->ec_sum += ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) ai->ec_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) if (ec > ai->max_ec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) ai->max_ec = ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) if (ec < ai->min_ec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) ai->min_ec = ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) * late_analysis - analyze the overall situation with PEB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) * This is a helper function which takes a look what PEBs we have after we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) * gather information about all of them ("ai" is compete). It decides whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) * the flash is empty and should be formatted of whether there are too many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) * corrupted PEBs and we should not attach this MTD device. Returns zero if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) * should proceed with attaching the MTD device, and %-EINVAL if we should not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) static int late_analysis(struct ubi_device *ubi, struct ubi_attach_info *ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) struct ubi_ainf_peb *aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) int max_corr, peb_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) max_corr = peb_count / 20 ?: 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) * Few corrupted PEBs is not a problem and may be just a result of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) * unclean reboots. However, many of them may indicate some problems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) * with the flash HW or driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) if (ai->corr_peb_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) ubi_err(ubi, "%d PEBs are corrupted and preserved",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) ai->corr_peb_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) pr_err("Corrupted PEBs are:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) list_for_each_entry(aeb, &ai->corr, u.list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) pr_cont(" %d", aeb->pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) pr_cont("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) * If too many PEBs are corrupted, we refuse attaching,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) * otherwise, only print a warning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) if (ai->corr_peb_count >= max_corr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) ubi_err(ubi, "too many corrupted PEBs, refusing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) if (ai->empty_peb_count + ai->maybe_bad_peb_count == peb_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) * All PEBs are empty, or almost all - a couple PEBs look like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) * they may be bad PEBs which were not marked as bad yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) * This piece of code basically tries to distinguish between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) * the following situations:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) * 1. Flash is empty, but there are few bad PEBs, which are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) * marked as bad so far, and which were read with error. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) * want to go ahead and format this flash. While formatting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) * the faulty PEBs will probably be marked as bad.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) * 2. Flash contains non-UBI data and we do not want to format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) * it and destroy possibly important information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) if (ai->maybe_bad_peb_count <= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) ai->is_empty = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) ubi_msg(ubi, "empty MTD device detected");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) get_random_bytes(&ubi->image_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) sizeof(ubi->image_seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) ubi_err(ubi, "MTD device is not UBI-formatted and possibly contains non-UBI data - refusing it");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) * destroy_av - free volume attaching information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) * @av: volume attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) * @list: put the aeb elements in there if !NULL, otherwise free them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) * This function destroys the volume attaching information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) struct ubi_ainf_peb *aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) struct rb_node *this = av->root.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) while (this) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) if (this->rb_left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) this = this->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) else if (this->rb_right)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) this = this->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) aeb = rb_entry(this, struct ubi_ainf_peb, u.rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) this = rb_parent(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) if (this) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) if (this->rb_left == &aeb->u.rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) this->rb_left = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) this->rb_right = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) if (list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) list_add_tail(&aeb->u.list, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) ubi_free_aeb(ai, aeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) kfree(av);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) * destroy_ai - destroy attaching information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) static void destroy_ai(struct ubi_attach_info *ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) struct ubi_ainf_peb *aeb, *aeb_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) struct ubi_ainf_volume *av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) struct rb_node *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) list_del(&aeb->u.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) ubi_free_aeb(ai, aeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) list_del(&aeb->u.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) ubi_free_aeb(ai, aeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) list_del(&aeb->u.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) ubi_free_aeb(ai, aeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) list_del(&aeb->u.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) ubi_free_aeb(ai, aeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) list_for_each_entry_safe(aeb, aeb_tmp, &ai->fastmap, u.list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) list_del(&aeb->u.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) ubi_free_aeb(ai, aeb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) /* Destroy the volume RB-tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) rb = ai->volumes.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) while (rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) if (rb->rb_left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) rb = rb->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) else if (rb->rb_right)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) rb = rb->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) av = rb_entry(rb, struct ubi_ainf_volume, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) rb = rb_parent(rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) if (rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) if (rb->rb_left == &av->rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) rb->rb_left = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) rb->rb_right = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) destroy_av(ai, av, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) kmem_cache_destroy(ai->aeb_slab_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) kfree(ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) * scan_all - scan entire MTD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) * @ai: attach info object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) * @start: start scanning at this PEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) * This function does full scanning of an MTD device and returns complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) * information about it in form of a "struct ubi_attach_info" object. In case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) * of failure, an error code is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) static int scan_all(struct ubi_device *ubi, struct ubi_attach_info *ai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) int start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) int err, pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) struct rb_node *rb1, *rb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) struct ubi_ainf_volume *av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) struct ubi_ainf_peb *aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) if (!ai->ech)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) if (!ai->vidb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) goto out_ech;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) for (pnum = start; pnum < ubi->peb_count; pnum++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) dbg_gen("process PEB %d", pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) err = scan_peb(ubi, ai, pnum, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) goto out_vidh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) ubi_msg(ubi, "scanning is finished");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) /* Calculate mean erase counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) if (ai->ec_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) err = late_analysis(ubi, ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) goto out_vidh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) * In case of unknown erase counter we use the mean erase counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) * value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) if (aeb->ec == UBI_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) aeb->ec = ai->mean_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) list_for_each_entry(aeb, &ai->free, u.list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) if (aeb->ec == UBI_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) aeb->ec = ai->mean_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) list_for_each_entry(aeb, &ai->corr, u.list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) if (aeb->ec == UBI_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) aeb->ec = ai->mean_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) list_for_each_entry(aeb, &ai->erase, u.list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) if (aeb->ec == UBI_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) aeb->ec = ai->mean_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) err = self_check_ai(ubi, ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) goto out_vidh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) ubi_free_vid_buf(ai->vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) kfree(ai->ech);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) out_vidh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) ubi_free_vid_buf(ai->vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) out_ech:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) kfree(ai->ech);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) return err;
^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) static struct ubi_attach_info *alloc_ai(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) struct ubi_attach_info *ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) ai = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) if (!ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) return ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) INIT_LIST_HEAD(&ai->corr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) INIT_LIST_HEAD(&ai->free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) INIT_LIST_HEAD(&ai->erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) INIT_LIST_HEAD(&ai->alien);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) INIT_LIST_HEAD(&ai->fastmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) ai->volumes = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) ai->aeb_slab_cache = kmem_cache_create("ubi_aeb_slab_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) sizeof(struct ubi_ainf_peb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) if (!ai->aeb_slab_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) kfree(ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) ai = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) return ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) #ifdef CONFIG_MTD_UBI_FASTMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) * scan_fast - try to find a fastmap and attach from it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) * @ai: attach info object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) * Returns 0 on success, negative return values indicate an internal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) * error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) * UBI_NO_FASTMAP denotes that no fastmap was found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) * UBI_BAD_FASTMAP denotes that the found fastmap was invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) static int scan_fast(struct ubi_device *ubi, struct ubi_attach_info **ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) int err, pnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) struct ubi_attach_info *scan_ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) scan_ai = alloc_ai();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) if (!scan_ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) scan_ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) if (!scan_ai->ech)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) goto out_ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) scan_ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) if (!scan_ai->vidb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) goto out_ech;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) dbg_gen("process PEB %d", pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) err = scan_peb(ubi, scan_ai, pnum, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) goto out_vidh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) ubi_free_vid_buf(scan_ai->vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) kfree(scan_ai->ech);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) if (scan_ai->force_full_scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) err = UBI_NO_FASTMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) err = ubi_scan_fastmap(ubi, *ai, scan_ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) * Didn't attach via fastmap, do a full scan but reuse what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) * we've aready scanned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) destroy_ai(*ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) *ai = scan_ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) destroy_ai(scan_ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) out_vidh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) ubi_free_vid_buf(scan_ai->vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) out_ech:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) kfree(scan_ai->ech);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) out_ai:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) destroy_ai(scan_ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) * ubi_attach - attach an MTD device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) * @ubi: UBI device descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) * @force_scan: if set to non-zero attach by scanning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) int ubi_attach(struct ubi_device *ubi, int force_scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) struct ubi_attach_info *ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) ai = alloc_ai();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) if (!ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) #ifdef CONFIG_MTD_UBI_FASTMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) /* On small flash devices we disable fastmap in any case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) if ((int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd) <= UBI_FM_MAX_START) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) ubi->fm_disabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) force_scan = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) if (force_scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) err = scan_all(ubi, ai, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) err = scan_fast(ubi, &ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) if (err > 0 || mtd_is_eccerr(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) if (err != UBI_NO_FASTMAP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) destroy_ai(ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) ai = alloc_ai();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) if (!ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) err = scan_all(ubi, ai, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) err = scan_all(ubi, ai, UBI_FM_MAX_START);
^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) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) err = scan_all(ubi, ai, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) goto out_ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) ubi->bad_peb_count = ai->bad_peb_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) ubi->corr_peb_count = ai->corr_peb_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) ubi->max_ec = ai->max_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) ubi->mean_ec = ai->mean_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) dbg_gen("max. sequence number: %llu", ai->max_sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) err = ubi_read_volume_table(ubi, ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) goto out_ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) err = ubi_wl_init(ubi, ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) goto out_vtbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) err = ubi_eba_init(ubi, ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) goto out_wl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) #ifdef CONFIG_MTD_UBI_FASTMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) if (ubi->fm && ubi_dbg_chk_fastmap(ubi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) struct ubi_attach_info *scan_ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) scan_ai = alloc_ai();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) if (!scan_ai) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) goto out_wl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) err = scan_all(ubi, scan_ai, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) destroy_ai(scan_ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) goto out_wl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) err = self_check_eba(ubi, ai, scan_ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) destroy_ai(scan_ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) goto out_wl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) destroy_ai(ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) out_wl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) ubi_wl_close(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) out_vtbl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) ubi_free_all_volumes(ubi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) vfree(ubi->vtbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) out_ai:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) destroy_ai(ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) * self_check_ai - check the attaching information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) * @ubi: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) * @ai: attaching information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) * This function returns zero if the attaching information is all right, and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) * negative error code if not or if an error occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) struct ubi_vid_io_buf *vidb = ai->vidb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) struct ubi_vid_hdr *vidh = ubi_get_vid_hdr(vidb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) int pnum, err, vols_found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) struct rb_node *rb1, *rb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) struct ubi_ainf_volume *av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) struct ubi_ainf_peb *aeb, *last_aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) uint8_t *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) if (!ubi_dbg_chk_gen(ubi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) * At first, check that attaching information is OK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) int leb_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) vols_found += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) if (ai->is_empty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) ubi_err(ubi, "bad is_empty flag");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) goto bad_av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) if (av->vol_id < 0 || av->highest_lnum < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) av->data_pad < 0 || av->last_data_size < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) ubi_err(ubi, "negative values");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) goto bad_av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) if (av->vol_id >= UBI_MAX_VOLUMES &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) av->vol_id < UBI_INTERNAL_VOL_START) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) ubi_err(ubi, "bad vol_id");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) goto bad_av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) if (av->vol_id > ai->highest_vol_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) ubi_err(ubi, "highest_vol_id is %d, but vol_id %d is there",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) ai->highest_vol_id, av->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) if (av->vol_type != UBI_DYNAMIC_VOLUME &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) av->vol_type != UBI_STATIC_VOLUME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) ubi_err(ubi, "bad vol_type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) goto bad_av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) if (av->data_pad > ubi->leb_size / 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) ubi_err(ubi, "bad data_pad");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) goto bad_av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) last_aeb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) last_aeb = aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) leb_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) if (aeb->pnum < 0 || aeb->ec < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) ubi_err(ubi, "negative values");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) goto bad_aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) if (aeb->ec < ai->min_ec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) ubi_err(ubi, "bad ai->min_ec (%d), %d found",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) ai->min_ec, aeb->ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) goto bad_aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) if (aeb->ec > ai->max_ec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) ubi_err(ubi, "bad ai->max_ec (%d), %d found",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) ai->max_ec, aeb->ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) goto bad_aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) if (aeb->pnum >= ubi->peb_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) ubi_err(ubi, "too high PEB number %d, total PEBs %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) aeb->pnum, ubi->peb_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) goto bad_aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) if (av->vol_type == UBI_STATIC_VOLUME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) if (aeb->lnum >= av->used_ebs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) ubi_err(ubi, "bad lnum or used_ebs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) goto bad_aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) if (av->used_ebs != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) ubi_err(ubi, "non-zero used_ebs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) goto bad_aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) if (aeb->lnum > av->highest_lnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) ubi_err(ubi, "incorrect highest_lnum or lnum");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) goto bad_aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) if (av->leb_count != leb_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) ubi_err(ubi, "bad leb_count, %d objects in the tree",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) leb_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) goto bad_av;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) if (!last_aeb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) aeb = last_aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) if (aeb->lnum != av->highest_lnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) ubi_err(ubi, "bad highest_lnum");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) goto bad_aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) if (vols_found != ai->vols_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) ubi_err(ubi, "bad ai->vols_found %d, should be %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) ai->vols_found, vols_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) /* Check that attaching information is correct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) last_aeb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) int vol_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) last_aeb = aeb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) if (err && err != UBI_IO_BITFLIPS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) ubi_err(ubi, "VID header is not OK (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) if (av->vol_type != vol_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) ubi_err(ubi, "bad vol_type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) goto bad_vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) ubi_err(ubi, "bad sqnum %llu", aeb->sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) goto bad_vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) if (av->vol_id != be32_to_cpu(vidh->vol_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) ubi_err(ubi, "bad vol_id %d", av->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) goto bad_vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) if (av->compat != vidh->compat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) ubi_err(ubi, "bad compat %d", vidh->compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) goto bad_vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) if (aeb->lnum != be32_to_cpu(vidh->lnum)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) ubi_err(ubi, "bad lnum %d", aeb->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) goto bad_vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) ubi_err(ubi, "bad used_ebs %d", av->used_ebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) goto bad_vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) if (av->data_pad != be32_to_cpu(vidh->data_pad)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) ubi_err(ubi, "bad data_pad %d", av->data_pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) goto bad_vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) if (!last_aeb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) if (av->highest_lnum != be32_to_cpu(vidh->lnum)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) ubi_err(ubi, "bad highest_lnum %d", av->highest_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) goto bad_vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) if (av->last_data_size != be32_to_cpu(vidh->data_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) ubi_err(ubi, "bad last_data_size %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) av->last_data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) goto bad_vid_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) * Make sure that all the physical eraseblocks are in one of the lists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) * or trees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) buf = kzalloc(ubi->peb_count, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) for (pnum = 0; pnum < ubi->peb_count; pnum++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) err = ubi_io_is_bad(ubi, pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) } else if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) buf[pnum] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) buf[aeb->pnum] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) list_for_each_entry(aeb, &ai->free, u.list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) buf[aeb->pnum] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) list_for_each_entry(aeb, &ai->corr, u.list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) buf[aeb->pnum] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) list_for_each_entry(aeb, &ai->erase, u.list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) buf[aeb->pnum] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) list_for_each_entry(aeb, &ai->alien, u.list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) buf[aeb->pnum] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) for (pnum = 0; pnum < ubi->peb_count; pnum++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) if (!buf[pnum]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) ubi_err(ubi, "PEB %d is not referred", pnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) err = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) bad_aeb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) ubi_err(ubi, "bad attaching information about LEB %d", aeb->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) ubi_dump_aeb(aeb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) ubi_dump_av(av);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) bad_av:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) ubi_err(ubi, "bad attaching information about volume %d", av->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) ubi_dump_av(av);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) bad_vid_hdr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) ubi_err(ubi, "bad attaching information about volume %d", av->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) ubi_dump_av(av);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) ubi_dump_vid_hdr(vidh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) }