blob: 3397f11d0ba915fd29ca9cd6f65717d2f6a83f10 [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
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. */
110
111 /* THINK
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? */
115
116 if (rw == WRITE &&
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);
124 }
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 */
128 } else {
129 drbd_req_free(req);
130 }
131}
132
133static void queue_barrier(struct drbd_conf *mdev)
134{
135 struct drbd_tl_epoch *b;
136
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))
143 return;
144
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);
154}
155
156static void _about_to_complete_local_write(struct drbd_conf *mdev,
157 struct drbd_request *req)
158{
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;
164
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)
169 queue_barrier(mdev);
170
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;
177
178 /* ASSERT:
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) {
184 if (OVERLAPS) {
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);
189 }
190 }
191
192 /* maybe "wake" those conflicting epoch entries
193 * that wait for this request to finish.
194 *
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?
201 *
202 * anyways, if we found one,
203 * we just have to do a wake_up. */
204#undef OVERLAPS
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) {
208 if (OVERLAPS) {
209 wake_up(&mdev->misc_wait);
210 break;
211 }
212 }
213 }
214#undef OVERLAPS
215}
216
217void complete_master_bio(struct drbd_conf *mdev,
218 struct bio_and_error *m)
219{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700220 bio_endio(m->bio, m->error);
221 dec_ap_bio(mdev);
222}
223
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.
229 */
230void _req_may_be_done(struct drbd_request *req, struct bio_and_error *m)
231{
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;
236
Philipp Reisnerb411b362009-09-25 16:07:19 -0700237 /* 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
242 * the worker,
243 * the receiver,
244 * the bio_endio completion callbacks.
245 */
246 if (s & RQ_NET_QUEUED)
247 return;
248 if (s & RQ_NET_PENDING)
249 return;
250 if (s & RQ_LOCAL_PENDING)
251 return;
252
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.
260 */
261
262 /*
263 * figure out whether to report success or failure.
264 *
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.
268 *
269 * what to do about the failures is handled elsewhere.
270 * what we need to do here is just: complete the master_bio.
271 *
272 * local completion error, if any, has been stored as ERR_PTR
273 * in private_bio within drbd_endio_pri.
274 */
275 int ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
276 int error = PTR_ERR(req->private_bio);
277
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);
282 else
283 D_ASSERT((s & RQ_NET_MASK) == 0);
284
285 /* for writes we need to do some extra housekeeping */
286 if (rw == WRITE)
287 _about_to_complete_local_write(mdev, req);
288
289 /* Update disk stats */
290 _drbd_end_io_acct(mdev, req);
291
292 m->error = ok ? 0 : (error ?: -EIO);
293 m->bio = req->master_bio;
294 req->master_bio = NULL;
295 }
296
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);
303 }
304 /* else: network part and not DONE yet. that is
305 * protocol A or B, barrier ack still pending... */
306}
307
308/*
309 * checks whether there was an overlapping request
310 * or ee already registered.
311 *
312 * if so, return 1, in which case this request is completed on the spot,
313 * without ever being submitted or send.
314 *
315 * return 0 if it is ok to submit this request.
316 *
317 * NOTE:
318 * paranoia: assume something above us is broken, and issues different write
319 * requests for the same block simultaneously...
320 *
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.
326 *
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...
330 */
331static int _req_conflicts(struct drbd_request *req)
332{
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;
340
341 D_ASSERT(hlist_unhashed(&req->colision));
342
343 if (!get_net_conf(mdev))
344 return 0;
345
346 /* BUG_ON */
347 ERR_IF (mdev->tl_hash_s == 0)
348 goto out_no_conflict;
349 BUG_ON(mdev->tl_hash == NULL);
350
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) {
354 if (OVERLAPS) {
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);
361 goto out_conflict;
362 }
363 }
364
365 if (mdev->ee_hash_s) {
366 /* now, check for overlapping requests with remote origin */
367 BUG_ON(mdev->ee_hash == NULL);
368#undef OVERLAPS
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) {
372 if (OVERLAPS) {
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);
379 goto out_conflict;
380 }
381 }
382 }
383#undef OVERLAPS
384
385out_no_conflict:
386 /* this is like it should be, and what we expected.
387 * our users do behave after all... */
388 put_net_conf(mdev);
389 return 0;
390
391out_conflict:
392 put_net_conf(mdev);
393 return 1;
394}
395
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).
400 *
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 ...
407 */
408void __req_mod(struct drbd_request *req, enum drbd_req_event what,
409 struct bio_and_error *m)
410{
411 struct drbd_conf *mdev = req->mdev;
412 m->bio = NULL;
413
Philipp Reisnerb411b362009-09-25 16:07:19 -0700414 switch (what) {
415 default:
416 dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
417 break;
418
419 /* does not happen...
420 * initialization done in drbd_req_new
421 case created:
422 break;
423 */
424
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);
431 break;
432
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;
437 break;
438
439 case completed_ok:
440 if (bio_data_dir(req->master_bio) == WRITE)
441 mdev->writ_cnt += req->size>>9;
442 else
443 mdev->read_cnt += req->size>>9;
444
445 req->rq_state |= (RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
446 req->rq_state &= ~RQ_LOCAL_PENDING;
447
448 _req_may_be_done(req, m);
449 put_ldev(mdev);
450 break;
451
452 case write_completed_with_error:
453 req->rq_state |= RQ_LOCAL_COMPLETED;
454 req->rq_state &= ~RQ_LOCAL_PENDING;
455
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);
461 put_ldev(mdev);
462 break;
463
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);
469 put_ldev(mdev);
470 break;
471
472 case read_completed_with_error:
473 drbd_set_out_of_sync(mdev, req->sector, req->size);
474
475 req->rq_state |= RQ_LOCAL_COMPLETED;
476 req->rq_state &= ~RQ_LOCAL_PENDING;
477
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);
484
485 __drbd_chk_io_error(mdev, FALSE);
486 put_ldev(mdev);
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 */
493
494 /* fall through: _req_mod(req,queue_for_net_read); */
495
496 case queue_for_net_read:
497 /* READ or READA, and
498 * no local disk,
499 * or target area marked as invalid,
500 * or just got an io-error. */
501 /* from drbd_make_request_common
502 * or from bio_endio during read io-error recovery */
503
504 /* so we can verify the handle in the answer packet
505 * corresponding hlist_del is in _req_may_be_done() */
506 hlist_add_head(&req->colision, ar_hash_slot(mdev, req->sector));
507
Lars Ellenberg83c38832009-11-03 02:22:06 +0100508 set_bit(UNPLUG_REMOTE, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700509
510 D_ASSERT(req->rq_state & RQ_NET_PENDING);
511 req->rq_state |= RQ_NET_QUEUED;
512 req->w.cb = (req->rq_state & RQ_LOCAL_MASK)
513 ? w_read_retry_remote
514 : w_send_read_req;
515 drbd_queue_work(&mdev->data.work, &req->w);
516 break;
517
518 case queue_for_net_write:
519 /* assert something? */
520 /* from drbd_make_request_common only */
521
522 hlist_add_head(&req->colision, tl_hash_slot(mdev, req->sector));
523 /* corresponding hlist_del is in _req_may_be_done() */
524
525 /* NOTE
526 * In case the req ended up on the transfer log before being
527 * queued on the worker, it could lead to this request being
528 * missed during cleanup after connection loss.
529 * So we have to do both operations here,
530 * within the same lock that protects the transfer log.
531 *
532 * _req_add_to_epoch(req); this has to be after the
533 * _maybe_start_new_epoch(req); which happened in
534 * drbd_make_request_common, because we now may set the bit
535 * again ourselves to close the current epoch.
536 *
537 * Add req to the (now) current epoch (barrier). */
538
Lars Ellenberg83c38832009-11-03 02:22:06 +0100539 /* otherwise we may lose an unplug, which may cause some remote
540 * io-scheduler timeout to expire, increasing maximum latency,
541 * hurting performance. */
542 set_bit(UNPLUG_REMOTE, &mdev->flags);
543
Philipp Reisnerb411b362009-09-25 16:07:19 -0700544 /* see drbd_make_request_common,
545 * just after it grabs the req_lock */
546 D_ASSERT(test_bit(CREATE_BARRIER, &mdev->flags) == 0);
547
548 req->epoch = mdev->newest_tle->br_number;
549 list_add_tail(&req->tl_requests,
550 &mdev->newest_tle->requests);
551
552 /* increment size of current epoch */
553 mdev->newest_tle->n_req++;
554
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);
560
561 /* close the epoch, in case it outgrew the limit */
562 if (mdev->newest_tle->n_req >= mdev->net_conf->max_epoch_size)
563 queue_barrier(mdev);
564
565 break;
566
567 case send_canceled:
568 /* treat it the same */
569 case send_failed:
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);
576 break;
577
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 */
592 }
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);
601 break;
602
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);
614 break;
615
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;
627 /* fall through */
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.
633 *
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. */
638
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
643 * protocol != C */
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);
649 break;
650
651 case neg_acked:
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);
656
657 req->rq_state |= RQ_NET_DONE;
658 _req_may_be_done(req, m);
659 /* else: done by handed_over_to_network */
660 break;
661
662 case barrier_acked:
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");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700668 list_move(&req->tl_requests, &mdev->out_of_sequence_requests);
669 }
670 D_ASSERT(req->rq_state & RQ_NET_SENT);
671 req->rq_state |= RQ_NET_DONE;
672 _req_may_be_done(req, m);
673 break;
674
675 case data_received:
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);
681 break;
682 };
683}
684
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.
691 */
692static int drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
693{
694 unsigned long sbnr, ebnr;
695 sector_t esector, nr_sectors;
696
697 if (mdev->state.disk == D_UP_TO_DATE)
698 return 1;
699 if (mdev->state.disk >= D_OUTDATED)
700 return 0;
701 if (mdev->state.disk < D_INCONSISTENT)
702 return 0;
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;
706
707 D_ASSERT(sector < nr_sectors);
708 D_ASSERT(esector < nr_sectors);
709
710 sbnr = BM_SECT_TO_BIT(sector);
711 ebnr = BM_SECT_TO_BIT(esector);
712
713 return 0 == drbd_bm_count_bits(mdev, sbnr, ebnr);
714}
715
716static int drbd_make_request_common(struct drbd_conf *mdev, struct bio *bio)
717{
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;
723 int local, remote;
724 int err = -EIO;
Philipp Reisner9a25a042010-05-10 16:42:23 +0200725 int ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700726
727 /* allocate outside of all locks; */
728 req = drbd_req_new(mdev, bio);
729 if (!req) {
730 dec_ap_bio(mdev);
731 /* only pass the error to the upper layers.
732 * if user cannot handle io errors, that's not our business. */
733 dev_err(DEV, "could not kmalloc() req\n");
734 bio_endio(bio, -ENOMEM);
735 return 0;
736 }
737
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738 local = get_ldev(mdev);
739 if (!local) {
740 bio_put(req->private_bio); /* or we get a bio leak */
741 req->private_bio = NULL;
742 }
743 if (rw == WRITE) {
744 remote = 1;
745 } else {
746 /* READ || READA */
747 if (local) {
748 if (!drbd_may_do_local_read(mdev, sector, size)) {
749 /* we could kick the syncer to
750 * sync this extent asap, wait for
751 * it, then continue locally.
752 * Or just issue the request remotely.
753 */
754 local = 0;
755 bio_put(req->private_bio);
756 req->private_bio = NULL;
757 put_ldev(mdev);
758 }
759 }
760 remote = !local && mdev->state.pdsk >= D_UP_TO_DATE;
761 }
762
763 /* If we have a disk, but a READA request is mapped to remote,
764 * we are R_PRIMARY, D_INCONSISTENT, SyncTarget.
765 * Just fail that READA request right here.
766 *
767 * THINK: maybe fail all READA when not local?
768 * or make this configurable...
769 * if network is slow, READA won't do any good.
770 */
771 if (rw == READA && mdev->state.disk >= D_INCONSISTENT && !local) {
772 err = -EWOULDBLOCK;
773 goto fail_and_free_req;
774 }
775
776 /* For WRITES going to the local disk, grab a reference on the target
777 * extent. This waits for any resync activity in the corresponding
778 * resync extent to finish, and, if necessary, pulls in the target
779 * extent into the activity log, which involves further disk io because
780 * of transactional on-disk meta data updates. */
781 if (rw == WRITE && local)
782 drbd_al_begin_io(mdev, sector);
783
784 remote = remote && (mdev->state.pdsk == D_UP_TO_DATE ||
785 (mdev->state.pdsk == D_INCONSISTENT &&
786 mdev->state.conn >= C_CONNECTED));
787
Philipp Reisner9a25a042010-05-10 16:42:23 +0200788 if (!(local || remote) && !mdev->state.susp) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700789 dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
790 goto fail_free_complete;
791 }
792
793 /* For WRITE request, we have to make sure that we have an
794 * unused_spare_tle, in case we need to start a new epoch.
795 * I try to be smart and avoid to pre-allocate always "just in case",
796 * but there is a race between testing the bit and pointer outside the
797 * spinlock, and grabbing the spinlock.
798 * if we lost that race, we retry. */
799 if (rw == WRITE && remote &&
800 mdev->unused_spare_tle == NULL &&
801 test_bit(CREATE_BARRIER, &mdev->flags)) {
802allocate_barrier:
803 b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_NOIO);
804 if (!b) {
805 dev_err(DEV, "Failed to alloc barrier.\n");
806 err = -ENOMEM;
807 goto fail_free_complete;
808 }
809 }
810
811 /* GOOD, everything prepared, grab the spin_lock */
812 spin_lock_irq(&mdev->req_lock);
813
Philipp Reisner9a25a042010-05-10 16:42:23 +0200814 if (mdev->state.susp) {
815 /* If we got suspended, use the retry mechanism of
816 generic_make_request() to restart processing of this
817 bio. In the next call to drbd_make_request_26
818 we sleep in inc_ap_bio() */
819 ret = 1;
820 spin_unlock_irq(&mdev->req_lock);
821 goto fail_free_complete;
822 }
823
Philipp Reisnerb411b362009-09-25 16:07:19 -0700824 if (remote) {
825 remote = (mdev->state.pdsk == D_UP_TO_DATE ||
826 (mdev->state.pdsk == D_INCONSISTENT &&
827 mdev->state.conn >= C_CONNECTED));
828 if (!remote)
829 dev_warn(DEV, "lost connection while grabbing the req_lock!\n");
830 if (!(local || remote)) {
831 dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
832 spin_unlock_irq(&mdev->req_lock);
833 goto fail_free_complete;
834 }
835 }
836
837 if (b && mdev->unused_spare_tle == NULL) {
838 mdev->unused_spare_tle = b;
839 b = NULL;
840 }
841 if (rw == WRITE && remote &&
842 mdev->unused_spare_tle == NULL &&
843 test_bit(CREATE_BARRIER, &mdev->flags)) {
844 /* someone closed the current epoch
845 * while we were grabbing the spinlock */
846 spin_unlock_irq(&mdev->req_lock);
847 goto allocate_barrier;
848 }
849
850
851 /* Update disk stats */
852 _drbd_start_io_acct(mdev, req, bio);
853
854 /* _maybe_start_new_epoch(mdev);
855 * If we need to generate a write barrier packet, we have to add the
856 * new epoch (barrier) object, and queue the barrier packet for sending,
857 * and queue the req's data after it _within the same lock_, otherwise
858 * we have race conditions were the reorder domains could be mixed up.
859 *
860 * Even read requests may start a new epoch and queue the corresponding
861 * barrier packet. To get the write ordering right, we only have to
862 * make sure that, if this is a write request and it triggered a
863 * barrier packet, this request is queued within the same spinlock. */
864 if (remote && mdev->unused_spare_tle &&
865 test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
866 _tl_add_barrier(mdev, mdev->unused_spare_tle);
867 mdev->unused_spare_tle = NULL;
868 } else {
869 D_ASSERT(!(remote && rw == WRITE &&
870 test_bit(CREATE_BARRIER, &mdev->flags)));
871 }
872
873 /* NOTE
874 * Actually, 'local' may be wrong here already, since we may have failed
875 * to write to the meta data, and may become wrong anytime because of
876 * local io-error for some other request, which would lead to us
877 * "detaching" the local disk.
878 *
879 * 'remote' may become wrong any time because the network could fail.
880 *
881 * This is a harmless race condition, though, since it is handled
882 * correctly at the appropriate places; so it just defers the failure
883 * of the respective operation.
884 */
885
886 /* mark them early for readability.
887 * this just sets some state flags. */
888 if (remote)
889 _req_mod(req, to_be_send);
890 if (local)
891 _req_mod(req, to_be_submitted);
892
893 /* check this request on the collision detection hash tables.
894 * if we have a conflict, just complete it here.
895 * THINK do we want to check reads, too? (I don't think so...) */
896 if (rw == WRITE && _req_conflicts(req)) {
897 /* this is a conflicting request.
898 * even though it may have been only _partially_
899 * overlapping with one of the currently pending requests,
900 * without even submitting or sending it, we will
901 * pretend that it was successfully served right now.
902 */
903 if (local) {
904 bio_put(req->private_bio);
905 req->private_bio = NULL;
906 drbd_al_complete_io(mdev, req->sector);
907 put_ldev(mdev);
908 local = 0;
909 }
910 if (remote)
911 dec_ap_pending(mdev);
912 _drbd_end_io_acct(mdev, req);
913 /* THINK: do we want to fail it (-EIO), or pretend success? */
914 bio_endio(req->master_bio, 0);
915 req->master_bio = NULL;
916 dec_ap_bio(mdev);
917 drbd_req_free(req);
918 remote = 0;
919 }
920
921 /* NOTE remote first: to get the concurrent write detection right,
922 * we must register the request before start of local IO. */
923 if (remote) {
924 /* either WRITE and C_CONNECTED,
925 * or READ, and no local disk,
926 * or READ, but not in sync.
927 */
928 _req_mod(req, (rw == WRITE)
929 ? queue_for_net_write
930 : queue_for_net_read);
931 }
932 spin_unlock_irq(&mdev->req_lock);
933 kfree(b); /* if someone else has beaten us to it... */
934
935 if (local) {
936 req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
937
Philipp Reisnerb411b362009-09-25 16:07:19 -0700938 if (FAULT_ACTIVE(mdev, rw == WRITE ? DRBD_FAULT_DT_WR
939 : rw == READ ? DRBD_FAULT_DT_RD
940 : DRBD_FAULT_DT_RA))
941 bio_endio(req->private_bio, -EIO);
942 else
943 generic_make_request(req->private_bio);
944 }
945
946 /* we need to plug ALWAYS since we possibly need to kick lo_dev.
947 * we plug after submit, so we won't miss an unplug event */
948 drbd_plug_device(mdev);
949
950 return 0;
951
952fail_free_complete:
953 if (rw == WRITE && local)
954 drbd_al_complete_io(mdev, sector);
955fail_and_free_req:
956 if (local) {
957 bio_put(req->private_bio);
958 req->private_bio = NULL;
959 put_ldev(mdev);
960 }
Philipp Reisner9a25a042010-05-10 16:42:23 +0200961 if (!ret)
962 bio_endio(bio, err);
963
Philipp Reisnerb411b362009-09-25 16:07:19 -0700964 drbd_req_free(req);
965 dec_ap_bio(mdev);
966 kfree(b);
967
Philipp Reisner9a25a042010-05-10 16:42:23 +0200968 return ret;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700969}
970
971/* helper function for drbd_make_request
972 * if we can determine just by the mdev (state) that this request will fail,
973 * return 1
974 * otherwise return 0
975 */
976static int drbd_fail_request_early(struct drbd_conf *mdev, int is_write)
977{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700978 if (mdev->state.role != R_PRIMARY &&
979 (!allow_oos || is_write)) {
980 if (__ratelimit(&drbd_ratelimit_state)) {
981 dev_err(DEV, "Process %s[%u] tried to %s; "
982 "since we are not in Primary state, "
983 "we cannot allow this\n",
984 current->comm, current->pid,
985 is_write ? "WRITE" : "READ");
986 }
987 return 1;
988 }
989
990 /*
991 * Paranoia: we might have been primary, but sync target, or
992 * even diskless, then lost the connection.
993 * This should have been handled (panic? suspend?) somewhere
994 * else. But maybe it was not, so check again here.
995 * Caution: as long as we do not have a read/write lock on mdev,
996 * to serialize state changes, this is racy, since we may lose
997 * the connection *after* we test for the cstate.
998 */
999 if (mdev->state.disk < D_UP_TO_DATE && mdev->state.pdsk < D_UP_TO_DATE) {
1000 if (__ratelimit(&drbd_ratelimit_state))
1001 dev_err(DEV, "Sorry, I have no access to good data anymore.\n");
1002 return 1;
1003 }
1004
1005 return 0;
1006}
1007
1008int drbd_make_request_26(struct request_queue *q, struct bio *bio)
1009{
1010 unsigned int s_enr, e_enr;
1011 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
1012
1013 if (drbd_fail_request_early(mdev, bio_data_dir(bio) & WRITE)) {
1014 bio_endio(bio, -EPERM);
1015 return 0;
1016 }
1017
1018 /* Reject barrier requests if we know the underlying device does
1019 * not support them.
1020 * XXX: Need to get this info from peer as well some how so we
1021 * XXX: reject if EITHER side/data/metadata area does not support them.
1022 *
1023 * because of those XXX, this is not yet enabled,
1024 * i.e. in drbd_init_set_defaults we set the NO_BARRIER_SUPP bit.
1025 */
1026 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER) && test_bit(NO_BARRIER_SUPP, &mdev->flags))) {
1027 /* dev_warn(DEV, "Rejecting barrier request as underlying device does not support\n"); */
1028 bio_endio(bio, -EOPNOTSUPP);
1029 return 0;
1030 }
1031
1032 /*
1033 * what we "blindly" assume:
1034 */
1035 D_ASSERT(bio->bi_size > 0);
1036 D_ASSERT((bio->bi_size & 0x1ff) == 0);
1037 D_ASSERT(bio->bi_idx == 0);
1038
1039 /* to make some things easier, force alignment of requests within the
1040 * granularity of our hash tables */
1041 s_enr = bio->bi_sector >> HT_SHIFT;
1042 e_enr = (bio->bi_sector+(bio->bi_size>>9)-1) >> HT_SHIFT;
1043
1044 if (likely(s_enr == e_enr)) {
1045 inc_ap_bio(mdev, 1);
1046 return drbd_make_request_common(mdev, bio);
1047 }
1048
1049 /* can this bio be split generically?
1050 * Maybe add our own split-arbitrary-bios function. */
1051 if (bio->bi_vcnt != 1 || bio->bi_idx != 0 || bio->bi_size > DRBD_MAX_SEGMENT_SIZE) {
1052 /* rather error out here than BUG in bio_split */
1053 dev_err(DEV, "bio would need to, but cannot, be split: "
1054 "(vcnt=%u,idx=%u,size=%u,sector=%llu)\n",
1055 bio->bi_vcnt, bio->bi_idx, bio->bi_size,
1056 (unsigned long long)bio->bi_sector);
1057 bio_endio(bio, -EINVAL);
1058 } else {
1059 /* This bio crosses some boundary, so we have to split it. */
1060 struct bio_pair *bp;
1061 /* works for the "do not cross hash slot boundaries" case
1062 * e.g. sector 262269, size 4096
1063 * s_enr = 262269 >> 6 = 4097
1064 * e_enr = (262269+8-1) >> 6 = 4098
1065 * HT_SHIFT = 6
1066 * sps = 64, mask = 63
1067 * first_sectors = 64 - (262269 & 63) = 3
1068 */
1069 const sector_t sect = bio->bi_sector;
1070 const int sps = 1 << HT_SHIFT; /* sectors per slot */
1071 const int mask = sps - 1;
1072 const sector_t first_sectors = sps - (sect & mask);
1073 bp = bio_split(bio,
1074#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
1075 bio_split_pool,
1076#endif
1077 first_sectors);
1078
1079 /* we need to get a "reference count" (ap_bio_cnt)
1080 * to avoid races with the disconnect/reconnect/suspend code.
Philipp Reisner9a25a042010-05-10 16:42:23 +02001081 * In case we need to split the bio here, we need to get three references
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082 * atomically, otherwise we might deadlock when trying to submit the
1083 * second one! */
Philipp Reisner9a25a042010-05-10 16:42:23 +02001084 inc_ap_bio(mdev, 3);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001085
1086 D_ASSERT(e_enr == s_enr + 1);
1087
Philipp Reisner9a25a042010-05-10 16:42:23 +02001088 while (drbd_make_request_common(mdev, &bp->bio1))
1089 inc_ap_bio(mdev, 1);
1090
1091 while (drbd_make_request_common(mdev, &bp->bio2))
1092 inc_ap_bio(mdev, 1);
1093
1094 dec_ap_bio(mdev);
1095
Philipp Reisnerb411b362009-09-25 16:07:19 -07001096 bio_pair_release(bp);
1097 }
1098 return 0;
1099}
1100
1101/* This is called by bio_add_page(). With this function we reduce
1102 * the number of BIOs that span over multiple DRBD_MAX_SEGMENT_SIZEs
1103 * units (was AL_EXTENTs).
1104 *
1105 * we do the calculation within the lower 32bit of the byte offsets,
1106 * since we don't care for actual offset, but only check whether it
1107 * would cross "activity log extent" boundaries.
1108 *
1109 * As long as the BIO is empty we have to allow at least one bvec,
1110 * regardless of size and offset. so the resulting bio may still
1111 * cross extent boundaries. those are dealt with (bio_split) in
1112 * drbd_make_request_26.
1113 */
1114int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
1115{
1116 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
1117 unsigned int bio_offset =
1118 (unsigned int)bvm->bi_sector << 9; /* 32 bit */
1119 unsigned int bio_size = bvm->bi_size;
1120 int limit, backing_limit;
1121
1122 limit = DRBD_MAX_SEGMENT_SIZE
1123 - ((bio_offset & (DRBD_MAX_SEGMENT_SIZE-1)) + bio_size);
1124 if (limit < 0)
1125 limit = 0;
1126 if (bio_size == 0) {
1127 if (limit <= bvec->bv_len)
1128 limit = bvec->bv_len;
1129 } else if (limit && get_ldev(mdev)) {
1130 struct request_queue * const b =
1131 mdev->ldev->backing_bdev->bd_disk->queue;
Lars Ellenberga1c88d02010-05-14 19:16:41 +02001132 if (b->merge_bvec_fn) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001133 backing_limit = b->merge_bvec_fn(b, bvm, bvec);
1134 limit = min(limit, backing_limit);
1135 }
1136 put_ldev(mdev);
1137 }
1138 return limit;
1139}