blob: e6c4d579eaba0146f0dc06be5aeb7b814f7c4eb2 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_req.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 */
25
Philipp Reisnerb411b362009-09-25 16:07:19 -070026#include <linux/module.h>
27
28#include <linux/slab.h>
29#include <linux/drbd.h>
30#include "drbd_int.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070031#include "drbd_req.h"
32
33
34/* Update disk stats at start of I/O request */
35static void _drbd_start_io_acct(struct drbd_conf *mdev, struct drbd_request *req, struct bio *bio)
36{
37 const int rw = bio_data_dir(bio);
38 int cpu;
39 cpu = part_stat_lock();
40 part_stat_inc(cpu, &mdev->vdisk->part0, ios[rw]);
41 part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], bio_sectors(bio));
Philipp Reisner753c8912009-11-18 15:52:51 +010042 part_inc_in_flight(&mdev->vdisk->part0, rw);
Philipp Reisnerb411b362009-09-25 16:07:19 -070043 part_stat_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -070044}
45
46/* Update disk stats when completing request upwards */
47static void _drbd_end_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
48{
49 int rw = bio_data_dir(req->master_bio);
50 unsigned long duration = jiffies - req->start_time;
51 int cpu;
52 cpu = part_stat_lock();
53 part_stat_add(cpu, &mdev->vdisk->part0, ticks[rw], duration);
54 part_round_stats(cpu, &mdev->vdisk->part0);
Philipp Reisner753c8912009-11-18 15:52:51 +010055 part_dec_in_flight(&mdev->vdisk->part0, rw);
Philipp Reisnerb411b362009-09-25 16:07:19 -070056 part_stat_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -070057}
58
59static void _req_is_done(struct drbd_conf *mdev, struct drbd_request *req, const int rw)
60{
61 const unsigned long s = req->rq_state;
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. */
65 if (rw == WRITE) {
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);
79
80 if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
81 drbd_set_in_sync(mdev, req->sector, req->size);
82
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.
92 */
93 if (s & RQ_LOCAL_MASK) {
94 if (get_ldev_if_state(mdev, D_FAILED)) {
95 drbd_al_complete_io(mdev, req->sector);
96 put_ldev(mdev);
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);
101 }
102 }
103 }
104
Philipp Reisner32fa7e92010-05-26 17:13:18 +0200105 drbd_req_free(req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700106}
107
108static void queue_barrier(struct drbd_conf *mdev)
109{
110 struct drbd_tl_epoch *b;
111
112 /* We are within the req_lock. Once we queued the barrier for sending,
113 * we set the CREATE_BARRIER bit. It is cleared as soon as a new
114 * barrier/epoch object is added. This is the only place this bit is
115 * set. It indicates that the barrier for this epoch is already queued,
116 * and no new epoch has been created yet. */
117 if (test_bit(CREATE_BARRIER, &mdev->flags))
118 return;
119
120 b = mdev->newest_tle;
121 b->w.cb = w_send_barrier;
122 /* inc_ap_pending done here, so we won't
123 * get imbalanced on connection loss.
124 * dec_ap_pending will be done in got_BarrierAck
125 * or (on connection loss) in tl_clear. */
126 inc_ap_pending(mdev);
127 drbd_queue_work(&mdev->data.work, &b->w);
128 set_bit(CREATE_BARRIER, &mdev->flags);
129}
130
131static void _about_to_complete_local_write(struct drbd_conf *mdev,
132 struct drbd_request *req)
133{
134 const unsigned long s = req->rq_state;
135 struct drbd_request *i;
136 struct drbd_epoch_entry *e;
137 struct hlist_node *n;
138 struct hlist_head *slot;
139
140 /* before we can signal completion to the upper layers,
141 * we may need to close the current epoch */
142 if (mdev->state.conn >= C_CONNECTED &&
143 req->epoch == mdev->newest_tle->br_number)
144 queue_barrier(mdev);
145
146 /* we need to do the conflict detection stuff,
147 * if we have the ee_hash (two_primaries) and
148 * this has been on the network */
149 if ((s & RQ_NET_DONE) && mdev->ee_hash != NULL) {
150 const sector_t sector = req->sector;
151 const int size = req->size;
152
153 /* ASSERT:
154 * there must be no conflicting requests, since
155 * they must have been failed on the spot */
156#define OVERLAPS overlaps(sector, size, i->sector, i->size)
157 slot = tl_hash_slot(mdev, sector);
158 hlist_for_each_entry(i, n, slot, colision) {
159 if (OVERLAPS) {
160 dev_alert(DEV, "LOGIC BUG: completed: %p %llus +%u; "
161 "other: %p %llus +%u\n",
162 req, (unsigned long long)sector, size,
163 i, (unsigned long long)i->sector, i->size);
164 }
165 }
166
167 /* maybe "wake" those conflicting epoch entries
168 * that wait for this request to finish.
169 *
170 * currently, there can be only _one_ such ee
171 * (well, or some more, which would be pending
172 * P_DISCARD_ACK not yet sent by the asender...),
173 * since we block the receiver thread upon the
174 * first conflict detection, which will wait on
175 * misc_wait. maybe we want to assert that?
176 *
177 * anyways, if we found one,
178 * we just have to do a wake_up. */
179#undef OVERLAPS
180#define OVERLAPS overlaps(sector, size, e->sector, e->size)
181 slot = ee_hash_slot(mdev, req->sector);
182 hlist_for_each_entry(e, n, slot, colision) {
183 if (OVERLAPS) {
184 wake_up(&mdev->misc_wait);
185 break;
186 }
187 }
188 }
189#undef OVERLAPS
190}
191
192void complete_master_bio(struct drbd_conf *mdev,
193 struct bio_and_error *m)
194{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700195 bio_endio(m->bio, m->error);
196 dec_ap_bio(mdev);
197}
198
199/* Helper for __req_mod().
200 * Set m->bio to the master bio, if it is fit to be completed,
201 * or leave it alone (it is initialized to NULL in __req_mod),
202 * if it has already been completed, or cannot be completed yet.
203 * If m->bio is set, the error status to be returned is placed in m->error.
204 */
205void _req_may_be_done(struct drbd_request *req, struct bio_and_error *m)
206{
207 const unsigned long s = req->rq_state;
208 struct drbd_conf *mdev = req->mdev;
209 /* only WRITES may end up here without a master bio (on barrier ack) */
210 int rw = req->master_bio ? bio_data_dir(req->master_bio) : WRITE;
211
Philipp Reisnerb411b362009-09-25 16:07:19 -0700212 /* we must not complete the master bio, while it is
213 * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
214 * not yet acknowledged by the peer
215 * not yet completed by the local io subsystem
216 * these flags may get cleared in any order by
217 * the worker,
218 * the receiver,
219 * the bio_endio completion callbacks.
220 */
221 if (s & RQ_NET_QUEUED)
222 return;
223 if (s & RQ_NET_PENDING)
224 return;
225 if (s & RQ_LOCAL_PENDING)
226 return;
227
228 if (req->master_bio) {
229 /* this is data_received (remote read)
230 * or protocol C P_WRITE_ACK
231 * or protocol B P_RECV_ACK
232 * or protocol A "handed_over_to_network" (SendAck)
233 * or canceled or failed,
234 * or killed from the transfer log due to connection loss.
235 */
236
237 /*
238 * figure out whether to report success or failure.
239 *
240 * report success when at least one of the operations succeeded.
241 * or, to put the other way,
242 * only report failure, when both operations failed.
243 *
244 * what to do about the failures is handled elsewhere.
245 * what we need to do here is just: complete the master_bio.
246 *
247 * local completion error, if any, has been stored as ERR_PTR
248 * in private_bio within drbd_endio_pri.
249 */
250 int ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
251 int error = PTR_ERR(req->private_bio);
252
253 /* remove the request from the conflict detection
254 * respective block_id verification hash */
255 if (!hlist_unhashed(&req->colision))
256 hlist_del(&req->colision);
257 else
258 D_ASSERT((s & RQ_NET_MASK) == 0);
259
260 /* for writes we need to do some extra housekeeping */
261 if (rw == WRITE)
262 _about_to_complete_local_write(mdev, req);
263
264 /* Update disk stats */
265 _drbd_end_io_acct(mdev, req);
266
267 m->error = ok ? 0 : (error ?: -EIO);
268 m->bio = req->master_bio;
269 req->master_bio = NULL;
270 }
271
272 if ((s & RQ_NET_MASK) == 0 || (s & RQ_NET_DONE)) {
273 /* this is disconnected (local only) operation,
274 * or protocol C P_WRITE_ACK,
275 * or protocol A or B P_BARRIER_ACK,
276 * or killed from the transfer log due to connection loss. */
277 _req_is_done(mdev, req, rw);
278 }
279 /* else: network part and not DONE yet. that is
280 * protocol A or B, barrier ack still pending... */
281}
282
283/*
284 * checks whether there was an overlapping request
285 * or ee already registered.
286 *
287 * if so, return 1, in which case this request is completed on the spot,
288 * without ever being submitted or send.
289 *
290 * return 0 if it is ok to submit this request.
291 *
292 * NOTE:
293 * paranoia: assume something above us is broken, and issues different write
294 * requests for the same block simultaneously...
295 *
296 * To ensure these won't be reordered differently on both nodes, resulting in
297 * diverging data sets, we discard the later one(s). Not that this is supposed
298 * to happen, but this is the rationale why we also have to check for
299 * conflicting requests with local origin, and why we have to do so regardless
300 * of whether we allowed multiple primaries.
301 *
302 * BTW, in case we only have one primary, the ee_hash is empty anyways, and the
303 * second hlist_for_each_entry becomes a noop. This is even simpler than to
304 * grab a reference on the net_conf, and check for the two_primaries flag...
305 */
306static int _req_conflicts(struct drbd_request *req)
307{
308 struct drbd_conf *mdev = req->mdev;
309 const sector_t sector = req->sector;
310 const int size = req->size;
311 struct drbd_request *i;
312 struct drbd_epoch_entry *e;
313 struct hlist_node *n;
314 struct hlist_head *slot;
315
316 D_ASSERT(hlist_unhashed(&req->colision));
317
318 if (!get_net_conf(mdev))
319 return 0;
320
321 /* BUG_ON */
322 ERR_IF (mdev->tl_hash_s == 0)
323 goto out_no_conflict;
324 BUG_ON(mdev->tl_hash == NULL);
325
326#define OVERLAPS overlaps(i->sector, i->size, sector, size)
327 slot = tl_hash_slot(mdev, sector);
328 hlist_for_each_entry(i, n, slot, colision) {
329 if (OVERLAPS) {
330 dev_alert(DEV, "%s[%u] Concurrent local write detected! "
331 "[DISCARD L] new: %llus +%u; "
332 "pending: %llus +%u\n",
333 current->comm, current->pid,
334 (unsigned long long)sector, size,
335 (unsigned long long)i->sector, i->size);
336 goto out_conflict;
337 }
338 }
339
340 if (mdev->ee_hash_s) {
341 /* now, check for overlapping requests with remote origin */
342 BUG_ON(mdev->ee_hash == NULL);
343#undef OVERLAPS
344#define OVERLAPS overlaps(e->sector, e->size, sector, size)
345 slot = ee_hash_slot(mdev, sector);
346 hlist_for_each_entry(e, n, slot, colision) {
347 if (OVERLAPS) {
348 dev_alert(DEV, "%s[%u] Concurrent remote write detected!"
349 " [DISCARD L] new: %llus +%u; "
350 "pending: %llus +%u\n",
351 current->comm, current->pid,
352 (unsigned long long)sector, size,
353 (unsigned long long)e->sector, e->size);
354 goto out_conflict;
355 }
356 }
357 }
358#undef OVERLAPS
359
360out_no_conflict:
361 /* this is like it should be, and what we expected.
362 * our users do behave after all... */
363 put_net_conf(mdev);
364 return 0;
365
366out_conflict:
367 put_net_conf(mdev);
368 return 1;
369}
370
371/* obviously this could be coded as many single functions
372 * instead of one huge switch,
373 * or by putting the code directly in the respective locations
374 * (as it has been before).
375 *
376 * but having it this way
377 * enforces that it is all in this one place, where it is easier to audit,
378 * it makes it obvious that whatever "event" "happens" to a request should
379 * happen "atomically" within the req_lock,
380 * and it enforces that we have to think in a very structured manner
381 * about the "events" that may happen to a request during its life time ...
382 */
383void __req_mod(struct drbd_request *req, enum drbd_req_event what,
384 struct bio_and_error *m)
385{
386 struct drbd_conf *mdev = req->mdev;
387 m->bio = NULL;
388
Philipp Reisnerb411b362009-09-25 16:07:19 -0700389 switch (what) {
390 default:
391 dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
392 break;
393
394 /* does not happen...
395 * initialization done in drbd_req_new
396 case created:
397 break;
398 */
399
400 case to_be_send: /* via network */
401 /* reached via drbd_make_request_common
402 * and from w_read_retry_remote */
403 D_ASSERT(!(req->rq_state & RQ_NET_MASK));
404 req->rq_state |= RQ_NET_PENDING;
405 inc_ap_pending(mdev);
406 break;
407
408 case to_be_submitted: /* locally */
409 /* reached via drbd_make_request_common */
410 D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
411 req->rq_state |= RQ_LOCAL_PENDING;
412 break;
413
414 case completed_ok:
415 if (bio_data_dir(req->master_bio) == WRITE)
416 mdev->writ_cnt += req->size>>9;
417 else
418 mdev->read_cnt += req->size>>9;
419
420 req->rq_state |= (RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
421 req->rq_state &= ~RQ_LOCAL_PENDING;
422
423 _req_may_be_done(req, m);
424 put_ldev(mdev);
425 break;
426
427 case write_completed_with_error:
428 req->rq_state |= RQ_LOCAL_COMPLETED;
429 req->rq_state &= ~RQ_LOCAL_PENDING;
430
431 dev_alert(DEV, "Local WRITE failed sec=%llus size=%u\n",
432 (unsigned long long)req->sector, req->size);
433 /* and now: check how to handle local io error. */
434 __drbd_chk_io_error(mdev, FALSE);
435 _req_may_be_done(req, m);
436 put_ldev(mdev);
437 break;
438
439 case read_ahead_completed_with_error:
440 /* it is legal to fail READA */
441 req->rq_state |= RQ_LOCAL_COMPLETED;
442 req->rq_state &= ~RQ_LOCAL_PENDING;
443 _req_may_be_done(req, m);
444 put_ldev(mdev);
445 break;
446
447 case read_completed_with_error:
448 drbd_set_out_of_sync(mdev, req->sector, req->size);
449
450 req->rq_state |= RQ_LOCAL_COMPLETED;
451 req->rq_state &= ~RQ_LOCAL_PENDING;
452
453 dev_alert(DEV, "Local READ failed sec=%llus size=%u\n",
454 (unsigned long long)req->sector, req->size);
455 /* _req_mod(req,to_be_send); oops, recursion... */
456 D_ASSERT(!(req->rq_state & RQ_NET_MASK));
457 req->rq_state |= RQ_NET_PENDING;
458 inc_ap_pending(mdev);
459
460 __drbd_chk_io_error(mdev, FALSE);
461 put_ldev(mdev);
462 /* NOTE: if we have no connection,
463 * or know the peer has no good data either,
464 * then we don't actually need to "queue_for_net_read",
465 * but we do so anyways, since the drbd_io_error()
466 * and the potential state change to "Diskless"
467 * needs to be done from process context */
468
469 /* fall through: _req_mod(req,queue_for_net_read); */
470
471 case queue_for_net_read:
472 /* READ or READA, and
473 * no local disk,
474 * or target area marked as invalid,
475 * or just got an io-error. */
476 /* from drbd_make_request_common
477 * or from bio_endio during read io-error recovery */
478
479 /* so we can verify the handle in the answer packet
480 * corresponding hlist_del is in _req_may_be_done() */
481 hlist_add_head(&req->colision, ar_hash_slot(mdev, req->sector));
482
Lars Ellenberg83c38832009-11-03 02:22:06 +0100483 set_bit(UNPLUG_REMOTE, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700484
485 D_ASSERT(req->rq_state & RQ_NET_PENDING);
486 req->rq_state |= RQ_NET_QUEUED;
487 req->w.cb = (req->rq_state & RQ_LOCAL_MASK)
488 ? w_read_retry_remote
489 : w_send_read_req;
490 drbd_queue_work(&mdev->data.work, &req->w);
491 break;
492
493 case queue_for_net_write:
494 /* assert something? */
495 /* from drbd_make_request_common only */
496
497 hlist_add_head(&req->colision, tl_hash_slot(mdev, req->sector));
498 /* corresponding hlist_del is in _req_may_be_done() */
499
500 /* NOTE
501 * In case the req ended up on the transfer log before being
502 * queued on the worker, it could lead to this request being
503 * missed during cleanup after connection loss.
504 * So we have to do both operations here,
505 * within the same lock that protects the transfer log.
506 *
507 * _req_add_to_epoch(req); this has to be after the
508 * _maybe_start_new_epoch(req); which happened in
509 * drbd_make_request_common, because we now may set the bit
510 * again ourselves to close the current epoch.
511 *
512 * Add req to the (now) current epoch (barrier). */
513
Lars Ellenberg83c38832009-11-03 02:22:06 +0100514 /* otherwise we may lose an unplug, which may cause some remote
515 * io-scheduler timeout to expire, increasing maximum latency,
516 * hurting performance. */
517 set_bit(UNPLUG_REMOTE, &mdev->flags);
518
Philipp Reisnerb411b362009-09-25 16:07:19 -0700519 /* see drbd_make_request_common,
520 * just after it grabs the req_lock */
521 D_ASSERT(test_bit(CREATE_BARRIER, &mdev->flags) == 0);
522
523 req->epoch = mdev->newest_tle->br_number;
524 list_add_tail(&req->tl_requests,
525 &mdev->newest_tle->requests);
526
527 /* increment size of current epoch */
528 mdev->newest_tle->n_req++;
529
530 /* queue work item to send data */
531 D_ASSERT(req->rq_state & RQ_NET_PENDING);
532 req->rq_state |= RQ_NET_QUEUED;
533 req->w.cb = w_send_dblock;
534 drbd_queue_work(&mdev->data.work, &req->w);
535
536 /* close the epoch, in case it outgrew the limit */
537 if (mdev->newest_tle->n_req >= mdev->net_conf->max_epoch_size)
538 queue_barrier(mdev);
539
540 break;
541
542 case send_canceled:
543 /* treat it the same */
544 case send_failed:
545 /* real cleanup will be done from tl_clear. just update flags
546 * so it is no longer marked as on the worker queue */
547 req->rq_state &= ~RQ_NET_QUEUED;
548 /* if we did it right, tl_clear should be scheduled only after
549 * this, so this should not be necessary! */
550 _req_may_be_done(req, m);
551 break;
552
553 case handed_over_to_network:
554 /* assert something? */
555 if (bio_data_dir(req->master_bio) == WRITE &&
556 mdev->net_conf->wire_protocol == DRBD_PROT_A) {
557 /* this is what is dangerous about protocol A:
558 * pretend it was successfully written on the peer. */
559 if (req->rq_state & RQ_NET_PENDING) {
560 dec_ap_pending(mdev);
561 req->rq_state &= ~RQ_NET_PENDING;
562 req->rq_state |= RQ_NET_OK;
563 } /* else: neg-ack was faster... */
564 /* it is still not yet RQ_NET_DONE until the
565 * corresponding epoch barrier got acked as well,
566 * so we know what to dirty on connection loss */
567 }
568 req->rq_state &= ~RQ_NET_QUEUED;
569 req->rq_state |= RQ_NET_SENT;
570 /* because _drbd_send_zc_bio could sleep, and may want to
571 * dereference the bio even after the "write_acked_by_peer" and
572 * "completed_ok" events came in, once we return from
573 * _drbd_send_zc_bio (drbd_send_dblock), we have to check
574 * whether it is done already, and end it. */
575 _req_may_be_done(req, m);
576 break;
577
578 case connection_lost_while_pending:
579 /* transfer log cleanup after connection loss */
580 /* assert something? */
581 if (req->rq_state & RQ_NET_PENDING)
582 dec_ap_pending(mdev);
583 req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
584 req->rq_state |= RQ_NET_DONE;
585 /* if it is still queued, we may not complete it here.
586 * it will be canceled soon. */
587 if (!(req->rq_state & RQ_NET_QUEUED))
588 _req_may_be_done(req, m);
589 break;
590
591 case write_acked_by_peer_and_sis:
592 req->rq_state |= RQ_NET_SIS;
593 case conflict_discarded_by_peer:
594 /* for discarded conflicting writes of multiple primaries,
595 * there is no need to keep anything in the tl, potential
596 * node crashes are covered by the activity log. */
597 if (what == conflict_discarded_by_peer)
598 dev_alert(DEV, "Got DiscardAck packet %llus +%u!"
599 " DRBD is not a random data generator!\n",
600 (unsigned long long)req->sector, req->size);
601 req->rq_state |= RQ_NET_DONE;
602 /* fall through */
603 case write_acked_by_peer:
604 /* protocol C; successfully written on peer.
605 * Nothing to do here.
606 * We want to keep the tl in place for all protocols, to cater
607 * for volatile write-back caches on lower level devices.
608 *
609 * A barrier request is expected to have forced all prior
610 * requests onto stable storage, so completion of a barrier
611 * request could set NET_DONE right here, and not wait for the
612 * P_BARRIER_ACK, but that is an unnecessary optimization. */
613
614 /* this makes it effectively the same as for: */
615 case recv_acked_by_peer:
616 /* protocol B; pretends to be successfully written on peer.
617 * see also notes above in handed_over_to_network about
618 * protocol != C */
619 req->rq_state |= RQ_NET_OK;
620 D_ASSERT(req->rq_state & RQ_NET_PENDING);
621 dec_ap_pending(mdev);
622 req->rq_state &= ~RQ_NET_PENDING;
623 _req_may_be_done(req, m);
624 break;
625
626 case neg_acked:
627 /* assert something? */
628 if (req->rq_state & RQ_NET_PENDING)
629 dec_ap_pending(mdev);
630 req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
631
632 req->rq_state |= RQ_NET_DONE;
633 _req_may_be_done(req, m);
634 /* else: done by handed_over_to_network */
635 break;
636
637 case barrier_acked:
638 if (req->rq_state & RQ_NET_PENDING) {
639 /* barrier came in before all requests have been acked.
640 * this is bad, because if the connection is lost now,
641 * we won't be able to clean them up... */
642 dev_err(DEV, "FIXME (barrier_acked but pending)\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700643 list_move(&req->tl_requests, &mdev->out_of_sequence_requests);
644 }
645 D_ASSERT(req->rq_state & RQ_NET_SENT);
646 req->rq_state |= RQ_NET_DONE;
647 _req_may_be_done(req, m);
648 break;
649
650 case data_received:
651 D_ASSERT(req->rq_state & RQ_NET_PENDING);
652 dec_ap_pending(mdev);
653 req->rq_state &= ~RQ_NET_PENDING;
654 req->rq_state |= (RQ_NET_OK|RQ_NET_DONE);
655 _req_may_be_done(req, m);
656 break;
657 };
658}
659
660/* we may do a local read if:
661 * - we are consistent (of course),
662 * - or we are generally inconsistent,
663 * BUT we are still/already IN SYNC for this area.
664 * since size may be bigger than BM_BLOCK_SIZE,
665 * we may need to check several bits.
666 */
667static int drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
668{
669 unsigned long sbnr, ebnr;
670 sector_t esector, nr_sectors;
671
672 if (mdev->state.disk == D_UP_TO_DATE)
673 return 1;
674 if (mdev->state.disk >= D_OUTDATED)
675 return 0;
676 if (mdev->state.disk < D_INCONSISTENT)
677 return 0;
678 /* state.disk == D_INCONSISTENT We will have a look at the BitMap */
679 nr_sectors = drbd_get_capacity(mdev->this_bdev);
680 esector = sector + (size >> 9) - 1;
681
682 D_ASSERT(sector < nr_sectors);
683 D_ASSERT(esector < nr_sectors);
684
685 sbnr = BM_SECT_TO_BIT(sector);
686 ebnr = BM_SECT_TO_BIT(esector);
687
688 return 0 == drbd_bm_count_bits(mdev, sbnr, ebnr);
689}
690
691static int drbd_make_request_common(struct drbd_conf *mdev, struct bio *bio)
692{
693 const int rw = bio_rw(bio);
694 const int size = bio->bi_size;
695 const sector_t sector = bio->bi_sector;
696 struct drbd_tl_epoch *b = NULL;
697 struct drbd_request *req;
698 int local, remote;
699 int err = -EIO;
Philipp Reisner9a25a042010-05-10 16:42:23 +0200700 int ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700701
702 /* allocate outside of all locks; */
703 req = drbd_req_new(mdev, bio);
704 if (!req) {
705 dec_ap_bio(mdev);
706 /* only pass the error to the upper layers.
707 * if user cannot handle io errors, that's not our business. */
708 dev_err(DEV, "could not kmalloc() req\n");
709 bio_endio(bio, -ENOMEM);
710 return 0;
711 }
712
Philipp Reisnerb411b362009-09-25 16:07:19 -0700713 local = get_ldev(mdev);
714 if (!local) {
715 bio_put(req->private_bio); /* or we get a bio leak */
716 req->private_bio = NULL;
717 }
718 if (rw == WRITE) {
719 remote = 1;
720 } else {
721 /* READ || READA */
722 if (local) {
723 if (!drbd_may_do_local_read(mdev, sector, size)) {
724 /* we could kick the syncer to
725 * sync this extent asap, wait for
726 * it, then continue locally.
727 * Or just issue the request remotely.
728 */
729 local = 0;
730 bio_put(req->private_bio);
731 req->private_bio = NULL;
732 put_ldev(mdev);
733 }
734 }
735 remote = !local && mdev->state.pdsk >= D_UP_TO_DATE;
736 }
737
738 /* If we have a disk, but a READA request is mapped to remote,
739 * we are R_PRIMARY, D_INCONSISTENT, SyncTarget.
740 * Just fail that READA request right here.
741 *
742 * THINK: maybe fail all READA when not local?
743 * or make this configurable...
744 * if network is slow, READA won't do any good.
745 */
746 if (rw == READA && mdev->state.disk >= D_INCONSISTENT && !local) {
747 err = -EWOULDBLOCK;
748 goto fail_and_free_req;
749 }
750
751 /* For WRITES going to the local disk, grab a reference on the target
752 * extent. This waits for any resync activity in the corresponding
753 * resync extent to finish, and, if necessary, pulls in the target
754 * extent into the activity log, which involves further disk io because
755 * of transactional on-disk meta data updates. */
756 if (rw == WRITE && local)
757 drbd_al_begin_io(mdev, sector);
758
759 remote = remote && (mdev->state.pdsk == D_UP_TO_DATE ||
760 (mdev->state.pdsk == D_INCONSISTENT &&
761 mdev->state.conn >= C_CONNECTED));
762
Philipp Reisner9a25a042010-05-10 16:42:23 +0200763 if (!(local || remote) && !mdev->state.susp) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700764 dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
765 goto fail_free_complete;
766 }
767
768 /* For WRITE request, we have to make sure that we have an
769 * unused_spare_tle, in case we need to start a new epoch.
770 * I try to be smart and avoid to pre-allocate always "just in case",
771 * but there is a race between testing the bit and pointer outside the
772 * spinlock, and grabbing the spinlock.
773 * if we lost that race, we retry. */
774 if (rw == WRITE && remote &&
775 mdev->unused_spare_tle == NULL &&
776 test_bit(CREATE_BARRIER, &mdev->flags)) {
777allocate_barrier:
778 b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_NOIO);
779 if (!b) {
780 dev_err(DEV, "Failed to alloc barrier.\n");
781 err = -ENOMEM;
782 goto fail_free_complete;
783 }
784 }
785
786 /* GOOD, everything prepared, grab the spin_lock */
787 spin_lock_irq(&mdev->req_lock);
788
Philipp Reisner9a25a042010-05-10 16:42:23 +0200789 if (mdev->state.susp) {
790 /* If we got suspended, use the retry mechanism of
791 generic_make_request() to restart processing of this
792 bio. In the next call to drbd_make_request_26
793 we sleep in inc_ap_bio() */
794 ret = 1;
795 spin_unlock_irq(&mdev->req_lock);
796 goto fail_free_complete;
797 }
798
Philipp Reisnerb411b362009-09-25 16:07:19 -0700799 if (remote) {
800 remote = (mdev->state.pdsk == D_UP_TO_DATE ||
801 (mdev->state.pdsk == D_INCONSISTENT &&
802 mdev->state.conn >= C_CONNECTED));
803 if (!remote)
804 dev_warn(DEV, "lost connection while grabbing the req_lock!\n");
805 if (!(local || remote)) {
806 dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
807 spin_unlock_irq(&mdev->req_lock);
808 goto fail_free_complete;
809 }
810 }
811
812 if (b && mdev->unused_spare_tle == NULL) {
813 mdev->unused_spare_tle = b;
814 b = NULL;
815 }
816 if (rw == WRITE && remote &&
817 mdev->unused_spare_tle == NULL &&
818 test_bit(CREATE_BARRIER, &mdev->flags)) {
819 /* someone closed the current epoch
820 * while we were grabbing the spinlock */
821 spin_unlock_irq(&mdev->req_lock);
822 goto allocate_barrier;
823 }
824
825
826 /* Update disk stats */
827 _drbd_start_io_acct(mdev, req, bio);
828
829 /* _maybe_start_new_epoch(mdev);
830 * If we need to generate a write barrier packet, we have to add the
831 * new epoch (barrier) object, and queue the barrier packet for sending,
832 * and queue the req's data after it _within the same lock_, otherwise
833 * we have race conditions were the reorder domains could be mixed up.
834 *
835 * Even read requests may start a new epoch and queue the corresponding
836 * barrier packet. To get the write ordering right, we only have to
837 * make sure that, if this is a write request and it triggered a
838 * barrier packet, this request is queued within the same spinlock. */
839 if (remote && mdev->unused_spare_tle &&
840 test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
841 _tl_add_barrier(mdev, mdev->unused_spare_tle);
842 mdev->unused_spare_tle = NULL;
843 } else {
844 D_ASSERT(!(remote && rw == WRITE &&
845 test_bit(CREATE_BARRIER, &mdev->flags)));
846 }
847
848 /* NOTE
849 * Actually, 'local' may be wrong here already, since we may have failed
850 * to write to the meta data, and may become wrong anytime because of
851 * local io-error for some other request, which would lead to us
852 * "detaching" the local disk.
853 *
854 * 'remote' may become wrong any time because the network could fail.
855 *
856 * This is a harmless race condition, though, since it is handled
857 * correctly at the appropriate places; so it just defers the failure
858 * of the respective operation.
859 */
860
861 /* mark them early for readability.
862 * this just sets some state flags. */
863 if (remote)
864 _req_mod(req, to_be_send);
865 if (local)
866 _req_mod(req, to_be_submitted);
867
868 /* check this request on the collision detection hash tables.
869 * if we have a conflict, just complete it here.
870 * THINK do we want to check reads, too? (I don't think so...) */
871 if (rw == WRITE && _req_conflicts(req)) {
872 /* this is a conflicting request.
873 * even though it may have been only _partially_
874 * overlapping with one of the currently pending requests,
875 * without even submitting or sending it, we will
876 * pretend that it was successfully served right now.
877 */
878 if (local) {
879 bio_put(req->private_bio);
880 req->private_bio = NULL;
881 drbd_al_complete_io(mdev, req->sector);
882 put_ldev(mdev);
883 local = 0;
884 }
885 if (remote)
886 dec_ap_pending(mdev);
887 _drbd_end_io_acct(mdev, req);
888 /* THINK: do we want to fail it (-EIO), or pretend success? */
889 bio_endio(req->master_bio, 0);
890 req->master_bio = NULL;
891 dec_ap_bio(mdev);
892 drbd_req_free(req);
893 remote = 0;
894 }
895
896 /* NOTE remote first: to get the concurrent write detection right,
897 * we must register the request before start of local IO. */
898 if (remote) {
899 /* either WRITE and C_CONNECTED,
900 * or READ, and no local disk,
901 * or READ, but not in sync.
902 */
903 _req_mod(req, (rw == WRITE)
904 ? queue_for_net_write
905 : queue_for_net_read);
906 }
907 spin_unlock_irq(&mdev->req_lock);
908 kfree(b); /* if someone else has beaten us to it... */
909
910 if (local) {
911 req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
912
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 if (FAULT_ACTIVE(mdev, rw == WRITE ? DRBD_FAULT_DT_WR
914 : rw == READ ? DRBD_FAULT_DT_RD
915 : DRBD_FAULT_DT_RA))
916 bio_endio(req->private_bio, -EIO);
917 else
918 generic_make_request(req->private_bio);
919 }
920
921 /* we need to plug ALWAYS since we possibly need to kick lo_dev.
922 * we plug after submit, so we won't miss an unplug event */
923 drbd_plug_device(mdev);
924
925 return 0;
926
927fail_free_complete:
928 if (rw == WRITE && local)
929 drbd_al_complete_io(mdev, sector);
930fail_and_free_req:
931 if (local) {
932 bio_put(req->private_bio);
933 req->private_bio = NULL;
934 put_ldev(mdev);
935 }
Philipp Reisner9a25a042010-05-10 16:42:23 +0200936 if (!ret)
937 bio_endio(bio, err);
938
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939 drbd_req_free(req);
940 dec_ap_bio(mdev);
941 kfree(b);
942
Philipp Reisner9a25a042010-05-10 16:42:23 +0200943 return ret;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700944}
945
946/* helper function for drbd_make_request
947 * if we can determine just by the mdev (state) that this request will fail,
948 * return 1
949 * otherwise return 0
950 */
951static int drbd_fail_request_early(struct drbd_conf *mdev, int is_write)
952{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953 if (mdev->state.role != R_PRIMARY &&
954 (!allow_oos || is_write)) {
955 if (__ratelimit(&drbd_ratelimit_state)) {
956 dev_err(DEV, "Process %s[%u] tried to %s; "
957 "since we are not in Primary state, "
958 "we cannot allow this\n",
959 current->comm, current->pid,
960 is_write ? "WRITE" : "READ");
961 }
962 return 1;
963 }
964
965 /*
966 * Paranoia: we might have been primary, but sync target, or
967 * even diskless, then lost the connection.
968 * This should have been handled (panic? suspend?) somewhere
969 * else. But maybe it was not, so check again here.
970 * Caution: as long as we do not have a read/write lock on mdev,
971 * to serialize state changes, this is racy, since we may lose
972 * the connection *after* we test for the cstate.
973 */
974 if (mdev->state.disk < D_UP_TO_DATE && mdev->state.pdsk < D_UP_TO_DATE) {
975 if (__ratelimit(&drbd_ratelimit_state))
976 dev_err(DEV, "Sorry, I have no access to good data anymore.\n");
977 return 1;
978 }
979
980 return 0;
981}
982
983int drbd_make_request_26(struct request_queue *q, struct bio *bio)
984{
985 unsigned int s_enr, e_enr;
986 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
987
988 if (drbd_fail_request_early(mdev, bio_data_dir(bio) & WRITE)) {
989 bio_endio(bio, -EPERM);
990 return 0;
991 }
992
993 /* Reject barrier requests if we know the underlying device does
994 * not support them.
995 * XXX: Need to get this info from peer as well some how so we
996 * XXX: reject if EITHER side/data/metadata area does not support them.
997 *
998 * because of those XXX, this is not yet enabled,
999 * i.e. in drbd_init_set_defaults we set the NO_BARRIER_SUPP bit.
1000 */
1001 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER) && test_bit(NO_BARRIER_SUPP, &mdev->flags))) {
1002 /* dev_warn(DEV, "Rejecting barrier request as underlying device does not support\n"); */
1003 bio_endio(bio, -EOPNOTSUPP);
1004 return 0;
1005 }
1006
1007 /*
1008 * what we "blindly" assume:
1009 */
1010 D_ASSERT(bio->bi_size > 0);
1011 D_ASSERT((bio->bi_size & 0x1ff) == 0);
1012 D_ASSERT(bio->bi_idx == 0);
1013
1014 /* to make some things easier, force alignment of requests within the
1015 * granularity of our hash tables */
1016 s_enr = bio->bi_sector >> HT_SHIFT;
1017 e_enr = (bio->bi_sector+(bio->bi_size>>9)-1) >> HT_SHIFT;
1018
1019 if (likely(s_enr == e_enr)) {
1020 inc_ap_bio(mdev, 1);
1021 return drbd_make_request_common(mdev, bio);
1022 }
1023
1024 /* can this bio be split generically?
1025 * Maybe add our own split-arbitrary-bios function. */
1026 if (bio->bi_vcnt != 1 || bio->bi_idx != 0 || bio->bi_size > DRBD_MAX_SEGMENT_SIZE) {
1027 /* rather error out here than BUG in bio_split */
1028 dev_err(DEV, "bio would need to, but cannot, be split: "
1029 "(vcnt=%u,idx=%u,size=%u,sector=%llu)\n",
1030 bio->bi_vcnt, bio->bi_idx, bio->bi_size,
1031 (unsigned long long)bio->bi_sector);
1032 bio_endio(bio, -EINVAL);
1033 } else {
1034 /* This bio crosses some boundary, so we have to split it. */
1035 struct bio_pair *bp;
1036 /* works for the "do not cross hash slot boundaries" case
1037 * e.g. sector 262269, size 4096
1038 * s_enr = 262269 >> 6 = 4097
1039 * e_enr = (262269+8-1) >> 6 = 4098
1040 * HT_SHIFT = 6
1041 * sps = 64, mask = 63
1042 * first_sectors = 64 - (262269 & 63) = 3
1043 */
1044 const sector_t sect = bio->bi_sector;
1045 const int sps = 1 << HT_SHIFT; /* sectors per slot */
1046 const int mask = sps - 1;
1047 const sector_t first_sectors = sps - (sect & mask);
1048 bp = bio_split(bio,
1049#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
1050 bio_split_pool,
1051#endif
1052 first_sectors);
1053
1054 /* we need to get a "reference count" (ap_bio_cnt)
1055 * to avoid races with the disconnect/reconnect/suspend code.
Philipp Reisner9a25a042010-05-10 16:42:23 +02001056 * In case we need to split the bio here, we need to get three references
Philipp Reisnerb411b362009-09-25 16:07:19 -07001057 * atomically, otherwise we might deadlock when trying to submit the
1058 * second one! */
Philipp Reisner9a25a042010-05-10 16:42:23 +02001059 inc_ap_bio(mdev, 3);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060
1061 D_ASSERT(e_enr == s_enr + 1);
1062
Philipp Reisner9a25a042010-05-10 16:42:23 +02001063 while (drbd_make_request_common(mdev, &bp->bio1))
1064 inc_ap_bio(mdev, 1);
1065
1066 while (drbd_make_request_common(mdev, &bp->bio2))
1067 inc_ap_bio(mdev, 1);
1068
1069 dec_ap_bio(mdev);
1070
Philipp Reisnerb411b362009-09-25 16:07:19 -07001071 bio_pair_release(bp);
1072 }
1073 return 0;
1074}
1075
1076/* This is called by bio_add_page(). With this function we reduce
1077 * the number of BIOs that span over multiple DRBD_MAX_SEGMENT_SIZEs
1078 * units (was AL_EXTENTs).
1079 *
1080 * we do the calculation within the lower 32bit of the byte offsets,
1081 * since we don't care for actual offset, but only check whether it
1082 * would cross "activity log extent" boundaries.
1083 *
1084 * As long as the BIO is empty we have to allow at least one bvec,
1085 * regardless of size and offset. so the resulting bio may still
1086 * cross extent boundaries. those are dealt with (bio_split) in
1087 * drbd_make_request_26.
1088 */
1089int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
1090{
1091 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
1092 unsigned int bio_offset =
1093 (unsigned int)bvm->bi_sector << 9; /* 32 bit */
1094 unsigned int bio_size = bvm->bi_size;
1095 int limit, backing_limit;
1096
1097 limit = DRBD_MAX_SEGMENT_SIZE
1098 - ((bio_offset & (DRBD_MAX_SEGMENT_SIZE-1)) + bio_size);
1099 if (limit < 0)
1100 limit = 0;
1101 if (bio_size == 0) {
1102 if (limit <= bvec->bv_len)
1103 limit = bvec->bv_len;
1104 } else if (limit && get_ldev(mdev)) {
1105 struct request_queue * const b =
1106 mdev->ldev->backing_bdev->bd_disk->queue;
Lars Ellenberga1c88d02010-05-14 19:16:41 +02001107 if (b->merge_bvec_fn) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001108 backing_limit = b->merge_bvec_fn(b, bvm, bvec);
1109 limit = min(limit, backing_limit);
1110 }
1111 put_ldev(mdev);
1112 }
1113 return limit;
1114}