blob: fba3dc6a97e9de056cb85817e9b3b32e40798fd3 [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 *
32 * Found logical eraseblocks are represented by &struct ubi_scan_leb objects.
33 * 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.
41 */
42
43#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040045#include <linux/crc32.h>
Artem Bityutskiy3013ee32009-01-16 19:08:43 +020046#include <linux/math64.h>
Matthieu CASTET095751a2010-06-03 16:14:27 +020047#include <linux/random.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040048#include "ubi.h"
49
50#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
Artem Bityutskiye88d6e102007-08-29 14:51:52 +030051static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040052#else
53#define paranoid_check_si(ubi, si) 0
54#endif
55
56/* Temporary variables used during scanning */
57static struct ubi_ec_hdr *ech;
58static struct ubi_vid_hdr *vidh;
59
Artem Bityutskiy941dfb02007-05-05 16:33:13 +030060/**
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030061 * add_to_list - add physical eraseblock to a list.
62 * @si: scanning information
63 * @pnum: physical eraseblock number to add
64 * @ec: erase counter of the physical eraseblock
65 * @list: the list to add to
66 *
Artem Bityutskiy3fb34122010-09-03 15:36:12 +030067 * This function adds physical eraseblock @pnum to free, erase, or alien lists.
68 * Returns zero in case of success and a negative error code in case of
69 * failure.
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030070 */
71static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
72 struct list_head *list)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040073{
74 struct ubi_scan_leb *seb;
75
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030076 if (list == &si->free) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040077 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030078 si->free_peb_count += 1;
79 } else if (list == &si->erase) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040080 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030081 si->erase_peb_count += 1;
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030082 } else if (list == &si->alien) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040083 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030084 si->alien_peb_count += 1;
85 } else
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040086 BUG();
87
88 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
89 if (!seb)
90 return -ENOMEM;
91
92 seb->pnum = pnum;
93 seb->ec = ec;
94 list_add_tail(&seb->u.list, list);
95 return 0;
96}
97
98/**
Artem Bityutskiy3fb34122010-09-03 15:36:12 +030099 * add_corrupted - add a corrupted physical eraseblock.
100 * @si: scanning information
101 * @pnum: physical eraseblock number to add
102 * @ec: erase counter of the physical eraseblock
103 *
104 * This function adds corrupted physical eraseblock @pnum to the 'corr' list.
105 * Returns zero in case of success and a negative error code in case of
106 * failure.
107 */
108static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)
109{
110 struct ubi_scan_leb *seb;
111
112 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
113
114 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
115 if (!seb)
116 return -ENOMEM;
117
118 si->corr_peb_count += 1;
119 seb->pnum = pnum;
120 seb->ec = ec;
121 list_add(&seb->u.list, &si->corr);
122 return 0;
123}
124
125/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300126 * validate_vid_hdr - check volume identifier header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400127 * @vid_hdr: the volume identifier header to check
128 * @sv: information about the volume this logical eraseblock belongs to
129 * @pnum: physical eraseblock number the VID header came from
130 *
131 * This function checks that data stored in @vid_hdr is consistent. Returns
132 * non-zero if an inconsistency was found and zero if not.
133 *
134 * Note, UBI does sanity check of everything it reads from the flash media.
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300135 * 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 +0400136 * information in the VID header is consistent to the information in other VID
137 * headers of the same volume.
138 */
139static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
140 const struct ubi_scan_volume *sv, int pnum)
141{
142 int vol_type = vid_hdr->vol_type;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300143 int vol_id = be32_to_cpu(vid_hdr->vol_id);
144 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
145 int data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400146
147 if (sv->leb_count != 0) {
148 int sv_vol_type;
149
150 /*
151 * This is not the first logical eraseblock belonging to this
152 * volume. Ensure that the data in its VID header is consistent
153 * to the data in previous logical eraseblock headers.
154 */
155
156 if (vol_id != sv->vol_id) {
157 dbg_err("inconsistent vol_id");
158 goto bad;
159 }
160
161 if (sv->vol_type == UBI_STATIC_VOLUME)
162 sv_vol_type = UBI_VID_STATIC;
163 else
164 sv_vol_type = UBI_VID_DYNAMIC;
165
166 if (vol_type != sv_vol_type) {
167 dbg_err("inconsistent vol_type");
168 goto bad;
169 }
170
171 if (used_ebs != sv->used_ebs) {
172 dbg_err("inconsistent used_ebs");
173 goto bad;
174 }
175
176 if (data_pad != sv->data_pad) {
177 dbg_err("inconsistent data_pad");
178 goto bad;
179 }
180 }
181
182 return 0;
183
184bad:
185 ubi_err("inconsistent VID header at PEB %d", pnum);
186 ubi_dbg_dump_vid_hdr(vid_hdr);
187 ubi_dbg_dump_sv(sv);
188 return -EINVAL;
189}
190
191/**
192 * add_volume - add volume to the scanning information.
193 * @si: scanning information
194 * @vol_id: ID of the volume to add
195 * @pnum: physical eraseblock number
196 * @vid_hdr: volume identifier header
197 *
198 * If the volume corresponding to the @vid_hdr logical eraseblock is already
199 * present in the scanning information, this function does nothing. Otherwise
200 * it adds corresponding volume to the scanning information. Returns a pointer
201 * to the scanning volume object in case of success and a negative error code
202 * in case of failure.
203 */
204static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
205 int pnum,
206 const struct ubi_vid_hdr *vid_hdr)
207{
208 struct ubi_scan_volume *sv;
209 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
210
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300211 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400212
213 /* Walk the volume RB-tree to look if this volume is already present */
214 while (*p) {
215 parent = *p;
216 sv = rb_entry(parent, struct ubi_scan_volume, rb);
217
218 if (vol_id == sv->vol_id)
219 return sv;
220
221 if (vol_id > sv->vol_id)
222 p = &(*p)->rb_left;
223 else
224 p = &(*p)->rb_right;
225 }
226
227 /* The volume is absent - add it */
228 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
229 if (!sv)
230 return ERR_PTR(-ENOMEM);
231
232 sv->highest_lnum = sv->leb_count = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400233 sv->vol_id = vol_id;
234 sv->root = RB_ROOT;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300235 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
236 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400237 sv->compat = vid_hdr->compat;
238 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
239 : UBI_STATIC_VOLUME;
240 if (vol_id > si->highest_vol_id)
241 si->highest_vol_id = vol_id;
242
243 rb_link_node(&sv->rb, parent, p);
244 rb_insert_color(&sv->rb, &si->volumes);
245 si->vols_found += 1;
246 dbg_bld("added volume %d", vol_id);
247 return sv;
248}
249
250/**
251 * compare_lebs - find out which logical eraseblock is newer.
252 * @ubi: UBI device description object
253 * @seb: first logical eraseblock to compare
254 * @pnum: physical eraseblock number of the second logical eraseblock to
255 * compare
256 * @vid_hdr: volume identifier header of the second logical eraseblock
257 *
258 * This function compares 2 copies of a LEB and informs which one is newer. In
259 * case of success this function returns a positive value, in case of failure, a
260 * negative error code is returned. The success return codes use the following
261 * bits:
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900262 * o bit 0 is cleared: the first PEB (described by @seb) is newer than the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400263 * second PEB (described by @pnum and @vid_hdr);
264 * o bit 0 is set: the second PEB is newer;
265 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
266 * o bit 1 is set: bit-flips were detected in the newer LEB;
267 * o bit 2 is cleared: the older LEB is not corrupted;
268 * o bit 2 is set: the older LEB is corrupted.
269 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300270static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
271 int pnum, const struct ubi_vid_hdr *vid_hdr)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400272{
273 void *buf;
274 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
275 uint32_t data_crc, crc;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300276 struct ubi_vid_hdr *vh = NULL;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300277 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400278
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300279 if (sqnum2 == seb->sqnum) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400280 /*
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300281 * This must be a really ancient UBI image which has been
282 * created before sequence numbers support has been added. At
283 * that times we used 32-bit LEB versions stored in logical
284 * eraseblocks. That was before UBI got into mainline. We do not
285 * support these images anymore. Well, those images will work
286 * still work, but only if no unclean reboots happened.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400287 */
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300288 ubi_err("unsupported on-flash UBI format\n");
289 return -EINVAL;
290 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400291
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300292 /* Obviously the LEB with lower sequence counter is older */
293 second_is_newer = !!(sqnum2 > seb->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400294
295 /*
296 * Now we know which copy is newer. If the copy flag of the PEB with
297 * newer version is not set, then we just return, otherwise we have to
298 * check data CRC. For the second PEB we already have the VID header,
299 * for the first one - we'll need to re-read it from flash.
300 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300301 * Note: this may be optimized so that we wouldn't read twice.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400302 */
303
304 if (second_is_newer) {
305 if (!vid_hdr->copy_flag) {
306 /* It is not a copy, so it is newer */
307 dbg_bld("second PEB %d is newer, copy_flag is unset",
308 pnum);
309 return 1;
310 }
311 } else {
312 pnum = seb->pnum;
313
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300314 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300315 if (!vh)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400316 return -ENOMEM;
317
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300318 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400319 if (err) {
320 if (err == UBI_IO_BITFLIPS)
321 bitflips = 1;
322 else {
323 dbg_err("VID of PEB %d header is bad, but it "
324 "was OK earlier", pnum);
325 if (err > 0)
326 err = -EIO;
327
328 goto out_free_vidh;
329 }
330 }
331
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300332 if (!vh->copy_flag) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400333 /* It is not a copy, so it is newer */
334 dbg_bld("first PEB %d is newer, copy_flag is unset",
335 pnum);
336 err = bitflips << 1;
337 goto out_free_vidh;
338 }
339
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300340 vid_hdr = vh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400341 }
342
343 /* Read the data of the copy and check the CRC */
344
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300345 len = be32_to_cpu(vid_hdr->data_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300346 buf = vmalloc(len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400347 if (!buf) {
348 err = -ENOMEM;
349 goto out_free_vidh;
350 }
351
352 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
Zoltan Sogorb77bcb02008-10-29 09:50:02 +0100353 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400354 goto out_free_buf;
355
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300356 data_crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400357 crc = crc32(UBI_CRC32_INIT, buf, len);
358 if (crc != data_crc) {
359 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
360 pnum, crc, data_crc);
361 corrupted = 1;
362 bitflips = 0;
363 second_is_newer = !second_is_newer;
364 } else {
365 dbg_bld("PEB %d CRC is OK", pnum);
366 bitflips = !!err;
367 }
368
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300369 vfree(buf);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300370 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400371
372 if (second_is_newer)
373 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
374 else
375 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
376
377 return second_is_newer | (bitflips << 1) | (corrupted << 2);
378
379out_free_buf:
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300380 vfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400381out_free_vidh:
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300382 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400383 return err;
384}
385
386/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300387 * ubi_scan_add_used - add physical eraseblock to the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400388 * @ubi: UBI device description object
389 * @si: scanning information
390 * @pnum: the physical eraseblock number
391 * @ec: erase counter
392 * @vid_hdr: the volume identifier header
393 * @bitflips: if bit-flips were detected when this physical eraseblock was read
394 *
Artem Bityutskiy79b510c2007-05-05 17:36:17 +0300395 * This function adds information about a used physical eraseblock to the
396 * 'used' tree of the corresponding volume. The function is rather complex
397 * because it has to handle cases when this is not the first physical
398 * eraseblock belonging to the same logical eraseblock, and the newer one has
399 * to be picked, while the older one has to be dropped. This function returns
400 * zero in case of success and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400401 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300402int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400403 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
404 int bitflips)
405{
406 int err, vol_id, lnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400407 unsigned long long sqnum;
408 struct ubi_scan_volume *sv;
409 struct ubi_scan_leb *seb;
410 struct rb_node **p, *parent = NULL;
411
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300412 vol_id = be32_to_cpu(vid_hdr->vol_id);
413 lnum = be32_to_cpu(vid_hdr->lnum);
414 sqnum = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400415
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300416 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
417 pnum, vol_id, lnum, ec, sqnum, bitflips);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400418
419 sv = add_volume(si, vol_id, pnum, vid_hdr);
Julien Brunel0e4a0082008-09-26 15:27:25 +0200420 if (IS_ERR(sv))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400421 return PTR_ERR(sv);
422
Brijesh Singh76eafe42007-07-06 14:35:43 +0300423 if (si->max_sqnum < sqnum)
424 si->max_sqnum = sqnum;
425
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400426 /*
427 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
428 * if this is the first instance of this logical eraseblock or not.
429 */
430 p = &sv->root.rb_node;
431 while (*p) {
432 int cmp_res;
433
434 parent = *p;
435 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
436 if (lnum != seb->lnum) {
437 if (lnum < seb->lnum)
438 p = &(*p)->rb_left;
439 else
440 p = &(*p)->rb_right;
441 continue;
442 }
443
444 /*
445 * There is already a physical eraseblock describing the same
446 * logical eraseblock present.
447 */
448
449 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300450 "EC %d", seb->pnum, seb->sqnum, seb->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400451
452 /*
453 * Make sure that the logical eraseblocks have different
454 * sequence numbers. Otherwise the image is bad.
455 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300456 * However, if the sequence number is zero, we assume it must
457 * be an ancient UBI image from the era when UBI did not have
458 * sequence numbers. We still can attach these images, unless
459 * there is a need to distinguish between old and new
460 * eraseblocks, in which case we'll refuse the image in
461 * 'compare_lebs()'. In other words, we attach old clean
462 * images, but refuse attaching old images with duplicated
463 * logical eraseblocks because there was an unclean reboot.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400464 */
465 if (seb->sqnum == sqnum && sqnum != 0) {
466 ubi_err("two LEBs with same sequence number %llu",
467 sqnum);
468 ubi_dbg_dump_seb(seb, 0);
469 ubi_dbg_dump_vid_hdr(vid_hdr);
470 return -EINVAL;
471 }
472
473 /*
474 * Now we have to drop the older one and preserve the newer
475 * one.
476 */
477 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
478 if (cmp_res < 0)
479 return cmp_res;
480
481 if (cmp_res & 1) {
482 /*
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900483 * This logical eraseblock is newer than the one
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400484 * found earlier.
485 */
486 err = validate_vid_hdr(vid_hdr, sv, pnum);
487 if (err)
488 return err;
489
490 if (cmp_res & 4)
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300491 err = add_corrupted(si, seb->pnum, seb->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400492 else
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300493 err = add_to_list(si, seb->pnum, seb->ec,
494 &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400495 if (err)
496 return err;
497
498 seb->ec = ec;
499 seb->pnum = pnum;
500 seb->scrub = ((cmp_res & 2) || bitflips);
501 seb->sqnum = sqnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400502
503 if (sv->highest_lnum == lnum)
504 sv->last_data_size =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300505 be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400506
507 return 0;
508 } else {
509 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200510 * This logical eraseblock is older than the one found
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400511 * previously.
512 */
513 if (cmp_res & 4)
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300514 return add_corrupted(si, pnum, ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400515 else
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300516 return add_to_list(si, pnum, ec, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400517 }
518 }
519
520 /*
521 * We've met this logical eraseblock for the first time, add it to the
522 * scanning information.
523 */
524
525 err = validate_vid_hdr(vid_hdr, sv, pnum);
526 if (err)
527 return err;
528
529 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
530 if (!seb)
531 return -ENOMEM;
532
533 seb->ec = ec;
534 seb->pnum = pnum;
535 seb->lnum = lnum;
536 seb->sqnum = sqnum;
537 seb->scrub = bitflips;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400538
539 if (sv->highest_lnum <= lnum) {
540 sv->highest_lnum = lnum;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300541 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400542 }
543
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400544 sv->leb_count += 1;
545 rb_link_node(&seb->u.rb, parent, p);
546 rb_insert_color(&seb->u.rb, &sv->root);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300547 si->used_peb_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400548 return 0;
549}
550
551/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300552 * ubi_scan_find_sv - find volume in the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400553 * @si: scanning information
554 * @vol_id: the requested volume ID
555 *
556 * This function returns a pointer to the volume description or %NULL if there
557 * are no data about this volume in the scanning information.
558 */
559struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
560 int vol_id)
561{
562 struct ubi_scan_volume *sv;
563 struct rb_node *p = si->volumes.rb_node;
564
565 while (p) {
566 sv = rb_entry(p, struct ubi_scan_volume, rb);
567
568 if (vol_id == sv->vol_id)
569 return sv;
570
571 if (vol_id > sv->vol_id)
572 p = p->rb_left;
573 else
574 p = p->rb_right;
575 }
576
577 return NULL;
578}
579
580/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300581 * ubi_scan_find_seb - find LEB in the volume scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400582 * @sv: a pointer to the volume scanning information
583 * @lnum: the requested logical eraseblock
584 *
585 * This function returns a pointer to the scanning logical eraseblock or %NULL
586 * if there are no data about it in the scanning volume information.
587 */
588struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
589 int lnum)
590{
591 struct ubi_scan_leb *seb;
592 struct rb_node *p = sv->root.rb_node;
593
594 while (p) {
595 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
596
597 if (lnum == seb->lnum)
598 return seb;
599
600 if (lnum > seb->lnum)
601 p = p->rb_left;
602 else
603 p = p->rb_right;
604 }
605
606 return NULL;
607}
608
609/**
610 * ubi_scan_rm_volume - delete scanning information about a volume.
611 * @si: scanning information
612 * @sv: the volume scanning information to delete
613 */
614void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
615{
616 struct rb_node *rb;
617 struct ubi_scan_leb *seb;
618
619 dbg_bld("remove scanning information about volume %d", sv->vol_id);
620
621 while ((rb = rb_first(&sv->root))) {
622 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
623 rb_erase(&seb->u.rb, &sv->root);
624 list_add_tail(&seb->u.list, &si->erase);
625 }
626
627 rb_erase(&sv->rb, &si->volumes);
628 kfree(sv);
629 si->vols_found -= 1;
630}
631
632/**
633 * ubi_scan_erase_peb - erase a physical eraseblock.
634 * @ubi: UBI device description object
635 * @si: scanning information
636 * @pnum: physical eraseblock number to erase;
637 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
638 *
639 * This function erases physical eraseblock 'pnum', and writes the erase
640 * counter header to it. This function should only be used on UBI device
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300641 * initialization stages, when the EBA sub-system had not been yet initialized.
642 * This function returns zero in case of success and a negative error code in
643 * case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400644 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300645int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
646 int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400647{
648 int err;
649 struct ubi_ec_hdr *ec_hdr;
650
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400651 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
652 /*
653 * Erase counter overflow. Upgrade UBI and use 64-bit
654 * erase counters internally.
655 */
656 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
657 return -EINVAL;
658 }
659
Florin Malitadcec4c32007-07-19 15:22:41 -0400660 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
661 if (!ec_hdr)
662 return -ENOMEM;
663
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300664 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400665
666 err = ubi_io_sync_erase(ubi, pnum, 0);
667 if (err < 0)
668 goto out_free;
669
670 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
671
672out_free:
673 kfree(ec_hdr);
674 return err;
675}
676
677/**
678 * ubi_scan_get_free_peb - get a free physical eraseblock.
679 * @ubi: UBI device description object
680 * @si: scanning information
681 *
682 * This function returns a free physical eraseblock. It is supposed to be
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300683 * called on the UBI initialization stages when the wear-leveling sub-system is
684 * not initialized yet. This function picks a physical eraseblocks from one of
685 * the lists, writes the EC header if it is needed, and removes it from the
686 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400687 *
688 * This function returns scanning physical eraseblock information in case of
689 * success and an error code in case of failure.
690 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300691struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400692 struct ubi_scan_info *si)
693{
694 int err = 0, i;
695 struct ubi_scan_leb *seb;
696
697 if (!list_empty(&si->free)) {
698 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
699 list_del(&seb->u.list);
700 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
701 return seb;
702 }
703
704 for (i = 0; i < 2; i++) {
705 struct list_head *head;
706 struct ubi_scan_leb *tmp_seb;
707
708 if (i == 0)
709 head = &si->erase;
710 else
711 head = &si->corr;
712
713 /*
714 * We try to erase the first physical eraseblock from the @head
715 * list and pick it if we succeed, or try to erase the
716 * next one if not. And so forth. We don't want to take care
717 * about bad eraseblocks here - they'll be handled later.
718 */
719 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
720 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
721 seb->ec = si->mean_ec;
722
723 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
724 if (err)
725 continue;
726
727 seb->ec += 1;
728 list_del(&seb->u.list);
729 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
730 return seb;
731 }
732 }
733
734 ubi_err("no eraseblocks found");
735 return ERR_PTR(-ENOSPC);
736}
737
738/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300739 * process_eb - read, check UBI headers, and add them to scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400740 * @ubi: UBI device description object
741 * @si: scanning information
742 * @pnum: the physical eraseblock number
743 *
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300744 * This function returns a zero if the physical eraseblock was successfully
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400745 * handled and a negative error code in case of failure.
746 */
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300747static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
748 int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400749{
Artem Bityutskiyc18a8412008-01-24 11:19:14 +0200750 long long uninitialized_var(ec);
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300751 int err, bitflips = 0, vol_id, ec_err = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400752
753 dbg_bld("scan PEB %d", pnum);
754
755 /* Skip bad physical eraseblocks */
756 err = ubi_io_is_bad(ubi, pnum);
757 if (err < 0)
758 return err;
759 else if (err) {
760 /*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300761 * FIXME: this is actually duty of the I/O sub-system to
762 * initialize this, but MTD does not provide enough
763 * information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400764 */
765 si->bad_peb_count += 1;
766 return 0;
767 }
768
769 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
770 if (err < 0)
771 return err;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300772 switch (err) {
773 case 0:
774 break;
775 case UBI_IO_BITFLIPS:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400776 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300777 break;
778 case UBI_IO_FF:
779 case UBI_IO_FF_BITFLIPS:
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300780 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300781 case UBI_IO_BAD_HDR_EBADMSG:
782 si->read_err_count += 1;
783 case UBI_IO_BAD_HDR:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400784 /*
785 * We have to also look at the VID header, possibly it is not
786 * corrupted. Set %bitflips flag in order to make this PEB be
787 * moved and EC be re-created.
788 */
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300789 ec_err = err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400790 ec = UBI_SCAN_UNKNOWN_EC;
791 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300792 break;
793 default:
794 ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
795 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400796 }
797
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300798 if (!ec_err) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300799 int image_seq;
800
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400801 /* Make sure UBI version is OK */
802 if (ech->version != UBI_VERSION) {
803 ubi_err("this UBI version is %d, image version is %d",
804 UBI_VERSION, (int)ech->version);
805 return -EINVAL;
806 }
807
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300808 ec = be64_to_cpu(ech->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400809 if (ec > UBI_MAX_ERASECOUNTER) {
810 /*
811 * Erase counter overflow. The EC headers have 64 bits
812 * reserved, but we anyway make use of only 31 bit
813 * values, as this seems to be enough for any existing
814 * flash. Upgrade UBI and use 64-bit erase counters
815 * internally.
816 */
817 ubi_err("erase counter overflow, max is %d",
818 UBI_MAX_ERASECOUNTER);
819 ubi_dbg_dump_ec_hdr(ech);
820 return -EINVAL;
821 }
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300822
Adrian Hunter32bc4822009-07-24 19:16:04 +0300823 /*
824 * Make sure that all PEBs have the same image sequence number.
825 * This allows us to detect situations when users flash UBI
826 * images incorrectly, so that the flash has the new UBI image
827 * and leftovers from the old one. This feature was added
828 * relatively recently, and the sequence number was always
829 * zero, because old UBI implementations always set it to zero.
830 * For this reasons, we do not panic if some PEBs have zero
831 * sequence number, while other PEBs have non-zero sequence
832 * number.
833 */
Holger Brunck3dc948d2009-07-13 16:47:57 +0200834 image_seq = be32_to_cpu(ech->image_seq);
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300835 if (!ubi->image_seq && image_seq)
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300836 ubi->image_seq = image_seq;
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300837 if (ubi->image_seq && image_seq &&
838 ubi->image_seq != image_seq) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300839 ubi_err("bad image sequence number %d in PEB %d, "
840 "expected %d", image_seq, pnum, ubi->image_seq);
841 ubi_dbg_dump_ec_hdr(ech);
842 return -EINVAL;
843 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400844 }
845
846 /* OK, we've done with the EC header, let's look at the VID header */
847
848 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
849 if (err < 0)
850 return err;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300851 switch (err) {
852 case 0:
853 break;
854 case UBI_IO_BITFLIPS:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400855 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300856 break;
857 case UBI_IO_BAD_HDR_EBADMSG:
858 si->read_err_count += 1;
859 case UBI_IO_BAD_HDR:
860 case UBI_IO_FF_BITFLIPS:
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300861 err = add_corrupted(si, pnum, ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400862 if (err)
863 return err;
864 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300865 case UBI_IO_FF:
866 if (ec_err)
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300867 err = add_corrupted(si, pnum, ec);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300868 else
869 err = add_to_list(si, pnum, ec, &si->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400870 if (err)
871 return err;
872 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300873 default:
874 ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
875 err);
876 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400877 }
878
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300879 vol_id = be32_to_cpu(vidh->vol_id);
Artem Bityutskiy91f2d532008-01-24 11:23:23 +0200880 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300881 int lnum = be32_to_cpu(vidh->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400882
883 /* Unsupported internal volume */
884 switch (vidh->compat) {
885 case UBI_COMPAT_DELETE:
886 ubi_msg("\"delete\" compatible internal volume %d:%d"
Brijesh Singh158132c2010-06-16 09:28:26 +0300887 " found, will remove it", vol_id, lnum);
Artem Bityutskiy80c1c162010-08-28 12:11:40 +0300888 err = add_to_list(si, pnum, ec, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400889 if (err)
890 return err;
Brijesh Singh158132c2010-06-16 09:28:26 +0300891 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400892
893 case UBI_COMPAT_RO:
894 ubi_msg("read-only compatible internal volume %d:%d"
895 " found, switch to read-only mode",
896 vol_id, lnum);
897 ubi->ro_mode = 1;
898 break;
899
900 case UBI_COMPAT_PRESERVE:
901 ubi_msg("\"preserve\" compatible internal volume %d:%d"
902 " found", vol_id, lnum);
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300903 err = add_to_list(si, pnum, ec, &si->alien);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400904 if (err)
905 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400906 return 0;
907
908 case UBI_COMPAT_REJECT:
909 ubi_err("incompatible internal volume %d:%d found",
910 vol_id, lnum);
911 return -EINVAL;
912 }
913 }
914
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300915 if (ec_err)
Artem Bityutskiy29a88c92009-07-19 14:03:16 +0300916 ubi_warn("valid VID header but corrupted EC header at PEB %d",
917 pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400918 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
919 if (err)
920 return err;
921
922adjust_mean_ec:
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300923 if (!ec_err) {
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +0300924 si->ec_sum += ec;
925 si->ec_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400926 if (ec > si->max_ec)
927 si->max_ec = ec;
928 if (ec < si->min_ec)
929 si->min_ec = ec;
930 }
931
932 return 0;
933}
934
935/**
Artem Bityutskiy0798cea2010-04-30 13:02:33 +0300936 * check_what_we_have - check what PEB were found by scanning.
937 * @ubi: UBI device description object
938 * @si: scanning information
939 *
940 * This is a helper function which takes a look what PEBs were found by
941 * scanning, and decides whether the flash is empty and should be formatted and
942 * whether there are too many corrupted PEBs and we should not attach this
943 * MTD device. Returns zero if we should proceed with attaching the MTD device,
944 * and %-EINVAL if we should not.
945 */
Artem Bityutskiyf5d5b1f2010-06-14 08:15:39 +0300946static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
Artem Bityutskiy0798cea2010-04-30 13:02:33 +0300947{
948 struct ubi_scan_leb *seb;
949 int max_corr;
950
951 max_corr = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
952 max_corr = max_corr / 20 ?: 8;
953
954 /*
955 * Few corrupted PEBs are not a problem and may be just a result of
956 * unclean reboots. However, many of them may indicate some problems
957 * with the flash HW or driver.
958 */
959 if (si->corr_peb_count >= 8) {
960 ubi_warn("%d PEBs are corrupted", si->corr_peb_count);
961 printk(KERN_WARNING "corrupted PEBs are:");
962 list_for_each_entry(seb, &si->corr, u.list)
963 printk(KERN_CONT " %d", seb->pnum);
964 printk(KERN_CONT "\n");
965
966 /*
967 * If too many PEBs are corrupted, we refuse attaching,
968 * otherwise, only print a warning.
969 */
970 if (si->corr_peb_count >= max_corr) {
971 ubi_err("too many corrupted PEBs, refusing this device");
972 return -EINVAL;
973 }
974 }
975
976 if (si->free_peb_count + si->used_peb_count +
977 si->alien_peb_count == 0) {
978 /* No UBI-formatted eraseblocks were found */
979 if (si->corr_peb_count == si->read_err_count &&
980 si->corr_peb_count < 8) {
981 /* No or just few corrupted PEBs, and all of them had a
982 * read error. We assume that those are bad PEBs, which
983 * were just not marked as bad so far.
984 *
985 * This piece of code basically tries to distinguish
986 * between the following 2 situations:
987 *
988 * 1. Flash is empty, but there are few bad PEBs, which
989 * are not marked as bad so far, and which were read
990 * with error. We want to go ahead and format this
991 * flash. While formating, the faulty PEBs will
992 * probably be marked as bad.
993 *
994 * 2. Flash probably contains non-UBI data and we do
995 * not want to format it and destroy possibly needed
996 * data (e.g., consider the case when the bootloader
997 * MTD partition was accidentally fed to UBI).
998 */
999 si->is_empty = 1;
1000 ubi_msg("empty MTD device detected");
Matthieu CASTET095751a2010-06-03 16:14:27 +02001001 get_random_bytes(&ubi->image_seq, sizeof(ubi->image_seq));
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001002 } else {
1003 ubi_err("MTD device possibly contains non-UBI data, "
1004 "refusing it");
1005 return -EINVAL;
1006 }
1007 }
1008
Artem Bityutskiy7cdb9962010-07-30 16:31:38 +03001009 if (si->corr_peb_count > 0)
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001010 ubi_msg("corrupted PEBs will be formatted");
1011 return 0;
1012}
1013
1014/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001015 * ubi_scan - scan an MTD device.
1016 * @ubi: UBI device description object
1017 *
1018 * This function does full scanning of an MTD device and returns complete
1019 * information about it. In case of failure, an error code is returned.
1020 */
1021struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
1022{
1023 int err, pnum;
1024 struct rb_node *rb1, *rb2;
1025 struct ubi_scan_volume *sv;
1026 struct ubi_scan_leb *seb;
1027 struct ubi_scan_info *si;
1028
1029 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
1030 if (!si)
1031 return ERR_PTR(-ENOMEM);
1032
1033 INIT_LIST_HEAD(&si->corr);
1034 INIT_LIST_HEAD(&si->free);
1035 INIT_LIST_HEAD(&si->erase);
1036 INIT_LIST_HEAD(&si->alien);
1037 si->volumes = RB_ROOT;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001038
1039 err = -ENOMEM;
1040 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1041 if (!ech)
1042 goto out_si;
1043
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001044 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001045 if (!vidh)
1046 goto out_ech;
1047
1048 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1049 cond_resched();
1050
Artem Bityutskiyc8566352008-07-16 17:40:22 +03001051 dbg_gen("process PEB %d", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001052 err = process_eb(ubi, si, pnum);
1053 if (err < 0)
1054 goto out_vidh;
1055 }
1056
1057 dbg_msg("scanning is finished");
1058
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +03001059 /* Calculate mean erase counter */
Artem Bityutskiy3013ee32009-01-16 19:08:43 +02001060 if (si->ec_count)
1061 si->mean_ec = div_u64(si->ec_sum, si->ec_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001062
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001063 err = check_what_we_have(ubi, si);
1064 if (err)
1065 goto out_vidh;
Artem Bityutskiy4a406852009-07-19 14:33:14 +03001066
1067 /*
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001068 * In case of unknown erase counter we use the mean erase counter
1069 * value.
1070 */
1071 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1072 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1073 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1074 seb->ec = si->mean_ec;
1075 }
1076
1077 list_for_each_entry(seb, &si->free, u.list) {
1078 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1079 seb->ec = si->mean_ec;
1080 }
1081
1082 list_for_each_entry(seb, &si->corr, u.list)
1083 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1084 seb->ec = si->mean_ec;
1085
1086 list_for_each_entry(seb, &si->erase, u.list)
1087 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1088 seb->ec = si->mean_ec;
1089
1090 err = paranoid_check_si(ubi, si);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001091 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001092 goto out_vidh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001093
1094 ubi_free_vid_hdr(ubi, vidh);
1095 kfree(ech);
1096
1097 return si;
1098
1099out_vidh:
1100 ubi_free_vid_hdr(ubi, vidh);
1101out_ech:
1102 kfree(ech);
1103out_si:
1104 ubi_scan_destroy_si(si);
1105 return ERR_PTR(err);
1106}
1107
1108/**
1109 * destroy_sv - free the scanning volume information
1110 * @sv: scanning volume information
1111 *
1112 * This function destroys the volume RB-tree (@sv->root) and the scanning
1113 * volume information.
1114 */
1115static void destroy_sv(struct ubi_scan_volume *sv)
1116{
1117 struct ubi_scan_leb *seb;
1118 struct rb_node *this = sv->root.rb_node;
1119
1120 while (this) {
1121 if (this->rb_left)
1122 this = this->rb_left;
1123 else if (this->rb_right)
1124 this = this->rb_right;
1125 else {
1126 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1127 this = rb_parent(this);
1128 if (this) {
1129 if (this->rb_left == &seb->u.rb)
1130 this->rb_left = NULL;
1131 else
1132 this->rb_right = NULL;
1133 }
1134
1135 kfree(seb);
1136 }
1137 }
1138 kfree(sv);
1139}
1140
1141/**
1142 * ubi_scan_destroy_si - destroy scanning information.
1143 * @si: scanning information
1144 */
1145void ubi_scan_destroy_si(struct ubi_scan_info *si)
1146{
1147 struct ubi_scan_leb *seb, *seb_tmp;
1148 struct ubi_scan_volume *sv;
1149 struct rb_node *rb;
1150
1151 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1152 list_del(&seb->u.list);
1153 kfree(seb);
1154 }
1155 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1156 list_del(&seb->u.list);
1157 kfree(seb);
1158 }
1159 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1160 list_del(&seb->u.list);
1161 kfree(seb);
1162 }
1163 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1164 list_del(&seb->u.list);
1165 kfree(seb);
1166 }
1167
1168 /* Destroy the volume RB-tree */
1169 rb = si->volumes.rb_node;
1170 while (rb) {
1171 if (rb->rb_left)
1172 rb = rb->rb_left;
1173 else if (rb->rb_right)
1174 rb = rb->rb_right;
1175 else {
1176 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1177
1178 rb = rb_parent(rb);
1179 if (rb) {
1180 if (rb->rb_left == &sv->rb)
1181 rb->rb_left = NULL;
1182 else
1183 rb->rb_right = NULL;
1184 }
1185
1186 destroy_sv(sv);
1187 }
1188 }
1189
1190 kfree(si);
1191}
1192
1193#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1194
1195/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001196 * paranoid_check_si - check the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001197 * @ubi: UBI device description object
1198 * @si: scanning information
1199 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001200 * This function returns zero if the scanning information is all right, and a
1201 * negative error code if not or if an error occurred.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001202 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001203static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001204{
1205 int pnum, err, vols_found = 0;
1206 struct rb_node *rb1, *rb2;
1207 struct ubi_scan_volume *sv;
1208 struct ubi_scan_leb *seb, *last_seb;
1209 uint8_t *buf;
1210
1211 /*
Artem Bityutskiy78d87c92007-05-05 11:24:02 +03001212 * At first, check that scanning information is OK.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001213 */
1214 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1215 int leb_count = 0;
1216
1217 cond_resched();
1218
1219 vols_found += 1;
1220
1221 if (si->is_empty) {
1222 ubi_err("bad is_empty flag");
1223 goto bad_sv;
1224 }
1225
1226 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1227 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1228 sv->data_pad < 0 || sv->last_data_size < 0) {
1229 ubi_err("negative values");
1230 goto bad_sv;
1231 }
1232
1233 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1234 sv->vol_id < UBI_INTERNAL_VOL_START) {
1235 ubi_err("bad vol_id");
1236 goto bad_sv;
1237 }
1238
1239 if (sv->vol_id > si->highest_vol_id) {
1240 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1241 si->highest_vol_id, sv->vol_id);
1242 goto out;
1243 }
1244
1245 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1246 sv->vol_type != UBI_STATIC_VOLUME) {
1247 ubi_err("bad vol_type");
1248 goto bad_sv;
1249 }
1250
1251 if (sv->data_pad > ubi->leb_size / 2) {
1252 ubi_err("bad data_pad");
1253 goto bad_sv;
1254 }
1255
1256 last_seb = NULL;
1257 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1258 cond_resched();
1259
1260 last_seb = seb;
1261 leb_count += 1;
1262
1263 if (seb->pnum < 0 || seb->ec < 0) {
1264 ubi_err("negative values");
1265 goto bad_seb;
1266 }
1267
1268 if (seb->ec < si->min_ec) {
1269 ubi_err("bad si->min_ec (%d), %d found",
1270 si->min_ec, seb->ec);
1271 goto bad_seb;
1272 }
1273
1274 if (seb->ec > si->max_ec) {
1275 ubi_err("bad si->max_ec (%d), %d found",
1276 si->max_ec, seb->ec);
1277 goto bad_seb;
1278 }
1279
1280 if (seb->pnum >= ubi->peb_count) {
1281 ubi_err("too high PEB number %d, total PEBs %d",
1282 seb->pnum, ubi->peb_count);
1283 goto bad_seb;
1284 }
1285
1286 if (sv->vol_type == UBI_STATIC_VOLUME) {
1287 if (seb->lnum >= sv->used_ebs) {
1288 ubi_err("bad lnum or used_ebs");
1289 goto bad_seb;
1290 }
1291 } else {
1292 if (sv->used_ebs != 0) {
1293 ubi_err("non-zero used_ebs");
1294 goto bad_seb;
1295 }
1296 }
1297
1298 if (seb->lnum > sv->highest_lnum) {
1299 ubi_err("incorrect highest_lnum or lnum");
1300 goto bad_seb;
1301 }
1302 }
1303
1304 if (sv->leb_count != leb_count) {
1305 ubi_err("bad leb_count, %d objects in the tree",
1306 leb_count);
1307 goto bad_sv;
1308 }
1309
1310 if (!last_seb)
1311 continue;
1312
1313 seb = last_seb;
1314
1315 if (seb->lnum != sv->highest_lnum) {
1316 ubi_err("bad highest_lnum");
1317 goto bad_seb;
1318 }
1319 }
1320
1321 if (vols_found != si->vols_found) {
1322 ubi_err("bad si->vols_found %d, should be %d",
1323 si->vols_found, vols_found);
1324 goto out;
1325 }
1326
1327 /* Check that scanning information is correct */
1328 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1329 last_seb = NULL;
1330 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1331 int vol_type;
1332
1333 cond_resched();
1334
1335 last_seb = seb;
1336
1337 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1338 if (err && err != UBI_IO_BITFLIPS) {
1339 ubi_err("VID header is not OK (%d)", err);
1340 if (err > 0)
1341 err = -EIO;
1342 return err;
1343 }
1344
1345 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1346 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1347 if (sv->vol_type != vol_type) {
1348 ubi_err("bad vol_type");
1349 goto bad_vid_hdr;
1350 }
1351
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001352 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001353 ubi_err("bad sqnum %llu", seb->sqnum);
1354 goto bad_vid_hdr;
1355 }
1356
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001357 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001358 ubi_err("bad vol_id %d", sv->vol_id);
1359 goto bad_vid_hdr;
1360 }
1361
1362 if (sv->compat != vidh->compat) {
1363 ubi_err("bad compat %d", vidh->compat);
1364 goto bad_vid_hdr;
1365 }
1366
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001367 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001368 ubi_err("bad lnum %d", seb->lnum);
1369 goto bad_vid_hdr;
1370 }
1371
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001372 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001373 ubi_err("bad used_ebs %d", sv->used_ebs);
1374 goto bad_vid_hdr;
1375 }
1376
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001377 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001378 ubi_err("bad data_pad %d", sv->data_pad);
1379 goto bad_vid_hdr;
1380 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001381 }
1382
1383 if (!last_seb)
1384 continue;
1385
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001386 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001387 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1388 goto bad_vid_hdr;
1389 }
1390
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001391 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001392 ubi_err("bad last_data_size %d", sv->last_data_size);
1393 goto bad_vid_hdr;
1394 }
1395 }
1396
1397 /*
1398 * Make sure that all the physical eraseblocks are in one of the lists
1399 * or trees.
1400 */
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001401 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001402 if (!buf)
1403 return -ENOMEM;
1404
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001405 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1406 err = ubi_io_is_bad(ubi, pnum);
Artem Bityutskiy341e1a02007-05-03 11:59:51 +03001407 if (err < 0) {
1408 kfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001409 return err;
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +03001410 } else if (err)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001411 buf[pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001412 }
1413
1414 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1415 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001416 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001417
1418 list_for_each_entry(seb, &si->free, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001419 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001420
1421 list_for_each_entry(seb, &si->corr, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001422 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001423
1424 list_for_each_entry(seb, &si->erase, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001425 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001426
1427 list_for_each_entry(seb, &si->alien, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001428 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001429
1430 err = 0;
1431 for (pnum = 0; pnum < ubi->peb_count; pnum++)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001432 if (!buf[pnum]) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001433 ubi_err("PEB %d is not referred", pnum);
1434 err = 1;
1435 }
1436
1437 kfree(buf);
1438 if (err)
1439 goto out;
1440 return 0;
1441
1442bad_seb:
1443 ubi_err("bad scanning information about LEB %d", seb->lnum);
1444 ubi_dbg_dump_seb(seb, 0);
1445 ubi_dbg_dump_sv(sv);
1446 goto out;
1447
1448bad_sv:
1449 ubi_err("bad scanning information about volume %d", sv->vol_id);
1450 ubi_dbg_dump_sv(sv);
1451 goto out;
1452
1453bad_vid_hdr:
1454 ubi_err("bad scanning information about volume %d", sv->vol_id);
1455 ubi_dbg_dump_sv(sv);
1456 ubi_dbg_dump_vid_hdr(vidh);
1457
1458out:
1459 ubi_dbg_dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001460 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001461}
1462
1463#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */