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