eba.c (799dca34ac543485f581bd8464ec9b1c4f0f852a) eba.c (3291b52f9ff0acc80a8ee3f92a960db937dccecb)
1/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
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.
8 *

--- 499 unchanged lines hidden (view full) ---

508 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
509 * returned for any volume type if an ECC error was detected by the MTD device
510 * driver. Other negative error cored may be returned in case of other errors.
511 */
512int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
513 void *buf, int offset, int len, int check)
514{
515 int err, pnum, scrub = 0, vol_id = vol->vol_id;
1/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
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.
8 *

--- 499 unchanged lines hidden (view full) ---

508 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
509 * returned for any volume type if an ECC error was detected by the MTD device
510 * driver. Other negative error cored may be returned in case of other errors.
511 */
512int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
513 void *buf, int offset, int len, int check)
514{
515 int err, pnum, scrub = 0, vol_id = vol->vol_id;
516 struct ubi_vid_io_buf *vidb;
516 struct ubi_vid_hdr *vid_hdr;
517 uint32_t uninitialized_var(crc);
518
519 err = leb_read_lock(ubi, vol_id, lnum);
520 if (err)
521 return err;
522
523 pnum = vol->eba_tbl->entries[lnum].pnum;

--- 14 unchanged lines hidden (view full) ---

538 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
539 len, offset, vol_id, lnum, pnum);
540
541 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
542 check = 0;
543
544retry:
545 if (check) {
517 struct ubi_vid_hdr *vid_hdr;
518 uint32_t uninitialized_var(crc);
519
520 err = leb_read_lock(ubi, vol_id, lnum);
521 if (err)
522 return err;
523
524 pnum = vol->eba_tbl->entries[lnum].pnum;

--- 14 unchanged lines hidden (view full) ---

539 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
540 len, offset, vol_id, lnum, pnum);
541
542 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
543 check = 0;
544
545retry:
546 if (check) {
546 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
547 if (!vid_hdr) {
547 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
548 if (!vidb) {
548 err = -ENOMEM;
549 goto out_unlock;
550 }
551
549 err = -ENOMEM;
550 goto out_unlock;
551 }
552
552 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
553 vid_hdr = ubi_get_vid_hdr(vidb);
554
555 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
553 if (err && err != UBI_IO_BITFLIPS) {
554 if (err > 0) {
555 /*
556 * The header is either absent or corrupted.
557 * The former case means there is a bug -
558 * switch to read-only mode just in case.
559 * The latter case means a real corruption - we
560 * may try to recover data. FIXME: but this is

--- 29 unchanged lines hidden (view full) ---

590 goto out_free;
591 } else if (err == UBI_IO_BITFLIPS)
592 scrub = 1;
593
594 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
595 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
596
597 crc = be32_to_cpu(vid_hdr->data_crc);
556 if (err && err != UBI_IO_BITFLIPS) {
557 if (err > 0) {
558 /*
559 * The header is either absent or corrupted.
560 * The former case means there is a bug -
561 * switch to read-only mode just in case.
562 * The latter case means a real corruption - we
563 * may try to recover data. FIXME: but this is

--- 29 unchanged lines hidden (view full) ---

593 goto out_free;
594 } else if (err == UBI_IO_BITFLIPS)
595 scrub = 1;
596
597 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
598 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
599
600 crc = be32_to_cpu(vid_hdr->data_crc);
598 ubi_free_vid_hdr(ubi, vid_hdr);
601 ubi_free_vid_buf(vidb);
599 }
600
601 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
602 if (err) {
603 if (err == UBI_IO_BITFLIPS)
604 scrub = 1;
605 else if (mtd_is_eccerr(err)) {
606 if (vol->vol_type == UBI_DYNAMIC_VOLUME)

--- 20 unchanged lines hidden (view full) ---

627
628 if (scrub)
629 err = ubi_wl_scrub_peb(ubi, pnum);
630
631 leb_read_unlock(ubi, vol_id, lnum);
632 return err;
633
634out_free:
602 }
603
604 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
605 if (err) {
606 if (err == UBI_IO_BITFLIPS)
607 scrub = 1;
608 else if (mtd_is_eccerr(err)) {
609 if (vol->vol_type == UBI_DYNAMIC_VOLUME)

--- 20 unchanged lines hidden (view full) ---

630
631 if (scrub)
632 err = ubi_wl_scrub_peb(ubi, pnum);
633
634 leb_read_unlock(ubi, vol_id, lnum);
635 return err;
636
637out_free:
635 ubi_free_vid_hdr(ubi, vid_hdr);
638 ubi_free_vid_buf(vidb);
636out_unlock:
637 leb_read_unlock(ubi, vol_id, lnum);
638 return err;
639}
640
641/**
642 * ubi_eba_read_leb_sg - read data into a scatter gather list.
643 * @ubi: UBI device description object

--- 52 unchanged lines hidden (view full) ---

696/**
697 * try_recover_peb - try to recover from write failure.
698 * @vol: volume description object
699 * @pnum: the physical eraseblock to recover
700 * @lnum: logical eraseblock number
701 * @buf: data which was not written because of the write failure
702 * @offset: offset of the failed write
703 * @len: how many bytes should have been written
639out_unlock:
640 leb_read_unlock(ubi, vol_id, lnum);
641 return err;
642}
643
644/**
645 * ubi_eba_read_leb_sg - read data into a scatter gather list.
646 * @ubi: UBI device description object

--- 52 unchanged lines hidden (view full) ---

699/**
700 * try_recover_peb - try to recover from write failure.
701 * @vol: volume description object
702 * @pnum: the physical eraseblock to recover
703 * @lnum: logical eraseblock number
704 * @buf: data which was not written because of the write failure
705 * @offset: offset of the failed write
706 * @len: how many bytes should have been written
704 * @vid: VID header
707 * @vidb: VID buffer
705 * @retry: whether the caller should retry in case of failure
706 *
707 * This function is called in case of a write failure and moves all good data
708 * from the potentially bad physical eraseblock to a good physical eraseblock.
709 * This function also writes the data which was not written due to the failure.
710 * Returns 0 in case of success, and a negative error code in case of failure.
711 * In case of failure, the %retry parameter is set to false if this is a fatal
712 * error (retrying won't help), and true otherwise.
713 */
714static int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum,
715 const void *buf, int offset, int len,
708 * @retry: whether the caller should retry in case of failure
709 *
710 * This function is called in case of a write failure and moves all good data
711 * from the potentially bad physical eraseblock to a good physical eraseblock.
712 * This function also writes the data which was not written due to the failure.
713 * Returns 0 in case of success, and a negative error code in case of failure.
714 * In case of failure, the %retry parameter is set to false if this is a fatal
715 * error (retrying won't help), and true otherwise.
716 */
717static int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum,
718 const void *buf, int offset, int len,
716 struct ubi_vid_hdr *vid_hdr, bool *retry)
719 struct ubi_vid_io_buf *vidb, bool *retry)
717{
718 struct ubi_device *ubi = vol->ubi;
720{
721 struct ubi_device *ubi = vol->ubi;
722 struct ubi_vid_hdr *vid_hdr;
719 int new_pnum, err, vol_id = vol->vol_id, data_size;
720 uint32_t crc;
721
722 *retry = false;
723
724 new_pnum = ubi_wl_get_peb(ubi);
725 if (new_pnum < 0) {
726 err = new_pnum;
727 goto out_put;
728 }
729
730 ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
731 pnum, new_pnum);
732
723 int new_pnum, err, vol_id = vol->vol_id, data_size;
724 uint32_t crc;
725
726 *retry = false;
727
728 new_pnum = ubi_wl_get_peb(ubi);
729 if (new_pnum < 0) {
730 err = new_pnum;
731 goto out_put;
732 }
733
734 ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
735 pnum, new_pnum);
736
733 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
737 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
734 if (err && err != UBI_IO_BITFLIPS) {
735 if (err > 0)
736 err = -EIO;
737 goto out_put;
738 }
739
740 ubi_assert(vid_hdr->vol_type == UBI_VID_DYNAMIC);
741

--- 12 unchanged lines hidden (view full) ---

754 memcpy(ubi->peb_buf + offset, buf, len);
755
756 data_size = offset + len;
757 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
758 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
759 vid_hdr->copy_flag = 1;
760 vid_hdr->data_size = cpu_to_be32(data_size);
761 vid_hdr->data_crc = cpu_to_be32(crc);
738 if (err && err != UBI_IO_BITFLIPS) {
739 if (err > 0)
740 err = -EIO;
741 goto out_put;
742 }
743
744 ubi_assert(vid_hdr->vol_type == UBI_VID_DYNAMIC);
745

--- 12 unchanged lines hidden (view full) ---

758 memcpy(ubi->peb_buf + offset, buf, len);
759
760 data_size = offset + len;
761 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
762 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
763 vid_hdr->copy_flag = 1;
764 vid_hdr->data_size = cpu_to_be32(data_size);
765 vid_hdr->data_crc = cpu_to_be32(crc);
762 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
766 err = ubi_io_write_vid_hdr(ubi, new_pnum, vidb);
763 if (err)
764 goto out_unlock;
765
766 err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
767
768out_unlock:
769 mutex_unlock(&ubi->buf_mutex);
770

--- 34 unchanged lines hidden (view full) ---

805 * Returns 0 in case of success, and a negative error code in case of failure.
806 * This function tries %UBI_IO_RETRIES before giving up.
807 */
808static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
809 const void *buf, int offset, int len)
810{
811 int err, idx = vol_id2idx(ubi, vol_id), tries;
812 struct ubi_volume *vol = ubi->volumes[idx];
767 if (err)
768 goto out_unlock;
769
770 err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
771
772out_unlock:
773 mutex_unlock(&ubi->buf_mutex);
774

--- 34 unchanged lines hidden (view full) ---

809 * Returns 0 in case of success, and a negative error code in case of failure.
810 * This function tries %UBI_IO_RETRIES before giving up.
811 */
812static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
813 const void *buf, int offset, int len)
814{
815 int err, idx = vol_id2idx(ubi, vol_id), tries;
816 struct ubi_volume *vol = ubi->volumes[idx];
813 struct ubi_vid_hdr *vid_hdr;
817 struct ubi_vid_io_buf *vidb;
814
818
815 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
816 if (!vid_hdr)
819 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
820 if (!vidb)
817 return -ENOMEM;
818
819 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
820 bool retry;
821
821 return -ENOMEM;
822
823 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
824 bool retry;
825
822 err = try_recover_peb(vol, pnum, lnum, buf, offset, len,
823 vid_hdr, &retry);
826 err = try_recover_peb(vol, pnum, lnum, buf, offset, len, vidb,
827 &retry);
824 if (!err || !retry)
825 break;
826
827 ubi_msg(ubi, "try again");
828 }
829
828 if (!err || !retry)
829 break;
830
831 ubi_msg(ubi, "try again");
832 }
833
830 ubi_free_vid_hdr(ubi, vid_hdr);
834 ubi_free_vid_buf(vidb);
831
832 return err;
833}
834
835/**
836 * try_write_vid_and_data - try to write VID header and data to a new PEB.
837 * @vol: volume description object
838 * @lnum: logical eraseblock number
835
836 return err;
837}
838
839/**
840 * try_write_vid_and_data - try to write VID header and data to a new PEB.
841 * @vol: volume description object
842 * @lnum: logical eraseblock number
839 * @vid_hdr: VID header to write
843 * @vidb: the VID buffer to write
840 * @buf: buffer containing the data
841 * @offset: where to start writing data
842 * @len: how many bytes should be written
843 *
844 * This function tries to write VID header and data belonging to logical
845 * eraseblock @lnum of volume @vol to a new physical eraseblock. Returns zero
846 * in case of success and a negative error code in case of failure.
847 * In case of error, it is possible that something was still written to the
848 * flash media, but may be some garbage.
849 */
850static int try_write_vid_and_data(struct ubi_volume *vol, int lnum,
844 * @buf: buffer containing the data
845 * @offset: where to start writing data
846 * @len: how many bytes should be written
847 *
848 * This function tries to write VID header and data belonging to logical
849 * eraseblock @lnum of volume @vol to a new physical eraseblock. Returns zero
850 * in case of success and a negative error code in case of failure.
851 * In case of error, it is possible that something was still written to the
852 * flash media, but may be some garbage.
853 */
854static int try_write_vid_and_data(struct ubi_volume *vol, int lnum,
851 struct ubi_vid_hdr *vid_hdr, const void *buf,
855 struct ubi_vid_io_buf *vidb, const void *buf,
852 int offset, int len)
853{
854 struct ubi_device *ubi = vol->ubi;
855 int pnum, opnum, err, vol_id = vol->vol_id;
856
857 pnum = ubi_wl_get_peb(ubi);
858 if (pnum < 0) {
859 err = pnum;
860 goto out_put;
861 }
862
863 opnum = vol->eba_tbl->entries[lnum].pnum;
864
865 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
866 len, offset, vol_id, lnum, pnum);
867
856 int offset, int len)
857{
858 struct ubi_device *ubi = vol->ubi;
859 int pnum, opnum, err, vol_id = vol->vol_id;
860
861 pnum = ubi_wl_get_peb(ubi);
862 if (pnum < 0) {
863 err = pnum;
864 goto out_put;
865 }
866
867 opnum = vol->eba_tbl->entries[lnum].pnum;
868
869 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
870 len, offset, vol_id, lnum, pnum);
871
868 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
872 err = ubi_io_write_vid_hdr(ubi, pnum, vidb);
869 if (err) {
870 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
871 vol_id, lnum, pnum);
872 goto out_put;
873 }
874
875 if (len) {
876 err = ubi_io_write_data(ubi, buf, pnum, offset, len);

--- 32 unchanged lines hidden (view full) ---

909 * of failure. In case of error, it is possible that something was still
910 * written to the flash media, but may be some garbage.
911 * This function retries %UBI_IO_RETRIES times before giving up.
912 */
913int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
914 const void *buf, int offset, int len)
915{
916 int err, pnum, tries, vol_id = vol->vol_id;
873 if (err) {
874 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
875 vol_id, lnum, pnum);
876 goto out_put;
877 }
878
879 if (len) {
880 err = ubi_io_write_data(ubi, buf, pnum, offset, len);

--- 32 unchanged lines hidden (view full) ---

913 * of failure. In case of error, it is possible that something was still
914 * written to the flash media, but may be some garbage.
915 * This function retries %UBI_IO_RETRIES times before giving up.
916 */
917int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
918 const void *buf, int offset, int len)
919{
920 int err, pnum, tries, vol_id = vol->vol_id;
921 struct ubi_vid_io_buf *vidb;
917 struct ubi_vid_hdr *vid_hdr;
918
919 if (ubi->ro_mode)
920 return -EROFS;
921
922 err = leb_write_lock(ubi, vol_id, lnum);
923 if (err)
924 return err;

--- 13 unchanged lines hidden (view full) ---

938
939 goto out;
940 }
941
942 /*
943 * The logical eraseblock is not mapped. We have to get a free physical
944 * eraseblock and write the volume identifier header there first.
945 */
922 struct ubi_vid_hdr *vid_hdr;
923
924 if (ubi->ro_mode)
925 return -EROFS;
926
927 err = leb_write_lock(ubi, vol_id, lnum);
928 if (err)
929 return err;

--- 13 unchanged lines hidden (view full) ---

943
944 goto out;
945 }
946
947 /*
948 * The logical eraseblock is not mapped. We have to get a free physical
949 * eraseblock and write the volume identifier header there first.
950 */
946 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
947 if (!vid_hdr) {
951 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
952 if (!vidb) {
948 leb_write_unlock(ubi, vol_id, lnum);
949 return -ENOMEM;
950 }
951
953 leb_write_unlock(ubi, vol_id, lnum);
954 return -ENOMEM;
955 }
956
957 vid_hdr = ubi_get_vid_hdr(vidb);
958
952 vid_hdr->vol_type = UBI_VID_DYNAMIC;
953 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
954 vid_hdr->vol_id = cpu_to_be32(vol_id);
955 vid_hdr->lnum = cpu_to_be32(lnum);
956 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
957 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
958
959 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
959 vid_hdr->vol_type = UBI_VID_DYNAMIC;
960 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
961 vid_hdr->vol_id = cpu_to_be32(vol_id);
962 vid_hdr->lnum = cpu_to_be32(lnum);
963 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
964 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
965
966 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
960 err = try_write_vid_and_data(vol, lnum, vid_hdr, buf, offset,
961 len);
967 err = try_write_vid_and_data(vol, lnum, vidb, buf, offset, len);
962 if (err != -EIO || !ubi->bad_allowed)
963 break;
964
965 /*
966 * Fortunately, this is the first write operation to this
967 * physical eraseblock, so just put it and request a new one.
968 * We assume that if this physical eraseblock went bad, the
969 * erase code will handle that.
970 */
971 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
972 ubi_msg(ubi, "try another PEB");
973 }
974
968 if (err != -EIO || !ubi->bad_allowed)
969 break;
970
971 /*
972 * Fortunately, this is the first write operation to this
973 * physical eraseblock, so just put it and request a new one.
974 * We assume that if this physical eraseblock went bad, the
975 * erase code will handle that.
976 */
977 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
978 ubi_msg(ubi, "try another PEB");
979 }
980
975 ubi_free_vid_hdr(ubi, vid_hdr);
981 ubi_free_vid_buf(vidb);
976
977out:
978 if (err)
979 ubi_ro_mode(ubi);
980
981 leb_write_unlock(ubi, vol_id, lnum);
982
983 return err;

--- 20 unchanged lines hidden (view full) ---

1004 * It is prohibited to write more than once to logical eraseblocks of static
1005 * volumes. This function returns zero in case of success and a negative error
1006 * code in case of failure.
1007 */
1008int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
1009 int lnum, const void *buf, int len, int used_ebs)
1010{
1011 int err, tries, data_size = len, vol_id = vol->vol_id;
982
983out:
984 if (err)
985 ubi_ro_mode(ubi);
986
987 leb_write_unlock(ubi, vol_id, lnum);
988
989 return err;

--- 20 unchanged lines hidden (view full) ---

1010 * It is prohibited to write more than once to logical eraseblocks of static
1011 * volumes. This function returns zero in case of success and a negative error
1012 * code in case of failure.
1013 */
1014int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
1015 int lnum, const void *buf, int len, int used_ebs)
1016{
1017 int err, tries, data_size = len, vol_id = vol->vol_id;
1018 struct ubi_vid_io_buf *vidb;
1012 struct ubi_vid_hdr *vid_hdr;
1013 uint32_t crc;
1014
1015 if (ubi->ro_mode)
1016 return -EROFS;
1017
1018 if (lnum == used_ebs - 1)
1019 /* If this is the last LEB @len may be unaligned */
1020 len = ALIGN(data_size, ubi->min_io_size);
1021 else
1022 ubi_assert(!(len & (ubi->min_io_size - 1)));
1023
1019 struct ubi_vid_hdr *vid_hdr;
1020 uint32_t crc;
1021
1022 if (ubi->ro_mode)
1023 return -EROFS;
1024
1025 if (lnum == used_ebs - 1)
1026 /* If this is the last LEB @len may be unaligned */
1027 len = ALIGN(data_size, ubi->min_io_size);
1028 else
1029 ubi_assert(!(len & (ubi->min_io_size - 1)));
1030
1024 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1025 if (!vid_hdr)
1031 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
1032 if (!vidb)
1026 return -ENOMEM;
1027
1033 return -ENOMEM;
1034
1035 vid_hdr = ubi_get_vid_hdr(vidb);
1036
1028 err = leb_write_lock(ubi, vol_id, lnum);
1029 if (err)
1030 goto out;
1031
1032 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1033 vid_hdr->vol_id = cpu_to_be32(vol_id);
1034 vid_hdr->lnum = cpu_to_be32(lnum);
1035 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
1036 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
1037
1038 crc = crc32(UBI_CRC32_INIT, buf, data_size);
1039 vid_hdr->vol_type = UBI_VID_STATIC;
1040 vid_hdr->data_size = cpu_to_be32(data_size);
1041 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
1042 vid_hdr->data_crc = cpu_to_be32(crc);
1043
1044 ubi_assert(vol->eba_tbl->entries[lnum].pnum < 0);
1045
1046 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
1037 err = leb_write_lock(ubi, vol_id, lnum);
1038 if (err)
1039 goto out;
1040
1041 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1042 vid_hdr->vol_id = cpu_to_be32(vol_id);
1043 vid_hdr->lnum = cpu_to_be32(lnum);
1044 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
1045 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
1046
1047 crc = crc32(UBI_CRC32_INIT, buf, data_size);
1048 vid_hdr->vol_type = UBI_VID_STATIC;
1049 vid_hdr->data_size = cpu_to_be32(data_size);
1050 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
1051 vid_hdr->data_crc = cpu_to_be32(crc);
1052
1053 ubi_assert(vol->eba_tbl->entries[lnum].pnum < 0);
1054
1055 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
1047 err = try_write_vid_and_data(vol, lnum, vid_hdr, buf, 0, len);
1056 err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
1048 if (err != -EIO || !ubi->bad_allowed)
1049 break;
1050
1051 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1052 ubi_msg(ubi, "try another PEB");
1053 }
1054
1055 if (err)
1056 ubi_ro_mode(ubi);
1057
1058 leb_write_unlock(ubi, vol_id, lnum);
1059
1060out:
1057 if (err != -EIO || !ubi->bad_allowed)
1058 break;
1059
1060 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1061 ubi_msg(ubi, "try another PEB");
1062 }
1063
1064 if (err)
1065 ubi_ro_mode(ubi);
1066
1067 leb_write_unlock(ubi, vol_id, lnum);
1068
1069out:
1061 ubi_free_vid_hdr(ubi, vid_hdr);
1070 ubi_free_vid_buf(vidb);
1062
1063 return err;
1064}
1065
1066/*
1067 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
1068 * @ubi: UBI device description object
1069 * @vol: volume description object

--- 9 unchanged lines hidden (view full) ---

1079 *
1080 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
1081 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
1082 */
1083int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
1084 int lnum, const void *buf, int len)
1085{
1086 int err, tries, vol_id = vol->vol_id;
1071
1072 return err;
1073}
1074
1075/*
1076 * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
1077 * @ubi: UBI device description object
1078 * @vol: volume description object

--- 9 unchanged lines hidden (view full) ---

1088 *
1089 * UBI reserves one LEB for the "atomic LEB change" operation, so only one
1090 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
1091 */
1092int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
1093 int lnum, const void *buf, int len)
1094{
1095 int err, tries, vol_id = vol->vol_id;
1096 struct ubi_vid_io_buf *vidb;
1087 struct ubi_vid_hdr *vid_hdr;
1088 uint32_t crc;
1089
1090 if (ubi->ro_mode)
1091 return -EROFS;
1092
1093 if (len == 0) {
1094 /*
1095 * Special case when data length is zero. In this case the LEB
1096 * has to be unmapped and mapped somewhere else.
1097 */
1098 err = ubi_eba_unmap_leb(ubi, vol, lnum);
1099 if (err)
1100 return err;
1101 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
1102 }
1103
1097 struct ubi_vid_hdr *vid_hdr;
1098 uint32_t crc;
1099
1100 if (ubi->ro_mode)
1101 return -EROFS;
1102
1103 if (len == 0) {
1104 /*
1105 * Special case when data length is zero. In this case the LEB
1106 * has to be unmapped and mapped somewhere else.
1107 */
1108 err = ubi_eba_unmap_leb(ubi, vol, lnum);
1109 if (err)
1110 return err;
1111 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
1112 }
1113
1104 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1105 if (!vid_hdr)
1114 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
1115 if (!vidb)
1106 return -ENOMEM;
1107
1116 return -ENOMEM;
1117
1118 vid_hdr = ubi_get_vid_hdr(vidb);
1119
1108 mutex_lock(&ubi->alc_mutex);
1109 err = leb_write_lock(ubi, vol_id, lnum);
1110 if (err)
1111 goto out_mutex;
1112
1113 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1114 vid_hdr->vol_id = cpu_to_be32(vol_id);
1115 vid_hdr->lnum = cpu_to_be32(lnum);

--- 4 unchanged lines hidden (view full) ---

1120 vid_hdr->vol_type = UBI_VID_DYNAMIC;
1121 vid_hdr->data_size = cpu_to_be32(len);
1122 vid_hdr->copy_flag = 1;
1123 vid_hdr->data_crc = cpu_to_be32(crc);
1124
1125 dbg_eba("change LEB %d:%d", vol_id, lnum);
1126
1127 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
1120 mutex_lock(&ubi->alc_mutex);
1121 err = leb_write_lock(ubi, vol_id, lnum);
1122 if (err)
1123 goto out_mutex;
1124
1125 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1126 vid_hdr->vol_id = cpu_to_be32(vol_id);
1127 vid_hdr->lnum = cpu_to_be32(lnum);

--- 4 unchanged lines hidden (view full) ---

1132 vid_hdr->vol_type = UBI_VID_DYNAMIC;
1133 vid_hdr->data_size = cpu_to_be32(len);
1134 vid_hdr->copy_flag = 1;
1135 vid_hdr->data_crc = cpu_to_be32(crc);
1136
1137 dbg_eba("change LEB %d:%d", vol_id, lnum);
1138
1139 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
1128 err = try_write_vid_and_data(vol, lnum, vid_hdr, buf, 0, len);
1140 err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
1129 if (err != -EIO || !ubi->bad_allowed)
1130 break;
1131
1132 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1133 ubi_msg(ubi, "try another PEB");
1134 }
1135
1136 /*
1137 * This flash device does not admit of bad eraseblocks or
1138 * something nasty and unexpected happened. Switch to read-only
1139 * mode just in case.
1140 */
1141 if (err)
1142 ubi_ro_mode(ubi);
1143
1144 leb_write_unlock(ubi, vol_id, lnum);
1145
1146out_mutex:
1147 mutex_unlock(&ubi->alc_mutex);
1141 if (err != -EIO || !ubi->bad_allowed)
1142 break;
1143
1144 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1145 ubi_msg(ubi, "try another PEB");
1146 }
1147
1148 /*
1149 * This flash device does not admit of bad eraseblocks or
1150 * something nasty and unexpected happened. Switch to read-only
1151 * mode just in case.
1152 */
1153 if (err)
1154 ubi_ro_mode(ubi);
1155
1156 leb_write_unlock(ubi, vol_id, lnum);
1157
1158out_mutex:
1159 mutex_unlock(&ubi->alc_mutex);
1148 ubi_free_vid_hdr(ubi, vid_hdr);
1160 ubi_free_vid_buf(vidb);
1149 return err;
1150}
1151
1152/**
1153 * is_error_sane - check whether a read error is sane.
1154 * @err: code of the error happened during reading
1155 *
1156 * This is a helper function for 'ubi_eba_copy_leb()' which is called when we

--- 29 unchanged lines hidden (view full) ---

1186 * This function copies logical eraseblock from physical eraseblock @from to
1187 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
1188 * function. Returns:
1189 * o %0 in case of success;
1190 * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
1191 * o a negative error code in case of failure.
1192 */
1193int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1161 return err;
1162}
1163
1164/**
1165 * is_error_sane - check whether a read error is sane.
1166 * @err: code of the error happened during reading
1167 *
1168 * This is a helper function for 'ubi_eba_copy_leb()' which is called when we

--- 29 unchanged lines hidden (view full) ---

1198 * This function copies logical eraseblock from physical eraseblock @from to
1199 * physical eraseblock @to. The @vid_hdr buffer may be changed by this
1200 * function. Returns:
1201 * o %0 in case of success;
1202 * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
1203 * o a negative error code in case of failure.
1204 */
1205int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1194 struct ubi_vid_hdr *vid_hdr)
1206 struct ubi_vid_io_buf *vidb)
1195{
1196 int err, vol_id, lnum, data_size, aldata_size, idx;
1207{
1208 int err, vol_id, lnum, data_size, aldata_size, idx;
1209 struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb);
1197 struct ubi_volume *vol;
1198 uint32_t crc;
1199
1200 vol_id = be32_to_cpu(vid_hdr->vol_id);
1201 lnum = be32_to_cpu(vid_hdr->lnum);
1202
1203 dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
1204

--- 95 unchanged lines hidden (view full) ---

1300 */
1301 if (data_size > 0) {
1302 vid_hdr->copy_flag = 1;
1303 vid_hdr->data_size = cpu_to_be32(data_size);
1304 vid_hdr->data_crc = cpu_to_be32(crc);
1305 }
1306 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1307
1210 struct ubi_volume *vol;
1211 uint32_t crc;
1212
1213 vol_id = be32_to_cpu(vid_hdr->vol_id);
1214 lnum = be32_to_cpu(vid_hdr->lnum);
1215
1216 dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
1217

--- 95 unchanged lines hidden (view full) ---

1313 */
1314 if (data_size > 0) {
1315 vid_hdr->copy_flag = 1;
1316 vid_hdr->data_size = cpu_to_be32(data_size);
1317 vid_hdr->data_crc = cpu_to_be32(crc);
1318 }
1319 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1320
1308 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1321 err = ubi_io_write_vid_hdr(ubi, to, vidb);
1309 if (err) {
1310 if (err == -EIO)
1311 err = MOVE_TARGET_WR_ERR;
1312 goto out_unlock_buf;
1313 }
1314
1315 cond_resched();
1316
1317 /* Read the VID header back and check if it was written correctly */
1322 if (err) {
1323 if (err == -EIO)
1324 err = MOVE_TARGET_WR_ERR;
1325 goto out_unlock_buf;
1326 }
1327
1328 cond_resched();
1329
1330 /* Read the VID header back and check if it was written correctly */
1318 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1331 err = ubi_io_read_vid_hdr(ubi, to, vidb, 1);
1319 if (err) {
1320 if (err != UBI_IO_BITFLIPS) {
1321 ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
1322 err, to);
1323 if (is_error_sane(err))
1324 err = MOVE_TARGET_RD_ERR;
1325 } else
1326 err = MOVE_TARGET_BITFLIPS;

--- 262 unchanged lines hidden ---
1332 if (err) {
1333 if (err != UBI_IO_BITFLIPS) {
1334 ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
1335 err, to);
1336 if (is_error_sane(err))
1337 err = MOVE_TARGET_RD_ERR;
1338 } else
1339 err = MOVE_TARGET_BITFLIPS;

--- 262 unchanged lines hidden ---