new_pnum = ubi_wl_get_peb(ubi);
        if (new_pnum < 0) {
                ubi_free_vid_hdr(ubi, vid_hdr);
+               up_read(&ubi->fm_sem);
                return new_pnum;
        }
 
        if (err && err != UBI_IO_BITFLIPS) {
                if (err > 0)
                        err = -EIO;
+               up_read(&ubi->fm_sem);
                goto out_put;
        }
 
        vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
        err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
-       if (err)
+       if (err) {
+               up_read(&ubi->fm_sem);
                goto write_error;
+       }
 
        data_size = offset + len;
        mutex_lock(&ubi->buf_mutex);
        /* Read everything before the area where the write failure happened */
        if (offset > 0) {
                err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
-               if (err && err != UBI_IO_BITFLIPS)
+               if (err && err != UBI_IO_BITFLIPS) {
+                       up_read(&ubi->fm_sem);
                        goto out_unlock;
+               }
        }
 
        memcpy(ubi->peb_buf + offset, buf, len);
        err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
        if (err) {
                mutex_unlock(&ubi->buf_mutex);
+               up_read(&ubi->fm_sem);
                goto write_error;
        }
 
        mutex_unlock(&ubi->buf_mutex);
        ubi_free_vid_hdr(ubi, vid_hdr);
 
-       down_read(&ubi->fm_sem);
        vol->eba_tbl[lnum] = new_pnum;
        up_read(&ubi->fm_sem);
        ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
        if (pnum < 0) {
                ubi_free_vid_hdr(ubi, vid_hdr);
                leb_write_unlock(ubi, vol_id, lnum);
+               up_read(&ubi->fm_sem);
                return pnum;
        }
 
        if (err) {
                ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
                         vol_id, lnum, pnum);
+               up_read(&ubi->fm_sem);
                goto write_error;
        }
 
                if (err) {
                        ubi_warn(ubi, "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
                                 len, offset, vol_id, lnum, pnum);
+                       up_read(&ubi->fm_sem);
                        goto write_error;
                }
        }
 
-       down_read(&ubi->fm_sem);
        vol->eba_tbl[lnum] = pnum;
        up_read(&ubi->fm_sem);
 
        if (pnum < 0) {
                ubi_free_vid_hdr(ubi, vid_hdr);
                leb_write_unlock(ubi, vol_id, lnum);
+               up_read(&ubi->fm_sem);
                return pnum;
        }
 
        if (err) {
                ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
                         vol_id, lnum, pnum);
+               up_read(&ubi->fm_sem);
                goto write_error;
        }
 
        if (err) {
                ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
                         len, pnum);
+               up_read(&ubi->fm_sem);
                goto write_error;
        }
 
        ubi_assert(vol->eba_tbl[lnum] < 0);
-       down_read(&ubi->fm_sem);
        vol->eba_tbl[lnum] = pnum;
        up_read(&ubi->fm_sem);
 
        pnum = ubi_wl_get_peb(ubi);
        if (pnum < 0) {
                err = pnum;
+               up_read(&ubi->fm_sem);
                goto out_leb_unlock;
        }
 
        if (err) {
                ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
                         vol_id, lnum, pnum);
+               up_read(&ubi->fm_sem);
                goto write_error;
        }
 
        if (err) {
                ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
                         len, pnum);
+               up_read(&ubi->fm_sem);
                goto write_error;
        }
 
-       down_read(&ubi->fm_sem);
        old_pnum = vol->eba_tbl[lnum];
        vol->eba_tbl[lnum] = pnum;
        up_read(&ubi->fm_sem);
 
 
 /* ubi_wl_get_peb - works exaclty like __wl_get_peb but keeps track of
  * the fastmap pool.
+ * Returns with ubi->fm_sem held in read mode!
  */
 int ubi_wl_get_peb(struct ubi_device *ubi)
 {
        struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
 
 again:
+       down_read(&ubi->fm_sem);
        spin_lock(&ubi->wl_lock);
        /* We check here also for the WL pool because at this point we can
         * refill the WL pool synchronous. */
        if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
                spin_unlock(&ubi->wl_lock);
+               up_read(&ubi->fm_sem);
                ret = ubi_update_fastmap(ubi);
                if (ret) {
                        ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
+                       down_read(&ubi->fm_sem);
                        return -ENOSPC;
                }
+               down_read(&ubi->fm_sem);
                spin_lock(&ubi->wl_lock);
        }
 
                        goto out;
                }
                retried = 1;
+               up_read(&ubi->fm_sem);
                goto again;
        }
 
        spin_lock(&ubi->wl_lock);
        peb = wl_get_peb(ubi);
        spin_unlock(&ubi->wl_lock);
+       down_read(&ubi->fm_sem);
 
        if (peb < 0)
                return peb;