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