wake_up(&mddev->sb_wait);
 }
 
+static void no_op(struct percpu_ref *r) {}
+
 int mddev_init(struct mddev *mddev)
 {
 
                            PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
                return -ENOMEM;
 
+       if (percpu_ref_init(&mddev->writes_pending, no_op,
+                           PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
+               percpu_ref_exit(&mddev->active_io);
+               return -ENOMEM;
+       }
+
+       /* We want to start with the refcount at zero */
+       percpu_ref_put(&mddev->writes_pending);
+
        mutex_init(&mddev->open_mutex);
        mutex_init(&mddev->reconfig_mutex);
        mutex_init(&mddev->sync_mutex);
 void mddev_destroy(struct mddev *mddev)
 {
        percpu_ref_exit(&mddev->active_io);
+       percpu_ref_exit(&mddev->writes_pending);
 }
 EXPORT_SYMBOL_GPL(mddev_destroy);
 
        kobject_put(&mddev->kobj);
 }
 
-static void no_op(struct percpu_ref *r) {}
-
-int mddev_init_writes_pending(struct mddev *mddev)
-{
-       if (mddev->writes_pending.percpu_count_ptr)
-               return 0;
-       if (percpu_ref_init(&mddev->writes_pending, no_op,
-                           PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
-               return -ENOMEM;
-       /* We want to start with the refcount at zero */
-       percpu_ref_put(&mddev->writes_pending);
-       return 0;
-}
-EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
-
 struct mddev *md_alloc(dev_t dev, char *name)
 {
        /*
         */
        __md_stop_writes(mddev);
        __md_stop(mddev);
-       percpu_ref_exit(&mddev->writes_pending);
 }
 
 EXPORT_SYMBOL_GPL(md_stop);
 {
        struct mddev *mddev = disk->private_data;
 
-       percpu_ref_exit(&mddev->writes_pending);
        mddev_free(mddev);
 }
 
 
 extern void md_wakeup_thread(struct md_thread __rcu *thread);
 extern void md_check_recovery(struct mddev *mddev);
 extern void md_reap_sync_thread(struct mddev *mddev);
-extern int mddev_init_writes_pending(struct mddev *mddev);
 extern bool md_write_start(struct mddev *mddev, struct bio *bi);
 extern void md_write_inc(struct mddev *mddev, struct bio *bi);
 extern void md_write_end(struct mddev *mddev);