blob: 5692b4ce0d9c26242ea7a9c4ad71919ee803707c [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Artem Bityutskiy (Битюцкий Артём)
19 */
20
21/*
Artem Bityutskiyfbd01072012-05-17 16:12:26 +030022 * UBI attaching sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040023 *
Artem Bityutskiyfbd01072012-05-17 16:12:26 +030024 * This sub-system is responsible for attaching MTD devices and it also
25 * implements flash media scanning.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040026 *
Artem Bityutskiya4e60422012-05-17 13:09:08 +030027 * The attaching information is represented by a &struct ubi_attach_info'
Artem Bityutskiyfbd01072012-05-17 16:12:26 +030028 * object. Information about volumes is represented by &struct ubi_ainf_volume
29 * objects which are kept in volume RB-tree with root at the @volumes field.
30 * The RB-tree is indexed by the volume ID.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040031 *
Artem Bityutskiyfbd01072012-05-17 16:12:26 +030032 * Logical eraseblocks are represented by &struct ubi_ainf_peb objects. These
33 * objects are kept in per-volume RB-trees with the root at the corresponding
34 * &struct ubi_ainf_volume object. To put it differently, we keep an RB-tree of
35 * per-volume objects and each of these objects is the root of RB-tree of
36 * per-LEB objects.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040037 *
38 * Corrupted physical eraseblocks are put to the @corr list, free physical
39 * eraseblocks are put to the @free list and the physical eraseblock to be
40 * erased are put to the @erase list.
Artem Bityutskiy0525dac2010-09-03 17:11:37 +030041 *
Artem Bityutskiyfef2deb2010-10-29 08:34:50 +030042 * About corruptions
43 * ~~~~~~~~~~~~~~~~~
44 *
45 * UBI protects EC and VID headers with CRC-32 checksums, so it can detect
46 * whether the headers are corrupted or not. Sometimes UBI also protects the
47 * data with CRC-32, e.g., when it executes the atomic LEB change operation, or
48 * when it moves the contents of a PEB for wear-leveling purposes.
49 *
Artem Bityutskiy0525dac2010-09-03 17:11:37 +030050 * UBI tries to distinguish between 2 types of corruptions.
Artem Bityutskiyfef2deb2010-10-29 08:34:50 +030051 *
52 * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
53 * tries to handle them gracefully, without printing too many warnings and
Artem Bityutskiyfbd01072012-05-17 16:12:26 +030054 * error messages. The idea is that we do not lose important data in these
55 * cases - we may lose only the data which were being written to the media just
56 * before the power cut happened, and the upper layers (e.g., UBIFS) are
57 * supposed to handle such data losses (e.g., by using the FS journal).
Artem Bityutskiyfef2deb2010-10-29 08:34:50 +030058 *
59 * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
60 * the reason is a power cut, UBI puts this PEB to the @erase list, and all
61 * PEBs in the @erase list are scheduled for erasure later.
Artem Bityutskiy0525dac2010-09-03 17:11:37 +030062 *
63 * 2. Unexpected corruptions which are not caused by power cuts. During
Artem Bityutskiyfbd01072012-05-17 16:12:26 +030064 * attaching, such PEBs are put to the @corr list and UBI preserves them.
Artem Bityutskiyfef2deb2010-10-29 08:34:50 +030065 * Obviously, this lessens the amount of available PEBs, and if at some point
66 * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
67 * about such PEBs every time the MTD device is attached.
Artem Bityutskiy45aafd32010-10-20 11:54:58 +030068 *
69 * However, it is difficult to reliably distinguish between these types of
Artem Bityutskiyfbd01072012-05-17 16:12:26 +030070 * corruptions and UBI's strategy is as follows (in case of attaching by
71 * scanning). UBI assumes corruption type 2 if the VID header is corrupted and
72 * the data area does not contain all 0xFFs, and there were no bit-flips or
73 * integrity errors (e.g., ECC errors in case of NAND) while reading the data
74 * area. Otherwise UBI assumes corruption type 1. So the decision criteria
75 * are as follows.
76 * o If the data area contains only 0xFFs, there are no data, and it is safe
Artem Bityutskiyfef2deb2010-10-29 08:34:50 +030077 * to just erase this PEB - this is corruption type 1.
78 * o If the data area has bit-flips or data integrity errors (ECC errors on
Artem Bityutskiy45aafd32010-10-20 11:54:58 +030079 * NAND), it is probably a PEB which was being erased when power cut
Artem Bityutskiyfef2deb2010-10-29 08:34:50 +030080 * happened, so this is corruption type 1. However, this is just a guess,
81 * which might be wrong.
Brian Norris55393ba2012-08-31 14:43:41 -070082 * o Otherwise this is corruption type 2.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040083 */
84
85#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090086#include <linux/slab.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040087#include <linux/crc32.h>
Artem Bityutskiy3013ee32009-01-16 19:08:43 +020088#include <linux/math64.h>
Matthieu CASTET095751a2010-06-03 16:14:27 +020089#include <linux/random.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040090#include "ubi.h"
91
Artem Bityutskiya4e60422012-05-17 13:09:08 +030092static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040093
94/* Temporary variables used during scanning */
95static struct ubi_ec_hdr *ech;
96static struct ubi_vid_hdr *vidh;
97
Boris Brezillonde4c4552016-09-16 16:59:14 +020098#define AV_FIND BIT(0)
99#define AV_ADD BIT(1)
100#define AV_FIND_OR_ADD (AV_FIND | AV_ADD)
101
102/**
103 * find_or_add_av - internal function to find a volume, add a volume or do
104 * both (find and add if missing).
105 * @ai: attaching information
106 * @vol_id: the requested volume ID
107 * @flags: a combination of the %AV_FIND and %AV_ADD flags describing the
108 * expected operation. If only %AV_ADD is set, -EEXIST is returned
109 * if the volume already exists. If only %AV_FIND is set, NULL is
110 * returned if the volume does not exist. And if both flags are
111 * set, the helper first tries to find an existing volume, and if
112 * it does not exist it creates a new one.
113 * @created: in value used to inform the caller whether it"s a newly created
114 * volume or not.
115 *
116 * This function returns a pointer to a volume description or an ERR_PTR if
117 * the operation failed. It can also return NULL if only %AV_FIND is set and
118 * the volume does not exist.
119 */
120static struct ubi_ainf_volume *find_or_add_av(struct ubi_attach_info *ai,
121 int vol_id, unsigned int flags,
122 bool *created)
123{
124 struct ubi_ainf_volume *av;
125 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
126
127 /* Walk the volume RB-tree to look if this volume is already present */
128 while (*p) {
129 parent = *p;
130 av = rb_entry(parent, struct ubi_ainf_volume, rb);
131
132 if (vol_id == av->vol_id) {
133 *created = false;
134
135 if (!(flags & AV_FIND))
136 return ERR_PTR(-EEXIST);
137
138 return av;
139 }
140
141 if (vol_id > av->vol_id)
142 p = &(*p)->rb_left;
143 else
144 p = &(*p)->rb_right;
145 }
146
147 if (!(flags & AV_ADD))
148 return NULL;
149
150 /* The volume is absent - add it */
151 av = kzalloc(sizeof(*av), GFP_KERNEL);
152 if (!av)
153 return ERR_PTR(-ENOMEM);
154
155 av->vol_id = vol_id;
156
157 if (vol_id > ai->highest_vol_id)
158 ai->highest_vol_id = vol_id;
159
160 rb_link_node(&av->rb, parent, p);
161 rb_insert_color(&av->rb, &ai->volumes);
162 ai->vols_found += 1;
163 *created = true;
164 dbg_bld("added volume %d", vol_id);
165 return av;
166}
167
168/**
169 * ubi_find_or_add_av - search for a volume in the attaching information and
170 * add one if it does not exist.
171 * @ai: attaching information
172 * @vol_id: the requested volume ID
173 * @created: whether the volume has been created or not
174 *
175 * This function returns a pointer to the new volume description or an
176 * ERR_PTR if the operation failed.
177 */
178static struct ubi_ainf_volume *ubi_find_or_add_av(struct ubi_attach_info *ai,
179 int vol_id, bool *created)
180{
181 return find_or_add_av(ai, vol_id, AV_FIND_OR_ADD, created);
182}
183
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300184/**
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300185 * add_to_list - add physical eraseblock to a list.
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300186 * @ai: attaching information
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300187 * @pnum: physical eraseblock number to add
Joel Reardon6dd3bc72012-05-16 14:20:56 +0200188 * @vol_id: the last used volume id for the PEB
189 * @lnum: the last used LEB number for the PEB
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300190 * @ec: erase counter of the physical eraseblock
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300191 * @to_head: if not zero, add to the head of the list
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300192 * @list: the list to add to
193 *
Artem Bityutskiyfbd01072012-05-17 16:12:26 +0300194 * This function allocates a 'struct ubi_ainf_peb' object for physical
195 * eraseblock @pnum and adds it to the "free", "erase", or "alien" lists.
Joel Reardon6dd3bc72012-05-16 14:20:56 +0200196 * It stores the @lnum and @vol_id alongside, which can both be
197 * %UBI_UNKNOWN if they are not available, not readable, or not assigned.
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300198 * If @to_head is not zero, PEB will be added to the head of the list, which
199 * basically means it will be processed first later. E.g., we add corrupted
200 * PEBs (corrupted due to power cuts) to the head of the erase list to make
201 * sure we erase them first and get rid of corruptions ASAP. This function
202 * returns zero in case of success and a negative error code in case of
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300203 * failure.
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300204 */
Joel Reardon6dd3bc72012-05-16 14:20:56 +0200205static int add_to_list(struct ubi_attach_info *ai, int pnum, int vol_id,
206 int lnum, int ec, int to_head, struct list_head *list)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400207{
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300208 struct ubi_ainf_peb *aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400209
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300210 if (list == &ai->free) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400211 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300212 } else if (list == &ai->erase) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400213 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300214 } else if (list == &ai->alien) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400215 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300216 ai->alien_peb_count += 1;
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300217 } else
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400218 BUG();
219
Artem Bityutskiy1fc2e3e2012-05-17 15:56:03 +0300220 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300221 if (!aeb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400222 return -ENOMEM;
223
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300224 aeb->pnum = pnum;
Joel Reardon6dd3bc72012-05-16 14:20:56 +0200225 aeb->vol_id = vol_id;
226 aeb->lnum = lnum;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300227 aeb->ec = ec;
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300228 if (to_head)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300229 list_add(&aeb->u.list, list);
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300230 else
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300231 list_add_tail(&aeb->u.list, list);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400232 return 0;
233}
234
235/**
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300236 * add_corrupted - add a corrupted physical eraseblock.
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300237 * @ai: attaching information
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300238 * @pnum: physical eraseblock number to add
239 * @ec: erase counter of the physical eraseblock
240 *
Artem Bityutskiyfbd01072012-05-17 16:12:26 +0300241 * This function allocates a 'struct ubi_ainf_peb' object for a corrupted
242 * physical eraseblock @pnum and adds it to the 'corr' list. The corruption
243 * was presumably not caused by a power cut. Returns zero in case of success
244 * and a negative error code in case of failure.
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300245 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300246static int add_corrupted(struct ubi_attach_info *ai, int pnum, int ec)
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300247{
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300248 struct ubi_ainf_peb *aeb;
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300249
250 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
251
Artem Bityutskiy1fc2e3e2012-05-17 15:56:03 +0300252 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300253 if (!aeb)
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300254 return -ENOMEM;
255
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300256 ai->corr_peb_count += 1;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300257 aeb->pnum = pnum;
258 aeb->ec = ec;
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300259 list_add(&aeb->u.list, &ai->corr);
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300260 return 0;
261}
262
263/**
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +0200264 * add_fastmap - add a Fastmap related physical eraseblock.
265 * @ai: attaching information
266 * @pnum: physical eraseblock number the VID header came from
267 * @vid_hdr: the volume identifier header
268 * @ec: erase counter of the physical eraseblock
269 *
270 * This function allocates a 'struct ubi_ainf_peb' object for a Fastamp
271 * physical eraseblock @pnum and adds it to the 'fastmap' list.
272 * Such blocks can be Fastmap super and data blocks from both the most
273 * recent Fastmap we're attaching from or from old Fastmaps which will
274 * be erased.
275 */
276static int add_fastmap(struct ubi_attach_info *ai, int pnum,
277 struct ubi_vid_hdr *vid_hdr, int ec)
278{
279 struct ubi_ainf_peb *aeb;
280
281 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
282 if (!aeb)
283 return -ENOMEM;
284
285 aeb->pnum = pnum;
Boris Brezillon3f84e452016-09-16 16:59:10 +0200286 aeb->vol_id = be32_to_cpu(vid_hdr->vol_id);
287 aeb->sqnum = be64_to_cpu(vid_hdr->sqnum);
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +0200288 aeb->ec = ec;
289 list_add(&aeb->u.list, &ai->fastmap);
290
291 dbg_bld("add to fastmap list: PEB %d, vol_id %d, sqnum: %llu", pnum,
292 aeb->vol_id, aeb->sqnum);
293
294 return 0;
295}
296
297/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300298 * validate_vid_hdr - check volume identifier header.
Tanya Brokhman326087032014-10-20 19:57:00 +0300299 * @ubi: UBI device description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400300 * @vid_hdr: the volume identifier header to check
Artem Bityutskiy517af482012-05-17 14:38:34 +0300301 * @av: information about the volume this logical eraseblock belongs to
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400302 * @pnum: physical eraseblock number the VID header came from
303 *
304 * This function checks that data stored in @vid_hdr is consistent. Returns
305 * non-zero if an inconsistency was found and zero if not.
306 *
307 * Note, UBI does sanity check of everything it reads from the flash media.
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300308 * Most of the checks are done in the I/O sub-system. Here we check that the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400309 * information in the VID header is consistent to the information in other VID
310 * headers of the same volume.
311 */
Tanya Brokhman326087032014-10-20 19:57:00 +0300312static int validate_vid_hdr(const struct ubi_device *ubi,
313 const struct ubi_vid_hdr *vid_hdr,
Artem Bityutskiy517af482012-05-17 14:38:34 +0300314 const struct ubi_ainf_volume *av, int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400315{
316 int vol_type = vid_hdr->vol_type;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300317 int vol_id = be32_to_cpu(vid_hdr->vol_id);
318 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
319 int data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400320
Artem Bityutskiy517af482012-05-17 14:38:34 +0300321 if (av->leb_count != 0) {
322 int av_vol_type;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400323
324 /*
325 * This is not the first logical eraseblock belonging to this
326 * volume. Ensure that the data in its VID header is consistent
327 * to the data in previous logical eraseblock headers.
328 */
329
Artem Bityutskiy517af482012-05-17 14:38:34 +0300330 if (vol_id != av->vol_id) {
Tanya Brokhman326087032014-10-20 19:57:00 +0300331 ubi_err(ubi, "inconsistent vol_id");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400332 goto bad;
333 }
334
Artem Bityutskiy517af482012-05-17 14:38:34 +0300335 if (av->vol_type == UBI_STATIC_VOLUME)
336 av_vol_type = UBI_VID_STATIC;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400337 else
Artem Bityutskiy517af482012-05-17 14:38:34 +0300338 av_vol_type = UBI_VID_DYNAMIC;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400339
Artem Bityutskiy517af482012-05-17 14:38:34 +0300340 if (vol_type != av_vol_type) {
Tanya Brokhman326087032014-10-20 19:57:00 +0300341 ubi_err(ubi, "inconsistent vol_type");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400342 goto bad;
343 }
344
Artem Bityutskiy517af482012-05-17 14:38:34 +0300345 if (used_ebs != av->used_ebs) {
Tanya Brokhman326087032014-10-20 19:57:00 +0300346 ubi_err(ubi, "inconsistent used_ebs");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400347 goto bad;
348 }
349
Artem Bityutskiy517af482012-05-17 14:38:34 +0300350 if (data_pad != av->data_pad) {
Tanya Brokhman326087032014-10-20 19:57:00 +0300351 ubi_err(ubi, "inconsistent data_pad");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400352 goto bad;
353 }
354 }
355
356 return 0;
357
358bad:
Tanya Brokhman326087032014-10-20 19:57:00 +0300359 ubi_err(ubi, "inconsistent VID header at PEB %d", pnum);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300360 ubi_dump_vid_hdr(vid_hdr);
Artem Bityutskiy517af482012-05-17 14:38:34 +0300361 ubi_dump_av(av);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400362 return -EINVAL;
363}
364
365/**
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300366 * add_volume - add volume to the attaching information.
367 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400368 * @vol_id: ID of the volume to add
369 * @pnum: physical eraseblock number
370 * @vid_hdr: volume identifier header
371 *
372 * If the volume corresponding to the @vid_hdr logical eraseblock is already
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300373 * present in the attaching information, this function does nothing. Otherwise
374 * it adds corresponding volume to the attaching information. Returns a pointer
Artem Bityutskiyfbd01072012-05-17 16:12:26 +0300375 * to the allocated "av" object in case of success and a negative error code in
376 * case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400377 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300378static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *ai,
Artem Bityutskiyafc15a82012-05-17 07:46:17 +0300379 int vol_id, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400380 const struct ubi_vid_hdr *vid_hdr)
381{
Artem Bityutskiy517af482012-05-17 14:38:34 +0300382 struct ubi_ainf_volume *av;
Boris Brezillonde4c4552016-09-16 16:59:14 +0200383 bool created;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400384
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300385 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400386
Boris Brezillonde4c4552016-09-16 16:59:14 +0200387 av = ubi_find_or_add_av(ai, vol_id, &created);
388 if (IS_ERR(av) || !created)
389 return av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400390
Artem Bityutskiy517af482012-05-17 14:38:34 +0300391 av->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
392 av->data_pad = be32_to_cpu(vid_hdr->data_pad);
393 av->compat = vid_hdr->compat;
394 av->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400395 : UBI_STATIC_VOLUME;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400396
Artem Bityutskiy517af482012-05-17 14:38:34 +0300397 return av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400398}
399
400/**
Richard Weinbergerdac6e202012-09-26 17:51:47 +0200401 * ubi_compare_lebs - find out which logical eraseblock is newer.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400402 * @ubi: UBI device description object
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300403 * @aeb: first logical eraseblock to compare
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400404 * @pnum: physical eraseblock number of the second logical eraseblock to
405 * compare
406 * @vid_hdr: volume identifier header of the second logical eraseblock
407 *
408 * This function compares 2 copies of a LEB and informs which one is newer. In
409 * case of success this function returns a positive value, in case of failure, a
410 * negative error code is returned. The success return codes use the following
411 * bits:
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300412 * o bit 0 is cleared: the first PEB (described by @aeb) is newer than the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400413 * second PEB (described by @pnum and @vid_hdr);
414 * o bit 0 is set: the second PEB is newer;
415 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
416 * o bit 1 is set: bit-flips were detected in the newer LEB;
417 * o bit 2 is cleared: the older LEB is not corrupted;
418 * o bit 2 is set: the older LEB is corrupted.
419 */
Richard Weinbergerdac6e202012-09-26 17:51:47 +0200420int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300421 int pnum, const struct ubi_vid_hdr *vid_hdr)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400422{
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400423 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
424 uint32_t data_crc, crc;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300425 struct ubi_vid_hdr *vh = NULL;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300426 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400427
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300428 if (sqnum2 == aeb->sqnum) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400429 /*
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300430 * This must be a really ancient UBI image which has been
431 * created before sequence numbers support has been added. At
432 * that times we used 32-bit LEB versions stored in logical
433 * eraseblocks. That was before UBI got into mainline. We do not
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300434 * support these images anymore. Well, those images still work,
435 * but only if no unclean reboots happened.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400436 */
Tanya Brokhman326087032014-10-20 19:57:00 +0300437 ubi_err(ubi, "unsupported on-flash UBI format");
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300438 return -EINVAL;
439 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400440
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300441 /* Obviously the LEB with lower sequence counter is older */
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300442 second_is_newer = (sqnum2 > aeb->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400443
444 /*
445 * Now we know which copy is newer. If the copy flag of the PEB with
446 * newer version is not set, then we just return, otherwise we have to
447 * check data CRC. For the second PEB we already have the VID header,
448 * for the first one - we'll need to re-read it from flash.
449 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300450 * Note: this may be optimized so that we wouldn't read twice.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400451 */
452
453 if (second_is_newer) {
454 if (!vid_hdr->copy_flag) {
455 /* It is not a copy, so it is newer */
456 dbg_bld("second PEB %d is newer, copy_flag is unset",
457 pnum);
458 return 1;
459 }
460 } else {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300461 if (!aeb->copy_flag) {
Artem Bityutskiyfb22b592010-10-19 22:00:11 +0300462 /* It is not a copy, so it is newer */
463 dbg_bld("first PEB %d is newer, copy_flag is unset",
464 pnum);
465 return bitflips << 1;
466 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400467
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300468 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300469 if (!vh)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400470 return -ENOMEM;
471
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300472 pnum = aeb->pnum;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300473 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400474 if (err) {
475 if (err == UBI_IO_BITFLIPS)
476 bitflips = 1;
477 else {
Tanya Brokhman326087032014-10-20 19:57:00 +0300478 ubi_err(ubi, "VID of PEB %d header is bad, but it was OK earlier, err %d",
Artem Bityutskiy049333c2012-08-27 14:43:54 +0300479 pnum, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400480 if (err > 0)
481 err = -EIO;
482
483 goto out_free_vidh;
484 }
485 }
486
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300487 vid_hdr = vh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400488 }
489
490 /* Read the data of the copy and check the CRC */
491
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300492 len = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400493
Artem Bityutskiyd125a752012-10-26 16:11:26 +0300494 mutex_lock(&ubi->buf_mutex);
495 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len);
Brian Norrisd57f40542011-09-20 18:34:25 -0700496 if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
Artem Bityutskiyd125a752012-10-26 16:11:26 +0300497 goto out_unlock;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400498
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300499 data_crc = be32_to_cpu(vid_hdr->data_crc);
Artem Bityutskiyd125a752012-10-26 16:11:26 +0300500 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400501 if (crc != data_crc) {
502 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
503 pnum, crc, data_crc);
504 corrupted = 1;
505 bitflips = 0;
506 second_is_newer = !second_is_newer;
507 } else {
508 dbg_bld("PEB %d CRC is OK", pnum);
Brian Norris8eef7d72015-02-28 02:23:25 -0800509 bitflips |= !!err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400510 }
Artem Bityutskiyd125a752012-10-26 16:11:26 +0300511 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400512
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300513 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400514
515 if (second_is_newer)
516 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
517 else
518 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
519
520 return second_is_newer | (bitflips << 1) | (corrupted << 2);
521
Artem Bityutskiyd125a752012-10-26 16:11:26 +0300522out_unlock:
523 mutex_unlock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400524out_free_vidh:
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300525 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400526 return err;
527}
528
529/**
Artem Bityutskiyfbd01072012-05-17 16:12:26 +0300530 * ubi_add_to_av - add used physical eraseblock to the attaching information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400531 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300532 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400533 * @pnum: the physical eraseblock number
534 * @ec: erase counter
535 * @vid_hdr: the volume identifier header
536 * @bitflips: if bit-flips were detected when this physical eraseblock was read
537 *
Artem Bityutskiy79b510c2007-05-05 17:36:17 +0300538 * This function adds information about a used physical eraseblock to the
539 * 'used' tree of the corresponding volume. The function is rather complex
540 * because it has to handle cases when this is not the first physical
541 * eraseblock belonging to the same logical eraseblock, and the newer one has
542 * to be picked, while the older one has to be dropped. This function returns
543 * zero in case of success and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400544 */
Artem Bityutskiy35611882012-05-17 15:31:31 +0300545int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
546 int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400547{
548 int err, vol_id, lnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400549 unsigned long long sqnum;
Artem Bityutskiy517af482012-05-17 14:38:34 +0300550 struct ubi_ainf_volume *av;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300551 struct ubi_ainf_peb *aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400552 struct rb_node **p, *parent = NULL;
553
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300554 vol_id = be32_to_cpu(vid_hdr->vol_id);
555 lnum = be32_to_cpu(vid_hdr->lnum);
556 sqnum = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400557
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300558 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
559 pnum, vol_id, lnum, ec, sqnum, bitflips);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400560
Artem Bityutskiy517af482012-05-17 14:38:34 +0300561 av = add_volume(ai, vol_id, pnum, vid_hdr);
562 if (IS_ERR(av))
563 return PTR_ERR(av);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400564
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300565 if (ai->max_sqnum < sqnum)
566 ai->max_sqnum = sqnum;
Brijesh Singh76eafe42007-07-06 14:35:43 +0300567
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400568 /*
569 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
570 * if this is the first instance of this logical eraseblock or not.
571 */
Artem Bityutskiy517af482012-05-17 14:38:34 +0300572 p = &av->root.rb_node;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400573 while (*p) {
574 int cmp_res;
575
576 parent = *p;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300577 aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
578 if (lnum != aeb->lnum) {
579 if (lnum < aeb->lnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400580 p = &(*p)->rb_left;
581 else
582 p = &(*p)->rb_right;
583 continue;
584 }
585
586 /*
587 * There is already a physical eraseblock describing the same
588 * logical eraseblock present.
589 */
590
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300591 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, EC %d",
592 aeb->pnum, aeb->sqnum, aeb->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400593
594 /*
595 * Make sure that the logical eraseblocks have different
596 * sequence numbers. Otherwise the image is bad.
597 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300598 * However, if the sequence number is zero, we assume it must
599 * be an ancient UBI image from the era when UBI did not have
600 * sequence numbers. We still can attach these images, unless
601 * there is a need to distinguish between old and new
602 * eraseblocks, in which case we'll refuse the image in
Richard Weinbergerdac6e202012-09-26 17:51:47 +0200603 * 'ubi_compare_lebs()'. In other words, we attach old clean
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300604 * images, but refuse attaching old images with duplicated
605 * logical eraseblocks because there was an unclean reboot.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400606 */
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300607 if (aeb->sqnum == sqnum && sqnum != 0) {
Tanya Brokhman326087032014-10-20 19:57:00 +0300608 ubi_err(ubi, "two LEBs with same sequence number %llu",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400609 sqnum);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300610 ubi_dump_aeb(aeb, 0);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300611 ubi_dump_vid_hdr(vid_hdr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400612 return -EINVAL;
613 }
614
615 /*
616 * Now we have to drop the older one and preserve the newer
617 * one.
618 */
Richard Weinbergerdac6e202012-09-26 17:51:47 +0200619 cmp_res = ubi_compare_lebs(ubi, aeb, pnum, vid_hdr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400620 if (cmp_res < 0)
621 return cmp_res;
622
623 if (cmp_res & 1) {
624 /*
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900625 * This logical eraseblock is newer than the one
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400626 * found earlier.
627 */
Tanya Brokhman326087032014-10-20 19:57:00 +0300628 err = validate_vid_hdr(ubi, vid_hdr, av, pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400629 if (err)
630 return err;
631
Joel Reardon6dd3bc72012-05-16 14:20:56 +0200632 err = add_to_list(ai, aeb->pnum, aeb->vol_id,
633 aeb->lnum, aeb->ec, cmp_res & 4,
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300634 &ai->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400635 if (err)
636 return err;
637
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300638 aeb->ec = ec;
639 aeb->pnum = pnum;
Joel Reardon6dd3bc72012-05-16 14:20:56 +0200640 aeb->vol_id = vol_id;
641 aeb->lnum = lnum;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300642 aeb->scrub = ((cmp_res & 2) || bitflips);
643 aeb->copy_flag = vid_hdr->copy_flag;
644 aeb->sqnum = sqnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400645
Artem Bityutskiy517af482012-05-17 14:38:34 +0300646 if (av->highest_lnum == lnum)
647 av->last_data_size =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300648 be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400649
650 return 0;
651 } else {
652 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200653 * This logical eraseblock is older than the one found
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400654 * previously.
655 */
Joel Reardon6dd3bc72012-05-16 14:20:56 +0200656 return add_to_list(ai, pnum, vol_id, lnum, ec,
657 cmp_res & 4, &ai->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400658 }
659 }
660
661 /*
662 * We've met this logical eraseblock for the first time, add it to the
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300663 * attaching information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400664 */
665
Tanya Brokhman326087032014-10-20 19:57:00 +0300666 err = validate_vid_hdr(ubi, vid_hdr, av, pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400667 if (err)
668 return err;
669
Artem Bityutskiy1fc2e3e2012-05-17 15:56:03 +0300670 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300671 if (!aeb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400672 return -ENOMEM;
673
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300674 aeb->ec = ec;
675 aeb->pnum = pnum;
Joel Reardon6dd3bc72012-05-16 14:20:56 +0200676 aeb->vol_id = vol_id;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300677 aeb->lnum = lnum;
678 aeb->scrub = bitflips;
679 aeb->copy_flag = vid_hdr->copy_flag;
680 aeb->sqnum = sqnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400681
Artem Bityutskiy517af482012-05-17 14:38:34 +0300682 if (av->highest_lnum <= lnum) {
683 av->highest_lnum = lnum;
684 av->last_data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400685 }
686
Artem Bityutskiy517af482012-05-17 14:38:34 +0300687 av->leb_count += 1;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300688 rb_link_node(&aeb->u.rb, parent, p);
Artem Bityutskiy517af482012-05-17 14:38:34 +0300689 rb_insert_color(&aeb->u.rb, &av->root);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400690 return 0;
691}
692
693/**
Boris Brezillonde4c4552016-09-16 16:59:14 +0200694 * ubi_add_av - add volume to the attaching information.
695 * @ai: attaching information
696 * @vol_id: the requested volume ID
697 *
698 * This function returns a pointer to the new volume description or an
699 * ERR_PTR if the operation failed.
700 */
701struct ubi_ainf_volume *ubi_add_av(struct ubi_attach_info *ai, int vol_id)
702{
703 bool created;
704
705 return find_or_add_av(ai, vol_id, AV_ADD, &created);
706}
707
708/**
Artem Bityutskiydcd85fd2012-05-17 15:33:20 +0300709 * ubi_find_av - find volume in the attaching information.
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300710 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400711 * @vol_id: the requested volume ID
712 *
713 * This function returns a pointer to the volume description or %NULL if there
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300714 * are no data about this volume in the attaching information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400715 */
Artem Bityutskiydcd85fd2012-05-17 15:33:20 +0300716struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
717 int vol_id)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400718{
Boris Brezillonde4c4552016-09-16 16:59:14 +0200719 bool created;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400720
Boris Brezillonde4c4552016-09-16 16:59:14 +0200721 return find_or_add_av((struct ubi_attach_info *)ai, vol_id, AV_FIND,
722 &created);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400723}
724
Boris Brezillonf9efe8d2016-09-16 16:59:15 +0200725static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av,
726 struct list_head *list);
727
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400728/**
Artem Bityutskiyd717dc22012-05-17 15:36:39 +0300729 * ubi_remove_av - delete attaching information about a volume.
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300730 * @ai: attaching information
Artem Bityutskiy517af482012-05-17 14:38:34 +0300731 * @av: the volume attaching information to delete
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400732 */
Artem Bityutskiyd717dc22012-05-17 15:36:39 +0300733void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400734{
Artem Bityutskiy517af482012-05-17 14:38:34 +0300735 dbg_bld("remove attaching information about volume %d", av->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400736
Artem Bityutskiy517af482012-05-17 14:38:34 +0300737 rb_erase(&av->rb, &ai->volumes);
Boris Brezillonf9efe8d2016-09-16 16:59:15 +0200738 destroy_av(ai, av, &ai->erase);
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300739 ai->vols_found -= 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400740}
741
742/**
Artem Bityutskiy13d33da2012-05-17 15:20:28 +0300743 * early_erase_peb - erase a physical eraseblock.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400744 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300745 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400746 * @pnum: physical eraseblock number to erase;
Artem Bityutskiy9c47fb22012-05-18 12:54:58 +0300747 * @ec: erase counter value to write (%UBI_UNKNOWN if it is unknown)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400748 *
749 * This function erases physical eraseblock 'pnum', and writes the erase
750 * counter header to it. This function should only be used on UBI device
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300751 * initialization stages, when the EBA sub-system had not been yet initialized.
752 * This function returns zero in case of success and a negative error code in
753 * case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400754 */
Artem Bityutskiy13d33da2012-05-17 15:20:28 +0300755static int early_erase_peb(struct ubi_device *ubi,
756 const struct ubi_attach_info *ai, int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400757{
758 int err;
759 struct ubi_ec_hdr *ec_hdr;
760
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400761 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
762 /*
763 * Erase counter overflow. Upgrade UBI and use 64-bit
764 * erase counters internally.
765 */
Tanya Brokhman326087032014-10-20 19:57:00 +0300766 ubi_err(ubi, "erase counter overflow at PEB %d, EC %d",
767 pnum, ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400768 return -EINVAL;
769 }
770
Florin Malitadcec4c32007-07-19 15:22:41 -0400771 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
772 if (!ec_hdr)
773 return -ENOMEM;
774
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300775 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400776
777 err = ubi_io_sync_erase(ubi, pnum, 0);
778 if (err < 0)
779 goto out_free;
780
781 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
782
783out_free:
784 kfree(ec_hdr);
785 return err;
786}
787
788/**
Artem Bityutskiyc87fbd72012-05-17 15:38:56 +0300789 * ubi_early_get_peb - get a free physical eraseblock.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400790 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300791 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400792 *
793 * This function returns a free physical eraseblock. It is supposed to be
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300794 * called on the UBI initialization stages when the wear-leveling sub-system is
795 * not initialized yet. This function picks a physical eraseblocks from one of
796 * the lists, writes the EC header if it is needed, and removes it from the
797 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400798 *
Artem Bityutskiyfbd01072012-05-17 16:12:26 +0300799 * This function returns a pointer to the "aeb" of the found free PEB in case
800 * of success and an error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400801 */
Artem Bityutskiyc87fbd72012-05-17 15:38:56 +0300802struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
803 struct ubi_attach_info *ai)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400804{
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300805 int err = 0;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300806 struct ubi_ainf_peb *aeb, *tmp_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400807
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300808 if (!list_empty(&ai->free)) {
809 aeb = list_entry(ai->free.next, struct ubi_ainf_peb, u.list);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300810 list_del(&aeb->u.list);
811 dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec);
812 return aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400813 }
814
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300815 /*
816 * We try to erase the first physical eraseblock from the erase list
817 * and pick it if we succeed, or try to erase the next one if not. And
818 * so forth. We don't want to take care about bad eraseblocks here -
819 * they'll be handled later.
820 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300821 list_for_each_entry_safe(aeb, tmp_aeb, &ai->erase, u.list) {
Artem Bityutskiy9c47fb22012-05-18 12:54:58 +0300822 if (aeb->ec == UBI_UNKNOWN)
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300823 aeb->ec = ai->mean_ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400824
Artem Bityutskiy13d33da2012-05-17 15:20:28 +0300825 err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1);
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300826 if (err)
827 continue;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400828
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300829 aeb->ec += 1;
830 list_del(&aeb->u.list);
831 dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec);
832 return aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400833 }
834
Tanya Brokhman326087032014-10-20 19:57:00 +0300835 ubi_err(ubi, "no free eraseblocks");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400836 return ERR_PTR(-ENOSPC);
837}
838
839/**
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300840 * check_corruption - check the data area of PEB.
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300841 * @ubi: UBI device description object
Richard Weinbergerdac6e202012-09-26 17:51:47 +0200842 * @vid_hdr: the (corrupted) VID header of this PEB
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300843 * @pnum: the physical eraseblock number to check
844 *
845 * This is a helper function which is used to distinguish between VID header
846 * corruptions caused by power cuts and other reasons. If the PEB contains only
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300847 * 0xFF bytes in the data area, the VID header is most probably corrupted
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300848 * because of a power cut (%0 is returned in this case). Otherwise, it was
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300849 * probably corrupted for some other reasons (%1 is returned in this case). A
850 * negative error code is returned if a read error occurred.
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300851 *
852 * If the corruption reason was a power cut, UBI can safely erase this PEB.
853 * Otherwise, it should preserve it to avoid possibly destroying important
854 * information.
855 */
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300856static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
857 int pnum)
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300858{
859 int err;
860
861 mutex_lock(&ubi->buf_mutex);
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200862 memset(ubi->peb_buf, 0x00, ubi->leb_size);
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300863
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200864 err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start,
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300865 ubi->leb_size);
Brian Norrisd57f40542011-09-20 18:34:25 -0700866 if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) {
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300867 /*
868 * Bit-flips or integrity errors while reading the data area.
869 * It is difficult to say for sure what type of corruption is
870 * this, but presumably a power cut happened while this PEB was
871 * erased, so it became unstable and corrupted, and should be
872 * erased.
873 */
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300874 err = 0;
875 goto out_unlock;
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300876 }
877
878 if (err)
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300879 goto out_unlock;
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300880
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200881 if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size))
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300882 goto out_unlock;
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300883
Tanya Brokhman326087032014-10-20 19:57:00 +0300884 ubi_err(ubi, "PEB %d contains corrupted VID header, and the data does not contain all 0xFF",
Artem Bityutskiy049333c2012-08-27 14:43:54 +0300885 pnum);
Tanya Brokhman326087032014-10-20 19:57:00 +0300886 ubi_err(ubi, "this may be a non-UBI PEB or a severe VID header corruption which requires manual inspection");
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300887 ubi_dump_vid_hdr(vid_hdr);
Artem Bityutskiy719bb842012-08-27 17:14:58 +0300888 pr_err("hexdump of PEB %d offset %d, length %d",
889 pnum, ubi->leb_start, ubi->leb_size);
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300890 ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200891 ubi->peb_buf, ubi->leb_size, 1);
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300892 err = 1;
893
894out_unlock:
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300895 mutex_unlock(&ubi->buf_mutex);
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300896 return err;
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300897}
898
Richard Weinberger243a4f82016-06-14 10:12:13 +0200899static bool vol_ignored(int vol_id)
900{
901 switch (vol_id) {
902 case UBI_LAYOUT_VOLUME_ID:
903 return true;
904 }
905
906#ifdef CONFIG_MTD_UBI_FASTMAP
907 return ubi_is_fm_vol(vol_id);
908#else
909 return false;
910#endif
911}
912
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300913/**
Artem Bityutskiyfbd01072012-05-17 16:12:26 +0300914 * scan_peb - scan and process UBI headers of a PEB.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400915 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300916 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400917 * @pnum: the physical eraseblock number
Richard Weinberger74f2c6e2016-06-14 10:12:17 +0200918 * @fast: true if we're scanning for a Fastmap
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400919 *
Artem Bityutskiyfbd01072012-05-17 16:12:26 +0300920 * This function reads UBI headers of PEB @pnum, checks them, and adds
921 * information about this PEB to the corresponding list or RB-tree in the
922 * "attaching info" structure. Returns zero if the physical eraseblock was
923 * successfully handled and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400924 */
Richard Weinberger74f2c6e2016-06-14 10:12:17 +0200925static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai,
926 int pnum, bool fast)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400927{
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +0200928 long long ec;
Richard Weinbergerdac6e202012-09-26 17:51:47 +0200929 int err, bitflips = 0, vol_id = -1, ec_err = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400930
931 dbg_bld("scan PEB %d", pnum);
932
933 /* Skip bad physical eraseblocks */
934 err = ubi_io_is_bad(ubi, pnum);
935 if (err < 0)
936 return err;
937 else if (err) {
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300938 ai->bad_peb_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400939 return 0;
940 }
941
942 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
943 if (err < 0)
944 return err;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300945 switch (err) {
946 case 0:
947 break;
948 case UBI_IO_BITFLIPS:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400949 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300950 break;
951 case UBI_IO_FF:
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300952 ai->empty_peb_count += 1;
Joel Reardon6dd3bc72012-05-16 14:20:56 +0200953 return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
954 UBI_UNKNOWN, 0, &ai->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300955 case UBI_IO_FF_BITFLIPS:
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300956 ai->empty_peb_count += 1;
Joel Reardon6dd3bc72012-05-16 14:20:56 +0200957 return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
958 UBI_UNKNOWN, 1, &ai->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300959 case UBI_IO_BAD_HDR_EBADMSG:
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300960 case UBI_IO_BAD_HDR:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400961 /*
962 * We have to also look at the VID header, possibly it is not
963 * corrupted. Set %bitflips flag in order to make this PEB be
964 * moved and EC be re-created.
965 */
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300966 ec_err = err;
Artem Bityutskiy9c47fb22012-05-18 12:54:58 +0300967 ec = UBI_UNKNOWN;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400968 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300969 break;
970 default:
Tanya Brokhman326087032014-10-20 19:57:00 +0300971 ubi_err(ubi, "'ubi_io_read_ec_hdr()' returned unknown code %d",
972 err);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300973 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400974 }
975
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300976 if (!ec_err) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300977 int image_seq;
978
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400979 /* Make sure UBI version is OK */
980 if (ech->version != UBI_VERSION) {
Tanya Brokhman326087032014-10-20 19:57:00 +0300981 ubi_err(ubi, "this UBI version is %d, image version is %d",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400982 UBI_VERSION, (int)ech->version);
983 return -EINVAL;
984 }
985
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300986 ec = be64_to_cpu(ech->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400987 if (ec > UBI_MAX_ERASECOUNTER) {
988 /*
989 * Erase counter overflow. The EC headers have 64 bits
990 * reserved, but we anyway make use of only 31 bit
991 * values, as this seems to be enough for any existing
992 * flash. Upgrade UBI and use 64-bit erase counters
993 * internally.
994 */
Tanya Brokhman326087032014-10-20 19:57:00 +0300995 ubi_err(ubi, "erase counter overflow, max is %d",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400996 UBI_MAX_ERASECOUNTER);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300997 ubi_dump_ec_hdr(ech);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400998 return -EINVAL;
999 }
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +03001000
Adrian Hunter32bc4822009-07-24 19:16:04 +03001001 /*
1002 * Make sure that all PEBs have the same image sequence number.
1003 * This allows us to detect situations when users flash UBI
1004 * images incorrectly, so that the flash has the new UBI image
1005 * and leftovers from the old one. This feature was added
1006 * relatively recently, and the sequence number was always
1007 * zero, because old UBI implementations always set it to zero.
1008 * For this reasons, we do not panic if some PEBs have zero
1009 * sequence number, while other PEBs have non-zero sequence
1010 * number.
1011 */
Holger Brunck3dc948d2009-07-13 16:47:57 +02001012 image_seq = be32_to_cpu(ech->image_seq);
Richard Genoud55b80c42013-09-28 15:55:14 +02001013 if (!ubi->image_seq)
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +03001014 ubi->image_seq = image_seq;
Richard Genoud55b80c42013-09-28 15:55:14 +02001015 if (image_seq && ubi->image_seq != image_seq) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001016 ubi_err(ubi, "bad image sequence number %d in PEB %d, expected %d",
Artem Bityutskiy049333c2012-08-27 14:43:54 +03001017 image_seq, pnum, ubi->image_seq);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +03001018 ubi_dump_ec_hdr(ech);
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +03001019 return -EINVAL;
1020 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001021 }
1022
1023 /* OK, we've done with the EC header, let's look at the VID header */
1024
1025 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
1026 if (err < 0)
1027 return err;
Artem Bityutskiyb3321502010-09-03 14:40:55 +03001028 switch (err) {
1029 case 0:
1030 break;
1031 case UBI_IO_BITFLIPS:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001032 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +03001033 break;
1034 case UBI_IO_BAD_HDR_EBADMSG:
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001035 if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
1036 /*
1037 * Both EC and VID headers are corrupted and were read
1038 * with data integrity error, probably this is a bad
1039 * PEB, bit it is not marked as bad yet. This may also
1040 * be a result of power cut during erasure.
1041 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001042 ai->maybe_bad_peb_count += 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +03001043 case UBI_IO_BAD_HDR:
Richard Weinberger74f2c6e2016-06-14 10:12:17 +02001044 /*
1045 * If we're facing a bad VID header we have to drop *all*
1046 * Fastmap data structures we find. The most recent Fastmap
1047 * could be bad and therefore there is a chance that we attach
1048 * from an old one. On a fine MTD stack a PEB must not render
1049 * bad all of a sudden, but the reality is different.
1050 * So, let's be paranoid and help finding the root cause by
1051 * falling back to scanning mode instead of attaching with a
1052 * bad EBA table and cause data corruption which is hard to
1053 * analyze.
1054 */
1055 if (fast)
1056 ai->force_full_scan = 1;
1057
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +03001058 if (ec_err)
1059 /*
1060 * Both headers are corrupted. There is a possibility
1061 * that this a valid UBI PEB which has corresponding
1062 * LEB, but the headers are corrupted. However, it is
1063 * impossible to distinguish it from a PEB which just
Artem Bityutskiy45aafd32010-10-20 11:54:58 +03001064 * contains garbage because of a power cut during erase
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +03001065 * operation. So we just schedule this PEB for erasure.
Artem Bityutskiy7ac760c2010-12-02 06:34:01 +02001066 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001067 * Besides, in case of NOR flash, we deliberately
Artem Bityutskiy7ac760c2010-12-02 06:34:01 +02001068 * corrupt both headers because NOR flash erasure is
1069 * slow and can start from the end.
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +03001070 */
1071 err = 0;
1072 else
1073 /*
1074 * The EC was OK, but the VID header is corrupted. We
1075 * have to check what is in the data area.
1076 */
Artem Bityutskiy45aafd32010-10-20 11:54:58 +03001077 err = check_corruption(ubi, vidh, pnum);
Artem Bityutskiydf3fca42010-10-20 11:51:21 +03001078
1079 if (err < 0)
1080 return err;
1081 else if (!err)
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +03001082 /* This corruption is caused by a power cut */
Joel Reardon6dd3bc72012-05-16 14:20:56 +02001083 err = add_to_list(ai, pnum, UBI_UNKNOWN,
1084 UBI_UNKNOWN, ec, 1, &ai->erase);
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +03001085 else
1086 /* This is an unexpected corruption */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001087 err = add_corrupted(ai, pnum, ec);
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +03001088 if (err)
1089 return err;
1090 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +03001091 case UBI_IO_FF_BITFLIPS:
Joel Reardon6dd3bc72012-05-16 14:20:56 +02001092 err = add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
1093 ec, 1, &ai->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001094 if (err)
1095 return err;
1096 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +03001097 case UBI_IO_FF:
Matthieu CASTET193819c2012-08-22 16:03:46 +02001098 if (ec_err || bitflips)
Joel Reardon6dd3bc72012-05-16 14:20:56 +02001099 err = add_to_list(ai, pnum, UBI_UNKNOWN,
1100 UBI_UNKNOWN, ec, 1, &ai->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +03001101 else
Joel Reardon6dd3bc72012-05-16 14:20:56 +02001102 err = add_to_list(ai, pnum, UBI_UNKNOWN,
1103 UBI_UNKNOWN, ec, 0, &ai->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001104 if (err)
1105 return err;
1106 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +03001107 default:
Tanya Brokhman326087032014-10-20 19:57:00 +03001108 ubi_err(ubi, "'ubi_io_read_vid_hdr()' returned unknown code %d",
Artem Bityutskiyb3321502010-09-03 14:40:55 +03001109 err);
1110 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001111 }
1112
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001113 vol_id = be32_to_cpu(vidh->vol_id);
Richard Weinberger243a4f82016-06-14 10:12:13 +02001114 if (vol_id > UBI_MAX_VOLUMES && !vol_ignored(vol_id)) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001115 int lnum = be32_to_cpu(vidh->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001116
1117 /* Unsupported internal volume */
1118 switch (vidh->compat) {
1119 case UBI_COMPAT_DELETE:
Richard Weinberger243a4f82016-06-14 10:12:13 +02001120 ubi_msg(ubi, "\"delete\" compatible internal volume %d:%d found, will remove it",
1121 vol_id, lnum);
1122
Joel Reardon6dd3bc72012-05-16 14:20:56 +02001123 err = add_to_list(ai, pnum, vol_id, lnum,
1124 ec, 1, &ai->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001125 if (err)
1126 return err;
Brijesh Singh158132c2010-06-16 09:28:26 +03001127 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001128
1129 case UBI_COMPAT_RO:
Tanya Brokhman326087032014-10-20 19:57:00 +03001130 ubi_msg(ubi, "read-only compatible internal volume %d:%d found, switch to read-only mode",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001131 vol_id, lnum);
1132 ubi->ro_mode = 1;
1133 break;
1134
1135 case UBI_COMPAT_PRESERVE:
Tanya Brokhman326087032014-10-20 19:57:00 +03001136 ubi_msg(ubi, "\"preserve\" compatible internal volume %d:%d found",
Artem Bityutskiy049333c2012-08-27 14:43:54 +03001137 vol_id, lnum);
Joel Reardon6dd3bc72012-05-16 14:20:56 +02001138 err = add_to_list(ai, pnum, vol_id, lnum,
1139 ec, 0, &ai->alien);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001140 if (err)
1141 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001142 return 0;
1143
1144 case UBI_COMPAT_REJECT:
Tanya Brokhman326087032014-10-20 19:57:00 +03001145 ubi_err(ubi, "incompatible internal volume %d:%d found",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001146 vol_id, lnum);
1147 return -EINVAL;
1148 }
1149 }
1150
Artem Bityutskiye0e718c2010-09-03 14:53:23 +03001151 if (ec_err)
Tanya Brokhman326087032014-10-20 19:57:00 +03001152 ubi_warn(ubi, "valid VID header but corrupted EC header at PEB %d",
Artem Bityutskiy29a88c92009-07-19 14:03:16 +03001153 pnum);
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +02001154
1155 if (ubi_is_fm_vol(vol_id))
1156 err = add_fastmap(ai, pnum, vidh, ec);
1157 else
1158 err = ubi_add_to_av(ubi, ai, pnum, ec, vidh, bitflips);
1159
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001160 if (err)
1161 return err;
1162
1163adjust_mean_ec:
Artem Bityutskiye0e718c2010-09-03 14:53:23 +03001164 if (!ec_err) {
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001165 ai->ec_sum += ec;
1166 ai->ec_count += 1;
1167 if (ec > ai->max_ec)
1168 ai->max_ec = ec;
1169 if (ec < ai->min_ec)
1170 ai->min_ec = ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001171 }
1172
1173 return 0;
1174}
1175
1176/**
Artem Bityutskiyfbd01072012-05-17 16:12:26 +03001177 * late_analysis - analyze the overall situation with PEB.
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001178 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001179 * @ai: attaching information
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001180 *
Artem Bityutskiyfbd01072012-05-17 16:12:26 +03001181 * This is a helper function which takes a look what PEBs we have after we
1182 * gather information about all of them ("ai" is compete). It decides whether
1183 * the flash is empty and should be formatted of whether there are too many
1184 * corrupted PEBs and we should not attach this MTD device. Returns zero if we
1185 * should proceed with attaching the MTD device, and %-EINVAL if we should not.
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001186 */
Artem Bityutskiyfbd01072012-05-17 16:12:26 +03001187static int late_analysis(struct ubi_device *ubi, struct ubi_attach_info *ai)
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001188{
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001189 struct ubi_ainf_peb *aeb;
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001190 int max_corr, peb_count;
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001191
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001192 peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count;
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001193 max_corr = peb_count / 20 ?: 8;
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001194
1195 /*
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001196 * Few corrupted PEBs is not a problem and may be just a result of
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001197 * unclean reboots. However, many of them may indicate some problems
1198 * with the flash HW or driver.
1199 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001200 if (ai->corr_peb_count) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001201 ubi_err(ubi, "%d PEBs are corrupted and preserved",
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001202 ai->corr_peb_count);
Artem Bityutskiye28453b2012-08-27 15:13:05 +03001203 pr_err("Corrupted PEBs are:");
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001204 list_for_each_entry(aeb, &ai->corr, u.list)
Artem Bityutskiye28453b2012-08-27 15:13:05 +03001205 pr_cont(" %d", aeb->pnum);
1206 pr_cont("\n");
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001207
1208 /*
1209 * If too many PEBs are corrupted, we refuse attaching,
1210 * otherwise, only print a warning.
1211 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001212 if (ai->corr_peb_count >= max_corr) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001213 ubi_err(ubi, "too many corrupted PEBs, refusing");
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001214 return -EINVAL;
1215 }
1216 }
1217
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001218 if (ai->empty_peb_count + ai->maybe_bad_peb_count == peb_count) {
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001219 /*
1220 * All PEBs are empty, or almost all - a couple PEBs look like
1221 * they may be bad PEBs which were not marked as bad yet.
1222 *
1223 * This piece of code basically tries to distinguish between
1224 * the following situations:
1225 *
1226 * 1. Flash is empty, but there are few bad PEBs, which are not
1227 * marked as bad so far, and which were read with error. We
1228 * want to go ahead and format this flash. While formatting,
1229 * the faulty PEBs will probably be marked as bad.
1230 *
1231 * 2. Flash contains non-UBI data and we do not want to format
1232 * it and destroy possibly important information.
1233 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001234 if (ai->maybe_bad_peb_count <= 2) {
1235 ai->is_empty = 1;
Tanya Brokhman326087032014-10-20 19:57:00 +03001236 ubi_msg(ubi, "empty MTD device detected");
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001237 get_random_bytes(&ubi->image_seq,
1238 sizeof(ubi->image_seq));
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001239 } else {
Tanya Brokhman326087032014-10-20 19:57:00 +03001240 ubi_err(ubi, "MTD device is not UBI-formatted and possibly contains non-UBI data - refusing it");
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001241 return -EINVAL;
1242 }
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001243
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001244 }
1245
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001246 return 0;
1247}
1248
1249/**
Artem Bityutskiyfbd01072012-05-17 16:12:26 +03001250 * destroy_av - free volume attaching information.
1251 * @av: volume attaching information
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001252 * @ai: attaching information
Boris Brezillonf9efe8d2016-09-16 16:59:15 +02001253 * @list: put the aeb elements in there if !NULL, otherwise free them
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001254 *
Artem Bityutskiyfbd01072012-05-17 16:12:26 +03001255 * This function destroys the volume attaching information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001256 */
Boris Brezillonf9efe8d2016-09-16 16:59:15 +02001257static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av,
1258 struct list_head *list)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001259{
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001260 struct ubi_ainf_peb *aeb;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001261 struct rb_node *this = av->root.rb_node;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001262
1263 while (this) {
1264 if (this->rb_left)
1265 this = this->rb_left;
1266 else if (this->rb_right)
1267 this = this->rb_right;
1268 else {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001269 aeb = rb_entry(this, struct ubi_ainf_peb, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001270 this = rb_parent(this);
1271 if (this) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001272 if (this->rb_left == &aeb->u.rb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001273 this->rb_left = NULL;
1274 else
1275 this->rb_right = NULL;
1276 }
1277
Boris Brezillonf9efe8d2016-09-16 16:59:15 +02001278 if (list)
1279 list_add_tail(&aeb->u.list, list);
1280 else
1281 kmem_cache_free(ai->aeb_slab_cache, aeb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001282 }
1283 }
Artem Bityutskiy517af482012-05-17 14:38:34 +03001284 kfree(av);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001285}
1286
1287/**
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001288 * destroy_ai - destroy attaching information.
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001289 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001290 */
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001291static void destroy_ai(struct ubi_attach_info *ai)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001292{
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001293 struct ubi_ainf_peb *aeb, *aeb_tmp;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001294 struct ubi_ainf_volume *av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001295 struct rb_node *rb;
1296
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001297 list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001298 list_del(&aeb->u.list);
Artem Bityutskiy1fc2e3e2012-05-17 15:56:03 +03001299 kmem_cache_free(ai->aeb_slab_cache, aeb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001300 }
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001301 list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001302 list_del(&aeb->u.list);
Artem Bityutskiy1fc2e3e2012-05-17 15:56:03 +03001303 kmem_cache_free(ai->aeb_slab_cache, aeb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001304 }
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001305 list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001306 list_del(&aeb->u.list);
Artem Bityutskiy1fc2e3e2012-05-17 15:56:03 +03001307 kmem_cache_free(ai->aeb_slab_cache, aeb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001308 }
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001309 list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001310 list_del(&aeb->u.list);
Artem Bityutskiy1fc2e3e2012-05-17 15:56:03 +03001311 kmem_cache_free(ai->aeb_slab_cache, aeb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001312 }
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +02001313 list_for_each_entry_safe(aeb, aeb_tmp, &ai->fastmap, u.list) {
1314 list_del(&aeb->u.list);
1315 kmem_cache_free(ai->aeb_slab_cache, aeb);
1316 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001317
1318 /* Destroy the volume RB-tree */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001319 rb = ai->volumes.rb_node;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001320 while (rb) {
1321 if (rb->rb_left)
1322 rb = rb->rb_left;
1323 else if (rb->rb_right)
1324 rb = rb->rb_right;
1325 else {
Artem Bityutskiy517af482012-05-17 14:38:34 +03001326 av = rb_entry(rb, struct ubi_ainf_volume, rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001327
1328 rb = rb_parent(rb);
1329 if (rb) {
Artem Bityutskiy517af482012-05-17 14:38:34 +03001330 if (rb->rb_left == &av->rb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001331 rb->rb_left = NULL;
1332 else
1333 rb->rb_right = NULL;
1334 }
1335
Boris Brezillonf9efe8d2016-09-16 16:59:15 +02001336 destroy_av(ai, av, NULL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001337 }
1338 }
1339
Julia Lawallf9a113d2015-09-13 14:15:16 +02001340 kmem_cache_destroy(ai->aeb_slab_cache);
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001341 kfree(ai);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001342}
1343
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001344/**
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001345 * scan_all - scan entire MTD device.
1346 * @ubi: UBI device description object
1347 * @ai: attach info object
1348 * @start: start scanning at this PEB
1349 *
1350 * This function does full scanning of an MTD device and returns complete
1351 * information about it in form of a "struct ubi_attach_info" object. In case
1352 * of failure, an error code is returned.
1353 */
1354static int scan_all(struct ubi_device *ubi, struct ubi_attach_info *ai,
1355 int start)
1356{
1357 int err, pnum;
1358 struct rb_node *rb1, *rb2;
1359 struct ubi_ainf_volume *av;
1360 struct ubi_ainf_peb *aeb;
1361
1362 err = -ENOMEM;
1363
1364 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1365 if (!ech)
1366 return err;
1367
1368 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
1369 if (!vidh)
1370 goto out_ech;
1371
1372 for (pnum = start; pnum < ubi->peb_count; pnum++) {
1373 cond_resched();
1374
1375 dbg_gen("process PEB %d", pnum);
Richard Weinberger74f2c6e2016-06-14 10:12:17 +02001376 err = scan_peb(ubi, ai, pnum, false);
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001377 if (err < 0)
1378 goto out_vidh;
1379 }
1380
Tanya Brokhman326087032014-10-20 19:57:00 +03001381 ubi_msg(ubi, "scanning is finished");
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001382
1383 /* Calculate mean erase counter */
1384 if (ai->ec_count)
1385 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
1386
1387 err = late_analysis(ubi, ai);
1388 if (err)
1389 goto out_vidh;
1390
1391 /*
1392 * In case of unknown erase counter we use the mean erase counter
1393 * value.
1394 */
1395 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1396 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
1397 if (aeb->ec == UBI_UNKNOWN)
1398 aeb->ec = ai->mean_ec;
1399 }
1400
1401 list_for_each_entry(aeb, &ai->free, u.list) {
1402 if (aeb->ec == UBI_UNKNOWN)
1403 aeb->ec = ai->mean_ec;
1404 }
1405
1406 list_for_each_entry(aeb, &ai->corr, u.list)
1407 if (aeb->ec == UBI_UNKNOWN)
1408 aeb->ec = ai->mean_ec;
1409
1410 list_for_each_entry(aeb, &ai->erase, u.list)
1411 if (aeb->ec == UBI_UNKNOWN)
1412 aeb->ec = ai->mean_ec;
1413
1414 err = self_check_ai(ubi, ai);
1415 if (err)
1416 goto out_vidh;
1417
1418 ubi_free_vid_hdr(ubi, vidh);
1419 kfree(ech);
1420
1421 return 0;
1422
1423out_vidh:
1424 ubi_free_vid_hdr(ubi, vidh);
1425out_ech:
1426 kfree(ech);
1427 return err;
1428}
1429
Richard Weinbergerd2158f62014-10-06 15:58:07 +02001430static struct ubi_attach_info *alloc_ai(void)
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001431{
1432 struct ubi_attach_info *ai;
1433
1434 ai = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL);
1435 if (!ai)
1436 return ai;
1437
1438 INIT_LIST_HEAD(&ai->corr);
1439 INIT_LIST_HEAD(&ai->free);
1440 INIT_LIST_HEAD(&ai->erase);
1441 INIT_LIST_HEAD(&ai->alien);
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +02001442 INIT_LIST_HEAD(&ai->fastmap);
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001443 ai->volumes = RB_ROOT;
Richard Weinbergerd2158f62014-10-06 15:58:07 +02001444 ai->aeb_slab_cache = kmem_cache_create("ubi_aeb_slab_cache",
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001445 sizeof(struct ubi_ainf_peb),
1446 0, 0, NULL);
1447 if (!ai->aeb_slab_cache) {
1448 kfree(ai);
1449 ai = NULL;
1450 }
1451
1452 return ai;
1453}
1454
Richard Weinberger98105d02014-10-06 15:39:01 +02001455#ifdef CONFIG_MTD_UBI_FASTMAP
1456
1457/**
Richard Weinbergerd139d302016-06-14 10:12:12 +02001458 * scan_fast - try to find a fastmap and attach from it.
Richard Weinberger98105d02014-10-06 15:39:01 +02001459 * @ubi: UBI device description object
1460 * @ai: attach info object
1461 *
1462 * Returns 0 on success, negative return values indicate an internal
1463 * error.
1464 * UBI_NO_FASTMAP denotes that no fastmap was found.
1465 * UBI_BAD_FASTMAP denotes that the found fastmap was invalid.
1466 */
1467static int scan_fast(struct ubi_device *ubi, struct ubi_attach_info **ai)
1468{
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +02001469 int err, pnum;
1470 struct ubi_attach_info *scan_ai;
Richard Weinberger98105d02014-10-06 15:39:01 +02001471
1472 err = -ENOMEM;
1473
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +02001474 scan_ai = alloc_ai();
1475 if (!scan_ai)
1476 goto out;
1477
Richard Weinberger98105d02014-10-06 15:39:01 +02001478 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1479 if (!ech)
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +02001480 goto out_ai;
Richard Weinberger98105d02014-10-06 15:39:01 +02001481
1482 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
1483 if (!vidh)
1484 goto out_ech;
1485
1486 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
Richard Weinberger98105d02014-10-06 15:39:01 +02001487 cond_resched();
1488
1489 dbg_gen("process PEB %d", pnum);
Richard Weinberger74f2c6e2016-06-14 10:12:17 +02001490 err = scan_peb(ubi, scan_ai, pnum, true);
Richard Weinberger98105d02014-10-06 15:39:01 +02001491 if (err < 0)
1492 goto out_vidh;
Richard Weinberger98105d02014-10-06 15:39:01 +02001493 }
1494
1495 ubi_free_vid_hdr(ubi, vidh);
1496 kfree(ech);
1497
Richard Weinberger74f2c6e2016-06-14 10:12:17 +02001498 if (scan_ai->force_full_scan)
1499 err = UBI_NO_FASTMAP;
1500 else
1501 err = ubi_scan_fastmap(ubi, *ai, scan_ai);
1502
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +02001503 if (err) {
1504 /*
1505 * Didn't attach via fastmap, do a full scan but reuse what
1506 * we've aready scanned.
1507 */
1508 destroy_ai(*ai);
1509 *ai = scan_ai;
1510 } else
1511 destroy_ai(scan_ai);
Richard Weinberger98105d02014-10-06 15:39:01 +02001512
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +02001513 return err;
Richard Weinberger98105d02014-10-06 15:39:01 +02001514
1515out_vidh:
1516 ubi_free_vid_hdr(ubi, vidh);
1517out_ech:
1518 kfree(ech);
Richard Weinbergerfdf10ed2016-06-14 10:12:15 +02001519out_ai:
1520 destroy_ai(scan_ai);
Richard Weinberger98105d02014-10-06 15:39:01 +02001521out:
1522 return err;
1523}
1524
1525#endif
1526
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001527/**
1528 * ubi_attach - attach an MTD device.
1529 * @ubi: UBI device descriptor
1530 * @force_scan: if set to non-zero attach by scanning
1531 *
1532 * This function returns zero in case of success and a negative error code in
1533 * case of failure.
1534 */
1535int ubi_attach(struct ubi_device *ubi, int force_scan)
1536{
1537 int err;
1538 struct ubi_attach_info *ai;
1539
Richard Weinbergerd2158f62014-10-06 15:58:07 +02001540 ai = alloc_ai();
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001541 if (!ai)
1542 return -ENOMEM;
1543
1544#ifdef CONFIG_MTD_UBI_FASTMAP
1545 /* On small flash devices we disable fastmap in any case. */
1546 if ((int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd) <= UBI_FM_MAX_START) {
1547 ubi->fm_disabled = 1;
1548 force_scan = 1;
1549 }
1550
1551 if (force_scan)
1552 err = scan_all(ubi, ai, 0);
1553 else {
Richard Weinberger98105d02014-10-06 15:39:01 +02001554 err = scan_fast(ubi, &ai);
Richard Weinberger180a5352015-03-09 10:04:09 +01001555 if (err > 0 || mtd_is_eccerr(err)) {
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001556 if (err != UBI_NO_FASTMAP) {
1557 destroy_ai(ai);
Richard Weinbergerd2158f62014-10-06 15:58:07 +02001558 ai = alloc_ai();
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001559 if (!ai)
1560 return -ENOMEM;
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001561
Richard Weinberger4b3e0a22013-09-28 15:55:12 +02001562 err = scan_all(ubi, ai, 0);
1563 } else {
1564 err = scan_all(ubi, ai, UBI_FM_MAX_START);
1565 }
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001566 }
1567 }
1568#else
1569 err = scan_all(ubi, ai, 0);
1570#endif
1571 if (err)
1572 goto out_ai;
1573
1574 ubi->bad_peb_count = ai->bad_peb_count;
1575 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
1576 ubi->corr_peb_count = ai->corr_peb_count;
1577 ubi->max_ec = ai->max_ec;
1578 ubi->mean_ec = ai->mean_ec;
1579 dbg_gen("max. sequence number: %llu", ai->max_sqnum);
1580
1581 err = ubi_read_volume_table(ubi, ai);
1582 if (err)
1583 goto out_ai;
1584
1585 err = ubi_wl_init(ubi, ai);
1586 if (err)
1587 goto out_vtbl;
1588
1589 err = ubi_eba_init(ubi, ai);
1590 if (err)
1591 goto out_wl;
1592
1593#ifdef CONFIG_MTD_UBI_FASTMAP
Richard Weinberger560d86a2014-10-27 13:00:26 +01001594 if (ubi->fm && ubi_dbg_chk_fastmap(ubi)) {
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001595 struct ubi_attach_info *scan_ai;
1596
Richard Weinbergerd2158f62014-10-06 15:58:07 +02001597 scan_ai = alloc_ai();
Julia Lawall4d525142013-12-29 23:47:19 +01001598 if (!scan_ai) {
1599 err = -ENOMEM;
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001600 goto out_wl;
Julia Lawall4d525142013-12-29 23:47:19 +01001601 }
Richard Weinbergerdac6e202012-09-26 17:51:47 +02001602
1603 err = scan_all(ubi, scan_ai, 0);
1604 if (err) {
1605 destroy_ai(scan_ai);
1606 goto out_wl;
1607 }
1608
1609 err = self_check_eba(ubi, ai, scan_ai);
1610 destroy_ai(scan_ai);
1611
1612 if (err)
1613 goto out_wl;
1614 }
1615#endif
1616
1617 destroy_ai(ai);
1618 return 0;
1619
1620out_wl:
1621 ubi_wl_close(ubi);
1622out_vtbl:
1623 ubi_free_internal_volumes(ubi);
1624 vfree(ubi->vtbl);
1625out_ai:
1626 destroy_ai(ai);
1627 return err;
1628}
1629
1630/**
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001631 * self_check_ai - check the attaching information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001632 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001633 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001634 *
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001635 * This function returns zero if the attaching information is all right, and a
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001636 * negative error code if not or if an error occurred.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001637 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001638static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001639{
1640 int pnum, err, vols_found = 0;
1641 struct rb_node *rb1, *rb2;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001642 struct ubi_ainf_volume *av;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001643 struct ubi_ainf_peb *aeb, *last_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001644 uint8_t *buf;
1645
Ezequiel Garcia64575572012-11-28 09:18:29 -03001646 if (!ubi_dbg_chk_gen(ubi))
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001647 return 0;
1648
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001649 /*
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001650 * At first, check that attaching information is OK.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001651 */
Artem Bityutskiy517af482012-05-17 14:38:34 +03001652 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001653 int leb_count = 0;
1654
1655 cond_resched();
1656
1657 vols_found += 1;
1658
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001659 if (ai->is_empty) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001660 ubi_err(ubi, "bad is_empty flag");
Artem Bityutskiy517af482012-05-17 14:38:34 +03001661 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001662 }
1663
Artem Bityutskiy517af482012-05-17 14:38:34 +03001664 if (av->vol_id < 0 || av->highest_lnum < 0 ||
1665 av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 ||
1666 av->data_pad < 0 || av->last_data_size < 0) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001667 ubi_err(ubi, "negative values");
Artem Bityutskiy517af482012-05-17 14:38:34 +03001668 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001669 }
1670
Artem Bityutskiy517af482012-05-17 14:38:34 +03001671 if (av->vol_id >= UBI_MAX_VOLUMES &&
1672 av->vol_id < UBI_INTERNAL_VOL_START) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001673 ubi_err(ubi, "bad vol_id");
Artem Bityutskiy517af482012-05-17 14:38:34 +03001674 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001675 }
1676
Artem Bityutskiy517af482012-05-17 14:38:34 +03001677 if (av->vol_id > ai->highest_vol_id) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001678 ubi_err(ubi, "highest_vol_id is %d, but vol_id %d is there",
Artem Bityutskiy517af482012-05-17 14:38:34 +03001679 ai->highest_vol_id, av->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001680 goto out;
1681 }
1682
Artem Bityutskiy517af482012-05-17 14:38:34 +03001683 if (av->vol_type != UBI_DYNAMIC_VOLUME &&
1684 av->vol_type != UBI_STATIC_VOLUME) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001685 ubi_err(ubi, "bad vol_type");
Artem Bityutskiy517af482012-05-17 14:38:34 +03001686 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001687 }
1688
Artem Bityutskiy517af482012-05-17 14:38:34 +03001689 if (av->data_pad > ubi->leb_size / 2) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001690 ubi_err(ubi, "bad data_pad");
Artem Bityutskiy517af482012-05-17 14:38:34 +03001691 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001692 }
1693
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001694 last_aeb = NULL;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001695 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001696 cond_resched();
1697
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001698 last_aeb = aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001699 leb_count += 1;
1700
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001701 if (aeb->pnum < 0 || aeb->ec < 0) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001702 ubi_err(ubi, "negative values");
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001703 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001704 }
1705
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001706 if (aeb->ec < ai->min_ec) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001707 ubi_err(ubi, "bad ai->min_ec (%d), %d found",
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001708 ai->min_ec, aeb->ec);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001709 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001710 }
1711
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001712 if (aeb->ec > ai->max_ec) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001713 ubi_err(ubi, "bad ai->max_ec (%d), %d found",
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001714 ai->max_ec, aeb->ec);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001715 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001716 }
1717
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001718 if (aeb->pnum >= ubi->peb_count) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001719 ubi_err(ubi, "too high PEB number %d, total PEBs %d",
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001720 aeb->pnum, ubi->peb_count);
1721 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001722 }
1723
Artem Bityutskiy517af482012-05-17 14:38:34 +03001724 if (av->vol_type == UBI_STATIC_VOLUME) {
1725 if (aeb->lnum >= av->used_ebs) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001726 ubi_err(ubi, "bad lnum or used_ebs");
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001727 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001728 }
1729 } else {
Artem Bityutskiy517af482012-05-17 14:38:34 +03001730 if (av->used_ebs != 0) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001731 ubi_err(ubi, "non-zero used_ebs");
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001732 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001733 }
1734 }
1735
Artem Bityutskiy517af482012-05-17 14:38:34 +03001736 if (aeb->lnum > av->highest_lnum) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001737 ubi_err(ubi, "incorrect highest_lnum or lnum");
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001738 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001739 }
1740 }
1741
Artem Bityutskiy517af482012-05-17 14:38:34 +03001742 if (av->leb_count != leb_count) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001743 ubi_err(ubi, "bad leb_count, %d objects in the tree",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001744 leb_count);
Artem Bityutskiy517af482012-05-17 14:38:34 +03001745 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001746 }
1747
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001748 if (!last_aeb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001749 continue;
1750
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001751 aeb = last_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001752
Artem Bityutskiy517af482012-05-17 14:38:34 +03001753 if (aeb->lnum != av->highest_lnum) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001754 ubi_err(ubi, "bad highest_lnum");
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001755 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001756 }
1757 }
1758
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001759 if (vols_found != ai->vols_found) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001760 ubi_err(ubi, "bad ai->vols_found %d, should be %d",
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001761 ai->vols_found, vols_found);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001762 goto out;
1763 }
1764
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001765 /* Check that attaching information is correct */
Artem Bityutskiy517af482012-05-17 14:38:34 +03001766 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001767 last_aeb = NULL;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001768 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001769 int vol_type;
1770
1771 cond_resched();
1772
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001773 last_aeb = aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001774
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001775 err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidh, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001776 if (err && err != UBI_IO_BITFLIPS) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001777 ubi_err(ubi, "VID header is not OK (%d)",
1778 err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001779 if (err > 0)
1780 err = -EIO;
1781 return err;
1782 }
1783
1784 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1785 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001786 if (av->vol_type != vol_type) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001787 ubi_err(ubi, "bad vol_type");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001788 goto bad_vid_hdr;
1789 }
1790
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001791 if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001792 ubi_err(ubi, "bad sqnum %llu", aeb->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001793 goto bad_vid_hdr;
1794 }
1795
Artem Bityutskiy517af482012-05-17 14:38:34 +03001796 if (av->vol_id != be32_to_cpu(vidh->vol_id)) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001797 ubi_err(ubi, "bad vol_id %d", av->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001798 goto bad_vid_hdr;
1799 }
1800
Artem Bityutskiy517af482012-05-17 14:38:34 +03001801 if (av->compat != vidh->compat) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001802 ubi_err(ubi, "bad compat %d", vidh->compat);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001803 goto bad_vid_hdr;
1804 }
1805
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001806 if (aeb->lnum != be32_to_cpu(vidh->lnum)) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001807 ubi_err(ubi, "bad lnum %d", aeb->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001808 goto bad_vid_hdr;
1809 }
1810
Artem Bityutskiy517af482012-05-17 14:38:34 +03001811 if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001812 ubi_err(ubi, "bad used_ebs %d", av->used_ebs);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001813 goto bad_vid_hdr;
1814 }
1815
Artem Bityutskiy517af482012-05-17 14:38:34 +03001816 if (av->data_pad != be32_to_cpu(vidh->data_pad)) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001817 ubi_err(ubi, "bad data_pad %d", av->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001818 goto bad_vid_hdr;
1819 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001820 }
1821
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001822 if (!last_aeb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001823 continue;
1824
Artem Bityutskiy517af482012-05-17 14:38:34 +03001825 if (av->highest_lnum != be32_to_cpu(vidh->lnum)) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001826 ubi_err(ubi, "bad highest_lnum %d", av->highest_lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001827 goto bad_vid_hdr;
1828 }
1829
Artem Bityutskiy517af482012-05-17 14:38:34 +03001830 if (av->last_data_size != be32_to_cpu(vidh->data_size)) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001831 ubi_err(ubi, "bad last_data_size %d",
1832 av->last_data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001833 goto bad_vid_hdr;
1834 }
1835 }
1836
1837 /*
1838 * Make sure that all the physical eraseblocks are in one of the lists
1839 * or trees.
1840 */
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001841 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001842 if (!buf)
1843 return -ENOMEM;
1844
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001845 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1846 err = ubi_io_is_bad(ubi, pnum);
Artem Bityutskiy341e1a02007-05-03 11:59:51 +03001847 if (err < 0) {
1848 kfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001849 return err;
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +03001850 } else if (err)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001851 buf[pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001852 }
1853
Artem Bityutskiy517af482012-05-17 14:38:34 +03001854 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
1855 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001856 buf[aeb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001857
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001858 list_for_each_entry(aeb, &ai->free, u.list)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001859 buf[aeb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001860
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001861 list_for_each_entry(aeb, &ai->corr, u.list)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001862 buf[aeb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001863
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001864 list_for_each_entry(aeb, &ai->erase, u.list)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001865 buf[aeb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001866
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001867 list_for_each_entry(aeb, &ai->alien, u.list)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001868 buf[aeb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001869
1870 err = 0;
1871 for (pnum = 0; pnum < ubi->peb_count; pnum++)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001872 if (!buf[pnum]) {
Tanya Brokhman326087032014-10-20 19:57:00 +03001873 ubi_err(ubi, "PEB %d is not referred", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001874 err = 1;
1875 }
1876
1877 kfree(buf);
1878 if (err)
1879 goto out;
1880 return 0;
1881
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001882bad_aeb:
Tanya Brokhman326087032014-10-20 19:57:00 +03001883 ubi_err(ubi, "bad attaching information about LEB %d", aeb->lnum);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001884 ubi_dump_aeb(aeb, 0);
Artem Bityutskiy517af482012-05-17 14:38:34 +03001885 ubi_dump_av(av);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001886 goto out;
1887
Artem Bityutskiy517af482012-05-17 14:38:34 +03001888bad_av:
Tanya Brokhman326087032014-10-20 19:57:00 +03001889 ubi_err(ubi, "bad attaching information about volume %d", av->vol_id);
Artem Bityutskiy517af482012-05-17 14:38:34 +03001890 ubi_dump_av(av);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001891 goto out;
1892
1893bad_vid_hdr:
Tanya Brokhman326087032014-10-20 19:57:00 +03001894 ubi_err(ubi, "bad attaching information about volume %d", av->vol_id);
Artem Bityutskiy517af482012-05-17 14:38:34 +03001895 ubi_dump_av(av);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +03001896 ubi_dump_vid_hdr(vidh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001897
1898out:
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001899 dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001900 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001901}