11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ae4a8104SArtem Bityutskiy /*
3ae4a8104SArtem Bityutskiy * Copyright (c) International Business Machines Corp., 2006
4ae4a8104SArtem Bityutskiy *
5ae4a8104SArtem Bityutskiy * Author: Artem Bityutskiy (Битюцкий Артём)
6ae4a8104SArtem Bityutskiy */
7ae4a8104SArtem Bityutskiy
8ae4a8104SArtem Bityutskiy /*
9ae4a8104SArtem Bityutskiy * UBI attaching sub-system.
10ae4a8104SArtem Bityutskiy *
11ae4a8104SArtem Bityutskiy * This sub-system is responsible for attaching MTD devices and it also
12ae4a8104SArtem Bityutskiy * implements flash media scanning.
13ae4a8104SArtem Bityutskiy *
14ae4a8104SArtem Bityutskiy * The attaching information is represented by a &struct ubi_attach_info'
15ae4a8104SArtem Bityutskiy * object. Information about volumes is represented by &struct ubi_ainf_volume
16ae4a8104SArtem Bityutskiy * objects which are kept in volume RB-tree with root at the @volumes field.
17ae4a8104SArtem Bityutskiy * The RB-tree is indexed by the volume ID.
18ae4a8104SArtem Bityutskiy *
19ae4a8104SArtem Bityutskiy * Logical eraseblocks are represented by &struct ubi_ainf_peb objects. These
20ae4a8104SArtem Bityutskiy * objects are kept in per-volume RB-trees with the root at the corresponding
21ae4a8104SArtem Bityutskiy * &struct ubi_ainf_volume object. To put it differently, we keep an RB-tree of
22ae4a8104SArtem Bityutskiy * per-volume objects and each of these objects is the root of RB-tree of
23ae4a8104SArtem Bityutskiy * per-LEB objects.
24ae4a8104SArtem Bityutskiy *
25ae4a8104SArtem Bityutskiy * Corrupted physical eraseblocks are put to the @corr list, free physical
26ae4a8104SArtem Bityutskiy * eraseblocks are put to the @free list and the physical eraseblock to be
27ae4a8104SArtem Bityutskiy * erased are put to the @erase list.
28ae4a8104SArtem Bityutskiy *
29ae4a8104SArtem Bityutskiy * About corruptions
30ae4a8104SArtem Bityutskiy * ~~~~~~~~~~~~~~~~~
31ae4a8104SArtem Bityutskiy *
32ae4a8104SArtem Bityutskiy * UBI protects EC and VID headers with CRC-32 checksums, so it can detect
33ae4a8104SArtem Bityutskiy * whether the headers are corrupted or not. Sometimes UBI also protects the
34ae4a8104SArtem Bityutskiy * data with CRC-32, e.g., when it executes the atomic LEB change operation, or
35ae4a8104SArtem Bityutskiy * when it moves the contents of a PEB for wear-leveling purposes.
36ae4a8104SArtem Bityutskiy *
37ae4a8104SArtem Bityutskiy * UBI tries to distinguish between 2 types of corruptions.
38ae4a8104SArtem Bityutskiy *
39ae4a8104SArtem Bityutskiy * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
40ae4a8104SArtem Bityutskiy * tries to handle them gracefully, without printing too many warnings and
41ae4a8104SArtem Bityutskiy * error messages. The idea is that we do not lose important data in these
42ae4a8104SArtem Bityutskiy * cases - we may lose only the data which were being written to the media just
43ae4a8104SArtem Bityutskiy * before the power cut happened, and the upper layers (e.g., UBIFS) are
44ae4a8104SArtem Bityutskiy * supposed to handle such data losses (e.g., by using the FS journal).
45ae4a8104SArtem Bityutskiy *
46ae4a8104SArtem Bityutskiy * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
47ae4a8104SArtem Bityutskiy * the reason is a power cut, UBI puts this PEB to the @erase list, and all
48ae4a8104SArtem Bityutskiy * PEBs in the @erase list are scheduled for erasure later.
49ae4a8104SArtem Bityutskiy *
50ae4a8104SArtem Bityutskiy * 2. Unexpected corruptions which are not caused by power cuts. During
51ae4a8104SArtem Bityutskiy * attaching, such PEBs are put to the @corr list and UBI preserves them.
52ae4a8104SArtem Bityutskiy * Obviously, this lessens the amount of available PEBs, and if at some point
53ae4a8104SArtem Bityutskiy * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
54ae4a8104SArtem Bityutskiy * about such PEBs every time the MTD device is attached.
55ae4a8104SArtem Bityutskiy *
56ae4a8104SArtem Bityutskiy * However, it is difficult to reliably distinguish between these types of
57ae4a8104SArtem Bityutskiy * corruptions and UBI's strategy is as follows (in case of attaching by
58ae4a8104SArtem Bityutskiy * scanning). UBI assumes corruption type 2 if the VID header is corrupted and
59ae4a8104SArtem Bityutskiy * the data area does not contain all 0xFFs, and there were no bit-flips or
60ae4a8104SArtem Bityutskiy * integrity errors (e.g., ECC errors in case of NAND) while reading the data
61ae4a8104SArtem Bityutskiy * area. Otherwise UBI assumes corruption type 1. So the decision criteria
62ae4a8104SArtem Bityutskiy * are as follows.
63ae4a8104SArtem Bityutskiy * o If the data area contains only 0xFFs, there are no data, and it is safe
64ae4a8104SArtem Bityutskiy * to just erase this PEB - this is corruption type 1.
65ae4a8104SArtem Bityutskiy * o If the data area has bit-flips or data integrity errors (ECC errors on
66ae4a8104SArtem Bityutskiy * NAND), it is probably a PEB which was being erased when power cut
67ae4a8104SArtem Bityutskiy * happened, so this is corruption type 1. However, this is just a guess,
68ae4a8104SArtem Bityutskiy * which might be wrong.
6955393ba1SBrian Norris * o Otherwise this is corruption type 2.
70ae4a8104SArtem Bityutskiy */
71ae4a8104SArtem Bityutskiy
72ae4a8104SArtem Bityutskiy #include <linux/err.h>
73ae4a8104SArtem Bityutskiy #include <linux/slab.h>
74ae4a8104SArtem Bityutskiy #include <linux/crc32.h>
75ae4a8104SArtem Bityutskiy #include <linux/math64.h>
76ae4a8104SArtem Bityutskiy #include <linux/random.h>
77ae4a8104SArtem Bityutskiy #include "ubi.h"
78ae4a8104SArtem Bityutskiy
79ae4a8104SArtem Bityutskiy static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai);
80ae4a8104SArtem Bityutskiy
81de4c455bSBoris Brezillon #define AV_FIND BIT(0)
82de4c455bSBoris Brezillon #define AV_ADD BIT(1)
83de4c455bSBoris Brezillon #define AV_FIND_OR_ADD (AV_FIND | AV_ADD)
84de4c455bSBoris Brezillon
85de4c455bSBoris Brezillon /**
86de4c455bSBoris Brezillon * find_or_add_av - internal function to find a volume, add a volume or do
87de4c455bSBoris Brezillon * both (find and add if missing).
88de4c455bSBoris Brezillon * @ai: attaching information
89de4c455bSBoris Brezillon * @vol_id: the requested volume ID
90de4c455bSBoris Brezillon * @flags: a combination of the %AV_FIND and %AV_ADD flags describing the
91de4c455bSBoris Brezillon * expected operation. If only %AV_ADD is set, -EEXIST is returned
92de4c455bSBoris Brezillon * if the volume already exists. If only %AV_FIND is set, NULL is
93de4c455bSBoris Brezillon * returned if the volume does not exist. And if both flags are
94de4c455bSBoris Brezillon * set, the helper first tries to find an existing volume, and if
95de4c455bSBoris Brezillon * it does not exist it creates a new one.
96de4c455bSBoris Brezillon * @created: in value used to inform the caller whether it"s a newly created
97de4c455bSBoris Brezillon * volume or not.
98de4c455bSBoris Brezillon *
99de4c455bSBoris Brezillon * This function returns a pointer to a volume description or an ERR_PTR if
100de4c455bSBoris Brezillon * the operation failed. It can also return NULL if only %AV_FIND is set and
101de4c455bSBoris Brezillon * the volume does not exist.
102de4c455bSBoris Brezillon */
find_or_add_av(struct ubi_attach_info * ai,int vol_id,unsigned int flags,bool * created)103de4c455bSBoris Brezillon static struct ubi_ainf_volume *find_or_add_av(struct ubi_attach_info *ai,
104de4c455bSBoris Brezillon int vol_id, unsigned int flags,
105de4c455bSBoris Brezillon bool *created)
106de4c455bSBoris Brezillon {
107de4c455bSBoris Brezillon struct ubi_ainf_volume *av;
108de4c455bSBoris Brezillon struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
109de4c455bSBoris Brezillon
110de4c455bSBoris Brezillon /* Walk the volume RB-tree to look if this volume is already present */
111de4c455bSBoris Brezillon while (*p) {
112de4c455bSBoris Brezillon parent = *p;
113de4c455bSBoris Brezillon av = rb_entry(parent, struct ubi_ainf_volume, rb);
114de4c455bSBoris Brezillon
115de4c455bSBoris Brezillon if (vol_id == av->vol_id) {
116de4c455bSBoris Brezillon *created = false;
117de4c455bSBoris Brezillon
118de4c455bSBoris Brezillon if (!(flags & AV_FIND))
119de4c455bSBoris Brezillon return ERR_PTR(-EEXIST);
120de4c455bSBoris Brezillon
121de4c455bSBoris Brezillon return av;
122de4c455bSBoris Brezillon }
123de4c455bSBoris Brezillon
124de4c455bSBoris Brezillon if (vol_id > av->vol_id)
125de4c455bSBoris Brezillon p = &(*p)->rb_left;
126de4c455bSBoris Brezillon else
127de4c455bSBoris Brezillon p = &(*p)->rb_right;
128de4c455bSBoris Brezillon }
129de4c455bSBoris Brezillon
130de4c455bSBoris Brezillon if (!(flags & AV_ADD))
131de4c455bSBoris Brezillon return NULL;
132de4c455bSBoris Brezillon
133de4c455bSBoris Brezillon /* The volume is absent - add it */
134de4c455bSBoris Brezillon av = kzalloc(sizeof(*av), GFP_KERNEL);
135de4c455bSBoris Brezillon if (!av)
136de4c455bSBoris Brezillon return ERR_PTR(-ENOMEM);
137de4c455bSBoris Brezillon
138de4c455bSBoris Brezillon av->vol_id = vol_id;
139de4c455bSBoris Brezillon
140de4c455bSBoris Brezillon if (vol_id > ai->highest_vol_id)
141de4c455bSBoris Brezillon ai->highest_vol_id = vol_id;
142de4c455bSBoris Brezillon
143de4c455bSBoris Brezillon rb_link_node(&av->rb, parent, p);
144de4c455bSBoris Brezillon rb_insert_color(&av->rb, &ai->volumes);
145de4c455bSBoris Brezillon ai->vols_found += 1;
146de4c455bSBoris Brezillon *created = true;
147de4c455bSBoris Brezillon dbg_bld("added volume %d", vol_id);
148de4c455bSBoris Brezillon return av;
149de4c455bSBoris Brezillon }
150de4c455bSBoris Brezillon
151de4c455bSBoris Brezillon /**
152de4c455bSBoris Brezillon * ubi_find_or_add_av - search for a volume in the attaching information and
153de4c455bSBoris Brezillon * add one if it does not exist.
154de4c455bSBoris Brezillon * @ai: attaching information
155de4c455bSBoris Brezillon * @vol_id: the requested volume ID
156de4c455bSBoris Brezillon * @created: whether the volume has been created or not
157de4c455bSBoris Brezillon *
158de4c455bSBoris Brezillon * This function returns a pointer to the new volume description or an
159de4c455bSBoris Brezillon * ERR_PTR if the operation failed.
160de4c455bSBoris Brezillon */
ubi_find_or_add_av(struct ubi_attach_info * ai,int vol_id,bool * created)161de4c455bSBoris Brezillon static struct ubi_ainf_volume *ubi_find_or_add_av(struct ubi_attach_info *ai,
162de4c455bSBoris Brezillon int vol_id, bool *created)
163de4c455bSBoris Brezillon {
164de4c455bSBoris Brezillon return find_or_add_av(ai, vol_id, AV_FIND_OR_ADD, created);
165de4c455bSBoris Brezillon }
166de4c455bSBoris Brezillon
167ae4a8104SArtem Bityutskiy /**
16891f4285fSBoris Brezillon * ubi_alloc_aeb - allocate an aeb element
16991f4285fSBoris Brezillon * @ai: attaching information
17091f4285fSBoris Brezillon * @pnum: physical eraseblock number
17191f4285fSBoris Brezillon * @ec: erase counter of the physical eraseblock
17291f4285fSBoris Brezillon *
17391f4285fSBoris Brezillon * Allocate an aeb object and initialize the pnum and ec information.
17491f4285fSBoris Brezillon * vol_id and lnum are set to UBI_UNKNOWN, and the other fields are
17591f4285fSBoris Brezillon * initialized to zero.
17691f4285fSBoris Brezillon * Note that the element is not added in any list or RB tree.
17791f4285fSBoris Brezillon */
ubi_alloc_aeb(struct ubi_attach_info * ai,int pnum,int ec)17891f4285fSBoris Brezillon struct ubi_ainf_peb *ubi_alloc_aeb(struct ubi_attach_info *ai, int pnum,
17991f4285fSBoris Brezillon int ec)
18091f4285fSBoris Brezillon {
18191f4285fSBoris Brezillon struct ubi_ainf_peb *aeb;
18291f4285fSBoris Brezillon
18391f4285fSBoris Brezillon aeb = kmem_cache_zalloc(ai->aeb_slab_cache, GFP_KERNEL);
18491f4285fSBoris Brezillon if (!aeb)
18591f4285fSBoris Brezillon return NULL;
18691f4285fSBoris Brezillon
18791f4285fSBoris Brezillon aeb->pnum = pnum;
18891f4285fSBoris Brezillon aeb->ec = ec;
18991f4285fSBoris Brezillon aeb->vol_id = UBI_UNKNOWN;
19091f4285fSBoris Brezillon aeb->lnum = UBI_UNKNOWN;
19191f4285fSBoris Brezillon
19291f4285fSBoris Brezillon return aeb;
19391f4285fSBoris Brezillon }
19491f4285fSBoris Brezillon
19591f4285fSBoris Brezillon /**
19691f4285fSBoris Brezillon * ubi_free_aeb - free an aeb element
19791f4285fSBoris Brezillon * @ai: attaching information
19891f4285fSBoris Brezillon * @aeb: the element to free
19991f4285fSBoris Brezillon *
20091f4285fSBoris Brezillon * Free an aeb object. The caller must have removed the element from any list
20191f4285fSBoris Brezillon * or RB tree.
20291f4285fSBoris Brezillon */
ubi_free_aeb(struct ubi_attach_info * ai,struct ubi_ainf_peb * aeb)20391f4285fSBoris Brezillon void ubi_free_aeb(struct ubi_attach_info *ai, struct ubi_ainf_peb *aeb)
20491f4285fSBoris Brezillon {
20591f4285fSBoris Brezillon kmem_cache_free(ai->aeb_slab_cache, aeb);
20691f4285fSBoris Brezillon }
20791f4285fSBoris Brezillon
20891f4285fSBoris Brezillon /**
209ae4a8104SArtem Bityutskiy * add_to_list - add physical eraseblock to a list.
210ae4a8104SArtem Bityutskiy * @ai: attaching information
211ae4a8104SArtem Bityutskiy * @pnum: physical eraseblock number to add
2126dd3bc7eSJoel Reardon * @vol_id: the last used volume id for the PEB
2136dd3bc7eSJoel Reardon * @lnum: the last used LEB number for the PEB
214ae4a8104SArtem Bityutskiy * @ec: erase counter of the physical eraseblock
215ae4a8104SArtem Bityutskiy * @to_head: if not zero, add to the head of the list
216ae4a8104SArtem Bityutskiy * @list: the list to add to
217ae4a8104SArtem Bityutskiy *
218ae4a8104SArtem Bityutskiy * This function allocates a 'struct ubi_ainf_peb' object for physical
219ae4a8104SArtem Bityutskiy * eraseblock @pnum and adds it to the "free", "erase", or "alien" lists.
2206dd3bc7eSJoel Reardon * It stores the @lnum and @vol_id alongside, which can both be
2216dd3bc7eSJoel Reardon * %UBI_UNKNOWN if they are not available, not readable, or not assigned.
222ae4a8104SArtem Bityutskiy * If @to_head is not zero, PEB will be added to the head of the list, which
223ae4a8104SArtem Bityutskiy * basically means it will be processed first later. E.g., we add corrupted
224ae4a8104SArtem Bityutskiy * PEBs (corrupted due to power cuts) to the head of the erase list to make
225ae4a8104SArtem Bityutskiy * sure we erase them first and get rid of corruptions ASAP. This function
226ae4a8104SArtem Bityutskiy * returns zero in case of success and a negative error code in case of
227ae4a8104SArtem Bityutskiy * failure.
228ae4a8104SArtem Bityutskiy */
add_to_list(struct ubi_attach_info * ai,int pnum,int vol_id,int lnum,int ec,int to_head,struct list_head * list)2296dd3bc7eSJoel Reardon static int add_to_list(struct ubi_attach_info *ai, int pnum, int vol_id,
2306dd3bc7eSJoel Reardon int lnum, int ec, int to_head, struct list_head *list)
231ae4a8104SArtem Bityutskiy {
232ae4a8104SArtem Bityutskiy struct ubi_ainf_peb *aeb;
233ae4a8104SArtem Bityutskiy
234ae4a8104SArtem Bityutskiy if (list == &ai->free) {
235ae4a8104SArtem Bityutskiy dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
236ae4a8104SArtem Bityutskiy } else if (list == &ai->erase) {
237ae4a8104SArtem Bityutskiy dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
238ae4a8104SArtem Bityutskiy } else if (list == &ai->alien) {
239ae4a8104SArtem Bityutskiy dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
240ae4a8104SArtem Bityutskiy ai->alien_peb_count += 1;
241ae4a8104SArtem Bityutskiy } else
242ae4a8104SArtem Bityutskiy BUG();
243ae4a8104SArtem Bityutskiy
24491f4285fSBoris Brezillon aeb = ubi_alloc_aeb(ai, pnum, ec);
245ae4a8104SArtem Bityutskiy if (!aeb)
246ae4a8104SArtem Bityutskiy return -ENOMEM;
247ae4a8104SArtem Bityutskiy
2486dd3bc7eSJoel Reardon aeb->vol_id = vol_id;
2496dd3bc7eSJoel Reardon aeb->lnum = lnum;
250ae4a8104SArtem Bityutskiy if (to_head)
251ae4a8104SArtem Bityutskiy list_add(&aeb->u.list, list);
252ae4a8104SArtem Bityutskiy else
253ae4a8104SArtem Bityutskiy list_add_tail(&aeb->u.list, list);
254ae4a8104SArtem Bityutskiy return 0;
255ae4a8104SArtem Bityutskiy }
256ae4a8104SArtem Bityutskiy
257ae4a8104SArtem Bityutskiy /**
258ae4a8104SArtem Bityutskiy * add_corrupted - add a corrupted physical eraseblock.
259ae4a8104SArtem Bityutskiy * @ai: attaching information
260ae4a8104SArtem Bityutskiy * @pnum: physical eraseblock number to add
261ae4a8104SArtem Bityutskiy * @ec: erase counter of the physical eraseblock
262ae4a8104SArtem Bityutskiy *
263ae4a8104SArtem Bityutskiy * This function allocates a 'struct ubi_ainf_peb' object for a corrupted
264ae4a8104SArtem Bityutskiy * physical eraseblock @pnum and adds it to the 'corr' list. The corruption
265ae4a8104SArtem Bityutskiy * was presumably not caused by a power cut. Returns zero in case of success
266ae4a8104SArtem Bityutskiy * and a negative error code in case of failure.
267ae4a8104SArtem Bityutskiy */
add_corrupted(struct ubi_attach_info * ai,int pnum,int ec)268ae4a8104SArtem Bityutskiy static int add_corrupted(struct ubi_attach_info *ai, int pnum, int ec)
269ae4a8104SArtem Bityutskiy {
270ae4a8104SArtem Bityutskiy struct ubi_ainf_peb *aeb;
271ae4a8104SArtem Bityutskiy
272ae4a8104SArtem Bityutskiy dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
273ae4a8104SArtem Bityutskiy
27491f4285fSBoris Brezillon aeb = ubi_alloc_aeb(ai, pnum, ec);
275ae4a8104SArtem Bityutskiy if (!aeb)
276ae4a8104SArtem Bityutskiy return -ENOMEM;
277ae4a8104SArtem Bityutskiy
278ae4a8104SArtem Bityutskiy ai->corr_peb_count += 1;
279ae4a8104SArtem Bityutskiy list_add(&aeb->u.list, &ai->corr);
280ae4a8104SArtem Bityutskiy return 0;
281ae4a8104SArtem Bityutskiy }
282ae4a8104SArtem Bityutskiy
283ae4a8104SArtem Bityutskiy /**
284fdf10ed7SRichard Weinberger * add_fastmap - add a Fastmap related physical eraseblock.
285fdf10ed7SRichard Weinberger * @ai: attaching information
286fdf10ed7SRichard Weinberger * @pnum: physical eraseblock number the VID header came from
287fdf10ed7SRichard Weinberger * @vid_hdr: the volume identifier header
288fdf10ed7SRichard Weinberger * @ec: erase counter of the physical eraseblock
289fdf10ed7SRichard Weinberger *
290fdf10ed7SRichard Weinberger * This function allocates a 'struct ubi_ainf_peb' object for a Fastamp
291fdf10ed7SRichard Weinberger * physical eraseblock @pnum and adds it to the 'fastmap' list.
292fdf10ed7SRichard Weinberger * Such blocks can be Fastmap super and data blocks from both the most
293fdf10ed7SRichard Weinberger * recent Fastmap we're attaching from or from old Fastmaps which will
294fdf10ed7SRichard Weinberger * be erased.
295fdf10ed7SRichard Weinberger */
add_fastmap(struct ubi_attach_info * ai,int pnum,struct ubi_vid_hdr * vid_hdr,int ec)296fdf10ed7SRichard Weinberger static int add_fastmap(struct ubi_attach_info *ai, int pnum,
297fdf10ed7SRichard Weinberger struct ubi_vid_hdr *vid_hdr, int ec)
298fdf10ed7SRichard Weinberger {
299fdf10ed7SRichard Weinberger struct ubi_ainf_peb *aeb;
300fdf10ed7SRichard Weinberger
30191f4285fSBoris Brezillon aeb = ubi_alloc_aeb(ai, pnum, ec);
302fdf10ed7SRichard Weinberger if (!aeb)
303fdf10ed7SRichard Weinberger return -ENOMEM;
304fdf10ed7SRichard Weinberger
3053f84e454SBoris Brezillon aeb->vol_id = be32_to_cpu(vid_hdr->vol_id);
3063f84e454SBoris Brezillon aeb->sqnum = be64_to_cpu(vid_hdr->sqnum);
307fdf10ed7SRichard Weinberger list_add(&aeb->u.list, &ai->fastmap);
308fdf10ed7SRichard Weinberger
309fdf10ed7SRichard Weinberger dbg_bld("add to fastmap list: PEB %d, vol_id %d, sqnum: %llu", pnum,
310fdf10ed7SRichard Weinberger aeb->vol_id, aeb->sqnum);
311fdf10ed7SRichard Weinberger
312fdf10ed7SRichard Weinberger return 0;
313fdf10ed7SRichard Weinberger }
314fdf10ed7SRichard Weinberger
315fdf10ed7SRichard Weinberger /**
316ae4a8104SArtem Bityutskiy * validate_vid_hdr - check volume identifier header.
31732608703STanya Brokhman * @ubi: UBI device description object
318ae4a8104SArtem Bityutskiy * @vid_hdr: the volume identifier header to check
319ae4a8104SArtem Bityutskiy * @av: information about the volume this logical eraseblock belongs to
320ae4a8104SArtem Bityutskiy * @pnum: physical eraseblock number the VID header came from
321ae4a8104SArtem Bityutskiy *
322ae4a8104SArtem Bityutskiy * This function checks that data stored in @vid_hdr is consistent. Returns
323ae4a8104SArtem Bityutskiy * non-zero if an inconsistency was found and zero if not.
324ae4a8104SArtem Bityutskiy *
325ae4a8104SArtem Bityutskiy * Note, UBI does sanity check of everything it reads from the flash media.
326ae4a8104SArtem Bityutskiy * Most of the checks are done in the I/O sub-system. Here we check that the
327ae4a8104SArtem Bityutskiy * information in the VID header is consistent to the information in other VID
328ae4a8104SArtem Bityutskiy * headers of the same volume.
329ae4a8104SArtem Bityutskiy */
validate_vid_hdr(const struct ubi_device * ubi,const struct ubi_vid_hdr * vid_hdr,const struct ubi_ainf_volume * av,int pnum)33032608703STanya Brokhman static int validate_vid_hdr(const struct ubi_device *ubi,
33132608703STanya Brokhman const struct ubi_vid_hdr *vid_hdr,
332ae4a8104SArtem Bityutskiy const struct ubi_ainf_volume *av, int pnum)
333ae4a8104SArtem Bityutskiy {
334ae4a8104SArtem Bityutskiy int vol_type = vid_hdr->vol_type;
335ae4a8104SArtem Bityutskiy int vol_id = be32_to_cpu(vid_hdr->vol_id);
336ae4a8104SArtem Bityutskiy int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
337ae4a8104SArtem Bityutskiy int data_pad = be32_to_cpu(vid_hdr->data_pad);
338ae4a8104SArtem Bityutskiy
339ae4a8104SArtem Bityutskiy if (av->leb_count != 0) {
340ae4a8104SArtem Bityutskiy int av_vol_type;
341ae4a8104SArtem Bityutskiy
342ae4a8104SArtem Bityutskiy /*
343ae4a8104SArtem Bityutskiy * This is not the first logical eraseblock belonging to this
344ae4a8104SArtem Bityutskiy * volume. Ensure that the data in its VID header is consistent
345ae4a8104SArtem Bityutskiy * to the data in previous logical eraseblock headers.
346ae4a8104SArtem Bityutskiy */
347ae4a8104SArtem Bityutskiy
348ae4a8104SArtem Bityutskiy if (vol_id != av->vol_id) {
34932608703STanya Brokhman ubi_err(ubi, "inconsistent vol_id");
350ae4a8104SArtem Bityutskiy goto bad;
351ae4a8104SArtem Bityutskiy }
352ae4a8104SArtem Bityutskiy
353ae4a8104SArtem Bityutskiy if (av->vol_type == UBI_STATIC_VOLUME)
354ae4a8104SArtem Bityutskiy av_vol_type = UBI_VID_STATIC;
355ae4a8104SArtem Bityutskiy else
356ae4a8104SArtem Bityutskiy av_vol_type = UBI_VID_DYNAMIC;
357ae4a8104SArtem Bityutskiy
358ae4a8104SArtem Bityutskiy if (vol_type != av_vol_type) {
35932608703STanya Brokhman ubi_err(ubi, "inconsistent vol_type");
360ae4a8104SArtem Bityutskiy goto bad;
361ae4a8104SArtem Bityutskiy }
362ae4a8104SArtem Bityutskiy
363ae4a8104SArtem Bityutskiy if (used_ebs != av->used_ebs) {
36432608703STanya Brokhman ubi_err(ubi, "inconsistent used_ebs");
365ae4a8104SArtem Bityutskiy goto bad;
366ae4a8104SArtem Bityutskiy }
367ae4a8104SArtem Bityutskiy
368ae4a8104SArtem Bityutskiy if (data_pad != av->data_pad) {
36932608703STanya Brokhman ubi_err(ubi, "inconsistent data_pad");
370ae4a8104SArtem Bityutskiy goto bad;
371ae4a8104SArtem Bityutskiy }
372ae4a8104SArtem Bityutskiy }
373ae4a8104SArtem Bityutskiy
374ae4a8104SArtem Bityutskiy return 0;
375ae4a8104SArtem Bityutskiy
376ae4a8104SArtem Bityutskiy bad:
37732608703STanya Brokhman ubi_err(ubi, "inconsistent VID header at PEB %d", pnum);
378ae4a8104SArtem Bityutskiy ubi_dump_vid_hdr(vid_hdr);
379ae4a8104SArtem Bityutskiy ubi_dump_av(av);
380ae4a8104SArtem Bityutskiy return -EINVAL;
381ae4a8104SArtem Bityutskiy }
382ae4a8104SArtem Bityutskiy
383ae4a8104SArtem Bityutskiy /**
384ae4a8104SArtem Bityutskiy * add_volume - add volume to the attaching information.
385ae4a8104SArtem Bityutskiy * @ai: attaching information
386ae4a8104SArtem Bityutskiy * @vol_id: ID of the volume to add
387ae4a8104SArtem Bityutskiy * @pnum: physical eraseblock number
388ae4a8104SArtem Bityutskiy * @vid_hdr: volume identifier header
389ae4a8104SArtem Bityutskiy *
390ae4a8104SArtem Bityutskiy * If the volume corresponding to the @vid_hdr logical eraseblock is already
391ae4a8104SArtem Bityutskiy * present in the attaching information, this function does nothing. Otherwise
392ae4a8104SArtem Bityutskiy * it adds corresponding volume to the attaching information. Returns a pointer
393ae4a8104SArtem Bityutskiy * to the allocated "av" object in case of success and a negative error code in
394ae4a8104SArtem Bityutskiy * case of failure.
395ae4a8104SArtem Bityutskiy */
add_volume(struct ubi_attach_info * ai,int vol_id,int pnum,const struct ubi_vid_hdr * vid_hdr)396ae4a8104SArtem Bityutskiy static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *ai,
397ae4a8104SArtem Bityutskiy int vol_id, int pnum,
398ae4a8104SArtem Bityutskiy const struct ubi_vid_hdr *vid_hdr)
399ae4a8104SArtem Bityutskiy {
400ae4a8104SArtem Bityutskiy struct ubi_ainf_volume *av;
401de4c455bSBoris Brezillon bool created;
402ae4a8104SArtem Bityutskiy
403ae4a8104SArtem Bityutskiy ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
404ae4a8104SArtem Bityutskiy
405de4c455bSBoris Brezillon av = ubi_find_or_add_av(ai, vol_id, &created);
406de4c455bSBoris Brezillon if (IS_ERR(av) || !created)
407ae4a8104SArtem Bityutskiy return av;
408ae4a8104SArtem Bityutskiy
409ae4a8104SArtem Bityutskiy av->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
410ae4a8104SArtem Bityutskiy av->data_pad = be32_to_cpu(vid_hdr->data_pad);
411ae4a8104SArtem Bityutskiy av->compat = vid_hdr->compat;
412ae4a8104SArtem Bityutskiy av->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
413ae4a8104SArtem Bityutskiy : UBI_STATIC_VOLUME;
414ae4a8104SArtem Bityutskiy
415ae4a8104SArtem Bityutskiy return av;
416ae4a8104SArtem Bityutskiy }
417ae4a8104SArtem Bityutskiy
418ae4a8104SArtem Bityutskiy /**
419dac6e208SRichard Weinberger * ubi_compare_lebs - find out which logical eraseblock is newer.
420ae4a8104SArtem Bityutskiy * @ubi: UBI device description object
421ae4a8104SArtem Bityutskiy * @aeb: first logical eraseblock to compare
422ae4a8104SArtem Bityutskiy * @pnum: physical eraseblock number of the second logical eraseblock to
423ae4a8104SArtem Bityutskiy * compare
424ae4a8104SArtem Bityutskiy * @vid_hdr: volume identifier header of the second logical eraseblock
425ae4a8104SArtem Bityutskiy *
426ae4a8104SArtem Bityutskiy * This function compares 2 copies of a LEB and informs which one is newer. In
427ae4a8104SArtem Bityutskiy * case of success this function returns a positive value, in case of failure, a
428ae4a8104SArtem Bityutskiy * negative error code is returned. The success return codes use the following
429ae4a8104SArtem Bityutskiy * bits:
430ae4a8104SArtem Bityutskiy * o bit 0 is cleared: the first PEB (described by @aeb) is newer than the
431ae4a8104SArtem Bityutskiy * second PEB (described by @pnum and @vid_hdr);
432ae4a8104SArtem Bityutskiy * o bit 0 is set: the second PEB is newer;
433ae4a8104SArtem Bityutskiy * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
434ae4a8104SArtem Bityutskiy * o bit 1 is set: bit-flips were detected in the newer LEB;
435ae4a8104SArtem Bityutskiy * o bit 2 is cleared: the older LEB is not corrupted;
436ae4a8104SArtem Bityutskiy * o bit 2 is set: the older LEB is corrupted.
437ae4a8104SArtem Bityutskiy */
ubi_compare_lebs(struct ubi_device * ubi,const struct ubi_ainf_peb * aeb,int pnum,const struct ubi_vid_hdr * vid_hdr)438dac6e208SRichard Weinberger int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
439ae4a8104SArtem Bityutskiy int pnum, const struct ubi_vid_hdr *vid_hdr)
440ae4a8104SArtem Bityutskiy {
441ae4a8104SArtem Bityutskiy int len, err, second_is_newer, bitflips = 0, corrupted = 0;
442ae4a8104SArtem Bityutskiy uint32_t data_crc, crc;
4433291b52fSBoris Brezillon struct ubi_vid_io_buf *vidb = NULL;
444ae4a8104SArtem Bityutskiy unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
445ae4a8104SArtem Bityutskiy
446ae4a8104SArtem Bityutskiy if (sqnum2 == aeb->sqnum) {
447ae4a8104SArtem Bityutskiy /*
448ae4a8104SArtem Bityutskiy * This must be a really ancient UBI image which has been
449ae4a8104SArtem Bityutskiy * created before sequence numbers support has been added. At
450ae4a8104SArtem Bityutskiy * that times we used 32-bit LEB versions stored in logical
451ae4a8104SArtem Bityutskiy * eraseblocks. That was before UBI got into mainline. We do not
452ae4a8104SArtem Bityutskiy * support these images anymore. Well, those images still work,
453ae4a8104SArtem Bityutskiy * but only if no unclean reboots happened.
454ae4a8104SArtem Bityutskiy */
45532608703STanya Brokhman ubi_err(ubi, "unsupported on-flash UBI format");
456ae4a8104SArtem Bityutskiy return -EINVAL;
457ae4a8104SArtem Bityutskiy }
458ae4a8104SArtem Bityutskiy
459ae4a8104SArtem Bityutskiy /* Obviously the LEB with lower sequence counter is older */
460ae4a8104SArtem Bityutskiy second_is_newer = (sqnum2 > aeb->sqnum);
461ae4a8104SArtem Bityutskiy
462ae4a8104SArtem Bityutskiy /*
463ae4a8104SArtem Bityutskiy * Now we know which copy is newer. If the copy flag of the PEB with
464ae4a8104SArtem Bityutskiy * newer version is not set, then we just return, otherwise we have to
465ae4a8104SArtem Bityutskiy * check data CRC. For the second PEB we already have the VID header,
466ae4a8104SArtem Bityutskiy * for the first one - we'll need to re-read it from flash.
467ae4a8104SArtem Bityutskiy *
468ae4a8104SArtem Bityutskiy * Note: this may be optimized so that we wouldn't read twice.
469ae4a8104SArtem Bityutskiy */
470ae4a8104SArtem Bityutskiy
471ae4a8104SArtem Bityutskiy if (second_is_newer) {
472ae4a8104SArtem Bityutskiy if (!vid_hdr->copy_flag) {
473ae4a8104SArtem Bityutskiy /* It is not a copy, so it is newer */
474ae4a8104SArtem Bityutskiy dbg_bld("second PEB %d is newer, copy_flag is unset",
475ae4a8104SArtem Bityutskiy pnum);
476ae4a8104SArtem Bityutskiy return 1;
477ae4a8104SArtem Bityutskiy }
478ae4a8104SArtem Bityutskiy } else {
479ae4a8104SArtem Bityutskiy if (!aeb->copy_flag) {
480ae4a8104SArtem Bityutskiy /* It is not a copy, so it is newer */
481ae4a8104SArtem Bityutskiy dbg_bld("first PEB %d is newer, copy_flag is unset",
482ae4a8104SArtem Bityutskiy pnum);
483ae4a8104SArtem Bityutskiy return bitflips << 1;
484ae4a8104SArtem Bityutskiy }
485ae4a8104SArtem Bityutskiy
4863291b52fSBoris Brezillon vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
4873291b52fSBoris Brezillon if (!vidb)
488ae4a8104SArtem Bityutskiy return -ENOMEM;
489ae4a8104SArtem Bityutskiy
490ae4a8104SArtem Bityutskiy pnum = aeb->pnum;
4913291b52fSBoris Brezillon err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0);
492ae4a8104SArtem Bityutskiy if (err) {
493ae4a8104SArtem Bityutskiy if (err == UBI_IO_BITFLIPS)
494ae4a8104SArtem Bityutskiy bitflips = 1;
495ae4a8104SArtem Bityutskiy else {
49632608703STanya Brokhman ubi_err(ubi, "VID of PEB %d header is bad, but it was OK earlier, err %d",
497049333ceSArtem Bityutskiy pnum, err);
498ae4a8104SArtem Bityutskiy if (err > 0)
499ae4a8104SArtem Bityutskiy err = -EIO;
500ae4a8104SArtem Bityutskiy
501ae4a8104SArtem Bityutskiy goto out_free_vidh;
502ae4a8104SArtem Bityutskiy }
503ae4a8104SArtem Bityutskiy }
504ae4a8104SArtem Bityutskiy
5053291b52fSBoris Brezillon vid_hdr = ubi_get_vid_hdr(vidb);
506ae4a8104SArtem Bityutskiy }
507ae4a8104SArtem Bityutskiy
508ae4a8104SArtem Bityutskiy /* Read the data of the copy and check the CRC */
509ae4a8104SArtem Bityutskiy
510ae4a8104SArtem Bityutskiy len = be32_to_cpu(vid_hdr->data_size);
511ae4a8104SArtem Bityutskiy
512d125a753SArtem Bityutskiy mutex_lock(&ubi->buf_mutex);
513d125a753SArtem Bityutskiy err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len);
514ae4a8104SArtem Bityutskiy if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
515d125a753SArtem Bityutskiy goto out_unlock;
516ae4a8104SArtem Bityutskiy
517ae4a8104SArtem Bityutskiy data_crc = be32_to_cpu(vid_hdr->data_crc);
518d125a753SArtem Bityutskiy crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, len);
519ae4a8104SArtem Bityutskiy if (crc != data_crc) {
520ae4a8104SArtem Bityutskiy dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
521ae4a8104SArtem Bityutskiy pnum, crc, data_crc);
522ae4a8104SArtem Bityutskiy corrupted = 1;
523ae4a8104SArtem Bityutskiy bitflips = 0;
524ae4a8104SArtem Bityutskiy second_is_newer = !second_is_newer;
525ae4a8104SArtem Bityutskiy } else {
526ae4a8104SArtem Bityutskiy dbg_bld("PEB %d CRC is OK", pnum);
5278eef7d70SBrian Norris bitflips |= !!err;
528ae4a8104SArtem Bityutskiy }
529d125a753SArtem Bityutskiy mutex_unlock(&ubi->buf_mutex);
530ae4a8104SArtem Bityutskiy
5313291b52fSBoris Brezillon ubi_free_vid_buf(vidb);
532ae4a8104SArtem Bityutskiy
533ae4a8104SArtem Bityutskiy if (second_is_newer)
534ae4a8104SArtem Bityutskiy dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
535ae4a8104SArtem Bityutskiy else
536ae4a8104SArtem Bityutskiy dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
537ae4a8104SArtem Bityutskiy
538ae4a8104SArtem Bityutskiy return second_is_newer | (bitflips << 1) | (corrupted << 2);
539ae4a8104SArtem Bityutskiy
540d125a753SArtem Bityutskiy out_unlock:
541d125a753SArtem Bityutskiy mutex_unlock(&ubi->buf_mutex);
542ae4a8104SArtem Bityutskiy out_free_vidh:
5433291b52fSBoris Brezillon ubi_free_vid_buf(vidb);
544ae4a8104SArtem Bityutskiy return err;
545ae4a8104SArtem Bityutskiy }
546ae4a8104SArtem Bityutskiy
547ae4a8104SArtem Bityutskiy /**
548ae4a8104SArtem Bityutskiy * ubi_add_to_av - add used physical eraseblock to the attaching information.
549ae4a8104SArtem Bityutskiy * @ubi: UBI device description object
550ae4a8104SArtem Bityutskiy * @ai: attaching information
551ae4a8104SArtem Bityutskiy * @pnum: the physical eraseblock number
552ae4a8104SArtem Bityutskiy * @ec: erase counter
553ae4a8104SArtem Bityutskiy * @vid_hdr: the volume identifier header
554ae4a8104SArtem Bityutskiy * @bitflips: if bit-flips were detected when this physical eraseblock was read
555ae4a8104SArtem Bityutskiy *
556ae4a8104SArtem Bityutskiy * This function adds information about a used physical eraseblock to the
557ae4a8104SArtem Bityutskiy * 'used' tree of the corresponding volume. The function is rather complex
558ae4a8104SArtem Bityutskiy * because it has to handle cases when this is not the first physical
559ae4a8104SArtem Bityutskiy * eraseblock belonging to the same logical eraseblock, and the newer one has
560ae4a8104SArtem Bityutskiy * to be picked, while the older one has to be dropped. This function returns
561ae4a8104SArtem Bityutskiy * zero in case of success and a negative error code in case of failure.
562ae4a8104SArtem Bityutskiy */
ubi_add_to_av(struct ubi_device * ubi,struct ubi_attach_info * ai,int pnum,int ec,const struct ubi_vid_hdr * vid_hdr,int bitflips)563ae4a8104SArtem Bityutskiy int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
564ae4a8104SArtem Bityutskiy int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips)
565ae4a8104SArtem Bityutskiy {
566ae4a8104SArtem Bityutskiy int err, vol_id, lnum;
567ae4a8104SArtem Bityutskiy unsigned long long sqnum;
568ae4a8104SArtem Bityutskiy struct ubi_ainf_volume *av;
569ae4a8104SArtem Bityutskiy struct ubi_ainf_peb *aeb;
570ae4a8104SArtem Bityutskiy struct rb_node **p, *parent = NULL;
571ae4a8104SArtem Bityutskiy
572ae4a8104SArtem Bityutskiy vol_id = be32_to_cpu(vid_hdr->vol_id);
573ae4a8104SArtem Bityutskiy lnum = be32_to_cpu(vid_hdr->lnum);
574ae4a8104SArtem Bityutskiy sqnum = be64_to_cpu(vid_hdr->sqnum);
575ae4a8104SArtem Bityutskiy
576ae4a8104SArtem Bityutskiy dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
577ae4a8104SArtem Bityutskiy pnum, vol_id, lnum, ec, sqnum, bitflips);
578ae4a8104SArtem Bityutskiy
579ae4a8104SArtem Bityutskiy av = add_volume(ai, vol_id, pnum, vid_hdr);
580ae4a8104SArtem Bityutskiy if (IS_ERR(av))
581ae4a8104SArtem Bityutskiy return PTR_ERR(av);
582ae4a8104SArtem Bityutskiy
583ae4a8104SArtem Bityutskiy if (ai->max_sqnum < sqnum)
584ae4a8104SArtem Bityutskiy ai->max_sqnum = sqnum;
585ae4a8104SArtem Bityutskiy
586ae4a8104SArtem Bityutskiy /*
587ae4a8104SArtem Bityutskiy * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
588ae4a8104SArtem Bityutskiy * if this is the first instance of this logical eraseblock or not.
589ae4a8104SArtem Bityutskiy */
590ae4a8104SArtem Bityutskiy p = &av->root.rb_node;
591ae4a8104SArtem Bityutskiy while (*p) {
592ae4a8104SArtem Bityutskiy int cmp_res;
593ae4a8104SArtem Bityutskiy
594ae4a8104SArtem Bityutskiy parent = *p;
595ae4a8104SArtem Bityutskiy aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
596ae4a8104SArtem Bityutskiy if (lnum != aeb->lnum) {
597ae4a8104SArtem Bityutskiy if (lnum < aeb->lnum)
598ae4a8104SArtem Bityutskiy p = &(*p)->rb_left;
599ae4a8104SArtem Bityutskiy else
600ae4a8104SArtem Bityutskiy p = &(*p)->rb_right;
601ae4a8104SArtem Bityutskiy continue;
602ae4a8104SArtem Bityutskiy }
603ae4a8104SArtem Bityutskiy
604ae4a8104SArtem Bityutskiy /*
605ae4a8104SArtem Bityutskiy * There is already a physical eraseblock describing the same
606ae4a8104SArtem Bityutskiy * logical eraseblock present.
607ae4a8104SArtem Bityutskiy */
608ae4a8104SArtem Bityutskiy
609ae4a8104SArtem Bityutskiy dbg_bld("this LEB already exists: PEB %d, sqnum %llu, EC %d",
610ae4a8104SArtem Bityutskiy aeb->pnum, aeb->sqnum, aeb->ec);
611ae4a8104SArtem Bityutskiy
612ae4a8104SArtem Bityutskiy /*
613ae4a8104SArtem Bityutskiy * Make sure that the logical eraseblocks have different
614ae4a8104SArtem Bityutskiy * sequence numbers. Otherwise the image is bad.
615ae4a8104SArtem Bityutskiy *
616ae4a8104SArtem Bityutskiy * However, if the sequence number is zero, we assume it must
617ae4a8104SArtem Bityutskiy * be an ancient UBI image from the era when UBI did not have
618ae4a8104SArtem Bityutskiy * sequence numbers. We still can attach these images, unless
619ae4a8104SArtem Bityutskiy * there is a need to distinguish between old and new
620ae4a8104SArtem Bityutskiy * eraseblocks, in which case we'll refuse the image in
621dac6e208SRichard Weinberger * 'ubi_compare_lebs()'. In other words, we attach old clean
622ae4a8104SArtem Bityutskiy * images, but refuse attaching old images with duplicated
623ae4a8104SArtem Bityutskiy * logical eraseblocks because there was an unclean reboot.
624ae4a8104SArtem Bityutskiy */
625ae4a8104SArtem Bityutskiy if (aeb->sqnum == sqnum && sqnum != 0) {
62632608703STanya Brokhman ubi_err(ubi, "two LEBs with same sequence number %llu",
627ae4a8104SArtem Bityutskiy sqnum);
628ae4a8104SArtem Bityutskiy ubi_dump_aeb(aeb, 0);
629ae4a8104SArtem Bityutskiy ubi_dump_vid_hdr(vid_hdr);
630ae4a8104SArtem Bityutskiy return -EINVAL;
631ae4a8104SArtem Bityutskiy }
632ae4a8104SArtem Bityutskiy
633ae4a8104SArtem Bityutskiy /*
634ae4a8104SArtem Bityutskiy * Now we have to drop the older one and preserve the newer
635ae4a8104SArtem Bityutskiy * one.
636ae4a8104SArtem Bityutskiy */
637dac6e208SRichard Weinberger cmp_res = ubi_compare_lebs(ubi, aeb, pnum, vid_hdr);
638ae4a8104SArtem Bityutskiy if (cmp_res < 0)
639ae4a8104SArtem Bityutskiy return cmp_res;
640ae4a8104SArtem Bityutskiy
641ae4a8104SArtem Bityutskiy if (cmp_res & 1) {
642ae4a8104SArtem Bityutskiy /*
643ae4a8104SArtem Bityutskiy * This logical eraseblock is newer than the one
644ae4a8104SArtem Bityutskiy * found earlier.
645ae4a8104SArtem Bityutskiy */
64632608703STanya Brokhman err = validate_vid_hdr(ubi, vid_hdr, av, pnum);
647ae4a8104SArtem Bityutskiy if (err)
648ae4a8104SArtem Bityutskiy return err;
649ae4a8104SArtem Bityutskiy
6506dd3bc7eSJoel Reardon err = add_to_list(ai, aeb->pnum, aeb->vol_id,
6516dd3bc7eSJoel Reardon aeb->lnum, aeb->ec, cmp_res & 4,
652ae4a8104SArtem Bityutskiy &ai->erase);
653ae4a8104SArtem Bityutskiy if (err)
654ae4a8104SArtem Bityutskiy return err;
655ae4a8104SArtem Bityutskiy
656ae4a8104SArtem Bityutskiy aeb->ec = ec;
657ae4a8104SArtem Bityutskiy aeb->pnum = pnum;
6586dd3bc7eSJoel Reardon aeb->vol_id = vol_id;
6596dd3bc7eSJoel Reardon aeb->lnum = lnum;
660ae4a8104SArtem Bityutskiy aeb->scrub = ((cmp_res & 2) || bitflips);
661ae4a8104SArtem Bityutskiy aeb->copy_flag = vid_hdr->copy_flag;
662ae4a8104SArtem Bityutskiy aeb->sqnum = sqnum;
663ae4a8104SArtem Bityutskiy
664ae4a8104SArtem Bityutskiy if (av->highest_lnum == lnum)
665ae4a8104SArtem Bityutskiy av->last_data_size =
666ae4a8104SArtem Bityutskiy be32_to_cpu(vid_hdr->data_size);
667ae4a8104SArtem Bityutskiy
668ae4a8104SArtem Bityutskiy return 0;
669ae4a8104SArtem Bityutskiy } else {
670ae4a8104SArtem Bityutskiy /*
671ae4a8104SArtem Bityutskiy * This logical eraseblock is older than the one found
672ae4a8104SArtem Bityutskiy * previously.
673ae4a8104SArtem Bityutskiy */
6746dd3bc7eSJoel Reardon return add_to_list(ai, pnum, vol_id, lnum, ec,
6756dd3bc7eSJoel Reardon cmp_res & 4, &ai->erase);
676ae4a8104SArtem Bityutskiy }
677ae4a8104SArtem Bityutskiy }
678ae4a8104SArtem Bityutskiy
679ae4a8104SArtem Bityutskiy /*
680ae4a8104SArtem Bityutskiy * We've met this logical eraseblock for the first time, add it to the
681ae4a8104SArtem Bityutskiy * attaching information.
682ae4a8104SArtem Bityutskiy */
683ae4a8104SArtem Bityutskiy
68432608703STanya Brokhman err = validate_vid_hdr(ubi, vid_hdr, av, pnum);
685ae4a8104SArtem Bityutskiy if (err)
686ae4a8104SArtem Bityutskiy return err;
687ae4a8104SArtem Bityutskiy
68891f4285fSBoris Brezillon aeb = ubi_alloc_aeb(ai, pnum, ec);
689ae4a8104SArtem Bityutskiy if (!aeb)
690ae4a8104SArtem Bityutskiy return -ENOMEM;
691ae4a8104SArtem Bityutskiy
6926dd3bc7eSJoel Reardon aeb->vol_id = vol_id;
693ae4a8104SArtem Bityutskiy aeb->lnum = lnum;
694ae4a8104SArtem Bityutskiy aeb->scrub = bitflips;
695ae4a8104SArtem Bityutskiy aeb->copy_flag = vid_hdr->copy_flag;
696ae4a8104SArtem Bityutskiy aeb->sqnum = sqnum;
697ae4a8104SArtem Bityutskiy
698ae4a8104SArtem Bityutskiy if (av->highest_lnum <= lnum) {
699ae4a8104SArtem Bityutskiy av->highest_lnum = lnum;
700ae4a8104SArtem Bityutskiy av->last_data_size = be32_to_cpu(vid_hdr->data_size);
701ae4a8104SArtem Bityutskiy }
702ae4a8104SArtem Bityutskiy
703ae4a8104SArtem Bityutskiy av->leb_count += 1;
704ae4a8104SArtem Bityutskiy rb_link_node(&aeb->u.rb, parent, p);
705ae4a8104SArtem Bityutskiy rb_insert_color(&aeb->u.rb, &av->root);
706ae4a8104SArtem Bityutskiy return 0;
707ae4a8104SArtem Bityutskiy }
708ae4a8104SArtem Bityutskiy
709ae4a8104SArtem Bityutskiy /**
710de4c455bSBoris Brezillon * ubi_add_av - add volume to the attaching information.
711de4c455bSBoris Brezillon * @ai: attaching information
712de4c455bSBoris Brezillon * @vol_id: the requested volume ID
713de4c455bSBoris Brezillon *
714de4c455bSBoris Brezillon * This function returns a pointer to the new volume description or an
715de4c455bSBoris Brezillon * ERR_PTR if the operation failed.
716de4c455bSBoris Brezillon */
ubi_add_av(struct ubi_attach_info * ai,int vol_id)717de4c455bSBoris Brezillon struct ubi_ainf_volume *ubi_add_av(struct ubi_attach_info *ai, int vol_id)
718de4c455bSBoris Brezillon {
719de4c455bSBoris Brezillon bool created;
720de4c455bSBoris Brezillon
721de4c455bSBoris Brezillon return find_or_add_av(ai, vol_id, AV_ADD, &created);
722de4c455bSBoris Brezillon }
723de4c455bSBoris Brezillon
724de4c455bSBoris Brezillon /**
725ae4a8104SArtem Bityutskiy * ubi_find_av - find volume in the attaching information.
726ae4a8104SArtem Bityutskiy * @ai: attaching information
727ae4a8104SArtem Bityutskiy * @vol_id: the requested volume ID
728ae4a8104SArtem Bityutskiy *
729ae4a8104SArtem Bityutskiy * This function returns a pointer to the volume description or %NULL if there
730ae4a8104SArtem Bityutskiy * are no data about this volume in the attaching information.
731ae4a8104SArtem Bityutskiy */
ubi_find_av(const struct ubi_attach_info * ai,int vol_id)732ae4a8104SArtem Bityutskiy struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
733ae4a8104SArtem Bityutskiy int vol_id)
734ae4a8104SArtem Bityutskiy {
735de4c455bSBoris Brezillon bool created;
736ae4a8104SArtem Bityutskiy
737de4c455bSBoris Brezillon return find_or_add_av((struct ubi_attach_info *)ai, vol_id, AV_FIND,
738de4c455bSBoris Brezillon &created);
739ae4a8104SArtem Bityutskiy }
740ae4a8104SArtem Bityutskiy
741f9efe8d8SBoris Brezillon static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av,
742f9efe8d8SBoris Brezillon struct list_head *list);
743f9efe8d8SBoris Brezillon
744ae4a8104SArtem Bityutskiy /**
745ae4a8104SArtem Bityutskiy * ubi_remove_av - delete attaching information about a volume.
746ae4a8104SArtem Bityutskiy * @ai: attaching information
747ae4a8104SArtem Bityutskiy * @av: the volume attaching information to delete
748ae4a8104SArtem Bityutskiy */
ubi_remove_av(struct ubi_attach_info * ai,struct ubi_ainf_volume * av)749ae4a8104SArtem Bityutskiy void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av)
750ae4a8104SArtem Bityutskiy {
751ae4a8104SArtem Bityutskiy dbg_bld("remove attaching information about volume %d", av->vol_id);
752ae4a8104SArtem Bityutskiy
753ae4a8104SArtem Bityutskiy rb_erase(&av->rb, &ai->volumes);
754f9efe8d8SBoris Brezillon destroy_av(ai, av, &ai->erase);
755ae4a8104SArtem Bityutskiy ai->vols_found -= 1;
756ae4a8104SArtem Bityutskiy }
757ae4a8104SArtem Bityutskiy
758ae4a8104SArtem Bityutskiy /**
759ae4a8104SArtem Bityutskiy * early_erase_peb - erase a physical eraseblock.
760ae4a8104SArtem Bityutskiy * @ubi: UBI device description object
761ae4a8104SArtem Bityutskiy * @ai: attaching information
762ae4a8104SArtem Bityutskiy * @pnum: physical eraseblock number to erase;
763ae4a8104SArtem Bityutskiy * @ec: erase counter value to write (%UBI_UNKNOWN if it is unknown)
764ae4a8104SArtem Bityutskiy *
765ae4a8104SArtem Bityutskiy * This function erases physical eraseblock 'pnum', and writes the erase
766ae4a8104SArtem Bityutskiy * counter header to it. This function should only be used on UBI device
767ae4a8104SArtem Bityutskiy * initialization stages, when the EBA sub-system had not been yet initialized.
768ae4a8104SArtem Bityutskiy * This function returns zero in case of success and a negative error code in
769ae4a8104SArtem Bityutskiy * case of failure.
770ae4a8104SArtem Bityutskiy */
early_erase_peb(struct ubi_device * ubi,const struct ubi_attach_info * ai,int pnum,int ec)771ae4a8104SArtem Bityutskiy static int early_erase_peb(struct ubi_device *ubi,
772ae4a8104SArtem Bityutskiy const struct ubi_attach_info *ai, int pnum, int ec)
773ae4a8104SArtem Bityutskiy {
774ae4a8104SArtem Bityutskiy int err;
775ae4a8104SArtem Bityutskiy struct ubi_ec_hdr *ec_hdr;
776ae4a8104SArtem Bityutskiy
777ae4a8104SArtem Bityutskiy if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
778ae4a8104SArtem Bityutskiy /*
779ae4a8104SArtem Bityutskiy * Erase counter overflow. Upgrade UBI and use 64-bit
780ae4a8104SArtem Bityutskiy * erase counters internally.
781ae4a8104SArtem Bityutskiy */
78232608703STanya Brokhman ubi_err(ubi, "erase counter overflow at PEB %d, EC %d",
78332608703STanya Brokhman pnum, ec);
784ae4a8104SArtem Bityutskiy return -EINVAL;
785ae4a8104SArtem Bityutskiy }
786ae4a8104SArtem Bityutskiy
787ae4a8104SArtem Bityutskiy ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
788ae4a8104SArtem Bityutskiy if (!ec_hdr)
789ae4a8104SArtem Bityutskiy return -ENOMEM;
790ae4a8104SArtem Bityutskiy
791ae4a8104SArtem Bityutskiy ec_hdr->ec = cpu_to_be64(ec);
792ae4a8104SArtem Bityutskiy
793ae4a8104SArtem Bityutskiy err = ubi_io_sync_erase(ubi, pnum, 0);
794ae4a8104SArtem Bityutskiy if (err < 0)
795ae4a8104SArtem Bityutskiy goto out_free;
796ae4a8104SArtem Bityutskiy
797ae4a8104SArtem Bityutskiy err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
798ae4a8104SArtem Bityutskiy
799ae4a8104SArtem Bityutskiy out_free:
800ae4a8104SArtem Bityutskiy kfree(ec_hdr);
801ae4a8104SArtem Bityutskiy return err;
802ae4a8104SArtem Bityutskiy }
803ae4a8104SArtem Bityutskiy
804ae4a8104SArtem Bityutskiy /**
805ae4a8104SArtem Bityutskiy * ubi_early_get_peb - get a free physical eraseblock.
806ae4a8104SArtem Bityutskiy * @ubi: UBI device description object
807ae4a8104SArtem Bityutskiy * @ai: attaching information
808ae4a8104SArtem Bityutskiy *
809ae4a8104SArtem Bityutskiy * This function returns a free physical eraseblock. It is supposed to be
810ae4a8104SArtem Bityutskiy * called on the UBI initialization stages when the wear-leveling sub-system is
811ae4a8104SArtem Bityutskiy * not initialized yet. This function picks a physical eraseblocks from one of
812ae4a8104SArtem Bityutskiy * the lists, writes the EC header if it is needed, and removes it from the
813ae4a8104SArtem Bityutskiy * list.
814ae4a8104SArtem Bityutskiy *
815ae4a8104SArtem Bityutskiy * This function returns a pointer to the "aeb" of the found free PEB in case
816ae4a8104SArtem Bityutskiy * of success and an error code in case of failure.
817ae4a8104SArtem Bityutskiy */
ubi_early_get_peb(struct ubi_device * ubi,struct ubi_attach_info * ai)818ae4a8104SArtem Bityutskiy struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
819ae4a8104SArtem Bityutskiy struct ubi_attach_info *ai)
820ae4a8104SArtem Bityutskiy {
821ae4a8104SArtem Bityutskiy int err = 0;
822ae4a8104SArtem Bityutskiy struct ubi_ainf_peb *aeb, *tmp_aeb;
823ae4a8104SArtem Bityutskiy
824ae4a8104SArtem Bityutskiy if (!list_empty(&ai->free)) {
825ae4a8104SArtem Bityutskiy aeb = list_entry(ai->free.next, struct ubi_ainf_peb, u.list);
826ae4a8104SArtem Bityutskiy list_del(&aeb->u.list);
827ae4a8104SArtem Bityutskiy dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec);
828ae4a8104SArtem Bityutskiy return aeb;
829ae4a8104SArtem Bityutskiy }
830ae4a8104SArtem Bityutskiy
831ae4a8104SArtem Bityutskiy /*
832ae4a8104SArtem Bityutskiy * We try to erase the first physical eraseblock from the erase list
833ae4a8104SArtem Bityutskiy * and pick it if we succeed, or try to erase the next one if not. And
834ae4a8104SArtem Bityutskiy * so forth. We don't want to take care about bad eraseblocks here -
835ae4a8104SArtem Bityutskiy * they'll be handled later.
836ae4a8104SArtem Bityutskiy */
837ae4a8104SArtem Bityutskiy list_for_each_entry_safe(aeb, tmp_aeb, &ai->erase, u.list) {
838ae4a8104SArtem Bityutskiy if (aeb->ec == UBI_UNKNOWN)
839ae4a8104SArtem Bityutskiy aeb->ec = ai->mean_ec;
840ae4a8104SArtem Bityutskiy
841ae4a8104SArtem Bityutskiy err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1);
842ae4a8104SArtem Bityutskiy if (err)
843ae4a8104SArtem Bityutskiy continue;
844ae4a8104SArtem Bityutskiy
845ae4a8104SArtem Bityutskiy aeb->ec += 1;
846ae4a8104SArtem Bityutskiy list_del(&aeb->u.list);
847ae4a8104SArtem Bityutskiy dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec);
848ae4a8104SArtem Bityutskiy return aeb;
849ae4a8104SArtem Bityutskiy }
850ae4a8104SArtem Bityutskiy
85132608703STanya Brokhman ubi_err(ubi, "no free eraseblocks");
852ae4a8104SArtem Bityutskiy return ERR_PTR(-ENOSPC);
853ae4a8104SArtem Bityutskiy }
854ae4a8104SArtem Bityutskiy
855ae4a8104SArtem Bityutskiy /**
856ae4a8104SArtem Bityutskiy * check_corruption - check the data area of PEB.
857ae4a8104SArtem Bityutskiy * @ubi: UBI device description object
858dac6e208SRichard Weinberger * @vid_hdr: the (corrupted) VID header of this PEB
859ae4a8104SArtem Bityutskiy * @pnum: the physical eraseblock number to check
860ae4a8104SArtem Bityutskiy *
861ae4a8104SArtem Bityutskiy * This is a helper function which is used to distinguish between VID header
862ae4a8104SArtem Bityutskiy * corruptions caused by power cuts and other reasons. If the PEB contains only
863ae4a8104SArtem Bityutskiy * 0xFF bytes in the data area, the VID header is most probably corrupted
864ae4a8104SArtem Bityutskiy * because of a power cut (%0 is returned in this case). Otherwise, it was
865ae4a8104SArtem Bityutskiy * probably corrupted for some other reasons (%1 is returned in this case). A
866ae4a8104SArtem Bityutskiy * negative error code is returned if a read error occurred.
867ae4a8104SArtem Bityutskiy *
868ae4a8104SArtem Bityutskiy * If the corruption reason was a power cut, UBI can safely erase this PEB.
869ae4a8104SArtem Bityutskiy * Otherwise, it should preserve it to avoid possibly destroying important
870ae4a8104SArtem Bityutskiy * information.
871ae4a8104SArtem Bityutskiy */
check_corruption(struct ubi_device * ubi,struct ubi_vid_hdr * vid_hdr,int pnum)872ae4a8104SArtem Bityutskiy static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
873ae4a8104SArtem Bityutskiy int pnum)
874ae4a8104SArtem Bityutskiy {
875ae4a8104SArtem Bityutskiy int err;
876ae4a8104SArtem Bityutskiy
877ae4a8104SArtem Bityutskiy mutex_lock(&ubi->buf_mutex);
878ae4a8104SArtem Bityutskiy memset(ubi->peb_buf, 0x00, ubi->leb_size);
879ae4a8104SArtem Bityutskiy
880ae4a8104SArtem Bityutskiy err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start,
881ae4a8104SArtem Bityutskiy ubi->leb_size);
882ae4a8104SArtem Bityutskiy if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) {
883ae4a8104SArtem Bityutskiy /*
884ae4a8104SArtem Bityutskiy * Bit-flips or integrity errors while reading the data area.
885ae4a8104SArtem Bityutskiy * It is difficult to say for sure what type of corruption is
886ae4a8104SArtem Bityutskiy * this, but presumably a power cut happened while this PEB was
887ae4a8104SArtem Bityutskiy * erased, so it became unstable and corrupted, and should be
888ae4a8104SArtem Bityutskiy * erased.
889ae4a8104SArtem Bityutskiy */
890ae4a8104SArtem Bityutskiy err = 0;
891ae4a8104SArtem Bityutskiy goto out_unlock;
892ae4a8104SArtem Bityutskiy }
893ae4a8104SArtem Bityutskiy
894ae4a8104SArtem Bityutskiy if (err)
895ae4a8104SArtem Bityutskiy goto out_unlock;
896ae4a8104SArtem Bityutskiy
897ae4a8104SArtem Bityutskiy if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size))
898ae4a8104SArtem Bityutskiy goto out_unlock;
899ae4a8104SArtem Bityutskiy
90032608703STanya Brokhman ubi_err(ubi, "PEB %d contains corrupted VID header, and the data does not contain all 0xFF",
901049333ceSArtem Bityutskiy pnum);
90232608703STanya Brokhman ubi_err(ubi, "this may be a non-UBI PEB or a severe VID header corruption which requires manual inspection");
903ae4a8104SArtem Bityutskiy ubi_dump_vid_hdr(vid_hdr);
904719bb840SArtem Bityutskiy pr_err("hexdump of PEB %d offset %d, length %d",
905ae4a8104SArtem Bityutskiy pnum, ubi->leb_start, ubi->leb_size);
906ae4a8104SArtem Bityutskiy ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
907ae4a8104SArtem Bityutskiy ubi->peb_buf, ubi->leb_size, 1);
908ae4a8104SArtem Bityutskiy err = 1;
909ae4a8104SArtem Bityutskiy
910ae4a8104SArtem Bityutskiy out_unlock:
911ae4a8104SArtem Bityutskiy mutex_unlock(&ubi->buf_mutex);
912ae4a8104SArtem Bityutskiy return err;
913ae4a8104SArtem Bityutskiy }
914ae4a8104SArtem Bityutskiy
vol_ignored(int vol_id)915243a4f81SRichard Weinberger static bool vol_ignored(int vol_id)
916243a4f81SRichard Weinberger {
917243a4f81SRichard Weinberger switch (vol_id) {
918243a4f81SRichard Weinberger case UBI_LAYOUT_VOLUME_ID:
919243a4f81SRichard Weinberger return true;
920243a4f81SRichard Weinberger }
921243a4f81SRichard Weinberger
922243a4f81SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
923243a4f81SRichard Weinberger return ubi_is_fm_vol(vol_id);
924243a4f81SRichard Weinberger #else
925243a4f81SRichard Weinberger return false;
926243a4f81SRichard Weinberger #endif
927243a4f81SRichard Weinberger }
928243a4f81SRichard Weinberger
929ae4a8104SArtem Bityutskiy /**
930ae4a8104SArtem Bityutskiy * scan_peb - scan and process UBI headers of a PEB.
931ae4a8104SArtem Bityutskiy * @ubi: UBI device description object
932ae4a8104SArtem Bityutskiy * @ai: attaching information
933ae4a8104SArtem Bityutskiy * @pnum: the physical eraseblock number
93474f2c6e9SRichard Weinberger * @fast: true if we're scanning for a Fastmap
935ae4a8104SArtem Bityutskiy *
936ae4a8104SArtem Bityutskiy * This function reads UBI headers of PEB @pnum, checks them, and adds
937ae4a8104SArtem Bityutskiy * information about this PEB to the corresponding list or RB-tree in the
938ae4a8104SArtem Bityutskiy * "attaching info" structure. Returns zero if the physical eraseblock was
939ae4a8104SArtem Bityutskiy * successfully handled and a negative error code in case of failure.
940ae4a8104SArtem Bityutskiy */
scan_peb(struct ubi_device * ubi,struct ubi_attach_info * ai,int pnum,bool fast)94174f2c6e9SRichard Weinberger static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai,
94274f2c6e9SRichard Weinberger int pnum, bool fast)
943ae4a8104SArtem Bityutskiy {
9447b6b749bSBoris Brezillon struct ubi_ec_hdr *ech = ai->ech;
9453291b52fSBoris Brezillon struct ubi_vid_io_buf *vidb = ai->vidb;
9463291b52fSBoris Brezillon struct ubi_vid_hdr *vidh = ubi_get_vid_hdr(vidb);
947fdf10ed7SRichard Weinberger long long ec;
948dac6e208SRichard Weinberger int err, bitflips = 0, vol_id = -1, ec_err = 0;
949ae4a8104SArtem Bityutskiy
950ae4a8104SArtem Bityutskiy dbg_bld("scan PEB %d", pnum);
951ae4a8104SArtem Bityutskiy
952ae4a8104SArtem Bityutskiy /* Skip bad physical eraseblocks */
953ae4a8104SArtem Bityutskiy err = ubi_io_is_bad(ubi, pnum);
954ae4a8104SArtem Bityutskiy if (err < 0)
955ae4a8104SArtem Bityutskiy return err;
956ae4a8104SArtem Bityutskiy else if (err) {
957ae4a8104SArtem Bityutskiy ai->bad_peb_count += 1;
958ae4a8104SArtem Bityutskiy return 0;
959ae4a8104SArtem Bityutskiy }
960ae4a8104SArtem Bityutskiy
961ae4a8104SArtem Bityutskiy err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
962ae4a8104SArtem Bityutskiy if (err < 0)
963ae4a8104SArtem Bityutskiy return err;
964ae4a8104SArtem Bityutskiy switch (err) {
965ae4a8104SArtem Bityutskiy case 0:
966ae4a8104SArtem Bityutskiy break;
967ae4a8104SArtem Bityutskiy case UBI_IO_BITFLIPS:
968ae4a8104SArtem Bityutskiy bitflips = 1;
969ae4a8104SArtem Bityutskiy break;
970ae4a8104SArtem Bityutskiy case UBI_IO_FF:
971ae4a8104SArtem Bityutskiy ai->empty_peb_count += 1;
9726dd3bc7eSJoel Reardon return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
9736dd3bc7eSJoel Reardon UBI_UNKNOWN, 0, &ai->erase);
974ae4a8104SArtem Bityutskiy case UBI_IO_FF_BITFLIPS:
975ae4a8104SArtem Bityutskiy ai->empty_peb_count += 1;
9766dd3bc7eSJoel Reardon return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
9776dd3bc7eSJoel Reardon UBI_UNKNOWN, 1, &ai->erase);
978ae4a8104SArtem Bityutskiy case UBI_IO_BAD_HDR_EBADMSG:
979ae4a8104SArtem Bityutskiy case UBI_IO_BAD_HDR:
980ae4a8104SArtem Bityutskiy /*
981ae4a8104SArtem Bityutskiy * We have to also look at the VID header, possibly it is not
982ae4a8104SArtem Bityutskiy * corrupted. Set %bitflips flag in order to make this PEB be
983ae4a8104SArtem Bityutskiy * moved and EC be re-created.
984ae4a8104SArtem Bityutskiy */
985ae4a8104SArtem Bityutskiy ec_err = err;
986ae4a8104SArtem Bityutskiy ec = UBI_UNKNOWN;
987ae4a8104SArtem Bityutskiy bitflips = 1;
988ae4a8104SArtem Bityutskiy break;
989ae4a8104SArtem Bityutskiy default:
99032608703STanya Brokhman ubi_err(ubi, "'ubi_io_read_ec_hdr()' returned unknown code %d",
99132608703STanya Brokhman err);
992ae4a8104SArtem Bityutskiy return -EINVAL;
993ae4a8104SArtem Bityutskiy }
994ae4a8104SArtem Bityutskiy
995ae4a8104SArtem Bityutskiy if (!ec_err) {
996ae4a8104SArtem Bityutskiy int image_seq;
997ae4a8104SArtem Bityutskiy
998ae4a8104SArtem Bityutskiy /* Make sure UBI version is OK */
999ae4a8104SArtem Bityutskiy if (ech->version != UBI_VERSION) {
100032608703STanya Brokhman ubi_err(ubi, "this UBI version is %d, image version is %d",
1001ae4a8104SArtem Bityutskiy UBI_VERSION, (int)ech->version);
1002ae4a8104SArtem Bityutskiy return -EINVAL;
1003ae4a8104SArtem Bityutskiy }
1004ae4a8104SArtem Bityutskiy
1005ae4a8104SArtem Bityutskiy ec = be64_to_cpu(ech->ec);
1006ae4a8104SArtem Bityutskiy if (ec > UBI_MAX_ERASECOUNTER) {
1007ae4a8104SArtem Bityutskiy /*
1008ae4a8104SArtem Bityutskiy * Erase counter overflow. The EC headers have 64 bits
1009ae4a8104SArtem Bityutskiy * reserved, but we anyway make use of only 31 bit
1010ae4a8104SArtem Bityutskiy * values, as this seems to be enough for any existing
1011ae4a8104SArtem Bityutskiy * flash. Upgrade UBI and use 64-bit erase counters
1012ae4a8104SArtem Bityutskiy * internally.
1013ae4a8104SArtem Bityutskiy */
101432608703STanya Brokhman ubi_err(ubi, "erase counter overflow, max is %d",
1015ae4a8104SArtem Bityutskiy UBI_MAX_ERASECOUNTER);
1016ae4a8104SArtem Bityutskiy ubi_dump_ec_hdr(ech);
1017ae4a8104SArtem Bityutskiy return -EINVAL;
1018ae4a8104SArtem Bityutskiy }
1019ae4a8104SArtem Bityutskiy
1020ae4a8104SArtem Bityutskiy /*
1021ae4a8104SArtem Bityutskiy * Make sure that all PEBs have the same image sequence number.
1022ae4a8104SArtem Bityutskiy * This allows us to detect situations when users flash UBI
1023ae4a8104SArtem Bityutskiy * images incorrectly, so that the flash has the new UBI image
1024ae4a8104SArtem Bityutskiy * and leftovers from the old one. This feature was added
1025ae4a8104SArtem Bityutskiy * relatively recently, and the sequence number was always
1026ae4a8104SArtem Bityutskiy * zero, because old UBI implementations always set it to zero.
1027ae4a8104SArtem Bityutskiy * For this reasons, we do not panic if some PEBs have zero
1028ae4a8104SArtem Bityutskiy * sequence number, while other PEBs have non-zero sequence
1029ae4a8104SArtem Bityutskiy * number.
1030ae4a8104SArtem Bityutskiy */
1031ae4a8104SArtem Bityutskiy image_seq = be32_to_cpu(ech->image_seq);
103255b80c40SRichard Genoud if (!ubi->image_seq)
1033ae4a8104SArtem Bityutskiy ubi->image_seq = image_seq;
103455b80c40SRichard Genoud if (image_seq && ubi->image_seq != image_seq) {
103532608703STanya Brokhman ubi_err(ubi, "bad image sequence number %d in PEB %d, expected %d",
1036049333ceSArtem Bityutskiy image_seq, pnum, ubi->image_seq);
1037ae4a8104SArtem Bityutskiy ubi_dump_ec_hdr(ech);
1038ae4a8104SArtem Bityutskiy return -EINVAL;
1039ae4a8104SArtem Bityutskiy }
1040ae4a8104SArtem Bityutskiy }
1041ae4a8104SArtem Bityutskiy
1042ae4a8104SArtem Bityutskiy /* OK, we've done with the EC header, let's look at the VID header */
1043ae4a8104SArtem Bityutskiy
10443291b52fSBoris Brezillon err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0);
1045ae4a8104SArtem Bityutskiy if (err < 0)
1046ae4a8104SArtem Bityutskiy return err;
1047ae4a8104SArtem Bityutskiy switch (err) {
1048ae4a8104SArtem Bityutskiy case 0:
1049ae4a8104SArtem Bityutskiy break;
1050ae4a8104SArtem Bityutskiy case UBI_IO_BITFLIPS:
1051ae4a8104SArtem Bityutskiy bitflips = 1;
1052ae4a8104SArtem Bityutskiy break;
1053ae4a8104SArtem Bityutskiy case UBI_IO_BAD_HDR_EBADMSG:
1054ae4a8104SArtem Bityutskiy if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
1055ae4a8104SArtem Bityutskiy /*
1056ae4a8104SArtem Bityutskiy * Both EC and VID headers are corrupted and were read
1057ae4a8104SArtem Bityutskiy * with data integrity error, probably this is a bad
1058ae4a8104SArtem Bityutskiy * PEB, bit it is not marked as bad yet. This may also
1059ae4a8104SArtem Bityutskiy * be a result of power cut during erasure.
1060ae4a8104SArtem Bityutskiy */
1061ae4a8104SArtem Bityutskiy ai->maybe_bad_peb_count += 1;
1062025a06c1SMiquel Raynal fallthrough;
1063ae4a8104SArtem Bityutskiy case UBI_IO_BAD_HDR:
106474f2c6e9SRichard Weinberger /*
106574f2c6e9SRichard Weinberger * If we're facing a bad VID header we have to drop *all*
106674f2c6e9SRichard Weinberger * Fastmap data structures we find. The most recent Fastmap
106774f2c6e9SRichard Weinberger * could be bad and therefore there is a chance that we attach
106874f2c6e9SRichard Weinberger * from an old one. On a fine MTD stack a PEB must not render
106974f2c6e9SRichard Weinberger * bad all of a sudden, but the reality is different.
107074f2c6e9SRichard Weinberger * So, let's be paranoid and help finding the root cause by
107174f2c6e9SRichard Weinberger * falling back to scanning mode instead of attaching with a
107274f2c6e9SRichard Weinberger * bad EBA table and cause data corruption which is hard to
107374f2c6e9SRichard Weinberger * analyze.
107474f2c6e9SRichard Weinberger */
107574f2c6e9SRichard Weinberger if (fast)
107674f2c6e9SRichard Weinberger ai->force_full_scan = 1;
107774f2c6e9SRichard Weinberger
1078ae4a8104SArtem Bityutskiy if (ec_err)
1079ae4a8104SArtem Bityutskiy /*
1080ae4a8104SArtem Bityutskiy * Both headers are corrupted. There is a possibility
1081ae4a8104SArtem Bityutskiy * that this a valid UBI PEB which has corresponding
1082ae4a8104SArtem Bityutskiy * LEB, but the headers are corrupted. However, it is
1083ae4a8104SArtem Bityutskiy * impossible to distinguish it from a PEB which just
1084ae4a8104SArtem Bityutskiy * contains garbage because of a power cut during erase
1085ae4a8104SArtem Bityutskiy * operation. So we just schedule this PEB for erasure.
1086ae4a8104SArtem Bityutskiy *
1087ae4a8104SArtem Bityutskiy * Besides, in case of NOR flash, we deliberately
1088ae4a8104SArtem Bityutskiy * corrupt both headers because NOR flash erasure is
1089ae4a8104SArtem Bityutskiy * slow and can start from the end.
1090ae4a8104SArtem Bityutskiy */
1091ae4a8104SArtem Bityutskiy err = 0;
1092ae4a8104SArtem Bityutskiy else
1093ae4a8104SArtem Bityutskiy /*
1094ae4a8104SArtem Bityutskiy * The EC was OK, but the VID header is corrupted. We
1095ae4a8104SArtem Bityutskiy * have to check what is in the data area.
1096ae4a8104SArtem Bityutskiy */
1097ae4a8104SArtem Bityutskiy err = check_corruption(ubi, vidh, pnum);
1098ae4a8104SArtem Bityutskiy
1099ae4a8104SArtem Bityutskiy if (err < 0)
1100ae4a8104SArtem Bityutskiy return err;
1101ae4a8104SArtem Bityutskiy else if (!err)
1102ae4a8104SArtem Bityutskiy /* This corruption is caused by a power cut */
11036dd3bc7eSJoel Reardon err = add_to_list(ai, pnum, UBI_UNKNOWN,
11046dd3bc7eSJoel Reardon UBI_UNKNOWN, ec, 1, &ai->erase);
1105ae4a8104SArtem Bityutskiy else
1106ae4a8104SArtem Bityutskiy /* This is an unexpected corruption */
1107ae4a8104SArtem Bityutskiy err = add_corrupted(ai, pnum, ec);
1108ae4a8104SArtem Bityutskiy if (err)
1109ae4a8104SArtem Bityutskiy return err;
1110ae4a8104SArtem Bityutskiy goto adjust_mean_ec;
1111ae4a8104SArtem Bityutskiy case UBI_IO_FF_BITFLIPS:
11126dd3bc7eSJoel Reardon err = add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
11136dd3bc7eSJoel Reardon ec, 1, &ai->erase);
1114ae4a8104SArtem Bityutskiy if (err)
1115ae4a8104SArtem Bityutskiy return err;
1116ae4a8104SArtem Bityutskiy goto adjust_mean_ec;
1117ae4a8104SArtem Bityutskiy case UBI_IO_FF:
1118193819cfSMatthieu CASTET if (ec_err || bitflips)
11196dd3bc7eSJoel Reardon err = add_to_list(ai, pnum, UBI_UNKNOWN,
11206dd3bc7eSJoel Reardon UBI_UNKNOWN, ec, 1, &ai->erase);
1121ae4a8104SArtem Bityutskiy else
11226dd3bc7eSJoel Reardon err = add_to_list(ai, pnum, UBI_UNKNOWN,
11236dd3bc7eSJoel Reardon UBI_UNKNOWN, ec, 0, &ai->free);
1124ae4a8104SArtem Bityutskiy if (err)
1125ae4a8104SArtem Bityutskiy return err;
1126ae4a8104SArtem Bityutskiy goto adjust_mean_ec;
1127ae4a8104SArtem Bityutskiy default:
112832608703STanya Brokhman ubi_err(ubi, "'ubi_io_read_vid_hdr()' returned unknown code %d",
1129ae4a8104SArtem Bityutskiy err);
1130ae4a8104SArtem Bityutskiy return -EINVAL;
1131ae4a8104SArtem Bityutskiy }
1132ae4a8104SArtem Bityutskiy
1133ae4a8104SArtem Bityutskiy vol_id = be32_to_cpu(vidh->vol_id);
1134243a4f81SRichard Weinberger if (vol_id > UBI_MAX_VOLUMES && !vol_ignored(vol_id)) {
1135ae4a8104SArtem Bityutskiy int lnum = be32_to_cpu(vidh->lnum);
1136ae4a8104SArtem Bityutskiy
1137ae4a8104SArtem Bityutskiy /* Unsupported internal volume */
1138ae4a8104SArtem Bityutskiy switch (vidh->compat) {
1139ae4a8104SArtem Bityutskiy case UBI_COMPAT_DELETE:
114032608703STanya Brokhman ubi_msg(ubi, "\"delete\" compatible internal volume %d:%d found, will remove it",
1141049333ceSArtem Bityutskiy vol_id, lnum);
1142243a4f81SRichard Weinberger
11436dd3bc7eSJoel Reardon err = add_to_list(ai, pnum, vol_id, lnum,
11446dd3bc7eSJoel Reardon ec, 1, &ai->erase);
1145ae4a8104SArtem Bityutskiy if (err)
1146ae4a8104SArtem Bityutskiy return err;
1147ae4a8104SArtem Bityutskiy return 0;
1148ae4a8104SArtem Bityutskiy
1149ae4a8104SArtem Bityutskiy case UBI_COMPAT_RO:
115032608703STanya Brokhman ubi_msg(ubi, "read-only compatible internal volume %d:%d found, switch to read-only mode",
1151ae4a8104SArtem Bityutskiy vol_id, lnum);
1152ae4a8104SArtem Bityutskiy ubi->ro_mode = 1;
1153ae4a8104SArtem Bityutskiy break;
1154ae4a8104SArtem Bityutskiy
1155ae4a8104SArtem Bityutskiy case UBI_COMPAT_PRESERVE:
115632608703STanya Brokhman ubi_msg(ubi, "\"preserve\" compatible internal volume %d:%d found",
1157049333ceSArtem Bityutskiy vol_id, lnum);
11586dd3bc7eSJoel Reardon err = add_to_list(ai, pnum, vol_id, lnum,
11596dd3bc7eSJoel Reardon ec, 0, &ai->alien);
1160ae4a8104SArtem Bityutskiy if (err)
1161ae4a8104SArtem Bityutskiy return err;
1162ae4a8104SArtem Bityutskiy return 0;
1163ae4a8104SArtem Bityutskiy
1164ae4a8104SArtem Bityutskiy case UBI_COMPAT_REJECT:
116532608703STanya Brokhman ubi_err(ubi, "incompatible internal volume %d:%d found",
1166ae4a8104SArtem Bityutskiy vol_id, lnum);
1167ae4a8104SArtem Bityutskiy return -EINVAL;
1168ae4a8104SArtem Bityutskiy }
1169ae4a8104SArtem Bityutskiy }
1170ae4a8104SArtem Bityutskiy
1171ae4a8104SArtem Bityutskiy if (ec_err)
117232608703STanya Brokhman ubi_warn(ubi, "valid VID header but corrupted EC header at PEB %d",
1173ae4a8104SArtem Bityutskiy pnum);
1174fdf10ed7SRichard Weinberger
1175fdf10ed7SRichard Weinberger if (ubi_is_fm_vol(vol_id))
1176fdf10ed7SRichard Weinberger err = add_fastmap(ai, pnum, vidh, ec);
1177fdf10ed7SRichard Weinberger else
1178ae4a8104SArtem Bityutskiy err = ubi_add_to_av(ubi, ai, pnum, ec, vidh, bitflips);
1179fdf10ed7SRichard Weinberger
1180ae4a8104SArtem Bityutskiy if (err)
1181ae4a8104SArtem Bityutskiy return err;
1182ae4a8104SArtem Bityutskiy
1183ae4a8104SArtem Bityutskiy adjust_mean_ec:
1184ae4a8104SArtem Bityutskiy if (!ec_err) {
1185ae4a8104SArtem Bityutskiy ai->ec_sum += ec;
1186ae4a8104SArtem Bityutskiy ai->ec_count += 1;
1187ae4a8104SArtem Bityutskiy if (ec > ai->max_ec)
1188ae4a8104SArtem Bityutskiy ai->max_ec = ec;
1189ae4a8104SArtem Bityutskiy if (ec < ai->min_ec)
1190ae4a8104SArtem Bityutskiy ai->min_ec = ec;
1191ae4a8104SArtem Bityutskiy }
1192ae4a8104SArtem Bityutskiy
1193ae4a8104SArtem Bityutskiy return 0;
1194ae4a8104SArtem Bityutskiy }
1195ae4a8104SArtem Bityutskiy
1196ae4a8104SArtem Bityutskiy /**
1197ae4a8104SArtem Bityutskiy * late_analysis - analyze the overall situation with PEB.
1198ae4a8104SArtem Bityutskiy * @ubi: UBI device description object
1199ae4a8104SArtem Bityutskiy * @ai: attaching information
1200ae4a8104SArtem Bityutskiy *
1201ae4a8104SArtem Bityutskiy * This is a helper function which takes a look what PEBs we have after we
1202ae4a8104SArtem Bityutskiy * gather information about all of them ("ai" is compete). It decides whether
1203ae4a8104SArtem Bityutskiy * the flash is empty and should be formatted of whether there are too many
1204ae4a8104SArtem Bityutskiy * corrupted PEBs and we should not attach this MTD device. Returns zero if we
1205ae4a8104SArtem Bityutskiy * should proceed with attaching the MTD device, and %-EINVAL if we should not.
1206ae4a8104SArtem Bityutskiy */
late_analysis(struct ubi_device * ubi,struct ubi_attach_info * ai)1207ae4a8104SArtem Bityutskiy static int late_analysis(struct ubi_device *ubi, struct ubi_attach_info *ai)
1208ae4a8104SArtem Bityutskiy {
1209ae4a8104SArtem Bityutskiy struct ubi_ainf_peb *aeb;
1210ae4a8104SArtem Bityutskiy int max_corr, peb_count;
1211ae4a8104SArtem Bityutskiy
1212ae4a8104SArtem Bityutskiy peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count;
1213ae4a8104SArtem Bityutskiy max_corr = peb_count / 20 ?: 8;
1214ae4a8104SArtem Bityutskiy
1215ae4a8104SArtem Bityutskiy /*
1216ae4a8104SArtem Bityutskiy * Few corrupted PEBs is not a problem and may be just a result of
1217ae4a8104SArtem Bityutskiy * unclean reboots. However, many of them may indicate some problems
1218ae4a8104SArtem Bityutskiy * with the flash HW or driver.
1219ae4a8104SArtem Bityutskiy */
1220ae4a8104SArtem Bityutskiy if (ai->corr_peb_count) {
122132608703STanya Brokhman ubi_err(ubi, "%d PEBs are corrupted and preserved",
1222ae4a8104SArtem Bityutskiy ai->corr_peb_count);
1223e28453bbSArtem Bityutskiy pr_err("Corrupted PEBs are:");
1224ae4a8104SArtem Bityutskiy list_for_each_entry(aeb, &ai->corr, u.list)
1225e28453bbSArtem Bityutskiy pr_cont(" %d", aeb->pnum);
1226e28453bbSArtem Bityutskiy pr_cont("\n");
1227ae4a8104SArtem Bityutskiy
1228ae4a8104SArtem Bityutskiy /*
1229ae4a8104SArtem Bityutskiy * If too many PEBs are corrupted, we refuse attaching,
1230ae4a8104SArtem Bityutskiy * otherwise, only print a warning.
1231ae4a8104SArtem Bityutskiy */
1232ae4a8104SArtem Bityutskiy if (ai->corr_peb_count >= max_corr) {
123332608703STanya Brokhman ubi_err(ubi, "too many corrupted PEBs, refusing");
1234ae4a8104SArtem Bityutskiy return -EINVAL;
1235ae4a8104SArtem Bityutskiy }
1236ae4a8104SArtem Bityutskiy }
1237ae4a8104SArtem Bityutskiy
1238ae4a8104SArtem Bityutskiy if (ai->empty_peb_count + ai->maybe_bad_peb_count == peb_count) {
1239ae4a8104SArtem Bityutskiy /*
1240ae4a8104SArtem Bityutskiy * All PEBs are empty, or almost all - a couple PEBs look like
1241ae4a8104SArtem Bityutskiy * they may be bad PEBs which were not marked as bad yet.
1242ae4a8104SArtem Bityutskiy *
1243ae4a8104SArtem Bityutskiy * This piece of code basically tries to distinguish between
1244ae4a8104SArtem Bityutskiy * the following situations:
1245ae4a8104SArtem Bityutskiy *
1246ae4a8104SArtem Bityutskiy * 1. Flash is empty, but there are few bad PEBs, which are not
1247ae4a8104SArtem Bityutskiy * marked as bad so far, and which were read with error. We
1248ae4a8104SArtem Bityutskiy * want to go ahead and format this flash. While formatting,
1249ae4a8104SArtem Bityutskiy * the faulty PEBs will probably be marked as bad.
1250ae4a8104SArtem Bityutskiy *
1251ae4a8104SArtem Bityutskiy * 2. Flash contains non-UBI data and we do not want to format
1252ae4a8104SArtem Bityutskiy * it and destroy possibly important information.
1253ae4a8104SArtem Bityutskiy */
1254ae4a8104SArtem Bityutskiy if (ai->maybe_bad_peb_count <= 2) {
1255ae4a8104SArtem Bityutskiy ai->is_empty = 1;
125632608703STanya Brokhman ubi_msg(ubi, "empty MTD device detected");
1257ae4a8104SArtem Bityutskiy get_random_bytes(&ubi->image_seq,
1258ae4a8104SArtem Bityutskiy sizeof(ubi->image_seq));
1259ae4a8104SArtem Bityutskiy } else {
126032608703STanya Brokhman ubi_err(ubi, "MTD device is not UBI-formatted and possibly contains non-UBI data - refusing it");
1261ae4a8104SArtem Bityutskiy return -EINVAL;
1262ae4a8104SArtem Bityutskiy }
1263ae4a8104SArtem Bityutskiy
1264ae4a8104SArtem Bityutskiy }
1265ae4a8104SArtem Bityutskiy
1266ae4a8104SArtem Bityutskiy return 0;
1267ae4a8104SArtem Bityutskiy }
1268ae4a8104SArtem Bityutskiy
1269ae4a8104SArtem Bityutskiy /**
1270ae4a8104SArtem Bityutskiy * destroy_av - free volume attaching information.
1271ae4a8104SArtem Bityutskiy * @av: volume attaching information
1272ae4a8104SArtem Bityutskiy * @ai: attaching information
1273f9efe8d8SBoris Brezillon * @list: put the aeb elements in there if !NULL, otherwise free them
1274ae4a8104SArtem Bityutskiy *
1275ae4a8104SArtem Bityutskiy * This function destroys the volume attaching information.
1276ae4a8104SArtem Bityutskiy */
destroy_av(struct ubi_attach_info * ai,struct ubi_ainf_volume * av,struct list_head * list)1277f9efe8d8SBoris Brezillon static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av,
1278f9efe8d8SBoris Brezillon struct list_head *list)
1279ae4a8104SArtem Bityutskiy {
1280ae4a8104SArtem Bityutskiy struct ubi_ainf_peb *aeb;
1281ae4a8104SArtem Bityutskiy struct rb_node *this = av->root.rb_node;
1282ae4a8104SArtem Bityutskiy
1283ae4a8104SArtem Bityutskiy while (this) {
1284ae4a8104SArtem Bityutskiy if (this->rb_left)
1285ae4a8104SArtem Bityutskiy this = this->rb_left;
1286ae4a8104SArtem Bityutskiy else if (this->rb_right)
1287ae4a8104SArtem Bityutskiy this = this->rb_right;
1288ae4a8104SArtem Bityutskiy else {
1289ae4a8104SArtem Bityutskiy aeb = rb_entry(this, struct ubi_ainf_peb, u.rb);
1290ae4a8104SArtem Bityutskiy this = rb_parent(this);
1291ae4a8104SArtem Bityutskiy if (this) {
1292ae4a8104SArtem Bityutskiy if (this->rb_left == &aeb->u.rb)
1293ae4a8104SArtem Bityutskiy this->rb_left = NULL;
1294ae4a8104SArtem Bityutskiy else
1295ae4a8104SArtem Bityutskiy this->rb_right = NULL;
1296ae4a8104SArtem Bityutskiy }
1297ae4a8104SArtem Bityutskiy
1298f9efe8d8SBoris Brezillon if (list)
1299f9efe8d8SBoris Brezillon list_add_tail(&aeb->u.list, list);
1300f9efe8d8SBoris Brezillon else
130191f4285fSBoris Brezillon ubi_free_aeb(ai, aeb);
1302ae4a8104SArtem Bityutskiy }
1303ae4a8104SArtem Bityutskiy }
1304ae4a8104SArtem Bityutskiy kfree(av);
1305ae4a8104SArtem Bityutskiy }
1306ae4a8104SArtem Bityutskiy
1307ae4a8104SArtem Bityutskiy /**
1308dac6e208SRichard Weinberger * destroy_ai - destroy attaching information.
1309ae4a8104SArtem Bityutskiy * @ai: attaching information
1310ae4a8104SArtem Bityutskiy */
destroy_ai(struct ubi_attach_info * ai)1311dac6e208SRichard Weinberger static void destroy_ai(struct ubi_attach_info *ai)
1312ae4a8104SArtem Bityutskiy {
1313ae4a8104SArtem Bityutskiy struct ubi_ainf_peb *aeb, *aeb_tmp;
1314ae4a8104SArtem Bityutskiy struct ubi_ainf_volume *av;
1315ae4a8104SArtem Bityutskiy struct rb_node *rb;
1316ae4a8104SArtem Bityutskiy
1317ae4a8104SArtem Bityutskiy list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) {
1318ae4a8104SArtem Bityutskiy list_del(&aeb->u.list);
131991f4285fSBoris Brezillon ubi_free_aeb(ai, aeb);
1320ae4a8104SArtem Bityutskiy }
1321ae4a8104SArtem Bityutskiy list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) {
1322ae4a8104SArtem Bityutskiy list_del(&aeb->u.list);
132391f4285fSBoris Brezillon ubi_free_aeb(ai, aeb);
1324ae4a8104SArtem Bityutskiy }
1325ae4a8104SArtem Bityutskiy list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) {
1326ae4a8104SArtem Bityutskiy list_del(&aeb->u.list);
132791f4285fSBoris Brezillon ubi_free_aeb(ai, aeb);
1328ae4a8104SArtem Bityutskiy }
1329ae4a8104SArtem Bityutskiy list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) {
1330ae4a8104SArtem Bityutskiy list_del(&aeb->u.list);
133191f4285fSBoris Brezillon ubi_free_aeb(ai, aeb);
1332ae4a8104SArtem Bityutskiy }
1333fdf10ed7SRichard Weinberger list_for_each_entry_safe(aeb, aeb_tmp, &ai->fastmap, u.list) {
1334fdf10ed7SRichard Weinberger list_del(&aeb->u.list);
133591f4285fSBoris Brezillon ubi_free_aeb(ai, aeb);
1336fdf10ed7SRichard Weinberger }
1337ae4a8104SArtem Bityutskiy
1338ae4a8104SArtem Bityutskiy /* Destroy the volume RB-tree */
1339ae4a8104SArtem Bityutskiy rb = ai->volumes.rb_node;
1340ae4a8104SArtem Bityutskiy while (rb) {
1341ae4a8104SArtem Bityutskiy if (rb->rb_left)
1342ae4a8104SArtem Bityutskiy rb = rb->rb_left;
1343ae4a8104SArtem Bityutskiy else if (rb->rb_right)
1344ae4a8104SArtem Bityutskiy rb = rb->rb_right;
1345ae4a8104SArtem Bityutskiy else {
1346ae4a8104SArtem Bityutskiy av = rb_entry(rb, struct ubi_ainf_volume, rb);
1347ae4a8104SArtem Bityutskiy
1348ae4a8104SArtem Bityutskiy rb = rb_parent(rb);
1349ae4a8104SArtem Bityutskiy if (rb) {
1350ae4a8104SArtem Bityutskiy if (rb->rb_left == &av->rb)
1351ae4a8104SArtem Bityutskiy rb->rb_left = NULL;
1352ae4a8104SArtem Bityutskiy else
1353ae4a8104SArtem Bityutskiy rb->rb_right = NULL;
1354ae4a8104SArtem Bityutskiy }
1355ae4a8104SArtem Bityutskiy
1356f9efe8d8SBoris Brezillon destroy_av(ai, av, NULL);
1357ae4a8104SArtem Bityutskiy }
1358ae4a8104SArtem Bityutskiy }
1359ae4a8104SArtem Bityutskiy
1360ae4a8104SArtem Bityutskiy kmem_cache_destroy(ai->aeb_slab_cache);
1361ae4a8104SArtem Bityutskiy kfree(ai);
1362ae4a8104SArtem Bityutskiy }
1363ae4a8104SArtem Bityutskiy
1364ae4a8104SArtem Bityutskiy /**
1365dac6e208SRichard Weinberger * scan_all - scan entire MTD device.
1366dac6e208SRichard Weinberger * @ubi: UBI device description object
1367dac6e208SRichard Weinberger * @ai: attach info object
1368dac6e208SRichard Weinberger * @start: start scanning at this PEB
1369dac6e208SRichard Weinberger *
1370dac6e208SRichard Weinberger * This function does full scanning of an MTD device and returns complete
1371dac6e208SRichard Weinberger * information about it in form of a "struct ubi_attach_info" object. In case
1372dac6e208SRichard Weinberger * of failure, an error code is returned.
1373dac6e208SRichard Weinberger */
scan_all(struct ubi_device * ubi,struct ubi_attach_info * ai,int start)1374dac6e208SRichard Weinberger static int scan_all(struct ubi_device *ubi, struct ubi_attach_info *ai,
1375dac6e208SRichard Weinberger int start)
1376dac6e208SRichard Weinberger {
1377dac6e208SRichard Weinberger int err, pnum;
1378dac6e208SRichard Weinberger struct rb_node *rb1, *rb2;
1379dac6e208SRichard Weinberger struct ubi_ainf_volume *av;
1380dac6e208SRichard Weinberger struct ubi_ainf_peb *aeb;
1381dac6e208SRichard Weinberger
1382dac6e208SRichard Weinberger err = -ENOMEM;
1383dac6e208SRichard Weinberger
13847b6b749bSBoris Brezillon ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
13857b6b749bSBoris Brezillon if (!ai->ech)
1386dac6e208SRichard Weinberger return err;
1387dac6e208SRichard Weinberger
13883291b52fSBoris Brezillon ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
13893291b52fSBoris Brezillon if (!ai->vidb)
1390dac6e208SRichard Weinberger goto out_ech;
1391dac6e208SRichard Weinberger
1392dac6e208SRichard Weinberger for (pnum = start; pnum < ubi->peb_count; pnum++) {
1393dac6e208SRichard Weinberger cond_resched();
1394dac6e208SRichard Weinberger
1395dac6e208SRichard Weinberger dbg_gen("process PEB %d", pnum);
139674f2c6e9SRichard Weinberger err = scan_peb(ubi, ai, pnum, false);
1397dac6e208SRichard Weinberger if (err < 0)
1398dac6e208SRichard Weinberger goto out_vidh;
1399dac6e208SRichard Weinberger }
1400dac6e208SRichard Weinberger
140132608703STanya Brokhman ubi_msg(ubi, "scanning is finished");
1402dac6e208SRichard Weinberger
1403dac6e208SRichard Weinberger /* Calculate mean erase counter */
1404dac6e208SRichard Weinberger if (ai->ec_count)
1405dac6e208SRichard Weinberger ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
1406dac6e208SRichard Weinberger
1407dac6e208SRichard Weinberger err = late_analysis(ubi, ai);
1408dac6e208SRichard Weinberger if (err)
1409dac6e208SRichard Weinberger goto out_vidh;
1410dac6e208SRichard Weinberger
1411dac6e208SRichard Weinberger /*
1412dac6e208SRichard Weinberger * In case of unknown erase counter we use the mean erase counter
1413dac6e208SRichard Weinberger * value.
1414dac6e208SRichard Weinberger */
1415dac6e208SRichard Weinberger ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1416dac6e208SRichard Weinberger ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
1417dac6e208SRichard Weinberger if (aeb->ec == UBI_UNKNOWN)
1418dac6e208SRichard Weinberger aeb->ec = ai->mean_ec;
1419dac6e208SRichard Weinberger }
1420dac6e208SRichard Weinberger
1421dac6e208SRichard Weinberger list_for_each_entry(aeb, &ai->free, u.list) {
1422dac6e208SRichard Weinberger if (aeb->ec == UBI_UNKNOWN)
1423dac6e208SRichard Weinberger aeb->ec = ai->mean_ec;
1424dac6e208SRichard Weinberger }
1425dac6e208SRichard Weinberger
1426dac6e208SRichard Weinberger list_for_each_entry(aeb, &ai->corr, u.list)
1427dac6e208SRichard Weinberger if (aeb->ec == UBI_UNKNOWN)
1428dac6e208SRichard Weinberger aeb->ec = ai->mean_ec;
1429dac6e208SRichard Weinberger
1430dac6e208SRichard Weinberger list_for_each_entry(aeb, &ai->erase, u.list)
1431dac6e208SRichard Weinberger if (aeb->ec == UBI_UNKNOWN)
1432dac6e208SRichard Weinberger aeb->ec = ai->mean_ec;
1433dac6e208SRichard Weinberger
1434dac6e208SRichard Weinberger err = self_check_ai(ubi, ai);
1435dac6e208SRichard Weinberger if (err)
1436dac6e208SRichard Weinberger goto out_vidh;
1437dac6e208SRichard Weinberger
14383291b52fSBoris Brezillon ubi_free_vid_buf(ai->vidb);
14397b6b749bSBoris Brezillon kfree(ai->ech);
1440dac6e208SRichard Weinberger
1441dac6e208SRichard Weinberger return 0;
1442dac6e208SRichard Weinberger
1443dac6e208SRichard Weinberger out_vidh:
14443291b52fSBoris Brezillon ubi_free_vid_buf(ai->vidb);
1445dac6e208SRichard Weinberger out_ech:
14467b6b749bSBoris Brezillon kfree(ai->ech);
1447dac6e208SRichard Weinberger return err;
1448dac6e208SRichard Weinberger }
1449dac6e208SRichard Weinberger
alloc_ai(const char * slab_name)1450*612824ddSZhihao Cheng static struct ubi_attach_info *alloc_ai(const char *slab_name)
1451dac6e208SRichard Weinberger {
1452dac6e208SRichard Weinberger struct ubi_attach_info *ai;
1453dac6e208SRichard Weinberger
1454dac6e208SRichard Weinberger ai = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL);
1455dac6e208SRichard Weinberger if (!ai)
1456dac6e208SRichard Weinberger return ai;
1457dac6e208SRichard Weinberger
1458dac6e208SRichard Weinberger INIT_LIST_HEAD(&ai->corr);
1459dac6e208SRichard Weinberger INIT_LIST_HEAD(&ai->free);
1460dac6e208SRichard Weinberger INIT_LIST_HEAD(&ai->erase);
1461dac6e208SRichard Weinberger INIT_LIST_HEAD(&ai->alien);
1462fdf10ed7SRichard Weinberger INIT_LIST_HEAD(&ai->fastmap);
1463dac6e208SRichard Weinberger ai->volumes = RB_ROOT;
1464*612824ddSZhihao Cheng ai->aeb_slab_cache = kmem_cache_create(slab_name,
1465dac6e208SRichard Weinberger sizeof(struct ubi_ainf_peb),
1466dac6e208SRichard Weinberger 0, 0, NULL);
1467dac6e208SRichard Weinberger if (!ai->aeb_slab_cache) {
1468dac6e208SRichard Weinberger kfree(ai);
1469dac6e208SRichard Weinberger ai = NULL;
1470dac6e208SRichard Weinberger }
1471dac6e208SRichard Weinberger
1472dac6e208SRichard Weinberger return ai;
1473dac6e208SRichard Weinberger }
1474dac6e208SRichard Weinberger
147598105d08SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
147698105d08SRichard Weinberger
147798105d08SRichard Weinberger /**
1478d139d300SRichard Weinberger * scan_fast - try to find a fastmap and attach from it.
147998105d08SRichard Weinberger * @ubi: UBI device description object
148098105d08SRichard Weinberger * @ai: attach info object
148198105d08SRichard Weinberger *
148298105d08SRichard Weinberger * Returns 0 on success, negative return values indicate an internal
148398105d08SRichard Weinberger * error.
148498105d08SRichard Weinberger * UBI_NO_FASTMAP denotes that no fastmap was found.
148598105d08SRichard Weinberger * UBI_BAD_FASTMAP denotes that the found fastmap was invalid.
148698105d08SRichard Weinberger */
scan_fast(struct ubi_device * ubi,struct ubi_attach_info ** ai)148798105d08SRichard Weinberger static int scan_fast(struct ubi_device *ubi, struct ubi_attach_info **ai)
148898105d08SRichard Weinberger {
1489fdf10ed7SRichard Weinberger int err, pnum;
1490fdf10ed7SRichard Weinberger struct ubi_attach_info *scan_ai;
149198105d08SRichard Weinberger
149298105d08SRichard Weinberger err = -ENOMEM;
149398105d08SRichard Weinberger
1494*612824ddSZhihao Cheng scan_ai = alloc_ai("ubi_aeb_slab_cache_fastmap");
1495fdf10ed7SRichard Weinberger if (!scan_ai)
1496fdf10ed7SRichard Weinberger goto out;
1497fdf10ed7SRichard Weinberger
14987b6b749bSBoris Brezillon scan_ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
14997b6b749bSBoris Brezillon if (!scan_ai->ech)
1500fdf10ed7SRichard Weinberger goto out_ai;
150198105d08SRichard Weinberger
15023291b52fSBoris Brezillon scan_ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
15033291b52fSBoris Brezillon if (!scan_ai->vidb)
150498105d08SRichard Weinberger goto out_ech;
150598105d08SRichard Weinberger
150698105d08SRichard Weinberger for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
150798105d08SRichard Weinberger cond_resched();
150898105d08SRichard Weinberger
150998105d08SRichard Weinberger dbg_gen("process PEB %d", pnum);
151074f2c6e9SRichard Weinberger err = scan_peb(ubi, scan_ai, pnum, true);
151198105d08SRichard Weinberger if (err < 0)
151298105d08SRichard Weinberger goto out_vidh;
151398105d08SRichard Weinberger }
151498105d08SRichard Weinberger
15153291b52fSBoris Brezillon ubi_free_vid_buf(scan_ai->vidb);
15167b6b749bSBoris Brezillon kfree(scan_ai->ech);
151798105d08SRichard Weinberger
151874f2c6e9SRichard Weinberger if (scan_ai->force_full_scan)
151974f2c6e9SRichard Weinberger err = UBI_NO_FASTMAP;
152074f2c6e9SRichard Weinberger else
1521fdf10ed7SRichard Weinberger err = ubi_scan_fastmap(ubi, *ai, scan_ai);
152274f2c6e9SRichard Weinberger
1523fdf10ed7SRichard Weinberger if (err) {
1524fdf10ed7SRichard Weinberger /*
1525fdf10ed7SRichard Weinberger * Didn't attach via fastmap, do a full scan but reuse what
1526fdf10ed7SRichard Weinberger * we've aready scanned.
1527fdf10ed7SRichard Weinberger */
152898105d08SRichard Weinberger destroy_ai(*ai);
1529fdf10ed7SRichard Weinberger *ai = scan_ai;
1530fdf10ed7SRichard Weinberger } else
1531fdf10ed7SRichard Weinberger destroy_ai(scan_ai);
153298105d08SRichard Weinberger
1533fdf10ed7SRichard Weinberger return err;
153498105d08SRichard Weinberger
153598105d08SRichard Weinberger out_vidh:
15363291b52fSBoris Brezillon ubi_free_vid_buf(scan_ai->vidb);
153798105d08SRichard Weinberger out_ech:
15387b6b749bSBoris Brezillon kfree(scan_ai->ech);
1539fdf10ed7SRichard Weinberger out_ai:
1540fdf10ed7SRichard Weinberger destroy_ai(scan_ai);
154198105d08SRichard Weinberger out:
154298105d08SRichard Weinberger return err;
154398105d08SRichard Weinberger }
154498105d08SRichard Weinberger
154598105d08SRichard Weinberger #endif
154698105d08SRichard Weinberger
1547dac6e208SRichard Weinberger /**
1548dac6e208SRichard Weinberger * ubi_attach - attach an MTD device.
1549dac6e208SRichard Weinberger * @ubi: UBI device descriptor
1550dac6e208SRichard Weinberger * @force_scan: if set to non-zero attach by scanning
1551dac6e208SRichard Weinberger *
1552dac6e208SRichard Weinberger * This function returns zero in case of success and a negative error code in
1553dac6e208SRichard Weinberger * case of failure.
1554dac6e208SRichard Weinberger */
ubi_attach(struct ubi_device * ubi,int force_scan)1555dac6e208SRichard Weinberger int ubi_attach(struct ubi_device *ubi, int force_scan)
1556dac6e208SRichard Weinberger {
1557dac6e208SRichard Weinberger int err;
1558dac6e208SRichard Weinberger struct ubi_attach_info *ai;
1559dac6e208SRichard Weinberger
1560*612824ddSZhihao Cheng ai = alloc_ai("ubi_aeb_slab_cache");
1561dac6e208SRichard Weinberger if (!ai)
1562dac6e208SRichard Weinberger return -ENOMEM;
1563dac6e208SRichard Weinberger
1564dac6e208SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
1565dac6e208SRichard Weinberger /* On small flash devices we disable fastmap in any case. */
1566dac6e208SRichard Weinberger if ((int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd) <= UBI_FM_MAX_START) {
1567dac6e208SRichard Weinberger ubi->fm_disabled = 1;
1568dac6e208SRichard Weinberger force_scan = 1;
1569dac6e208SRichard Weinberger }
1570dac6e208SRichard Weinberger
1571dac6e208SRichard Weinberger if (force_scan)
1572dac6e208SRichard Weinberger err = scan_all(ubi, ai, 0);
1573dac6e208SRichard Weinberger else {
157498105d08SRichard Weinberger err = scan_fast(ubi, &ai);
1575180a5357SRichard Weinberger if (err > 0 || mtd_is_eccerr(err)) {
1576dac6e208SRichard Weinberger if (err != UBI_NO_FASTMAP) {
1577dac6e208SRichard Weinberger destroy_ai(ai);
1578*612824ddSZhihao Cheng ai = alloc_ai("ubi_aeb_slab_cache");
1579dac6e208SRichard Weinberger if (!ai)
1580dac6e208SRichard Weinberger return -ENOMEM;
1581dac6e208SRichard Weinberger
15824b3e0a25SRichard Weinberger err = scan_all(ubi, ai, 0);
15834b3e0a25SRichard Weinberger } else {
1584dac6e208SRichard Weinberger err = scan_all(ubi, ai, UBI_FM_MAX_START);
1585dac6e208SRichard Weinberger }
1586dac6e208SRichard Weinberger }
15874b3e0a25SRichard Weinberger }
1588dac6e208SRichard Weinberger #else
1589dac6e208SRichard Weinberger err = scan_all(ubi, ai, 0);
1590dac6e208SRichard Weinberger #endif
1591dac6e208SRichard Weinberger if (err)
1592dac6e208SRichard Weinberger goto out_ai;
1593dac6e208SRichard Weinberger
1594dac6e208SRichard Weinberger ubi->bad_peb_count = ai->bad_peb_count;
1595dac6e208SRichard Weinberger ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
1596dac6e208SRichard Weinberger ubi->corr_peb_count = ai->corr_peb_count;
1597dac6e208SRichard Weinberger ubi->max_ec = ai->max_ec;
1598dac6e208SRichard Weinberger ubi->mean_ec = ai->mean_ec;
1599dac6e208SRichard Weinberger dbg_gen("max. sequence number: %llu", ai->max_sqnum);
1600dac6e208SRichard Weinberger
1601dac6e208SRichard Weinberger err = ubi_read_volume_table(ubi, ai);
1602dac6e208SRichard Weinberger if (err)
1603dac6e208SRichard Weinberger goto out_ai;
1604dac6e208SRichard Weinberger
1605dac6e208SRichard Weinberger err = ubi_wl_init(ubi, ai);
1606dac6e208SRichard Weinberger if (err)
1607dac6e208SRichard Weinberger goto out_vtbl;
1608dac6e208SRichard Weinberger
1609dac6e208SRichard Weinberger err = ubi_eba_init(ubi, ai);
1610dac6e208SRichard Weinberger if (err)
1611dac6e208SRichard Weinberger goto out_wl;
1612dac6e208SRichard Weinberger
1613dac6e208SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
1614560d86a1SRichard Weinberger if (ubi->fm && ubi_dbg_chk_fastmap(ubi)) {
1615dac6e208SRichard Weinberger struct ubi_attach_info *scan_ai;
1616dac6e208SRichard Weinberger
1617*612824ddSZhihao Cheng scan_ai = alloc_ai("ubi_aeb_slab_cache_dbg_chk_fastmap");
16184d525145SJulia Lawall if (!scan_ai) {
16194d525145SJulia Lawall err = -ENOMEM;
1620dac6e208SRichard Weinberger goto out_wl;
16214d525145SJulia Lawall }
1622dac6e208SRichard Weinberger
1623dac6e208SRichard Weinberger err = scan_all(ubi, scan_ai, 0);
1624dac6e208SRichard Weinberger if (err) {
1625dac6e208SRichard Weinberger destroy_ai(scan_ai);
1626dac6e208SRichard Weinberger goto out_wl;
1627dac6e208SRichard Weinberger }
1628dac6e208SRichard Weinberger
1629dac6e208SRichard Weinberger err = self_check_eba(ubi, ai, scan_ai);
1630dac6e208SRichard Weinberger destroy_ai(scan_ai);
1631dac6e208SRichard Weinberger
1632dac6e208SRichard Weinberger if (err)
1633dac6e208SRichard Weinberger goto out_wl;
1634dac6e208SRichard Weinberger }
1635dac6e208SRichard Weinberger #endif
1636dac6e208SRichard Weinberger
1637dac6e208SRichard Weinberger destroy_ai(ai);
1638dac6e208SRichard Weinberger return 0;
1639dac6e208SRichard Weinberger
1640dac6e208SRichard Weinberger out_wl:
1641dac6e208SRichard Weinberger ubi_wl_close(ubi);
1642dac6e208SRichard Weinberger out_vtbl:
1643fc55dacfSHou Tao ubi_free_all_volumes(ubi);
1644dac6e208SRichard Weinberger vfree(ubi->vtbl);
1645dac6e208SRichard Weinberger out_ai:
1646dac6e208SRichard Weinberger destroy_ai(ai);
1647dac6e208SRichard Weinberger return err;
1648dac6e208SRichard Weinberger }
1649dac6e208SRichard Weinberger
1650dac6e208SRichard Weinberger /**
1651ae4a8104SArtem Bityutskiy * self_check_ai - check the attaching information.
1652ae4a8104SArtem Bityutskiy * @ubi: UBI device description object
1653ae4a8104SArtem Bityutskiy * @ai: attaching information
1654ae4a8104SArtem Bityutskiy *
1655ae4a8104SArtem Bityutskiy * This function returns zero if the attaching information is all right, and a
1656ae4a8104SArtem Bityutskiy * negative error code if not or if an error occurred.
1657ae4a8104SArtem Bityutskiy */
self_check_ai(struct ubi_device * ubi,struct ubi_attach_info * ai)1658ae4a8104SArtem Bityutskiy static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai)
1659ae4a8104SArtem Bityutskiy {
16603291b52fSBoris Brezillon struct ubi_vid_io_buf *vidb = ai->vidb;
16613291b52fSBoris Brezillon struct ubi_vid_hdr *vidh = ubi_get_vid_hdr(vidb);
1662ae4a8104SArtem Bityutskiy int pnum, err, vols_found = 0;
1663ae4a8104SArtem Bityutskiy struct rb_node *rb1, *rb2;
1664ae4a8104SArtem Bityutskiy struct ubi_ainf_volume *av;
1665ae4a8104SArtem Bityutskiy struct ubi_ainf_peb *aeb, *last_aeb;
1666ae4a8104SArtem Bityutskiy uint8_t *buf;
1667ae4a8104SArtem Bityutskiy
166864575574SEzequiel Garcia if (!ubi_dbg_chk_gen(ubi))
1669ae4a8104SArtem Bityutskiy return 0;
1670ae4a8104SArtem Bityutskiy
1671ae4a8104SArtem Bityutskiy /*
1672ae4a8104SArtem Bityutskiy * At first, check that attaching information is OK.
1673ae4a8104SArtem Bityutskiy */
1674ae4a8104SArtem Bityutskiy ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1675ae4a8104SArtem Bityutskiy int leb_count = 0;
1676ae4a8104SArtem Bityutskiy
1677ae4a8104SArtem Bityutskiy cond_resched();
1678ae4a8104SArtem Bityutskiy
1679ae4a8104SArtem Bityutskiy vols_found += 1;
1680ae4a8104SArtem Bityutskiy
1681ae4a8104SArtem Bityutskiy if (ai->is_empty) {
168232608703STanya Brokhman ubi_err(ubi, "bad is_empty flag");
1683ae4a8104SArtem Bityutskiy goto bad_av;
1684ae4a8104SArtem Bityutskiy }
1685ae4a8104SArtem Bityutskiy
1686ae4a8104SArtem Bityutskiy if (av->vol_id < 0 || av->highest_lnum < 0 ||
1687ae4a8104SArtem Bityutskiy av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 ||
1688ae4a8104SArtem Bityutskiy av->data_pad < 0 || av->last_data_size < 0) {
168932608703STanya Brokhman ubi_err(ubi, "negative values");
1690ae4a8104SArtem Bityutskiy goto bad_av;
1691ae4a8104SArtem Bityutskiy }
1692ae4a8104SArtem Bityutskiy
1693ae4a8104SArtem Bityutskiy if (av->vol_id >= UBI_MAX_VOLUMES &&
1694ae4a8104SArtem Bityutskiy av->vol_id < UBI_INTERNAL_VOL_START) {
169532608703STanya Brokhman ubi_err(ubi, "bad vol_id");
1696ae4a8104SArtem Bityutskiy goto bad_av;
1697ae4a8104SArtem Bityutskiy }
1698ae4a8104SArtem Bityutskiy
1699ae4a8104SArtem Bityutskiy if (av->vol_id > ai->highest_vol_id) {
170032608703STanya Brokhman ubi_err(ubi, "highest_vol_id is %d, but vol_id %d is there",
1701ae4a8104SArtem Bityutskiy ai->highest_vol_id, av->vol_id);
1702ae4a8104SArtem Bityutskiy goto out;
1703ae4a8104SArtem Bityutskiy }
1704ae4a8104SArtem Bityutskiy
1705ae4a8104SArtem Bityutskiy if (av->vol_type != UBI_DYNAMIC_VOLUME &&
1706ae4a8104SArtem Bityutskiy av->vol_type != UBI_STATIC_VOLUME) {
170732608703STanya Brokhman ubi_err(ubi, "bad vol_type");
1708ae4a8104SArtem Bityutskiy goto bad_av;
1709ae4a8104SArtem Bityutskiy }
1710ae4a8104SArtem Bityutskiy
1711ae4a8104SArtem Bityutskiy if (av->data_pad > ubi->leb_size / 2) {
171232608703STanya Brokhman ubi_err(ubi, "bad data_pad");
1713ae4a8104SArtem Bityutskiy goto bad_av;
1714ae4a8104SArtem Bityutskiy }
1715ae4a8104SArtem Bityutskiy
1716ae4a8104SArtem Bityutskiy last_aeb = NULL;
1717ae4a8104SArtem Bityutskiy ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
1718ae4a8104SArtem Bityutskiy cond_resched();
1719ae4a8104SArtem Bityutskiy
1720ae4a8104SArtem Bityutskiy last_aeb = aeb;
1721ae4a8104SArtem Bityutskiy leb_count += 1;
1722ae4a8104SArtem Bityutskiy
1723ae4a8104SArtem Bityutskiy if (aeb->pnum < 0 || aeb->ec < 0) {
172432608703STanya Brokhman ubi_err(ubi, "negative values");
1725ae4a8104SArtem Bityutskiy goto bad_aeb;
1726ae4a8104SArtem Bityutskiy }
1727ae4a8104SArtem Bityutskiy
1728ae4a8104SArtem Bityutskiy if (aeb->ec < ai->min_ec) {
172932608703STanya Brokhman ubi_err(ubi, "bad ai->min_ec (%d), %d found",
1730ae4a8104SArtem Bityutskiy ai->min_ec, aeb->ec);
1731ae4a8104SArtem Bityutskiy goto bad_aeb;
1732ae4a8104SArtem Bityutskiy }
1733ae4a8104SArtem Bityutskiy
1734ae4a8104SArtem Bityutskiy if (aeb->ec > ai->max_ec) {
173532608703STanya Brokhman ubi_err(ubi, "bad ai->max_ec (%d), %d found",
1736ae4a8104SArtem Bityutskiy ai->max_ec, aeb->ec);
1737ae4a8104SArtem Bityutskiy goto bad_aeb;
1738ae4a8104SArtem Bityutskiy }
1739ae4a8104SArtem Bityutskiy
1740ae4a8104SArtem Bityutskiy if (aeb->pnum >= ubi->peb_count) {
174132608703STanya Brokhman ubi_err(ubi, "too high PEB number %d, total PEBs %d",
1742ae4a8104SArtem Bityutskiy aeb->pnum, ubi->peb_count);
1743ae4a8104SArtem Bityutskiy goto bad_aeb;
1744ae4a8104SArtem Bityutskiy }
1745ae4a8104SArtem Bityutskiy
1746ae4a8104SArtem Bityutskiy if (av->vol_type == UBI_STATIC_VOLUME) {
1747ae4a8104SArtem Bityutskiy if (aeb->lnum >= av->used_ebs) {
174832608703STanya Brokhman ubi_err(ubi, "bad lnum or used_ebs");
1749ae4a8104SArtem Bityutskiy goto bad_aeb;
1750ae4a8104SArtem Bityutskiy }
1751ae4a8104SArtem Bityutskiy } else {
1752ae4a8104SArtem Bityutskiy if (av->used_ebs != 0) {
175332608703STanya Brokhman ubi_err(ubi, "non-zero used_ebs");
1754ae4a8104SArtem Bityutskiy goto bad_aeb;
1755ae4a8104SArtem Bityutskiy }
1756ae4a8104SArtem Bityutskiy }
1757ae4a8104SArtem Bityutskiy
1758ae4a8104SArtem Bityutskiy if (aeb->lnum > av->highest_lnum) {
175932608703STanya Brokhman ubi_err(ubi, "incorrect highest_lnum or lnum");
1760ae4a8104SArtem Bityutskiy goto bad_aeb;
1761ae4a8104SArtem Bityutskiy }
1762ae4a8104SArtem Bityutskiy }
1763ae4a8104SArtem Bityutskiy
1764ae4a8104SArtem Bityutskiy if (av->leb_count != leb_count) {
176532608703STanya Brokhman ubi_err(ubi, "bad leb_count, %d objects in the tree",
1766ae4a8104SArtem Bityutskiy leb_count);
1767ae4a8104SArtem Bityutskiy goto bad_av;
1768ae4a8104SArtem Bityutskiy }
1769ae4a8104SArtem Bityutskiy
1770ae4a8104SArtem Bityutskiy if (!last_aeb)
1771ae4a8104SArtem Bityutskiy continue;
1772ae4a8104SArtem Bityutskiy
1773ae4a8104SArtem Bityutskiy aeb = last_aeb;
1774ae4a8104SArtem Bityutskiy
1775ae4a8104SArtem Bityutskiy if (aeb->lnum != av->highest_lnum) {
177632608703STanya Brokhman ubi_err(ubi, "bad highest_lnum");
1777ae4a8104SArtem Bityutskiy goto bad_aeb;
1778ae4a8104SArtem Bityutskiy }
1779ae4a8104SArtem Bityutskiy }
1780ae4a8104SArtem Bityutskiy
1781ae4a8104SArtem Bityutskiy if (vols_found != ai->vols_found) {
178232608703STanya Brokhman ubi_err(ubi, "bad ai->vols_found %d, should be %d",
1783ae4a8104SArtem Bityutskiy ai->vols_found, vols_found);
1784ae4a8104SArtem Bityutskiy goto out;
1785ae4a8104SArtem Bityutskiy }
1786ae4a8104SArtem Bityutskiy
1787ae4a8104SArtem Bityutskiy /* Check that attaching information is correct */
1788ae4a8104SArtem Bityutskiy ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1789ae4a8104SArtem Bityutskiy last_aeb = NULL;
1790ae4a8104SArtem Bityutskiy ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
1791ae4a8104SArtem Bityutskiy int vol_type;
1792ae4a8104SArtem Bityutskiy
1793ae4a8104SArtem Bityutskiy cond_resched();
1794ae4a8104SArtem Bityutskiy
1795ae4a8104SArtem Bityutskiy last_aeb = aeb;
1796ae4a8104SArtem Bityutskiy
17973291b52fSBoris Brezillon err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidb, 1);
1798ae4a8104SArtem Bityutskiy if (err && err != UBI_IO_BITFLIPS) {
179932608703STanya Brokhman ubi_err(ubi, "VID header is not OK (%d)",
180032608703STanya Brokhman err);
1801ae4a8104SArtem Bityutskiy if (err > 0)
1802ae4a8104SArtem Bityutskiy err = -EIO;
1803ae4a8104SArtem Bityutskiy return err;
1804ae4a8104SArtem Bityutskiy }
1805ae4a8104SArtem Bityutskiy
1806ae4a8104SArtem Bityutskiy vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1807ae4a8104SArtem Bityutskiy UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1808ae4a8104SArtem Bityutskiy if (av->vol_type != vol_type) {
180932608703STanya Brokhman ubi_err(ubi, "bad vol_type");
1810ae4a8104SArtem Bityutskiy goto bad_vid_hdr;
1811ae4a8104SArtem Bityutskiy }
1812ae4a8104SArtem Bityutskiy
1813ae4a8104SArtem Bityutskiy if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) {
181432608703STanya Brokhman ubi_err(ubi, "bad sqnum %llu", aeb->sqnum);
1815ae4a8104SArtem Bityutskiy goto bad_vid_hdr;
1816ae4a8104SArtem Bityutskiy }
1817ae4a8104SArtem Bityutskiy
1818ae4a8104SArtem Bityutskiy if (av->vol_id != be32_to_cpu(vidh->vol_id)) {
181932608703STanya Brokhman ubi_err(ubi, "bad vol_id %d", av->vol_id);
1820ae4a8104SArtem Bityutskiy goto bad_vid_hdr;
1821ae4a8104SArtem Bityutskiy }
1822ae4a8104SArtem Bityutskiy
1823ae4a8104SArtem Bityutskiy if (av->compat != vidh->compat) {
182432608703STanya Brokhman ubi_err(ubi, "bad compat %d", vidh->compat);
1825ae4a8104SArtem Bityutskiy goto bad_vid_hdr;
1826ae4a8104SArtem Bityutskiy }
1827ae4a8104SArtem Bityutskiy
1828ae4a8104SArtem Bityutskiy if (aeb->lnum != be32_to_cpu(vidh->lnum)) {
182932608703STanya Brokhman ubi_err(ubi, "bad lnum %d", aeb->lnum);
1830ae4a8104SArtem Bityutskiy goto bad_vid_hdr;
1831ae4a8104SArtem Bityutskiy }
1832ae4a8104SArtem Bityutskiy
1833ae4a8104SArtem Bityutskiy if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) {
183432608703STanya Brokhman ubi_err(ubi, "bad used_ebs %d", av->used_ebs);
1835ae4a8104SArtem Bityutskiy goto bad_vid_hdr;
1836ae4a8104SArtem Bityutskiy }
1837ae4a8104SArtem Bityutskiy
1838ae4a8104SArtem Bityutskiy if (av->data_pad != be32_to_cpu(vidh->data_pad)) {
183932608703STanya Brokhman ubi_err(ubi, "bad data_pad %d", av->data_pad);
1840ae4a8104SArtem Bityutskiy goto bad_vid_hdr;
1841ae4a8104SArtem Bityutskiy }
1842ae4a8104SArtem Bityutskiy }
1843ae4a8104SArtem Bityutskiy
1844ae4a8104SArtem Bityutskiy if (!last_aeb)
1845ae4a8104SArtem Bityutskiy continue;
1846ae4a8104SArtem Bityutskiy
1847ae4a8104SArtem Bityutskiy if (av->highest_lnum != be32_to_cpu(vidh->lnum)) {
184832608703STanya Brokhman ubi_err(ubi, "bad highest_lnum %d", av->highest_lnum);
1849ae4a8104SArtem Bityutskiy goto bad_vid_hdr;
1850ae4a8104SArtem Bityutskiy }
1851ae4a8104SArtem Bityutskiy
1852ae4a8104SArtem Bityutskiy if (av->last_data_size != be32_to_cpu(vidh->data_size)) {
185332608703STanya Brokhman ubi_err(ubi, "bad last_data_size %d",
185432608703STanya Brokhman av->last_data_size);
1855ae4a8104SArtem Bityutskiy goto bad_vid_hdr;
1856ae4a8104SArtem Bityutskiy }
1857ae4a8104SArtem Bityutskiy }
1858ae4a8104SArtem Bityutskiy
1859ae4a8104SArtem Bityutskiy /*
1860ae4a8104SArtem Bityutskiy * Make sure that all the physical eraseblocks are in one of the lists
1861ae4a8104SArtem Bityutskiy * or trees.
1862ae4a8104SArtem Bityutskiy */
1863ae4a8104SArtem Bityutskiy buf = kzalloc(ubi->peb_count, GFP_KERNEL);
1864ae4a8104SArtem Bityutskiy if (!buf)
1865ae4a8104SArtem Bityutskiy return -ENOMEM;
1866ae4a8104SArtem Bityutskiy
1867ae4a8104SArtem Bityutskiy for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1868ae4a8104SArtem Bityutskiy err = ubi_io_is_bad(ubi, pnum);
1869ae4a8104SArtem Bityutskiy if (err < 0) {
1870ae4a8104SArtem Bityutskiy kfree(buf);
1871ae4a8104SArtem Bityutskiy return err;
1872ae4a8104SArtem Bityutskiy } else if (err)
1873ae4a8104SArtem Bityutskiy buf[pnum] = 1;
1874ae4a8104SArtem Bityutskiy }
1875ae4a8104SArtem Bityutskiy
1876ae4a8104SArtem Bityutskiy ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
1877ae4a8104SArtem Bityutskiy ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
1878ae4a8104SArtem Bityutskiy buf[aeb->pnum] = 1;
1879ae4a8104SArtem Bityutskiy
1880ae4a8104SArtem Bityutskiy list_for_each_entry(aeb, &ai->free, u.list)
1881ae4a8104SArtem Bityutskiy buf[aeb->pnum] = 1;
1882ae4a8104SArtem Bityutskiy
1883ae4a8104SArtem Bityutskiy list_for_each_entry(aeb, &ai->corr, u.list)
1884ae4a8104SArtem Bityutskiy buf[aeb->pnum] = 1;
1885ae4a8104SArtem Bityutskiy
1886ae4a8104SArtem Bityutskiy list_for_each_entry(aeb, &ai->erase, u.list)
1887ae4a8104SArtem Bityutskiy buf[aeb->pnum] = 1;
1888ae4a8104SArtem Bityutskiy
1889ae4a8104SArtem Bityutskiy list_for_each_entry(aeb, &ai->alien, u.list)
1890ae4a8104SArtem Bityutskiy buf[aeb->pnum] = 1;
1891ae4a8104SArtem Bityutskiy
1892ae4a8104SArtem Bityutskiy err = 0;
1893ae4a8104SArtem Bityutskiy for (pnum = 0; pnum < ubi->peb_count; pnum++)
1894ae4a8104SArtem Bityutskiy if (!buf[pnum]) {
189532608703STanya Brokhman ubi_err(ubi, "PEB %d is not referred", pnum);
1896ae4a8104SArtem Bityutskiy err = 1;
1897ae4a8104SArtem Bityutskiy }
1898ae4a8104SArtem Bityutskiy
1899ae4a8104SArtem Bityutskiy kfree(buf);
1900ae4a8104SArtem Bityutskiy if (err)
1901ae4a8104SArtem Bityutskiy goto out;
1902ae4a8104SArtem Bityutskiy return 0;
1903ae4a8104SArtem Bityutskiy
1904ae4a8104SArtem Bityutskiy bad_aeb:
190532608703STanya Brokhman ubi_err(ubi, "bad attaching information about LEB %d", aeb->lnum);
1906ae4a8104SArtem Bityutskiy ubi_dump_aeb(aeb, 0);
1907ae4a8104SArtem Bityutskiy ubi_dump_av(av);
1908ae4a8104SArtem Bityutskiy goto out;
1909ae4a8104SArtem Bityutskiy
1910ae4a8104SArtem Bityutskiy bad_av:
191132608703STanya Brokhman ubi_err(ubi, "bad attaching information about volume %d", av->vol_id);
1912ae4a8104SArtem Bityutskiy ubi_dump_av(av);
1913ae4a8104SArtem Bityutskiy goto out;
1914ae4a8104SArtem Bityutskiy
1915ae4a8104SArtem Bityutskiy bad_vid_hdr:
191632608703STanya Brokhman ubi_err(ubi, "bad attaching information about volume %d", av->vol_id);
1917ae4a8104SArtem Bityutskiy ubi_dump_av(av);
1918ae4a8104SArtem Bityutskiy ubi_dump_vid_hdr(vidh);
1919ae4a8104SArtem Bityutskiy
1920ae4a8104SArtem Bityutskiy out:
1921ae4a8104SArtem Bityutskiy dump_stack();
1922ae4a8104SArtem Bityutskiy return -EINVAL;
1923ae4a8104SArtem Bityutskiy }
1924