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 /* Update disk stats at start of I/O request */
35 static void _drbd_start_io_acct(struct drbd_conf
*mdev
, struct drbd_request
*req
, struct bio
*bio
)
37 const int rw
= bio_data_dir(bio
);
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
));
42 part_inc_in_flight(&mdev
->vdisk
->part0
, rw
);
46 /* Update disk stats when completing request upwards */
47 static void _drbd_end_io_acct(struct drbd_conf
*mdev
, struct drbd_request
*req
)
49 int rw
= bio_data_dir(req
->master_bio
);
50 unsigned long duration
= jiffies
- req
->start_time
;
52 cpu
= part_stat_lock();
53 part_stat_add(cpu
, &mdev
->vdisk
->part0
, ticks
[rw
], duration
);
54 part_round_stats(cpu
, &mdev
->vdisk
->part0
);
55 part_dec_in_flight(&mdev
->vdisk
->part0
, rw
);
59 static struct drbd_request
*drbd_req_new(struct drbd_conf
*mdev
,
62 struct drbd_request
*req
;
64 req
= mempool_alloc(drbd_request_mempool
, GFP_NOIO
);
68 drbd_req_make_private_bio(req
, bio_src
);
69 req
->rq_state
= bio_data_dir(bio_src
) == WRITE
? RQ_WRITE
: 0;
71 req
->master_bio
= bio_src
;
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
);
82 static void drbd_req_free(struct drbd_request
*req
)
84 mempool_free(req
, drbd_request_mempool
);
87 /* rw is bio_data_dir(), only READ or WRITE */
88 static void _req_is_done(struct drbd_conf
*mdev
, struct drbd_request
*req
, const int rw
)
90 const unsigned long s
= req
->rq_state
;
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
);
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. */
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
))
109 drbd_set_out_of_sync(mdev
, req
->i
.sector
, req
->i
.size
);
111 if ((s
& RQ_NET_OK
) && (s
& RQ_LOCAL_OK
) && (s
& RQ_NET_SIS
))
112 drbd_set_in_sync(mdev
, req
->i
.sector
, req
->i
.size
);
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.
124 if (s
& RQ_LOCAL_MASK
) {
125 if (get_ldev_if_state(mdev
, D_FAILED
)) {
126 if (s
& RQ_IN_ACT_LOG
)
127 drbd_al_complete_io(mdev
, req
->i
.sector
);
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",
132 (unsigned long long) req
->i
.sector
);
140 static void queue_barrier(struct drbd_conf
*mdev
)
142 struct drbd_tl_epoch
*b
;
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
))
152 b
= mdev
->tconn
->newest_tle
;
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
);
159 drbd_queue_work(&mdev
->tconn
->data
.work
, &b
->w
);
160 set_bit(CREATE_BARRIER
, &mdev
->flags
);
163 static void _about_to_complete_local_write(struct drbd_conf
*mdev
,
164 struct drbd_request
*req
)
166 const unsigned long s
= req
->rq_state
;
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.
174 if (mdev
->state
.conn
>= C_CONNECTED
&&
175 (s
& RQ_NET_SENT
) != 0 &&
176 req
->epoch
== mdev
->tconn
->newest_tle
->br_number
)
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
);
184 void complete_master_bio(struct drbd_conf
*mdev
,
185 struct bio_and_error
*m
)
187 bio_endio(m
->bio
, m
->error
);
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.
197 void _req_may_be_done(struct drbd_request
*req
, struct bio_and_error
*m
)
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
;
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
211 * the bio_endio completion callbacks.
213 if (s
& RQ_NET_QUEUED
)
215 if (s
& RQ_NET_PENDING
)
217 if (s
& RQ_LOCAL_PENDING
)
220 if (req
->master_bio
) {
221 /* this is DATA_RECEIVED (remote read)
222 * or protocol C P_WRITE_ACK
223 * or protocol B P_RECV_ACK
224 * or protocol A "HANDED_OVER_TO_NETWORK" (SendAck)
225 * or canceled or failed,
226 * or killed from the transfer log due to connection loss.
230 * figure out whether to report success or failure.
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.
236 * what to do about the failures is handled elsewhere.
237 * what we need to do here is just: complete the master_bio.
239 * local completion error, if any, has been stored as ERR_PTR
240 * in private_bio within drbd_endio_pri.
242 int ok
= (s
& RQ_LOCAL_OK
) || (s
& RQ_NET_OK
);
243 int error
= PTR_ERR(req
->private_bio
);
245 /* remove the request from the conflict detection
246 * respective block_id verification hash */
247 if (!drbd_interval_empty(&req
->i
)) {
248 struct rb_root
*root
;
251 root
= &mdev
->write_requests
;
253 root
= &mdev
->read_requests
;
254 drbd_remove_interval(root
, &req
->i
);
256 D_ASSERT((s
& (RQ_NET_MASK
& ~RQ_NET_DONE
)) == 0);
258 /* for writes we need to do some extra housekeeping */
260 _about_to_complete_local_write(mdev
, req
);
262 /* Update disk stats */
263 _drbd_end_io_acct(mdev
, req
);
265 m
->error
= ok
? 0 : (error
?: -EIO
);
266 m
->bio
= req
->master_bio
;
267 req
->master_bio
= NULL
;
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
);
277 /* else: network part and not DONE yet. that is
278 * protocol A or B, barrier ack still pending... */
281 static void _req_may_be_done_not_susp(struct drbd_request
*req
, struct bio_and_error
*m
)
283 struct drbd_conf
*mdev
= req
->mdev
;
285 if (!is_susp(mdev
->state
))
286 _req_may_be_done(req
, m
);
290 * checks whether there was an overlapping request
291 * or ee already registered.
293 * if so, return 1, in which case this request is completed on the spot,
294 * without ever being submitted or send.
296 * return 0 if it is ok to submit this request.
299 * paranoia: assume something above us is broken, and issues different write
300 * requests for the same block simultaneously...
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.
308 * In case we only have one primary, the epoch_entries tree is empty.
310 static int _req_conflicts(struct drbd_request
*req
)
312 struct drbd_conf
*mdev
= req
->mdev
;
313 const sector_t sector
= req
->i
.sector
;
314 const int size
= req
->i
.size
;
315 struct drbd_interval
*i
;
317 D_ASSERT(drbd_interval_empty(&req
->i
));
319 if (!get_net_conf(mdev
->tconn
))
322 i
= drbd_find_overlap(&mdev
->write_requests
, sector
, size
);
324 struct drbd_request
*req2
=
325 container_of(i
, struct drbd_request
, i
);
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
);
336 if (!RB_EMPTY_ROOT(&mdev
->epoch_entries
)) {
337 /* check for overlapping requests with remote origin */
338 i
= drbd_find_overlap(&mdev
->epoch_entries
, sector
, size
);
340 struct drbd_epoch_entry
*e
=
341 container_of(i
, struct drbd_epoch_entry
, i
);
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
);
353 /* this is like it should be, and what we expected.
354 * our users do behave after all... */
355 put_net_conf(mdev
->tconn
);
359 put_net_conf(mdev
->tconn
);
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).
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 ...
375 int __req_mod(struct drbd_request
*req
, enum drbd_req_event what
,
376 struct bio_and_error
*m
)
378 struct drbd_conf
*mdev
= req
->mdev
;
384 dev_err(DEV
, "LOGIC BUG in %s:%u\n", __FILE__
, __LINE__
);
387 /* does not happen...
388 * initialization done in drbd_req_new
393 case TO_BE_SENT
: /* via network */
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
);
401 case TO_BE_SUBMITTED
: /* locally */
402 /* reached via drbd_make_request_common */
403 D_ASSERT(!(req
->rq_state
& RQ_LOCAL_MASK
));
404 req
->rq_state
|= RQ_LOCAL_PENDING
;
408 if (bio_data_dir(req
->master_bio
) == WRITE
)
409 mdev
->writ_cnt
+= req
->i
.size
>> 9;
411 mdev
->read_cnt
+= req
->i
.size
>> 9;
413 req
->rq_state
|= (RQ_LOCAL_COMPLETED
|RQ_LOCAL_OK
);
414 req
->rq_state
&= ~RQ_LOCAL_PENDING
;
416 _req_may_be_done_not_susp(req
, m
);
420 case WRITE_COMPLETED_WITH_ERROR
:
421 req
->rq_state
|= RQ_LOCAL_COMPLETED
;
422 req
->rq_state
&= ~RQ_LOCAL_PENDING
;
424 __drbd_chk_io_error(mdev
, false);
425 _req_may_be_done_not_susp(req
, m
);
429 case READ_AHEAD_COMPLETED_WITH_ERROR
:
430 /* it is legal to fail READA */
431 req
->rq_state
|= RQ_LOCAL_COMPLETED
;
432 req
->rq_state
&= ~RQ_LOCAL_PENDING
;
433 _req_may_be_done_not_susp(req
, m
);
437 case READ_COMPLETED_WITH_ERROR
:
438 drbd_set_out_of_sync(mdev
, req
->i
.sector
, req
->i
.size
);
440 req
->rq_state
|= RQ_LOCAL_COMPLETED
;
441 req
->rq_state
&= ~RQ_LOCAL_PENDING
;
443 D_ASSERT(!(req
->rq_state
& RQ_NET_MASK
));
445 __drbd_chk_io_error(mdev
, false);
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
) {
451 _req_may_be_done_not_susp(req
, m
);
455 /* _req_mod(req,TO_BE_SENT); oops, recursion... */
456 req
->rq_state
|= RQ_NET_PENDING
;
457 inc_ap_pending(mdev
);
458 /* fall through: _req_mod(req,QUEUE_FOR_NET_READ); */
460 case QUEUE_FOR_NET_READ
:
461 /* READ or READA, and
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 */
468 /* so we can verify the handle in the answer packet
469 * corresponding hlist_del is in _req_may_be_done() */
470 drbd_insert_interval(&mdev
->read_requests
, &req
->i
);
472 set_bit(UNPLUG_REMOTE
, &mdev
->flags
);
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
479 drbd_queue_work(&mdev
->tconn
->data
.work
, &req
->w
);
482 case QUEUE_FOR_NET_WRITE
:
483 /* assert something? */
484 /* from drbd_make_request_common only */
486 /* corresponding hlist_del is in _req_may_be_done() */
487 drbd_insert_interval(&mdev
->write_requests
, &req
->i
);
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.
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.
501 * Add req to the (now) current epoch (barrier). */
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
);
508 /* see drbd_make_request_common,
509 * just after it grabs the req_lock */
510 D_ASSERT(test_bit(CREATE_BARRIER
, &mdev
->flags
) == 0);
512 req
->epoch
= mdev
->tconn
->newest_tle
->br_number
;
514 /* increment size of current epoch */
515 mdev
->tconn
->newest_tle
->n_writes
++;
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
;
521 drbd_queue_work(&mdev
->tconn
->data
.work
, &req
->w
);
523 /* close the epoch, in case it outgrew the limit */
524 if (mdev
->tconn
->newest_tle
->n_writes
>= mdev
->tconn
->net_conf
->max_epoch_size
)
529 case QUEUE_FOR_SEND_OOS
:
530 req
->rq_state
|= RQ_NET_QUEUED
;
531 req
->w
.cb
= w_send_oos
;
532 drbd_queue_work(&mdev
->tconn
->data
.work
, &req
->w
);
535 case OOS_HANDED_TO_NETWORK
:
536 /* actually the same */
538 /* treat it the same */
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! */
545 _req_may_be_done_not_susp(req
, m
);
548 case HANDED_OVER_TO_NETWORK
:
549 /* assert something? */
550 if (bio_data_dir(req
->master_bio
) == WRITE
)
551 atomic_add(req
->i
.size
>> 9, &mdev
->ap_in_flight
);
553 if (bio_data_dir(req
->master_bio
) == WRITE
&&
554 mdev
->tconn
->net_conf
->wire_protocol
== DRBD_PROT_A
) {
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 */
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
569 * dereference the bio even after the "WRITE_ACKED_BY_PEER" and
570 * "COMPLETED_OK" events came in, once we return from
571 * _drbd_send_zc_bio (drbd_send_dblock), we have to check
572 * whether it is done already, and end it. */
573 _req_may_be_done_not_susp(req
, m
);
576 case READ_RETRY_REMOTE_CANCELED
:
577 req
->rq_state
&= ~RQ_NET_QUEUED
;
578 /* fall through, in case we raced with drbd_disconnect */
579 case CONNECTION_LOST_WHILE_PENDING
:
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
;
586 if (req
->rq_state
& RQ_NET_SENT
&& req
->rq_state
& RQ_WRITE
)
587 atomic_sub(req
->i
.size
>> 9, &mdev
->ap_in_flight
);
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
))
592 _req_may_be_done(req
, m
); /* Allowed while state.susp */
595 case WRITE_ACKED_BY_PEER_AND_SIS
:
596 req
->rq_state
|= RQ_NET_SIS
;
597 case CONFLICT_DISCARDED_BY_PEER
:
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. */
601 if (what
== CONFLICT_DISCARDED_BY_PEER
)
602 dev_alert(DEV
, "Got DiscardAck packet %llus +%u!"
603 " DRBD is not a random data generator!\n",
604 (unsigned long long)req
->i
.sector
, req
->i
.size
);
605 req
->rq_state
|= RQ_NET_DONE
;
607 case WRITE_ACKED_BY_PEER
:
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.
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. */
618 /* this makes it effectively the same as for: */
619 case RECV_ACKED_BY_PEER
:
620 /* protocol B; pretends to be successfully written on peer.
621 * see also notes above in HANDED_OVER_TO_NETWORK about
623 req
->rq_state
|= RQ_NET_OK
;
624 D_ASSERT(req
->rq_state
& RQ_NET_PENDING
);
625 dec_ap_pending(mdev
);
626 atomic_sub(req
->i
.size
>> 9, &mdev
->ap_in_flight
);
627 req
->rq_state
&= ~RQ_NET_PENDING
;
628 _req_may_be_done_not_susp(req
, m
);
632 /* assert something? */
633 if (req
->rq_state
& RQ_NET_PENDING
) {
634 dec_ap_pending(mdev
);
635 atomic_sub(req
->i
.size
>> 9, &mdev
->ap_in_flight
);
637 req
->rq_state
&= ~(RQ_NET_OK
|RQ_NET_PENDING
);
639 req
->rq_state
|= RQ_NET_DONE
;
640 _req_may_be_done_not_susp(req
, m
);
641 /* else: done by HANDED_OVER_TO_NETWORK */
644 case FAIL_FROZEN_DISK_IO
:
645 if (!(req
->rq_state
& RQ_LOCAL_COMPLETED
))
648 _req_may_be_done(req
, m
); /* Allowed while state.susp */
651 case RESTART_FROZEN_DISK_IO
:
652 if (!(req
->rq_state
& RQ_LOCAL_COMPLETED
))
655 req
->rq_state
&= ~RQ_LOCAL_COMPLETED
;
658 if (bio_data_dir(req
->master_bio
) == WRITE
)
662 req
->w
.cb
= w_restart_disk_io
;
663 drbd_queue_work(&mdev
->tconn
->data
.work
, &req
->w
);
667 /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
668 before the connection loss (B&C only); only P_BARRIER_ACK was missing.
669 Trowing them out of the TL here by pretending we got a BARRIER_ACK
670 We ensure that the peer was not rebooted */
671 if (!(req
->rq_state
& RQ_NET_OK
)) {
673 drbd_queue_work(&mdev
->tconn
->data
.work
, &req
->w
);
674 rv
= req
->rq_state
& RQ_WRITE
? MR_WRITE
: MR_READ
;
678 /* else, fall through to BARRIER_ACKED */
681 if (!(req
->rq_state
& RQ_WRITE
))
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... */
688 dev_err(DEV
, "FIXME (BARRIER_ACKED but pending)\n");
689 list_move(&req
->tl_requests
, &mdev
->tconn
->out_of_sequence_requests
);
691 if ((req
->rq_state
& RQ_NET_MASK
) != 0) {
692 req
->rq_state
|= RQ_NET_DONE
;
693 if (mdev
->tconn
->net_conf
->wire_protocol
== DRBD_PROT_A
)
694 atomic_sub(req
->i
.size
>>9, &mdev
->ap_in_flight
);
696 _req_may_be_done(req
, m
); /* Allowed while state.susp */
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
);
704 _req_may_be_done_not_susp(req
, m
);
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.
718 static int drbd_may_do_local_read(struct drbd_conf
*mdev
, sector_t sector
, int size
)
720 unsigned long sbnr
, ebnr
;
721 sector_t esector
, nr_sectors
;
723 if (mdev
->state
.disk
== D_UP_TO_DATE
)
725 if (mdev
->state
.disk
>= D_OUTDATED
)
727 if (mdev
->state
.disk
< D_INCONSISTENT
)
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;
733 D_ASSERT(sector
< nr_sectors
);
734 D_ASSERT(esector
< nr_sectors
);
736 sbnr
= BM_SECT_TO_BIT(sector
);
737 ebnr
= BM_SECT_TO_BIT(esector
);
739 return 0 == drbd_bm_count_bits(mdev
, sbnr
, ebnr
);
742 static int drbd_make_request_common(struct drbd_conf
*mdev
, struct bio
*bio
, unsigned long start_time
)
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
;
749 int local
, remote
, send_oos
= 0;
753 /* allocate outside of all locks; */
754 req
= drbd_req_new(mdev
, bio
);
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
);
763 req
->start_time
= start_time
;
765 local
= get_ldev(mdev
);
767 bio_put(req
->private_bio
); /* or we get a bio leak */
768 req
->private_bio
= NULL
;
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.
782 bio_put(req
->private_bio
);
783 req
->private_bio
= NULL
;
787 remote
= !local
&& mdev
->state
.pdsk
>= D_UP_TO_DATE
;
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.
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.
798 if (rw
== READA
&& mdev
->state
.disk
>= D_INCONSISTENT
&& !local
) {
800 goto fail_and_free_req
;
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. */
808 if (rw
== WRITE
&& local
&& !test_bit(AL_SUSPENDED
, &mdev
->flags
)) {
809 req
->rq_state
|= RQ_IN_ACT_LOG
;
810 drbd_al_begin_io(mdev
, sector
);
813 remote
= remote
&& drbd_should_do_remote(mdev
->state
);
814 send_oos
= rw
== WRITE
&& drbd_should_send_oos(mdev
->state
);
815 D_ASSERT(!(remote
&& send_oos
));
817 if (!(local
|| remote
) && !is_susp(mdev
->state
)) {
818 if (__ratelimit(&drbd_ratelimit_state
))
819 dev_err(DEV
, "IO ERROR: neither local nor remote disk\n");
820 goto fail_free_complete
;
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. */
829 if (rw
== WRITE
&& (remote
|| send_oos
) &&
830 mdev
->tconn
->unused_spare_tle
== NULL
&&
831 test_bit(CREATE_BARRIER
, &mdev
->flags
)) {
833 b
= kmalloc(sizeof(struct drbd_tl_epoch
), GFP_NOIO
);
835 dev_err(DEV
, "Failed to alloc barrier.\n");
837 goto fail_free_complete
;
841 /* GOOD, everything prepared, grab the spin_lock */
842 spin_lock_irq(&mdev
->tconn
->req_lock
);
844 if (is_susp(mdev
->state
)) {
845 /* If we got suspended, use the retry mechanism of
846 generic_make_request() to restart processing of this
847 bio. In the next call to drbd_make_request
848 we sleep in inc_ap_bio() */
850 spin_unlock_irq(&mdev
->tconn
->req_lock
);
851 goto fail_free_complete
;
854 if (remote
|| send_oos
) {
855 remote
= drbd_should_do_remote(mdev
->state
);
856 send_oos
= rw
== WRITE
&& drbd_should_send_oos(mdev
->state
);
857 D_ASSERT(!(remote
&& send_oos
));
859 if (!(remote
|| send_oos
))
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");
863 spin_unlock_irq(&mdev
->tconn
->req_lock
);
864 goto fail_free_complete
;
868 if (b
&& mdev
->tconn
->unused_spare_tle
== NULL
) {
869 mdev
->tconn
->unused_spare_tle
= b
;
872 if (rw
== WRITE
&& (remote
|| send_oos
) &&
873 mdev
->tconn
->unused_spare_tle
== NULL
&&
874 test_bit(CREATE_BARRIER
, &mdev
->flags
)) {
875 /* someone closed the current epoch
876 * while we were grabbing the spinlock */
877 spin_unlock_irq(&mdev
->tconn
->req_lock
);
878 goto allocate_barrier
;
882 /* Update disk stats */
883 _drbd_start_io_acct(mdev
, req
, bio
);
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.
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. */
895 if ((remote
|| send_oos
) && mdev
->tconn
->unused_spare_tle
&&
896 test_and_clear_bit(CREATE_BARRIER
, &mdev
->flags
)) {
897 _tl_add_barrier(mdev
, mdev
->tconn
->unused_spare_tle
);
898 mdev
->tconn
->unused_spare_tle
= NULL
;
900 D_ASSERT(!(remote
&& rw
== WRITE
&&
901 test_bit(CREATE_BARRIER
, &mdev
->flags
)));
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.
910 * 'remote' may become wrong any time because the network could fail.
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.
917 /* mark them early for readability.
918 * this just sets some state flags. */
920 _req_mod(req
, TO_BE_SENT
);
922 _req_mod(req
, TO_BE_SUBMITTED
);
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...) */
927 if (rw
== WRITE
&& _req_conflicts(req
))
928 goto fail_conflicting
;
930 list_add_tail(&req
->tl_requests
, &mdev
->tconn
->newest_tle
->requests
);
932 /* NOTE remote first: to get the concurrent write detection right,
933 * we must register the request before start of local IO. */
935 /* either WRITE and C_CONNECTED,
936 * or READ, and no local disk,
937 * or READ, but not in sync.
939 _req_mod(req
, (rw
== WRITE
)
940 ? QUEUE_FOR_NET_WRITE
941 : QUEUE_FOR_NET_READ
);
943 if (send_oos
&& drbd_set_out_of_sync(mdev
, sector
, size
))
944 _req_mod(req
, QUEUE_FOR_SEND_OOS
);
947 mdev
->tconn
->net_conf
->on_congestion
!= OC_BLOCK
&& mdev
->tconn
->agreed_pro_version
>= 96) {
950 if (mdev
->tconn
->net_conf
->cong_fill
&&
951 atomic_read(&mdev
->ap_in_flight
) >= mdev
->tconn
->net_conf
->cong_fill
) {
952 dev_info(DEV
, "Congestion-fill threshold reached\n");
956 if (mdev
->act_log
->used
>= mdev
->tconn
->net_conf
->cong_extents
) {
957 dev_info(DEV
, "Congestion-extents threshold reached\n");
962 queue_barrier(mdev
); /* last barrier, after mirrored writes */
964 if (mdev
->tconn
->net_conf
->on_congestion
== OC_PULL_AHEAD
)
965 _drbd_set_state(_NS(mdev
, conn
, C_AHEAD
), 0, NULL
);
966 else /*mdev->tconn->net_conf->on_congestion == OC_DISCONNECT */
967 _drbd_set_state(_NS(mdev
, conn
, C_DISCONNECTING
), 0, NULL
);
971 spin_unlock_irq(&mdev
->tconn
->req_lock
);
972 kfree(b
); /* if someone else has beaten us to it... */
975 req
->private_bio
->bi_bdev
= mdev
->ldev
->backing_bdev
;
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
982 if (get_ldev(mdev
)) {
983 if (drbd_insert_fault(mdev
, rw
== WRITE
? DRBD_FAULT_DT_WR
984 : rw
== READ
? DRBD_FAULT_DT_RD
986 bio_endio(req
->private_bio
, -EIO
);
988 generic_make_request(req
->private_bio
);
991 bio_endio(req
->private_bio
, -EIO
);
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.
1003 _drbd_end_io_acct(mdev
, req
);
1004 spin_unlock_irq(&mdev
->tconn
->req_lock
);
1006 dec_ap_pending(mdev
);
1007 /* THINK: do we want to fail it (-EIO), or pretend success?
1008 * this pretends success. */
1012 if (req
->rq_state
& RQ_IN_ACT_LOG
)
1013 drbd_al_complete_io(mdev
, sector
);
1016 bio_put(req
->private_bio
);
1017 req
->private_bio
= NULL
;
1021 bio_endio(bio
, err
);
1030 /* helper function for drbd_make_request
1031 * if we can determine just by the mdev (state) that this request will fail,
1033 * otherwise return 0
1035 static int drbd_fail_request_early(struct drbd_conf
*mdev
, int is_write
)
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");
1052 int drbd_make_request(struct request_queue
*q
, struct bio
*bio
)
1054 unsigned int s_enr
, e_enr
;
1055 struct drbd_conf
*mdev
= (struct drbd_conf
*) q
->queuedata
;
1056 unsigned long start_time
;
1058 if (drbd_fail_request_early(mdev
, bio_data_dir(bio
) & WRITE
)) {
1059 bio_endio(bio
, -EPERM
);
1063 start_time
= jiffies
;
1066 * what we "blindly" assume:
1068 D_ASSERT(bio
->bi_size
> 0);
1069 D_ASSERT((bio
->bi_size
& 0x1ff) == 0);
1070 D_ASSERT(bio
->bi_idx
== 0);
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
;
1077 if (likely(s_enr
== e_enr
)) {
1078 inc_ap_bio(mdev
, 1);
1079 return drbd_make_request_common(mdev
, bio
, start_time
);
1082 /* can this bio be split generically?
1083 * Maybe add our own split-arbitrary-bios function. */
1084 if (bio
->bi_vcnt
!= 1 || bio
->bi_idx
!= 0 || bio
->bi_size
> DRBD_MAX_BIO_SIZE
) {
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
);
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
1099 * sps = 64, mask = 63
1100 * first_sectors = 64 - (262269 & 63) = 3
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
);
1106 bp
= bio_split(bio
, first_sectors
);
1108 /* we need to get a "reference count" (ap_bio_cnt)
1109 * to avoid races with the disconnect/reconnect/suspend code.
1110 * In case we need to split the bio here, we need to get three references
1111 * atomically, otherwise we might deadlock when trying to submit the
1113 inc_ap_bio(mdev
, 3);
1115 D_ASSERT(e_enr
== s_enr
+ 1);
1117 while (drbd_make_request_common(mdev
, &bp
->bio1
, start_time
))
1118 inc_ap_bio(mdev
, 1);
1120 while (drbd_make_request_common(mdev
, &bp
->bio2
, start_time
))
1121 inc_ap_bio(mdev
, 1);
1125 bio_pair_release(bp
);
1130 /* This is called by bio_add_page(). With this function we reduce
1131 * the number of BIOs that span over multiple DRBD_MAX_BIO_SIZEs
1132 * units (was AL_EXTENTs).
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.
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
1141 * drbd_make_request.
1143 int drbd_merge_bvec(struct request_queue
*q
, struct bvec_merge_data
*bvm
, struct bio_vec
*bvec
)
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
;
1151 limit
= DRBD_MAX_BIO_SIZE
1152 - ((bio_offset
& (DRBD_MAX_BIO_SIZE
-1)) + bio_size
);
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
;
1161 if (b
->merge_bvec_fn
) {
1162 backing_limit
= b
->merge_bvec_fn(b
, bvm
, bvec
);
1163 limit
= min(limit
, backing_limit
);
1170 void request_timer_fn(unsigned long data
)
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 */
1177 if (get_net_conf(mdev
->tconn
)) {
1178 et
= mdev
->tconn
->net_conf
->timeout
*HZ
/10 * mdev
->tconn
->net_conf
->ko_count
;
1179 put_net_conf(mdev
->tconn
);
1181 if (!et
|| mdev
->state
.conn
< C_WF_REPORT_PARAMS
)
1182 return; /* Recurring timer stopped */
1184 spin_lock_irq(&mdev
->tconn
->req_lock
);
1185 le
= &mdev
->tconn
->oldest_tle
->requests
;
1186 if (list_empty(le
)) {
1187 spin_unlock_irq(&mdev
->tconn
->req_lock
);
1188 mod_timer(&mdev
->request_timer
, jiffies
+ et
);
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
);
1199 dev_warn(DEV
, "Local backing block device frozen?\n");
1200 mod_timer(&mdev
->request_timer
, jiffies
+ et
);
1203 mod_timer(&mdev
->request_timer
, req
->start_time
+ et
);
1206 spin_unlock_irq(&mdev
->tconn
->req_lock
);