blob: 06151fde2cc750715c85ada575a2d2c9858f4955 [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 Bityutskiya4e60422012-05-17 13:09:08 +030027 * The attaching information is represented by a &struct ubi_attach_info'
28 * object. Information about found volumes is represented by
29 * &struct ubi_ainf_volume objects which are kept in volume RB-tree with root
30 * at the @volumes field. The RB-tree is indexed by the volume ID.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040031 *
Artem Bityutskiy227423d2012-05-17 06:23:22 +030032 * Scanned logical eraseblocks are represented by &struct ubi_ainf_peb objects.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040033 * These objects are kept in per-volume RB-trees with the root at the
Artem Bityutskiycb28a932012-05-17 06:59:30 +030034 * corresponding &struct ubi_ainf_volume object. To put it differently, we keep
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040035 * 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 Bityutskiya4e60422012-05-17 13:09:08 +030091static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai);
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.
Artem Bityutskiya4e60422012-05-17 13:09:08 +030099 * @ai: attaching information
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300100 * @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 Bityutskiya4e60422012-05-17 13:09:08 +0300113static int add_to_list(struct ubi_attach_info *ai, int pnum, int ec,
Artem Bityutskiyafc15a82012-05-17 07:46:17 +0300114 int to_head, struct list_head *list)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400115{
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300116 struct ubi_ainf_peb *aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400117
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300118 if (list == &ai->free) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400119 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300120 } else if (list == &ai->erase) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400121 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300122 } else if (list == &ai->alien) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400123 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300124 ai->alien_peb_count += 1;
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300125 } else
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400126 BUG();
127
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300128 aeb = kmem_cache_alloc(ai->scan_leb_slab, GFP_KERNEL);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300129 if (!aeb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400130 return -ENOMEM;
131
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300132 aeb->pnum = pnum;
133 aeb->ec = ec;
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300134 if (to_head)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300135 list_add(&aeb->u.list, list);
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300136 else
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300137 list_add_tail(&aeb->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.
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300143 * @ai: attaching information
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300144 * @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 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300151static int add_corrupted(struct ubi_attach_info *ai, int pnum, int ec)
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300152{
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300153 struct ubi_ainf_peb *aeb;
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300154
155 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
156
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300157 aeb = kmem_cache_alloc(ai->scan_leb_slab, GFP_KERNEL);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300158 if (!aeb)
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300159 return -ENOMEM;
160
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300161 ai->corr_peb_count += 1;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300162 aeb->pnum = pnum;
163 aeb->ec = ec;
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300164 list_add(&aeb->u.list, &ai->corr);
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300165 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
Artem Bityutskiy517af482012-05-17 14:38:34 +0300171 * @av: information about the volume this logical eraseblock belongs to
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400172 * @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,
Artem Bityutskiy517af482012-05-17 14:38:34 +0300183 const struct ubi_ainf_volume *av, int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400184{
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
Artem Bityutskiy517af482012-05-17 14:38:34 +0300190 if (av->leb_count != 0) {
191 int av_vol_type;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400192
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
Artem Bityutskiy517af482012-05-17 14:38:34 +0300199 if (vol_id != av->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
Artem Bityutskiy517af482012-05-17 14:38:34 +0300204 if (av->vol_type == UBI_STATIC_VOLUME)
205 av_vol_type = UBI_VID_STATIC;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400206 else
Artem Bityutskiy517af482012-05-17 14:38:34 +0300207 av_vol_type = UBI_VID_DYNAMIC;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400208
Artem Bityutskiy517af482012-05-17 14:38:34 +0300209 if (vol_type != av_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
Artem Bityutskiy517af482012-05-17 14:38:34 +0300214 if (used_ebs != av->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
Artem Bityutskiy517af482012-05-17 14:38:34 +0300219 if (data_pad != av->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 Bityutskiy517af482012-05-17 14:38:34 +0300230 ubi_dump_av(av);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400231 return -EINVAL;
232}
233
234/**
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300235 * add_volume - add volume to the attaching information.
236 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400237 * @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
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300242 * present in the attaching information, this function does nothing. Otherwise
243 * it adds corresponding volume to the attaching information. Returns a pointer
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400244 * to the scanning volume object in case of success and a negative error code
245 * in case of failure.
246 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300247static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *ai,
Artem Bityutskiyafc15a82012-05-17 07:46:17 +0300248 int vol_id, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400249 const struct ubi_vid_hdr *vid_hdr)
250{
Artem Bityutskiy517af482012-05-17 14:38:34 +0300251 struct ubi_ainf_volume *av;
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300252 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400253
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;
Artem Bityutskiy517af482012-05-17 14:38:34 +0300259 av = rb_entry(parent, struct ubi_ainf_volume, rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400260
Artem Bityutskiy517af482012-05-17 14:38:34 +0300261 if (vol_id == av->vol_id)
262 return av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400263
Artem Bityutskiy517af482012-05-17 14:38:34 +0300264 if (vol_id > av->vol_id)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400265 p = &(*p)->rb_left;
266 else
267 p = &(*p)->rb_right;
268 }
269
270 /* The volume is absent - add it */
Artem Bityutskiy517af482012-05-17 14:38:34 +0300271 av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL);
272 if (!av)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400273 return ERR_PTR(-ENOMEM);
274
Artem Bityutskiy517af482012-05-17 14:38:34 +0300275 av->highest_lnum = av->leb_count = 0;
276 av->vol_id = vol_id;
277 av->root = RB_ROOT;
278 av->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
279 av->data_pad = be32_to_cpu(vid_hdr->data_pad);
280 av->compat = vid_hdr->compat;
281 av->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400282 : UBI_STATIC_VOLUME;
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300283 if (vol_id > ai->highest_vol_id)
284 ai->highest_vol_id = vol_id;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400285
Artem Bityutskiy517af482012-05-17 14:38:34 +0300286 rb_link_node(&av->rb, parent, p);
287 rb_insert_color(&av->rb, &ai->volumes);
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300288 ai->vols_found += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400289 dbg_bld("added volume %d", vol_id);
Artem Bityutskiy517af482012-05-17 14:38:34 +0300290 return av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400291}
292
293/**
294 * compare_lebs - find out which logical eraseblock is newer.
295 * @ubi: UBI device description object
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300296 * @aeb: first logical eraseblock to compare
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400297 * @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:
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300305 * o bit 0 is cleared: the first PEB (described by @aeb) 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 Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300313static int compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300314 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 Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300322 if (sqnum2 == aeb->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 */
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300336 second_is_newer = (sqnum2 > aeb->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 Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300355 if (!aeb->copy_flag) {
Artem Bityutskiyfb22b592010-10-19 22:00:11 +0300356 /* 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 Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300366 pnum = aeb->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 Bityutskiya4e60422012-05-17 13:09:08 +0300428 * ubi_scan_add_used - add physical eraseblock to the attaching information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400429 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300430 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400431 * @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 Bityutskiya4e60422012-05-17 13:09:08 +0300443int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_attach_info *ai,
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;
Artem Bityutskiy517af482012-05-17 14:38:34 +0300449 struct ubi_ainf_volume *av;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300450 struct ubi_ainf_peb *aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400451 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
Artem Bityutskiy517af482012-05-17 14:38:34 +0300460 av = add_volume(ai, vol_id, pnum, vid_hdr);
461 if (IS_ERR(av))
462 return PTR_ERR(av);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400463
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300464 if (ai->max_sqnum < sqnum)
465 ai->max_sqnum = sqnum;
Brijesh Singh76eafe42007-07-06 14:35:43 +0300466
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 */
Artem Bityutskiy517af482012-05-17 14:38:34 +0300471 p = &av->root.rb_node;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400472 while (*p) {
473 int cmp_res;
474
475 parent = *p;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300476 aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
477 if (lnum != aeb->lnum) {
478 if (lnum < aeb->lnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400479 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
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300490 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, EC %d",
491 aeb->pnum, aeb->sqnum, aeb->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 */
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300506 if (aeb->sqnum == sqnum && sqnum != 0) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400507 ubi_err("two LEBs with same sequence number %llu",
508 sqnum);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300509 ubi_dump_aeb(aeb, 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 */
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300518 cmp_res = compare_lebs(ubi, aeb, pnum, vid_hdr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400519 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 */
Artem Bityutskiy517af482012-05-17 14:38:34 +0300527 err = validate_vid_hdr(vid_hdr, av, pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400528 if (err)
529 return err;
530
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300531 err = add_to_list(ai, aeb->pnum, aeb->ec, cmp_res & 4,
532 &ai->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400533 if (err)
534 return err;
535
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300536 aeb->ec = ec;
537 aeb->pnum = pnum;
538 aeb->scrub = ((cmp_res & 2) || bitflips);
539 aeb->copy_flag = vid_hdr->copy_flag;
540 aeb->sqnum = sqnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400541
Artem Bityutskiy517af482012-05-17 14:38:34 +0300542 if (av->highest_lnum == lnum)
543 av->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 Bityutskiya4e60422012-05-17 13:09:08 +0300552 return add_to_list(ai, pnum, ec, cmp_res & 4,
553 &ai->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
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300559 * attaching information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400560 */
561
Artem Bityutskiy517af482012-05-17 14:38:34 +0300562 err = validate_vid_hdr(vid_hdr, av, pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400563 if (err)
564 return err;
565
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300566 aeb = kmem_cache_alloc(ai->scan_leb_slab, GFP_KERNEL);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300567 if (!aeb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400568 return -ENOMEM;
569
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300570 aeb->ec = ec;
571 aeb->pnum = pnum;
572 aeb->lnum = lnum;
573 aeb->scrub = bitflips;
574 aeb->copy_flag = vid_hdr->copy_flag;
575 aeb->sqnum = sqnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400576
Artem Bityutskiy517af482012-05-17 14:38:34 +0300577 if (av->highest_lnum <= lnum) {
578 av->highest_lnum = lnum;
579 av->last_data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400580 }
581
Artem Bityutskiy517af482012-05-17 14:38:34 +0300582 av->leb_count += 1;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300583 rb_link_node(&aeb->u.rb, parent, p);
Artem Bityutskiy517af482012-05-17 14:38:34 +0300584 rb_insert_color(&aeb->u.rb, &av->root);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400585 return 0;
586}
587
588/**
Artem Bityutskiy517af482012-05-17 14:38:34 +0300589 * ubi_scan_find_av - find volume in the attaching information.
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300590 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400591 * @vol_id: the requested volume ID
592 *
593 * This function returns a pointer to the volume description or %NULL if there
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300594 * are no data about this volume in the attaching information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400595 */
Artem Bityutskiy517af482012-05-17 14:38:34 +0300596struct ubi_ainf_volume *ubi_scan_find_av(const struct ubi_attach_info *ai,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400597 int vol_id)
598{
Artem Bityutskiy517af482012-05-17 14:38:34 +0300599 struct ubi_ainf_volume *av;
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300600 struct rb_node *p = ai->volumes.rb_node;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400601
602 while (p) {
Artem Bityutskiy517af482012-05-17 14:38:34 +0300603 av = rb_entry(p, struct ubi_ainf_volume, rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400604
Artem Bityutskiy517af482012-05-17 14:38:34 +0300605 if (vol_id == av->vol_id)
606 return av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400607
Artem Bityutskiy517af482012-05-17 14:38:34 +0300608 if (vol_id > av->vol_id)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400609 p = p->rb_left;
610 else
611 p = p->rb_right;
612 }
613
614 return NULL;
615}
616
617/**
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300618 * ubi_scan_rm_volume - delete attaching information about a volume.
619 * @ai: attaching information
Artem Bityutskiy517af482012-05-17 14:38:34 +0300620 * @av: the volume attaching information to delete
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400621 */
Artem Bityutskiy517af482012-05-17 14:38:34 +0300622void ubi_scan_rm_volume(struct ubi_attach_info *ai, struct ubi_ainf_volume *av)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400623{
624 struct rb_node *rb;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300625 struct ubi_ainf_peb *aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400626
Artem Bityutskiy517af482012-05-17 14:38:34 +0300627 dbg_bld("remove attaching information about volume %d", av->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400628
Artem Bityutskiy517af482012-05-17 14:38:34 +0300629 while ((rb = rb_first(&av->root))) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300630 aeb = rb_entry(rb, struct ubi_ainf_peb, u.rb);
Artem Bityutskiy517af482012-05-17 14:38:34 +0300631 rb_erase(&aeb->u.rb, &av->root);
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300632 list_add_tail(&aeb->u.list, &ai->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400633 }
634
Artem Bityutskiy517af482012-05-17 14:38:34 +0300635 rb_erase(&av->rb, &ai->volumes);
636 kfree(av);
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300637 ai->vols_found -= 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400638}
639
640/**
Artem Bityutskiy13d33da2012-05-17 15:20:28 +0300641 * early_erase_peb - erase a physical eraseblock.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400642 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300643 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400644 * @pnum: physical eraseblock number to erase;
645 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
646 *
647 * This function erases physical eraseblock 'pnum', and writes the erase
648 * counter header to it. This function should only be used on UBI device
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300649 * initialization stages, when the EBA sub-system had not been yet initialized.
650 * This function returns zero in case of success and a negative error code in
651 * case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400652 */
Artem Bityutskiy13d33da2012-05-17 15:20:28 +0300653static int early_erase_peb(struct ubi_device *ubi,
654 const struct ubi_attach_info *ai, int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400655{
656 int err;
657 struct ubi_ec_hdr *ec_hdr;
658
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400659 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
660 /*
661 * Erase counter overflow. Upgrade UBI and use 64-bit
662 * erase counters internally.
663 */
664 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
665 return -EINVAL;
666 }
667
Florin Malitadcec4c32007-07-19 15:22:41 -0400668 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
669 if (!ec_hdr)
670 return -ENOMEM;
671
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300672 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400673
674 err = ubi_io_sync_erase(ubi, pnum, 0);
675 if (err < 0)
676 goto out_free;
677
678 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
679
680out_free:
681 kfree(ec_hdr);
682 return err;
683}
684
685/**
686 * ubi_scan_get_free_peb - get a free physical eraseblock.
687 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300688 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400689 *
690 * This function returns a free physical eraseblock. It is supposed to be
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300691 * called on the UBI initialization stages when the wear-leveling sub-system is
692 * not initialized yet. This function picks a physical eraseblocks from one of
693 * the lists, writes the EC header if it is needed, and removes it from the
694 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400695 *
696 * This function returns scanning physical eraseblock information in case of
697 * success and an error code in case of failure.
698 */
Artem Bityutskiy227423d2012-05-17 06:23:22 +0300699struct ubi_ainf_peb *ubi_scan_get_free_peb(struct ubi_device *ubi,
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300700 struct ubi_attach_info *ai)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400701{
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300702 int err = 0;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300703 struct ubi_ainf_peb *aeb, *tmp_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400704
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300705 if (!list_empty(&ai->free)) {
706 aeb = list_entry(ai->free.next, struct ubi_ainf_peb, u.list);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300707 list_del(&aeb->u.list);
708 dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec);
709 return aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400710 }
711
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300712 /*
713 * We try to erase the first physical eraseblock from the erase list
714 * and pick it if we succeed, or try to erase the next one if not. And
715 * so forth. We don't want to take care about bad eraseblocks here -
716 * they'll be handled later.
717 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300718 list_for_each_entry_safe(aeb, tmp_aeb, &ai->erase, u.list) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300719 if (aeb->ec == UBI_SCAN_UNKNOWN_EC)
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300720 aeb->ec = ai->mean_ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400721
Artem Bityutskiy13d33da2012-05-17 15:20:28 +0300722 err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1);
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300723 if (err)
724 continue;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400725
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +0300726 aeb->ec += 1;
727 list_del(&aeb->u.list);
728 dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec);
729 return aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400730 }
731
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +0300732 ubi_err("no free eraseblocks");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400733 return ERR_PTR(-ENOSPC);
734}
735
736/**
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300737 * check_corruption - check the data area of PEB.
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300738 * @ubi: UBI device description object
739 * @vid_hrd: the (corrupted) VID header of this PEB
740 * @pnum: the physical eraseblock number to check
741 *
742 * This is a helper function which is used to distinguish between VID header
743 * corruptions caused by power cuts and other reasons. If the PEB contains only
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300744 * 0xFF bytes in the data area, the VID header is most probably corrupted
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300745 * because of a power cut (%0 is returned in this case). Otherwise, it was
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300746 * probably corrupted for some other reasons (%1 is returned in this case). A
747 * negative error code is returned if a read error occurred.
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300748 *
749 * If the corruption reason was a power cut, UBI can safely erase this PEB.
750 * Otherwise, it should preserve it to avoid possibly destroying important
751 * information.
752 */
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300753static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
754 int pnum)
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300755{
756 int err;
757
758 mutex_lock(&ubi->buf_mutex);
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200759 memset(ubi->peb_buf, 0x00, ubi->leb_size);
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300760
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200761 err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start,
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300762 ubi->leb_size);
Brian Norrisd57f40542011-09-20 18:34:25 -0700763 if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) {
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300764 /*
765 * Bit-flips or integrity errors while reading the data area.
766 * It is difficult to say for sure what type of corruption is
767 * this, but presumably a power cut happened while this PEB was
768 * erased, so it became unstable and corrupted, and should be
769 * erased.
770 */
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300771 err = 0;
772 goto out_unlock;
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300773 }
774
775 if (err)
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300776 goto out_unlock;
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300777
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200778 if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size))
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300779 goto out_unlock;
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300780
781 ubi_err("PEB %d contains corrupted VID header, and the data does not "
782 "contain all 0xFF, this may be a non-UBI PEB or a severe VID "
783 "header corruption which requires manual inspection", pnum);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300784 ubi_dump_vid_hdr(vid_hdr);
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300785 dbg_msg("hexdump of PEB %d offset %d, length %d",
786 pnum, ubi->leb_start, ubi->leb_size);
787 ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
Artem Bityutskiy0ca39d72012-03-08 15:29:37 +0200788 ubi->peb_buf, ubi->leb_size, 1);
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300789 err = 1;
790
791out_unlock:
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300792 mutex_unlock(&ubi->buf_mutex);
Dan Carpenter1b1d76e2010-11-18 06:58:04 +0300793 return err;
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300794}
795
796/**
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300797 * process_eb - read, check UBI headers, and add them to attaching information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400798 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300799 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400800 * @pnum: the physical eraseblock number
801 *
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300802 * This function returns a zero if the physical eraseblock was successfully
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400803 * handled and a negative error code in case of failure.
804 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300805static int process_eb(struct ubi_device *ubi, struct ubi_attach_info *ai,
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300806 int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400807{
Artem Bityutskiyc18a8412008-01-24 11:19:14 +0200808 long long uninitialized_var(ec);
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300809 int err, bitflips = 0, vol_id, ec_err = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400810
811 dbg_bld("scan PEB %d", pnum);
812
813 /* Skip bad physical eraseblocks */
814 err = ubi_io_is_bad(ubi, pnum);
815 if (err < 0)
816 return err;
817 else if (err) {
818 /*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300819 * FIXME: this is actually duty of the I/O sub-system to
820 * initialize this, but MTD does not provide enough
821 * information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400822 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300823 ai->bad_peb_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400824 return 0;
825 }
826
827 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
828 if (err < 0)
829 return err;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300830 switch (err) {
831 case 0:
832 break;
833 case UBI_IO_BITFLIPS:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400834 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300835 break;
836 case UBI_IO_FF:
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300837 ai->empty_peb_count += 1;
838 return add_to_list(ai, pnum, UBI_SCAN_UNKNOWN_EC, 0,
839 &ai->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300840 case UBI_IO_FF_BITFLIPS:
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300841 ai->empty_peb_count += 1;
842 return add_to_list(ai, pnum, UBI_SCAN_UNKNOWN_EC, 1,
843 &ai->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300844 case UBI_IO_BAD_HDR_EBADMSG:
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300845 case UBI_IO_BAD_HDR:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400846 /*
847 * We have to also look at the VID header, possibly it is not
848 * corrupted. Set %bitflips flag in order to make this PEB be
849 * moved and EC be re-created.
850 */
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300851 ec_err = err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400852 ec = UBI_SCAN_UNKNOWN_EC;
853 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300854 break;
855 default:
856 ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
857 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400858 }
859
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300860 if (!ec_err) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300861 int image_seq;
862
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400863 /* Make sure UBI version is OK */
864 if (ech->version != UBI_VERSION) {
865 ubi_err("this UBI version is %d, image version is %d",
866 UBI_VERSION, (int)ech->version);
867 return -EINVAL;
868 }
869
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300870 ec = be64_to_cpu(ech->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400871 if (ec > UBI_MAX_ERASECOUNTER) {
872 /*
873 * Erase counter overflow. The EC headers have 64 bits
874 * reserved, but we anyway make use of only 31 bit
875 * values, as this seems to be enough for any existing
876 * flash. Upgrade UBI and use 64-bit erase counters
877 * internally.
878 */
879 ubi_err("erase counter overflow, max is %d",
880 UBI_MAX_ERASECOUNTER);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300881 ubi_dump_ec_hdr(ech);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400882 return -EINVAL;
883 }
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300884
Adrian Hunter32bc4822009-07-24 19:16:04 +0300885 /*
886 * Make sure that all PEBs have the same image sequence number.
887 * This allows us to detect situations when users flash UBI
888 * images incorrectly, so that the flash has the new UBI image
889 * and leftovers from the old one. This feature was added
890 * relatively recently, and the sequence number was always
891 * zero, because old UBI implementations always set it to zero.
892 * For this reasons, we do not panic if some PEBs have zero
893 * sequence number, while other PEBs have non-zero sequence
894 * number.
895 */
Holger Brunck3dc948d2009-07-13 16:47:57 +0200896 image_seq = be32_to_cpu(ech->image_seq);
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300897 if (!ubi->image_seq && image_seq)
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300898 ubi->image_seq = image_seq;
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300899 if (ubi->image_seq && image_seq &&
900 ubi->image_seq != image_seq) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300901 ubi_err("bad image sequence number %d in PEB %d, "
902 "expected %d", image_seq, pnum, ubi->image_seq);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +0300903 ubi_dump_ec_hdr(ech);
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300904 return -EINVAL;
905 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400906 }
907
908 /* OK, we've done with the EC header, let's look at the VID header */
909
910 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
911 if (err < 0)
912 return err;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300913 switch (err) {
914 case 0:
915 break;
916 case UBI_IO_BITFLIPS:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400917 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300918 break;
919 case UBI_IO_BAD_HDR_EBADMSG:
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300920 if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
921 /*
922 * Both EC and VID headers are corrupted and were read
923 * with data integrity error, probably this is a bad
924 * PEB, bit it is not marked as bad yet. This may also
925 * be a result of power cut during erasure.
926 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300927 ai->maybe_bad_peb_count += 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300928 case UBI_IO_BAD_HDR:
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300929 if (ec_err)
930 /*
931 * Both headers are corrupted. There is a possibility
932 * that this a valid UBI PEB which has corresponding
933 * LEB, but the headers are corrupted. However, it is
934 * impossible to distinguish it from a PEB which just
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300935 * contains garbage because of a power cut during erase
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300936 * operation. So we just schedule this PEB for erasure.
Artem Bityutskiy7ac760c2010-12-02 06:34:01 +0200937 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300938 * Besides, in case of NOR flash, we deliberately
Artem Bityutskiy7ac760c2010-12-02 06:34:01 +0200939 * corrupt both headers because NOR flash erasure is
940 * slow and can start from the end.
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300941 */
942 err = 0;
943 else
944 /*
945 * The EC was OK, but the VID header is corrupted. We
946 * have to check what is in the data area.
947 */
Artem Bityutskiy45aafd32010-10-20 11:54:58 +0300948 err = check_corruption(ubi, vidh, pnum);
Artem Bityutskiydf3fca42010-10-20 11:51:21 +0300949
950 if (err < 0)
951 return err;
952 else if (!err)
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300953 /* This corruption is caused by a power cut */
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300954 err = add_to_list(ai, pnum, ec, 1, &ai->erase);
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300955 else
956 /* This is an unexpected corruption */
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300957 err = add_corrupted(ai, pnum, ec);
Artem Bityutskiyfeeba4b2010-09-03 22:50:53 +0300958 if (err)
959 return err;
960 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300961 case UBI_IO_FF_BITFLIPS:
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300962 err = add_to_list(ai, pnum, ec, 1, &ai->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400963 if (err)
964 return err;
965 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300966 case UBI_IO_FF:
967 if (ec_err)
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300968 err = add_to_list(ai, pnum, ec, 1, &ai->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300969 else
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300970 err = add_to_list(ai, pnum, ec, 0, &ai->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400971 if (err)
972 return err;
973 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300974 default:
975 ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
976 err);
977 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400978 }
979
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300980 vol_id = be32_to_cpu(vidh->vol_id);
Artem Bityutskiy91f2d532008-01-24 11:23:23 +0200981 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300982 int lnum = be32_to_cpu(vidh->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400983
984 /* Unsupported internal volume */
985 switch (vidh->compat) {
986 case UBI_COMPAT_DELETE:
987 ubi_msg("\"delete\" compatible internal volume %d:%d"
Brijesh Singh158132c2010-06-16 09:28:26 +0300988 " found, will remove it", vol_id, lnum);
Artem Bityutskiya4e60422012-05-17 13:09:08 +0300989 err = add_to_list(ai, pnum, ec, 1, &ai->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400990 if (err)
991 return err;
Brijesh Singh158132c2010-06-16 09:28:26 +0300992 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400993
994 case UBI_COMPAT_RO:
995 ubi_msg("read-only compatible internal volume %d:%d"
996 " found, switch to read-only mode",
997 vol_id, lnum);
998 ubi->ro_mode = 1;
999 break;
1000
1001 case UBI_COMPAT_PRESERVE:
1002 ubi_msg("\"preserve\" compatible internal volume %d:%d"
1003 " found", vol_id, lnum);
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001004 err = add_to_list(ai, pnum, ec, 0, &ai->alien);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001005 if (err)
1006 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001007 return 0;
1008
1009 case UBI_COMPAT_REJECT:
1010 ubi_err("incompatible internal volume %d:%d found",
1011 vol_id, lnum);
1012 return -EINVAL;
1013 }
1014 }
1015
Artem Bityutskiye0e718c2010-09-03 14:53:23 +03001016 if (ec_err)
Artem Bityutskiy29a88c92009-07-19 14:03:16 +03001017 ubi_warn("valid VID header but corrupted EC header at PEB %d",
1018 pnum);
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001019 err = ubi_scan_add_used(ubi, ai, pnum, ec, vidh, bitflips);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001020 if (err)
1021 return err;
1022
1023adjust_mean_ec:
Artem Bityutskiye0e718c2010-09-03 14:53:23 +03001024 if (!ec_err) {
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001025 ai->ec_sum += ec;
1026 ai->ec_count += 1;
1027 if (ec > ai->max_ec)
1028 ai->max_ec = ec;
1029 if (ec < ai->min_ec)
1030 ai->min_ec = ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001031 }
1032
1033 return 0;
1034}
1035
1036/**
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001037 * check_what_we_have - check what PEB were found by scanning.
1038 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001039 * @ai: attaching information
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001040 *
1041 * This is a helper function which takes a look what PEBs were found by
1042 * scanning, and decides whether the flash is empty and should be formatted and
1043 * whether there are too many corrupted PEBs and we should not attach this
1044 * MTD device. Returns zero if we should proceed with attaching the MTD device,
1045 * and %-EINVAL if we should not.
1046 */
Artem Bityutskiyafc15a82012-05-17 07:46:17 +03001047static int check_what_we_have(struct ubi_device *ubi,
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001048 struct ubi_attach_info *ai)
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001049{
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001050 struct ubi_ainf_peb *aeb;
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001051 int max_corr, peb_count;
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001052
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001053 peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count;
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001054 max_corr = peb_count / 20 ?: 8;
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001055
1056 /*
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001057 * Few corrupted PEBs is not a problem and may be just a result of
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001058 * unclean reboots. However, many of them may indicate some problems
1059 * with the flash HW or driver.
1060 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001061 if (ai->corr_peb_count) {
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001062 ubi_err("%d PEBs are corrupted and preserved",
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001063 ai->corr_peb_count);
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001064 printk(KERN_ERR "Corrupted PEBs are:");
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001065 list_for_each_entry(aeb, &ai->corr, u.list)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001066 printk(KERN_CONT " %d", aeb->pnum);
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001067 printk(KERN_CONT "\n");
1068
1069 /*
1070 * If too many PEBs are corrupted, we refuse attaching,
1071 * otherwise, only print a warning.
1072 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001073 if (ai->corr_peb_count >= max_corr) {
Artem Bityutskiyfeddbb32011-03-28 10:12:25 +03001074 ubi_err("too many corrupted PEBs, refusing");
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001075 return -EINVAL;
1076 }
1077 }
1078
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001079 if (ai->empty_peb_count + ai->maybe_bad_peb_count == peb_count) {
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001080 /*
1081 * All PEBs are empty, or almost all - a couple PEBs look like
1082 * they may be bad PEBs which were not marked as bad yet.
1083 *
1084 * This piece of code basically tries to distinguish between
1085 * the following situations:
1086 *
1087 * 1. Flash is empty, but there are few bad PEBs, which are not
1088 * marked as bad so far, and which were read with error. We
1089 * want to go ahead and format this flash. While formatting,
1090 * the faulty PEBs will probably be marked as bad.
1091 *
1092 * 2. Flash contains non-UBI data and we do not want to format
1093 * it and destroy possibly important information.
1094 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001095 if (ai->maybe_bad_peb_count <= 2) {
1096 ai->is_empty = 1;
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001097 ubi_msg("empty MTD device detected");
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001098 get_random_bytes(&ubi->image_seq,
1099 sizeof(ubi->image_seq));
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001100 } else {
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001101 ubi_err("MTD device is not UBI-formatted and possibly "
1102 "contains non-UBI data - refusing it");
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001103 return -EINVAL;
1104 }
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001105
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001106 }
1107
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001108 return 0;
1109}
1110
1111/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001112 * ubi_scan - scan an MTD device.
1113 * @ubi: UBI device description object
1114 *
1115 * This function does full scanning of an MTD device and returns complete
1116 * information about it. In case of failure, an error code is returned.
1117 */
Artem Bityutskiyafc15a82012-05-17 07:46:17 +03001118struct ubi_attach_info *ubi_scan(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001119{
1120 int err, pnum;
1121 struct rb_node *rb1, *rb2;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001122 struct ubi_ainf_volume *av;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001123 struct ubi_ainf_peb *aeb;
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001124 struct ubi_attach_info *ai;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001125
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001126 ai = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL);
1127 if (!ai)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001128 return ERR_PTR(-ENOMEM);
1129
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001130 INIT_LIST_HEAD(&ai->corr);
1131 INIT_LIST_HEAD(&ai->free);
1132 INIT_LIST_HEAD(&ai->erase);
1133 INIT_LIST_HEAD(&ai->alien);
1134 ai->volumes = RB_ROOT;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001135
1136 err = -ENOMEM;
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001137 ai->scan_leb_slab = kmem_cache_create("ubi_scan_leb_slab",
Artem Bityutskiy227423d2012-05-17 06:23:22 +03001138 sizeof(struct ubi_ainf_peb),
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +02001139 0, 0, NULL);
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001140 if (!ai->scan_leb_slab)
1141 goto out_ai;
Artem Bityutskiy6c1e8752010-10-31 17:54:14 +02001142
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001143 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1144 if (!ech)
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001145 goto out_ai;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001146
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001147 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001148 if (!vidh)
1149 goto out_ech;
1150
1151 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1152 cond_resched();
1153
Artem Bityutskiyc8566352008-07-16 17:40:22 +03001154 dbg_gen("process PEB %d", pnum);
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001155 err = process_eb(ubi, ai, pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001156 if (err < 0)
1157 goto out_vidh;
1158 }
1159
1160 dbg_msg("scanning is finished");
1161
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +03001162 /* Calculate mean erase counter */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001163 if (ai->ec_count)
1164 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001165
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001166 err = check_what_we_have(ubi, ai);
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001167 if (err)
1168 goto out_vidh;
Artem Bityutskiy4a406852009-07-19 14:33:14 +03001169
1170 /*
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001171 * In case of unknown erase counter we use the mean erase counter
1172 * value.
1173 */
Artem Bityutskiy517af482012-05-17 14:38:34 +03001174 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1175 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001176 if (aeb->ec == UBI_SCAN_UNKNOWN_EC)
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001177 aeb->ec = ai->mean_ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001178 }
1179
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001180 list_for_each_entry(aeb, &ai->free, u.list) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001181 if (aeb->ec == UBI_SCAN_UNKNOWN_EC)
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001182 aeb->ec = ai->mean_ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001183 }
1184
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001185 list_for_each_entry(aeb, &ai->corr, u.list)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001186 if (aeb->ec == UBI_SCAN_UNKNOWN_EC)
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001187 aeb->ec = ai->mean_ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001188
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001189 list_for_each_entry(aeb, &ai->erase, u.list)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001190 if (aeb->ec == UBI_SCAN_UNKNOWN_EC)
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001191 aeb->ec = ai->mean_ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001192
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001193 err = self_check_ai(ubi, ai);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001194 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001195 goto out_vidh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001196
1197 ubi_free_vid_hdr(ubi, vidh);
1198 kfree(ech);
1199
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001200 return ai;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001201
1202out_vidh:
1203 ubi_free_vid_hdr(ubi, vidh);
1204out_ech:
1205 kfree(ech);
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001206out_ai:
1207 ubi_scan_destroy_ai(ai);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001208 return ERR_PTR(err);
1209}
1210
1211/**
Artem Bityutskiy517af482012-05-17 14:38:34 +03001212 * destroy_av - free the scanning volume information
1213 * @av: scanning volume information
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001214 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001215 *
Artem Bityutskiy517af482012-05-17 14:38:34 +03001216 * This function destroys the volume RB-tree (@av->root) and the scanning
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001217 * volume information.
1218 */
Artem Bityutskiy517af482012-05-17 14:38:34 +03001219static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001220{
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001221 struct ubi_ainf_peb *aeb;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001222 struct rb_node *this = av->root.rb_node;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001223
1224 while (this) {
1225 if (this->rb_left)
1226 this = this->rb_left;
1227 else if (this->rb_right)
1228 this = this->rb_right;
1229 else {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001230 aeb = rb_entry(this, struct ubi_ainf_peb, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001231 this = rb_parent(this);
1232 if (this) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001233 if (this->rb_left == &aeb->u.rb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001234 this->rb_left = NULL;
1235 else
1236 this->rb_right = NULL;
1237 }
1238
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001239 kmem_cache_free(ai->scan_leb_slab, aeb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001240 }
1241 }
Artem Bityutskiy517af482012-05-17 14:38:34 +03001242 kfree(av);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001243}
1244
1245/**
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001246 * ubi_scan_destroy_ai - destroy attaching information.
1247 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001248 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001249void ubi_scan_destroy_ai(struct ubi_attach_info *ai)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001250{
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001251 struct ubi_ainf_peb *aeb, *aeb_tmp;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001252 struct ubi_ainf_volume *av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001253 struct rb_node *rb;
1254
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001255 list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001256 list_del(&aeb->u.list);
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001257 kmem_cache_free(ai->scan_leb_slab, aeb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001258 }
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001259 list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001260 list_del(&aeb->u.list);
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001261 kmem_cache_free(ai->scan_leb_slab, aeb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001262 }
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001263 list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001264 list_del(&aeb->u.list);
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001265 kmem_cache_free(ai->scan_leb_slab, aeb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001266 }
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001267 list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001268 list_del(&aeb->u.list);
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001269 kmem_cache_free(ai->scan_leb_slab, aeb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001270 }
1271
1272 /* Destroy the volume RB-tree */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001273 rb = ai->volumes.rb_node;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001274 while (rb) {
1275 if (rb->rb_left)
1276 rb = rb->rb_left;
1277 else if (rb->rb_right)
1278 rb = rb->rb_right;
1279 else {
Artem Bityutskiy517af482012-05-17 14:38:34 +03001280 av = rb_entry(rb, struct ubi_ainf_volume, rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001281
1282 rb = rb_parent(rb);
1283 if (rb) {
Artem Bityutskiy517af482012-05-17 14:38:34 +03001284 if (rb->rb_left == &av->rb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001285 rb->rb_left = NULL;
1286 else
1287 rb->rb_right = NULL;
1288 }
1289
Artem Bityutskiy517af482012-05-17 14:38:34 +03001290 destroy_av(ai, av);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001291 }
1292 }
1293
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001294 if (ai->scan_leb_slab)
1295 kmem_cache_destroy(ai->scan_leb_slab);
Richard Weinbergera29852b2012-01-30 18:20:13 +01001296
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001297 kfree(ai);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001298}
1299
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001300/**
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001301 * self_check_ai - check the attaching information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001302 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001303 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001304 *
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001305 * This function returns zero if the attaching information is all right, and a
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001306 * negative error code if not or if an error occurred.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001307 */
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001308static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001309{
1310 int pnum, err, vols_found = 0;
1311 struct rb_node *rb1, *rb2;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001312 struct ubi_ainf_volume *av;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001313 struct ubi_ainf_peb *aeb, *last_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001314 uint8_t *buf;
1315
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001316 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001317 return 0;
1318
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001319 /*
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001320 * At first, check that attaching information is OK.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001321 */
Artem Bityutskiy517af482012-05-17 14:38:34 +03001322 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001323 int leb_count = 0;
1324
1325 cond_resched();
1326
1327 vols_found += 1;
1328
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001329 if (ai->is_empty) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001330 ubi_err("bad is_empty flag");
Artem Bityutskiy517af482012-05-17 14:38:34 +03001331 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001332 }
1333
Artem Bityutskiy517af482012-05-17 14:38:34 +03001334 if (av->vol_id < 0 || av->highest_lnum < 0 ||
1335 av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 ||
1336 av->data_pad < 0 || av->last_data_size < 0) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001337 ubi_err("negative values");
Artem Bityutskiy517af482012-05-17 14:38:34 +03001338 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001339 }
1340
Artem Bityutskiy517af482012-05-17 14:38:34 +03001341 if (av->vol_id >= UBI_MAX_VOLUMES &&
1342 av->vol_id < UBI_INTERNAL_VOL_START) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001343 ubi_err("bad vol_id");
Artem Bityutskiy517af482012-05-17 14:38:34 +03001344 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001345 }
1346
Artem Bityutskiy517af482012-05-17 14:38:34 +03001347 if (av->vol_id > ai->highest_vol_id) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001348 ubi_err("highest_vol_id is %d, but vol_id %d is there",
Artem Bityutskiy517af482012-05-17 14:38:34 +03001349 ai->highest_vol_id, av->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001350 goto out;
1351 }
1352
Artem Bityutskiy517af482012-05-17 14:38:34 +03001353 if (av->vol_type != UBI_DYNAMIC_VOLUME &&
1354 av->vol_type != UBI_STATIC_VOLUME) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001355 ubi_err("bad vol_type");
Artem Bityutskiy517af482012-05-17 14:38:34 +03001356 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001357 }
1358
Artem Bityutskiy517af482012-05-17 14:38:34 +03001359 if (av->data_pad > ubi->leb_size / 2) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001360 ubi_err("bad data_pad");
Artem Bityutskiy517af482012-05-17 14:38:34 +03001361 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001362 }
1363
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001364 last_aeb = NULL;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001365 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001366 cond_resched();
1367
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001368 last_aeb = aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001369 leb_count += 1;
1370
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001371 if (aeb->pnum < 0 || aeb->ec < 0) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001372 ubi_err("negative values");
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001373 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001374 }
1375
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001376 if (aeb->ec < ai->min_ec) {
1377 ubi_err("bad ai->min_ec (%d), %d found",
1378 ai->min_ec, aeb->ec);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001379 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001380 }
1381
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001382 if (aeb->ec > ai->max_ec) {
1383 ubi_err("bad ai->max_ec (%d), %d found",
1384 ai->max_ec, aeb->ec);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001385 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001386 }
1387
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001388 if (aeb->pnum >= ubi->peb_count) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001389 ubi_err("too high PEB number %d, total PEBs %d",
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001390 aeb->pnum, ubi->peb_count);
1391 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001392 }
1393
Artem Bityutskiy517af482012-05-17 14:38:34 +03001394 if (av->vol_type == UBI_STATIC_VOLUME) {
1395 if (aeb->lnum >= av->used_ebs) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001396 ubi_err("bad lnum or used_ebs");
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001397 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001398 }
1399 } else {
Artem Bityutskiy517af482012-05-17 14:38:34 +03001400 if (av->used_ebs != 0) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001401 ubi_err("non-zero used_ebs");
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001402 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001403 }
1404 }
1405
Artem Bityutskiy517af482012-05-17 14:38:34 +03001406 if (aeb->lnum > av->highest_lnum) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001407 ubi_err("incorrect highest_lnum or lnum");
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001408 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001409 }
1410 }
1411
Artem Bityutskiy517af482012-05-17 14:38:34 +03001412 if (av->leb_count != leb_count) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001413 ubi_err("bad leb_count, %d objects in the tree",
1414 leb_count);
Artem Bityutskiy517af482012-05-17 14:38:34 +03001415 goto bad_av;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001416 }
1417
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001418 if (!last_aeb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001419 continue;
1420
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001421 aeb = last_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001422
Artem Bityutskiy517af482012-05-17 14:38:34 +03001423 if (aeb->lnum != av->highest_lnum) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001424 ubi_err("bad highest_lnum");
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001425 goto bad_aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001426 }
1427 }
1428
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001429 if (vols_found != ai->vols_found) {
1430 ubi_err("bad ai->vols_found %d, should be %d",
1431 ai->vols_found, vols_found);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001432 goto out;
1433 }
1434
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001435 /* Check that attaching information is correct */
Artem Bityutskiy517af482012-05-17 14:38:34 +03001436 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001437 last_aeb = NULL;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001438 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001439 int vol_type;
1440
1441 cond_resched();
1442
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001443 last_aeb = aeb;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001444
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001445 err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidh, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001446 if (err && err != UBI_IO_BITFLIPS) {
1447 ubi_err("VID header is not OK (%d)", err);
1448 if (err > 0)
1449 err = -EIO;
1450 return err;
1451 }
1452
1453 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1454 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001455 if (av->vol_type != vol_type) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001456 ubi_err("bad vol_type");
1457 goto bad_vid_hdr;
1458 }
1459
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001460 if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) {
1461 ubi_err("bad sqnum %llu", aeb->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001462 goto bad_vid_hdr;
1463 }
1464
Artem Bityutskiy517af482012-05-17 14:38:34 +03001465 if (av->vol_id != be32_to_cpu(vidh->vol_id)) {
1466 ubi_err("bad vol_id %d", av->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001467 goto bad_vid_hdr;
1468 }
1469
Artem Bityutskiy517af482012-05-17 14:38:34 +03001470 if (av->compat != vidh->compat) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001471 ubi_err("bad compat %d", vidh->compat);
1472 goto bad_vid_hdr;
1473 }
1474
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001475 if (aeb->lnum != be32_to_cpu(vidh->lnum)) {
1476 ubi_err("bad lnum %d", aeb->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001477 goto bad_vid_hdr;
1478 }
1479
Artem Bityutskiy517af482012-05-17 14:38:34 +03001480 if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) {
1481 ubi_err("bad used_ebs %d", av->used_ebs);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001482 goto bad_vid_hdr;
1483 }
1484
Artem Bityutskiy517af482012-05-17 14:38:34 +03001485 if (av->data_pad != be32_to_cpu(vidh->data_pad)) {
1486 ubi_err("bad data_pad %d", av->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001487 goto bad_vid_hdr;
1488 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001489 }
1490
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001491 if (!last_aeb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001492 continue;
1493
Artem Bityutskiy517af482012-05-17 14:38:34 +03001494 if (av->highest_lnum != be32_to_cpu(vidh->lnum)) {
1495 ubi_err("bad highest_lnum %d", av->highest_lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001496 goto bad_vid_hdr;
1497 }
1498
Artem Bityutskiy517af482012-05-17 14:38:34 +03001499 if (av->last_data_size != be32_to_cpu(vidh->data_size)) {
1500 ubi_err("bad last_data_size %d", av->last_data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001501 goto bad_vid_hdr;
1502 }
1503 }
1504
1505 /*
1506 * Make sure that all the physical eraseblocks are in one of the lists
1507 * or trees.
1508 */
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001509 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001510 if (!buf)
1511 return -ENOMEM;
1512
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001513 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1514 err = ubi_io_is_bad(ubi, pnum);
Artem Bityutskiy341e1a02007-05-03 11:59:51 +03001515 if (err < 0) {
1516 kfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001517 return err;
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +03001518 } else if (err)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001519 buf[pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001520 }
1521
Artem Bityutskiy517af482012-05-17 14:38:34 +03001522 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
1523 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001524 buf[aeb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001525
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001526 list_for_each_entry(aeb, &ai->free, u.list)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001527 buf[aeb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001528
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001529 list_for_each_entry(aeb, &ai->corr, u.list)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001530 buf[aeb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001531
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001532 list_for_each_entry(aeb, &ai->erase, u.list)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001533 buf[aeb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001534
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001535 list_for_each_entry(aeb, &ai->alien, u.list)
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001536 buf[aeb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001537
1538 err = 0;
1539 for (pnum = 0; pnum < ubi->peb_count; pnum++)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001540 if (!buf[pnum]) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001541 ubi_err("PEB %d is not referred", pnum);
1542 err = 1;
1543 }
1544
1545 kfree(buf);
1546 if (err)
1547 goto out;
1548 return 0;
1549
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001550bad_aeb:
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001551 ubi_err("bad attaching information about LEB %d", aeb->lnum);
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001552 ubi_dump_aeb(aeb, 0);
Artem Bityutskiy517af482012-05-17 14:38:34 +03001553 ubi_dump_av(av);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001554 goto out;
1555
Artem Bityutskiy517af482012-05-17 14:38:34 +03001556bad_av:
1557 ubi_err("bad attaching information about volume %d", av->vol_id);
1558 ubi_dump_av(av);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001559 goto out;
1560
1561bad_vid_hdr:
Artem Bityutskiy517af482012-05-17 14:38:34 +03001562 ubi_err("bad attaching information about volume %d", av->vol_id);
1563 ubi_dump_av(av);
Artem Bityutskiya904e3f2012-04-25 09:02:44 +03001564 ubi_dump_vid_hdr(vidh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001565
1566out:
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001567 dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001568 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001569}