Merge branch 'amd-iommu/fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/joro...
[pandora-kernel.git] / drivers / mtd / ubi / io.c
1 /*
2  * Copyright (c) International Business Machines Corp., 2006
3  * Copyright (c) Nokia Corporation, 2006, 2007
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Artem Bityutskiy (Битюцкий Артём)
20  */
21
22 /*
23  * UBI input/output sub-system.
24  *
25  * This sub-system provides a uniform way to work with all kinds of the
26  * underlying MTD devices. It also implements handy functions for reading and
27  * writing UBI headers.
28  *
29  * We are trying to have a paranoid mindset and not to trust to what we read
30  * from the flash media in order to be more secure and robust. So this
31  * sub-system validates every single header it reads from the flash media.
32  *
33  * Some words about how the eraseblock headers are stored.
34  *
35  * The erase counter header is always stored at offset zero. By default, the
36  * VID header is stored after the EC header at the closest aligned offset
37  * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID
38  * header at the closest aligned offset. But this default layout may be
39  * changed. For example, for different reasons (e.g., optimization) UBI may be
40  * asked to put the VID header at further offset, and even at an unaligned
41  * offset. Of course, if the offset of the VID header is unaligned, UBI adds
42  * proper padding in front of it. Data offset may also be changed but it has to
43  * be aligned.
44  *
45  * About minimal I/O units. In general, UBI assumes flash device model where
46  * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1,
47  * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the
48  * @ubi->mtd->writesize field. But as an exception, UBI admits of using another
49  * (smaller) minimal I/O unit size for EC and VID headers to make it possible
50  * to do different optimizations.
51  *
52  * This is extremely useful in case of NAND flashes which admit of several
53  * write operations to one NAND page. In this case UBI can fit EC and VID
54  * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal
55  * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still
56  * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI
57  * users.
58  *
59  * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so
60  * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID
61  * headers.
62  *
63  * Q: why not just to treat sub-page as a minimal I/O unit of this flash
64  * device, e.g., make @ubi->min_io_size = 512 in the example above?
65  *
66  * A: because when writing a sub-page, MTD still writes a full 2K page but the
67  * bytes which are no relevant to the sub-page are 0xFF. So, basically, writing
68  * 4x512 sub-pages is 4 times slower then writing one 2KiB NAND page. Thus, we
69  * prefer to use sub-pages only for EV and VID headers.
70  *
71  * As it was noted above, the VID header may start at a non-aligned offset.
72  * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page,
73  * the VID header may reside at offset 1984 which is the last 64 bytes of the
74  * last sub-page (EC header is always at offset zero). This causes some
75  * difficulties when reading and writing VID headers.
76  *
77  * Suppose we have a 64-byte buffer and we read a VID header at it. We change
78  * the data and want to write this VID header out. As we can only write in
79  * 512-byte chunks, we have to allocate one more buffer and copy our VID header
80  * to offset 448 of this buffer.
81  *
82  * The I/O sub-system does the following trick in order to avoid this extra
83  * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID
84  * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer.
85  * When the VID header is being written out, it shifts the VID header pointer
86  * back and writes the whole sub-page.
87  */
88
89 #include <linux/crc32.h>
90 #include <linux/err.h>
91 #include "ubi.h"
92
93 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
94 static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum);
95 static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
96 static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
97                                  const struct ubi_ec_hdr *ec_hdr);
98 static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
99 static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
100                                   const struct ubi_vid_hdr *vid_hdr);
101 static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
102                                  int len);
103 static int paranoid_check_empty(struct ubi_device *ubi, int pnum);
104 #else
105 #define paranoid_check_not_bad(ubi, pnum) 0
106 #define paranoid_check_peb_ec_hdr(ubi, pnum)  0
107 #define paranoid_check_ec_hdr(ubi, pnum, ec_hdr)  0
108 #define paranoid_check_peb_vid_hdr(ubi, pnum) 0
109 #define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
110 #define paranoid_check_all_ff(ubi, pnum, offset, len) 0
111 #define paranoid_check_empty(ubi, pnum) 0
112 #endif
113
114 /**
115  * ubi_io_read - read data from a physical eraseblock.
116  * @ubi: UBI device description object
117  * @buf: buffer where to store the read data
118  * @pnum: physical eraseblock number to read from
119  * @offset: offset within the physical eraseblock from where to read
120  * @len: how many bytes to read
121  *
122  * This function reads data from offset @offset of physical eraseblock @pnum
123  * and stores the read data in the @buf buffer. The following return codes are
124  * possible:
125  *
126  * o %0 if all the requested data were successfully read;
127  * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
128  *   correctable bit-flips were detected; this is harmless but may indicate
129  *   that this eraseblock may become bad soon (but do not have to);
130  * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
131  *   example it can be an ECC error in case of NAND; this most probably means
132  *   that the data is corrupted;
133  * o %-EIO if some I/O error occurred;
134  * o other negative error codes in case of other errors.
135  */
136 int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
137                 int len)
138 {
139         int err, retries = 0;
140         size_t read;
141         loff_t addr;
142
143         dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
144
145         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
146         ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
147         ubi_assert(len > 0);
148
149         err = paranoid_check_not_bad(ubi, pnum);
150         if (err)
151                 return err > 0 ? -EINVAL : err;
152
153         addr = (loff_t)pnum * ubi->peb_size + offset;
154 retry:
155         err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
156         if (err) {
157                 if (err == -EUCLEAN) {
158                         /*
159                          * -EUCLEAN is reported if there was a bit-flip which
160                          * was corrected, so this is harmless.
161                          *
162                          * We do not report about it here unless debugging is
163                          * enabled. A corresponding message will be printed
164                          * later, when it is has been scrubbed.
165                          */
166                         dbg_msg("fixable bit-flip detected at PEB %d", pnum);
167                         ubi_assert(len == read);
168                         return UBI_IO_BITFLIPS;
169                 }
170
171                 if (read != len && retries++ < UBI_IO_RETRIES) {
172                         dbg_io("error %d while reading %d bytes from PEB %d:%d,"
173                                " read only %zd bytes, retry",
174                                err, len, pnum, offset, read);
175                         yield();
176                         goto retry;
177                 }
178
179                 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
180                         "read %zd bytes", err, len, pnum, offset, read);
181                 ubi_dbg_dump_stack();
182
183                 /*
184                  * The driver should never return -EBADMSG if it failed to read
185                  * all the requested data. But some buggy drivers might do
186                  * this, so we change it to -EIO.
187                  */
188                 if (read != len && err == -EBADMSG) {
189                         ubi_assert(0);
190                         err = -EIO;
191                 }
192         } else {
193                 ubi_assert(len == read);
194
195                 if (ubi_dbg_is_bitflip()) {
196                         dbg_gen("bit-flip (emulated)");
197                         err = UBI_IO_BITFLIPS;
198                 }
199         }
200
201         return err;
202 }
203
204 /**
205  * ubi_io_write - write data to a physical eraseblock.
206  * @ubi: UBI device description object
207  * @buf: buffer with the data to write
208  * @pnum: physical eraseblock number to write to
209  * @offset: offset within the physical eraseblock where to write
210  * @len: how many bytes to write
211  *
212  * This function writes @len bytes of data from buffer @buf to offset @offset
213  * of physical eraseblock @pnum. If all the data were successfully written,
214  * zero is returned. If an error occurred, this function returns a negative
215  * error code. If %-EIO is returned, the physical eraseblock most probably went
216  * bad.
217  *
218  * Note, in case of an error, it is possible that something was still written
219  * to the flash media, but may be some garbage.
220  */
221 int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
222                  int len)
223 {
224         int err;
225         size_t written;
226         loff_t addr;
227
228         dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
229
230         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
231         ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
232         ubi_assert(offset % ubi->hdrs_min_io_size == 0);
233         ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
234
235         if (ubi->ro_mode) {
236                 ubi_err("read-only mode");
237                 return -EROFS;
238         }
239
240         /* The below has to be compiled out if paranoid checks are disabled */
241
242         err = paranoid_check_not_bad(ubi, pnum);
243         if (err)
244                 return err > 0 ? -EINVAL : err;
245
246         /* The area we are writing to has to contain all 0xFF bytes */
247         err = paranoid_check_all_ff(ubi, pnum, offset, len);
248         if (err)
249                 return err > 0 ? -EINVAL : err;
250
251         if (offset >= ubi->leb_start) {
252                 /*
253                  * We write to the data area of the physical eraseblock. Make
254                  * sure it has valid EC and VID headers.
255                  */
256                 err = paranoid_check_peb_ec_hdr(ubi, pnum);
257                 if (err)
258                         return err > 0 ? -EINVAL : err;
259                 err = paranoid_check_peb_vid_hdr(ubi, pnum);
260                 if (err)
261                         return err > 0 ? -EINVAL : err;
262         }
263
264         if (ubi_dbg_is_write_failure()) {
265                 dbg_err("cannot write %d bytes to PEB %d:%d "
266                         "(emulated)", len, pnum, offset);
267                 ubi_dbg_dump_stack();
268                 return -EIO;
269         }
270
271         addr = (loff_t)pnum * ubi->peb_size + offset;
272         err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf);
273         if (err) {
274                 ubi_err("error %d while writing %d bytes to PEB %d:%d, written"
275                         " %zd bytes", err, len, pnum, offset, written);
276                 ubi_dbg_dump_stack();
277         } else
278                 ubi_assert(written == len);
279
280         return err;
281 }
282
283 /**
284  * erase_callback - MTD erasure call-back.
285  * @ei: MTD erase information object.
286  *
287  * Note, even though MTD erase interface is asynchronous, all the current
288  * implementations are synchronous anyway.
289  */
290 static void erase_callback(struct erase_info *ei)
291 {
292         wake_up_interruptible((wait_queue_head_t *)ei->priv);
293 }
294
295 /**
296  * do_sync_erase - synchronously erase a physical eraseblock.
297  * @ubi: UBI device description object
298  * @pnum: the physical eraseblock number to erase
299  *
300  * This function synchronously erases physical eraseblock @pnum and returns
301  * zero in case of success and a negative error code in case of failure. If
302  * %-EIO is returned, the physical eraseblock most probably went bad.
303  */
304 static int do_sync_erase(struct ubi_device *ubi, int pnum)
305 {
306         int err, retries = 0;
307         struct erase_info ei;
308         wait_queue_head_t wq;
309
310         dbg_io("erase PEB %d", pnum);
311
312 retry:
313         init_waitqueue_head(&wq);
314         memset(&ei, 0, sizeof(struct erase_info));
315
316         ei.mtd      = ubi->mtd;
317         ei.addr     = (loff_t)pnum * ubi->peb_size;
318         ei.len      = ubi->peb_size;
319         ei.callback = erase_callback;
320         ei.priv     = (unsigned long)&wq;
321
322         err = ubi->mtd->erase(ubi->mtd, &ei);
323         if (err) {
324                 if (retries++ < UBI_IO_RETRIES) {
325                         dbg_io("error %d while erasing PEB %d, retry",
326                                err, pnum);
327                         yield();
328                         goto retry;
329                 }
330                 ubi_err("cannot erase PEB %d, error %d", pnum, err);
331                 ubi_dbg_dump_stack();
332                 return err;
333         }
334
335         err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
336                                            ei.state == MTD_ERASE_FAILED);
337         if (err) {
338                 ubi_err("interrupted PEB %d erasure", pnum);
339                 return -EINTR;
340         }
341
342         if (ei.state == MTD_ERASE_FAILED) {
343                 if (retries++ < UBI_IO_RETRIES) {
344                         dbg_io("error while erasing PEB %d, retry", pnum);
345                         yield();
346                         goto retry;
347                 }
348                 ubi_err("cannot erase PEB %d", pnum);
349                 ubi_dbg_dump_stack();
350                 return -EIO;
351         }
352
353         err = paranoid_check_all_ff(ubi, pnum, 0, ubi->peb_size);
354         if (err)
355                 return err > 0 ? -EINVAL : err;
356
357         if (ubi_dbg_is_erase_failure() && !err) {
358                 dbg_err("cannot erase PEB %d (emulated)", pnum);
359                 return -EIO;
360         }
361
362         return 0;
363 }
364
365 /**
366  * check_pattern - check if buffer contains only a certain byte pattern.
367  * @buf: buffer to check
368  * @patt: the pattern to check
369  * @size: buffer size in bytes
370  *
371  * This function returns %1 in there are only @patt bytes in @buf, and %0 if
372  * something else was also found.
373  */
374 static int check_pattern(const void *buf, uint8_t patt, int size)
375 {
376         int i;
377
378         for (i = 0; i < size; i++)
379                 if (((const uint8_t *)buf)[i] != patt)
380                         return 0;
381         return 1;
382 }
383
384 /* Patterns to write to a physical eraseblock when torturing it */
385 static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
386
387 /**
388  * torture_peb - test a supposedly bad physical eraseblock.
389  * @ubi: UBI device description object
390  * @pnum: the physical eraseblock number to test
391  *
392  * This function returns %-EIO if the physical eraseblock did not pass the
393  * test, a positive number of erase operations done if the test was
394  * successfully passed, and other negative error codes in case of other errors.
395  */
396 static int torture_peb(struct ubi_device *ubi, int pnum)
397 {
398         int err, i, patt_count;
399
400         ubi_msg("run torture test for PEB %d", pnum);
401         patt_count = ARRAY_SIZE(patterns);
402         ubi_assert(patt_count > 0);
403
404         mutex_lock(&ubi->buf_mutex);
405         for (i = 0; i < patt_count; i++) {
406                 err = do_sync_erase(ubi, pnum);
407                 if (err)
408                         goto out;
409
410                 /* Make sure the PEB contains only 0xFF bytes */
411                 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
412                 if (err)
413                         goto out;
414
415                 err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
416                 if (err == 0) {
417                         ubi_err("erased PEB %d, but a non-0xFF byte found",
418                                 pnum);
419                         err = -EIO;
420                         goto out;
421                 }
422
423                 /* Write a pattern and check it */
424                 memset(ubi->peb_buf1, patterns[i], ubi->peb_size);
425                 err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
426                 if (err)
427                         goto out;
428
429                 memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size);
430                 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
431                 if (err)
432                         goto out;
433
434                 err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size);
435                 if (err == 0) {
436                         ubi_err("pattern %x checking failed for PEB %d",
437                                 patterns[i], pnum);
438                         err = -EIO;
439                         goto out;
440                 }
441         }
442
443         err = patt_count;
444         ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum);
445
446 out:
447         mutex_unlock(&ubi->buf_mutex);
448         if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
449                 /*
450                  * If a bit-flip or data integrity error was detected, the test
451                  * has not passed because it happened on a freshly erased
452                  * physical eraseblock which means something is wrong with it.
453                  */
454                 ubi_err("read problems on freshly erased PEB %d, must be bad",
455                         pnum);
456                 err = -EIO;
457         }
458         return err;
459 }
460
461 /**
462  * ubi_io_sync_erase - synchronously erase a physical eraseblock.
463  * @ubi: UBI device description object
464  * @pnum: physical eraseblock number to erase
465  * @torture: if this physical eraseblock has to be tortured
466  *
467  * This function synchronously erases physical eraseblock @pnum. If @torture
468  * flag is not zero, the physical eraseblock is checked by means of writing
469  * different patterns to it and reading them back. If the torturing is enabled,
470  * the physical eraseblock is erased more than once.
471  *
472  * This function returns the number of erasures made in case of success, %-EIO
473  * if the erasure failed or the torturing test failed, and other negative error
474  * codes in case of other errors. Note, %-EIO means that the physical
475  * eraseblock is bad.
476  */
477 int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
478 {
479         int err, ret = 0;
480
481         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
482
483         err = paranoid_check_not_bad(ubi, pnum);
484         if (err != 0)
485                 return err > 0 ? -EINVAL : err;
486
487         if (ubi->ro_mode) {
488                 ubi_err("read-only mode");
489                 return -EROFS;
490         }
491
492         if (torture) {
493                 ret = torture_peb(ubi, pnum);
494                 if (ret < 0)
495                         return ret;
496         }
497
498         err = do_sync_erase(ubi, pnum);
499         if (err)
500                 return err;
501
502         return ret + 1;
503 }
504
505 /**
506  * ubi_io_is_bad - check if a physical eraseblock is bad.
507  * @ubi: UBI device description object
508  * @pnum: the physical eraseblock number to check
509  *
510  * This function returns a positive number if the physical eraseblock is bad,
511  * zero if not, and a negative error code if an error occurred.
512  */
513 int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
514 {
515         struct mtd_info *mtd = ubi->mtd;
516
517         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
518
519         if (ubi->bad_allowed) {
520                 int ret;
521
522                 ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
523                 if (ret < 0)
524                         ubi_err("error %d while checking if PEB %d is bad",
525                                 ret, pnum);
526                 else if (ret)
527                         dbg_io("PEB %d is bad", pnum);
528                 return ret;
529         }
530
531         return 0;
532 }
533
534 /**
535  * ubi_io_mark_bad - mark a physical eraseblock as bad.
536  * @ubi: UBI device description object
537  * @pnum: the physical eraseblock number to mark
538  *
539  * This function returns zero in case of success and a negative error code in
540  * case of failure.
541  */
542 int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
543 {
544         int err;
545         struct mtd_info *mtd = ubi->mtd;
546
547         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
548
549         if (ubi->ro_mode) {
550                 ubi_err("read-only mode");
551                 return -EROFS;
552         }
553
554         if (!ubi->bad_allowed)
555                 return 0;
556
557         err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
558         if (err)
559                 ubi_err("cannot mark PEB %d bad, error %d", pnum, err);
560         return err;
561 }
562
563 /**
564  * validate_ec_hdr - validate an erase counter header.
565  * @ubi: UBI device description object
566  * @ec_hdr: the erase counter header to check
567  *
568  * This function returns zero if the erase counter header is OK, and %1 if
569  * not.
570  */
571 static int validate_ec_hdr(const struct ubi_device *ubi,
572                            const struct ubi_ec_hdr *ec_hdr)
573 {
574         long long ec;
575         int vid_hdr_offset, leb_start;
576
577         ec = be64_to_cpu(ec_hdr->ec);
578         vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
579         leb_start = be32_to_cpu(ec_hdr->data_offset);
580
581         if (ec_hdr->version != UBI_VERSION) {
582                 ubi_err("node with incompatible UBI version found: "
583                         "this UBI version is %d, image version is %d",
584                         UBI_VERSION, (int)ec_hdr->version);
585                 goto bad;
586         }
587
588         if (vid_hdr_offset != ubi->vid_hdr_offset) {
589                 ubi_err("bad VID header offset %d, expected %d",
590                         vid_hdr_offset, ubi->vid_hdr_offset);
591                 goto bad;
592         }
593
594         if (leb_start != ubi->leb_start) {
595                 ubi_err("bad data offset %d, expected %d",
596                         leb_start, ubi->leb_start);
597                 goto bad;
598         }
599
600         if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
601                 ubi_err("bad erase counter %lld", ec);
602                 goto bad;
603         }
604
605         return 0;
606
607 bad:
608         ubi_err("bad EC header");
609         ubi_dbg_dump_ec_hdr(ec_hdr);
610         ubi_dbg_dump_stack();
611         return 1;
612 }
613
614 /**
615  * ubi_io_read_ec_hdr - read and check an erase counter header.
616  * @ubi: UBI device description object
617  * @pnum: physical eraseblock to read from
618  * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
619  * header
620  * @verbose: be verbose if the header is corrupted or was not found
621  *
622  * This function reads erase counter header from physical eraseblock @pnum and
623  * stores it in @ec_hdr. This function also checks CRC checksum of the read
624  * erase counter header. The following codes may be returned:
625  *
626  * o %0 if the CRC checksum is correct and the header was successfully read;
627  * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
628  *   and corrected by the flash driver; this is harmless but may indicate that
629  *   this eraseblock may become bad soon (but may be not);
630  * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error);
631  * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
632  * o a negative error code in case of failure.
633  */
634 int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
635                        struct ubi_ec_hdr *ec_hdr, int verbose)
636 {
637         int err, read_err = 0;
638         uint32_t crc, magic, hdr_crc;
639
640         dbg_io("read EC header from PEB %d", pnum);
641         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
642
643         err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
644         if (err) {
645                 if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
646                         return err;
647
648                 /*
649                  * We read all the data, but either a correctable bit-flip
650                  * occurred, or MTD reported about some data integrity error,
651                  * like an ECC error in case of NAND. The former is harmless,
652                  * the later may mean that the read data is corrupted. But we
653                  * have a CRC check-sum and we will detect this. If the EC
654                  * header is still OK, we just report this as there was a
655                  * bit-flip.
656                  */
657                 read_err = err;
658         }
659
660         magic = be32_to_cpu(ec_hdr->magic);
661         if (magic != UBI_EC_HDR_MAGIC) {
662                 /*
663                  * The magic field is wrong. Let's check if we have read all
664                  * 0xFF. If yes, this physical eraseblock is assumed to be
665                  * empty.
666                  *
667                  * But if there was a read error, we do not test it for all
668                  * 0xFFs. Even if it does contain all 0xFFs, this error
669                  * indicates that something is still wrong with this physical
670                  * eraseblock and we anyway cannot treat it as empty.
671                  */
672                 if (read_err != -EBADMSG &&
673                     check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
674                         /* The physical eraseblock is supposedly empty */
675                         err = paranoid_check_all_ff(ubi, pnum, 0,
676                                                     ubi->peb_size);
677                         if (err)
678                                 return err > 0 ? UBI_IO_BAD_EC_HDR : err;
679
680                         if (verbose)
681                                 ubi_warn("no EC header found at PEB %d, "
682                                          "only 0xFF bytes", pnum);
683                         else if (UBI_IO_DEBUG)
684                                 dbg_msg("no EC header found at PEB %d, "
685                                         "only 0xFF bytes", pnum);
686                         return UBI_IO_PEB_EMPTY;
687                 }
688
689                 /*
690                  * This is not a valid erase counter header, and these are not
691                  * 0xFF bytes. Report that the header is corrupted.
692                  */
693                 if (verbose) {
694                         ubi_warn("bad magic number at PEB %d: %08x instead of "
695                                  "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
696                         ubi_dbg_dump_ec_hdr(ec_hdr);
697                 } else if (UBI_IO_DEBUG)
698                         dbg_msg("bad magic number at PEB %d: %08x instead of "
699                                 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
700                 return UBI_IO_BAD_EC_HDR;
701         }
702
703         crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
704         hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
705
706         if (hdr_crc != crc) {
707                 if (verbose) {
708                         ubi_warn("bad EC header CRC at PEB %d, calculated "
709                                  "%#08x, read %#08x", pnum, crc, hdr_crc);
710                         ubi_dbg_dump_ec_hdr(ec_hdr);
711                 } else if (UBI_IO_DEBUG)
712                         dbg_msg("bad EC header CRC at PEB %d, calculated "
713                                 "%#08x, read %#08x", pnum, crc, hdr_crc);
714                 return UBI_IO_BAD_EC_HDR;
715         }
716
717         /* And of course validate what has just been read from the media */
718         err = validate_ec_hdr(ubi, ec_hdr);
719         if (err) {
720                 ubi_err("validation failed for PEB %d", pnum);
721                 return -EINVAL;
722         }
723
724         return read_err ? UBI_IO_BITFLIPS : 0;
725 }
726
727 /**
728  * ubi_io_write_ec_hdr - write an erase counter header.
729  * @ubi: UBI device description object
730  * @pnum: physical eraseblock to write to
731  * @ec_hdr: the erase counter header to write
732  *
733  * This function writes erase counter header described by @ec_hdr to physical
734  * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
735  * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
736  * field.
737  *
738  * This function returns zero in case of success and a negative error code in
739  * case of failure. If %-EIO is returned, the physical eraseblock most probably
740  * went bad.
741  */
742 int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
743                         struct ubi_ec_hdr *ec_hdr)
744 {
745         int err;
746         uint32_t crc;
747
748         dbg_io("write EC header to PEB %d", pnum);
749         ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
750
751         ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
752         ec_hdr->version = UBI_VERSION;
753         ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
754         ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
755         crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
756         ec_hdr->hdr_crc = cpu_to_be32(crc);
757
758         err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
759         if (err)
760                 return -EINVAL;
761
762         err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
763         return err;
764 }
765
766 /**
767  * validate_vid_hdr - validate a volume identifier header.
768  * @ubi: UBI device description object
769  * @vid_hdr: the volume identifier header to check
770  *
771  * This function checks that data stored in the volume identifier header
772  * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
773  */
774 static int validate_vid_hdr(const struct ubi_device *ubi,
775                             const struct ubi_vid_hdr *vid_hdr)
776 {
777         int vol_type = vid_hdr->vol_type;
778         int copy_flag = vid_hdr->copy_flag;
779         int vol_id = be32_to_cpu(vid_hdr->vol_id);
780         int lnum = be32_to_cpu(vid_hdr->lnum);
781         int compat = vid_hdr->compat;
782         int data_size = be32_to_cpu(vid_hdr->data_size);
783         int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
784         int data_pad = be32_to_cpu(vid_hdr->data_pad);
785         int data_crc = be32_to_cpu(vid_hdr->data_crc);
786         int usable_leb_size = ubi->leb_size - data_pad;
787
788         if (copy_flag != 0 && copy_flag != 1) {
789                 dbg_err("bad copy_flag");
790                 goto bad;
791         }
792
793         if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
794             data_pad < 0) {
795                 dbg_err("negative values");
796                 goto bad;
797         }
798
799         if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
800                 dbg_err("bad vol_id");
801                 goto bad;
802         }
803
804         if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
805                 dbg_err("bad compat");
806                 goto bad;
807         }
808
809         if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
810             compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
811             compat != UBI_COMPAT_REJECT) {
812                 dbg_err("bad compat");
813                 goto bad;
814         }
815
816         if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
817                 dbg_err("bad vol_type");
818                 goto bad;
819         }
820
821         if (data_pad >= ubi->leb_size / 2) {
822                 dbg_err("bad data_pad");
823                 goto bad;
824         }
825
826         if (vol_type == UBI_VID_STATIC) {
827                 /*
828                  * Although from high-level point of view static volumes may
829                  * contain zero bytes of data, but no VID headers can contain
830                  * zero at these fields, because they empty volumes do not have
831                  * mapped logical eraseblocks.
832                  */
833                 if (used_ebs == 0) {
834                         dbg_err("zero used_ebs");
835                         goto bad;
836                 }
837                 if (data_size == 0) {
838                         dbg_err("zero data_size");
839                         goto bad;
840                 }
841                 if (lnum < used_ebs - 1) {
842                         if (data_size != usable_leb_size) {
843                                 dbg_err("bad data_size");
844                                 goto bad;
845                         }
846                 } else if (lnum == used_ebs - 1) {
847                         if (data_size == 0) {
848                                 dbg_err("bad data_size at last LEB");
849                                 goto bad;
850                         }
851                 } else {
852                         dbg_err("too high lnum");
853                         goto bad;
854                 }
855         } else {
856                 if (copy_flag == 0) {
857                         if (data_crc != 0) {
858                                 dbg_err("non-zero data CRC");
859                                 goto bad;
860                         }
861                         if (data_size != 0) {
862                                 dbg_err("non-zero data_size");
863                                 goto bad;
864                         }
865                 } else {
866                         if (data_size == 0) {
867                                 dbg_err("zero data_size of copy");
868                                 goto bad;
869                         }
870                 }
871                 if (used_ebs != 0) {
872                         dbg_err("bad used_ebs");
873                         goto bad;
874                 }
875         }
876
877         return 0;
878
879 bad:
880         ubi_err("bad VID header");
881         ubi_dbg_dump_vid_hdr(vid_hdr);
882         ubi_dbg_dump_stack();
883         return 1;
884 }
885
886 /**
887  * ubi_io_read_vid_hdr - read and check a volume identifier header.
888  * @ubi: UBI device description object
889  * @pnum: physical eraseblock number to read from
890  * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
891  * identifier header
892  * @verbose: be verbose if the header is corrupted or wasn't found
893  *
894  * This function reads the volume identifier header from physical eraseblock
895  * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
896  * volume identifier header. The following codes may be returned:
897  *
898  * o %0 if the CRC checksum is correct and the header was successfully read;
899  * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
900  *   and corrected by the flash driver; this is harmless but may indicate that
901  *   this eraseblock may become bad soon;
902  * o %UBI_IO_BAD_VID_HDR if the volume identifier header is corrupted (a CRC
903  *   error detected);
904  * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
905  *   header there);
906  * o a negative error code in case of failure.
907  */
908 int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
909                         struct ubi_vid_hdr *vid_hdr, int verbose)
910 {
911         int err, read_err = 0;
912         uint32_t crc, magic, hdr_crc;
913         void *p;
914
915         dbg_io("read VID header from PEB %d", pnum);
916         ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
917
918         p = (char *)vid_hdr - ubi->vid_hdr_shift;
919         err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
920                           ubi->vid_hdr_alsize);
921         if (err) {
922                 if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
923                         return err;
924
925                 /*
926                  * We read all the data, but either a correctable bit-flip
927                  * occurred, or MTD reported about some data integrity error,
928                  * like an ECC error in case of NAND. The former is harmless,
929                  * the later may mean the read data is corrupted. But we have a
930                  * CRC check-sum and we will identify this. If the VID header is
931                  * still OK, we just report this as there was a bit-flip.
932                  */
933                 read_err = err;
934         }
935
936         magic = be32_to_cpu(vid_hdr->magic);
937         if (magic != UBI_VID_HDR_MAGIC) {
938                 /*
939                  * If we have read all 0xFF bytes, the VID header probably does
940                  * not exist and the physical eraseblock is assumed to be free.
941                  *
942                  * But if there was a read error, we do not test the data for
943                  * 0xFFs. Even if it does contain all 0xFFs, this error
944                  * indicates that something is still wrong with this physical
945                  * eraseblock and it cannot be regarded as free.
946                  */
947                 if (read_err != -EBADMSG &&
948                     check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
949                         /* The physical eraseblock is supposedly free */
950
951                         /*
952                          * The below is just a paranoid check, it has to be
953                          * compiled out if paranoid checks are disabled.
954                          */
955                         err = paranoid_check_empty(ubi, pnum);
956                         if (err)
957                                 return err > 0 ? UBI_IO_BAD_VID_HDR : err;
958
959                         if (verbose)
960                                 ubi_warn("no VID header found at PEB %d, "
961                                          "only 0xFF bytes", pnum);
962                         else if (UBI_IO_DEBUG)
963                                 dbg_msg("no VID header found at PEB %d, "
964                                         "only 0xFF bytes", pnum);
965                         return UBI_IO_PEB_FREE;
966                 }
967
968                 /*
969                  * This is not a valid VID header, and these are not 0xFF
970                  * bytes. Report that the header is corrupted.
971                  */
972                 if (verbose) {
973                         ubi_warn("bad magic number at PEB %d: %08x instead of "
974                                  "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
975                         ubi_dbg_dump_vid_hdr(vid_hdr);
976                 } else if (UBI_IO_DEBUG)
977                         dbg_msg("bad magic number at PEB %d: %08x instead of "
978                                 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
979                 return UBI_IO_BAD_VID_HDR;
980         }
981
982         crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
983         hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
984
985         if (hdr_crc != crc) {
986                 if (verbose) {
987                         ubi_warn("bad CRC at PEB %d, calculated %#08x, "
988                                  "read %#08x", pnum, crc, hdr_crc);
989                         ubi_dbg_dump_vid_hdr(vid_hdr);
990                 } else if (UBI_IO_DEBUG)
991                         dbg_msg("bad CRC at PEB %d, calculated %#08x, "
992                                 "read %#08x", pnum, crc, hdr_crc);
993                 return UBI_IO_BAD_VID_HDR;
994         }
995
996         /* Validate the VID header that we have just read */
997         err = validate_vid_hdr(ubi, vid_hdr);
998         if (err) {
999                 ubi_err("validation failed for PEB %d", pnum);
1000                 return -EINVAL;
1001         }
1002
1003         return read_err ? UBI_IO_BITFLIPS : 0;
1004 }
1005
1006 /**
1007  * ubi_io_write_vid_hdr - write a volume identifier header.
1008  * @ubi: UBI device description object
1009  * @pnum: the physical eraseblock number to write to
1010  * @vid_hdr: the volume identifier header to write
1011  *
1012  * This function writes the volume identifier header described by @vid_hdr to
1013  * physical eraseblock @pnum. This function automatically fills the
1014  * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
1015  * header CRC checksum and stores it at vid_hdr->hdr_crc.
1016  *
1017  * This function returns zero in case of success and a negative error code in
1018  * case of failure. If %-EIO is returned, the physical eraseblock probably went
1019  * bad.
1020  */
1021 int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1022                          struct ubi_vid_hdr *vid_hdr)
1023 {
1024         int err;
1025         uint32_t crc;
1026         void *p;
1027
1028         dbg_io("write VID header to PEB %d", pnum);
1029         ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
1030
1031         err = paranoid_check_peb_ec_hdr(ubi, pnum);
1032         if (err)
1033                 return err > 0 ? -EINVAL : err;
1034
1035         vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
1036         vid_hdr->version = UBI_VERSION;
1037         crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
1038         vid_hdr->hdr_crc = cpu_to_be32(crc);
1039
1040         err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1041         if (err)
1042                 return -EINVAL;
1043
1044         p = (char *)vid_hdr - ubi->vid_hdr_shift;
1045         err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1046                            ubi->vid_hdr_alsize);
1047         return err;
1048 }
1049
1050 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1051
1052 /**
1053  * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
1054  * @ubi: UBI device description object
1055  * @pnum: physical eraseblock number to check
1056  *
1057  * This function returns zero if the physical eraseblock is good, a positive
1058  * number if it is bad and a negative error code if an error occurred.
1059  */
1060 static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1061 {
1062         int err;
1063
1064         err = ubi_io_is_bad(ubi, pnum);
1065         if (!err)
1066                 return err;
1067
1068         ubi_err("paranoid check failed for PEB %d", pnum);
1069         ubi_dbg_dump_stack();
1070         return err;
1071 }
1072
1073 /**
1074  * paranoid_check_ec_hdr - check if an erase counter header is all right.
1075  * @ubi: UBI device description object
1076  * @pnum: physical eraseblock number the erase counter header belongs to
1077  * @ec_hdr: the erase counter header to check
1078  *
1079  * This function returns zero if the erase counter header contains valid
1080  * values, and %1 if not.
1081  */
1082 static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1083                                  const struct ubi_ec_hdr *ec_hdr)
1084 {
1085         int err;
1086         uint32_t magic;
1087
1088         magic = be32_to_cpu(ec_hdr->magic);
1089         if (magic != UBI_EC_HDR_MAGIC) {
1090                 ubi_err("bad magic %#08x, must be %#08x",
1091                         magic, UBI_EC_HDR_MAGIC);
1092                 goto fail;
1093         }
1094
1095         err = validate_ec_hdr(ubi, ec_hdr);
1096         if (err) {
1097                 ubi_err("paranoid check failed for PEB %d", pnum);
1098                 goto fail;
1099         }
1100
1101         return 0;
1102
1103 fail:
1104         ubi_dbg_dump_ec_hdr(ec_hdr);
1105         ubi_dbg_dump_stack();
1106         return 1;
1107 }
1108
1109 /**
1110  * paranoid_check_peb_ec_hdr - check erase counter header.
1111  * @ubi: UBI device description object
1112  * @pnum: the physical eraseblock number to check
1113  *
1114  * This function returns zero if the erase counter header is all right, %1 if
1115  * not, and a negative error code if an error occurred.
1116  */
1117 static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1118 {
1119         int err;
1120         uint32_t crc, hdr_crc;
1121         struct ubi_ec_hdr *ec_hdr;
1122
1123         ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1124         if (!ec_hdr)
1125                 return -ENOMEM;
1126
1127         err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
1128         if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1129                 goto exit;
1130
1131         crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
1132         hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
1133         if (hdr_crc != crc) {
1134                 ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
1135                 ubi_err("paranoid check failed for PEB %d", pnum);
1136                 ubi_dbg_dump_ec_hdr(ec_hdr);
1137                 ubi_dbg_dump_stack();
1138                 err = 1;
1139                 goto exit;
1140         }
1141
1142         err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
1143
1144 exit:
1145         kfree(ec_hdr);
1146         return err;
1147 }
1148
1149 /**
1150  * paranoid_check_vid_hdr - check that a volume identifier header is all right.
1151  * @ubi: UBI device description object
1152  * @pnum: physical eraseblock number the volume identifier header belongs to
1153  * @vid_hdr: the volume identifier header to check
1154  *
1155  * This function returns zero if the volume identifier header is all right, and
1156  * %1 if not.
1157  */
1158 static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1159                                   const struct ubi_vid_hdr *vid_hdr)
1160 {
1161         int err;
1162         uint32_t magic;
1163
1164         magic = be32_to_cpu(vid_hdr->magic);
1165         if (magic != UBI_VID_HDR_MAGIC) {
1166                 ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
1167                         magic, pnum, UBI_VID_HDR_MAGIC);
1168                 goto fail;
1169         }
1170
1171         err = validate_vid_hdr(ubi, vid_hdr);
1172         if (err) {
1173                 ubi_err("paranoid check failed for PEB %d", pnum);
1174                 goto fail;
1175         }
1176
1177         return err;
1178
1179 fail:
1180         ubi_err("paranoid check failed for PEB %d", pnum);
1181         ubi_dbg_dump_vid_hdr(vid_hdr);
1182         ubi_dbg_dump_stack();
1183         return 1;
1184
1185 }
1186
1187 /**
1188  * paranoid_check_peb_vid_hdr - check volume identifier header.
1189  * @ubi: UBI device description object
1190  * @pnum: the physical eraseblock number to check
1191  *
1192  * This function returns zero if the volume identifier header is all right,
1193  * %1 if not, and a negative error code if an error occurred.
1194  */
1195 static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1196 {
1197         int err;
1198         uint32_t crc, hdr_crc;
1199         struct ubi_vid_hdr *vid_hdr;
1200         void *p;
1201
1202         vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1203         if (!vid_hdr)
1204                 return -ENOMEM;
1205
1206         p = (char *)vid_hdr - ubi->vid_hdr_shift;
1207         err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1208                           ubi->vid_hdr_alsize);
1209         if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1210                 goto exit;
1211
1212         crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
1213         hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1214         if (hdr_crc != crc) {
1215                 ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
1216                         "read %#08x", pnum, crc, hdr_crc);
1217                 ubi_err("paranoid check failed for PEB %d", pnum);
1218                 ubi_dbg_dump_vid_hdr(vid_hdr);
1219                 ubi_dbg_dump_stack();
1220                 err = 1;
1221                 goto exit;
1222         }
1223
1224         err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1225
1226 exit:
1227         ubi_free_vid_hdr(ubi, vid_hdr);
1228         return err;
1229 }
1230
1231 /**
1232  * paranoid_check_all_ff - check that a region of flash is empty.
1233  * @ubi: UBI device description object
1234  * @pnum: the physical eraseblock number to check
1235  * @offset: the starting offset within the physical eraseblock to check
1236  * @len: the length of the region to check
1237  *
1238  * This function returns zero if only 0xFF bytes are present at offset
1239  * @offset of the physical eraseblock @pnum, %1 if not, and a negative error
1240  * code if an error occurred.
1241  */
1242 static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
1243                                  int len)
1244 {
1245         size_t read;
1246         int err;
1247         loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1248
1249         mutex_lock(&ubi->dbg_buf_mutex);
1250         err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf);
1251         if (err && err != -EUCLEAN) {
1252                 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1253                         "read %zd bytes", err, len, pnum, offset, read);
1254                 goto error;
1255         }
1256
1257         err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
1258         if (err == 0) {
1259                 ubi_err("flash region at PEB %d:%d, length %d does not "
1260                         "contain all 0xFF bytes", pnum, offset, len);
1261                 goto fail;
1262         }
1263         mutex_unlock(&ubi->dbg_buf_mutex);
1264
1265         return 0;
1266
1267 fail:
1268         ubi_err("paranoid check failed for PEB %d", pnum);
1269         ubi_msg("hex dump of the %d-%d region", offset, offset + len);
1270         print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1271                        ubi->dbg_peb_buf, len, 1);
1272         err = 1;
1273 error:
1274         ubi_dbg_dump_stack();
1275         mutex_unlock(&ubi->dbg_buf_mutex);
1276         return err;
1277 }
1278
1279 /**
1280  * paranoid_check_empty - whether a PEB is empty.
1281  * @ubi: UBI device description object
1282  * @pnum: the physical eraseblock number to check
1283  *
1284  * This function makes sure PEB @pnum is empty, which means it contains only
1285  * %0xFF data bytes. Returns zero if the PEB is empty, %1 if not, and a
1286  * negative error code in case of failure.
1287  *
1288  * Empty PEBs have the EC header, and do not have the VID header. The caller of
1289  * this function should have already made sure the PEB does not have the VID
1290  * header. However, this function re-checks that, because it is possible that
1291  * the header and data has already been written to the PEB.
1292  *
1293  * Let's consider a possible scenario. Suppose there are 2 tasks - A and B.
1294  * Task A is in 'wear_leveling_worker()'. It is reading VID header of PEB X to
1295  * find which LEB it corresponds to. PEB X is currently unmapped, and has no
1296  * VID header. Task B is trying to write to PEB X.
1297  *
1298  * Task A: in 'ubi_io_read_vid_hdr()': reads the VID header from PEB X. The
1299  *         read data contain all 0xFF bytes;
1300  * Task B: writes VID header and some data to PEB X;
1301  * Task A: assumes PEB X is empty, calls 'paranoid_check_empty()'. And if we
1302  *         do not re-read the VID header, and do not cancel the checking if it
1303  *         is there, we fail.
1304  */
1305 static int paranoid_check_empty(struct ubi_device *ubi, int pnum)
1306 {
1307         int err, offs = ubi->vid_hdr_aloffset, len = ubi->vid_hdr_alsize;
1308         size_t read;
1309         uint32_t magic;
1310         const struct ubi_vid_hdr *vid_hdr;
1311
1312         mutex_lock(&ubi->dbg_buf_mutex);
1313         err = ubi->mtd->read(ubi->mtd, offs, len, &read, ubi->dbg_peb_buf);
1314         if (err && err != -EUCLEAN) {
1315                 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1316                         "read %zd bytes", err, len, pnum, offs, read);
1317                 goto error;
1318         }
1319
1320         vid_hdr = ubi->dbg_peb_buf;
1321         magic = be32_to_cpu(vid_hdr->magic);
1322         if (magic == UBI_VID_HDR_MAGIC)
1323                 /* The PEB contains VID header, so it is not empty */
1324                 goto out;
1325
1326         err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
1327         if (err == 0) {
1328                 ubi_err("flash region at PEB %d:%d, length %d does not "
1329                         "contain all 0xFF bytes", pnum, offs, len);
1330                 goto fail;
1331         }
1332
1333 out:
1334         mutex_unlock(&ubi->dbg_buf_mutex);
1335         return 0;
1336
1337 fail:
1338         ubi_err("paranoid check failed for PEB %d", pnum);
1339         ubi_msg("hex dump of the %d-%d region", offs, offs + len);
1340         print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1341                        ubi->dbg_peb_buf, len, 1);
1342         err = 1;
1343 error:
1344         ubi_dbg_dump_stack();
1345         mutex_unlock(&ubi->dbg_buf_mutex);
1346         return err;
1347 }
1348
1349 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */