4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
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>.
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)
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.
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.
26 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/drbd.h>
34 static bool drbd_may_do_local_read(struct drbd_device
*device
, sector_t sector
, int size
);
36 /* Update disk stats at start of I/O request */
37 static void _drbd_start_io_acct(struct drbd_device
*device
, struct drbd_request
*req
)
39 const int rw
= bio_data_dir(req
->master_bio
);
41 cpu
= part_stat_lock();
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);
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... */
47 part_inc_in_flight(&device
->vdisk
->part0
, rw
);
51 /* Update disk stats when completing request upwards */
52 static void _drbd_end_io_acct(struct drbd_device
*device
, struct drbd_request
*req
)
54 int rw
= bio_data_dir(req
->master_bio
);
55 unsigned long duration
= jiffies
- req
->start_time
;
57 cpu
= part_stat_lock();
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
);
64 static struct drbd_request
*drbd_req_new(struct drbd_device
*device
,
67 struct drbd_request
*req
;
69 req
= mempool_alloc(drbd_request_mempool
, GFP_NOIO
);
73 drbd_req_make_private_bio(req
, bio_src
);
74 req
->rq_state
= bio_data_dir(bio_src
) == WRITE
? RQ_WRITE
: 0;
76 req
->master_bio
= bio_src
;
79 drbd_clear_interval(&req
->i
);
80 req
->i
.sector
= bio_src
->bi_iter
.bi_sector
;
81 req
->i
.size
= bio_src
->bi_iter
.bi_size
;
83 req
->i
.waiting
= false;
85 INIT_LIST_HEAD(&req
->tl_requests
);
86 INIT_LIST_HEAD(&req
->w
.list
);
88 /* one reference to be put by __drbd_make_request */
89 atomic_set(&req
->completion_ref
, 1);
90 /* one kref as long as completion_ref > 0 */
91 kref_init(&req
->kref
);
95 void drbd_req_destroy(struct kref
*kref
)
97 struct drbd_request
*req
= container_of(kref
, struct drbd_request
, kref
);
98 struct drbd_device
*device
= req
->device
;
99 const unsigned s
= req
->rq_state
;
101 if ((req
->master_bio
&& !(s
& RQ_POSTPONED
)) ||
102 atomic_read(&req
->completion_ref
) ||
103 (s
& RQ_LOCAL_PENDING
) ||
104 ((s
& RQ_NET_MASK
) && !(s
& RQ_NET_DONE
))) {
105 drbd_err(device
, "drbd_req_destroy: Logic BUG rq_state = 0x%x, completion_ref = %d\n",
106 s
, atomic_read(&req
->completion_ref
));
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 */
116 list_del_init(&req
->tl_requests
);
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. */
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 */
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.
133 if ((s
& (RQ_POSTPONED
|RQ_LOCAL_MASK
|RQ_NET_MASK
)) != RQ_POSTPONED
) {
134 if (!(s
& RQ_NET_OK
) || !(s
& RQ_LOCAL_OK
))
135 drbd_set_out_of_sync(device
, req
->i
.sector
, req
->i
.size
);
137 if ((s
& RQ_NET_OK
) && (s
& RQ_LOCAL_OK
) && (s
& RQ_NET_SIS
))
138 drbd_set_in_sync(device
, req
->i
.sector
, req
->i
.size
);
141 /* one might be tempted to move the drbd_al_complete_io
142 * to the local io completion callback drbd_request_endio.
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.
151 if (s
& RQ_IN_ACT_LOG
) {
152 if (get_ldev_if_state(device
, D_FAILED
)) {
153 drbd_al_complete_io(device
, &req
->i
);
155 } else if (__ratelimit(&drbd_ratelimit_state
)) {
156 drbd_warn(device
, "Should have called drbd_al_complete_io(, %llu, %u), "
157 "but my Disk seems to have failed :(\n",
158 (unsigned long long) req
->i
.sector
, req
->i
.size
);
163 mempool_free(req
, drbd_request_mempool
);
166 static void wake_all_senders(struct drbd_connection
*connection
)
168 wake_up(&connection
->sender_work
.q_wait
);
171 /* must hold resource->req_lock */
172 void start_new_tl_epoch(struct drbd_connection
*connection
)
174 /* no point closing an epoch, if it is empty, anyways. */
175 if (connection
->current_tle_writes
== 0)
178 connection
->current_tle_writes
= 0;
179 atomic_inc(&connection
->current_tle_nr
);
180 wake_all_senders(connection
);
183 void complete_master_bio(struct drbd_device
*device
,
184 struct bio_and_error
*m
)
186 bio_endio(m
->bio
, m
->error
);
191 static void drbd_remove_request_interval(struct rb_root
*root
,
192 struct drbd_request
*req
)
194 struct drbd_device
*device
= req
->device
;
195 struct drbd_interval
*i
= &req
->i
;
197 drbd_remove_interval(root
, i
);
199 /* Wake up any processes waiting for this request to complete. */
201 wake_up(&device
->misc_wait
);
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.
211 void drbd_req_complete(struct drbd_request
*req
, struct bio_and_error
*m
)
213 const unsigned s
= req
->rq_state
;
214 struct drbd_device
*device
= req
->device
;
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
225 * the bio_endio completion callbacks.
227 if ((s
& RQ_LOCAL_PENDING
&& !(s
& RQ_LOCAL_ABORTED
)) ||
228 (s
& RQ_NET_QUEUED
) || (s
& RQ_NET_PENDING
) ||
229 (s
& RQ_COMPLETION_SUSP
)) {
230 drbd_err(device
, "drbd_req_complete: Logic BUG rq_state = 0x%x\n", s
);
234 if (!req
->master_bio
) {
235 drbd_err(device
, "drbd_req_complete: Logic BUG, master_bio == NULL!\n");
239 rw
= bio_rw(req
->master_bio
);
242 * figure out whether to report success or failure.
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.
248 * what to do about the failures is handled elsewhere.
249 * what we need to do here is just: complete the master_bio.
251 * local completion error, if any, has been stored as ERR_PTR
252 * in private_bio within drbd_request_endio.
254 ok
= (s
& RQ_LOCAL_OK
) || (s
& RQ_NET_OK
);
255 error
= PTR_ERR(req
->private_bio
);
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
;
263 root
= &device
->write_requests
;
265 root
= &device
->read_requests
;
266 drbd_remove_request_interval(root
, req
);
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.
277 req
->epoch
== atomic_read(&first_peer_device(device
)->connection
->current_tle_nr
))
278 start_new_tl_epoch(first_peer_device(device
)->connection
);
280 /* Update disk stats */
281 _drbd_end_io_acct(device
, req
);
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.
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.
293 * READA may fail, and will not be retried.
295 * WRITE should have used all available paths already.
297 if (!ok
&& rw
== READ
&& !list_empty(&req
->tl_requests
))
298 req
->rq_state
|= RQ_POSTPONED
;
300 if (!(req
->rq_state
& RQ_POSTPONED
)) {
301 m
->error
= ok
? 0 : (error
?: -EIO
);
302 m
->bio
= req
->master_bio
;
303 req
->master_bio
= NULL
;
307 static int drbd_req_put_completion_ref(struct drbd_request
*req
, struct bio_and_error
*m
, int put
)
309 struct drbd_device
*device
= req
->device
;
310 D_ASSERT(device
, m
|| (req
->rq_state
& RQ_POSTPONED
));
312 if (!atomic_sub_and_test(put
, &req
->completion_ref
))
315 drbd_req_complete(req
, m
);
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
);
327 /* I'd like this to be the only place that manipulates
328 * req->completion_ref and req->kref. */
329 static void mod_rq_state(struct drbd_request
*req
, struct bio_and_error
*m
,
332 struct drbd_device
*device
= req
->device
;
333 unsigned s
= req
->rq_state
;
337 if (drbd_suspended(device
) && !((s
| clear
) & RQ_COMPLETION_SUSP
))
338 set
|= RQ_COMPLETION_SUSP
;
342 req
->rq_state
&= ~clear
;
343 req
->rq_state
|= set
;
346 if (req
->rq_state
== s
)
349 /* intent: get references */
351 if (!(s
& RQ_LOCAL_PENDING
) && (set
& RQ_LOCAL_PENDING
))
352 atomic_inc(&req
->completion_ref
);
354 if (!(s
& RQ_NET_PENDING
) && (set
& RQ_NET_PENDING
)) {
355 inc_ap_pending(device
);
356 atomic_inc(&req
->completion_ref
);
359 if (!(s
& RQ_NET_QUEUED
) && (set
& RQ_NET_QUEUED
))
360 atomic_inc(&req
->completion_ref
);
362 if (!(s
& RQ_EXP_BARR_ACK
) && (set
& RQ_EXP_BARR_ACK
))
363 kref_get(&req
->kref
); /* wait for the DONE */
365 if (!(s
& RQ_NET_SENT
) && (set
& RQ_NET_SENT
))
366 atomic_add(req
->i
.size
>> 9, &device
->ap_in_flight
);
368 if (!(s
& RQ_COMPLETION_SUSP
) && (set
& RQ_COMPLETION_SUSP
))
369 atomic_inc(&req
->completion_ref
);
371 /* progress: put references */
373 if ((s
& RQ_COMPLETION_SUSP
) && (clear
& RQ_COMPLETION_SUSP
))
376 if (!(s
& RQ_LOCAL_ABORTED
) && (set
& RQ_LOCAL_ABORTED
)) {
377 D_ASSERT(device
, req
->rq_state
& RQ_LOCAL_PENDING
);
378 /* local completion may still come in later,
379 * we need to keep the req object around. */
380 kref_get(&req
->kref
);
384 if ((s
& RQ_LOCAL_PENDING
) && (clear
& RQ_LOCAL_PENDING
)) {
385 if (req
->rq_state
& RQ_LOCAL_ABORTED
)
391 if ((s
& RQ_NET_PENDING
) && (clear
& RQ_NET_PENDING
)) {
392 dec_ap_pending(device
);
396 if ((s
& RQ_NET_QUEUED
) && (clear
& RQ_NET_QUEUED
))
399 if ((s
& RQ_EXP_BARR_ACK
) && !(s
& RQ_NET_DONE
) && (set
& RQ_NET_DONE
)) {
400 if (req
->rq_state
& RQ_NET_SENT
)
401 atomic_sub(req
->i
.size
>> 9, &device
->ap_in_flight
);
405 /* potentially complete and destroy */
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
)
414 "mod_rq_state: Logic BUG: %x -> %x: refcount = %d, should be >= %d\n",
415 s
, req
->rq_state
, refcount
, at_least
);
418 /* If we made progress, retry conflicting peer requests, if any. */
420 wake_up(&device
->misc_wait
);
423 k_put
+= drbd_req_put_completion_ref(req
, m
, c_put
);
425 kref_sub(&req
->kref
, k_put
, drbd_req_destroy
);
428 static void drbd_report_io_error(struct drbd_device
*device
, struct drbd_request
*req
)
430 char b
[BDEVNAME_SIZE
];
432 if (!__ratelimit(&drbd_ratelimit_state
))
435 drbd_warn(device
, "local %s IO error sector %llu+%u on %s\n",
436 (req
->rq_state
& RQ_WRITE
) ? "WRITE" : "READ",
437 (unsigned long long)req
->i
.sector
,
439 bdevname(device
->ldev
->backing_bdev
, b
));
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).
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 ...
454 int __req_mod(struct drbd_request
*req
, enum drbd_req_event what
,
455 struct bio_and_error
*m
)
457 struct drbd_device
*const device
= req
->device
;
458 struct drbd_peer_device
*const peer_device
= first_peer_device(device
);
459 struct drbd_connection
*const connection
= peer_device
? peer_device
->connection
: NULL
;
468 drbd_err(device
, "LOGIC BUG in %s:%u\n", __FILE__
, __LINE__
);
471 /* does not happen...
472 * initialization done in drbd_req_new
477 case TO_BE_SENT
: /* via network */
478 /* reached via __drbd_make_request
479 * and from w_read_retry_remote */
480 D_ASSERT(device
, !(req
->rq_state
& RQ_NET_MASK
));
482 nc
= rcu_dereference(connection
->net_conf
);
483 p
= nc
->wire_protocol
;
486 p
== DRBD_PROT_C
? RQ_EXP_WRITE_ACK
:
487 p
== DRBD_PROT_B
? RQ_EXP_RECEIVE_ACK
: 0;
488 mod_rq_state(req
, m
, 0, RQ_NET_PENDING
);
491 case TO_BE_SUBMITTED
: /* locally */
492 /* reached via __drbd_make_request */
493 D_ASSERT(device
, !(req
->rq_state
& RQ_LOCAL_MASK
));
494 mod_rq_state(req
, m
, 0, RQ_LOCAL_PENDING
);
498 if (req
->rq_state
& RQ_WRITE
)
499 device
->writ_cnt
+= req
->i
.size
>> 9;
501 device
->read_cnt
+= req
->i
.size
>> 9;
503 mod_rq_state(req
, m
, RQ_LOCAL_PENDING
,
504 RQ_LOCAL_COMPLETED
|RQ_LOCAL_OK
);
508 mod_rq_state(req
, m
, 0, RQ_LOCAL_ABORTED
);
511 case WRITE_COMPLETED_WITH_ERROR
:
512 drbd_report_io_error(device
, req
);
513 __drbd_chk_io_error(device
, DRBD_WRITE_ERROR
);
514 mod_rq_state(req
, m
, RQ_LOCAL_PENDING
, RQ_LOCAL_COMPLETED
);
517 case READ_COMPLETED_WITH_ERROR
:
518 drbd_set_out_of_sync(device
, req
->i
.sector
, req
->i
.size
);
519 drbd_report_io_error(device
, req
);
520 __drbd_chk_io_error(device
, DRBD_READ_ERROR
);
522 case READ_AHEAD_COMPLETED_WITH_ERROR
:
523 /* it is legal to fail READA, no __drbd_chk_io_error in that case. */
524 mod_rq_state(req
, m
, RQ_LOCAL_PENDING
, RQ_LOCAL_COMPLETED
);
527 case DISCARD_COMPLETED_NOTSUPP
:
528 case DISCARD_COMPLETED_WITH_ERROR
:
529 /* I'd rather not detach from local disk just because it
530 * failed a REQ_DISCARD. */
531 mod_rq_state(req
, m
, RQ_LOCAL_PENDING
, RQ_LOCAL_COMPLETED
);
534 case QUEUE_FOR_NET_READ
:
535 /* READ or READA, and
537 * or target area marked as invalid,
538 * or just got an io-error. */
539 /* from __drbd_make_request
540 * or from bio_endio during read io-error recovery */
542 /* So we can verify the handle in the answer packet.
543 * Corresponding drbd_remove_request_interval is in
544 * drbd_req_complete() */
545 D_ASSERT(device
, drbd_interval_empty(&req
->i
));
546 drbd_insert_interval(&device
->read_requests
, &req
->i
);
548 set_bit(UNPLUG_REMOTE
, &device
->flags
);
550 D_ASSERT(device
, req
->rq_state
& RQ_NET_PENDING
);
551 D_ASSERT(device
, (req
->rq_state
& RQ_LOCAL_MASK
) == 0);
552 mod_rq_state(req
, m
, 0, RQ_NET_QUEUED
);
553 req
->w
.cb
= w_send_read_req
;
554 drbd_queue_work(&connection
->sender_work
,
558 case QUEUE_FOR_NET_WRITE
:
559 /* assert something? */
560 /* from __drbd_make_request only */
562 /* Corresponding drbd_remove_request_interval is in
563 * drbd_req_complete() */
564 D_ASSERT(device
, drbd_interval_empty(&req
->i
));
565 drbd_insert_interval(&device
->write_requests
, &req
->i
);
568 * In case the req ended up on the transfer log before being
569 * queued on the worker, it could lead to this request being
570 * missed during cleanup after connection loss.
571 * So we have to do both operations here,
572 * within the same lock that protects the transfer log.
574 * _req_add_to_epoch(req); this has to be after the
575 * _maybe_start_new_epoch(req); which happened in
576 * __drbd_make_request, because we now may set the bit
577 * again ourselves to close the current epoch.
579 * Add req to the (now) current epoch (barrier). */
581 /* otherwise we may lose an unplug, which may cause some remote
582 * io-scheduler timeout to expire, increasing maximum latency,
583 * hurting performance. */
584 set_bit(UNPLUG_REMOTE
, &device
->flags
);
586 /* queue work item to send data */
587 D_ASSERT(device
, req
->rq_state
& RQ_NET_PENDING
);
588 mod_rq_state(req
, m
, 0, RQ_NET_QUEUED
|RQ_EXP_BARR_ACK
);
589 req
->w
.cb
= w_send_dblock
;
590 drbd_queue_work(&connection
->sender_work
,
593 /* close the epoch, in case it outgrew the limit */
595 nc
= rcu_dereference(connection
->net_conf
);
596 p
= nc
->max_epoch_size
;
598 if (connection
->current_tle_writes
>= p
)
599 start_new_tl_epoch(connection
);
603 case QUEUE_FOR_SEND_OOS
:
604 mod_rq_state(req
, m
, 0, RQ_NET_QUEUED
);
605 req
->w
.cb
= w_send_out_of_sync
;
606 drbd_queue_work(&connection
->sender_work
,
610 case READ_RETRY_REMOTE_CANCELED
:
613 /* real cleanup will be done from tl_clear. just update flags
614 * so it is no longer marked as on the worker queue */
615 mod_rq_state(req
, m
, RQ_NET_QUEUED
, 0);
618 case HANDED_OVER_TO_NETWORK
:
619 /* assert something? */
620 if (bio_data_dir(req
->master_bio
) == WRITE
&&
621 !(req
->rq_state
& (RQ_EXP_RECEIVE_ACK
| RQ_EXP_WRITE_ACK
))) {
622 /* this is what is dangerous about protocol A:
623 * pretend it was successfully written on the peer. */
624 if (req
->rq_state
& RQ_NET_PENDING
)
625 mod_rq_state(req
, m
, RQ_NET_PENDING
, RQ_NET_OK
);
626 /* else: neg-ack was faster... */
627 /* it is still not yet RQ_NET_DONE until the
628 * corresponding epoch barrier got acked as well,
629 * so we know what to dirty on connection loss */
631 mod_rq_state(req
, m
, RQ_NET_QUEUED
, RQ_NET_SENT
);
634 case OOS_HANDED_TO_NETWORK
:
635 /* Was not set PENDING, no longer QUEUED, so is now DONE
636 * as far as this connection is concerned. */
637 mod_rq_state(req
, m
, RQ_NET_QUEUED
, RQ_NET_DONE
);
640 case CONNECTION_LOST_WHILE_PENDING
:
641 /* transfer log cleanup after connection loss */
643 RQ_NET_OK
|RQ_NET_PENDING
|RQ_COMPLETION_SUSP
,
647 case CONFLICT_RESOLVED
:
648 /* for superseded conflicting writes of multiple primaries,
649 * there is no need to keep anything in the tl, potential
650 * node crashes are covered by the activity log.
652 * If this request had been marked as RQ_POSTPONED before,
653 * it will actually not be completed, but "restarted",
654 * resubmitted from the retry worker context. */
655 D_ASSERT(device
, req
->rq_state
& RQ_NET_PENDING
);
656 D_ASSERT(device
, req
->rq_state
& RQ_EXP_WRITE_ACK
);
657 mod_rq_state(req
, m
, RQ_NET_PENDING
, RQ_NET_DONE
|RQ_NET_OK
);
660 case WRITE_ACKED_BY_PEER_AND_SIS
:
661 req
->rq_state
|= RQ_NET_SIS
;
662 case WRITE_ACKED_BY_PEER
:
663 D_ASSERT(device
, req
->rq_state
& RQ_EXP_WRITE_ACK
);
664 /* protocol C; successfully written on peer.
665 * Nothing more to do here.
666 * We want to keep the tl in place for all protocols, to cater
667 * for volatile write-back caches on lower level devices. */
670 case RECV_ACKED_BY_PEER
:
671 D_ASSERT(device
, req
->rq_state
& RQ_EXP_RECEIVE_ACK
);
672 /* protocol B; pretends to be successfully written on peer.
673 * see also notes above in HANDED_OVER_TO_NETWORK about
676 D_ASSERT(device
, req
->rq_state
& RQ_NET_PENDING
);
677 mod_rq_state(req
, m
, RQ_NET_PENDING
, RQ_NET_OK
);
681 D_ASSERT(device
, req
->rq_state
& RQ_EXP_WRITE_ACK
);
682 /* If this node has already detected the write conflict, the
683 * worker will be waiting on misc_wait. Wake it up once this
684 * request has completed locally.
686 D_ASSERT(device
, req
->rq_state
& RQ_NET_PENDING
);
687 req
->rq_state
|= RQ_POSTPONED
;
689 wake_up(&device
->misc_wait
);
690 /* Do not clear RQ_NET_PENDING. This request will make further
691 * progress via restart_conflicting_writes() or
692 * fail_postponed_requests(). Hopefully. */
696 mod_rq_state(req
, m
, RQ_NET_OK
|RQ_NET_PENDING
, 0);
699 case FAIL_FROZEN_DISK_IO
:
700 if (!(req
->rq_state
& RQ_LOCAL_COMPLETED
))
702 mod_rq_state(req
, m
, RQ_COMPLETION_SUSP
, 0);
705 case RESTART_FROZEN_DISK_IO
:
706 if (!(req
->rq_state
& RQ_LOCAL_COMPLETED
))
710 RQ_COMPLETION_SUSP
|RQ_LOCAL_COMPLETED
,
714 if (bio_data_dir(req
->master_bio
) == WRITE
)
717 get_ldev(device
); /* always succeeds in this call path */
718 req
->w
.cb
= w_restart_disk_io
;
719 drbd_queue_work(&connection
->sender_work
,
724 /* Simply complete (local only) READs. */
725 if (!(req
->rq_state
& RQ_WRITE
) && !req
->w
.cb
) {
726 mod_rq_state(req
, m
, RQ_COMPLETION_SUSP
, 0);
730 /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
731 before the connection loss (B&C only); only P_BARRIER_ACK
732 (or the local completion?) was missing when we suspended.
733 Throwing them out of the TL here by pretending we got a BARRIER_ACK.
734 During connection handshake, we ensure that the peer was not rebooted. */
735 if (!(req
->rq_state
& RQ_NET_OK
)) {
736 /* FIXME could this possibly be a req->dw.cb == w_send_out_of_sync?
737 * in that case we must not set RQ_NET_PENDING. */
739 mod_rq_state(req
, m
, RQ_COMPLETION_SUSP
, RQ_NET_QUEUED
|RQ_NET_PENDING
);
741 /* w.cb expected to be w_send_dblock, or w_send_read_req */
742 drbd_queue_work(&connection
->sender_work
,
744 rv
= req
->rq_state
& RQ_WRITE
? MR_WRITE
: MR_READ
;
745 } /* else: FIXME can this happen? */
748 /* else, fall through to BARRIER_ACKED */
751 /* barrier ack for READ requests does not make sense */
752 if (!(req
->rq_state
& RQ_WRITE
))
755 if (req
->rq_state
& RQ_NET_PENDING
) {
756 /* barrier came in before all requests were acked.
757 * this is bad, because if the connection is lost now,
758 * we won't be able to clean them up... */
759 drbd_err(device
, "FIXME (BARRIER_ACKED but pending)\n");
761 /* Allowed to complete requests, even while suspended.
762 * As this is called for all requests within a matching epoch,
763 * we need to filter, and only set RQ_NET_DONE for those that
764 * have actually been on the wire. */
765 mod_rq_state(req
, m
, RQ_COMPLETION_SUSP
,
766 (req
->rq_state
& RQ_NET_MASK
) ? RQ_NET_DONE
: 0);
770 D_ASSERT(device
, req
->rq_state
& RQ_NET_PENDING
);
771 mod_rq_state(req
, m
, RQ_NET_PENDING
, RQ_NET_OK
|RQ_NET_DONE
);
774 case QUEUE_AS_DRBD_BARRIER
:
775 start_new_tl_epoch(connection
);
776 mod_rq_state(req
, m
, 0, RQ_NET_OK
|RQ_NET_DONE
);
783 /* we may do a local read if:
784 * - we are consistent (of course),
785 * - or we are generally inconsistent,
786 * BUT we are still/already IN SYNC for this area.
787 * since size may be bigger than BM_BLOCK_SIZE,
788 * we may need to check several bits.
790 static bool drbd_may_do_local_read(struct drbd_device
*device
, sector_t sector
, int size
)
792 unsigned long sbnr
, ebnr
;
793 sector_t esector
, nr_sectors
;
795 if (device
->state
.disk
== D_UP_TO_DATE
)
797 if (device
->state
.disk
!= D_INCONSISTENT
)
799 esector
= sector
+ (size
>> 9) - 1;
800 nr_sectors
= drbd_get_capacity(device
->this_bdev
);
801 D_ASSERT(device
, sector
< nr_sectors
);
802 D_ASSERT(device
, esector
< nr_sectors
);
804 sbnr
= BM_SECT_TO_BIT(sector
);
805 ebnr
= BM_SECT_TO_BIT(esector
);
807 return drbd_bm_count_bits(device
, sbnr
, ebnr
) == 0;
810 static bool remote_due_to_read_balancing(struct drbd_device
*device
, sector_t sector
,
811 enum drbd_read_balancing rbm
)
813 struct backing_dev_info
*bdi
;
817 case RB_CONGESTED_REMOTE
:
818 bdi
= &device
->ldev
->backing_bdev
->bd_disk
->queue
->backing_dev_info
;
819 return bdi_read_congested(bdi
);
820 case RB_LEAST_PENDING
:
821 return atomic_read(&device
->local_cnt
) >
822 atomic_read(&device
->ap_pending_cnt
) + atomic_read(&device
->rs_pending_cnt
);
823 case RB_32K_STRIPING
: /* stripe_shift = 15 */
824 case RB_64K_STRIPING
:
825 case RB_128K_STRIPING
:
826 case RB_256K_STRIPING
:
827 case RB_512K_STRIPING
:
828 case RB_1M_STRIPING
: /* stripe_shift = 20 */
829 stripe_shift
= (rbm
- RB_32K_STRIPING
+ 15);
830 return (sector
>> (stripe_shift
- 9)) & 1;
832 return test_and_change_bit(READ_BALANCE_RR
, &device
->flags
);
833 case RB_PREFER_REMOTE
:
835 case RB_PREFER_LOCAL
:
842 * complete_conflicting_writes - wait for any conflicting write requests
844 * The write_requests tree contains all active write requests which we
845 * currently know about. Wait for any requests to complete which conflict with
848 * Only way out: remove the conflicting intervals from the tree.
850 static void complete_conflicting_writes(struct drbd_request
*req
)
853 struct drbd_device
*device
= req
->device
;
854 struct drbd_interval
*i
;
855 sector_t sector
= req
->i
.sector
;
856 int size
= req
->i
.size
;
858 i
= drbd_find_overlap(&device
->write_requests
, sector
, size
);
863 prepare_to_wait(&device
->misc_wait
, &wait
, TASK_UNINTERRUPTIBLE
);
864 i
= drbd_find_overlap(&device
->write_requests
, sector
, size
);
867 /* Indicate to wake up device->misc_wait on progress. */
869 spin_unlock_irq(&device
->resource
->req_lock
);
871 spin_lock_irq(&device
->resource
->req_lock
);
873 finish_wait(&device
->misc_wait
, &wait
);
876 /* called within req_lock and rcu_read_lock() */
877 static void maybe_pull_ahead(struct drbd_device
*device
)
879 struct drbd_connection
*connection
= first_peer_device(device
)->connection
;
881 bool congested
= false;
882 enum drbd_on_congestion on_congestion
;
885 nc
= rcu_dereference(connection
->net_conf
);
886 on_congestion
= nc
? nc
->on_congestion
: OC_BLOCK
;
888 if (on_congestion
== OC_BLOCK
||
889 connection
->agreed_pro_version
< 96)
892 /* If I don't even have good local storage, we can not reasonably try
893 * to pull ahead of the peer. We also need the local reference to make
894 * sure device->act_log is there.
896 if (!get_ldev_if_state(device
, D_UP_TO_DATE
))
900 atomic_read(&device
->ap_in_flight
) >= nc
->cong_fill
) {
901 drbd_info(device
, "Congestion-fill threshold reached\n");
905 if (device
->act_log
->used
>= nc
->cong_extents
) {
906 drbd_info(device
, "Congestion-extents threshold reached\n");
911 /* start a new epoch for non-mirrored writes */
912 start_new_tl_epoch(first_peer_device(device
)->connection
);
914 if (on_congestion
== OC_PULL_AHEAD
)
915 _drbd_set_state(_NS(device
, conn
, C_AHEAD
), 0, NULL
);
916 else /*nc->on_congestion == OC_DISCONNECT */
917 _drbd_set_state(_NS(device
, conn
, C_DISCONNECTING
), 0, NULL
);
922 /* If this returns false, and req->private_bio is still set,
923 * this should be submitted locally.
925 * If it returns false, but req->private_bio is not set,
926 * we do not have access to good data :(
928 * Otherwise, this destroys req->private_bio, if any,
931 static bool do_remote_read(struct drbd_request
*req
)
933 struct drbd_device
*device
= req
->device
;
934 enum drbd_read_balancing rbm
;
936 if (req
->private_bio
) {
937 if (!drbd_may_do_local_read(device
,
938 req
->i
.sector
, req
->i
.size
)) {
939 bio_put(req
->private_bio
);
940 req
->private_bio
= NULL
;
945 if (device
->state
.pdsk
!= D_UP_TO_DATE
)
948 if (req
->private_bio
== NULL
)
951 /* TODO: improve read balancing decisions, take into account drbd
952 * protocol, pending requests etc. */
955 rbm
= rcu_dereference(device
->ldev
->disk_conf
)->read_balancing
;
958 if (rbm
== RB_PREFER_LOCAL
&& req
->private_bio
)
959 return false; /* submit locally */
961 if (remote_due_to_read_balancing(device
, req
->i
.sector
, rbm
)) {
962 if (req
->private_bio
) {
963 bio_put(req
->private_bio
);
964 req
->private_bio
= NULL
;
973 /* returns number of connections (== 1, for drbd 8.4)
974 * expected to actually write this data,
975 * which does NOT include those that we are L_AHEAD for. */
976 static int drbd_process_write_request(struct drbd_request
*req
)
978 struct drbd_device
*device
= req
->device
;
979 int remote
, send_oos
;
981 remote
= drbd_should_do_remote(device
->state
);
982 send_oos
= drbd_should_send_out_of_sync(device
->state
);
984 /* Need to replicate writes. Unless it is an empty flush,
985 * which is better mapped to a DRBD P_BARRIER packet,
986 * also for drbd wire protocol compatibility reasons.
987 * If this was a flush, just start a new epoch.
988 * Unless the current epoch was empty anyways, or we are not currently
989 * replicating, in which case there is no point. */
990 if (unlikely(req
->i
.size
== 0)) {
991 /* The only size==0 bios we expect are empty flushes. */
992 D_ASSERT(device
, req
->master_bio
->bi_rw
& REQ_FLUSH
);
994 _req_mod(req
, QUEUE_AS_DRBD_BARRIER
);
998 if (!remote
&& !send_oos
)
1001 D_ASSERT(device
, !(remote
&& send_oos
));
1004 _req_mod(req
, TO_BE_SENT
);
1005 _req_mod(req
, QUEUE_FOR_NET_WRITE
);
1006 } else if (drbd_set_out_of_sync(device
, req
->i
.sector
, req
->i
.size
))
1007 _req_mod(req
, QUEUE_FOR_SEND_OOS
);
1013 drbd_submit_req_private_bio(struct drbd_request
*req
)
1015 struct drbd_device
*device
= req
->device
;
1016 struct bio
*bio
= req
->private_bio
;
1017 const int rw
= bio_rw(bio
);
1019 bio
->bi_bdev
= device
->ldev
->backing_bdev
;
1021 /* State may have changed since we grabbed our reference on the
1022 * ->ldev member. Double check, and short-circuit to endio.
1023 * In case the last activity log transaction failed to get on
1024 * stable storage, and this is a WRITE, we may not even submit
1026 if (get_ldev(device
)) {
1027 if (drbd_insert_fault(device
,
1028 rw
== WRITE
? DRBD_FAULT_DT_WR
1029 : rw
== READ
? DRBD_FAULT_DT_RD
1030 : DRBD_FAULT_DT_RA
))
1031 bio_endio(bio
, -EIO
);
1033 generic_make_request(bio
);
1036 bio_endio(bio
, -EIO
);
1039 static void drbd_queue_write(struct drbd_device
*device
, struct drbd_request
*req
)
1041 spin_lock(&device
->submit
.lock
);
1042 list_add_tail(&req
->tl_requests
, &device
->submit
.writes
);
1043 spin_unlock(&device
->submit
.lock
);
1044 queue_work(device
->submit
.wq
, &device
->submit
.worker
);
1047 /* returns the new drbd_request pointer, if the caller is expected to
1048 * drbd_send_and_submit() it (to save latency), or NULL if we queued the
1049 * request on the submitter thread.
1050 * Returns ERR_PTR(-ENOMEM) if we cannot allocate a drbd_request.
1052 static struct drbd_request
*
1053 drbd_request_prepare(struct drbd_device
*device
, struct bio
*bio
, unsigned long start_time
)
1055 const int rw
= bio_data_dir(bio
);
1056 struct drbd_request
*req
;
1058 /* allocate outside of all locks; */
1059 req
= drbd_req_new(device
, bio
);
1062 /* only pass the error to the upper layers.
1063 * if user cannot handle io errors, that's not our business. */
1064 drbd_err(device
, "could not kmalloc() req\n");
1065 bio_endio(bio
, -ENOMEM
);
1066 return ERR_PTR(-ENOMEM
);
1068 req
->start_time
= start_time
;
1070 if (!get_ldev(device
)) {
1071 bio_put(req
->private_bio
);
1072 req
->private_bio
= NULL
;
1075 /* Update disk stats */
1076 _drbd_start_io_acct(device
, req
);
1078 if (rw
== WRITE
&& req
->private_bio
&& req
->i
.size
1079 && !test_bit(AL_SUSPENDED
, &device
->flags
)) {
1080 if (!drbd_al_begin_io_fastpath(device
, &req
->i
)) {
1081 drbd_queue_write(device
, req
);
1084 req
->rq_state
|= RQ_IN_ACT_LOG
;
1090 static void drbd_send_and_submit(struct drbd_device
*device
, struct drbd_request
*req
)
1092 struct drbd_resource
*resource
= device
->resource
;
1093 const int rw
= bio_rw(req
->master_bio
);
1094 struct bio_and_error m
= { NULL
, };
1095 bool no_remote
= false;
1096 bool submit_private_bio
= false;
1098 spin_lock_irq(&resource
->req_lock
);
1100 /* This may temporarily give up the req_lock,
1101 * but will re-aquire it before it returns here.
1102 * Needs to be before the check on drbd_suspended() */
1103 complete_conflicting_writes(req
);
1104 /* no more giving up req_lock from now on! */
1106 /* check for congestion, and potentially stop sending
1107 * full data updates, but start sending "dirty bits" only. */
1108 maybe_pull_ahead(device
);
1112 if (drbd_suspended(device
)) {
1113 /* push back and retry: */
1114 req
->rq_state
|= RQ_POSTPONED
;
1115 if (req
->private_bio
) {
1116 bio_put(req
->private_bio
);
1117 req
->private_bio
= NULL
;
1123 /* We fail READ/READA early, if we can not serve it.
1124 * We must do this before req is registered on any lists.
1125 * Otherwise, drbd_req_complete() will queue failed READ for retry. */
1127 if (!do_remote_read(req
) && !req
->private_bio
)
1131 /* which transfer log epoch does this belong to? */
1132 req
->epoch
= atomic_read(&first_peer_device(device
)->connection
->current_tle_nr
);
1134 /* no point in adding empty flushes to the transfer log,
1135 * they are mapped to drbd barriers already. */
1136 if (likely(req
->i
.size
!=0)) {
1138 first_peer_device(device
)->connection
->current_tle_writes
++;
1140 list_add_tail(&req
->tl_requests
, &first_peer_device(device
)->connection
->transfer_log
);
1144 if (!drbd_process_write_request(req
))
1147 /* We either have a private_bio, or we can read from remote.
1148 * Otherwise we had done the goto nodata above. */
1149 if (req
->private_bio
== NULL
) {
1150 _req_mod(req
, TO_BE_SENT
);
1151 _req_mod(req
, QUEUE_FOR_NET_READ
);
1156 if (req
->private_bio
) {
1157 /* needs to be marked within the same spinlock */
1158 _req_mod(req
, TO_BE_SUBMITTED
);
1159 /* but we need to give up the spinlock to submit */
1160 submit_private_bio
= true;
1161 } else if (no_remote
) {
1163 if (__ratelimit(&drbd_ratelimit_state
))
1164 drbd_err(device
, "IO ERROR: neither local nor remote data, sector %llu+%u\n",
1165 (unsigned long long)req
->i
.sector
, req
->i
.size
>> 9);
1166 /* A write may have been queued for send_oos, however.
1167 * So we can not simply free it, we must go through drbd_req_put_completion_ref() */
1171 if (drbd_req_put_completion_ref(req
, &m
, 1))
1172 kref_put(&req
->kref
, drbd_req_destroy
);
1173 spin_unlock_irq(&resource
->req_lock
);
1175 /* Even though above is a kref_put(), this is safe.
1176 * As long as we still need to submit our private bio,
1177 * we hold a completion ref, and the request cannot disappear.
1178 * If however this request did not even have a private bio to submit
1179 * (e.g. remote read), req may already be invalid now.
1180 * That's why we cannot check on req->private_bio. */
1181 if (submit_private_bio
)
1182 drbd_submit_req_private_bio(req
);
1184 complete_master_bio(device
, &m
);
1187 void __drbd_make_request(struct drbd_device
*device
, struct bio
*bio
, unsigned long start_time
)
1189 struct drbd_request
*req
= drbd_request_prepare(device
, bio
, start_time
);
1190 if (IS_ERR_OR_NULL(req
))
1192 drbd_send_and_submit(device
, req
);
1195 static void submit_fast_path(struct drbd_device
*device
, struct list_head
*incoming
)
1197 struct drbd_request
*req
, *tmp
;
1198 list_for_each_entry_safe(req
, tmp
, incoming
, tl_requests
) {
1199 const int rw
= bio_data_dir(req
->master_bio
);
1201 if (rw
== WRITE
/* rw != WRITE should not even end up here! */
1202 && req
->private_bio
&& req
->i
.size
1203 && !test_bit(AL_SUSPENDED
, &device
->flags
)) {
1204 if (!drbd_al_begin_io_fastpath(device
, &req
->i
))
1207 req
->rq_state
|= RQ_IN_ACT_LOG
;
1210 list_del_init(&req
->tl_requests
);
1211 drbd_send_and_submit(device
, req
);
1215 static bool prepare_al_transaction_nonblock(struct drbd_device
*device
,
1216 struct list_head
*incoming
,
1217 struct list_head
*pending
)
1219 struct drbd_request
*req
, *tmp
;
1223 spin_lock_irq(&device
->al_lock
);
1224 list_for_each_entry_safe(req
, tmp
, incoming
, tl_requests
) {
1225 err
= drbd_al_begin_io_nonblock(device
, &req
->i
);
1230 req
->rq_state
|= RQ_IN_ACT_LOG
;
1231 list_move_tail(&req
->tl_requests
, pending
);
1233 spin_unlock_irq(&device
->al_lock
);
1235 wake_up(&device
->al_wait
);
1237 return !list_empty(pending
);
1240 void do_submit(struct work_struct
*ws
)
1242 struct drbd_device
*device
= container_of(ws
, struct drbd_device
, submit
.worker
);
1243 LIST_HEAD(incoming
);
1245 struct drbd_request
*req
, *tmp
;
1248 spin_lock(&device
->submit
.lock
);
1249 list_splice_tail_init(&device
->submit
.writes
, &incoming
);
1250 spin_unlock(&device
->submit
.lock
);
1252 submit_fast_path(device
, &incoming
);
1253 if (list_empty(&incoming
))
1257 wait_event(device
->al_wait
, prepare_al_transaction_nonblock(device
, &incoming
, &pending
));
1258 /* Maybe more was queued, while we prepared the transaction?
1259 * Try to stuff them into this transaction as well.
1260 * Be strictly non-blocking here, no wait_event, we already
1261 * have something to commit.
1262 * Stop if we don't make any more progres.
1265 LIST_HEAD(more_pending
);
1266 LIST_HEAD(more_incoming
);
1269 /* It is ok to look outside the lock,
1270 * it's only an optimization anyways */
1271 if (list_empty(&device
->submit
.writes
))
1274 spin_lock(&device
->submit
.lock
);
1275 list_splice_tail_init(&device
->submit
.writes
, &more_incoming
);
1276 spin_unlock(&device
->submit
.lock
);
1278 if (list_empty(&more_incoming
))
1281 made_progress
= prepare_al_transaction_nonblock(device
, &more_incoming
, &more_pending
);
1283 list_splice_tail_init(&more_pending
, &pending
);
1284 list_splice_tail_init(&more_incoming
, &incoming
);
1289 drbd_al_begin_io_commit(device
);
1291 list_for_each_entry_safe(req
, tmp
, &pending
, tl_requests
) {
1292 list_del_init(&req
->tl_requests
);
1293 drbd_send_and_submit(device
, req
);
1296 /* If all currently hot activity log extents are kept busy by
1297 * incoming requests, we still must not totally starve new
1298 * requests to cold extents. In that case, prepare one request
1299 * in blocking mode. */
1300 list_for_each_entry_safe(req
, tmp
, &incoming
, tl_requests
) {
1301 list_del_init(&req
->tl_requests
);
1302 req
->rq_state
|= RQ_IN_ACT_LOG
;
1303 if (!drbd_al_begin_io_prepare(device
, &req
->i
)) {
1304 /* Corresponding extent was hot after all? */
1305 drbd_send_and_submit(device
, req
);
1307 /* Found a request to a cold extent.
1308 * Put on "pending" list,
1309 * and try to cumulate with more. */
1310 list_add(&req
->tl_requests
, &pending
);
1311 goto skip_fast_path
;
1317 void drbd_make_request(struct request_queue
*q
, struct bio
*bio
)
1319 struct drbd_device
*device
= (struct drbd_device
*) q
->queuedata
;
1320 unsigned long start_time
;
1322 start_time
= jiffies
;
1325 * what we "blindly" assume:
1327 D_ASSERT(device
, IS_ALIGNED(bio
->bi_iter
.bi_size
, 512));
1330 __drbd_make_request(device
, bio
, start_time
);
1333 /* This is called by bio_add_page().
1335 * q->max_hw_sectors and other global limits are already enforced there.
1337 * We need to call down to our lower level device,
1338 * in case it has special restrictions.
1340 * We also may need to enforce configured max-bio-bvecs limits.
1342 * As long as the BIO is empty we have to allow at least one bvec,
1343 * regardless of size and offset, so no need to ask lower levels.
1345 int drbd_merge_bvec(struct request_queue
*q
, struct bvec_merge_data
*bvm
, struct bio_vec
*bvec
)
1347 struct drbd_device
*device
= (struct drbd_device
*) q
->queuedata
;
1348 unsigned int bio_size
= bvm
->bi_size
;
1349 int limit
= DRBD_MAX_BIO_SIZE
;
1352 if (bio_size
&& get_ldev(device
)) {
1353 unsigned int max_hw_sectors
= queue_max_hw_sectors(q
);
1354 struct request_queue
* const b
=
1355 device
->ldev
->backing_bdev
->bd_disk
->queue
;
1356 if (b
->merge_bvec_fn
) {
1357 backing_limit
= b
->merge_bvec_fn(b
, bvm
, bvec
);
1358 limit
= min(limit
, backing_limit
);
1361 if ((limit
>> 9) > max_hw_sectors
)
1362 limit
= max_hw_sectors
<< 9;
1367 static void find_oldest_requests(
1368 struct drbd_connection
*connection
,
1369 struct drbd_device
*device
,
1370 struct drbd_request
**oldest_req_waiting_for_peer
,
1371 struct drbd_request
**oldest_req_waiting_for_disk
)
1373 struct drbd_request
*r
;
1374 *oldest_req_waiting_for_peer
= NULL
;
1375 *oldest_req_waiting_for_disk
= NULL
;
1376 list_for_each_entry(r
, &connection
->transfer_log
, tl_requests
) {
1377 const unsigned s
= r
->rq_state
;
1378 if (!*oldest_req_waiting_for_peer
1379 && ((s
& RQ_NET_MASK
) && !(s
& RQ_NET_DONE
)))
1380 *oldest_req_waiting_for_peer
= r
;
1382 if (!*oldest_req_waiting_for_disk
1383 && (s
& RQ_LOCAL_PENDING
) && r
->device
== device
)
1384 *oldest_req_waiting_for_disk
= r
;
1386 if (*oldest_req_waiting_for_peer
&& *oldest_req_waiting_for_disk
)
1391 void request_timer_fn(unsigned long data
)
1393 struct drbd_device
*device
= (struct drbd_device
*) data
;
1394 struct drbd_connection
*connection
= first_peer_device(device
)->connection
;
1395 struct drbd_request
*req_disk
, *req_peer
; /* oldest request */
1396 struct net_conf
*nc
;
1397 unsigned long ent
= 0, dt
= 0, et
, nt
; /* effective timeout = ko_count * timeout */
1401 nc
= rcu_dereference(connection
->net_conf
);
1402 if (nc
&& device
->state
.conn
>= C_WF_REPORT_PARAMS
)
1403 ent
= nc
->timeout
* HZ
/10 * nc
->ko_count
;
1405 if (get_ldev(device
)) { /* implicit state.disk >= D_INCONSISTENT */
1406 dt
= rcu_dereference(device
->ldev
->disk_conf
)->disk_timeout
* HZ
/ 10;
1411 et
= min_not_zero(dt
, ent
);
1414 return; /* Recurring timer stopped */
1418 spin_lock_irq(&device
->resource
->req_lock
);
1419 find_oldest_requests(connection
, device
, &req_peer
, &req_disk
);
1420 if (req_peer
== NULL
&& req_disk
== NULL
) {
1421 spin_unlock_irq(&device
->resource
->req_lock
);
1422 mod_timer(&device
->request_timer
, now
+ et
);
1426 /* The request is considered timed out, if
1427 * - we have some effective timeout from the configuration,
1428 * with above state restrictions applied,
1429 * - the oldest request is waiting for a response from the network
1430 * resp. the local disk,
1431 * - the oldest request is in fact older than the effective timeout,
1432 * - the connection was established (resp. disk was attached)
1433 * for longer than the timeout already.
1434 * Note that for 32bit jiffies and very stable connections/disks,
1435 * we may have a wrap around, which is catched by
1436 * !time_in_range(now, last_..._jif, last_..._jif + timeout).
1438 * Side effect: once per 32bit wrap-around interval, which means every
1439 * ~198 days with 250 HZ, we have a window where the timeout would need
1440 * to expire twice (worst case) to become effective. Good enough.
1442 if (ent
&& req_peer
&&
1443 time_after(now
, req_peer
->start_time
+ ent
) &&
1444 !time_in_range(now
, connection
->last_reconnect_jif
, connection
->last_reconnect_jif
+ ent
)) {
1445 drbd_warn(device
, "Remote failed to finish a request within ko-count * timeout\n");
1446 _drbd_set_state(_NS(device
, conn
, C_TIMEOUT
), CS_VERBOSE
| CS_HARD
, NULL
);
1448 if (dt
&& req_disk
&&
1449 time_after(now
, req_disk
->start_time
+ dt
) &&
1450 !time_in_range(now
, device
->last_reattach_jif
, device
->last_reattach_jif
+ dt
)) {
1451 drbd_warn(device
, "Local backing device failed to meet the disk-timeout\n");
1452 __drbd_chk_io_error(device
, DRBD_FORCE_DETACH
);
1455 /* Reschedule timer for the nearest not already expired timeout.
1456 * Fallback to now + min(effective network timeout, disk timeout). */
1457 ent
= (ent
&& req_peer
&& time_before(now
, req_peer
->start_time
+ ent
))
1458 ? req_peer
->start_time
+ ent
: now
+ et
;
1459 dt
= (dt
&& req_disk
&& time_before(now
, req_disk
->start_time
+ dt
))
1460 ? req_disk
->start_time
+ dt
: now
+ et
;
1461 nt
= time_before(ent
, dt
) ? ent
: dt
;
1462 spin_unlock_irq(&connection
->resource
->req_lock
);
1463 mod_timer(&device
->request_timer
, nt
);