2 * Copyright (c) International Business Machines Corp., 2006
4 * SPDX-License-Identifier: GPL-2.0+
6 * Author: Artem Bityutskiy (Битюцкий Артём)
10 * The UBI Eraseblock Association (EBA) unit.
12 * This unit is responsible for I/O to/from logical eraseblock.
14 * Although in this implementation the EBA table is fully kept and managed in
15 * RAM, which assumes poor scalability, it might be (partially) maintained on
16 * flash in future implementations.
18 * The EBA unit implements per-logical eraseblock locking. Before accessing a
19 * logical eraseblock it is locked for reading or writing. The per-logical
20 * eraseblock locking is implemented by means of the lock tree. The lock tree
21 * is an RB-tree which refers all the currently locked logical eraseblocks. The
22 * lock tree elements are &struct ubi_ltree_entry objects. They are indexed by
23 * (@vol_id, @lnum) pairs.
25 * EBA also maintains the global sequence counter which is incremented each
26 * time a logical eraseblock is mapped to a physical eraseblock and it is
27 * stored in the volume identifier header. This means that each VID header has
28 * a unique sequence number. The sequence number is only increased an we assume
29 * 64 bits is enough to never overflow.
33 #include <linux/slab.h>
34 #include <linux/crc32.h>
35 #include <linux/err.h>
38 #include <ubi_uboot.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 static unsigned long long 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);
191 * leb_read_lock - lock logical eraseblock for reading.
192 * @ubi: UBI device description object
194 * @lnum: logical eraseblock number
196 * This function locks a logical eraseblock for reading. Returns zero in case
197 * of success and a negative error code in case of failure.
199 static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
201 struct ubi_ltree_entry *le;
203 le = ltree_add_entry(ubi, vol_id, lnum);
206 down_read(&le->mutex);
211 * leb_read_unlock - unlock logical eraseblock.
212 * @ubi: UBI device description object
214 * @lnum: logical eraseblock number
216 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);
225 if (le->users == 0) {
226 rb_erase(&le->rb, &ubi->ltree);
229 spin_unlock(&ubi->ltree_lock);
237 * leb_write_lock - lock logical eraseblock for writing.
238 * @ubi: UBI device description object
240 * @lnum: logical eraseblock number
242 * This function locks a logical eraseblock for writing. Returns zero in case
243 * of success and a negative error code in case of failure.
245 static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
247 struct ubi_ltree_entry *le;
249 le = ltree_add_entry(ubi, vol_id, lnum);
252 down_write(&le->mutex);
257 * leb_write_lock - lock logical eraseblock for writing.
258 * @ubi: UBI device description object
260 * @lnum: logical eraseblock number
262 * This function locks a logical eraseblock for writing if there is no
263 * contention and does nothing if there is contention. Returns %0 in case of
264 * success, %1 in case of contention, and and a negative error code in case of
267 static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
270 struct ubi_ltree_entry *le;
272 le = ltree_add_entry(ubi, vol_id, lnum);
275 if (down_write_trylock(&le->mutex))
278 /* Contention, cancel */
279 spin_lock(&ubi->ltree_lock);
281 ubi_assert(le->users >= 0);
282 if (le->users == 0) {
283 rb_erase(&le->rb, &ubi->ltree);
287 spin_unlock(&ubi->ltree_lock);
295 * leb_write_unlock - unlock logical eraseblock.
296 * @ubi: UBI device description object
298 * @lnum: logical eraseblock number
300 static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
303 struct ubi_ltree_entry *le;
305 spin_lock(&ubi->ltree_lock);
306 le = ltree_lookup(ubi, vol_id, lnum);
308 ubi_assert(le->users >= 0);
309 if (le->users == 0) {
310 rb_erase(&le->rb, &ubi->ltree);
314 spin_unlock(&ubi->ltree_lock);
316 up_write(&le->mutex);
322 * ubi_eba_unmap_leb - un-map logical eraseblock.
323 * @ubi: UBI device description object
324 * @vol: volume description object
325 * @lnum: logical eraseblock number
327 * This function un-maps logical eraseblock @lnum and schedules corresponding
328 * physical eraseblock for erasure. Returns zero in case of success and a
329 * negative error code in case of failure.
331 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
334 int err, pnum, vol_id = vol->vol_id;
339 err = leb_write_lock(ubi, vol_id, lnum);
343 pnum = vol->eba_tbl[lnum];
345 /* This logical eraseblock is already unmapped */
348 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
350 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
351 err = ubi_wl_put_peb(ubi, pnum, 0);
354 leb_write_unlock(ubi, vol_id, lnum);
359 * ubi_eba_read_leb - read data.
360 * @ubi: UBI device description object
361 * @vol: volume description object
362 * @lnum: logical eraseblock number
363 * @buf: buffer to store the read data
364 * @offset: offset from where to read
365 * @len: how many bytes to read
366 * @check: data CRC check flag
368 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
369 * bytes. The @check flag only makes sense for static volumes and forces
370 * eraseblock data CRC checking.
372 * In case of success this function returns zero. In case of a static volume,
373 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
374 * returned for any volume type if an ECC error was detected by the MTD device
375 * driver. Other negative error cored may be returned in case of other errors.
377 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
378 void *buf, int offset, int len, int check)
380 int err, pnum, scrub = 0, vol_id = vol->vol_id;
381 struct ubi_vid_hdr *vid_hdr;
382 uint32_t uninitialized_var(crc);
384 err = leb_read_lock(ubi, vol_id, lnum);
388 pnum = vol->eba_tbl[lnum];
391 * The logical eraseblock is not mapped, fill the whole buffer
392 * with 0xFF bytes. The exception is static volumes for which
393 * it is an error to read unmapped logical eraseblocks.
395 dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
396 len, offset, vol_id, lnum);
397 leb_read_unlock(ubi, vol_id, lnum);
398 ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
399 memset(buf, 0xFF, len);
403 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
404 len, offset, vol_id, lnum, pnum);
406 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
411 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
417 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
418 if (err && err != UBI_IO_BITFLIPS) {
421 * The header is either absent or corrupted.
422 * The former case means there is a bug -
423 * switch to read-only mode just in case.
424 * The latter case means a real corruption - we
425 * may try to recover data. FIXME: but this is
428 if (err == UBI_IO_BAD_VID_HDR) {
429 ubi_warn("bad VID header at PEB %d, LEB"
430 "%d:%d", pnum, vol_id, lnum);
436 } else if (err == UBI_IO_BITFLIPS)
439 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
440 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
442 crc = be32_to_cpu(vid_hdr->data_crc);
443 ubi_free_vid_hdr(ubi, vid_hdr);
446 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
448 if (err == UBI_IO_BITFLIPS) {
451 } else if (mtd_is_eccerr(err)) {
452 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
456 ubi_msg("force data checking");
465 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
467 ubi_warn("CRC error: calculated %#08x, must be %#08x",
475 err = ubi_wl_scrub_peb(ubi, pnum);
477 leb_read_unlock(ubi, vol_id, lnum);
481 ubi_free_vid_hdr(ubi, vid_hdr);
483 leb_read_unlock(ubi, vol_id, lnum);
488 * recover_peb - recover from write failure.
489 * @ubi: UBI device description object
490 * @pnum: the physical eraseblock to recover
492 * @lnum: logical eraseblock number
493 * @buf: data which was not written because of the write failure
494 * @offset: offset of the failed write
495 * @len: how many bytes should have been written
497 * This function is called in case of a write failure and moves all good data
498 * from the potentially bad physical eraseblock to a good physical eraseblock.
499 * This function also writes the data which was not written due to the failure.
500 * Returns new physical eraseblock number in case of success, and a negative
501 * error code in case of failure.
503 static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
504 const void *buf, int offset, int len)
506 int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
507 struct ubi_volume *vol = ubi->volumes[idx];
508 struct ubi_vid_hdr *vid_hdr;
510 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
515 mutex_lock(&ubi->buf_mutex);
518 new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
520 mutex_unlock(&ubi->buf_mutex);
521 ubi_free_vid_hdr(ubi, vid_hdr);
525 ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum);
527 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
528 if (err && err != UBI_IO_BITFLIPS) {
534 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
535 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
539 data_size = offset + len;
540 memset(ubi->peb_buf1 + offset, 0xFF, len);
542 /* Read everything before the area where the write failure happened */
544 err = ubi_io_read_data(ubi, ubi->peb_buf1, pnum, 0, offset);
545 if (err && err != UBI_IO_BITFLIPS)
549 memcpy(ubi->peb_buf1 + offset, buf, len);
551 err = ubi_io_write_data(ubi, ubi->peb_buf1, new_pnum, 0, data_size);
555 mutex_unlock(&ubi->buf_mutex);
556 ubi_free_vid_hdr(ubi, vid_hdr);
558 vol->eba_tbl[lnum] = new_pnum;
559 ubi_wl_put_peb(ubi, pnum, 1);
561 ubi_msg("data was successfully recovered");
565 mutex_unlock(&ubi->buf_mutex);
566 ubi_wl_put_peb(ubi, new_pnum, 1);
567 ubi_free_vid_hdr(ubi, vid_hdr);
572 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
575 ubi_warn("failed to write to PEB %d", new_pnum);
576 ubi_wl_put_peb(ubi, new_pnum, 1);
577 if (++tries > UBI_IO_RETRIES) {
578 mutex_unlock(&ubi->buf_mutex);
579 ubi_free_vid_hdr(ubi, vid_hdr);
582 ubi_msg("try again");
587 * ubi_eba_write_leb - write data to dynamic volume.
588 * @ubi: UBI device description object
589 * @vol: volume description object
590 * @lnum: logical eraseblock number
591 * @buf: the data to write
592 * @offset: offset within the logical eraseblock where to write
593 * @len: how many bytes to write
596 * This function writes data to logical eraseblock @lnum of a dynamic volume
597 * @vol. Returns zero in case of success and a negative error code in case
598 * of failure. In case of error, it is possible that something was still
599 * written to the flash media, but may be some garbage.
601 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
602 const void *buf, int offset, int len, int dtype)
604 int err, pnum, tries = 0, vol_id = vol->vol_id;
605 struct ubi_vid_hdr *vid_hdr;
610 err = leb_write_lock(ubi, vol_id, lnum);
614 pnum = vol->eba_tbl[lnum];
616 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
617 len, offset, vol_id, lnum, pnum);
619 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
621 ubi_warn("failed to write data to PEB %d", pnum);
622 if (err == -EIO && ubi->bad_allowed)
623 err = recover_peb(ubi, pnum, vol_id, lnum, buf,
628 leb_write_unlock(ubi, vol_id, lnum);
633 * The logical eraseblock is not mapped. We have to get a free physical
634 * eraseblock and write the volume identifier header there first.
636 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
638 leb_write_unlock(ubi, vol_id, lnum);
642 vid_hdr->vol_type = UBI_VID_DYNAMIC;
643 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
644 vid_hdr->vol_id = cpu_to_be32(vol_id);
645 vid_hdr->lnum = cpu_to_be32(lnum);
646 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
647 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
650 pnum = ubi_wl_get_peb(ubi, dtype);
652 ubi_free_vid_hdr(ubi, vid_hdr);
653 leb_write_unlock(ubi, vol_id, lnum);
657 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
658 len, offset, vol_id, lnum, pnum);
660 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
662 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
668 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
670 ubi_warn("failed to write %d bytes at offset %d of "
671 "LEB %d:%d, PEB %d", len, offset, vol_id,
677 vol->eba_tbl[lnum] = pnum;
679 leb_write_unlock(ubi, vol_id, lnum);
680 ubi_free_vid_hdr(ubi, vid_hdr);
684 if (err != -EIO || !ubi->bad_allowed) {
686 leb_write_unlock(ubi, vol_id, lnum);
687 ubi_free_vid_hdr(ubi, vid_hdr);
692 * Fortunately, this is the first write operation to this physical
693 * eraseblock, so just put it and request a new one. We assume that if
694 * this physical eraseblock went bad, the erase code will handle that.
696 err = ubi_wl_put_peb(ubi, pnum, 1);
697 if (err || ++tries > UBI_IO_RETRIES) {
699 leb_write_unlock(ubi, vol_id, lnum);
700 ubi_free_vid_hdr(ubi, vid_hdr);
704 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
705 ubi_msg("try another PEB");
710 * ubi_eba_write_leb_st - write data to static volume.
711 * @ubi: UBI device description object
712 * @vol: volume description object
713 * @lnum: logical eraseblock number
714 * @buf: data to write
715 * @len: how many bytes to write
717 * @used_ebs: how many logical eraseblocks will this volume contain
719 * This function writes data to logical eraseblock @lnum of static volume
720 * @vol. The @used_ebs argument should contain total number of logical
721 * eraseblock in this static volume.
723 * When writing to the last logical eraseblock, the @len argument doesn't have
724 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
725 * to the real data size, although the @buf buffer has to contain the
726 * alignment. In all other cases, @len has to be aligned.
728 * It is prohibited to write more then once to logical eraseblocks of static
729 * volumes. This function returns zero in case of success and a negative error
730 * code in case of failure.
732 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
733 int lnum, const void *buf, int len, int dtype,
736 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
737 struct ubi_vid_hdr *vid_hdr;
743 if (lnum == used_ebs - 1)
744 /* If this is the last LEB @len may be unaligned */
745 len = ALIGN(data_size, ubi->min_io_size);
747 ubi_assert(!(len & (ubi->min_io_size - 1)));
749 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
753 err = leb_write_lock(ubi, vol_id, lnum);
755 ubi_free_vid_hdr(ubi, vid_hdr);
759 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
760 vid_hdr->vol_id = cpu_to_be32(vol_id);
761 vid_hdr->lnum = cpu_to_be32(lnum);
762 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
763 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
765 crc = crc32(UBI_CRC32_INIT, buf, data_size);
766 vid_hdr->vol_type = UBI_VID_STATIC;
767 vid_hdr->data_size = cpu_to_be32(data_size);
768 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
769 vid_hdr->data_crc = cpu_to_be32(crc);
772 pnum = ubi_wl_get_peb(ubi, dtype);
774 ubi_free_vid_hdr(ubi, vid_hdr);
775 leb_write_unlock(ubi, vol_id, lnum);
779 dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
780 len, vol_id, lnum, pnum, used_ebs);
782 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
784 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
789 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
791 ubi_warn("failed to write %d bytes of data to PEB %d",
796 ubi_assert(vol->eba_tbl[lnum] < 0);
797 vol->eba_tbl[lnum] = pnum;
799 leb_write_unlock(ubi, vol_id, lnum);
800 ubi_free_vid_hdr(ubi, vid_hdr);
804 if (err != -EIO || !ubi->bad_allowed) {
806 * This flash device does not admit of bad eraseblocks or
807 * something nasty and unexpected happened. Switch to read-only
811 leb_write_unlock(ubi, vol_id, lnum);
812 ubi_free_vid_hdr(ubi, vid_hdr);
816 err = ubi_wl_put_peb(ubi, pnum, 1);
817 if (err || ++tries > UBI_IO_RETRIES) {
819 leb_write_unlock(ubi, vol_id, lnum);
820 ubi_free_vid_hdr(ubi, vid_hdr);
824 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
825 ubi_msg("try another PEB");
830 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
831 * @ubi: UBI device description object
832 * @vol: volume description object
833 * @lnum: logical eraseblock number
834 * @buf: data to write
835 * @len: how many bytes to write
838 * This function changes the contents of a logical eraseblock atomically. @buf
839 * has to contain new logical eraseblock data, and @len - the length of the
840 * data, which has to be aligned. This function guarantees that in case of an
841 * unclean reboot the old contents is preserved. Returns zero in case of
842 * success and a negative error code in case of failure.
844 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
845 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
847 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
848 int lnum, const void *buf, int len, int dtype)
850 int err, pnum, tries = 0, vol_id = vol->vol_id;
851 struct ubi_vid_hdr *vid_hdr;
859 * Special case when data length is zero. In this case the LEB
860 * has to be unmapped and mapped somewhere else.
862 err = ubi_eba_unmap_leb(ubi, vol, lnum);
865 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
868 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
872 mutex_lock(&ubi->alc_mutex);
873 err = leb_write_lock(ubi, vol_id, lnum);
877 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
878 vid_hdr->vol_id = cpu_to_be32(vol_id);
879 vid_hdr->lnum = cpu_to_be32(lnum);
880 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
881 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
883 crc = crc32(UBI_CRC32_INIT, buf, len);
884 vid_hdr->vol_type = UBI_VID_DYNAMIC;
885 vid_hdr->data_size = cpu_to_be32(len);
886 vid_hdr->copy_flag = 1;
887 vid_hdr->data_crc = cpu_to_be32(crc);
890 pnum = ubi_wl_get_peb(ubi, dtype);
896 dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
897 vol_id, lnum, vol->eba_tbl[lnum], pnum);
899 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
901 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
906 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
908 ubi_warn("failed to write %d bytes of data to PEB %d",
913 if (vol->eba_tbl[lnum] >= 0) {
914 err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1);
919 vol->eba_tbl[lnum] = pnum;
922 leb_write_unlock(ubi, vol_id, lnum);
924 mutex_unlock(&ubi->alc_mutex);
925 ubi_free_vid_hdr(ubi, vid_hdr);
929 if (err != -EIO || !ubi->bad_allowed) {
931 * This flash device does not admit of bad eraseblocks or
932 * something nasty and unexpected happened. Switch to read-only
939 err = ubi_wl_put_peb(ubi, pnum, 1);
940 if (err || ++tries > UBI_IO_RETRIES) {
945 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
946 ubi_msg("try another PEB");
951 * ubi_eba_copy_leb - copy logical eraseblock.
952 * @ubi: UBI device description object
953 * @from: physical eraseblock number from where to copy
954 * @to: physical eraseblock number where to copy
955 * @vid_hdr: VID header of the @from physical eraseblock
957 * This function copies logical eraseblock from physical eraseblock @from to
958 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
960 * o %0 in case of success;
961 * o %1 if the operation was canceled and should be tried later (e.g.,
962 * because a bit-flip was detected at the target PEB);
963 * o %2 if the volume is being deleted and this LEB should not be moved.
965 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
966 struct ubi_vid_hdr *vid_hdr)
968 int err, vol_id, lnum, data_size, aldata_size, idx;
969 struct ubi_volume *vol;
972 vol_id = be32_to_cpu(vid_hdr->vol_id);
973 lnum = be32_to_cpu(vid_hdr->lnum);
975 dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
977 if (vid_hdr->vol_type == UBI_VID_STATIC) {
978 data_size = be32_to_cpu(vid_hdr->data_size);
979 aldata_size = ALIGN(data_size, ubi->min_io_size);
981 data_size = aldata_size =
982 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
984 idx = vol_id2idx(ubi, vol_id);
985 spin_lock(&ubi->volumes_lock);
987 * Note, we may race with volume deletion, which means that the volume
988 * this logical eraseblock belongs to might be being deleted. Since the
989 * volume deletion unmaps all the volume's logical eraseblocks, it will
990 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
992 vol = ubi->volumes[idx];
994 /* No need to do further work, cancel */
995 dbg_eba("volume %d is being removed, cancel", vol_id);
996 spin_unlock(&ubi->volumes_lock);
999 spin_unlock(&ubi->volumes_lock);
1002 * We do not want anybody to write to this logical eraseblock while we
1003 * are moving it, so lock it.
1005 * Note, we are using non-waiting locking here, because we cannot sleep
1006 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
1007 * unmapping the LEB which is mapped to the PEB we are going to move
1008 * (@from). This task locks the LEB and goes sleep in the
1009 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
1010 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
1011 * LEB is already locked, we just do not move it and return %1.
1013 err = leb_write_trylock(ubi, vol_id, lnum);
1015 dbg_eba("contention on LEB %d:%d, cancel", vol_id, lnum);
1020 * The LEB might have been put meanwhile, and the task which put it is
1021 * probably waiting on @ubi->move_mutex. No need to continue the work,
1024 if (vol->eba_tbl[lnum] != from) {
1025 dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
1026 "PEB %d, cancel", vol_id, lnum, from,
1027 vol->eba_tbl[lnum]);
1029 goto out_unlock_leb;
1033 * OK, now the LEB is locked and we can safely start moving iy. Since
1034 * this function utilizes thie @ubi->peb1_buf buffer which is shared
1035 * with some other functions, so lock the buffer by taking the
1038 mutex_lock(&ubi->buf_mutex);
1039 dbg_eba("read %d bytes of data", aldata_size);
1040 err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
1041 if (err && err != UBI_IO_BITFLIPS) {
1042 ubi_warn("error %d while reading data from PEB %d",
1044 goto out_unlock_buf;
1048 * Now we have got to calculate how much data we have to to copy. In
1049 * case of a static volume it is fairly easy - the VID header contains
1050 * the data size. In case of a dynamic volume it is more difficult - we
1051 * have to read the contents, cut 0xFF bytes from the end and copy only
1052 * the first part. We must do this to avoid writing 0xFF bytes as it
1053 * may have some side-effects. And not only this. It is important not
1054 * to include those 0xFFs to CRC because later the they may be filled
1057 if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1058 aldata_size = data_size =
1059 ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
1062 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
1066 * It may turn out to me that the whole @from physical eraseblock
1067 * contains only 0xFF bytes. Then we have to only write the VID header
1068 * and do not write any data. This also means we should not set
1069 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1071 if (data_size > 0) {
1072 vid_hdr->copy_flag = 1;
1073 vid_hdr->data_size = cpu_to_be32(data_size);
1074 vid_hdr->data_crc = cpu_to_be32(crc);
1076 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
1078 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1080 goto out_unlock_buf;
1084 /* Read the VID header back and check if it was written correctly */
1085 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1087 if (err != UBI_IO_BITFLIPS)
1088 ubi_warn("cannot read VID header back from PEB %d", to);
1091 goto out_unlock_buf;
1094 if (data_size > 0) {
1095 err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
1097 goto out_unlock_buf;
1102 * We've written the data and are going to read it back to make
1103 * sure it was written correctly.
1106 err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
1108 if (err != UBI_IO_BITFLIPS)
1109 ubi_warn("cannot read data back from PEB %d",
1113 goto out_unlock_buf;
1118 if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
1119 ubi_warn("read data back from PEB %d - it is different",
1121 goto out_unlock_buf;
1125 ubi_assert(vol->eba_tbl[lnum] == from);
1126 vol->eba_tbl[lnum] = to;
1129 mutex_unlock(&ubi->buf_mutex);
1131 leb_write_unlock(ubi, vol_id, lnum);
1136 * ubi_eba_init_scan - initialize the EBA unit using scanning information.
1137 * @ubi: UBI device description object
1138 * @si: scanning information
1140 * This function returns zero in case of success and a negative error code in
1143 int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1145 int i, j, err, num_volumes;
1146 struct ubi_scan_volume *sv;
1147 struct ubi_volume *vol;
1148 struct ubi_scan_leb *seb;
1151 dbg_eba("initialize EBA unit");
1153 spin_lock_init(&ubi->ltree_lock);
1154 mutex_init(&ubi->alc_mutex);
1155 ubi->ltree = RB_ROOT;
1157 ubi->global_sqnum = si->max_sqnum + 1;
1158 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1160 for (i = 0; i < num_volumes; i++) {
1161 vol = ubi->volumes[i];
1167 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1169 if (!vol->eba_tbl) {
1174 for (j = 0; j < vol->reserved_pebs; j++)
1175 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1177 sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
1181 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
1182 if (seb->lnum >= vol->reserved_pebs)
1184 * This may happen in case of an unclean reboot
1187 ubi_scan_move_to_list(sv, seb, &si->erase);
1188 vol->eba_tbl[seb->lnum] = seb->pnum;
1192 if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1193 ubi_err("no enough physical eraseblocks (%d, need %d)",
1194 ubi->avail_pebs, EBA_RESERVED_PEBS);
1198 ubi->avail_pebs -= EBA_RESERVED_PEBS;
1199 ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1201 if (ubi->bad_allowed) {
1202 ubi_calculate_reserved(ubi);
1204 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1205 /* No enough free physical eraseblocks */
1206 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1207 ubi_warn("cannot reserve enough PEBs for bad PEB "
1208 "handling, reserved %d, need %d",
1209 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1211 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1213 ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1214 ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
1217 dbg_eba("EBA unit is initialized");
1221 for (i = 0; i < num_volumes; i++) {
1222 if (!ubi->volumes[i])
1224 kfree(ubi->volumes[i]->eba_tbl);
1230 * ubi_eba_close - close EBA unit.
1231 * @ubi: UBI device description object
1233 void ubi_eba_close(const struct ubi_device *ubi)
1235 int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1237 dbg_eba("close EBA unit");
1239 for (i = 0; i < num_volumes; i++) {
1240 if (!ubi->volumes[i])
1242 kfree(ubi->volumes[i]->eba_tbl);