40eca9ce5fab216d0bb9951d91a7ff37095376e9
[safe/jmp/linux-2.6] / drivers / mtd / ubi / scan.c
1 /*
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 /*
22  * UBI scanning sub-system.
23  *
24  * This sub-system is responsible for scanning the flash media, checking UBI
25  * headers and providing complete information about the UBI flash image.
26  *
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.
31  *
32  * Found logical eraseblocks are represented by &struct ubi_scan_leb objects.
33  * These objects are kept in per-volume RB-trees with the root at the
34  * corresponding &struct ubi_scan_volume object. To put it differently, we keep
35  * an RB-tree of per-volume objects and each of these objects is the root of
36  * RB-tree of per-eraseblock objects.
37  *
38  * Corrupted physical eraseblocks are put to the @corr list, free physical
39  * eraseblocks are put to the @free list and the physical eraseblock to be
40  * erased are put to the @erase list.
41  */
42
43 #include <linux/err.h>
44 #include <linux/crc32.h>
45 #include <asm/div64.h>
46 #include "ubi.h"
47
48 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
49 static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
50 #else
51 #define paranoid_check_si(ubi, si) 0
52 #endif
53
54 /* Temporary variables used during scanning */
55 static struct ubi_ec_hdr *ech;
56 static struct ubi_vid_hdr *vidh;
57
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
64  *
65  * This function adds physical eraseblock @pnum to free, erase, corrupted or
66  * alien lists. Returns zero in case of success and a negative error code in
67  * case of failure.
68  */
69 static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
70                        struct list_head *list)
71 {
72         struct ubi_scan_leb *seb;
73
74         if (list == &si->free)
75                 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
76         else if (list == &si->erase)
77                 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
78         else if (list == &si->corr)
79                 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
80         else if (list == &si->alien)
81                 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
82         else
83                 BUG();
84
85         seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
86         if (!seb)
87                 return -ENOMEM;
88
89         seb->pnum = pnum;
90         seb->ec = ec;
91         list_add_tail(&seb->u.list, list);
92         return 0;
93 }
94
95 /**
96  * validate_vid_hdr - check that volume identifier header is correct and
97  * consistent.
98  * @vid_hdr: the volume identifier header to check
99  * @sv: information about the volume this logical eraseblock belongs to
100  * @pnum: physical eraseblock number the VID header came from
101  *
102  * This function checks that data stored in @vid_hdr is consistent. Returns
103  * non-zero if an inconsistency was found and zero if not.
104  *
105  * Note, UBI does sanity check of everything it reads from the flash media.
106  * Most of the checks are done in the I/O sub-system. Here we check that the
107  * information in the VID header is consistent to the information in other VID
108  * headers of the same volume.
109  */
110 static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
111                             const struct ubi_scan_volume *sv, int pnum)
112 {
113         int vol_type = vid_hdr->vol_type;
114         int vol_id = be32_to_cpu(vid_hdr->vol_id);
115         int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
116         int data_pad = be32_to_cpu(vid_hdr->data_pad);
117
118         if (sv->leb_count != 0) {
119                 int sv_vol_type;
120
121                 /*
122                  * This is not the first logical eraseblock belonging to this
123                  * volume. Ensure that the data in its VID header is consistent
124                  * to the data in previous logical eraseblock headers.
125                  */
126
127                 if (vol_id != sv->vol_id) {
128                         dbg_err("inconsistent vol_id");
129                         goto bad;
130                 }
131
132                 if (sv->vol_type == UBI_STATIC_VOLUME)
133                         sv_vol_type = UBI_VID_STATIC;
134                 else
135                         sv_vol_type = UBI_VID_DYNAMIC;
136
137                 if (vol_type != sv_vol_type) {
138                         dbg_err("inconsistent vol_type");
139                         goto bad;
140                 }
141
142                 if (used_ebs != sv->used_ebs) {
143                         dbg_err("inconsistent used_ebs");
144                         goto bad;
145                 }
146
147                 if (data_pad != sv->data_pad) {
148                         dbg_err("inconsistent data_pad");
149                         goto bad;
150                 }
151         }
152
153         return 0;
154
155 bad:
156         ubi_err("inconsistent VID header at PEB %d", pnum);
157         ubi_dbg_dump_vid_hdr(vid_hdr);
158         ubi_dbg_dump_sv(sv);
159         return -EINVAL;
160 }
161
162 /**
163  * add_volume - add volume to the scanning information.
164  * @si: scanning information
165  * @vol_id: ID of the volume to add
166  * @pnum: physical eraseblock number
167  * @vid_hdr: volume identifier header
168  *
169  * If the volume corresponding to the @vid_hdr logical eraseblock is already
170  * present in the scanning information, this function does nothing. Otherwise
171  * it adds corresponding volume to the scanning information. Returns a pointer
172  * to the scanning volume object in case of success and a negative error code
173  * in case of failure.
174  */
175 static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
176                                           int pnum,
177                                           const struct ubi_vid_hdr *vid_hdr)
178 {
179         struct ubi_scan_volume *sv;
180         struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
181
182         ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
183
184         /* Walk the volume RB-tree to look if this volume is already present */
185         while (*p) {
186                 parent = *p;
187                 sv = rb_entry(parent, struct ubi_scan_volume, rb);
188
189                 if (vol_id == sv->vol_id)
190                         return sv;
191
192                 if (vol_id > sv->vol_id)
193                         p = &(*p)->rb_left;
194                 else
195                         p = &(*p)->rb_right;
196         }
197
198         /* The volume is absent - add it */
199         sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL);
200         if (!sv)
201                 return ERR_PTR(-ENOMEM);
202
203         sv->highest_lnum = sv->leb_count = 0;
204         sv->vol_id = vol_id;
205         sv->root = RB_ROOT;
206         sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
207         sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
208         sv->compat = vid_hdr->compat;
209         sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
210                                                             : UBI_STATIC_VOLUME;
211         if (vol_id > si->highest_vol_id)
212                 si->highest_vol_id = vol_id;
213
214         rb_link_node(&sv->rb, parent, p);
215         rb_insert_color(&sv->rb, &si->volumes);
216         si->vols_found += 1;
217         dbg_bld("added volume %d", vol_id);
218         return sv;
219 }
220
221 /**
222  * compare_lebs - find out which logical eraseblock is newer.
223  * @ubi: UBI device description object
224  * @seb: first logical eraseblock to compare
225  * @pnum: physical eraseblock number of the second logical eraseblock to
226  * compare
227  * @vid_hdr: volume identifier header of the second logical eraseblock
228  *
229  * This function compares 2 copies of a LEB and informs which one is newer. In
230  * case of success this function returns a positive value, in case of failure, a
231  * negative error code is returned. The success return codes use the following
232  * bits:
233  *     o bit 0 is cleared: the first PEB (described by @seb) is newer then the
234  *       second PEB (described by @pnum and @vid_hdr);
235  *     o bit 0 is set: the second PEB is newer;
236  *     o bit 1 is cleared: no bit-flips were detected in the newer LEB;
237  *     o bit 1 is set: bit-flips were detected in the newer LEB;
238  *     o bit 2 is cleared: the older LEB is not corrupted;
239  *     o bit 2 is set: the older LEB is corrupted.
240  */
241 static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
242                         int pnum, const struct ubi_vid_hdr *vid_hdr)
243 {
244         void *buf;
245         int len, err, second_is_newer, bitflips = 0, corrupted = 0;
246         uint32_t data_crc, crc;
247         struct ubi_vid_hdr *vh = NULL;
248         unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
249
250         if (seb->sqnum == 0 && sqnum2 == 0) {
251                 long long abs, v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver);
252
253                 /*
254                  * UBI constantly increases the logical eraseblock version
255                  * number and it can overflow. Thus, we have to bear in mind
256                  * that versions that are close to %0xFFFFFFFF are less then
257                  * versions that are close to %0.
258                  *
259                  * The UBI WL sub-system guarantees that the number of pending
260                  * tasks is not greater then %0x7FFFFFFF. So, if the difference
261                  * between any two versions is greater or equivalent to
262                  * %0x7FFFFFFF, there was an overflow and the logical
263                  * eraseblock with lower version is actually newer then the one
264                  * with higher version.
265                  *
266                  * FIXME: but this is anyway obsolete and will be removed at
267                  * some point.
268                  */
269                 dbg_bld("using old crappy leb_ver stuff");
270
271                 if (v1 == v2) {
272                         ubi_err("PEB %d and PEB %d have the same version %lld",
273                                 seb->pnum, pnum, v1);
274                         return -EINVAL;
275                 }
276
277                 abs = v1 - v2;
278                 if (abs < 0)
279                         abs = -abs;
280
281                 if (abs < 0x7FFFFFFF)
282                         /* Non-overflow situation */
283                         second_is_newer = (v2 > v1);
284                 else
285                         second_is_newer = (v2 < v1);
286         } else
287                 /* Obviously the LEB with lower sequence counter is older */
288                 second_is_newer = sqnum2 > seb->sqnum;
289
290         /*
291          * Now we know which copy is newer. If the copy flag of the PEB with
292          * newer version is not set, then we just return, otherwise we have to
293          * check data CRC. For the second PEB we already have the VID header,
294          * for the first one - we'll need to re-read it from flash.
295          *
296          * FIXME: this may be optimized so that we wouldn't read twice.
297          */
298
299         if (second_is_newer) {
300                 if (!vid_hdr->copy_flag) {
301                         /* It is not a copy, so it is newer */
302                         dbg_bld("second PEB %d is newer, copy_flag is unset",
303                                 pnum);
304                         return 1;
305                 }
306         } else {
307                 pnum = seb->pnum;
308
309                 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
310                 if (!vh)
311                         return -ENOMEM;
312
313                 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
314                 if (err) {
315                         if (err == UBI_IO_BITFLIPS)
316                                 bitflips = 1;
317                         else {
318                                 dbg_err("VID of PEB %d header is bad, but it "
319                                         "was OK earlier", pnum);
320                                 if (err > 0)
321                                         err = -EIO;
322
323                                 goto out_free_vidh;
324                         }
325                 }
326
327                 if (!vh->copy_flag) {
328                         /* It is not a copy, so it is newer */
329                         dbg_bld("first PEB %d is newer, copy_flag is unset",
330                                 pnum);
331                         err = bitflips << 1;
332                         goto out_free_vidh;
333                 }
334
335                 vid_hdr = vh;
336         }
337
338         /* Read the data of the copy and check the CRC */
339
340         len = be32_to_cpu(vid_hdr->data_size);
341         buf = vmalloc(len);
342         if (!buf) {
343                 err = -ENOMEM;
344                 goto out_free_vidh;
345         }
346
347         err = ubi_io_read_data(ubi, buf, pnum, 0, len);
348         if (err && err != UBI_IO_BITFLIPS)
349                 goto out_free_buf;
350
351         data_crc = be32_to_cpu(vid_hdr->data_crc);
352         crc = crc32(UBI_CRC32_INIT, buf, len);
353         if (crc != data_crc) {
354                 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
355                         pnum, crc, data_crc);
356                 corrupted = 1;
357                 bitflips = 0;
358                 second_is_newer = !second_is_newer;
359         } else {
360                 dbg_bld("PEB %d CRC is OK", pnum);
361                 bitflips = !!err;
362         }
363
364         vfree(buf);
365         ubi_free_vid_hdr(ubi, vh);
366
367         if (second_is_newer)
368                 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
369         else
370                 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
371
372         return second_is_newer | (bitflips << 1) | (corrupted << 2);
373
374 out_free_buf:
375         vfree(buf);
376 out_free_vidh:
377         ubi_free_vid_hdr(ubi, vh);
378         return err;
379 }
380
381 /**
382  * ubi_scan_add_used - add information about a physical eraseblock to the
383  * scanning information.
384  * @ubi: UBI device description object
385  * @si: scanning information
386  * @pnum: the physical eraseblock number
387  * @ec: erase counter
388  * @vid_hdr: the volume identifier header
389  * @bitflips: if bit-flips were detected when this physical eraseblock was read
390  *
391  * This function adds information about a used physical eraseblock to the
392  * 'used' tree of the corresponding volume. The function is rather complex
393  * because it has to handle cases when this is not the first physical
394  * eraseblock belonging to the same logical eraseblock, and the newer one has
395  * to be picked, while the older one has to be dropped. This function returns
396  * zero in case of success and a negative error code in case of failure.
397  */
398 int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
399                       int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
400                       int bitflips)
401 {
402         int err, vol_id, lnum;
403         uint32_t leb_ver;
404         unsigned long long sqnum;
405         struct ubi_scan_volume *sv;
406         struct ubi_scan_leb *seb;
407         struct rb_node **p, *parent = NULL;
408
409         vol_id = be32_to_cpu(vid_hdr->vol_id);
410         lnum = be32_to_cpu(vid_hdr->lnum);
411         sqnum = be64_to_cpu(vid_hdr->sqnum);
412         leb_ver = be32_to_cpu(vid_hdr->leb_ver);
413
414         dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d",
415                 pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips);
416
417         sv = add_volume(si, vol_id, pnum, vid_hdr);
418         if (IS_ERR(sv) < 0)
419                 return PTR_ERR(sv);
420
421         if (si->max_sqnum < sqnum)
422                 si->max_sqnum = sqnum;
423
424         /*
425          * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
426          * if this is the first instance of this logical eraseblock or not.
427          */
428         p = &sv->root.rb_node;
429         while (*p) {
430                 int cmp_res;
431
432                 parent = *p;
433                 seb = rb_entry(parent, struct ubi_scan_leb, u.rb);
434                 if (lnum != seb->lnum) {
435                         if (lnum < seb->lnum)
436                                 p = &(*p)->rb_left;
437                         else
438                                 p = &(*p)->rb_right;
439                         continue;
440                 }
441
442                 /*
443                  * There is already a physical eraseblock describing the same
444                  * logical eraseblock present.
445                  */
446
447                 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
448                         "LEB ver %u, EC %d", seb->pnum, seb->sqnum,
449                         seb->leb_ver, seb->ec);
450
451                 /*
452                  * Make sure that the logical eraseblocks have different
453                  * versions. Otherwise the image is bad.
454                  */
455                 if (seb->leb_ver == leb_ver && leb_ver != 0) {
456                         ubi_err("two LEBs with same version %u", leb_ver);
457                         ubi_dbg_dump_seb(seb, 0);
458                         ubi_dbg_dump_vid_hdr(vid_hdr);
459                         return -EINVAL;
460                 }
461
462                 /*
463                  * Make sure that the logical eraseblocks have different
464                  * sequence numbers. Otherwise the image is bad.
465                  *
466                  * FIXME: remove 'sqnum != 0' check when leb_ver is removed.
467                  */
468                 if (seb->sqnum == sqnum && sqnum != 0) {
469                         ubi_err("two LEBs with same sequence number %llu",
470                                 sqnum);
471                         ubi_dbg_dump_seb(seb, 0);
472                         ubi_dbg_dump_vid_hdr(vid_hdr);
473                         return -EINVAL;
474                 }
475
476                 /*
477                  * Now we have to drop the older one and preserve the newer
478                  * one.
479                  */
480                 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr);
481                 if (cmp_res < 0)
482                         return cmp_res;
483
484                 if (cmp_res & 1) {
485                         /*
486                          * This logical eraseblock is newer then the one
487                          * found earlier.
488                          */
489                         err = validate_vid_hdr(vid_hdr, sv, pnum);
490                         if (err)
491                                 return err;
492
493                         if (cmp_res & 4)
494                                 err = add_to_list(si, seb->pnum, seb->ec,
495                                                   &si->corr);
496                         else
497                                 err = add_to_list(si, seb->pnum, seb->ec,
498                                                   &si->erase);
499                         if (err)
500                                 return err;
501
502                         seb->ec = ec;
503                         seb->pnum = pnum;
504                         seb->scrub = ((cmp_res & 2) || bitflips);
505                         seb->sqnum = sqnum;
506                         seb->leb_ver = leb_ver;
507
508                         if (sv->highest_lnum == lnum)
509                                 sv->last_data_size =
510                                         be32_to_cpu(vid_hdr->data_size);
511
512                         return 0;
513                 } else {
514                         /*
515                          * This logical eraseblock is older then the one found
516                          * previously.
517                          */
518                         if (cmp_res & 4)
519                                 return add_to_list(si, pnum, ec, &si->corr);
520                         else
521                                 return add_to_list(si, pnum, ec, &si->erase);
522                 }
523         }
524
525         /*
526          * We've met this logical eraseblock for the first time, add it to the
527          * scanning information.
528          */
529
530         err = validate_vid_hdr(vid_hdr, sv, pnum);
531         if (err)
532                 return err;
533
534         seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
535         if (!seb)
536                 return -ENOMEM;
537
538         seb->ec = ec;
539         seb->pnum = pnum;
540         seb->lnum = lnum;
541         seb->sqnum = sqnum;
542         seb->scrub = bitflips;
543         seb->leb_ver = leb_ver;
544
545         if (sv->highest_lnum <= lnum) {
546                 sv->highest_lnum = lnum;
547                 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
548         }
549
550         sv->leb_count += 1;
551         rb_link_node(&seb->u.rb, parent, p);
552         rb_insert_color(&seb->u.rb, &sv->root);
553         return 0;
554 }
555
556 /**
557  * ubi_scan_find_sv - find information about a particular volume in the
558  * scanning information.
559  * @si: scanning information
560  * @vol_id: the requested volume ID
561  *
562  * This function returns a pointer to the volume description or %NULL if there
563  * are no data about this volume in the scanning information.
564  */
565 struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
566                                          int vol_id)
567 {
568         struct ubi_scan_volume *sv;
569         struct rb_node *p = si->volumes.rb_node;
570
571         while (p) {
572                 sv = rb_entry(p, struct ubi_scan_volume, rb);
573
574                 if (vol_id == sv->vol_id)
575                         return sv;
576
577                 if (vol_id > sv->vol_id)
578                         p = p->rb_left;
579                 else
580                         p = p->rb_right;
581         }
582
583         return NULL;
584 }
585
586 /**
587  * ubi_scan_find_seb - find information about a particular logical
588  * eraseblock in the volume scanning information.
589  * @sv: a pointer to the volume scanning information
590  * @lnum: the requested logical eraseblock
591  *
592  * This function returns a pointer to the scanning logical eraseblock or %NULL
593  * if there are no data about it in the scanning volume information.
594  */
595 struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
596                                        int lnum)
597 {
598         struct ubi_scan_leb *seb;
599         struct rb_node *p = sv->root.rb_node;
600
601         while (p) {
602                 seb = rb_entry(p, struct ubi_scan_leb, u.rb);
603
604                 if (lnum == seb->lnum)
605                         return seb;
606
607                 if (lnum > seb->lnum)
608                         p = p->rb_left;
609                 else
610                         p = p->rb_right;
611         }
612
613         return NULL;
614 }
615
616 /**
617  * ubi_scan_rm_volume - delete scanning information about a volume.
618  * @si: scanning information
619  * @sv: the volume scanning information to delete
620  */
621 void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
622 {
623         struct rb_node *rb;
624         struct ubi_scan_leb *seb;
625
626         dbg_bld("remove scanning information about volume %d", sv->vol_id);
627
628         while ((rb = rb_first(&sv->root))) {
629                 seb = rb_entry(rb, struct ubi_scan_leb, u.rb);
630                 rb_erase(&seb->u.rb, &sv->root);
631                 list_add_tail(&seb->u.list, &si->erase);
632         }
633
634         rb_erase(&sv->rb, &si->volumes);
635         kfree(sv);
636         si->vols_found -= 1;
637 }
638
639 /**
640  * ubi_scan_erase_peb - erase a physical eraseblock.
641  * @ubi: UBI device description object
642  * @si: scanning information
643  * @pnum: physical eraseblock number to erase;
644  * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown)
645  *
646  * This function erases physical eraseblock 'pnum', and writes the erase
647  * counter header to it. This function should only be used on UBI device
648  * initialization stages, when the EBA sub-system had not been yet initialized.
649  * This function returns zero in case of success and a negative error code in
650  * case of failure.
651  */
652 int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
653                        int pnum, int ec)
654 {
655         int err;
656         struct ubi_ec_hdr *ec_hdr;
657
658         if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
659                 /*
660                  * Erase counter overflow. Upgrade UBI and use 64-bit
661                  * erase counters internally.
662                  */
663                 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
664                 return -EINVAL;
665         }
666
667         ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
668         if (!ec_hdr)
669                 return -ENOMEM;
670
671         ec_hdr->ec = cpu_to_be64(ec);
672
673         err = ubi_io_sync_erase(ubi, pnum, 0);
674         if (err < 0)
675                 goto out_free;
676
677         err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
678
679 out_free:
680         kfree(ec_hdr);
681         return err;
682 }
683
684 /**
685  * ubi_scan_get_free_peb - get a free physical eraseblock.
686  * @ubi: UBI device description object
687  * @si: scanning information
688  *
689  * This function returns a free physical eraseblock. It is supposed to be
690  * called on the UBI initialization stages when the wear-leveling sub-system is
691  * not initialized yet. This function picks a physical eraseblocks from one of
692  * the lists, writes the EC header if it is needed, and removes it from the
693  * list.
694  *
695  * This function returns scanning physical eraseblock information in case of
696  * success and an error code in case of failure.
697  */
698 struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
699                                            struct ubi_scan_info *si)
700 {
701         int err = 0, i;
702         struct ubi_scan_leb *seb;
703
704         if (!list_empty(&si->free)) {
705                 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
706                 list_del(&seb->u.list);
707                 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec);
708                 return seb;
709         }
710
711         for (i = 0; i < 2; i++) {
712                 struct list_head *head;
713                 struct ubi_scan_leb *tmp_seb;
714
715                 if (i == 0)
716                         head = &si->erase;
717                 else
718                         head = &si->corr;
719
720                 /*
721                  * We try to erase the first physical eraseblock from the @head
722                  * list and pick it if we succeed, or try to erase the
723                  * next one if not. And so forth. We don't want to take care
724                  * about bad eraseblocks here - they'll be handled later.
725                  */
726                 list_for_each_entry_safe(seb, tmp_seb, head, u.list) {
727                         if (seb->ec == UBI_SCAN_UNKNOWN_EC)
728                                 seb->ec = si->mean_ec;
729
730                         err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
731                         if (err)
732                                 continue;
733
734                         seb->ec += 1;
735                         list_del(&seb->u.list);
736                         dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
737                         return seb;
738                 }
739         }
740
741         ubi_err("no eraseblocks found");
742         return ERR_PTR(-ENOSPC);
743 }
744
745 /**
746  * process_eb - read UBI headers, check them and add corresponding data
747  * to the scanning information.
748  * @ubi: UBI device description object
749  * @si: scanning information
750  * @pnum: the physical eraseblock number
751  *
752  * This function returns a zero if the physical eraseblock was successfully
753  * handled and a negative error code in case of failure.
754  */
755 static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum)
756 {
757         long long uninitialized_var(ec);
758         int err, bitflips = 0, vol_id, ec_corr = 0;
759
760         dbg_bld("scan PEB %d", pnum);
761
762         /* Skip bad physical eraseblocks */
763         err = ubi_io_is_bad(ubi, pnum);
764         if (err < 0)
765                 return err;
766         else if (err) {
767                 /*
768                  * FIXME: this is actually duty of the I/O sub-system to
769                  * initialize this, but MTD does not provide enough
770                  * information.
771                  */
772                 si->bad_peb_count += 1;
773                 return 0;
774         }
775
776         err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
777         if (err < 0)
778                 return err;
779         else if (err == UBI_IO_BITFLIPS)
780                 bitflips = 1;
781         else if (err == UBI_IO_PEB_EMPTY)
782                 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
783         else if (err == UBI_IO_BAD_EC_HDR) {
784                 /*
785                  * We have to also look at the VID header, possibly it is not
786                  * corrupted. Set %bitflips flag in order to make this PEB be
787                  * moved and EC be re-created.
788                  */
789                 ec_corr = 1;
790                 ec = UBI_SCAN_UNKNOWN_EC;
791                 bitflips = 1;
792         }
793
794         si->is_empty = 0;
795
796         if (!ec_corr) {
797                 /* Make sure UBI version is OK */
798                 if (ech->version != UBI_VERSION) {
799                         ubi_err("this UBI version is %d, image version is %d",
800                                 UBI_VERSION, (int)ech->version);
801                         return -EINVAL;
802                 }
803
804                 ec = be64_to_cpu(ech->ec);
805                 if (ec > UBI_MAX_ERASECOUNTER) {
806                         /*
807                          * Erase counter overflow. The EC headers have 64 bits
808                          * reserved, but we anyway make use of only 31 bit
809                          * values, as this seems to be enough for any existing
810                          * flash. Upgrade UBI and use 64-bit erase counters
811                          * internally.
812                          */
813                         ubi_err("erase counter overflow, max is %d",
814                                 UBI_MAX_ERASECOUNTER);
815                         ubi_dbg_dump_ec_hdr(ech);
816                         return -EINVAL;
817                 }
818         }
819
820         /* OK, we've done with the EC header, let's look at the VID header */
821
822         err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
823         if (err < 0)
824                 return err;
825         else if (err == UBI_IO_BITFLIPS)
826                 bitflips = 1;
827         else if (err == UBI_IO_BAD_VID_HDR ||
828                  (err == UBI_IO_PEB_FREE && ec_corr)) {
829                 /* VID header is corrupted */
830                 err = add_to_list(si, pnum, ec, &si->corr);
831                 if (err)
832                         return err;
833                 goto adjust_mean_ec;
834         } else if (err == UBI_IO_PEB_FREE) {
835                 /* No VID header - the physical eraseblock is free */
836                 err = add_to_list(si, pnum, ec, &si->free);
837                 if (err)
838                         return err;
839                 goto adjust_mean_ec;
840         }
841
842         vol_id = be32_to_cpu(vidh->vol_id);
843         if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
844                 int lnum = be32_to_cpu(vidh->lnum);
845
846                 /* Unsupported internal volume */
847                 switch (vidh->compat) {
848                 case UBI_COMPAT_DELETE:
849                         ubi_msg("\"delete\" compatible internal volume %d:%d"
850                                 " found, remove it", vol_id, lnum);
851                         err = add_to_list(si, pnum, ec, &si->corr);
852                         if (err)
853                                 return err;
854                         break;
855
856                 case UBI_COMPAT_RO:
857                         ubi_msg("read-only compatible internal volume %d:%d"
858                                 " found, switch to read-only mode",
859                                 vol_id, lnum);
860                         ubi->ro_mode = 1;
861                         break;
862
863                 case UBI_COMPAT_PRESERVE:
864                         ubi_msg("\"preserve\" compatible internal volume %d:%d"
865                                 " found", vol_id, lnum);
866                         err = add_to_list(si, pnum, ec, &si->alien);
867                         if (err)
868                                 return err;
869                         si->alien_peb_count += 1;
870                         return 0;
871
872                 case UBI_COMPAT_REJECT:
873                         ubi_err("incompatible internal volume %d:%d found",
874                                 vol_id, lnum);
875                         return -EINVAL;
876                 }
877         }
878
879         /* Both UBI headers seem to be fine */
880         err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
881         if (err)
882                 return err;
883
884 adjust_mean_ec:
885         if (!ec_corr) {
886                 si->ec_sum += ec;
887                 si->ec_count += 1;
888                 if (ec > si->max_ec)
889                         si->max_ec = ec;
890                 if (ec < si->min_ec)
891                         si->min_ec = ec;
892         }
893
894         return 0;
895 }
896
897 /**
898  * ubi_scan - scan an MTD device.
899  * @ubi: UBI device description object
900  *
901  * This function does full scanning of an MTD device and returns complete
902  * information about it. In case of failure, an error code is returned.
903  */
904 struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
905 {
906         int err, pnum;
907         struct rb_node *rb1, *rb2;
908         struct ubi_scan_volume *sv;
909         struct ubi_scan_leb *seb;
910         struct ubi_scan_info *si;
911
912         si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL);
913         if (!si)
914                 return ERR_PTR(-ENOMEM);
915
916         INIT_LIST_HEAD(&si->corr);
917         INIT_LIST_HEAD(&si->free);
918         INIT_LIST_HEAD(&si->erase);
919         INIT_LIST_HEAD(&si->alien);
920         si->volumes = RB_ROOT;
921         si->is_empty = 1;
922
923         err = -ENOMEM;
924         ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
925         if (!ech)
926                 goto out_si;
927
928         vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
929         if (!vidh)
930                 goto out_ech;
931
932         for (pnum = 0; pnum < ubi->peb_count; pnum++) {
933                 cond_resched();
934
935                 dbg_gen("process PEB %d", pnum);
936                 err = process_eb(ubi, si, pnum);
937                 if (err < 0)
938                         goto out_vidh;
939         }
940
941         dbg_msg("scanning is finished");
942
943         /* Calculate mean erase counter */
944         if (si->ec_count) {
945                 do_div(si->ec_sum, si->ec_count);
946                 si->mean_ec = si->ec_sum;
947         }
948
949         if (si->is_empty)
950                 ubi_msg("empty MTD device detected");
951
952         /*
953          * In case of unknown erase counter we use the mean erase counter
954          * value.
955          */
956         ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
957                 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
958                         if (seb->ec == UBI_SCAN_UNKNOWN_EC)
959                                 seb->ec = si->mean_ec;
960         }
961
962         list_for_each_entry(seb, &si->free, u.list) {
963                 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
964                         seb->ec = si->mean_ec;
965         }
966
967         list_for_each_entry(seb, &si->corr, u.list)
968                 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
969                         seb->ec = si->mean_ec;
970
971         list_for_each_entry(seb, &si->erase, u.list)
972                 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
973                         seb->ec = si->mean_ec;
974
975         err = paranoid_check_si(ubi, si);
976         if (err) {
977                 if (err > 0)
978                         err = -EINVAL;
979                 goto out_vidh;
980         }
981
982         ubi_free_vid_hdr(ubi, vidh);
983         kfree(ech);
984
985         return si;
986
987 out_vidh:
988         ubi_free_vid_hdr(ubi, vidh);
989 out_ech:
990         kfree(ech);
991 out_si:
992         ubi_scan_destroy_si(si);
993         return ERR_PTR(err);
994 }
995
996 /**
997  * destroy_sv - free the scanning volume information
998  * @sv: scanning volume information
999  *
1000  * This function destroys the volume RB-tree (@sv->root) and the scanning
1001  * volume information.
1002  */
1003 static void destroy_sv(struct ubi_scan_volume *sv)
1004 {
1005         struct ubi_scan_leb *seb;
1006         struct rb_node *this = sv->root.rb_node;
1007
1008         while (this) {
1009                 if (this->rb_left)
1010                         this = this->rb_left;
1011                 else if (this->rb_right)
1012                         this = this->rb_right;
1013                 else {
1014                         seb = rb_entry(this, struct ubi_scan_leb, u.rb);
1015                         this = rb_parent(this);
1016                         if (this) {
1017                                 if (this->rb_left == &seb->u.rb)
1018                                         this->rb_left = NULL;
1019                                 else
1020                                         this->rb_right = NULL;
1021                         }
1022
1023                         kfree(seb);
1024                 }
1025         }
1026         kfree(sv);
1027 }
1028
1029 /**
1030  * ubi_scan_destroy_si - destroy scanning information.
1031  * @si: scanning information
1032  */
1033 void ubi_scan_destroy_si(struct ubi_scan_info *si)
1034 {
1035         struct ubi_scan_leb *seb, *seb_tmp;
1036         struct ubi_scan_volume *sv;
1037         struct rb_node *rb;
1038
1039         list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1040                 list_del(&seb->u.list);
1041                 kfree(seb);
1042         }
1043         list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1044                 list_del(&seb->u.list);
1045                 kfree(seb);
1046         }
1047         list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1048                 list_del(&seb->u.list);
1049                 kfree(seb);
1050         }
1051         list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1052                 list_del(&seb->u.list);
1053                 kfree(seb);
1054         }
1055
1056         /* Destroy the volume RB-tree */
1057         rb = si->volumes.rb_node;
1058         while (rb) {
1059                 if (rb->rb_left)
1060                         rb = rb->rb_left;
1061                 else if (rb->rb_right)
1062                         rb = rb->rb_right;
1063                 else {
1064                         sv = rb_entry(rb, struct ubi_scan_volume, rb);
1065
1066                         rb = rb_parent(rb);
1067                         if (rb) {
1068                                 if (rb->rb_left == &sv->rb)
1069                                         rb->rb_left = NULL;
1070                                 else
1071                                         rb->rb_right = NULL;
1072                         }
1073
1074                         destroy_sv(sv);
1075                 }
1076         }
1077
1078         kfree(si);
1079 }
1080
1081 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1082
1083 /**
1084  * paranoid_check_si - check if the scanning information is correct and
1085  * consistent.
1086  * @ubi: UBI device description object
1087  * @si: scanning information
1088  *
1089  * This function returns zero if the scanning information is all right, %1 if
1090  * not and a negative error code if an error occurred.
1091  */
1092 static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1093 {
1094         int pnum, err, vols_found = 0;
1095         struct rb_node *rb1, *rb2;
1096         struct ubi_scan_volume *sv;
1097         struct ubi_scan_leb *seb, *last_seb;
1098         uint8_t *buf;
1099
1100         /*
1101          * At first, check that scanning information is OK.
1102          */
1103         ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1104                 int leb_count = 0;
1105
1106                 cond_resched();
1107
1108                 vols_found += 1;
1109
1110                 if (si->is_empty) {
1111                         ubi_err("bad is_empty flag");
1112                         goto bad_sv;
1113                 }
1114
1115                 if (sv->vol_id < 0 || sv->highest_lnum < 0 ||
1116                     sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 ||
1117                     sv->data_pad < 0 || sv->last_data_size < 0) {
1118                         ubi_err("negative values");
1119                         goto bad_sv;
1120                 }
1121
1122                 if (sv->vol_id >= UBI_MAX_VOLUMES &&
1123                     sv->vol_id < UBI_INTERNAL_VOL_START) {
1124                         ubi_err("bad vol_id");
1125                         goto bad_sv;
1126                 }
1127
1128                 if (sv->vol_id > si->highest_vol_id) {
1129                         ubi_err("highest_vol_id is %d, but vol_id %d is there",
1130                                 si->highest_vol_id, sv->vol_id);
1131                         goto out;
1132                 }
1133
1134                 if (sv->vol_type != UBI_DYNAMIC_VOLUME &&
1135                     sv->vol_type != UBI_STATIC_VOLUME) {
1136                         ubi_err("bad vol_type");
1137                         goto bad_sv;
1138                 }
1139
1140                 if (sv->data_pad > ubi->leb_size / 2) {
1141                         ubi_err("bad data_pad");
1142                         goto bad_sv;
1143                 }
1144
1145                 last_seb = NULL;
1146                 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1147                         cond_resched();
1148
1149                         last_seb = seb;
1150                         leb_count += 1;
1151
1152                         if (seb->pnum < 0 || seb->ec < 0) {
1153                                 ubi_err("negative values");
1154                                 goto bad_seb;
1155                         }
1156
1157                         if (seb->ec < si->min_ec) {
1158                                 ubi_err("bad si->min_ec (%d), %d found",
1159                                         si->min_ec, seb->ec);
1160                                 goto bad_seb;
1161                         }
1162
1163                         if (seb->ec > si->max_ec) {
1164                                 ubi_err("bad si->max_ec (%d), %d found",
1165                                         si->max_ec, seb->ec);
1166                                 goto bad_seb;
1167                         }
1168
1169                         if (seb->pnum >= ubi->peb_count) {
1170                                 ubi_err("too high PEB number %d, total PEBs %d",
1171                                         seb->pnum, ubi->peb_count);
1172                                 goto bad_seb;
1173                         }
1174
1175                         if (sv->vol_type == UBI_STATIC_VOLUME) {
1176                                 if (seb->lnum >= sv->used_ebs) {
1177                                         ubi_err("bad lnum or used_ebs");
1178                                         goto bad_seb;
1179                                 }
1180                         } else {
1181                                 if (sv->used_ebs != 0) {
1182                                         ubi_err("non-zero used_ebs");
1183                                         goto bad_seb;
1184                                 }
1185                         }
1186
1187                         if (seb->lnum > sv->highest_lnum) {
1188                                 ubi_err("incorrect highest_lnum or lnum");
1189                                 goto bad_seb;
1190                         }
1191                 }
1192
1193                 if (sv->leb_count != leb_count) {
1194                         ubi_err("bad leb_count, %d objects in the tree",
1195                                 leb_count);
1196                         goto bad_sv;
1197                 }
1198
1199                 if (!last_seb)
1200                         continue;
1201
1202                 seb = last_seb;
1203
1204                 if (seb->lnum != sv->highest_lnum) {
1205                         ubi_err("bad highest_lnum");
1206                         goto bad_seb;
1207                 }
1208         }
1209
1210         if (vols_found != si->vols_found) {
1211                 ubi_err("bad si->vols_found %d, should be %d",
1212                         si->vols_found, vols_found);
1213                 goto out;
1214         }
1215
1216         /* Check that scanning information is correct */
1217         ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1218                 last_seb = NULL;
1219                 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1220                         int vol_type;
1221
1222                         cond_resched();
1223
1224                         last_seb = seb;
1225
1226                         err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1);
1227                         if (err && err != UBI_IO_BITFLIPS) {
1228                                 ubi_err("VID header is not OK (%d)", err);
1229                                 if (err > 0)
1230                                         err = -EIO;
1231                                 return err;
1232                         }
1233
1234                         vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1235                                    UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1236                         if (sv->vol_type != vol_type) {
1237                                 ubi_err("bad vol_type");
1238                                 goto bad_vid_hdr;
1239                         }
1240
1241                         if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
1242                                 ubi_err("bad sqnum %llu", seb->sqnum);
1243                                 goto bad_vid_hdr;
1244                         }
1245
1246                         if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
1247                                 ubi_err("bad vol_id %d", sv->vol_id);
1248                                 goto bad_vid_hdr;
1249                         }
1250
1251                         if (sv->compat != vidh->compat) {
1252                                 ubi_err("bad compat %d", vidh->compat);
1253                                 goto bad_vid_hdr;
1254                         }
1255
1256                         if (seb->lnum != be32_to_cpu(vidh->lnum)) {
1257                                 ubi_err("bad lnum %d", seb->lnum);
1258                                 goto bad_vid_hdr;
1259                         }
1260
1261                         if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
1262                                 ubi_err("bad used_ebs %d", sv->used_ebs);
1263                                 goto bad_vid_hdr;
1264                         }
1265
1266                         if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
1267                                 ubi_err("bad data_pad %d", sv->data_pad);
1268                                 goto bad_vid_hdr;
1269                         }
1270
1271                         if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) {
1272                                 ubi_err("bad leb_ver %u", seb->leb_ver);
1273                                 goto bad_vid_hdr;
1274                         }
1275                 }
1276
1277                 if (!last_seb)
1278                         continue;
1279
1280                 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
1281                         ubi_err("bad highest_lnum %d", sv->highest_lnum);
1282                         goto bad_vid_hdr;
1283                 }
1284
1285                 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
1286                         ubi_err("bad last_data_size %d", sv->last_data_size);
1287                         goto bad_vid_hdr;
1288                 }
1289         }
1290
1291         /*
1292          * Make sure that all the physical eraseblocks are in one of the lists
1293          * or trees.
1294          */
1295         buf = kzalloc(ubi->peb_count, GFP_KERNEL);
1296         if (!buf)
1297                 return -ENOMEM;
1298
1299         for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1300                 err = ubi_io_is_bad(ubi, pnum);
1301                 if (err < 0) {
1302                         kfree(buf);
1303                         return err;
1304                 }
1305                 else if (err)
1306                         buf[pnum] = 1;
1307         }
1308
1309         ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb)
1310                 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb)
1311                         buf[seb->pnum] = 1;
1312
1313         list_for_each_entry(seb, &si->free, u.list)
1314                 buf[seb->pnum] = 1;
1315
1316         list_for_each_entry(seb, &si->corr, u.list)
1317                 buf[seb->pnum] = 1;
1318
1319         list_for_each_entry(seb, &si->erase, u.list)
1320                 buf[seb->pnum] = 1;
1321
1322         list_for_each_entry(seb, &si->alien, u.list)
1323                 buf[seb->pnum] = 1;
1324
1325         err = 0;
1326         for (pnum = 0; pnum < ubi->peb_count; pnum++)
1327                 if (!buf[pnum]) {
1328                         ubi_err("PEB %d is not referred", pnum);
1329                         err = 1;
1330                 }
1331
1332         kfree(buf);
1333         if (err)
1334                 goto out;
1335         return 0;
1336
1337 bad_seb:
1338         ubi_err("bad scanning information about LEB %d", seb->lnum);
1339         ubi_dbg_dump_seb(seb, 0);
1340         ubi_dbg_dump_sv(sv);
1341         goto out;
1342
1343 bad_sv:
1344         ubi_err("bad scanning information about volume %d", sv->vol_id);
1345         ubi_dbg_dump_sv(sv);
1346         goto out;
1347
1348 bad_vid_hdr:
1349         ubi_err("bad scanning information about volume %d", sv->vol_id);
1350         ubi_dbg_dump_sv(sv);
1351         ubi_dbg_dump_vid_hdr(vidh);
1352
1353 out:
1354         ubi_dbg_dump_stack();
1355         return 1;
1356 }
1357
1358 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */