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 void _req_is_done(struct drbd_conf
*mdev
, struct drbd_request
*req
, const int rw
)
61 const unsigned long s
= req
->rq_state
;
62 /* if it was a write, we may have to set the corresponding
63 * bit(s) out-of-sync first. If it had a local part, we need to
64 * release the reference to the activity log. */
66 /* remove it from the transfer log.
67 * well, only if it had been there in the first
68 * place... if it had not (local only or conflicting
69 * and never sent), it should still be "empty" as
70 * initialized in drbd_req_new(), so we can list_del() it
71 * here unconditionally */
72 list_del(&req
->tl_requests
);
73 /* Set out-of-sync unless both OK flags are set
74 * (local only or remote failed).
75 * Other places where we set out-of-sync:
76 * READ with local io-error */
77 if (!(s
& RQ_NET_OK
) || !(s
& RQ_LOCAL_OK
))
78 drbd_set_out_of_sync(mdev
, req
->sector
, req
->size
);
80 if ((s
& RQ_NET_OK
) && (s
& RQ_LOCAL_OK
) && (s
& RQ_NET_SIS
))
81 drbd_set_in_sync(mdev
, req
->sector
, req
->size
);
83 /* one might be tempted to move the drbd_al_complete_io
84 * to the local io completion callback drbd_endio_pri.
85 * but, if this was a mirror write, we may only
86 * drbd_al_complete_io after this is RQ_NET_DONE,
87 * otherwise the extent could be dropped from the al
88 * before it has actually been written on the peer.
89 * if we crash before our peer knows about the request,
90 * but after the extent has been dropped from the al,
91 * we would forget to resync the corresponding extent.
93 if (s
& RQ_LOCAL_MASK
) {
94 if (get_ldev_if_state(mdev
, D_FAILED
)) {
95 drbd_al_complete_io(mdev
, req
->sector
);
97 } else if (__ratelimit(&drbd_ratelimit_state
)) {
98 dev_warn(DEV
, "Should have called drbd_al_complete_io(, %llu), "
99 "but my Disk seems to have failed :(\n",
100 (unsigned long long) req
->sector
);
105 /* if it was a local io error, we want to notify our
106 * peer about that, and see if we need to
107 * detach the disk and stuff.
108 * to avoid allocating some special work
109 * struct, reuse the request. */
112 * why do we do this not when we detect the error,
113 * but delay it until it is "done", i.e. possibly
114 * until the next barrier ack? */
117 ((s
& RQ_LOCAL_MASK
) && !(s
& RQ_LOCAL_OK
))) {
118 if (!(req
->w
.list
.next
== LIST_POISON1
||
119 list_empty(&req
->w
.list
))) {
120 /* DEBUG ASSERT only; if this triggers, we
121 * probably corrupt the worker list here */
122 dev_err(DEV
, "req->w.list.next = %p\n", req
->w
.list
.next
);
123 dev_err(DEV
, "req->w.list.prev = %p\n", req
->w
.list
.prev
);
125 req
->w
.cb
= w_io_error
;
126 drbd_queue_work(&mdev
->data
.work
, &req
->w
);
127 /* drbd_req_free() is done in w_io_error */
133 static void queue_barrier(struct drbd_conf
*mdev
)
135 struct drbd_tl_epoch
*b
;
137 /* We are within the req_lock. Once we queued the barrier for sending,
138 * we set the CREATE_BARRIER bit. It is cleared as soon as a new
139 * barrier/epoch object is added. This is the only place this bit is
140 * set. It indicates that the barrier for this epoch is already queued,
141 * and no new epoch has been created yet. */
142 if (test_bit(CREATE_BARRIER
, &mdev
->flags
))
145 b
= mdev
->newest_tle
;
146 b
->w
.cb
= w_send_barrier
;
147 /* inc_ap_pending done here, so we won't
148 * get imbalanced on connection loss.
149 * dec_ap_pending will be done in got_BarrierAck
150 * or (on connection loss) in tl_clear. */
151 inc_ap_pending(mdev
);
152 drbd_queue_work(&mdev
->data
.work
, &b
->w
);
153 set_bit(CREATE_BARRIER
, &mdev
->flags
);
156 static void _about_to_complete_local_write(struct drbd_conf
*mdev
,
157 struct drbd_request
*req
)
159 const unsigned long s
= req
->rq_state
;
160 struct drbd_request
*i
;
161 struct drbd_epoch_entry
*e
;
162 struct hlist_node
*n
;
163 struct hlist_head
*slot
;
165 /* before we can signal completion to the upper layers,
166 * we may need to close the current epoch */
167 if (mdev
->state
.conn
>= C_CONNECTED
&&
168 req
->epoch
== mdev
->newest_tle
->br_number
)
171 /* we need to do the conflict detection stuff,
172 * if we have the ee_hash (two_primaries) and
173 * this has been on the network */
174 if ((s
& RQ_NET_DONE
) && mdev
->ee_hash
!= NULL
) {
175 const sector_t sector
= req
->sector
;
176 const int size
= req
->size
;
179 * there must be no conflicting requests, since
180 * they must have been failed on the spot */
181 #define OVERLAPS overlaps(sector, size, i->sector, i->size)
182 slot
= tl_hash_slot(mdev
, sector
);
183 hlist_for_each_entry(i
, n
, slot
, colision
) {
185 dev_alert(DEV
, "LOGIC BUG: completed: %p %llus +%u; "
186 "other: %p %llus +%u\n",
187 req
, (unsigned long long)sector
, size
,
188 i
, (unsigned long long)i
->sector
, i
->size
);
192 /* maybe "wake" those conflicting epoch entries
193 * that wait for this request to finish.
195 * currently, there can be only _one_ such ee
196 * (well, or some more, which would be pending
197 * P_DISCARD_ACK not yet sent by the asender...),
198 * since we block the receiver thread upon the
199 * first conflict detection, which will wait on
200 * misc_wait. maybe we want to assert that?
202 * anyways, if we found one,
203 * we just have to do a wake_up. */
205 #define OVERLAPS overlaps(sector, size, e->sector, e->size)
206 slot
= ee_hash_slot(mdev
, req
->sector
);
207 hlist_for_each_entry(e
, n
, slot
, colision
) {
209 wake_up(&mdev
->misc_wait
);
217 void complete_master_bio(struct drbd_conf
*mdev
,
218 struct bio_and_error
*m
)
220 bio_endio(m
->bio
, m
->error
);
224 /* Helper for __req_mod().
225 * Set m->bio to the master bio, if it is fit to be completed,
226 * or leave it alone (it is initialized to NULL in __req_mod),
227 * if it has already been completed, or cannot be completed yet.
228 * If m->bio is set, the error status to be returned is placed in m->error.
230 void _req_may_be_done(struct drbd_request
*req
, struct bio_and_error
*m
)
232 const unsigned long s
= req
->rq_state
;
233 struct drbd_conf
*mdev
= req
->mdev
;
234 /* only WRITES may end up here without a master bio (on barrier ack) */
235 int rw
= req
->master_bio
? bio_data_dir(req
->master_bio
) : WRITE
;
237 /* we must not complete the master bio, while it is
238 * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
239 * not yet acknowledged by the peer
240 * not yet completed by the local io subsystem
241 * these flags may get cleared in any order by
244 * the bio_endio completion callbacks.
246 if (s
& RQ_NET_QUEUED
)
248 if (s
& RQ_NET_PENDING
)
250 if (s
& RQ_LOCAL_PENDING
)
253 if (req
->master_bio
) {
254 /* this is data_received (remote read)
255 * or protocol C P_WRITE_ACK
256 * or protocol B P_RECV_ACK
257 * or protocol A "handed_over_to_network" (SendAck)
258 * or canceled or failed,
259 * or killed from the transfer log due to connection loss.
263 * figure out whether to report success or failure.
265 * report success when at least one of the operations succeeded.
266 * or, to put the other way,
267 * only report failure, when both operations failed.
269 * what to do about the failures is handled elsewhere.
270 * what we need to do here is just: complete the master_bio.
272 * local completion error, if any, has been stored as ERR_PTR
273 * in private_bio within drbd_endio_pri.
275 int ok
= (s
& RQ_LOCAL_OK
) || (s
& RQ_NET_OK
);
276 int error
= PTR_ERR(req
->private_bio
);
278 /* remove the request from the conflict detection
279 * respective block_id verification hash */
280 if (!hlist_unhashed(&req
->colision
))
281 hlist_del(&req
->colision
);
283 D_ASSERT((s
& RQ_NET_MASK
) == 0);
285 /* for writes we need to do some extra housekeeping */
287 _about_to_complete_local_write(mdev
, req
);
289 /* Update disk stats */
290 _drbd_end_io_acct(mdev
, req
);
292 m
->error
= ok
? 0 : (error
?: -EIO
);
293 m
->bio
= req
->master_bio
;
294 req
->master_bio
= NULL
;
297 if ((s
& RQ_NET_MASK
) == 0 || (s
& RQ_NET_DONE
)) {
298 /* this is disconnected (local only) operation,
299 * or protocol C P_WRITE_ACK,
300 * or protocol A or B P_BARRIER_ACK,
301 * or killed from the transfer log due to connection loss. */
302 _req_is_done(mdev
, req
, rw
);
304 /* else: network part and not DONE yet. that is
305 * protocol A or B, barrier ack still pending... */
309 * checks whether there was an overlapping request
310 * or ee already registered.
312 * if so, return 1, in which case this request is completed on the spot,
313 * without ever being submitted or send.
315 * return 0 if it is ok to submit this request.
318 * paranoia: assume something above us is broken, and issues different write
319 * requests for the same block simultaneously...
321 * To ensure these won't be reordered differently on both nodes, resulting in
322 * diverging data sets, we discard the later one(s). Not that this is supposed
323 * to happen, but this is the rationale why we also have to check for
324 * conflicting requests with local origin, and why we have to do so regardless
325 * of whether we allowed multiple primaries.
327 * BTW, in case we only have one primary, the ee_hash is empty anyways, and the
328 * second hlist_for_each_entry becomes a noop. This is even simpler than to
329 * grab a reference on the net_conf, and check for the two_primaries flag...
331 static int _req_conflicts(struct drbd_request
*req
)
333 struct drbd_conf
*mdev
= req
->mdev
;
334 const sector_t sector
= req
->sector
;
335 const int size
= req
->size
;
336 struct drbd_request
*i
;
337 struct drbd_epoch_entry
*e
;
338 struct hlist_node
*n
;
339 struct hlist_head
*slot
;
341 D_ASSERT(hlist_unhashed(&req
->colision
));
343 if (!get_net_conf(mdev
))
347 ERR_IF (mdev
->tl_hash_s
== 0)
348 goto out_no_conflict
;
349 BUG_ON(mdev
->tl_hash
== NULL
);
351 #define OVERLAPS overlaps(i->sector, i->size, sector, size)
352 slot
= tl_hash_slot(mdev
, sector
);
353 hlist_for_each_entry(i
, n
, slot
, colision
) {
355 dev_alert(DEV
, "%s[%u] Concurrent local write detected! "
356 "[DISCARD L] new: %llus +%u; "
357 "pending: %llus +%u\n",
358 current
->comm
, current
->pid
,
359 (unsigned long long)sector
, size
,
360 (unsigned long long)i
->sector
, i
->size
);
365 if (mdev
->ee_hash_s
) {
366 /* now, check for overlapping requests with remote origin */
367 BUG_ON(mdev
->ee_hash
== NULL
);
369 #define OVERLAPS overlaps(e->sector, e->size, sector, size)
370 slot
= ee_hash_slot(mdev
, sector
);
371 hlist_for_each_entry(e
, n
, slot
, colision
) {
373 dev_alert(DEV
, "%s[%u] Concurrent remote write detected!"
374 " [DISCARD L] new: %llus +%u; "
375 "pending: %llus +%u\n",
376 current
->comm
, current
->pid
,
377 (unsigned long long)sector
, size
,
378 (unsigned long long)e
->sector
, e
->size
);
386 /* this is like it should be, and what we expected.
387 * our users do behave after all... */
396 /* obviously this could be coded as many single functions
397 * instead of one huge switch,
398 * or by putting the code directly in the respective locations
399 * (as it has been before).
401 * but having it this way
402 * enforces that it is all in this one place, where it is easier to audit,
403 * it makes it obvious that whatever "event" "happens" to a request should
404 * happen "atomically" within the req_lock,
405 * and it enforces that we have to think in a very structured manner
406 * about the "events" that may happen to a request during its life time ...
408 void __req_mod(struct drbd_request
*req
, enum drbd_req_event what
,
409 struct bio_and_error
*m
)
411 struct drbd_conf
*mdev
= req
->mdev
;
416 dev_err(DEV
, "LOGIC BUG in %s:%u\n", __FILE__
, __LINE__
);
419 /* does not happen...
420 * initialization done in drbd_req_new
425 case to_be_send
: /* via network */
426 /* reached via drbd_make_request_common
427 * and from w_read_retry_remote */
428 D_ASSERT(!(req
->rq_state
& RQ_NET_MASK
));
429 req
->rq_state
|= RQ_NET_PENDING
;
430 inc_ap_pending(mdev
);
433 case to_be_submitted
: /* locally */
434 /* reached via drbd_make_request_common */
435 D_ASSERT(!(req
->rq_state
& RQ_LOCAL_MASK
));
436 req
->rq_state
|= RQ_LOCAL_PENDING
;
440 if (bio_data_dir(req
->master_bio
) == WRITE
)
441 mdev
->writ_cnt
+= req
->size
>>9;
443 mdev
->read_cnt
+= req
->size
>>9;
445 req
->rq_state
|= (RQ_LOCAL_COMPLETED
|RQ_LOCAL_OK
);
446 req
->rq_state
&= ~RQ_LOCAL_PENDING
;
448 _req_may_be_done(req
, m
);
452 case write_completed_with_error
:
453 req
->rq_state
|= RQ_LOCAL_COMPLETED
;
454 req
->rq_state
&= ~RQ_LOCAL_PENDING
;
456 dev_alert(DEV
, "Local WRITE failed sec=%llus size=%u\n",
457 (unsigned long long)req
->sector
, req
->size
);
458 /* and now: check how to handle local io error. */
459 __drbd_chk_io_error(mdev
, FALSE
);
460 _req_may_be_done(req
, m
);
464 case read_ahead_completed_with_error
:
465 /* it is legal to fail READA */
466 req
->rq_state
|= RQ_LOCAL_COMPLETED
;
467 req
->rq_state
&= ~RQ_LOCAL_PENDING
;
468 _req_may_be_done(req
, m
);
472 case read_completed_with_error
:
473 drbd_set_out_of_sync(mdev
, req
->sector
, req
->size
);
475 req
->rq_state
|= RQ_LOCAL_COMPLETED
;
476 req
->rq_state
&= ~RQ_LOCAL_PENDING
;
478 dev_alert(DEV
, "Local READ failed sec=%llus size=%u\n",
479 (unsigned long long)req
->sector
, req
->size
);
480 /* _req_mod(req,to_be_send); oops, recursion... */
481 D_ASSERT(!(req
->rq_state
& RQ_NET_MASK
));
482 req
->rq_state
|= RQ_NET_PENDING
;
483 inc_ap_pending(mdev
);
485 __drbd_chk_io_error(mdev
, FALSE
);
487 /* NOTE: if we have no connection,
488 * or know the peer has no good data either,
489 * then we don't actually need to "queue_for_net_read",
490 * but we do so anyways, since the drbd_io_error()
491 * and the potential state change to "Diskless"
492 * needs to be done from process context */
494 /* fall through: _req_mod(req,queue_for_net_read); */
496 case queue_for_net_read
:
497 /* READ or READA, and
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 */
504 /* so we can verify the handle in the answer packet
505 * corresponding hlist_del is in _req_may_be_done() */
506 hlist_add_head(&req
->colision
, ar_hash_slot(mdev
, req
->sector
));
508 set_bit(UNPLUG_REMOTE
, &mdev
->flags
);
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
515 drbd_queue_work(&mdev
->data
.work
, &req
->w
);
518 case queue_for_net_write
:
519 /* assert something? */
520 /* from drbd_make_request_common only */
522 hlist_add_head(&req
->colision
, tl_hash_slot(mdev
, req
->sector
));
523 /* corresponding hlist_del is in _req_may_be_done() */
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.
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.
537 * Add req to the (now) current epoch (barrier). */
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
);
544 /* see drbd_make_request_common,
545 * just after it grabs the req_lock */
546 D_ASSERT(test_bit(CREATE_BARRIER
, &mdev
->flags
) == 0);
548 req
->epoch
= mdev
->newest_tle
->br_number
;
549 list_add_tail(&req
->tl_requests
,
550 &mdev
->newest_tle
->requests
);
552 /* increment size of current epoch */
553 mdev
->newest_tle
->n_req
++;
555 /* queue work item to send data */
556 D_ASSERT(req
->rq_state
& RQ_NET_PENDING
);
557 req
->rq_state
|= RQ_NET_QUEUED
;
558 req
->w
.cb
= w_send_dblock
;
559 drbd_queue_work(&mdev
->data
.work
, &req
->w
);
561 /* close the epoch, in case it outgrew the limit */
562 if (mdev
->newest_tle
->n_req
>= mdev
->net_conf
->max_epoch_size
)
568 /* treat it the same */
570 /* real cleanup will be done from tl_clear. just update flags
571 * so it is no longer marked as on the worker queue */
572 req
->rq_state
&= ~RQ_NET_QUEUED
;
573 /* if we did it right, tl_clear should be scheduled only after
574 * this, so this should not be necessary! */
575 _req_may_be_done(req
, m
);
578 case handed_over_to_network
:
579 /* assert something? */
580 if (bio_data_dir(req
->master_bio
) == WRITE
&&
581 mdev
->net_conf
->wire_protocol
== DRBD_PROT_A
) {
582 /* this is what is dangerous about protocol A:
583 * pretend it was successfully written on the peer. */
584 if (req
->rq_state
& RQ_NET_PENDING
) {
585 dec_ap_pending(mdev
);
586 req
->rq_state
&= ~RQ_NET_PENDING
;
587 req
->rq_state
|= RQ_NET_OK
;
588 } /* else: neg-ack was faster... */
589 /* it is still not yet RQ_NET_DONE until the
590 * corresponding epoch barrier got acked as well,
591 * so we know what to dirty on connection loss */
593 req
->rq_state
&= ~RQ_NET_QUEUED
;
594 req
->rq_state
|= RQ_NET_SENT
;
595 /* because _drbd_send_zc_bio could sleep, and may want to
596 * dereference the bio even after the "write_acked_by_peer" and
597 * "completed_ok" events came in, once we return from
598 * _drbd_send_zc_bio (drbd_send_dblock), we have to check
599 * whether it is done already, and end it. */
600 _req_may_be_done(req
, m
);
603 case connection_lost_while_pending
:
604 /* transfer log cleanup after connection loss */
605 /* assert something? */
606 if (req
->rq_state
& RQ_NET_PENDING
)
607 dec_ap_pending(mdev
);
608 req
->rq_state
&= ~(RQ_NET_OK
|RQ_NET_PENDING
);
609 req
->rq_state
|= RQ_NET_DONE
;
610 /* if it is still queued, we may not complete it here.
611 * it will be canceled soon. */
612 if (!(req
->rq_state
& RQ_NET_QUEUED
))
613 _req_may_be_done(req
, m
);
616 case write_acked_by_peer_and_sis
:
617 req
->rq_state
|= RQ_NET_SIS
;
618 case conflict_discarded_by_peer
:
619 /* for discarded conflicting writes of multiple primaries,
620 * there is no need to keep anything in the tl, potential
621 * node crashes are covered by the activity log. */
622 if (what
== conflict_discarded_by_peer
)
623 dev_alert(DEV
, "Got DiscardAck packet %llus +%u!"
624 " DRBD is not a random data generator!\n",
625 (unsigned long long)req
->sector
, req
->size
);
626 req
->rq_state
|= RQ_NET_DONE
;
628 case write_acked_by_peer
:
629 /* protocol C; successfully written on peer.
630 * Nothing to do here.
631 * We want to keep the tl in place for all protocols, to cater
632 * for volatile write-back caches on lower level devices.
634 * A barrier request is expected to have forced all prior
635 * requests onto stable storage, so completion of a barrier
636 * request could set NET_DONE right here, and not wait for the
637 * P_BARRIER_ACK, but that is an unnecessary optimization. */
639 /* this makes it effectively the same as for: */
640 case recv_acked_by_peer
:
641 /* protocol B; pretends to be successfully written on peer.
642 * see also notes above in handed_over_to_network about
644 req
->rq_state
|= RQ_NET_OK
;
645 D_ASSERT(req
->rq_state
& RQ_NET_PENDING
);
646 dec_ap_pending(mdev
);
647 req
->rq_state
&= ~RQ_NET_PENDING
;
648 _req_may_be_done(req
, m
);
652 /* assert something? */
653 if (req
->rq_state
& RQ_NET_PENDING
)
654 dec_ap_pending(mdev
);
655 req
->rq_state
&= ~(RQ_NET_OK
|RQ_NET_PENDING
);
657 req
->rq_state
|= RQ_NET_DONE
;
658 _req_may_be_done(req
, m
);
659 /* else: done by handed_over_to_network */
663 if (req
->rq_state
& RQ_NET_PENDING
) {
664 /* barrier came in before all requests have been acked.
665 * this is bad, because if the connection is lost now,
666 * we won't be able to clean them up... */
667 dev_err(DEV
, "FIXME (barrier_acked but pending)\n");
668 list_move(&req
->tl_requests
, &mdev
->out_of_sequence_requests
);
670 D_ASSERT(req
->rq_state
& RQ_NET_SENT
);
671 req
->rq_state
|= RQ_NET_DONE
;
672 _req_may_be_done(req
, m
);
676 D_ASSERT(req
->rq_state
& RQ_NET_PENDING
);
677 dec_ap_pending(mdev
);
678 req
->rq_state
&= ~RQ_NET_PENDING
;
679 req
->rq_state
|= (RQ_NET_OK
|RQ_NET_DONE
);
680 _req_may_be_done(req
, m
);
685 /* we may do a local read if:
686 * - we are consistent (of course),
687 * - or we are generally inconsistent,
688 * BUT we are still/already IN SYNC for this area.
689 * since size may be bigger than BM_BLOCK_SIZE,
690 * we may need to check several bits.
692 static int drbd_may_do_local_read(struct drbd_conf
*mdev
, sector_t sector
, int size
)
694 unsigned long sbnr
, ebnr
;
695 sector_t esector
, nr_sectors
;
697 if (mdev
->state
.disk
== D_UP_TO_DATE
)
699 if (mdev
->state
.disk
>= D_OUTDATED
)
701 if (mdev
->state
.disk
< D_INCONSISTENT
)
703 /* state.disk == D_INCONSISTENT We will have a look at the BitMap */
704 nr_sectors
= drbd_get_capacity(mdev
->this_bdev
);
705 esector
= sector
+ (size
>> 9) - 1;
707 D_ASSERT(sector
< nr_sectors
);
708 D_ASSERT(esector
< nr_sectors
);
710 sbnr
= BM_SECT_TO_BIT(sector
);
711 ebnr
= BM_SECT_TO_BIT(esector
);
713 return 0 == drbd_bm_count_bits(mdev
, sbnr
, ebnr
);
716 static int drbd_make_request_common(struct drbd_conf
*mdev
, struct bio
*bio
)
718 const int rw
= bio_rw(bio
);
719 const int size
= bio
->bi_size
;
720 const sector_t sector
= bio
->bi_sector
;
721 struct drbd_tl_epoch
*b
= NULL
;
722 struct drbd_request
*req
;
726 /* allocate outside of all locks; */
727 req
= drbd_req_new(mdev
, bio
);
730 /* only pass the error to the upper layers.
731 * if user cannot handle io errors, that's not our business. */
732 dev_err(DEV
, "could not kmalloc() req\n");
733 bio_endio(bio
, -ENOMEM
);
737 local
= get_ldev(mdev
);
739 bio_put(req
->private_bio
); /* or we get a bio leak */
740 req
->private_bio
= NULL
;
747 if (!drbd_may_do_local_read(mdev
, sector
, size
)) {
748 /* we could kick the syncer to
749 * sync this extent asap, wait for
750 * it, then continue locally.
751 * Or just issue the request remotely.
754 bio_put(req
->private_bio
);
755 req
->private_bio
= NULL
;
759 remote
= !local
&& mdev
->state
.pdsk
>= D_UP_TO_DATE
;
762 /* If we have a disk, but a READA request is mapped to remote,
763 * we are R_PRIMARY, D_INCONSISTENT, SyncTarget.
764 * Just fail that READA request right here.
766 * THINK: maybe fail all READA when not local?
767 * or make this configurable...
768 * if network is slow, READA won't do any good.
770 if (rw
== READA
&& mdev
->state
.disk
>= D_INCONSISTENT
&& !local
) {
772 goto fail_and_free_req
;
775 /* For WRITES going to the local disk, grab a reference on the target
776 * extent. This waits for any resync activity in the corresponding
777 * resync extent to finish, and, if necessary, pulls in the target
778 * extent into the activity log, which involves further disk io because
779 * of transactional on-disk meta data updates. */
780 if (rw
== WRITE
&& local
)
781 drbd_al_begin_io(mdev
, sector
);
783 remote
= remote
&& (mdev
->state
.pdsk
== D_UP_TO_DATE
||
784 (mdev
->state
.pdsk
== D_INCONSISTENT
&&
785 mdev
->state
.conn
>= C_CONNECTED
));
787 if (!(local
|| remote
)) {
788 dev_err(DEV
, "IO ERROR: neither local nor remote disk\n");
789 goto fail_free_complete
;
792 /* For WRITE request, we have to make sure that we have an
793 * unused_spare_tle, in case we need to start a new epoch.
794 * I try to be smart and avoid to pre-allocate always "just in case",
795 * but there is a race between testing the bit and pointer outside the
796 * spinlock, and grabbing the spinlock.
797 * if we lost that race, we retry. */
798 if (rw
== WRITE
&& remote
&&
799 mdev
->unused_spare_tle
== NULL
&&
800 test_bit(CREATE_BARRIER
, &mdev
->flags
)) {
802 b
= kmalloc(sizeof(struct drbd_tl_epoch
), GFP_NOIO
);
804 dev_err(DEV
, "Failed to alloc barrier.\n");
806 goto fail_free_complete
;
810 /* GOOD, everything prepared, grab the spin_lock */
811 spin_lock_irq(&mdev
->req_lock
);
814 remote
= (mdev
->state
.pdsk
== D_UP_TO_DATE
||
815 (mdev
->state
.pdsk
== D_INCONSISTENT
&&
816 mdev
->state
.conn
>= C_CONNECTED
));
818 dev_warn(DEV
, "lost connection while grabbing the req_lock!\n");
819 if (!(local
|| remote
)) {
820 dev_err(DEV
, "IO ERROR: neither local nor remote disk\n");
821 spin_unlock_irq(&mdev
->req_lock
);
822 goto fail_free_complete
;
826 if (b
&& mdev
->unused_spare_tle
== NULL
) {
827 mdev
->unused_spare_tle
= b
;
830 if (rw
== WRITE
&& remote
&&
831 mdev
->unused_spare_tle
== NULL
&&
832 test_bit(CREATE_BARRIER
, &mdev
->flags
)) {
833 /* someone closed the current epoch
834 * while we were grabbing the spinlock */
835 spin_unlock_irq(&mdev
->req_lock
);
836 goto allocate_barrier
;
840 /* Update disk stats */
841 _drbd_start_io_acct(mdev
, req
, bio
);
843 /* _maybe_start_new_epoch(mdev);
844 * If we need to generate a write barrier packet, we have to add the
845 * new epoch (barrier) object, and queue the barrier packet for sending,
846 * and queue the req's data after it _within the same lock_, otherwise
847 * we have race conditions were the reorder domains could be mixed up.
849 * Even read requests may start a new epoch and queue the corresponding
850 * barrier packet. To get the write ordering right, we only have to
851 * make sure that, if this is a write request and it triggered a
852 * barrier packet, this request is queued within the same spinlock. */
853 if (remote
&& mdev
->unused_spare_tle
&&
854 test_and_clear_bit(CREATE_BARRIER
, &mdev
->flags
)) {
855 _tl_add_barrier(mdev
, mdev
->unused_spare_tle
);
856 mdev
->unused_spare_tle
= NULL
;
858 D_ASSERT(!(remote
&& rw
== WRITE
&&
859 test_bit(CREATE_BARRIER
, &mdev
->flags
)));
863 * Actually, 'local' may be wrong here already, since we may have failed
864 * to write to the meta data, and may become wrong anytime because of
865 * local io-error for some other request, which would lead to us
866 * "detaching" the local disk.
868 * 'remote' may become wrong any time because the network could fail.
870 * This is a harmless race condition, though, since it is handled
871 * correctly at the appropriate places; so it just defers the failure
872 * of the respective operation.
875 /* mark them early for readability.
876 * this just sets some state flags. */
878 _req_mod(req
, to_be_send
);
880 _req_mod(req
, to_be_submitted
);
882 /* check this request on the collision detection hash tables.
883 * if we have a conflict, just complete it here.
884 * THINK do we want to check reads, too? (I don't think so...) */
885 if (rw
== WRITE
&& _req_conflicts(req
)) {
886 /* this is a conflicting request.
887 * even though it may have been only _partially_
888 * overlapping with one of the currently pending requests,
889 * without even submitting or sending it, we will
890 * pretend that it was successfully served right now.
893 bio_put(req
->private_bio
);
894 req
->private_bio
= NULL
;
895 drbd_al_complete_io(mdev
, req
->sector
);
900 dec_ap_pending(mdev
);
901 _drbd_end_io_acct(mdev
, req
);
902 /* THINK: do we want to fail it (-EIO), or pretend success? */
903 bio_endio(req
->master_bio
, 0);
904 req
->master_bio
= NULL
;
910 /* NOTE remote first: to get the concurrent write detection right,
911 * we must register the request before start of local IO. */
913 /* either WRITE and C_CONNECTED,
914 * or READ, and no local disk,
915 * or READ, but not in sync.
917 _req_mod(req
, (rw
== WRITE
)
918 ? queue_for_net_write
919 : queue_for_net_read
);
921 spin_unlock_irq(&mdev
->req_lock
);
922 kfree(b
); /* if someone else has beaten us to it... */
925 req
->private_bio
->bi_bdev
= mdev
->ldev
->backing_bdev
;
927 if (FAULT_ACTIVE(mdev
, rw
== WRITE
? DRBD_FAULT_DT_WR
928 : rw
== READ
? DRBD_FAULT_DT_RD
930 bio_endio(req
->private_bio
, -EIO
);
932 generic_make_request(req
->private_bio
);
935 /* we need to plug ALWAYS since we possibly need to kick lo_dev.
936 * we plug after submit, so we won't miss an unplug event */
937 drbd_plug_device(mdev
);
942 if (rw
== WRITE
&& local
)
943 drbd_al_complete_io(mdev
, sector
);
946 bio_put(req
->private_bio
);
947 req
->private_bio
= NULL
;
958 /* helper function for drbd_make_request
959 * if we can determine just by the mdev (state) that this request will fail,
963 static int drbd_fail_request_early(struct drbd_conf
*mdev
, int is_write
)
966 if (mdev
->state
.conn
== C_DISCONNECTING
&&
967 mdev
->state
.disk
== D_DISKLESS
)
970 if (mdev
->state
.role
!= R_PRIMARY
&&
971 (!allow_oos
|| is_write
)) {
972 if (__ratelimit(&drbd_ratelimit_state
)) {
973 dev_err(DEV
, "Process %s[%u] tried to %s; "
974 "since we are not in Primary state, "
975 "we cannot allow this\n",
976 current
->comm
, current
->pid
,
977 is_write
? "WRITE" : "READ");
983 * Paranoia: we might have been primary, but sync target, or
984 * even diskless, then lost the connection.
985 * This should have been handled (panic? suspend?) somewhere
986 * else. But maybe it was not, so check again here.
987 * Caution: as long as we do not have a read/write lock on mdev,
988 * to serialize state changes, this is racy, since we may lose
989 * the connection *after* we test for the cstate.
991 if (mdev
->state
.disk
< D_UP_TO_DATE
&& mdev
->state
.pdsk
< D_UP_TO_DATE
) {
992 if (__ratelimit(&drbd_ratelimit_state
))
993 dev_err(DEV
, "Sorry, I have no access to good data anymore.\n");
1000 int drbd_make_request_26(struct request_queue
*q
, struct bio
*bio
)
1002 unsigned int s_enr
, e_enr
;
1003 struct drbd_conf
*mdev
= (struct drbd_conf
*) q
->queuedata
;
1005 if (drbd_fail_request_early(mdev
, bio_data_dir(bio
) & WRITE
)) {
1006 bio_endio(bio
, -EPERM
);
1010 /* Reject barrier requests if we know the underlying device does
1012 * XXX: Need to get this info from peer as well some how so we
1013 * XXX: reject if EITHER side/data/metadata area does not support them.
1015 * because of those XXX, this is not yet enabled,
1016 * i.e. in drbd_init_set_defaults we set the NO_BARRIER_SUPP bit.
1018 if (unlikely(bio_rw_flagged(bio
, BIO_RW_BARRIER
) && test_bit(NO_BARRIER_SUPP
, &mdev
->flags
))) {
1019 /* dev_warn(DEV, "Rejecting barrier request as underlying device does not support\n"); */
1020 bio_endio(bio
, -EOPNOTSUPP
);
1025 * what we "blindly" assume:
1027 D_ASSERT(bio
->bi_size
> 0);
1028 D_ASSERT((bio
->bi_size
& 0x1ff) == 0);
1029 D_ASSERT(bio
->bi_idx
== 0);
1031 /* to make some things easier, force alignment of requests within the
1032 * granularity of our hash tables */
1033 s_enr
= bio
->bi_sector
>> HT_SHIFT
;
1034 e_enr
= (bio
->bi_sector
+(bio
->bi_size
>>9)-1) >> HT_SHIFT
;
1036 if (likely(s_enr
== e_enr
)) {
1037 inc_ap_bio(mdev
, 1);
1038 return drbd_make_request_common(mdev
, bio
);
1041 /* can this bio be split generically?
1042 * Maybe add our own split-arbitrary-bios function. */
1043 if (bio
->bi_vcnt
!= 1 || bio
->bi_idx
!= 0 || bio
->bi_size
> DRBD_MAX_SEGMENT_SIZE
) {
1044 /* rather error out here than BUG in bio_split */
1045 dev_err(DEV
, "bio would need to, but cannot, be split: "
1046 "(vcnt=%u,idx=%u,size=%u,sector=%llu)\n",
1047 bio
->bi_vcnt
, bio
->bi_idx
, bio
->bi_size
,
1048 (unsigned long long)bio
->bi_sector
);
1049 bio_endio(bio
, -EINVAL
);
1051 /* This bio crosses some boundary, so we have to split it. */
1052 struct bio_pair
*bp
;
1053 /* works for the "do not cross hash slot boundaries" case
1054 * e.g. sector 262269, size 4096
1055 * s_enr = 262269 >> 6 = 4097
1056 * e_enr = (262269+8-1) >> 6 = 4098
1058 * sps = 64, mask = 63
1059 * first_sectors = 64 - (262269 & 63) = 3
1061 const sector_t sect
= bio
->bi_sector
;
1062 const int sps
= 1 << HT_SHIFT
; /* sectors per slot */
1063 const int mask
= sps
- 1;
1064 const sector_t first_sectors
= sps
- (sect
& mask
);
1066 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
1071 /* we need to get a "reference count" (ap_bio_cnt)
1072 * to avoid races with the disconnect/reconnect/suspend code.
1073 * In case we need to split the bio here, we need to get two references
1074 * atomically, otherwise we might deadlock when trying to submit the
1076 inc_ap_bio(mdev
, 2);
1078 D_ASSERT(e_enr
== s_enr
+ 1);
1080 drbd_make_request_common(mdev
, &bp
->bio1
);
1081 drbd_make_request_common(mdev
, &bp
->bio2
);
1082 bio_pair_release(bp
);
1087 /* This is called by bio_add_page(). With this function we reduce
1088 * the number of BIOs that span over multiple DRBD_MAX_SEGMENT_SIZEs
1089 * units (was AL_EXTENTs).
1091 * we do the calculation within the lower 32bit of the byte offsets,
1092 * since we don't care for actual offset, but only check whether it
1093 * would cross "activity log extent" boundaries.
1095 * As long as the BIO is empty we have to allow at least one bvec,
1096 * regardless of size and offset. so the resulting bio may still
1097 * cross extent boundaries. those are dealt with (bio_split) in
1098 * drbd_make_request_26.
1100 int drbd_merge_bvec(struct request_queue
*q
, struct bvec_merge_data
*bvm
, struct bio_vec
*bvec
)
1102 struct drbd_conf
*mdev
= (struct drbd_conf
*) q
->queuedata
;
1103 unsigned int bio_offset
=
1104 (unsigned int)bvm
->bi_sector
<< 9; /* 32 bit */
1105 unsigned int bio_size
= bvm
->bi_size
;
1106 int limit
, backing_limit
;
1108 limit
= DRBD_MAX_SEGMENT_SIZE
1109 - ((bio_offset
& (DRBD_MAX_SEGMENT_SIZE
-1)) + bio_size
);
1112 if (bio_size
== 0) {
1113 if (limit
<= bvec
->bv_len
)
1114 limit
= bvec
->bv_len
;
1115 } else if (limit
&& get_ldev(mdev
)) {
1116 struct request_queue
* const b
=
1117 mdev
->ldev
->backing_bdev
->bd_disk
->queue
;
1118 if (b
->merge_bvec_fn
&& mdev
->ldev
->dc
.use_bmbv
) {
1119 backing_limit
= b
->merge_bvec_fn(b
, bvm
, bvec
);
1120 limit
= min(limit
, backing_limit
);