blob: 93361eadab8d4ad6ce58b3c150e02b90b3ae7b98 [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>
44#include <linux/crc32.h>
Artem Bityutskiy3013ee32009-01-16 19:08:43 +020045#include <linux/math64.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040046#include "ubi.h"
47
48#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
Artem Bityutskiye88d6e102007-08-29 14:51:52 +030049static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040050#else
51#define paranoid_check_si(ubi, si) 0
52#endif
53
54/* Temporary variables used during scanning */
55static struct ubi_ec_hdr *ech;
56static struct ubi_vid_hdr *vidh;
57
Artem Bityutskiy941dfb02007-05-05 16:33:13 +030058/**
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030059 * add_to_list - add physical eraseblock to a list.
60 * @si: scanning information
61 * @pnum: physical eraseblock number to add
62 * @ec: erase counter of the physical eraseblock
63 * @list: the list to add to
64 *
65 * This function adds physical eraseblock @pnum to free, erase, corrupted or
66 * alien lists. Returns zero in case of success and a negative error code in
67 * case of failure.
68 */
69static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
70 struct list_head *list)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040071{
72 struct ubi_scan_leb *seb;
73
74 if (list == &si->free)
75 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
76 else if (list == &si->erase)
77 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
78 else if (list == &si->corr)
79 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
80 else if (list == &si->alien)
81 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
82 else
83 BUG();
84
85 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
86 if (!seb)
87 return -ENOMEM;
88
89 seb->pnum = pnum;
90 seb->ec = ec;
91 list_add_tail(&seb->u.list, list);
92 return 0;
93}
94
95/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +030096 * validate_vid_hdr - check volume identifier header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040097 * @vid_hdr: the volume identifier header to check
98 * @sv: information about the volume this logical eraseblock belongs to
99 * @pnum: physical eraseblock number the VID header came from
100 *
101 * This function checks that data stored in @vid_hdr is consistent. Returns
102 * non-zero if an inconsistency was found and zero if not.
103 *
104 * Note, UBI does sanity check of everything it reads from the flash media.
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300105 * 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 +0400106 * information in the VID header is consistent to the information in other VID
107 * headers of the same volume.
108 */
109static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
110 const struct ubi_scan_volume *sv, int pnum)
111{
112 int vol_type = vid_hdr->vol_type;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300113 int vol_id = be32_to_cpu(vid_hdr->vol_id);
114 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
115 int data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400116
117 if (sv->leb_count != 0) {
118 int sv_vol_type;
119
120 /*
121 * This is not the first logical eraseblock belonging to this
122 * volume. Ensure that the data in its VID header is consistent
123 * to the data in previous logical eraseblock headers.
124 */
125
126 if (vol_id != sv->vol_id) {
127 dbg_err("inconsistent vol_id");
128 goto bad;
129 }
130
131 if (sv->vol_type == UBI_STATIC_VOLUME)
132 sv_vol_type = UBI_VID_STATIC;
133 else
134 sv_vol_type = UBI_VID_DYNAMIC;
135
136 if (vol_type != sv_vol_type) {
137 dbg_err("inconsistent vol_type");
138 goto bad;
139 }
140
141 if (used_ebs != sv->used_ebs) {
142 dbg_err("inconsistent used_ebs");
143 goto bad;
144 }
145
146 if (data_pad != sv->data_pad) {
147 dbg_err("inconsistent data_pad");
148 goto bad;
149 }
150 }
151
152 return 0;
153
154bad:
155 ubi_err("inconsistent VID header at PEB %d", pnum);
156 ubi_dbg_dump_vid_hdr(vid_hdr);
157 ubi_dbg_dump_sv(sv);
158 return -EINVAL;
159}
160
161/**
162 * add_volume - add volume to the scanning information.
163 * @si: scanning information
164 * @vol_id: ID of the volume to add
165 * @pnum: physical eraseblock number
166 * @vid_hdr: volume identifier header
167 *
168 * If the volume corresponding to the @vid_hdr logical eraseblock is already
169 * present in the scanning information, this function does nothing. Otherwise
170 * it adds corresponding volume to the scanning information. Returns a pointer
171 * to the scanning volume object in case of success and a negative error code
172 * in case of failure.
173 */
174static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
175 int pnum,
176 const struct ubi_vid_hdr *vid_hdr)
177{
178 struct ubi_scan_volume *sv;
179 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
180
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300181 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400182
183 /* Walk the volume RB-tree to look if this volume is already present */
184 while (*p) {
185 parent = *p;
186 sv = rb_entry(parent, struct ubi_scan_volume, rb);
187
188 if (vol_id == sv->vol_id)
189 return sv;
190
191 if (vol_id > sv->vol_id)
192 p = &(*p)->rb_left;
193 else
194 p = &(*p)->rb_right;
195 }
196
197 /* The volume is absent - add it */
198 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
199 if (!sv)
200 return ERR_PTR(-ENOMEM);
201
202 sv->highest_lnum = sv->leb_count = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400203 sv->vol_id = vol_id;
204 sv->root = RB_ROOT;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300205 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
206 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400207 sv->compat = vid_hdr->compat;
208 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
209 : UBI_STATIC_VOLUME;
210 if (vol_id > si->highest_vol_id)
211 si->highest_vol_id = vol_id;
212
213 rb_link_node(&sv->rb, parent, p);
214 rb_insert_color(&sv->rb, &si->volumes);
215 si->vols_found += 1;
216 dbg_bld("added volume %d", vol_id);
217 return sv;
218}
219
220/**
221 * compare_lebs - find out which logical eraseblock is newer.
222 * @ubi: UBI device description object
223 * @seb: first logical eraseblock to compare
224 * @pnum: physical eraseblock number of the second logical eraseblock to
225 * compare
226 * @vid_hdr: volume identifier header of the second logical eraseblock
227 *
228 * This function compares 2 copies of a LEB and informs which one is newer. In
229 * case of success this function returns a positive value, in case of failure, a
230 * negative error code is returned. The success return codes use the following
231 * bits:
232 * o bit 0 is cleared: the first PEB (described by @seb) is newer then the
233 * second PEB (described by @pnum and @vid_hdr);
234 * o bit 0 is set: the second PEB is newer;
235 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
236 * o bit 1 is set: bit-flips were detected in the newer LEB;
237 * o bit 2 is cleared: the older LEB is not corrupted;
238 * o bit 2 is set: the older LEB is corrupted.
239 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300240static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
241 int pnum, const struct ubi_vid_hdr *vid_hdr)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400242{
243 void *buf;
244 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
245 uint32_t data_crc, crc;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300246 struct ubi_vid_hdr *vh = NULL;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300247 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400248
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300249 if (sqnum2 == seb->sqnum) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400250 /*
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300251 * This must be a really ancient UBI image which has been
252 * created before sequence numbers support has been added. At
253 * that times we used 32-bit LEB versions stored in logical
254 * eraseblocks. That was before UBI got into mainline. We do not
255 * support these images anymore. Well, those images will work
256 * still work, but only if no unclean reboots happened.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400257 */
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300258 ubi_err("unsupported on-flash UBI format\n");
259 return -EINVAL;
260 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400261
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300262 /* Obviously the LEB with lower sequence counter is older */
263 second_is_newer = !!(sqnum2 > seb->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400264
265 /*
266 * Now we know which copy is newer. If the copy flag of the PEB with
267 * newer version is not set, then we just return, otherwise we have to
268 * check data CRC. For the second PEB we already have the VID header,
269 * for the first one - we'll need to re-read it from flash.
270 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300271 * Note: this may be optimized so that we wouldn't read twice.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400272 */
273
274 if (second_is_newer) {
275 if (!vid_hdr->copy_flag) {
276 /* It is not a copy, so it is newer */
277 dbg_bld("second PEB %d is newer, copy_flag is unset",
278 pnum);
279 return 1;
280 }
281 } else {
282 pnum = seb->pnum;
283
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300284 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300285 if (!vh)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400286 return -ENOMEM;
287
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300288 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400289 if (err) {
290 if (err == UBI_IO_BITFLIPS)
291 bitflips = 1;
292 else {
293 dbg_err("VID of PEB %d header is bad, but it "
294 "was OK earlier", pnum);
295 if (err > 0)
296 err = -EIO;
297
298 goto out_free_vidh;
299 }
300 }
301
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300302 if (!vh->copy_flag) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400303 /* It is not a copy, so it is newer */
304 dbg_bld("first PEB %d is newer, copy_flag is unset",
305 pnum);
306 err = bitflips << 1;
307 goto out_free_vidh;
308 }
309
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300310 vid_hdr = vh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400311 }
312
313 /* Read the data of the copy and check the CRC */
314
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300315 len = be32_to_cpu(vid_hdr->data_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300316 buf = vmalloc(len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400317 if (!buf) {
318 err = -ENOMEM;
319 goto out_free_vidh;
320 }
321
322 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
Zoltan Sogorb77bcb02008-10-29 09:50:02 +0100323 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400324 goto out_free_buf;
325
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300326 data_crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400327 crc = crc32(UBI_CRC32_INIT, buf, len);
328 if (crc != data_crc) {
329 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
330 pnum, crc, data_crc);
331 corrupted = 1;
332 bitflips = 0;
333 second_is_newer = !second_is_newer;
334 } else {
335 dbg_bld("PEB %d CRC is OK", pnum);
336 bitflips = !!err;
337 }
338
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300339 vfree(buf);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300340 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400341
342 if (second_is_newer)
343 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
344 else
345 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
346
347 return second_is_newer | (bitflips << 1) | (corrupted << 2);
348
349out_free_buf:
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300350 vfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400351out_free_vidh:
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300352 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400353 return err;
354}
355
356/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300357 * ubi_scan_add_used - add physical eraseblock to the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400358 * @ubi: UBI device description object
359 * @si: scanning information
360 * @pnum: the physical eraseblock number
361 * @ec: erase counter
362 * @vid_hdr: the volume identifier header
363 * @bitflips: if bit-flips were detected when this physical eraseblock was read
364 *
Artem Bityutskiy79b510c2007-05-05 17:36:17 +0300365 * This function adds information about a used physical eraseblock to the
366 * 'used' tree of the corresponding volume. The function is rather complex
367 * because it has to handle cases when this is not the first physical
368 * eraseblock belonging to the same logical eraseblock, and the newer one has
369 * to be picked, while the older one has to be dropped. This function returns
370 * zero in case of success and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400371 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300372int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400373 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
374 int bitflips)
375{
376 int err, vol_id, lnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400377 unsigned long long sqnum;
378 struct ubi_scan_volume *sv;
379 struct ubi_scan_leb *seb;
380 struct rb_node **p, *parent = NULL;
381
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300382 vol_id = be32_to_cpu(vid_hdr->vol_id);
383 lnum = be32_to_cpu(vid_hdr->lnum);
384 sqnum = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400385
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300386 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
387 pnum, vol_id, lnum, ec, sqnum, bitflips);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400388
389 sv = add_volume(si, vol_id, pnum, vid_hdr);
Julien Brunel0e4a0082008-09-26 15:27:25 +0200390 if (IS_ERR(sv))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400391 return PTR_ERR(sv);
392
Brijesh Singh76eafe42007-07-06 14:35:43 +0300393 if (si->max_sqnum < sqnum)
394 si->max_sqnum = sqnum;
395
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400396 /*
397 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
398 * if this is the first instance of this logical eraseblock or not.
399 */
400 p = &sv->root.rb_node;
401 while (*p) {
402 int cmp_res;
403
404 parent = *p;
405 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
406 if (lnum != seb->lnum) {
407 if (lnum < seb->lnum)
408 p = &(*p)->rb_left;
409 else
410 p = &(*p)->rb_right;
411 continue;
412 }
413
414 /*
415 * There is already a physical eraseblock describing the same
416 * logical eraseblock present.
417 */
418
419 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300420 "EC %d", seb->pnum, seb->sqnum, seb->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400421
422 /*
423 * Make sure that the logical eraseblocks have different
424 * sequence numbers. Otherwise the image is bad.
425 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300426 * However, if the sequence number is zero, we assume it must
427 * be an ancient UBI image from the era when UBI did not have
428 * sequence numbers. We still can attach these images, unless
429 * there is a need to distinguish between old and new
430 * eraseblocks, in which case we'll refuse the image in
431 * 'compare_lebs()'. In other words, we attach old clean
432 * images, but refuse attaching old images with duplicated
433 * logical eraseblocks because there was an unclean reboot.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400434 */
435 if (seb->sqnum == sqnum && sqnum != 0) {
436 ubi_err("two LEBs with same sequence number %llu",
437 sqnum);
438 ubi_dbg_dump_seb(seb, 0);
439 ubi_dbg_dump_vid_hdr(vid_hdr);
440 return -EINVAL;
441 }
442
443 /*
444 * Now we have to drop the older one and preserve the newer
445 * one.
446 */
447 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
448 if (cmp_res < 0)
449 return cmp_res;
450
451 if (cmp_res & 1) {
452 /*
453 * This logical eraseblock is newer then the one
454 * found earlier.
455 */
456 err = validate_vid_hdr(vid_hdr, sv, pnum);
457 if (err)
458 return err;
459
460 if (cmp_res & 4)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300461 err = add_to_list(si, seb->pnum, seb->ec,
462 &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400463 else
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300464 err = add_to_list(si, seb->pnum, seb->ec,
465 &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400466 if (err)
467 return err;
468
469 seb->ec = ec;
470 seb->pnum = pnum;
471 seb->scrub = ((cmp_res & 2) || bitflips);
472 seb->sqnum = sqnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400473
474 if (sv->highest_lnum == lnum)
475 sv->last_data_size =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300476 be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400477
478 return 0;
479 } else {
480 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200481 * This logical eraseblock is older than the one found
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400482 * previously.
483 */
484 if (cmp_res & 4)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300485 return add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400486 else
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300487 return add_to_list(si, pnum, ec, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400488 }
489 }
490
491 /*
492 * We've met this logical eraseblock for the first time, add it to the
493 * scanning information.
494 */
495
496 err = validate_vid_hdr(vid_hdr, sv, pnum);
497 if (err)
498 return err;
499
500 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
501 if (!seb)
502 return -ENOMEM;
503
504 seb->ec = ec;
505 seb->pnum = pnum;
506 seb->lnum = lnum;
507 seb->sqnum = sqnum;
508 seb->scrub = bitflips;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400509
510 if (sv->highest_lnum <= lnum) {
511 sv->highest_lnum = lnum;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300512 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400513 }
514
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400515 sv->leb_count += 1;
516 rb_link_node(&seb->u.rb, parent, p);
517 rb_insert_color(&seb->u.rb, &sv->root);
518 return 0;
519}
520
521/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300522 * ubi_scan_find_sv - find volume in the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400523 * @si: scanning information
524 * @vol_id: the requested volume ID
525 *
526 * This function returns a pointer to the volume description or %NULL if there
527 * are no data about this volume in the scanning information.
528 */
529struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
530 int vol_id)
531{
532 struct ubi_scan_volume *sv;
533 struct rb_node *p = si->volumes.rb_node;
534
535 while (p) {
536 sv = rb_entry(p, struct ubi_scan_volume, rb);
537
538 if (vol_id == sv->vol_id)
539 return sv;
540
541 if (vol_id > sv->vol_id)
542 p = p->rb_left;
543 else
544 p = p->rb_right;
545 }
546
547 return NULL;
548}
549
550/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300551 * ubi_scan_find_seb - find LEB in the volume scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400552 * @sv: a pointer to the volume scanning information
553 * @lnum: the requested logical eraseblock
554 *
555 * This function returns a pointer to the scanning logical eraseblock or %NULL
556 * if there are no data about it in the scanning volume information.
557 */
558struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
559 int lnum)
560{
561 struct ubi_scan_leb *seb;
562 struct rb_node *p = sv->root.rb_node;
563
564 while (p) {
565 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
566
567 if (lnum == seb->lnum)
568 return seb;
569
570 if (lnum > seb->lnum)
571 p = p->rb_left;
572 else
573 p = p->rb_right;
574 }
575
576 return NULL;
577}
578
579/**
580 * ubi_scan_rm_volume - delete scanning information about a volume.
581 * @si: scanning information
582 * @sv: the volume scanning information to delete
583 */
584void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
585{
586 struct rb_node *rb;
587 struct ubi_scan_leb *seb;
588
589 dbg_bld("remove scanning information about volume %d", sv->vol_id);
590
591 while ((rb = rb_first(&sv->root))) {
592 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
593 rb_erase(&seb->u.rb, &sv->root);
594 list_add_tail(&seb->u.list, &si->erase);
595 }
596
597 rb_erase(&sv->rb, &si->volumes);
598 kfree(sv);
599 si->vols_found -= 1;
600}
601
602/**
603 * ubi_scan_erase_peb - erase a physical eraseblock.
604 * @ubi: UBI device description object
605 * @si: scanning information
606 * @pnum: physical eraseblock number to erase;
607 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
608 *
609 * This function erases physical eraseblock 'pnum', and writes the erase
610 * counter header to it. This function should only be used on UBI device
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300611 * initialization stages, when the EBA sub-system had not been yet initialized.
612 * This function returns zero in case of success and a negative error code in
613 * case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400614 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300615int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
616 int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400617{
618 int err;
619 struct ubi_ec_hdr *ec_hdr;
620
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400621 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
622 /*
623 * Erase counter overflow. Upgrade UBI and use 64-bit
624 * erase counters internally.
625 */
626 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
627 return -EINVAL;
628 }
629
Florin Malitadcec4c32007-07-19 15:22:41 -0400630 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
631 if (!ec_hdr)
632 return -ENOMEM;
633
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300634 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400635
636 err = ubi_io_sync_erase(ubi, pnum, 0);
637 if (err < 0)
638 goto out_free;
639
640 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
641
642out_free:
643 kfree(ec_hdr);
644 return err;
645}
646
647/**
648 * ubi_scan_get_free_peb - get a free physical eraseblock.
649 * @ubi: UBI device description object
650 * @si: scanning information
651 *
652 * This function returns a free physical eraseblock. It is supposed to be
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300653 * called on the UBI initialization stages when the wear-leveling sub-system is
654 * not initialized yet. This function picks a physical eraseblocks from one of
655 * the lists, writes the EC header if it is needed, and removes it from the
656 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400657 *
658 * This function returns scanning physical eraseblock information in case of
659 * success and an error code in case of failure.
660 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300661struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400662 struct ubi_scan_info *si)
663{
664 int err = 0, i;
665 struct ubi_scan_leb *seb;
666
667 if (!list_empty(&si->free)) {
668 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
669 list_del(&seb->u.list);
670 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
671 return seb;
672 }
673
674 for (i = 0; i < 2; i++) {
675 struct list_head *head;
676 struct ubi_scan_leb *tmp_seb;
677
678 if (i == 0)
679 head = &si->erase;
680 else
681 head = &si->corr;
682
683 /*
684 * We try to erase the first physical eraseblock from the @head
685 * list and pick it if we succeed, or try to erase the
686 * next one if not. And so forth. We don't want to take care
687 * about bad eraseblocks here - they'll be handled later.
688 */
689 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
690 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
691 seb->ec = si->mean_ec;
692
693 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
694 if (err)
695 continue;
696
697 seb->ec += 1;
698 list_del(&seb->u.list);
699 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
700 return seb;
701 }
702 }
703
704 ubi_err("no eraseblocks found");
705 return ERR_PTR(-ENOSPC);
706}
707
708/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300709 * process_eb - read, check UBI headers, and add them to scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400710 * @ubi: UBI device description object
711 * @si: scanning information
712 * @pnum: the physical eraseblock number
713 *
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300714 * This function returns a zero if the physical eraseblock was successfully
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400715 * handled and a negative error code in case of failure.
716 */
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300717static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
718 int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400719{
Artem Bityutskiyc18a8412008-01-24 11:19:14 +0200720 long long uninitialized_var(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400721 int err, bitflips = 0, vol_id, ec_corr = 0;
722
723 dbg_bld("scan PEB %d", pnum);
724
725 /* Skip bad physical eraseblocks */
726 err = ubi_io_is_bad(ubi, pnum);
727 if (err < 0)
728 return err;
729 else if (err) {
730 /*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300731 * FIXME: this is actually duty of the I/O sub-system to
732 * initialize this, but MTD does not provide enough
733 * information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400734 */
735 si->bad_peb_count += 1;
736 return 0;
737 }
738
739 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
740 if (err < 0)
741 return err;
742 else if (err == UBI_IO_BITFLIPS)
743 bitflips = 1;
744 else if (err == UBI_IO_PEB_EMPTY)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300745 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400746 else if (err == UBI_IO_BAD_EC_HDR) {
747 /*
748 * We have to also look at the VID header, possibly it is not
749 * corrupted. Set %bitflips flag in order to make this PEB be
750 * moved and EC be re-created.
751 */
752 ec_corr = 1;
753 ec = UBI_SCAN_UNKNOWN_EC;
754 bitflips = 1;
755 }
756
757 si->is_empty = 0;
758
759 if (!ec_corr) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300760 int image_seq;
761
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400762 /* Make sure UBI version is OK */
763 if (ech->version != UBI_VERSION) {
764 ubi_err("this UBI version is %d, image version is %d",
765 UBI_VERSION, (int)ech->version);
766 return -EINVAL;
767 }
768
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300769 ec = be64_to_cpu(ech->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400770 if (ec > UBI_MAX_ERASECOUNTER) {
771 /*
772 * Erase counter overflow. The EC headers have 64 bits
773 * reserved, but we anyway make use of only 31 bit
774 * values, as this seems to be enough for any existing
775 * flash. Upgrade UBI and use 64-bit erase counters
776 * internally.
777 */
778 ubi_err("erase counter overflow, max is %d",
779 UBI_MAX_ERASECOUNTER);
780 ubi_dbg_dump_ec_hdr(ech);
781 return -EINVAL;
782 }
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300783
Adrian Hunter32bc4822009-07-24 19:16:04 +0300784 /*
785 * Make sure that all PEBs have the same image sequence number.
786 * This allows us to detect situations when users flash UBI
787 * images incorrectly, so that the flash has the new UBI image
788 * and leftovers from the old one. This feature was added
789 * relatively recently, and the sequence number was always
790 * zero, because old UBI implementations always set it to zero.
791 * For this reasons, we do not panic if some PEBs have zero
792 * sequence number, while other PEBs have non-zero sequence
793 * number.
794 */
Holger Brunck3dc948d2009-07-13 16:47:57 +0200795 image_seq = be32_to_cpu(ech->image_seq);
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300796 if (!si->image_seq_set) {
797 ubi->image_seq = image_seq;
798 si->image_seq_set = 1;
Adrian Hunter32bc4822009-07-24 19:16:04 +0300799 } else if (ubi->image_seq && ubi->image_seq != image_seq) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300800 ubi_err("bad image sequence number %d in PEB %d, "
801 "expected %d", image_seq, pnum, ubi->image_seq);
802 ubi_dbg_dump_ec_hdr(ech);
803 return -EINVAL;
804 }
805
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400806 }
807
808 /* OK, we've done with the EC header, let's look at the VID header */
809
810 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
811 if (err < 0)
812 return err;
813 else if (err == UBI_IO_BITFLIPS)
814 bitflips = 1;
815 else if (err == UBI_IO_BAD_VID_HDR ||
816 (err == UBI_IO_PEB_FREE && ec_corr)) {
817 /* VID header is corrupted */
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300818 err = add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400819 if (err)
820 return err;
821 goto adjust_mean_ec;
822 } else if (err == UBI_IO_PEB_FREE) {
823 /* No VID header - the physical eraseblock is free */
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300824 err = add_to_list(si, pnum, ec, &si->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400825 if (err)
826 return err;
827 goto adjust_mean_ec;
828 }
829
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300830 vol_id = be32_to_cpu(vidh->vol_id);
Artem Bityutskiy91f2d532008-01-24 11:23:23 +0200831 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300832 int lnum = be32_to_cpu(vidh->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400833
834 /* Unsupported internal volume */
835 switch (vidh->compat) {
836 case UBI_COMPAT_DELETE:
837 ubi_msg("\"delete\" compatible internal volume %d:%d"
838 " found, remove it", vol_id, lnum);
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300839 err = add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400840 if (err)
841 return err;
842 break;
843
844 case UBI_COMPAT_RO:
845 ubi_msg("read-only compatible internal volume %d:%d"
846 " found, switch to read-only mode",
847 vol_id, lnum);
848 ubi->ro_mode = 1;
849 break;
850
851 case UBI_COMPAT_PRESERVE:
852 ubi_msg("\"preserve\" compatible internal volume %d:%d"
853 " found", vol_id, lnum);
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300854 err = add_to_list(si, pnum, ec, &si->alien);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400855 if (err)
856 return err;
857 si->alien_peb_count += 1;
858 return 0;
859
860 case UBI_COMPAT_REJECT:
861 ubi_err("incompatible internal volume %d:%d found",
862 vol_id, lnum);
863 return -EINVAL;
864 }
865 }
866
Artem Bityutskiy29a88c92009-07-19 14:03:16 +0300867 if (ec_corr)
868 ubi_warn("valid VID header but corrupted EC header at PEB %d",
869 pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400870 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
871 if (err)
872 return err;
873
874adjust_mean_ec:
875 if (!ec_corr) {
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +0300876 si->ec_sum += ec;
877 si->ec_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400878 if (ec > si->max_ec)
879 si->max_ec = ec;
880 if (ec < si->min_ec)
881 si->min_ec = ec;
882 }
883
884 return 0;
885}
886
887/**
888 * ubi_scan - scan an MTD device.
889 * @ubi: UBI device description object
890 *
891 * This function does full scanning of an MTD device and returns complete
892 * information about it. In case of failure, an error code is returned.
893 */
894struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
895{
896 int err, pnum;
897 struct rb_node *rb1, *rb2;
898 struct ubi_scan_volume *sv;
899 struct ubi_scan_leb *seb;
900 struct ubi_scan_info *si;
901
902 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
903 if (!si)
904 return ERR_PTR(-ENOMEM);
905
906 INIT_LIST_HEAD(&si->corr);
907 INIT_LIST_HEAD(&si->free);
908 INIT_LIST_HEAD(&si->erase);
909 INIT_LIST_HEAD(&si->alien);
910 si->volumes = RB_ROOT;
911 si->is_empty = 1;
912
913 err = -ENOMEM;
914 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
915 if (!ech)
916 goto out_si;
917
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300918 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400919 if (!vidh)
920 goto out_ech;
921
922 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
923 cond_resched();
924
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300925 dbg_gen("process PEB %d", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400926 err = process_eb(ubi, si, pnum);
927 if (err < 0)
928 goto out_vidh;
929 }
930
931 dbg_msg("scanning is finished");
932
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +0300933 /* Calculate mean erase counter */
Artem Bityutskiy3013ee32009-01-16 19:08:43 +0200934 if (si->ec_count)
935 si->mean_ec = div_u64(si->ec_sum, si->ec_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400936
937 if (si->is_empty)
938 ubi_msg("empty MTD device detected");
939
940 /*
941 * In case of unknown erase counter we use the mean erase counter
942 * value.
943 */
944 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
945 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
946 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
947 seb->ec = si->mean_ec;
948 }
949
950 list_for_each_entry(seb, &si->free, u.list) {
951 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
952 seb->ec = si->mean_ec;
953 }
954
955 list_for_each_entry(seb, &si->corr, u.list)
956 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
957 seb->ec = si->mean_ec;
958
959 list_for_each_entry(seb, &si->erase, u.list)
960 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
961 seb->ec = si->mean_ec;
962
963 err = paranoid_check_si(ubi, si);
964 if (err) {
965 if (err > 0)
966 err = -EINVAL;
967 goto out_vidh;
968 }
969
970 ubi_free_vid_hdr(ubi, vidh);
971 kfree(ech);
972
973 return si;
974
975out_vidh:
976 ubi_free_vid_hdr(ubi, vidh);
977out_ech:
978 kfree(ech);
979out_si:
980 ubi_scan_destroy_si(si);
981 return ERR_PTR(err);
982}
983
984/**
985 * destroy_sv - free the scanning volume information
986 * @sv: scanning volume information
987 *
988 * This function destroys the volume RB-tree (@sv->root) and the scanning
989 * volume information.
990 */
991static void destroy_sv(struct ubi_scan_volume *sv)
992{
993 struct ubi_scan_leb *seb;
994 struct rb_node *this = sv->root.rb_node;
995
996 while (this) {
997 if (this->rb_left)
998 this = this->rb_left;
999 else if (this->rb_right)
1000 this = this->rb_right;
1001 else {
1002 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1003 this = rb_parent(this);
1004 if (this) {
1005 if (this->rb_left == &seb->u.rb)
1006 this->rb_left = NULL;
1007 else
1008 this->rb_right = NULL;
1009 }
1010
1011 kfree(seb);
1012 }
1013 }
1014 kfree(sv);
1015}
1016
1017/**
1018 * ubi_scan_destroy_si - destroy scanning information.
1019 * @si: scanning information
1020 */
1021void ubi_scan_destroy_si(struct ubi_scan_info *si)
1022{
1023 struct ubi_scan_leb *seb, *seb_tmp;
1024 struct ubi_scan_volume *sv;
1025 struct rb_node *rb;
1026
1027 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1028 list_del(&seb->u.list);
1029 kfree(seb);
1030 }
1031 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1032 list_del(&seb->u.list);
1033 kfree(seb);
1034 }
1035 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1036 list_del(&seb->u.list);
1037 kfree(seb);
1038 }
1039 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1040 list_del(&seb->u.list);
1041 kfree(seb);
1042 }
1043
1044 /* Destroy the volume RB-tree */
1045 rb = si->volumes.rb_node;
1046 while (rb) {
1047 if (rb->rb_left)
1048 rb = rb->rb_left;
1049 else if (rb->rb_right)
1050 rb = rb->rb_right;
1051 else {
1052 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1053
1054 rb = rb_parent(rb);
1055 if (rb) {
1056 if (rb->rb_left == &sv->rb)
1057 rb->rb_left = NULL;
1058 else
1059 rb->rb_right = NULL;
1060 }
1061
1062 destroy_sv(sv);
1063 }
1064 }
1065
1066 kfree(si);
1067}
1068
1069#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1070
1071/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001072 * paranoid_check_si - check the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001073 * @ubi: UBI device description object
1074 * @si: scanning information
1075 *
1076 * This function returns zero if the scanning information is all right, %1 if
1077 * not and a negative error code if an error occurred.
1078 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001079static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001080{
1081 int pnum, err, vols_found = 0;
1082 struct rb_node *rb1, *rb2;
1083 struct ubi_scan_volume *sv;
1084 struct ubi_scan_leb *seb, *last_seb;
1085 uint8_t *buf;
1086
1087 /*
Artem Bityutskiy78d87c92007-05-05 11:24:02 +03001088 * At first, check that scanning information is OK.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001089 */
1090 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1091 int leb_count = 0;
1092
1093 cond_resched();
1094
1095 vols_found += 1;
1096
1097 if (si->is_empty) {
1098 ubi_err("bad is_empty flag");
1099 goto bad_sv;
1100 }
1101
1102 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1103 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1104 sv->data_pad < 0 || sv->last_data_size < 0) {
1105 ubi_err("negative values");
1106 goto bad_sv;
1107 }
1108
1109 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1110 sv->vol_id < UBI_INTERNAL_VOL_START) {
1111 ubi_err("bad vol_id");
1112 goto bad_sv;
1113 }
1114
1115 if (sv->vol_id > si->highest_vol_id) {
1116 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1117 si->highest_vol_id, sv->vol_id);
1118 goto out;
1119 }
1120
1121 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1122 sv->vol_type != UBI_STATIC_VOLUME) {
1123 ubi_err("bad vol_type");
1124 goto bad_sv;
1125 }
1126
1127 if (sv->data_pad > ubi->leb_size / 2) {
1128 ubi_err("bad data_pad");
1129 goto bad_sv;
1130 }
1131
1132 last_seb = NULL;
1133 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1134 cond_resched();
1135
1136 last_seb = seb;
1137 leb_count += 1;
1138
1139 if (seb->pnum < 0 || seb->ec < 0) {
1140 ubi_err("negative values");
1141 goto bad_seb;
1142 }
1143
1144 if (seb->ec < si->min_ec) {
1145 ubi_err("bad si->min_ec (%d), %d found",
1146 si->min_ec, seb->ec);
1147 goto bad_seb;
1148 }
1149
1150 if (seb->ec > si->max_ec) {
1151 ubi_err("bad si->max_ec (%d), %d found",
1152 si->max_ec, seb->ec);
1153 goto bad_seb;
1154 }
1155
1156 if (seb->pnum >= ubi->peb_count) {
1157 ubi_err("too high PEB number %d, total PEBs %d",
1158 seb->pnum, ubi->peb_count);
1159 goto bad_seb;
1160 }
1161
1162 if (sv->vol_type == UBI_STATIC_VOLUME) {
1163 if (seb->lnum >= sv->used_ebs) {
1164 ubi_err("bad lnum or used_ebs");
1165 goto bad_seb;
1166 }
1167 } else {
1168 if (sv->used_ebs != 0) {
1169 ubi_err("non-zero used_ebs");
1170 goto bad_seb;
1171 }
1172 }
1173
1174 if (seb->lnum > sv->highest_lnum) {
1175 ubi_err("incorrect highest_lnum or lnum");
1176 goto bad_seb;
1177 }
1178 }
1179
1180 if (sv->leb_count != leb_count) {
1181 ubi_err("bad leb_count, %d objects in the tree",
1182 leb_count);
1183 goto bad_sv;
1184 }
1185
1186 if (!last_seb)
1187 continue;
1188
1189 seb = last_seb;
1190
1191 if (seb->lnum != sv->highest_lnum) {
1192 ubi_err("bad highest_lnum");
1193 goto bad_seb;
1194 }
1195 }
1196
1197 if (vols_found != si->vols_found) {
1198 ubi_err("bad si->vols_found %d, should be %d",
1199 si->vols_found, vols_found);
1200 goto out;
1201 }
1202
1203 /* Check that scanning information is correct */
1204 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1205 last_seb = NULL;
1206 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1207 int vol_type;
1208
1209 cond_resched();
1210
1211 last_seb = seb;
1212
1213 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1214 if (err && err != UBI_IO_BITFLIPS) {
1215 ubi_err("VID header is not OK (%d)", err);
1216 if (err > 0)
1217 err = -EIO;
1218 return err;
1219 }
1220
1221 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1222 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1223 if (sv->vol_type != vol_type) {
1224 ubi_err("bad vol_type");
1225 goto bad_vid_hdr;
1226 }
1227
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001228 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001229 ubi_err("bad sqnum %llu", seb->sqnum);
1230 goto bad_vid_hdr;
1231 }
1232
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001233 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001234 ubi_err("bad vol_id %d", sv->vol_id);
1235 goto bad_vid_hdr;
1236 }
1237
1238 if (sv->compat != vidh->compat) {
1239 ubi_err("bad compat %d", vidh->compat);
1240 goto bad_vid_hdr;
1241 }
1242
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001243 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001244 ubi_err("bad lnum %d", seb->lnum);
1245 goto bad_vid_hdr;
1246 }
1247
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001248 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001249 ubi_err("bad used_ebs %d", sv->used_ebs);
1250 goto bad_vid_hdr;
1251 }
1252
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001253 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001254 ubi_err("bad data_pad %d", sv->data_pad);
1255 goto bad_vid_hdr;
1256 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001257 }
1258
1259 if (!last_seb)
1260 continue;
1261
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001262 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001263 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1264 goto bad_vid_hdr;
1265 }
1266
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001267 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001268 ubi_err("bad last_data_size %d", sv->last_data_size);
1269 goto bad_vid_hdr;
1270 }
1271 }
1272
1273 /*
1274 * Make sure that all the physical eraseblocks are in one of the lists
1275 * or trees.
1276 */
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001277 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001278 if (!buf)
1279 return -ENOMEM;
1280
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001281 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1282 err = ubi_io_is_bad(ubi, pnum);
Artem Bityutskiy341e1a02007-05-03 11:59:51 +03001283 if (err < 0) {
1284 kfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001285 return err;
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +03001286 } else if (err)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001287 buf[pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001288 }
1289
1290 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1291 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001292 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001293
1294 list_for_each_entry(seb, &si->free, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001295 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001296
1297 list_for_each_entry(seb, &si->corr, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001298 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001299
1300 list_for_each_entry(seb, &si->erase, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001301 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001302
1303 list_for_each_entry(seb, &si->alien, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001304 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001305
1306 err = 0;
1307 for (pnum = 0; pnum < ubi->peb_count; pnum++)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001308 if (!buf[pnum]) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001309 ubi_err("PEB %d is not referred", pnum);
1310 err = 1;
1311 }
1312
1313 kfree(buf);
1314 if (err)
1315 goto out;
1316 return 0;
1317
1318bad_seb:
1319 ubi_err("bad scanning information about LEB %d", seb->lnum);
1320 ubi_dbg_dump_seb(seb, 0);
1321 ubi_dbg_dump_sv(sv);
1322 goto out;
1323
1324bad_sv:
1325 ubi_err("bad scanning information about volume %d", sv->vol_id);
1326 ubi_dbg_dump_sv(sv);
1327 goto out;
1328
1329bad_vid_hdr:
1330 ubi_err("bad scanning information about volume %d", sv->vol_id);
1331 ubi_dbg_dump_sv(sv);
1332 ubi_dbg_dump_vid_hdr(vidh);
1333
1334out:
1335 ubi_dbg_dump_stack();
1336 return 1;
1337}
1338
1339#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */