Remove nearly duplicated code and prepare for the following patch.
Cc: Jamal Hadi Salim <jhs@mojatatu.com>
Acked-by: Jamal Hadi Salim <jhs@mojatatu.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
        sch->qstats.backlog = 0;
 }
 
+static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
+                                         struct Qdisc **pold)
+{
+       struct Qdisc *old;
+
+       sch_tree_lock(sch);
+       old = *pold;
+       *pold = new;
+       if (old != NULL) {
+               qdisc_tree_decrease_qlen(old, old->q.qlen);
+               qdisc_reset(old);
+       }
+       sch_tree_unlock(sch);
+
+       return old;
+}
+
 static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
                                              struct sk_buff_head *list)
 {
 
                        new->reshape_fail = cbq_reshape_fail;
 #endif
        }
-       sch_tree_lock(sch);
-       *old = cl->q;
-       cl->q = new;
-       qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
-       qdisc_reset(*old);
-       sch_tree_unlock(sch);
 
+       *old = qdisc_replace(sch, new, &cl->q);
        return 0;
 }
 
 
                        new = &noop_qdisc;
        }
 
-       sch_tree_lock(sch);
-       drr_purge_queue(cl);
-       *old = cl->qdisc;
-       cl->qdisc = new;
-       sch_tree_unlock(sch);
+       *old = qdisc_replace(sch, new, &cl->qdisc);
        return 0;
 }
 
 
                        new = &noop_qdisc;
        }
 
-       sch_tree_lock(sch);
-       *old = p->q;
-       p->q = new;
-       qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
-       qdisc_reset(*old);
-       sch_tree_unlock(sch);
-
+       *old = qdisc_replace(sch, new, &p->q);
        return 0;
 }
 
 
                        new = &noop_qdisc;
        }
 
-       sch_tree_lock(sch);
-       hfsc_purge_queue(sch, cl);
-       *old = cl->qdisc;
-       cl->qdisc = new;
-       sch_tree_unlock(sch);
+       *old = qdisc_replace(sch, new, &cl->qdisc);
        return 0;
 }
 
 
                                     cl->common.classid)) == NULL)
                return -ENOBUFS;
 
-       sch_tree_lock(sch);
-       *old = cl->un.leaf.q;
-       cl->un.leaf.q = new;
-       if (*old != NULL) {
-               qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
-               qdisc_reset(*old);
-       }
-       sch_tree_unlock(sch);
+       *old = qdisc_replace(sch, new, &cl->un.leaf.q);
        return 0;
 }
 
 
        if (new == NULL)
                new = &noop_qdisc;
 
-       sch_tree_lock(sch);
-       *old = q->queues[band];
-       q->queues[band] = new;
-       qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
-       qdisc_reset(*old);
-       sch_tree_unlock(sch);
-
+       *old = qdisc_replace(sch, new, &q->queues[band]);
        return 0;
 }
 
 
 {
        struct netem_sched_data *q = qdisc_priv(sch);
 
-       sch_tree_lock(sch);
-       *old = q->qdisc;
-       q->qdisc = new;
-       if (*old) {
-               qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
-               qdisc_reset(*old);
-       }
-       sch_tree_unlock(sch);
-
+       *old = qdisc_replace(sch, new, &q->qdisc);
        return 0;
 }
 
 
        if (new == NULL)
                new = &noop_qdisc;
 
-       sch_tree_lock(sch);
-       *old = q->queues[band];
-       q->queues[band] = new;
-       qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
-       qdisc_reset(*old);
-       sch_tree_unlock(sch);
-
+       *old = qdisc_replace(sch, new, &q->queues[band]);
        return 0;
 }
 
 
                        new = &noop_qdisc;
        }
 
-       sch_tree_lock(sch);
-       qfq_purge_queue(cl);
-       *old = cl->qdisc;
-       cl->qdisc = new;
-       sch_tree_unlock(sch);
+       *old = qdisc_replace(sch, new, &cl->qdisc);
        return 0;
 }
 
 
        if (new == NULL)
                new = &noop_qdisc;
 
-       sch_tree_lock(sch);
-       *old = q->qdisc;
-       q->qdisc = new;
-       qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
-       qdisc_reset(*old);
-       sch_tree_unlock(sch);
+       *old = qdisc_replace(sch, new, &q->qdisc);
        return 0;
 }
 
 
        if (new == NULL)
                new = &noop_qdisc;
 
-       sch_tree_lock(sch);
-       *old = q->qdisc;
-       q->qdisc = new;
-       qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
-       qdisc_reset(*old);
-       sch_tree_unlock(sch);
+       *old = qdisc_replace(sch, new, &q->qdisc);
        return 0;
 }
 
 
        if (new == NULL)
                new = &noop_qdisc;
 
-       sch_tree_lock(sch);
-       *old = q->qdisc;
-       q->qdisc = new;
-       qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
-       qdisc_reset(*old);
-       sch_tree_unlock(sch);
-
+       *old = qdisc_replace(sch, new, &q->qdisc);
        return 0;
 }