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