drbd: Fix postponed requests
[deliverable/linux.git] / drivers / block / drbd / drbd_req.c
CommitLineData
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
57bcb6cf
PR
34static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size);
35
b411b363
PR
36/* Update disk stats at start of I/O request */
37static void _drbd_start_io_acct(struct drbd_conf *mdev, struct drbd_request *req, struct bio *bio)
38{
39 const int rw = bio_data_dir(bio);
40 int cpu;
41 cpu = part_stat_lock();
72585d24 42 part_round_stats(cpu, &mdev->vdisk->part0);
b411b363
PR
43 part_stat_inc(cpu, &mdev->vdisk->part0, ios[rw]);
44 part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], bio_sectors(bio));
376694a0
PR
45 (void) cpu; /* The macro invocations above want the cpu argument, I do not like
46 the compiler warning about cpu only assigned but never used... */
753c8913 47 part_inc_in_flight(&mdev->vdisk->part0, rw);
b411b363 48 part_stat_unlock();
b411b363
PR
49}
50
51/* Update disk stats when completing request upwards */
52static void _drbd_end_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
53{
54 int rw = bio_data_dir(req->master_bio);
55 unsigned long duration = jiffies - req->start_time;
56 int cpu;
57 cpu = part_stat_lock();
58 part_stat_add(cpu, &mdev->vdisk->part0, ticks[rw], duration);
59 part_round_stats(cpu, &mdev->vdisk->part0);
753c8913 60 part_dec_in_flight(&mdev->vdisk->part0, rw);
b411b363 61 part_stat_unlock();
b411b363
PR
62}
63
9e204cdd
AG
64static struct drbd_request *drbd_req_new(struct drbd_conf *mdev,
65 struct bio *bio_src)
66{
67 struct drbd_request *req;
68
69 req = mempool_alloc(drbd_request_mempool, GFP_NOIO);
70 if (!req)
71 return NULL;
72
73 drbd_req_make_private_bio(req, bio_src);
74 req->rq_state = bio_data_dir(bio_src) == WRITE ? RQ_WRITE : 0;
a21e9298 75 req->w.mdev = mdev;
9e204cdd
AG
76 req->master_bio = bio_src;
77 req->epoch = 0;
53840641 78
9e204cdd
AG
79 drbd_clear_interval(&req->i);
80 req->i.sector = bio_src->bi_sector;
81 req->i.size = bio_src->bi_size;
5e472264 82 req->i.local = true;
53840641
AG
83 req->i.waiting = false;
84
9e204cdd
AG
85 INIT_LIST_HEAD(&req->tl_requests);
86 INIT_LIST_HEAD(&req->w.list);
87
a0d856df 88 /* one reference to be put by __drbd_make_request */
b406777e 89 atomic_set(&req->completion_ref, 1);
a0d856df 90 /* one kref as long as completion_ref > 0 */
b406777e 91 kref_init(&req->kref);
9e204cdd
AG
92 return req;
93}
94
9a278a79 95void drbd_req_destroy(struct kref *kref)
b411b363 96{
b406777e
LE
97 struct drbd_request *req = container_of(kref, struct drbd_request, kref);
98 struct drbd_conf *mdev = req->w.mdev;
a0d856df
LE
99 const unsigned s = req->rq_state;
100
101 if ((req->master_bio && !(s & RQ_POSTPONED)) ||
102 atomic_read(&req->completion_ref) ||
103 (s & RQ_LOCAL_PENDING) ||
104 ((s & RQ_NET_MASK) && !(s & RQ_NET_DONE))) {
105 dev_err(DEV, "drbd_req_destroy: Logic BUG rq_state = 0x%x, completion_ref = %d\n",
106 s, atomic_read(&req->completion_ref));
107 return;
108 }
288f422e
PR
109
110 /* remove it from the transfer log.
111 * well, only if it had been there in the first
112 * place... if it had not (local only or conflicting
113 * and never sent), it should still be "empty" as
114 * initialized in drbd_req_new(), so we can list_del() it
115 * here unconditionally */
2312f0b3 116 list_del_init(&req->tl_requests);
288f422e 117
b411b363
PR
118 /* if it was a write, we may have to set the corresponding
119 * bit(s) out-of-sync first. If it had a local part, we need to
120 * release the reference to the activity log. */
b406777e 121 if (s & RQ_WRITE) {
b411b363
PR
122 /* Set out-of-sync unless both OK flags are set
123 * (local only or remote failed).
124 * Other places where we set out-of-sync:
125 * READ with local io-error */
d7644018
PR
126 if (!(s & RQ_POSTPONED)) {
127 if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
128 drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
b411b363 129
d7644018
PR
130 if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
131 drbd_set_in_sync(mdev, req->i.sector, req->i.size);
132 }
b411b363
PR
133
134 /* one might be tempted to move the drbd_al_complete_io
fcefa62e 135 * to the local io completion callback drbd_request_endio.
b411b363
PR
136 * but, if this was a mirror write, we may only
137 * drbd_al_complete_io after this is RQ_NET_DONE,
138 * otherwise the extent could be dropped from the al
139 * before it has actually been written on the peer.
140 * if we crash before our peer knows about the request,
141 * but after the extent has been dropped from the al,
142 * we would forget to resync the corresponding extent.
143 */
76590cd1 144 if (s & RQ_IN_ACT_LOG) {
b411b363 145 if (get_ldev_if_state(mdev, D_FAILED)) {
76590cd1 146 drbd_al_complete_io(mdev, &req->i);
b411b363
PR
147 put_ldev(mdev);
148 } else if (__ratelimit(&drbd_ratelimit_state)) {
181286ad
LE
149 dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu, %u), "
150 "but my Disk seems to have failed :(\n",
151 (unsigned long long) req->i.sector, req->i.size);
b411b363
PR
152 }
153 }
154 }
155
9a278a79 156 mempool_free(req, drbd_request_mempool);
b411b363
PR
157}
158
b6dd1a89
LE
159static void wake_all_senders(struct drbd_tconn *tconn) {
160 wake_up(&tconn->sender_work.q_wait);
b411b363
PR
161}
162
b6dd1a89
LE
163/* must hold resource->req_lock */
164static void start_new_tl_epoch(struct drbd_tconn *tconn)
b411b363 165{
99b4d8fe
LE
166 /* no point closing an epoch, if it is empty, anyways. */
167 if (tconn->current_tle_writes == 0)
168 return;
169
b6dd1a89
LE
170 tconn->current_tle_writes = 0;
171 atomic_inc(&tconn->current_tle_nr);
172 wake_all_senders(tconn);
b411b363
PR
173}
174
175void complete_master_bio(struct drbd_conf *mdev,
176 struct bio_and_error *m)
177{
b411b363
PR
178 bio_endio(m->bio, m->error);
179 dec_ap_bio(mdev);
180}
181
53840641
AG
182
183static void drbd_remove_request_interval(struct rb_root *root,
184 struct drbd_request *req)
185{
a21e9298 186 struct drbd_conf *mdev = req->w.mdev;
53840641
AG
187 struct drbd_interval *i = &req->i;
188
189 drbd_remove_interval(root, i);
190
191 /* Wake up any processes waiting for this request to complete. */
192 if (i->waiting)
193 wake_up(&mdev->misc_wait);
194}
195
b411b363
PR
196/* Helper for __req_mod().
197 * Set m->bio to the master bio, if it is fit to be completed,
198 * or leave it alone (it is initialized to NULL in __req_mod),
199 * if it has already been completed, or cannot be completed yet.
200 * If m->bio is set, the error status to be returned is placed in m->error.
201 */
6870ca6d 202static
a0d856df 203void drbd_req_complete(struct drbd_request *req, struct bio_and_error *m)
b411b363 204{
a0d856df 205 const unsigned s = req->rq_state;
a21e9298 206 struct drbd_conf *mdev = req->w.mdev;
a0d856df
LE
207 int rw;
208 int error, ok;
b411b363 209
b411b363
PR
210 /* we must not complete the master bio, while it is
211 * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
212 * not yet acknowledged by the peer
213 * not yet completed by the local io subsystem
214 * these flags may get cleared in any order by
215 * the worker,
216 * the receiver,
217 * the bio_endio completion callbacks.
218 */
a0d856df
LE
219 if ((s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED)) ||
220 (s & RQ_NET_QUEUED) || (s & RQ_NET_PENDING) ||
221 (s & RQ_COMPLETION_SUSP)) {
222 dev_err(DEV, "drbd_req_complete: Logic BUG rq_state = 0x%x\n", s);
b411b363 223 return;
a0d856df
LE
224 }
225
226 if (!req->master_bio) {
227 dev_err(DEV, "drbd_req_complete: Logic BUG, master_bio == NULL!\n");
b411b363 228 return;
a0d856df 229 }
b411b363 230
a0d856df 231 rw = bio_rw(req->master_bio);
b406777e 232
a0d856df
LE
233 /*
234 * figure out whether to report success or failure.
235 *
236 * report success when at least one of the operations succeeded.
237 * or, to put the other way,
238 * only report failure, when both operations failed.
239 *
240 * what to do about the failures is handled elsewhere.
241 * what we need to do here is just: complete the master_bio.
242 *
243 * local completion error, if any, has been stored as ERR_PTR
244 * in private_bio within drbd_request_endio.
245 */
246 ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
247 error = PTR_ERR(req->private_bio);
4439c400 248
a0d856df
LE
249 /* remove the request from the conflict detection
250 * respective block_id verification hash */
251 if (!drbd_interval_empty(&req->i)) {
252 struct rb_root *root;
b411b363 253
a0d856df
LE
254 if (rw == WRITE)
255 root = &mdev->write_requests;
256 else
257 root = &mdev->read_requests;
258 drbd_remove_request_interval(root, req);
259 } else if (!(s & RQ_POSTPONED))
260 D_ASSERT((s & (RQ_NET_MASK & ~RQ_NET_DONE)) == 0);
261
262 /* Before we can signal completion to the upper layers,
263 * we may need to close the current transfer log epoch.
264 * We are within the request lock, so we can simply compare
265 * the request epoch number with the current transfer log
266 * epoch number. If they match, increase the current_tle_nr,
267 * and reset the transfer log epoch write_cnt.
268 */
269 if (rw == WRITE &&
270 req->epoch == atomic_read(&mdev->tconn->current_tle_nr))
271 start_new_tl_epoch(mdev->tconn);
b411b363 272
a0d856df
LE
273 /* Update disk stats */
274 _drbd_end_io_acct(mdev, req);
b411b363 275
a0d856df
LE
276 /* If READ failed,
277 * have it be pushed back to the retry work queue,
278 * so it will re-enter __drbd_make_request(),
279 * and be re-assigned to a suitable local or remote path,
280 * or failed if we do not have access to good data anymore.
281 *
282 * Unless it was failed early by __drbd_make_request(),
283 * because no path was available, in which case
284 * it was not even added to the transfer_log.
285 *
286 * READA may fail, and will not be retried.
287 *
288 * WRITE should have used all available paths already.
289 */
290 if (!ok && rw == READ && !list_empty(&req->tl_requests))
291 req->rq_state |= RQ_POSTPONED;
292
293 if (!(req->rq_state & RQ_POSTPONED)) {
294 m->error = ok ? 0 : (error ?: -EIO);
295 m->bio = req->master_bio;
296 req->master_bio = NULL;
b411b363 297 }
b411b363
PR
298}
299
a0d856df 300static int drbd_req_put_completion_ref(struct drbd_request *req, struct bio_and_error *m, int put)
cfa03415 301{
a21e9298 302 struct drbd_conf *mdev = req->w.mdev;
a0d856df
LE
303 D_ASSERT(m || (req->rq_state & RQ_POSTPONED));
304
305 if (!atomic_sub_and_test(put, &req->completion_ref))
306 return 0;
307
a0d856df 308 drbd_req_complete(req, m);
9a278a79
LE
309
310 if (req->rq_state & RQ_POSTPONED) {
311 /* don't destroy the req object just yet,
312 * but queue it for retry */
313 drbd_restart_request(req);
314 return 0;
315 }
316
a0d856df
LE
317 return 1;
318}
319
320/* I'd like this to be the only place that manipulates
321 * req->completion_ref and req->kref. */
322static void mod_rq_state(struct drbd_request *req, struct bio_and_error *m,
323 int clear, int set)
324{
325 struct drbd_conf *mdev = req->w.mdev;
326 unsigned s = req->rq_state;
327 int c_put = 0;
328 int k_put = 0;
329
5af2e8ce
PR
330 if (drbd_suspended(mdev) && !((s | clear) & RQ_COMPLETION_SUSP))
331 set |= RQ_COMPLETION_SUSP;
332
a0d856df
LE
333 /* apply */
334
335 req->rq_state &= ~clear;
336 req->rq_state |= set;
337
338 /* no change? */
339 if (req->rq_state == s)
340 return;
341
342 /* intent: get references */
343
344 if (!(s & RQ_LOCAL_PENDING) && (set & RQ_LOCAL_PENDING))
345 atomic_inc(&req->completion_ref);
346
347 if (!(s & RQ_NET_PENDING) && (set & RQ_NET_PENDING)) {
348 inc_ap_pending(mdev);
349 atomic_inc(&req->completion_ref);
350 }
351
352 if (!(s & RQ_NET_QUEUED) && (set & RQ_NET_QUEUED))
353 atomic_inc(&req->completion_ref);
354
355 if (!(s & RQ_EXP_BARR_ACK) && (set & RQ_EXP_BARR_ACK))
356 kref_get(&req->kref); /* wait for the DONE */
357
358 if (!(s & RQ_NET_SENT) && (set & RQ_NET_SENT))
359 atomic_add(req->i.size >> 9, &mdev->ap_in_flight);
360
5af2e8ce
PR
361 if (!(s & RQ_COMPLETION_SUSP) && (set & RQ_COMPLETION_SUSP))
362 atomic_inc(&req->completion_ref);
363
a0d856df
LE
364 /* progress: put references */
365
366 if ((s & RQ_COMPLETION_SUSP) && (clear & RQ_COMPLETION_SUSP))
367 ++c_put;
368
369 if (!(s & RQ_LOCAL_ABORTED) && (set & RQ_LOCAL_ABORTED)) {
370 D_ASSERT(req->rq_state & RQ_LOCAL_PENDING);
371 /* local completion may still come in later,
372 * we need to keep the req object around. */
373 kref_get(&req->kref);
374 ++c_put;
375 }
376
377 if ((s & RQ_LOCAL_PENDING) && (clear & RQ_LOCAL_PENDING)) {
378 if (req->rq_state & RQ_LOCAL_ABORTED)
379 ++k_put;
380 else
381 ++c_put;
382 }
383
384 if ((s & RQ_NET_PENDING) && (clear & RQ_NET_PENDING)) {
385 dec_ap_pending(mdev);
386 ++c_put;
387 }
388
389 if ((s & RQ_NET_QUEUED) && (clear & RQ_NET_QUEUED))
390 ++c_put;
391
392 if ((s & RQ_EXP_BARR_ACK) && !(s & RQ_NET_DONE) && (set & RQ_NET_DONE)) {
393 if (req->rq_state & RQ_NET_SENT)
394 atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
395 ++k_put;
396 }
397
398 /* potentially complete and destroy */
399
400 if (k_put || c_put) {
401 /* Completion does it's own kref_put. If we are going to
402 * kref_sub below, we need req to be still around then. */
403 int at_least = k_put + !!c_put;
404 int refcount = atomic_read(&req->kref.refcount);
405 if (refcount < at_least)
406 dev_err(DEV,
407 "mod_rq_state: Logic BUG: %x -> %x: refcount = %d, should be >= %d\n",
408 s, req->rq_state, refcount, at_least);
409 }
410
411 /* If we made progress, retry conflicting peer requests, if any. */
412 if (req->i.waiting)
413 wake_up(&mdev->misc_wait);
414
415 if (c_put)
416 k_put += drbd_req_put_completion_ref(req, m, c_put);
417 if (k_put)
418 kref_sub(&req->kref, k_put, drbd_req_destroy);
cfa03415
PR
419}
420
b411b363
PR
421/* obviously this could be coded as many single functions
422 * instead of one huge switch,
423 * or by putting the code directly in the respective locations
424 * (as it has been before).
425 *
426 * but having it this way
427 * enforces that it is all in this one place, where it is easier to audit,
428 * it makes it obvious that whatever "event" "happens" to a request should
429 * happen "atomically" within the req_lock,
430 * and it enforces that we have to think in a very structured manner
431 * about the "events" that may happen to a request during its life time ...
432 */
2a80699f 433int __req_mod(struct drbd_request *req, enum drbd_req_event what,
b411b363
PR
434 struct bio_and_error *m)
435{
a21e9298 436 struct drbd_conf *mdev = req->w.mdev;
44ed167d 437 struct net_conf *nc;
303d1448 438 int p, rv = 0;
7be8da07
AG
439
440 if (m)
441 m->bio = NULL;
b411b363 442
b411b363
PR
443 switch (what) {
444 default:
445 dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
446 break;
447
448 /* does not happen...
449 * initialization done in drbd_req_new
8554df1c 450 case CREATED:
b411b363
PR
451 break;
452 */
453
8554df1c 454 case TO_BE_SENT: /* via network */
7be8da07 455 /* reached via __drbd_make_request
b411b363
PR
456 * and from w_read_retry_remote */
457 D_ASSERT(!(req->rq_state & RQ_NET_MASK));
44ed167d
PR
458 rcu_read_lock();
459 nc = rcu_dereference(mdev->tconn->net_conf);
460 p = nc->wire_protocol;
461 rcu_read_unlock();
303d1448
PR
462 req->rq_state |=
463 p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
464 p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
a0d856df 465 mod_rq_state(req, m, 0, RQ_NET_PENDING);
b411b363
PR
466 break;
467
8554df1c 468 case TO_BE_SUBMITTED: /* locally */
7be8da07 469 /* reached via __drbd_make_request */
b411b363 470 D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
a0d856df 471 mod_rq_state(req, m, 0, RQ_LOCAL_PENDING);
b411b363
PR
472 break;
473
8554df1c 474 case COMPLETED_OK:
cdfda633 475 if (req->rq_state & RQ_WRITE)
ace652ac 476 mdev->writ_cnt += req->i.size >> 9;
b411b363 477 else
ace652ac 478 mdev->read_cnt += req->i.size >> 9;
b411b363 479
a0d856df
LE
480 mod_rq_state(req, m, RQ_LOCAL_PENDING,
481 RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
b411b363
PR
482 break;
483
cdfda633 484 case ABORT_DISK_IO:
a0d856df 485 mod_rq_state(req, m, 0, RQ_LOCAL_ABORTED);
b411b363
PR
486 break;
487
8554df1c 488 case READ_COMPLETED_WITH_ERROR:
ace652ac 489 drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
a0d856df
LE
490 /* fall through. */
491 case WRITE_COMPLETED_WITH_ERROR:
0c849666 492 __drbd_chk_io_error(mdev, DRBD_IO_ERROR);
a0d856df
LE
493 /* fall through. */
494 case READ_AHEAD_COMPLETED_WITH_ERROR:
495 /* it is legal to fail READA, no __drbd_chk_io_error in that case. */
496 mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
4439c400 497 break;
b411b363 498
8554df1c 499 case QUEUE_FOR_NET_READ:
b411b363
PR
500 /* READ or READA, and
501 * no local disk,
502 * or target area marked as invalid,
503 * or just got an io-error. */
7be8da07 504 /* from __drbd_make_request
b411b363
PR
505 * or from bio_endio during read io-error recovery */
506
6870ca6d
LE
507 /* So we can verify the handle in the answer packet.
508 * Corresponding drbd_remove_request_interval is in
a0d856df 509 * drbd_req_complete() */
97ddb687 510 D_ASSERT(drbd_interval_empty(&req->i));
dac1389c 511 drbd_insert_interval(&mdev->read_requests, &req->i);
b411b363 512
83c38830 513 set_bit(UNPLUG_REMOTE, &mdev->flags);
b411b363
PR
514
515 D_ASSERT(req->rq_state & RQ_NET_PENDING);
4439c400 516 D_ASSERT((req->rq_state & RQ_LOCAL_MASK) == 0);
a0d856df 517 mod_rq_state(req, m, 0, RQ_NET_QUEUED);
4439c400 518 req->w.cb = w_send_read_req;
d5b27b01 519 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
b411b363
PR
520 break;
521
8554df1c 522 case QUEUE_FOR_NET_WRITE:
b411b363 523 /* assert something? */
7be8da07 524 /* from __drbd_make_request only */
b411b363 525
6870ca6d 526 /* Corresponding drbd_remove_request_interval is in
a0d856df 527 * drbd_req_complete() */
97ddb687 528 D_ASSERT(drbd_interval_empty(&req->i));
de696716 529 drbd_insert_interval(&mdev->write_requests, &req->i);
b411b363
PR
530
531 /* NOTE
532 * In case the req ended up on the transfer log before being
533 * queued on the worker, it could lead to this request being
534 * missed during cleanup after connection loss.
535 * So we have to do both operations here,
536 * within the same lock that protects the transfer log.
537 *
538 * _req_add_to_epoch(req); this has to be after the
539 * _maybe_start_new_epoch(req); which happened in
7be8da07 540 * __drbd_make_request, because we now may set the bit
b411b363
PR
541 * again ourselves to close the current epoch.
542 *
543 * Add req to the (now) current epoch (barrier). */
544
83c38830
LE
545 /* otherwise we may lose an unplug, which may cause some remote
546 * io-scheduler timeout to expire, increasing maximum latency,
547 * hurting performance. */
548 set_bit(UNPLUG_REMOTE, &mdev->flags);
549
b411b363
PR
550 /* queue work item to send data */
551 D_ASSERT(req->rq_state & RQ_NET_PENDING);
a0d856df 552 mod_rq_state(req, m, 0, RQ_NET_QUEUED|RQ_EXP_BARR_ACK);
b411b363 553 req->w.cb = w_send_dblock;
d5b27b01 554 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
b411b363
PR
555
556 /* close the epoch, in case it outgrew the limit */
44ed167d
PR
557 rcu_read_lock();
558 nc = rcu_dereference(mdev->tconn->net_conf);
559 p = nc->max_epoch_size;
560 rcu_read_unlock();
b6dd1a89
LE
561 if (mdev->tconn->current_tle_writes >= p)
562 start_new_tl_epoch(mdev->tconn);
b411b363
PR
563
564 break;
565
8554df1c 566 case QUEUE_FOR_SEND_OOS:
a0d856df 567 mod_rq_state(req, m, 0, RQ_NET_QUEUED);
8f7bed77 568 req->w.cb = w_send_out_of_sync;
d5b27b01 569 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
73a01a18
PR
570 break;
571
ea9d6729 572 case READ_RETRY_REMOTE_CANCELED:
8554df1c 573 case SEND_CANCELED:
8554df1c 574 case SEND_FAILED:
b411b363
PR
575 /* real cleanup will be done from tl_clear. just update flags
576 * so it is no longer marked as on the worker queue */
a0d856df 577 mod_rq_state(req, m, RQ_NET_QUEUED, 0);
b411b363
PR
578 break;
579
8554df1c 580 case HANDED_OVER_TO_NETWORK:
b411b363
PR
581 /* assert something? */
582 if (bio_data_dir(req->master_bio) == WRITE &&
303d1448 583 !(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK))) {
b411b363
PR
584 /* this is what is dangerous about protocol A:
585 * pretend it was successfully written on the peer. */
a0d856df
LE
586 if (req->rq_state & RQ_NET_PENDING)
587 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
588 /* else: neg-ack was faster... */
b411b363
PR
589 /* it is still not yet RQ_NET_DONE until the
590 * corresponding epoch barrier got acked as well,
591 * so we know what to dirty on connection loss */
592 }
a0d856df 593 mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_SENT);
27a434fe
LE
594 break;
595
596 case OOS_HANDED_TO_NETWORK:
597 /* Was not set PENDING, no longer QUEUED, so is now DONE
598 * as far as this connection is concerned. */
a0d856df 599 mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_DONE);
b411b363
PR
600 break;
601
8554df1c 602 case CONNECTION_LOST_WHILE_PENDING:
b411b363 603 /* transfer log cleanup after connection loss */
a0d856df
LE
604 mod_rq_state(req, m,
605 RQ_NET_OK|RQ_NET_PENDING|RQ_COMPLETION_SUSP,
606 RQ_NET_DONE);
b411b363
PR
607 break;
608
d4dabbe2
LE
609 case CONFLICT_RESOLVED:
610 /* for superseded conflicting writes of multiple primaries,
b411b363 611 * there is no need to keep anything in the tl, potential
934722a2
LE
612 * node crashes are covered by the activity log.
613 *
614 * If this request had been marked as RQ_POSTPONED before,
d4dabbe2 615 * it will actually not be completed, but "restarted",
934722a2
LE
616 * resubmitted from the retry worker context. */
617 D_ASSERT(req->rq_state & RQ_NET_PENDING);
618 D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
619 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_DONE|RQ_NET_OK);
620 break;
621
0afd569a 622 case WRITE_ACKED_BY_PEER_AND_SIS:
934722a2 623 req->rq_state |= RQ_NET_SIS;
8554df1c 624 case WRITE_ACKED_BY_PEER:
303d1448 625 D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
b411b363 626 /* protocol C; successfully written on peer.
0afd569a 627 * Nothing more to do here.
b411b363 628 * We want to keep the tl in place for all protocols, to cater
0afd569a 629 * for volatile write-back caches on lower level devices. */
b411b363 630
303d1448 631 goto ack_common;
8554df1c 632 case RECV_ACKED_BY_PEER:
303d1448 633 D_ASSERT(req->rq_state & RQ_EXP_RECEIVE_ACK);
b411b363 634 /* protocol B; pretends to be successfully written on peer.
8554df1c 635 * see also notes above in HANDED_OVER_TO_NETWORK about
b411b363 636 * protocol != C */
303d1448 637 ack_common:
b411b363 638 D_ASSERT(req->rq_state & RQ_NET_PENDING);
a0d856df 639 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
b411b363
PR
640 break;
641
7be8da07 642 case POSTPONE_WRITE:
303d1448
PR
643 D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
644 /* If this node has already detected the write conflict, the
7be8da07
AG
645 * worker will be waiting on misc_wait. Wake it up once this
646 * request has completed locally.
647 */
648 D_ASSERT(req->rq_state & RQ_NET_PENDING);
649 req->rq_state |= RQ_POSTPONED;
a0d856df
LE
650 if (req->i.waiting)
651 wake_up(&mdev->misc_wait);
652 /* Do not clear RQ_NET_PENDING. This request will make further
653 * progress via restart_conflicting_writes() or
654 * fail_postponed_requests(). Hopefully. */
7be8da07
AG
655 break;
656
8554df1c 657 case NEG_ACKED:
46e21bba 658 mod_rq_state(req, m, RQ_NET_OK|RQ_NET_PENDING, 0);
b411b363
PR
659 break;
660
8554df1c 661 case FAIL_FROZEN_DISK_IO:
265be2d0
PR
662 if (!(req->rq_state & RQ_LOCAL_COMPLETED))
663 break;
a0d856df 664 mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
265be2d0
PR
665 break;
666
8554df1c 667 case RESTART_FROZEN_DISK_IO:
265be2d0
PR
668 if (!(req->rq_state & RQ_LOCAL_COMPLETED))
669 break;
670
a0d856df
LE
671 mod_rq_state(req, m,
672 RQ_COMPLETION_SUSP|RQ_LOCAL_COMPLETED,
673 RQ_LOCAL_PENDING);
265be2d0
PR
674
675 rv = MR_READ;
676 if (bio_data_dir(req->master_bio) == WRITE)
677 rv = MR_WRITE;
678
a0d856df 679 get_ldev(mdev); /* always succeeds in this call path */
265be2d0 680 req->w.cb = w_restart_disk_io;
d5b27b01 681 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
265be2d0
PR
682 break;
683
8554df1c 684 case RESEND:
8a0bab2a
PR
685 /* Simply complete (local only) READs. */
686 if (!(req->rq_state & RQ_WRITE) && !req->w.cb) {
687 mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
688 break;
689 }
690
11b58e73 691 /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
a0d856df
LE
692 before the connection loss (B&C only); only P_BARRIER_ACK
693 (or the local completion?) was missing when we suspended.
6870ca6d
LE
694 Throwing them out of the TL here by pretending we got a BARRIER_ACK.
695 During connection handshake, we ensure that the peer was not rebooted. */
11b58e73 696 if (!(req->rq_state & RQ_NET_OK)) {
a0d856df
LE
697 /* FIXME could this possibly be a req->w.cb == w_send_out_of_sync?
698 * in that case we must not set RQ_NET_PENDING. */
699
700 mod_rq_state(req, m, RQ_COMPLETION_SUSP, RQ_NET_QUEUED|RQ_NET_PENDING);
11b58e73 701 if (req->w.cb) {
d5b27b01 702 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
11b58e73 703 rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
a0d856df 704 } /* else: FIXME can this happen? */
11b58e73
PR
705 break;
706 }
8554df1c 707 /* else, fall through to BARRIER_ACKED */
11b58e73 708
8554df1c 709 case BARRIER_ACKED:
a0d856df 710 /* barrier ack for READ requests does not make sense */
288f422e
PR
711 if (!(req->rq_state & RQ_WRITE))
712 break;
713
b411b363 714 if (req->rq_state & RQ_NET_PENDING) {
a209b4ae 715 /* barrier came in before all requests were acked.
b411b363
PR
716 * this is bad, because if the connection is lost now,
717 * we won't be able to clean them up... */
8554df1c 718 dev_err(DEV, "FIXME (BARRIER_ACKED but pending)\n");
b411b363 719 }
a0d856df
LE
720 /* Allowed to complete requests, even while suspended.
721 * As this is called for all requests within a matching epoch,
722 * we need to filter, and only set RQ_NET_DONE for those that
723 * have actually been on the wire. */
724 mod_rq_state(req, m, RQ_COMPLETION_SUSP,
725 (req->rq_state & RQ_NET_MASK) ? RQ_NET_DONE : 0);
b411b363
PR
726 break;
727
8554df1c 728 case DATA_RECEIVED:
b411b363 729 D_ASSERT(req->rq_state & RQ_NET_PENDING);
a0d856df 730 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK|RQ_NET_DONE);
b411b363
PR
731 break;
732 };
2a80699f
PR
733
734 return rv;
b411b363
PR
735}
736
737/* we may do a local read if:
738 * - we are consistent (of course),
739 * - or we are generally inconsistent,
740 * BUT we are still/already IN SYNC for this area.
741 * since size may be bigger than BM_BLOCK_SIZE,
742 * we may need to check several bits.
743 */
0da34df0 744static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
b411b363
PR
745{
746 unsigned long sbnr, ebnr;
747 sector_t esector, nr_sectors;
748
749 if (mdev->state.disk == D_UP_TO_DATE)
0da34df0 750 return true;
8c387def 751 if (mdev->state.disk != D_INCONSISTENT)
0da34df0 752 return false;
b411b363 753 esector = sector + (size >> 9) - 1;
8ca9844f 754 nr_sectors = drbd_get_capacity(mdev->this_bdev);
b411b363
PR
755 D_ASSERT(sector < nr_sectors);
756 D_ASSERT(esector < nr_sectors);
757
758 sbnr = BM_SECT_TO_BIT(sector);
759 ebnr = BM_SECT_TO_BIT(esector);
760
0da34df0 761 return drbd_bm_count_bits(mdev, sbnr, ebnr) == 0;
b411b363
PR
762}
763
5da9c836
LE
764static bool remote_due_to_read_balancing(struct drbd_conf *mdev, sector_t sector,
765 enum drbd_read_balancing rbm)
380207d0 766{
380207d0 767 struct backing_dev_info *bdi;
d60de03a 768 int stripe_shift;
380207d0 769
380207d0
PR
770 switch (rbm) {
771 case RB_CONGESTED_REMOTE:
772 bdi = &mdev->ldev->backing_bdev->bd_disk->queue->backing_dev_info;
773 return bdi_read_congested(bdi);
774 case RB_LEAST_PENDING:
775 return atomic_read(&mdev->local_cnt) >
776 atomic_read(&mdev->ap_pending_cnt) + atomic_read(&mdev->rs_pending_cnt);
d60de03a
PR
777 case RB_32K_STRIPING: /* stripe_shift = 15 */
778 case RB_64K_STRIPING:
779 case RB_128K_STRIPING:
780 case RB_256K_STRIPING:
781 case RB_512K_STRIPING:
782 case RB_1M_STRIPING: /* stripe_shift = 20 */
783 stripe_shift = (rbm - RB_32K_STRIPING + 15);
784 return (sector >> (stripe_shift - 9)) & 1;
380207d0
PR
785 case RB_ROUND_ROBIN:
786 return test_and_change_bit(READ_BALANCE_RR, &mdev->flags);
787 case RB_PREFER_REMOTE:
788 return true;
789 case RB_PREFER_LOCAL:
790 default:
791 return false;
792 }
793}
794
6024fece
AG
795/*
796 * complete_conflicting_writes - wait for any conflicting write requests
797 *
798 * The write_requests tree contains all active write requests which we
799 * currently know about. Wait for any requests to complete which conflict with
800 * the new one.
648e46b5
LE
801 *
802 * Only way out: remove the conflicting intervals from the tree.
6024fece 803 */
648e46b5 804static void complete_conflicting_writes(struct drbd_request *req)
6024fece 805{
648e46b5
LE
806 DEFINE_WAIT(wait);
807 struct drbd_conf *mdev = req->w.mdev;
808 struct drbd_interval *i;
809 sector_t sector = req->i.sector;
810 int size = req->i.size;
811
812 i = drbd_find_overlap(&mdev->write_requests, sector, size);
813 if (!i)
814 return;
6024fece 815
648e46b5
LE
816 for (;;) {
817 prepare_to_wait(&mdev->misc_wait, &wait, TASK_UNINTERRUPTIBLE);
6024fece
AG
818 i = drbd_find_overlap(&mdev->write_requests, sector, size);
819 if (!i)
648e46b5
LE
820 break;
821 /* Indicate to wake up device->misc_wait on progress. */
822 i->waiting = true;
823 spin_unlock_irq(&mdev->tconn->req_lock);
824 schedule();
825 spin_lock_irq(&mdev->tconn->req_lock);
6024fece 826 }
648e46b5 827 finish_wait(&mdev->misc_wait, &wait);
6024fece
AG
828}
829
5da9c836 830/* called within req_lock and rcu_read_lock() */
3b9ef85e 831static void maybe_pull_ahead(struct drbd_conf *mdev)
5da9c836
LE
832{
833 struct drbd_tconn *tconn = mdev->tconn;
834 struct net_conf *nc;
835 bool congested = false;
836 enum drbd_on_congestion on_congestion;
837
838 nc = rcu_dereference(tconn->net_conf);
839 on_congestion = nc ? nc->on_congestion : OC_BLOCK;
840 if (on_congestion == OC_BLOCK ||
841 tconn->agreed_pro_version < 96)
3b9ef85e
LE
842 return;
843
844 /* If I don't even have good local storage, we can not reasonably try
845 * to pull ahead of the peer. We also need the local reference to make
846 * sure mdev->act_log is there.
847 */
848 if (!get_ldev_if_state(mdev, D_UP_TO_DATE))
849 return;
5da9c836
LE
850
851 if (nc->cong_fill &&
852 atomic_read(&mdev->ap_in_flight) >= nc->cong_fill) {
853 dev_info(DEV, "Congestion-fill threshold reached\n");
854 congested = true;
855 }
856
857 if (mdev->act_log->used >= nc->cong_extents) {
858 dev_info(DEV, "Congestion-extents threshold reached\n");
859 congested = true;
860 }
861
862 if (congested) {
99b4d8fe
LE
863 /* start a new epoch for non-mirrored writes */
864 start_new_tl_epoch(mdev->tconn);
5da9c836
LE
865
866 if (on_congestion == OC_PULL_AHEAD)
867 _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
868 else /*nc->on_congestion == OC_DISCONNECT */
869 _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
870 }
3b9ef85e 871 put_ldev(mdev);
5da9c836
LE
872}
873
874/* If this returns false, and req->private_bio is still set,
875 * this should be submitted locally.
876 *
877 * If it returns false, but req->private_bio is not set,
878 * we do not have access to good data :(
879 *
880 * Otherwise, this destroys req->private_bio, if any,
881 * and returns true.
882 */
883static bool do_remote_read(struct drbd_request *req)
884{
885 struct drbd_conf *mdev = req->w.mdev;
886 enum drbd_read_balancing rbm;
887
888 if (req->private_bio) {
889 if (!drbd_may_do_local_read(mdev,
890 req->i.sector, req->i.size)) {
891 bio_put(req->private_bio);
892 req->private_bio = NULL;
893 put_ldev(mdev);
894 }
895 }
896
897 if (mdev->state.pdsk != D_UP_TO_DATE)
898 return false;
899
a0d856df
LE
900 if (req->private_bio == NULL)
901 return true;
902
5da9c836
LE
903 /* TODO: improve read balancing decisions, take into account drbd
904 * protocol, pending requests etc. */
905
906 rcu_read_lock();
907 rbm = rcu_dereference(mdev->ldev->disk_conf)->read_balancing;
908 rcu_read_unlock();
909
910 if (rbm == RB_PREFER_LOCAL && req->private_bio)
911 return false; /* submit locally */
912
5da9c836
LE
913 if (remote_due_to_read_balancing(mdev, req->i.sector, rbm)) {
914 if (req->private_bio) {
915 bio_put(req->private_bio);
916 req->private_bio = NULL;
917 put_ldev(mdev);
918 }
919 return true;
920 }
921
922 return false;
923}
924
925/* returns number of connections (== 1, for drbd 8.4)
926 * expected to actually write this data,
927 * which does NOT include those that we are L_AHEAD for. */
928static int drbd_process_write_request(struct drbd_request *req)
929{
930 struct drbd_conf *mdev = req->w.mdev;
931 int remote, send_oos;
932
933 rcu_read_lock();
934 remote = drbd_should_do_remote(mdev->state);
935 if (remote) {
3b9ef85e 936 maybe_pull_ahead(mdev);
5da9c836
LE
937 remote = drbd_should_do_remote(mdev->state);
938 }
939 send_oos = drbd_should_send_out_of_sync(mdev->state);
940 rcu_read_unlock();
941
519b6d3e
LE
942 /* Need to replicate writes. Unless it is an empty flush,
943 * which is better mapped to a DRBD P_BARRIER packet,
944 * also for drbd wire protocol compatibility reasons.
945 * If this was a flush, just start a new epoch.
946 * Unless the current epoch was empty anyways, or we are not currently
947 * replicating, in which case there is no point. */
948 if (unlikely(req->i.size == 0)) {
949 /* The only size==0 bios we expect are empty flushes. */
950 D_ASSERT(req->master_bio->bi_rw & REQ_FLUSH);
99b4d8fe 951 if (remote)
519b6d3e
LE
952 start_new_tl_epoch(mdev->tconn);
953 return 0;
954 }
955
5da9c836
LE
956 if (!remote && !send_oos)
957 return 0;
958
959 D_ASSERT(!(remote && send_oos));
960
961 if (remote) {
962 _req_mod(req, TO_BE_SENT);
963 _req_mod(req, QUEUE_FOR_NET_WRITE);
964 } else if (drbd_set_out_of_sync(mdev, req->i.sector, req->i.size))
965 _req_mod(req, QUEUE_FOR_SEND_OOS);
966
967 return remote;
968}
969
970static void
971drbd_submit_req_private_bio(struct drbd_request *req)
972{
973 struct drbd_conf *mdev = req->w.mdev;
974 struct bio *bio = req->private_bio;
975 const int rw = bio_rw(bio);
976
977 bio->bi_bdev = mdev->ldev->backing_bdev;
978
979 /* State may have changed since we grabbed our reference on the
980 * ->ldev member. Double check, and short-circuit to endio.
981 * In case the last activity log transaction failed to get on
982 * stable storage, and this is a WRITE, we may not even submit
983 * this bio. */
984 if (get_ldev(mdev)) {
985 if (drbd_insert_fault(mdev,
986 rw == WRITE ? DRBD_FAULT_DT_WR
987 : rw == READ ? DRBD_FAULT_DT_RD
988 : DRBD_FAULT_DT_RA))
989 bio_endio(bio, -EIO);
990 else
991 generic_make_request(bio);
992 put_ldev(mdev);
993 } else
994 bio_endio(bio, -EIO);
995}
996
5df69ece 997void __drbd_make_request(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
b411b363
PR
998{
999 const int rw = bio_rw(bio);
5da9c836 1000 struct bio_and_error m = { NULL, };
b411b363 1001 struct drbd_request *req;
5da9c836 1002 bool no_remote = false;
b411b363
PR
1003
1004 /* allocate outside of all locks; */
1005 req = drbd_req_new(mdev, bio);
1006 if (!req) {
1007 dec_ap_bio(mdev);
1008 /* only pass the error to the upper layers.
1009 * if user cannot handle io errors, that's not our business. */
1010 dev_err(DEV, "could not kmalloc() req\n");
1011 bio_endio(bio, -ENOMEM);
5df69ece 1012 return;
b411b363 1013 }
aeda1cd6 1014 req->start_time = start_time;
b411b363 1015
5da9c836
LE
1016 if (!get_ldev(mdev)) {
1017 bio_put(req->private_bio);
b411b363
PR
1018 req->private_bio = NULL;
1019 }
b411b363
PR
1020
1021 /* For WRITES going to the local disk, grab a reference on the target
1022 * extent. This waits for any resync activity in the corresponding
1023 * resync extent to finish, and, if necessary, pulls in the target
1024 * extent into the activity log, which involves further disk io because
519b6d3e
LE
1025 * of transactional on-disk meta data updates.
1026 * Empty flushes don't need to go into the activity log, they can only
1027 * flush data for pending writes which are already in there. */
1028 if (rw == WRITE && req->private_bio && req->i.size
5da9c836 1029 && !test_bit(AL_SUSPENDED, &mdev->flags)) {
0778286a 1030 req->rq_state |= RQ_IN_ACT_LOG;
181286ad 1031 drbd_al_begin_io(mdev, &req->i);
0778286a 1032 }
b411b363 1033
87eeee41 1034 spin_lock_irq(&mdev->tconn->req_lock);
6024fece 1035 if (rw == WRITE) {
648e46b5
LE
1036 /* This may temporarily give up the req_lock,
1037 * but will re-aquire it before it returns here.
1038 * Needs to be before the check on drbd_suspended() */
1039 complete_conflicting_writes(req);
6024fece
AG
1040 }
1041
5da9c836 1042 /* no more giving up req_lock from now on! */
73a01a18 1043
5da9c836
LE
1044 if (drbd_suspended(mdev)) {
1045 /* push back and retry: */
1046 req->rq_state |= RQ_POSTPONED;
1047 if (req->private_bio) {
1048 bio_put(req->private_bio);
1049 req->private_bio = NULL;
d7644018 1050 put_ldev(mdev);
b411b363 1051 }
5da9c836 1052 goto out;
b411b363
PR
1053 }
1054
b411b363
PR
1055 /* Update disk stats */
1056 _drbd_start_io_acct(mdev, req, bio);
1057
5da9c836
LE
1058 /* We fail READ/READA early, if we can not serve it.
1059 * We must do this before req is registered on any lists.
a0d856df 1060 * Otherwise, drbd_req_complete() will queue failed READ for retry. */
5da9c836
LE
1061 if (rw != WRITE) {
1062 if (!do_remote_read(req) && !req->private_bio)
1063 goto nodata;
1064 }
b411b363 1065
b6dd1a89
LE
1066 /* which transfer log epoch does this belong to? */
1067 req->epoch = atomic_read(&mdev->tconn->current_tle_nr);
b6dd1a89 1068
519b6d3e
LE
1069 /* no point in adding empty flushes to the transfer log,
1070 * they are mapped to drbd barriers already. */
99b4d8fe
LE
1071 if (likely(req->i.size!=0)) {
1072 if (rw == WRITE)
1073 mdev->tconn->current_tle_writes++;
1074
519b6d3e 1075 list_add_tail(&req->tl_requests, &mdev->tconn->transfer_log);
99b4d8fe 1076 }
288f422e 1077
5da9c836
LE
1078 if (rw == WRITE) {
1079 if (!drbd_process_write_request(req))
1080 no_remote = true;
1081 } else {
1082 /* We either have a private_bio, or we can read from remote.
1083 * Otherwise we had done the goto nodata above. */
1084 if (req->private_bio == NULL) {
1085 _req_mod(req, TO_BE_SENT);
1086 _req_mod(req, QUEUE_FOR_NET_READ);
1087 } else
1088 no_remote = true;
b411b363 1089 }
67531718 1090
5da9c836
LE
1091 if (req->private_bio) {
1092 /* needs to be marked within the same spinlock */
1093 _req_mod(req, TO_BE_SUBMITTED);
1094 /* but we need to give up the spinlock to submit */
1095 spin_unlock_irq(&mdev->tconn->req_lock);
1096 drbd_submit_req_private_bio(req);
a0d856df 1097 spin_lock_irq(&mdev->tconn->req_lock);
5da9c836
LE
1098 } else if (no_remote) {
1099nodata:
1100 if (__ratelimit(&drbd_ratelimit_state))
1101 dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
1102 /* A write may have been queued for send_oos, however.
a0d856df 1103 * So we can not simply free it, we must go through drbd_req_put_completion_ref() */
67531718
PR
1104 }
1105
5da9c836 1106out:
a0d856df
LE
1107 if (drbd_req_put_completion_ref(req, &m, 1))
1108 kref_put(&req->kref, drbd_req_destroy);
87eeee41 1109 spin_unlock_irq(&mdev->tconn->req_lock);
b411b363 1110
5da9c836
LE
1111 if (m.bio)
1112 complete_master_bio(mdev, &m);
5df69ece 1113 return;
b411b363
PR
1114}
1115
2f58dcfc 1116int drbd_make_request(struct request_queue *q, struct bio *bio)
b411b363 1117{
b411b363 1118 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
aeda1cd6 1119 unsigned long start_time;
b411b363 1120
aeda1cd6
PR
1121 start_time = jiffies;
1122
b411b363
PR
1123 /*
1124 * what we "blindly" assume:
1125 */
c670a398 1126 D_ASSERT(IS_ALIGNED(bio->bi_size, 512));
b411b363 1127
5df69ece
LE
1128 inc_ap_bio(mdev);
1129 __drbd_make_request(mdev, bio, start_time);
69b6a3b1
PR
1130
1131 return 0;
b411b363
PR
1132}
1133
23361cf3
LE
1134/* This is called by bio_add_page().
1135 *
1136 * q->max_hw_sectors and other global limits are already enforced there.
b411b363 1137 *
23361cf3
LE
1138 * We need to call down to our lower level device,
1139 * in case it has special restrictions.
1140 *
1141 * We also may need to enforce configured max-bio-bvecs limits.
b411b363
PR
1142 *
1143 * As long as the BIO is empty we have to allow at least one bvec,
23361cf3 1144 * regardless of size and offset, so no need to ask lower levels.
b411b363
PR
1145 */
1146int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
1147{
1148 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
b411b363 1149 unsigned int bio_size = bvm->bi_size;
23361cf3
LE
1150 int limit = DRBD_MAX_BIO_SIZE;
1151 int backing_limit;
1152
1153 if (bio_size && get_ldev(mdev)) {
b411b363
PR
1154 struct request_queue * const b =
1155 mdev->ldev->backing_bdev->bd_disk->queue;
a1c88d0d 1156 if (b->merge_bvec_fn) {
b411b363
PR
1157 backing_limit = b->merge_bvec_fn(b, bvm, bvec);
1158 limit = min(limit, backing_limit);
1159 }
1160 put_ldev(mdev);
1161 }
1162 return limit;
1163}
7fde2be9 1164
b6dd1a89
LE
1165struct drbd_request *find_oldest_request(struct drbd_tconn *tconn)
1166{
1167 /* Walk the transfer log,
1168 * and find the oldest not yet completed request */
1169 struct drbd_request *r;
1170 list_for_each_entry(r, &tconn->transfer_log, tl_requests) {
b406777e 1171 if (atomic_read(&r->completion_ref))
b6dd1a89
LE
1172 return r;
1173 }
1174 return NULL;
1175}
1176
7fde2be9
PR
1177void request_timer_fn(unsigned long data)
1178{
1179 struct drbd_conf *mdev = (struct drbd_conf *) data;
8b924f1d 1180 struct drbd_tconn *tconn = mdev->tconn;
7fde2be9 1181 struct drbd_request *req; /* oldest request */
44ed167d 1182 struct net_conf *nc;
3b03ad59 1183 unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
07be15b1 1184 unsigned long now;
44ed167d
PR
1185
1186 rcu_read_lock();
1187 nc = rcu_dereference(tconn->net_conf);
07be15b1
LE
1188 if (nc && mdev->state.conn >= C_WF_REPORT_PARAMS)
1189 ent = nc->timeout * HZ/10 * nc->ko_count;
cdfda633 1190
07be15b1 1191 if (get_ldev(mdev)) { /* implicit state.disk >= D_INCONSISTENT */
cdfda633
PR
1192 dt = rcu_dereference(mdev->ldev->disk_conf)->disk_timeout * HZ / 10;
1193 put_ldev(mdev);
1194 }
44ed167d 1195 rcu_read_unlock();
7fde2be9 1196
cdfda633
PR
1197 et = min_not_zero(dt, ent);
1198
07be15b1 1199 if (!et)
7fde2be9
PR
1200 return; /* Recurring timer stopped */
1201
07be15b1
LE
1202 now = jiffies;
1203
8b924f1d 1204 spin_lock_irq(&tconn->req_lock);
b6dd1a89
LE
1205 req = find_oldest_request(tconn);
1206 if (!req) {
8b924f1d 1207 spin_unlock_irq(&tconn->req_lock);
07be15b1 1208 mod_timer(&mdev->request_timer, now + et);
7fde2be9
PR
1209 return;
1210 }
1211
07be15b1
LE
1212 /* The request is considered timed out, if
1213 * - we have some effective timeout from the configuration,
1214 * with above state restrictions applied,
1215 * - the oldest request is waiting for a response from the network
1216 * resp. the local disk,
1217 * - the oldest request is in fact older than the effective timeout,
1218 * - the connection was established (resp. disk was attached)
1219 * for longer than the timeout already.
1220 * Note that for 32bit jiffies and very stable connections/disks,
1221 * we may have a wrap around, which is catched by
1222 * !time_in_range(now, last_..._jif, last_..._jif + timeout).
1223 *
1224 * Side effect: once per 32bit wrap-around interval, which means every
1225 * ~198 days with 250 HZ, we have a window where the timeout would need
1226 * to expire twice (worst case) to become effective. Good enough.
1227 */
1228 if (ent && req->rq_state & RQ_NET_PENDING &&
1229 time_after(now, req->start_time + ent) &&
1230 !time_in_range(now, tconn->last_reconnect_jif, tconn->last_reconnect_jif + ent)) {
1231 dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
1232 _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
cdfda633 1233 }
07be15b1
LE
1234 if (dt && req->rq_state & RQ_LOCAL_PENDING && req->w.mdev == mdev &&
1235 time_after(now, req->start_time + dt) &&
1236 !time_in_range(now, mdev->last_reattach_jif, mdev->last_reattach_jif + dt)) {
1237 dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
0c849666 1238 __drbd_chk_io_error(mdev, DRBD_FORCE_DETACH);
7fde2be9 1239 }
07be15b1 1240 nt = (time_after(now, req->start_time + et) ? now : req->start_time) + et;
8b924f1d 1241 spin_unlock_irq(&tconn->req_lock);
3b03ad59 1242 mod_timer(&mdev->request_timer, nt);
7fde2be9 1243}
This page took 0.18267 seconds and 5 git commands to generate.