UBI: fix sparse warnings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / ubi / scan.c
blob18c347b24e27365f02d491d9321efa5457ffc9ce
1 /*
2 * Copyright (c) International Business Machines Corp., 2006
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.
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.
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
18 * Author: Artem Bityutskiy (Битюцкий Артём)
22 * UBI scanning unit.
24 * This unit is responsible for scanning the flash media, checking UBI
25 * headers and providing complete information about the UBI flash image.
27 * The scanning information is represented by a &struct ubi_scan_info' object.
28 * 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.
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.
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.
43 #include <linux/err.h>
44 #include <linux/crc32.h>
45 #include "ubi.h"
47 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
48 static int paranoid_check_si(const struct ubi_device *ubi,
49 struct ubi_scan_info *si);
50 #else
51 #define paranoid_check_si(ubi, si) 0
52 #endif
54 /* Temporary variables used during scanning */
55 static struct ubi_ec_hdr *ech;
56 static struct ubi_vid_hdr *vidh;
58 /**
59 * add_to_list - add physical eraseblock to a list.
60 * @si: scanning information
61 * @pnum: physical eraseblock number to add
62 * @ec: erase counter of the physical eraseblock
63 * @list: the list to add to
65 * This function adds physical eraseblock @pnum to free, erase, corrupted or
66 * alien lists. Returns zero in case of success and a negative error code in
67 * case of failure.
69 static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
70 struct list_head *list)
72 struct ubi_scan_leb *seb;
74 if (list == &si->free)
75 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
76 else if (list == &si->erase)
77 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
78 else if (list == &si->corr)
79 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
80 else if (list == &si->alien)
81 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
82 else
83 BUG();
85 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
86 if (!seb)
87 return -ENOMEM;
89 seb->pnum = pnum;
90 seb->ec = ec;
91 list_add_tail(&seb->u.list, list);
92 return 0;
95 /**
96 * commit_to_mean_value - commit intermediate results to the final mean erase
97 * counter value.
98 * @si: scanning information
100 * This is a helper function which calculates partial mean erase counter mean
101 * value and adds it to the resulting mean value. As we can work only in
102 * integer arithmetic and we want to calculate the mean value of erase counter
103 * accurately, we first sum erase counter values in @si->ec_sum variable and
104 * count these components in @si->ec_count. If this temporary @si->ec_sum is
105 * going to overflow, we calculate the partial mean value
106 * (@si->ec_sum/@si->ec_count) and add it to @si->mean_ec.
108 static void commit_to_mean_value(struct ubi_scan_info *si)
110 si->ec_sum /= si->ec_count;
111 if (si->ec_sum % si->ec_count >= si->ec_count / 2)
112 si->mean_ec += 1;
113 si->mean_ec += si->ec_sum;
117 * validate_vid_hdr - check that volume identifier header is correct and
118 * consistent.
119 * @vid_hdr: the volume identifier header to check
120 * @sv: information about the volume this logical eraseblock belongs to
121 * @pnum: physical eraseblock number the VID header came from
123 * This function checks that data stored in @vid_hdr is consistent. Returns
124 * non-zero if an inconsistency was found and zero if not.
126 * Note, UBI does sanity check of everything it reads from the flash media.
127 * Most of the checks are done in the I/O unit. Here we check that the
128 * information in the VID header is consistent to the information in other VID
129 * headers of the same volume.
131 static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
132 const struct ubi_scan_volume *sv, int pnum)
134 int vol_type = vid_hdr->vol_type;
135 int vol_id = be32_to_cpu(vid_hdr->vol_id);
136 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
137 int data_pad = be32_to_cpu(vid_hdr->data_pad);
139 if (sv->leb_count != 0) {
140 int sv_vol_type;
143 * This is not the first logical eraseblock belonging to this
144 * volume. Ensure that the data in its VID header is consistent
145 * to the data in previous logical eraseblock headers.
148 if (vol_id != sv->vol_id) {
149 dbg_err("inconsistent vol_id");
150 goto bad;
153 if (sv->vol_type == UBI_STATIC_VOLUME)
154 sv_vol_type = UBI_VID_STATIC;
155 else
156 sv_vol_type = UBI_VID_DYNAMIC;
158 if (vol_type != sv_vol_type) {
159 dbg_err("inconsistent vol_type");
160 goto bad;
163 if (used_ebs != sv->used_ebs) {
164 dbg_err("inconsistent used_ebs");
165 goto bad;
168 if (data_pad != sv->data_pad) {
169 dbg_err("inconsistent data_pad");
170 goto bad;
174 return 0;
176 bad:
177 ubi_err("inconsistent VID header at PEB %d", pnum);
178 ubi_dbg_dump_vid_hdr(vid_hdr);
179 ubi_dbg_dump_sv(sv);
180 return -EINVAL;
184 * add_volume - add volume to the scanning information.
185 * @si: scanning information
186 * @vol_id: ID of the volume to add
187 * @pnum: physical eraseblock number
188 * @vid_hdr: volume identifier header
190 * If the volume corresponding to the @vid_hdr logical eraseblock is already
191 * present in the scanning information, this function does nothing. Otherwise
192 * it adds corresponding volume to the scanning information. Returns a pointer
193 * to the scanning volume object in case of success and a negative error code
194 * in case of failure.
196 static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
197 int pnum,
198 const struct ubi_vid_hdr *vid_hdr)
200 struct ubi_scan_volume *sv;
201 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
203 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
205 /* Walk the volume RB-tree to look if this volume is already present */
206 while (*p) {
207 parent = *p;
208 sv = rb_entry(parent, struct ubi_scan_volume, rb);
210 if (vol_id == sv->vol_id)
211 return sv;
213 if (vol_id > sv->vol_id)
214 p = &(*p)->rb_left;
215 else
216 p = &(*p)->rb_right;
219 /* The volume is absent - add it */
220 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
221 if (!sv)
222 return ERR_PTR(-ENOMEM);
224 sv->highest_lnum = sv->leb_count = 0;
225 sv->vol_id = vol_id;
226 sv->root = RB_ROOT;
227 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
228 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
229 sv->compat = vid_hdr->compat;
230 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
231 : UBI_STATIC_VOLUME;
232 if (vol_id > si->highest_vol_id)
233 si->highest_vol_id = vol_id;
235 rb_link_node(&sv->rb, parent, p);
236 rb_insert_color(&sv->rb, &si->volumes);
237 si->vols_found += 1;
238 dbg_bld("added volume %d", vol_id);
239 return sv;
243 * compare_lebs - find out which logical eraseblock is newer.
244 * @ubi: UBI device description object
245 * @seb: first logical eraseblock to compare
246 * @pnum: physical eraseblock number of the second logical eraseblock to
247 * compare
248 * @vid_hdr: volume identifier header of the second logical eraseblock
250 * This function compares 2 copies of a LEB and informs which one is newer. In
251 * case of success this function returns a positive value, in case of failure, a
252 * negative error code is returned. The success return codes use the following
253 * bits:
254 * o bit 0 is cleared: the first PEB (described by @seb) is newer then the
255 * second PEB (described by @pnum and @vid_hdr);
256 * o bit 0 is set: the second PEB is newer;
257 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
258 * o bit 1 is set: bit-flips were detected in the newer LEB;
259 * o bit 2 is cleared: the older LEB is not corrupted;
260 * o bit 2 is set: the older LEB is corrupted.
262 static int compare_lebs(const struct ubi_device *ubi,
263 const struct ubi_scan_leb *seb, int pnum,
264 const struct ubi_vid_hdr *vid_hdr)
266 void *buf;
267 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
268 uint32_t data_crc, crc;
269 struct ubi_vid_hdr *vh = NULL;
270 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
272 if (seb->sqnum == 0 && sqnum2 == 0) {
273 long long abs, v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver);
276 * UBI constantly increases the logical eraseblock version
277 * number and it can overflow. Thus, we have to bear in mind
278 * that versions that are close to %0xFFFFFFFF are less then
279 * versions that are close to %0.
281 * The UBI WL unit guarantees that the number of pending tasks
282 * is not greater then %0x7FFFFFFF. So, if the difference
283 * between any two versions is greater or equivalent to
284 * %0x7FFFFFFF, there was an overflow and the logical
285 * eraseblock with lower version is actually newer then the one
286 * with higher version.
288 * FIXME: but this is anyway obsolete and will be removed at
289 * some point.
292 dbg_bld("using old crappy leb_ver stuff");
294 abs = v1 - v2;
295 if (abs < 0)
296 abs = -abs;
298 if (abs < 0x7FFFFFFF)
299 /* Non-overflow situation */
300 second_is_newer = (v2 > v1);
301 else
302 second_is_newer = (v2 < v1);
303 } else
304 /* Obviously the LEB with lower sequence counter is older */
305 second_is_newer = sqnum2 > seb->sqnum;
308 * Now we know which copy is newer. If the copy flag of the PEB with
309 * newer version is not set, then we just return, otherwise we have to
310 * check data CRC. For the second PEB we already have the VID header,
311 * for the first one - we'll need to re-read it from flash.
313 * FIXME: this may be optimized so that we wouldn't read twice.
316 if (second_is_newer) {
317 if (!vid_hdr->copy_flag) {
318 /* It is not a copy, so it is newer */
319 dbg_bld("second PEB %d is newer, copy_flag is unset",
320 pnum);
321 return 1;
323 } else {
324 pnum = seb->pnum;
326 vh = ubi_zalloc_vid_hdr(ubi);
327 if (!vh)
328 return -ENOMEM;
330 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
331 if (err) {
332 if (err == UBI_IO_BITFLIPS)
333 bitflips = 1;
334 else {
335 dbg_err("VID of PEB %d header is bad, but it "
336 "was OK earlier", pnum);
337 if (err > 0)
338 err = -EIO;
340 goto out_free_vidh;
344 if (!vh->copy_flag) {
345 /* It is not a copy, so it is newer */
346 dbg_bld("first PEB %d is newer, copy_flag is unset",
347 pnum);
348 err = bitflips << 1;
349 goto out_free_vidh;
352 vid_hdr = vh;
355 /* Read the data of the copy and check the CRC */
357 len = be32_to_cpu(vid_hdr->data_size);
358 buf = vmalloc(len);
359 if (!buf) {
360 err = -ENOMEM;
361 goto out_free_vidh;
364 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
365 if (err && err != UBI_IO_BITFLIPS)
366 goto out_free_buf;
368 data_crc = be32_to_cpu(vid_hdr->data_crc);
369 crc = crc32(UBI_CRC32_INIT, buf, len);
370 if (crc != data_crc) {
371 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
372 pnum, crc, data_crc);
373 corrupted = 1;
374 bitflips = 0;
375 second_is_newer = !second_is_newer;
376 } else {
377 dbg_bld("PEB %d CRC is OK", pnum);
378 bitflips = !!err;
381 vfree(buf);
382 ubi_free_vid_hdr(ubi, vh);
384 if (second_is_newer)
385 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
386 else
387 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
389 return second_is_newer | (bitflips << 1) | (corrupted << 2);
391 out_free_buf:
392 vfree(buf);
393 out_free_vidh:
394 ubi_free_vid_hdr(ubi, vh);
395 ubi_assert(err < 0);
396 return err;
400 * ubi_scan_add_used - add information about a physical eraseblock to the
401 * scanning information.
402 * @ubi: UBI device description object
403 * @si: scanning information
404 * @pnum: the physical eraseblock number
405 * @ec: erase counter
406 * @vid_hdr: the volume identifier header
407 * @bitflips: if bit-flips were detected when this physical eraseblock was read
409 * This function adds information about a used physical eraseblock to the
410 * 'used' tree of the corresponding volume. The function is rather complex
411 * because it has to handle cases when this is not the first physical
412 * eraseblock belonging to the same logical eraseblock, and the newer one has
413 * to be picked, while the older one has to be dropped. This function returns
414 * zero in case of success and a negative error code in case of failure.
416 int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si,
417 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
418 int bitflips)
420 int err, vol_id, lnum;
421 uint32_t leb_ver;
422 unsigned long long sqnum;
423 struct ubi_scan_volume *sv;
424 struct ubi_scan_leb *seb;
425 struct rb_node **p, *parent = NULL;
427 vol_id = be32_to_cpu(vid_hdr->vol_id);
428 lnum = be32_to_cpu(vid_hdr->lnum);
429 sqnum = be64_to_cpu(vid_hdr->sqnum);
430 leb_ver = be32_to_cpu(vid_hdr->leb_ver);
432 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d",
433 pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips);
435 sv = add_volume(si, vol_id, pnum, vid_hdr);
436 if (IS_ERR(sv) < 0)
437 return PTR_ERR(sv);
439 if (si->max_sqnum < sqnum)
440 si->max_sqnum = sqnum;
443 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
444 * if this is the first instance of this logical eraseblock or not.
446 p = &sv->root.rb_node;
447 while (*p) {
448 int cmp_res;
450 parent = *p;
451 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
452 if (lnum != seb->lnum) {
453 if (lnum < seb->lnum)
454 p = &(*p)->rb_left;
455 else
456 p = &(*p)->rb_right;
457 continue;
461 * There is already a physical eraseblock describing the same
462 * logical eraseblock present.
465 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
466 "LEB ver %u, EC %d", seb->pnum, seb->sqnum,
467 seb->leb_ver, seb->ec);
470 * Make sure that the logical eraseblocks have different
471 * versions. Otherwise the image is bad.
473 if (seb->leb_ver == leb_ver && leb_ver != 0) {
474 ubi_err("two LEBs with same version %u", leb_ver);
475 ubi_dbg_dump_seb(seb, 0);
476 ubi_dbg_dump_vid_hdr(vid_hdr);
477 return -EINVAL;
481 * Make sure that the logical eraseblocks have different
482 * sequence numbers. Otherwise the image is bad.
484 * FIXME: remove 'sqnum != 0' check when leb_ver is removed.
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;
495 * Now we have to drop the older one and preserve the newer
496 * one.
498 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
499 if (cmp_res < 0)
500 return cmp_res;
502 if (cmp_res & 1) {
504 * This logical eraseblock is newer then the one
505 * found earlier.
507 err = validate_vid_hdr(vid_hdr, sv, pnum);
508 if (err)
509 return err;
511 if (cmp_res & 4)
512 err = add_to_list(si, seb->pnum, seb->ec,
513 &si->corr);
514 else
515 err = add_to_list(si, seb->pnum, seb->ec,
516 &si->erase);
517 if (err)
518 return err;
520 seb->ec = ec;
521 seb->pnum = pnum;
522 seb->scrub = ((cmp_res & 2) || bitflips);
523 seb->sqnum = sqnum;
524 seb->leb_ver = leb_ver;
526 if (sv->highest_lnum == lnum)
527 sv->last_data_size =
528 be32_to_cpu(vid_hdr->data_size);
530 return 0;
531 } else {
533 * This logical eraseblock is older then the one found
534 * previously.
536 if (cmp_res & 4)
537 return add_to_list(si, pnum, ec, &si->corr);
538 else
539 return add_to_list(si, pnum, ec, &si->erase);
544 * We've met this logical eraseblock for the first time, add it to the
545 * scanning information.
548 err = validate_vid_hdr(vid_hdr, sv, pnum);
549 if (err)
550 return err;
552 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
553 if (!seb)
554 return -ENOMEM;
556 seb->ec = ec;
557 seb->pnum = pnum;
558 seb->lnum = lnum;
559 seb->sqnum = sqnum;
560 seb->scrub = bitflips;
561 seb->leb_ver = leb_ver;
563 if (sv->highest_lnum <= lnum) {
564 sv->highest_lnum = lnum;
565 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
568 sv->leb_count += 1;
569 rb_link_node(&seb->u.rb, parent, p);
570 rb_insert_color(&seb->u.rb, &sv->root);
571 return 0;
575 * ubi_scan_find_sv - find information about a particular volume in the
576 * scanning information.
577 * @si: scanning information
578 * @vol_id: the requested volume ID
580 * This function returns a pointer to the volume description or %NULL if there
581 * are no data about this volume in the scanning information.
583 struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
584 int vol_id)
586 struct ubi_scan_volume *sv;
587 struct rb_node *p = si->volumes.rb_node;
589 while (p) {
590 sv = rb_entry(p, struct ubi_scan_volume, rb);
592 if (vol_id == sv->vol_id)
593 return sv;
595 if (vol_id > sv->vol_id)
596 p = p->rb_left;
597 else
598 p = p->rb_right;
601 return NULL;
605 * ubi_scan_find_seb - find information about a particular logical
606 * eraseblock in the volume scanning information.
607 * @sv: a pointer to the volume scanning information
608 * @lnum: the requested logical eraseblock
610 * This function returns a pointer to the scanning logical eraseblock or %NULL
611 * if there are no data about it in the scanning volume information.
613 struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
614 int lnum)
616 struct ubi_scan_leb *seb;
617 struct rb_node *p = sv->root.rb_node;
619 while (p) {
620 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
622 if (lnum == seb->lnum)
623 return seb;
625 if (lnum > seb->lnum)
626 p = p->rb_left;
627 else
628 p = p->rb_right;
631 return NULL;
635 * ubi_scan_rm_volume - delete scanning information about a volume.
636 * @si: scanning information
637 * @sv: the volume scanning information to delete
639 void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
641 struct rb_node *rb;
642 struct ubi_scan_leb *seb;
644 dbg_bld("remove scanning information about volume %d", sv->vol_id);
646 while ((rb = rb_first(&sv->root))) {
647 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
648 rb_erase(&seb->u.rb, &sv->root);
649 list_add_tail(&seb->u.list, &si->erase);
652 rb_erase(&sv->rb, &si->volumes);
653 kfree(sv);
654 si->vols_found -= 1;
658 * ubi_scan_erase_peb - erase a physical eraseblock.
659 * @ubi: UBI device description object
660 * @si: scanning information
661 * @pnum: physical eraseblock number to erase;
662 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
664 * This function erases physical eraseblock 'pnum', and writes the erase
665 * counter header to it. This function should only be used on UBI device
666 * initialization stages, when the EBA unit had not been yet initialized. This
667 * function returns zero in case of success and a negative error code in case
668 * of failure.
670 int ubi_scan_erase_peb(const struct ubi_device *ubi,
671 const struct ubi_scan_info *si, int pnum, int ec)
673 int err;
674 struct ubi_ec_hdr *ec_hdr;
676 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
678 * Erase counter overflow. Upgrade UBI and use 64-bit
679 * erase counters internally.
681 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
682 return -EINVAL;
685 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
686 if (!ec_hdr)
687 return -ENOMEM;
689 ec_hdr->ec = cpu_to_be64(ec);
691 err = ubi_io_sync_erase(ubi, pnum, 0);
692 if (err < 0)
693 goto out_free;
695 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
697 out_free:
698 kfree(ec_hdr);
699 return err;
703 * ubi_scan_get_free_peb - get a free physical eraseblock.
704 * @ubi: UBI device description object
705 * @si: scanning information
707 * This function returns a free physical eraseblock. It is supposed to be
708 * called on the UBI initialization stages when the wear-leveling unit is not
709 * initialized yet. This function picks a physical eraseblocks from one of the
710 * lists, writes the EC header if it is needed, and removes it from the list.
712 * This function returns scanning physical eraseblock information in case of
713 * success and an error code in case of failure.
715 struct ubi_scan_leb *ubi_scan_get_free_peb(const struct ubi_device *ubi,
716 struct ubi_scan_info *si)
718 int err = 0, i;
719 struct ubi_scan_leb *seb;
721 if (!list_empty(&si->free)) {
722 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
723 list_del(&seb->u.list);
724 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
725 return seb;
728 for (i = 0; i < 2; i++) {
729 struct list_head *head;
730 struct ubi_scan_leb *tmp_seb;
732 if (i == 0)
733 head = &si->erase;
734 else
735 head = &si->corr;
738 * We try to erase the first physical eraseblock from the @head
739 * list and pick it if we succeed, or try to erase the
740 * next one if not. And so forth. We don't want to take care
741 * about bad eraseblocks here - they'll be handled later.
743 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
744 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
745 seb->ec = si->mean_ec;
747 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
748 if (err)
749 continue;
751 seb->ec += 1;
752 list_del(&seb->u.list);
753 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
754 return seb;
758 ubi_err("no eraseblocks found");
759 return ERR_PTR(-ENOSPC);
763 * process_eb - read UBI headers, check them and add corresponding data
764 * to the scanning information.
765 * @ubi: UBI device description object
766 * @si: scanning information
767 * @pnum: the physical eraseblock number
769 * This function returns a zero if the physical eraseblock was successfully
770 * handled and a negative error code in case of failure.
772 static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum)
774 long long ec;
775 int err, bitflips = 0, vol_id, ec_corr = 0;
777 dbg_bld("scan PEB %d", pnum);
779 /* Skip bad physical eraseblocks */
780 err = ubi_io_is_bad(ubi, pnum);
781 if (err < 0)
782 return err;
783 else if (err) {
785 * FIXME: this is actually duty of the I/O unit to initialize
786 * this, but MTD does not provide enough information.
788 si->bad_peb_count += 1;
789 return 0;
792 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
793 if (err < 0)
794 return err;
795 else if (err == UBI_IO_BITFLIPS)
796 bitflips = 1;
797 else if (err == UBI_IO_PEB_EMPTY)
798 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
799 else if (err == UBI_IO_BAD_EC_HDR) {
801 * We have to also look at the VID header, possibly it is not
802 * corrupted. Set %bitflips flag in order to make this PEB be
803 * moved and EC be re-created.
805 ec_corr = 1;
806 ec = UBI_SCAN_UNKNOWN_EC;
807 bitflips = 1;
810 si->is_empty = 0;
812 if (!ec_corr) {
813 /* Make sure UBI version is OK */
814 if (ech->version != UBI_VERSION) {
815 ubi_err("this UBI version is %d, image version is %d",
816 UBI_VERSION, (int)ech->version);
817 return -EINVAL;
820 ec = be64_to_cpu(ech->ec);
821 if (ec > UBI_MAX_ERASECOUNTER) {
823 * Erase counter overflow. The EC headers have 64 bits
824 * reserved, but we anyway make use of only 31 bit
825 * values, as this seems to be enough for any existing
826 * flash. Upgrade UBI and use 64-bit erase counters
827 * internally.
829 ubi_err("erase counter overflow, max is %d",
830 UBI_MAX_ERASECOUNTER);
831 ubi_dbg_dump_ec_hdr(ech);
832 return -EINVAL;
836 /* OK, we've done with the EC header, let's look at the VID header */
838 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
839 if (err < 0)
840 return err;
841 else if (err == UBI_IO_BITFLIPS)
842 bitflips = 1;
843 else if (err == UBI_IO_BAD_VID_HDR ||
844 (err == UBI_IO_PEB_FREE && ec_corr)) {
845 /* VID header is corrupted */
846 err = add_to_list(si, pnum, ec, &si->corr);
847 if (err)
848 return err;
849 goto adjust_mean_ec;
850 } else if (err == UBI_IO_PEB_FREE) {
851 /* No VID header - the physical eraseblock is free */
852 err = add_to_list(si, pnum, ec, &si->free);
853 if (err)
854 return err;
855 goto adjust_mean_ec;
858 vol_id = be32_to_cpu(vidh->vol_id);
859 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOL_ID) {
860 int lnum = be32_to_cpu(vidh->lnum);
862 /* Unsupported internal volume */
863 switch (vidh->compat) {
864 case UBI_COMPAT_DELETE:
865 ubi_msg("\"delete\" compatible internal volume %d:%d"
866 " found, remove it", vol_id, lnum);
867 err = add_to_list(si, pnum, ec, &si->corr);
868 if (err)
869 return err;
870 break;
872 case UBI_COMPAT_RO:
873 ubi_msg("read-only compatible internal volume %d:%d"
874 " found, switch to read-only mode",
875 vol_id, lnum);
876 ubi->ro_mode = 1;
877 break;
879 case UBI_COMPAT_PRESERVE:
880 ubi_msg("\"preserve\" compatible internal volume %d:%d"
881 " found", vol_id, lnum);
882 err = add_to_list(si, pnum, ec, &si->alien);
883 if (err)
884 return err;
885 si->alien_peb_count += 1;
886 return 0;
888 case UBI_COMPAT_REJECT:
889 ubi_err("incompatible internal volume %d:%d found",
890 vol_id, lnum);
891 return -EINVAL;
895 /* Both UBI headers seem to be fine */
896 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
897 if (err)
898 return err;
900 adjust_mean_ec:
901 if (!ec_corr) {
902 if (si->ec_sum + ec < ec) {
903 commit_to_mean_value(si);
904 si->ec_sum = 0;
905 si->ec_count = 0;
906 } else {
907 si->ec_sum += ec;
908 si->ec_count += 1;
911 if (ec > si->max_ec)
912 si->max_ec = ec;
913 if (ec < si->min_ec)
914 si->min_ec = ec;
917 return 0;
921 * ubi_scan - scan an MTD device.
922 * @ubi: UBI device description object
924 * This function does full scanning of an MTD device and returns complete
925 * information about it. In case of failure, an error code is returned.
927 struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
929 int err, pnum;
930 struct rb_node *rb1, *rb2;
931 struct ubi_scan_volume *sv;
932 struct ubi_scan_leb *seb;
933 struct ubi_scan_info *si;
935 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
936 if (!si)
937 return ERR_PTR(-ENOMEM);
939 INIT_LIST_HEAD(&si->corr);
940 INIT_LIST_HEAD(&si->free);
941 INIT_LIST_HEAD(&si->erase);
942 INIT_LIST_HEAD(&si->alien);
943 si->volumes = RB_ROOT;
944 si->is_empty = 1;
946 err = -ENOMEM;
947 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
948 if (!ech)
949 goto out_si;
951 vidh = ubi_zalloc_vid_hdr(ubi);
952 if (!vidh)
953 goto out_ech;
955 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
956 cond_resched();
958 dbg_msg("process PEB %d", pnum);
959 err = process_eb(ubi, si, pnum);
960 if (err < 0)
961 goto out_vidh;
964 dbg_msg("scanning is finished");
966 /* Finish mean erase counter calculations */
967 if (si->ec_count)
968 commit_to_mean_value(si);
970 if (si->is_empty)
971 ubi_msg("empty MTD device detected");
974 * In case of unknown erase counter we use the mean erase counter
975 * value.
977 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
978 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
979 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
980 seb->ec = si->mean_ec;
983 list_for_each_entry(seb, &si->free, u.list) {
984 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
985 seb->ec = si->mean_ec;
988 list_for_each_entry(seb, &si->corr, u.list)
989 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
990 seb->ec = si->mean_ec;
992 list_for_each_entry(seb, &si->erase, u.list)
993 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
994 seb->ec = si->mean_ec;
996 err = paranoid_check_si(ubi, si);
997 if (err) {
998 if (err > 0)
999 err = -EINVAL;
1000 goto out_vidh;
1003 ubi_free_vid_hdr(ubi, vidh);
1004 kfree(ech);
1006 return si;
1008 out_vidh:
1009 ubi_free_vid_hdr(ubi, vidh);
1010 out_ech:
1011 kfree(ech);
1012 out_si:
1013 ubi_scan_destroy_si(si);
1014 return ERR_PTR(err);
1018 * destroy_sv - free the scanning volume information
1019 * @sv: scanning volume information
1021 * This function destroys the volume RB-tree (@sv->root) and the scanning
1022 * volume information.
1024 static void destroy_sv(struct ubi_scan_volume *sv)
1026 struct ubi_scan_leb *seb;
1027 struct rb_node *this = sv->root.rb_node;
1029 while (this) {
1030 if (this->rb_left)
1031 this = this->rb_left;
1032 else if (this->rb_right)
1033 this = this->rb_right;
1034 else {
1035 seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1036 this = rb_parent(this);
1037 if (this) {
1038 if (this->rb_left == &seb->u.rb)
1039 this->rb_left = NULL;
1040 else
1041 this->rb_right = NULL;
1044 kfree(seb);
1047 kfree(sv);
1051 * ubi_scan_destroy_si - destroy scanning information.
1052 * @si: scanning information
1054 void ubi_scan_destroy_si(struct ubi_scan_info *si)
1056 struct ubi_scan_leb *seb, *seb_tmp;
1057 struct ubi_scan_volume *sv;
1058 struct rb_node *rb;
1060 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1061 list_del(&seb->u.list);
1062 kfree(seb);
1064 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1065 list_del(&seb->u.list);
1066 kfree(seb);
1068 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1069 list_del(&seb->u.list);
1070 kfree(seb);
1072 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1073 list_del(&seb->u.list);
1074 kfree(seb);
1077 /* Destroy the volume RB-tree */
1078 rb = si->volumes.rb_node;
1079 while (rb) {
1080 if (rb->rb_left)
1081 rb = rb->rb_left;
1082 else if (rb->rb_right)
1083 rb = rb->rb_right;
1084 else {
1085 sv = rb_entry(rb, struct ubi_scan_volume, rb);
1087 rb = rb_parent(rb);
1088 if (rb) {
1089 if (rb->rb_left == &sv->rb)
1090 rb->rb_left = NULL;
1091 else
1092 rb->rb_right = NULL;
1095 destroy_sv(sv);
1099 kfree(si);
1102 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1105 * paranoid_check_si - check if the scanning information is correct and
1106 * consistent.
1107 * @ubi: UBI device description object
1108 * @si: scanning information
1110 * This function returns zero if the scanning information is all right, %1 if
1111 * not and a negative error code if an error occurred.
1113 static int paranoid_check_si(const struct ubi_device *ubi,
1114 struct ubi_scan_info *si)
1116 int pnum, err, vols_found = 0;
1117 struct rb_node *rb1, *rb2;
1118 struct ubi_scan_volume *sv;
1119 struct ubi_scan_leb *seb, *last_seb;
1120 uint8_t *buf;
1123 * At first, check that scanning information is OK.
1125 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1126 int leb_count = 0;
1128 cond_resched();
1130 vols_found += 1;
1132 if (si->is_empty) {
1133 ubi_err("bad is_empty flag");
1134 goto bad_sv;
1137 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1138 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1139 sv->data_pad < 0 || sv->last_data_size < 0) {
1140 ubi_err("negative values");
1141 goto bad_sv;
1144 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1145 sv->vol_id < UBI_INTERNAL_VOL_START) {
1146 ubi_err("bad vol_id");
1147 goto bad_sv;
1150 if (sv->vol_id > si->highest_vol_id) {
1151 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1152 si->highest_vol_id, sv->vol_id);
1153 goto out;
1156 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1157 sv->vol_type != UBI_STATIC_VOLUME) {
1158 ubi_err("bad vol_type");
1159 goto bad_sv;
1162 if (sv->data_pad > ubi->leb_size / 2) {
1163 ubi_err("bad data_pad");
1164 goto bad_sv;
1167 last_seb = NULL;
1168 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1169 cond_resched();
1171 last_seb = seb;
1172 leb_count += 1;
1174 if (seb->pnum < 0 || seb->ec < 0) {
1175 ubi_err("negative values");
1176 goto bad_seb;
1179 if (seb->ec < si->min_ec) {
1180 ubi_err("bad si->min_ec (%d), %d found",
1181 si->min_ec, seb->ec);
1182 goto bad_seb;
1185 if (seb->ec > si->max_ec) {
1186 ubi_err("bad si->max_ec (%d), %d found",
1187 si->max_ec, seb->ec);
1188 goto bad_seb;
1191 if (seb->pnum >= ubi->peb_count) {
1192 ubi_err("too high PEB number %d, total PEBs %d",
1193 seb->pnum, ubi->peb_count);
1194 goto bad_seb;
1197 if (sv->vol_type == UBI_STATIC_VOLUME) {
1198 if (seb->lnum >= sv->used_ebs) {
1199 ubi_err("bad lnum or used_ebs");
1200 goto bad_seb;
1202 } else {
1203 if (sv->used_ebs != 0) {
1204 ubi_err("non-zero used_ebs");
1205 goto bad_seb;
1209 if (seb->lnum > sv->highest_lnum) {
1210 ubi_err("incorrect highest_lnum or lnum");
1211 goto bad_seb;
1215 if (sv->leb_count != leb_count) {
1216 ubi_err("bad leb_count, %d objects in the tree",
1217 leb_count);
1218 goto bad_sv;
1221 if (!last_seb)
1222 continue;
1224 seb = last_seb;
1226 if (seb->lnum != sv->highest_lnum) {
1227 ubi_err("bad highest_lnum");
1228 goto bad_seb;
1232 if (vols_found != si->vols_found) {
1233 ubi_err("bad si->vols_found %d, should be %d",
1234 si->vols_found, vols_found);
1235 goto out;
1238 /* Check that scanning information is correct */
1239 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1240 last_seb = NULL;
1241 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1242 int vol_type;
1244 cond_resched();
1246 last_seb = seb;
1248 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1249 if (err && err != UBI_IO_BITFLIPS) {
1250 ubi_err("VID header is not OK (%d)", err);
1251 if (err > 0)
1252 err = -EIO;
1253 return err;
1256 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1257 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1258 if (sv->vol_type != vol_type) {
1259 ubi_err("bad vol_type");
1260 goto bad_vid_hdr;
1263 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
1264 ubi_err("bad sqnum %llu", seb->sqnum);
1265 goto bad_vid_hdr;
1268 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
1269 ubi_err("bad vol_id %d", sv->vol_id);
1270 goto bad_vid_hdr;
1273 if (sv->compat != vidh->compat) {
1274 ubi_err("bad compat %d", vidh->compat);
1275 goto bad_vid_hdr;
1278 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
1279 ubi_err("bad lnum %d", seb->lnum);
1280 goto bad_vid_hdr;
1283 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
1284 ubi_err("bad used_ebs %d", sv->used_ebs);
1285 goto bad_vid_hdr;
1288 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
1289 ubi_err("bad data_pad %d", sv->data_pad);
1290 goto bad_vid_hdr;
1293 if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) {
1294 ubi_err("bad leb_ver %u", seb->leb_ver);
1295 goto bad_vid_hdr;
1299 if (!last_seb)
1300 continue;
1302 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
1303 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1304 goto bad_vid_hdr;
1307 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
1308 ubi_err("bad last_data_size %d", sv->last_data_size);
1309 goto bad_vid_hdr;
1314 * Make sure that all the physical eraseblocks are in one of the lists
1315 * or trees.
1317 buf = kmalloc(ubi->peb_count, GFP_KERNEL);
1318 if (!buf)
1319 return -ENOMEM;
1321 memset(buf, 1, ubi->peb_count);
1322 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1323 err = ubi_io_is_bad(ubi, pnum);
1324 if (err < 0) {
1325 kfree(buf);
1326 return err;
1328 else if (err)
1329 buf[pnum] = 0;
1332 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1333 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1334 buf[seb->pnum] = 0;
1336 list_for_each_entry(seb, &si->free, u.list)
1337 buf[seb->pnum] = 0;
1339 list_for_each_entry(seb, &si->corr, u.list)
1340 buf[seb->pnum] = 0;
1342 list_for_each_entry(seb, &si->erase, u.list)
1343 buf[seb->pnum] = 0;
1345 list_for_each_entry(seb, &si->alien, u.list)
1346 buf[seb->pnum] = 0;
1348 err = 0;
1349 for (pnum = 0; pnum < ubi->peb_count; pnum++)
1350 if (buf[pnum]) {
1351 ubi_err("PEB %d is not referred", pnum);
1352 err = 1;
1355 kfree(buf);
1356 if (err)
1357 goto out;
1358 return 0;
1360 bad_seb:
1361 ubi_err("bad scanning information about LEB %d", seb->lnum);
1362 ubi_dbg_dump_seb(seb, 0);
1363 ubi_dbg_dump_sv(sv);
1364 goto out;
1366 bad_sv:
1367 ubi_err("bad scanning information about volume %d", sv->vol_id);
1368 ubi_dbg_dump_sv(sv);
1369 goto out;
1371 bad_vid_hdr:
1372 ubi_err("bad scanning information about volume %d", sv->vol_id);
1373 ubi_dbg_dump_sv(sv);
1374 ubi_dbg_dump_vid_hdr(vidh);
1376 out:
1377 ubi_dbg_dump_stack();
1378 return 1;
1381 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */