blob: bfed0834cd6ffcfe07b31c46072d17b6b245c667 [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();
Lars Ellenberg031a7c12012-01-21 16:50:25 +010040 part_round_stats(cpu, &mdev->vdisk->part0);
Philipp Reisnerb411b362009-09-25 16:07:19 -070041 part_stat_inc(cpu, &mdev->vdisk->part0, ios[rw]);
42 part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], bio_sectors(bio));
Philipp Reisner753c8912009-11-18 15:52:51 +010043 part_inc_in_flight(&mdev->vdisk->part0, rw);
Philipp Reisnerb411b362009-09-25 16:07:19 -070044 part_stat_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -070045}
46
47/* Update disk stats when completing request upwards */
48static void _drbd_end_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
49{
50 int rw = bio_data_dir(req->master_bio);
51 unsigned long duration = jiffies - req->start_time;
52 int cpu;
53 cpu = part_stat_lock();
54 part_stat_add(cpu, &mdev->vdisk->part0, ticks[rw], duration);
55 part_round_stats(cpu, &mdev->vdisk->part0);
Philipp Reisner753c8912009-11-18 15:52:51 +010056 part_dec_in_flight(&mdev->vdisk->part0, rw);
Philipp Reisnerb411b362009-09-25 16:07:19 -070057 part_stat_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -070058}
59
60static void _req_is_done(struct drbd_conf *mdev, struct drbd_request *req, const int rw)
61{
62 const unsigned long s = req->rq_state;
Philipp Reisner288f4222010-05-27 15:07:43 +020063
64 /* remove it from the transfer log.
65 * well, only if it had been there in the first
66 * place... if it had not (local only or conflicting
67 * and never sent), it should still be "empty" as
68 * initialized in drbd_req_new(), so we can list_del() it
69 * here unconditionally */
70 list_del(&req->tl_requests);
71
Philipp Reisnerb411b362009-09-25 16:07:19 -070072 /* if it was a write, we may have to set the corresponding
73 * bit(s) out-of-sync first. If it had a local part, we need to
74 * release the reference to the activity log. */
75 if (rw == WRITE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -070076 /* Set out-of-sync unless both OK flags are set
77 * (local only or remote failed).
78 * Other places where we set out-of-sync:
79 * READ with local io-error */
80 if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
81 drbd_set_out_of_sync(mdev, req->sector, req->size);
82
83 if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
84 drbd_set_in_sync(mdev, req->sector, req->size);
85
86 /* one might be tempted to move the drbd_al_complete_io
87 * to the local io completion callback drbd_endio_pri.
88 * but, if this was a mirror write, we may only
89 * drbd_al_complete_io after this is RQ_NET_DONE,
90 * otherwise the extent could be dropped from the al
91 * before it has actually been written on the peer.
92 * if we crash before our peer knows about the request,
93 * but after the extent has been dropped from the al,
94 * we would forget to resync the corresponding extent.
95 */
96 if (s & RQ_LOCAL_MASK) {
97 if (get_ldev_if_state(mdev, D_FAILED)) {
Philipp Reisner07782862010-08-31 12:00:50 +020098 if (s & RQ_IN_ACT_LOG)
99 drbd_al_complete_io(mdev, req->sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700100 put_ldev(mdev);
101 } else if (__ratelimit(&drbd_ratelimit_state)) {
102 dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu), "
103 "but my Disk seems to have failed :(\n",
104 (unsigned long long) req->sector);
105 }
106 }
107 }
108
Philipp Reisner32fa7e92010-05-26 17:13:18 +0200109 drbd_req_free(req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700110}
111
112static void queue_barrier(struct drbd_conf *mdev)
113{
114 struct drbd_tl_epoch *b;
115
116 /* We are within the req_lock. Once we queued the barrier for sending,
117 * we set the CREATE_BARRIER bit. It is cleared as soon as a new
118 * barrier/epoch object is added. This is the only place this bit is
119 * set. It indicates that the barrier for this epoch is already queued,
120 * and no new epoch has been created yet. */
121 if (test_bit(CREATE_BARRIER, &mdev->flags))
122 return;
123
124 b = mdev->newest_tle;
125 b->w.cb = w_send_barrier;
126 /* inc_ap_pending done here, so we won't
127 * get imbalanced on connection loss.
128 * dec_ap_pending will be done in got_BarrierAck
129 * or (on connection loss) in tl_clear. */
130 inc_ap_pending(mdev);
131 drbd_queue_work(&mdev->data.work, &b->w);
132 set_bit(CREATE_BARRIER, &mdev->flags);
133}
134
135static void _about_to_complete_local_write(struct drbd_conf *mdev,
136 struct drbd_request *req)
137{
138 const unsigned long s = req->rq_state;
139 struct drbd_request *i;
140 struct drbd_epoch_entry *e;
141 struct hlist_node *n;
142 struct hlist_head *slot;
143
Lars Ellenberg8a3c1042010-12-05 14:11:14 +0100144 /* Before we can signal completion to the upper layers,
145 * we may need to close the current epoch.
146 * We can skip this, if this request has not even been sent, because we
147 * did not have a fully established connection yet/anymore, during
148 * bitmap exchange, or while we are C_AHEAD due to congestion policy.
149 */
150 if (mdev->state.conn >= C_CONNECTED &&
151 (s & RQ_NET_SENT) != 0 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700152 req->epoch == mdev->newest_tle->br_number)
153 queue_barrier(mdev);
154
155 /* we need to do the conflict detection stuff,
156 * if we have the ee_hash (two_primaries) and
157 * this has been on the network */
158 if ((s & RQ_NET_DONE) && mdev->ee_hash != NULL) {
159 const sector_t sector = req->sector;
160 const int size = req->size;
161
162 /* ASSERT:
163 * there must be no conflicting requests, since
164 * they must have been failed on the spot */
165#define OVERLAPS overlaps(sector, size, i->sector, i->size)
166 slot = tl_hash_slot(mdev, sector);
Bart Van Assche24c48302011-05-21 18:32:29 +0200167 hlist_for_each_entry(i, n, slot, collision) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700168 if (OVERLAPS) {
169 dev_alert(DEV, "LOGIC BUG: completed: %p %llus +%u; "
170 "other: %p %llus +%u\n",
171 req, (unsigned long long)sector, size,
172 i, (unsigned long long)i->sector, i->size);
173 }
174 }
175
176 /* maybe "wake" those conflicting epoch entries
177 * that wait for this request to finish.
178 *
179 * currently, there can be only _one_ such ee
180 * (well, or some more, which would be pending
181 * P_DISCARD_ACK not yet sent by the asender...),
182 * since we block the receiver thread upon the
183 * first conflict detection, which will wait on
184 * misc_wait. maybe we want to assert that?
185 *
186 * anyways, if we found one,
187 * we just have to do a wake_up. */
188#undef OVERLAPS
189#define OVERLAPS overlaps(sector, size, e->sector, e->size)
190 slot = ee_hash_slot(mdev, req->sector);
Bart Van Assche24c48302011-05-21 18:32:29 +0200191 hlist_for_each_entry(e, n, slot, collision) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700192 if (OVERLAPS) {
193 wake_up(&mdev->misc_wait);
194 break;
195 }
196 }
197 }
198#undef OVERLAPS
199}
200
201void complete_master_bio(struct drbd_conf *mdev,
202 struct bio_and_error *m)
203{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700204 bio_endio(m->bio, m->error);
205 dec_ap_bio(mdev);
206}
207
208/* Helper for __req_mod().
209 * Set m->bio to the master bio, if it is fit to be completed,
210 * or leave it alone (it is initialized to NULL in __req_mod),
211 * if it has already been completed, or cannot be completed yet.
212 * If m->bio is set, the error status to be returned is placed in m->error.
213 */
214void _req_may_be_done(struct drbd_request *req, struct bio_and_error *m)
215{
216 const unsigned long s = req->rq_state;
217 struct drbd_conf *mdev = req->mdev;
Philipp Reisner2b4dd362011-03-14 13:01:50 +0100218 int rw = req->rq_state & RQ_WRITE ? WRITE : READ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700219
Philipp Reisnerb411b362009-09-25 16:07:19 -0700220 /* we must not complete the master bio, while it is
221 * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
222 * not yet acknowledged by the peer
223 * not yet completed by the local io subsystem
224 * these flags may get cleared in any order by
225 * the worker,
226 * the receiver,
227 * the bio_endio completion callbacks.
228 */
229 if (s & RQ_NET_QUEUED)
230 return;
231 if (s & RQ_NET_PENDING)
232 return;
Philipp Reisner2b4dd362011-03-14 13:01:50 +0100233 if (s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700234 return;
235
236 if (req->master_bio) {
237 /* this is data_received (remote read)
238 * or protocol C P_WRITE_ACK
239 * or protocol B P_RECV_ACK
240 * or protocol A "handed_over_to_network" (SendAck)
241 * or canceled or failed,
242 * or killed from the transfer log due to connection loss.
243 */
244
245 /*
246 * figure out whether to report success or failure.
247 *
248 * report success when at least one of the operations succeeded.
249 * or, to put the other way,
250 * only report failure, when both operations failed.
251 *
252 * what to do about the failures is handled elsewhere.
253 * what we need to do here is just: complete the master_bio.
254 *
255 * local completion error, if any, has been stored as ERR_PTR
256 * in private_bio within drbd_endio_pri.
257 */
258 int ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
259 int error = PTR_ERR(req->private_bio);
260
261 /* remove the request from the conflict detection
262 * respective block_id verification hash */
Bart Van Assche24c48302011-05-21 18:32:29 +0200263 if (!hlist_unhashed(&req->collision))
264 hlist_del(&req->collision);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700265 else
Philipp Reisner8825f7c2010-10-21 17:21:19 +0200266 D_ASSERT((s & (RQ_NET_MASK & ~RQ_NET_DONE)) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700267
268 /* for writes we need to do some extra housekeeping */
269 if (rw == WRITE)
270 _about_to_complete_local_write(mdev, req);
271
272 /* Update disk stats */
273 _drbd_end_io_acct(mdev, req);
274
275 m->error = ok ? 0 : (error ?: -EIO);
276 m->bio = req->master_bio;
277 req->master_bio = NULL;
278 }
279
Philipp Reisner2b4dd362011-03-14 13:01:50 +0100280 if (s & RQ_LOCAL_PENDING)
281 return;
282
Philipp Reisnerb411b362009-09-25 16:07:19 -0700283 if ((s & RQ_NET_MASK) == 0 || (s & RQ_NET_DONE)) {
284 /* this is disconnected (local only) operation,
285 * or protocol C P_WRITE_ACK,
286 * or protocol A or B P_BARRIER_ACK,
287 * or killed from the transfer log due to connection loss. */
288 _req_is_done(mdev, req, rw);
289 }
290 /* else: network part and not DONE yet. that is
291 * protocol A or B, barrier ack still pending... */
292}
293
Philipp Reisnercfa03412010-06-23 17:18:51 +0200294static void _req_may_be_done_not_susp(struct drbd_request *req, struct bio_and_error *m)
295{
296 struct drbd_conf *mdev = req->mdev;
297
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200298 if (!is_susp(mdev->state))
Philipp Reisnercfa03412010-06-23 17:18:51 +0200299 _req_may_be_done(req, m);
300}
301
Philipp Reisnerb411b362009-09-25 16:07:19 -0700302/*
303 * checks whether there was an overlapping request
304 * or ee already registered.
305 *
306 * if so, return 1, in which case this request is completed on the spot,
307 * without ever being submitted or send.
308 *
309 * return 0 if it is ok to submit this request.
310 *
311 * NOTE:
312 * paranoia: assume something above us is broken, and issues different write
313 * requests for the same block simultaneously...
314 *
315 * To ensure these won't be reordered differently on both nodes, resulting in
316 * diverging data sets, we discard the later one(s). Not that this is supposed
317 * to happen, but this is the rationale why we also have to check for
318 * conflicting requests with local origin, and why we have to do so regardless
319 * of whether we allowed multiple primaries.
320 *
321 * BTW, in case we only have one primary, the ee_hash is empty anyways, and the
322 * second hlist_for_each_entry becomes a noop. This is even simpler than to
323 * grab a reference on the net_conf, and check for the two_primaries flag...
324 */
325static int _req_conflicts(struct drbd_request *req)
326{
327 struct drbd_conf *mdev = req->mdev;
328 const sector_t sector = req->sector;
329 const int size = req->size;
330 struct drbd_request *i;
331 struct drbd_epoch_entry *e;
332 struct hlist_node *n;
333 struct hlist_head *slot;
334
Bart Van Assche24c48302011-05-21 18:32:29 +0200335 D_ASSERT(hlist_unhashed(&req->collision));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700336
337 if (!get_net_conf(mdev))
338 return 0;
339
340 /* BUG_ON */
341 ERR_IF (mdev->tl_hash_s == 0)
342 goto out_no_conflict;
343 BUG_ON(mdev->tl_hash == NULL);
344
345#define OVERLAPS overlaps(i->sector, i->size, sector, size)
346 slot = tl_hash_slot(mdev, sector);
Bart Van Assche24c48302011-05-21 18:32:29 +0200347 hlist_for_each_entry(i, n, slot, collision) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700348 if (OVERLAPS) {
349 dev_alert(DEV, "%s[%u] Concurrent local write detected! "
350 "[DISCARD L] new: %llus +%u; "
351 "pending: %llus +%u\n",
352 current->comm, current->pid,
353 (unsigned long long)sector, size,
354 (unsigned long long)i->sector, i->size);
355 goto out_conflict;
356 }
357 }
358
359 if (mdev->ee_hash_s) {
360 /* now, check for overlapping requests with remote origin */
361 BUG_ON(mdev->ee_hash == NULL);
362#undef OVERLAPS
363#define OVERLAPS overlaps(e->sector, e->size, sector, size)
364 slot = ee_hash_slot(mdev, sector);
Bart Van Assche24c48302011-05-21 18:32:29 +0200365 hlist_for_each_entry(e, n, slot, collision) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700366 if (OVERLAPS) {
367 dev_alert(DEV, "%s[%u] Concurrent remote write detected!"
368 " [DISCARD L] new: %llus +%u; "
369 "pending: %llus +%u\n",
370 current->comm, current->pid,
371 (unsigned long long)sector, size,
372 (unsigned long long)e->sector, e->size);
373 goto out_conflict;
374 }
375 }
376 }
377#undef OVERLAPS
378
379out_no_conflict:
380 /* this is like it should be, and what we expected.
381 * our users do behave after all... */
382 put_net_conf(mdev);
383 return 0;
384
385out_conflict:
386 put_net_conf(mdev);
387 return 1;
388}
389
390/* obviously this could be coded as many single functions
391 * instead of one huge switch,
392 * or by putting the code directly in the respective locations
393 * (as it has been before).
394 *
395 * but having it this way
396 * enforces that it is all in this one place, where it is easier to audit,
397 * it makes it obvious that whatever "event" "happens" to a request should
398 * happen "atomically" within the req_lock,
399 * and it enforces that we have to think in a very structured manner
400 * about the "events" that may happen to a request during its life time ...
401 */
Philipp Reisner2a806992010-06-09 14:07:43 +0200402int __req_mod(struct drbd_request *req, enum drbd_req_event what,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700403 struct bio_and_error *m)
404{
405 struct drbd_conf *mdev = req->mdev;
Philipp Reisner2a806992010-06-09 14:07:43 +0200406 int rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700407 m->bio = NULL;
408
Philipp Reisnerb411b362009-09-25 16:07:19 -0700409 switch (what) {
410 default:
411 dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
412 break;
413
414 /* does not happen...
415 * initialization done in drbd_req_new
416 case created:
417 break;
418 */
419
420 case to_be_send: /* via network */
421 /* reached via drbd_make_request_common
422 * and from w_read_retry_remote */
423 D_ASSERT(!(req->rq_state & RQ_NET_MASK));
424 req->rq_state |= RQ_NET_PENDING;
425 inc_ap_pending(mdev);
426 break;
427
428 case to_be_submitted: /* locally */
429 /* reached via drbd_make_request_common */
430 D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
431 req->rq_state |= RQ_LOCAL_PENDING;
432 break;
433
434 case completed_ok:
Philipp Reisner2b4dd362011-03-14 13:01:50 +0100435 if (req->rq_state & RQ_WRITE)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700436 mdev->writ_cnt += req->size>>9;
437 else
438 mdev->read_cnt += req->size>>9;
439
440 req->rq_state |= (RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
441 req->rq_state &= ~RQ_LOCAL_PENDING;
442
Philipp Reisnercfa03412010-06-23 17:18:51 +0200443 _req_may_be_done_not_susp(req, m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444 put_ldev(mdev);
445 break;
446
Philipp Reisner2b4dd362011-03-14 13:01:50 +0100447 case abort_disk_io:
448 req->rq_state |= RQ_LOCAL_ABORTED;
449 if (req->rq_state & RQ_WRITE)
450 _req_may_be_done_not_susp(req, m);
451 else
452 goto goto_queue_for_net_read;
453 break;
454
Philipp Reisnerb411b362009-09-25 16:07:19 -0700455 case write_completed_with_error:
456 req->rq_state |= RQ_LOCAL_COMPLETED;
457 req->rq_state &= ~RQ_LOCAL_PENDING;
458
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100459 __drbd_chk_io_error(mdev, false);
Philipp Reisnercfa03412010-06-23 17:18:51 +0200460 _req_may_be_done_not_susp(req, m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700461 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;
Philipp Reisnercfa03412010-06-23 17:18:51 +0200468 _req_may_be_done_not_susp(req, m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700469 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
Philipp Reisnerb411b362009-09-25 16:07:19 -0700478 D_ASSERT(!(req->rq_state & RQ_NET_MASK));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700479
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100480 __drbd_chk_io_error(mdev, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700481 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700482
Philipp Reisner2b4dd362011-03-14 13:01:50 +0100483 goto_queue_for_net_read:
484
Lars Ellenbergd255e5f2010-05-27 09:45:45 +0200485 /* no point in retrying if there is no good remote data,
486 * or we have no connection. */
487 if (mdev->state.pdsk != D_UP_TO_DATE) {
Philipp Reisnercfa03412010-06-23 17:18:51 +0200488 _req_may_be_done_not_susp(req, m);
Lars Ellenbergd255e5f2010-05-27 09:45:45 +0200489 break;
490 }
491
492 /* _req_mod(req,to_be_send); oops, recursion... */
493 req->rq_state |= RQ_NET_PENDING;
494 inc_ap_pending(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700495 /* fall through: _req_mod(req,queue_for_net_read); */
496
497 case queue_for_net_read:
498 /* READ or READA, and
499 * no local disk,
500 * or target area marked as invalid,
501 * or just got an io-error. */
502 /* from drbd_make_request_common
503 * or from bio_endio during read io-error recovery */
504
505 /* so we can verify the handle in the answer packet
506 * corresponding hlist_del is in _req_may_be_done() */
Bart Van Assche24c48302011-05-21 18:32:29 +0200507 hlist_add_head(&req->collision, ar_hash_slot(mdev, req->sector));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700508
Lars Ellenberg83c38832009-11-03 02:22:06 +0100509 set_bit(UNPLUG_REMOTE, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700510
511 D_ASSERT(req->rq_state & RQ_NET_PENDING);
512 req->rq_state |= RQ_NET_QUEUED;
513 req->w.cb = (req->rq_state & RQ_LOCAL_MASK)
514 ? w_read_retry_remote
515 : w_send_read_req;
516 drbd_queue_work(&mdev->data.work, &req->w);
517 break;
518
519 case queue_for_net_write:
520 /* assert something? */
521 /* from drbd_make_request_common only */
522
Bart Van Assche24c48302011-05-21 18:32:29 +0200523 hlist_add_head(&req->collision, tl_hash_slot(mdev, req->sector));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700524 /* corresponding hlist_del is in _req_may_be_done() */
525
526 /* NOTE
527 * In case the req ended up on the transfer log before being
528 * queued on the worker, it could lead to this request being
529 * missed during cleanup after connection loss.
530 * So we have to do both operations here,
531 * within the same lock that protects the transfer log.
532 *
533 * _req_add_to_epoch(req); this has to be after the
534 * _maybe_start_new_epoch(req); which happened in
535 * drbd_make_request_common, because we now may set the bit
536 * again ourselves to close the current epoch.
537 *
538 * Add req to the (now) current epoch (barrier). */
539
Lars Ellenberg83c38832009-11-03 02:22:06 +0100540 /* otherwise we may lose an unplug, which may cause some remote
541 * io-scheduler timeout to expire, increasing maximum latency,
542 * hurting performance. */
543 set_bit(UNPLUG_REMOTE, &mdev->flags);
544
Philipp Reisnerb411b362009-09-25 16:07:19 -0700545 /* see drbd_make_request_common,
546 * just after it grabs the req_lock */
547 D_ASSERT(test_bit(CREATE_BARRIER, &mdev->flags) == 0);
548
549 req->epoch = mdev->newest_tle->br_number;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700550
551 /* increment size of current epoch */
Philipp Reisner7e602c02010-05-27 14:49:27 +0200552 mdev->newest_tle->n_writes++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700553
554 /* queue work item to send data */
555 D_ASSERT(req->rq_state & RQ_NET_PENDING);
556 req->rq_state |= RQ_NET_QUEUED;
557 req->w.cb = w_send_dblock;
558 drbd_queue_work(&mdev->data.work, &req->w);
559
560 /* close the epoch, in case it outgrew the limit */
Philipp Reisner7e602c02010-05-27 14:49:27 +0200561 if (mdev->newest_tle->n_writes >= mdev->net_conf->max_epoch_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700562 queue_barrier(mdev);
563
564 break;
565
Philipp Reisner73a01a12010-10-27 14:33:00 +0200566 case queue_for_send_oos:
567 req->rq_state |= RQ_NET_QUEUED;
568 req->w.cb = w_send_oos;
569 drbd_queue_work(&mdev->data.work, &req->w);
570 break;
571
Philipp Reisnerb411b362009-09-25 16:07:19 -0700572 case send_canceled:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573 case send_failed:
574 /* real cleanup will be done from tl_clear. just update flags
575 * so it is no longer marked as on the worker queue */
576 req->rq_state &= ~RQ_NET_QUEUED;
577 /* if we did it right, tl_clear should be scheduled only after
578 * this, so this should not be necessary! */
Philipp Reisnercfa03412010-06-23 17:18:51 +0200579 _req_may_be_done_not_susp(req, m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700580 break;
581
582 case handed_over_to_network:
583 /* assert something? */
Philipp Reisner759fbdf2010-10-26 16:02:27 +0200584 if (bio_data_dir(req->master_bio) == WRITE)
585 atomic_add(req->size>>9, &mdev->ap_in_flight);
586
Philipp Reisnerb411b362009-09-25 16:07:19 -0700587 if (bio_data_dir(req->master_bio) == WRITE &&
588 mdev->net_conf->wire_protocol == DRBD_PROT_A) {
589 /* this is what is dangerous about protocol A:
590 * pretend it was successfully written on the peer. */
591 if (req->rq_state & RQ_NET_PENDING) {
592 dec_ap_pending(mdev);
593 req->rq_state &= ~RQ_NET_PENDING;
594 req->rq_state |= RQ_NET_OK;
595 } /* else: neg-ack was faster... */
596 /* it is still not yet RQ_NET_DONE until the
597 * corresponding epoch barrier got acked as well,
598 * so we know what to dirty on connection loss */
599 }
600 req->rq_state &= ~RQ_NET_QUEUED;
601 req->rq_state |= RQ_NET_SENT;
Lars Ellenberg6d49e102012-01-11 09:43:25 +0100602 _req_may_be_done_not_susp(req, m);
603 break;
604
605 case oos_handed_to_network:
606 /* Was not set PENDING, no longer QUEUED, so is now DONE
607 * as far as this connection is concerned. */
608 req->rq_state &= ~RQ_NET_QUEUED;
609 req->rq_state |= RQ_NET_DONE;
Philipp Reisnercfa03412010-06-23 17:18:51 +0200610 _req_may_be_done_not_susp(req, m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700611 break;
612
Lars Ellenbergd255e5f2010-05-27 09:45:45 +0200613 case read_retry_remote_canceled:
614 req->rq_state &= ~RQ_NET_QUEUED;
615 /* fall through, in case we raced with drbd_disconnect */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700616 case connection_lost_while_pending:
617 /* transfer log cleanup after connection loss */
618 /* assert something? */
619 if (req->rq_state & RQ_NET_PENDING)
620 dec_ap_pending(mdev);
621 req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
622 req->rq_state |= RQ_NET_DONE;
Philipp Reisner759fbdf2010-10-26 16:02:27 +0200623 if (req->rq_state & RQ_NET_SENT && req->rq_state & RQ_WRITE)
624 atomic_sub(req->size>>9, &mdev->ap_in_flight);
625
Philipp Reisnerb411b362009-09-25 16:07:19 -0700626 /* if it is still queued, we may not complete it here.
627 * it will be canceled soon. */
628 if (!(req->rq_state & RQ_NET_QUEUED))
Philipp Reisnercfa03412010-06-23 17:18:51 +0200629 _req_may_be_done(req, m); /* Allowed while state.susp */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700630 break;
631
632 case write_acked_by_peer_and_sis:
633 req->rq_state |= RQ_NET_SIS;
634 case conflict_discarded_by_peer:
635 /* for discarded conflicting writes of multiple primaries,
636 * there is no need to keep anything in the tl, potential
637 * node crashes are covered by the activity log. */
638 if (what == conflict_discarded_by_peer)
639 dev_alert(DEV, "Got DiscardAck packet %llus +%u!"
640 " DRBD is not a random data generator!\n",
641 (unsigned long long)req->sector, req->size);
642 req->rq_state |= RQ_NET_DONE;
643 /* fall through */
644 case write_acked_by_peer:
645 /* protocol C; successfully written on peer.
646 * Nothing to do here.
647 * We want to keep the tl in place for all protocols, to cater
648 * for volatile write-back caches on lower level devices.
649 *
650 * A barrier request is expected to have forced all prior
651 * requests onto stable storage, so completion of a barrier
652 * request could set NET_DONE right here, and not wait for the
653 * P_BARRIER_ACK, but that is an unnecessary optimization. */
654
655 /* this makes it effectively the same as for: */
656 case recv_acked_by_peer:
657 /* protocol B; pretends to be successfully written on peer.
658 * see also notes above in handed_over_to_network about
659 * protocol != C */
660 req->rq_state |= RQ_NET_OK;
661 D_ASSERT(req->rq_state & RQ_NET_PENDING);
662 dec_ap_pending(mdev);
Philipp Reisner759fbdf2010-10-26 16:02:27 +0200663 atomic_sub(req->size>>9, &mdev->ap_in_flight);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700664 req->rq_state &= ~RQ_NET_PENDING;
Philipp Reisnercfa03412010-06-23 17:18:51 +0200665 _req_may_be_done_not_susp(req, m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 break;
667
668 case neg_acked:
669 /* assert something? */
Philipp Reisner759fbdf2010-10-26 16:02:27 +0200670 if (req->rq_state & RQ_NET_PENDING) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700671 dec_ap_pending(mdev);
Philipp Reisner759fbdf2010-10-26 16:02:27 +0200672 atomic_sub(req->size>>9, &mdev->ap_in_flight);
673 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700674 req->rq_state &= ~(RQ_NET_OK|RQ_NET_PENDING);
675
676 req->rq_state |= RQ_NET_DONE;
Philipp Reisnercfa03412010-06-23 17:18:51 +0200677 _req_may_be_done_not_susp(req, m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700678 /* else: done by handed_over_to_network */
679 break;
680
Philipp Reisner265be2d2010-05-31 10:14:17 +0200681 case fail_frozen_disk_io:
682 if (!(req->rq_state & RQ_LOCAL_COMPLETED))
683 break;
684
Philipp Reisnercfa03412010-06-23 17:18:51 +0200685 _req_may_be_done(req, m); /* Allowed while state.susp */
Philipp Reisner265be2d2010-05-31 10:14:17 +0200686 break;
687
688 case restart_frozen_disk_io:
689 if (!(req->rq_state & RQ_LOCAL_COMPLETED))
690 break;
691
692 req->rq_state &= ~RQ_LOCAL_COMPLETED;
693
694 rv = MR_READ;
695 if (bio_data_dir(req->master_bio) == WRITE)
696 rv = MR_WRITE;
697
698 get_ldev(mdev);
699 req->w.cb = w_restart_disk_io;
700 drbd_queue_work(&mdev->data.work, &req->w);
701 break;
702
Philipp Reisner11b58e72010-05-12 17:08:26 +0200703 case resend:
704 /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
Philipp Reisner47ff2d02010-06-18 13:56:57 +0200705 before the connection loss (B&C only); only P_BARRIER_ACK was missing.
Philipp Reisner11b58e72010-05-12 17:08:26 +0200706 Trowing them out of the TL here by pretending we got a BARRIER_ACK
Philipp Reisner481c6f52010-06-22 14:03:27 +0200707 We ensure that the peer was not rebooted */
Philipp Reisner11b58e72010-05-12 17:08:26 +0200708 if (!(req->rq_state & RQ_NET_OK)) {
709 if (req->w.cb) {
710 drbd_queue_work(&mdev->data.work, &req->w);
711 rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
712 }
713 break;
714 }
715 /* else, fall through to barrier_acked */
716
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717 case barrier_acked:
Philipp Reisner288f4222010-05-27 15:07:43 +0200718 if (!(req->rq_state & RQ_WRITE))
719 break;
720
Philipp Reisnerb411b362009-09-25 16:07:19 -0700721 if (req->rq_state & RQ_NET_PENDING) {
722 /* barrier came in before all requests have been acked.
723 * this is bad, because if the connection is lost now,
724 * we won't be able to clean them up... */
725 dev_err(DEV, "FIXME (barrier_acked but pending)\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700726 list_move(&req->tl_requests, &mdev->out_of_sequence_requests);
727 }
Lars Ellenberge636db52011-01-21 17:10:37 +0100728 if ((req->rq_state & RQ_NET_MASK) != 0) {
729 req->rq_state |= RQ_NET_DONE;
730 if (mdev->net_conf->wire_protocol == DRBD_PROT_A)
731 atomic_sub(req->size>>9, &mdev->ap_in_flight);
732 }
Philipp Reisnercfa03412010-06-23 17:18:51 +0200733 _req_may_be_done(req, m); /* Allowed while state.susp */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700734 break;
735
736 case data_received:
737 D_ASSERT(req->rq_state & RQ_NET_PENDING);
738 dec_ap_pending(mdev);
739 req->rq_state &= ~RQ_NET_PENDING;
740 req->rq_state |= (RQ_NET_OK|RQ_NET_DONE);
Philipp Reisnercfa03412010-06-23 17:18:51 +0200741 _req_may_be_done_not_susp(req, m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700742 break;
743 };
Philipp Reisner2a806992010-06-09 14:07:43 +0200744
745 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700746}
747
748/* we may do a local read if:
749 * - we are consistent (of course),
750 * - or we are generally inconsistent,
751 * BUT we are still/already IN SYNC for this area.
752 * since size may be bigger than BM_BLOCK_SIZE,
753 * we may need to check several bits.
754 */
755static int drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
756{
757 unsigned long sbnr, ebnr;
758 sector_t esector, nr_sectors;
759
760 if (mdev->state.disk == D_UP_TO_DATE)
761 return 1;
762 if (mdev->state.disk >= D_OUTDATED)
763 return 0;
764 if (mdev->state.disk < D_INCONSISTENT)
765 return 0;
766 /* state.disk == D_INCONSISTENT We will have a look at the BitMap */
767 nr_sectors = drbd_get_capacity(mdev->this_bdev);
768 esector = sector + (size >> 9) - 1;
769
770 D_ASSERT(sector < nr_sectors);
771 D_ASSERT(esector < nr_sectors);
772
773 sbnr = BM_SECT_TO_BIT(sector);
774 ebnr = BM_SECT_TO_BIT(esector);
775
776 return 0 == drbd_bm_count_bits(mdev, sbnr, ebnr);
777}
778
Philipp Reisneraeda1cd62010-11-09 17:45:06 +0100779static int drbd_make_request_common(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780{
781 const int rw = bio_rw(bio);
782 const int size = bio->bi_size;
783 const sector_t sector = bio->bi_sector;
784 struct drbd_tl_epoch *b = NULL;
785 struct drbd_request *req;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200786 int local, remote, send_oos = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700787 int err = -EIO;
Philipp Reisner9a25a042010-05-10 16:42:23 +0200788 int ret = 0;
Philipp Reisnerfc288452012-01-16 12:14:01 +0100789 union drbd_state s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700790
791 /* allocate outside of all locks; */
792 req = drbd_req_new(mdev, bio);
793 if (!req) {
794 dec_ap_bio(mdev);
795 /* only pass the error to the upper layers.
796 * if user cannot handle io errors, that's not our business. */
797 dev_err(DEV, "could not kmalloc() req\n");
798 bio_endio(bio, -ENOMEM);
799 return 0;
800 }
Philipp Reisneraeda1cd62010-11-09 17:45:06 +0100801 req->start_time = start_time;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700802
Philipp Reisnerb411b362009-09-25 16:07:19 -0700803 local = get_ldev(mdev);
804 if (!local) {
805 bio_put(req->private_bio); /* or we get a bio leak */
806 req->private_bio = NULL;
807 }
808 if (rw == WRITE) {
809 remote = 1;
810 } else {
811 /* READ || READA */
812 if (local) {
813 if (!drbd_may_do_local_read(mdev, sector, size)) {
814 /* we could kick the syncer to
815 * sync this extent asap, wait for
816 * it, then continue locally.
817 * Or just issue the request remotely.
818 */
819 local = 0;
820 bio_put(req->private_bio);
821 req->private_bio = NULL;
822 put_ldev(mdev);
823 }
824 }
825 remote = !local && mdev->state.pdsk >= D_UP_TO_DATE;
826 }
827
828 /* If we have a disk, but a READA request is mapped to remote,
829 * we are R_PRIMARY, D_INCONSISTENT, SyncTarget.
830 * Just fail that READA request right here.
831 *
832 * THINK: maybe fail all READA when not local?
833 * or make this configurable...
834 * if network is slow, READA won't do any good.
835 */
836 if (rw == READA && mdev->state.disk >= D_INCONSISTENT && !local) {
837 err = -EWOULDBLOCK;
838 goto fail_and_free_req;
839 }
840
841 /* For WRITES going to the local disk, grab a reference on the target
842 * extent. This waits for any resync activity in the corresponding
843 * resync extent to finish, and, if necessary, pulls in the target
844 * extent into the activity log, which involves further disk io because
845 * of transactional on-disk meta data updates. */
Philipp Reisner07782862010-08-31 12:00:50 +0200846 if (rw == WRITE && local && !test_bit(AL_SUSPENDED, &mdev->flags)) {
847 req->rq_state |= RQ_IN_ACT_LOG;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700848 drbd_al_begin_io(mdev, sector);
Philipp Reisner07782862010-08-31 12:00:50 +0200849 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700850
Philipp Reisnerfc288452012-01-16 12:14:01 +0100851 s = mdev->state;
852 remote = remote && drbd_should_do_remote(s);
853 send_oos = rw == WRITE && drbd_should_send_oos(s);
Philipp Reisner37190942010-11-10 12:08:37 +0100854 D_ASSERT(!(remote && send_oos));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700855
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200856 if (!(local || remote) && !is_susp(mdev->state)) {
Lars Ellenbergfb2c7a12010-10-19 12:08:13 +0200857 if (__ratelimit(&drbd_ratelimit_state))
858 dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700859 goto fail_free_complete;
860 }
861
862 /* For WRITE request, we have to make sure that we have an
863 * unused_spare_tle, in case we need to start a new epoch.
864 * I try to be smart and avoid to pre-allocate always "just in case",
865 * but there is a race between testing the bit and pointer outside the
866 * spinlock, and grabbing the spinlock.
867 * if we lost that race, we retry. */
Philipp Reisner73a01a12010-10-27 14:33:00 +0200868 if (rw == WRITE && (remote || send_oos) &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700869 mdev->unused_spare_tle == NULL &&
870 test_bit(CREATE_BARRIER, &mdev->flags)) {
871allocate_barrier:
872 b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_NOIO);
873 if (!b) {
874 dev_err(DEV, "Failed to alloc barrier.\n");
875 err = -ENOMEM;
876 goto fail_free_complete;
877 }
878 }
879
880 /* GOOD, everything prepared, grab the spin_lock */
881 spin_lock_irq(&mdev->req_lock);
882
Philipp Reisnerfb22c402010-09-08 23:20:21 +0200883 if (is_susp(mdev->state)) {
Philipp Reisner9a25a042010-05-10 16:42:23 +0200884 /* If we got suspended, use the retry mechanism of
885 generic_make_request() to restart processing of this
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +0100886 bio. In the next call to drbd_make_request
Philipp Reisner9a25a042010-05-10 16:42:23 +0200887 we sleep in inc_ap_bio() */
888 ret = 1;
889 spin_unlock_irq(&mdev->req_lock);
890 goto fail_free_complete;
891 }
892
Philipp Reisner73a01a12010-10-27 14:33:00 +0200893 if (remote || send_oos) {
Philipp Reisner6a35c452011-01-17 20:27:30 +0100894 remote = drbd_should_do_remote(mdev->state);
895 send_oos = rw == WRITE && drbd_should_send_oos(mdev->state);
Philipp Reisner37190942010-11-10 12:08:37 +0100896 D_ASSERT(!(remote && send_oos));
Philipp Reisner73a01a12010-10-27 14:33:00 +0200897
898 if (!(remote || send_oos))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700899 dev_warn(DEV, "lost connection while grabbing the req_lock!\n");
900 if (!(local || remote)) {
901 dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
902 spin_unlock_irq(&mdev->req_lock);
903 goto fail_free_complete;
904 }
905 }
906
907 if (b && mdev->unused_spare_tle == NULL) {
908 mdev->unused_spare_tle = b;
909 b = NULL;
910 }
Philipp Reisner73a01a12010-10-27 14:33:00 +0200911 if (rw == WRITE && (remote || send_oos) &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700912 mdev->unused_spare_tle == NULL &&
913 test_bit(CREATE_BARRIER, &mdev->flags)) {
914 /* someone closed the current epoch
915 * while we were grabbing the spinlock */
916 spin_unlock_irq(&mdev->req_lock);
917 goto allocate_barrier;
918 }
919
920
921 /* Update disk stats */
922 _drbd_start_io_acct(mdev, req, bio);
923
924 /* _maybe_start_new_epoch(mdev);
925 * If we need to generate a write barrier packet, we have to add the
926 * new epoch (barrier) object, and queue the barrier packet for sending,
927 * and queue the req's data after it _within the same lock_, otherwise
928 * we have race conditions were the reorder domains could be mixed up.
929 *
930 * Even read requests may start a new epoch and queue the corresponding
931 * barrier packet. To get the write ordering right, we only have to
932 * make sure that, if this is a write request and it triggered a
933 * barrier packet, this request is queued within the same spinlock. */
Philipp Reisner73a01a12010-10-27 14:33:00 +0200934 if ((remote || send_oos) && mdev->unused_spare_tle &&
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935 test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
936 _tl_add_barrier(mdev, mdev->unused_spare_tle);
937 mdev->unused_spare_tle = NULL;
938 } else {
939 D_ASSERT(!(remote && rw == WRITE &&
940 test_bit(CREATE_BARRIER, &mdev->flags)));
941 }
942
943 /* NOTE
944 * Actually, 'local' may be wrong here already, since we may have failed
945 * to write to the meta data, and may become wrong anytime because of
946 * local io-error for some other request, which would lead to us
947 * "detaching" the local disk.
948 *
949 * 'remote' may become wrong any time because the network could fail.
950 *
951 * This is a harmless race condition, though, since it is handled
952 * correctly at the appropriate places; so it just defers the failure
953 * of the respective operation.
954 */
955
956 /* mark them early for readability.
957 * this just sets some state flags. */
958 if (remote)
959 _req_mod(req, to_be_send);
960 if (local)
961 _req_mod(req, to_be_submitted);
962
963 /* check this request on the collision detection hash tables.
964 * if we have a conflict, just complete it here.
965 * THINK do we want to check reads, too? (I don't think so...) */
Lars Ellenbergd28fd092010-07-09 23:28:10 +0200966 if (rw == WRITE && _req_conflicts(req))
967 goto fail_conflicting;
Philipp Reisner288f4222010-05-27 15:07:43 +0200968
969 list_add_tail(&req->tl_requests, &mdev->newest_tle->requests);
970
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971 /* NOTE remote first: to get the concurrent write detection right,
972 * we must register the request before start of local IO. */
973 if (remote) {
974 /* either WRITE and C_CONNECTED,
975 * or READ, and no local disk,
976 * or READ, but not in sync.
977 */
978 _req_mod(req, (rw == WRITE)
979 ? queue_for_net_write
980 : queue_for_net_read);
981 }
Philipp Reisner73a01a12010-10-27 14:33:00 +0200982 if (send_oos && drbd_set_out_of_sync(mdev, sector, size))
983 _req_mod(req, queue_for_send_oos);
Philipp Reisner67531712010-10-27 12:21:30 +0200984
Philipp Reisner73a01a12010-10-27 14:33:00 +0200985 if (remote &&
986 mdev->net_conf->on_congestion != OC_BLOCK && mdev->agreed_pro_version >= 96) {
Philipp Reisner67531712010-10-27 12:21:30 +0200987 int congested = 0;
988
989 if (mdev->net_conf->cong_fill &&
990 atomic_read(&mdev->ap_in_flight) >= mdev->net_conf->cong_fill) {
991 dev_info(DEV, "Congestion-fill threshold reached\n");
992 congested = 1;
993 }
994
995 if (mdev->act_log->used >= mdev->net_conf->cong_extents) {
996 dev_info(DEV, "Congestion-extents threshold reached\n");
997 congested = 1;
998 }
999
Philipp Reisner71c78cf2011-01-14 19:20:34 +01001000 if (congested) {
Philipp Reisner039312b2011-01-21 14:13:22 +01001001 queue_barrier(mdev); /* last barrier, after mirrored writes */
Philipp Reisner73a01a12010-10-27 14:33:00 +02001002
Philipp Reisner67531712010-10-27 12:21:30 +02001003 if (mdev->net_conf->on_congestion == OC_PULL_AHEAD)
1004 _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
1005 else /*mdev->net_conf->on_congestion == OC_DISCONNECT */
1006 _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
1007 }
1008 }
1009
Philipp Reisnerb411b362009-09-25 16:07:19 -07001010 spin_unlock_irq(&mdev->req_lock);
1011 kfree(b); /* if someone else has beaten us to it... */
1012
1013 if (local) {
1014 req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
1015
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001016 /* State may have changed since we grabbed our reference on the
1017 * mdev->ldev member. Double check, and short-circuit to endio.
1018 * In case the last activity log transaction failed to get on
1019 * stable storage, and this is a WRITE, we may not even submit
1020 * this bio. */
1021 if (get_ldev(mdev)) {
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001022 if (drbd_insert_fault(mdev, rw == WRITE ? DRBD_FAULT_DT_WR
1023 : rw == READ ? DRBD_FAULT_DT_RD
1024 : DRBD_FAULT_DT_RA))
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001025 bio_endio(req->private_bio, -EIO);
1026 else
1027 generic_make_request(req->private_bio);
1028 put_ldev(mdev);
1029 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07001030 bio_endio(req->private_bio, -EIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031 }
1032
Philipp Reisnerb411b362009-09-25 16:07:19 -07001033 return 0;
1034
Lars Ellenbergd28fd092010-07-09 23:28:10 +02001035fail_conflicting:
1036 /* this is a conflicting request.
1037 * even though it may have been only _partially_
1038 * overlapping with one of the currently pending requests,
1039 * without even submitting or sending it, we will
1040 * pretend that it was successfully served right now.
1041 */
1042 _drbd_end_io_acct(mdev, req);
1043 spin_unlock_irq(&mdev->req_lock);
1044 if (remote)
1045 dec_ap_pending(mdev);
1046 /* THINK: do we want to fail it (-EIO), or pretend success?
1047 * this pretends success. */
1048 err = 0;
1049
Philipp Reisnerb411b362009-09-25 16:07:19 -07001050fail_free_complete:
Lars Ellenberg76727f62011-05-16 15:31:45 +02001051 if (req->rq_state & RQ_IN_ACT_LOG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001052 drbd_al_complete_io(mdev, sector);
1053fail_and_free_req:
1054 if (local) {
1055 bio_put(req->private_bio);
1056 req->private_bio = NULL;
1057 put_ldev(mdev);
1058 }
Philipp Reisner9a25a042010-05-10 16:42:23 +02001059 if (!ret)
1060 bio_endio(bio, err);
1061
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062 drbd_req_free(req);
1063 dec_ap_bio(mdev);
1064 kfree(b);
1065
Philipp Reisner9a25a042010-05-10 16:42:23 +02001066 return ret;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001067}
1068
1069/* helper function for drbd_make_request
1070 * if we can determine just by the mdev (state) that this request will fail,
1071 * return 1
1072 * otherwise return 0
1073 */
1074static int drbd_fail_request_early(struct drbd_conf *mdev, int is_write)
1075{
Philipp Reisnerb411b362009-09-25 16:07:19 -07001076 if (mdev->state.role != R_PRIMARY &&
1077 (!allow_oos || is_write)) {
1078 if (__ratelimit(&drbd_ratelimit_state)) {
1079 dev_err(DEV, "Process %s[%u] tried to %s; "
1080 "since we are not in Primary state, "
1081 "we cannot allow this\n",
1082 current->comm, current->pid,
1083 is_write ? "WRITE" : "READ");
1084 }
1085 return 1;
1086 }
1087
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088 return 0;
1089}
1090
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001091void drbd_make_request(struct request_queue *q, struct bio *bio)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001092{
1093 unsigned int s_enr, e_enr;
1094 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
Philipp Reisneraeda1cd62010-11-09 17:45:06 +01001095 unsigned long start_time;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001096
1097 if (drbd_fail_request_early(mdev, bio_data_dir(bio) & WRITE)) {
1098 bio_endio(bio, -EPERM);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001099 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001100 }
1101
Philipp Reisneraeda1cd62010-11-09 17:45:06 +01001102 start_time = jiffies;
1103
Philipp Reisnerb411b362009-09-25 16:07:19 -07001104 /*
1105 * what we "blindly" assume:
1106 */
1107 D_ASSERT(bio->bi_size > 0);
1108 D_ASSERT((bio->bi_size & 0x1ff) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001109
1110 /* to make some things easier, force alignment of requests within the
1111 * granularity of our hash tables */
1112 s_enr = bio->bi_sector >> HT_SHIFT;
1113 e_enr = (bio->bi_sector+(bio->bi_size>>9)-1) >> HT_SHIFT;
1114
1115 if (likely(s_enr == e_enr)) {
1116 inc_ap_bio(mdev, 1);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001117 drbd_make_request_common(mdev, bio, start_time);
1118 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001119 }
1120
1121 /* can this bio be split generically?
1122 * Maybe add our own split-arbitrary-bios function. */
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001123 if (bio->bi_vcnt != 1 || bio->bi_idx != 0 || bio->bi_size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001124 /* rather error out here than BUG in bio_split */
1125 dev_err(DEV, "bio would need to, but cannot, be split: "
1126 "(vcnt=%u,idx=%u,size=%u,sector=%llu)\n",
1127 bio->bi_vcnt, bio->bi_idx, bio->bi_size,
1128 (unsigned long long)bio->bi_sector);
1129 bio_endio(bio, -EINVAL);
1130 } else {
1131 /* This bio crosses some boundary, so we have to split it. */
1132 struct bio_pair *bp;
1133 /* works for the "do not cross hash slot boundaries" case
1134 * e.g. sector 262269, size 4096
1135 * s_enr = 262269 >> 6 = 4097
1136 * e_enr = (262269+8-1) >> 6 = 4098
1137 * HT_SHIFT = 6
1138 * sps = 64, mask = 63
1139 * first_sectors = 64 - (262269 & 63) = 3
1140 */
1141 const sector_t sect = bio->bi_sector;
1142 const int sps = 1 << HT_SHIFT; /* sectors per slot */
1143 const int mask = sps - 1;
1144 const sector_t first_sectors = sps - (sect & mask);
Or Gerlitz03567812011-01-13 10:43:40 +01001145 bp = bio_split(bio, first_sectors);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001146
1147 /* we need to get a "reference count" (ap_bio_cnt)
1148 * to avoid races with the disconnect/reconnect/suspend code.
Philipp Reisner9a25a042010-05-10 16:42:23 +02001149 * In case we need to split the bio here, we need to get three references
Philipp Reisnerb411b362009-09-25 16:07:19 -07001150 * atomically, otherwise we might deadlock when trying to submit the
1151 * second one! */
Philipp Reisner9a25a042010-05-10 16:42:23 +02001152 inc_ap_bio(mdev, 3);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001153
1154 D_ASSERT(e_enr == s_enr + 1);
1155
Philipp Reisneraeda1cd62010-11-09 17:45:06 +01001156 while (drbd_make_request_common(mdev, &bp->bio1, start_time))
Philipp Reisner9a25a042010-05-10 16:42:23 +02001157 inc_ap_bio(mdev, 1);
1158
Philipp Reisneraeda1cd62010-11-09 17:45:06 +01001159 while (drbd_make_request_common(mdev, &bp->bio2, start_time))
Philipp Reisner9a25a042010-05-10 16:42:23 +02001160 inc_ap_bio(mdev, 1);
1161
1162 dec_ap_bio(mdev);
1163
Philipp Reisnerb411b362009-09-25 16:07:19 -07001164 bio_pair_release(bp);
1165 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001166}
1167
1168/* This is called by bio_add_page(). With this function we reduce
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001169 * the number of BIOs that span over multiple DRBD_MAX_BIO_SIZEs
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170 * units (was AL_EXTENTs).
1171 *
1172 * we do the calculation within the lower 32bit of the byte offsets,
1173 * since we don't care for actual offset, but only check whether it
1174 * would cross "activity log extent" boundaries.
1175 *
1176 * As long as the BIO is empty we have to allow at least one bvec,
1177 * regardless of size and offset. so the resulting bio may still
1178 * cross extent boundaries. those are dealt with (bio_split) in
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +01001179 * drbd_make_request.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180 */
1181int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
1182{
1183 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
1184 unsigned int bio_offset =
1185 (unsigned int)bvm->bi_sector << 9; /* 32 bit */
1186 unsigned int bio_size = bvm->bi_size;
1187 int limit, backing_limit;
1188
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001189 limit = DRBD_MAX_BIO_SIZE
1190 - ((bio_offset & (DRBD_MAX_BIO_SIZE-1)) + bio_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001191 if (limit < 0)
1192 limit = 0;
1193 if (bio_size == 0) {
1194 if (limit <= bvec->bv_len)
1195 limit = bvec->bv_len;
1196 } else if (limit && get_ldev(mdev)) {
1197 struct request_queue * const b =
1198 mdev->ldev->backing_bdev->bd_disk->queue;
Lars Ellenberga1c88d02010-05-14 19:16:41 +02001199 if (b->merge_bvec_fn) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001200 backing_limit = b->merge_bvec_fn(b, bvm, bvec);
1201 limit = min(limit, backing_limit);
1202 }
1203 put_ldev(mdev);
1204 }
1205 return limit;
1206}
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001207
1208void request_timer_fn(unsigned long data)
1209{
1210 struct drbd_conf *mdev = (struct drbd_conf *) data;
1211 struct drbd_request *req; /* oldest request */
1212 struct list_head *le;
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001213 unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001214
1215 if (get_net_conf(mdev)) {
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001216 ent = mdev->net_conf->timeout*HZ/10 * mdev->net_conf->ko_count;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001217 put_net_conf(mdev);
1218 }
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001219 if (get_ldev(mdev)) {
1220 dt = mdev->ldev->dc.disk_timeout * HZ / 10;
1221 put_ldev(mdev);
1222 }
1223 et = min_not_zero(dt, ent);
1224
1225 if (!et || (mdev->state.conn < C_WF_REPORT_PARAMS && mdev->state.disk <= D_FAILED))
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001226 return; /* Recurring timer stopped */
1227
1228 spin_lock_irq(&mdev->req_lock);
1229 le = &mdev->oldest_tle->requests;
1230 if (list_empty(le)) {
1231 spin_unlock_irq(&mdev->req_lock);
1232 mod_timer(&mdev->request_timer, jiffies + et);
1233 return;
1234 }
1235
1236 le = le->prev;
1237 req = list_entry(le, struct drbd_request, tl_requests);
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001238 if (ent && req->rq_state & RQ_NET_PENDING) {
1239 if (time_is_before_eq_jiffies(req->start_time + ent)) {
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001240 dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001241 _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001242 }
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001243 }
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001244 if (dt && req->rq_state & RQ_LOCAL_PENDING) {
1245 if (time_is_before_eq_jiffies(req->start_time + dt)) {
1246 dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
1247 __drbd_chk_io_error(mdev, 1);
1248 }
1249 }
1250 nt = (time_is_before_eq_jiffies(req->start_time + et) ? jiffies : req->start_time) + et;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001251 spin_unlock_irq(&mdev->req_lock);
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001252 mod_timer(&mdev->request_timer, nt);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001253}