blob: dc7f9dba5cce550c0c3556b6e7715e66a932001b [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 Bityutskiy85c6e6e2008-07-16 10:25:56 +030022 * UBI scanning sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040023 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030024 * This sub-system is responsible for scanning the flash media, checking UBI
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040025 * headers and providing complete information about the UBI flash image.
26 *
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030027 * The scanning information is represented by a &struct ubi_scan_info' object.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040028 * Information about found volumes is represented by &struct ubi_scan_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.
31 *
Artem Bityutskiy0525dac2010-09-03 17:11:37 +030032 * Scanned logical eraseblocks are represented by &struct ubi_scan_leb objects.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040033 * These objects are kept in per-volume RB-trees with the root at the
34 * corresponding &struct ubi_scan_volume object. To put it differently, we keep
35 * an RB-tree of per-volume objects and each of these objects is the root of
36 * RB-tree of per-eraseblock objects.
37 *
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
54 * error messages. The idea is that we do not lose important data in these case
55 * - we may lose only the data which was being written to the media just before
56 * the power cut happened, and the upper layers (e.g., UBIFS) are supposed to
57 * handle such data losses (e.g., by using the FS journal).
58 *
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 Bityutskiyfef2deb2010-10-29 08:34:50 +030064 * scanning, such PEBs are put to the @corr list and UBI preserves them.
65 * 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 Bityutskiyfef2deb2010-10-29 08:34:50 +030070 * corruptions and UBI's strategy is as follows. UBI assumes corruption type 2
71 * if the VID header is corrupted and the data area does not contain all 0xFFs,
72 * and there were no bit-flips or integrity errors while reading the data area.
73 * Otherwise UBI assumes corruption type 1. So the decision criteria are as
74 * follows.
75 * o If the data area contains only 0xFFs, there is no data, and it is safe
76 * to just erase this PEB - this is corruption type 1.
77 * o If the data area has bit-flips or data integrity errors (ECC errors on
Artem Bityutskiy45aafd32010-10-20 11:54:58 +030078 * NAND), it is probably a PEB which was being erased when power cut
Artem Bityutskiyfef2deb2010-10-29 08:34:50 +030079 * happened, so this is corruption type 1. However, this is just a guess,
80 * which might be wrong.
81 * o Otherwise this it corruption type 2.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040082 */
83
84#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090085#include <linux/slab.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040086#include <linux/crc32.h>
Artem Bityutskiy3013ee32009-01-16 19:08:43 +020087#include <linux/math64.h>
Matthieu CASTET095751a2010-06-03 16:14:27 +020088#include <linux/random.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040089#include "ubi.h"
90
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +030091static int self_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040092
93/* Temporary variables used during scanning */
94static struct ubi_ec_hdr *ech;
95static struct ubi_vid_hdr *vidh;
96
Artem Bityutskiy941dfb02007-05-05 16:33:13 +030097/**
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030098 * add_to_list - add physical eraseblock to a list.
99 * @si: scanning information
100 * @pnum: physical eraseblock number to add
101 * @ec: erase counter of the physical eraseblock
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300102 * @to_head: if not zero, add to the head of the list
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300103 * @list: the list to add to
104 *
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300105 * This function adds physical eraseblock @pnum to free, erase, or alien lists.
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300106 * If @to_head is not zero, PEB will be added to the head of the list, which
107 * basically means it will be processed first later. E.g., we add corrupted
108 * PEBs (corrupted due to power cuts) to the head of the erase list to make
109 * sure we erase them first and get rid of corruptions ASAP. This function
110 * returns zero in case of success and a negative error code in case of
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300111 * failure.
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300112 */
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300113static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, int to_head,
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300114 struct list_head *list)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400115{
116 struct ubi_scan_leb *seb;
117
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300118 if (list == &si->free) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400119 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300120 } else if (list == &si->erase) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400121 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300122 } else if (list == &si->alien) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400123 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300124 si->alien_peb_count += 1;
125 } else
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400126 BUG();
127
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +0200128 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400129 if (!seb)
130 return -ENOMEM;
131
132 seb->pnum = pnum;
133 seb->ec = ec;
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300134 if (to_head)
135 list_add(&seb->u.list, list);
136 else
137 list_add_tail(&seb->u.list, list);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400138 return 0;
139}
140
141/**
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300142 * add_corrupted - add a corrupted physical eraseblock.
143 * @si: scanning information
144 * @pnum: physical eraseblock number to add
145 * @ec: erase counter of the physical eraseblock
146 *
147 * This function adds corrupted physical eraseblock @pnum to the 'corr' list.
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300148 * The corruption was presumably not caused by a power cut. Returns zero in
149 * case of success and a negative error code in case of failure.
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300150 */
151static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)
152{
153 struct ubi_scan_leb *seb;
154
155 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
156
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +0200157 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300158 if (!seb)
159 return -ENOMEM;
160
161 si->corr_peb_count += 1;
162 seb->pnum = pnum;
163 seb->ec = ec;
164 list_add(&seb->u.list, &si->corr);
165 return 0;
166}
167
168/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300169 * validate_vid_hdr - check volume identifier header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400170 * @vid_hdr: the volume identifier header to check
171 * @sv: information about the volume this logical eraseblock belongs to
172 * @pnum: physical eraseblock number the VID header came from
173 *
174 * This function checks that data stored in @vid_hdr is consistent. Returns
175 * non-zero if an inconsistency was found and zero if not.
176 *
177 * Note, UBI does sanity check of everything it reads from the flash media.
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300178 * 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 +0400179 * information in the VID header is consistent to the information in other VID
180 * headers of the same volume.
181 */
182static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
183 const struct ubi_scan_volume *sv, int pnum)
184{
185 int vol_type = vid_hdr->vol_type;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300186 int vol_id = be32_to_cpu(vid_hdr->vol_id);
187 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
188 int data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400189
190 if (sv->leb_count != 0) {
191 int sv_vol_type;
192
193 /*
194 * This is not the first logical eraseblock belonging to this
195 * volume. Ensure that the data in its VID header is consistent
196 * to the data in previous logical eraseblock headers.
197 */
198
199 if (vol_id != sv->vol_id) {
Artem Bityutskiye2986822012-05-16 18:39:56 +0300200 ubi_err("inconsistent vol_id");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400201 goto bad;
202 }
203
204 if (sv->vol_type == UBI_STATIC_VOLUME)
205 sv_vol_type = UBI_VID_STATIC;
206 else
207 sv_vol_type = UBI_VID_DYNAMIC;
208
209 if (vol_type != sv_vol_type) {
Artem Bityutskiye2986822012-05-16 18:39:56 +0300210 ubi_err("inconsistent vol_type");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400211 goto bad;
212 }
213
214 if (used_ebs != sv->used_ebs) {
Artem Bityutskiye2986822012-05-16 18:39:56 +0300215 ubi_err("inconsistent used_ebs");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400216 goto bad;
217 }
218
219 if (data_pad != sv->data_pad) {
Artem Bityutskiye2986822012-05-16 18:39:56 +0300220 ubi_err("inconsistent data_pad");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400221 goto bad;
222 }
223 }
224
225 return 0;
226
227bad:
228 ubi_err("inconsistent VID header at PEB %d", pnum);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300229 ubi_dump_vid_hdr(vid_hdr);
Artem Bityutskiy614c74a752012-05-16 17:59:36 +0300230 ubi_dump_sv(sv);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400231 return -EINVAL;
232}
233
234/**
235 * add_volume - add volume to the scanning information.
236 * @si: scanning information
237 * @vol_id: ID of the volume to add
238 * @pnum: physical eraseblock number
239 * @vid_hdr: volume identifier header
240 *
241 * If the volume corresponding to the @vid_hdr logical eraseblock is already
242 * present in the scanning information, this function does nothing. Otherwise
243 * it adds corresponding volume to the scanning information. Returns a pointer
244 * to the scanning volume object in case of success and a negative error code
245 * in case of failure.
246 */
247static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
248 int pnum,
249 const struct ubi_vid_hdr *vid_hdr)
250{
251 struct ubi_scan_volume *sv;
252 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
253
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300254 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400255
256 /* Walk the volume RB-tree to look if this volume is already present */
257 while (*p) {
258 parent = *p;
259 sv = rb_entry(parent, struct ubi_scan_volume, rb);
260
261 if (vol_id == sv->vol_id)
262 return sv;
263
264 if (vol_id > sv->vol_id)
265 p = &(*p)->rb_left;
266 else
267 p = &(*p)->rb_right;
268 }
269
270 /* The volume is absent - add it */
271 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
272 if (!sv)
273 return ERR_PTR(-ENOMEM);
274
275 sv->highest_lnum = sv->leb_count = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400276 sv->vol_id = vol_id;
277 sv->root = RB_ROOT;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300278 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
279 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400280 sv->compat = vid_hdr->compat;
281 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
282 : UBI_STATIC_VOLUME;
283 if (vol_id > si->highest_vol_id)
284 si->highest_vol_id = vol_id;
285
286 rb_link_node(&sv->rb, parent, p);
287 rb_insert_color(&sv->rb, &si->volumes);
288 si->vols_found += 1;
289 dbg_bld("added volume %d", vol_id);
290 return sv;
291}
292
293/**
294 * compare_lebs - find out which logical eraseblock is newer.
295 * @ubi: UBI device description object
296 * @seb: first logical eraseblock to compare
297 * @pnum: physical eraseblock number of the second logical eraseblock to
298 * compare
299 * @vid_hdr: volume identifier header of the second logical eraseblock
300 *
301 * This function compares 2 copies of a LEB and informs which one is newer. In
302 * case of success this function returns a positive value, in case of failure, a
303 * negative error code is returned. The success return codes use the following
304 * bits:
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900305 * o bit 0 is cleared: the first PEB (described by @seb) is newer than the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400306 * second PEB (described by @pnum and @vid_hdr);
307 * o bit 0 is set: the second PEB is newer;
308 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
309 * o bit 1 is set: bit-flips were detected in the newer LEB;
310 * o bit 2 is cleared: the older LEB is not corrupted;
311 * o bit 2 is set: the older LEB is corrupted.
312 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300313static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
314 int pnum, const struct ubi_vid_hdr *vid_hdr)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400315{
316 void *buf;
317 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
318 uint32_t data_crc, crc;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300319 struct ubi_vid_hdr *vh = NULL;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300320 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400321
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300322 if (sqnum2 == seb->sqnum) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400323 /*
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300324 * This must be a really ancient UBI image which has been
325 * created before sequence numbers support has been added. At
326 * that times we used 32-bit LEB versions stored in logical
327 * eraseblocks. That was before UBI got into mainline. We do not
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300328 * support these images anymore. Well, those images still work,
329 * but only if no unclean reboots happened.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400330 */
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300331 ubi_err("unsupported on-flash UBI format\n");
332 return -EINVAL;
333 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400334
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300335 /* Obviously the LEB with lower sequence counter is older */
Richard Weinberger0964f6a2012-05-08 00:47:20 +0200336 second_is_newer = (sqnum2 > seb->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400337
338 /*
339 * Now we know which copy is newer. If the copy flag of the PEB with
340 * newer version is not set, then we just return, otherwise we have to
341 * check data CRC. For the second PEB we already have the VID header,
342 * for the first one - we'll need to re-read it from flash.
343 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300344 * Note: this may be optimized so that we wouldn't read twice.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400345 */
346
347 if (second_is_newer) {
348 if (!vid_hdr->copy_flag) {
349 /* It is not a copy, so it is newer */
350 dbg_bld("second PEB %d is newer, copy_flag is unset",
351 pnum);
352 return 1;
353 }
354 } else {
Artem Bityutskiyfb22b592010-10-19 22:00:11 +0300355 if (!seb->copy_flag) {
356 /* It is not a copy, so it is newer */
357 dbg_bld("first PEB %d is newer, copy_flag is unset",
358 pnum);
359 return bitflips << 1;
360 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400361
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300362 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300363 if (!vh)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400364 return -ENOMEM;
365
Artem Bityutskiyfb22b592010-10-19 22:00:11 +0300366 pnum = seb->pnum;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300367 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400368 if (err) {
369 if (err == UBI_IO_BITFLIPS)
370 bitflips = 1;
371 else {
Artem Bityutskiye2986822012-05-16 18:39:56 +0300372 ubi_err("VID of PEB %d header is bad, but it "
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300373 "was OK earlier, err %d", pnum, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400374 if (err > 0)
375 err = -EIO;
376
377 goto out_free_vidh;
378 }
379 }
380
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300381 vid_hdr = vh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400382 }
383
384 /* Read the data of the copy and check the CRC */
385
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300386 len = be32_to_cpu(vid_hdr->data_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300387 buf = vmalloc(len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400388 if (!buf) {
389 err = -ENOMEM;
390 goto out_free_vidh;
391 }
392
393 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
Brian Norrisd57f40542011-09-20 18:34:25 -0700394 if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400395 goto out_free_buf;
396
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300397 data_crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400398 crc = crc32(UBI_CRC32_INIT, buf, len);
399 if (crc != data_crc) {
400 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
401 pnum, crc, data_crc);
402 corrupted = 1;
403 bitflips = 0;
404 second_is_newer = !second_is_newer;
405 } else {
406 dbg_bld("PEB %d CRC is OK", pnum);
407 bitflips = !!err;
408 }
409
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300410 vfree(buf);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300411 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400412
413 if (second_is_newer)
414 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
415 else
416 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
417
418 return second_is_newer | (bitflips << 1) | (corrupted << 2);
419
420out_free_buf:
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300421 vfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400422out_free_vidh:
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300423 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400424 return err;
425}
426
427/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300428 * ubi_scan_add_used - add physical eraseblock to the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400429 * @ubi: UBI device description object
430 * @si: scanning information
431 * @pnum: the physical eraseblock number
432 * @ec: erase counter
433 * @vid_hdr: the volume identifier header
434 * @bitflips: if bit-flips were detected when this physical eraseblock was read
435 *
Artem Bityutskiy79b510c2007-05-05 17:36:17 +0300436 * This function adds information about a used physical eraseblock to the
437 * 'used' tree of the corresponding volume. The function is rather complex
438 * because it has to handle cases when this is not the first physical
439 * eraseblock belonging to the same logical eraseblock, and the newer one has
440 * to be picked, while the older one has to be dropped. This function returns
441 * zero in case of success and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400442 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300443int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400444 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
445 int bitflips)
446{
447 int err, vol_id, lnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400448 unsigned long long sqnum;
449 struct ubi_scan_volume *sv;
450 struct ubi_scan_leb *seb;
451 struct rb_node **p, *parent = NULL;
452
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300453 vol_id = be32_to_cpu(vid_hdr->vol_id);
454 lnum = be32_to_cpu(vid_hdr->lnum);
455 sqnum = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400456
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300457 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
458 pnum, vol_id, lnum, ec, sqnum, bitflips);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400459
460 sv = add_volume(si, vol_id, pnum, vid_hdr);
Julien Brunel0e4a0082008-09-26 15:27:25 +0200461 if (IS_ERR(sv))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400462 return PTR_ERR(sv);
463
Brijesh Singh76eafe42007-07-06 14:35:43 +0300464 if (si->max_sqnum < sqnum)
465 si->max_sqnum = sqnum;
466
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400467 /*
468 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
469 * if this is the first instance of this logical eraseblock or not.
470 */
471 p = &sv->root.rb_node;
472 while (*p) {
473 int cmp_res;
474
475 parent = *p;
476 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
477 if (lnum != seb->lnum) {
478 if (lnum < seb->lnum)
479 p = &(*p)->rb_left;
480 else
481 p = &(*p)->rb_right;
482 continue;
483 }
484
485 /*
486 * There is already a physical eraseblock describing the same
487 * logical eraseblock present.
488 */
489
490 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300491 "EC %d", seb->pnum, seb->sqnum, seb->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400492
493 /*
494 * Make sure that the logical eraseblocks have different
495 * sequence numbers. Otherwise the image is bad.
496 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300497 * However, if the sequence number is zero, we assume it must
498 * be an ancient UBI image from the era when UBI did not have
499 * sequence numbers. We still can attach these images, unless
500 * there is a need to distinguish between old and new
501 * eraseblocks, in which case we'll refuse the image in
502 * 'compare_lebs()'. In other words, we attach old clean
503 * images, but refuse attaching old images with duplicated
504 * logical eraseblocks because there was an unclean reboot.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400505 */
506 if (seb->sqnum == sqnum && sqnum != 0) {
507 ubi_err("two LEBs with same sequence number %llu",
508 sqnum);
Artem Bityutskiyb989bd42012-05-16 18:01:58 +0300509 ubi_dump_seb(seb, 0);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300510 ubi_dump_vid_hdr(vid_hdr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400511 return -EINVAL;
512 }
513
514 /*
515 * Now we have to drop the older one and preserve the newer
516 * one.
517 */
518 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
519 if (cmp_res < 0)
520 return cmp_res;
521
522 if (cmp_res & 1) {
523 /*
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900524 * This logical eraseblock is newer than the one
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400525 * found earlier.
526 */
527 err = validate_vid_hdr(vid_hdr, sv, pnum);
528 if (err)
529 return err;
530
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300531 err = add_to_list(si, seb->pnum, seb->ec, cmp_res & 4,
532 &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400533 if (err)
534 return err;
535
536 seb->ec = ec;
537 seb->pnum = pnum;
538 seb->scrub = ((cmp_res & 2) || bitflips);
Artem Bityutskiyfb22b592010-10-19 22:00:11 +0300539 seb->copy_flag = vid_hdr->copy_flag;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400540 seb->sqnum = sqnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400541
542 if (sv->highest_lnum == lnum)
543 sv->last_data_size =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300544 be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400545
546 return 0;
547 } else {
548 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200549 * This logical eraseblock is older than the one found
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400550 * previously.
551 */
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300552 return add_to_list(si, pnum, ec, cmp_res & 4,
553 &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400554 }
555 }
556
557 /*
558 * We've met this logical eraseblock for the first time, add it to the
559 * scanning information.
560 */
561
562 err = validate_vid_hdr(vid_hdr, sv, pnum);
563 if (err)
564 return err;
565
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +0200566 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400567 if (!seb)
568 return -ENOMEM;
569
570 seb->ec = ec;
571 seb->pnum = pnum;
572 seb->lnum = lnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400573 seb->scrub = bitflips;
Artem Bityutskiyfb22b592010-10-19 22:00:11 +0300574 seb->copy_flag = vid_hdr->copy_flag;
575 seb->sqnum = sqnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400576
577 if (sv->highest_lnum <= lnum) {
578 sv->highest_lnum = lnum;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300579 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400580 }
581
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400582 sv->leb_count += 1;
583 rb_link_node(&seb->u.rb, parent, p);
584 rb_insert_color(&seb->u.rb, &sv->root);
585 return 0;
586}
587
588/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300589 * ubi_scan_find_sv - find volume in the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400590 * @si: scanning information
591 * @vol_id: the requested volume ID
592 *
593 * This function returns a pointer to the volume description or %NULL if there
594 * are no data about this volume in the scanning information.
595 */
596struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
597 int vol_id)
598{
599 struct ubi_scan_volume *sv;
600 struct rb_node *p = si->volumes.rb_node;
601
602 while (p) {
603 sv = rb_entry(p, struct ubi_scan_volume, rb);
604
605 if (vol_id == sv->vol_id)
606 return sv;
607
608 if (vol_id > sv->vol_id)
609 p = p->rb_left;
610 else
611 p = p->rb_right;
612 }
613
614 return NULL;
615}
616
617/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300618 * ubi_scan_find_seb - find LEB in the volume scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400619 * @sv: a pointer to the volume scanning information
620 * @lnum: the requested logical eraseblock
621 *
622 * This function returns a pointer to the scanning logical eraseblock or %NULL
623 * if there are no data about it in the scanning volume information.
624 */
625struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
626 int lnum)
627{
628 struct ubi_scan_leb *seb;
629 struct rb_node *p = sv->root.rb_node;
630
631 while (p) {
632 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
633
634 if (lnum == seb->lnum)
635 return seb;
636
637 if (lnum > seb->lnum)
638 p = p->rb_left;
639 else
640 p = p->rb_right;
641 }
642
643 return NULL;
644}
645
646/**
647 * ubi_scan_rm_volume - delete scanning information about a volume.
648 * @si: scanning information
649 * @sv: the volume scanning information to delete
650 */
651void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
652{
653 struct rb_node *rb;
654 struct ubi_scan_leb *seb;
655
656 dbg_bld("remove scanning information about volume %d", sv->vol_id);
657
658 while ((rb = rb_first(&sv->root))) {
659 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
660 rb_erase(&seb->u.rb, &sv->root);
661 list_add_tail(&seb->u.list, &si->erase);
662 }
663
664 rb_erase(&sv->rb, &si->volumes);
665 kfree(sv);
666 si->vols_found -= 1;
667}
668
669/**
670 * ubi_scan_erase_peb - erase a physical eraseblock.
671 * @ubi: UBI device description object
672 * @si: scanning information
673 * @pnum: physical eraseblock number to erase;
674 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
675 *
676 * This function erases physical eraseblock 'pnum', and writes the erase
677 * counter header to it. This function should only be used on UBI device
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300678 * initialization stages, when the EBA sub-system had not been yet initialized.
679 * This function returns zero in case of success and a negative error code in
680 * case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400681 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300682int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
683 int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400684{
685 int err;
686 struct ubi_ec_hdr *ec_hdr;
687
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400688 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
689 /*
690 * Erase counter overflow. Upgrade UBI and use 64-bit
691 * erase counters internally.
692 */
693 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
694 return -EINVAL;
695 }
696
Florin Malitadcec4c32007-07-19 15:22:41 -0400697 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
698 if (!ec_hdr)
699 return -ENOMEM;
700
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300701 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400702
703 err = ubi_io_sync_erase(ubi, pnum, 0);
704 if (err < 0)
705 goto out_free;
706
707 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
708
709out_free:
710 kfree(ec_hdr);
711 return err;
712}
713
714/**
715 * ubi_scan_get_free_peb - get a free physical eraseblock.
716 * @ubi: UBI device description object
717 * @si: scanning information
718 *
719 * This function returns a free physical eraseblock. It is supposed to be
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300720 * called on the UBI initialization stages when the wear-leveling sub-system is
721 * not initialized yet. This function picks a physical eraseblocks from one of
722 * the lists, writes the EC header if it is needed, and removes it from the
723 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400724 *
725 * This function returns scanning physical eraseblock information in case of
726 * success and an error code in case of failure.
727 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300728struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400729 struct ubi_scan_info *si)
730{
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300731 int err = 0;
732 struct ubi_scan_leb *seb, *tmp_seb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400733
734 if (!list_empty(&si->free)) {
735 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
736 list_del(&seb->u.list);
737 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
738 return seb;
739 }
740
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300741 /*
742 * We try to erase the first physical eraseblock from the erase list
743 * and pick it if we succeed, or try to erase the next one if not. And
744 * so forth. We don't want to take care about bad eraseblocks here -
745 * they'll be handled later.
746 */
747 list_for_each_entry_safe(seb, tmp_seb, &si->erase, u.list) {
748 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
749 seb->ec = si->mean_ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400750
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300751 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
752 if (err)
753 continue;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400754
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300755 seb->ec += 1;
756 list_del(&seb->u.list);
757 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
758 return seb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400759 }
760
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300761 ubi_err("no free eraseblocks");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400762 return ERR_PTR(-ENOSPC);
763}
764
765/**
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300766 * check_corruption - check the data area of PEB.
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300767 * @ubi: UBI device description object
768 * @vid_hrd: the (corrupted) VID header of this PEB
769 * @pnum: the physical eraseblock number to check
770 *
771 * This is a helper function which is used to distinguish between VID header
772 * corruptions caused by power cuts and other reasons. If the PEB contains only
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300773 * 0xFF bytes in the data area, the VID header is most probably corrupted
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300774 * because of a power cut (%0 is returned in this case). Otherwise, it was
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300775 * probably corrupted for some other reasons (%1 is returned in this case). A
776 * negative error code is returned if a read error occurred.
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300777 *
778 * If the corruption reason was a power cut, UBI can safely erase this PEB.
779 * Otherwise, it should preserve it to avoid possibly destroying important
780 * information.
781 */
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300782static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
783 int pnum)
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300784{
785 int err;
786
787 mutex_lock(&ubi->buf_mutex);
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200788 memset(ubi->peb_buf, 0x00, ubi->leb_size);
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300789
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200790 err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start,
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300791 ubi->leb_size);
Brian Norrisd57f40542011-09-20 18:34:25 -0700792 if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) {
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300793 /*
794 * Bit-flips or integrity errors while reading the data area.
795 * It is difficult to say for sure what type of corruption is
796 * this, but presumably a power cut happened while this PEB was
797 * erased, so it became unstable and corrupted, and should be
798 * erased.
799 */
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300800 err = 0;
801 goto out_unlock;
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300802 }
803
804 if (err)
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300805 goto out_unlock;
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300806
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200807 if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size))
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300808 goto out_unlock;
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300809
810 ubi_err("PEB %d contains corrupted VID header, and the data does not "
811 "contain all 0xFF, this may be a non-UBI PEB or a severe VID "
812 "header corruption which requires manual inspection", pnum);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300813 ubi_dump_vid_hdr(vid_hdr);
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300814 dbg_msg("hexdump of PEB %d offset %d, length %d",
815 pnum, ubi->leb_start, ubi->leb_size);
816 ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200817 ubi->peb_buf, ubi->leb_size, 1);
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300818 err = 1;
819
820out_unlock:
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300821 mutex_unlock(&ubi->buf_mutex);
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300822 return err;
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300823}
824
825/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300826 * process_eb - read, check UBI headers, and add them to scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400827 * @ubi: UBI device description object
828 * @si: scanning information
829 * @pnum: the physical eraseblock number
830 *
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300831 * This function returns a zero if the physical eraseblock was successfully
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400832 * handled and a negative error code in case of failure.
833 */
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300834static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
835 int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400836{
Artem Bityutskiyc18a8412008-01-24 11:19:14 +0200837 long long uninitialized_var(ec);
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300838 int err, bitflips = 0, vol_id, ec_err = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400839
840 dbg_bld("scan PEB %d", pnum);
841
842 /* Skip bad physical eraseblocks */
843 err = ubi_io_is_bad(ubi, pnum);
844 if (err < 0)
845 return err;
846 else if (err) {
847 /*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300848 * FIXME: this is actually duty of the I/O sub-system to
849 * initialize this, but MTD does not provide enough
850 * information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400851 */
852 si->bad_peb_count += 1;
853 return 0;
854 }
855
856 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
857 if (err < 0)
858 return err;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300859 switch (err) {
860 case 0:
861 break;
862 case UBI_IO_BITFLIPS:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400863 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300864 break;
865 case UBI_IO_FF:
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300866 si->empty_peb_count += 1;
867 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 0,
868 &si->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300869 case UBI_IO_FF_BITFLIPS:
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300870 si->empty_peb_count += 1;
871 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 1,
872 &si->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300873 case UBI_IO_BAD_HDR_EBADMSG:
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300874 case UBI_IO_BAD_HDR:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400875 /*
876 * We have to also look at the VID header, possibly it is not
877 * corrupted. Set %bitflips flag in order to make this PEB be
878 * moved and EC be re-created.
879 */
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300880 ec_err = err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400881 ec = UBI_SCAN_UNKNOWN_EC;
882 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300883 break;
884 default:
885 ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
886 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400887 }
888
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300889 if (!ec_err) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300890 int image_seq;
891
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400892 /* Make sure UBI version is OK */
893 if (ech->version != UBI_VERSION) {
894 ubi_err("this UBI version is %d, image version is %d",
895 UBI_VERSION, (int)ech->version);
896 return -EINVAL;
897 }
898
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300899 ec = be64_to_cpu(ech->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400900 if (ec > UBI_MAX_ERASECOUNTER) {
901 /*
902 * Erase counter overflow. The EC headers have 64 bits
903 * reserved, but we anyway make use of only 31 bit
904 * values, as this seems to be enough for any existing
905 * flash. Upgrade UBI and use 64-bit erase counters
906 * internally.
907 */
908 ubi_err("erase counter overflow, max is %d",
909 UBI_MAX_ERASECOUNTER);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300910 ubi_dump_ec_hdr(ech);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400911 return -EINVAL;
912 }
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300913
Adrian Hunter32bc4822009-07-24 19:16:04 +0300914 /*
915 * Make sure that all PEBs have the same image sequence number.
916 * This allows us to detect situations when users flash UBI
917 * images incorrectly, so that the flash has the new UBI image
918 * and leftovers from the old one. This feature was added
919 * relatively recently, and the sequence number was always
920 * zero, because old UBI implementations always set it to zero.
921 * For this reasons, we do not panic if some PEBs have zero
922 * sequence number, while other PEBs have non-zero sequence
923 * number.
924 */
Holger Brunck3dc948d2009-07-13 16:47:57 +0200925 image_seq = be32_to_cpu(ech->image_seq);
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300926 if (!ubi->image_seq && image_seq)
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300927 ubi->image_seq = image_seq;
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300928 if (ubi->image_seq && image_seq &&
929 ubi->image_seq != image_seq) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300930 ubi_err("bad image sequence number %d in PEB %d, "
931 "expected %d", image_seq, pnum, ubi->image_seq);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300932 ubi_dump_ec_hdr(ech);
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300933 return -EINVAL;
934 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400935 }
936
937 /* OK, we've done with the EC header, let's look at the VID header */
938
939 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
940 if (err < 0)
941 return err;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300942 switch (err) {
943 case 0:
944 break;
945 case UBI_IO_BITFLIPS:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400946 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300947 break;
948 case UBI_IO_BAD_HDR_EBADMSG:
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300949 if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
950 /*
951 * Both EC and VID headers are corrupted and were read
952 * with data integrity error, probably this is a bad
953 * PEB, bit it is not marked as bad yet. This may also
954 * be a result of power cut during erasure.
955 */
956 si->maybe_bad_peb_count += 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300957 case UBI_IO_BAD_HDR:
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300958 if (ec_err)
959 /*
960 * Both headers are corrupted. There is a possibility
961 * that this a valid UBI PEB which has corresponding
962 * LEB, but the headers are corrupted. However, it is
963 * impossible to distinguish it from a PEB which just
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300964 * contains garbage because of a power cut during erase
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300965 * operation. So we just schedule this PEB for erasure.
Artem Bityutskiy7ac760c2010-12-02 06:34:01 +0200966 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300967 * Besides, in case of NOR flash, we deliberately
Artem Bityutskiy7ac760c2010-12-02 06:34:01 +0200968 * corrupt both headers because NOR flash erasure is
969 * slow and can start from the end.
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300970 */
971 err = 0;
972 else
973 /*
974 * The EC was OK, but the VID header is corrupted. We
975 * have to check what is in the data area.
976 */
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300977 err = check_corruption(ubi, vidh, pnum);
Artem Bityutskiydf3fca42010-10-20 11:51:21 +0300978
979 if (err < 0)
980 return err;
981 else if (!err)
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300982 /* This corruption is caused by a power cut */
983 err = add_to_list(si, pnum, ec, 1, &si->erase);
984 else
985 /* This is an unexpected corruption */
986 err = add_corrupted(si, pnum, ec);
987 if (err)
988 return err;
989 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300990 case UBI_IO_FF_BITFLIPS:
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300991 err = add_to_list(si, pnum, ec, 1, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400992 if (err)
993 return err;
994 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300995 case UBI_IO_FF:
996 if (ec_err)
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300997 err = add_to_list(si, pnum, ec, 1, &si->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300998 else
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300999 err = add_to_list(si, pnum, ec, 0, &si->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001000 if (err)
1001 return err;
1002 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +03001003 default:
1004 ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
1005 err);
1006 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001007 }
1008
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001009 vol_id = be32_to_cpu(vidh->vol_id);
Artem Bityutskiy91f2d532008-01-24 11:23:23 +02001010 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001011 int lnum = be32_to_cpu(vidh->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001012
1013 /* Unsupported internal volume */
1014 switch (vidh->compat) {
1015 case UBI_COMPAT_DELETE:
1016 ubi_msg("\"delete\" compatible internal volume %d:%d"
Brijesh Singh158132c2010-06-16 09:28:26 +03001017 " found, will remove it", vol_id, lnum);
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001018 err = add_to_list(si, pnum, ec, 1, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001019 if (err)
1020 return err;
Brijesh Singh158132c2010-06-16 09:28:26 +03001021 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001022
1023 case UBI_COMPAT_RO:
1024 ubi_msg("read-only compatible internal volume %d:%d"
1025 " found, switch to read-only mode",
1026 vol_id, lnum);
1027 ubi->ro_mode = 1;
1028 break;
1029
1030 case UBI_COMPAT_PRESERVE:
1031 ubi_msg("\"preserve\" compatible internal volume %d:%d"
1032 " found", vol_id, lnum);
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001033 err = add_to_list(si, pnum, ec, 0, &si->alien);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001034 if (err)
1035 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001036 return 0;
1037
1038 case UBI_COMPAT_REJECT:
1039 ubi_err("incompatible internal volume %d:%d found",
1040 vol_id, lnum);
1041 return -EINVAL;
1042 }
1043 }
1044
Artem Bityutskiye0e718c2010-09-03 14:53:23 +03001045 if (ec_err)
Artem Bityutskiy29a88c92009-07-19 14:03:16 +03001046 ubi_warn("valid VID header but corrupted EC header at PEB %d",
1047 pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001048 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
1049 if (err)
1050 return err;
1051
1052adjust_mean_ec:
Artem Bityutskiye0e718c2010-09-03 14:53:23 +03001053 if (!ec_err) {
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +03001054 si->ec_sum += ec;
1055 si->ec_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001056 if (ec > si->max_ec)
1057 si->max_ec = ec;
1058 if (ec < si->min_ec)
1059 si->min_ec = ec;
1060 }
1061
1062 return 0;
1063}
1064
1065/**
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001066 * check_what_we_have - check what PEB were found by scanning.
1067 * @ubi: UBI device description object
1068 * @si: scanning information
1069 *
1070 * This is a helper function which takes a look what PEBs were found by
1071 * scanning, and decides whether the flash is empty and should be formatted and
1072 * whether there are too many corrupted PEBs and we should not attach this
1073 * MTD device. Returns zero if we should proceed with attaching the MTD device,
1074 * and %-EINVAL if we should not.
1075 */
Artem Bityutskiyf5d5b1f2010-06-14 08:15:39 +03001076static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001077{
1078 struct ubi_scan_leb *seb;
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001079 int max_corr, peb_count;
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001080
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001081 peb_count = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
1082 max_corr = peb_count / 20 ?: 8;
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001083
1084 /*
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001085 * Few corrupted PEBs is not a problem and may be just a result of
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001086 * unclean reboots. However, many of them may indicate some problems
1087 * with the flash HW or driver.
1088 */
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001089 if (si->corr_peb_count) {
1090 ubi_err("%d PEBs are corrupted and preserved",
1091 si->corr_peb_count);
1092 printk(KERN_ERR "Corrupted PEBs are:");
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001093 list_for_each_entry(seb, &si->corr, u.list)
1094 printk(KERN_CONT " %d", seb->pnum);
1095 printk(KERN_CONT "\n");
1096
1097 /*
1098 * If too many PEBs are corrupted, we refuse attaching,
1099 * otherwise, only print a warning.
1100 */
1101 if (si->corr_peb_count >= max_corr) {
Artem Bityutskiyfeddbb32011-03-28 10:12:25 +03001102 ubi_err("too many corrupted PEBs, refusing");
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001103 return -EINVAL;
1104 }
1105 }
1106
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001107 if (si->empty_peb_count + si->maybe_bad_peb_count == peb_count) {
1108 /*
1109 * All PEBs are empty, or almost all - a couple PEBs look like
1110 * they may be bad PEBs which were not marked as bad yet.
1111 *
1112 * This piece of code basically tries to distinguish between
1113 * the following situations:
1114 *
1115 * 1. Flash is empty, but there are few bad PEBs, which are not
1116 * marked as bad so far, and which were read with error. We
1117 * want to go ahead and format this flash. While formatting,
1118 * the faulty PEBs will probably be marked as bad.
1119 *
1120 * 2. Flash contains non-UBI data and we do not want to format
1121 * it and destroy possibly important information.
1122 */
1123 if (si->maybe_bad_peb_count <= 2) {
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001124 si->is_empty = 1;
1125 ubi_msg("empty MTD device detected");
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001126 get_random_bytes(&ubi->image_seq,
1127 sizeof(ubi->image_seq));
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001128 } else {
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001129 ubi_err("MTD device is not UBI-formatted and possibly "
1130 "contains non-UBI data - refusing it");
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001131 return -EINVAL;
1132 }
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001133
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001134 }
1135
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001136 return 0;
1137}
1138
1139/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001140 * ubi_scan - scan an MTD device.
1141 * @ubi: UBI device description object
1142 *
1143 * This function does full scanning of an MTD device and returns complete
1144 * information about it. In case of failure, an error code is returned.
1145 */
1146struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
1147{
1148 int err, pnum;
1149 struct rb_node *rb1, *rb2;
1150 struct ubi_scan_volume *sv;
1151 struct ubi_scan_leb *seb;
1152 struct ubi_scan_info *si;
1153
1154 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
1155 if (!si)
1156 return ERR_PTR(-ENOMEM);
1157
1158 INIT_LIST_HEAD(&si->corr);
1159 INIT_LIST_HEAD(&si->free);
1160 INIT_LIST_HEAD(&si->erase);
1161 INIT_LIST_HEAD(&si->alien);
1162 si->volumes = RB_ROOT;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001163
1164 err = -ENOMEM;
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +02001165 si->scan_leb_slab = kmem_cache_create("ubi_scan_leb_slab",
1166 sizeof(struct ubi_scan_leb),
1167 0, 0, NULL);
1168 if (!si->scan_leb_slab)
1169 goto out_si;
1170
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001171 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1172 if (!ech)
Richard Weinbergera29852b2012-01-30 18:20:13 +01001173 goto out_si;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001174
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001175 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001176 if (!vidh)
1177 goto out_ech;
1178
1179 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1180 cond_resched();
1181
Artem Bityutskiyc8566352008-07-16 17:40:22 +03001182 dbg_gen("process PEB %d", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001183 err = process_eb(ubi, si, pnum);
1184 if (err < 0)
1185 goto out_vidh;
1186 }
1187
1188 dbg_msg("scanning is finished");
1189
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +03001190 /* Calculate mean erase counter */
Artem Bityutskiy3013ee32009-01-16 19:08:43 +02001191 if (si->ec_count)
1192 si->mean_ec = div_u64(si->ec_sum, si->ec_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001193
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001194 err = check_what_we_have(ubi, si);
1195 if (err)
1196 goto out_vidh;
Artem Bityutskiy4a406852009-07-19 14:33:14 +03001197
1198 /*
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001199 * In case of unknown erase counter we use the mean erase counter
1200 * value.
1201 */
1202 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1203 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1204 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1205 seb->ec = si->mean_ec;
1206 }
1207
1208 list_for_each_entry(seb, &si->free, u.list) {
1209 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1210 seb->ec = si->mean_ec;
1211 }
1212
1213 list_for_each_entry(seb, &si->corr, u.list)
1214 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1215 seb->ec = si->mean_ec;
1216
1217 list_for_each_entry(seb, &si->erase, u.list)
1218 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1219 seb->ec = si->mean_ec;
1220
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001221 err = self_check_si(ubi, si);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001222 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001223 goto out_vidh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001224
1225 ubi_free_vid_hdr(ubi, vidh);
1226 kfree(ech);
1227
1228 return si;
1229
1230out_vidh:
1231 ubi_free_vid_hdr(ubi, vidh);
1232out_ech:
1233 kfree(ech);
1234out_si:
1235 ubi_scan_destroy_si(si);
1236 return ERR_PTR(err);
1237}
1238
1239/**
1240 * destroy_sv - free the scanning volume information
1241 * @sv: scanning volume information
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +02001242 * @si: scanning information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001243 *
1244 * This function destroys the volume RB-tree (@sv->root) and the scanning
1245 * volume information.
1246 */
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +02001247static void destroy_sv(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001248{
1249 struct ubi_scan_leb *seb;
1250 struct rb_node *this = sv->root.rb_node;
1251
1252 while (this) {
1253 if (this->rb_left)
1254 this = this->rb_left;
1255 else if (this->rb_right)
1256 this = this->rb_right;
1257 else {
1258 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1259 this = rb_parent(this);
1260 if (this) {
1261 if (this->rb_left == &seb->u.rb)
1262 this->rb_left = NULL;
1263 else
1264 this->rb_right = NULL;
1265 }
1266
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +02001267 kmem_cache_free(si->scan_leb_slab, seb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001268 }
1269 }
1270 kfree(sv);
1271}
1272
1273/**
1274 * ubi_scan_destroy_si - destroy scanning information.
1275 * @si: scanning information
1276 */
1277void ubi_scan_destroy_si(struct ubi_scan_info *si)
1278{
1279 struct ubi_scan_leb *seb, *seb_tmp;
1280 struct ubi_scan_volume *sv;
1281 struct rb_node *rb;
1282
1283 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1284 list_del(&seb->u.list);
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +02001285 kmem_cache_free(si->scan_leb_slab, seb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001286 }
1287 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1288 list_del(&seb->u.list);
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +02001289 kmem_cache_free(si->scan_leb_slab, seb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001290 }
1291 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1292 list_del(&seb->u.list);
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +02001293 kmem_cache_free(si->scan_leb_slab, seb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001294 }
1295 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1296 list_del(&seb->u.list);
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +02001297 kmem_cache_free(si->scan_leb_slab, seb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001298 }
1299
1300 /* Destroy the volume RB-tree */
1301 rb = si->volumes.rb_node;
1302 while (rb) {
1303 if (rb->rb_left)
1304 rb = rb->rb_left;
1305 else if (rb->rb_right)
1306 rb = rb->rb_right;
1307 else {
1308 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1309
1310 rb = rb_parent(rb);
1311 if (rb) {
1312 if (rb->rb_left == &sv->rb)
1313 rb->rb_left = NULL;
1314 else
1315 rb->rb_right = NULL;
1316 }
1317
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +02001318 destroy_sv(si, sv);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001319 }
1320 }
1321
Richard Weinbergera29852b2012-01-30 18:20:13 +01001322 if (si->scan_leb_slab)
1323 kmem_cache_destroy(si->scan_leb_slab);
1324
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001325 kfree(si);
1326}
1327
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001328/**
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001329 * self_check_si - check the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001330 * @ubi: UBI device description object
1331 * @si: scanning information
1332 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001333 * This function returns zero if the scanning information is all right, and a
1334 * negative error code if not or if an error occurred.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001335 */
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001336static int self_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001337{
1338 int pnum, err, vols_found = 0;
1339 struct rb_node *rb1, *rb2;
1340 struct ubi_scan_volume *sv;
1341 struct ubi_scan_leb *seb, *last_seb;
1342 uint8_t *buf;
1343
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001344 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001345 return 0;
1346
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001347 /*
Artem Bityutskiy78d87c92007-05-05 11:24:02 +03001348 * At first, check that scanning information is OK.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001349 */
1350 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1351 int leb_count = 0;
1352
1353 cond_resched();
1354
1355 vols_found += 1;
1356
1357 if (si->is_empty) {
1358 ubi_err("bad is_empty flag");
1359 goto bad_sv;
1360 }
1361
1362 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1363 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1364 sv->data_pad < 0 || sv->last_data_size < 0) {
1365 ubi_err("negative values");
1366 goto bad_sv;
1367 }
1368
1369 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1370 sv->vol_id < UBI_INTERNAL_VOL_START) {
1371 ubi_err("bad vol_id");
1372 goto bad_sv;
1373 }
1374
1375 if (sv->vol_id > si->highest_vol_id) {
1376 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1377 si->highest_vol_id, sv->vol_id);
1378 goto out;
1379 }
1380
1381 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1382 sv->vol_type != UBI_STATIC_VOLUME) {
1383 ubi_err("bad vol_type");
1384 goto bad_sv;
1385 }
1386
1387 if (sv->data_pad > ubi->leb_size / 2) {
1388 ubi_err("bad data_pad");
1389 goto bad_sv;
1390 }
1391
1392 last_seb = NULL;
1393 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1394 cond_resched();
1395
1396 last_seb = seb;
1397 leb_count += 1;
1398
1399 if (seb->pnum < 0 || seb->ec < 0) {
1400 ubi_err("negative values");
1401 goto bad_seb;
1402 }
1403
1404 if (seb->ec < si->min_ec) {
1405 ubi_err("bad si->min_ec (%d), %d found",
1406 si->min_ec, seb->ec);
1407 goto bad_seb;
1408 }
1409
1410 if (seb->ec > si->max_ec) {
1411 ubi_err("bad si->max_ec (%d), %d found",
1412 si->max_ec, seb->ec);
1413 goto bad_seb;
1414 }
1415
1416 if (seb->pnum >= ubi->peb_count) {
1417 ubi_err("too high PEB number %d, total PEBs %d",
1418 seb->pnum, ubi->peb_count);
1419 goto bad_seb;
1420 }
1421
1422 if (sv->vol_type == UBI_STATIC_VOLUME) {
1423 if (seb->lnum >= sv->used_ebs) {
1424 ubi_err("bad lnum or used_ebs");
1425 goto bad_seb;
1426 }
1427 } else {
1428 if (sv->used_ebs != 0) {
1429 ubi_err("non-zero used_ebs");
1430 goto bad_seb;
1431 }
1432 }
1433
1434 if (seb->lnum > sv->highest_lnum) {
1435 ubi_err("incorrect highest_lnum or lnum");
1436 goto bad_seb;
1437 }
1438 }
1439
1440 if (sv->leb_count != leb_count) {
1441 ubi_err("bad leb_count, %d objects in the tree",
1442 leb_count);
1443 goto bad_sv;
1444 }
1445
1446 if (!last_seb)
1447 continue;
1448
1449 seb = last_seb;
1450
1451 if (seb->lnum != sv->highest_lnum) {
1452 ubi_err("bad highest_lnum");
1453 goto bad_seb;
1454 }
1455 }
1456
1457 if (vols_found != si->vols_found) {
1458 ubi_err("bad si->vols_found %d, should be %d",
1459 si->vols_found, vols_found);
1460 goto out;
1461 }
1462
1463 /* Check that scanning information is correct */
1464 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1465 last_seb = NULL;
1466 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1467 int vol_type;
1468
1469 cond_resched();
1470
1471 last_seb = seb;
1472
1473 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1474 if (err && err != UBI_IO_BITFLIPS) {
1475 ubi_err("VID header is not OK (%d)", err);
1476 if (err > 0)
1477 err = -EIO;
1478 return err;
1479 }
1480
1481 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1482 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1483 if (sv->vol_type != vol_type) {
1484 ubi_err("bad vol_type");
1485 goto bad_vid_hdr;
1486 }
1487
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001488 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001489 ubi_err("bad sqnum %llu", seb->sqnum);
1490 goto bad_vid_hdr;
1491 }
1492
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001493 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001494 ubi_err("bad vol_id %d", sv->vol_id);
1495 goto bad_vid_hdr;
1496 }
1497
1498 if (sv->compat != vidh->compat) {
1499 ubi_err("bad compat %d", vidh->compat);
1500 goto bad_vid_hdr;
1501 }
1502
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001503 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001504 ubi_err("bad lnum %d", seb->lnum);
1505 goto bad_vid_hdr;
1506 }
1507
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001508 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001509 ubi_err("bad used_ebs %d", sv->used_ebs);
1510 goto bad_vid_hdr;
1511 }
1512
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001513 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001514 ubi_err("bad data_pad %d", sv->data_pad);
1515 goto bad_vid_hdr;
1516 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001517 }
1518
1519 if (!last_seb)
1520 continue;
1521
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001522 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001523 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1524 goto bad_vid_hdr;
1525 }
1526
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001527 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001528 ubi_err("bad last_data_size %d", sv->last_data_size);
1529 goto bad_vid_hdr;
1530 }
1531 }
1532
1533 /*
1534 * Make sure that all the physical eraseblocks are in one of the lists
1535 * or trees.
1536 */
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001537 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001538 if (!buf)
1539 return -ENOMEM;
1540
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001541 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1542 err = ubi_io_is_bad(ubi, pnum);
Artem Bityutskiy341e1a02007-05-03 11:59:51 +03001543 if (err < 0) {
1544 kfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001545 return err;
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +03001546 } else if (err)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001547 buf[pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001548 }
1549
1550 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1551 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001552 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001553
1554 list_for_each_entry(seb, &si->free, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001555 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001556
1557 list_for_each_entry(seb, &si->corr, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001558 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001559
1560 list_for_each_entry(seb, &si->erase, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001561 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001562
1563 list_for_each_entry(seb, &si->alien, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001564 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001565
1566 err = 0;
1567 for (pnum = 0; pnum < ubi->peb_count; pnum++)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001568 if (!buf[pnum]) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001569 ubi_err("PEB %d is not referred", pnum);
1570 err = 1;
1571 }
1572
1573 kfree(buf);
1574 if (err)
1575 goto out;
1576 return 0;
1577
1578bad_seb:
1579 ubi_err("bad scanning information about LEB %d", seb->lnum);
Artem Bityutskiyb989bd42012-05-16 18:01:58 +03001580 ubi_dump_seb(seb, 0);
Artem Bityutskiy614c74a752012-05-16 17:59:36 +03001581 ubi_dump_sv(sv);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001582 goto out;
1583
1584bad_sv:
1585 ubi_err("bad scanning information about volume %d", sv->vol_id);
Artem Bityutskiy614c74a752012-05-16 17:59:36 +03001586 ubi_dump_sv(sv);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001587 goto out;
1588
1589bad_vid_hdr:
1590 ubi_err("bad scanning information about volume %d", sv->vol_id);
Artem Bityutskiy614c74a752012-05-16 17:59:36 +03001591 ubi_dump_sv(sv);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +03001592 ubi_dump_vid_hdr(vidh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001593
1594out:
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001595 dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001596 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001597}