Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / mtd / ubi / wl.c
index fb870f4..e8db4cb 100644 (file)
  * @list: a link in the list of pending works
  * @func: worker function
  * @e: physical eraseblock to erase
+ * @vol_id: the volume ID on which this erasure is being performed
+ * @lnum: the logical eraseblock number
  * @torture: if the physical eraseblock has to be tortured
  *
  * The @func pointer points to the worker function. If the @cancel argument is
@@ -152,6 +154,8 @@ struct ubi_work {
        int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
        /* The below fields are only relevant to erasure works */
        struct ubi_wl_entry *e;
+       int vol_id;
+       int lnum;
        int torture;
 };
 
@@ -579,13 +583,15 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
  * schedule_erase - schedule an erase work.
  * @ubi: UBI device description object
  * @e: the WL entry of the physical eraseblock to erase
+ * @vol_id: the volume ID that last used this PEB
+ * @lnum: the last used logical eraseblock number for the PEB
  * @torture: if the physical eraseblock has to be tortured
  *
  * This function returns zero in case of success and a %-ENOMEM in case of
  * failure.
  */
 static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
-                         int torture)
+                         int vol_id, int lnum, int torture)
 {
        struct ubi_work *wl_wrk;
 
@@ -598,6 +604,8 @@ static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
 
        wl_wrk->func = &erase_worker;
        wl_wrk->e = e;
+       wl_wrk->vol_id = vol_id;
+       wl_wrk->lnum = lnum;
        wl_wrk->torture = torture;
 
        schedule_ubi_work(ubi, wl_wrk);
@@ -618,7 +626,7 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
                                int cancel)
 {
        int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
-       int vol_id = -1, uninitialized_var(lnum);
+       int vol_id = -1, lnum = -1;
        struct ubi_wl_entry *e1, *e2;
        struct ubi_vid_hdr *vid_hdr;
 
@@ -798,7 +806,7 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
        ubi->move_to_put = ubi->wl_scheduled = 0;
        spin_unlock(&ubi->wl_lock);
 
-       err = schedule_erase(ubi, e1, 0);
+       err = schedule_erase(ubi, e1, vol_id, lnum, 0);
        if (err) {
                kmem_cache_free(ubi_wl_entry_slab, e1);
                if (e2)
@@ -813,7 +821,7 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
                 */
                dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
                       e2->pnum, vol_id, lnum);
-               err = schedule_erase(ubi, e2, 0);
+               err = schedule_erase(ubi, e2, vol_id, lnum, 0);
                if (err) {
                        kmem_cache_free(ubi_wl_entry_slab, e2);
                        goto out_ro;
@@ -852,7 +860,7 @@ out_not_moved:
        spin_unlock(&ubi->wl_lock);
 
        ubi_free_vid_hdr(ubi, vid_hdr);
-       err = schedule_erase(ubi, e2, torture);
+       err = schedule_erase(ubi, e2, vol_id, lnum, torture);
        if (err) {
                kmem_cache_free(ubi_wl_entry_slab, e2);
                goto out_ro;
@@ -971,6 +979,8 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
 {
        struct ubi_wl_entry *e = wl_wrk->e;
        int pnum = e->pnum, err, need;
+       int vol_id = wl_wrk->vol_id;
+       int lnum = wl_wrk->lnum;
 
        if (cancel) {
                dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
@@ -979,7 +989,8 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
                return 0;
        }
 
-       dbg_wl("erase PEB %d EC %d", pnum, e->ec);
+       dbg_wl("erase PEB %d EC %d LEB %d:%d",
+              pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
 
        err = sync_erase(ubi, e, wl_wrk->torture);
        if (!err) {
@@ -1009,7 +1020,7 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
                int err1;
 
                /* Re-schedule the LEB for erasure */
-               err1 = schedule_erase(ubi, e, 0);
+               err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
                if (err1) {
                        err = err1;
                        goto out_ro;
@@ -1077,6 +1088,8 @@ out_ro:
 /**
  * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
  * @ubi: UBI device description object
+ * @vol_id: the volume ID that last used this PEB
+ * @lnum: the last used logical eraseblock number for the PEB
  * @pnum: physical eraseblock to return
  * @torture: if this physical eraseblock has to be tortured
  *
@@ -1085,7 +1098,8 @@ out_ro:
  * occurred to this @pnum and it has to be tested. This function returns zero
  * in case of success, and a negative error code in case of failure.
  */
-int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
+int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
+                  int pnum, int torture)
 {
        int err;
        struct ubi_wl_entry *e;
@@ -1151,7 +1165,7 @@ retry:
        }
        spin_unlock(&ubi->wl_lock);
 
-       err = schedule_erase(ubi, e, torture);
+       err = schedule_erase(ubi, e, vol_id, lnum, torture);
        if (err) {
                spin_lock(&ubi->wl_lock);
                wl_tree_add(e, &ubi->used);
@@ -1175,7 +1189,7 @@ int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
 {
        struct ubi_wl_entry *e;
 
-       dbg_msg("schedule PEB %d for scrubbing", pnum);
+       ubi_msg("schedule PEB %d for scrubbing", pnum);
 
 retry:
        spin_lock(&ubi->wl_lock);
@@ -1227,23 +1241,54 @@ retry:
 /**
  * ubi_wl_flush - flush all pending works.
  * @ubi: UBI device description object
+ * @vol_id: the volume id to flush for
+ * @lnum: the logical eraseblock number to flush for
  *
- * This function returns zero in case of success and a negative error code in
- * case of failure.
+ * This function executes all pending works for a particular volume id /
+ * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
+ * acts as a wildcard for all of the corresponding volume numbers or logical
+ * eraseblock numbers. It returns zero in case of success and a negative error
+ * code in case of failure.
  */
-int ubi_wl_flush(struct ubi_device *ubi)
+int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
 {
-       int err;
+       int err = 0;
+       int found = 1;
 
        /*
         * Erase while the pending works queue is not empty, but not more than
         * the number of currently pending works.
         */
-       dbg_wl("flush (%d pending works)", ubi->works_count);
-       while (ubi->works_count) {
-               err = do_work(ubi);
-               if (err)
-                       return err;
+       dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
+              vol_id, lnum, ubi->works_count);
+
+       while (found) {
+               struct ubi_work *wrk;
+               found = 0;
+
+               down_read(&ubi->work_sem);
+               spin_lock(&ubi->wl_lock);
+               list_for_each_entry(wrk, &ubi->works, list) {
+                       if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
+                           (lnum == UBI_ALL || wrk->lnum == lnum)) {
+                               list_del(&wrk->list);
+                               ubi->works_count -= 1;
+                               ubi_assert(ubi->works_count >= 0);
+                               spin_unlock(&ubi->wl_lock);
+
+                               err = wrk->func(ubi, wrk, 0);
+                               if (err) {
+                                       up_read(&ubi->work_sem);
+                                       return err;
+                               }
+
+                               spin_lock(&ubi->wl_lock);
+                               found = 1;
+                               break;
+                       }
+               }
+               spin_unlock(&ubi->wl_lock);
+               up_read(&ubi->work_sem);
        }
 
        /*
@@ -1253,18 +1298,7 @@ int ubi_wl_flush(struct ubi_device *ubi)
        down_write(&ubi->work_sem);
        up_write(&ubi->work_sem);
 
-       /*
-        * And in case last was the WL worker and it canceled the LEB
-        * movement, flush again.
-        */
-       while (ubi->works_count) {
-               dbg_wl("flush more (%d pending works)", ubi->works_count);
-               err = do_work(ubi);
-               if (err)
-                       return err;
-       }
-
-       return 0;
+       return err;
 }
 
 /**
@@ -1373,18 +1407,18 @@ static void cancel_pending(struct ubi_device *ubi)
 }
 
 /**
- * ubi_wl_init_scan - initialize the WL sub-system using scanning information.
+ * ubi_wl_init - initialize the WL 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_wl_init_scan(struct ubi_device *ubi, struct ubi_attach_info *si)
+int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
 {
        int err, i;
        struct rb_node *rb1, *rb2;
-       struct ubi_ainf_volume *sv;
+       struct ubi_ainf_volume *av;
        struct ubi_ainf_peb *aeb, *tmp;
        struct ubi_wl_entry *e;
 
@@ -1392,7 +1426,7 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_attach_info *si)
        spin_lock_init(&ubi->wl_lock);
        mutex_init(&ubi->move_mutex);
        init_rwsem(&ubi->work_sem);
-       ubi->max_ec = si->max_ec;
+       ubi->max_ec = ai->max_ec;
        INIT_LIST_HEAD(&ubi->works);
 
        sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
@@ -1406,7 +1440,7 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_attach_info *si)
                INIT_LIST_HEAD(&ubi->pq[i]);
        ubi->pq_head = 0;
 
-       list_for_each_entry_safe(aeb, tmp, &si->erase, u.list) {
+       list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
                cond_resched();
 
                e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
@@ -1416,13 +1450,13 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_attach_info *si)
                e->pnum = aeb->pnum;
                e->ec = aeb->ec;
                ubi->lookuptbl[e->pnum] = e;
-               if (schedule_erase(ubi, e, 0)) {
+               if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
                        kmem_cache_free(ubi_wl_entry_slab, e);
                        goto out_free;
                }
        }
 
-       list_for_each_entry(aeb, &si->free, u.list) {
+       list_for_each_entry(aeb, &ai->free, u.list) {
                cond_resched();
 
                e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
@@ -1436,8 +1470,8 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_attach_info *si)
                ubi->lookuptbl[e->pnum] = e;
        }
 
-       ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
-               ubi_rb_for_each_entry(rb2, aeb, &sv->root, u.rb) {
+       ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
+               ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
                        cond_resched();
 
                        e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
@@ -1465,6 +1499,7 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_attach_info *si)
                if (ubi->corr_peb_count)
                        ubi_err("%d PEBs are corrupted and not used",
                                ubi->corr_peb_count);
+               err = -ENOSPC;
                goto out_free;
        }
        ubi->avail_pebs -= WL_RESERVED_PEBS;