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_VOLUME_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 = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
142 return ERR_PTR(-ENOMEM);
145 init_rwsem(&le->mutex);
149 spin_lock(&ubi->ltree_lock);
150 le1 = ltree_lookup(ubi, vol_id, lnum);
154 * This logical eraseblock is already locked. The newly
155 * allocated lock entry is not needed.
160 struct rb_node **p, *parent = NULL;
163 * No lock entry, add the newly allocated one to the
164 * @ubi->ltree RB-tree.
168 p = &ubi->ltree.rb_node;
171 le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
173 if (vol_id < le1->vol_id)
175 else if (vol_id > le1->vol_id)
178 ubi_assert(lnum != le1->lnum);
179 if (lnum < le1->lnum)
186 rb_link_node(&le->rb, parent, p);
187 rb_insert_color(&le->rb, &ubi->ltree);
190 spin_unlock(&ubi->ltree_lock);
199 * leb_read_lock - lock logical eraseblock for reading.
200 * @ubi: UBI device description object
202 * @lnum: logical eraseblock number
204 * This function locks a logical eraseblock for reading. Returns zero in case
205 * of success and a negative error code in case of failure.
207 static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
209 struct ubi_ltree_entry *le;
211 le = ltree_add_entry(ubi, vol_id, lnum);
214 down_read(&le->mutex);
219 * leb_read_unlock - unlock logical eraseblock.
220 * @ubi: UBI device description object
222 * @lnum: logical eraseblock number
224 static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
227 struct ubi_ltree_entry *le;
229 spin_lock(&ubi->ltree_lock);
230 le = ltree_lookup(ubi, vol_id, lnum);
232 ubi_assert(le->users >= 0);
233 if (le->users == 0) {
234 rb_erase(&le->rb, &ubi->ltree);
237 spin_unlock(&ubi->ltree_lock);
245 * leb_write_lock - lock logical eraseblock for writing.
246 * @ubi: UBI device description object
248 * @lnum: logical eraseblock number
250 * This function locks a logical eraseblock for writing. Returns zero in case
251 * of success and a negative error code in case of failure.
253 static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
255 struct ubi_ltree_entry *le;
257 le = ltree_add_entry(ubi, vol_id, lnum);
260 down_write(&le->mutex);
265 * leb_write_lock - lock logical eraseblock for writing.
266 * @ubi: UBI device description object
268 * @lnum: logical eraseblock number
270 * This function locks a logical eraseblock for writing if there is no
271 * contention and does nothing if there is contention. Returns %0 in case of
272 * success, %1 in case of contention, and and a negative error code in case of
275 static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
278 struct ubi_ltree_entry *le;
280 le = ltree_add_entry(ubi, vol_id, lnum);
283 if (down_write_trylock(&le->mutex))
286 /* Contention, cancel */
287 spin_lock(&ubi->ltree_lock);
289 ubi_assert(le->users >= 0);
290 if (le->users == 0) {
291 rb_erase(&le->rb, &ubi->ltree);
295 spin_unlock(&ubi->ltree_lock);
303 * leb_write_unlock - unlock logical eraseblock.
304 * @ubi: UBI device description object
306 * @lnum: logical eraseblock number
308 static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
311 struct ubi_ltree_entry *le;
313 spin_lock(&ubi->ltree_lock);
314 le = ltree_lookup(ubi, vol_id, lnum);
316 ubi_assert(le->users >= 0);
317 if (le->users == 0) {
318 rb_erase(&le->rb, &ubi->ltree);
322 spin_unlock(&ubi->ltree_lock);
324 up_write(&le->mutex);
330 * ubi_eba_unmap_leb - un-map logical eraseblock.
331 * @ubi: UBI device description object
332 * @vol: volume description object
333 * @lnum: logical eraseblock number
335 * This function un-maps logical eraseblock @lnum and schedules corresponding
336 * physical eraseblock for erasure. Returns zero in case of success and a
337 * negative error code in case of failure.
339 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
342 int err, pnum, vol_id = vol->vol_id;
347 err = leb_write_lock(ubi, vol_id, lnum);
351 pnum = vol->eba_tbl[lnum];
353 /* This logical eraseblock is already unmapped */
356 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
358 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
359 err = ubi_wl_put_peb(ubi, pnum, 0);
362 leb_write_unlock(ubi, vol_id, lnum);
367 * ubi_eba_read_leb - read data.
368 * @ubi: UBI device description object
369 * @vol: volume description object
370 * @lnum: logical eraseblock number
371 * @buf: buffer to store the read data
372 * @offset: offset from where to read
373 * @len: how many bytes to read
374 * @check: data CRC check flag
376 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
377 * bytes. The @check flag only makes sense for static volumes and forces
378 * eraseblock data CRC checking.
380 * In case of success this function returns zero. In case of a static volume,
381 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
382 * returned for any volume type if an ECC error was detected by the MTD device
383 * driver. Other negative error cored may be returned in case of other errors.
385 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
386 void *buf, int offset, int len, int check)
388 int err, pnum, scrub = 0, vol_id = vol->vol_id;
389 struct ubi_vid_hdr *vid_hdr;
390 uint32_t uninitialized_var(crc);
392 err = leb_read_lock(ubi, vol_id, lnum);
396 pnum = vol->eba_tbl[lnum];
399 * The logical eraseblock is not mapped, fill the whole buffer
400 * with 0xFF bytes. The exception is static volumes for which
401 * it is an error to read unmapped logical eraseblocks.
403 dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
404 len, offset, vol_id, lnum);
405 leb_read_unlock(ubi, vol_id, lnum);
406 ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
407 memset(buf, 0xFF, len);
411 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
412 len, offset, vol_id, lnum, pnum);
414 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
419 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
425 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
426 if (err && err != UBI_IO_BITFLIPS) {
429 * The header is either absent or corrupted.
430 * The former case means there is a bug -
431 * switch to read-only mode just in case.
432 * The latter case means a real corruption - we
433 * may try to recover data. FIXME: but this is
436 if (err == UBI_IO_BAD_VID_HDR) {
437 ubi_warn("bad VID header at PEB %d, LEB"
438 "%d:%d", pnum, vol_id, lnum);
444 } else if (err == UBI_IO_BITFLIPS)
447 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
448 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
450 crc = be32_to_cpu(vid_hdr->data_crc);
451 ubi_free_vid_hdr(ubi, vid_hdr);
454 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
456 if (err == UBI_IO_BITFLIPS) {
459 } else if (err == -EBADMSG) {
460 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
464 ubi_msg("force data checking");
473 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
475 ubi_warn("CRC error: calculated %#08x, must be %#08x",
483 err = ubi_wl_scrub_peb(ubi, pnum);
485 leb_read_unlock(ubi, vol_id, lnum);
489 ubi_free_vid_hdr(ubi, vid_hdr);
491 leb_read_unlock(ubi, vol_id, lnum);
496 * recover_peb - recover from write failure.
497 * @ubi: UBI device description object
498 * @pnum: the physical eraseblock to recover
500 * @lnum: logical eraseblock number
501 * @buf: data which was not written because of the write failure
502 * @offset: offset of the failed write
503 * @len: how many bytes should have been written
505 * This function is called in case of a write failure and moves all good data
506 * from the potentially bad physical eraseblock to a good physical eraseblock.
507 * This function also writes the data which was not written due to the failure.
508 * Returns new physical eraseblock number in case of success, and a negative
509 * error code in case of failure.
511 static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
512 const void *buf, int offset, int len)
514 int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
515 struct ubi_volume *vol = ubi->volumes[idx];
516 struct ubi_vid_hdr *vid_hdr;
518 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
523 mutex_lock(&ubi->buf_mutex);
526 new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
528 mutex_unlock(&ubi->buf_mutex);
529 ubi_free_vid_hdr(ubi, vid_hdr);
533 ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum);
535 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
536 if (err && err != UBI_IO_BITFLIPS) {
542 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
543 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
547 data_size = offset + len;
548 memset(ubi->peb_buf1 + offset, 0xFF, len);
550 /* Read everything before the area where the write failure happened */
552 err = ubi_io_read_data(ubi, ubi->peb_buf1, pnum, 0, offset);
553 if (err && err != UBI_IO_BITFLIPS)
557 memcpy(ubi->peb_buf1 + offset, buf, len);
559 err = ubi_io_write_data(ubi, ubi->peb_buf1, new_pnum, 0, data_size);
563 mutex_unlock(&ubi->buf_mutex);
564 ubi_free_vid_hdr(ubi, vid_hdr);
566 vol->eba_tbl[lnum] = new_pnum;
567 ubi_wl_put_peb(ubi, pnum, 1);
569 ubi_msg("data was successfully recovered");
573 mutex_unlock(&ubi->buf_mutex);
574 ubi_wl_put_peb(ubi, new_pnum, 1);
575 ubi_free_vid_hdr(ubi, vid_hdr);
580 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
583 ubi_warn("failed to write to PEB %d", new_pnum);
584 ubi_wl_put_peb(ubi, new_pnum, 1);
585 if (++tries > UBI_IO_RETRIES) {
586 mutex_unlock(&ubi->buf_mutex);
587 ubi_free_vid_hdr(ubi, vid_hdr);
590 ubi_msg("try again");
595 * ubi_eba_write_leb - write data to dynamic volume.
596 * @ubi: UBI device description object
597 * @vol: volume description object
598 * @lnum: logical eraseblock number
599 * @buf: the data to write
600 * @offset: offset within the logical eraseblock where to write
601 * @len: how many bytes to write
604 * This function writes data to logical eraseblock @lnum of a dynamic volume
605 * @vol. Returns zero in case of success and a negative error code in case
606 * of failure. In case of error, it is possible that something was still
607 * written to the flash media, but may be some garbage.
609 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
610 const void *buf, int offset, int len, int dtype)
612 int err, pnum, tries = 0, vol_id = vol->vol_id;
613 struct ubi_vid_hdr *vid_hdr;
618 err = leb_write_lock(ubi, vol_id, lnum);
622 pnum = vol->eba_tbl[lnum];
624 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
625 len, offset, vol_id, lnum, pnum);
627 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
629 ubi_warn("failed to write data to PEB %d", pnum);
630 if (err == -EIO && ubi->bad_allowed)
631 err = recover_peb(ubi, pnum, vol_id, lnum, buf,
636 leb_write_unlock(ubi, vol_id, lnum);
641 * The logical eraseblock is not mapped. We have to get a free physical
642 * eraseblock and write the volume identifier header there first.
644 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
646 leb_write_unlock(ubi, vol_id, lnum);
650 vid_hdr->vol_type = UBI_VID_DYNAMIC;
651 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
652 vid_hdr->vol_id = cpu_to_be32(vol_id);
653 vid_hdr->lnum = cpu_to_be32(lnum);
654 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
655 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
658 pnum = ubi_wl_get_peb(ubi, dtype);
660 ubi_free_vid_hdr(ubi, vid_hdr);
661 leb_write_unlock(ubi, vol_id, lnum);
665 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
666 len, offset, vol_id, lnum, pnum);
668 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
670 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
676 err = ubi_io_write_data(ubi, buf, pnum, offset, len);
678 ubi_warn("failed to write %d bytes at offset %d of "
679 "LEB %d:%d, PEB %d", len, offset, vol_id,
685 vol->eba_tbl[lnum] = pnum;
687 leb_write_unlock(ubi, vol_id, lnum);
688 ubi_free_vid_hdr(ubi, vid_hdr);
692 if (err != -EIO || !ubi->bad_allowed) {
694 leb_write_unlock(ubi, vol_id, lnum);
695 ubi_free_vid_hdr(ubi, vid_hdr);
700 * Fortunately, this is the first write operation to this physical
701 * eraseblock, so just put it and request a new one. We assume that if
702 * this physical eraseblock went bad, the erase code will handle that.
704 err = ubi_wl_put_peb(ubi, pnum, 1);
705 if (err || ++tries > UBI_IO_RETRIES) {
707 leb_write_unlock(ubi, vol_id, lnum);
708 ubi_free_vid_hdr(ubi, vid_hdr);
712 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
713 ubi_msg("try another PEB");
718 * ubi_eba_write_leb_st - write data to static volume.
719 * @ubi: UBI device description object
720 * @vol: volume description object
721 * @lnum: logical eraseblock number
722 * @buf: data to write
723 * @len: how many bytes to write
725 * @used_ebs: how many logical eraseblocks will this volume contain
727 * This function writes data to logical eraseblock @lnum of static volume
728 * @vol. The @used_ebs argument should contain total number of logical
729 * eraseblock in this static volume.
731 * When writing to the last logical eraseblock, the @len argument doesn't have
732 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
733 * to the real data size, although the @buf buffer has to contain the
734 * alignment. In all other cases, @len has to be aligned.
736 * It is prohibited to write more then once to logical eraseblocks of static
737 * volumes. This function returns zero in case of success and a negative error
738 * code in case of failure.
740 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
741 int lnum, const void *buf, int len, int dtype,
744 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
745 struct ubi_vid_hdr *vid_hdr;
751 if (lnum == used_ebs - 1)
752 /* If this is the last LEB @len may be unaligned */
753 len = ALIGN(data_size, ubi->min_io_size);
755 ubi_assert(len % ubi->min_io_size == 0);
757 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
761 err = leb_write_lock(ubi, vol_id, lnum);
763 ubi_free_vid_hdr(ubi, vid_hdr);
767 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
768 vid_hdr->vol_id = cpu_to_be32(vol_id);
769 vid_hdr->lnum = cpu_to_be32(lnum);
770 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
771 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
773 crc = crc32(UBI_CRC32_INIT, buf, data_size);
774 vid_hdr->vol_type = UBI_VID_STATIC;
775 vid_hdr->data_size = cpu_to_be32(data_size);
776 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
777 vid_hdr->data_crc = cpu_to_be32(crc);
780 pnum = ubi_wl_get_peb(ubi, dtype);
782 ubi_free_vid_hdr(ubi, vid_hdr);
783 leb_write_unlock(ubi, vol_id, lnum);
787 dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
788 len, vol_id, lnum, pnum, used_ebs);
790 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
792 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
797 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
799 ubi_warn("failed to write %d bytes of data to PEB %d",
804 ubi_assert(vol->eba_tbl[lnum] < 0);
805 vol->eba_tbl[lnum] = pnum;
807 leb_write_unlock(ubi, vol_id, lnum);
808 ubi_free_vid_hdr(ubi, vid_hdr);
812 if (err != -EIO || !ubi->bad_allowed) {
814 * This flash device does not admit of bad eraseblocks or
815 * something nasty and unexpected happened. Switch to read-only
819 leb_write_unlock(ubi, vol_id, lnum);
820 ubi_free_vid_hdr(ubi, vid_hdr);
824 err = ubi_wl_put_peb(ubi, pnum, 1);
825 if (err || ++tries > UBI_IO_RETRIES) {
827 leb_write_unlock(ubi, vol_id, lnum);
828 ubi_free_vid_hdr(ubi, vid_hdr);
832 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
833 ubi_msg("try another PEB");
838 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
839 * @ubi: UBI device description object
840 * @vol: volume description object
841 * @lnum: logical eraseblock number
842 * @buf: data to write
843 * @len: how many bytes to write
846 * This function changes the contents of a logical eraseblock atomically. @buf
847 * has to contain new logical eraseblock data, and @len - the length of the
848 * data, which has to be aligned. This function guarantees that in case of an
849 * unclean reboot the old contents is preserved. Returns zero in case of
850 * success and a negative error code in case of failure.
852 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
853 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
855 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
856 int lnum, const void *buf, int len, int dtype)
858 int err, pnum, tries = 0, vol_id = vol->vol_id;
859 struct ubi_vid_hdr *vid_hdr;
867 * Special case when data length is zero. In this case the LEB
868 * has to be unmapped and mapped somewhere else.
870 err = ubi_eba_unmap_leb(ubi, vol, lnum);
873 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
876 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
880 mutex_lock(&ubi->alc_mutex);
881 err = leb_write_lock(ubi, vol_id, lnum);
885 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
886 vid_hdr->vol_id = cpu_to_be32(vol_id);
887 vid_hdr->lnum = cpu_to_be32(lnum);
888 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
889 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
891 crc = crc32(UBI_CRC32_INIT, buf, len);
892 vid_hdr->vol_type = UBI_VID_DYNAMIC;
893 vid_hdr->data_size = cpu_to_be32(len);
894 vid_hdr->copy_flag = 1;
895 vid_hdr->data_crc = cpu_to_be32(crc);
898 pnum = ubi_wl_get_peb(ubi, dtype);
904 dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
905 vol_id, lnum, vol->eba_tbl[lnum], pnum);
907 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
909 ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
914 err = ubi_io_write_data(ubi, buf, pnum, 0, len);
916 ubi_warn("failed to write %d bytes of data to PEB %d",
921 if (vol->eba_tbl[lnum] >= 0) {
922 err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1);
927 vol->eba_tbl[lnum] = pnum;
930 leb_write_unlock(ubi, vol_id, lnum);
932 mutex_unlock(&ubi->alc_mutex);
933 ubi_free_vid_hdr(ubi, vid_hdr);
937 if (err != -EIO || !ubi->bad_allowed) {
939 * This flash device does not admit of bad eraseblocks or
940 * something nasty and unexpected happened. Switch to read-only
947 err = ubi_wl_put_peb(ubi, pnum, 1);
948 if (err || ++tries > UBI_IO_RETRIES) {
953 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
954 ubi_msg("try another PEB");
959 * ubi_eba_copy_leb - copy logical eraseblock.
960 * @ubi: UBI device description object
961 * @from: physical eraseblock number from where to copy
962 * @to: physical eraseblock number where to copy
963 * @vid_hdr: VID header of the @from physical eraseblock
965 * This function copies logical eraseblock from physical eraseblock @from to
966 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
968 * o %0 in case of success;
969 * o %1 if the operation was canceled and should be tried later (e.g.,
970 * because a bit-flip was detected at the target PEB);
971 * o %2 if the volume is being deleted and this LEB should not be moved.
973 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
974 struct ubi_vid_hdr *vid_hdr)
976 int err, vol_id, lnum, data_size, aldata_size, idx;
977 struct ubi_volume *vol;
980 vol_id = be32_to_cpu(vid_hdr->vol_id);
981 lnum = be32_to_cpu(vid_hdr->lnum);
983 dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
985 if (vid_hdr->vol_type == UBI_VID_STATIC) {
986 data_size = be32_to_cpu(vid_hdr->data_size);
987 aldata_size = ALIGN(data_size, ubi->min_io_size);
989 data_size = aldata_size =
990 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
992 idx = vol_id2idx(ubi, vol_id);
993 spin_lock(&ubi->volumes_lock);
995 * Note, we may race with volume deletion, which means that the volume
996 * this logical eraseblock belongs to might be being deleted. Since the
997 * volume deletion unmaps all the volume's logical eraseblocks, it will
998 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
1000 vol = ubi->volumes[idx];
1002 /* No need to do further work, cancel */
1003 dbg_eba("volume %d is being removed, cancel", vol_id);
1004 spin_unlock(&ubi->volumes_lock);
1007 spin_unlock(&ubi->volumes_lock);
1010 * We do not want anybody to write to this logical eraseblock while we
1011 * are moving it, so lock it.
1013 * Note, we are using non-waiting locking here, because we cannot sleep
1014 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
1015 * unmapping the LEB which is mapped to the PEB we are going to move
1016 * (@from). This task locks the LEB and goes sleep in the
1017 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
1018 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
1019 * LEB is already locked, we just do not move it and return %1.
1021 err = leb_write_trylock(ubi, vol_id, lnum);
1023 dbg_eba("contention on LEB %d:%d, cancel", vol_id, lnum);
1028 * The LEB might have been put meanwhile, and the task which put it is
1029 * probably waiting on @ubi->move_mutex. No need to continue the work,
1032 if (vol->eba_tbl[lnum] != from) {
1033 dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
1034 "PEB %d, cancel", vol_id, lnum, from,
1035 vol->eba_tbl[lnum]);
1037 goto out_unlock_leb;
1041 * OK, now the LEB is locked and we can safely start moving iy. Since
1042 * this function utilizes thie @ubi->peb1_buf buffer which is shared
1043 * with some other functions, so lock the buffer by taking the
1046 mutex_lock(&ubi->buf_mutex);
1047 dbg_eba("read %d bytes of data", aldata_size);
1048 err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
1049 if (err && err != UBI_IO_BITFLIPS) {
1050 ubi_warn("error %d while reading data from PEB %d",
1052 goto out_unlock_buf;
1056 * Now we have got to calculate how much data we have to to copy. In
1057 * case of a static volume it is fairly easy - the VID header contains
1058 * the data size. In case of a dynamic volume it is more difficult - we
1059 * have to read the contents, cut 0xFF bytes from the end and copy only
1060 * the first part. We must do this to avoid writing 0xFF bytes as it
1061 * may have some side-effects. And not only this. It is important not
1062 * to include those 0xFFs to CRC because later the they may be filled
1065 if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1066 aldata_size = data_size =
1067 ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
1070 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
1074 * It may turn out to me that the whole @from physical eraseblock
1075 * contains only 0xFF bytes. Then we have to only write the VID header
1076 * and do not write any data. This also means we should not set
1077 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1079 if (data_size > 0) {
1080 vid_hdr->copy_flag = 1;
1081 vid_hdr->data_size = cpu_to_be32(data_size);
1082 vid_hdr->data_crc = cpu_to_be32(crc);
1084 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
1086 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1088 goto out_unlock_buf;
1092 /* Read the VID header back and check if it was written correctly */
1093 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1095 if (err != UBI_IO_BITFLIPS)
1096 ubi_warn("cannot read VID header back from PEB %d", to);
1099 goto out_unlock_buf;
1102 if (data_size > 0) {
1103 err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
1105 goto out_unlock_buf;
1110 * We've written the data and are going to read it back to make
1111 * sure it was written correctly.
1114 err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
1116 if (err != UBI_IO_BITFLIPS)
1117 ubi_warn("cannot read data back from PEB %d",
1121 goto out_unlock_buf;
1126 if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
1127 ubi_warn("read data back from PEB %d - it is different",
1129 goto out_unlock_buf;
1133 ubi_assert(vol->eba_tbl[lnum] == from);
1134 vol->eba_tbl[lnum] = to;
1137 mutex_unlock(&ubi->buf_mutex);
1139 leb_write_unlock(ubi, vol_id, lnum);
1144 * ubi_eba_init_scan - initialize the EBA unit using scanning information.
1145 * @ubi: UBI device description object
1146 * @si: scanning information
1148 * This function returns zero in case of success and a negative error code in
1151 int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1153 int i, j, err, num_volumes;
1154 struct ubi_scan_volume *sv;
1155 struct ubi_volume *vol;
1156 struct ubi_scan_leb *seb;
1159 dbg_eba("initialize EBA unit");
1161 spin_lock_init(&ubi->ltree_lock);
1162 mutex_init(&ubi->alc_mutex);
1163 ubi->ltree = RB_ROOT;
1165 ubi->global_sqnum = si->max_sqnum + 1;
1166 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1168 for (i = 0; i < num_volumes; i++) {
1169 vol = ubi->volumes[i];
1175 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1177 if (!vol->eba_tbl) {
1182 for (j = 0; j < vol->reserved_pebs; j++)
1183 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1185 sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
1189 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
1190 if (seb->lnum >= vol->reserved_pebs)
1192 * This may happen in case of an unclean reboot
1195 ubi_scan_move_to_list(sv, seb, &si->erase);
1196 vol->eba_tbl[seb->lnum] = seb->pnum;
1200 if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1201 ubi_err("no enough physical eraseblocks (%d, need %d)",
1202 ubi->avail_pebs, EBA_RESERVED_PEBS);
1206 ubi->avail_pebs -= EBA_RESERVED_PEBS;
1207 ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1209 if (ubi->bad_allowed) {
1210 ubi_calculate_reserved(ubi);
1212 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1213 /* No enough free physical eraseblocks */
1214 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1215 ubi_warn("cannot reserve enough PEBs for bad PEB "
1216 "handling, reserved %d, need %d",
1217 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1219 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1221 ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1222 ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
1225 dbg_eba("EBA unit is initialized");
1229 for (i = 0; i < num_volumes; i++) {
1230 if (!ubi->volumes[i])
1232 kfree(ubi->volumes[i]->eba_tbl);
1238 * ubi_eba_close - close EBA unit.
1239 * @ubi: UBI device description object
1241 void ubi_eba_close(const struct ubi_device *ubi)
1243 int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1245 dbg_eba("close EBA unit");
1247 for (i = 0; i < num_volumes; i++) {
1248 if (!ubi->volumes[i])
1250 kfree(ubi->volumes[i]->eba_tbl);