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