]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/block/drbd/drbd_req.c
drbd: Load balancing of read requests
[mirror_ubuntu-bionic-kernel.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
34/* Update disk stats at start of I/O request */
35static 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();
40 part_stat_inc(cpu, &mdev->vdisk->part0, ios[rw]);
41 part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], bio_sectors(bio));
376694a0
PR
42 (void) cpu; /* The macro invocations above want the cpu argument, I do not like
43 the compiler warning about cpu only assigned but never used... */
753c8913 44 part_inc_in_flight(&mdev->vdisk->part0, rw);
b411b363 45 part_stat_unlock();
b411b363
PR
46}
47
48/* Update disk stats when completing request upwards */
49static void _drbd_end_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
50{
51 int rw = bio_data_dir(req->master_bio);
52 unsigned long duration = jiffies - req->start_time;
53 int cpu;
54 cpu = part_stat_lock();
55 part_stat_add(cpu, &mdev->vdisk->part0, ticks[rw], duration);
56 part_round_stats(cpu, &mdev->vdisk->part0);
753c8913 57 part_dec_in_flight(&mdev->vdisk->part0, rw);
b411b363 58 part_stat_unlock();
b411b363
PR
59}
60
9e204cdd
AG
61static struct drbd_request *drbd_req_new(struct drbd_conf *mdev,
62 struct bio *bio_src)
63{
64 struct drbd_request *req;
65
66 req = mempool_alloc(drbd_request_mempool, GFP_NOIO);
67 if (!req)
68 return NULL;
69
70 drbd_req_make_private_bio(req, bio_src);
71 req->rq_state = bio_data_dir(bio_src) == WRITE ? RQ_WRITE : 0;
a21e9298 72 req->w.mdev = mdev;
9e204cdd
AG
73 req->master_bio = bio_src;
74 req->epoch = 0;
53840641 75
9e204cdd
AG
76 drbd_clear_interval(&req->i);
77 req->i.sector = bio_src->bi_sector;
78 req->i.size = bio_src->bi_size;
5e472264 79 req->i.local = true;
53840641
AG
80 req->i.waiting = false;
81
9e204cdd
AG
82 INIT_LIST_HEAD(&req->tl_requests);
83 INIT_LIST_HEAD(&req->w.list);
84
85 return req;
86}
87
88static void drbd_req_free(struct drbd_request *req)
89{
90 mempool_free(req, drbd_request_mempool);
91}
92
93/* rw is bio_data_dir(), only READ or WRITE */
b411b363
PR
94static void _req_is_done(struct drbd_conf *mdev, struct drbd_request *req, const int rw)
95{
96 const unsigned long s = req->rq_state;
288f422e
PR
97
98 /* remove it from the transfer log.
99 * well, only if it had been there in the first
100 * place... if it had not (local only or conflicting
101 * and never sent), it should still be "empty" as
102 * initialized in drbd_req_new(), so we can list_del() it
103 * here unconditionally */
104 list_del(&req->tl_requests);
105
b411b363
PR
106 /* if it was a write, we may have to set the corresponding
107 * bit(s) out-of-sync first. If it had a local part, we need to
108 * release the reference to the activity log. */
109 if (rw == WRITE) {
b411b363
PR
110 /* Set out-of-sync unless both OK flags are set
111 * (local only or remote failed).
112 * Other places where we set out-of-sync:
113 * READ with local io-error */
114 if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
ace652ac 115 drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
b411b363
PR
116
117 if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
ace652ac 118 drbd_set_in_sync(mdev, req->i.sector, req->i.size);
b411b363
PR
119
120 /* one might be tempted to move the drbd_al_complete_io
fcefa62e 121 * to the local io completion callback drbd_request_endio.
b411b363
PR
122 * but, if this was a mirror write, we may only
123 * drbd_al_complete_io after this is RQ_NET_DONE,
124 * otherwise the extent could be dropped from the al
125 * before it has actually been written on the peer.
126 * if we crash before our peer knows about the request,
127 * but after the extent has been dropped from the al,
128 * we would forget to resync the corresponding extent.
129 */
130 if (s & RQ_LOCAL_MASK) {
131 if (get_ldev_if_state(mdev, D_FAILED)) {
0778286a 132 if (s & RQ_IN_ACT_LOG)
181286ad 133 drbd_al_complete_io(mdev, &req->i);
b411b363
PR
134 put_ldev(mdev);
135 } else if (__ratelimit(&drbd_ratelimit_state)) {
181286ad
LE
136 dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu, %u), "
137 "but my Disk seems to have failed :(\n",
138 (unsigned long long) req->i.sector, req->i.size);
b411b363
PR
139 }
140 }
141 }
142
32fa7e91 143 drbd_req_free(req);
b411b363
PR
144}
145
146static void queue_barrier(struct drbd_conf *mdev)
147{
148 struct drbd_tl_epoch *b;
6936fcb4 149 struct drbd_tconn *tconn = mdev->tconn;
b411b363
PR
150
151 /* We are within the req_lock. Once we queued the barrier for sending,
152 * we set the CREATE_BARRIER bit. It is cleared as soon as a new
153 * barrier/epoch object is added. This is the only place this bit is
154 * set. It indicates that the barrier for this epoch is already queued,
155 * and no new epoch has been created yet. */
6936fcb4 156 if (test_bit(CREATE_BARRIER, &tconn->flags))
b411b363
PR
157 return;
158
6936fcb4 159 b = tconn->newest_tle;
b411b363 160 b->w.cb = w_send_barrier;
a21e9298 161 b->w.mdev = mdev;
b411b363
PR
162 /* inc_ap_pending done here, so we won't
163 * get imbalanced on connection loss.
164 * dec_ap_pending will be done in got_BarrierAck
165 * or (on connection loss) in tl_clear. */
166 inc_ap_pending(mdev);
6936fcb4
PR
167 drbd_queue_work(&tconn->data.work, &b->w);
168 set_bit(CREATE_BARRIER, &tconn->flags);
b411b363
PR
169}
170
171static void _about_to_complete_local_write(struct drbd_conf *mdev,
172 struct drbd_request *req)
173{
174 const unsigned long s = req->rq_state;
b411b363 175
8a3c1044
LE
176 /* Before we can signal completion to the upper layers,
177 * we may need to close the current epoch.
178 * We can skip this, if this request has not even been sent, because we
179 * did not have a fully established connection yet/anymore, during
180 * bitmap exchange, or while we are C_AHEAD due to congestion policy.
181 */
182 if (mdev->state.conn >= C_CONNECTED &&
183 (s & RQ_NET_SENT) != 0 &&
87eeee41 184 req->epoch == mdev->tconn->newest_tle->br_number)
b411b363 185 queue_barrier(mdev);
b411b363
PR
186}
187
188void complete_master_bio(struct drbd_conf *mdev,
189 struct bio_and_error *m)
190{
b411b363
PR
191 bio_endio(m->bio, m->error);
192 dec_ap_bio(mdev);
193}
194
53840641
AG
195
196static void drbd_remove_request_interval(struct rb_root *root,
197 struct drbd_request *req)
198{
a21e9298 199 struct drbd_conf *mdev = req->w.mdev;
53840641
AG
200 struct drbd_interval *i = &req->i;
201
202 drbd_remove_interval(root, i);
203
204 /* Wake up any processes waiting for this request to complete. */
205 if (i->waiting)
206 wake_up(&mdev->misc_wait);
207}
208
b411b363
PR
209/* Helper for __req_mod().
210 * Set m->bio to the master bio, if it is fit to be completed,
211 * or leave it alone (it is initialized to NULL in __req_mod),
212 * if it has already been completed, or cannot be completed yet.
213 * If m->bio is set, the error status to be returned is placed in m->error.
214 */
215void _req_may_be_done(struct drbd_request *req, struct bio_and_error *m)
216{
217 const unsigned long s = req->rq_state;
a21e9298 218 struct drbd_conf *mdev = req->w.mdev;
cdfda633 219 int rw = req->rq_state & RQ_WRITE ? WRITE : READ;
b411b363 220
b411b363
PR
221 /* we must not complete the master bio, while it is
222 * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
223 * not yet acknowledged by the peer
224 * not yet completed by the local io subsystem
225 * these flags may get cleared in any order by
226 * the worker,
227 * the receiver,
228 * the bio_endio completion callbacks.
229 */
cdfda633 230 if (s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED))
7be8da07
AG
231 return;
232 if (req->i.waiting) {
233 /* Retry all conflicting peer requests. */
234 wake_up(&mdev->misc_wait);
235 }
b411b363
PR
236 if (s & RQ_NET_QUEUED)
237 return;
238 if (s & RQ_NET_PENDING)
239 return;
b411b363
PR
240
241 if (req->master_bio) {
8554df1c 242 /* this is DATA_RECEIVED (remote read)
b411b363
PR
243 * or protocol C P_WRITE_ACK
244 * or protocol B P_RECV_ACK
8554df1c 245 * or protocol A "HANDED_OVER_TO_NETWORK" (SendAck)
b411b363
PR
246 * or canceled or failed,
247 * or killed from the transfer log due to connection loss.
248 */
249
250 /*
251 * figure out whether to report success or failure.
252 *
253 * report success when at least one of the operations succeeded.
254 * or, to put the other way,
255 * only report failure, when both operations failed.
256 *
257 * what to do about the failures is handled elsewhere.
258 * what we need to do here is just: complete the master_bio.
259 *
260 * local completion error, if any, has been stored as ERR_PTR
fcefa62e 261 * in private_bio within drbd_request_endio.
b411b363
PR
262 */
263 int ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
264 int error = PTR_ERR(req->private_bio);
265
266 /* remove the request from the conflict detection
267 * respective block_id verification hash */
dac1389c
AG
268 if (!drbd_interval_empty(&req->i)) {
269 struct rb_root *root;
270
dac1389c
AG
271 if (rw == WRITE)
272 root = &mdev->write_requests;
273 else
274 root = &mdev->read_requests;
53840641 275 drbd_remove_request_interval(root, req);
7be8da07 276 } else if (!(s & RQ_POSTPONED))
8825f7c3 277 D_ASSERT((s & (RQ_NET_MASK & ~RQ_NET_DONE)) == 0);
b411b363
PR
278
279 /* for writes we need to do some extra housekeeping */
280 if (rw == WRITE)
281 _about_to_complete_local_write(mdev, req);
282
283 /* Update disk stats */
284 _drbd_end_io_acct(mdev, req);
285
7be8da07
AG
286 if (!(s & RQ_POSTPONED)) {
287 m->error = ok ? 0 : (error ?: -EIO);
288 m->bio = req->master_bio;
289 }
b411b363
PR
290 req->master_bio = NULL;
291 }
292
cdfda633
PR
293 if (s & RQ_LOCAL_PENDING)
294 return;
295
b411b363
PR
296 if ((s & RQ_NET_MASK) == 0 || (s & RQ_NET_DONE)) {
297 /* this is disconnected (local only) operation,
a209b4ae 298 * or protocol A, B, or C P_BARRIER_ACK,
b411b363
PR
299 * or killed from the transfer log due to connection loss. */
300 _req_is_done(mdev, req, rw);
301 }
302 /* else: network part and not DONE yet. that is
a209b4ae 303 * protocol A, B, or C, barrier ack still pending... */
b411b363
PR
304}
305
cfa03415
PR
306static void _req_may_be_done_not_susp(struct drbd_request *req, struct bio_and_error *m)
307{
a21e9298 308 struct drbd_conf *mdev = req->w.mdev;
cfa03415 309
2aebfabb 310 if (!drbd_suspended(mdev))
cfa03415
PR
311 _req_may_be_done(req, m);
312}
313
b411b363
PR
314/* obviously this could be coded as many single functions
315 * instead of one huge switch,
316 * or by putting the code directly in the respective locations
317 * (as it has been before).
318 *
319 * but having it this way
320 * enforces that it is all in this one place, where it is easier to audit,
321 * it makes it obvious that whatever "event" "happens" to a request should
322 * happen "atomically" within the req_lock,
323 * and it enforces that we have to think in a very structured manner
324 * about the "events" that may happen to a request during its life time ...
325 */
2a80699f 326int __req_mod(struct drbd_request *req, enum drbd_req_event what,
b411b363
PR
327 struct bio_and_error *m)
328{
a21e9298 329 struct drbd_conf *mdev = req->w.mdev;
44ed167d 330 struct net_conf *nc;
303d1448 331 int p, rv = 0;
7be8da07
AG
332
333 if (m)
334 m->bio = NULL;
b411b363 335
b411b363
PR
336 switch (what) {
337 default:
338 dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
339 break;
340
341 /* does not happen...
342 * initialization done in drbd_req_new
8554df1c 343 case CREATED:
b411b363
PR
344 break;
345 */
346
8554df1c 347 case TO_BE_SENT: /* via network */
7be8da07 348 /* reached via __drbd_make_request
b411b363
PR
349 * and from w_read_retry_remote */
350 D_ASSERT(!(req->rq_state & RQ_NET_MASK));
351 req->rq_state |= RQ_NET_PENDING;
44ed167d
PR
352 rcu_read_lock();
353 nc = rcu_dereference(mdev->tconn->net_conf);
354 p = nc->wire_protocol;
355 rcu_read_unlock();
303d1448
PR
356 req->rq_state |=
357 p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
358 p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
b411b363
PR
359 inc_ap_pending(mdev);
360 break;
361
8554df1c 362 case TO_BE_SUBMITTED: /* locally */
7be8da07 363 /* reached via __drbd_make_request */
b411b363
PR
364 D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
365 req->rq_state |= RQ_LOCAL_PENDING;
366 break;
367
8554df1c 368 case COMPLETED_OK:
cdfda633 369 if (req->rq_state & RQ_WRITE)
ace652ac 370 mdev->writ_cnt += req->i.size >> 9;
b411b363 371 else
ace652ac 372 mdev->read_cnt += req->i.size >> 9;
b411b363
PR
373
374 req->rq_state |= (RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
375 req->rq_state &= ~RQ_LOCAL_PENDING;
376
cfa03415 377 _req_may_be_done_not_susp(req, m);
b411b363
PR
378 put_ldev(mdev);
379 break;
380
cdfda633
PR
381 case ABORT_DISK_IO:
382 req->rq_state |= RQ_LOCAL_ABORTED;
383 if (req->rq_state & RQ_WRITE)
384 _req_may_be_done_not_susp(req, m);
385 else
386 goto goto_queue_for_net_read;
387 break;
388
8554df1c 389 case WRITE_COMPLETED_WITH_ERROR:
b411b363
PR
390 req->rq_state |= RQ_LOCAL_COMPLETED;
391 req->rq_state &= ~RQ_LOCAL_PENDING;
392
81e84650 393 __drbd_chk_io_error(mdev, false);
cfa03415 394 _req_may_be_done_not_susp(req, m);
b411b363
PR
395 put_ldev(mdev);
396 break;
397
8554df1c 398 case READ_AHEAD_COMPLETED_WITH_ERROR:
b411b363
PR
399 /* it is legal to fail READA */
400 req->rq_state |= RQ_LOCAL_COMPLETED;
401 req->rq_state &= ~RQ_LOCAL_PENDING;
cfa03415 402 _req_may_be_done_not_susp(req, m);
b411b363
PR
403 put_ldev(mdev);
404 break;
405
8554df1c 406 case READ_COMPLETED_WITH_ERROR:
ace652ac 407 drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
b411b363
PR
408
409 req->rq_state |= RQ_LOCAL_COMPLETED;
410 req->rq_state &= ~RQ_LOCAL_PENDING;
411
b411b363 412 D_ASSERT(!(req->rq_state & RQ_NET_MASK));
b411b363 413
81e84650 414 __drbd_chk_io_error(mdev, false);
b411b363 415 put_ldev(mdev);
b411b363 416
cdfda633
PR
417 goto_queue_for_net_read:
418
d255e5ff
LE
419 /* no point in retrying if there is no good remote data,
420 * or we have no connection. */
421 if (mdev->state.pdsk != D_UP_TO_DATE) {
cfa03415 422 _req_may_be_done_not_susp(req, m);
d255e5ff
LE
423 break;
424 }
425
8554df1c 426 /* _req_mod(req,TO_BE_SENT); oops, recursion... */
d255e5ff
LE
427 req->rq_state |= RQ_NET_PENDING;
428 inc_ap_pending(mdev);
8554df1c 429 /* fall through: _req_mod(req,QUEUE_FOR_NET_READ); */
b411b363 430
8554df1c 431 case QUEUE_FOR_NET_READ:
b411b363
PR
432 /* READ or READA, and
433 * no local disk,
434 * or target area marked as invalid,
435 * or just got an io-error. */
7be8da07 436 /* from __drbd_make_request
b411b363
PR
437 * or from bio_endio during read io-error recovery */
438
439 /* so we can verify the handle in the answer packet
440 * corresponding hlist_del is in _req_may_be_done() */
97ddb687 441 D_ASSERT(drbd_interval_empty(&req->i));
dac1389c 442 drbd_insert_interval(&mdev->read_requests, &req->i);
b411b363 443
83c38830 444 set_bit(UNPLUG_REMOTE, &mdev->flags);
b411b363
PR
445
446 D_ASSERT(req->rq_state & RQ_NET_PENDING);
447 req->rq_state |= RQ_NET_QUEUED;
448 req->w.cb = (req->rq_state & RQ_LOCAL_MASK)
449 ? w_read_retry_remote
450 : w_send_read_req;
e42325a5 451 drbd_queue_work(&mdev->tconn->data.work, &req->w);
b411b363
PR
452 break;
453
8554df1c 454 case QUEUE_FOR_NET_WRITE:
b411b363 455 /* assert something? */
7be8da07 456 /* from __drbd_make_request only */
b411b363 457
b411b363 458 /* corresponding hlist_del is in _req_may_be_done() */
97ddb687 459 D_ASSERT(drbd_interval_empty(&req->i));
de696716 460 drbd_insert_interval(&mdev->write_requests, &req->i);
b411b363
PR
461
462 /* NOTE
463 * In case the req ended up on the transfer log before being
464 * queued on the worker, it could lead to this request being
465 * missed during cleanup after connection loss.
466 * So we have to do both operations here,
467 * within the same lock that protects the transfer log.
468 *
469 * _req_add_to_epoch(req); this has to be after the
470 * _maybe_start_new_epoch(req); which happened in
7be8da07 471 * __drbd_make_request, because we now may set the bit
b411b363
PR
472 * again ourselves to close the current epoch.
473 *
474 * Add req to the (now) current epoch (barrier). */
475
83c38830
LE
476 /* otherwise we may lose an unplug, which may cause some remote
477 * io-scheduler timeout to expire, increasing maximum latency,
478 * hurting performance. */
479 set_bit(UNPLUG_REMOTE, &mdev->flags);
480
7be8da07 481 /* see __drbd_make_request,
b411b363 482 * just after it grabs the req_lock */
6936fcb4 483 D_ASSERT(test_bit(CREATE_BARRIER, &mdev->tconn->flags) == 0);
b411b363 484
87eeee41 485 req->epoch = mdev->tconn->newest_tle->br_number;
b411b363
PR
486
487 /* increment size of current epoch */
87eeee41 488 mdev->tconn->newest_tle->n_writes++;
b411b363
PR
489
490 /* queue work item to send data */
491 D_ASSERT(req->rq_state & RQ_NET_PENDING);
492 req->rq_state |= RQ_NET_QUEUED;
493 req->w.cb = w_send_dblock;
e42325a5 494 drbd_queue_work(&mdev->tconn->data.work, &req->w);
b411b363
PR
495
496 /* close the epoch, in case it outgrew the limit */
44ed167d
PR
497 rcu_read_lock();
498 nc = rcu_dereference(mdev->tconn->net_conf);
499 p = nc->max_epoch_size;
500 rcu_read_unlock();
501 if (mdev->tconn->newest_tle->n_writes >= p)
b411b363
PR
502 queue_barrier(mdev);
503
504 break;
505
8554df1c 506 case QUEUE_FOR_SEND_OOS:
73a01a18 507 req->rq_state |= RQ_NET_QUEUED;
8f7bed77 508 req->w.cb = w_send_out_of_sync;
e42325a5 509 drbd_queue_work(&mdev->tconn->data.work, &req->w);
73a01a18
PR
510 break;
511
8554df1c 512 case OOS_HANDED_TO_NETWORK:
73a01a18 513 /* actually the same */
8554df1c 514 case SEND_CANCELED:
b411b363 515 /* treat it the same */
8554df1c 516 case SEND_FAILED:
b411b363
PR
517 /* real cleanup will be done from tl_clear. just update flags
518 * so it is no longer marked as on the worker queue */
519 req->rq_state &= ~RQ_NET_QUEUED;
520 /* if we did it right, tl_clear should be scheduled only after
521 * this, so this should not be necessary! */
cfa03415 522 _req_may_be_done_not_susp(req, m);
b411b363
PR
523 break;
524
8554df1c 525 case HANDED_OVER_TO_NETWORK:
b411b363 526 /* assert something? */
759fbdfb 527 if (bio_data_dir(req->master_bio) == WRITE)
ace652ac 528 atomic_add(req->i.size >> 9, &mdev->ap_in_flight);
759fbdfb 529
b411b363 530 if (bio_data_dir(req->master_bio) == WRITE &&
303d1448 531 !(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK))) {
b411b363
PR
532 /* this is what is dangerous about protocol A:
533 * pretend it was successfully written on the peer. */
534 if (req->rq_state & RQ_NET_PENDING) {
535 dec_ap_pending(mdev);
536 req->rq_state &= ~RQ_NET_PENDING;
537 req->rq_state |= RQ_NET_OK;
538 } /* else: neg-ack was faster... */
539 /* it is still not yet RQ_NET_DONE until the
540 * corresponding epoch barrier got acked as well,
541 * so we know what to dirty on connection loss */
542 }
543 req->rq_state &= ~RQ_NET_QUEUED;
544 req->rq_state |= RQ_NET_SENT;
545 /* because _drbd_send_zc_bio could sleep, and may want to
8554df1c
AG
546 * dereference the bio even after the "WRITE_ACKED_BY_PEER" and
547 * "COMPLETED_OK" events came in, once we return from
b411b363
PR
548 * _drbd_send_zc_bio (drbd_send_dblock), we have to check
549 * whether it is done already, and end it. */
cfa03415 550 _req_may_be_done_not_susp(req, m);
b411b363
PR
551 break;
552
8554df1c 553 case READ_RETRY_REMOTE_CANCELED:
d255e5ff
LE
554 req->rq_state &= ~RQ_NET_QUEUED;
555 /* fall through, in case we raced with drbd_disconnect */
8554df1c 556 case CONNECTION_LOST_WHILE_PENDING:
b411b363
PR
557 /* transfer log cleanup after connection loss */
558 /* assert something? */
559 if (req->rq_state & RQ_NET_PENDING)
560 dec_ap_pending(mdev);
561 req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
562 req->rq_state |= RQ_NET_DONE;
759fbdfb 563 if (req->rq_state & RQ_NET_SENT && req->rq_state & RQ_WRITE)
ace652ac 564 atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
759fbdfb 565
380207d0
PR
566 if (!(req->rq_state & RQ_WRITE) &&
567 mdev->state.disk == D_UP_TO_DATE &&
568 !IS_ERR_OR_NULL(req->private_bio))
569 goto goto_read_retry_local;
570
b411b363
PR
571 /* if it is still queued, we may not complete it here.
572 * it will be canceled soon. */
573 if (!(req->rq_state & RQ_NET_QUEUED))
cfa03415 574 _req_may_be_done(req, m); /* Allowed while state.susp */
b411b363
PR
575 break;
576
8554df1c 577 case WRITE_ACKED_BY_PEER_AND_SIS:
b411b363 578 req->rq_state |= RQ_NET_SIS;
7be8da07 579 case DISCARD_WRITE:
b411b363
PR
580 /* for discarded conflicting writes of multiple primaries,
581 * there is no need to keep anything in the tl, potential
582 * node crashes are covered by the activity log. */
b411b363
PR
583 req->rq_state |= RQ_NET_DONE;
584 /* fall through */
8554df1c 585 case WRITE_ACKED_BY_PEER:
303d1448 586 D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
b411b363
PR
587 /* protocol C; successfully written on peer.
588 * Nothing to do here.
589 * We want to keep the tl in place for all protocols, to cater
590 * for volatile write-back caches on lower level devices.
591 *
592 * A barrier request is expected to have forced all prior
593 * requests onto stable storage, so completion of a barrier
594 * request could set NET_DONE right here, and not wait for the
595 * P_BARRIER_ACK, but that is an unnecessary optimization. */
596
303d1448 597 goto ack_common;
b411b363 598 /* this makes it effectively the same as for: */
8554df1c 599 case RECV_ACKED_BY_PEER:
303d1448 600 D_ASSERT(req->rq_state & RQ_EXP_RECEIVE_ACK);
b411b363 601 /* protocol B; pretends to be successfully written on peer.
8554df1c 602 * see also notes above in HANDED_OVER_TO_NETWORK about
b411b363 603 * protocol != C */
303d1448 604 ack_common:
b411b363
PR
605 req->rq_state |= RQ_NET_OK;
606 D_ASSERT(req->rq_state & RQ_NET_PENDING);
607 dec_ap_pending(mdev);
ace652ac 608 atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
b411b363 609 req->rq_state &= ~RQ_NET_PENDING;
cfa03415 610 _req_may_be_done_not_susp(req, m);
b411b363
PR
611 break;
612
7be8da07 613 case POSTPONE_WRITE:
303d1448
PR
614 D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
615 /* If this node has already detected the write conflict, the
7be8da07
AG
616 * worker will be waiting on misc_wait. Wake it up once this
617 * request has completed locally.
618 */
619 D_ASSERT(req->rq_state & RQ_NET_PENDING);
620 req->rq_state |= RQ_POSTPONED;
621 _req_may_be_done_not_susp(req, m);
622 break;
623
8554df1c 624 case NEG_ACKED:
b411b363 625 /* assert something? */
759fbdfb 626 if (req->rq_state & RQ_NET_PENDING) {
b411b363 627 dec_ap_pending(mdev);
ace652ac 628 atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
759fbdfb 629 }
b411b363
PR
630 req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
631
632 req->rq_state |= RQ_NET_DONE;
380207d0
PR
633
634 if (!(req->rq_state & RQ_WRITE) &&
635 mdev->state.disk == D_UP_TO_DATE &&
636 !IS_ERR_OR_NULL(req->private_bio))
637 goto goto_read_retry_local;
638
cfa03415 639 _req_may_be_done_not_susp(req, m);
8554df1c 640 /* else: done by HANDED_OVER_TO_NETWORK */
b411b363
PR
641 break;
642
380207d0
PR
643 goto_read_retry_local:
644 req->rq_state |= RQ_LOCAL_PENDING;
645 req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
646 generic_make_request(req->private_bio);
647 break;
648
8554df1c 649 case FAIL_FROZEN_DISK_IO:
265be2d0
PR
650 if (!(req->rq_state & RQ_LOCAL_COMPLETED))
651 break;
652
cfa03415 653 _req_may_be_done(req, m); /* Allowed while state.susp */
265be2d0
PR
654 break;
655
8554df1c 656 case RESTART_FROZEN_DISK_IO:
265be2d0
PR
657 if (!(req->rq_state & RQ_LOCAL_COMPLETED))
658 break;
659
660 req->rq_state &= ~RQ_LOCAL_COMPLETED;
661
662 rv = MR_READ;
663 if (bio_data_dir(req->master_bio) == WRITE)
664 rv = MR_WRITE;
665
666 get_ldev(mdev);
667 req->w.cb = w_restart_disk_io;
e42325a5 668 drbd_queue_work(&mdev->tconn->data.work, &req->w);
265be2d0
PR
669 break;
670
8554df1c 671 case RESEND:
11b58e73 672 /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
47ff2d0a 673 before the connection loss (B&C only); only P_BARRIER_ACK was missing.
11b58e73 674 Trowing them out of the TL here by pretending we got a BARRIER_ACK
481c6f50 675 We ensure that the peer was not rebooted */
11b58e73
PR
676 if (!(req->rq_state & RQ_NET_OK)) {
677 if (req->w.cb) {
e42325a5 678 drbd_queue_work(&mdev->tconn->data.work, &req->w);
11b58e73
PR
679 rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
680 }
681 break;
682 }
8554df1c 683 /* else, fall through to BARRIER_ACKED */
11b58e73 684
8554df1c 685 case BARRIER_ACKED:
288f422e
PR
686 if (!(req->rq_state & RQ_WRITE))
687 break;
688
b411b363 689 if (req->rq_state & RQ_NET_PENDING) {
a209b4ae 690 /* barrier came in before all requests were acked.
b411b363
PR
691 * this is bad, because if the connection is lost now,
692 * we won't be able to clean them up... */
8554df1c 693 dev_err(DEV, "FIXME (BARRIER_ACKED but pending)\n");
87eeee41 694 list_move(&req->tl_requests, &mdev->tconn->out_of_sequence_requests);
b411b363 695 }
e636db5b
LE
696 if ((req->rq_state & RQ_NET_MASK) != 0) {
697 req->rq_state |= RQ_NET_DONE;
303d1448 698 if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)))
89e58e75 699 atomic_sub(req->i.size>>9, &mdev->ap_in_flight);
e636db5b 700 }
cfa03415 701 _req_may_be_done(req, m); /* Allowed while state.susp */
b411b363
PR
702 break;
703
8554df1c 704 case DATA_RECEIVED:
b411b363
PR
705 D_ASSERT(req->rq_state & RQ_NET_PENDING);
706 dec_ap_pending(mdev);
707 req->rq_state &= ~RQ_NET_PENDING;
708 req->rq_state |= (RQ_NET_OK|RQ_NET_DONE);
380207d0
PR
709 if (!IS_ERR_OR_NULL(req->private_bio)) {
710 bio_put(req->private_bio);
711 req->private_bio = NULL;
712 put_ldev(mdev);
713 }
cfa03415 714 _req_may_be_done_not_susp(req, m);
b411b363
PR
715 break;
716 };
2a80699f
PR
717
718 return rv;
b411b363
PR
719}
720
721/* we may do a local read if:
722 * - we are consistent (of course),
723 * - or we are generally inconsistent,
724 * BUT we are still/already IN SYNC for this area.
725 * since size may be bigger than BM_BLOCK_SIZE,
726 * we may need to check several bits.
727 */
0da34df0 728static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
b411b363
PR
729{
730 unsigned long sbnr, ebnr;
731 sector_t esector, nr_sectors;
732
733 if (mdev->state.disk == D_UP_TO_DATE)
0da34df0 734 return true;
8c387def 735 if (mdev->state.disk != D_INCONSISTENT)
0da34df0 736 return false;
b411b363 737 esector = sector + (size >> 9) - 1;
8ca9844f 738 nr_sectors = drbd_get_capacity(mdev->this_bdev);
b411b363
PR
739 D_ASSERT(sector < nr_sectors);
740 D_ASSERT(esector < nr_sectors);
741
742 sbnr = BM_SECT_TO_BIT(sector);
743 ebnr = BM_SECT_TO_BIT(esector);
744
0da34df0 745 return drbd_bm_count_bits(mdev, sbnr, ebnr) == 0;
b411b363
PR
746}
747
380207d0
PR
748static bool remote_due_to_read_balancing(struct drbd_conf *mdev)
749{
750 enum drbd_read_balancing rbm;
751 struct backing_dev_info *bdi;
752
753 if (mdev->state.pdsk < D_UP_TO_DATE)
754 return false;
755
756 rcu_read_lock();
757 rbm = rcu_dereference(mdev->ldev->disk_conf)->read_balancing;
758 rcu_read_unlock();
759
760 switch (rbm) {
761 case RB_CONGESTED_REMOTE:
762 bdi = &mdev->ldev->backing_bdev->bd_disk->queue->backing_dev_info;
763 return bdi_read_congested(bdi);
764 case RB_LEAST_PENDING:
765 return atomic_read(&mdev->local_cnt) >
766 atomic_read(&mdev->ap_pending_cnt) + atomic_read(&mdev->rs_pending_cnt);
767 case RB_ROUND_ROBIN:
768 return test_and_change_bit(READ_BALANCE_RR, &mdev->flags);
769 case RB_PREFER_REMOTE:
770 return true;
771 case RB_PREFER_LOCAL:
772 default:
773 return false;
774 }
775}
776
6024fece
AG
777/*
778 * complete_conflicting_writes - wait for any conflicting write requests
779 *
780 * The write_requests tree contains all active write requests which we
781 * currently know about. Wait for any requests to complete which conflict with
782 * the new one.
783 */
784static int complete_conflicting_writes(struct drbd_conf *mdev,
785 sector_t sector, int size)
786{
787 for(;;) {
6024fece 788 struct drbd_interval *i;
7be8da07 789 int err;
6024fece
AG
790
791 i = drbd_find_overlap(&mdev->write_requests, sector, size);
792 if (!i)
793 return 0;
7be8da07
AG
794 err = drbd_wait_misc(mdev, i);
795 if (err)
796 return err;
6024fece
AG
797 }
798}
799
7be8da07 800int __drbd_make_request(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
b411b363
PR
801{
802 const int rw = bio_rw(bio);
803 const int size = bio->bi_size;
804 const sector_t sector = bio->bi_sector;
805 struct drbd_tl_epoch *b = NULL;
806 struct drbd_request *req;
44ed167d 807 struct net_conf *nc;
73a01a18 808 int local, remote, send_oos = 0;
6024fece 809 int err;
9a25a04c 810 int ret = 0;
b411b363
PR
811
812 /* allocate outside of all locks; */
813 req = drbd_req_new(mdev, bio);
814 if (!req) {
815 dec_ap_bio(mdev);
816 /* only pass the error to the upper layers.
817 * if user cannot handle io errors, that's not our business. */
818 dev_err(DEV, "could not kmalloc() req\n");
819 bio_endio(bio, -ENOMEM);
820 return 0;
821 }
aeda1cd6 822 req->start_time = start_time;
b411b363 823
b411b363
PR
824 local = get_ldev(mdev);
825 if (!local) {
826 bio_put(req->private_bio); /* or we get a bio leak */
827 req->private_bio = NULL;
828 }
829 if (rw == WRITE) {
830 remote = 1;
831 } else {
832 /* READ || READA */
833 if (local) {
834 if (!drbd_may_do_local_read(mdev, sector, size)) {
835 /* we could kick the syncer to
836 * sync this extent asap, wait for
837 * it, then continue locally.
838 * Or just issue the request remotely.
839 */
840 local = 0;
841 bio_put(req->private_bio);
842 req->private_bio = NULL;
843 put_ldev(mdev);
380207d0
PR
844 } else if (remote_due_to_read_balancing(mdev)) {
845 /* Keep the private bio in case we need it
846 for a local retry */
847 local = 0;
b411b363
PR
848 }
849 }
850 remote = !local && mdev->state.pdsk >= D_UP_TO_DATE;
851 }
852
853 /* If we have a disk, but a READA request is mapped to remote,
854 * we are R_PRIMARY, D_INCONSISTENT, SyncTarget.
855 * Just fail that READA request right here.
856 *
857 * THINK: maybe fail all READA when not local?
858 * or make this configurable...
859 * if network is slow, READA won't do any good.
860 */
861 if (rw == READA && mdev->state.disk >= D_INCONSISTENT && !local) {
862 err = -EWOULDBLOCK;
863 goto fail_and_free_req;
864 }
865
866 /* For WRITES going to the local disk, grab a reference on the target
867 * extent. This waits for any resync activity in the corresponding
868 * resync extent to finish, and, if necessary, pulls in the target
869 * extent into the activity log, which involves further disk io because
870 * of transactional on-disk meta data updates. */
0778286a
PR
871 if (rw == WRITE && local && !test_bit(AL_SUSPENDED, &mdev->flags)) {
872 req->rq_state |= RQ_IN_ACT_LOG;
181286ad 873 drbd_al_begin_io(mdev, &req->i);
0778286a 874 }
b411b363 875
6a35c45f 876 remote = remote && drbd_should_do_remote(mdev->state);
8f7bed77 877 send_oos = rw == WRITE && drbd_should_send_out_of_sync(mdev->state);
3719094e 878 D_ASSERT(!(remote && send_oos));
b411b363 879
2aebfabb 880 if (!(local || remote) && !drbd_suspended(mdev)) {
fb2c7a10
LE
881 if (__ratelimit(&drbd_ratelimit_state))
882 dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
6024fece 883 err = -EIO;
b411b363
PR
884 goto fail_free_complete;
885 }
886
887 /* For WRITE request, we have to make sure that we have an
888 * unused_spare_tle, in case we need to start a new epoch.
889 * I try to be smart and avoid to pre-allocate always "just in case",
890 * but there is a race between testing the bit and pointer outside the
891 * spinlock, and grabbing the spinlock.
892 * if we lost that race, we retry. */
73a01a18 893 if (rw == WRITE && (remote || send_oos) &&
87eeee41 894 mdev->tconn->unused_spare_tle == NULL &&
6936fcb4 895 test_bit(CREATE_BARRIER, &mdev->tconn->flags)) {
b411b363
PR
896allocate_barrier:
897 b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_NOIO);
898 if (!b) {
899 dev_err(DEV, "Failed to alloc barrier.\n");
900 err = -ENOMEM;
901 goto fail_free_complete;
902 }
903 }
904
905 /* GOOD, everything prepared, grab the spin_lock */
87eeee41 906 spin_lock_irq(&mdev->tconn->req_lock);
b411b363 907
6024fece
AG
908 if (rw == WRITE) {
909 err = complete_conflicting_writes(mdev, sector, size);
910 if (err) {
7be8da07
AG
911 if (err != -ERESTARTSYS)
912 _conn_request_state(mdev->tconn,
913 NS(conn, C_TIMEOUT),
914 CS_HARD);
6024fece 915 spin_unlock_irq(&mdev->tconn->req_lock);
7be8da07 916 err = -EIO;
6024fece
AG
917 goto fail_free_complete;
918 }
919 }
920
2aebfabb 921 if (drbd_suspended(mdev)) {
9a25a04c
PR
922 /* If we got suspended, use the retry mechanism of
923 generic_make_request() to restart processing of this
2f58dcfc 924 bio. In the next call to drbd_make_request
9a25a04c
PR
925 we sleep in inc_ap_bio() */
926 ret = 1;
87eeee41 927 spin_unlock_irq(&mdev->tconn->req_lock);
9a25a04c
PR
928 goto fail_free_complete;
929 }
930
73a01a18 931 if (remote || send_oos) {
6a35c45f 932 remote = drbd_should_do_remote(mdev->state);
8f7bed77 933 send_oos = rw == WRITE && drbd_should_send_out_of_sync(mdev->state);
3719094e 934 D_ASSERT(!(remote && send_oos));
73a01a18
PR
935
936 if (!(remote || send_oos))
b411b363
PR
937 dev_warn(DEV, "lost connection while grabbing the req_lock!\n");
938 if (!(local || remote)) {
939 dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
87eeee41 940 spin_unlock_irq(&mdev->tconn->req_lock);
6024fece 941 err = -EIO;
b411b363
PR
942 goto fail_free_complete;
943 }
944 }
945
87eeee41
PR
946 if (b && mdev->tconn->unused_spare_tle == NULL) {
947 mdev->tconn->unused_spare_tle = b;
b411b363
PR
948 b = NULL;
949 }
73a01a18 950 if (rw == WRITE && (remote || send_oos) &&
87eeee41 951 mdev->tconn->unused_spare_tle == NULL &&
6936fcb4 952 test_bit(CREATE_BARRIER, &mdev->tconn->flags)) {
b411b363
PR
953 /* someone closed the current epoch
954 * while we were grabbing the spinlock */
87eeee41 955 spin_unlock_irq(&mdev->tconn->req_lock);
b411b363
PR
956 goto allocate_barrier;
957 }
958
959
960 /* Update disk stats */
961 _drbd_start_io_acct(mdev, req, bio);
962
963 /* _maybe_start_new_epoch(mdev);
964 * If we need to generate a write barrier packet, we have to add the
965 * new epoch (barrier) object, and queue the barrier packet for sending,
966 * and queue the req's data after it _within the same lock_, otherwise
967 * we have race conditions were the reorder domains could be mixed up.
968 *
969 * Even read requests may start a new epoch and queue the corresponding
970 * barrier packet. To get the write ordering right, we only have to
971 * make sure that, if this is a write request and it triggered a
972 * barrier packet, this request is queued within the same spinlock. */
87eeee41 973 if ((remote || send_oos) && mdev->tconn->unused_spare_tle &&
6936fcb4 974 test_and_clear_bit(CREATE_BARRIER, &mdev->tconn->flags)) {
2f5cdd0b 975 _tl_add_barrier(mdev->tconn, mdev->tconn->unused_spare_tle);
87eeee41 976 mdev->tconn->unused_spare_tle = NULL;
b411b363
PR
977 } else {
978 D_ASSERT(!(remote && rw == WRITE &&
6936fcb4 979 test_bit(CREATE_BARRIER, &mdev->tconn->flags)));
b411b363
PR
980 }
981
982 /* NOTE
983 * Actually, 'local' may be wrong here already, since we may have failed
984 * to write to the meta data, and may become wrong anytime because of
985 * local io-error for some other request, which would lead to us
986 * "detaching" the local disk.
987 *
988 * 'remote' may become wrong any time because the network could fail.
989 *
990 * This is a harmless race condition, though, since it is handled
991 * correctly at the appropriate places; so it just defers the failure
992 * of the respective operation.
993 */
994
995 /* mark them early for readability.
996 * this just sets some state flags. */
997 if (remote)
8554df1c 998 _req_mod(req, TO_BE_SENT);
b411b363 999 if (local)
8554df1c 1000 _req_mod(req, TO_BE_SUBMITTED);
b411b363 1001
87eeee41 1002 list_add_tail(&req->tl_requests, &mdev->tconn->newest_tle->requests);
288f422e 1003
b411b363
PR
1004 /* NOTE remote first: to get the concurrent write detection right,
1005 * we must register the request before start of local IO. */
1006 if (remote) {
1007 /* either WRITE and C_CONNECTED,
1008 * or READ, and no local disk,
1009 * or READ, but not in sync.
1010 */
1011 _req_mod(req, (rw == WRITE)
8554df1c
AG
1012 ? QUEUE_FOR_NET_WRITE
1013 : QUEUE_FOR_NET_READ);
b411b363 1014 }
73a01a18 1015 if (send_oos && drbd_set_out_of_sync(mdev, sector, size))
8554df1c 1016 _req_mod(req, QUEUE_FOR_SEND_OOS);
67531718 1017
44ed167d
PR
1018 rcu_read_lock();
1019 nc = rcu_dereference(mdev->tconn->net_conf);
73a01a18 1020 if (remote &&
44ed167d 1021 nc->on_congestion != OC_BLOCK && mdev->tconn->agreed_pro_version >= 96) {
67531718
PR
1022 int congested = 0;
1023
44ed167d
PR
1024 if (nc->cong_fill &&
1025 atomic_read(&mdev->ap_in_flight) >= nc->cong_fill) {
67531718
PR
1026 dev_info(DEV, "Congestion-fill threshold reached\n");
1027 congested = 1;
1028 }
1029
44ed167d 1030 if (mdev->act_log->used >= nc->cong_extents) {
67531718
PR
1031 dev_info(DEV, "Congestion-extents threshold reached\n");
1032 congested = 1;
1033 }
1034
71c78cfb 1035 if (congested) {
039312b6 1036 queue_barrier(mdev); /* last barrier, after mirrored writes */
73a01a18 1037
44ed167d 1038 if (nc->on_congestion == OC_PULL_AHEAD)
67531718 1039 _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
44ed167d 1040 else /*nc->on_congestion == OC_DISCONNECT */
67531718
PR
1041 _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
1042 }
1043 }
44ed167d 1044 rcu_read_unlock();
67531718 1045
87eeee41 1046 spin_unlock_irq(&mdev->tconn->req_lock);
b411b363
PR
1047 kfree(b); /* if someone else has beaten us to it... */
1048
1049 if (local) {
1050 req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
1051
6719fb03
LE
1052 /* State may have changed since we grabbed our reference on the
1053 * mdev->ldev member. Double check, and short-circuit to endio.
1054 * In case the last activity log transaction failed to get on
1055 * stable storage, and this is a WRITE, we may not even submit
1056 * this bio. */
1057 if (get_ldev(mdev)) {
0cf9d27e
AG
1058 if (drbd_insert_fault(mdev, rw == WRITE ? DRBD_FAULT_DT_WR
1059 : rw == READ ? DRBD_FAULT_DT_RD
1060 : DRBD_FAULT_DT_RA))
6719fb03
LE
1061 bio_endio(req->private_bio, -EIO);
1062 else
1063 generic_make_request(req->private_bio);
1064 put_ldev(mdev);
1065 } else
b411b363 1066 bio_endio(req->private_bio, -EIO);
b411b363
PR
1067 }
1068
b411b363
PR
1069 return 0;
1070
1071fail_free_complete:
76727f68 1072 if (req->rq_state & RQ_IN_ACT_LOG)
181286ad 1073 drbd_al_complete_io(mdev, &req->i);
b411b363 1074fail_and_free_req:
380207d0 1075 if (!IS_ERR_OR_NULL(req->private_bio)) {
b411b363
PR
1076 bio_put(req->private_bio);
1077 req->private_bio = NULL;
1078 put_ldev(mdev);
1079 }
9a25a04c
PR
1080 if (!ret)
1081 bio_endio(bio, err);
1082
b411b363
PR
1083 drbd_req_free(req);
1084 dec_ap_bio(mdev);
1085 kfree(b);
1086
9a25a04c 1087 return ret;
b411b363
PR
1088}
1089
2f58dcfc 1090int drbd_make_request(struct request_queue *q, struct bio *bio)
b411b363 1091{
b411b363 1092 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
aeda1cd6 1093 unsigned long start_time;
b411b363 1094
aeda1cd6
PR
1095 start_time = jiffies;
1096
b411b363
PR
1097 /*
1098 * what we "blindly" assume:
1099 */
1100 D_ASSERT(bio->bi_size > 0);
c670a398 1101 D_ASSERT(IS_ALIGNED(bio->bi_size, 512));
b411b363 1102
23361cf3
LE
1103 inc_ap_bio(mdev);
1104 return __drbd_make_request(mdev, bio, start_time);
b411b363
PR
1105}
1106
23361cf3
LE
1107/* This is called by bio_add_page().
1108 *
1109 * q->max_hw_sectors and other global limits are already enforced there.
b411b363 1110 *
23361cf3
LE
1111 * We need to call down to our lower level device,
1112 * in case it has special restrictions.
1113 *
1114 * We also may need to enforce configured max-bio-bvecs limits.
b411b363
PR
1115 *
1116 * As long as the BIO is empty we have to allow at least one bvec,
23361cf3 1117 * regardless of size and offset, so no need to ask lower levels.
b411b363
PR
1118 */
1119int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
1120{
1121 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
b411b363 1122 unsigned int bio_size = bvm->bi_size;
23361cf3
LE
1123 int limit = DRBD_MAX_BIO_SIZE;
1124 int backing_limit;
1125
1126 if (bio_size && get_ldev(mdev)) {
b411b363
PR
1127 struct request_queue * const b =
1128 mdev->ldev->backing_bdev->bd_disk->queue;
a1c88d0d 1129 if (b->merge_bvec_fn) {
b411b363
PR
1130 backing_limit = b->merge_bvec_fn(b, bvm, bvec);
1131 limit = min(limit, backing_limit);
1132 }
1133 put_ldev(mdev);
1134 }
1135 return limit;
1136}
7fde2be9
PR
1137
1138void request_timer_fn(unsigned long data)
1139{
1140 struct drbd_conf *mdev = (struct drbd_conf *) data;
8b924f1d 1141 struct drbd_tconn *tconn = mdev->tconn;
7fde2be9
PR
1142 struct drbd_request *req; /* oldest request */
1143 struct list_head *le;
44ed167d 1144 struct net_conf *nc;
3b03ad59 1145 unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
44ed167d
PR
1146
1147 rcu_read_lock();
1148 nc = rcu_dereference(tconn->net_conf);
cdfda633
PR
1149 ent = nc ? nc->timeout * HZ/10 * nc->ko_count : 0;
1150
1151 if (get_ldev(mdev)) {
1152 dt = rcu_dereference(mdev->ldev->disk_conf)->disk_timeout * HZ / 10;
1153 put_ldev(mdev);
1154 }
44ed167d 1155 rcu_read_unlock();
7fde2be9 1156
cdfda633
PR
1157 et = min_not_zero(dt, ent);
1158
1159 if (!et || (mdev->state.conn < C_WF_REPORT_PARAMS && mdev->state.disk <= D_FAILED))
7fde2be9
PR
1160 return; /* Recurring timer stopped */
1161
8b924f1d
PR
1162 spin_lock_irq(&tconn->req_lock);
1163 le = &tconn->oldest_tle->requests;
7fde2be9 1164 if (list_empty(le)) {
8b924f1d 1165 spin_unlock_irq(&tconn->req_lock);
7fde2be9
PR
1166 mod_timer(&mdev->request_timer, jiffies + et);
1167 return;
1168 }
1169
1170 le = le->prev;
1171 req = list_entry(le, struct drbd_request, tl_requests);
cdfda633
PR
1172 if (ent && req->rq_state & RQ_NET_PENDING) {
1173 if (time_is_before_eq_jiffies(req->start_time + ent)) {
7fde2be9 1174 dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
cdfda633
PR
1175 _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
1176 }
1177 }
1178 if (dt && req->rq_state & RQ_LOCAL_PENDING) {
1179 if (time_is_before_eq_jiffies(req->start_time + dt)) {
1180 dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
1181 __drbd_chk_io_error(mdev, 1);
7fde2be9 1182 }
7fde2be9 1183 }
3b03ad59 1184 nt = (time_is_before_eq_jiffies(req->start_time + et) ? jiffies : req->start_time) + et;
8b924f1d 1185 spin_unlock_irq(&tconn->req_lock);
3b03ad59 1186 mod_timer(&mdev->request_timer, nt);
7fde2be9 1187}