+/* Switch to next dev, via round-robin, after MIN_READS reads */
+#define MIN_READS 128
+
+/* choose_mirror
+ * @ms: the mirror set
+ *
+ * This function is used for read balancing.
+ *
+ * Returns: chosen mirror, or NULL on failure
+ */
+static struct mirror *choose_mirror(struct mirror_set *ms)
+{
+ struct mirror *start_mirror = ms->read_mirror;
+
+ /*
+ * Perform MIN_READS on each working mirror then
+ * advance to the next one. start_mirror stores
+ * the first we tried, so we know when we're done.
+ */
+ do {
+ if (likely(!atomic_read(&ms->read_mirror->error_count)) &&
+ !atomic_dec_and_test(&ms->read_count))
+ goto use_mirror;
+
+ atomic_set(&ms->read_count, MIN_READS);
+
+ if (ms->read_mirror-- == ms->mirror)
+ ms->read_mirror += ms->nr_mirrors;
+ } while (ms->read_mirror != start_mirror);
+
+ /*
+ * We've rejected every mirror.
+ * Confirm the start_mirror can be used.
+ */
+ if (unlikely(atomic_read(&ms->read_mirror->error_count)))
+ return NULL;
+
+use_mirror:
+ return ms->read_mirror;
+}
+
+/* fail_mirror
+ * @m: mirror device to fail
+ *
+ * If the device is valid, mark it invalid. Also,
+ * if this is the default mirror device (i.e. the primary
+ * device) and the mirror set is in-sync, choose an
+ * alternate primary device.
+ *
+ * This function cannot block.
+ */
+static void fail_mirror(struct mirror *m)
+{
+ struct mirror_set *ms = m->ms;
+ struct mirror *new;
+
+ atomic_inc(&m->error_count);
+
+ if (atomic_read(&m->error_count) > 1)
+ return;
+
+ if (m != ms->default_mirror)
+ return;
+
+ /*
+ * If the default mirror fails, change it.
+ * In the case of cluster mirroring, the default
+ * is changed in rh_update_states.
+ */
+ if (!ms->in_sync) {
+ /*
+ * Can not switch primary. Better to issue requests
+ * to same failing device than to risk returning
+ * corrupt data.
+ */
+ DMERR("Primary mirror device has failed while mirror is not in-sync");
+ DMERR("Unable to choose alternative primary device");
+ return;
+ }
+
+ for (new = ms->mirror; new < ms->mirror + ms->nr_mirrors; new++)
+ if (!atomic_read(&new->error_count)) {
+ ms->default_mirror = new;
+ break;
+ }
+
+ if (unlikely(new == ms->mirror + ms->nr_mirrors))
+ DMWARN("All sides of mirror have failed.");
+}
+
+static int default_ok(struct mirror *m)
+{
+ return !atomic_read(&m->ms->default_mirror->error_count);
+}
+
+static int mirror_available(struct mirror_set *ms, struct bio *bio)