]>
Commit | Line | Data |
---|---|---|
b411b363 PR |
1 | /* |
2 | drbd_req.c | |
3 | ||
4 | This file is part of DRBD by Philipp Reisner and Lars Ellenberg. | |
5 | ||
6 | Copyright (C) 2001-2008, LINBIT Information Technologies GmbH. | |
7 | Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>. | |
8 | Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. | |
9 | ||
10 | drbd is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation; either version 2, or (at your option) | |
13 | any later version. | |
14 | ||
15 | drbd is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with drbd; see the file COPYING. If not, write to | |
22 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | ||
24 | */ | |
25 | ||
b411b363 PR |
26 | #include <linux/module.h> |
27 | ||
28 | #include <linux/slab.h> | |
29 | #include <linux/drbd.h> | |
30 | #include "drbd_int.h" | |
b411b363 PR |
31 | #include "drbd_req.h" |
32 | ||
33 | ||
34 | /* Update disk stats at start of I/O request */ | |
35 | static void _drbd_start_io_acct(struct drbd_conf *mdev, struct drbd_request *req, struct bio *bio) | |
36 | { | |
37 | const int rw = bio_data_dir(bio); | |
38 | int cpu; | |
39 | cpu = part_stat_lock(); | |
031a7c17 | 40 | part_round_stats(cpu, &mdev->vdisk->part0); |
b411b363 PR |
41 | part_stat_inc(cpu, &mdev->vdisk->part0, ios[rw]); |
42 | part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], bio_sectors(bio)); | |
753c8913 | 43 | part_inc_in_flight(&mdev->vdisk->part0, rw); |
b411b363 | 44 | part_stat_unlock(); |
b411b363 PR |
45 | } |
46 | ||
47 | /* Update disk stats when completing request upwards */ | |
48 | static void _drbd_end_io_acct(struct drbd_conf *mdev, struct drbd_request *req) | |
49 | { | |
50 | int rw = bio_data_dir(req->master_bio); | |
51 | unsigned long duration = jiffies - req->start_time; | |
52 | int cpu; | |
53 | cpu = part_stat_lock(); | |
54 | part_stat_add(cpu, &mdev->vdisk->part0, ticks[rw], duration); | |
55 | part_round_stats(cpu, &mdev->vdisk->part0); | |
753c8913 | 56 | part_dec_in_flight(&mdev->vdisk->part0, rw); |
b411b363 | 57 | part_stat_unlock(); |
b411b363 PR |
58 | } |
59 | ||
60 | static void _req_is_done(struct drbd_conf *mdev, struct drbd_request *req, const int rw) | |
61 | { | |
62 | const unsigned long s = req->rq_state; | |
288f422e PR |
63 | |
64 | /* remove it from the transfer log. | |
65 | * well, only if it had been there in the first | |
66 | * place... if it had not (local only or conflicting | |
67 | * and never sent), it should still be "empty" as | |
68 | * initialized in drbd_req_new(), so we can list_del() it | |
69 | * here unconditionally */ | |
70 | list_del(&req->tl_requests); | |
71 | ||
b411b363 PR |
72 | /* if it was a write, we may have to set the corresponding |
73 | * bit(s) out-of-sync first. If it had a local part, we need to | |
74 | * release the reference to the activity log. */ | |
75 | if (rw == WRITE) { | |
b411b363 PR |
76 | /* Set out-of-sync unless both OK flags are set |
77 | * (local only or remote failed). | |
78 | * Other places where we set out-of-sync: | |
79 | * READ with local io-error */ | |
80 | if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK)) | |
81 | drbd_set_out_of_sync(mdev, req->sector, req->size); | |
82 | ||
83 | if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS)) | |
84 | drbd_set_in_sync(mdev, req->sector, req->size); | |
85 | ||
86 | /* one might be tempted to move the drbd_al_complete_io | |
87 | * to the local io completion callback drbd_endio_pri. | |
88 | * but, if this was a mirror write, we may only | |
89 | * drbd_al_complete_io after this is RQ_NET_DONE, | |
90 | * otherwise the extent could be dropped from the al | |
91 | * before it has actually been written on the peer. | |
92 | * if we crash before our peer knows about the request, | |
93 | * but after the extent has been dropped from the al, | |
94 | * we would forget to resync the corresponding extent. | |
95 | */ | |
96 | if (s & RQ_LOCAL_MASK) { | |
97 | if (get_ldev_if_state(mdev, D_FAILED)) { | |
0778286a PR |
98 | if (s & RQ_IN_ACT_LOG) |
99 | drbd_al_complete_io(mdev, req->sector); | |
b411b363 PR |
100 | put_ldev(mdev); |
101 | } else if (__ratelimit(&drbd_ratelimit_state)) { | |
102 | dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu), " | |
103 | "but my Disk seems to have failed :(\n", | |
104 | (unsigned long long) req->sector); | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
32fa7e91 | 109 | drbd_req_free(req); |
b411b363 PR |
110 | } |
111 | ||
112 | static void queue_barrier(struct drbd_conf *mdev) | |
113 | { | |
114 | struct drbd_tl_epoch *b; | |
115 | ||
116 | /* We are within the req_lock. Once we queued the barrier for sending, | |
117 | * we set the CREATE_BARRIER bit. It is cleared as soon as a new | |
118 | * barrier/epoch object is added. This is the only place this bit is | |
119 | * set. It indicates that the barrier for this epoch is already queued, | |
120 | * and no new epoch has been created yet. */ | |
06f10adb | 121 | if (drbd_test_flag(mdev, CREATE_BARRIER)) |
b411b363 PR |
122 | return; |
123 | ||
124 | b = mdev->newest_tle; | |
125 | b->w.cb = w_send_barrier; | |
126 | /* inc_ap_pending done here, so we won't | |
127 | * get imbalanced on connection loss. | |
128 | * dec_ap_pending will be done in got_BarrierAck | |
129 | * or (on connection loss) in tl_clear. */ | |
130 | inc_ap_pending(mdev); | |
131 | drbd_queue_work(&mdev->data.work, &b->w); | |
06f10adb | 132 | drbd_set_flag(mdev, CREATE_BARRIER); |
b411b363 PR |
133 | } |
134 | ||
135 | static void _about_to_complete_local_write(struct drbd_conf *mdev, | |
136 | struct drbd_request *req) | |
137 | { | |
138 | const unsigned long s = req->rq_state; | |
139 | struct drbd_request *i; | |
140 | struct drbd_epoch_entry *e; | |
141 | struct hlist_node *n; | |
142 | struct hlist_head *slot; | |
143 | ||
8a3c1044 LE |
144 | /* Before we can signal completion to the upper layers, |
145 | * we may need to close the current epoch. | |
146 | * We can skip this, if this request has not even been sent, because we | |
147 | * did not have a fully established connection yet/anymore, during | |
148 | * bitmap exchange, or while we are C_AHEAD due to congestion policy. | |
149 | */ | |
150 | if (mdev->state.conn >= C_CONNECTED && | |
151 | (s & RQ_NET_SENT) != 0 && | |
b411b363 PR |
152 | req->epoch == mdev->newest_tle->br_number) |
153 | queue_barrier(mdev); | |
154 | ||
155 | /* we need to do the conflict detection stuff, | |
156 | * if we have the ee_hash (two_primaries) and | |
157 | * this has been on the network */ | |
158 | if ((s & RQ_NET_DONE) && mdev->ee_hash != NULL) { | |
159 | const sector_t sector = req->sector; | |
160 | const int size = req->size; | |
161 | ||
162 | /* ASSERT: | |
163 | * there must be no conflicting requests, since | |
164 | * they must have been failed on the spot */ | |
165 | #define OVERLAPS overlaps(sector, size, i->sector, i->size) | |
166 | slot = tl_hash_slot(mdev, sector); | |
24c4830c | 167 | hlist_for_each_entry(i, n, slot, collision) { |
b411b363 PR |
168 | if (OVERLAPS) { |
169 | dev_alert(DEV, "LOGIC BUG: completed: %p %llus +%u; " | |
170 | "other: %p %llus +%u\n", | |
171 | req, (unsigned long long)sector, size, | |
172 | i, (unsigned long long)i->sector, i->size); | |
173 | } | |
174 | } | |
175 | ||
176 | /* maybe "wake" those conflicting epoch entries | |
177 | * that wait for this request to finish. | |
178 | * | |
179 | * currently, there can be only _one_ such ee | |
180 | * (well, or some more, which would be pending | |
181 | * P_DISCARD_ACK not yet sent by the asender...), | |
182 | * since we block the receiver thread upon the | |
183 | * first conflict detection, which will wait on | |
184 | * misc_wait. maybe we want to assert that? | |
185 | * | |
186 | * anyways, if we found one, | |
187 | * we just have to do a wake_up. */ | |
188 | #undef OVERLAPS | |
189 | #define OVERLAPS overlaps(sector, size, e->sector, e->size) | |
190 | slot = ee_hash_slot(mdev, req->sector); | |
24c4830c | 191 | hlist_for_each_entry(e, n, slot, collision) { |
b411b363 PR |
192 | if (OVERLAPS) { |
193 | wake_up(&mdev->misc_wait); | |
194 | break; | |
195 | } | |
196 | } | |
197 | } | |
198 | #undef OVERLAPS | |
199 | } | |
200 | ||
201 | void complete_master_bio(struct drbd_conf *mdev, | |
202 | struct bio_and_error *m) | |
203 | { | |
b411b363 PR |
204 | bio_endio(m->bio, m->error); |
205 | dec_ap_bio(mdev); | |
206 | } | |
207 | ||
208 | /* Helper for __req_mod(). | |
209 | * Set m->bio to the master bio, if it is fit to be completed, | |
210 | * or leave it alone (it is initialized to NULL in __req_mod), | |
211 | * if it has already been completed, or cannot be completed yet. | |
212 | * If m->bio is set, the error status to be returned is placed in m->error. | |
213 | */ | |
214 | void _req_may_be_done(struct drbd_request *req, struct bio_and_error *m) | |
215 | { | |
216 | const unsigned long s = req->rq_state; | |
217 | struct drbd_conf *mdev = req->mdev; | |
2b4dd36f | 218 | int rw = req->rq_state & RQ_WRITE ? WRITE : READ; |
b411b363 | 219 | |
b411b363 PR |
220 | /* we must not complete the master bio, while it is |
221 | * still being processed by _drbd_send_zc_bio (drbd_send_dblock) | |
222 | * not yet acknowledged by the peer | |
223 | * not yet completed by the local io subsystem | |
224 | * these flags may get cleared in any order by | |
225 | * the worker, | |
226 | * the receiver, | |
227 | * the bio_endio completion callbacks. | |
228 | */ | |
229 | if (s & RQ_NET_QUEUED) | |
230 | return; | |
231 | if (s & RQ_NET_PENDING) | |
232 | return; | |
2b4dd36f | 233 | if (s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED)) |
b411b363 PR |
234 | return; |
235 | ||
236 | if (req->master_bio) { | |
237 | /* this is data_received (remote read) | |
238 | * or protocol C P_WRITE_ACK | |
239 | * or protocol B P_RECV_ACK | |
240 | * or protocol A "handed_over_to_network" (SendAck) | |
241 | * or canceled or failed, | |
242 | * or killed from the transfer log due to connection loss. | |
243 | */ | |
244 | ||
245 | /* | |
246 | * figure out whether to report success or failure. | |
247 | * | |
248 | * report success when at least one of the operations succeeded. | |
249 | * or, to put the other way, | |
250 | * only report failure, when both operations failed. | |
251 | * | |
252 | * what to do about the failures is handled elsewhere. | |
253 | * what we need to do here is just: complete the master_bio. | |
254 | * | |
255 | * local completion error, if any, has been stored as ERR_PTR | |
256 | * in private_bio within drbd_endio_pri. | |
257 | */ | |
258 | int ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK); | |
259 | int error = PTR_ERR(req->private_bio); | |
260 | ||
261 | /* remove the request from the conflict detection | |
262 | * respective block_id verification hash */ | |
24c4830c BVA |
263 | if (!hlist_unhashed(&req->collision)) |
264 | hlist_del(&req->collision); | |
b411b363 | 265 | else |
8825f7c3 | 266 | D_ASSERT((s & (RQ_NET_MASK & ~RQ_NET_DONE)) == 0); |
b411b363 PR |
267 | |
268 | /* for writes we need to do some extra housekeeping */ | |
269 | if (rw == WRITE) | |
270 | _about_to_complete_local_write(mdev, req); | |
271 | ||
272 | /* Update disk stats */ | |
273 | _drbd_end_io_acct(mdev, req); | |
274 | ||
275 | m->error = ok ? 0 : (error ?: -EIO); | |
276 | m->bio = req->master_bio; | |
277 | req->master_bio = NULL; | |
278 | } | |
279 | ||
2b4dd36f PR |
280 | if (s & RQ_LOCAL_PENDING) |
281 | return; | |
282 | ||
b411b363 PR |
283 | if ((s & RQ_NET_MASK) == 0 || (s & RQ_NET_DONE)) { |
284 | /* this is disconnected (local only) operation, | |
285 | * or protocol C P_WRITE_ACK, | |
286 | * or protocol A or B P_BARRIER_ACK, | |
287 | * or killed from the transfer log due to connection loss. */ | |
288 | _req_is_done(mdev, req, rw); | |
289 | } | |
290 | /* else: network part and not DONE yet. that is | |
291 | * protocol A or B, barrier ack still pending... */ | |
292 | } | |
293 | ||
cfa03415 PR |
294 | static void _req_may_be_done_not_susp(struct drbd_request *req, struct bio_and_error *m) |
295 | { | |
296 | struct drbd_conf *mdev = req->mdev; | |
297 | ||
fb22c402 | 298 | if (!is_susp(mdev->state)) |
cfa03415 PR |
299 | _req_may_be_done(req, m); |
300 | } | |
301 | ||
b411b363 PR |
302 | /* |
303 | * checks whether there was an overlapping request | |
304 | * or ee already registered. | |
305 | * | |
306 | * if so, return 1, in which case this request is completed on the spot, | |
307 | * without ever being submitted or send. | |
308 | * | |
309 | * return 0 if it is ok to submit this request. | |
310 | * | |
311 | * NOTE: | |
312 | * paranoia: assume something above us is broken, and issues different write | |
313 | * requests for the same block simultaneously... | |
314 | * | |
315 | * To ensure these won't be reordered differently on both nodes, resulting in | |
316 | * diverging data sets, we discard the later one(s). Not that this is supposed | |
317 | * to happen, but this is the rationale why we also have to check for | |
318 | * conflicting requests with local origin, and why we have to do so regardless | |
319 | * of whether we allowed multiple primaries. | |
320 | * | |
321 | * BTW, in case we only have one primary, the ee_hash is empty anyways, and the | |
322 | * second hlist_for_each_entry becomes a noop. This is even simpler than to | |
323 | * grab a reference on the net_conf, and check for the two_primaries flag... | |
324 | */ | |
325 | static int _req_conflicts(struct drbd_request *req) | |
326 | { | |
327 | struct drbd_conf *mdev = req->mdev; | |
328 | const sector_t sector = req->sector; | |
329 | const int size = req->size; | |
330 | struct drbd_request *i; | |
331 | struct drbd_epoch_entry *e; | |
332 | struct hlist_node *n; | |
333 | struct hlist_head *slot; | |
334 | ||
24c4830c | 335 | D_ASSERT(hlist_unhashed(&req->collision)); |
b411b363 PR |
336 | |
337 | if (!get_net_conf(mdev)) | |
338 | return 0; | |
339 | ||
340 | /* BUG_ON */ | |
341 | ERR_IF (mdev->tl_hash_s == 0) | |
342 | goto out_no_conflict; | |
343 | BUG_ON(mdev->tl_hash == NULL); | |
344 | ||
345 | #define OVERLAPS overlaps(i->sector, i->size, sector, size) | |
346 | slot = tl_hash_slot(mdev, sector); | |
24c4830c | 347 | hlist_for_each_entry(i, n, slot, collision) { |
b411b363 PR |
348 | if (OVERLAPS) { |
349 | dev_alert(DEV, "%s[%u] Concurrent local write detected! " | |
350 | "[DISCARD L] new: %llus +%u; " | |
351 | "pending: %llus +%u\n", | |
352 | current->comm, current->pid, | |
353 | (unsigned long long)sector, size, | |
354 | (unsigned long long)i->sector, i->size); | |
355 | goto out_conflict; | |
356 | } | |
357 | } | |
358 | ||
359 | if (mdev->ee_hash_s) { | |
360 | /* now, check for overlapping requests with remote origin */ | |
361 | BUG_ON(mdev->ee_hash == NULL); | |
362 | #undef OVERLAPS | |
363 | #define OVERLAPS overlaps(e->sector, e->size, sector, size) | |
364 | slot = ee_hash_slot(mdev, sector); | |
24c4830c | 365 | hlist_for_each_entry(e, n, slot, collision) { |
b411b363 PR |
366 | if (OVERLAPS) { |
367 | dev_alert(DEV, "%s[%u] Concurrent remote write detected!" | |
368 | " [DISCARD L] new: %llus +%u; " | |
369 | "pending: %llus +%u\n", | |
370 | current->comm, current->pid, | |
371 | (unsigned long long)sector, size, | |
372 | (unsigned long long)e->sector, e->size); | |
373 | goto out_conflict; | |
374 | } | |
375 | } | |
376 | } | |
377 | #undef OVERLAPS | |
378 | ||
379 | out_no_conflict: | |
380 | /* this is like it should be, and what we expected. | |
381 | * our users do behave after all... */ | |
382 | put_net_conf(mdev); | |
383 | return 0; | |
384 | ||
385 | out_conflict: | |
386 | put_net_conf(mdev); | |
387 | return 1; | |
388 | } | |
389 | ||
ccae7868 LE |
390 | static void drbd_report_io_error(struct drbd_conf *mdev, struct drbd_request *req) |
391 | { | |
392 | char b[BDEVNAME_SIZE]; | |
393 | ||
394 | if (__ratelimit(&drbd_ratelimit_state)) | |
395 | return; | |
396 | ||
397 | dev_warn(DEV, "local %s IO error sector %llu+%u on %s\n", | |
398 | (req->rq_state & RQ_WRITE) ? "WRITE" : "READ", | |
399 | (unsigned long long)req->sector, | |
400 | req->size >> 9, | |
401 | bdevname(mdev->ldev->backing_bdev, b)); | |
402 | } | |
403 | ||
b411b363 PR |
404 | /* obviously this could be coded as many single functions |
405 | * instead of one huge switch, | |
406 | * or by putting the code directly in the respective locations | |
407 | * (as it has been before). | |
408 | * | |
409 | * but having it this way | |
410 | * enforces that it is all in this one place, where it is easier to audit, | |
411 | * it makes it obvious that whatever "event" "happens" to a request should | |
412 | * happen "atomically" within the req_lock, | |
413 | * and it enforces that we have to think in a very structured manner | |
414 | * about the "events" that may happen to a request during its life time ... | |
415 | */ | |
2a80699f | 416 | int __req_mod(struct drbd_request *req, enum drbd_req_event what, |
b411b363 PR |
417 | struct bio_and_error *m) |
418 | { | |
419 | struct drbd_conf *mdev = req->mdev; | |
2a80699f | 420 | int rv = 0; |
b411b363 PR |
421 | m->bio = NULL; |
422 | ||
b411b363 PR |
423 | switch (what) { |
424 | default: | |
425 | dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__); | |
426 | break; | |
427 | ||
428 | /* does not happen... | |
429 | * initialization done in drbd_req_new | |
430 | case created: | |
431 | break; | |
432 | */ | |
433 | ||
434 | case to_be_send: /* via network */ | |
435 | /* reached via drbd_make_request_common | |
436 | * and from w_read_retry_remote */ | |
437 | D_ASSERT(!(req->rq_state & RQ_NET_MASK)); | |
438 | req->rq_state |= RQ_NET_PENDING; | |
439 | inc_ap_pending(mdev); | |
440 | break; | |
441 | ||
442 | case to_be_submitted: /* locally */ | |
443 | /* reached via drbd_make_request_common */ | |
444 | D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK)); | |
445 | req->rq_state |= RQ_LOCAL_PENDING; | |
446 | break; | |
447 | ||
448 | case completed_ok: | |
2b4dd36f | 449 | if (req->rq_state & RQ_WRITE) |
b411b363 PR |
450 | mdev->writ_cnt += req->size>>9; |
451 | else | |
452 | mdev->read_cnt += req->size>>9; | |
453 | ||
454 | req->rq_state |= (RQ_LOCAL_COMPLETED|RQ_LOCAL_OK); | |
455 | req->rq_state &= ~RQ_LOCAL_PENDING; | |
456 | ||
cfa03415 | 457 | _req_may_be_done_not_susp(req, m); |
b411b363 PR |
458 | break; |
459 | ||
2b4dd36f PR |
460 | case abort_disk_io: |
461 | req->rq_state |= RQ_LOCAL_ABORTED; | |
462 | if (req->rq_state & RQ_WRITE) | |
463 | _req_may_be_done_not_susp(req, m); | |
464 | else | |
465 | goto goto_queue_for_net_read; | |
466 | break; | |
467 | ||
b411b363 PR |
468 | case write_completed_with_error: |
469 | req->rq_state |= RQ_LOCAL_COMPLETED; | |
470 | req->rq_state &= ~RQ_LOCAL_PENDING; | |
471 | ||
ccae7868 | 472 | drbd_report_io_error(mdev, req); |
a2a3c74f | 473 | __drbd_chk_io_error(mdev, DRBD_WRITE_ERROR); |
cfa03415 | 474 | _req_may_be_done_not_susp(req, m); |
b411b363 PR |
475 | break; |
476 | ||
477 | case read_ahead_completed_with_error: | |
478 | /* it is legal to fail READA */ | |
479 | req->rq_state |= RQ_LOCAL_COMPLETED; | |
480 | req->rq_state &= ~RQ_LOCAL_PENDING; | |
cfa03415 | 481 | _req_may_be_done_not_susp(req, m); |
b411b363 PR |
482 | break; |
483 | ||
484 | case read_completed_with_error: | |
485 | drbd_set_out_of_sync(mdev, req->sector, req->size); | |
486 | ||
487 | req->rq_state |= RQ_LOCAL_COMPLETED; | |
488 | req->rq_state &= ~RQ_LOCAL_PENDING; | |
489 | ||
1ed25b26 LE |
490 | if (req->rq_state & RQ_LOCAL_ABORTED) { |
491 | _req_may_be_done(req, m); | |
492 | break; | |
493 | } | |
b411b363 | 494 | |
ccae7868 | 495 | drbd_report_io_error(mdev, req); |
a2a3c74f | 496 | __drbd_chk_io_error(mdev, DRBD_READ_ERROR); |
b411b363 | 497 | |
2b4dd36f PR |
498 | goto_queue_for_net_read: |
499 | ||
1ed25b26 LE |
500 | D_ASSERT(!(req->rq_state & RQ_NET_MASK)); |
501 | ||
d255e5ff LE |
502 | /* no point in retrying if there is no good remote data, |
503 | * or we have no connection. */ | |
504 | if (mdev->state.pdsk != D_UP_TO_DATE) { | |
cfa03415 | 505 | _req_may_be_done_not_susp(req, m); |
d255e5ff LE |
506 | break; |
507 | } | |
508 | ||
509 | /* _req_mod(req,to_be_send); oops, recursion... */ | |
510 | req->rq_state |= RQ_NET_PENDING; | |
511 | inc_ap_pending(mdev); | |
b411b363 PR |
512 | /* fall through: _req_mod(req,queue_for_net_read); */ |
513 | ||
514 | case queue_for_net_read: | |
515 | /* READ or READA, and | |
516 | * no local disk, | |
517 | * or target area marked as invalid, | |
518 | * or just got an io-error. */ | |
519 | /* from drbd_make_request_common | |
520 | * or from bio_endio during read io-error recovery */ | |
521 | ||
522 | /* so we can verify the handle in the answer packet | |
523 | * corresponding hlist_del is in _req_may_be_done() */ | |
24c4830c | 524 | hlist_add_head(&req->collision, ar_hash_slot(mdev, req->sector)); |
b411b363 | 525 | |
06f10adb | 526 | drbd_set_flag(mdev, UNPLUG_REMOTE); |
b411b363 PR |
527 | |
528 | D_ASSERT(req->rq_state & RQ_NET_PENDING); | |
529 | req->rq_state |= RQ_NET_QUEUED; | |
530 | req->w.cb = (req->rq_state & RQ_LOCAL_MASK) | |
531 | ? w_read_retry_remote | |
532 | : w_send_read_req; | |
533 | drbd_queue_work(&mdev->data.work, &req->w); | |
534 | break; | |
535 | ||
536 | case queue_for_net_write: | |
537 | /* assert something? */ | |
538 | /* from drbd_make_request_common only */ | |
539 | ||
24c4830c | 540 | hlist_add_head(&req->collision, tl_hash_slot(mdev, req->sector)); |
b411b363 PR |
541 | /* corresponding hlist_del is in _req_may_be_done() */ |
542 | ||
543 | /* NOTE | |
544 | * In case the req ended up on the transfer log before being | |
545 | * queued on the worker, it could lead to this request being | |
546 | * missed during cleanup after connection loss. | |
547 | * So we have to do both operations here, | |
548 | * within the same lock that protects the transfer log. | |
549 | * | |
550 | * _req_add_to_epoch(req); this has to be after the | |
551 | * _maybe_start_new_epoch(req); which happened in | |
552 | * drbd_make_request_common, because we now may set the bit | |
553 | * again ourselves to close the current epoch. | |
554 | * | |
555 | * Add req to the (now) current epoch (barrier). */ | |
556 | ||
83c38830 LE |
557 | /* otherwise we may lose an unplug, which may cause some remote |
558 | * io-scheduler timeout to expire, increasing maximum latency, | |
559 | * hurting performance. */ | |
06f10adb | 560 | drbd_set_flag(mdev, UNPLUG_REMOTE); |
83c38830 | 561 | |
b411b363 PR |
562 | /* see drbd_make_request_common, |
563 | * just after it grabs the req_lock */ | |
06f10adb | 564 | D_ASSERT(drbd_test_flag(mdev, CREATE_BARRIER) == 0); |
b411b363 PR |
565 | |
566 | req->epoch = mdev->newest_tle->br_number; | |
b411b363 PR |
567 | |
568 | /* increment size of current epoch */ | |
7e602c0a | 569 | mdev->newest_tle->n_writes++; |
b411b363 PR |
570 | |
571 | /* queue work item to send data */ | |
572 | D_ASSERT(req->rq_state & RQ_NET_PENDING); | |
573 | req->rq_state |= RQ_NET_QUEUED; | |
574 | req->w.cb = w_send_dblock; | |
575 | drbd_queue_work(&mdev->data.work, &req->w); | |
576 | ||
577 | /* close the epoch, in case it outgrew the limit */ | |
7e602c0a | 578 | if (mdev->newest_tle->n_writes >= mdev->net_conf->max_epoch_size) |
b411b363 PR |
579 | queue_barrier(mdev); |
580 | ||
581 | break; | |
582 | ||
73a01a18 PR |
583 | case queue_for_send_oos: |
584 | req->rq_state |= RQ_NET_QUEUED; | |
585 | req->w.cb = w_send_oos; | |
586 | drbd_queue_work(&mdev->data.work, &req->w); | |
587 | break; | |
588 | ||
41c4a003 | 589 | case read_retry_remote_canceled: |
b411b363 | 590 | case send_canceled: |
b411b363 PR |
591 | case send_failed: |
592 | /* real cleanup will be done from tl_clear. just update flags | |
593 | * so it is no longer marked as on the worker queue */ | |
594 | req->rq_state &= ~RQ_NET_QUEUED; | |
595 | /* if we did it right, tl_clear should be scheduled only after | |
596 | * this, so this should not be necessary! */ | |
cfa03415 | 597 | _req_may_be_done_not_susp(req, m); |
b411b363 PR |
598 | break; |
599 | ||
600 | case handed_over_to_network: | |
601 | /* assert something? */ | |
759fbdfb PR |
602 | if (bio_data_dir(req->master_bio) == WRITE) |
603 | atomic_add(req->size>>9, &mdev->ap_in_flight); | |
604 | ||
b411b363 PR |
605 | if (bio_data_dir(req->master_bio) == WRITE && |
606 | mdev->net_conf->wire_protocol == DRBD_PROT_A) { | |
607 | /* this is what is dangerous about protocol A: | |
608 | * pretend it was successfully written on the peer. */ | |
609 | if (req->rq_state & RQ_NET_PENDING) { | |
610 | dec_ap_pending(mdev); | |
611 | req->rq_state &= ~RQ_NET_PENDING; | |
612 | req->rq_state |= RQ_NET_OK; | |
613 | } /* else: neg-ack was faster... */ | |
614 | /* it is still not yet RQ_NET_DONE until the | |
615 | * corresponding epoch barrier got acked as well, | |
616 | * so we know what to dirty on connection loss */ | |
617 | } | |
618 | req->rq_state &= ~RQ_NET_QUEUED; | |
619 | req->rq_state |= RQ_NET_SENT; | |
6d49e101 LE |
620 | _req_may_be_done_not_susp(req, m); |
621 | break; | |
622 | ||
623 | case oos_handed_to_network: | |
624 | /* Was not set PENDING, no longer QUEUED, so is now DONE | |
625 | * as far as this connection is concerned. */ | |
626 | req->rq_state &= ~RQ_NET_QUEUED; | |
627 | req->rq_state |= RQ_NET_DONE; | |
cfa03415 | 628 | _req_may_be_done_not_susp(req, m); |
b411b363 PR |
629 | break; |
630 | ||
631 | case connection_lost_while_pending: | |
632 | /* transfer log cleanup after connection loss */ | |
633 | /* assert something? */ | |
634 | if (req->rq_state & RQ_NET_PENDING) | |
635 | dec_ap_pending(mdev); | |
636 | req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING); | |
637 | req->rq_state |= RQ_NET_DONE; | |
759fbdfb PR |
638 | if (req->rq_state & RQ_NET_SENT && req->rq_state & RQ_WRITE) |
639 | atomic_sub(req->size>>9, &mdev->ap_in_flight); | |
640 | ||
b411b363 PR |
641 | /* if it is still queued, we may not complete it here. |
642 | * it will be canceled soon. */ | |
643 | if (!(req->rq_state & RQ_NET_QUEUED)) | |
cfa03415 | 644 | _req_may_be_done(req, m); /* Allowed while state.susp */ |
b411b363 PR |
645 | break; |
646 | ||
b411b363 PR |
647 | case conflict_discarded_by_peer: |
648 | /* for discarded conflicting writes of multiple primaries, | |
649 | * there is no need to keep anything in the tl, potential | |
650 | * node crashes are covered by the activity log. */ | |
651 | if (what == conflict_discarded_by_peer) | |
652 | dev_alert(DEV, "Got DiscardAck packet %llus +%u!" | |
653 | " DRBD is not a random data generator!\n", | |
654 | (unsigned long long)req->sector, req->size); | |
655 | req->rq_state |= RQ_NET_DONE; | |
656 | /* fall through */ | |
d64957c9 | 657 | case write_acked_by_peer_and_sis: |
b411b363 | 658 | case write_acked_by_peer: |
d64957c9 LE |
659 | if (what == write_acked_by_peer_and_sis) |
660 | req->rq_state |= RQ_NET_SIS; | |
b411b363 | 661 | /* protocol C; successfully written on peer. |
d64957c9 | 662 | * Nothing more to do here. |
b411b363 | 663 | * We want to keep the tl in place for all protocols, to cater |
d64957c9 | 664 | * for volatile write-back caches on lower level devices. */ |
b411b363 | 665 | |
b411b363 PR |
666 | case recv_acked_by_peer: |
667 | /* protocol B; pretends to be successfully written on peer. | |
668 | * see also notes above in handed_over_to_network about | |
669 | * protocol != C */ | |
670 | req->rq_state |= RQ_NET_OK; | |
671 | D_ASSERT(req->rq_state & RQ_NET_PENDING); | |
672 | dec_ap_pending(mdev); | |
759fbdfb | 673 | atomic_sub(req->size>>9, &mdev->ap_in_flight); |
b411b363 | 674 | req->rq_state &= ~RQ_NET_PENDING; |
cfa03415 | 675 | _req_may_be_done_not_susp(req, m); |
b411b363 PR |
676 | break; |
677 | ||
678 | case neg_acked: | |
679 | /* assert something? */ | |
759fbdfb | 680 | if (req->rq_state & RQ_NET_PENDING) { |
b411b363 | 681 | dec_ap_pending(mdev); |
759fbdfb PR |
682 | atomic_sub(req->size>>9, &mdev->ap_in_flight); |
683 | } | |
b411b363 PR |
684 | req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING); |
685 | ||
686 | req->rq_state |= RQ_NET_DONE; | |
cfa03415 | 687 | _req_may_be_done_not_susp(req, m); |
b411b363 PR |
688 | /* else: done by handed_over_to_network */ |
689 | break; | |
690 | ||
265be2d0 PR |
691 | case fail_frozen_disk_io: |
692 | if (!(req->rq_state & RQ_LOCAL_COMPLETED)) | |
693 | break; | |
694 | ||
cfa03415 | 695 | _req_may_be_done(req, m); /* Allowed while state.susp */ |
265be2d0 PR |
696 | break; |
697 | ||
698 | case restart_frozen_disk_io: | |
699 | if (!(req->rq_state & RQ_LOCAL_COMPLETED)) | |
700 | break; | |
701 | ||
702 | req->rq_state &= ~RQ_LOCAL_COMPLETED; | |
703 | ||
704 | rv = MR_READ; | |
705 | if (bio_data_dir(req->master_bio) == WRITE) | |
706 | rv = MR_WRITE; | |
707 | ||
708 | get_ldev(mdev); | |
709 | req->w.cb = w_restart_disk_io; | |
710 | drbd_queue_work(&mdev->data.work, &req->w); | |
711 | break; | |
712 | ||
11b58e73 | 713 | case resend: |
509fc019 PR |
714 | /* Simply complete (local only) READs. */ |
715 | if (!(req->rq_state & RQ_WRITE) && !req->w.cb) { | |
716 | _req_may_be_done(req, m); | |
717 | break; | |
718 | } | |
719 | ||
11b58e73 | 720 | /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK |
47ff2d0a | 721 | before the connection loss (B&C only); only P_BARRIER_ACK was missing. |
11b58e73 | 722 | Trowing them out of the TL here by pretending we got a BARRIER_ACK |
481c6f50 | 723 | We ensure that the peer was not rebooted */ |
11b58e73 PR |
724 | if (!(req->rq_state & RQ_NET_OK)) { |
725 | if (req->w.cb) { | |
726 | drbd_queue_work(&mdev->data.work, &req->w); | |
727 | rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ; | |
728 | } | |
729 | break; | |
730 | } | |
731 | /* else, fall through to barrier_acked */ | |
732 | ||
b411b363 | 733 | case barrier_acked: |
288f422e PR |
734 | if (!(req->rq_state & RQ_WRITE)) |
735 | break; | |
736 | ||
b411b363 PR |
737 | if (req->rq_state & RQ_NET_PENDING) { |
738 | /* barrier came in before all requests have been acked. | |
739 | * this is bad, because if the connection is lost now, | |
740 | * we won't be able to clean them up... */ | |
741 | dev_err(DEV, "FIXME (barrier_acked but pending)\n"); | |
b411b363 PR |
742 | list_move(&req->tl_requests, &mdev->out_of_sequence_requests); |
743 | } | |
e636db5b LE |
744 | if ((req->rq_state & RQ_NET_MASK) != 0) { |
745 | req->rq_state |= RQ_NET_DONE; | |
746 | if (mdev->net_conf->wire_protocol == DRBD_PROT_A) | |
747 | atomic_sub(req->size>>9, &mdev->ap_in_flight); | |
748 | } | |
cfa03415 | 749 | _req_may_be_done(req, m); /* Allowed while state.susp */ |
b411b363 PR |
750 | break; |
751 | ||
752 | case data_received: | |
753 | D_ASSERT(req->rq_state & RQ_NET_PENDING); | |
754 | dec_ap_pending(mdev); | |
755 | req->rq_state &= ~RQ_NET_PENDING; | |
756 | req->rq_state |= (RQ_NET_OK|RQ_NET_DONE); | |
cfa03415 | 757 | _req_may_be_done_not_susp(req, m); |
b411b363 PR |
758 | break; |
759 | }; | |
2a80699f PR |
760 | |
761 | return rv; | |
b411b363 PR |
762 | } |
763 | ||
764 | /* we may do a local read if: | |
765 | * - we are consistent (of course), | |
766 | * - or we are generally inconsistent, | |
767 | * BUT we are still/already IN SYNC for this area. | |
768 | * since size may be bigger than BM_BLOCK_SIZE, | |
769 | * we may need to check several bits. | |
770 | */ | |
771 | static int drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size) | |
772 | { | |
773 | unsigned long sbnr, ebnr; | |
774 | sector_t esector, nr_sectors; | |
775 | ||
776 | if (mdev->state.disk == D_UP_TO_DATE) | |
777 | return 1; | |
778 | if (mdev->state.disk >= D_OUTDATED) | |
779 | return 0; | |
780 | if (mdev->state.disk < D_INCONSISTENT) | |
781 | return 0; | |
782 | /* state.disk == D_INCONSISTENT We will have a look at the BitMap */ | |
783 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
784 | esector = sector + (size >> 9) - 1; | |
785 | ||
786 | D_ASSERT(sector < nr_sectors); | |
787 | D_ASSERT(esector < nr_sectors); | |
788 | ||
789 | sbnr = BM_SECT_TO_BIT(sector); | |
790 | ebnr = BM_SECT_TO_BIT(esector); | |
791 | ||
792 | return 0 == drbd_bm_count_bits(mdev, sbnr, ebnr); | |
793 | } | |
794 | ||
0d5934e3 LE |
795 | static void maybe_pull_ahead(struct drbd_conf *mdev) |
796 | { | |
797 | int congested = 0; | |
798 | ||
799 | /* If I don't even have good local storage, we can not reasonably try | |
800 | * to pull ahead of the peer. We also need the local reference to make | |
801 | * sure mdev->act_log is there. | |
802 | * Note: caller has to make sure that net_conf is there. | |
803 | */ | |
804 | if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) | |
805 | return; | |
806 | ||
807 | if (mdev->net_conf->cong_fill && | |
808 | atomic_read(&mdev->ap_in_flight) >= mdev->net_conf->cong_fill) { | |
809 | dev_info(DEV, "Congestion-fill threshold reached\n"); | |
810 | congested = 1; | |
811 | } | |
812 | ||
813 | if (mdev->act_log->used >= mdev->net_conf->cong_extents) { | |
814 | dev_info(DEV, "Congestion-extents threshold reached\n"); | |
815 | congested = 1; | |
816 | } | |
817 | ||
818 | if (congested) { | |
819 | queue_barrier(mdev); /* last barrier, after mirrored writes */ | |
820 | ||
821 | if (mdev->net_conf->on_congestion == OC_PULL_AHEAD) | |
822 | _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL); | |
823 | else /*mdev->net_conf->on_congestion == OC_DISCONNECT */ | |
824 | _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL); | |
825 | } | |
826 | put_ldev(mdev); | |
827 | } | |
828 | ||
aeda1cd6 | 829 | static int drbd_make_request_common(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time) |
b411b363 PR |
830 | { |
831 | const int rw = bio_rw(bio); | |
832 | const int size = bio->bi_size; | |
833 | const sector_t sector = bio->bi_sector; | |
834 | struct drbd_tl_epoch *b = NULL; | |
835 | struct drbd_request *req; | |
73a01a18 | 836 | int local, remote, send_oos = 0; |
b411b363 | 837 | int err = -EIO; |
9a25a04c | 838 | int ret = 0; |
fc28845b | 839 | union drbd_state s; |
b411b363 PR |
840 | |
841 | /* allocate outside of all locks; */ | |
842 | req = drbd_req_new(mdev, bio); | |
843 | if (!req) { | |
844 | dec_ap_bio(mdev); | |
845 | /* only pass the error to the upper layers. | |
846 | * if user cannot handle io errors, that's not our business. */ | |
847 | dev_err(DEV, "could not kmalloc() req\n"); | |
848 | bio_endio(bio, -ENOMEM); | |
849 | return 0; | |
850 | } | |
aeda1cd6 | 851 | req->start_time = start_time; |
b411b363 | 852 | |
b411b363 PR |
853 | local = get_ldev(mdev); |
854 | if (!local) { | |
855 | bio_put(req->private_bio); /* or we get a bio leak */ | |
856 | req->private_bio = NULL; | |
857 | } | |
858 | if (rw == WRITE) { | |
227f052f LE |
859 | /* Need to replicate writes. Unless it is an empty flush, |
860 | * which is better mapped to a DRBD P_BARRIER packet, | |
861 | * also for drbd wire protocol compatibility reasons. */ | |
862 | if (unlikely(size == 0)) { | |
863 | /* The only size==0 bios we expect are empty flushes. */ | |
864 | D_ASSERT(bio->bi_rw & REQ_FLUSH); | |
865 | remote = 0; | |
866 | } else | |
867 | remote = 1; | |
b411b363 PR |
868 | } else { |
869 | /* READ || READA */ | |
870 | if (local) { | |
871 | if (!drbd_may_do_local_read(mdev, sector, size)) { | |
872 | /* we could kick the syncer to | |
873 | * sync this extent asap, wait for | |
874 | * it, then continue locally. | |
875 | * Or just issue the request remotely. | |
876 | */ | |
877 | local = 0; | |
878 | bio_put(req->private_bio); | |
879 | req->private_bio = NULL; | |
880 | put_ldev(mdev); | |
881 | } | |
882 | } | |
883 | remote = !local && mdev->state.pdsk >= D_UP_TO_DATE; | |
884 | } | |
885 | ||
886 | /* If we have a disk, but a READA request is mapped to remote, | |
887 | * we are R_PRIMARY, D_INCONSISTENT, SyncTarget. | |
888 | * Just fail that READA request right here. | |
889 | * | |
890 | * THINK: maybe fail all READA when not local? | |
891 | * or make this configurable... | |
892 | * if network is slow, READA won't do any good. | |
893 | */ | |
894 | if (rw == READA && mdev->state.disk >= D_INCONSISTENT && !local) { | |
895 | err = -EWOULDBLOCK; | |
896 | goto fail_and_free_req; | |
897 | } | |
898 | ||
899 | /* For WRITES going to the local disk, grab a reference on the target | |
900 | * extent. This waits for any resync activity in the corresponding | |
901 | * resync extent to finish, and, if necessary, pulls in the target | |
902 | * extent into the activity log, which involves further disk io because | |
227f052f LE |
903 | * of transactional on-disk meta data updates. |
904 | * Empty flushes don't need to go into the activity log, they can only | |
905 | * flush data for pending writes which are already in there. */ | |
906 | if (rw == WRITE && local && size | |
06f10adb | 907 | && !drbd_test_flag(mdev, AL_SUSPENDED)) { |
0778286a | 908 | req->rq_state |= RQ_IN_ACT_LOG; |
b411b363 | 909 | drbd_al_begin_io(mdev, sector); |
0778286a | 910 | } |
b411b363 | 911 | |
fc28845b PR |
912 | s = mdev->state; |
913 | remote = remote && drbd_should_do_remote(s); | |
914 | send_oos = rw == WRITE && drbd_should_send_oos(s); | |
3719094e | 915 | D_ASSERT(!(remote && send_oos)); |
b411b363 | 916 | |
fb22c402 | 917 | if (!(local || remote) && !is_susp(mdev->state)) { |
fb2c7a10 | 918 | if (__ratelimit(&drbd_ratelimit_state)) |
ccae7868 LE |
919 | dev_err(DEV, "IO ERROR: neither local nor remote data, sector %llu+%u\n", |
920 | (unsigned long long)req->sector, req->size >> 9); | |
b411b363 PR |
921 | goto fail_free_complete; |
922 | } | |
923 | ||
924 | /* For WRITE request, we have to make sure that we have an | |
925 | * unused_spare_tle, in case we need to start a new epoch. | |
926 | * I try to be smart and avoid to pre-allocate always "just in case", | |
927 | * but there is a race between testing the bit and pointer outside the | |
928 | * spinlock, and grabbing the spinlock. | |
929 | * if we lost that race, we retry. */ | |
73a01a18 | 930 | if (rw == WRITE && (remote || send_oos) && |
b411b363 | 931 | mdev->unused_spare_tle == NULL && |
06f10adb | 932 | drbd_test_flag(mdev, CREATE_BARRIER)) { |
b411b363 PR |
933 | allocate_barrier: |
934 | b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_NOIO); | |
935 | if (!b) { | |
936 | dev_err(DEV, "Failed to alloc barrier.\n"); | |
937 | err = -ENOMEM; | |
938 | goto fail_free_complete; | |
939 | } | |
940 | } | |
941 | ||
942 | /* GOOD, everything prepared, grab the spin_lock */ | |
943 | spin_lock_irq(&mdev->req_lock); | |
944 | ||
fb22c402 | 945 | if (is_susp(mdev->state)) { |
9a25a04c | 946 | /* If we got suspended, use the retry mechanism of |
f6d0a8db | 947 | drbd_make_request() to restart processing of this |
2f58dcfc | 948 | bio. In the next call to drbd_make_request |
9a25a04c PR |
949 | we sleep in inc_ap_bio() */ |
950 | ret = 1; | |
951 | spin_unlock_irq(&mdev->req_lock); | |
952 | goto fail_free_complete; | |
953 | } | |
954 | ||
73a01a18 | 955 | if (remote || send_oos) { |
6a35c45f PR |
956 | remote = drbd_should_do_remote(mdev->state); |
957 | send_oos = rw == WRITE && drbd_should_send_oos(mdev->state); | |
3719094e | 958 | D_ASSERT(!(remote && send_oos)); |
73a01a18 PR |
959 | |
960 | if (!(remote || send_oos)) | |
b411b363 PR |
961 | dev_warn(DEV, "lost connection while grabbing the req_lock!\n"); |
962 | if (!(local || remote)) { | |
963 | dev_err(DEV, "IO ERROR: neither local nor remote disk\n"); | |
964 | spin_unlock_irq(&mdev->req_lock); | |
965 | goto fail_free_complete; | |
966 | } | |
967 | } | |
968 | ||
969 | if (b && mdev->unused_spare_tle == NULL) { | |
970 | mdev->unused_spare_tle = b; | |
971 | b = NULL; | |
972 | } | |
73a01a18 | 973 | if (rw == WRITE && (remote || send_oos) && |
b411b363 | 974 | mdev->unused_spare_tle == NULL && |
06f10adb | 975 | drbd_test_flag(mdev, CREATE_BARRIER)) { |
b411b363 PR |
976 | /* someone closed the current epoch |
977 | * while we were grabbing the spinlock */ | |
978 | spin_unlock_irq(&mdev->req_lock); | |
979 | goto allocate_barrier; | |
980 | } | |
981 | ||
982 | ||
983 | /* Update disk stats */ | |
984 | _drbd_start_io_acct(mdev, req, bio); | |
985 | ||
986 | /* _maybe_start_new_epoch(mdev); | |
987 | * If we need to generate a write barrier packet, we have to add the | |
988 | * new epoch (barrier) object, and queue the barrier packet for sending, | |
989 | * and queue the req's data after it _within the same lock_, otherwise | |
990 | * we have race conditions were the reorder domains could be mixed up. | |
991 | * | |
992 | * Even read requests may start a new epoch and queue the corresponding | |
993 | * barrier packet. To get the write ordering right, we only have to | |
994 | * make sure that, if this is a write request and it triggered a | |
995 | * barrier packet, this request is queued within the same spinlock. */ | |
73a01a18 | 996 | if ((remote || send_oos) && mdev->unused_spare_tle && |
06f10adb | 997 | drbd_test_and_clear_flag(mdev, CREATE_BARRIER)) { |
b411b363 PR |
998 | _tl_add_barrier(mdev, mdev->unused_spare_tle); |
999 | mdev->unused_spare_tle = NULL; | |
1000 | } else { | |
1001 | D_ASSERT(!(remote && rw == WRITE && | |
06f10adb | 1002 | drbd_test_flag(mdev, CREATE_BARRIER))); |
b411b363 PR |
1003 | } |
1004 | ||
1005 | /* NOTE | |
1006 | * Actually, 'local' may be wrong here already, since we may have failed | |
1007 | * to write to the meta data, and may become wrong anytime because of | |
1008 | * local io-error for some other request, which would lead to us | |
1009 | * "detaching" the local disk. | |
1010 | * | |
1011 | * 'remote' may become wrong any time because the network could fail. | |
1012 | * | |
1013 | * This is a harmless race condition, though, since it is handled | |
1014 | * correctly at the appropriate places; so it just defers the failure | |
1015 | * of the respective operation. | |
1016 | */ | |
1017 | ||
1018 | /* mark them early for readability. | |
1019 | * this just sets some state flags. */ | |
1020 | if (remote) | |
1021 | _req_mod(req, to_be_send); | |
1022 | if (local) | |
1023 | _req_mod(req, to_be_submitted); | |
1024 | ||
1025 | /* check this request on the collision detection hash tables. | |
1026 | * if we have a conflict, just complete it here. | |
1027 | * THINK do we want to check reads, too? (I don't think so...) */ | |
d28fd092 LE |
1028 | if (rw == WRITE && _req_conflicts(req)) |
1029 | goto fail_conflicting; | |
288f422e | 1030 | |
227f052f LE |
1031 | /* no point in adding empty flushes to the transfer log, |
1032 | * they are mapped to drbd barriers already. */ | |
1033 | if (likely(size!=0)) | |
1034 | list_add_tail(&req->tl_requests, &mdev->newest_tle->requests); | |
288f422e | 1035 | |
b411b363 PR |
1036 | /* NOTE remote first: to get the concurrent write detection right, |
1037 | * we must register the request before start of local IO. */ | |
1038 | if (remote) { | |
1039 | /* either WRITE and C_CONNECTED, | |
1040 | * or READ, and no local disk, | |
1041 | * or READ, but not in sync. | |
1042 | */ | |
1043 | _req_mod(req, (rw == WRITE) | |
1044 | ? queue_for_net_write | |
1045 | : queue_for_net_read); | |
1046 | } | |
73a01a18 PR |
1047 | if (send_oos && drbd_set_out_of_sync(mdev, sector, size)) |
1048 | _req_mod(req, queue_for_send_oos); | |
67531718 | 1049 | |
73a01a18 | 1050 | if (remote && |
0d5934e3 LE |
1051 | mdev->net_conf->on_congestion != OC_BLOCK && mdev->agreed_pro_version >= 96) |
1052 | maybe_pull_ahead(mdev); | |
67531718 | 1053 | |
227f052f LE |
1054 | /* If this was a flush, queue a drbd barrier/start a new epoch. |
1055 | * Unless the current epoch was empty anyways, or we are not currently | |
1056 | * replicating, in which case there is no point. */ | |
1057 | if (unlikely(bio->bi_rw & REQ_FLUSH) | |
1058 | && mdev->newest_tle->n_writes | |
1059 | && drbd_should_do_remote(mdev->state)) | |
1060 | queue_barrier(mdev); | |
1061 | ||
b411b363 PR |
1062 | spin_unlock_irq(&mdev->req_lock); |
1063 | kfree(b); /* if someone else has beaten us to it... */ | |
1064 | ||
1065 | if (local) { | |
1066 | req->private_bio->bi_bdev = mdev->ldev->backing_bdev; | |
1067 | ||
6719fb03 LE |
1068 | /* State may have changed since we grabbed our reference on the |
1069 | * mdev->ldev member. Double check, and short-circuit to endio. | |
1070 | * In case the last activity log transaction failed to get on | |
1071 | * stable storage, and this is a WRITE, we may not even submit | |
1072 | * this bio. */ | |
1073 | if (get_ldev(mdev)) { | |
0cf9d27e AG |
1074 | if (drbd_insert_fault(mdev, rw == WRITE ? DRBD_FAULT_DT_WR |
1075 | : rw == READ ? DRBD_FAULT_DT_RD | |
1076 | : DRBD_FAULT_DT_RA)) | |
6719fb03 LE |
1077 | bio_endio(req->private_bio, -EIO); |
1078 | else | |
1079 | generic_make_request(req->private_bio); | |
1080 | put_ldev(mdev); | |
1081 | } else | |
b411b363 | 1082 | bio_endio(req->private_bio, -EIO); |
b411b363 PR |
1083 | } |
1084 | ||
b411b363 PR |
1085 | return 0; |
1086 | ||
d28fd092 LE |
1087 | fail_conflicting: |
1088 | /* this is a conflicting request. | |
1089 | * even though it may have been only _partially_ | |
1090 | * overlapping with one of the currently pending requests, | |
1091 | * without even submitting or sending it, we will | |
1092 | * pretend that it was successfully served right now. | |
1093 | */ | |
1094 | _drbd_end_io_acct(mdev, req); | |
1095 | spin_unlock_irq(&mdev->req_lock); | |
1096 | if (remote) | |
1097 | dec_ap_pending(mdev); | |
1098 | /* THINK: do we want to fail it (-EIO), or pretend success? | |
1099 | * this pretends success. */ | |
1100 | err = 0; | |
1101 | ||
b411b363 | 1102 | fail_free_complete: |
76727f68 | 1103 | if (req->rq_state & RQ_IN_ACT_LOG) |
b411b363 PR |
1104 | drbd_al_complete_io(mdev, sector); |
1105 | fail_and_free_req: | |
1106 | if (local) { | |
1107 | bio_put(req->private_bio); | |
1108 | req->private_bio = NULL; | |
1109 | put_ldev(mdev); | |
1110 | } | |
9a25a04c PR |
1111 | if (!ret) |
1112 | bio_endio(bio, err); | |
1113 | ||
b411b363 PR |
1114 | drbd_req_free(req); |
1115 | dec_ap_bio(mdev); | |
1116 | kfree(b); | |
1117 | ||
9a25a04c | 1118 | return ret; |
b411b363 PR |
1119 | } |
1120 | ||
1121 | /* helper function for drbd_make_request | |
1122 | * if we can determine just by the mdev (state) that this request will fail, | |
1123 | * return 1 | |
1124 | * otherwise return 0 | |
1125 | */ | |
1126 | static int drbd_fail_request_early(struct drbd_conf *mdev, int is_write) | |
1127 | { | |
b411b363 PR |
1128 | if (mdev->state.role != R_PRIMARY && |
1129 | (!allow_oos || is_write)) { | |
1130 | if (__ratelimit(&drbd_ratelimit_state)) { | |
1131 | dev_err(DEV, "Process %s[%u] tried to %s; " | |
1132 | "since we are not in Primary state, " | |
1133 | "we cannot allow this\n", | |
1134 | current->comm, current->pid, | |
1135 | is_write ? "WRITE" : "READ"); | |
1136 | } | |
1137 | return 1; | |
1138 | } | |
1139 | ||
b411b363 PR |
1140 | return 0; |
1141 | } | |
1142 | ||
5a7bbad2 | 1143 | void drbd_make_request(struct request_queue *q, struct bio *bio) |
b411b363 PR |
1144 | { |
1145 | unsigned int s_enr, e_enr; | |
1146 | struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata; | |
aeda1cd6 | 1147 | unsigned long start_time; |
b411b363 PR |
1148 | |
1149 | if (drbd_fail_request_early(mdev, bio_data_dir(bio) & WRITE)) { | |
1150 | bio_endio(bio, -EPERM); | |
5a7bbad2 | 1151 | return; |
b411b363 PR |
1152 | } |
1153 | ||
aeda1cd6 PR |
1154 | start_time = jiffies; |
1155 | ||
b411b363 PR |
1156 | /* |
1157 | * what we "blindly" assume: | |
1158 | */ | |
b411b363 | 1159 | D_ASSERT((bio->bi_size & 0x1ff) == 0); |
b411b363 PR |
1160 | |
1161 | /* to make some things easier, force alignment of requests within the | |
1162 | * granularity of our hash tables */ | |
1163 | s_enr = bio->bi_sector >> HT_SHIFT; | |
a73ff323 | 1164 | e_enr = bio->bi_size ? (bio->bi_sector+(bio->bi_size>>9)-1) >> HT_SHIFT : s_enr; |
b411b363 PR |
1165 | |
1166 | if (likely(s_enr == e_enr)) { | |
f6d0a8db PR |
1167 | do { |
1168 | inc_ap_bio(mdev, 1); | |
1169 | } while (drbd_make_request_common(mdev, bio, start_time)); | |
5a7bbad2 | 1170 | return; |
b411b363 PR |
1171 | } |
1172 | ||
1173 | /* can this bio be split generically? | |
1174 | * Maybe add our own split-arbitrary-bios function. */ | |
1816a2b4 | 1175 | if (bio->bi_vcnt != 1 || bio->bi_idx != 0 || bio->bi_size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
1176 | /* rather error out here than BUG in bio_split */ |
1177 | dev_err(DEV, "bio would need to, but cannot, be split: " | |
1178 | "(vcnt=%u,idx=%u,size=%u,sector=%llu)\n", | |
1179 | bio->bi_vcnt, bio->bi_idx, bio->bi_size, | |
1180 | (unsigned long long)bio->bi_sector); | |
1181 | bio_endio(bio, -EINVAL); | |
1182 | } else { | |
1183 | /* This bio crosses some boundary, so we have to split it. */ | |
1184 | struct bio_pair *bp; | |
1185 | /* works for the "do not cross hash slot boundaries" case | |
1186 | * e.g. sector 262269, size 4096 | |
1187 | * s_enr = 262269 >> 6 = 4097 | |
1188 | * e_enr = (262269+8-1) >> 6 = 4098 | |
1189 | * HT_SHIFT = 6 | |
1190 | * sps = 64, mask = 63 | |
1191 | * first_sectors = 64 - (262269 & 63) = 3 | |
1192 | */ | |
1193 | const sector_t sect = bio->bi_sector; | |
1194 | const int sps = 1 << HT_SHIFT; /* sectors per slot */ | |
1195 | const int mask = sps - 1; | |
1196 | const sector_t first_sectors = sps - (sect & mask); | |
03567812 | 1197 | bp = bio_split(bio, first_sectors); |
b411b363 PR |
1198 | |
1199 | /* we need to get a "reference count" (ap_bio_cnt) | |
1200 | * to avoid races with the disconnect/reconnect/suspend code. | |
9a25a04c | 1201 | * In case we need to split the bio here, we need to get three references |
b411b363 PR |
1202 | * atomically, otherwise we might deadlock when trying to submit the |
1203 | * second one! */ | |
9a25a04c | 1204 | inc_ap_bio(mdev, 3); |
b411b363 PR |
1205 | |
1206 | D_ASSERT(e_enr == s_enr + 1); | |
1207 | ||
aeda1cd6 | 1208 | while (drbd_make_request_common(mdev, &bp->bio1, start_time)) |
9a25a04c PR |
1209 | inc_ap_bio(mdev, 1); |
1210 | ||
aeda1cd6 | 1211 | while (drbd_make_request_common(mdev, &bp->bio2, start_time)) |
9a25a04c PR |
1212 | inc_ap_bio(mdev, 1); |
1213 | ||
1214 | dec_ap_bio(mdev); | |
1215 | ||
b411b363 PR |
1216 | bio_pair_release(bp); |
1217 | } | |
b411b363 PR |
1218 | } |
1219 | ||
1220 | /* This is called by bio_add_page(). With this function we reduce | |
1816a2b4 | 1221 | * the number of BIOs that span over multiple DRBD_MAX_BIO_SIZEs |
b411b363 PR |
1222 | * units (was AL_EXTENTs). |
1223 | * | |
1224 | * we do the calculation within the lower 32bit of the byte offsets, | |
1225 | * since we don't care for actual offset, but only check whether it | |
1226 | * would cross "activity log extent" boundaries. | |
1227 | * | |
1228 | * As long as the BIO is empty we have to allow at least one bvec, | |
1229 | * regardless of size and offset. so the resulting bio may still | |
1230 | * cross extent boundaries. those are dealt with (bio_split) in | |
2f58dcfc | 1231 | * drbd_make_request. |
b411b363 PR |
1232 | */ |
1233 | int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec) | |
1234 | { | |
1235 | struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata; | |
1236 | unsigned int bio_offset = | |
1237 | (unsigned int)bvm->bi_sector << 9; /* 32 bit */ | |
1238 | unsigned int bio_size = bvm->bi_size; | |
1239 | int limit, backing_limit; | |
1240 | ||
1816a2b4 LE |
1241 | limit = DRBD_MAX_BIO_SIZE |
1242 | - ((bio_offset & (DRBD_MAX_BIO_SIZE-1)) + bio_size); | |
b411b363 PR |
1243 | if (limit < 0) |
1244 | limit = 0; | |
1245 | if (bio_size == 0) { | |
1246 | if (limit <= bvec->bv_len) | |
1247 | limit = bvec->bv_len; | |
1248 | } else if (limit && get_ldev(mdev)) { | |
1249 | struct request_queue * const b = | |
1250 | mdev->ldev->backing_bdev->bd_disk->queue; | |
a1c88d0d | 1251 | if (b->merge_bvec_fn) { |
b411b363 PR |
1252 | backing_limit = b->merge_bvec_fn(b, bvm, bvec); |
1253 | limit = min(limit, backing_limit); | |
1254 | } | |
1255 | put_ldev(mdev); | |
1256 | } | |
1257 | return limit; | |
1258 | } | |
7fde2be9 PR |
1259 | |
1260 | void request_timer_fn(unsigned long data) | |
1261 | { | |
1262 | struct drbd_conf *mdev = (struct drbd_conf *) data; | |
1263 | struct drbd_request *req; /* oldest request */ | |
1264 | struct list_head *le; | |
dfa8bedb | 1265 | unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */ |
ba280c09 | 1266 | unsigned long now; |
7fde2be9 PR |
1267 | |
1268 | if (get_net_conf(mdev)) { | |
ba280c09 LE |
1269 | if (mdev->state.conn >= C_WF_REPORT_PARAMS) |
1270 | ent = mdev->net_conf->timeout*HZ/10 | |
1271 | * mdev->net_conf->ko_count; | |
7fde2be9 PR |
1272 | put_net_conf(mdev); |
1273 | } | |
ba280c09 | 1274 | if (get_ldev(mdev)) { /* implicit state.disk >= D_INCONSISTENT */ |
dfa8bedb PR |
1275 | dt = mdev->ldev->dc.disk_timeout * HZ / 10; |
1276 | put_ldev(mdev); | |
1277 | } | |
1278 | et = min_not_zero(dt, ent); | |
1279 | ||
ba280c09 | 1280 | if (!et) |
7fde2be9 PR |
1281 | return; /* Recurring timer stopped */ |
1282 | ||
ba280c09 LE |
1283 | now = jiffies; |
1284 | ||
7fde2be9 PR |
1285 | spin_lock_irq(&mdev->req_lock); |
1286 | le = &mdev->oldest_tle->requests; | |
1287 | if (list_empty(le)) { | |
1288 | spin_unlock_irq(&mdev->req_lock); | |
ba280c09 | 1289 | mod_timer(&mdev->request_timer, now + et); |
7fde2be9 PR |
1290 | return; |
1291 | } | |
1292 | ||
1293 | le = le->prev; | |
1294 | req = list_entry(le, struct drbd_request, tl_requests); | |
ba280c09 LE |
1295 | |
1296 | /* The request is considered timed out, if | |
1297 | * - we have some effective timeout from the configuration, | |
1298 | * with above state restrictions applied, | |
1299 | * - the oldest request is waiting for a response from the network | |
1300 | * resp. the local disk, | |
1301 | * - the oldest request is in fact older than the effective timeout, | |
1302 | * - the connection was established (resp. disk was attached) | |
1303 | * for longer than the timeout already. | |
1304 | * Note that for 32bit jiffies and very stable connections/disks, | |
1305 | * we may have a wrap around, which is catched by | |
1306 | * !time_in_range(now, last_..._jif, last_..._jif + timeout). | |
1307 | * | |
1308 | * Side effect: once per 32bit wrap-around interval, which means every | |
1309 | * ~198 days with 250 HZ, we have a window where the timeout would need | |
1310 | * to expire twice (worst case) to become effective. Good enough. | |
1311 | */ | |
1312 | if (ent && req->rq_state & RQ_NET_PENDING && | |
1313 | time_after(now, req->start_time + ent) && | |
1314 | !time_in_range(now, mdev->last_reconnect_jif, mdev->last_reconnect_jif + ent)) { | |
1315 | dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n"); | |
1316 | _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL); | |
7fde2be9 | 1317 | } |
ba280c09 LE |
1318 | if (dt && req->rq_state & RQ_LOCAL_PENDING && |
1319 | time_after(now, req->start_time + dt) && | |
1320 | !time_in_range(now, mdev->last_reattach_jif, mdev->last_reattach_jif + dt)) { | |
1321 | dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n"); | |
383606e0 | 1322 | __drbd_chk_io_error(mdev, DRBD_FORCE_DETACH); |
dfa8bedb | 1323 | } |
ba280c09 | 1324 | nt = (time_after(now, req->start_time + et) ? now : req->start_time) + et; |
7fde2be9 | 1325 | spin_unlock_irq(&mdev->req_lock); |
dfa8bedb | 1326 | mod_timer(&mdev->request_timer, nt); |
7fde2be9 | 1327 | } |