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