blob: 19dc5e04fd6138c133b28c126f04d2a66b2f33e2 [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 *
Artem Bityutskiy0525dac2010-09-03 17:11:37 +030032 * Scanned logical eraseblocks are represented by &struct ubi_scan_leb objects.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040033 * 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.
Artem Bityutskiy0525dac2010-09-03 17:11:37 +030041 *
42 * UBI tries to distinguish between 2 types of corruptions.
43 * 1. Corruptions caused by power cuts. These are harmless and expected
44 * corruptions and UBI tries to handle them gracefully, without printing too
45 * many warnings and error messages. The idea is that we do not lose
46 * important data in these case - we may lose only the data which was being
47 * written to the media just before the power cut happened, and the upper
48 * layers are supposed to handle these situations. UBI puts these PEBs to
49 * the head of the @erase list and they are scheduled for erasure.
50 *
51 * 2. Unexpected corruptions which are not caused by power cuts. During
52 * scanning, such PEBs are put to the @corr list and UBI preserves them.
53 * Obviously, this lessens the amount of available PEBs, and if at some
54 * point UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly
55 * informs about such PEBs every time the MTD device is attached.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040056 */
57
58#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/slab.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040060#include <linux/crc32.h>
Artem Bityutskiy3013ee32009-01-16 19:08:43 +020061#include <linux/math64.h>
Matthieu CASTET095751a2010-06-03 16:14:27 +020062#include <linux/random.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040063#include "ubi.h"
64
65#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
Artem Bityutskiye88d6e102007-08-29 14:51:52 +030066static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040067#else
68#define paranoid_check_si(ubi, si) 0
69#endif
70
71/* Temporary variables used during scanning */
72static struct ubi_ec_hdr *ech;
73static struct ubi_vid_hdr *vidh;
74
Artem Bityutskiy941dfb02007-05-05 16:33:13 +030075/**
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030076 * add_to_list - add physical eraseblock to a list.
77 * @si: scanning information
78 * @pnum: physical eraseblock number to add
79 * @ec: erase counter of the physical eraseblock
Artem Bityutskiy0525dac2010-09-03 17:11:37 +030080 * @to_head: if not zero, add to the head of the list
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030081 * @list: the list to add to
82 *
Artem Bityutskiy3fb34122010-09-03 15:36:12 +030083 * This function adds physical eraseblock @pnum to free, erase, or alien lists.
Artem Bityutskiy0525dac2010-09-03 17:11:37 +030084 * If @to_head is not zero, PEB will be added to the head of the list, which
85 * basically means it will be processed first later. E.g., we add corrupted
86 * PEBs (corrupted due to power cuts) to the head of the erase list to make
87 * sure we erase them first and get rid of corruptions ASAP. This function
88 * returns zero in case of success and a negative error code in case of
Artem Bityutskiy3fb34122010-09-03 15:36:12 +030089 * failure.
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030090 */
Artem Bityutskiy0525dac2010-09-03 17:11:37 +030091static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, int to_head,
Artem Bityutskiy78d87c92007-05-05 11:24:02 +030092 struct list_head *list)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040093{
94 struct ubi_scan_leb *seb;
95
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030096 if (list == &si->free) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040097 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +030098 } else if (list == &si->erase) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040099 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300100 } else if (list == &si->alien) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400101 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
Artem Bityutskiy33789fb2010-04-30 12:31:26 +0300102 si->alien_peb_count += 1;
103 } else
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400104 BUG();
105
106 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
107 if (!seb)
108 return -ENOMEM;
109
110 seb->pnum = pnum;
111 seb->ec = ec;
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300112 if (to_head)
113 list_add(&seb->u.list, list);
114 else
115 list_add_tail(&seb->u.list, list);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400116 return 0;
117}
118
119/**
Artem Bityutskiy3fb34122010-09-03 15:36:12 +0300120 * add_corrupted - add a corrupted physical eraseblock.
121 * @si: scanning information
122 * @pnum: physical eraseblock number to add
123 * @ec: erase counter of the physical eraseblock
124 *
125 * This function adds corrupted physical eraseblock @pnum to the 'corr' list.
126 * Returns zero in case of success and a negative error code in case of
127 * failure.
128 */
129static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)
130{
131 struct ubi_scan_leb *seb;
132
133 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
134
135 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
136 if (!seb)
137 return -ENOMEM;
138
139 si->corr_peb_count += 1;
140 seb->pnum = pnum;
141 seb->ec = ec;
142 list_add(&seb->u.list, &si->corr);
143 return 0;
144}
145
146/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300147 * validate_vid_hdr - check volume identifier header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400148 * @vid_hdr: the volume identifier header to check
149 * @sv: information about the volume this logical eraseblock belongs to
150 * @pnum: physical eraseblock number the VID header came from
151 *
152 * This function checks that data stored in @vid_hdr is consistent. Returns
153 * non-zero if an inconsistency was found and zero if not.
154 *
155 * Note, UBI does sanity check of everything it reads from the flash media.
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300156 * 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 +0400157 * information in the VID header is consistent to the information in other VID
158 * headers of the same volume.
159 */
160static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
161 const struct ubi_scan_volume *sv, int pnum)
162{
163 int vol_type = vid_hdr->vol_type;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300164 int vol_id = be32_to_cpu(vid_hdr->vol_id);
165 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
166 int data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400167
168 if (sv->leb_count != 0) {
169 int sv_vol_type;
170
171 /*
172 * This is not the first logical eraseblock belonging to this
173 * volume. Ensure that the data in its VID header is consistent
174 * to the data in previous logical eraseblock headers.
175 */
176
177 if (vol_id != sv->vol_id) {
178 dbg_err("inconsistent vol_id");
179 goto bad;
180 }
181
182 if (sv->vol_type == UBI_STATIC_VOLUME)
183 sv_vol_type = UBI_VID_STATIC;
184 else
185 sv_vol_type = UBI_VID_DYNAMIC;
186
187 if (vol_type != sv_vol_type) {
188 dbg_err("inconsistent vol_type");
189 goto bad;
190 }
191
192 if (used_ebs != sv->used_ebs) {
193 dbg_err("inconsistent used_ebs");
194 goto bad;
195 }
196
197 if (data_pad != sv->data_pad) {
198 dbg_err("inconsistent data_pad");
199 goto bad;
200 }
201 }
202
203 return 0;
204
205bad:
206 ubi_err("inconsistent VID header at PEB %d", pnum);
207 ubi_dbg_dump_vid_hdr(vid_hdr);
208 ubi_dbg_dump_sv(sv);
209 return -EINVAL;
210}
211
212/**
213 * add_volume - add volume to the scanning information.
214 * @si: scanning information
215 * @vol_id: ID of the volume to add
216 * @pnum: physical eraseblock number
217 * @vid_hdr: volume identifier header
218 *
219 * If the volume corresponding to the @vid_hdr logical eraseblock is already
220 * present in the scanning information, this function does nothing. Otherwise
221 * it adds corresponding volume to the scanning information. Returns a pointer
222 * to the scanning volume object in case of success and a negative error code
223 * in case of failure.
224 */
225static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
226 int pnum,
227 const struct ubi_vid_hdr *vid_hdr)
228{
229 struct ubi_scan_volume *sv;
230 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
231
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300232 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400233
234 /* Walk the volume RB-tree to look if this volume is already present */
235 while (*p) {
236 parent = *p;
237 sv = rb_entry(parent, struct ubi_scan_volume, rb);
238
239 if (vol_id == sv->vol_id)
240 return sv;
241
242 if (vol_id > sv->vol_id)
243 p = &(*p)->rb_left;
244 else
245 p = &(*p)->rb_right;
246 }
247
248 /* The volume is absent - add it */
249 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
250 if (!sv)
251 return ERR_PTR(-ENOMEM);
252
253 sv->highest_lnum = sv->leb_count = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400254 sv->vol_id = vol_id;
255 sv->root = RB_ROOT;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300256 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
257 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400258 sv->compat = vid_hdr->compat;
259 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
260 : UBI_STATIC_VOLUME;
261 if (vol_id > si->highest_vol_id)
262 si->highest_vol_id = vol_id;
263
264 rb_link_node(&sv->rb, parent, p);
265 rb_insert_color(&sv->rb, &si->volumes);
266 si->vols_found += 1;
267 dbg_bld("added volume %d", vol_id);
268 return sv;
269}
270
271/**
272 * compare_lebs - find out which logical eraseblock is newer.
273 * @ubi: UBI device description object
274 * @seb: first logical eraseblock to compare
275 * @pnum: physical eraseblock number of the second logical eraseblock to
276 * compare
277 * @vid_hdr: volume identifier header of the second logical eraseblock
278 *
279 * This function compares 2 copies of a LEB and informs which one is newer. In
280 * case of success this function returns a positive value, in case of failure, a
281 * negative error code is returned. The success return codes use the following
282 * bits:
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900283 * o bit 0 is cleared: the first PEB (described by @seb) is newer than the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400284 * second PEB (described by @pnum and @vid_hdr);
285 * o bit 0 is set: the second PEB is newer;
286 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
287 * o bit 1 is set: bit-flips were detected in the newer LEB;
288 * o bit 2 is cleared: the older LEB is not corrupted;
289 * o bit 2 is set: the older LEB is corrupted.
290 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300291static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
292 int pnum, const struct ubi_vid_hdr *vid_hdr)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400293{
294 void *buf;
295 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
296 uint32_t data_crc, crc;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300297 struct ubi_vid_hdr *vh = NULL;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300298 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400299
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300300 if (sqnum2 == seb->sqnum) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400301 /*
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300302 * This must be a really ancient UBI image which has been
303 * created before sequence numbers support has been added. At
304 * that times we used 32-bit LEB versions stored in logical
305 * eraseblocks. That was before UBI got into mainline. We do not
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300306 * support these images anymore. Well, those images still work,
307 * but only if no unclean reboots happened.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400308 */
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300309 ubi_err("unsupported on-flash UBI format\n");
310 return -EINVAL;
311 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400312
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300313 /* Obviously the LEB with lower sequence counter is older */
314 second_is_newer = !!(sqnum2 > seb->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400315
316 /*
317 * Now we know which copy is newer. If the copy flag of the PEB with
318 * newer version is not set, then we just return, otherwise we have to
319 * check data CRC. For the second PEB we already have the VID header,
320 * for the first one - we'll need to re-read it from flash.
321 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300322 * Note: this may be optimized so that we wouldn't read twice.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400323 */
324
325 if (second_is_newer) {
326 if (!vid_hdr->copy_flag) {
327 /* It is not a copy, so it is newer */
328 dbg_bld("second PEB %d is newer, copy_flag is unset",
329 pnum);
330 return 1;
331 }
332 } else {
333 pnum = seb->pnum;
334
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300335 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300336 if (!vh)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400337 return -ENOMEM;
338
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300339 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400340 if (err) {
341 if (err == UBI_IO_BITFLIPS)
342 bitflips = 1;
343 else {
344 dbg_err("VID of PEB %d header is bad, but it "
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300345 "was OK earlier, err %d", pnum, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400346 if (err > 0)
347 err = -EIO;
348
349 goto out_free_vidh;
350 }
351 }
352
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300353 if (!vh->copy_flag) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400354 /* It is not a copy, so it is newer */
355 dbg_bld("first PEB %d is newer, copy_flag is unset",
356 pnum);
357 err = bitflips << 1;
358 goto out_free_vidh;
359 }
360
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300361 vid_hdr = vh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400362 }
363
364 /* Read the data of the copy and check the CRC */
365
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300366 len = be32_to_cpu(vid_hdr->data_size);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300367 buf = vmalloc(len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400368 if (!buf) {
369 err = -ENOMEM;
370 goto out_free_vidh;
371 }
372
373 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
Zoltan Sogorb77bcb02008-10-29 09:50:02 +0100374 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400375 goto out_free_buf;
376
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300377 data_crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400378 crc = crc32(UBI_CRC32_INIT, buf, len);
379 if (crc != data_crc) {
380 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
381 pnum, crc, data_crc);
382 corrupted = 1;
383 bitflips = 0;
384 second_is_newer = !second_is_newer;
385 } else {
386 dbg_bld("PEB %d CRC is OK", pnum);
387 bitflips = !!err;
388 }
389
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300390 vfree(buf);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300391 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400392
393 if (second_is_newer)
394 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
395 else
396 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
397
398 return second_is_newer | (bitflips << 1) | (corrupted << 2);
399
400out_free_buf:
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300401 vfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400402out_free_vidh:
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300403 ubi_free_vid_hdr(ubi, vh);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400404 return err;
405}
406
407/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300408 * ubi_scan_add_used - add physical eraseblock to the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400409 * @ubi: UBI device description object
410 * @si: scanning information
411 * @pnum: the physical eraseblock number
412 * @ec: erase counter
413 * @vid_hdr: the volume identifier header
414 * @bitflips: if bit-flips were detected when this physical eraseblock was read
415 *
Artem Bityutskiy79b510c2007-05-05 17:36:17 +0300416 * This function adds information about a used physical eraseblock to the
417 * 'used' tree of the corresponding volume. The function is rather complex
418 * because it has to handle cases when this is not the first physical
419 * eraseblock belonging to the same logical eraseblock, and the newer one has
420 * to be picked, while the older one has to be dropped. This function returns
421 * zero in case of success and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400422 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300423int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400424 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
425 int bitflips)
426{
427 int err, vol_id, lnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400428 unsigned long long sqnum;
429 struct ubi_scan_volume *sv;
430 struct ubi_scan_leb *seb;
431 struct rb_node **p, *parent = NULL;
432
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300433 vol_id = be32_to_cpu(vid_hdr->vol_id);
434 lnum = be32_to_cpu(vid_hdr->lnum);
435 sqnum = be64_to_cpu(vid_hdr->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400436
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300437 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
438 pnum, vol_id, lnum, ec, sqnum, bitflips);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400439
440 sv = add_volume(si, vol_id, pnum, vid_hdr);
Julien Brunel0e4a0082008-09-26 15:27:25 +0200441 if (IS_ERR(sv))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400442 return PTR_ERR(sv);
443
Brijesh Singh76eafe42007-07-06 14:35:43 +0300444 if (si->max_sqnum < sqnum)
445 si->max_sqnum = sqnum;
446
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400447 /*
448 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
449 * if this is the first instance of this logical eraseblock or not.
450 */
451 p = &sv->root.rb_node;
452 while (*p) {
453 int cmp_res;
454
455 parent = *p;
456 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
457 if (lnum != seb->lnum) {
458 if (lnum < seb->lnum)
459 p = &(*p)->rb_left;
460 else
461 p = &(*p)->rb_right;
462 continue;
463 }
464
465 /*
466 * There is already a physical eraseblock describing the same
467 * logical eraseblock present.
468 */
469
470 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300471 "EC %d", seb->pnum, seb->sqnum, seb->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400472
473 /*
474 * Make sure that the logical eraseblocks have different
475 * sequence numbers. Otherwise the image is bad.
476 *
Artem Bityutskiy9869cd82008-07-18 13:53:39 +0300477 * However, if the sequence number is zero, we assume it must
478 * be an ancient UBI image from the era when UBI did not have
479 * sequence numbers. We still can attach these images, unless
480 * there is a need to distinguish between old and new
481 * eraseblocks, in which case we'll refuse the image in
482 * 'compare_lebs()'. In other words, we attach old clean
483 * images, but refuse attaching old images with duplicated
484 * logical eraseblocks because there was an unclean reboot.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400485 */
486 if (seb->sqnum == sqnum && sqnum != 0) {
487 ubi_err("two LEBs with same sequence number %llu",
488 sqnum);
489 ubi_dbg_dump_seb(seb, 0);
490 ubi_dbg_dump_vid_hdr(vid_hdr);
491 return -EINVAL;
492 }
493
494 /*
495 * Now we have to drop the older one and preserve the newer
496 * one.
497 */
498 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
499 if (cmp_res < 0)
500 return cmp_res;
501
502 if (cmp_res & 1) {
503 /*
Shinya Kuribayashi3f502622010-05-06 19:21:47 +0900504 * This logical eraseblock is newer than the one
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400505 * found earlier.
506 */
507 err = validate_vid_hdr(vid_hdr, sv, pnum);
508 if (err)
509 return err;
510
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300511 err = add_to_list(si, seb->pnum, seb->ec, cmp_res & 4,
512 &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400513 if (err)
514 return err;
515
516 seb->ec = ec;
517 seb->pnum = pnum;
518 seb->scrub = ((cmp_res & 2) || bitflips);
519 seb->sqnum = sqnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400520
521 if (sv->highest_lnum == lnum)
522 sv->last_data_size =
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300523 be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400524
525 return 0;
526 } else {
527 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200528 * This logical eraseblock is older than the one found
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400529 * previously.
530 */
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300531 return add_to_list(si, pnum, ec, cmp_res & 4,
532 &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400533 }
534 }
535
536 /*
537 * We've met this logical eraseblock for the first time, add it to the
538 * scanning information.
539 */
540
541 err = validate_vid_hdr(vid_hdr, sv, pnum);
542 if (err)
543 return err;
544
545 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
546 if (!seb)
547 return -ENOMEM;
548
549 seb->ec = ec;
550 seb->pnum = pnum;
551 seb->lnum = lnum;
552 seb->sqnum = sqnum;
553 seb->scrub = bitflips;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400554
555 if (sv->highest_lnum <= lnum) {
556 sv->highest_lnum = lnum;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300557 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400558 }
559
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400560 sv->leb_count += 1;
561 rb_link_node(&seb->u.rb, parent, p);
562 rb_insert_color(&seb->u.rb, &sv->root);
563 return 0;
564}
565
566/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300567 * ubi_scan_find_sv - find volume in the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400568 * @si: scanning information
569 * @vol_id: the requested volume ID
570 *
571 * This function returns a pointer to the volume description or %NULL if there
572 * are no data about this volume in the scanning information.
573 */
574struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
575 int vol_id)
576{
577 struct ubi_scan_volume *sv;
578 struct rb_node *p = si->volumes.rb_node;
579
580 while (p) {
581 sv = rb_entry(p, struct ubi_scan_volume, rb);
582
583 if (vol_id == sv->vol_id)
584 return sv;
585
586 if (vol_id > sv->vol_id)
587 p = p->rb_left;
588 else
589 p = p->rb_right;
590 }
591
592 return NULL;
593}
594
595/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300596 * ubi_scan_find_seb - find LEB in the volume scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400597 * @sv: a pointer to the volume scanning information
598 * @lnum: the requested logical eraseblock
599 *
600 * This function returns a pointer to the scanning logical eraseblock or %NULL
601 * if there are no data about it in the scanning volume information.
602 */
603struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
604 int lnum)
605{
606 struct ubi_scan_leb *seb;
607 struct rb_node *p = sv->root.rb_node;
608
609 while (p) {
610 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
611
612 if (lnum == seb->lnum)
613 return seb;
614
615 if (lnum > seb->lnum)
616 p = p->rb_left;
617 else
618 p = p->rb_right;
619 }
620
621 return NULL;
622}
623
624/**
625 * ubi_scan_rm_volume - delete scanning information about a volume.
626 * @si: scanning information
627 * @sv: the volume scanning information to delete
628 */
629void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
630{
631 struct rb_node *rb;
632 struct ubi_scan_leb *seb;
633
634 dbg_bld("remove scanning information about volume %d", sv->vol_id);
635
636 while ((rb = rb_first(&sv->root))) {
637 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
638 rb_erase(&seb->u.rb, &sv->root);
639 list_add_tail(&seb->u.list, &si->erase);
640 }
641
642 rb_erase(&sv->rb, &si->volumes);
643 kfree(sv);
644 si->vols_found -= 1;
645}
646
647/**
648 * ubi_scan_erase_peb - erase a physical eraseblock.
649 * @ubi: UBI device description object
650 * @si: scanning information
651 * @pnum: physical eraseblock number to erase;
652 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
653 *
654 * This function erases physical eraseblock 'pnum', and writes the erase
655 * counter header to it. This function should only be used on UBI device
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300656 * initialization stages, when the EBA sub-system had not been yet initialized.
657 * This function returns zero in case of success and a negative error code in
658 * case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400659 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300660int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
661 int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400662{
663 int err;
664 struct ubi_ec_hdr *ec_hdr;
665
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400666 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
667 /*
668 * Erase counter overflow. Upgrade UBI and use 64-bit
669 * erase counters internally.
670 */
671 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
672 return -EINVAL;
673 }
674
Florin Malitadcec4c32007-07-19 15:22:41 -0400675 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
676 if (!ec_hdr)
677 return -ENOMEM;
678
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300679 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400680
681 err = ubi_io_sync_erase(ubi, pnum, 0);
682 if (err < 0)
683 goto out_free;
684
685 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
686
687out_free:
688 kfree(ec_hdr);
689 return err;
690}
691
692/**
693 * ubi_scan_get_free_peb - get a free physical eraseblock.
694 * @ubi: UBI device description object
695 * @si: scanning information
696 *
697 * This function returns a free physical eraseblock. It is supposed to be
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300698 * called on the UBI initialization stages when the wear-leveling sub-system is
699 * not initialized yet. This function picks a physical eraseblocks from one of
700 * the lists, writes the EC header if it is needed, and removes it from the
701 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400702 *
703 * This function returns scanning physical eraseblock information in case of
704 * success and an error code in case of failure.
705 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300706struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400707 struct ubi_scan_info *si)
708{
709 int err = 0, i;
710 struct ubi_scan_leb *seb;
711
712 if (!list_empty(&si->free)) {
713 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
714 list_del(&seb->u.list);
715 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
716 return seb;
717 }
718
719 for (i = 0; i < 2; i++) {
720 struct list_head *head;
721 struct ubi_scan_leb *tmp_seb;
722
723 if (i == 0)
724 head = &si->erase;
725 else
726 head = &si->corr;
727
728 /*
729 * We try to erase the first physical eraseblock from the @head
730 * list and pick it if we succeed, or try to erase the
731 * next one if not. And so forth. We don't want to take care
732 * about bad eraseblocks here - they'll be handled later.
733 */
734 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
735 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
736 seb->ec = si->mean_ec;
737
738 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
739 if (err)
740 continue;
741
742 seb->ec += 1;
743 list_del(&seb->u.list);
744 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
745 return seb;
746 }
747 }
748
749 ubi_err("no eraseblocks found");
750 return ERR_PTR(-ENOSPC);
751}
752
753/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +0300754 * process_eb - read, check UBI headers, and add them to scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400755 * @ubi: UBI device description object
756 * @si: scanning information
757 * @pnum: the physical eraseblock number
758 *
Artem Bityutskiy78d87c92007-05-05 11:24:02 +0300759 * This function returns a zero if the physical eraseblock was successfully
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400760 * handled and a negative error code in case of failure.
761 */
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300762static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
763 int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400764{
Artem Bityutskiyc18a8412008-01-24 11:19:14 +0200765 long long uninitialized_var(ec);
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300766 int err, bitflips = 0, vol_id, ec_err = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400767
768 dbg_bld("scan PEB %d", pnum);
769
770 /* Skip bad physical eraseblocks */
771 err = ubi_io_is_bad(ubi, pnum);
772 if (err < 0)
773 return err;
774 else if (err) {
775 /*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300776 * FIXME: this is actually duty of the I/O sub-system to
777 * initialize this, but MTD does not provide enough
778 * information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400779 */
780 si->bad_peb_count += 1;
781 return 0;
782 }
783
784 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
785 if (err < 0)
786 return err;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300787 switch (err) {
788 case 0:
789 break;
790 case UBI_IO_BITFLIPS:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400791 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300792 break;
793 case UBI_IO_FF:
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300794 si->empty_peb_count += 1;
795 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 0,
796 &si->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300797 case UBI_IO_FF_BITFLIPS:
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300798 si->empty_peb_count += 1;
799 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 1,
800 &si->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300801 case UBI_IO_BAD_HDR_EBADMSG:
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300802 case UBI_IO_BAD_HDR:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400803 /*
804 * We have to also look at the VID header, possibly it is not
805 * corrupted. Set %bitflips flag in order to make this PEB be
806 * moved and EC be re-created.
807 */
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300808 ec_err = err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400809 ec = UBI_SCAN_UNKNOWN_EC;
810 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300811 break;
812 default:
813 ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
814 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400815 }
816
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300817 if (!ec_err) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300818 int image_seq;
819
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400820 /* Make sure UBI version is OK */
821 if (ech->version != UBI_VERSION) {
822 ubi_err("this UBI version is %d, image version is %d",
823 UBI_VERSION, (int)ech->version);
824 return -EINVAL;
825 }
826
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300827 ec = be64_to_cpu(ech->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400828 if (ec > UBI_MAX_ERASECOUNTER) {
829 /*
830 * Erase counter overflow. The EC headers have 64 bits
831 * reserved, but we anyway make use of only 31 bit
832 * values, as this seems to be enough for any existing
833 * flash. Upgrade UBI and use 64-bit erase counters
834 * internally.
835 */
836 ubi_err("erase counter overflow, max is %d",
837 UBI_MAX_ERASECOUNTER);
838 ubi_dbg_dump_ec_hdr(ech);
839 return -EINVAL;
840 }
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300841
Adrian Hunter32bc4822009-07-24 19:16:04 +0300842 /*
843 * Make sure that all PEBs have the same image sequence number.
844 * This allows us to detect situations when users flash UBI
845 * images incorrectly, so that the flash has the new UBI image
846 * and leftovers from the old one. This feature was added
847 * relatively recently, and the sequence number was always
848 * zero, because old UBI implementations always set it to zero.
849 * For this reasons, we do not panic if some PEBs have zero
850 * sequence number, while other PEBs have non-zero sequence
851 * number.
852 */
Holger Brunck3dc948d2009-07-13 16:47:57 +0200853 image_seq = be32_to_cpu(ech->image_seq);
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300854 if (!ubi->image_seq && image_seq)
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300855 ubi->image_seq = image_seq;
Artem Bityutskiy2eadaad2009-09-30 10:01:28 +0300856 if (ubi->image_seq && image_seq &&
857 ubi->image_seq != image_seq) {
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300858 ubi_err("bad image sequence number %d in PEB %d, "
859 "expected %d", image_seq, pnum, ubi->image_seq);
860 ubi_dbg_dump_ec_hdr(ech);
861 return -EINVAL;
862 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400863 }
864
865 /* OK, we've done with the EC header, let's look at the VID header */
866
867 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
868 if (err < 0)
869 return err;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300870 switch (err) {
871 case 0:
872 break;
873 case UBI_IO_BITFLIPS:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400874 bitflips = 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300875 break;
876 case UBI_IO_BAD_HDR_EBADMSG:
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300877 if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
878 /*
879 * Both EC and VID headers are corrupted and were read
880 * with data integrity error, probably this is a bad
881 * PEB, bit it is not marked as bad yet. This may also
882 * be a result of power cut during erasure.
883 */
884 si->maybe_bad_peb_count += 1;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300885 case UBI_IO_BAD_HDR:
886 case UBI_IO_FF_BITFLIPS:
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300887 err = add_to_list(si, pnum, ec, 1, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400888 if (err)
889 return err;
890 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300891 case UBI_IO_FF:
892 if (ec_err)
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300893 err = add_to_list(si, pnum, ec, 1, &si->erase);
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300894 else
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300895 err = add_to_list(si, pnum, ec, 0, &si->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400896 if (err)
897 return err;
898 goto adjust_mean_ec;
Artem Bityutskiyb3321502010-09-03 14:40:55 +0300899 default:
900 ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
901 err);
902 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400903 }
904
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300905 vol_id = be32_to_cpu(vidh->vol_id);
Artem Bityutskiy91f2d532008-01-24 11:23:23 +0200906 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300907 int lnum = be32_to_cpu(vidh->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400908
909 /* Unsupported internal volume */
910 switch (vidh->compat) {
911 case UBI_COMPAT_DELETE:
912 ubi_msg("\"delete\" compatible internal volume %d:%d"
Brijesh Singh158132c2010-06-16 09:28:26 +0300913 " found, will remove it", vol_id, lnum);
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300914 err = add_to_list(si, pnum, ec, 1, &si->erase);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400915 if (err)
916 return err;
Brijesh Singh158132c2010-06-16 09:28:26 +0300917 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400918
919 case UBI_COMPAT_RO:
920 ubi_msg("read-only compatible internal volume %d:%d"
921 " found, switch to read-only mode",
922 vol_id, lnum);
923 ubi->ro_mode = 1;
924 break;
925
926 case UBI_COMPAT_PRESERVE:
927 ubi_msg("\"preserve\" compatible internal volume %d:%d"
928 " found", vol_id, lnum);
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300929 err = add_to_list(si, pnum, ec, 0, &si->alien);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400930 if (err)
931 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400932 return 0;
933
934 case UBI_COMPAT_REJECT:
935 ubi_err("incompatible internal volume %d:%d found",
936 vol_id, lnum);
937 return -EINVAL;
938 }
939 }
940
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300941 if (ec_err)
Artem Bityutskiy29a88c92009-07-19 14:03:16 +0300942 ubi_warn("valid VID header but corrupted EC header at PEB %d",
943 pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400944 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
945 if (err)
946 return err;
947
948adjust_mean_ec:
Artem Bityutskiye0e718c2010-09-03 14:53:23 +0300949 if (!ec_err) {
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +0300950 si->ec_sum += ec;
951 si->ec_count += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400952 if (ec > si->max_ec)
953 si->max_ec = ec;
954 if (ec < si->min_ec)
955 si->min_ec = ec;
956 }
957
958 return 0;
959}
960
961/**
Artem Bityutskiy0798cea2010-04-30 13:02:33 +0300962 * check_what_we_have - check what PEB were found by scanning.
963 * @ubi: UBI device description object
964 * @si: scanning information
965 *
966 * This is a helper function which takes a look what PEBs were found by
967 * scanning, and decides whether the flash is empty and should be formatted and
968 * whether there are too many corrupted PEBs and we should not attach this
969 * MTD device. Returns zero if we should proceed with attaching the MTD device,
970 * and %-EINVAL if we should not.
971 */
Artem Bityutskiyf5d5b1f2010-06-14 08:15:39 +0300972static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
Artem Bityutskiy0798cea2010-04-30 13:02:33 +0300973{
974 struct ubi_scan_leb *seb;
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300975 int max_corr, peb_count;
Artem Bityutskiy0798cea2010-04-30 13:02:33 +0300976
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300977 peb_count = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
978 max_corr = peb_count / 20 ?: 8;
Artem Bityutskiy0798cea2010-04-30 13:02:33 +0300979
980 /*
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300981 * Few corrupted PEBs is not a problem and may be just a result of
Artem Bityutskiy0798cea2010-04-30 13:02:33 +0300982 * unclean reboots. However, many of them may indicate some problems
983 * with the flash HW or driver.
984 */
Artem Bityutskiy0525dac2010-09-03 17:11:37 +0300985 if (si->corr_peb_count) {
986 ubi_err("%d PEBs are corrupted and preserved",
987 si->corr_peb_count);
988 printk(KERN_ERR "Corrupted PEBs are:");
Artem Bityutskiy0798cea2010-04-30 13:02:33 +0300989 list_for_each_entry(seb, &si->corr, u.list)
990 printk(KERN_CONT " %d", seb->pnum);
991 printk(KERN_CONT "\n");
992
993 /*
994 * If too many PEBs are corrupted, we refuse attaching,
995 * otherwise, only print a warning.
996 */
997 if (si->corr_peb_count >= max_corr) {
998 ubi_err("too many corrupted PEBs, refusing this device");
999 return -EINVAL;
1000 }
1001 }
1002
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001003 if (si->empty_peb_count + si->maybe_bad_peb_count == peb_count) {
1004 /*
1005 * All PEBs are empty, or almost all - a couple PEBs look like
1006 * they may be bad PEBs which were not marked as bad yet.
1007 *
1008 * This piece of code basically tries to distinguish between
1009 * the following situations:
1010 *
1011 * 1. Flash is empty, but there are few bad PEBs, which are not
1012 * marked as bad so far, and which were read with error. We
1013 * want to go ahead and format this flash. While formatting,
1014 * the faulty PEBs will probably be marked as bad.
1015 *
1016 * 2. Flash contains non-UBI data and we do not want to format
1017 * it and destroy possibly important information.
1018 */
1019 if (si->maybe_bad_peb_count <= 2) {
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001020 si->is_empty = 1;
1021 ubi_msg("empty MTD device detected");
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001022 get_random_bytes(&ubi->image_seq,
1023 sizeof(ubi->image_seq));
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001024 } else {
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001025 ubi_err("MTD device is not UBI-formatted and possibly "
1026 "contains non-UBI data - refusing it");
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001027 return -EINVAL;
1028 }
Artem Bityutskiy0525dac2010-09-03 17:11:37 +03001029
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001030 }
1031
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001032 return 0;
1033}
1034
1035/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001036 * ubi_scan - scan an MTD device.
1037 * @ubi: UBI device description object
1038 *
1039 * This function does full scanning of an MTD device and returns complete
1040 * information about it. In case of failure, an error code is returned.
1041 */
1042struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
1043{
1044 int err, pnum;
1045 struct rb_node *rb1, *rb2;
1046 struct ubi_scan_volume *sv;
1047 struct ubi_scan_leb *seb;
1048 struct ubi_scan_info *si;
1049
1050 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
1051 if (!si)
1052 return ERR_PTR(-ENOMEM);
1053
1054 INIT_LIST_HEAD(&si->corr);
1055 INIT_LIST_HEAD(&si->free);
1056 INIT_LIST_HEAD(&si->erase);
1057 INIT_LIST_HEAD(&si->alien);
1058 si->volumes = RB_ROOT;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001059
1060 err = -ENOMEM;
1061 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1062 if (!ech)
1063 goto out_si;
1064
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001065 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001066 if (!vidh)
1067 goto out_ech;
1068
1069 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1070 cond_resched();
1071
Artem Bityutskiyc8566352008-07-16 17:40:22 +03001072 dbg_gen("process PEB %d", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001073 err = process_eb(ubi, si, pnum);
1074 if (err < 0)
1075 goto out_vidh;
1076 }
1077
1078 dbg_msg("scanning is finished");
1079
Artem Bityutskiy4bc1dca2008-04-19 20:44:31 +03001080 /* Calculate mean erase counter */
Artem Bityutskiy3013ee32009-01-16 19:08:43 +02001081 if (si->ec_count)
1082 si->mean_ec = div_u64(si->ec_sum, si->ec_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001083
Artem Bityutskiy0798cea2010-04-30 13:02:33 +03001084 err = check_what_we_have(ubi, si);
1085 if (err)
1086 goto out_vidh;
Artem Bityutskiy4a406852009-07-19 14:33:14 +03001087
1088 /*
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001089 * In case of unknown erase counter we use the mean erase counter
1090 * value.
1091 */
1092 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1093 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1094 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1095 seb->ec = si->mean_ec;
1096 }
1097
1098 list_for_each_entry(seb, &si->free, u.list) {
1099 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1100 seb->ec = si->mean_ec;
1101 }
1102
1103 list_for_each_entry(seb, &si->corr, u.list)
1104 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1105 seb->ec = si->mean_ec;
1106
1107 list_for_each_entry(seb, &si->erase, u.list)
1108 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
1109 seb->ec = si->mean_ec;
1110
1111 err = paranoid_check_si(ubi, si);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001112 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001113 goto out_vidh;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001114
1115 ubi_free_vid_hdr(ubi, vidh);
1116 kfree(ech);
1117
1118 return si;
1119
1120out_vidh:
1121 ubi_free_vid_hdr(ubi, vidh);
1122out_ech:
1123 kfree(ech);
1124out_si:
1125 ubi_scan_destroy_si(si);
1126 return ERR_PTR(err);
1127}
1128
1129/**
1130 * destroy_sv - free the scanning volume information
1131 * @sv: scanning volume information
1132 *
1133 * This function destroys the volume RB-tree (@sv->root) and the scanning
1134 * volume information.
1135 */
1136static void destroy_sv(struct ubi_scan_volume *sv)
1137{
1138 struct ubi_scan_leb *seb;
1139 struct rb_node *this = sv->root.rb_node;
1140
1141 while (this) {
1142 if (this->rb_left)
1143 this = this->rb_left;
1144 else if (this->rb_right)
1145 this = this->rb_right;
1146 else {
1147 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1148 this = rb_parent(this);
1149 if (this) {
1150 if (this->rb_left == &seb->u.rb)
1151 this->rb_left = NULL;
1152 else
1153 this->rb_right = NULL;
1154 }
1155
1156 kfree(seb);
1157 }
1158 }
1159 kfree(sv);
1160}
1161
1162/**
1163 * ubi_scan_destroy_si - destroy scanning information.
1164 * @si: scanning information
1165 */
1166void ubi_scan_destroy_si(struct ubi_scan_info *si)
1167{
1168 struct ubi_scan_leb *seb, *seb_tmp;
1169 struct ubi_scan_volume *sv;
1170 struct rb_node *rb;
1171
1172 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1173 list_del(&seb->u.list);
1174 kfree(seb);
1175 }
1176 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1177 list_del(&seb->u.list);
1178 kfree(seb);
1179 }
1180 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1181 list_del(&seb->u.list);
1182 kfree(seb);
1183 }
1184 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1185 list_del(&seb->u.list);
1186 kfree(seb);
1187 }
1188
1189 /* Destroy the volume RB-tree */
1190 rb = si->volumes.rb_node;
1191 while (rb) {
1192 if (rb->rb_left)
1193 rb = rb->rb_left;
1194 else if (rb->rb_right)
1195 rb = rb->rb_right;
1196 else {
1197 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1198
1199 rb = rb_parent(rb);
1200 if (rb) {
1201 if (rb->rb_left == &sv->rb)
1202 rb->rb_left = NULL;
1203 else
1204 rb->rb_right = NULL;
1205 }
1206
1207 destroy_sv(sv);
1208 }
1209 }
1210
1211 kfree(si);
1212}
1213
1214#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1215
1216/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001217 * paranoid_check_si - check the scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001218 * @ubi: UBI device description object
1219 * @si: scanning information
1220 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001221 * This function returns zero if the scanning information is all right, and a
1222 * negative error code if not or if an error occurred.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001223 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001224static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001225{
1226 int pnum, err, vols_found = 0;
1227 struct rb_node *rb1, *rb2;
1228 struct ubi_scan_volume *sv;
1229 struct ubi_scan_leb *seb, *last_seb;
1230 uint8_t *buf;
1231
1232 /*
Artem Bityutskiy78d87c92007-05-05 11:24:02 +03001233 * At first, check that scanning information is OK.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001234 */
1235 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1236 int leb_count = 0;
1237
1238 cond_resched();
1239
1240 vols_found += 1;
1241
1242 if (si->is_empty) {
1243 ubi_err("bad is_empty flag");
1244 goto bad_sv;
1245 }
1246
1247 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1248 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1249 sv->data_pad < 0 || sv->last_data_size < 0) {
1250 ubi_err("negative values");
1251 goto bad_sv;
1252 }
1253
1254 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1255 sv->vol_id < UBI_INTERNAL_VOL_START) {
1256 ubi_err("bad vol_id");
1257 goto bad_sv;
1258 }
1259
1260 if (sv->vol_id > si->highest_vol_id) {
1261 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1262 si->highest_vol_id, sv->vol_id);
1263 goto out;
1264 }
1265
1266 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1267 sv->vol_type != UBI_STATIC_VOLUME) {
1268 ubi_err("bad vol_type");
1269 goto bad_sv;
1270 }
1271
1272 if (sv->data_pad > ubi->leb_size / 2) {
1273 ubi_err("bad data_pad");
1274 goto bad_sv;
1275 }
1276
1277 last_seb = NULL;
1278 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1279 cond_resched();
1280
1281 last_seb = seb;
1282 leb_count += 1;
1283
1284 if (seb->pnum < 0 || seb->ec < 0) {
1285 ubi_err("negative values");
1286 goto bad_seb;
1287 }
1288
1289 if (seb->ec < si->min_ec) {
1290 ubi_err("bad si->min_ec (%d), %d found",
1291 si->min_ec, seb->ec);
1292 goto bad_seb;
1293 }
1294
1295 if (seb->ec > si->max_ec) {
1296 ubi_err("bad si->max_ec (%d), %d found",
1297 si->max_ec, seb->ec);
1298 goto bad_seb;
1299 }
1300
1301 if (seb->pnum >= ubi->peb_count) {
1302 ubi_err("too high PEB number %d, total PEBs %d",
1303 seb->pnum, ubi->peb_count);
1304 goto bad_seb;
1305 }
1306
1307 if (sv->vol_type == UBI_STATIC_VOLUME) {
1308 if (seb->lnum >= sv->used_ebs) {
1309 ubi_err("bad lnum or used_ebs");
1310 goto bad_seb;
1311 }
1312 } else {
1313 if (sv->used_ebs != 0) {
1314 ubi_err("non-zero used_ebs");
1315 goto bad_seb;
1316 }
1317 }
1318
1319 if (seb->lnum > sv->highest_lnum) {
1320 ubi_err("incorrect highest_lnum or lnum");
1321 goto bad_seb;
1322 }
1323 }
1324
1325 if (sv->leb_count != leb_count) {
1326 ubi_err("bad leb_count, %d objects in the tree",
1327 leb_count);
1328 goto bad_sv;
1329 }
1330
1331 if (!last_seb)
1332 continue;
1333
1334 seb = last_seb;
1335
1336 if (seb->lnum != sv->highest_lnum) {
1337 ubi_err("bad highest_lnum");
1338 goto bad_seb;
1339 }
1340 }
1341
1342 if (vols_found != si->vols_found) {
1343 ubi_err("bad si->vols_found %d, should be %d",
1344 si->vols_found, vols_found);
1345 goto out;
1346 }
1347
1348 /* Check that scanning information is correct */
1349 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1350 last_seb = NULL;
1351 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1352 int vol_type;
1353
1354 cond_resched();
1355
1356 last_seb = seb;
1357
1358 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1359 if (err && err != UBI_IO_BITFLIPS) {
1360 ubi_err("VID header is not OK (%d)", err);
1361 if (err > 0)
1362 err = -EIO;
1363 return err;
1364 }
1365
1366 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1367 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1368 if (sv->vol_type != vol_type) {
1369 ubi_err("bad vol_type");
1370 goto bad_vid_hdr;
1371 }
1372
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001373 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001374 ubi_err("bad sqnum %llu", seb->sqnum);
1375 goto bad_vid_hdr;
1376 }
1377
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001378 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001379 ubi_err("bad vol_id %d", sv->vol_id);
1380 goto bad_vid_hdr;
1381 }
1382
1383 if (sv->compat != vidh->compat) {
1384 ubi_err("bad compat %d", vidh->compat);
1385 goto bad_vid_hdr;
1386 }
1387
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001388 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001389 ubi_err("bad lnum %d", seb->lnum);
1390 goto bad_vid_hdr;
1391 }
1392
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001393 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001394 ubi_err("bad used_ebs %d", sv->used_ebs);
1395 goto bad_vid_hdr;
1396 }
1397
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001398 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001399 ubi_err("bad data_pad %d", sv->data_pad);
1400 goto bad_vid_hdr;
1401 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001402 }
1403
1404 if (!last_seb)
1405 continue;
1406
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001407 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001408 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1409 goto bad_vid_hdr;
1410 }
1411
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001412 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001413 ubi_err("bad last_data_size %d", sv->last_data_size);
1414 goto bad_vid_hdr;
1415 }
1416 }
1417
1418 /*
1419 * Make sure that all the physical eraseblocks are in one of the lists
1420 * or trees.
1421 */
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001422 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001423 if (!buf)
1424 return -ENOMEM;
1425
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001426 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1427 err = ubi_io_is_bad(ubi, pnum);
Artem Bityutskiy341e1a02007-05-03 11:59:51 +03001428 if (err < 0) {
1429 kfree(buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001430 return err;
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +03001431 } else if (err)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001432 buf[pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001433 }
1434
1435 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1436 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001437 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001438
1439 list_for_each_entry(seb, &si->free, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001440 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001441
1442 list_for_each_entry(seb, &si->corr, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001443 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001444
1445 list_for_each_entry(seb, &si->erase, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001446 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001447
1448 list_for_each_entry(seb, &si->alien, u.list)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001449 buf[seb->pnum] = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001450
1451 err = 0;
1452 for (pnum = 0; pnum < ubi->peb_count; pnum++)
Mariusz Kozlowskid9b07442007-08-01 00:02:10 +02001453 if (!buf[pnum]) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001454 ubi_err("PEB %d is not referred", pnum);
1455 err = 1;
1456 }
1457
1458 kfree(buf);
1459 if (err)
1460 goto out;
1461 return 0;
1462
1463bad_seb:
1464 ubi_err("bad scanning information about LEB %d", seb->lnum);
1465 ubi_dbg_dump_seb(seb, 0);
1466 ubi_dbg_dump_sv(sv);
1467 goto out;
1468
1469bad_sv:
1470 ubi_err("bad scanning information about volume %d", sv->vol_id);
1471 ubi_dbg_dump_sv(sv);
1472 goto out;
1473
1474bad_vid_hdr:
1475 ubi_err("bad scanning information about volume %d", sv->vol_id);
1476 ubi_dbg_dump_sv(sv);
1477 ubi_dbg_dump_vid_hdr(vidh);
1478
1479out:
1480 ubi_dbg_dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001481 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001482}
1483
1484#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */