blob: f02a06fc35d5301aeacaf23d7faf3cb75db0bc11 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocherff94bc42014-06-24 10:10:04 +02002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02005 * Author: Artem Bityutskiy (Битюцкий Артём)
6 */
7
8/*
9 * UBI attaching sub-system.
10 *
11 * This sub-system is responsible for attaching MTD devices and it also
12 * implements flash media scanning.
13 *
14 * The attaching information is represented by a &struct ubi_attach_info'
15 * object. Information about volumes is represented by &struct ubi_ainf_volume
16 * objects which are kept in volume RB-tree with root at the @volumes field.
17 * The RB-tree is indexed by the volume ID.
18 *
19 * Logical eraseblocks are represented by &struct ubi_ainf_peb objects. These
20 * objects are kept in per-volume RB-trees with the root at the corresponding
21 * &struct ubi_ainf_volume object. To put it differently, we keep an RB-tree of
22 * per-volume objects and each of these objects is the root of RB-tree of
23 * per-LEB objects.
24 *
25 * Corrupted physical eraseblocks are put to the @corr list, free physical
26 * eraseblocks are put to the @free list and the physical eraseblock to be
27 * erased are put to the @erase list.
28 *
29 * About corruptions
30 * ~~~~~~~~~~~~~~~~~
31 *
32 * UBI protects EC and VID headers with CRC-32 checksums, so it can detect
33 * whether the headers are corrupted or not. Sometimes UBI also protects the
34 * data with CRC-32, e.g., when it executes the atomic LEB change operation, or
35 * when it moves the contents of a PEB for wear-leveling purposes.
36 *
37 * UBI tries to distinguish between 2 types of corruptions.
38 *
39 * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
40 * tries to handle them gracefully, without printing too many warnings and
41 * error messages. The idea is that we do not lose important data in these
42 * cases - we may lose only the data which were being written to the media just
43 * before the power cut happened, and the upper layers (e.g., UBIFS) are
44 * supposed to handle such data losses (e.g., by using the FS journal).
45 *
46 * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
47 * the reason is a power cut, UBI puts this PEB to the @erase list, and all
48 * PEBs in the @erase list are scheduled for erasure later.
49 *
50 * 2. Unexpected corruptions which are not caused by power cuts. During
51 * attaching, such PEBs are put to the @corr list and UBI preserves them.
52 * Obviously, this lessens the amount of available PEBs, and if at some point
53 * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
54 * about such PEBs every time the MTD device is attached.
55 *
56 * However, it is difficult to reliably distinguish between these types of
57 * corruptions and UBI's strategy is as follows (in case of attaching by
58 * scanning). UBI assumes corruption type 2 if the VID header is corrupted and
59 * the data area does not contain all 0xFFs, and there were no bit-flips or
60 * integrity errors (e.g., ECC errors in case of NAND) while reading the data
61 * area. Otherwise UBI assumes corruption type 1. So the decision criteria
62 * are as follows.
63 * o If the data area contains only 0xFFs, there are no data, and it is safe
64 * to just erase this PEB - this is corruption type 1.
65 * o If the data area has bit-flips or data integrity errors (ECC errors on
66 * NAND), it is probably a PEB which was being erased when power cut
67 * happened, so this is corruption type 1. However, this is just a guess,
68 * which might be wrong.
69 * o Otherwise this is corruption type 2.
70 */
71
Heiko Schocherff94bc42014-06-24 10:10:04 +020072#ifndef __UBOOT__
Simon Glass61b29b82020-02-03 07:36:15 -070073#include <dm/devres.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020074#include <linux/err.h>
75#include <linux/slab.h>
76#include <linux/crc32.h>
77#include <linux/random.h>
Simon Glass3db71102019-11-14 12:57:16 -070078#include <u-boot/crc.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020079#else
80#include <div64.h>
81#include <linux/err.h>
82#endif
83
84#include <linux/math64.h>
85
86#include <ubi_uboot.h>
87#include "ubi.h"
88
89static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai);
90
91/* Temporary variables used during scanning */
92static struct ubi_ec_hdr *ech;
93static struct ubi_vid_hdr *vidh;
94
95/**
96 * add_to_list - add physical eraseblock to a list.
97 * @ai: attaching information
98 * @pnum: physical eraseblock number to add
99 * @vol_id: the last used volume id for the PEB
100 * @lnum: the last used LEB number for the PEB
101 * @ec: erase counter of the physical eraseblock
102 * @to_head: if not zero, add to the head of the list
103 * @list: the list to add to
104 *
105 * This function allocates a 'struct ubi_ainf_peb' object for physical
106 * eraseblock @pnum and adds it to the "free", "erase", or "alien" lists.
107 * It stores the @lnum and @vol_id alongside, which can both be
108 * %UBI_UNKNOWN if they are not available, not readable, or not assigned.
109 * If @to_head is not zero, PEB will be added to the head of the list, which
110 * basically means it will be processed first later. E.g., we add corrupted
111 * PEBs (corrupted due to power cuts) to the head of the erase list to make
112 * sure we erase them first and get rid of corruptions ASAP. This function
113 * returns zero in case of success and a negative error code in case of
114 * failure.
115 */
116static int add_to_list(struct ubi_attach_info *ai, int pnum, int vol_id,
117 int lnum, int ec, int to_head, struct list_head *list)
118{
119 struct ubi_ainf_peb *aeb;
120
121 if (list == &ai->free) {
122 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
123 } else if (list == &ai->erase) {
124 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
125 } else if (list == &ai->alien) {
126 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
127 ai->alien_peb_count += 1;
128 } else
129 BUG();
130
131 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
132 if (!aeb)
133 return -ENOMEM;
134
135 aeb->pnum = pnum;
136 aeb->vol_id = vol_id;
137 aeb->lnum = lnum;
138 aeb->ec = ec;
139 if (to_head)
140 list_add(&aeb->u.list, list);
141 else
142 list_add_tail(&aeb->u.list, list);
143 return 0;
144}
145
146/**
147 * add_corrupted - add a corrupted physical eraseblock.
148 * @ai: attaching information
149 * @pnum: physical eraseblock number to add
150 * @ec: erase counter of the physical eraseblock
151 *
152 * This function allocates a 'struct ubi_ainf_peb' object for a corrupted
153 * physical eraseblock @pnum and adds it to the 'corr' list. The corruption
154 * was presumably not caused by a power cut. Returns zero in case of success
155 * and a negative error code in case of failure.
156 */
157static int add_corrupted(struct ubi_attach_info *ai, int pnum, int ec)
158{
159 struct ubi_ainf_peb *aeb;
160
161 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
162
163 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
164 if (!aeb)
165 return -ENOMEM;
166
167 ai->corr_peb_count += 1;
168 aeb->pnum = pnum;
169 aeb->ec = ec;
170 list_add(&aeb->u.list, &ai->corr);
171 return 0;
172}
173
174/**
175 * validate_vid_hdr - check volume identifier header.
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200176 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +0200177 * @vid_hdr: the volume identifier header to check
178 * @av: information about the volume this logical eraseblock belongs to
179 * @pnum: physical eraseblock number the VID header came from
180 *
181 * This function checks that data stored in @vid_hdr is consistent. Returns
182 * non-zero if an inconsistency was found and zero if not.
183 *
184 * Note, UBI does sanity check of everything it reads from the flash media.
185 * Most of the checks are done in the I/O sub-system. Here we check that the
186 * information in the VID header is consistent to the information in other VID
187 * headers of the same volume.
188 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200189static int validate_vid_hdr(const struct ubi_device *ubi,
190 const struct ubi_vid_hdr *vid_hdr,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200191 const struct ubi_ainf_volume *av, int pnum)
192{
193 int vol_type = vid_hdr->vol_type;
194 int vol_id = be32_to_cpu(vid_hdr->vol_id);
195 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
196 int data_pad = be32_to_cpu(vid_hdr->data_pad);
197
198 if (av->leb_count != 0) {
199 int av_vol_type;
200
201 /*
202 * This is not the first logical eraseblock belonging to this
203 * volume. Ensure that the data in its VID header is consistent
204 * to the data in previous logical eraseblock headers.
205 */
206
207 if (vol_id != av->vol_id) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200208 ubi_err(ubi, "inconsistent vol_id");
Heiko Schocherff94bc42014-06-24 10:10:04 +0200209 goto bad;
210 }
211
212 if (av->vol_type == UBI_STATIC_VOLUME)
213 av_vol_type = UBI_VID_STATIC;
214 else
215 av_vol_type = UBI_VID_DYNAMIC;
216
217 if (vol_type != av_vol_type) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200218 ubi_err(ubi, "inconsistent vol_type");
Heiko Schocherff94bc42014-06-24 10:10:04 +0200219 goto bad;
220 }
221
222 if (used_ebs != av->used_ebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200223 ubi_err(ubi, "inconsistent used_ebs");
Heiko Schocherff94bc42014-06-24 10:10:04 +0200224 goto bad;
225 }
226
227 if (data_pad != av->data_pad) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200228 ubi_err(ubi, "inconsistent data_pad");
Heiko Schocherff94bc42014-06-24 10:10:04 +0200229 goto bad;
230 }
231 }
232
233 return 0;
234
235bad:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200236 ubi_err(ubi, "inconsistent VID header at PEB %d", pnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200237 ubi_dump_vid_hdr(vid_hdr);
238 ubi_dump_av(av);
239 return -EINVAL;
240}
241
242/**
243 * add_volume - add volume to the attaching information.
244 * @ai: attaching information
245 * @vol_id: ID of the volume to add
246 * @pnum: physical eraseblock number
247 * @vid_hdr: volume identifier header
248 *
249 * If the volume corresponding to the @vid_hdr logical eraseblock is already
250 * present in the attaching information, this function does nothing. Otherwise
251 * it adds corresponding volume to the attaching information. Returns a pointer
252 * to the allocated "av" object in case of success and a negative error code in
253 * case of failure.
254 */
255static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *ai,
256 int vol_id, int pnum,
257 const struct ubi_vid_hdr *vid_hdr)
258{
259 struct ubi_ainf_volume *av;
260 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
261
262 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
263
264 /* Walk the volume RB-tree to look if this volume is already present */
265 while (*p) {
266 parent = *p;
267 av = rb_entry(parent, struct ubi_ainf_volume, rb);
268
269 if (vol_id == av->vol_id)
270 return av;
271
272 if (vol_id > av->vol_id)
273 p = &(*p)->rb_left;
274 else
275 p = &(*p)->rb_right;
276 }
277
278 /* The volume is absent - add it */
279 av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL);
280 if (!av)
281 return ERR_PTR(-ENOMEM);
282
283 av->highest_lnum = av->leb_count = 0;
284 av->vol_id = vol_id;
285 av->root = RB_ROOT;
286 av->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
287 av->data_pad = be32_to_cpu(vid_hdr->data_pad);
288 av->compat = vid_hdr->compat;
289 av->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
290 : UBI_STATIC_VOLUME;
291 if (vol_id > ai->highest_vol_id)
292 ai->highest_vol_id = vol_id;
293
294 rb_link_node(&av->rb, parent, p);
295 rb_insert_color(&av->rb, &ai->volumes);
296 ai->vols_found += 1;
297 dbg_bld("added volume %d", vol_id);
298 return av;
299}
300
301/**
302 * ubi_compare_lebs - find out which logical eraseblock is newer.
303 * @ubi: UBI device description object
304 * @aeb: first logical eraseblock to compare
305 * @pnum: physical eraseblock number of the second logical eraseblock to
306 * compare
307 * @vid_hdr: volume identifier header of the second logical eraseblock
308 *
309 * This function compares 2 copies of a LEB and informs which one is newer. In
310 * case of success this function returns a positive value, in case of failure, a
311 * negative error code is returned. The success return codes use the following
312 * bits:
313 * o bit 0 is cleared: the first PEB (described by @aeb) is newer than the
314 * second PEB (described by @pnum and @vid_hdr);
315 * o bit 0 is set: the second PEB is newer;
316 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
317 * o bit 1 is set: bit-flips were detected in the newer LEB;
318 * o bit 2 is cleared: the older LEB is not corrupted;
319 * o bit 2 is set: the older LEB is corrupted.
320 */
321int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
322 int pnum, const struct ubi_vid_hdr *vid_hdr)
323{
324 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
325 uint32_t data_crc, crc;
326 struct ubi_vid_hdr *vh = NULL;
327 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
328
329 if (sqnum2 == aeb->sqnum) {
330 /*
331 * This must be a really ancient UBI image which has been
332 * created before sequence numbers support has been added. At
333 * that times we used 32-bit LEB versions stored in logical
334 * eraseblocks. That was before UBI got into mainline. We do not
335 * support these images anymore. Well, those images still work,
336 * but only if no unclean reboots happened.
337 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200338 ubi_err(ubi, "unsupported on-flash UBI format");
Heiko Schocherff94bc42014-06-24 10:10:04 +0200339 return -EINVAL;
340 }
341
342 /* Obviously the LEB with lower sequence counter is older */
343 second_is_newer = (sqnum2 > aeb->sqnum);
344
345 /*
346 * Now we know which copy is newer. If the copy flag of the PEB with
347 * newer version is not set, then we just return, otherwise we have to
348 * check data CRC. For the second PEB we already have the VID header,
349 * for the first one - we'll need to re-read it from flash.
350 *
351 * Note: this may be optimized so that we wouldn't read twice.
352 */
353
354 if (second_is_newer) {
355 if (!vid_hdr->copy_flag) {
356 /* It is not a copy, so it is newer */
357 dbg_bld("second PEB %d is newer, copy_flag is unset",
358 pnum);
359 return 1;
360 }
361 } else {
362 if (!aeb->copy_flag) {
363 /* It is not a copy, so it is newer */
364 dbg_bld("first PEB %d is newer, copy_flag is unset",
365 pnum);
366 return bitflips << 1;
367 }
368
369 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
370 if (!vh)
371 return -ENOMEM;
372
373 pnum = aeb->pnum;
374 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
375 if (err) {
376 if (err == UBI_IO_BITFLIPS)
377 bitflips = 1;
378 else {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200379 ubi_err(ubi, "VID of PEB %d header is bad, but it was OK earlier, err %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200380 pnum, err);
381 if (err > 0)
382 err = -EIO;
383
384 goto out_free_vidh;
385 }
386 }
387
388 vid_hdr = vh;
389 }
390
391 /* Read the data of the copy and check the CRC */
392
393 len = be32_to_cpu(vid_hdr->data_size);
394
395 mutex_lock(&ubi->buf_mutex);
396 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len);
397 if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
398 goto out_unlock;
399
400 data_crc = be32_to_cpu(vid_hdr->data_crc);
401 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, len);
402 if (crc != data_crc) {
403 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
404 pnum, crc, data_crc);
405 corrupted = 1;
406 bitflips = 0;
407 second_is_newer = !second_is_newer;
408 } else {
409 dbg_bld("PEB %d CRC is OK", pnum);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200410 bitflips |= !!err;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200411 }
412 mutex_unlock(&ubi->buf_mutex);
413
414 ubi_free_vid_hdr(ubi, vh);
415
416 if (second_is_newer)
417 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
418 else
419 dbg_bld("first PEB %d is newer, copy_flag is set", pnum);
420
421 return second_is_newer | (bitflips << 1) | (corrupted << 2);
422
423out_unlock:
424 mutex_unlock(&ubi->buf_mutex);
425out_free_vidh:
426 ubi_free_vid_hdr(ubi, vh);
427 return err;
428}
429
430/**
431 * ubi_add_to_av - add used physical eraseblock to the attaching information.
432 * @ubi: UBI device description object
433 * @ai: attaching information
434 * @pnum: the physical eraseblock number
435 * @ec: erase counter
436 * @vid_hdr: the volume identifier header
437 * @bitflips: if bit-flips were detected when this physical eraseblock was read
438 *
439 * This function adds information about a used physical eraseblock to the
440 * 'used' tree of the corresponding volume. The function is rather complex
441 * because it has to handle cases when this is not the first physical
442 * eraseblock belonging to the same logical eraseblock, and the newer one has
443 * to be picked, while the older one has to be dropped. This function returns
444 * zero in case of success and a negative error code in case of failure.
445 */
446int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
447 int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips)
448{
449 int err, vol_id, lnum;
450 unsigned long long sqnum;
451 struct ubi_ainf_volume *av;
452 struct ubi_ainf_peb *aeb;
453 struct rb_node **p, *parent = NULL;
454
455 vol_id = be32_to_cpu(vid_hdr->vol_id);
456 lnum = be32_to_cpu(vid_hdr->lnum);
457 sqnum = be64_to_cpu(vid_hdr->sqnum);
458
459 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
460 pnum, vol_id, lnum, ec, sqnum, bitflips);
461
462 av = add_volume(ai, vol_id, pnum, vid_hdr);
463 if (IS_ERR(av))
464 return PTR_ERR(av);
465
466 if (ai->max_sqnum < sqnum)
467 ai->max_sqnum = sqnum;
468
469 /*
470 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
471 * if this is the first instance of this logical eraseblock or not.
472 */
473 p = &av->root.rb_node;
474 while (*p) {
475 int cmp_res;
476
477 parent = *p;
478 aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
479 if (lnum != aeb->lnum) {
480 if (lnum < aeb->lnum)
481 p = &(*p)->rb_left;
482 else
483 p = &(*p)->rb_right;
484 continue;
485 }
486
487 /*
488 * There is already a physical eraseblock describing the same
489 * logical eraseblock present.
490 */
491
492 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, EC %d",
493 aeb->pnum, aeb->sqnum, aeb->ec);
494
495 /*
496 * Make sure that the logical eraseblocks have different
497 * sequence numbers. Otherwise the image is bad.
498 *
499 * However, if the sequence number is zero, we assume it must
500 * be an ancient UBI image from the era when UBI did not have
501 * sequence numbers. We still can attach these images, unless
502 * there is a need to distinguish between old and new
503 * eraseblocks, in which case we'll refuse the image in
504 * 'ubi_compare_lebs()'. In other words, we attach old clean
505 * images, but refuse attaching old images with duplicated
506 * logical eraseblocks because there was an unclean reboot.
507 */
508 if (aeb->sqnum == sqnum && sqnum != 0) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200509 ubi_err(ubi, "two LEBs with same sequence number %llu",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200510 sqnum);
511 ubi_dump_aeb(aeb, 0);
512 ubi_dump_vid_hdr(vid_hdr);
513 return -EINVAL;
514 }
515
516 /*
517 * Now we have to drop the older one and preserve the newer
518 * one.
519 */
520 cmp_res = ubi_compare_lebs(ubi, aeb, pnum, vid_hdr);
521 if (cmp_res < 0)
522 return cmp_res;
523
524 if (cmp_res & 1) {
525 /*
526 * This logical eraseblock is newer than the one
527 * found earlier.
528 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200529 err = validate_vid_hdr(ubi, vid_hdr, av, pnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200530 if (err)
531 return err;
532
533 err = add_to_list(ai, aeb->pnum, aeb->vol_id,
534 aeb->lnum, aeb->ec, cmp_res & 4,
535 &ai->erase);
536 if (err)
537 return err;
538
539 aeb->ec = ec;
540 aeb->pnum = pnum;
541 aeb->vol_id = vol_id;
542 aeb->lnum = lnum;
543 aeb->scrub = ((cmp_res & 2) || bitflips);
544 aeb->copy_flag = vid_hdr->copy_flag;
545 aeb->sqnum = sqnum;
546
547 if (av->highest_lnum == lnum)
548 av->last_data_size =
549 be32_to_cpu(vid_hdr->data_size);
550
551 return 0;
552 } else {
553 /*
554 * This logical eraseblock is older than the one found
555 * previously.
556 */
557 return add_to_list(ai, pnum, vol_id, lnum, ec,
558 cmp_res & 4, &ai->erase);
559 }
560 }
561
562 /*
563 * We've met this logical eraseblock for the first time, add it to the
564 * attaching information.
565 */
566
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200567 err = validate_vid_hdr(ubi, vid_hdr, av, pnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200568 if (err)
569 return err;
570
571 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
572 if (!aeb)
573 return -ENOMEM;
574
575 aeb->ec = ec;
576 aeb->pnum = pnum;
577 aeb->vol_id = vol_id;
578 aeb->lnum = lnum;
579 aeb->scrub = bitflips;
580 aeb->copy_flag = vid_hdr->copy_flag;
581 aeb->sqnum = sqnum;
582
583 if (av->highest_lnum <= lnum) {
584 av->highest_lnum = lnum;
585 av->last_data_size = be32_to_cpu(vid_hdr->data_size);
586 }
587
588 av->leb_count += 1;
589 rb_link_node(&aeb->u.rb, parent, p);
590 rb_insert_color(&aeb->u.rb, &av->root);
591 return 0;
592}
593
594/**
595 * ubi_find_av - find volume in the attaching information.
596 * @ai: attaching information
597 * @vol_id: the requested volume ID
598 *
599 * This function returns a pointer to the volume description or %NULL if there
600 * are no data about this volume in the attaching information.
601 */
602struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
603 int vol_id)
604{
605 struct ubi_ainf_volume *av;
606 struct rb_node *p = ai->volumes.rb_node;
607
608 while (p) {
609 av = rb_entry(p, struct ubi_ainf_volume, rb);
610
611 if (vol_id == av->vol_id)
612 return av;
613
614 if (vol_id > av->vol_id)
615 p = p->rb_left;
616 else
617 p = p->rb_right;
618 }
619
620 return NULL;
621}
622
623/**
624 * ubi_remove_av - delete attaching information about a volume.
625 * @ai: attaching information
626 * @av: the volume attaching information to delete
627 */
628void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av)
629{
630 struct rb_node *rb;
631 struct ubi_ainf_peb *aeb;
632
633 dbg_bld("remove attaching information about volume %d", av->vol_id);
634
635 while ((rb = rb_first(&av->root))) {
636 aeb = rb_entry(rb, struct ubi_ainf_peb, u.rb);
637 rb_erase(&aeb->u.rb, &av->root);
638 list_add_tail(&aeb->u.list, &ai->erase);
639 }
640
641 rb_erase(&av->rb, &ai->volumes);
642 kfree(av);
643 ai->vols_found -= 1;
644}
645
646/**
647 * early_erase_peb - erase a physical eraseblock.
648 * @ubi: UBI device description object
649 * @ai: attaching information
650 * @pnum: physical eraseblock number to erase;
651 * @ec: erase counter value to write (%UBI_UNKNOWN if it is unknown)
652 *
653 * This function erases physical eraseblock 'pnum', and writes the erase
654 * counter header to it. This function should only be used on UBI device
655 * initialization stages, when the EBA sub-system had not been yet initialized.
656 * This function returns zero in case of success and a negative error code in
657 * case of failure.
658 */
659static int early_erase_peb(struct ubi_device *ubi,
660 const struct ubi_attach_info *ai, int pnum, int ec)
661{
662 int err;
663 struct ubi_ec_hdr *ec_hdr;
664
665 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
666 /*
667 * Erase counter overflow. Upgrade UBI and use 64-bit
668 * erase counters internally.
669 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200670 ubi_err(ubi, "erase counter overflow at PEB %d, EC %d",
671 pnum, ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200672 return -EINVAL;
673 }
674
675 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
676 if (!ec_hdr)
677 return -ENOMEM;
678
679 ec_hdr->ec = cpu_to_be64(ec);
680
681 err = ubi_io_sync_erase(ubi, pnum, 0);
682 if (err < 0)
683 goto out_free;
684
685 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr);
686
687out_free:
688 kfree(ec_hdr);
689 return err;
690}
691
692/**
693 * ubi_early_get_peb - get a free physical eraseblock.
694 * @ubi: UBI device description object
695 * @ai: attaching information
696 *
697 * This function returns a free physical eraseblock. It is supposed to be
698 * called on the UBI initialization stages when the wear-leveling sub-system is
699 * not initialized yet. This function picks a physical eraseblocks from one of
700 * the lists, writes the EC header if it is needed, and removes it from the
701 * list.
702 *
703 * This function returns a pointer to the "aeb" of the found free PEB in case
704 * of success and an error code in case of failure.
705 */
706struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
707 struct ubi_attach_info *ai)
708{
709 int err = 0;
710 struct ubi_ainf_peb *aeb, *tmp_aeb;
711
712 if (!list_empty(&ai->free)) {
713 aeb = list_entry(ai->free.next, struct ubi_ainf_peb, u.list);
714 list_del(&aeb->u.list);
715 dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec);
716 return aeb;
717 }
718
719 /*
720 * We try to erase the first physical eraseblock from the erase list
721 * and pick it if we succeed, or try to erase the next one if not. And
722 * so forth. We don't want to take care about bad eraseblocks here -
723 * they'll be handled later.
724 */
725 list_for_each_entry_safe(aeb, tmp_aeb, &ai->erase, u.list) {
726 if (aeb->ec == UBI_UNKNOWN)
727 aeb->ec = ai->mean_ec;
728
729 err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1);
730 if (err)
731 continue;
732
733 aeb->ec += 1;
734 list_del(&aeb->u.list);
735 dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec);
736 return aeb;
737 }
738
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200739 ubi_err(ubi, "no free eraseblocks");
Heiko Schocherff94bc42014-06-24 10:10:04 +0200740 return ERR_PTR(-ENOSPC);
741}
742
743/**
744 * check_corruption - check the data area of PEB.
745 * @ubi: UBI device description object
746 * @vid_hdr: the (corrupted) VID header of this PEB
747 * @pnum: the physical eraseblock number to check
748 *
749 * This is a helper function which is used to distinguish between VID header
750 * corruptions caused by power cuts and other reasons. If the PEB contains only
751 * 0xFF bytes in the data area, the VID header is most probably corrupted
752 * because of a power cut (%0 is returned in this case). Otherwise, it was
753 * probably corrupted for some other reasons (%1 is returned in this case). A
754 * negative error code is returned if a read error occurred.
755 *
756 * If the corruption reason was a power cut, UBI can safely erase this PEB.
757 * Otherwise, it should preserve it to avoid possibly destroying important
758 * information.
759 */
760static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
761 int pnum)
762{
763 int err;
764
765 mutex_lock(&ubi->buf_mutex);
766 memset(ubi->peb_buf, 0x00, ubi->leb_size);
767
768 err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start,
769 ubi->leb_size);
770 if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) {
771 /*
772 * Bit-flips or integrity errors while reading the data area.
773 * It is difficult to say for sure what type of corruption is
774 * this, but presumably a power cut happened while this PEB was
775 * erased, so it became unstable and corrupted, and should be
776 * erased.
777 */
778 err = 0;
779 goto out_unlock;
780 }
781
782 if (err)
783 goto out_unlock;
784
785 if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size))
786 goto out_unlock;
787
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200788 ubi_err(ubi, "PEB %d contains corrupted VID header, and the data does not contain all 0xFF",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200789 pnum);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200790 ubi_err(ubi, "this may be a non-UBI PEB or a severe VID header corruption which requires manual inspection");
Heiko Schocherff94bc42014-06-24 10:10:04 +0200791 ubi_dump_vid_hdr(vid_hdr);
792 pr_err("hexdump of PEB %d offset %d, length %d",
793 pnum, ubi->leb_start, ubi->leb_size);
Alexey Brodkinf8c987f2018-06-05 17:17:57 +0300794 ubi_dbg_print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200795 ubi->peb_buf, ubi->leb_size, 1);
796 err = 1;
797
798out_unlock:
799 mutex_unlock(&ubi->buf_mutex);
800 return err;
801}
802
803/**
804 * scan_peb - scan and process UBI headers of a PEB.
805 * @ubi: UBI device description object
806 * @ai: attaching information
807 * @pnum: the physical eraseblock number
808 * @vid: The volume ID of the found volume will be stored in this pointer
809 * @sqnum: The sqnum of the found volume will be stored in this pointer
810 *
811 * This function reads UBI headers of PEB @pnum, checks them, and adds
812 * information about this PEB to the corresponding list or RB-tree in the
813 * "attaching info" structure. Returns zero if the physical eraseblock was
814 * successfully handled and a negative error code in case of failure.
815 */
816static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai,
817 int pnum, int *vid, unsigned long long *sqnum)
818{
819 long long uninitialized_var(ec);
820 int err, bitflips = 0, vol_id = -1, ec_err = 0;
821
822 dbg_bld("scan PEB %d", pnum);
823
824 /* Skip bad physical eraseblocks */
825 err = ubi_io_is_bad(ubi, pnum);
826 if (err < 0)
827 return err;
828 else if (err) {
829 ai->bad_peb_count += 1;
830 return 0;
831 }
832
833 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
834 if (err < 0)
835 return err;
836 switch (err) {
837 case 0:
838 break;
839 case UBI_IO_BITFLIPS:
840 bitflips = 1;
841 break;
842 case UBI_IO_FF:
843 ai->empty_peb_count += 1;
844 return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
845 UBI_UNKNOWN, 0, &ai->erase);
846 case UBI_IO_FF_BITFLIPS:
847 ai->empty_peb_count += 1;
848 return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
849 UBI_UNKNOWN, 1, &ai->erase);
850 case UBI_IO_BAD_HDR_EBADMSG:
851 case UBI_IO_BAD_HDR:
852 /*
853 * We have to also look at the VID header, possibly it is not
854 * corrupted. Set %bitflips flag in order to make this PEB be
855 * moved and EC be re-created.
856 */
857 ec_err = err;
858 ec = UBI_UNKNOWN;
859 bitflips = 1;
860 break;
861 default:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200862 ubi_err(ubi, "'ubi_io_read_ec_hdr()' returned unknown code %d",
863 err);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200864 return -EINVAL;
865 }
866
867 if (!ec_err) {
868 int image_seq;
869
870 /* Make sure UBI version is OK */
871 if (ech->version != UBI_VERSION) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200872 ubi_err(ubi, "this UBI version is %d, image version is %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200873 UBI_VERSION, (int)ech->version);
874 return -EINVAL;
875 }
876
877 ec = be64_to_cpu(ech->ec);
878 if (ec > UBI_MAX_ERASECOUNTER) {
879 /*
880 * Erase counter overflow. The EC headers have 64 bits
881 * reserved, but we anyway make use of only 31 bit
882 * values, as this seems to be enough for any existing
883 * flash. Upgrade UBI and use 64-bit erase counters
884 * internally.
885 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200886 ubi_err(ubi, "erase counter overflow, max is %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200887 UBI_MAX_ERASECOUNTER);
888 ubi_dump_ec_hdr(ech);
889 return -EINVAL;
890 }
891
892 /*
893 * Make sure that all PEBs have the same image sequence number.
894 * This allows us to detect situations when users flash UBI
895 * images incorrectly, so that the flash has the new UBI image
896 * and leftovers from the old one. This feature was added
897 * relatively recently, and the sequence number was always
898 * zero, because old UBI implementations always set it to zero.
899 * For this reasons, we do not panic if some PEBs have zero
900 * sequence number, while other PEBs have non-zero sequence
901 * number.
902 */
903 image_seq = be32_to_cpu(ech->image_seq);
904 if (!ubi->image_seq)
905 ubi->image_seq = image_seq;
906 if (image_seq && ubi->image_seq != image_seq) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200907 ubi_err(ubi, "bad image sequence number %d in PEB %d, expected %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200908 image_seq, pnum, ubi->image_seq);
909 ubi_dump_ec_hdr(ech);
910 return -EINVAL;
911 }
912 }
913
914 /* OK, we've done with the EC header, let's look at the VID header */
915
916 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
917 if (err < 0)
918 return err;
919 switch (err) {
920 case 0:
921 break;
922 case UBI_IO_BITFLIPS:
923 bitflips = 1;
924 break;
925 case UBI_IO_BAD_HDR_EBADMSG:
926 if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
927 /*
928 * Both EC and VID headers are corrupted and were read
929 * with data integrity error, probably this is a bad
930 * PEB, bit it is not marked as bad yet. This may also
931 * be a result of power cut during erasure.
932 */
933 ai->maybe_bad_peb_count += 1;
934 case UBI_IO_BAD_HDR:
935 if (ec_err)
936 /*
937 * Both headers are corrupted. There is a possibility
938 * that this a valid UBI PEB which has corresponding
939 * LEB, but the headers are corrupted. However, it is
940 * impossible to distinguish it from a PEB which just
941 * contains garbage because of a power cut during erase
942 * operation. So we just schedule this PEB for erasure.
943 *
944 * Besides, in case of NOR flash, we deliberately
945 * corrupt both headers because NOR flash erasure is
946 * slow and can start from the end.
947 */
948 err = 0;
949 else
950 /*
951 * The EC was OK, but the VID header is corrupted. We
952 * have to check what is in the data area.
953 */
954 err = check_corruption(ubi, vidh, pnum);
955
956 if (err < 0)
957 return err;
958 else if (!err)
959 /* This corruption is caused by a power cut */
960 err = add_to_list(ai, pnum, UBI_UNKNOWN,
961 UBI_UNKNOWN, ec, 1, &ai->erase);
962 else
963 /* This is an unexpected corruption */
964 err = add_corrupted(ai, pnum, ec);
965 if (err)
966 return err;
967 goto adjust_mean_ec;
968 case UBI_IO_FF_BITFLIPS:
969 err = add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
970 ec, 1, &ai->erase);
971 if (err)
972 return err;
973 goto adjust_mean_ec;
974 case UBI_IO_FF:
975 if (ec_err || bitflips)
976 err = add_to_list(ai, pnum, UBI_UNKNOWN,
977 UBI_UNKNOWN, ec, 1, &ai->erase);
978 else
979 err = add_to_list(ai, pnum, UBI_UNKNOWN,
980 UBI_UNKNOWN, ec, 0, &ai->free);
981 if (err)
982 return err;
983 goto adjust_mean_ec;
984 default:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200985 ubi_err(ubi, "'ubi_io_read_vid_hdr()' returned unknown code %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200986 err);
987 return -EINVAL;
988 }
989
990 vol_id = be32_to_cpu(vidh->vol_id);
991 if (vid)
992 *vid = vol_id;
993 if (sqnum)
994 *sqnum = be64_to_cpu(vidh->sqnum);
995 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOLUME_ID) {
996 int lnum = be32_to_cpu(vidh->lnum);
997
998 /* Unsupported internal volume */
999 switch (vidh->compat) {
1000 case UBI_COMPAT_DELETE:
1001 if (vol_id != UBI_FM_SB_VOLUME_ID
1002 && vol_id != UBI_FM_DATA_VOLUME_ID) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001003 ubi_msg(ubi, "\"delete\" compatible internal volume %d:%d found, will remove it",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001004 vol_id, lnum);
1005 }
1006 err = add_to_list(ai, pnum, vol_id, lnum,
1007 ec, 1, &ai->erase);
1008 if (err)
1009 return err;
1010 return 0;
1011
1012 case UBI_COMPAT_RO:
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001013 ubi_msg(ubi, "read-only compatible internal volume %d:%d found, switch to read-only mode",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001014 vol_id, lnum);
1015 ubi->ro_mode = 1;
1016 break;
1017
1018 case UBI_COMPAT_PRESERVE:
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001019 ubi_msg(ubi, "\"preserve\" compatible internal volume %d:%d found",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001020 vol_id, lnum);
1021 err = add_to_list(ai, pnum, vol_id, lnum,
1022 ec, 0, &ai->alien);
1023 if (err)
1024 return err;
1025 return 0;
1026
1027 case UBI_COMPAT_REJECT:
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001028 ubi_err(ubi, "incompatible internal volume %d:%d found",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001029 vol_id, lnum);
1030 return -EINVAL;
1031 }
1032 }
1033
1034 if (ec_err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001035 ubi_warn(ubi, "valid VID header but corrupted EC header at PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001036 pnum);
1037 err = ubi_add_to_av(ubi, ai, pnum, ec, vidh, bitflips);
1038 if (err)
1039 return err;
1040
1041adjust_mean_ec:
1042 if (!ec_err) {
1043 ai->ec_sum += ec;
1044 ai->ec_count += 1;
1045 if (ec > ai->max_ec)
1046 ai->max_ec = ec;
1047 if (ec < ai->min_ec)
1048 ai->min_ec = ec;
1049 }
1050
1051 return 0;
1052}
1053
1054/**
1055 * late_analysis - analyze the overall situation with PEB.
1056 * @ubi: UBI device description object
1057 * @ai: attaching information
1058 *
1059 * This is a helper function which takes a look what PEBs we have after we
1060 * gather information about all of them ("ai" is compete). It decides whether
1061 * the flash is empty and should be formatted of whether there are too many
1062 * corrupted PEBs and we should not attach this MTD device. Returns zero if we
1063 * should proceed with attaching the MTD device, and %-EINVAL if we should not.
1064 */
1065static int late_analysis(struct ubi_device *ubi, struct ubi_attach_info *ai)
1066{
1067 struct ubi_ainf_peb *aeb;
1068 int max_corr, peb_count;
1069
1070 peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count;
1071 max_corr = peb_count / 20 ?: 8;
1072
1073 /*
1074 * Few corrupted PEBs is not a problem and may be just a result of
1075 * unclean reboots. However, many of them may indicate some problems
1076 * with the flash HW or driver.
1077 */
1078 if (ai->corr_peb_count) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001079 ubi_err(ubi, "%d PEBs are corrupted and preserved",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001080 ai->corr_peb_count);
1081 pr_err("Corrupted PEBs are:");
1082 list_for_each_entry(aeb, &ai->corr, u.list)
1083 pr_cont(" %d", aeb->pnum);
1084 pr_cont("\n");
1085
1086 /*
1087 * If too many PEBs are corrupted, we refuse attaching,
1088 * otherwise, only print a warning.
1089 */
1090 if (ai->corr_peb_count >= max_corr) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001091 ubi_err(ubi, "too many corrupted PEBs, refusing");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001092 return -EINVAL;
1093 }
1094 }
1095
1096 if (ai->empty_peb_count + ai->maybe_bad_peb_count == peb_count) {
1097 /*
1098 * All PEBs are empty, or almost all - a couple PEBs look like
1099 * they may be bad PEBs which were not marked as bad yet.
1100 *
1101 * This piece of code basically tries to distinguish between
1102 * the following situations:
1103 *
1104 * 1. Flash is empty, but there are few bad PEBs, which are not
1105 * marked as bad so far, and which were read with error. We
1106 * want to go ahead and format this flash. While formatting,
1107 * the faulty PEBs will probably be marked as bad.
1108 *
1109 * 2. Flash contains non-UBI data and we do not want to format
1110 * it and destroy possibly important information.
1111 */
1112 if (ai->maybe_bad_peb_count <= 2) {
1113 ai->is_empty = 1;
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001114 ubi_msg(ubi, "empty MTD device detected");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001115 get_random_bytes(&ubi->image_seq,
1116 sizeof(ubi->image_seq));
1117 } else {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001118 ubi_err(ubi, "MTD device is not UBI-formatted and possibly contains non-UBI data - refusing it");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001119 return -EINVAL;
1120 }
1121
1122 }
1123
1124 return 0;
1125}
1126
1127/**
1128 * destroy_av - free volume attaching information.
1129 * @av: volume attaching information
1130 * @ai: attaching information
1131 *
1132 * This function destroys the volume attaching information.
1133 */
1134static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av)
1135{
1136 struct ubi_ainf_peb *aeb;
1137 struct rb_node *this = av->root.rb_node;
1138
1139 while (this) {
1140 if (this->rb_left)
1141 this = this->rb_left;
1142 else if (this->rb_right)
1143 this = this->rb_right;
1144 else {
1145 aeb = rb_entry(this, struct ubi_ainf_peb, u.rb);
1146 this = rb_parent(this);
1147 if (this) {
1148 if (this->rb_left == &aeb->u.rb)
1149 this->rb_left = NULL;
1150 else
1151 this->rb_right = NULL;
1152 }
1153
1154 kmem_cache_free(ai->aeb_slab_cache, aeb);
1155 }
1156 }
1157 kfree(av);
1158}
1159
1160/**
1161 * destroy_ai - destroy attaching information.
1162 * @ai: attaching information
1163 */
1164static void destroy_ai(struct ubi_attach_info *ai)
1165{
1166 struct ubi_ainf_peb *aeb, *aeb_tmp;
1167 struct ubi_ainf_volume *av;
1168 struct rb_node *rb;
1169
1170 list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) {
1171 list_del(&aeb->u.list);
1172 kmem_cache_free(ai->aeb_slab_cache, aeb);
1173 }
1174 list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) {
1175 list_del(&aeb->u.list);
1176 kmem_cache_free(ai->aeb_slab_cache, aeb);
1177 }
1178 list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) {
1179 list_del(&aeb->u.list);
1180 kmem_cache_free(ai->aeb_slab_cache, aeb);
1181 }
1182 list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) {
1183 list_del(&aeb->u.list);
1184 kmem_cache_free(ai->aeb_slab_cache, aeb);
1185 }
1186
1187 /* Destroy the volume RB-tree */
1188 rb = ai->volumes.rb_node;
1189 while (rb) {
1190 if (rb->rb_left)
1191 rb = rb->rb_left;
1192 else if (rb->rb_right)
1193 rb = rb->rb_right;
1194 else {
1195 av = rb_entry(rb, struct ubi_ainf_volume, rb);
1196
1197 rb = rb_parent(rb);
1198 if (rb) {
1199 if (rb->rb_left == &av->rb)
1200 rb->rb_left = NULL;
1201 else
1202 rb->rb_right = NULL;
1203 }
1204
1205 destroy_av(ai, av);
1206 }
1207 }
1208
Heinrich Schuchardtcd5f33e2017-11-08 22:30:59 +01001209 kmem_cache_destroy(ai->aeb_slab_cache);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001210
1211 kfree(ai);
1212}
1213
1214/**
1215 * scan_all - scan entire MTD device.
1216 * @ubi: UBI device description object
1217 * @ai: attach info object
1218 * @start: start scanning at this PEB
1219 *
1220 * This function does full scanning of an MTD device and returns complete
1221 * information about it in form of a "struct ubi_attach_info" object. In case
1222 * of failure, an error code is returned.
1223 */
1224static int scan_all(struct ubi_device *ubi, struct ubi_attach_info *ai,
1225 int start)
1226{
1227 int err, pnum;
1228 struct rb_node *rb1, *rb2;
1229 struct ubi_ainf_volume *av;
1230 struct ubi_ainf_peb *aeb;
1231
1232 err = -ENOMEM;
1233
1234 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1235 if (!ech)
1236 return err;
1237
1238 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
1239 if (!vidh)
1240 goto out_ech;
1241
1242 for (pnum = start; pnum < ubi->peb_count; pnum++) {
1243 cond_resched();
1244
1245 dbg_gen("process PEB %d", pnum);
1246 err = scan_peb(ubi, ai, pnum, NULL, NULL);
1247 if (err < 0)
1248 goto out_vidh;
1249 }
1250
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001251 ubi_msg(ubi, "scanning is finished");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001252
1253 /* Calculate mean erase counter */
1254 if (ai->ec_count)
1255 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
1256
1257 err = late_analysis(ubi, ai);
1258 if (err)
1259 goto out_vidh;
1260
1261 /*
1262 * In case of unknown erase counter we use the mean erase counter
1263 * value.
1264 */
1265 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1266 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
1267 if (aeb->ec == UBI_UNKNOWN)
1268 aeb->ec = ai->mean_ec;
1269 }
1270
1271 list_for_each_entry(aeb, &ai->free, u.list) {
1272 if (aeb->ec == UBI_UNKNOWN)
1273 aeb->ec = ai->mean_ec;
1274 }
1275
1276 list_for_each_entry(aeb, &ai->corr, u.list)
1277 if (aeb->ec == UBI_UNKNOWN)
1278 aeb->ec = ai->mean_ec;
1279
1280 list_for_each_entry(aeb, &ai->erase, u.list)
1281 if (aeb->ec == UBI_UNKNOWN)
1282 aeb->ec = ai->mean_ec;
1283
1284 err = self_check_ai(ubi, ai);
1285 if (err)
1286 goto out_vidh;
1287
1288 ubi_free_vid_hdr(ubi, vidh);
1289 kfree(ech);
1290
1291 return 0;
1292
1293out_vidh:
1294 ubi_free_vid_hdr(ubi, vidh);
1295out_ech:
1296 kfree(ech);
1297 return err;
1298}
1299
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001300static struct ubi_attach_info *alloc_ai(void)
1301{
1302 struct ubi_attach_info *ai;
1303
1304 ai = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL);
1305 if (!ai)
1306 return ai;
1307
1308 INIT_LIST_HEAD(&ai->corr);
1309 INIT_LIST_HEAD(&ai->free);
1310 INIT_LIST_HEAD(&ai->erase);
1311 INIT_LIST_HEAD(&ai->alien);
1312 ai->volumes = RB_ROOT;
1313 ai->aeb_slab_cache = kmem_cache_create("ubi_aeb_slab_cache",
1314 sizeof(struct ubi_ainf_peb),
1315 0, 0, NULL);
1316 if (!ai->aeb_slab_cache) {
1317 kfree(ai);
1318 ai = NULL;
1319 }
1320
1321 return ai;
1322}
1323
Heiko Schocherff94bc42014-06-24 10:10:04 +02001324#ifdef CONFIG_MTD_UBI_FASTMAP
1325
1326/**
1327 * scan_fastmap - try to find a fastmap and attach from it.
1328 * @ubi: UBI device description object
1329 * @ai: attach info object
1330 *
1331 * Returns 0 on success, negative return values indicate an internal
1332 * error.
1333 * UBI_NO_FASTMAP denotes that no fastmap was found.
1334 * UBI_BAD_FASTMAP denotes that the found fastmap was invalid.
1335 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001336static int scan_fast(struct ubi_device *ubi, struct ubi_attach_info **ai)
Heiko Schocherff94bc42014-06-24 10:10:04 +02001337{
1338 int err, pnum, fm_anchor = -1;
1339 unsigned long long max_sqnum = 0;
1340
1341 err = -ENOMEM;
1342
1343 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1344 if (!ech)
1345 goto out;
1346
1347 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
1348 if (!vidh)
1349 goto out_ech;
1350
1351 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
1352 int vol_id = -1;
1353 unsigned long long sqnum = -1;
1354 cond_resched();
1355
1356 dbg_gen("process PEB %d", pnum);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001357 err = scan_peb(ubi, *ai, pnum, &vol_id, &sqnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001358 if (err < 0)
1359 goto out_vidh;
1360
1361 if (vol_id == UBI_FM_SB_VOLUME_ID && sqnum > max_sqnum) {
1362 max_sqnum = sqnum;
1363 fm_anchor = pnum;
1364 }
1365 }
1366
1367 ubi_free_vid_hdr(ubi, vidh);
1368 kfree(ech);
1369
1370 if (fm_anchor < 0)
1371 return UBI_NO_FASTMAP;
1372
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001373 destroy_ai(*ai);
1374 *ai = alloc_ai();
1375 if (!*ai)
1376 return -ENOMEM;
1377
1378 return ubi_scan_fastmap(ubi, *ai, fm_anchor);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001379
1380out_vidh:
1381 ubi_free_vid_hdr(ubi, vidh);
1382out_ech:
1383 kfree(ech);
1384out:
1385 return err;
1386}
1387
1388#endif
1389
Heiko Schocherff94bc42014-06-24 10:10:04 +02001390/**
1391 * ubi_attach - attach an MTD device.
1392 * @ubi: UBI device descriptor
1393 * @force_scan: if set to non-zero attach by scanning
1394 *
1395 * This function returns zero in case of success and a negative error code in
1396 * case of failure.
1397 */
1398int ubi_attach(struct ubi_device *ubi, int force_scan)
1399{
1400 int err;
1401 struct ubi_attach_info *ai;
1402
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001403 ai = alloc_ai();
Heiko Schocherff94bc42014-06-24 10:10:04 +02001404 if (!ai)
1405 return -ENOMEM;
1406
1407#ifdef CONFIG_MTD_UBI_FASTMAP
1408 /* On small flash devices we disable fastmap in any case. */
1409 if ((int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd) <= UBI_FM_MAX_START) {
1410 ubi->fm_disabled = 1;
1411 force_scan = 1;
1412 }
1413
1414 if (force_scan)
1415 err = scan_all(ubi, ai, 0);
1416 else {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001417 err = scan_fast(ubi, &ai);
1418 if (err > 0 || mtd_is_eccerr(err)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001419 if (err != UBI_NO_FASTMAP) {
1420 destroy_ai(ai);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001421 ai = alloc_ai();
Heiko Schocherff94bc42014-06-24 10:10:04 +02001422 if (!ai)
1423 return -ENOMEM;
1424
1425 err = scan_all(ubi, ai, 0);
1426 } else {
1427 err = scan_all(ubi, ai, UBI_FM_MAX_START);
1428 }
1429 }
1430 }
1431#else
1432 err = scan_all(ubi, ai, 0);
1433#endif
1434 if (err)
1435 goto out_ai;
1436
1437 ubi->bad_peb_count = ai->bad_peb_count;
1438 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
1439 ubi->corr_peb_count = ai->corr_peb_count;
1440 ubi->max_ec = ai->max_ec;
1441 ubi->mean_ec = ai->mean_ec;
1442 dbg_gen("max. sequence number: %llu", ai->max_sqnum);
1443
1444 err = ubi_read_volume_table(ubi, ai);
1445 if (err)
1446 goto out_ai;
1447
1448 err = ubi_wl_init(ubi, ai);
1449 if (err)
1450 goto out_vtbl;
1451
1452 err = ubi_eba_init(ubi, ai);
1453 if (err)
1454 goto out_wl;
1455
1456#ifdef CONFIG_MTD_UBI_FASTMAP
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001457 if (ubi->fm && ubi_dbg_chk_fastmap(ubi)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001458 struct ubi_attach_info *scan_ai;
1459
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001460 scan_ai = alloc_ai();
Heiko Schocherff94bc42014-06-24 10:10:04 +02001461 if (!scan_ai) {
1462 err = -ENOMEM;
1463 goto out_wl;
1464 }
1465
1466 err = scan_all(ubi, scan_ai, 0);
1467 if (err) {
1468 destroy_ai(scan_ai);
1469 goto out_wl;
1470 }
1471
1472 err = self_check_eba(ubi, ai, scan_ai);
1473 destroy_ai(scan_ai);
1474
1475 if (err)
1476 goto out_wl;
1477 }
1478#endif
1479
1480 destroy_ai(ai);
1481 return 0;
1482
1483out_wl:
1484 ubi_wl_close(ubi);
1485out_vtbl:
1486 ubi_free_internal_volumes(ubi);
1487 vfree(ubi->vtbl);
1488out_ai:
1489 destroy_ai(ai);
1490 return err;
1491}
1492
1493/**
1494 * self_check_ai - check the attaching information.
1495 * @ubi: UBI device description object
1496 * @ai: attaching information
1497 *
1498 * This function returns zero if the attaching information is all right, and a
1499 * negative error code if not or if an error occurred.
1500 */
1501static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai)
1502{
1503 int pnum, err, vols_found = 0;
1504 struct rb_node *rb1, *rb2;
1505 struct ubi_ainf_volume *av;
1506 struct ubi_ainf_peb *aeb, *last_aeb;
1507 uint8_t *buf;
1508
1509 if (!ubi_dbg_chk_gen(ubi))
1510 return 0;
1511
1512 /*
1513 * At first, check that attaching information is OK.
1514 */
1515 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1516 int leb_count = 0;
1517
1518 cond_resched();
1519
1520 vols_found += 1;
1521
1522 if (ai->is_empty) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001523 ubi_err(ubi, "bad is_empty flag");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001524 goto bad_av;
1525 }
1526
1527 if (av->vol_id < 0 || av->highest_lnum < 0 ||
1528 av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 ||
1529 av->data_pad < 0 || av->last_data_size < 0) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001530 ubi_err(ubi, "negative values");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001531 goto bad_av;
1532 }
1533
1534 if (av->vol_id >= UBI_MAX_VOLUMES &&
1535 av->vol_id < UBI_INTERNAL_VOL_START) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001536 ubi_err(ubi, "bad vol_id");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001537 goto bad_av;
1538 }
1539
1540 if (av->vol_id > ai->highest_vol_id) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001541 ubi_err(ubi, "highest_vol_id is %d, but vol_id %d is there",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001542 ai->highest_vol_id, av->vol_id);
1543 goto out;
1544 }
1545
1546 if (av->vol_type != UBI_DYNAMIC_VOLUME &&
1547 av->vol_type != UBI_STATIC_VOLUME) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001548 ubi_err(ubi, "bad vol_type");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001549 goto bad_av;
1550 }
1551
1552 if (av->data_pad > ubi->leb_size / 2) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001553 ubi_err(ubi, "bad data_pad");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001554 goto bad_av;
1555 }
1556
1557 last_aeb = NULL;
1558 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
1559 cond_resched();
1560
1561 last_aeb = aeb;
1562 leb_count += 1;
1563
1564 if (aeb->pnum < 0 || aeb->ec < 0) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001565 ubi_err(ubi, "negative values");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001566 goto bad_aeb;
1567 }
1568
1569 if (aeb->ec < ai->min_ec) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001570 ubi_err(ubi, "bad ai->min_ec (%d), %d found",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001571 ai->min_ec, aeb->ec);
1572 goto bad_aeb;
1573 }
1574
1575 if (aeb->ec > ai->max_ec) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001576 ubi_err(ubi, "bad ai->max_ec (%d), %d found",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001577 ai->max_ec, aeb->ec);
1578 goto bad_aeb;
1579 }
1580
1581 if (aeb->pnum >= ubi->peb_count) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001582 ubi_err(ubi, "too high PEB number %d, total PEBs %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001583 aeb->pnum, ubi->peb_count);
1584 goto bad_aeb;
1585 }
1586
1587 if (av->vol_type == UBI_STATIC_VOLUME) {
1588 if (aeb->lnum >= av->used_ebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001589 ubi_err(ubi, "bad lnum or used_ebs");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001590 goto bad_aeb;
1591 }
1592 } else {
1593 if (av->used_ebs != 0) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001594 ubi_err(ubi, "non-zero used_ebs");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001595 goto bad_aeb;
1596 }
1597 }
1598
1599 if (aeb->lnum > av->highest_lnum) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001600 ubi_err(ubi, "incorrect highest_lnum or lnum");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001601 goto bad_aeb;
1602 }
1603 }
1604
1605 if (av->leb_count != leb_count) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001606 ubi_err(ubi, "bad leb_count, %d objects in the tree",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001607 leb_count);
1608 goto bad_av;
1609 }
1610
1611 if (!last_aeb)
1612 continue;
1613
1614 aeb = last_aeb;
1615
1616 if (aeb->lnum != av->highest_lnum) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001617 ubi_err(ubi, "bad highest_lnum");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001618 goto bad_aeb;
1619 }
1620 }
1621
1622 if (vols_found != ai->vols_found) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001623 ubi_err(ubi, "bad ai->vols_found %d, should be %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001624 ai->vols_found, vols_found);
1625 goto out;
1626 }
1627
1628 /* Check that attaching information is correct */
1629 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1630 last_aeb = NULL;
1631 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
1632 int vol_type;
1633
1634 cond_resched();
1635
1636 last_aeb = aeb;
1637
1638 err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidh, 1);
1639 if (err && err != UBI_IO_BITFLIPS) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001640 ubi_err(ubi, "VID header is not OK (%d)",
1641 err);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001642 if (err > 0)
1643 err = -EIO;
1644 return err;
1645 }
1646
1647 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1648 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1649 if (av->vol_type != vol_type) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001650 ubi_err(ubi, "bad vol_type");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001651 goto bad_vid_hdr;
1652 }
1653
1654 if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001655 ubi_err(ubi, "bad sqnum %llu", aeb->sqnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001656 goto bad_vid_hdr;
1657 }
1658
1659 if (av->vol_id != be32_to_cpu(vidh->vol_id)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001660 ubi_err(ubi, "bad vol_id %d", av->vol_id);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001661 goto bad_vid_hdr;
1662 }
1663
1664 if (av->compat != vidh->compat) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001665 ubi_err(ubi, "bad compat %d", vidh->compat);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001666 goto bad_vid_hdr;
1667 }
1668
1669 if (aeb->lnum != be32_to_cpu(vidh->lnum)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001670 ubi_err(ubi, "bad lnum %d", aeb->lnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001671 goto bad_vid_hdr;
1672 }
1673
1674 if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001675 ubi_err(ubi, "bad used_ebs %d", av->used_ebs);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001676 goto bad_vid_hdr;
1677 }
1678
1679 if (av->data_pad != be32_to_cpu(vidh->data_pad)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001680 ubi_err(ubi, "bad data_pad %d", av->data_pad);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001681 goto bad_vid_hdr;
1682 }
1683 }
1684
1685 if (!last_aeb)
1686 continue;
1687
1688 if (av->highest_lnum != be32_to_cpu(vidh->lnum)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001689 ubi_err(ubi, "bad highest_lnum %d", av->highest_lnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001690 goto bad_vid_hdr;
1691 }
1692
1693 if (av->last_data_size != be32_to_cpu(vidh->data_size)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001694 ubi_err(ubi, "bad last_data_size %d",
1695 av->last_data_size);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001696 goto bad_vid_hdr;
1697 }
1698 }
1699
1700 /*
1701 * Make sure that all the physical eraseblocks are in one of the lists
1702 * or trees.
1703 */
1704 buf = kzalloc(ubi->peb_count, GFP_KERNEL);
1705 if (!buf)
1706 return -ENOMEM;
1707
1708 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1709 err = ubi_io_is_bad(ubi, pnum);
1710 if (err < 0) {
1711 kfree(buf);
1712 return err;
1713 } else if (err)
1714 buf[pnum] = 1;
1715 }
1716
1717 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
1718 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
1719 buf[aeb->pnum] = 1;
1720
1721 list_for_each_entry(aeb, &ai->free, u.list)
1722 buf[aeb->pnum] = 1;
1723
1724 list_for_each_entry(aeb, &ai->corr, u.list)
1725 buf[aeb->pnum] = 1;
1726
1727 list_for_each_entry(aeb, &ai->erase, u.list)
1728 buf[aeb->pnum] = 1;
1729
1730 list_for_each_entry(aeb, &ai->alien, u.list)
1731 buf[aeb->pnum] = 1;
1732
1733 err = 0;
1734 for (pnum = 0; pnum < ubi->peb_count; pnum++)
1735 if (!buf[pnum]) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001736 ubi_err(ubi, "PEB %d is not referred", pnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001737 err = 1;
1738 }
1739
1740 kfree(buf);
1741 if (err)
1742 goto out;
1743 return 0;
1744
1745bad_aeb:
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001746 ubi_err(ubi, "bad attaching information about LEB %d", aeb->lnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001747 ubi_dump_aeb(aeb, 0);
1748 ubi_dump_av(av);
1749 goto out;
1750
1751bad_av:
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001752 ubi_err(ubi, "bad attaching information about volume %d", av->vol_id);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001753 ubi_dump_av(av);
1754 goto out;
1755
1756bad_vid_hdr:
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001757 ubi_err(ubi, "bad attaching information about volume %d", av->vol_id);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001758 ubi_dump_av(av);
1759 ubi_dump_vid_hdr(vidh);
1760
1761out:
1762 dump_stack();
1763 return -EINVAL;
1764}