2 * Copyright (c) International Business Machines Corp., 2006
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author: Artem Bityutskiy (Битюцкий Артём)
22 * The UBI Eraseblock Association (EBA) unit.
24 * This unit is responsible for I/O to/from logical eraseblock.
26 * Although in this implementation the EBA table is fully kept and managed in
27 * RAM, which assumes poor scalability, it might be (partially) maintained on
28 * flash in future implementations.
30 * The EBA unit implements per-logical eraseblock locking. Before accessing a
31 * logical eraseblock it is locked for reading or writing. The per-logical
32 * eraseblock locking is implemented by means of the lock tree. The lock tree
33 * is an RB-tree which refers all the currently locked logical eraseblocks. The
34 * lock tree elements are &struct ubi_ltree_entry objects. They are indexed by
35 * (@vol_id, @lnum) pairs.
37 * EBA also maintains the global sequence counter which is incremented each
38 * time a logical eraseblock is mapped to a physical eraseblock and it is
39 * stored in the volume identifier header. This means that each VID header has
40 * a unique sequence number. The sequence number is only increased an we assume
41 * 64 bits is enough to never overflow.
44 #include <linux/slab.h>
45 #include <linux/crc32.h>
46 #include <linux/err.h>
49 /* Number of physical eraseblocks reserved for atomic LEB change operation */
50 #define EBA_RESERVED_PEBS 1
53 * next_sqnum - get next sequence number.
54 * @ubi: UBI device description object
56 * This function returns next sequence number to use, which is just the current
57 * global sequence counter value. It also increases the global sequence
60 static unsigned long long next_sqnum(struct ubi_device *ubi)
62 unsigned long long sqnum;
64 spin_lock(&ubi->ltree_lock);
65 sqnum = ubi->global_sqnum++;
66 spin_unlock(&ubi->ltree_lock);
72 * ubi_get_compat - get compatibility flags of a volume.
73 * @ubi: UBI device description object
76 * This function returns compatibility flags for an internal volume. User
77 * volumes have no compatibility flags, so %0 is returned.
79 static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
81 if (vol_id == UBI_LAYOUT_VOL_ID)
82 return UBI_LAYOUT_VOLUME_COMPAT;
87 * ltree_lookup - look up the lock tree.
88 * @ubi: UBI device description object
90 * @lnum: logical eraseblock number
92 * This function returns a pointer to the corresponding &struct ubi_ltree_entry
93 * object if the logical eraseblock is locked and %NULL if it is not.
94 * @ubi->ltree_lock has to be locked.
96 static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
101 p = ubi->ltree.rb_node;
103 struct ubi_ltree_entry *le;
105 le = rb_entry(p, struct ubi_ltree_entry, rb);
107 if (vol_id < le->vol_id)
109 else if (vol_id > le->vol_id)
114 else if (lnum > le->lnum)
125 * ltree_add_entry - add new entry to the lock tree.
126 * @ubi: UBI device description object
128 * @lnum: logical eraseblock number
130 * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
131 * lock tree. If such entry is already there, its usage counter is increased.
132 * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
135 static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
136 int vol_id, int lnum)
138 struct ubi_ltree_entry *le, *le1, *le_free;
140 le = kmem_cache_alloc(ubi_ltree_slab, GFP_NOFS);
142 return ERR_PTR(-ENOMEM);
147 spin_lock(&ubi->ltree_lock);
148 le1 = ltree_lookup(ubi, vol_id, lnum);
152 * This logical eraseblock is already locked. The newly
153 * allocated lock entry is not needed.
158 struct rb_node **p, *parent = NULL;
161 * No lock entry, add the newly allocated one to the
162 * @ubi->ltree RB-tree.
166 p = &ubi->ltree.rb_node;
169 le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
171 if (vol_id < le1->vol_id)
173 else if (vol_id > le1->vol_id)
176 ubi_assert(lnum != le1->lnum);
177 if (lnum < le1->lnum)
184 rb_link_node(&le->rb, parent, p);
185 rb_insert_color(&le->rb, &ubi->ltree);
188 spin_unlock(&ubi->ltree_lock);
191 kmem_cache_free(ubi_ltree_slab, le_free);
197 * leb_read_lock - lock logical eraseblock for reading.
198 * @ubi: UBI device description object
200 * @lnum: logical eraseblock number
202 * This function locks a logical eraseblock for reading. Returns zero in case
203 * of success and a negative error code in case of failure.
205 static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
207 struct ubi_ltree_entry *le;
209 le = ltree_add_entry(ubi, vol_id, lnum);
212 down_read(&le->mutex);
217 * leb_read_unlock - unlock logical eraseblock.
218 * @ubi: UBI device description object
220 * @lnum: logical eraseblock number
222 static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
225 struct ubi_ltree_entry *le;
227 spin_lock(&ubi->ltree_lock);
228 le = ltree_lookup(ubi, vol_id, lnum);
230 ubi_assert(le->users >= 0);
231 if (le->users == 0) {
232 rb_erase(&le->rb, &ubi->ltree);
235 spin_unlock(&ubi->ltree_lock);
239 kmem_cache_free(ubi_ltree_slab, le);
243 * leb_write_lock - lock logical eraseblock for writing.
244 * @ubi: UBI device description object
246 * @lnum: logical eraseblock number
248 * This function locks a logical eraseblock for writing. Returns zero in case
249 * of success and a negative error code in case of failure.
251 static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
253 struct ubi_ltree_entry *le;
255 le = ltree_add_entry(ubi, vol_id, lnum);
258 down_write(&le->mutex);
263 * leb_write_unlock - unlock logical eraseblock.
264 * @ubi: UBI device description object
266 * @lnum: logical eraseblock number
268 static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
271 struct ubi_ltree_entry *le;
273 spin_lock(&ubi->ltree_lock);
274 le = ltree_lookup(ubi, vol_id, lnum);
276 ubi_assert(le->users >= 0);
277 if (le->users == 0) {
278 rb_erase(&le->rb, &ubi->ltree);
282 spin_unlock(&ubi->ltree_lock);
284 up_write(&le->mutex);
286 kmem_cache_free(ubi_ltree_slab, le);
290 * ubi_eba_unmap_leb - un-map logical eraseblock.
291 * @ubi: UBI device description object
292 * @vol: volume description object
293 * @lnum: logical eraseblock number
295 * This function un-maps logical eraseblock @lnum and schedules corresponding
296 * physical eraseblock for erasure. Returns zero in case of success and a
297 * negative error code in case of failure.
299 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
302 int err, pnum, vol_id = vol->vol_id;
307 err = leb_write_lock(ubi, vol_id, lnum);
311 pnum = vol->eba_tbl[lnum];
313 /* This logical eraseblock is already unmapped */
316 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
318 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
319 err = ubi_wl_put_peb(ubi, pnum, 0);
322 leb_write_unlock(ubi, vol_id, lnum);
327 * ubi_eba_read_leb - read data.
328 * @ubi: UBI device description object
329 * @vol: volume description object
330 * @lnum: logical eraseblock number
331 * @buf: buffer to store the read data
332 * @offset: offset from where to read
333 * @len: how many bytes to read
334 * @check: data CRC check flag
336 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
337 * bytes. The @check flag only makes sense for static volumes and forces
338 * eraseblock data CRC checking.
340 * In case of success this function returns zero. In case of a static volume,
341 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
342 * returned for any volume type if an ECC error was detected by the MTD device
343 * driver. Other negative error cored may be returned in case of other errors.
345 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
346 void *buf, int offset, int len, int check)
348 int err, pnum, scrub = 0, vol_id = vol->vol_id;
349 struct ubi_vid_hdr *vid_hdr;
350 uint32_t uninitialized_var(crc);
352 err = leb_read_lock(ubi, vol_id, lnum);
356 pnum = vol->eba_tbl[lnum];
359 * The logical eraseblock is not mapped, fill the whole buffer
360 * with 0xFF bytes. The exception is static volumes for which
361 * it is an error to read unmapped logical eraseblocks.
363 dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
364 len, offset, vol_id, lnum);
365 leb_read_unlock(ubi, vol_id, lnum);
366 ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
367 memset(buf, 0xFF, len);
371 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
372 len, offset, vol_id, lnum, pnum);
374 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
379 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
385 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
386 if (err && err != UBI_IO_BITFLIPS) {
389 * The header is either absent or corrupted.
390 * The former case means there is a bug -
391 * switch to read-only mode just in case.
392 * The latter case means a real corruption - we
393 * may try to recover data. FIXME: but this is
396 if (err == UBI_IO_BAD_VID_HDR) {
397 ubi_warn("bad VID header at PEB %d, LEB"
398 "%d:%d", pnum, vol_id, lnum);
404 } else if (err == UBI_IO_BITFLIPS)
407 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
408 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
410 crc = be32_to_cpu(vid_hdr->data_crc);
411 ubi_free_vid_hdr(ubi, vid_hdr);
414 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
416 if (err == UBI_IO_BITFLIPS) {
419 } else if (err == -EBADMSG) {
420 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
424 ubi_msg("force data checking");
433 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
435 ubi_warn("CRC error: calculated %#08x, must be %#08x",
443 err = ubi_wl_scrub_peb(ubi, pnum);
445 leb_read_unlock(ubi, vol_id, lnum);
449 ubi_free_vid_hdr(ubi, vid_hdr);
451 leb_read_unlock(ubi, vol_id, lnum);
456 * recover_peb - recover from write failure.
457 * @ubi: UBI device description object
458 * @pnum: the physical eraseblock to recover
460 * @lnum: logical eraseblock number
461 * @buf: data which was not written because of the write failure
462 * @offset: offset of the failed write
463 * @len: how many bytes should have been written
465 * This function is called in case of a write failure and moves all good data
466 * from the potentially bad physical eraseblock to a good physical eraseblock.
467 * This function also writes the data which was not written due to the failure.
468 * Returns new physical eraseblock number in case of success, and a negative
469 * error code in case of failure.
471 static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
472 const void *buf, int offset, int len)
474 int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
475 struct ubi_volume *vol = ubi->volumes[idx];
476 struct ubi_vid_hdr *vid_hdr;
478 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
483 mutex_lock(&ubi->buf_mutex);
486 new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
488 mutex_unlock(&ubi->buf_mutex);
489 ubi_free_vid_hdr(ubi, vid_hdr);
493 ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum);
495 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
496 if (err && err != UBI_IO_BITFLIPS) {
502 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
503 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
507 data_size = offset + len;
508 memset(ubi->peb_buf1 + offset, 0xFF, len);
510 /* Read everything before the area where the write failure happened */
512 err = ubi_io_read_data(ubi, ubi->peb_buf1, pnum, 0, offset);
513 if (err && err != UBI_IO_BITFLIPS)
517 memcpy(ubi->peb_buf1 + offset, buf, len);
519 err = ubi_io_write_data(ubi, ubi->peb_buf1, new_pnum, 0, data_size);
523 mutex_unlock(&ubi->buf_mutex);
524 ubi_free_vid_hdr(ubi, vid_hdr);
526 vol->eba_tbl[lnum] = new_pnum;
527 ubi_wl_put_peb(ubi, pnum, 1);
529 ubi_msg("data was successfully recovered");
533 mutex_unlock(&ubi->buf_mutex);
534 ubi_wl_put_peb(ubi, new_pnum, 1);
535 ubi_free_vid_hdr(ubi, vid_hdr);
540 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
543 ubi_warn("failed to write to PEB %d", new_pnum);
544 ubi_wl_put_peb(ubi, new_pnum, 1);
545 if (++tries > UBI_IO_RETRIES) {
546 mutex_unlock(&ubi->buf_mutex);
547 ubi_free_vid_hdr(ubi, vid_hdr);
550 ubi_msg("try again");
555 * ubi_eba_write_leb - write data to dynamic volume.
556 * @ubi: UBI device description object
557 * @vol: volume description object
558 * @lnum: logical eraseblock number
559 * @buf: the data to write
560 * @offset: offset within the logical eraseblock where to write
561 * @len: how many bytes to write
564 * This function writes data to logical eraseblock @lnum of a dynamic volume
565 * @vol. Returns zero in case of success and a negative error code in case
566 * of failure. In case of error, it is possible that something was still
567 * written to the flash media, but may be some garbage.
569 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
570 const void *buf, int offset, int len, int dtype)
572 int err, pnum, tries = 0, vol_id = vol->vol_id;
573 struct ubi_vid_hdr *vid_hdr;
578 err = leb_write_lock(ubi, vol_id, lnum);
582 pnum = vol->eba_tbl[lnum];
584 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
585 len, offset, vol_id, lnum, pnum);
587 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
589 ubi_warn("failed to write data to PEB %d", pnum);
590 if (err == -EIO && ubi->bad_allowed)
591 err = recover_peb(ubi, pnum, vol_id, lnum, buf,
596 leb_write_unlock(ubi, vol_id, lnum);
601 * The logical eraseblock is not mapped. We have to get a free physical
602 * eraseblock and write the volume identifier header there first.
604 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
606 leb_write_unlock(ubi, vol_id, lnum);
610 vid_hdr->vol_type = UBI_VID_DYNAMIC;
611 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
612 vid_hdr->vol_id = cpu_to_be32(vol_id);
613 vid_hdr->lnum = cpu_to_be32(lnum);
614 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
615 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
618 pnum = ubi_wl_get_peb(ubi, dtype);
620 ubi_free_vid_hdr(ubi, vid_hdr);
621 leb_write_unlock(ubi, vol_id, lnum);
625 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
626 len, offset, vol_id, lnum, pnum);
628 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
630 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
636 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
638 ubi_warn("failed to write %d bytes at offset %d of "
639 "LEB %d:%d, PEB %d", len, offset, vol_id,
645 vol->eba_tbl[lnum] = pnum;
647 leb_write_unlock(ubi, vol_id, lnum);
648 ubi_free_vid_hdr(ubi, vid_hdr);
652 if (err != -EIO || !ubi->bad_allowed) {
654 leb_write_unlock(ubi, vol_id, lnum);
655 ubi_free_vid_hdr(ubi, vid_hdr);
660 * Fortunately, this is the first write operation to this physical
661 * eraseblock, so just put it and request a new one. We assume that if
662 * this physical eraseblock went bad, the erase code will handle that.
664 err = ubi_wl_put_peb(ubi, pnum, 1);
665 if (err || ++tries > UBI_IO_RETRIES) {
667 leb_write_unlock(ubi, vol_id, lnum);
668 ubi_free_vid_hdr(ubi, vid_hdr);
672 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
673 ubi_msg("try another PEB");
678 * ubi_eba_write_leb_st - write data to static volume.
679 * @ubi: UBI device description object
680 * @vol: volume description object
681 * @lnum: logical eraseblock number
682 * @buf: data to write
683 * @len: how many bytes to write
685 * @used_ebs: how many logical eraseblocks will this volume contain
687 * This function writes data to logical eraseblock @lnum of static volume
688 * @vol. The @used_ebs argument should contain total number of logical
689 * eraseblock in this static volume.
691 * When writing to the last logical eraseblock, the @len argument doesn't have
692 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
693 * to the real data size, although the @buf buffer has to contain the
694 * alignment. In all other cases, @len has to be aligned.
696 * It is prohibited to write more then once to logical eraseblocks of static
697 * volumes. This function returns zero in case of success and a negative error
698 * code in case of failure.
700 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
701 int lnum, const void *buf, int len, int dtype,
704 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
705 struct ubi_vid_hdr *vid_hdr;
711 if (lnum == used_ebs - 1)
712 /* If this is the last LEB @len may be unaligned */
713 len = ALIGN(data_size, ubi->min_io_size);
715 ubi_assert(len % ubi->min_io_size == 0);
717 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
721 err = leb_write_lock(ubi, vol_id, lnum);
723 ubi_free_vid_hdr(ubi, vid_hdr);
727 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
728 vid_hdr->vol_id = cpu_to_be32(vol_id);
729 vid_hdr->lnum = cpu_to_be32(lnum);
730 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
731 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
733 crc = crc32(UBI_CRC32_INIT, buf, data_size);
734 vid_hdr->vol_type = UBI_VID_STATIC;
735 vid_hdr->data_size = cpu_to_be32(data_size);
736 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
737 vid_hdr->data_crc = cpu_to_be32(crc);
740 pnum = ubi_wl_get_peb(ubi, dtype);
742 ubi_free_vid_hdr(ubi, vid_hdr);
743 leb_write_unlock(ubi, vol_id, lnum);
747 dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
748 len, vol_id, lnum, pnum, used_ebs);
750 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
752 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
757 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
759 ubi_warn("failed to write %d bytes of data to PEB %d",
764 ubi_assert(vol->eba_tbl[lnum] < 0);
765 vol->eba_tbl[lnum] = pnum;
767 leb_write_unlock(ubi, vol_id, lnum);
768 ubi_free_vid_hdr(ubi, vid_hdr);
772 if (err != -EIO || !ubi->bad_allowed) {
774 * This flash device does not admit of bad eraseblocks or
775 * something nasty and unexpected happened. Switch to read-only
779 leb_write_unlock(ubi, vol_id, lnum);
780 ubi_free_vid_hdr(ubi, vid_hdr);
784 err = ubi_wl_put_peb(ubi, pnum, 1);
785 if (err || ++tries > UBI_IO_RETRIES) {
787 leb_write_unlock(ubi, vol_id, lnum);
788 ubi_free_vid_hdr(ubi, vid_hdr);
792 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
793 ubi_msg("try another PEB");
798 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
799 * @ubi: UBI device description object
800 * @vol: volume escription object
801 * @lnum: logical eraseblock number
802 * @buf: data to write
803 * @len: how many bytes to write
806 * This function changes the contents of a logical eraseblock atomically. @buf
807 * has to contain new logical eraseblock data, and @len - the length of the
808 * data, which has to be aligned. This function guarantees that in case of an
809 * unclean reboot the old contents is preserved. Returns zero in case of
810 * success and a negative error code in case of failure.
812 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
813 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
815 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
816 int lnum, const void *buf, int len, int dtype)
818 int err, pnum, tries = 0, vol_id = vol->vol_id;
819 struct ubi_vid_hdr *vid_hdr;
825 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
829 mutex_lock(&ubi->alc_mutex);
830 err = leb_write_lock(ubi, vol_id, lnum);
834 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
835 vid_hdr->vol_id = cpu_to_be32(vol_id);
836 vid_hdr->lnum = cpu_to_be32(lnum);
837 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
838 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
840 crc = crc32(UBI_CRC32_INIT, buf, len);
841 vid_hdr->vol_type = UBI_VID_DYNAMIC;
842 vid_hdr->data_size = cpu_to_be32(len);
843 vid_hdr->copy_flag = 1;
844 vid_hdr->data_crc = cpu_to_be32(crc);
847 pnum = ubi_wl_get_peb(ubi, dtype);
853 dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
854 vol_id, lnum, vol->eba_tbl[lnum], pnum);
856 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
858 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
863 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
865 ubi_warn("failed to write %d bytes of data to PEB %d",
870 if (vol->eba_tbl[lnum] >= 0) {
871 err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1);
876 vol->eba_tbl[lnum] = pnum;
879 leb_write_unlock(ubi, vol_id, lnum);
881 mutex_unlock(&ubi->alc_mutex);
882 ubi_free_vid_hdr(ubi, vid_hdr);
886 if (err != -EIO || !ubi->bad_allowed) {
888 * This flash device does not admit of bad eraseblocks or
889 * something nasty and unexpected happened. Switch to read-only
896 err = ubi_wl_put_peb(ubi, pnum, 1);
897 if (err || ++tries > UBI_IO_RETRIES) {
902 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
903 ubi_msg("try another PEB");
908 * ubi_eba_copy_leb - copy logical eraseblock.
909 * @ubi: UBI device description object
910 * @from: physical eraseblock number from where to copy
911 * @to: physical eraseblock number where to copy
912 * @vid_hdr: VID header of the @from physical eraseblock
914 * This function copies logical eraseblock from physical eraseblock @from to
915 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
916 * function. Returns zero in case of success, %UBI_IO_BITFLIPS if the operation
917 * was canceled because bit-flips were detected at the target PEB, and a
918 * negative error code in case of failure.
920 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
921 struct ubi_vid_hdr *vid_hdr)
923 int err, vol_id, lnum, data_size, aldata_size, pnum, idx;
924 struct ubi_volume *vol;
927 vol_id = be32_to_cpu(vid_hdr->vol_id);
928 lnum = be32_to_cpu(vid_hdr->lnum);
930 dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
932 if (vid_hdr->vol_type == UBI_VID_STATIC) {
933 data_size = be32_to_cpu(vid_hdr->data_size);
934 aldata_size = ALIGN(data_size, ubi->min_io_size);
936 data_size = aldata_size =
937 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
940 * We do not want anybody to write to this logical eraseblock while we
941 * are moving it, so we lock it.
943 err = leb_write_lock(ubi, vol_id, lnum);
947 mutex_lock(&ubi->buf_mutex);
950 * But the logical eraseblock might have been put by this time.
951 * Cancel if it is true.
953 idx = vol_id2idx(ubi, vol_id);
956 * We may race with volume deletion/re-size, so we have to hold
957 * @ubi->volumes_lock.
959 spin_lock(&ubi->volumes_lock);
960 vol = ubi->volumes[idx];
962 dbg_eba("volume %d was removed meanwhile", vol_id);
963 spin_unlock(&ubi->volumes_lock);
967 pnum = vol->eba_tbl[lnum];
969 dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
970 "PEB %d, cancel", vol_id, lnum, from, pnum);
971 spin_unlock(&ubi->volumes_lock);
974 spin_unlock(&ubi->volumes_lock);
976 /* OK, now the LEB is locked and we can safely start moving it */
978 dbg_eba("read %d bytes of data", aldata_size);
979 err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
980 if (err && err != UBI_IO_BITFLIPS) {
981 ubi_warn("error %d while reading data from PEB %d",
987 * Now we have got to calculate how much data we have to to copy. In
988 * case of a static volume it is fairly easy - the VID header contains
989 * the data size. In case of a dynamic volume it is more difficult - we
990 * have to read the contents, cut 0xFF bytes from the end and copy only
991 * the first part. We must do this to avoid writing 0xFF bytes as it
992 * may have some side-effects. And not only this. It is important not
993 * to include those 0xFFs to CRC because later the they may be filled
996 if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
997 aldata_size = data_size =
998 ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
1001 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
1005 * It may turn out to me that the whole @from physical eraseblock
1006 * contains only 0xFF bytes. Then we have to only write the VID header
1007 * and do not write any data. This also means we should not set
1008 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1010 if (data_size > 0) {
1011 vid_hdr->copy_flag = 1;
1012 vid_hdr->data_size = cpu_to_be32(data_size);
1013 vid_hdr->data_crc = cpu_to_be32(crc);
1015 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
1017 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1023 /* Read the VID header back and check if it was written correctly */
1024 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1026 if (err != UBI_IO_BITFLIPS)
1027 ubi_warn("cannot read VID header back from PEB %d", to);
1031 if (data_size > 0) {
1032 err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
1039 * We've written the data and are going to read it back to make
1040 * sure it was written correctly.
1043 err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
1045 if (err != UBI_IO_BITFLIPS)
1046 ubi_warn("cannot read data back from PEB %d",
1053 if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
1054 ubi_warn("read data back from PEB %d - it is different",
1060 ubi_assert(vol->eba_tbl[lnum] == from);
1061 vol->eba_tbl[lnum] = to;
1064 mutex_unlock(&ubi->buf_mutex);
1065 leb_write_unlock(ubi, vol_id, lnum);
1070 * ubi_eba_init_scan - initialize the EBA unit using scanning information.
1071 * @ubi: UBI device description object
1072 * @si: scanning information
1074 * This function returns zero in case of success and a negative error code in
1077 int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1079 int i, j, err, num_volumes;
1080 struct ubi_scan_volume *sv;
1081 struct ubi_volume *vol;
1082 struct ubi_scan_leb *seb;
1085 dbg_eba("initialize EBA unit");
1087 spin_lock_init(&ubi->ltree_lock);
1088 mutex_init(&ubi->alc_mutex);
1089 ubi->ltree = RB_ROOT;
1091 ubi->global_sqnum = si->max_sqnum + 1;
1092 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1094 for (i = 0; i < num_volumes; i++) {
1095 vol = ubi->volumes[i];
1101 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1103 if (!vol->eba_tbl) {
1108 for (j = 0; j < vol->reserved_pebs; j++)
1109 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1111 sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
1115 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
1116 if (seb->lnum >= vol->reserved_pebs)
1118 * This may happen in case of an unclean reboot
1121 ubi_scan_move_to_list(sv, seb, &si->erase);
1122 vol->eba_tbl[seb->lnum] = seb->pnum;
1126 if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1127 ubi_err("no enough physical eraseblocks (%d, need %d)",
1128 ubi->avail_pebs, EBA_RESERVED_PEBS);
1132 ubi->avail_pebs -= EBA_RESERVED_PEBS;
1133 ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1135 if (ubi->bad_allowed) {
1136 ubi_calculate_reserved(ubi);
1138 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1139 /* No enough free physical eraseblocks */
1140 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1141 ubi_warn("cannot reserve enough PEBs for bad PEB "
1142 "handling, reserved %d, need %d",
1143 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1145 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1147 ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1148 ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
1151 dbg_eba("EBA unit is initialized");
1155 for (i = 0; i < num_volumes; i++) {
1156 if (!ubi->volumes[i])
1158 kfree(ubi->volumes[i]->eba_tbl);
1164 * ubi_eba_close - close EBA unit.
1165 * @ubi: UBI device description object
1167 void ubi_eba_close(const struct ubi_device *ubi)
1169 int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1171 dbg_eba("close EBA unit");
1173 for (i = 0; i < num_volumes; i++) {
1174 if (!ubi->volumes[i])
1176 kfree(ubi->volumes[i]->eba_tbl);