1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) International Business Machines Corp., 2006
5 * Author: Artem Bityutskiy (Битюцкий Артём)
9 * The UBI Eraseblock Association (EBA) sub-system.
11 * This sub-system is responsible for I/O to/from logical eraseblock.
13 * Although in this implementation the EBA table is fully kept and managed in
14 * RAM, which assumes poor scalability, it might be (partially) maintained on
15 * flash in future implementations.
17 * The EBA sub-system implements per-logical eraseblock locking. Before
18 * accessing a logical eraseblock it is locked for reading or writing. The
19 * per-logical eraseblock locking is implemented by means of the lock tree. The
20 * lock tree is an RB-tree which refers all the currently locked logical
21 * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects.
22 * They are indexed by (@vol_id, @lnum) pairs.
24 * EBA also maintains the global sequence counter which is incremented each
25 * time a logical eraseblock is mapped to a physical eraseblock and it is
26 * stored in the volume identifier header. This means that each VID header has
27 * a unique sequence number. The sequence number is only increased an we assume
28 * 64 bits is enough to never overflow.
32 #include <linux/slab.h>
33 #include <linux/crc32.h>
35 #include <ubi_uboot.h>
38 #include <linux/err.h>
41 /* Number of physical eraseblocks reserved for atomic LEB change operation */
42 #define EBA_RESERVED_PEBS 1
45 * next_sqnum - get next sequence number.
46 * @ubi: UBI device description object
48 * This function returns next sequence number to use, which is just the current
49 * global sequence counter value. It also increases the global sequence
52 unsigned long long ubi_next_sqnum(struct ubi_device *ubi)
54 unsigned long long sqnum;
56 spin_lock(&ubi->ltree_lock);
57 sqnum = ubi->global_sqnum++;
58 spin_unlock(&ubi->ltree_lock);
64 * ubi_get_compat - get compatibility flags of a volume.
65 * @ubi: UBI device description object
68 * This function returns compatibility flags for an internal volume. User
69 * volumes have no compatibility flags, so %0 is returned.
71 static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
73 if (vol_id == UBI_LAYOUT_VOLUME_ID)
74 return UBI_LAYOUT_VOLUME_COMPAT;
79 * ltree_lookup - look up the lock tree.
80 * @ubi: UBI device description object
82 * @lnum: logical eraseblock number
84 * This function returns a pointer to the corresponding &struct ubi_ltree_entry
85 * object if the logical eraseblock is locked and %NULL if it is not.
86 * @ubi->ltree_lock has to be locked.
88 static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
93 p = ubi->ltree.rb_node;
95 struct ubi_ltree_entry *le;
97 le = rb_entry(p, struct ubi_ltree_entry, rb);
99 if (vol_id < le->vol_id)
101 else if (vol_id > le->vol_id)
106 else if (lnum > le->lnum)
117 * ltree_add_entry - add new entry to the lock tree.
118 * @ubi: UBI device description object
120 * @lnum: logical eraseblock number
122 * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
123 * lock tree. If such entry is already there, its usage counter is increased.
124 * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
127 static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
128 int vol_id, int lnum)
130 struct ubi_ltree_entry *le, *le1, *le_free;
132 le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
134 return ERR_PTR(-ENOMEM);
137 init_rwsem(&le->mutex);
141 spin_lock(&ubi->ltree_lock);
142 le1 = ltree_lookup(ubi, vol_id, lnum);
146 * This logical eraseblock is already locked. The newly
147 * allocated lock entry is not needed.
152 struct rb_node **p, *parent = NULL;
155 * No lock entry, add the newly allocated one to the
156 * @ubi->ltree RB-tree.
160 p = &ubi->ltree.rb_node;
163 le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
165 if (vol_id < le1->vol_id)
167 else if (vol_id > le1->vol_id)
170 ubi_assert(lnum != le1->lnum);
171 if (lnum < le1->lnum)
178 rb_link_node(&le->rb, parent, p);
179 rb_insert_color(&le->rb, &ubi->ltree);
182 spin_unlock(&ubi->ltree_lock);
189 * leb_read_lock - lock logical eraseblock for reading.
190 * @ubi: UBI device description object
192 * @lnum: logical eraseblock number
194 * This function locks a logical eraseblock for reading. Returns zero in case
195 * of success and a negative error code in case of failure.
197 static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
199 struct ubi_ltree_entry *le;
201 le = ltree_add_entry(ubi, vol_id, lnum);
204 down_read(&le->mutex);
209 * leb_read_unlock - unlock logical eraseblock.
210 * @ubi: UBI device description object
212 * @lnum: logical eraseblock number
214 static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
216 struct ubi_ltree_entry *le;
218 spin_lock(&ubi->ltree_lock);
219 le = ltree_lookup(ubi, vol_id, lnum);
221 ubi_assert(le->users >= 0);
223 if (le->users == 0) {
224 rb_erase(&le->rb, &ubi->ltree);
227 spin_unlock(&ubi->ltree_lock);
231 * leb_write_lock - lock logical eraseblock for writing.
232 * @ubi: UBI device description object
234 * @lnum: logical eraseblock number
236 * This function locks a logical eraseblock for writing. Returns zero in case
237 * of success and a negative error code in case of failure.
239 static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
241 struct ubi_ltree_entry *le;
243 le = ltree_add_entry(ubi, vol_id, lnum);
246 down_write(&le->mutex);
251 * leb_write_lock - lock logical eraseblock for writing.
252 * @ubi: UBI device description object
254 * @lnum: logical eraseblock number
256 * This function locks a logical eraseblock for writing if there is no
257 * contention and does nothing if there is contention. Returns %0 in case of
258 * success, %1 in case of contention, and and a negative error code in case of
261 static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
263 struct ubi_ltree_entry *le;
265 le = ltree_add_entry(ubi, vol_id, lnum);
268 if (down_write_trylock(&le->mutex))
271 /* Contention, cancel */
272 spin_lock(&ubi->ltree_lock);
274 ubi_assert(le->users >= 0);
275 if (le->users == 0) {
276 rb_erase(&le->rb, &ubi->ltree);
279 spin_unlock(&ubi->ltree_lock);
285 * leb_write_unlock - unlock logical eraseblock.
286 * @ubi: UBI device description object
288 * @lnum: logical eraseblock number
290 static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
292 struct ubi_ltree_entry *le;
294 spin_lock(&ubi->ltree_lock);
295 le = ltree_lookup(ubi, vol_id, lnum);
297 ubi_assert(le->users >= 0);
298 up_write(&le->mutex);
299 if (le->users == 0) {
300 rb_erase(&le->rb, &ubi->ltree);
303 spin_unlock(&ubi->ltree_lock);
307 * ubi_eba_unmap_leb - un-map logical eraseblock.
308 * @ubi: UBI device description object
309 * @vol: volume description object
310 * @lnum: logical eraseblock number
312 * This function un-maps logical eraseblock @lnum and schedules corresponding
313 * physical eraseblock for erasure. Returns zero in case of success and a
314 * negative error code in case of failure.
316 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
319 int err, pnum, vol_id = vol->vol_id;
324 err = leb_write_lock(ubi, vol_id, lnum);
328 pnum = vol->eba_tbl[lnum];
330 /* This logical eraseblock is already unmapped */
333 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
335 down_read(&ubi->fm_eba_sem);
336 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
337 up_read(&ubi->fm_eba_sem);
338 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
341 leb_write_unlock(ubi, vol_id, lnum);
346 * ubi_eba_read_leb - read data.
347 * @ubi: UBI device description object
348 * @vol: volume description object
349 * @lnum: logical eraseblock number
350 * @buf: buffer to store the read data
351 * @offset: offset from where to read
352 * @len: how many bytes to read
353 * @check: data CRC check flag
355 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
356 * bytes. The @check flag only makes sense for static volumes and forces
357 * eraseblock data CRC checking.
359 * In case of success this function returns zero. In case of a static volume,
360 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
361 * returned for any volume type if an ECC error was detected by the MTD device
362 * driver. Other negative error cored may be returned in case of other errors.
364 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
365 void *buf, int offset, int len, int check)
367 int err, pnum, scrub = 0, vol_id = vol->vol_id;
368 struct ubi_vid_hdr *vid_hdr;
369 uint32_t uninitialized_var(crc);
371 err = leb_read_lock(ubi, vol_id, lnum);
375 pnum = vol->eba_tbl[lnum];
378 * The logical eraseblock is not mapped, fill the whole buffer
379 * with 0xFF bytes. The exception is static volumes for which
380 * it is an error to read unmapped logical eraseblocks.
382 dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
383 len, offset, vol_id, lnum);
384 leb_read_unlock(ubi, vol_id, lnum);
385 ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
386 memset(buf, 0xFF, len);
390 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
391 len, offset, vol_id, lnum, pnum);
393 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
398 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
404 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
405 if (err && err != UBI_IO_BITFLIPS) {
408 * The header is either absent or corrupted.
409 * The former case means there is a bug -
410 * switch to read-only mode just in case.
411 * The latter case means a real corruption - we
412 * may try to recover data. FIXME: but this is
415 if (err == UBI_IO_BAD_HDR_EBADMSG ||
416 err == UBI_IO_BAD_HDR) {
417 ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d",
426 } else if (err == UBI_IO_BITFLIPS)
429 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
430 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
432 crc = be32_to_cpu(vid_hdr->data_crc);
433 ubi_free_vid_hdr(ubi, vid_hdr);
436 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
438 if (err == UBI_IO_BITFLIPS)
440 else if (mtd_is_eccerr(err)) {
441 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
445 ubi_msg(ubi, "force data checking");
454 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
456 ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x",
464 err = ubi_wl_scrub_peb(ubi, pnum);
466 leb_read_unlock(ubi, vol_id, lnum);
470 ubi_free_vid_hdr(ubi, vid_hdr);
472 leb_read_unlock(ubi, vol_id, lnum);
478 * ubi_eba_read_leb_sg - read data into a scatter gather list.
479 * @ubi: UBI device description object
480 * @vol: volume description object
481 * @lnum: logical eraseblock number
482 * @sgl: UBI scatter gather list to store the read data
483 * @offset: offset from where to read
484 * @len: how many bytes to read
485 * @check: data CRC check flag
487 * This function works exactly like ubi_eba_read_leb(). But instead of
488 * storing the read data into a buffer it writes to an UBI scatter gather
491 int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
492 struct ubi_sgl *sgl, int lnum, int offset, int len,
497 struct scatterlist *sg;
500 ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT);
501 sg = &sgl->sg[sgl->list_pos];
502 if (len < sg->length - sgl->page_pos)
505 to_read = sg->length - sgl->page_pos;
507 ret = ubi_eba_read_leb(ubi, vol, lnum,
508 sg_virt(sg) + sgl->page_pos, offset,
516 sgl->page_pos += to_read;
517 if (sgl->page_pos == sg->length) {
534 * recover_peb - recover from write failure.
535 * @ubi: UBI device description object
536 * @pnum: the physical eraseblock to recover
538 * @lnum: logical eraseblock number
539 * @buf: data which was not written because of the write failure
540 * @offset: offset of the failed write
541 * @len: how many bytes should have been written
543 * This function is called in case of a write failure and moves all good data
544 * from the potentially bad physical eraseblock to a good physical eraseblock.
545 * This function also writes the data which was not written due to the failure.
546 * Returns new physical eraseblock number in case of success, and a negative
547 * error code in case of failure.
549 static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
550 const void *buf, int offset, int len)
552 int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
553 struct ubi_volume *vol = ubi->volumes[idx];
554 struct ubi_vid_hdr *vid_hdr;
556 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
561 new_pnum = ubi_wl_get_peb(ubi);
563 ubi_free_vid_hdr(ubi, vid_hdr);
564 up_read(&ubi->fm_eba_sem);
568 ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
571 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
572 if (err && err != UBI_IO_BITFLIPS) {
575 up_read(&ubi->fm_eba_sem);
579 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
580 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
582 up_read(&ubi->fm_eba_sem);
586 data_size = offset + len;
587 mutex_lock(&ubi->buf_mutex);
588 memset(ubi->peb_buf + offset, 0xFF, len);
590 /* Read everything before the area where the write failure happened */
592 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
593 if (err && err != UBI_IO_BITFLIPS) {
594 up_read(&ubi->fm_eba_sem);
599 memcpy(ubi->peb_buf + offset, buf, len);
601 err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
603 mutex_unlock(&ubi->buf_mutex);
604 up_read(&ubi->fm_eba_sem);
608 mutex_unlock(&ubi->buf_mutex);
609 ubi_free_vid_hdr(ubi, vid_hdr);
611 vol->eba_tbl[lnum] = new_pnum;
612 up_read(&ubi->fm_eba_sem);
613 ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
615 ubi_msg(ubi, "data was successfully recovered");
619 mutex_unlock(&ubi->buf_mutex);
621 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
622 ubi_free_vid_hdr(ubi, vid_hdr);
627 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
630 ubi_warn(ubi, "failed to write to PEB %d", new_pnum);
631 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
632 if (++tries > UBI_IO_RETRIES) {
633 ubi_free_vid_hdr(ubi, vid_hdr);
636 ubi_msg(ubi, "try again");
641 * ubi_eba_write_leb - write data to dynamic volume.
642 * @ubi: UBI device description object
643 * @vol: volume description object
644 * @lnum: logical eraseblock number
645 * @buf: the data to write
646 * @offset: offset within the logical eraseblock where to write
647 * @len: how many bytes to write
649 * This function writes data to logical eraseblock @lnum of a dynamic volume
650 * @vol. Returns zero in case of success and a negative error code in case
651 * of failure. In case of error, it is possible that something was still
652 * written to the flash media, but may be some garbage.
654 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
655 const void *buf, int offset, int len)
657 int err, pnum, tries = 0, vol_id = vol->vol_id;
658 struct ubi_vid_hdr *vid_hdr;
663 err = leb_write_lock(ubi, vol_id, lnum);
667 pnum = vol->eba_tbl[lnum];
669 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
670 len, offset, vol_id, lnum, pnum);
672 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
674 ubi_warn(ubi, "failed to write data to PEB %d", pnum);
675 if (err == -EIO && ubi->bad_allowed)
676 err = recover_peb(ubi, pnum, vol_id, lnum, buf,
681 leb_write_unlock(ubi, vol_id, lnum);
686 * The logical eraseblock is not mapped. We have to get a free physical
687 * eraseblock and write the volume identifier header there first.
689 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
691 leb_write_unlock(ubi, vol_id, lnum);
695 vid_hdr->vol_type = UBI_VID_DYNAMIC;
696 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
697 vid_hdr->vol_id = cpu_to_be32(vol_id);
698 vid_hdr->lnum = cpu_to_be32(lnum);
699 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
700 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
703 pnum = ubi_wl_get_peb(ubi);
705 ubi_free_vid_hdr(ubi, vid_hdr);
706 leb_write_unlock(ubi, vol_id, lnum);
707 up_read(&ubi->fm_eba_sem);
711 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
712 len, offset, vol_id, lnum, pnum);
714 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
716 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
718 up_read(&ubi->fm_eba_sem);
723 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
725 ubi_warn(ubi, "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
726 len, offset, vol_id, lnum, pnum);
727 up_read(&ubi->fm_eba_sem);
732 vol->eba_tbl[lnum] = pnum;
733 up_read(&ubi->fm_eba_sem);
735 leb_write_unlock(ubi, vol_id, lnum);
736 ubi_free_vid_hdr(ubi, vid_hdr);
740 if (err != -EIO || !ubi->bad_allowed) {
742 leb_write_unlock(ubi, vol_id, lnum);
743 ubi_free_vid_hdr(ubi, vid_hdr);
748 * Fortunately, this is the first write operation to this physical
749 * eraseblock, so just put it and request a new one. We assume that if
750 * this physical eraseblock went bad, the erase code will handle that.
752 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
753 if (err || ++tries > UBI_IO_RETRIES) {
755 leb_write_unlock(ubi, vol_id, lnum);
756 ubi_free_vid_hdr(ubi, vid_hdr);
760 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
761 ubi_msg(ubi, "try another PEB");
766 * ubi_eba_write_leb_st - write data to static volume.
767 * @ubi: UBI device description object
768 * @vol: volume description object
769 * @lnum: logical eraseblock number
770 * @buf: data to write
771 * @len: how many bytes to write
772 * @used_ebs: how many logical eraseblocks will this volume contain
774 * This function writes data to logical eraseblock @lnum of static volume
775 * @vol. The @used_ebs argument should contain total number of logical
776 * eraseblock in this static volume.
778 * When writing to the last logical eraseblock, the @len argument doesn't have
779 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
780 * to the real data size, although the @buf buffer has to contain the
781 * alignment. In all other cases, @len has to be aligned.
783 * It is prohibited to write more than once to logical eraseblocks of static
784 * volumes. This function returns zero in case of success and a negative error
785 * code in case of failure.
787 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
788 int lnum, const void *buf, int len, int used_ebs)
790 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
791 struct ubi_vid_hdr *vid_hdr;
797 if (lnum == used_ebs - 1)
798 /* If this is the last LEB @len may be unaligned */
799 len = ALIGN(data_size, ubi->min_io_size);
801 ubi_assert(!(len & (ubi->min_io_size - 1)));
803 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
807 err = leb_write_lock(ubi, vol_id, lnum);
809 ubi_free_vid_hdr(ubi, vid_hdr);
813 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
814 vid_hdr->vol_id = cpu_to_be32(vol_id);
815 vid_hdr->lnum = cpu_to_be32(lnum);
816 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
817 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
819 crc = crc32(UBI_CRC32_INIT, buf, data_size);
820 vid_hdr->vol_type = UBI_VID_STATIC;
821 vid_hdr->data_size = cpu_to_be32(data_size);
822 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
823 vid_hdr->data_crc = cpu_to_be32(crc);
826 pnum = ubi_wl_get_peb(ubi);
828 ubi_free_vid_hdr(ubi, vid_hdr);
829 leb_write_unlock(ubi, vol_id, lnum);
830 up_read(&ubi->fm_eba_sem);
834 dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
835 len, vol_id, lnum, pnum, used_ebs);
837 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
839 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
841 up_read(&ubi->fm_eba_sem);
845 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
847 ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
849 up_read(&ubi->fm_eba_sem);
853 ubi_assert(vol->eba_tbl[lnum] < 0);
854 vol->eba_tbl[lnum] = pnum;
855 up_read(&ubi->fm_eba_sem);
857 leb_write_unlock(ubi, vol_id, lnum);
858 ubi_free_vid_hdr(ubi, vid_hdr);
862 if (err != -EIO || !ubi->bad_allowed) {
864 * This flash device does not admit of bad eraseblocks or
865 * something nasty and unexpected happened. Switch to read-only
869 leb_write_unlock(ubi, vol_id, lnum);
870 ubi_free_vid_hdr(ubi, vid_hdr);
874 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
875 if (err || ++tries > UBI_IO_RETRIES) {
877 leb_write_unlock(ubi, vol_id, lnum);
878 ubi_free_vid_hdr(ubi, vid_hdr);
882 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
883 ubi_msg(ubi, "try another PEB");
888 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
889 * @ubi: UBI device description object
890 * @vol: volume description object
891 * @lnum: logical eraseblock number
892 * @buf: data to write
893 * @len: how many bytes to write
895 * This function changes the contents of a logical eraseblock atomically. @buf
896 * has to contain new logical eraseblock data, and @len - the length of the
897 * data, which has to be aligned. This function guarantees that in case of an
898 * unclean reboot the old contents is preserved. Returns zero in case of
899 * success and a negative error code in case of failure.
901 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
902 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
904 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
905 int lnum, const void *buf, int len)
907 int err, pnum, old_pnum, tries = 0, vol_id = vol->vol_id;
908 struct ubi_vid_hdr *vid_hdr;
916 * Special case when data length is zero. In this case the LEB
917 * has to be unmapped and mapped somewhere else.
919 err = ubi_eba_unmap_leb(ubi, vol, lnum);
922 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
925 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
929 mutex_lock(&ubi->alc_mutex);
930 err = leb_write_lock(ubi, vol_id, lnum);
934 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
935 vid_hdr->vol_id = cpu_to_be32(vol_id);
936 vid_hdr->lnum = cpu_to_be32(lnum);
937 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
938 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
940 crc = crc32(UBI_CRC32_INIT, buf, len);
941 vid_hdr->vol_type = UBI_VID_DYNAMIC;
942 vid_hdr->data_size = cpu_to_be32(len);
943 vid_hdr->copy_flag = 1;
944 vid_hdr->data_crc = cpu_to_be32(crc);
947 pnum = ubi_wl_get_peb(ubi);
950 up_read(&ubi->fm_eba_sem);
954 dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
955 vol_id, lnum, vol->eba_tbl[lnum], pnum);
957 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
959 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
961 up_read(&ubi->fm_eba_sem);
965 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
967 ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
969 up_read(&ubi->fm_eba_sem);
973 old_pnum = vol->eba_tbl[lnum];
974 vol->eba_tbl[lnum] = pnum;
975 up_read(&ubi->fm_eba_sem);
978 err = ubi_wl_put_peb(ubi, vol_id, lnum, old_pnum, 0);
984 leb_write_unlock(ubi, vol_id, lnum);
986 mutex_unlock(&ubi->alc_mutex);
987 ubi_free_vid_hdr(ubi, vid_hdr);
991 if (err != -EIO || !ubi->bad_allowed) {
993 * This flash device does not admit of bad eraseblocks or
994 * something nasty and unexpected happened. Switch to read-only
1001 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
1002 if (err || ++tries > UBI_IO_RETRIES) {
1004 goto out_leb_unlock;
1007 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1008 ubi_msg(ubi, "try another PEB");
1013 * is_error_sane - check whether a read error is sane.
1014 * @err: code of the error happened during reading
1016 * This is a helper function for 'ubi_eba_copy_leb()' which is called when we
1017 * cannot read data from the target PEB (an error @err happened). If the error
1018 * code is sane, then we treat this error as non-fatal. Otherwise the error is
1019 * fatal and UBI will be switched to R/O mode later.
1021 * The idea is that we try not to switch to R/O mode if the read error is
1022 * something which suggests there was a real read problem. E.g., %-EIO. Or a
1023 * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O
1024 * mode, simply because we do not know what happened at the MTD level, and we
1025 * cannot handle this. E.g., the underlying driver may have become crazy, and
1026 * it is safer to switch to R/O mode to preserve the data.
1028 * And bear in mind, this is about reading from the target PEB, i.e. the PEB
1029 * which we have just written.
1031 static int is_error_sane(int err)
1033 if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
1034 err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
1040 * ubi_eba_copy_leb - copy logical eraseblock.
1041 * @ubi: UBI device description object
1042 * @from: physical eraseblock number from where to copy
1043 * @to: physical eraseblock number where to copy
1044 * @vid_hdr: VID header of the @from physical eraseblock
1046 * This function copies logical eraseblock from physical eraseblock @from to
1047 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
1048 * function. Returns:
1049 * o %0 in case of success;
1050 * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
1051 * o a negative error code in case of failure.
1053 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1054 struct ubi_vid_hdr *vid_hdr)
1056 int err, vol_id, lnum, data_size, aldata_size, idx;
1057 struct ubi_volume *vol;
1060 vol_id = be32_to_cpu(vid_hdr->vol_id);
1061 lnum = be32_to_cpu(vid_hdr->lnum);
1063 dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
1065 if (vid_hdr->vol_type == UBI_VID_STATIC) {
1066 data_size = be32_to_cpu(vid_hdr->data_size);
1067 aldata_size = ALIGN(data_size, ubi->min_io_size);
1069 data_size = aldata_size =
1070 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
1072 idx = vol_id2idx(ubi, vol_id);
1073 spin_lock(&ubi->volumes_lock);
1075 * Note, we may race with volume deletion, which means that the volume
1076 * this logical eraseblock belongs to might be being deleted. Since the
1077 * volume deletion un-maps all the volume's logical eraseblocks, it will
1078 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
1080 vol = ubi->volumes[idx];
1081 spin_unlock(&ubi->volumes_lock);
1083 /* No need to do further work, cancel */
1084 dbg_wl("volume %d is being removed, cancel", vol_id);
1085 return MOVE_CANCEL_RACE;
1089 * We do not want anybody to write to this logical eraseblock while we
1090 * are moving it, so lock it.
1092 * Note, we are using non-waiting locking here, because we cannot sleep
1093 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
1094 * unmapping the LEB which is mapped to the PEB we are going to move
1095 * (@from). This task locks the LEB and goes sleep in the
1096 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
1097 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
1098 * LEB is already locked, we just do not move it and return
1099 * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because
1100 * we do not know the reasons of the contention - it may be just a
1101 * normal I/O on this LEB, so we want to re-try.
1103 err = leb_write_trylock(ubi, vol_id, lnum);
1105 dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
1110 * The LEB might have been put meanwhile, and the task which put it is
1111 * probably waiting on @ubi->move_mutex. No need to continue the work,
1114 if (vol->eba_tbl[lnum] != from) {
1115 dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
1116 vol_id, lnum, from, vol->eba_tbl[lnum]);
1117 err = MOVE_CANCEL_RACE;
1118 goto out_unlock_leb;
1122 * OK, now the LEB is locked and we can safely start moving it. Since
1123 * this function utilizes the @ubi->peb_buf buffer which is shared
1124 * with some other functions - we lock the buffer by taking the
1127 mutex_lock(&ubi->buf_mutex);
1128 dbg_wl("read %d bytes of data", aldata_size);
1129 err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
1130 if (err && err != UBI_IO_BITFLIPS) {
1131 ubi_warn(ubi, "error %d while reading data from PEB %d",
1133 err = MOVE_SOURCE_RD_ERR;
1134 goto out_unlock_buf;
1138 * Now we have got to calculate how much data we have to copy. In
1139 * case of a static volume it is fairly easy - the VID header contains
1140 * the data size. In case of a dynamic volume it is more difficult - we
1141 * have to read the contents, cut 0xFF bytes from the end and copy only
1142 * the first part. We must do this to avoid writing 0xFF bytes as it
1143 * may have some side-effects. And not only this. It is important not
1144 * to include those 0xFFs to CRC because later the they may be filled
1147 if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1148 aldata_size = data_size =
1149 ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
1152 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
1156 * It may turn out to be that the whole @from physical eraseblock
1157 * contains only 0xFF bytes. Then we have to only write the VID header
1158 * and do not write any data. This also means we should not set
1159 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1161 if (data_size > 0) {
1162 vid_hdr->copy_flag = 1;
1163 vid_hdr->data_size = cpu_to_be32(data_size);
1164 vid_hdr->data_crc = cpu_to_be32(crc);
1166 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1168 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1171 err = MOVE_TARGET_WR_ERR;
1172 goto out_unlock_buf;
1177 /* Read the VID header back and check if it was written correctly */
1178 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1180 if (err != UBI_IO_BITFLIPS) {
1181 ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
1183 if (is_error_sane(err))
1184 err = MOVE_TARGET_RD_ERR;
1186 err = MOVE_TARGET_BITFLIPS;
1187 goto out_unlock_buf;
1190 if (data_size > 0) {
1191 err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1194 err = MOVE_TARGET_WR_ERR;
1195 goto out_unlock_buf;
1201 * We've written the data and are going to read it back to make
1202 * sure it was written correctly.
1204 memset(ubi->peb_buf, 0xFF, aldata_size);
1205 err = ubi_io_read_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1207 if (err != UBI_IO_BITFLIPS) {
1208 ubi_warn(ubi, "error %d while reading data back from PEB %d",
1210 if (is_error_sane(err))
1211 err = MOVE_TARGET_RD_ERR;
1213 err = MOVE_TARGET_BITFLIPS;
1214 goto out_unlock_buf;
1219 if (crc != crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size)) {
1220 ubi_warn(ubi, "read data back from PEB %d and it is different",
1223 goto out_unlock_buf;
1227 ubi_assert(vol->eba_tbl[lnum] == from);
1228 down_read(&ubi->fm_eba_sem);
1229 vol->eba_tbl[lnum] = to;
1230 up_read(&ubi->fm_eba_sem);
1233 mutex_unlock(&ubi->buf_mutex);
1235 leb_write_unlock(ubi, vol_id, lnum);
1240 * print_rsvd_warning - warn about not having enough reserved PEBs.
1241 * @ubi: UBI device description object
1243 * This is a helper function for 'ubi_eba_init()' which is called when UBI
1244 * cannot reserve enough PEBs for bad block handling. This function makes a
1245 * decision whether we have to print a warning or not. The algorithm is as
1247 * o if this is a new UBI image, then just print the warning
1248 * o if this is an UBI image which has already been used for some time, print
1249 * a warning only if we can reserve less than 10% of the expected amount of
1252 * The idea is that when UBI is used, PEBs become bad, and the reserved pool
1253 * of PEBs becomes smaller, which is normal and we do not want to scare users
1254 * with a warning every time they attach the MTD device. This was an issue
1255 * reported by real users.
1257 static void print_rsvd_warning(struct ubi_device *ubi,
1258 struct ubi_attach_info *ai)
1261 * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
1262 * large number to distinguish between newly flashed and used images.
1264 if (ai->max_sqnum > (1 << 18)) {
1265 int min = ubi->beb_rsvd_level / 10;
1269 if (ubi->beb_rsvd_pebs > min)
1273 ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
1274 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1275 if (ubi->corr_peb_count)
1276 ubi_warn(ubi, "%d PEBs are corrupted and not used",
1277 ubi->corr_peb_count);
1281 * self_check_eba - run a self check on the EBA table constructed by fastmap.
1282 * @ubi: UBI device description object
1283 * @ai_fastmap: UBI attach info object created by fastmap
1284 * @ai_scan: UBI attach info object created by scanning
1286 * Returns < 0 in case of an internal error, 0 otherwise.
1287 * If a bad EBA table entry was found it will be printed out and
1288 * ubi_assert() triggers.
1290 int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
1291 struct ubi_attach_info *ai_scan)
1293 int i, j, num_volumes, ret = 0;
1294 int **scan_eba, **fm_eba;
1295 struct ubi_ainf_volume *av;
1296 struct ubi_volume *vol;
1297 struct ubi_ainf_peb *aeb;
1300 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1302 scan_eba = kmalloc(sizeof(*scan_eba) * num_volumes, GFP_KERNEL);
1306 fm_eba = kmalloc(sizeof(*fm_eba) * num_volumes, GFP_KERNEL);
1312 for (i = 0; i < num_volumes; i++) {
1313 vol = ubi->volumes[i];
1317 scan_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**scan_eba),
1324 fm_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**fm_eba),
1331 for (j = 0; j < vol->reserved_pebs; j++)
1332 scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED;
1334 av = ubi_find_av(ai_scan, idx2vol_id(ubi, i));
1338 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
1339 scan_eba[i][aeb->lnum] = aeb->pnum;
1341 av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i));
1345 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
1346 fm_eba[i][aeb->lnum] = aeb->pnum;
1348 for (j = 0; j < vol->reserved_pebs; j++) {
1349 if (scan_eba[i][j] != fm_eba[i][j]) {
1350 if (scan_eba[i][j] == UBI_LEB_UNMAPPED ||
1351 fm_eba[i][j] == UBI_LEB_UNMAPPED)
1354 ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!",
1355 vol->vol_id, i, fm_eba[i][j],
1363 for (i = 0; i < num_volumes; i++) {
1364 if (!ubi->volumes[i])
1377 * ubi_eba_init - initialize the EBA sub-system using attaching information.
1378 * @ubi: UBI device description object
1379 * @ai: attaching information
1381 * This function returns zero in case of success and a negative error code in
1384 int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
1386 int i, j, err, num_volumes;
1387 struct ubi_ainf_volume *av;
1388 struct ubi_volume *vol;
1389 struct ubi_ainf_peb *aeb;
1392 dbg_eba("initialize EBA sub-system");
1394 spin_lock_init(&ubi->ltree_lock);
1395 mutex_init(&ubi->alc_mutex);
1396 ubi->ltree = RB_ROOT;
1398 ubi->global_sqnum = ai->max_sqnum + 1;
1399 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1401 for (i = 0; i < num_volumes; i++) {
1402 vol = ubi->volumes[i];
1408 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1410 if (!vol->eba_tbl) {
1415 for (j = 0; j < vol->reserved_pebs; j++)
1416 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1418 av = ubi_find_av(ai, idx2vol_id(ubi, i));
1422 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
1423 if (aeb->lnum >= vol->reserved_pebs)
1425 * This may happen in case of an unclean reboot
1428 ubi_move_aeb_to_list(av, aeb, &ai->erase);
1430 vol->eba_tbl[aeb->lnum] = aeb->pnum;
1434 if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1435 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
1436 ubi->avail_pebs, EBA_RESERVED_PEBS);
1437 if (ubi->corr_peb_count)
1438 ubi_err(ubi, "%d PEBs are corrupted and not used",
1439 ubi->corr_peb_count);
1443 ubi->avail_pebs -= EBA_RESERVED_PEBS;
1444 ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1446 if (ubi->bad_allowed) {
1447 ubi_calculate_reserved(ubi);
1449 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1450 /* No enough free physical eraseblocks */
1451 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1452 print_rsvd_warning(ubi, ai);
1454 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1456 ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1457 ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
1460 dbg_eba("EBA sub-system is initialized");
1464 for (i = 0; i < num_volumes; i++) {
1465 if (!ubi->volumes[i])
1467 kfree(ubi->volumes[i]->eba_tbl);
1468 ubi->volumes[i]->eba_tbl = NULL;