blob: a20f278d0c6caf1425468d4dc71df8fdb2cc7be4 [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
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030075 if (list == &si->free) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040076 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030077 si->free_peb_count += 1;
78 } else if (list == &si->erase) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040079 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030080 si->erase_peb_count += 1;
81 } else if (list == &si->corr) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040082 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030083 si->corr_peb_count += 1;
84 } else if (list == &si->alien) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040085 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030086 si->alien_peb_count += 1;
87 } else
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040088 BUG();
89
90 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
91 if (!seb)
92 return -ENOMEM;
93
94 seb->pnum = pnum;
95 seb->ec = ec;
96 list_add_tail(&seb->u.list, list);
97 return 0;
98}
99
100/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300101 * validate_vid_hdr - check volume identifier header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400102 * @vid_hdr: the volume identifier header to check
103 * @sv: information about the volume this logical eraseblock belongs to
104 * @pnum: physical eraseblock number the VID header came from
105 *
106 * This function checks that data stored in @vid_hdr is consistent. Returns
107 * non-zero if an inconsistency was found and zero if not.
108 *
109 * Note, UBI does sanity check of everything it reads from the flash media.
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300110 * 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 +0400111 * information in the VID header is consistent to the information in other VID
112 * headers of the same volume.
113 */
114static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
115 const struct ubi_scan_volume *sv, int pnum)
116{
117 int vol_type = vid_hdr->vol_type;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300118 int vol_id = be32_to_cpu(vid_hdr->vol_id);
119 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
120 int data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400121
122 if (sv->leb_count != 0) {
123 int sv_vol_type;
124
125 /*
126 * This is not the first logical eraseblock belonging to this
127 * volume. Ensure that the data in its VID header is consistent
128 * to the data in previous logical eraseblock headers.
129 */
130
131 if (vol_id != sv->vol_id) {
132 dbg_err("inconsistent vol_id");
133 goto bad;
134 }
135
136 if (sv->vol_type == UBI_STATIC_VOLUME)
137 sv_vol_type = UBI_VID_STATIC;
138 else
139 sv_vol_type = UBI_VID_DYNAMIC;
140
141 if (vol_type != sv_vol_type) {
142 dbg_err("inconsistent vol_type");
143 goto bad;
144 }
145
146 if (used_ebs != sv->used_ebs) {
147 dbg_err("inconsistent used_ebs");
148 goto bad;
149 }
150
151 if (data_pad != sv->data_pad) {
152 dbg_err("inconsistent data_pad");
153 goto bad;
154 }
155 }
156
157 return 0;
158
159bad:
160 ubi_err("inconsistent VID header at PEB %d", pnum);
161 ubi_dbg_dump_vid_hdr(vid_hdr);
162 ubi_dbg_dump_sv(sv);
163 return -EINVAL;
164}
165
166/**
167 * add_volume - add volume to the scanning information.
168 * @si: scanning information
169 * @vol_id: ID of the volume to add
170 * @pnum: physical eraseblock number
171 * @vid_hdr: volume identifier header
172 *
173 * If the volume corresponding to the @vid_hdr logical eraseblock is already
174 * present in the scanning information, this function does nothing. Otherwise
175 * it adds corresponding volume to the scanning information. Returns a pointer
176 * to the scanning volume object in case of success and a negative error code
177 * in case of failure.
178 */
179static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
180 int pnum,
181 const struct ubi_vid_hdr *vid_hdr)
182{
183 struct ubi_scan_volume *sv;
184 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
185
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300186 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400187
188 /* Walk the volume RB-tree to look if this volume is already present */
189 while (*p) {
190 parent = *p;
191 sv = rb_entry(parent, struct ubi_scan_volume, rb);
192
193 if (vol_id == sv->vol_id)
194 return sv;
195
196 if (vol_id > sv->vol_id)
197 p = &(*p)->rb_left;
198 else
199 p = &(*p)->rb_right;
200 }
201
202 /* The volume is absent - add it */
203 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
204 if (!sv)
205 return ERR_PTR(-ENOMEM);
206
207 sv->highest_lnum = sv->leb_count = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400208 sv->vol_id = vol_id;
209 sv->root = RB_ROOT;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300210 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
211 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400212 sv->compat = vid_hdr->compat;
213 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
214 : UBI_STATIC_VOLUME;
215 if (vol_id > si->highest_vol_id)
216 si->highest_vol_id = vol_id;
217
218 rb_link_node(&sv->rb, parent, p);
219 rb_insert_color(&sv->rb, &si->volumes);
220 si->vols_found += 1;
221 dbg_bld("added volume %d", vol_id);
222 return sv;
223}
224
225/**
226 * compare_lebs - find out which logical eraseblock is newer.
227 * @ubi: UBI device description object
228 * @seb: first logical eraseblock to compare
229 * @pnum: physical eraseblock number of the second logical eraseblock to
230 * compare
231 * @vid_hdr: volume identifier header of the second logical eraseblock
232 *
233 * This function compares 2 copies of a LEB and informs which one is newer. In
234 * case of success this function returns a positive value, in case of failure, a
235 * negative error code is returned. The success return codes use the following
236 * bits:
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900237 * o bit 0 is cleared: the first PEB (described by @seb) is newer than the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400238 * second PEB (described by @pnum and @vid_hdr);
239 * o bit 0 is set: the second PEB is newer;
240 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
241 * o bit 1 is set: bit-flips were detected in the newer LEB;
242 * o bit 2 is cleared: the older LEB is not corrupted;
243 * o bit 2 is set: the older LEB is corrupted.
244 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300245static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
246 int pnum, const struct ubi_vid_hdr *vid_hdr)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400247{
248 void *buf;
249 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
250 uint32_t data_crc, crc;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300251 struct ubi_vid_hdr *vh = NULL;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300252 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400253
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300254 if (sqnum2 == seb->sqnum) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400255 /*
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300256 * This must be a really ancient UBI image which has been
257 * created before sequence numbers support has been added. At
258 * that times we used 32-bit LEB versions stored in logical
259 * eraseblocks. That was before UBI got into mainline. We do not
260 * support these images anymore. Well, those images will work
261 * still work, but only if no unclean reboots happened.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400262 */
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300263 ubi_err("unsupported on-flash UBI format\n");
264 return -EINVAL;
265 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400266
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300267 /* Obviously the LEB with lower sequence counter is older */
268 second_is_newer = !!(sqnum2 > seb->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400269
270 /*
271 * Now we know which copy is newer. If the copy flag of the PEB with
272 * newer version is not set, then we just return, otherwise we have to
273 * check data CRC. For the second PEB we already have the VID header,
274 * for the first one - we'll need to re-read it from flash.
275 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300276 * Note: this may be optimized so that we wouldn't read twice.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400277 */
278
279 if (second_is_newer) {
280 if (!vid_hdr->copy_flag) {
281 /* It is not a copy, so it is newer */
282 dbg_bld("second PEB %d is newer, copy_flag is unset",
283 pnum);
284 return 1;
285 }
286 } else {
287 pnum = seb->pnum;
288
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300289 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300290 if (!vh)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400291 return -ENOMEM;
292
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300293 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400294 if (err) {
295 if (err == UBI_IO_BITFLIPS)
296 bitflips = 1;
297 else {
298 dbg_err("VID of PEB %d header is bad, but it "
299 "was OK earlier", pnum);
300 if (err > 0)
301 err = -EIO;
302
303 goto out_free_vidh;
304 }
305 }
306
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300307 if (!vh->copy_flag) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400308 /* It is not a copy, so it is newer */
309 dbg_bld("first PEB %d is newer, copy_flag is unset",
310 pnum);
311 err = bitflips << 1;
312 goto out_free_vidh;
313 }
314
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300315 vid_hdr = vh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400316 }
317
318 /* Read the data of the copy and check the CRC */
319
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300320 len = be32_to_cpu(vid_hdr->data_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300321 buf = vmalloc(len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400322 if (!buf) {
323 err = -ENOMEM;
324 goto out_free_vidh;
325 }
326
327 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
Zoltan Sogorb77bcb02008-10-29 09:50:02 +0100328 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400329 goto out_free_buf;
330
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300331 data_crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400332 crc = crc32(UBI_CRC32_INIT, buf, len);
333 if (crc != data_crc) {
334 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
335 pnum, crc, data_crc);
336 corrupted = 1;
337 bitflips = 0;
338 second_is_newer = !second_is_newer;
339 } else {
340 dbg_bld("PEB %d CRC is OK", pnum);
341 bitflips = !!err;
342 }
343
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300344 vfree(buf);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300345 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400346
347 if (second_is_newer)
348 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
349 else
350 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
351
352 return second_is_newer | (bitflips << 1) | (corrupted << 2);
353
354out_free_buf:
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300355 vfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400356out_free_vidh:
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300357 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400358 return err;
359}
360
361/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300362 * ubi_scan_add_used - add physical eraseblock to the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400363 * @ubi: UBI device description object
364 * @si: scanning information
365 * @pnum: the physical eraseblock number
366 * @ec: erase counter
367 * @vid_hdr: the volume identifier header
368 * @bitflips: if bit-flips were detected when this physical eraseblock was read
369 *
Artem Bityutskiy79b510c2007-05-05 17:36:17 +0300370 * This function adds information about a used physical eraseblock to the
371 * 'used' tree of the corresponding volume. The function is rather complex
372 * because it has to handle cases when this is not the first physical
373 * eraseblock belonging to the same logical eraseblock, and the newer one has
374 * to be picked, while the older one has to be dropped. This function returns
375 * zero in case of success and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400376 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300377int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400378 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
379 int bitflips)
380{
381 int err, vol_id, lnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400382 unsigned long long sqnum;
383 struct ubi_scan_volume *sv;
384 struct ubi_scan_leb *seb;
385 struct rb_node **p, *parent = NULL;
386
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300387 vol_id = be32_to_cpu(vid_hdr->vol_id);
388 lnum = be32_to_cpu(vid_hdr->lnum);
389 sqnum = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400390
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300391 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
392 pnum, vol_id, lnum, ec, sqnum, bitflips);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400393
394 sv = add_volume(si, vol_id, pnum, vid_hdr);
Julien Brunel0e4a0082008-09-26 15:27:25 +0200395 if (IS_ERR(sv))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400396 return PTR_ERR(sv);
397
Brijesh Singh76eafe42007-07-06 14:35:43 +0300398 if (si->max_sqnum < sqnum)
399 si->max_sqnum = sqnum;
400
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400401 /*
402 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
403 * if this is the first instance of this logical eraseblock or not.
404 */
405 p = &sv->root.rb_node;
406 while (*p) {
407 int cmp_res;
408
409 parent = *p;
410 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
411 if (lnum != seb->lnum) {
412 if (lnum < seb->lnum)
413 p = &(*p)->rb_left;
414 else
415 p = &(*p)->rb_right;
416 continue;
417 }
418
419 /*
420 * There is already a physical eraseblock describing the same
421 * logical eraseblock present.
422 */
423
424 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300425 "EC %d", seb->pnum, seb->sqnum, seb->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400426
427 /*
428 * Make sure that the logical eraseblocks have different
429 * sequence numbers. Otherwise the image is bad.
430 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300431 * However, if the sequence number is zero, we assume it must
432 * be an ancient UBI image from the era when UBI did not have
433 * sequence numbers. We still can attach these images, unless
434 * there is a need to distinguish between old and new
435 * eraseblocks, in which case we'll refuse the image in
436 * 'compare_lebs()'. In other words, we attach old clean
437 * images, but refuse attaching old images with duplicated
438 * logical eraseblocks because there was an unclean reboot.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400439 */
440 if (seb->sqnum == sqnum && sqnum != 0) {
441 ubi_err("two LEBs with same sequence number %llu",
442 sqnum);
443 ubi_dbg_dump_seb(seb, 0);
444 ubi_dbg_dump_vid_hdr(vid_hdr);
445 return -EINVAL;
446 }
447
448 /*
449 * Now we have to drop the older one and preserve the newer
450 * one.
451 */
452 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
453 if (cmp_res < 0)
454 return cmp_res;
455
456 if (cmp_res & 1) {
457 /*
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900458 * This logical eraseblock is newer than the one
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400459 * found earlier.
460 */
461 err = validate_vid_hdr(vid_hdr, sv, pnum);
462 if (err)
463 return err;
464
465 if (cmp_res & 4)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300466 err = add_to_list(si, seb->pnum, seb->ec,
467 &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400468 else
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300469 err = add_to_list(si, seb->pnum, seb->ec,
470 &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400471 if (err)
472 return err;
473
474 seb->ec = ec;
475 seb->pnum = pnum;
476 seb->scrub = ((cmp_res & 2) || bitflips);
477 seb->sqnum = sqnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400478
479 if (sv->highest_lnum == lnum)
480 sv->last_data_size =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300481 be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400482
483 return 0;
484 } else {
485 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200486 * This logical eraseblock is older than the one found
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400487 * previously.
488 */
489 if (cmp_res & 4)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300490 return add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400491 else
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300492 return add_to_list(si, pnum, ec, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400493 }
494 }
495
496 /*
497 * We've met this logical eraseblock for the first time, add it to the
498 * scanning information.
499 */
500
501 err = validate_vid_hdr(vid_hdr, sv, pnum);
502 if (err)
503 return err;
504
505 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
506 if (!seb)
507 return -ENOMEM;
508
509 seb->ec = ec;
510 seb->pnum = pnum;
511 seb->lnum = lnum;
512 seb->sqnum = sqnum;
513 seb->scrub = bitflips;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400514
515 if (sv->highest_lnum <= lnum) {
516 sv->highest_lnum = lnum;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300517 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400518 }
519
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400520 sv->leb_count += 1;
521 rb_link_node(&seb->u.rb, parent, p);
522 rb_insert_color(&seb->u.rb, &sv->root);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300523 si->used_peb_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400524 return 0;
525}
526
527/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300528 * ubi_scan_find_sv - find volume in the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400529 * @si: scanning information
530 * @vol_id: the requested volume ID
531 *
532 * This function returns a pointer to the volume description or %NULL if there
533 * are no data about this volume in the scanning information.
534 */
535struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
536 int vol_id)
537{
538 struct ubi_scan_volume *sv;
539 struct rb_node *p = si->volumes.rb_node;
540
541 while (p) {
542 sv = rb_entry(p, struct ubi_scan_volume, rb);
543
544 if (vol_id == sv->vol_id)
545 return sv;
546
547 if (vol_id > sv->vol_id)
548 p = p->rb_left;
549 else
550 p = p->rb_right;
551 }
552
553 return NULL;
554}
555
556/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300557 * ubi_scan_find_seb - find LEB in the volume scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400558 * @sv: a pointer to the volume scanning information
559 * @lnum: the requested logical eraseblock
560 *
561 * This function returns a pointer to the scanning logical eraseblock or %NULL
562 * if there are no data about it in the scanning volume information.
563 */
564struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
565 int lnum)
566{
567 struct ubi_scan_leb *seb;
568 struct rb_node *p = sv->root.rb_node;
569
570 while (p) {
571 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
572
573 if (lnum == seb->lnum)
574 return seb;
575
576 if (lnum > seb->lnum)
577 p = p->rb_left;
578 else
579 p = p->rb_right;
580 }
581
582 return NULL;
583}
584
585/**
586 * ubi_scan_rm_volume - delete scanning information about a volume.
587 * @si: scanning information
588 * @sv: the volume scanning information to delete
589 */
590void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
591{
592 struct rb_node *rb;
593 struct ubi_scan_leb *seb;
594
595 dbg_bld("remove scanning information about volume %d", sv->vol_id);
596
597 while ((rb = rb_first(&sv->root))) {
598 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
599 rb_erase(&seb->u.rb, &sv->root);
600 list_add_tail(&seb->u.list, &si->erase);
601 }
602
603 rb_erase(&sv->rb, &si->volumes);
604 kfree(sv);
605 si->vols_found -= 1;
606}
607
608/**
609 * ubi_scan_erase_peb - erase a physical eraseblock.
610 * @ubi: UBI device description object
611 * @si: scanning information
612 * @pnum: physical eraseblock number to erase;
613 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
614 *
615 * This function erases physical eraseblock 'pnum', and writes the erase
616 * counter header to it. This function should only be used on UBI device
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300617 * initialization stages, when the EBA sub-system had not been yet initialized.
618 * This function returns zero in case of success and a negative error code in
619 * case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400620 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300621int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
622 int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400623{
624 int err;
625 struct ubi_ec_hdr *ec_hdr;
626
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400627 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
628 /*
629 * Erase counter overflow. Upgrade UBI and use 64-bit
630 * erase counters internally.
631 */
632 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
633 return -EINVAL;
634 }
635
Florin Malitadcec4c32007-07-19 15:22:41 -0400636 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
637 if (!ec_hdr)
638 return -ENOMEM;
639
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300640 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400641
642 err = ubi_io_sync_erase(ubi, pnum, 0);
643 if (err < 0)
644 goto out_free;
645
646 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
647
648out_free:
649 kfree(ec_hdr);
650 return err;
651}
652
653/**
654 * ubi_scan_get_free_peb - get a free physical eraseblock.
655 * @ubi: UBI device description object
656 * @si: scanning information
657 *
658 * This function returns a free physical eraseblock. It is supposed to be
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300659 * called on the UBI initialization stages when the wear-leveling sub-system is
660 * not initialized yet. This function picks a physical eraseblocks from one of
661 * the lists, writes the EC header if it is needed, and removes it from the
662 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400663 *
664 * This function returns scanning physical eraseblock information in case of
665 * success and an error code in case of failure.
666 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300667struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400668 struct ubi_scan_info *si)
669{
670 int err = 0, i;
671 struct ubi_scan_leb *seb;
672
673 if (!list_empty(&si->free)) {
674 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
675 list_del(&seb->u.list);
676 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
677 return seb;
678 }
679
680 for (i = 0; i < 2; i++) {
681 struct list_head *head;
682 struct ubi_scan_leb *tmp_seb;
683
684 if (i == 0)
685 head = &si->erase;
686 else
687 head = &si->corr;
688
689 /*
690 * We try to erase the first physical eraseblock from the @head
691 * list and pick it if we succeed, or try to erase the
692 * next one if not. And so forth. We don't want to take care
693 * about bad eraseblocks here - they'll be handled later.
694 */
695 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
696 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
697 seb->ec = si->mean_ec;
698
699 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
700 if (err)
701 continue;
702
703 seb->ec += 1;
704 list_del(&seb->u.list);
705 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
706 return seb;
707 }
708 }
709
710 ubi_err("no eraseblocks found");
711 return ERR_PTR(-ENOSPC);
712}
713
714/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300715 * process_eb - read, check UBI headers, and add them to scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400716 * @ubi: UBI device description object
717 * @si: scanning information
718 * @pnum: the physical eraseblock number
719 *
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300720 * This function returns a zero if the physical eraseblock was successfully
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400721 * handled and a negative error code in case of failure.
722 */
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300723static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
724 int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400725{
Artem Bityutskiyc18a8412008-01-24 11:19:14 +0200726 long long uninitialized_var(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400727 int err, bitflips = 0, vol_id, ec_corr = 0;
728
729 dbg_bld("scan PEB %d", pnum);
730
731 /* Skip bad physical eraseblocks */
732 err = ubi_io_is_bad(ubi, pnum);
733 if (err < 0)
734 return err;
735 else if (err) {
736 /*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300737 * FIXME: this is actually duty of the I/O sub-system to
738 * initialize this, but MTD does not provide enough
739 * information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400740 */
741 si->bad_peb_count += 1;
742 return 0;
743 }
744
745 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
746 if (err < 0)
747 return err;
748 else if (err == UBI_IO_BITFLIPS)
749 bitflips = 1;
750 else if (err == UBI_IO_PEB_EMPTY)
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300751 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
Artem Bityutskiyeb895802010-05-03 09:04:39 +0300752 else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400753 /*
754 * We have to also look at the VID header, possibly it is not
755 * corrupted. Set %bitflips flag in order to make this PEB be
756 * moved and EC be re-created.
757 */
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300758 ec_corr = err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400759 ec = UBI_SCAN_UNKNOWN_EC;
760 bitflips = 1;
761 }
762
763 si->is_empty = 0;
764
765 if (!ec_corr) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300766 int image_seq;
767
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400768 /* Make sure UBI version is OK */
769 if (ech->version != UBI_VERSION) {
770 ubi_err("this UBI version is %d, image version is %d",
771 UBI_VERSION, (int)ech->version);
772 return -EINVAL;
773 }
774
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300775 ec = be64_to_cpu(ech->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400776 if (ec > UBI_MAX_ERASECOUNTER) {
777 /*
778 * Erase counter overflow. The EC headers have 64 bits
779 * reserved, but we anyway make use of only 31 bit
780 * values, as this seems to be enough for any existing
781 * flash. Upgrade UBI and use 64-bit erase counters
782 * internally.
783 */
784 ubi_err("erase counter overflow, max is %d",
785 UBI_MAX_ERASECOUNTER);
786 ubi_dbg_dump_ec_hdr(ech);
787 return -EINVAL;
788 }
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300789
Adrian Hunter32bc4822009-07-24 19:16:04 +0300790 /*
791 * Make sure that all PEBs have the same image sequence number.
792 * This allows us to detect situations when users flash UBI
793 * images incorrectly, so that the flash has the new UBI image
794 * and leftovers from the old one. This feature was added
795 * relatively recently, and the sequence number was always
796 * zero, because old UBI implementations always set it to zero.
797 * For this reasons, we do not panic if some PEBs have zero
798 * sequence number, while other PEBs have non-zero sequence
799 * number.
800 */
Holger Brunck3dc948d2009-07-13 16:47:57 +0200801 image_seq = be32_to_cpu(ech->image_seq);
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300802 if (!ubi->image_seq && image_seq)
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300803 ubi->image_seq = image_seq;
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300804 if (ubi->image_seq && image_seq &&
805 ubi->image_seq != image_seq) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300806 ubi_err("bad image sequence number %d in PEB %d, "
807 "expected %d", image_seq, pnum, ubi->image_seq);
808 ubi_dbg_dump_ec_hdr(ech);
809 return -EINVAL;
810 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400811 }
812
813 /* OK, we've done with the EC header, let's look at the VID header */
814
815 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
816 if (err < 0)
817 return err;
818 else if (err == UBI_IO_BITFLIPS)
819 bitflips = 1;
Artem Bityutskiyeb895802010-05-03 09:04:39 +0300820 else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR ||
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400821 (err == UBI_IO_PEB_FREE && ec_corr)) {
822 /* VID header is corrupted */
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300823 if (err == UBI_IO_BAD_HDR_READ ||
824 ec_corr == UBI_IO_BAD_HDR_READ)
825 si->read_err_count += 1;
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300826 err = add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400827 if (err)
828 return err;
829 goto adjust_mean_ec;
830 } else if (err == UBI_IO_PEB_FREE) {
831 /* No VID header - the physical eraseblock is free */
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300832 err = add_to_list(si, pnum, ec, &si->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400833 if (err)
834 return err;
835 goto adjust_mean_ec;
836 }
837
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300838 vol_id = be32_to_cpu(vidh->vol_id);
Artem Bityutskiy91f2d532008-01-24 11:23:23 +0200839 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300840 int lnum = be32_to_cpu(vidh->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400841
842 /* Unsupported internal volume */
843 switch (vidh->compat) {
844 case UBI_COMPAT_DELETE:
845 ubi_msg("\"delete\" compatible internal volume %d:%d"
846 " found, remove it", vol_id, lnum);
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300847 err = add_to_list(si, pnum, ec, &si->corr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400848 if (err)
849 return err;
850 break;
851
852 case UBI_COMPAT_RO:
853 ubi_msg("read-only compatible internal volume %d:%d"
854 " found, switch to read-only mode",
855 vol_id, lnum);
856 ubi->ro_mode = 1;
857 break;
858
859 case UBI_COMPAT_PRESERVE:
860 ubi_msg("\"preserve\" compatible internal volume %d:%d"
861 " found", vol_id, lnum);
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300862 err = add_to_list(si, pnum, ec, &si->alien);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400863 if (err)
864 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400865 return 0;
866
867 case UBI_COMPAT_REJECT:
868 ubi_err("incompatible internal volume %d:%d found",
869 vol_id, lnum);
870 return -EINVAL;
871 }
872 }
873
Artem Bityutskiy29a88c92009-07-19 14:03:16 +0300874 if (ec_corr)
875 ubi_warn("valid VID header but corrupted EC header at PEB %d",
876 pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400877 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
878 if (err)
879 return err;
880
881adjust_mean_ec:
882 if (!ec_corr) {
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +0300883 si->ec_sum += ec;
884 si->ec_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400885 if (ec > si->max_ec)
886 si->max_ec = ec;
887 if (ec < si->min_ec)
888 si->min_ec = ec;
889 }
890
891 return 0;
892}
893
894/**
895 * ubi_scan - scan an MTD device.
896 * @ubi: UBI device description object
897 *
898 * This function does full scanning of an MTD device and returns complete
899 * information about it. In case of failure, an error code is returned.
900 */
901struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
902{
903 int err, pnum;
904 struct rb_node *rb1, *rb2;
905 struct ubi_scan_volume *sv;
906 struct ubi_scan_leb *seb;
907 struct ubi_scan_info *si;
908
909 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
910 if (!si)
911 return ERR_PTR(-ENOMEM);
912
913 INIT_LIST_HEAD(&si->corr);
914 INIT_LIST_HEAD(&si->free);
915 INIT_LIST_HEAD(&si->erase);
916 INIT_LIST_HEAD(&si->alien);
917 si->volumes = RB_ROOT;
918 si->is_empty = 1;
919
920 err = -ENOMEM;
921 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
922 if (!ech)
923 goto out_si;
924
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300925 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400926 if (!vidh)
927 goto out_ech;
928
929 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
930 cond_resched();
931
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300932 dbg_gen("process PEB %d", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400933 err = process_eb(ubi, si, pnum);
934 if (err < 0)
935 goto out_vidh;
936 }
937
938 dbg_msg("scanning is finished");
939
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +0300940 /* Calculate mean erase counter */
Artem Bityutskiy3013ee32009-01-16 19:08:43 +0200941 if (si->ec_count)
942 si->mean_ec = div_u64(si->ec_sum, si->ec_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400943
944 if (si->is_empty)
945 ubi_msg("empty MTD device detected");
946
947 /*
Artem Bityutskiy4a406852009-07-19 14:33:14 +0300948 * Few corrupted PEBs are not a problem and may be just a result of
949 * unclean reboots. However, many of them may indicate some problems
950 * with the flash HW or driver. Print a warning in this case.
951 */
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300952 if (si->corr_peb_count >= 8 ||
953 si->corr_peb_count >= ubi->peb_count / 4) {
954 ubi_warn("%d PEBs are corrupted", si->corr_peb_count);
Artem Bityutskiy4a406852009-07-19 14:33:14 +0300955 printk(KERN_WARNING "corrupted PEBs are:");
956 list_for_each_entry(seb, &si->corr, u.list)
957 printk(KERN_CONT " %d", seb->pnum);
958 printk(KERN_CONT "\n");
959 }
960
961 /*
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400962 * In case of unknown erase counter we use the mean erase counter
963 * value.
964 */
965 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
966 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
967 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
968 seb->ec = si->mean_ec;
969 }
970
971 list_for_each_entry(seb, &si->free, u.list) {
972 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
973 seb->ec = si->mean_ec;
974 }
975
976 list_for_each_entry(seb, &si->corr, u.list)
977 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
978 seb->ec = si->mean_ec;
979
980 list_for_each_entry(seb, &si->erase, u.list)
981 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
982 seb->ec = si->mean_ec;
983
984 err = paranoid_check_si(ubi, si);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +0200985 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400986 goto out_vidh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400987
988 ubi_free_vid_hdr(ubi, vidh);
989 kfree(ech);
990
991 return si;
992
993out_vidh:
994 ubi_free_vid_hdr(ubi, vidh);
995out_ech:
996 kfree(ech);
997out_si:
998 ubi_scan_destroy_si(si);
999 return ERR_PTR(err);
1000}
1001
1002/**
1003 * destroy_sv - free the scanning volume information
1004 * @sv: scanning volume information
1005 *
1006 * This function destroys the volume RB-tree (@sv->root) and the scanning
1007 * volume information.
1008 */
1009static void destroy_sv(struct ubi_scan_volume *sv)
1010{
1011 struct ubi_scan_leb *seb;
1012 struct rb_node *this = sv->root.rb_node;
1013
1014 while (this) {
1015 if (this->rb_left)
1016 this = this->rb_left;
1017 else if (this->rb_right)
1018 this = this->rb_right;
1019 else {
1020 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1021 this = rb_parent(this);
1022 if (this) {
1023 if (this->rb_left == &seb->u.rb)
1024 this->rb_left = NULL;
1025 else
1026 this->rb_right = NULL;
1027 }
1028
1029 kfree(seb);
1030 }
1031 }
1032 kfree(sv);
1033}
1034
1035/**
1036 * ubi_scan_destroy_si - destroy scanning information.
1037 * @si: scanning information
1038 */
1039void ubi_scan_destroy_si(struct ubi_scan_info *si)
1040{
1041 struct ubi_scan_leb *seb, *seb_tmp;
1042 struct ubi_scan_volume *sv;
1043 struct rb_node *rb;
1044
1045 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1046 list_del(&seb->u.list);
1047 kfree(seb);
1048 }
1049 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1050 list_del(&seb->u.list);
1051 kfree(seb);
1052 }
1053 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1054 list_del(&seb->u.list);
1055 kfree(seb);
1056 }
1057 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1058 list_del(&seb->u.list);
1059 kfree(seb);
1060 }
1061
1062 /* Destroy the volume RB-tree */
1063 rb = si->volumes.rb_node;
1064 while (rb) {
1065 if (rb->rb_left)
1066 rb = rb->rb_left;
1067 else if (rb->rb_right)
1068 rb = rb->rb_right;
1069 else {
1070 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1071
1072 rb = rb_parent(rb);
1073 if (rb) {
1074 if (rb->rb_left == &sv->rb)
1075 rb->rb_left = NULL;
1076 else
1077 rb->rb_right = NULL;
1078 }
1079
1080 destroy_sv(sv);
1081 }
1082 }
1083
1084 kfree(si);
1085}
1086
1087#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1088
1089/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001090 * paranoid_check_si - check the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001091 * @ubi: UBI device description object
1092 * @si: scanning information
1093 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001094 * This function returns zero if the scanning information is all right, and a
1095 * negative error code if not or if an error occurred.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001096 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001097static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001098{
1099 int pnum, err, vols_found = 0;
1100 struct rb_node *rb1, *rb2;
1101 struct ubi_scan_volume *sv;
1102 struct ubi_scan_leb *seb, *last_seb;
1103 uint8_t *buf;
1104
1105 /*
Artem Bityutskiy78d87c92007-05-05 11:24:02 +03001106 * At first, check that scanning information is OK.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001107 */
1108 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1109 int leb_count = 0;
1110
1111 cond_resched();
1112
1113 vols_found += 1;
1114
1115 if (si->is_empty) {
1116 ubi_err("bad is_empty flag");
1117 goto bad_sv;
1118 }
1119
1120 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1121 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1122 sv->data_pad < 0 || sv->last_data_size < 0) {
1123 ubi_err("negative values");
1124 goto bad_sv;
1125 }
1126
1127 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1128 sv->vol_id < UBI_INTERNAL_VOL_START) {
1129 ubi_err("bad vol_id");
1130 goto bad_sv;
1131 }
1132
1133 if (sv->vol_id > si->highest_vol_id) {
1134 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1135 si->highest_vol_id, sv->vol_id);
1136 goto out;
1137 }
1138
1139 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1140 sv->vol_type != UBI_STATIC_VOLUME) {
1141 ubi_err("bad vol_type");
1142 goto bad_sv;
1143 }
1144
1145 if (sv->data_pad > ubi->leb_size / 2) {
1146 ubi_err("bad data_pad");
1147 goto bad_sv;
1148 }
1149
1150 last_seb = NULL;
1151 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1152 cond_resched();
1153
1154 last_seb = seb;
1155 leb_count += 1;
1156
1157 if (seb->pnum < 0 || seb->ec < 0) {
1158 ubi_err("negative values");
1159 goto bad_seb;
1160 }
1161
1162 if (seb->ec < si->min_ec) {
1163 ubi_err("bad si->min_ec (%d), %d found",
1164 si->min_ec, seb->ec);
1165 goto bad_seb;
1166 }
1167
1168 if (seb->ec > si->max_ec) {
1169 ubi_err("bad si->max_ec (%d), %d found",
1170 si->max_ec, seb->ec);
1171 goto bad_seb;
1172 }
1173
1174 if (seb->pnum >= ubi->peb_count) {
1175 ubi_err("too high PEB number %d, total PEBs %d",
1176 seb->pnum, ubi->peb_count);
1177 goto bad_seb;
1178 }
1179
1180 if (sv->vol_type == UBI_STATIC_VOLUME) {
1181 if (seb->lnum >= sv->used_ebs) {
1182 ubi_err("bad lnum or used_ebs");
1183 goto bad_seb;
1184 }
1185 } else {
1186 if (sv->used_ebs != 0) {
1187 ubi_err("non-zero used_ebs");
1188 goto bad_seb;
1189 }
1190 }
1191
1192 if (seb->lnum > sv->highest_lnum) {
1193 ubi_err("incorrect highest_lnum or lnum");
1194 goto bad_seb;
1195 }
1196 }
1197
1198 if (sv->leb_count != leb_count) {
1199 ubi_err("bad leb_count, %d objects in the tree",
1200 leb_count);
1201 goto bad_sv;
1202 }
1203
1204 if (!last_seb)
1205 continue;
1206
1207 seb = last_seb;
1208
1209 if (seb->lnum != sv->highest_lnum) {
1210 ubi_err("bad highest_lnum");
1211 goto bad_seb;
1212 }
1213 }
1214
1215 if (vols_found != si->vols_found) {
1216 ubi_err("bad si->vols_found %d, should be %d",
1217 si->vols_found, vols_found);
1218 goto out;
1219 }
1220
1221 /* Check that scanning information is correct */
1222 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1223 last_seb = NULL;
1224 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1225 int vol_type;
1226
1227 cond_resched();
1228
1229 last_seb = seb;
1230
1231 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1232 if (err && err != UBI_IO_BITFLIPS) {
1233 ubi_err("VID header is not OK (%d)", err);
1234 if (err > 0)
1235 err = -EIO;
1236 return err;
1237 }
1238
1239 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1240 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1241 if (sv->vol_type != vol_type) {
1242 ubi_err("bad vol_type");
1243 goto bad_vid_hdr;
1244 }
1245
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001246 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001247 ubi_err("bad sqnum %llu", seb->sqnum);
1248 goto bad_vid_hdr;
1249 }
1250
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001251 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001252 ubi_err("bad vol_id %d", sv->vol_id);
1253 goto bad_vid_hdr;
1254 }
1255
1256 if (sv->compat != vidh->compat) {
1257 ubi_err("bad compat %d", vidh->compat);
1258 goto bad_vid_hdr;
1259 }
1260
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001261 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001262 ubi_err("bad lnum %d", seb->lnum);
1263 goto bad_vid_hdr;
1264 }
1265
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001266 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001267 ubi_err("bad used_ebs %d", sv->used_ebs);
1268 goto bad_vid_hdr;
1269 }
1270
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001271 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001272 ubi_err("bad data_pad %d", sv->data_pad);
1273 goto bad_vid_hdr;
1274 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001275 }
1276
1277 if (!last_seb)
1278 continue;
1279
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001280 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001281 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1282 goto bad_vid_hdr;
1283 }
1284
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001285 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001286 ubi_err("bad last_data_size %d", sv->last_data_size);
1287 goto bad_vid_hdr;
1288 }
1289 }
1290
1291 /*
1292 * Make sure that all the physical eraseblocks are in one of the lists
1293 * or trees.
1294 */
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001295 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001296 if (!buf)
1297 return -ENOMEM;
1298
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001299 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1300 err = ubi_io_is_bad(ubi, pnum);
Artem Bityutskiy341e1a02007-05-03 11:59:51 +03001301 if (err < 0) {
1302 kfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001303 return err;
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +03001304 } else if (err)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001305 buf[pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001306 }
1307
1308 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1309 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001310 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001311
1312 list_for_each_entry(seb, &si->free, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001313 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001314
1315 list_for_each_entry(seb, &si->corr, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001316 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001317
1318 list_for_each_entry(seb, &si->erase, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001319 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001320
1321 list_for_each_entry(seb, &si->alien, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001322 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001323
1324 err = 0;
1325 for (pnum = 0; pnum < ubi->peb_count; pnum++)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001326 if (!buf[pnum]) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001327 ubi_err("PEB %d is not referred", pnum);
1328 err = 1;
1329 }
1330
1331 kfree(buf);
1332 if (err)
1333 goto out;
1334 return 0;
1335
1336bad_seb:
1337 ubi_err("bad scanning information about LEB %d", seb->lnum);
1338 ubi_dbg_dump_seb(seb, 0);
1339 ubi_dbg_dump_sv(sv);
1340 goto out;
1341
1342bad_sv:
1343 ubi_err("bad scanning information about volume %d", sv->vol_id);
1344 ubi_dbg_dump_sv(sv);
1345 goto out;
1346
1347bad_vid_hdr:
1348 ubi_err("bad scanning information about volume %d", sv->vol_id);
1349 ubi_dbg_dump_sv(sv);
1350 ubi_dbg_dump_vid_hdr(vidh);
1351
1352out:
1353 ubi_dbg_dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001354 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001355}
1356
1357#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */