Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / mtd / ubi / eba.c
index 22b3636..9ca92fa 100644 (file)
@@ -341,7 +341,7 @@ int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
        dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
 
        vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
-       err = ubi_wl_put_peb(ubi, pnum, 0);
+       err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
 
 out_unlock:
        leb_write_unlock(ubi, vol_id, lnum);
@@ -420,9 +420,8 @@ retry:
                                 */
                                if (err == UBI_IO_BAD_HDR_EBADMSG ||
                                    err == UBI_IO_BAD_HDR) {
-                                       ubi_warn("corrupted VID header at PEB "
-                                                "%d, LEB %d:%d", pnum, vol_id,
-                                                lnum);
+                                       ubi_warn("corrupted VID header at PEB %d, LEB %d:%d",
+                                                pnum, vol_id, lnum);
                                        err = -EBADMSG;
                                } else
                                        ubi_ro_mode(ubi);
@@ -507,7 +506,7 @@ static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
                return -ENOMEM;
 
 retry:
-       new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
+       new_pnum = ubi_wl_get_peb(ubi);
        if (new_pnum < 0) {
                ubi_free_vid_hdr(ubi, vid_hdr);
                return new_pnum;
@@ -529,18 +528,18 @@ retry:
 
        data_size = offset + len;
        mutex_lock(&ubi->buf_mutex);
-       memset(ubi->peb_buf1 + offset, 0xFF, len);
+       memset(ubi->peb_buf + offset, 0xFF, len);
 
        /* Read everything before the area where the write failure happened */
        if (offset > 0) {
-               err = ubi_io_read_data(ubi, ubi->peb_buf1, pnum, 0, offset);
+               err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
                if (err && err != UBI_IO_BITFLIPS)
                        goto out_unlock;
        }
 
-       memcpy(ubi->peb_buf1 + offset, buf, len);
+       memcpy(ubi->peb_buf + offset, buf, len);
 
-       err = ubi_io_write_data(ubi, ubi->peb_buf1, new_pnum, 0, data_size);
+       err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
        if (err) {
                mutex_unlock(&ubi->buf_mutex);
                goto write_error;
@@ -550,7 +549,7 @@ retry:
        ubi_free_vid_hdr(ubi, vid_hdr);
 
        vol->eba_tbl[lnum] = new_pnum;
-       ubi_wl_put_peb(ubi, pnum, 1);
+       ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
 
        ubi_msg("data was successfully recovered");
        return 0;
@@ -558,7 +557,7 @@ retry:
 out_unlock:
        mutex_unlock(&ubi->buf_mutex);
 out_put:
-       ubi_wl_put_peb(ubi, new_pnum, 1);
+       ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
        ubi_free_vid_hdr(ubi, vid_hdr);
        return err;
 
@@ -568,7 +567,7 @@ write_error:
         * get another one.
         */
        ubi_warn("failed to write to PEB %d", new_pnum);
-       ubi_wl_put_peb(ubi, new_pnum, 1);
+       ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
        if (++tries > UBI_IO_RETRIES) {
                ubi_free_vid_hdr(ubi, vid_hdr);
                return err;
@@ -585,7 +584,6 @@ write_error:
  * @buf: the data to write
  * @offset: offset within the logical eraseblock where to write
  * @len: how many bytes to write
- * @dtype: data type
  *
  * This function writes data to logical eraseblock @lnum of a dynamic volume
  * @vol. Returns zero in case of success and a negative error code in case
@@ -593,7 +591,7 @@ write_error:
  * written to the flash media, but may be some garbage.
  */
 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
-                     const void *buf, int offset, int len, int dtype)
+                     const void *buf, int offset, int len)
 {
        int err, pnum, tries = 0, vol_id = vol->vol_id;
        struct ubi_vid_hdr *vid_hdr;
@@ -641,7 +639,7 @@ int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
        vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
 
 retry:
-       pnum = ubi_wl_get_peb(ubi, dtype);
+       pnum = ubi_wl_get_peb(ubi);
        if (pnum < 0) {
                ubi_free_vid_hdr(ubi, vid_hdr);
                leb_write_unlock(ubi, vol_id, lnum);
@@ -661,9 +659,8 @@ retry:
        if (len) {
                err = ubi_io_write_data(ubi, buf, pnum, offset, len);
                if (err) {
-                       ubi_warn("failed to write %d bytes at offset %d of "
-                                "LEB %d:%d, PEB %d", len, offset, vol_id,
-                                lnum, pnum);
+                       ubi_warn("failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
+                                len, offset, vol_id, lnum, pnum);
                        goto write_error;
                }
        }
@@ -687,7 +684,7 @@ write_error:
         * eraseblock, so just put it and request a new one. We assume that if
         * this physical eraseblock went bad, the erase code will handle that.
         */
-       err = ubi_wl_put_peb(ubi, pnum, 1);
+       err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
        if (err || ++tries > UBI_IO_RETRIES) {
                ubi_ro_mode(ubi);
                leb_write_unlock(ubi, vol_id, lnum);
@@ -707,7 +704,6 @@ write_error:
  * @lnum: logical eraseblock number
  * @buf: data to write
  * @len: how many bytes to write
- * @dtype: data type
  * @used_ebs: how many logical eraseblocks will this volume contain
  *
  * This function writes data to logical eraseblock @lnum of static volume
@@ -724,8 +720,7 @@ write_error:
  * code in case of failure.
  */
 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
-                        int lnum, const void *buf, int len, int dtype,
-                        int used_ebs)
+                        int lnum, const void *buf, int len, int used_ebs)
 {
        int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
        struct ubi_vid_hdr *vid_hdr;
@@ -763,7 +758,7 @@ int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
        vid_hdr->data_crc = cpu_to_be32(crc);
 
 retry:
-       pnum = ubi_wl_get_peb(ubi, dtype);
+       pnum = ubi_wl_get_peb(ubi);
        if (pnum < 0) {
                ubi_free_vid_hdr(ubi, vid_hdr);
                leb_write_unlock(ubi, vol_id, lnum);
@@ -807,7 +802,7 @@ write_error:
                return err;
        }
 
-       err = ubi_wl_put_peb(ubi, pnum, 1);
+       err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
        if (err || ++tries > UBI_IO_RETRIES) {
                ubi_ro_mode(ubi);
                leb_write_unlock(ubi, vol_id, lnum);
@@ -827,7 +822,6 @@ write_error:
  * @lnum: logical eraseblock number
  * @buf: data to write
  * @len: how many bytes to write
- * @dtype: data type
  *
  * This function changes the contents of a logical eraseblock atomically. @buf
  * has to contain new logical eraseblock data, and @len - the length of the
@@ -839,7 +833,7 @@ write_error:
  * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
  */
 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
-                             int lnum, const void *buf, int len, int dtype)
+                             int lnum, const void *buf, int len)
 {
        int err, pnum, tries = 0, vol_id = vol->vol_id;
        struct ubi_vid_hdr *vid_hdr;
@@ -856,7 +850,7 @@ int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
                err = ubi_eba_unmap_leb(ubi, vol, lnum);
                if (err)
                        return err;
-               return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
+               return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
        }
 
        vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
@@ -881,7 +875,7 @@ int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
        vid_hdr->data_crc = cpu_to_be32(crc);
 
 retry:
-       pnum = ubi_wl_get_peb(ubi, dtype);
+       pnum = ubi_wl_get_peb(ubi);
        if (pnum < 0) {
                err = pnum;
                goto out_leb_unlock;
@@ -905,7 +899,7 @@ retry:
        }
 
        if (vol->eba_tbl[lnum] >= 0) {
-               err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 0);
+               err = ubi_wl_put_peb(ubi, vol_id, lnum, vol->eba_tbl[lnum], 0);
                if (err)
                        goto out_leb_unlock;
        }
@@ -930,7 +924,7 @@ write_error:
                goto out_leb_unlock;
        }
 
-       err = ubi_wl_put_peb(ubi, pnum, 1);
+       err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
        if (err || ++tries > UBI_IO_RETRIES) {
                ubi_ro_mode(ubi);
                goto out_leb_unlock;
@@ -979,7 +973,7 @@ static int is_error_sane(int err)
  * physical eraseblock @to. The @vid_hdr buffer may be changed by this
  * function. Returns:
  *   o %0 in case of success;
- *   o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_CANCEL_BITFLIPS, etc;
+ *   o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
  *   o a negative error code in case of failure.
  */
 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
@@ -1044,22 +1038,21 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
         * cancel it.
         */
        if (vol->eba_tbl[lnum] != from) {
-               dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to "
-                      "PEB %d, cancel", vol_id, lnum, from,
-                      vol->eba_tbl[lnum]);
+               dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
+                      vol_id, lnum, from, vol->eba_tbl[lnum]);
                err = MOVE_CANCEL_RACE;
                goto out_unlock_leb;
        }
 
        /*
         * OK, now the LEB is locked and we can safely start moving it. Since
-        * this function utilizes the @ubi->peb_buf1 buffer which is shared
+        * this function utilizes the @ubi->peb_buf buffer which is shared
         * with some other functions - we lock the buffer by taking the
         * @ubi->buf_mutex.
         */
        mutex_lock(&ubi->buf_mutex);
        dbg_wl("read %d bytes of data", aldata_size);
-       err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
+       err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
        if (err && err != UBI_IO_BITFLIPS) {
                ubi_warn("error %d while reading data from PEB %d",
                         err, from);
@@ -1079,10 +1072,10 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
         */
        if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
                aldata_size = data_size =
-                       ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
+                       ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
 
        cond_resched();
-       crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
+       crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
        cond_resched();
 
        /*
@@ -1111,17 +1104,17 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
        err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
        if (err) {
                if (err != UBI_IO_BITFLIPS) {
-                       ubi_warn("error %d while reading VID header back from "
-                                 "PEB %d", err, to);
+                       ubi_warn("error %d while reading VID header back from PEB %d",
+                                err, to);
                        if (is_error_sane(err))
                                err = MOVE_TARGET_RD_ERR;
                } else
-                       err = MOVE_CANCEL_BITFLIPS;
+                       err = MOVE_TARGET_BITFLIPS;
                goto out_unlock_buf;
        }
 
        if (data_size > 0) {
-               err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
+               err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
                if (err) {
                        if (err == -EIO)
                                err = MOVE_TARGET_WR_ERR;
@@ -1134,24 +1127,24 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
                 * We've written the data and are going to read it back to make
                 * sure it was written correctly.
                 */
-
-               err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
+               memset(ubi->peb_buf, 0xFF, aldata_size);
+               err = ubi_io_read_data(ubi, ubi->peb_buf, to, 0, aldata_size);
                if (err) {
                        if (err != UBI_IO_BITFLIPS) {
-                               ubi_warn("error %d while reading data back "
-                                        "from PEB %d", err, to);
+                               ubi_warn("error %d while reading data back from PEB %d",
+                                        err, to);
                                if (is_error_sane(err))
                                        err = MOVE_TARGET_RD_ERR;
                        } else
-                               err = MOVE_CANCEL_BITFLIPS;
+                               err = MOVE_TARGET_BITFLIPS;
                        goto out_unlock_buf;
                }
 
                cond_resched();
 
-               if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
-                       ubi_warn("read data back from PEB %d and it is "
-                                "different", to);
+               if (crc != crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size)) {
+                       ubi_warn("read data back from PEB %d and it is different",
+                                to);
                        err = -EINVAL;
                        goto out_unlock_buf;
                }
@@ -1171,7 +1164,7 @@ out_unlock_leb:
  * print_rsvd_warning - warn about not having enough reserved PEBs.
  * @ubi: UBI device description object
  *
- * This is a helper function for 'ubi_eba_init_scan()' which is called when UBI
+ * This is a helper function for 'ubi_eba_init()' which is called when UBI
  * cannot reserve enough PEBs for bad block handling. This function makes a
  * decision whether we have to print a warning or not. The algorithm is as
  * follows:
@@ -1186,13 +1179,13 @@ out_unlock_leb:
  * reported by real users.
  */
 static void print_rsvd_warning(struct ubi_device *ubi,
-                              struct ubi_scan_info *si)
+                              struct ubi_attach_info *ai)
 {
        /*
         * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
         * large number to distinguish between newly flashed and used images.
         */
-       if (si->max_sqnum > (1 << 18)) {
+       if (ai->max_sqnum > (1 << 18)) {
                int min = ubi->beb_rsvd_level / 10;
 
                if (!min)
@@ -1201,27 +1194,27 @@ static void print_rsvd_warning(struct ubi_device *ubi,
                        return;
        }
 
-       ubi_warn("cannot reserve enough PEBs for bad PEB handling, reserved %d,"
-                " need %d", ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
+       ubi_warn("cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
+                ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
        if (ubi->corr_peb_count)
                ubi_warn("%d PEBs are corrupted and not used",
-                       ubi->corr_peb_count);
+                        ubi->corr_peb_count);
 }
 
 /**
- * ubi_eba_init_scan - initialize the EBA sub-system using scanning information.
+ * ubi_eba_init - initialize the EBA sub-system using attaching information.
  * @ubi: UBI device description object
- * @si: scanning information
+ * @ai: attaching information
  *
  * This function returns zero in case of success and a negative error code in
  * case of failure.
  */
-int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
+int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
 {
        int i, j, err, num_volumes;
-       struct ubi_scan_volume *sv;
+       struct ubi_ainf_volume *av;
        struct ubi_volume *vol;
-       struct ubi_scan_leb *seb;
+       struct ubi_ainf_peb *aeb;
        struct rb_node *rb;
 
        dbg_eba("initialize EBA sub-system");
@@ -1230,7 +1223,7 @@ int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
        mutex_init(&ubi->alc_mutex);
        ubi->ltree = RB_ROOT;
 
-       ubi->global_sqnum = si->max_sqnum + 1;
+       ubi->global_sqnum = ai->max_sqnum + 1;
        num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
 
        for (i = 0; i < num_volumes; i++) {
@@ -1250,19 +1243,19 @@ int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
                for (j = 0; j < vol->reserved_pebs; j++)
                        vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
 
-               sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
-               if (!sv)
+               av = ubi_find_av(ai, idx2vol_id(ubi, i));
+               if (!av)
                        continue;
 
-               ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
-                       if (seb->lnum >= vol->reserved_pebs)
+               ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
+                       if (aeb->lnum >= vol->reserved_pebs)
                                /*
                                 * This may happen in case of an unclean reboot
                                 * during re-size.
                                 */
-                               ubi_scan_move_to_list(sv, seb, &si->erase);
+                               ubi_move_aeb_to_list(av, aeb, &ai->erase);
                        else
-                               vol->eba_tbl[seb->lnum] = seb->pnum;
+                               vol->eba_tbl[aeb->lnum] = aeb->pnum;
                }
        }
 
@@ -1284,7 +1277,7 @@ int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
                if (ubi->avail_pebs < ubi->beb_rsvd_level) {
                        /* No enough free physical eraseblocks */
                        ubi->beb_rsvd_pebs = ubi->avail_pebs;
-                       print_rsvd_warning(ubi, si);
+                       print_rsvd_warning(ubi, ai);
                } else
                        ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;