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.
33 #include <dm/devres.h>
34 #include <linux/slab.h>
35 #include <linux/crc32.h>
36 #include <u-boot/crc.h>
38 #include <ubi_uboot.h>
41 #include <linux/err.h>
44 /* Number of physical eraseblocks reserved for atomic LEB change operation */
45 #define EBA_RESERVED_PEBS 1
48 * next_sqnum - get next sequence number.
49 * @ubi: UBI device description object
51 * This function returns next sequence number to use, which is just the current
52 * global sequence counter value. It also increases the global sequence
55 unsigned long long ubi_next_sqnum(struct ubi_device *ubi)
57 unsigned long long sqnum;
59 spin_lock(&ubi->ltree_lock);
60 sqnum = ubi->global_sqnum++;
61 spin_unlock(&ubi->ltree_lock);
67 * ubi_get_compat - get compatibility flags of a volume.
68 * @ubi: UBI device description object
71 * This function returns compatibility flags for an internal volume. User
72 * volumes have no compatibility flags, so %0 is returned.
74 static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
76 if (vol_id == UBI_LAYOUT_VOLUME_ID)
77 return UBI_LAYOUT_VOLUME_COMPAT;
82 * ltree_lookup - look up the lock tree.
83 * @ubi: UBI device description object
85 * @lnum: logical eraseblock number
87 * This function returns a pointer to the corresponding &struct ubi_ltree_entry
88 * object if the logical eraseblock is locked and %NULL if it is not.
89 * @ubi->ltree_lock has to be locked.
91 static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
96 p = ubi->ltree.rb_node;
98 struct ubi_ltree_entry *le;
100 le = rb_entry(p, struct ubi_ltree_entry, rb);
102 if (vol_id < le->vol_id)
104 else if (vol_id > le->vol_id)
109 else if (lnum > le->lnum)
120 * ltree_add_entry - add new entry to the lock tree.
121 * @ubi: UBI device description object
123 * @lnum: logical eraseblock number
125 * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
126 * lock tree. If such entry is already there, its usage counter is increased.
127 * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
130 static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
131 int vol_id, int lnum)
133 struct ubi_ltree_entry *le, *le1, *le_free;
135 le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
137 return ERR_PTR(-ENOMEM);
140 init_rwsem(&le->mutex);
144 spin_lock(&ubi->ltree_lock);
145 le1 = ltree_lookup(ubi, vol_id, lnum);
149 * This logical eraseblock is already locked. The newly
150 * allocated lock entry is not needed.
155 struct rb_node **p, *parent = NULL;
158 * No lock entry, add the newly allocated one to the
159 * @ubi->ltree RB-tree.
163 p = &ubi->ltree.rb_node;
166 le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
168 if (vol_id < le1->vol_id)
170 else if (vol_id > le1->vol_id)
173 ubi_assert(lnum != le1->lnum);
174 if (lnum < le1->lnum)
181 rb_link_node(&le->rb, parent, p);
182 rb_insert_color(&le->rb, &ubi->ltree);
185 spin_unlock(&ubi->ltree_lock);
192 * leb_read_lock - lock logical eraseblock for reading.
193 * @ubi: UBI device description object
195 * @lnum: logical eraseblock number
197 * This function locks a logical eraseblock for reading. Returns zero in case
198 * of success and a negative error code in case of failure.
200 static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
202 struct ubi_ltree_entry *le;
204 le = ltree_add_entry(ubi, vol_id, lnum);
207 down_read(&le->mutex);
212 * leb_read_unlock - unlock logical eraseblock.
213 * @ubi: UBI device description object
215 * @lnum: logical eraseblock number
217 static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
219 struct ubi_ltree_entry *le;
221 spin_lock(&ubi->ltree_lock);
222 le = ltree_lookup(ubi, vol_id, lnum);
224 ubi_assert(le->users >= 0);
226 if (le->users == 0) {
227 rb_erase(&le->rb, &ubi->ltree);
230 spin_unlock(&ubi->ltree_lock);
234 * leb_write_lock - lock logical eraseblock for writing.
235 * @ubi: UBI device description object
237 * @lnum: logical eraseblock number
239 * This function locks a logical eraseblock for writing. Returns zero in case
240 * of success and a negative error code in case of failure.
242 static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
244 struct ubi_ltree_entry *le;
246 le = ltree_add_entry(ubi, vol_id, lnum);
249 down_write(&le->mutex);
254 * leb_write_lock - lock logical eraseblock for writing.
255 * @ubi: UBI device description object
257 * @lnum: logical eraseblock number
259 * This function locks a logical eraseblock for writing if there is no
260 * contention and does nothing if there is contention. Returns %0 in case of
261 * success, %1 in case of contention, and and a negative error code in case of
264 static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
266 struct ubi_ltree_entry *le;
268 le = ltree_add_entry(ubi, vol_id, lnum);
271 if (down_write_trylock(&le->mutex))
274 /* Contention, cancel */
275 spin_lock(&ubi->ltree_lock);
277 ubi_assert(le->users >= 0);
278 if (le->users == 0) {
279 rb_erase(&le->rb, &ubi->ltree);
282 spin_unlock(&ubi->ltree_lock);
288 * leb_write_unlock - unlock logical eraseblock.
289 * @ubi: UBI device description object
291 * @lnum: logical eraseblock number
293 static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
295 struct ubi_ltree_entry *le;
297 spin_lock(&ubi->ltree_lock);
298 le = ltree_lookup(ubi, vol_id, lnum);
300 ubi_assert(le->users >= 0);
301 up_write(&le->mutex);
302 if (le->users == 0) {
303 rb_erase(&le->rb, &ubi->ltree);
306 spin_unlock(&ubi->ltree_lock);
310 * ubi_eba_unmap_leb - un-map logical eraseblock.
311 * @ubi: UBI device description object
312 * @vol: volume description object
313 * @lnum: logical eraseblock number
315 * This function un-maps logical eraseblock @lnum and schedules corresponding
316 * physical eraseblock for erasure. Returns zero in case of success and a
317 * negative error code in case of failure.
319 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
322 int err, pnum, vol_id = vol->vol_id;
327 err = leb_write_lock(ubi, vol_id, lnum);
331 pnum = vol->eba_tbl[lnum];
333 /* This logical eraseblock is already unmapped */
336 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
338 down_read(&ubi->fm_eba_sem);
339 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
340 up_read(&ubi->fm_eba_sem);
341 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
344 leb_write_unlock(ubi, vol_id, lnum);
349 * ubi_eba_read_leb - read data.
350 * @ubi: UBI device description object
351 * @vol: volume description object
352 * @lnum: logical eraseblock number
353 * @buf: buffer to store the read data
354 * @offset: offset from where to read
355 * @len: how many bytes to read
356 * @check: data CRC check flag
358 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
359 * bytes. The @check flag only makes sense for static volumes and forces
360 * eraseblock data CRC checking.
362 * In case of success this function returns zero. In case of a static volume,
363 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
364 * returned for any volume type if an ECC error was detected by the MTD device
365 * driver. Other negative error cored may be returned in case of other errors.
367 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
368 void *buf, int offset, int len, int check)
370 int err, pnum, scrub = 0, vol_id = vol->vol_id;
371 struct ubi_vid_hdr *vid_hdr;
372 uint32_t uninitialized_var(crc);
374 err = leb_read_lock(ubi, vol_id, lnum);
378 pnum = vol->eba_tbl[lnum];
381 * The logical eraseblock is not mapped, fill the whole buffer
382 * with 0xFF bytes. The exception is static volumes for which
383 * it is an error to read unmapped logical eraseblocks.
385 dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
386 len, offset, vol_id, lnum);
387 leb_read_unlock(ubi, vol_id, lnum);
388 ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
389 memset(buf, 0xFF, len);
393 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
394 len, offset, vol_id, lnum, pnum);
396 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
401 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
407 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
408 if (err && err != UBI_IO_BITFLIPS) {
411 * The header is either absent or corrupted.
412 * The former case means there is a bug -
413 * switch to read-only mode just in case.
414 * The latter case means a real corruption - we
415 * may try to recover data. FIXME: but this is
418 if (err == UBI_IO_BAD_HDR_EBADMSG ||
419 err == UBI_IO_BAD_HDR) {
420 ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d",
429 } else if (err == UBI_IO_BITFLIPS)
432 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
433 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
435 crc = be32_to_cpu(vid_hdr->data_crc);
436 ubi_free_vid_hdr(ubi, vid_hdr);
439 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
441 if (err == UBI_IO_BITFLIPS)
443 else if (mtd_is_eccerr(err)) {
444 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
448 ubi_msg(ubi, "force data checking");
457 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
459 ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x",
467 err = ubi_wl_scrub_peb(ubi, pnum);
469 leb_read_unlock(ubi, vol_id, lnum);
473 ubi_free_vid_hdr(ubi, vid_hdr);
475 leb_read_unlock(ubi, vol_id, lnum);
481 * ubi_eba_read_leb_sg - read data into a scatter gather list.
482 * @ubi: UBI device description object
483 * @vol: volume description object
484 * @lnum: logical eraseblock number
485 * @sgl: UBI scatter gather list to store the read data
486 * @offset: offset from where to read
487 * @len: how many bytes to read
488 * @check: data CRC check flag
490 * This function works exactly like ubi_eba_read_leb(). But instead of
491 * storing the read data into a buffer it writes to an UBI scatter gather
494 int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
495 struct ubi_sgl *sgl, int lnum, int offset, int len,
500 struct scatterlist *sg;
503 ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT);
504 sg = &sgl->sg[sgl->list_pos];
505 if (len < sg->length - sgl->page_pos)
508 to_read = sg->length - sgl->page_pos;
510 ret = ubi_eba_read_leb(ubi, vol, lnum,
511 sg_virt(sg) + sgl->page_pos, offset,
519 sgl->page_pos += to_read;
520 if (sgl->page_pos == sg->length) {
537 * recover_peb - recover from write failure.
538 * @ubi: UBI device description object
539 * @pnum: the physical eraseblock to recover
541 * @lnum: logical eraseblock number
542 * @buf: data which was not written because of the write failure
543 * @offset: offset of the failed write
544 * @len: how many bytes should have been written
546 * This function is called in case of a write failure and moves all good data
547 * from the potentially bad physical eraseblock to a good physical eraseblock.
548 * This function also writes the data which was not written due to the failure.
549 * Returns new physical eraseblock number in case of success, and a negative
550 * error code in case of failure.
552 static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
553 const void *buf, int offset, int len)
555 int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
556 struct ubi_volume *vol = ubi->volumes[idx];
557 struct ubi_vid_hdr *vid_hdr;
559 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
564 new_pnum = ubi_wl_get_peb(ubi);
566 ubi_free_vid_hdr(ubi, vid_hdr);
567 up_read(&ubi->fm_eba_sem);
571 ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
574 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
575 if (err && err != UBI_IO_BITFLIPS) {
578 up_read(&ubi->fm_eba_sem);
582 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
583 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
585 up_read(&ubi->fm_eba_sem);
589 data_size = offset + len;
590 mutex_lock(&ubi->buf_mutex);
591 memset(ubi->peb_buf + offset, 0xFF, len);
593 /* Read everything before the area where the write failure happened */
595 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
596 if (err && err != UBI_IO_BITFLIPS) {
597 up_read(&ubi->fm_eba_sem);
602 memcpy(ubi->peb_buf + offset, buf, len);
604 err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
606 mutex_unlock(&ubi->buf_mutex);
607 up_read(&ubi->fm_eba_sem);
611 mutex_unlock(&ubi->buf_mutex);
612 ubi_free_vid_hdr(ubi, vid_hdr);
614 vol->eba_tbl[lnum] = new_pnum;
615 up_read(&ubi->fm_eba_sem);
616 ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
618 ubi_msg(ubi, "data was successfully recovered");
622 mutex_unlock(&ubi->buf_mutex);
624 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
625 ubi_free_vid_hdr(ubi, vid_hdr);
630 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
633 ubi_warn(ubi, "failed to write to PEB %d", new_pnum);
634 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
635 if (++tries > UBI_IO_RETRIES) {
636 ubi_free_vid_hdr(ubi, vid_hdr);
639 ubi_msg(ubi, "try again");
644 * ubi_eba_write_leb - write data to dynamic volume.
645 * @ubi: UBI device description object
646 * @vol: volume description object
647 * @lnum: logical eraseblock number
648 * @buf: the data to write
649 * @offset: offset within the logical eraseblock where to write
650 * @len: how many bytes to write
652 * This function writes data to logical eraseblock @lnum of a dynamic volume
653 * @vol. Returns zero in case of success and a negative error code in case
654 * of failure. In case of error, it is possible that something was still
655 * written to the flash media, but may be some garbage.
657 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
658 const void *buf, int offset, int len)
660 int err, pnum, tries = 0, vol_id = vol->vol_id;
661 struct ubi_vid_hdr *vid_hdr;
666 err = leb_write_lock(ubi, vol_id, lnum);
670 pnum = vol->eba_tbl[lnum];
672 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
673 len, offset, vol_id, lnum, pnum);
675 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
677 ubi_warn(ubi, "failed to write data to PEB %d", pnum);
678 if (err == -EIO && ubi->bad_allowed)
679 err = recover_peb(ubi, pnum, vol_id, lnum, buf,
684 leb_write_unlock(ubi, vol_id, lnum);
689 * The logical eraseblock is not mapped. We have to get a free physical
690 * eraseblock and write the volume identifier header there first.
692 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
694 leb_write_unlock(ubi, vol_id, lnum);
698 vid_hdr->vol_type = UBI_VID_DYNAMIC;
699 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
700 vid_hdr->vol_id = cpu_to_be32(vol_id);
701 vid_hdr->lnum = cpu_to_be32(lnum);
702 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
703 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
706 pnum = ubi_wl_get_peb(ubi);
708 ubi_free_vid_hdr(ubi, vid_hdr);
709 leb_write_unlock(ubi, vol_id, lnum);
710 up_read(&ubi->fm_eba_sem);
714 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
715 len, offset, vol_id, lnum, pnum);
717 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
719 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
721 up_read(&ubi->fm_eba_sem);
726 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
728 ubi_warn(ubi, "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
729 len, offset, vol_id, lnum, pnum);
730 up_read(&ubi->fm_eba_sem);
735 vol->eba_tbl[lnum] = pnum;
736 up_read(&ubi->fm_eba_sem);
738 leb_write_unlock(ubi, vol_id, lnum);
739 ubi_free_vid_hdr(ubi, vid_hdr);
743 if (err != -EIO || !ubi->bad_allowed) {
745 leb_write_unlock(ubi, vol_id, lnum);
746 ubi_free_vid_hdr(ubi, vid_hdr);
751 * Fortunately, this is the first write operation to this physical
752 * eraseblock, so just put it and request a new one. We assume that if
753 * this physical eraseblock went bad, the erase code will handle that.
755 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
756 if (err || ++tries > UBI_IO_RETRIES) {
758 leb_write_unlock(ubi, vol_id, lnum);
759 ubi_free_vid_hdr(ubi, vid_hdr);
763 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
764 ubi_msg(ubi, "try another PEB");
769 * ubi_eba_write_leb_st - write data to static volume.
770 * @ubi: UBI device description object
771 * @vol: volume description object
772 * @lnum: logical eraseblock number
773 * @buf: data to write
774 * @len: how many bytes to write
775 * @used_ebs: how many logical eraseblocks will this volume contain
777 * This function writes data to logical eraseblock @lnum of static volume
778 * @vol. The @used_ebs argument should contain total number of logical
779 * eraseblock in this static volume.
781 * When writing to the last logical eraseblock, the @len argument doesn't have
782 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
783 * to the real data size, although the @buf buffer has to contain the
784 * alignment. In all other cases, @len has to be aligned.
786 * It is prohibited to write more than once to logical eraseblocks of static
787 * volumes. This function returns zero in case of success and a negative error
788 * code in case of failure.
790 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
791 int lnum, const void *buf, int len, int used_ebs)
793 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
794 struct ubi_vid_hdr *vid_hdr;
800 if (lnum == used_ebs - 1)
801 /* If this is the last LEB @len may be unaligned */
802 len = ALIGN(data_size, ubi->min_io_size);
804 ubi_assert(!(len & (ubi->min_io_size - 1)));
806 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
810 err = leb_write_lock(ubi, vol_id, lnum);
812 ubi_free_vid_hdr(ubi, vid_hdr);
816 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
817 vid_hdr->vol_id = cpu_to_be32(vol_id);
818 vid_hdr->lnum = cpu_to_be32(lnum);
819 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
820 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
822 crc = crc32(UBI_CRC32_INIT, buf, data_size);
823 vid_hdr->vol_type = UBI_VID_STATIC;
824 vid_hdr->data_size = cpu_to_be32(data_size);
825 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
826 vid_hdr->data_crc = cpu_to_be32(crc);
829 pnum = ubi_wl_get_peb(ubi);
831 ubi_free_vid_hdr(ubi, vid_hdr);
832 leb_write_unlock(ubi, vol_id, lnum);
833 up_read(&ubi->fm_eba_sem);
837 dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
838 len, vol_id, lnum, pnum, used_ebs);
840 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
842 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
844 up_read(&ubi->fm_eba_sem);
848 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
850 ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
852 up_read(&ubi->fm_eba_sem);
856 ubi_assert(vol->eba_tbl[lnum] < 0);
857 vol->eba_tbl[lnum] = pnum;
858 up_read(&ubi->fm_eba_sem);
860 leb_write_unlock(ubi, vol_id, lnum);
861 ubi_free_vid_hdr(ubi, vid_hdr);
865 if (err != -EIO || !ubi->bad_allowed) {
867 * This flash device does not admit of bad eraseblocks or
868 * something nasty and unexpected happened. Switch to read-only
872 leb_write_unlock(ubi, vol_id, lnum);
873 ubi_free_vid_hdr(ubi, vid_hdr);
877 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
878 if (err || ++tries > UBI_IO_RETRIES) {
880 leb_write_unlock(ubi, vol_id, lnum);
881 ubi_free_vid_hdr(ubi, vid_hdr);
885 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
886 ubi_msg(ubi, "try another PEB");
891 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
892 * @ubi: UBI device description object
893 * @vol: volume description object
894 * @lnum: logical eraseblock number
895 * @buf: data to write
896 * @len: how many bytes to write
898 * This function changes the contents of a logical eraseblock atomically. @buf
899 * has to contain new logical eraseblock data, and @len - the length of the
900 * data, which has to be aligned. This function guarantees that in case of an
901 * unclean reboot the old contents is preserved. Returns zero in case of
902 * success and a negative error code in case of failure.
904 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
905 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
907 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
908 int lnum, const void *buf, int len)
910 int err, pnum, old_pnum, tries = 0, vol_id = vol->vol_id;
911 struct ubi_vid_hdr *vid_hdr;
919 * Special case when data length is zero. In this case the LEB
920 * has to be unmapped and mapped somewhere else.
922 err = ubi_eba_unmap_leb(ubi, vol, lnum);
925 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
928 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
932 mutex_lock(&ubi->alc_mutex);
933 err = leb_write_lock(ubi, vol_id, lnum);
937 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
938 vid_hdr->vol_id = cpu_to_be32(vol_id);
939 vid_hdr->lnum = cpu_to_be32(lnum);
940 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
941 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
943 crc = crc32(UBI_CRC32_INIT, buf, len);
944 vid_hdr->vol_type = UBI_VID_DYNAMIC;
945 vid_hdr->data_size = cpu_to_be32(len);
946 vid_hdr->copy_flag = 1;
947 vid_hdr->data_crc = cpu_to_be32(crc);
950 pnum = ubi_wl_get_peb(ubi);
953 up_read(&ubi->fm_eba_sem);
957 dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
958 vol_id, lnum, vol->eba_tbl[lnum], pnum);
960 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
962 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
964 up_read(&ubi->fm_eba_sem);
968 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
970 ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
972 up_read(&ubi->fm_eba_sem);
976 old_pnum = vol->eba_tbl[lnum];
977 vol->eba_tbl[lnum] = pnum;
978 up_read(&ubi->fm_eba_sem);
981 err = ubi_wl_put_peb(ubi, vol_id, lnum, old_pnum, 0);
987 leb_write_unlock(ubi, vol_id, lnum);
989 mutex_unlock(&ubi->alc_mutex);
990 ubi_free_vid_hdr(ubi, vid_hdr);
994 if (err != -EIO || !ubi->bad_allowed) {
996 * This flash device does not admit of bad eraseblocks or
997 * something nasty and unexpected happened. Switch to read-only
1001 goto out_leb_unlock;
1004 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
1005 if (err || ++tries > UBI_IO_RETRIES) {
1007 goto out_leb_unlock;
1010 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1011 ubi_msg(ubi, "try another PEB");
1016 * is_error_sane - check whether a read error is sane.
1017 * @err: code of the error happened during reading
1019 * This is a helper function for 'ubi_eba_copy_leb()' which is called when we
1020 * cannot read data from the target PEB (an error @err happened). If the error
1021 * code is sane, then we treat this error as non-fatal. Otherwise the error is
1022 * fatal and UBI will be switched to R/O mode later.
1024 * The idea is that we try not to switch to R/O mode if the read error is
1025 * something which suggests there was a real read problem. E.g., %-EIO. Or a
1026 * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O
1027 * mode, simply because we do not know what happened at the MTD level, and we
1028 * cannot handle this. E.g., the underlying driver may have become crazy, and
1029 * it is safer to switch to R/O mode to preserve the data.
1031 * And bear in mind, this is about reading from the target PEB, i.e. the PEB
1032 * which we have just written.
1034 static int is_error_sane(int err)
1036 if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
1037 err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
1043 * ubi_eba_copy_leb - copy logical eraseblock.
1044 * @ubi: UBI device description object
1045 * @from: physical eraseblock number from where to copy
1046 * @to: physical eraseblock number where to copy
1047 * @vid_hdr: VID header of the @from physical eraseblock
1049 * This function copies logical eraseblock from physical eraseblock @from to
1050 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
1051 * function. Returns:
1052 * o %0 in case of success;
1053 * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
1054 * o a negative error code in case of failure.
1056 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1057 struct ubi_vid_hdr *vid_hdr)
1059 int err, vol_id, lnum, data_size, aldata_size, idx;
1060 struct ubi_volume *vol;
1063 vol_id = be32_to_cpu(vid_hdr->vol_id);
1064 lnum = be32_to_cpu(vid_hdr->lnum);
1066 dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
1068 if (vid_hdr->vol_type == UBI_VID_STATIC) {
1069 data_size = be32_to_cpu(vid_hdr->data_size);
1070 aldata_size = ALIGN(data_size, ubi->min_io_size);
1072 data_size = aldata_size =
1073 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
1075 idx = vol_id2idx(ubi, vol_id);
1076 spin_lock(&ubi->volumes_lock);
1078 * Note, we may race with volume deletion, which means that the volume
1079 * this logical eraseblock belongs to might be being deleted. Since the
1080 * volume deletion un-maps all the volume's logical eraseblocks, it will
1081 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
1083 vol = ubi->volumes[idx];
1084 spin_unlock(&ubi->volumes_lock);
1086 /* No need to do further work, cancel */
1087 dbg_wl("volume %d is being removed, cancel", vol_id);
1088 return MOVE_CANCEL_RACE;
1092 * We do not want anybody to write to this logical eraseblock while we
1093 * are moving it, so lock it.
1095 * Note, we are using non-waiting locking here, because we cannot sleep
1096 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
1097 * unmapping the LEB which is mapped to the PEB we are going to move
1098 * (@from). This task locks the LEB and goes sleep in the
1099 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
1100 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
1101 * LEB is already locked, we just do not move it and return
1102 * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because
1103 * we do not know the reasons of the contention - it may be just a
1104 * normal I/O on this LEB, so we want to re-try.
1106 err = leb_write_trylock(ubi, vol_id, lnum);
1108 dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
1113 * The LEB might have been put meanwhile, and the task which put it is
1114 * probably waiting on @ubi->move_mutex. No need to continue the work,
1117 if (vol->eba_tbl[lnum] != from) {
1118 dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
1119 vol_id, lnum, from, vol->eba_tbl[lnum]);
1120 err = MOVE_CANCEL_RACE;
1121 goto out_unlock_leb;
1125 * OK, now the LEB is locked and we can safely start moving it. Since
1126 * this function utilizes the @ubi->peb_buf buffer which is shared
1127 * with some other functions - we lock the buffer by taking the
1130 mutex_lock(&ubi->buf_mutex);
1131 dbg_wl("read %d bytes of data", aldata_size);
1132 err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
1133 if (err && err != UBI_IO_BITFLIPS) {
1134 ubi_warn(ubi, "error %d while reading data from PEB %d",
1136 err = MOVE_SOURCE_RD_ERR;
1137 goto out_unlock_buf;
1141 * Now we have got to calculate how much data we have to copy. In
1142 * case of a static volume it is fairly easy - the VID header contains
1143 * the data size. In case of a dynamic volume it is more difficult - we
1144 * have to read the contents, cut 0xFF bytes from the end and copy only
1145 * the first part. We must do this to avoid writing 0xFF bytes as it
1146 * may have some side-effects. And not only this. It is important not
1147 * to include those 0xFFs to CRC because later the they may be filled
1150 if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1151 aldata_size = data_size =
1152 ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
1155 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
1159 * It may turn out to be that the whole @from physical eraseblock
1160 * contains only 0xFF bytes. Then we have to only write the VID header
1161 * and do not write any data. This also means we should not set
1162 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1164 if (data_size > 0) {
1165 vid_hdr->copy_flag = 1;
1166 vid_hdr->data_size = cpu_to_be32(data_size);
1167 vid_hdr->data_crc = cpu_to_be32(crc);
1169 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1171 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1174 err = MOVE_TARGET_WR_ERR;
1175 goto out_unlock_buf;
1180 /* Read the VID header back and check if it was written correctly */
1181 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1183 if (err != UBI_IO_BITFLIPS) {
1184 ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
1186 if (is_error_sane(err))
1187 err = MOVE_TARGET_RD_ERR;
1189 err = MOVE_TARGET_BITFLIPS;
1190 goto out_unlock_buf;
1193 if (data_size > 0) {
1194 err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1197 err = MOVE_TARGET_WR_ERR;
1198 goto out_unlock_buf;
1204 * We've written the data and are going to read it back to make
1205 * sure it was written correctly.
1207 memset(ubi->peb_buf, 0xFF, aldata_size);
1208 err = ubi_io_read_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1210 if (err != UBI_IO_BITFLIPS) {
1211 ubi_warn(ubi, "error %d while reading data back from PEB %d",
1213 if (is_error_sane(err))
1214 err = MOVE_TARGET_RD_ERR;
1216 err = MOVE_TARGET_BITFLIPS;
1217 goto out_unlock_buf;
1222 if (crc != crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size)) {
1223 ubi_warn(ubi, "read data back from PEB %d and it is different",
1226 goto out_unlock_buf;
1230 ubi_assert(vol->eba_tbl[lnum] == from);
1231 down_read(&ubi->fm_eba_sem);
1232 vol->eba_tbl[lnum] = to;
1233 up_read(&ubi->fm_eba_sem);
1236 mutex_unlock(&ubi->buf_mutex);
1238 leb_write_unlock(ubi, vol_id, lnum);
1243 * print_rsvd_warning - warn about not having enough reserved PEBs.
1244 * @ubi: UBI device description object
1246 * This is a helper function for 'ubi_eba_init()' which is called when UBI
1247 * cannot reserve enough PEBs for bad block handling. This function makes a
1248 * decision whether we have to print a warning or not. The algorithm is as
1250 * o if this is a new UBI image, then just print the warning
1251 * o if this is an UBI image which has already been used for some time, print
1252 * a warning only if we can reserve less than 10% of the expected amount of
1255 * The idea is that when UBI is used, PEBs become bad, and the reserved pool
1256 * of PEBs becomes smaller, which is normal and we do not want to scare users
1257 * with a warning every time they attach the MTD device. This was an issue
1258 * reported by real users.
1260 static void print_rsvd_warning(struct ubi_device *ubi,
1261 struct ubi_attach_info *ai)
1264 * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
1265 * large number to distinguish between newly flashed and used images.
1267 if (ai->max_sqnum > (1 << 18)) {
1268 int min = ubi->beb_rsvd_level / 10;
1272 if (ubi->beb_rsvd_pebs > min)
1276 ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
1277 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1278 if (ubi->corr_peb_count)
1279 ubi_warn(ubi, "%d PEBs are corrupted and not used",
1280 ubi->corr_peb_count);
1284 * self_check_eba - run a self check on the EBA table constructed by fastmap.
1285 * @ubi: UBI device description object
1286 * @ai_fastmap: UBI attach info object created by fastmap
1287 * @ai_scan: UBI attach info object created by scanning
1289 * Returns < 0 in case of an internal error, 0 otherwise.
1290 * If a bad EBA table entry was found it will be printed out and
1291 * ubi_assert() triggers.
1293 int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
1294 struct ubi_attach_info *ai_scan)
1296 int i, j, num_volumes, ret = 0;
1297 int **scan_eba, **fm_eba;
1298 struct ubi_ainf_volume *av;
1299 struct ubi_volume *vol;
1300 struct ubi_ainf_peb *aeb;
1303 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1305 scan_eba = kmalloc(sizeof(*scan_eba) * num_volumes, GFP_KERNEL);
1309 fm_eba = kmalloc(sizeof(*fm_eba) * num_volumes, GFP_KERNEL);
1315 for (i = 0; i < num_volumes; i++) {
1316 vol = ubi->volumes[i];
1320 scan_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**scan_eba),
1327 fm_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**fm_eba),
1334 for (j = 0; j < vol->reserved_pebs; j++)
1335 scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED;
1337 av = ubi_find_av(ai_scan, idx2vol_id(ubi, i));
1341 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
1342 scan_eba[i][aeb->lnum] = aeb->pnum;
1344 av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i));
1348 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
1349 fm_eba[i][aeb->lnum] = aeb->pnum;
1351 for (j = 0; j < vol->reserved_pebs; j++) {
1352 if (scan_eba[i][j] != fm_eba[i][j]) {
1353 if (scan_eba[i][j] == UBI_LEB_UNMAPPED ||
1354 fm_eba[i][j] == UBI_LEB_UNMAPPED)
1357 ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!",
1358 vol->vol_id, i, fm_eba[i][j],
1366 for (i = 0; i < num_volumes; i++) {
1367 if (!ubi->volumes[i])
1380 * ubi_eba_init - initialize the EBA sub-system using attaching information.
1381 * @ubi: UBI device description object
1382 * @ai: attaching information
1384 * This function returns zero in case of success and a negative error code in
1387 int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
1389 int i, j, err, num_volumes;
1390 struct ubi_ainf_volume *av;
1391 struct ubi_volume *vol;
1392 struct ubi_ainf_peb *aeb;
1395 dbg_eba("initialize EBA sub-system");
1397 spin_lock_init(&ubi->ltree_lock);
1398 mutex_init(&ubi->alc_mutex);
1399 ubi->ltree = RB_ROOT;
1401 ubi->global_sqnum = ai->max_sqnum + 1;
1402 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1404 for (i = 0; i < num_volumes; i++) {
1405 vol = ubi->volumes[i];
1411 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1413 if (!vol->eba_tbl) {
1418 for (j = 0; j < vol->reserved_pebs; j++)
1419 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1421 av = ubi_find_av(ai, idx2vol_id(ubi, i));
1425 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
1426 if (aeb->lnum >= vol->reserved_pebs)
1428 * This may happen in case of an unclean reboot
1431 ubi_move_aeb_to_list(av, aeb, &ai->erase);
1433 vol->eba_tbl[aeb->lnum] = aeb->pnum;
1437 if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1438 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
1439 ubi->avail_pebs, EBA_RESERVED_PEBS);
1440 if (ubi->corr_peb_count)
1441 ubi_err(ubi, "%d PEBs are corrupted and not used",
1442 ubi->corr_peb_count);
1446 ubi->avail_pebs -= EBA_RESERVED_PEBS;
1447 ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1449 if (ubi->bad_allowed) {
1450 ubi_calculate_reserved(ubi);
1452 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1453 /* No enough free physical eraseblocks */
1454 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1455 print_rsvd_warning(ubi, ai);
1457 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1459 ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1460 ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
1463 dbg_eba("EBA sub-system is initialized");
1467 for (i = 0; i < num_volumes; i++) {
1468 if (!ubi->volumes[i])
1470 kfree(ubi->volumes[i]->eba_tbl);
1471 ubi->volumes[i]->eba_tbl = NULL;