Blame view

drivers/md/dm-raid1.c 35 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
  /*
   * Copyright (C) 2003 Sistina Software Limited.
1f965b194   Heinz Mauelshagen   dm raid1: separat...
3
   * Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
   *
   * This file is released under the GPL.
   */
06386bbfd   Jonathan Brassow   dm raid1: handle ...
7
  #include "dm-bio-record.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
11
12
13
  #include <linux/init.h>
  #include <linux/mempool.h>
  #include <linux/module.h>
  #include <linux/pagemap.h>
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
  #include <linux/workqueue.h>
1f965b194   Heinz Mauelshagen   dm raid1: separat...
15
  #include <linux/device-mapper.h>
a765e20ee   Alasdair G Kergon   dm: move include ...
16
17
18
  #include <linux/dm-io.h>
  #include <linux/dm-dirty-log.h>
  #include <linux/dm-kcopyd.h>
1f965b194   Heinz Mauelshagen   dm raid1: separat...
19
  #include <linux/dm-region-hash.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20

72d948616   Alasdair G Kergon   [PATCH] dm: impro...
21
  #define DM_MSG_PREFIX "raid1"
1f965b194   Heinz Mauelshagen   dm raid1: separat...
22
23
  
  #define MAX_RECOVERY 1	/* Maximum number of regions recovered in parallel. */
72d948616   Alasdair G Kergon   [PATCH] dm: impro...
24

65972a6fa   Kees Cook   dm mirror: remove...
25
  #define MAX_NR_MIRRORS	(DM_KCOPYD_MAX_REGIONS + 1)
ed63287dd   Lidong Zhong   dm raid1: keep is...
26
27
  #define DM_RAID1_HANDLE_ERRORS	0x01
  #define DM_RAID1_KEEP_LOG	0x02
f44db678e   Jonathan Brassow   dm raid1: handle ...
28
  #define errors_handled(p)	((p)->features & DM_RAID1_HANDLE_ERRORS)
ed63287dd   Lidong Zhong   dm raid1: keep is...
29
  #define keep_log(p)		((p)->features & DM_RAID1_KEEP_LOG)
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
30

33184048d   Jonathan E Brassow   [PATCH] dm: raid1...
31
  static DECLARE_WAIT_QUEUE_HEAD(_kmirrord_recovery_stopped);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
  /*-----------------------------------------------------------------
e4c8b3ba3   Neil Brown   [PATCH] dm: mirro...
34
35
   * Mirror set structures.
   *---------------------------------------------------------------*/
72f4b3141   Jonathan Brassow   dm raid1: handle ...
36
37
  enum dm_raid1_error {
  	DM_RAID1_WRITE_ERROR,
64b30c46e   Mikulas Patocka   dm raid1: report ...
38
  	DM_RAID1_FLUSH_ERROR,
72f4b3141   Jonathan Brassow   dm raid1: handle ...
39
40
41
  	DM_RAID1_SYNC_ERROR,
  	DM_RAID1_READ_ERROR
  };
e4c8b3ba3   Neil Brown   [PATCH] dm: mirro...
42
  struct mirror {
aa5617c55   Jonathan Brassow   dm raid1: add mir...
43
  	struct mirror_set *ms;
e4c8b3ba3   Neil Brown   [PATCH] dm: mirro...
44
  	atomic_t error_count;
39ed7adb1   Al Viro   dm-raid1 breakage...
45
  	unsigned long error_type;
e4c8b3ba3   Neil Brown   [PATCH] dm: mirro...
46
47
48
49
50
51
52
  	struct dm_dev *dev;
  	sector_t offset;
  };
  
  struct mirror_set {
  	struct dm_target *ti;
  	struct list_head list;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
53

a8e6afa23   Jonathan E Brassow   dm raid1: add han...
54
  	uint64_t features;
e4c8b3ba3   Neil Brown   [PATCH] dm: mirro...
55

72f4b3141   Jonathan Brassow   dm raid1: handle ...
56
  	spinlock_t lock;	/* protects the lists */
e4c8b3ba3   Neil Brown   [PATCH] dm: mirro...
57
58
  	struct bio_list reads;
  	struct bio_list writes;
72f4b3141   Jonathan Brassow   dm raid1: handle ...
59
  	struct bio_list failures;
047885076   Mikulas Patocka   dm raid1: add fra...
60
  	struct bio_list holds;	/* bios are waiting until suspend */
e4c8b3ba3   Neil Brown   [PATCH] dm: mirro...
61

1f965b194   Heinz Mauelshagen   dm raid1: separat...
62
63
  	struct dm_region_hash *rh;
  	struct dm_kcopyd_client *kcopyd_client;
88be163ab   Milan Broz   dm raid1: update ...
64
  	struct dm_io_client *io_client;
e4c8b3ba3   Neil Brown   [PATCH] dm: mirro...
65
66
67
  	/* recovery */
  	region_t nr_regions;
  	int in_sync;
fc1ff9588   Jonathan Brassow   dm raid1: handle ...
68
  	int log_failure;
929be8fcb   Mikulas Patocka   dm raid1: hold al...
69
  	int leg_failure;
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
70
  	atomic_t suspend;
e4c8b3ba3   Neil Brown   [PATCH] dm: mirro...
71

72f4b3141   Jonathan Brassow   dm raid1: handle ...
72
  	atomic_t default_mirror;	/* Default mirror */
e4c8b3ba3   Neil Brown   [PATCH] dm: mirro...
73

6ad36fe2b   Holger Smolinski   dm raid1: one kmi...
74
75
  	struct workqueue_struct *kmirrord_wq;
  	struct work_struct kmirrord_work;
a2aebe03b   Mikulas Patocka   dm raid1: use timer
76
77
  	struct timer_list timer;
  	unsigned long timer_pending;
72f4b3141   Jonathan Brassow   dm raid1: handle ...
78
  	struct work_struct trigger_event;
6ad36fe2b   Holger Smolinski   dm raid1: one kmi...
79

1f965b194   Heinz Mauelshagen   dm raid1: separat...
80
  	unsigned nr_mirrors;
b18ae8dd9   Gustavo A. R. Silva   dm: replace zero-...
81
  	struct mirror mirror[];
e4c8b3ba3   Neil Brown   [PATCH] dm: mirro...
82
  };
df5d2e908   Mikulas Patocka   dm kcopyd: introd...
83
84
  DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(raid1_resync_throttle,
  		"A percentage of time allocated for raid resynchronization");
1f965b194   Heinz Mauelshagen   dm raid1: separat...
85
  static void wakeup_mirrord(void *context)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
  {
1f965b194   Heinz Mauelshagen   dm raid1: separat...
87
  	struct mirror_set *ms = context;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88

6ad36fe2b   Holger Smolinski   dm raid1: one kmi...
89
90
  	queue_work(ms->kmirrord_wq, &ms->kmirrord_work);
  }
8376d3c1f   Kees Cook   md: Convert timer...
91
  static void delayed_wake_fn(struct timer_list *t)
a2aebe03b   Mikulas Patocka   dm raid1: use timer
92
  {
8376d3c1f   Kees Cook   md: Convert timer...
93
  	struct mirror_set *ms = from_timer(ms, t, timer);
a2aebe03b   Mikulas Patocka   dm raid1: use timer
94
95
  
  	clear_bit(0, &ms->timer_pending);
1f965b194   Heinz Mauelshagen   dm raid1: separat...
96
  	wakeup_mirrord(ms);
a2aebe03b   Mikulas Patocka   dm raid1: use timer
97
98
99
100
101
102
103
104
  }
  
  static void delayed_wake(struct mirror_set *ms)
  {
  	if (test_and_set_bit(0, &ms->timer_pending))
  		return;
  
  	ms->timer.expires = jiffies + HZ / 5;
a2aebe03b   Mikulas Patocka   dm raid1: use timer
105
106
  	add_timer(&ms->timer);
  }
1f965b194   Heinz Mauelshagen   dm raid1: separat...
107
  static void wakeup_all_recovery_waiters(void *context)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
  {
1f965b194   Heinz Mauelshagen   dm raid1: separat...
109
  	wake_up_all(&_kmirrord_recovery_stopped);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
  }
1f965b194   Heinz Mauelshagen   dm raid1: separat...
111
  static void queue_bio(struct mirror_set *ms, struct bio *bio, int rw)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
  {
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
  	int should_wake = 0;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
115
  	struct bio_list *bl;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116

1f965b194   Heinz Mauelshagen   dm raid1: separat...
117
118
119
120
121
  	bl = (rw == WRITE) ? &ms->writes : &ms->reads;
  	spin_lock_irqsave(&ms->lock, flags);
  	should_wake = !(bl->head);
  	bio_list_add(bl, bio);
  	spin_unlock_irqrestore(&ms->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
  
  	if (should_wake)
1f965b194   Heinz Mauelshagen   dm raid1: separat...
124
  		wakeup_mirrord(ms);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
  }
1f965b194   Heinz Mauelshagen   dm raid1: separat...
126
  static void dispatch_bios(void *context, struct bio_list *bio_list)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
  {
1f965b194   Heinz Mauelshagen   dm raid1: separat...
128
129
  	struct mirror_set *ms = context;
  	struct bio *bio;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130

1f965b194   Heinz Mauelshagen   dm raid1: separat...
131
132
  	while ((bio = bio_list_pop(bio_list)))
  		queue_bio(ms, bio, WRITE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
  }
89c7cd897   Mikulas Patocka   dm raid1: rename ...
134
  struct dm_raid1_bio_record {
06386bbfd   Jonathan Brassow   dm raid1: handle ...
135
  	struct mirror *m;
74d46992e   Christoph Hellwig   block: replace bi...
136
  	/* if details->bi_disk == NULL, details were not saved */
06386bbfd   Jonathan Brassow   dm raid1: handle ...
137
  	struct dm_bio_details details;
0045d61b5   Mikulas Patocka   dm raid1: dont us...
138
  	region_t write_region;
06386bbfd   Jonathan Brassow   dm raid1: handle ...
139
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
141
142
143
144
145
  /*
   * Every mirror should look like this one.
   */
  #define DEFAULT_MIRROR 0
  
  /*
06386bbfd   Jonathan Brassow   dm raid1: handle ...
146
147
   * This is yucky.  We squirrel the mirror struct away inside
   * bi_next for read/write buffers.  This is safe since the bh
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
   * doesn't get submitted to the lower levels of block layer.
   */
06386bbfd   Jonathan Brassow   dm raid1: handle ...
150
  static struct mirror *bio_get_m(struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
  {
06386bbfd   Jonathan Brassow   dm raid1: handle ...
152
  	return (struct mirror *) bio->bi_next;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
  }
06386bbfd   Jonathan Brassow   dm raid1: handle ...
154
  static void bio_set_m(struct bio *bio, struct mirror *m)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
  {
06386bbfd   Jonathan Brassow   dm raid1: handle ...
156
  	bio->bi_next = (struct bio *) m;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
  }
72f4b3141   Jonathan Brassow   dm raid1: handle ...
158
159
160
161
162
163
164
165
166
167
168
169
  static struct mirror *get_default_mirror(struct mirror_set *ms)
  {
  	return &ms->mirror[atomic_read(&ms->default_mirror)];
  }
  
  static void set_default_mirror(struct mirror *m)
  {
  	struct mirror_set *ms = m->ms;
  	struct mirror *m0 = &(ms->mirror[0]);
  
  	atomic_set(&ms->default_mirror, m - m0);
  }
87968ddd2   Mikulas Patocka   dm raid1: abstrac...
170
171
172
173
174
175
176
177
178
179
  static struct mirror *get_valid_mirror(struct mirror_set *ms)
  {
  	struct mirror *m;
  
  	for (m = ms->mirror; m < ms->mirror + ms->nr_mirrors; m++)
  		if (!atomic_read(&m->error_count))
  			return m;
  
  	return NULL;
  }
72f4b3141   Jonathan Brassow   dm raid1: handle ...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  /* fail_mirror
   * @m: mirror device to fail
   * @error_type: one of the enum's, DM_RAID1_*_ERROR
   *
   * If errors are being handled, record the type of
   * error encountered for this device.  If this type
   * of error has already been recorded, we can return;
   * otherwise, we must signal userspace by triggering
   * an event.  Additionally, if the device is the
   * primary device, we must choose a new primary, but
   * only if the mirror is in-sync.
   *
   * This function must not block.
   */
  static void fail_mirror(struct mirror *m, enum dm_raid1_error error_type)
  {
  	struct mirror_set *ms = m->ms;
  	struct mirror *new;
929be8fcb   Mikulas Patocka   dm raid1: hold al...
198
  	ms->leg_failure = 1;
72f4b3141   Jonathan Brassow   dm raid1: handle ...
199
200
201
202
203
204
205
206
207
  	/*
  	 * error_count is used for nothing more than a
  	 * simple way to tell if a device has encountered
  	 * errors.
  	 */
  	atomic_inc(&m->error_count);
  
  	if (test_and_set_bit(error_type, &m->error_type))
  		return;
d460c65a6   Jonathan Brassow   dm raid1: fix err...
208
209
  	if (!errors_handled(ms))
  		return;
72f4b3141   Jonathan Brassow   dm raid1: handle ...
210
211
  	if (m != get_default_mirror(ms))
  		goto out;
ed63287dd   Lidong Zhong   dm raid1: keep is...
212
  	if (!ms->in_sync && !keep_log(ms)) {
72f4b3141   Jonathan Brassow   dm raid1: handle ...
213
214
215
216
217
218
219
220
  		/*
  		 * Better to issue requests to same failing device
  		 * than to risk returning corrupt data.
  		 */
  		DMERR("Primary mirror (%s) failed while out-of-sync: "
  		      "Reads may fail.", m->dev->name);
  		goto out;
  	}
87968ddd2   Mikulas Patocka   dm raid1: abstrac...
221
222
223
224
  	new = get_valid_mirror(ms);
  	if (new)
  		set_default_mirror(new);
  	else
72f4b3141   Jonathan Brassow   dm raid1: handle ...
225
226
227
228
229
  		DMWARN("All sides of mirror have failed.");
  
  out:
  	schedule_work(&ms->trigger_event);
  }
c0da3748b   Mikulas Patocka   dm raid1: impleme...
230
231
232
233
234
235
  static int mirror_flush(struct dm_target *ti)
  {
  	struct mirror_set *ms = ti->private;
  	unsigned long error_bits;
  
  	unsigned int i;
65972a6fa   Kees Cook   dm mirror: remove...
236
  	struct dm_io_region io[MAX_NR_MIRRORS];
c0da3748b   Mikulas Patocka   dm raid1: impleme...
237
238
  	struct mirror *m;
  	struct dm_io_request io_req = {
e6047149d   Mike Christie   dm: use bio op ac...
239
  		.bi_op = REQ_OP_WRITE,
ff0361b34   Jan Kara   dm: make flush bi...
240
  		.bi_op_flags = REQ_PREFLUSH | REQ_SYNC,
c0da3748b   Mikulas Patocka   dm raid1: impleme...
241
  		.mem.type = DM_IO_KMEM,
5fc2ffeab   Mike Snitzer   dm raid1: support...
242
  		.mem.ptr.addr = NULL,
c0da3748b   Mikulas Patocka   dm raid1: impleme...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  		.client = ms->io_client,
  	};
  
  	for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++) {
  		io[i].bdev = m->dev->bdev;
  		io[i].sector = 0;
  		io[i].count = 0;
  	}
  
  	error_bits = -1;
  	dm_io(&io_req, ms->nr_mirrors, io, &error_bits);
  	if (unlikely(error_bits != 0)) {
  		for (i = 0; i < ms->nr_mirrors; i++)
  			if (test_bit(i, &error_bits))
  				fail_mirror(ms->mirror + i,
64b30c46e   Mikulas Patocka   dm raid1: report ...
258
  					    DM_RAID1_FLUSH_ERROR);
c0da3748b   Mikulas Patocka   dm raid1: impleme...
259
260
261
262
263
  		return -EIO;
  	}
  
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
265
266
267
268
269
270
  /*-----------------------------------------------------------------
   * Recovery.
   *
   * When a mirror is first activated we may find that some regions
   * are in the no-sync state.  We have to recover these by
   * recopying from the default mirror to all the others.
   *---------------------------------------------------------------*/
4cdc1d1fa   Alasdair G Kergon   dm io: write erro...
271
  static void recovery_complete(int read_err, unsigned long write_err,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
273
  			      void *context)
  {
1f965b194   Heinz Mauelshagen   dm raid1: separat...
274
275
  	struct dm_region *reg = context;
  	struct mirror_set *ms = dm_rh_region_context(reg);
8f0205b79   Jonathan Brassow   dm raid1: handle ...
276
  	int m, bit = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277

8f0205b79   Jonathan Brassow   dm raid1: handle ...
278
  	if (read_err) {
f44db678e   Jonathan Brassow   dm raid1: handle ...
279
280
  		/* Read error means the failure of default mirror. */
  		DMERR_LIMIT("Unable to read primary mirror during recovery");
8f0205b79   Jonathan Brassow   dm raid1: handle ...
281
282
  		fail_mirror(get_default_mirror(ms), DM_RAID1_SYNC_ERROR);
  	}
f44db678e   Jonathan Brassow   dm raid1: handle ...
283

8f0205b79   Jonathan Brassow   dm raid1: handle ...
284
  	if (write_err) {
4cdc1d1fa   Alasdair G Kergon   dm io: write erro...
285
  		DMERR_LIMIT("Write error during recovery (error = 0x%lx)",
f44db678e   Jonathan Brassow   dm raid1: handle ...
286
  			    write_err);
8f0205b79   Jonathan Brassow   dm raid1: handle ...
287
288
289
290
291
292
293
294
295
296
297
298
299
  		/*
  		 * Bits correspond to devices (excluding default mirror).
  		 * The default mirror cannot change during recovery.
  		 */
  		for (m = 0; m < ms->nr_mirrors; m++) {
  			if (&ms->mirror[m] == get_default_mirror(ms))
  				continue;
  			if (test_bit(bit, &write_err))
  				fail_mirror(ms->mirror + m,
  					    DM_RAID1_SYNC_ERROR);
  			bit++;
  		}
  	}
f44db678e   Jonathan Brassow   dm raid1: handle ...
300

1f965b194   Heinz Mauelshagen   dm raid1: separat...
301
  	dm_rh_recovery_end(reg, !(read_err || write_err));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
302
  }
7209049d4   Mike Snitzer   dm kcopyd: return...
303
  static void recover(struct mirror_set *ms, struct dm_region *reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
304
  {
1f965b194   Heinz Mauelshagen   dm raid1: separat...
305
  	unsigned i;
eb69aca5d   Heinz Mauelshagen   dm kcopyd: clean ...
306
  	struct dm_io_region from, to[DM_KCOPYD_MAX_REGIONS], *dest;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
  	struct mirror *m;
  	unsigned long flags = 0;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
309
310
  	region_t key = dm_rh_get_region_key(reg);
  	sector_t region_size = dm_rh_get_region_size(ms->rh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
312
  
  	/* fill in the source */
72f4b3141   Jonathan Brassow   dm raid1: handle ...
313
  	m = get_default_mirror(ms);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
  	from.bdev = m->dev->bdev;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
315
316
  	from.sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
  	if (key == (ms->nr_regions - 1)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
318
319
320
  		/*
  		 * The final region may be smaller than
  		 * region_size.
  		 */
1f965b194   Heinz Mauelshagen   dm raid1: separat...
321
  		from.count = ms->ti->len & (region_size - 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
322
  		if (!from.count)
1f965b194   Heinz Mauelshagen   dm raid1: separat...
323
  			from.count = region_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
  	} else
1f965b194   Heinz Mauelshagen   dm raid1: separat...
325
  		from.count = region_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
327
328
  
  	/* fill in the destinations */
  	for (i = 0, dest = to; i < ms->nr_mirrors; i++) {
72f4b3141   Jonathan Brassow   dm raid1: handle ...
329
  		if (&ms->mirror[i] == get_default_mirror(ms))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
331
332
333
  			continue;
  
  		m = ms->mirror + i;
  		dest->bdev = m->dev->bdev;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
334
  		dest->sector = m->offset + dm_rh_region_to_sector(ms->rh, key);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
336
337
338
339
  		dest->count = from.count;
  		dest++;
  	}
  
  	/* hand to kcopyd */
f7c83e2e4   Jonathan Brassow   dm raid1: kcopyd ...
340
341
  	if (!errors_handled(ms))
  		set_bit(DM_KCOPYD_IGNORE_ERROR, &flags);
7209049d4   Mike Snitzer   dm kcopyd: return...
342
343
  	dm_kcopyd_copy(ms->kcopyd_client, &from, ms->nr_mirrors - 1, to,
  		       flags, recovery_complete, reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
  }
ed63287dd   Lidong Zhong   dm raid1: keep is...
345
346
347
348
349
350
351
352
353
354
  static void reset_ms_flags(struct mirror_set *ms)
  {
  	unsigned int m;
  
  	ms->leg_failure = 0;
  	for (m = 0; m < ms->nr_mirrors; m++) {
  		atomic_set(&(ms->mirror[m].error_count), 0);
  		ms->mirror[m].error_type = 0;
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
356
  static void do_recovery(struct mirror_set *ms)
  {
1f965b194   Heinz Mauelshagen   dm raid1: separat...
357
358
  	struct dm_region *reg;
  	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
360
361
362
  
  	/*
  	 * Start quiescing some regions.
  	 */
1f965b194   Heinz Mauelshagen   dm raid1: separat...
363
  	dm_rh_recovery_prepare(ms->rh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
367
  
  	/*
  	 * Copy any already quiesced regions.
  	 */
7209049d4   Mike Snitzer   dm kcopyd: return...
368
369
  	while ((reg = dm_rh_recovery_start(ms->rh)))
  		recover(ms, reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
371
372
373
374
375
376
377
378
  
  	/*
  	 * Update the in sync flag.
  	 */
  	if (!ms->in_sync &&
  	    (log->type->get_sync_count(log) == ms->nr_regions)) {
  		/* the sync is complete */
  		dm_table_event(ms->ti->table);
  		ms->in_sync = 1;
ed63287dd   Lidong Zhong   dm raid1: keep is...
379
  		reset_ms_flags(ms);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380
381
382
383
384
385
386
387
  	}
  }
  
  /*-----------------------------------------------------------------
   * Reads
   *---------------------------------------------------------------*/
  static struct mirror *choose_mirror(struct mirror_set *ms, sector_t sector)
  {
06386bbfd   Jonathan Brassow   dm raid1: handle ...
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
  	struct mirror *m = get_default_mirror(ms);
  
  	do {
  		if (likely(!atomic_read(&m->error_count)))
  			return m;
  
  		if (m-- == ms->mirror)
  			m += ms->nr_mirrors;
  	} while (m != get_default_mirror(ms));
  
  	return NULL;
  }
  
  static int default_ok(struct mirror *m)
  {
  	struct mirror *default_mirror = get_default_mirror(m->ms);
  
  	return !atomic_read(&default_mirror->error_count);
  }
  
  static int mirror_available(struct mirror_set *ms, struct bio *bio)
  {
1f965b194   Heinz Mauelshagen   dm raid1: separat...
410
411
  	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  	region_t region = dm_rh_bio_to_region(ms->rh, bio);
06386bbfd   Jonathan Brassow   dm raid1: handle ...
412

1f965b194   Heinz Mauelshagen   dm raid1: separat...
413
  	if (log->type->in_sync(log, region, 0))
4f024f379   Kent Overstreet   block: Abstract o...
414
  		return choose_mirror(ms,  bio->bi_iter.bi_sector) ? 1 : 0;
06386bbfd   Jonathan Brassow   dm raid1: handle ...
415
416
  
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
417
418
419
420
421
  }
  
  /*
   * remap a buffer to a particular mirror.
   */
06386bbfd   Jonathan Brassow   dm raid1: handle ...
422
423
  static sector_t map_sector(struct mirror *m, struct bio *bio)
  {
4f024f379   Kent Overstreet   block: Abstract o...
424
  	if (unlikely(!bio->bi_iter.bi_size))
4184153f9   Mikulas Patocka   dm raid1: support...
425
  		return 0;
4f024f379   Kent Overstreet   block: Abstract o...
426
  	return m->offset + dm_target_offset(m->ms->ti, bio->bi_iter.bi_sector);
06386bbfd   Jonathan Brassow   dm raid1: handle ...
427
428
429
  }
  
  static void map_bio(struct mirror *m, struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
430
  {
74d46992e   Christoph Hellwig   block: replace bi...
431
  	bio_set_dev(bio, m->dev->bdev);
4f024f379   Kent Overstreet   block: Abstract o...
432
  	bio->bi_iter.bi_sector = map_sector(m, bio);
06386bbfd   Jonathan Brassow   dm raid1: handle ...
433
  }
22a1ceb1e   Heinz Mauelshagen   dm io: clean inte...
434
  static void map_region(struct dm_io_region *io, struct mirror *m,
06386bbfd   Jonathan Brassow   dm raid1: handle ...
435
436
437
438
  		       struct bio *bio)
  {
  	io->bdev = m->dev->bdev;
  	io->sector = map_sector(m, bio);
aa8b57aa3   Kent Overstreet   block: Use bio_se...
439
  	io->count = bio_sectors(bio);
06386bbfd   Jonathan Brassow   dm raid1: handle ...
440
  }
047885076   Mikulas Patocka   dm raid1: add fra...
441
442
443
  static void hold_bio(struct mirror_set *ms, struct bio *bio)
  {
  	/*
f07030409   Takahiro Yasui   dm raid1: fix dea...
444
445
  	 * Lock is required to avoid race condition during suspend
  	 * process.
047885076   Mikulas Patocka   dm raid1: add fra...
446
  	 */
f07030409   Takahiro Yasui   dm raid1: fix dea...
447
  	spin_lock_irq(&ms->lock);
047885076   Mikulas Patocka   dm raid1: add fra...
448
  	if (atomic_read(&ms->suspend)) {
f07030409   Takahiro Yasui   dm raid1: fix dea...
449
450
451
452
453
  		spin_unlock_irq(&ms->lock);
  
  		/*
  		 * If device is suspended, complete the bio.
  		 */
047885076   Mikulas Patocka   dm raid1: add fra...
454
  		if (dm_noflush_suspending(ms->ti))
4e4cbee93   Christoph Hellwig   block: switch bio...
455
  			bio->bi_status = BLK_STS_DM_REQUEUE;
047885076   Mikulas Patocka   dm raid1: add fra...
456
  		else
4e4cbee93   Christoph Hellwig   block: switch bio...
457
  			bio->bi_status = BLK_STS_IOERR;
4246a0b63   Christoph Hellwig   block: add a bi_e...
458
459
  
  		bio_endio(bio);
047885076   Mikulas Patocka   dm raid1: add fra...
460
461
462
463
464
465
  		return;
  	}
  
  	/*
  	 * Hold bio until the suspend is complete.
  	 */
047885076   Mikulas Patocka   dm raid1: add fra...
466
467
468
  	bio_list_add(&ms->holds, bio);
  	spin_unlock_irq(&ms->lock);
  }
06386bbfd   Jonathan Brassow   dm raid1: handle ...
469
470
471
472
473
474
475
476
477
478
479
480
  /*-----------------------------------------------------------------
   * Reads
   *---------------------------------------------------------------*/
  static void read_callback(unsigned long error, void *context)
  {
  	struct bio *bio = context;
  	struct mirror *m;
  
  	m = bio_get_m(bio);
  	bio_set_m(bio, NULL);
  
  	if (likely(!error)) {
4246a0b63   Christoph Hellwig   block: add a bi_e...
481
  		bio_endio(bio);
06386bbfd   Jonathan Brassow   dm raid1: handle ...
482
483
484
485
486
487
488
489
490
  		return;
  	}
  
  	fail_mirror(m, DM_RAID1_READ_ERROR);
  
  	if (likely(default_ok(m)) || mirror_available(m->ms, bio)) {
  		DMWARN_LIMIT("Read failure on mirror device %s.  "
  			     "Trying alternative device.",
  			     m->dev->name);
70246286e   Christoph Hellwig   block: get rid of...
491
  		queue_bio(m->ms, bio, bio_data_dir(bio));
06386bbfd   Jonathan Brassow   dm raid1: handle ...
492
493
494
495
496
  		return;
  	}
  
  	DMERR_LIMIT("Read failure on mirror device %s.  Failing I/O.",
  		    m->dev->name);
4246a0b63   Christoph Hellwig   block: add a bi_e...
497
  	bio_io_error(bio);
06386bbfd   Jonathan Brassow   dm raid1: handle ...
498
499
500
501
502
  }
  
  /* Asynchronous read. */
  static void read_async_bio(struct mirror *m, struct bio *bio)
  {
22a1ceb1e   Heinz Mauelshagen   dm io: clean inte...
503
  	struct dm_io_region io;
06386bbfd   Jonathan Brassow   dm raid1: handle ...
504
  	struct dm_io_request io_req = {
e6047149d   Mike Christie   dm: use bio op ac...
505
506
  		.bi_op = REQ_OP_READ,
  		.bi_op_flags = 0,
003b5c571   Kent Overstreet   block: Convert dr...
507
508
  		.mem.type = DM_IO_BIO,
  		.mem.ptr.bio = bio,
06386bbfd   Jonathan Brassow   dm raid1: handle ...
509
510
511
512
513
514
515
  		.notify.fn = read_callback,
  		.notify.context = bio,
  		.client = m->ms->io_client,
  	};
  
  	map_region(&io, m, bio);
  	bio_set_m(bio, m);
1f965b194   Heinz Mauelshagen   dm raid1: separat...
516
517
518
519
520
521
522
523
  	BUG_ON(dm_io(&io_req, 1, &io, NULL));
  }
  
  static inline int region_in_sync(struct mirror_set *ms, region_t region,
  				 int may_block)
  {
  	int state = dm_rh_get_state(ms->rh, region, may_block);
  	return state == DM_RH_CLEAN || state == DM_RH_DIRTY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
524
525
526
527
528
529
530
531
532
  }
  
  static void do_reads(struct mirror_set *ms, struct bio_list *reads)
  {
  	region_t region;
  	struct bio *bio;
  	struct mirror *m;
  
  	while ((bio = bio_list_pop(reads))) {
1f965b194   Heinz Mauelshagen   dm raid1: separat...
533
  		region = dm_rh_bio_to_region(ms->rh, bio);
06386bbfd   Jonathan Brassow   dm raid1: handle ...
534
  		m = get_default_mirror(ms);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
535
536
537
538
  
  		/*
  		 * We can only read balance if the region is in sync.
  		 */
1f965b194   Heinz Mauelshagen   dm raid1: separat...
539
  		if (likely(region_in_sync(ms, region, 1)))
4f024f379   Kent Overstreet   block: Abstract o...
540
  			m = choose_mirror(ms, bio->bi_iter.bi_sector);
06386bbfd   Jonathan Brassow   dm raid1: handle ...
541
542
  		else if (m && atomic_read(&m->error_count))
  			m = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
543

06386bbfd   Jonathan Brassow   dm raid1: handle ...
544
545
546
  		if (likely(m))
  			read_async_bio(m, bio);
  		else
4246a0b63   Christoph Hellwig   block: add a bi_e...
547
  			bio_io_error(bio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548
549
550
551
552
553
554
555
556
557
558
559
560
  	}
  }
  
  /*-----------------------------------------------------------------
   * Writes.
   *
   * We do different things with the write io depending on the
   * state of the region that it's in:
   *
   * SYNC: 	increment pending, use kcopyd to write to *all* mirrors
   * RECOVERING:	delay the io until recovery completes
   * NOSYNC:	increment pending, just write to the default mirror
   *---------------------------------------------------------------*/
72f4b3141   Jonathan Brassow   dm raid1: handle ...
561

72f4b3141   Jonathan Brassow   dm raid1: handle ...
562

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
563
564
  static void write_callback(unsigned long error, void *context)
  {
4246a0b63   Christoph Hellwig   block: add a bi_e...
565
  	unsigned i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
567
  	struct bio *bio = (struct bio *) context;
  	struct mirror_set *ms;
72f4b3141   Jonathan Brassow   dm raid1: handle ...
568
569
  	int should_wake = 0;
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
570

06386bbfd   Jonathan Brassow   dm raid1: handle ...
571
572
  	ms = bio_get_m(bio)->ms;
  	bio_set_m(bio, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
573
574
575
576
577
578
579
  
  	/*
  	 * NOTE: We don't decrement the pending count here,
  	 * instead it is done by the targets endio function.
  	 * This way we handle both writes to SYNC and NOSYNC
  	 * regions with the same code.
  	 */
60f355ead   Mikulas Patocka   dm raid1: hold wr...
580
  	if (likely(!error)) {
4246a0b63   Christoph Hellwig   block: add a bi_e...
581
  		bio_endio(bio);
60f355ead   Mikulas Patocka   dm raid1: hold wr...
582
583
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
584

f2ed51ac6   Mikulas Patocka   dm mirror: do not...
585
586
587
588
  	/*
  	 * If the bio is discard, return an error, but do not
  	 * degrade the array.
  	 */
e6047149d   Mike Christie   dm: use bio op ac...
589
  	if (bio_op(bio) == REQ_OP_DISCARD) {
4e4cbee93   Christoph Hellwig   block: switch bio...
590
  		bio->bi_status = BLK_STS_NOTSUPP;
4246a0b63   Christoph Hellwig   block: add a bi_e...
591
  		bio_endio(bio);
f2ed51ac6   Mikulas Patocka   dm mirror: do not...
592
593
  		return;
  	}
72f4b3141   Jonathan Brassow   dm raid1: handle ...
594
595
596
  	for (i = 0; i < ms->nr_mirrors; i++)
  		if (test_bit(i, &error))
  			fail_mirror(ms->mirror + i, DM_RAID1_WRITE_ERROR);
72f4b3141   Jonathan Brassow   dm raid1: handle ...
597

60f355ead   Mikulas Patocka   dm raid1: hold wr...
598
599
600
601
602
603
604
605
606
607
608
609
  	/*
  	 * Need to raise event.  Since raising
  	 * events can block, we need to do it in
  	 * the main thread.
  	 */
  	spin_lock_irqsave(&ms->lock, flags);
  	if (!ms->failures.head)
  		should_wake = 1;
  	bio_list_add(&ms->failures, bio);
  	spin_unlock_irqrestore(&ms->lock, flags);
  	if (should_wake)
  		wakeup_mirrord(ms);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610
611
612
613
614
  }
  
  static void do_write(struct mirror_set *ms, struct bio *bio)
  {
  	unsigned int i;
65972a6fa   Kees Cook   dm mirror: remove...
615
  	struct dm_io_region io[MAX_NR_MIRRORS], *dest = io;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
616
  	struct mirror *m;
88be163ab   Milan Broz   dm raid1: update ...
617
  	struct dm_io_request io_req = {
e6047149d   Mike Christie   dm: use bio op ac...
618
  		.bi_op = REQ_OP_WRITE,
70fd76140   Christoph Hellwig   block,fs: use REQ...
619
  		.bi_op_flags = bio->bi_opf & (REQ_FUA | REQ_PREFLUSH),
003b5c571   Kent Overstreet   block: Convert dr...
620
621
  		.mem.type = DM_IO_BIO,
  		.mem.ptr.bio = bio,
88be163ab   Milan Broz   dm raid1: update ...
622
623
624
625
  		.notify.fn = write_callback,
  		.notify.context = bio,
  		.client = ms->io_client,
  	};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
626

e6047149d   Mike Christie   dm: use bio op ac...
627
628
  	if (bio_op(bio) == REQ_OP_DISCARD) {
  		io_req.bi_op = REQ_OP_DISCARD;
5fc2ffeab   Mike Snitzer   dm raid1: support...
629
630
631
  		io_req.mem.type = DM_IO_KMEM;
  		io_req.mem.ptr.addr = NULL;
  	}
06386bbfd   Jonathan Brassow   dm raid1: handle ...
632
633
  	for (i = 0, m = ms->mirror; i < ms->nr_mirrors; i++, m++)
  		map_region(dest++, m, bio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634

06386bbfd   Jonathan Brassow   dm raid1: handle ...
635
636
637
638
639
  	/*
  	 * Use default mirror because we only need it to retrieve the reference
  	 * to the mirror set in write_callback().
  	 */
  	bio_set_m(bio, get_default_mirror(ms));
88be163ab   Milan Broz   dm raid1: update ...
640

1f965b194   Heinz Mauelshagen   dm raid1: separat...
641
  	BUG_ON(dm_io(&io_req, ms->nr_mirrors, io, NULL));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
642
643
644
645
646
647
648
  }
  
  static void do_writes(struct mirror_set *ms, struct bio_list *writes)
  {
  	int state;
  	struct bio *bio;
  	struct bio_list sync, nosync, recover, *this_list = NULL;
7513c2a76   Jonathan Brassow   dm raid1: add is_...
649
650
651
  	struct bio_list requeue;
  	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
  	region_t region;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
652
653
654
655
656
657
658
659
660
661
  
  	if (!writes->head)
  		return;
  
  	/*
  	 * Classify each write.
  	 */
  	bio_list_init(&sync);
  	bio_list_init(&nosync);
  	bio_list_init(&recover);
7513c2a76   Jonathan Brassow   dm raid1: add is_...
662
  	bio_list_init(&requeue);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
663
664
  
  	while ((bio = bio_list_pop(writes))) {
1eff9d322   Jens Axboe   block: rename bio...
665
  		if ((bio->bi_opf & REQ_PREFLUSH) ||
e6047149d   Mike Christie   dm: use bio op ac...
666
  		    (bio_op(bio) == REQ_OP_DISCARD)) {
4184153f9   Mikulas Patocka   dm raid1: support...
667
668
669
  			bio_list_add(&sync, bio);
  			continue;
  		}
7513c2a76   Jonathan Brassow   dm raid1: add is_...
670
671
672
673
674
675
676
677
678
  		region = dm_rh_bio_to_region(ms->rh, bio);
  
  		if (log->type->is_remote_recovering &&
  		    log->type->is_remote_recovering(log, region)) {
  			bio_list_add(&requeue, bio);
  			continue;
  		}
  
  		state = dm_rh_get_state(ms->rh, region, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
679
  		switch (state) {
1f965b194   Heinz Mauelshagen   dm raid1: separat...
680
681
  		case DM_RH_CLEAN:
  		case DM_RH_DIRTY:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
682
683
  			this_list = &sync;
  			break;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
684
  		case DM_RH_NOSYNC:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
685
686
  			this_list = &nosync;
  			break;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
687
  		case DM_RH_RECOVERING:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
689
690
691
692
693
694
695
  			this_list = &recover;
  			break;
  		}
  
  		bio_list_add(this_list, bio);
  	}
  
  	/*
7513c2a76   Jonathan Brassow   dm raid1: add is_...
696
697
698
699
700
701
702
  	 * Add bios that are delayed due to remote recovery
  	 * back on to the write queue
  	 */
  	if (unlikely(requeue.head)) {
  		spin_lock_irq(&ms->lock);
  		bio_list_merge(&ms->writes, &requeue);
  		spin_unlock_irq(&ms->lock);
69885683d   Mikulas Patocka   dm raid1: wake km...
703
  		delayed_wake(ms);
7513c2a76   Jonathan Brassow   dm raid1: add is_...
704
705
706
  	}
  
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
707
708
709
710
  	 * Increment the pending counts for any regions that will
  	 * be written to (writes to recover regions are going to
  	 * be delayed).
  	 */
1f965b194   Heinz Mauelshagen   dm raid1: separat...
711
712
  	dm_rh_inc_pending(ms->rh, &sync);
  	dm_rh_inc_pending(ms->rh, &nosync);
d2b698644   Jonathan Brassow   dm raid1: do not ...
713
714
715
716
717
718
719
  
  	/*
  	 * If the flush fails on a previous call and succeeds here,
  	 * we must not reset the log_failure variable.  We need
  	 * userspace interaction to do that.
  	 */
  	ms->log_failure = dm_rh_flush(ms->rh) ? 1 : ms->log_failure;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
720
721
722
723
  
  	/*
  	 * Dispatch io.
  	 */
5528d17de   Mikulas Patocka   dm raid1: fail wr...
724
  	if (unlikely(ms->log_failure) && errors_handled(ms)) {
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
725
726
727
  		spin_lock_irq(&ms->lock);
  		bio_list_merge(&ms->failures, &sync);
  		spin_unlock_irq(&ms->lock);
1f965b194   Heinz Mauelshagen   dm raid1: separat...
728
  		wakeup_mirrord(ms);
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
729
  	} else
fc1ff9588   Jonathan Brassow   dm raid1: handle ...
730
  		while ((bio = bio_list_pop(&sync)))
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
731
  			do_write(ms, bio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
732
733
  
  	while ((bio = bio_list_pop(&recover)))
1f965b194   Heinz Mauelshagen   dm raid1: separat...
734
  		dm_rh_delay(ms->rh, bio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
735
736
  
  	while ((bio = bio_list_pop(&nosync))) {
ed63287dd   Lidong Zhong   dm raid1: keep is...
737
  		if (unlikely(ms->leg_failure) && errors_handled(ms) && !keep_log(ms)) {
ede5ea0b8   Mikulas Patocka   dm raid1: always ...
738
739
740
741
742
  			spin_lock_irq(&ms->lock);
  			bio_list_add(&ms->failures, bio);
  			spin_unlock_irq(&ms->lock);
  			wakeup_mirrord(ms);
  		} else {
929be8fcb   Mikulas Patocka   dm raid1: hold al...
743
  			map_bio(get_default_mirror(ms), bio);
ed00aabd5   Christoph Hellwig   block: rename gen...
744
  			submit_bio_noacct(bio);
929be8fcb   Mikulas Patocka   dm raid1: hold al...
745
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
746
747
  	}
  }
72f4b3141   Jonathan Brassow   dm raid1: handle ...
748
749
750
  static void do_failures(struct mirror_set *ms, struct bio_list *failures)
  {
  	struct bio *bio;
0f398a840   Mikulas Patocka   dm raid1: use hol...
751
  	if (likely(!failures->head))
72f4b3141   Jonathan Brassow   dm raid1: handle ...
752
  		return;
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
753
754
  	/*
  	 * If the log has failed, unattempted writes are being
0f398a840   Mikulas Patocka   dm raid1: use hol...
755
  	 * put on the holds list.  We can't issue those writes
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
756
757
758
759
760
761
762
763
764
765
766
767
768
769
  	 * until a log has been marked, so we must store them.
  	 *
  	 * If a 'noflush' suspend is in progress, we can requeue
  	 * the I/O's to the core.  This give userspace a chance
  	 * to reconfigure the mirror, at which point the core
  	 * will reissue the writes.  If the 'noflush' flag is
  	 * not set, we have no choice but to return errors.
  	 *
  	 * Some writes on the failures list may have been
  	 * submitted before the log failure and represent a
  	 * failure to write to one of the devices.  It is ok
  	 * for us to treat them the same and requeue them
  	 * as well.
  	 */
0f398a840   Mikulas Patocka   dm raid1: use hol...
770
  	while ((bio = bio_list_pop(failures))) {
60f355ead   Mikulas Patocka   dm raid1: hold wr...
771
  		if (!ms->log_failure) {
0f398a840   Mikulas Patocka   dm raid1: use hol...
772
  			ms->in_sync = 0;
c58098be9   Mikulas Patocka   dm raid1: remove ...
773
  			dm_rh_mark_nosync(ms->rh, bio);
0f398a840   Mikulas Patocka   dm raid1: use hol...
774
  		}
60f355ead   Mikulas Patocka   dm raid1: hold wr...
775
776
777
  
  		/*
  		 * If all the legs are dead, fail the I/O.
ed63287dd   Lidong Zhong   dm raid1: keep is...
778
779
780
781
782
783
784
  		 * If the device has failed and keep_log is enabled,
  		 * fail the I/O.
  		 *
  		 * If we have been told to handle errors, and keep_log
  		 * isn't enabled, hold the bio and wait for userspace to
  		 * deal with the problem.
  		 *
60f355ead   Mikulas Patocka   dm raid1: hold wr...
785
786
787
788
  		 * Otherwise pretend that the I/O succeeded. (This would
  		 * be wrong if the failed leg returned after reboot and
  		 * got replicated back to the good legs.)
  		 */
ed63287dd   Lidong Zhong   dm raid1: keep is...
789
  		if (unlikely(!get_valid_mirror(ms) || (keep_log(ms) && ms->log_failure)))
4246a0b63   Christoph Hellwig   block: add a bi_e...
790
  			bio_io_error(bio);
ed63287dd   Lidong Zhong   dm raid1: keep is...
791
  		else if (errors_handled(ms) && !keep_log(ms))
60f355ead   Mikulas Patocka   dm raid1: hold wr...
792
793
  			hold_bio(ms, bio);
  		else
4246a0b63   Christoph Hellwig   block: add a bi_e...
794
  			bio_endio(bio);
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
795
  	}
72f4b3141   Jonathan Brassow   dm raid1: handle ...
796
797
798
799
800
801
802
803
804
  }
  
  static void trigger_event(struct work_struct *work)
  {
  	struct mirror_set *ms =
  		container_of(work, struct mirror_set, trigger_event);
  
  	dm_table_event(ms->ti->table);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
805
806
807
  /*-----------------------------------------------------------------
   * kmirrord
   *---------------------------------------------------------------*/
a2aebe03b   Mikulas Patocka   dm raid1: use timer
808
  static void do_mirror(struct work_struct *work)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
809
  {
1f965b194   Heinz Mauelshagen   dm raid1: separat...
810
811
  	struct mirror_set *ms = container_of(work, struct mirror_set,
  					     kmirrord_work);
72f4b3141   Jonathan Brassow   dm raid1: handle ...
812
813
  	struct bio_list reads, writes, failures;
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
814

72f4b3141   Jonathan Brassow   dm raid1: handle ...
815
  	spin_lock_irqsave(&ms->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
816
817
  	reads = ms->reads;
  	writes = ms->writes;
72f4b3141   Jonathan Brassow   dm raid1: handle ...
818
  	failures = ms->failures;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
819
820
  	bio_list_init(&ms->reads);
  	bio_list_init(&ms->writes);
72f4b3141   Jonathan Brassow   dm raid1: handle ...
821
822
  	bio_list_init(&ms->failures);
  	spin_unlock_irqrestore(&ms->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823

1f965b194   Heinz Mauelshagen   dm raid1: separat...
824
  	dm_rh_update_states(ms->rh, errors_handled(ms));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
825
826
827
  	do_recovery(ms);
  	do_reads(ms, &reads);
  	do_writes(ms, &writes);
72f4b3141   Jonathan Brassow   dm raid1: handle ...
828
  	do_failures(ms, &failures);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
829
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
830
831
832
833
834
835
  /*-----------------------------------------------------------------
   * Target functions
   *---------------------------------------------------------------*/
  static struct mirror_set *alloc_context(unsigned int nr_mirrors,
  					uint32_t region_size,
  					struct dm_target *ti,
416cd17b1   Heinz Mauelshagen   dm log: clean int...
836
  					struct dm_dirty_log *dl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
837
  {
bcd676542   Gustavo A. R. Silva   dm raid1: use str...
838
839
  	struct mirror_set *ms =
  		kzalloc(struct_size(ms, mirror, nr_mirrors), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
840

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
841
  	if (!ms) {
72d948616   Alasdair G Kergon   [PATCH] dm: impro...
842
  		ti->error = "Cannot allocate mirror context";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
843
844
  		return NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845
  	spin_lock_init(&ms->lock);
5339fc2d4   Mikulas Patocka   dm raid1: explici...
846
847
848
849
  	bio_list_init(&ms->reads);
  	bio_list_init(&ms->writes);
  	bio_list_init(&ms->failures);
  	bio_list_init(&ms->holds);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
851
852
853
854
  
  	ms->ti = ti;
  	ms->nr_mirrors = nr_mirrors;
  	ms->nr_regions = dm_sector_div_up(ti->len, region_size);
  	ms->in_sync = 0;
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
855
  	ms->log_failure = 0;
929be8fcb   Mikulas Patocka   dm raid1: hold al...
856
  	ms->leg_failure = 0;
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
857
  	atomic_set(&ms->suspend, 0);
72f4b3141   Jonathan Brassow   dm raid1: handle ...
858
  	atomic_set(&ms->default_mirror, DEFAULT_MIRROR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
859

bda8efec5   Mikulas Patocka   dm io: use fixed ...
860
  	ms->io_client = dm_io_client_create();
88be163ab   Milan Broz   dm raid1: update ...
861
862
863
864
865
  	if (IS_ERR(ms->io_client)) {
  		ti->error = "Error creating dm_io client";
  		kfree(ms);
   		return NULL;
  	}
1f965b194   Heinz Mauelshagen   dm raid1: separat...
866
867
868
869
870
  	ms->rh = dm_region_hash_create(ms, dispatch_bios, wakeup_mirrord,
  				       wakeup_all_recovery_waiters,
  				       ms->ti->begin, MAX_RECOVERY,
  				       dl, region_size, ms->nr_regions);
  	if (IS_ERR(ms->rh)) {
72d948616   Alasdair G Kergon   [PATCH] dm: impro...
871
  		ti->error = "Error creating dirty region hash";
a72cf737e   Dmitry Monakhov   dm raid1: fix lea...
872
  		dm_io_client_destroy(ms->io_client);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
873
874
875
876
877
878
879
880
881
882
883
884
  		kfree(ms);
  		return NULL;
  	}
  
  	return ms;
  }
  
  static void free_context(struct mirror_set *ms, struct dm_target *ti,
  			 unsigned int m)
  {
  	while (m--)
  		dm_put_device(ti, ms->mirror[m].dev);
88be163ab   Milan Broz   dm raid1: update ...
885
  	dm_io_client_destroy(ms->io_client);
1f965b194   Heinz Mauelshagen   dm raid1: separat...
886
  	dm_region_hash_destroy(ms->rh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
887
888
  	kfree(ms);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
889
890
891
  static int get_mirror(struct mirror_set *ms, struct dm_target *ti,
  		      unsigned int mirror, char **argv)
  {
4ee218cd6   Andrew Morton   [PATCH] dm: remov...
892
  	unsigned long long offset;
31998ef19   Mikulas Patocka   dm: reject traili...
893
  	char dummy;
e80d1c805   Vivek Goyal   dm: do not overri...
894
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
895

ef87bfc24   Milan Broz   dm: Check for dev...
896
897
  	if (sscanf(argv[1], "%llu%c", &offset, &dummy) != 1 ||
  	    offset != (sector_t)offset) {
72d948616   Alasdair G Kergon   [PATCH] dm: impro...
898
  		ti->error = "Invalid offset";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
899
900
  		return -EINVAL;
  	}
e80d1c805   Vivek Goyal   dm: do not overri...
901
902
903
  	ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
  			    &ms->mirror[mirror].dev);
  	if (ret) {
72d948616   Alasdair G Kergon   [PATCH] dm: impro...
904
  		ti->error = "Device lookup failure";
e80d1c805   Vivek Goyal   dm: do not overri...
905
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
906
  	}
aa5617c55   Jonathan Brassow   dm raid1: add mir...
907
  	ms->mirror[mirror].ms = ms;
72f4b3141   Jonathan Brassow   dm raid1: handle ...
908
909
  	atomic_set(&(ms->mirror[mirror].error_count), 0);
  	ms->mirror[mirror].error_type = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
911
912
913
  	ms->mirror[mirror].offset = offset;
  
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
914
915
916
  /*
   * Create dirty log: log_type #log_params <log_params>
   */
416cd17b1   Heinz Mauelshagen   dm log: clean int...
917
  static struct dm_dirty_log *create_dirty_log(struct dm_target *ti,
1f965b194   Heinz Mauelshagen   dm raid1: separat...
918
919
  					     unsigned argc, char **argv,
  					     unsigned *args_used)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
  {
1f965b194   Heinz Mauelshagen   dm raid1: separat...
921
  	unsigned param_count;
416cd17b1   Heinz Mauelshagen   dm log: clean int...
922
  	struct dm_dirty_log *dl;
31998ef19   Mikulas Patocka   dm: reject traili...
923
  	char dummy;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
  
  	if (argc < 2) {
72d948616   Alasdair G Kergon   [PATCH] dm: impro...
926
  		ti->error = "Insufficient mirror log arguments";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
927
928
  		return NULL;
  	}
31998ef19   Mikulas Patocka   dm: reject traili...
929
  	if (sscanf(argv[1], "%u%c", &param_count, &dummy) != 1) {
72d948616   Alasdair G Kergon   [PATCH] dm: impro...
930
  		ti->error = "Invalid mirror log argument count";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
931
932
933
934
935
936
  		return NULL;
  	}
  
  	*args_used = 2 + param_count;
  
  	if (argc < *args_used) {
72d948616   Alasdair G Kergon   [PATCH] dm: impro...
937
  		ti->error = "Insufficient mirror log arguments";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
938
939
  		return NULL;
  	}
c0da3748b   Mikulas Patocka   dm raid1: impleme...
940
941
  	dl = dm_dirty_log_create(argv[0], ti, mirror_flush, param_count,
  				 argv + 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
942
  	if (!dl) {
72d948616   Alasdair G Kergon   [PATCH] dm: impro...
943
  		ti->error = "Error creating mirror dirty log";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
944
945
  		return NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
946
947
  	return dl;
  }
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
948
949
950
951
952
  static int parse_features(struct mirror_set *ms, unsigned argc, char **argv,
  			  unsigned *args_used)
  {
  	unsigned num_features;
  	struct dm_target *ti = ms->ti;
31998ef19   Mikulas Patocka   dm: reject traili...
953
  	char dummy;
ed63287dd   Lidong Zhong   dm raid1: keep is...
954
  	int i;
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
955
956
957
958
959
  
  	*args_used = 0;
  
  	if (!argc)
  		return 0;
31998ef19   Mikulas Patocka   dm: reject traili...
960
  	if (sscanf(argv[0], "%u%c", &num_features, &dummy) != 1) {
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
961
962
963
964
965
966
967
968
969
970
971
972
  		ti->error = "Invalid number of features";
  		return -EINVAL;
  	}
  
  	argc--;
  	argv++;
  	(*args_used)++;
  
  	if (num_features > argc) {
  		ti->error = "Not enough arguments to support feature count";
  		return -EINVAL;
  	}
ed63287dd   Lidong Zhong   dm raid1: keep is...
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
  	for (i = 0; i < num_features; i++) {
  		if (!strcmp("handle_errors", argv[0]))
  			ms->features |= DM_RAID1_HANDLE_ERRORS;
  		else if (!strcmp("keep_log", argv[0]))
  			ms->features |= DM_RAID1_KEEP_LOG;
  		else {
  			ti->error = "Unrecognised feature requested";
  			return -EINVAL;
  		}
  
  		argc--;
  		argv++;
  		(*args_used)++;
  	}
  	if (!errors_handled(ms) && keep_log(ms)) {
  		ti->error = "keep_log feature requires the handle_errors feature";
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
989
990
  		return -EINVAL;
  	}
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
991
992
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
993
994
995
996
997
  /*
   * Construct a mirror mapping:
   *
   * log_type #log_params <log_params>
   * #mirrors [mirror_path offset]{2,}
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
998
   * [#features <features>]
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
999
1000
1001
   *
   * log_type is "core" or "disk"
   * #log_params is between 1 and 3
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
1002
   *
ed63287dd   Lidong Zhong   dm raid1: keep is...
1003
   * If present, supported features are "handle_errors" and "keep_log".
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1004
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1005
1006
1007
1008
1009
  static int mirror_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  {
  	int r;
  	unsigned int nr_mirrors, m, args_used;
  	struct mirror_set *ms;
416cd17b1   Heinz Mauelshagen   dm log: clean int...
1010
  	struct dm_dirty_log *dl;
31998ef19   Mikulas Patocka   dm: reject traili...
1011
  	char dummy;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1012
1013
1014
1015
1016
1017
1018
  
  	dl = create_dirty_log(ti, argc, argv, &args_used);
  	if (!dl)
  		return -EINVAL;
  
  	argv += args_used;
  	argc -= args_used;
31998ef19   Mikulas Patocka   dm: reject traili...
1019
  	if (!argc || sscanf(argv[0], "%u%c", &nr_mirrors, &dummy) != 1 ||
65972a6fa   Kees Cook   dm mirror: remove...
1020
  	    nr_mirrors < 2 || nr_mirrors > MAX_NR_MIRRORS) {
72d948616   Alasdair G Kergon   [PATCH] dm: impro...
1021
  		ti->error = "Invalid number of mirrors";
416cd17b1   Heinz Mauelshagen   dm log: clean int...
1022
  		dm_dirty_log_destroy(dl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1023
1024
1025
1026
  		return -EINVAL;
  	}
  
  	argv++, argc--;
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
1027
1028
  	if (argc < nr_mirrors * 2) {
  		ti->error = "Too few mirror arguments";
416cd17b1   Heinz Mauelshagen   dm log: clean int...
1029
  		dm_dirty_log_destroy(dl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1030
1031
1032
1033
1034
  		return -EINVAL;
  	}
  
  	ms = alloc_context(nr_mirrors, dl->type->get_region_size(dl), ti, dl);
  	if (!ms) {
416cd17b1   Heinz Mauelshagen   dm log: clean int...
1035
  		dm_dirty_log_destroy(dl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
  		return -ENOMEM;
  	}
  
  	/* Get the mirror parameter sets */
  	for (m = 0; m < nr_mirrors; m++) {
  		r = get_mirror(ms, ti, m, argv);
  		if (r) {
  			free_context(ms, ti, m);
  			return r;
  		}
  		argv += 2;
  		argc -= 2;
  	}
  
  	ti->private = ms;
542f90381   Mike Snitzer   dm: support non p...
1051
1052
1053
1054
  
  	r = dm_set_target_max_io_len(ti, dm_rh_get_region_size(ms->rh));
  	if (r)
  		goto err_free_context;
55a62eef8   Alasdair G Kergon   dm: rename reques...
1055
1056
  	ti->num_flush_bios = 1;
  	ti->num_discard_bios = 1;
30187e1d4   Mike Snitzer   dm: rename target...
1057
  	ti->per_io_data_size = sizeof(struct dm_raid1_bio_record);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1058

670368a8d   Tejun Heo   dm: stop using WQ...
1059
  	ms->kmirrord_wq = alloc_workqueue("kmirrord", WQ_MEM_RECLAIM, 0);
6ad36fe2b   Holger Smolinski   dm raid1: one kmi...
1060
1061
  	if (!ms->kmirrord_wq) {
  		DMERR("couldn't start kmirrord");
a72cf737e   Dmitry Monakhov   dm raid1: fix lea...
1062
1063
  		r = -ENOMEM;
  		goto err_free_context;
6ad36fe2b   Holger Smolinski   dm raid1: one kmi...
1064
1065
  	}
  	INIT_WORK(&ms->kmirrord_work, do_mirror);
8376d3c1f   Kees Cook   md: Convert timer...
1066
  	timer_setup(&ms->timer, delayed_wake_fn, 0);
a2aebe03b   Mikulas Patocka   dm raid1: use timer
1067
  	ms->timer_pending = 0;
72f4b3141   Jonathan Brassow   dm raid1: handle ...
1068
  	INIT_WORK(&ms->trigger_event, trigger_event);
6ad36fe2b   Holger Smolinski   dm raid1: one kmi...
1069

a8e6afa23   Jonathan E Brassow   dm raid1: add han...
1070
  	r = parse_features(ms, argc, argv, &args_used);
a72cf737e   Dmitry Monakhov   dm raid1: fix lea...
1071
1072
  	if (r)
  		goto err_destroy_wq;
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
1073
1074
1075
  
  	argv += args_used;
  	argc -= args_used;
f44db678e   Jonathan Brassow   dm raid1: handle ...
1076
1077
1078
1079
1080
1081
1082
1083
  	/*
  	 * Any read-balancing addition depends on the
  	 * DM_RAID1_HANDLE_ERRORS flag being present.
  	 * This is because the decision to balance depends
  	 * on the sync state of a region.  If the above
  	 * flag is not present, we ignore errors; and
  	 * the sync state may be inaccurate.
  	 */
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
1084
1085
  	if (argc) {
  		ti->error = "Too many mirror arguments";
a72cf737e   Dmitry Monakhov   dm raid1: fix lea...
1086
1087
  		r = -EINVAL;
  		goto err_destroy_wq;
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
1088
  	}
df5d2e908   Mikulas Patocka   dm kcopyd: introd...
1089
  	ms->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
fa34ce730   Mikulas Patocka   dm kcopyd: return...
1090
1091
  	if (IS_ERR(ms->kcopyd_client)) {
  		r = PTR_ERR(ms->kcopyd_client);
a72cf737e   Dmitry Monakhov   dm raid1: fix lea...
1092
  		goto err_destroy_wq;
fa34ce730   Mikulas Patocka   dm kcopyd: return...
1093
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1094

1f965b194   Heinz Mauelshagen   dm raid1: separat...
1095
  	wakeup_mirrord(ms);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1096
  	return 0;
a72cf737e   Dmitry Monakhov   dm raid1: fix lea...
1097
1098
1099
1100
1101
1102
  
  err_destroy_wq:
  	destroy_workqueue(ms->kmirrord_wq);
  err_free_context:
  	free_context(ms, ti, ms->nr_mirrors);
  	return r;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1103
1104
1105
1106
1107
  }
  
  static void mirror_dtr(struct dm_target *ti)
  {
  	struct mirror_set *ms = (struct mirror_set *) ti->private;
a2aebe03b   Mikulas Patocka   dm raid1: use timer
1108
  	del_timer_sync(&ms->timer);
6ad36fe2b   Holger Smolinski   dm raid1: one kmi...
1109
  	flush_workqueue(ms->kmirrord_wq);
43829731d   Tejun Heo   workqueue: deprec...
1110
  	flush_work(&ms->trigger_event);
eb69aca5d   Heinz Mauelshagen   dm kcopyd: clean ...
1111
  	dm_kcopyd_client_destroy(ms->kcopyd_client);
6ad36fe2b   Holger Smolinski   dm raid1: one kmi...
1112
  	destroy_workqueue(ms->kmirrord_wq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1113
1114
  	free_context(ms, ti, ms->nr_mirrors);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1115
1116
1117
  /*
   * Mirror mapping function
   */
7de3ee57d   Mikulas Patocka   dm: remove map_info
1118
  static int mirror_map(struct dm_target *ti, struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1119
  {
70246286e   Christoph Hellwig   block: get rid of...
1120
  	int r, rw = bio_data_dir(bio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1121
1122
  	struct mirror *m;
  	struct mirror_set *ms = ti->private;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
1123
  	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
0045d61b5   Mikulas Patocka   dm raid1: dont us...
1124
1125
  	struct dm_raid1_bio_record *bio_record =
  	  dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
74d46992e   Christoph Hellwig   block: replace bi...
1126
  	bio_record->details.bi_disk = NULL;
cd15fb64e   Mike Snitzer   Revert "dm mirror...
1127

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1128
  	if (rw == WRITE) {
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1129
  		/* Save region for mirror_end_io() handler */
0045d61b5   Mikulas Patocka   dm raid1: dont us...
1130
  		bio_record->write_region = dm_rh_bio_to_region(ms->rh, bio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1131
  		queue_bio(ms, bio, rw);
d2a7ad29a   Kiyoshi Ueda   [PATCH] dm: map a...
1132
  		return DM_MAPIO_SUBMITTED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1133
  	}
1f965b194   Heinz Mauelshagen   dm raid1: separat...
1134
  	r = log->type->in_sync(log, dm_rh_bio_to_region(ms->rh, bio), 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1135
  	if (r < 0 && r != -EWOULDBLOCK)
846785e6a   Christoph Hellwig   dm: don't return ...
1136
  		return DM_MAPIO_KILL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1137

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1138
  	/*
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1139
  	 * If region is not in-sync queue the bio.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1140
  	 */
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1141
  	if (!r || (r == -EWOULDBLOCK)) {
1eff9d322   Jens Axboe   block: rename bio...
1142
  		if (bio->bi_opf & REQ_RAHEAD)
846785e6a   Christoph Hellwig   dm: don't return ...
1143
  			return DM_MAPIO_KILL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1144

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1145
  		queue_bio(ms, bio, rw);
d2a7ad29a   Kiyoshi Ueda   [PATCH] dm: map a...
1146
  		return DM_MAPIO_SUBMITTED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1147
  	}
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1148
1149
1150
1151
  	/*
  	 * The region is in-sync and we can perform reads directly.
  	 * Store enough information so we can retry if it fails.
  	 */
4f024f379   Kent Overstreet   block: Abstract o...
1152
  	m = choose_mirror(ms, bio->bi_iter.bi_sector);
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1153
  	if (unlikely(!m))
846785e6a   Christoph Hellwig   dm: don't return ...
1154
  		return DM_MAPIO_KILL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1155

89c7cd897   Mikulas Patocka   dm raid1: rename ...
1156
  	dm_bio_record(&bio_record->details, bio);
89c7cd897   Mikulas Patocka   dm raid1: rename ...
1157
  	bio_record->m = m;
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1158
1159
  
  	map_bio(m, bio);
d2a7ad29a   Kiyoshi Ueda   [PATCH] dm: map a...
1160
  	return DM_MAPIO_REMAPPED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1161
  }
4e4cbee93   Christoph Hellwig   block: switch bio...
1162
1163
  static int mirror_end_io(struct dm_target *ti, struct bio *bio,
  		blk_status_t *error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1164
  {
70246286e   Christoph Hellwig   block: get rid of...
1165
  	int rw = bio_data_dir(bio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1166
  	struct mirror_set *ms = (struct mirror_set *) ti->private;
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1167
1168
  	struct mirror *m = NULL;
  	struct dm_bio_details *bd = NULL;
0045d61b5   Mikulas Patocka   dm raid1: dont us...
1169
1170
  	struct dm_raid1_bio_record *bio_record =
  	  dm_per_bio_data(bio, sizeof(struct dm_raid1_bio_record));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1171
1172
1173
1174
  
  	/*
  	 * We need to dec pending if this was a write.
  	 */
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1175
  	if (rw == WRITE) {
1eff9d322   Jens Axboe   block: rename bio...
1176
  		if (!(bio->bi_opf & REQ_PREFLUSH) &&
28a8f0d31   Mike Christie   block, drivers, f...
1177
  		    bio_op(bio) != REQ_OP_DISCARD)
0045d61b5   Mikulas Patocka   dm raid1: dont us...
1178
  			dm_rh_dec(ms->rh, bio_record->write_region);
1be569098   Christoph Hellwig   dm: change ->end_...
1179
  		return DM_ENDIO_DONE;
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1180
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1181

4e4cbee93   Christoph Hellwig   block: switch bio...
1182
  	if (*error == BLK_STS_NOTSUPP)
cd15fb64e   Mike Snitzer   Revert "dm mirror...
1183
  		goto out;
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1184

9966afaf9   Christoph Hellwig   dm: fix REQ_RAHEA...
1185
  	if (bio->bi_opf & REQ_RAHEAD)
cd15fb64e   Mike Snitzer   Revert "dm mirror...
1186
  		goto out;
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1187

1be569098   Christoph Hellwig   dm: change ->end_...
1188
  	if (unlikely(*error)) {
74d46992e   Christoph Hellwig   block: replace bi...
1189
  		if (!bio_record->details.bi_disk) {
cd15fb64e   Mike Snitzer   Revert "dm mirror...
1190
1191
1192
1193
1194
1195
  			/*
  			 * There wasn't enough memory to record necessary
  			 * information for a retry or there was no other
  			 * mirror in-sync.
  			 */
  			DMERR_LIMIT("Mirror read failed.");
c6b1e36c8   Linus Torvalds   Merge branch 'for...
1196
  			return DM_ENDIO_DONE;
cd15fb64e   Mike Snitzer   Revert "dm mirror...
1197
  		}
89c7cd897   Mikulas Patocka   dm raid1: rename ...
1198
  		m = bio_record->m;
e03f1a842   Adrian Bunk   dm-raid1.c: fix N...
1199

06386bbfd   Jonathan Brassow   dm raid1: handle ...
1200
1201
  		DMERR("Mirror read failed from %s. Trying alternative device.",
  		      m->dev->name);
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1202
1203
1204
1205
1206
1207
1208
  		fail_mirror(m, DM_RAID1_READ_ERROR);
  
  		/*
  		 * A failed read is requeued for another attempt using an intact
  		 * mirror.
  		 */
  		if (default_ok(m) || mirror_available(ms, bio)) {
89c7cd897   Mikulas Patocka   dm raid1: rename ...
1209
  			bd = &bio_record->details;
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1210
1211
  
  			dm_bio_restore(bd, bio);
74d46992e   Christoph Hellwig   block: replace bi...
1212
  			bio_record->details.bi_disk = NULL;
4e4cbee93   Christoph Hellwig   block: switch bio...
1213
  			bio->bi_status = 0;
f3a44fe06   Mikulas Patocka   dm raid1: fix imm...
1214

06386bbfd   Jonathan Brassow   dm raid1: handle ...
1215
  			queue_bio(ms, bio, rw);
19cbbc60c   Mikulas Patocka   dm raid: use DM_E...
1216
  			return DM_ENDIO_INCOMPLETE;
06386bbfd   Jonathan Brassow   dm raid1: handle ...
1217
1218
1219
  		}
  		DMERR("All replicated volumes dead, failing I/O");
  	}
cd15fb64e   Mike Snitzer   Revert "dm mirror...
1220
  out:
74d46992e   Christoph Hellwig   block: replace bi...
1221
  	bio_record->details.bi_disk = NULL;
cd15fb64e   Mike Snitzer   Revert "dm mirror...
1222

1be569098   Christoph Hellwig   dm: change ->end_...
1223
  	return DM_ENDIO_DONE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1224
  }
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
1225
  static void mirror_presuspend(struct dm_target *ti)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1226
1227
  {
  	struct mirror_set *ms = (struct mirror_set *) ti->private;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
1228
  	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1229

047885076   Mikulas Patocka   dm raid1: add fra...
1230
1231
  	struct bio_list holds;
  	struct bio *bio;
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
1232
1233
1234
  	atomic_set(&ms->suspend, 1);
  
  	/*
f07030409   Takahiro Yasui   dm raid1: fix dea...
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
  	 * Process bios in the hold list to start recovery waiting
  	 * for bios in the hold list. After the process, no bio has
  	 * a chance to be added in the hold list because ms->suspend
  	 * is set.
  	 */
  	spin_lock_irq(&ms->lock);
  	holds = ms->holds;
  	bio_list_init(&ms->holds);
  	spin_unlock_irq(&ms->lock);
  
  	while ((bio = bio_list_pop(&holds)))
  		hold_bio(ms, bio);
  
  	/*
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
1249
1250
1251
  	 * We must finish up all the work that we've
  	 * generated (i.e. recovery work).
  	 */
1f965b194   Heinz Mauelshagen   dm raid1: separat...
1252
  	dm_rh_stop_recovery(ms->rh);
33184048d   Jonathan E Brassow   [PATCH] dm: raid1...
1253

33184048d   Jonathan E Brassow   [PATCH] dm: raid1...
1254
  	wait_event(_kmirrord_recovery_stopped,
1f965b194   Heinz Mauelshagen   dm raid1: separat...
1255
  		   !dm_rh_recovery_in_flight(ms->rh));
33184048d   Jonathan E Brassow   [PATCH] dm: raid1...
1256

b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
  	if (log->type->presuspend && log->type->presuspend(log))
  		/* FIXME: need better error handling */
  		DMWARN("log presuspend failed");
  
  	/*
  	 * Now that recovery is complete/stopped and the
  	 * delayed bios are queued, we need to wait for
  	 * the worker thread to complete.  This way,
  	 * we know that all of our I/O has been pushed.
  	 */
  	flush_workqueue(ms->kmirrord_wq);
  }
  
  static void mirror_postsuspend(struct dm_target *ti)
  {
  	struct mirror_set *ms = ti->private;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
1273
  	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
1274

6b3df0d7a   Jonathan Brassow   dm log: split sus...
1275
  	if (log->type->postsuspend && log->type->postsuspend(log))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1276
  		/* FIXME: need better error handling */
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
1277
  		DMWARN("log postsuspend failed");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1278
1279
1280
1281
  }
  
  static void mirror_resume(struct dm_target *ti)
  {
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
1282
  	struct mirror_set *ms = ti->private;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
1283
  	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
1284
1285
  
  	atomic_set(&ms->suspend, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1286
1287
1288
  	if (log->type->resume && log->type->resume(log))
  		/* FIXME: need better error handling */
  		DMWARN("log resume failed");
1f965b194   Heinz Mauelshagen   dm raid1: separat...
1289
  	dm_rh_start_recovery(ms->rh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1290
  }
af195ac82   Jonathan Brassow   dm raid1: report ...
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
  /*
   * device_status_char
   * @m: mirror device/leg we want the status of
   *
   * We return one character representing the most severe error
   * we have encountered.
   *    A => Alive - No failures
   *    D => Dead - A write failure occurred leaving mirror out-of-sync
   *    S => Sync - A sychronization failure occurred, mirror out-of-sync
   *    R => Read - A read failure occurred, mirror data unaffected
   *
   * Returns: <char>
   */
  static char device_status_char(struct mirror *m)
  {
  	if (!atomic_read(&(m->error_count)))
  		return 'A';
64b30c46e   Mikulas Patocka   dm raid1: report ...
1308
1309
  	return (test_bit(DM_RAID1_FLUSH_ERROR, &(m->error_type))) ? 'F' :
  		(test_bit(DM_RAID1_WRITE_ERROR, &(m->error_type))) ? 'D' :
af195ac82   Jonathan Brassow   dm raid1: report ...
1310
1311
1312
  		(test_bit(DM_RAID1_SYNC_ERROR, &(m->error_type))) ? 'S' :
  		(test_bit(DM_RAID1_READ_ERROR, &(m->error_type))) ? 'R' : 'U';
  }
fd7c092e7   Mikulas Patocka   dm: fix truncated...
1313
1314
  static void mirror_status(struct dm_target *ti, status_type_t type,
  			  unsigned status_flags, char *result, unsigned maxlen)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1315
  {
315dcc226   Jonathan E Brassow   dm log: report fa...
1316
  	unsigned int m, sz = 0;
ed63287dd   Lidong Zhong   dm raid1: keep is...
1317
  	int num_feature_args = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1318
  	struct mirror_set *ms = (struct mirror_set *) ti->private;
1f965b194   Heinz Mauelshagen   dm raid1: separat...
1319
  	struct dm_dirty_log *log = dm_rh_dirty_log(ms->rh);
65972a6fa   Kees Cook   dm mirror: remove...
1320
  	char buffer[MAX_NR_MIRRORS + 1];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1321

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1322
1323
1324
  	switch (type) {
  	case STATUSTYPE_INFO:
  		DMEMIT("%d ", ms->nr_mirrors);
af195ac82   Jonathan Brassow   dm raid1: report ...
1325
  		for (m = 0; m < ms->nr_mirrors; m++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1326
  			DMEMIT("%s ", ms->mirror[m].dev->name);
af195ac82   Jonathan Brassow   dm raid1: report ...
1327
1328
1329
  			buffer[m] = device_status_char(&(ms->mirror[m]));
  		}
  		buffer[m] = '\0';
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1330

af195ac82   Jonathan Brassow   dm raid1: report ...
1331
  		DMEMIT("%llu/%llu 1 %s ",
1f965b194   Heinz Mauelshagen   dm raid1: separat...
1332
  		      (unsigned long long)log->type->get_sync_count(log),
af195ac82   Jonathan Brassow   dm raid1: report ...
1333
  		      (unsigned long long)ms->nr_regions, buffer);
315dcc226   Jonathan E Brassow   dm log: report fa...
1334

1f965b194   Heinz Mauelshagen   dm raid1: separat...
1335
  		sz += log->type->status(log, type, result+sz, maxlen-sz);
315dcc226   Jonathan E Brassow   dm log: report fa...
1336

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1337
1338
1339
  		break;
  
  	case STATUSTYPE_TABLE:
1f965b194   Heinz Mauelshagen   dm raid1: separat...
1340
  		sz = log->type->status(log, type, result, maxlen);
315dcc226   Jonathan E Brassow   dm log: report fa...
1341

e52b8f6db   Jonathan Brassow   [PATCH] dm mirror...
1342
  		DMEMIT("%d", ms->nr_mirrors);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1343
  		for (m = 0; m < ms->nr_mirrors; m++)
e52b8f6db   Jonathan Brassow   [PATCH] dm mirror...
1344
  			DMEMIT(" %s %llu", ms->mirror[m].dev->name,
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
1345
  			       (unsigned long long)ms->mirror[m].offset);
a8e6afa23   Jonathan E Brassow   dm raid1: add han...
1346

ed63287dd   Lidong Zhong   dm raid1: keep is...
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
  		num_feature_args += !!errors_handled(ms);
  		num_feature_args += !!keep_log(ms);
  		if (num_feature_args) {
  			DMEMIT(" %d", num_feature_args);
  			if (errors_handled(ms))
  				DMEMIT(" handle_errors");
  			if (keep_log(ms))
  				DMEMIT(" keep_log");
  		}
  
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1358
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1359
  }
af4874e03   Mike Snitzer   dm target:s intro...
1360
1361
1362
1363
1364
1365
1366
1367
1368
  static int mirror_iterate_devices(struct dm_target *ti,
  				  iterate_devices_callout_fn fn, void *data)
  {
  	struct mirror_set *ms = ti->private;
  	int ret = 0;
  	unsigned i;
  
  	for (i = 0; !ret && i < ms->nr_mirrors; i++)
  		ret = fn(ti, ms->mirror[i].dev,
5dea271b6   Mike Snitzer   dm table: pass co...
1369
  			 ms->mirror[i].offset, ti->len, data);
af4874e03   Mike Snitzer   dm target:s intro...
1370
1371
1372
  
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1373
1374
  static struct target_type mirror_target = {
  	.name	 = "mirror",
ed63287dd   Lidong Zhong   dm raid1: keep is...
1375
  	.version = {1, 14, 0},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1376
1377
1378
1379
1380
  	.module	 = THIS_MODULE,
  	.ctr	 = mirror_ctr,
  	.dtr	 = mirror_dtr,
  	.map	 = mirror_map,
  	.end_io	 = mirror_end_io,
b80aa7a0c   Jonathan Brassow   dm raid1: fix EIO...
1381
  	.presuspend = mirror_presuspend,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1382
1383
1384
  	.postsuspend = mirror_postsuspend,
  	.resume	 = mirror_resume,
  	.status	 = mirror_status,
af4874e03   Mike Snitzer   dm target:s intro...
1385
  	.iterate_devices = mirror_iterate_devices,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1386
1387
1388
1389
1390
  };
  
  static int __init dm_mirror_init(void)
  {
  	int r;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1391
  	r = dm_register_target(&mirror_target);
95f8fac8d   Mikulas Patocka   dm raid1: switch ...
1392
  	if (r < 0) {
0cd331243   Alasdair G Kergon   dm: remove duplic...
1393
  		DMERR("Failed to register mirror target");
95f8fac8d   Mikulas Patocka   dm raid1: switch ...
1394
1395
1396
1397
  		goto bad_target;
  	}
  
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1398

95f8fac8d   Mikulas Patocka   dm raid1: switch ...
1399
  bad_target:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1400
1401
1402
1403
1404
  	return r;
  }
  
  static void __exit dm_mirror_exit(void)
  {
10d3bd09a   Mikulas Patocka   dm: consolidate t...
1405
  	dm_unregister_target(&mirror_target);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1406
1407
1408
1409
1410
1411
1412
1413
1414
  }
  
  /* Module hooks */
  module_init(dm_mirror_init);
  module_exit(dm_mirror_exit);
  
  MODULE_DESCRIPTION(DM_NAME " mirror target");
  MODULE_AUTHOR("Joe Thornber");
  MODULE_LICENSE("GPL");