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