blob: aed19f33b8f3ec1685fd40acf121df738c2c77d4 [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>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040047#include "ubi.h"
48
49#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
Artem Bityutskiye88d6e102007-08-29 14:51:52 +030050static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040051#else
52#define paranoid_check_si(ubi, si) 0
53#endif
54
55/* Temporary variables used during scanning */
56static struct ubi_ec_hdr *ech;
57static struct ubi_vid_hdr *vidh;
58
Artem Bityutskiy941dfb02007-05-05 16:33:13 +030059/**
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030060 * add_to_list - add physical eraseblock to a list.
61 * @si: scanning information
62 * @pnum: physical eraseblock number to add
63 * @ec: erase counter of the physical eraseblock
64 * @list: the list to add to
65 *
66 * This function adds physical eraseblock @pnum to free, erase, corrupted or
67 * alien lists. Returns zero in case of success and a negative error code in
68 * case of failure.
69 */
70static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
71 struct list_head *list)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040072{
73 struct ubi_scan_leb *seb;
74
75 if (list == &si->free)
76 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
77 else if (list == &si->erase)
78 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy4a406852009-07-19 14:33:14 +030079 else if (list == &si->corr) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040080 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy4a406852009-07-19 14:33:14 +030081 si->corr_count += 1;
82 } 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);
84 else
85 BUG();
86
87 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
88 if (!seb)
89 return -ENOMEM;
90
91 seb->pnum = pnum;
92 seb->ec = ec;
93 list_add_tail(&seb->u.list, list);
94 return 0;
95}
96
97/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +030098 * validate_vid_hdr - check volume identifier header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040099 * @vid_hdr: the volume identifier header to check
100 * @sv: information about the volume this logical eraseblock belongs to
101 * @pnum: physical eraseblock number the VID header came from
102 *
103 * This function checks that data stored in @vid_hdr is consistent. Returns
104 * non-zero if an inconsistency was found and zero if not.
105 *
106 * Note, UBI does sanity check of everything it reads from the flash media.
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300107 * 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 +0400108 * information in the VID header is consistent to the information in other VID
109 * headers of the same volume.
110 */
111static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
112 const struct ubi_scan_volume *sv, int pnum)
113{
114 int vol_type = vid_hdr->vol_type;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300115 int vol_id = be32_to_cpu(vid_hdr->vol_id);
116 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
117 int data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400118
119 if (sv->leb_count != 0) {
120 int sv_vol_type;
121
122 /*
123 * This is not the first logical eraseblock belonging to this
124 * volume. Ensure that the data in its VID header is consistent
125 * to the data in previous logical eraseblock headers.
126 */
127
128 if (vol_id != sv->vol_id) {
129 dbg_err("inconsistent vol_id");
130 goto bad;
131 }
132
133 if (sv->vol_type == UBI_STATIC_VOLUME)
134 sv_vol_type = UBI_VID_STATIC;
135 else
136 sv_vol_type = UBI_VID_DYNAMIC;
137
138 if (vol_type != sv_vol_type) {
139 dbg_err("inconsistent vol_type");
140 goto bad;
141 }
142
143 if (used_ebs != sv->used_ebs) {
144 dbg_err("inconsistent used_ebs");
145 goto bad;
146 }
147
148 if (data_pad != sv->data_pad) {
149 dbg_err("inconsistent data_pad");
150 goto bad;
151 }
152 }
153
154 return 0;
155
156bad:
157 ubi_err("inconsistent VID header at PEB %d", pnum);
158 ubi_dbg_dump_vid_hdr(vid_hdr);
159 ubi_dbg_dump_sv(sv);
160 return -EINVAL;
161}
162
163/**
164 * add_volume - add volume to the scanning information.
165 * @si: scanning information
166 * @vol_id: ID of the volume to add
167 * @pnum: physical eraseblock number
168 * @vid_hdr: volume identifier header
169 *
170 * If the volume corresponding to the @vid_hdr logical eraseblock is already
171 * present in the scanning information, this function does nothing. Otherwise
172 * it adds corresponding volume to the scanning information. Returns a pointer
173 * to the scanning volume object in case of success and a negative error code
174 * in case of failure.
175 */
176static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
177 int pnum,
178 const struct ubi_vid_hdr *vid_hdr)
179{
180 struct ubi_scan_volume *sv;
181 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
182
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300183 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400184
185 /* Walk the volume RB-tree to look if this volume is already present */
186 while (*p) {
187 parent = *p;
188 sv = rb_entry(parent, struct ubi_scan_volume, rb);
189
190 if (vol_id == sv->vol_id)
191 return sv;
192
193 if (vol_id > sv->vol_id)
194 p = &(*p)->rb_left;
195 else
196 p = &(*p)->rb_right;
197 }
198
199 /* The volume is absent - add it */
200 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
201 if (!sv)
202 return ERR_PTR(-ENOMEM);
203
204 sv->highest_lnum = sv->leb_count = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400205 sv->vol_id = vol_id;
206 sv->root = RB_ROOT;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300207 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
208 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400209 sv->compat = vid_hdr->compat;
210 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
211 : UBI_STATIC_VOLUME;
212 if (vol_id > si->highest_vol_id)
213 si->highest_vol_id = vol_id;
214
215 rb_link_node(&sv->rb, parent, p);
216 rb_insert_color(&sv->rb, &si->volumes);
217 si->vols_found += 1;
218 dbg_bld("added volume %d", vol_id);
219 return sv;
220}
221
222/**
223 * compare_lebs - find out which logical eraseblock is newer.
224 * @ubi: UBI device description object
225 * @seb: first logical eraseblock to compare
226 * @pnum: physical eraseblock number of the second logical eraseblock to
227 * compare
228 * @vid_hdr: volume identifier header of the second logical eraseblock
229 *
230 * This function compares 2 copies of a LEB and informs which one is newer. In
231 * case of success this function returns a positive value, in case of failure, a
232 * negative error code is returned. The success return codes use the following
233 * bits:
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900234 * o bit 0 is cleared: the first PEB (described by @seb) is newer than the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400235 * second PEB (described by @pnum and @vid_hdr);
236 * o bit 0 is set: the second PEB is newer;
237 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
238 * o bit 1 is set: bit-flips were detected in the newer LEB;
239 * o bit 2 is cleared: the older LEB is not corrupted;
240 * o bit 2 is set: the older LEB is corrupted.
241 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300242static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
243 int pnum, const struct ubi_vid_hdr *vid_hdr)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400244{
245 void *buf;
246 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
247 uint32_t data_crc, crc;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300248 struct ubi_vid_hdr *vh = NULL;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300249 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400250
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300251 if (sqnum2 == seb->sqnum) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400252 /*
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300253 * This must be a really ancient UBI image which has been
254 * created before sequence numbers support has been added. At
255 * that times we used 32-bit LEB versions stored in logical
256 * eraseblocks. That was before UBI got into mainline. We do not
257 * support these images anymore. Well, those images will work
258 * still work, but only if no unclean reboots happened.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400259 */
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300260 ubi_err("unsupported on-flash UBI format\n");
261 return -EINVAL;
262 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400263
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300264 /* Obviously the LEB with lower sequence counter is older */
265 second_is_newer = !!(sqnum2 > seb->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400266
267 /*
268 * Now we know which copy is newer. If the copy flag of the PEB with
269 * newer version is not set, then we just return, otherwise we have to
270 * check data CRC. For the second PEB we already have the VID header,
271 * for the first one - we'll need to re-read it from flash.
272 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300273 * Note: this may be optimized so that we wouldn't read twice.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400274 */
275
276 if (second_is_newer) {
277 if (!vid_hdr->copy_flag) {
278 /* It is not a copy, so it is newer */
279 dbg_bld("second PEB %d is newer, copy_flag is unset",
280 pnum);
281 return 1;
282 }
283 } else {
284 pnum = seb->pnum;
285
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300286 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300287 if (!vh)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400288 return -ENOMEM;
289
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300290 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400291 if (err) {
292 if (err == UBI_IO_BITFLIPS)
293 bitflips = 1;
294 else {
295 dbg_err("VID of PEB %d header is bad, but it "
296 "was OK earlier", pnum);
297 if (err > 0)
298 err = -EIO;
299
300 goto out_free_vidh;
301 }
302 }
303
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300304 if (!vh->copy_flag) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400305 /* It is not a copy, so it is newer */
306 dbg_bld("first PEB %d is newer, copy_flag is unset",
307 pnum);
308 err = bitflips << 1;
309 goto out_free_vidh;
310 }
311
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300312 vid_hdr = vh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400313 }
314
315 /* Read the data of the copy and check the CRC */
316
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300317 len = be32_to_cpu(vid_hdr->data_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300318 buf = vmalloc(len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400319 if (!buf) {
320 err = -ENOMEM;
321 goto out_free_vidh;
322 }
323
324 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
Zoltan Sogorb77bcb02008-10-29 09:50:02 +0100325 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400326 goto out_free_buf;
327
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300328 data_crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400329 crc = crc32(UBI_CRC32_INIT, buf, len);
330 if (crc != data_crc) {
331 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
332 pnum, crc, data_crc);
333 corrupted = 1;
334 bitflips = 0;
335 second_is_newer = !second_is_newer;
336 } else {
337 dbg_bld("PEB %d CRC is OK", pnum);
338 bitflips = !!err;
339 }
340
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300341 vfree(buf);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300342 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400343
344 if (second_is_newer)
345 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
346 else
347 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
348
349 return second_is_newer | (bitflips << 1) | (corrupted << 2);
350
351out_free_buf:
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300352 vfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400353out_free_vidh:
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300354 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400355 return err;
356}
357
358/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300359 * ubi_scan_add_used - add physical eraseblock to the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400360 * @ubi: UBI device description object
361 * @si: scanning information
362 * @pnum: the physical eraseblock number
363 * @ec: erase counter
364 * @vid_hdr: the volume identifier header
365 * @bitflips: if bit-flips were detected when this physical eraseblock was read
366 *
Artem Bityutskiy79b510c2007-05-05 17:36:17 +0300367 * This function adds information about a used physical eraseblock to the
368 * 'used' tree of the corresponding volume. The function is rather complex
369 * because it has to handle cases when this is not the first physical
370 * eraseblock belonging to the same logical eraseblock, and the newer one has
371 * to be picked, while the older one has to be dropped. This function returns
372 * zero in case of success and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400373 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300374int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400375 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
376 int bitflips)
377{
378 int err, vol_id, lnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400379 unsigned long long sqnum;
380 struct ubi_scan_volume *sv;
381 struct ubi_scan_leb *seb;
382 struct rb_node **p, *parent = NULL;
383
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300384 vol_id = be32_to_cpu(vid_hdr->vol_id);
385 lnum = be32_to_cpu(vid_hdr->lnum);
386 sqnum = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400387
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300388 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
389 pnum, vol_id, lnum, ec, sqnum, bitflips);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400390
391 sv = add_volume(si, vol_id, pnum, vid_hdr);
Julien Brunel0e4a0082008-09-26 15:27:25 +0200392 if (IS_ERR(sv))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400393 return PTR_ERR(sv);
394
Brijesh Singh76eafe42007-07-06 14:35:43 +0300395 if (si->max_sqnum < sqnum)
396 si->max_sqnum = sqnum;
397
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400398 /*
399 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
400 * if this is the first instance of this logical eraseblock or not.
401 */
402 p = &sv->root.rb_node;
403 while (*p) {
404 int cmp_res;
405
406 parent = *p;
407 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
408 if (lnum != seb->lnum) {
409 if (lnum < seb->lnum)
410 p = &(*p)->rb_left;
411 else
412 p = &(*p)->rb_right;
413 continue;
414 }
415
416 /*
417 * There is already a physical eraseblock describing the same
418 * logical eraseblock present.
419 */
420
421 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300422 "EC %d", seb->pnum, seb->sqnum, seb->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400423
424 /*
425 * Make sure that the logical eraseblocks have different
426 * sequence numbers. Otherwise the image is bad.
427 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300428 * However, if the sequence number is zero, we assume it must
429 * be an ancient UBI image from the era when UBI did not have
430 * sequence numbers. We still can attach these images, unless
431 * there is a need to distinguish between old and new
432 * eraseblocks, in which case we'll refuse the image in
433 * 'compare_lebs()'. In other words, we attach old clean
434 * images, but refuse attaching old images with duplicated
435 * logical eraseblocks because there was an unclean reboot.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400436 */
437 if (seb->sqnum == sqnum && sqnum != 0) {
438 ubi_err("two LEBs with same sequence number %llu",
439 sqnum);
440 ubi_dbg_dump_seb(seb, 0);
441 ubi_dbg_dump_vid_hdr(vid_hdr);
442 return -EINVAL;
443 }
444
445 /*
446 * Now we have to drop the older one and preserve the newer
447 * one.
448 */
449 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
450 if (cmp_res < 0)
451 return cmp_res;
452
453 if (cmp_res & 1) {
454 /*
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900455 * This logical eraseblock is newer than the one
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400456 * found earlier.
457 */
458 err = validate_vid_hdr(vid_hdr, sv, pnum);
459 if (err)
460 return err;
461
462 if (cmp_res & 4)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300463 err = add_to_list(si, seb->pnum, seb->ec,
464 &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400465 else
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300466 err = add_to_list(si, seb->pnum, seb->ec,
467 &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400468 if (err)
469 return err;
470
471 seb->ec = ec;
472 seb->pnum = pnum;
473 seb->scrub = ((cmp_res & 2) || bitflips);
474 seb->sqnum = sqnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400475
476 if (sv->highest_lnum == lnum)
477 sv->last_data_size =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300478 be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400479
480 return 0;
481 } else {
482 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200483 * This logical eraseblock is older than the one found
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400484 * previously.
485 */
486 if (cmp_res & 4)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300487 return add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400488 else
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300489 return add_to_list(si, pnum, ec, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400490 }
491 }
492
493 /*
494 * We've met this logical eraseblock for the first time, add it to the
495 * scanning information.
496 */
497
498 err = validate_vid_hdr(vid_hdr, sv, pnum);
499 if (err)
500 return err;
501
502 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
503 if (!seb)
504 return -ENOMEM;
505
506 seb->ec = ec;
507 seb->pnum = pnum;
508 seb->lnum = lnum;
509 seb->sqnum = sqnum;
510 seb->scrub = bitflips;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400511
512 if (sv->highest_lnum <= lnum) {
513 sv->highest_lnum = lnum;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300514 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400515 }
516
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400517 sv->leb_count += 1;
518 rb_link_node(&seb->u.rb, parent, p);
519 rb_insert_color(&seb->u.rb, &sv->root);
520 return 0;
521}
522
523/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300524 * ubi_scan_find_sv - find volume in the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400525 * @si: scanning information
526 * @vol_id: the requested volume ID
527 *
528 * This function returns a pointer to the volume description or %NULL if there
529 * are no data about this volume in the scanning information.
530 */
531struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
532 int vol_id)
533{
534 struct ubi_scan_volume *sv;
535 struct rb_node *p = si->volumes.rb_node;
536
537 while (p) {
538 sv = rb_entry(p, struct ubi_scan_volume, rb);
539
540 if (vol_id == sv->vol_id)
541 return sv;
542
543 if (vol_id > sv->vol_id)
544 p = p->rb_left;
545 else
546 p = p->rb_right;
547 }
548
549 return NULL;
550}
551
552/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300553 * ubi_scan_find_seb - find LEB in the volume scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400554 * @sv: a pointer to the volume scanning information
555 * @lnum: the requested logical eraseblock
556 *
557 * This function returns a pointer to the scanning logical eraseblock or %NULL
558 * if there are no data about it in the scanning volume information.
559 */
560struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
561 int lnum)
562{
563 struct ubi_scan_leb *seb;
564 struct rb_node *p = sv->root.rb_node;
565
566 while (p) {
567 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
568
569 if (lnum == seb->lnum)
570 return seb;
571
572 if (lnum > seb->lnum)
573 p = p->rb_left;
574 else
575 p = p->rb_right;
576 }
577
578 return NULL;
579}
580
581/**
582 * ubi_scan_rm_volume - delete scanning information about a volume.
583 * @si: scanning information
584 * @sv: the volume scanning information to delete
585 */
586void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
587{
588 struct rb_node *rb;
589 struct ubi_scan_leb *seb;
590
591 dbg_bld("remove scanning information about volume %d", sv->vol_id);
592
593 while ((rb = rb_first(&sv->root))) {
594 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
595 rb_erase(&seb->u.rb, &sv->root);
596 list_add_tail(&seb->u.list, &si->erase);
597 }
598
599 rb_erase(&sv->rb, &si->volumes);
600 kfree(sv);
601 si->vols_found -= 1;
602}
603
604/**
605 * ubi_scan_erase_peb - erase a physical eraseblock.
606 * @ubi: UBI device description object
607 * @si: scanning information
608 * @pnum: physical eraseblock number to erase;
609 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
610 *
611 * This function erases physical eraseblock 'pnum', and writes the erase
612 * counter header to it. This function should only be used on UBI device
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300613 * initialization stages, when the EBA sub-system had not been yet initialized.
614 * This function returns zero in case of success and a negative error code in
615 * case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400616 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300617int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
618 int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400619{
620 int err;
621 struct ubi_ec_hdr *ec_hdr;
622
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400623 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
624 /*
625 * Erase counter overflow. Upgrade UBI and use 64-bit
626 * erase counters internally.
627 */
628 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
629 return -EINVAL;
630 }
631
Florin Malitadcec4c32007-07-19 15:22:41 -0400632 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
633 if (!ec_hdr)
634 return -ENOMEM;
635
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300636 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400637
638 err = ubi_io_sync_erase(ubi, pnum, 0);
639 if (err < 0)
640 goto out_free;
641
642 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
643
644out_free:
645 kfree(ec_hdr);
646 return err;
647}
648
649/**
650 * ubi_scan_get_free_peb - get a free physical eraseblock.
651 * @ubi: UBI device description object
652 * @si: scanning information
653 *
654 * This function returns a free physical eraseblock. It is supposed to be
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300655 * called on the UBI initialization stages when the wear-leveling sub-system is
656 * not initialized yet. This function picks a physical eraseblocks from one of
657 * the lists, writes the EC header if it is needed, and removes it from the
658 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400659 *
660 * This function returns scanning physical eraseblock information in case of
661 * success and an error code in case of failure.
662 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300663struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400664 struct ubi_scan_info *si)
665{
666 int err = 0, i;
667 struct ubi_scan_leb *seb;
668
669 if (!list_empty(&si->free)) {
670 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
671 list_del(&seb->u.list);
672 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
673 return seb;
674 }
675
676 for (i = 0; i < 2; i++) {
677 struct list_head *head;
678 struct ubi_scan_leb *tmp_seb;
679
680 if (i == 0)
681 head = &si->erase;
682 else
683 head = &si->corr;
684
685 /*
686 * We try to erase the first physical eraseblock from the @head
687 * list and pick it if we succeed, or try to erase the
688 * next one if not. And so forth. We don't want to take care
689 * about bad eraseblocks here - they'll be handled later.
690 */
691 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
692 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
693 seb->ec = si->mean_ec;
694
695 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
696 if (err)
697 continue;
698
699 seb->ec += 1;
700 list_del(&seb->u.list);
701 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
702 return seb;
703 }
704 }
705
706 ubi_err("no eraseblocks found");
707 return ERR_PTR(-ENOSPC);
708}
709
710/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300711 * process_eb - read, check UBI headers, and add them to scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400712 * @ubi: UBI device description object
713 * @si: scanning information
714 * @pnum: the physical eraseblock number
715 *
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300716 * This function returns a zero if the physical eraseblock was successfully
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400717 * handled and a negative error code in case of failure.
718 */
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300719static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
720 int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400721{
Artem Bityutskiyc18a8412008-01-24 11:19:14 +0200722 long long uninitialized_var(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400723 int err, bitflips = 0, vol_id, ec_corr = 0;
724
725 dbg_bld("scan PEB %d", pnum);
726
727 /* Skip bad physical eraseblocks */
728 err = ubi_io_is_bad(ubi, pnum);
729 if (err < 0)
730 return err;
731 else if (err) {
732 /*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300733 * FIXME: this is actually duty of the I/O sub-system to
734 * initialize this, but MTD does not provide enough
735 * information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400736 */
737 si->bad_peb_count += 1;
738 return 0;
739 }
740
741 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
742 if (err < 0)
743 return err;
744 else if (err == UBI_IO_BITFLIPS)
745 bitflips = 1;
746 else if (err == UBI_IO_PEB_EMPTY)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300747 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400748 else if (err == UBI_IO_BAD_EC_HDR) {
749 /*
750 * We have to also look at the VID header, possibly it is not
751 * corrupted. Set %bitflips flag in order to make this PEB be
752 * moved and EC be re-created.
753 */
754 ec_corr = 1;
755 ec = UBI_SCAN_UNKNOWN_EC;
756 bitflips = 1;
757 }
758
759 si->is_empty = 0;
760
761 if (!ec_corr) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300762 int image_seq;
763
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400764 /* Make sure UBI version is OK */
765 if (ech->version != UBI_VERSION) {
766 ubi_err("this UBI version is %d, image version is %d",
767 UBI_VERSION, (int)ech->version);
768 return -EINVAL;
769 }
770
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300771 ec = be64_to_cpu(ech->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400772 if (ec > UBI_MAX_ERASECOUNTER) {
773 /*
774 * Erase counter overflow. The EC headers have 64 bits
775 * reserved, but we anyway make use of only 31 bit
776 * values, as this seems to be enough for any existing
777 * flash. Upgrade UBI and use 64-bit erase counters
778 * internally.
779 */
780 ubi_err("erase counter overflow, max is %d",
781 UBI_MAX_ERASECOUNTER);
782 ubi_dbg_dump_ec_hdr(ech);
783 return -EINVAL;
784 }
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300785
Adrian Hunter32bc4822009-07-24 19:16:04 +0300786 /*
787 * Make sure that all PEBs have the same image sequence number.
788 * This allows us to detect situations when users flash UBI
789 * images incorrectly, so that the flash has the new UBI image
790 * and leftovers from the old one. This feature was added
791 * relatively recently, and the sequence number was always
792 * zero, because old UBI implementations always set it to zero.
793 * For this reasons, we do not panic if some PEBs have zero
794 * sequence number, while other PEBs have non-zero sequence
795 * number.
796 */
Holger Brunck3dc948d2009-07-13 16:47:57 +0200797 image_seq = be32_to_cpu(ech->image_seq);
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300798 if (!ubi->image_seq && image_seq)
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300799 ubi->image_seq = image_seq;
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300800 if (ubi->image_seq && image_seq &&
801 ubi->image_seq != image_seq) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300802 ubi_err("bad image sequence number %d in PEB %d, "
803 "expected %d", image_seq, pnum, ubi->image_seq);
804 ubi_dbg_dump_ec_hdr(ech);
805 return -EINVAL;
806 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400807 }
808
809 /* OK, we've done with the EC header, let's look at the VID header */
810
811 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
812 if (err < 0)
813 return err;
814 else if (err == UBI_IO_BITFLIPS)
815 bitflips = 1;
816 else if (err == UBI_IO_BAD_VID_HDR ||
817 (err == UBI_IO_PEB_FREE && ec_corr)) {
818 /* VID header is corrupted */
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300819 err = add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400820 if (err)
821 return err;
822 goto adjust_mean_ec;
823 } else if (err == UBI_IO_PEB_FREE) {
824 /* No VID header - the physical eraseblock is free */
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300825 err = add_to_list(si, pnum, ec, &si->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400826 if (err)
827 return err;
828 goto adjust_mean_ec;
829 }
830
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300831 vol_id = be32_to_cpu(vidh->vol_id);
Artem Bityutskiy91f2d532008-01-24 11:23:23 +0200832 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300833 int lnum = be32_to_cpu(vidh->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400834
835 /* Unsupported internal volume */
836 switch (vidh->compat) {
837 case UBI_COMPAT_DELETE:
838 ubi_msg("\"delete\" compatible internal volume %d:%d"
839 " found, remove it", vol_id, lnum);
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300840 err = add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400841 if (err)
842 return err;
843 break;
844
845 case UBI_COMPAT_RO:
846 ubi_msg("read-only compatible internal volume %d:%d"
847 " found, switch to read-only mode",
848 vol_id, lnum);
849 ubi->ro_mode = 1;
850 break;
851
852 case UBI_COMPAT_PRESERVE:
853 ubi_msg("\"preserve\" compatible internal volume %d:%d"
854 " found", vol_id, lnum);
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300855 err = add_to_list(si, pnum, ec, &si->alien);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400856 if (err)
857 return err;
858 si->alien_peb_count += 1;
859 return 0;
860
861 case UBI_COMPAT_REJECT:
862 ubi_err("incompatible internal volume %d:%d found",
863 vol_id, lnum);
864 return -EINVAL;
865 }
866 }
867
Artem Bityutskiy29a88c92009-07-19 14:03:16 +0300868 if (ec_corr)
869 ubi_warn("valid VID header but corrupted EC header at PEB %d",
870 pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400871 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
872 if (err)
873 return err;
874
875adjust_mean_ec:
876 if (!ec_corr) {
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +0300877 si->ec_sum += ec;
878 si->ec_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400879 if (ec > si->max_ec)
880 si->max_ec = ec;
881 if (ec < si->min_ec)
882 si->min_ec = ec;
883 }
884
885 return 0;
886}
887
888/**
889 * ubi_scan - scan an MTD device.
890 * @ubi: UBI device description object
891 *
892 * This function does full scanning of an MTD device and returns complete
893 * information about it. In case of failure, an error code is returned.
894 */
895struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
896{
897 int err, pnum;
898 struct rb_node *rb1, *rb2;
899 struct ubi_scan_volume *sv;
900 struct ubi_scan_leb *seb;
901 struct ubi_scan_info *si;
902
903 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
904 if (!si)
905 return ERR_PTR(-ENOMEM);
906
907 INIT_LIST_HEAD(&si->corr);
908 INIT_LIST_HEAD(&si->free);
909 INIT_LIST_HEAD(&si->erase);
910 INIT_LIST_HEAD(&si->alien);
911 si->volumes = RB_ROOT;
912 si->is_empty = 1;
913
914 err = -ENOMEM;
915 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
916 if (!ech)
917 goto out_si;
918
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300919 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400920 if (!vidh)
921 goto out_ech;
922
923 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
924 cond_resched();
925
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300926 dbg_gen("process PEB %d", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400927 err = process_eb(ubi, si, pnum);
928 if (err < 0)
929 goto out_vidh;
930 }
931
932 dbg_msg("scanning is finished");
933
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +0300934 /* Calculate mean erase counter */
Artem Bityutskiy3013ee32009-01-16 19:08:43 +0200935 if (si->ec_count)
936 si->mean_ec = div_u64(si->ec_sum, si->ec_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400937
938 if (si->is_empty)
939 ubi_msg("empty MTD device detected");
940
941 /*
Artem Bityutskiy4a406852009-07-19 14:33:14 +0300942 * Few corrupted PEBs are not a problem and may be just a result of
943 * unclean reboots. However, many of them may indicate some problems
944 * with the flash HW or driver. Print a warning in this case.
945 */
946 if (si->corr_count >= 8 || si->corr_count >= ubi->peb_count / 4) {
947 ubi_warn("%d PEBs are corrupted", si->corr_count);
948 printk(KERN_WARNING "corrupted PEBs are:");
949 list_for_each_entry(seb, &si->corr, u.list)
950 printk(KERN_CONT " %d", seb->pnum);
951 printk(KERN_CONT "\n");
952 }
953
954 /*
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400955 * In case of unknown erase counter we use the mean erase counter
956 * value.
957 */
958 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
959 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
960 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
961 seb->ec = si->mean_ec;
962 }
963
964 list_for_each_entry(seb, &si->free, u.list) {
965 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
966 seb->ec = si->mean_ec;
967 }
968
969 list_for_each_entry(seb, &si->corr, u.list)
970 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
971 seb->ec = si->mean_ec;
972
973 list_for_each_entry(seb, &si->erase, u.list)
974 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
975 seb->ec = si->mean_ec;
976
977 err = paranoid_check_si(ubi, si);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +0200978 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400979 goto out_vidh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400980
981 ubi_free_vid_hdr(ubi, vidh);
982 kfree(ech);
983
984 return si;
985
986out_vidh:
987 ubi_free_vid_hdr(ubi, vidh);
988out_ech:
989 kfree(ech);
990out_si:
991 ubi_scan_destroy_si(si);
992 return ERR_PTR(err);
993}
994
995/**
996 * destroy_sv - free the scanning volume information
997 * @sv: scanning volume information
998 *
999 * This function destroys the volume RB-tree (@sv->root) and the scanning
1000 * volume information.
1001 */
1002static void destroy_sv(struct ubi_scan_volume *sv)
1003{
1004 struct ubi_scan_leb *seb;
1005 struct rb_node *this = sv->root.rb_node;
1006
1007 while (this) {
1008 if (this->rb_left)
1009 this = this->rb_left;
1010 else if (this->rb_right)
1011 this = this->rb_right;
1012 else {
1013 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1014 this = rb_parent(this);
1015 if (this) {
1016 if (this->rb_left == &seb->u.rb)
1017 this->rb_left = NULL;
1018 else
1019 this->rb_right = NULL;
1020 }
1021
1022 kfree(seb);
1023 }
1024 }
1025 kfree(sv);
1026}
1027
1028/**
1029 * ubi_scan_destroy_si - destroy scanning information.
1030 * @si: scanning information
1031 */
1032void ubi_scan_destroy_si(struct ubi_scan_info *si)
1033{
1034 struct ubi_scan_leb *seb, *seb_tmp;
1035 struct ubi_scan_volume *sv;
1036 struct rb_node *rb;
1037
1038 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1039 list_del(&seb->u.list);
1040 kfree(seb);
1041 }
1042 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1043 list_del(&seb->u.list);
1044 kfree(seb);
1045 }
1046 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1047 list_del(&seb->u.list);
1048 kfree(seb);
1049 }
1050 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1051 list_del(&seb->u.list);
1052 kfree(seb);
1053 }
1054
1055 /* Destroy the volume RB-tree */
1056 rb = si->volumes.rb_node;
1057 while (rb) {
1058 if (rb->rb_left)
1059 rb = rb->rb_left;
1060 else if (rb->rb_right)
1061 rb = rb->rb_right;
1062 else {
1063 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1064
1065 rb = rb_parent(rb);
1066 if (rb) {
1067 if (rb->rb_left == &sv->rb)
1068 rb->rb_left = NULL;
1069 else
1070 rb->rb_right = NULL;
1071 }
1072
1073 destroy_sv(sv);
1074 }
1075 }
1076
1077 kfree(si);
1078}
1079
1080#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1081
1082/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001083 * paranoid_check_si - check the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001084 * @ubi: UBI device description object
1085 * @si: scanning information
1086 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001087 * This function returns zero if the scanning information is all right, and a
1088 * negative error code if not or if an error occurred.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001089 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001090static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001091{
1092 int pnum, err, vols_found = 0;
1093 struct rb_node *rb1, *rb2;
1094 struct ubi_scan_volume *sv;
1095 struct ubi_scan_leb *seb, *last_seb;
1096 uint8_t *buf;
1097
1098 /*
Artem Bityutskiy78d87c92007-05-05 11:24:02 +03001099 * At first, check that scanning information is OK.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001100 */
1101 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1102 int leb_count = 0;
1103
1104 cond_resched();
1105
1106 vols_found += 1;
1107
1108 if (si->is_empty) {
1109 ubi_err("bad is_empty flag");
1110 goto bad_sv;
1111 }
1112
1113 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1114 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1115 sv->data_pad < 0 || sv->last_data_size < 0) {
1116 ubi_err("negative values");
1117 goto bad_sv;
1118 }
1119
1120 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1121 sv->vol_id < UBI_INTERNAL_VOL_START) {
1122 ubi_err("bad vol_id");
1123 goto bad_sv;
1124 }
1125
1126 if (sv->vol_id > si->highest_vol_id) {
1127 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1128 si->highest_vol_id, sv->vol_id);
1129 goto out;
1130 }
1131
1132 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1133 sv->vol_type != UBI_STATIC_VOLUME) {
1134 ubi_err("bad vol_type");
1135 goto bad_sv;
1136 }
1137
1138 if (sv->data_pad > ubi->leb_size / 2) {
1139 ubi_err("bad data_pad");
1140 goto bad_sv;
1141 }
1142
1143 last_seb = NULL;
1144 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1145 cond_resched();
1146
1147 last_seb = seb;
1148 leb_count += 1;
1149
1150 if (seb->pnum < 0 || seb->ec < 0) {
1151 ubi_err("negative values");
1152 goto bad_seb;
1153 }
1154
1155 if (seb->ec < si->min_ec) {
1156 ubi_err("bad si->min_ec (%d), %d found",
1157 si->min_ec, seb->ec);
1158 goto bad_seb;
1159 }
1160
1161 if (seb->ec > si->max_ec) {
1162 ubi_err("bad si->max_ec (%d), %d found",
1163 si->max_ec, seb->ec);
1164 goto bad_seb;
1165 }
1166
1167 if (seb->pnum >= ubi->peb_count) {
1168 ubi_err("too high PEB number %d, total PEBs %d",
1169 seb->pnum, ubi->peb_count);
1170 goto bad_seb;
1171 }
1172
1173 if (sv->vol_type == UBI_STATIC_VOLUME) {
1174 if (seb->lnum >= sv->used_ebs) {
1175 ubi_err("bad lnum or used_ebs");
1176 goto bad_seb;
1177 }
1178 } else {
1179 if (sv->used_ebs != 0) {
1180 ubi_err("non-zero used_ebs");
1181 goto bad_seb;
1182 }
1183 }
1184
1185 if (seb->lnum > sv->highest_lnum) {
1186 ubi_err("incorrect highest_lnum or lnum");
1187 goto bad_seb;
1188 }
1189 }
1190
1191 if (sv->leb_count != leb_count) {
1192 ubi_err("bad leb_count, %d objects in the tree",
1193 leb_count);
1194 goto bad_sv;
1195 }
1196
1197 if (!last_seb)
1198 continue;
1199
1200 seb = last_seb;
1201
1202 if (seb->lnum != sv->highest_lnum) {
1203 ubi_err("bad highest_lnum");
1204 goto bad_seb;
1205 }
1206 }
1207
1208 if (vols_found != si->vols_found) {
1209 ubi_err("bad si->vols_found %d, should be %d",
1210 si->vols_found, vols_found);
1211 goto out;
1212 }
1213
1214 /* Check that scanning information is correct */
1215 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1216 last_seb = NULL;
1217 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1218 int vol_type;
1219
1220 cond_resched();
1221
1222 last_seb = seb;
1223
1224 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1225 if (err && err != UBI_IO_BITFLIPS) {
1226 ubi_err("VID header is not OK (%d)", err);
1227 if (err > 0)
1228 err = -EIO;
1229 return err;
1230 }
1231
1232 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1233 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1234 if (sv->vol_type != vol_type) {
1235 ubi_err("bad vol_type");
1236 goto bad_vid_hdr;
1237 }
1238
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001239 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001240 ubi_err("bad sqnum %llu", seb->sqnum);
1241 goto bad_vid_hdr;
1242 }
1243
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001244 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001245 ubi_err("bad vol_id %d", sv->vol_id);
1246 goto bad_vid_hdr;
1247 }
1248
1249 if (sv->compat != vidh->compat) {
1250 ubi_err("bad compat %d", vidh->compat);
1251 goto bad_vid_hdr;
1252 }
1253
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001254 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001255 ubi_err("bad lnum %d", seb->lnum);
1256 goto bad_vid_hdr;
1257 }
1258
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001259 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001260 ubi_err("bad used_ebs %d", sv->used_ebs);
1261 goto bad_vid_hdr;
1262 }
1263
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001264 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001265 ubi_err("bad data_pad %d", sv->data_pad);
1266 goto bad_vid_hdr;
1267 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001268 }
1269
1270 if (!last_seb)
1271 continue;
1272
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001273 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001274 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1275 goto bad_vid_hdr;
1276 }
1277
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001278 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001279 ubi_err("bad last_data_size %d", sv->last_data_size);
1280 goto bad_vid_hdr;
1281 }
1282 }
1283
1284 /*
1285 * Make sure that all the physical eraseblocks are in one of the lists
1286 * or trees.
1287 */
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001288 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001289 if (!buf)
1290 return -ENOMEM;
1291
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001292 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1293 err = ubi_io_is_bad(ubi, pnum);
Artem Bityutskiy341e1a02007-05-03 11:59:51 +03001294 if (err < 0) {
1295 kfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001296 return err;
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +03001297 } else if (err)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001298 buf[pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001299 }
1300
1301 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1302 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001303 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001304
1305 list_for_each_entry(seb, &si->free, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001306 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001307
1308 list_for_each_entry(seb, &si->corr, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001309 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001310
1311 list_for_each_entry(seb, &si->erase, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001312 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001313
1314 list_for_each_entry(seb, &si->alien, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001315 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001316
1317 err = 0;
1318 for (pnum = 0; pnum < ubi->peb_count; pnum++)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001319 if (!buf[pnum]) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001320 ubi_err("PEB %d is not referred", pnum);
1321 err = 1;
1322 }
1323
1324 kfree(buf);
1325 if (err)
1326 goto out;
1327 return 0;
1328
1329bad_seb:
1330 ubi_err("bad scanning information about LEB %d", seb->lnum);
1331 ubi_dbg_dump_seb(seb, 0);
1332 ubi_dbg_dump_sv(sv);
1333 goto out;
1334
1335bad_sv:
1336 ubi_err("bad scanning information about volume %d", sv->vol_id);
1337 ubi_dbg_dump_sv(sv);
1338 goto out;
1339
1340bad_vid_hdr:
1341 ubi_err("bad scanning information about volume %d", sv->vol_id);
1342 ubi_dbg_dump_sv(sv);
1343 ubi_dbg_dump_vid_hdr(vidh);
1344
1345out:
1346 ubi_dbg_dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001347 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001348}
1349
1350#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */