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