blob: 6e8d0976351066db031f146f3779cdaff96e9d15 [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
Philipp Reisner57bcb6c2011-12-03 11:18:56 +010034static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size);
35
Philipp Reisnerb411b362009-09-25 16:07:19 -070036/* Update disk stats at start of I/O request */
37static 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();
Philipp Reisner72585d22012-02-23 12:56:26 +010042 part_round_stats(cpu, &mdev->vdisk->part0);
Philipp Reisnerb411b362009-09-25 16:07:19 -070043 part_stat_inc(cpu, &mdev->vdisk->part0, ios[rw]);
44 part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], bio_sectors(bio));
Philipp Reisner376694a2011-11-07 10:54:28 +010045 (void) cpu; /* The macro invocations above want the cpu argument, I do not like
46 the compiler warning about cpu only assigned but never used... */
Philipp Reisner753c8912009-11-18 15:52:51 +010047 part_inc_in_flight(&mdev->vdisk->part0, rw);
Philipp Reisnerb411b362009-09-25 16:07:19 -070048 part_stat_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -070049}
50
51/* Update disk stats when completing request upwards */
52static void _drbd_end_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
53{
54 int rw = bio_data_dir(req->master_bio);
55 unsigned long duration = jiffies - req->start_time;
56 int cpu;
57 cpu = part_stat_lock();
58 part_stat_add(cpu, &mdev->vdisk->part0, ticks[rw], duration);
59 part_round_stats(cpu, &mdev->vdisk->part0);
Philipp Reisner753c8912009-11-18 15:52:51 +010060 part_dec_in_flight(&mdev->vdisk->part0, rw);
Philipp Reisnerb411b362009-09-25 16:07:19 -070061 part_stat_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -070062}
63
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +010064static struct drbd_request *drbd_req_new(struct drbd_conf *mdev,
65 struct bio *bio_src)
66{
67 struct drbd_request *req;
68
69 req = mempool_alloc(drbd_request_mempool, GFP_NOIO);
70 if (!req)
71 return NULL;
72
73 drbd_req_make_private_bio(req, bio_src);
74 req->rq_state = bio_data_dir(bio_src) == WRITE ? RQ_WRITE : 0;
Philipp Reisnera21e9292011-02-08 15:08:49 +010075 req->w.mdev = mdev;
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +010076 req->master_bio = bio_src;
77 req->epoch = 0;
Andreas Gruenbacher53840642011-01-28 10:31:04 +010078
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +010079 drbd_clear_interval(&req->i);
80 req->i.sector = bio_src->bi_sector;
81 req->i.size = bio_src->bi_size;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +010082 req->i.local = true;
Andreas Gruenbacher53840642011-01-28 10:31:04 +010083 req->i.waiting = false;
84
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +010085 INIT_LIST_HEAD(&req->tl_requests);
86 INIT_LIST_HEAD(&req->w.list);
87
Lars Ellenberga0d856d2012-01-24 17:19:42 +010088 /* one reference to be put by __drbd_make_request */
Lars Ellenbergb4067772012-01-24 16:58:11 +010089 atomic_set(&req->completion_ref, 1);
Lars Ellenberga0d856d2012-01-24 17:19:42 +010090 /* one kref as long as completion_ref > 0 */
Lars Ellenbergb4067772012-01-24 16:58:11 +010091 kref_init(&req->kref);
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +010092 return req;
93}
94
Lars Ellenberg9a278a72012-07-24 10:12:36 +020095void drbd_req_destroy(struct kref *kref)
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +010096{
Lars Ellenbergb4067772012-01-24 16:58:11 +010097 struct drbd_request *req = container_of(kref, struct drbd_request, kref);
98 struct drbd_conf *mdev = req->w.mdev;
Lars Ellenberga0d856d2012-01-24 17:19:42 +010099 const unsigned s = req->rq_state;
100
101 if ((req->master_bio && !(s & RQ_POSTPONED)) ||
102 atomic_read(&req->completion_ref) ||
103 (s & RQ_LOCAL_PENDING) ||
104 ((s & RQ_NET_MASK) && !(s & RQ_NET_DONE))) {
105 dev_err(DEV, "drbd_req_destroy: Logic BUG rq_state = 0x%x, completion_ref = %d\n",
106 s, atomic_read(&req->completion_ref));
107 return;
108 }
Philipp Reisner288f4222010-05-27 15:07:43 +0200109
110 /* remove it from the transfer log.
111 * well, only if it had been there in the first
112 * place... if it had not (local only or conflicting
113 * and never sent), it should still be "empty" as
114 * initialized in drbd_req_new(), so we can list_del() it
115 * here unconditionally */
Lars Ellenberg2312f0b32011-11-24 10:36:25 +0100116 list_del_init(&req->tl_requests);
Philipp Reisner288f4222010-05-27 15:07:43 +0200117
Philipp Reisnerb411b362009-09-25 16:07:19 -0700118 /* if it was a write, we may have to set the corresponding
119 * bit(s) out-of-sync first. If it had a local part, we need to
120 * release the reference to the activity log. */
Lars Ellenbergb4067772012-01-24 16:58:11 +0100121 if (s & RQ_WRITE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700122 /* Set out-of-sync unless both OK flags are set
123 * (local only or remote failed).
124 * Other places where we set out-of-sync:
125 * READ with local io-error */
126 if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100127 drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700128
129 if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100130 drbd_set_in_sync(mdev, req->i.sector, req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700131
132 /* one might be tempted to move the drbd_al_complete_io
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +0100133 * to the local io completion callback drbd_request_endio.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700134 * but, if this was a mirror write, we may only
135 * drbd_al_complete_io after this is RQ_NET_DONE,
136 * otherwise the extent could be dropped from the al
137 * before it has actually been written on the peer.
138 * if we crash before our peer knows about the request,
139 * but after the extent has been dropped from the al,
140 * we would forget to resync the corresponding extent.
141 */
142 if (s & RQ_LOCAL_MASK) {
143 if (get_ldev_if_state(mdev, D_FAILED)) {
Philipp Reisner07782862010-08-31 12:00:50 +0200144 if (s & RQ_IN_ACT_LOG)
Lars Ellenberg181286a2011-03-31 15:18:56 +0200145 drbd_al_complete_io(mdev, &req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700146 put_ldev(mdev);
147 } else if (__ratelimit(&drbd_ratelimit_state)) {
Lars Ellenberg181286a2011-03-31 15:18:56 +0200148 dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu, %u), "
149 "but my Disk seems to have failed :(\n",
150 (unsigned long long) req->i.sector, req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700151 }
152 }
153 }
154
Lars Ellenberg9a278a72012-07-24 10:12:36 +0200155 mempool_free(req, drbd_request_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700156}
157
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100158static void wake_all_senders(struct drbd_tconn *tconn) {
159 wake_up(&tconn->sender_work.q_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700160}
161
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100162/* must hold resource->req_lock */
163static void start_new_tl_epoch(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700164{
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100165 tconn->current_tle_writes = 0;
166 atomic_inc(&tconn->current_tle_nr);
167 wake_all_senders(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700168}
169
170void complete_master_bio(struct drbd_conf *mdev,
171 struct bio_and_error *m)
172{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700173 bio_endio(m->bio, m->error);
174 dec_ap_bio(mdev);
175}
176
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100177
178static void drbd_remove_request_interval(struct rb_root *root,
179 struct drbd_request *req)
180{
Philipp Reisnera21e9292011-02-08 15:08:49 +0100181 struct drbd_conf *mdev = req->w.mdev;
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100182 struct drbd_interval *i = &req->i;
183
184 drbd_remove_interval(root, i);
185
186 /* Wake up any processes waiting for this request to complete. */
187 if (i->waiting)
188 wake_up(&mdev->misc_wait);
189}
190
Philipp Reisnerb411b362009-09-25 16:07:19 -0700191/* Helper for __req_mod().
192 * Set m->bio to the master bio, if it is fit to be completed,
193 * or leave it alone (it is initialized to NULL in __req_mod),
194 * if it has already been completed, or cannot be completed yet.
195 * If m->bio is set, the error status to be returned is placed in m->error.
196 */
Lars Ellenberg6870ca62012-03-26 17:02:45 +0200197static
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100198void drbd_req_complete(struct drbd_request *req, struct bio_and_error *m)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700199{
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100200 const unsigned s = req->rq_state;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100201 struct drbd_conf *mdev = req->w.mdev;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100202 int rw;
203 int error, ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700204
Philipp Reisnerb411b362009-09-25 16:07:19 -0700205 /* we must not complete the master bio, while it is
206 * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
207 * not yet acknowledged by the peer
208 * not yet completed by the local io subsystem
209 * these flags may get cleared in any order by
210 * the worker,
211 * the receiver,
212 * the bio_endio completion callbacks.
213 */
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100214 if ((s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED)) ||
215 (s & RQ_NET_QUEUED) || (s & RQ_NET_PENDING) ||
216 (s & RQ_COMPLETION_SUSP)) {
217 dev_err(DEV, "drbd_req_complete: Logic BUG rq_state = 0x%x\n", s);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100218 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700219 }
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100220
221 if (!req->master_bio) {
222 dev_err(DEV, "drbd_req_complete: Logic BUG, master_bio == NULL!\n");
223 return;
224 }
225
226 rw = bio_rw(req->master_bio);
227
228 /*
229 * figure out whether to report success or failure.
230 *
231 * report success when at least one of the operations succeeded.
232 * or, to put the other way,
233 * only report failure, when both operations failed.
234 *
235 * what to do about the failures is handled elsewhere.
236 * what we need to do here is just: complete the master_bio.
237 *
238 * local completion error, if any, has been stored as ERR_PTR
239 * in private_bio within drbd_request_endio.
240 */
241 ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
242 error = PTR_ERR(req->private_bio);
243
244 /* remove the request from the conflict detection
245 * respective block_id verification hash */
246 if (!drbd_interval_empty(&req->i)) {
247 struct rb_root *root;
248
249 if (rw == WRITE)
250 root = &mdev->write_requests;
251 else
252 root = &mdev->read_requests;
253 drbd_remove_request_interval(root, req);
254 } else if (!(s & RQ_POSTPONED))
255 D_ASSERT((s & (RQ_NET_MASK & ~RQ_NET_DONE)) == 0);
256
257 /* Before we can signal completion to the upper layers,
258 * we may need to close the current transfer log epoch.
259 * We are within the request lock, so we can simply compare
260 * the request epoch number with the current transfer log
261 * epoch number. If they match, increase the current_tle_nr,
262 * and reset the transfer log epoch write_cnt.
263 */
264 if (rw == WRITE &&
265 req->epoch == atomic_read(&mdev->tconn->current_tle_nr))
266 start_new_tl_epoch(mdev->tconn);
267
268 /* Update disk stats */
269 _drbd_end_io_acct(mdev, req);
270
271 /* If READ failed,
272 * have it be pushed back to the retry work queue,
273 * so it will re-enter __drbd_make_request(),
274 * and be re-assigned to a suitable local or remote path,
275 * or failed if we do not have access to good data anymore.
276 *
277 * Unless it was failed early by __drbd_make_request(),
278 * because no path was available, in which case
279 * it was not even added to the transfer_log.
280 *
281 * READA may fail, and will not be retried.
282 *
283 * WRITE should have used all available paths already.
284 */
285 if (!ok && rw == READ && !list_empty(&req->tl_requests))
286 req->rq_state |= RQ_POSTPONED;
287
288 if (!(req->rq_state & RQ_POSTPONED)) {
289 m->error = ok ? 0 : (error ?: -EIO);
290 m->bio = req->master_bio;
291 req->master_bio = NULL;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100292 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700293}
294
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100295static int drbd_req_put_completion_ref(struct drbd_request *req, struct bio_and_error *m, int put)
Philipp Reisnercfa03412010-06-23 17:18:51 +0200296{
Philipp Reisnera21e9292011-02-08 15:08:49 +0100297 struct drbd_conf *mdev = req->w.mdev;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100298 D_ASSERT(m || (req->rq_state & RQ_POSTPONED));
Philipp Reisnercfa03412010-06-23 17:18:51 +0200299
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100300 if (!atomic_sub_and_test(put, &req->completion_ref))
301 return 0;
302
303 if (drbd_suspended(mdev)) {
304 /* We do not allow completion while suspended. Re-get a
305 * reference, so whatever happens when this is resumed
306 * may put and complete. */
307
308 D_ASSERT(!(req->rq_state & RQ_COMPLETION_SUSP));
309 req->rq_state |= RQ_COMPLETION_SUSP;
310 atomic_inc(&req->completion_ref);
311 return 0;
312 }
313
314 /* else */
315 drbd_req_complete(req, m);
Lars Ellenberg9a278a72012-07-24 10:12:36 +0200316
317 if (req->rq_state & RQ_POSTPONED) {
318 /* don't destroy the req object just yet,
319 * but queue it for retry */
320 drbd_restart_request(req);
321 return 0;
322 }
323
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100324 return 1;
325}
326
327/* I'd like this to be the only place that manipulates
328 * req->completion_ref and req->kref. */
329static void mod_rq_state(struct drbd_request *req, struct bio_and_error *m,
330 int clear, int set)
331{
332 struct drbd_conf *mdev = req->w.mdev;
333 unsigned s = req->rq_state;
334 int c_put = 0;
335 int k_put = 0;
336
337 /* apply */
338
339 req->rq_state &= ~clear;
340 req->rq_state |= set;
341
342 /* no change? */
343 if (req->rq_state == s)
344 return;
345
346 /* intent: get references */
347
348 if (!(s & RQ_LOCAL_PENDING) && (set & RQ_LOCAL_PENDING))
349 atomic_inc(&req->completion_ref);
350
351 if (!(s & RQ_NET_PENDING) && (set & RQ_NET_PENDING)) {
352 inc_ap_pending(mdev);
353 atomic_inc(&req->completion_ref);
354 }
355
356 if (!(s & RQ_NET_QUEUED) && (set & RQ_NET_QUEUED))
357 atomic_inc(&req->completion_ref);
358
359 if (!(s & RQ_EXP_BARR_ACK) && (set & RQ_EXP_BARR_ACK))
360 kref_get(&req->kref); /* wait for the DONE */
361
362 if (!(s & RQ_NET_SENT) && (set & RQ_NET_SENT))
363 atomic_add(req->i.size >> 9, &mdev->ap_in_flight);
364
365 /* progress: put references */
366
367 if ((s & RQ_COMPLETION_SUSP) && (clear & RQ_COMPLETION_SUSP))
368 ++c_put;
369
370 if (!(s & RQ_LOCAL_ABORTED) && (set & RQ_LOCAL_ABORTED)) {
371 D_ASSERT(req->rq_state & RQ_LOCAL_PENDING);
372 /* local completion may still come in later,
373 * we need to keep the req object around. */
374 kref_get(&req->kref);
375 ++c_put;
376 }
377
378 if ((s & RQ_LOCAL_PENDING) && (clear & RQ_LOCAL_PENDING)) {
379 if (req->rq_state & RQ_LOCAL_ABORTED)
380 ++k_put;
381 else
382 ++c_put;
383 }
384
385 if ((s & RQ_NET_PENDING) && (clear & RQ_NET_PENDING)) {
386 dec_ap_pending(mdev);
387 ++c_put;
388 }
389
390 if ((s & RQ_NET_QUEUED) && (clear & RQ_NET_QUEUED))
391 ++c_put;
392
393 if ((s & RQ_EXP_BARR_ACK) && !(s & RQ_NET_DONE) && (set & RQ_NET_DONE)) {
394 if (req->rq_state & RQ_NET_SENT)
395 atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
396 ++k_put;
397 }
398
399 /* potentially complete and destroy */
400
401 if (k_put || c_put) {
402 /* Completion does it's own kref_put. If we are going to
403 * kref_sub below, we need req to be still around then. */
404 int at_least = k_put + !!c_put;
405 int refcount = atomic_read(&req->kref.refcount);
406 if (refcount < at_least)
407 dev_err(DEV,
408 "mod_rq_state: Logic BUG: %x -> %x: refcount = %d, should be >= %d\n",
409 s, req->rq_state, refcount, at_least);
410 }
411
412 /* If we made progress, retry conflicting peer requests, if any. */
413 if (req->i.waiting)
414 wake_up(&mdev->misc_wait);
415
416 if (c_put)
417 k_put += drbd_req_put_completion_ref(req, m, c_put);
418 if (k_put)
419 kref_sub(&req->kref, k_put, drbd_req_destroy);
Philipp Reisnercfa03412010-06-23 17:18:51 +0200420}
421
Philipp Reisnerb411b362009-09-25 16:07:19 -0700422/* obviously this could be coded as many single functions
423 * instead of one huge switch,
424 * or by putting the code directly in the respective locations
425 * (as it has been before).
426 *
427 * but having it this way
428 * enforces that it is all in this one place, where it is easier to audit,
429 * it makes it obvious that whatever "event" "happens" to a request should
430 * happen "atomically" within the req_lock,
431 * and it enforces that we have to think in a very structured manner
432 * about the "events" that may happen to a request during its life time ...
433 */
Philipp Reisner2a806992010-06-09 14:07:43 +0200434int __req_mod(struct drbd_request *req, enum drbd_req_event what,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700435 struct bio_and_error *m)
436{
Philipp Reisnera21e9292011-02-08 15:08:49 +0100437 struct drbd_conf *mdev = req->w.mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200438 struct net_conf *nc;
Philipp Reisner303d1442011-04-13 16:24:47 -0700439 int p, rv = 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100440
441 if (m)
442 m->bio = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700443
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444 switch (what) {
445 default:
446 dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
447 break;
448
449 /* does not happen...
450 * initialization done in drbd_req_new
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100451 case CREATED:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700452 break;
453 */
454
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100455 case TO_BE_SENT: /* via network */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100456 /* reached via __drbd_make_request
Philipp Reisnerb411b362009-09-25 16:07:19 -0700457 * and from w_read_retry_remote */
458 D_ASSERT(!(req->rq_state & RQ_NET_MASK));
Philipp Reisner44ed1672011-04-19 17:10:19 +0200459 rcu_read_lock();
460 nc = rcu_dereference(mdev->tconn->net_conf);
461 p = nc->wire_protocol;
462 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -0700463 req->rq_state |=
464 p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
465 p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100466 mod_rq_state(req, m, 0, RQ_NET_PENDING);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700467 break;
468
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100469 case TO_BE_SUBMITTED: /* locally */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100470 /* reached via __drbd_make_request */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700471 D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100472 mod_rq_state(req, m, 0, RQ_LOCAL_PENDING);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700473 break;
474
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100475 case COMPLETED_OK:
Philipp Reisnercdfda632011-07-05 15:38:59 +0200476 if (req->rq_state & RQ_WRITE)
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100477 mdev->writ_cnt += req->i.size >> 9;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700478 else
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100479 mdev->read_cnt += req->i.size >> 9;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700480
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100481 mod_rq_state(req, m, RQ_LOCAL_PENDING,
482 RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700483 break;
484
Philipp Reisnercdfda632011-07-05 15:38:59 +0200485 case ABORT_DISK_IO:
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100486 mod_rq_state(req, m, 0, RQ_LOCAL_ABORTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700487 break;
488
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100489 case READ_COMPLETED_WITH_ERROR:
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100490 drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100491 /* fall through. */
492 case WRITE_COMPLETED_WITH_ERROR:
Lars Ellenberg0c849662012-07-30 09:07:28 +0200493 __drbd_chk_io_error(mdev, DRBD_IO_ERROR);
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100494 /* fall through. */
495 case READ_AHEAD_COMPLETED_WITH_ERROR:
496 /* it is legal to fail READA, no __drbd_chk_io_error in that case. */
497 mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
Lars Ellenberg4439c402012-03-26 17:29:30 +0200498 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700499
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100500 case QUEUE_FOR_NET_READ:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501 /* READ or READA, and
502 * no local disk,
503 * or target area marked as invalid,
504 * or just got an io-error. */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100505 /* from __drbd_make_request
Philipp Reisnerb411b362009-09-25 16:07:19 -0700506 * or from bio_endio during read io-error recovery */
507
Lars Ellenberg6870ca62012-03-26 17:02:45 +0200508 /* So we can verify the handle in the answer packet.
509 * Corresponding drbd_remove_request_interval is in
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100510 * drbd_req_complete() */
Lars Ellenberg97ddb682011-07-15 23:52:44 +0200511 D_ASSERT(drbd_interval_empty(&req->i));
Andreas Gruenbacherdac13892011-01-21 17:18:39 +0100512 drbd_insert_interval(&mdev->read_requests, &req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700513
Lars Ellenberg83c38832009-11-03 02:22:06 +0100514 set_bit(UNPLUG_REMOTE, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700515
516 D_ASSERT(req->rq_state & RQ_NET_PENDING);
Lars Ellenberg4439c402012-03-26 17:29:30 +0200517 D_ASSERT((req->rq_state & RQ_LOCAL_MASK) == 0);
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100518 mod_rq_state(req, m, 0, RQ_NET_QUEUED);
Lars Ellenberg4439c402012-03-26 17:29:30 +0200519 req->w.cb = w_send_read_req;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100520 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700521 break;
522
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100523 case QUEUE_FOR_NET_WRITE:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700524 /* assert something? */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100525 /* from __drbd_make_request only */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526
Lars Ellenberg6870ca62012-03-26 17:02:45 +0200527 /* Corresponding drbd_remove_request_interval is in
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100528 * drbd_req_complete() */
Lars Ellenberg97ddb682011-07-15 23:52:44 +0200529 D_ASSERT(drbd_interval_empty(&req->i));
Andreas Gruenbacherde696712011-01-20 15:00:24 +0100530 drbd_insert_interval(&mdev->write_requests, &req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700531
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
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100541 * __drbd_make_request, because we now may set the bit
Philipp Reisnerb411b362009-09-25 16:07:19 -0700542 * again ourselves to close the current epoch.
543 *
544 * Add req to the (now) current epoch (barrier). */
545
Lars Ellenberg83c38832009-11-03 02:22:06 +0100546 /* otherwise we may lose an unplug, which may cause some remote
547 * io-scheduler timeout to expire, increasing maximum latency,
548 * hurting performance. */
549 set_bit(UNPLUG_REMOTE, &mdev->flags);
550
Philipp Reisnerb411b362009-09-25 16:07:19 -0700551 /* queue work item to send data */
552 D_ASSERT(req->rq_state & RQ_NET_PENDING);
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100553 mod_rq_state(req, m, 0, RQ_NET_QUEUED|RQ_EXP_BARR_ACK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700554 req->w.cb = w_send_dblock;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100555 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700556
557 /* close the epoch, in case it outgrew the limit */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200558 rcu_read_lock();
559 nc = rcu_dereference(mdev->tconn->net_conf);
560 p = nc->max_epoch_size;
561 rcu_read_unlock();
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100562 if (mdev->tconn->current_tle_writes >= p)
563 start_new_tl_epoch(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700564
565 break;
566
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100567 case QUEUE_FOR_SEND_OOS:
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100568 mod_rq_state(req, m, 0, RQ_NET_QUEUED);
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +0100569 req->w.cb = w_send_out_of_sync;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100570 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200571 break;
572
Lars Ellenbergea9d6722012-03-26 16:46:39 +0200573 case READ_RETRY_REMOTE_CANCELED:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100574 case SEND_CANCELED:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100575 case SEND_FAILED:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700576 /* real cleanup will be done from tl_clear. just update flags
577 * so it is no longer marked as on the worker queue */
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100578 mod_rq_state(req, m, RQ_NET_QUEUED, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700579 break;
580
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100581 case HANDED_OVER_TO_NETWORK:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582 /* assert something? */
583 if (bio_data_dir(req->master_bio) == WRITE &&
Philipp Reisner303d1442011-04-13 16:24:47 -0700584 !(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700585 /* this is what is dangerous about protocol A:
586 * pretend it was successfully written on the peer. */
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100587 if (req->rq_state & RQ_NET_PENDING)
588 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
589 /* else: neg-ack was faster... */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700590 /* it is still not yet RQ_NET_DONE until the
591 * corresponding epoch barrier got acked as well,
592 * so we know what to dirty on connection loss */
593 }
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100594 mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_SENT);
Lars Ellenberg27a434f2012-03-26 16:44:59 +0200595 break;
596
597 case OOS_HANDED_TO_NETWORK:
598 /* Was not set PENDING, no longer QUEUED, so is now DONE
599 * as far as this connection is concerned. */
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100600 mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_DONE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700601 break;
602
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100603 case CONNECTION_LOST_WHILE_PENDING:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700604 /* transfer log cleanup after connection loss */
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100605 mod_rq_state(req, m,
606 RQ_NET_OK|RQ_NET_PENDING|RQ_COMPLETION_SUSP,
607 RQ_NET_DONE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700608 break;
609
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100610 case DISCARD_WRITE:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700611 /* for discarded conflicting writes of multiple primaries,
612 * there is no need to keep anything in the tl, potential
Lars Ellenberg934722a2012-07-24 09:31:18 +0200613 * node crashes are covered by the activity log.
614 *
615 * If this request had been marked as RQ_POSTPONED before,
616 * it will actually not be discarded, but "restarted",
617 * resubmitted from the retry worker context. */
618 D_ASSERT(req->rq_state & RQ_NET_PENDING);
619 D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
620 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_DONE|RQ_NET_OK);
621 break;
622
Lars Ellenberg0afd5692012-03-26 16:51:11 +0200623 case WRITE_ACKED_BY_PEER_AND_SIS:
Lars Ellenberg934722a2012-07-24 09:31:18 +0200624 req->rq_state |= RQ_NET_SIS;
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100625 case WRITE_ACKED_BY_PEER:
Philipp Reisner303d1442011-04-13 16:24:47 -0700626 D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700627 /* protocol C; successfully written on peer.
Lars Ellenberg0afd5692012-03-26 16:51:11 +0200628 * Nothing more to do here.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700629 * We want to keep the tl in place for all protocols, to cater
Lars Ellenberg0afd5692012-03-26 16:51:11 +0200630 * for volatile write-back caches on lower level devices. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700631
Philipp Reisner303d1442011-04-13 16:24:47 -0700632 goto ack_common;
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100633 case RECV_ACKED_BY_PEER:
Philipp Reisner303d1442011-04-13 16:24:47 -0700634 D_ASSERT(req->rq_state & RQ_EXP_RECEIVE_ACK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700635 /* protocol B; pretends to be successfully written on peer.
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100636 * see also notes above in HANDED_OVER_TO_NETWORK about
Philipp Reisnerb411b362009-09-25 16:07:19 -0700637 * protocol != C */
Philipp Reisner303d1442011-04-13 16:24:47 -0700638 ack_common:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700639 D_ASSERT(req->rq_state & RQ_NET_PENDING);
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100640 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700641 break;
642
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100643 case POSTPONE_WRITE:
Philipp Reisner303d1442011-04-13 16:24:47 -0700644 D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
645 /* If this node has already detected the write conflict, the
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100646 * worker will be waiting on misc_wait. Wake it up once this
647 * request has completed locally.
648 */
649 D_ASSERT(req->rq_state & RQ_NET_PENDING);
650 req->rq_state |= RQ_POSTPONED;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100651 if (req->i.waiting)
652 wake_up(&mdev->misc_wait);
653 /* Do not clear RQ_NET_PENDING. This request will make further
654 * progress via restart_conflicting_writes() or
655 * fail_postponed_requests(). Hopefully. */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100656 break;
657
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100658 case NEG_ACKED:
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100659 mod_rq_state(req, m, RQ_NET_OK|RQ_NET_PENDING, RQ_NET_DONE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700660 break;
661
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100662 case FAIL_FROZEN_DISK_IO:
Philipp Reisner265be2d2010-05-31 10:14:17 +0200663 if (!(req->rq_state & RQ_LOCAL_COMPLETED))
664 break;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100665 mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
Philipp Reisner265be2d2010-05-31 10:14:17 +0200666 break;
667
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100668 case RESTART_FROZEN_DISK_IO:
Philipp Reisner265be2d2010-05-31 10:14:17 +0200669 if (!(req->rq_state & RQ_LOCAL_COMPLETED))
670 break;
671
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100672 mod_rq_state(req, m,
673 RQ_COMPLETION_SUSP|RQ_LOCAL_COMPLETED,
674 RQ_LOCAL_PENDING);
Philipp Reisner265be2d2010-05-31 10:14:17 +0200675
676 rv = MR_READ;
677 if (bio_data_dir(req->master_bio) == WRITE)
678 rv = MR_WRITE;
679
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100680 get_ldev(mdev); /* always succeeds in this call path */
Philipp Reisner265be2d2010-05-31 10:14:17 +0200681 req->w.cb = w_restart_disk_io;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100682 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
Philipp Reisner265be2d2010-05-31 10:14:17 +0200683 break;
684
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100685 case RESEND:
Philipp Reisner8a0bab22012-08-07 13:28:00 +0200686 /* Simply complete (local only) READs. */
687 if (!(req->rq_state & RQ_WRITE) && !req->w.cb) {
688 mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
689 break;
690 }
691
Philipp Reisner11b58e72010-05-12 17:08:26 +0200692 /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100693 before the connection loss (B&C only); only P_BARRIER_ACK
694 (or the local completion?) was missing when we suspended.
Lars Ellenberg6870ca62012-03-26 17:02:45 +0200695 Throwing them out of the TL here by pretending we got a BARRIER_ACK.
696 During connection handshake, we ensure that the peer was not rebooted. */
Philipp Reisner11b58e72010-05-12 17:08:26 +0200697 if (!(req->rq_state & RQ_NET_OK)) {
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100698 /* FIXME could this possibly be a req->w.cb == w_send_out_of_sync?
699 * in that case we must not set RQ_NET_PENDING. */
700
701 mod_rq_state(req, m, RQ_COMPLETION_SUSP, RQ_NET_QUEUED|RQ_NET_PENDING);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200702 if (req->w.cb) {
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100703 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200704 rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100705 } /* else: FIXME can this happen? */
Philipp Reisner11b58e72010-05-12 17:08:26 +0200706 break;
707 }
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100708 /* else, fall through to BARRIER_ACKED */
Philipp Reisner11b58e72010-05-12 17:08:26 +0200709
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100710 case BARRIER_ACKED:
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100711 /* barrier ack for READ requests does not make sense */
Philipp Reisner288f4222010-05-27 15:07:43 +0200712 if (!(req->rq_state & RQ_WRITE))
713 break;
714
Philipp Reisnerb411b362009-09-25 16:07:19 -0700715 if (req->rq_state & RQ_NET_PENDING) {
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +0200716 /* barrier came in before all requests were acked.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717 * this is bad, because if the connection is lost now,
718 * we won't be able to clean them up... */
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100719 dev_err(DEV, "FIXME (BARRIER_ACKED but pending)\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700720 }
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100721 /* Allowed to complete requests, even while suspended.
722 * As this is called for all requests within a matching epoch,
723 * we need to filter, and only set RQ_NET_DONE for those that
724 * have actually been on the wire. */
725 mod_rq_state(req, m, RQ_COMPLETION_SUSP,
726 (req->rq_state & RQ_NET_MASK) ? RQ_NET_DONE : 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700727 break;
728
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100729 case DATA_RECEIVED:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700730 D_ASSERT(req->rq_state & RQ_NET_PENDING);
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100731 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK|RQ_NET_DONE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700732 break;
733 };
Philipp Reisner2a806992010-06-09 14:07:43 +0200734
735 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700736}
737
738/* we may do a local read if:
739 * - we are consistent (of course),
740 * - or we are generally inconsistent,
741 * BUT we are still/already IN SYNC for this area.
742 * since size may be bigger than BM_BLOCK_SIZE,
743 * we may need to check several bits.
744 */
Andreas Gruenbacher0da34df2010-12-19 20:48:29 +0100745static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700746{
747 unsigned long sbnr, ebnr;
748 sector_t esector, nr_sectors;
749
750 if (mdev->state.disk == D_UP_TO_DATE)
Andreas Gruenbacher0da34df2010-12-19 20:48:29 +0100751 return true;
Lars Ellenberg8c387de2011-02-18 14:13:07 +0100752 if (mdev->state.disk != D_INCONSISTENT)
Andreas Gruenbacher0da34df2010-12-19 20:48:29 +0100753 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700754 esector = sector + (size >> 9) - 1;
Andreas Gruenbacher8ca98442011-02-21 12:34:58 +0100755 nr_sectors = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700756 D_ASSERT(sector < nr_sectors);
757 D_ASSERT(esector < nr_sectors);
758
759 sbnr = BM_SECT_TO_BIT(sector);
760 ebnr = BM_SECT_TO_BIT(esector);
761
Andreas Gruenbacher0da34df2010-12-19 20:48:29 +0100762 return drbd_bm_count_bits(mdev, sbnr, ebnr) == 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700763}
764
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200765static bool remote_due_to_read_balancing(struct drbd_conf *mdev, sector_t sector,
766 enum drbd_read_balancing rbm)
Philipp Reisner380207d2011-11-11 12:31:20 +0100767{
Philipp Reisner380207d2011-11-11 12:31:20 +0100768 struct backing_dev_info *bdi;
Philipp Reisnerd60de032011-11-17 10:12:31 +0100769 int stripe_shift;
Philipp Reisner380207d2011-11-11 12:31:20 +0100770
Philipp Reisner380207d2011-11-11 12:31:20 +0100771 switch (rbm) {
772 case RB_CONGESTED_REMOTE:
773 bdi = &mdev->ldev->backing_bdev->bd_disk->queue->backing_dev_info;
774 return bdi_read_congested(bdi);
775 case RB_LEAST_PENDING:
776 return atomic_read(&mdev->local_cnt) >
777 atomic_read(&mdev->ap_pending_cnt) + atomic_read(&mdev->rs_pending_cnt);
Philipp Reisnerd60de032011-11-17 10:12:31 +0100778 case RB_32K_STRIPING: /* stripe_shift = 15 */
779 case RB_64K_STRIPING:
780 case RB_128K_STRIPING:
781 case RB_256K_STRIPING:
782 case RB_512K_STRIPING:
783 case RB_1M_STRIPING: /* stripe_shift = 20 */
784 stripe_shift = (rbm - RB_32K_STRIPING + 15);
785 return (sector >> (stripe_shift - 9)) & 1;
Philipp Reisner380207d2011-11-11 12:31:20 +0100786 case RB_ROUND_ROBIN:
787 return test_and_change_bit(READ_BALANCE_RR, &mdev->flags);
788 case RB_PREFER_REMOTE:
789 return true;
790 case RB_PREFER_LOCAL:
791 default:
792 return false;
793 }
794}
795
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100796/*
797 * complete_conflicting_writes - wait for any conflicting write requests
798 *
799 * The write_requests tree contains all active write requests which we
800 * currently know about. Wait for any requests to complete which conflict with
801 * the new one.
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200802 *
803 * Only way out: remove the conflicting intervals from the tree.
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100804 */
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200805static void complete_conflicting_writes(struct drbd_request *req)
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100806{
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200807 DEFINE_WAIT(wait);
808 struct drbd_conf *mdev = req->w.mdev;
809 struct drbd_interval *i;
810 sector_t sector = req->i.sector;
811 int size = req->i.size;
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100812
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200813 i = drbd_find_overlap(&mdev->write_requests, sector, size);
814 if (!i)
815 return;
816
817 for (;;) {
818 prepare_to_wait(&mdev->misc_wait, &wait, TASK_UNINTERRUPTIBLE);
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100819 i = drbd_find_overlap(&mdev->write_requests, sector, size);
820 if (!i)
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200821 break;
822 /* Indicate to wake up device->misc_wait on progress. */
823 i->waiting = true;
824 spin_unlock_irq(&mdev->tconn->req_lock);
825 schedule();
826 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100827 }
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200828 finish_wait(&mdev->misc_wait, &wait);
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100829}
830
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200831/* called within req_lock and rcu_read_lock() */
Lars Ellenberg3b9ef852012-07-30 09:06:26 +0200832static void maybe_pull_ahead(struct drbd_conf *mdev)
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200833{
834 struct drbd_tconn *tconn = mdev->tconn;
835 struct net_conf *nc;
836 bool congested = false;
837 enum drbd_on_congestion on_congestion;
838
839 nc = rcu_dereference(tconn->net_conf);
840 on_congestion = nc ? nc->on_congestion : OC_BLOCK;
841 if (on_congestion == OC_BLOCK ||
842 tconn->agreed_pro_version < 96)
Lars Ellenberg3b9ef852012-07-30 09:06:26 +0200843 return;
844
845 /* If I don't even have good local storage, we can not reasonably try
846 * to pull ahead of the peer. We also need the local reference to make
847 * sure mdev->act_log is there.
848 */
849 if (!get_ldev_if_state(mdev, D_UP_TO_DATE))
850 return;
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200851
852 if (nc->cong_fill &&
853 atomic_read(&mdev->ap_in_flight) >= nc->cong_fill) {
854 dev_info(DEV, "Congestion-fill threshold reached\n");
855 congested = true;
856 }
857
858 if (mdev->act_log->used >= nc->cong_extents) {
859 dev_info(DEV, "Congestion-extents threshold reached\n");
860 congested = true;
861 }
862
863 if (congested) {
864 if (mdev->tconn->current_tle_writes)
865 /* start a new epoch for non-mirrored writes */
866 start_new_tl_epoch(mdev->tconn);
867
868 if (on_congestion == OC_PULL_AHEAD)
869 _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
870 else /*nc->on_congestion == OC_DISCONNECT */
871 _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
872 }
Lars Ellenberg3b9ef852012-07-30 09:06:26 +0200873 put_ldev(mdev);
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200874}
875
876/* If this returns false, and req->private_bio is still set,
877 * this should be submitted locally.
878 *
879 * If it returns false, but req->private_bio is not set,
880 * we do not have access to good data :(
881 *
882 * Otherwise, this destroys req->private_bio, if any,
883 * and returns true.
884 */
885static bool do_remote_read(struct drbd_request *req)
886{
887 struct drbd_conf *mdev = req->w.mdev;
888 enum drbd_read_balancing rbm;
889
890 if (req->private_bio) {
891 if (!drbd_may_do_local_read(mdev,
892 req->i.sector, req->i.size)) {
893 bio_put(req->private_bio);
894 req->private_bio = NULL;
895 put_ldev(mdev);
896 }
897 }
898
899 if (mdev->state.pdsk != D_UP_TO_DATE)
900 return false;
901
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100902 if (req->private_bio == NULL)
903 return true;
904
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200905 /* TODO: improve read balancing decisions, take into account drbd
906 * protocol, pending requests etc. */
907
908 rcu_read_lock();
909 rbm = rcu_dereference(mdev->ldev->disk_conf)->read_balancing;
910 rcu_read_unlock();
911
912 if (rbm == RB_PREFER_LOCAL && req->private_bio)
913 return false; /* submit locally */
914
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200915 if (remote_due_to_read_balancing(mdev, req->i.sector, rbm)) {
916 if (req->private_bio) {
917 bio_put(req->private_bio);
918 req->private_bio = NULL;
919 put_ldev(mdev);
920 }
921 return true;
922 }
923
924 return false;
925}
926
927/* returns number of connections (== 1, for drbd 8.4)
928 * expected to actually write this data,
929 * which does NOT include those that we are L_AHEAD for. */
930static int drbd_process_write_request(struct drbd_request *req)
931{
932 struct drbd_conf *mdev = req->w.mdev;
933 int remote, send_oos;
934
935 rcu_read_lock();
936 remote = drbd_should_do_remote(mdev->state);
937 if (remote) {
Lars Ellenberg3b9ef852012-07-30 09:06:26 +0200938 maybe_pull_ahead(mdev);
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200939 remote = drbd_should_do_remote(mdev->state);
940 }
941 send_oos = drbd_should_send_out_of_sync(mdev->state);
942 rcu_read_unlock();
943
Lars Ellenberg519b6d32012-08-03 02:19:09 +0200944 /* Need to replicate writes. Unless it is an empty flush,
945 * which is better mapped to a DRBD P_BARRIER packet,
946 * also for drbd wire protocol compatibility reasons.
947 * If this was a flush, just start a new epoch.
948 * Unless the current epoch was empty anyways, or we are not currently
949 * replicating, in which case there is no point. */
950 if (unlikely(req->i.size == 0)) {
951 /* The only size==0 bios we expect are empty flushes. */
952 D_ASSERT(req->master_bio->bi_rw & REQ_FLUSH);
953 if (remote && mdev->tconn->current_tle_writes)
954 start_new_tl_epoch(mdev->tconn);
955 return 0;
956 }
957
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200958 if (!remote && !send_oos)
959 return 0;
960
961 D_ASSERT(!(remote && send_oos));
962
963 if (remote) {
964 _req_mod(req, TO_BE_SENT);
965 _req_mod(req, QUEUE_FOR_NET_WRITE);
966 } else if (drbd_set_out_of_sync(mdev, req->i.sector, req->i.size))
967 _req_mod(req, QUEUE_FOR_SEND_OOS);
968
969 return remote;
970}
971
972static void
973drbd_submit_req_private_bio(struct drbd_request *req)
974{
975 struct drbd_conf *mdev = req->w.mdev;
976 struct bio *bio = req->private_bio;
977 const int rw = bio_rw(bio);
978
979 bio->bi_bdev = mdev->ldev->backing_bdev;
980
981 /* State may have changed since we grabbed our reference on the
982 * ->ldev member. Double check, and short-circuit to endio.
983 * In case the last activity log transaction failed to get on
984 * stable storage, and this is a WRITE, we may not even submit
985 * this bio. */
986 if (get_ldev(mdev)) {
987 if (drbd_insert_fault(mdev,
988 rw == WRITE ? DRBD_FAULT_DT_WR
989 : rw == READ ? DRBD_FAULT_DT_RD
990 : DRBD_FAULT_DT_RA))
991 bio_endio(bio, -EIO);
992 else
993 generic_make_request(bio);
994 put_ldev(mdev);
995 } else
996 bio_endio(bio, -EIO);
997}
998
Lars Ellenberg5df69ec2012-01-24 16:49:58 +0100999void __drbd_make_request(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000{
1001 const int rw = bio_rw(bio);
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001002 struct bio_and_error m = { NULL, };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003 struct drbd_request *req;
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001004 bool no_remote = false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001005
1006 /* allocate outside of all locks; */
1007 req = drbd_req_new(mdev, bio);
1008 if (!req) {
1009 dec_ap_bio(mdev);
1010 /* only pass the error to the upper layers.
1011 * if user cannot handle io errors, that's not our business. */
1012 dev_err(DEV, "could not kmalloc() req\n");
1013 bio_endio(bio, -ENOMEM);
Lars Ellenberg5df69ec2012-01-24 16:49:58 +01001014 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001015 }
Philipp Reisneraeda1cd2010-11-09 17:45:06 +01001016 req->start_time = start_time;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001017
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001018 if (!get_ldev(mdev)) {
1019 bio_put(req->private_bio);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001020 req->private_bio = NULL;
1021 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022
1023 /* For WRITES going to the local disk, grab a reference on the target
1024 * extent. This waits for any resync activity in the corresponding
1025 * resync extent to finish, and, if necessary, pulls in the target
1026 * extent into the activity log, which involves further disk io because
Lars Ellenberg519b6d32012-08-03 02:19:09 +02001027 * of transactional on-disk meta data updates.
1028 * Empty flushes don't need to go into the activity log, they can only
1029 * flush data for pending writes which are already in there. */
1030 if (rw == WRITE && req->private_bio && req->i.size
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001031 && !test_bit(AL_SUSPENDED, &mdev->flags)) {
Philipp Reisner07782862010-08-31 12:00:50 +02001032 req->rq_state |= RQ_IN_ACT_LOG;
Lars Ellenberg181286a2011-03-31 15:18:56 +02001033 drbd_al_begin_io(mdev, &req->i);
Philipp Reisner07782862010-08-31 12:00:50 +02001034 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001035
Philipp Reisner87eeee42011-01-19 14:16:30 +01001036 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +01001037 if (rw == WRITE) {
Lars Ellenberg648e46b2012-03-26 20:12:24 +02001038 /* This may temporarily give up the req_lock,
1039 * but will re-aquire it before it returns here.
1040 * Needs to be before the check on drbd_suspended() */
1041 complete_conflicting_writes(req);
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +01001042 }
1043
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001044 /* no more giving up req_lock from now on! */
1045
Philipp Reisner2aebfab2011-03-28 16:48:11 +02001046 if (drbd_suspended(mdev)) {
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001047 /* push back and retry: */
1048 req->rq_state |= RQ_POSTPONED;
1049 if (req->private_bio) {
1050 bio_put(req->private_bio);
1051 req->private_bio = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001052 }
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001053 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001054 }
1055
Philipp Reisnerb411b362009-09-25 16:07:19 -07001056 /* Update disk stats */
1057 _drbd_start_io_acct(mdev, req, bio);
1058
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001059 /* We fail READ/READA early, if we can not serve it.
1060 * We must do this before req is registered on any lists.
Lars Ellenberga0d856d2012-01-24 17:19:42 +01001061 * Otherwise, drbd_req_complete() will queue failed READ for retry. */
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001062 if (rw != WRITE) {
1063 if (!do_remote_read(req) && !req->private_bio)
1064 goto nodata;
1065 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001066
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001067 /* which transfer log epoch does this belong to? */
1068 req->epoch = atomic_read(&mdev->tconn->current_tle_nr);
1069 if (rw == WRITE)
1070 mdev->tconn->current_tle_writes++;
1071
Lars Ellenberg519b6d32012-08-03 02:19:09 +02001072 /* no point in adding empty flushes to the transfer log,
1073 * they are mapped to drbd barriers already. */
1074 if (likely(req->i.size!=0))
1075 list_add_tail(&req->tl_requests, &mdev->tconn->transfer_log);
Philipp Reisner288f4222010-05-27 15:07:43 +02001076
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001077 if (rw == WRITE) {
1078 if (!drbd_process_write_request(req))
1079 no_remote = true;
1080 } else {
1081 /* We either have a private_bio, or we can read from remote.
1082 * Otherwise we had done the goto nodata above. */
1083 if (req->private_bio == NULL) {
1084 _req_mod(req, TO_BE_SENT);
1085 _req_mod(req, QUEUE_FOR_NET_READ);
1086 } else
1087 no_remote = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088 }
Philipp Reisner67531712010-10-27 12:21:30 +02001089
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001090 if (req->private_bio) {
1091 /* needs to be marked within the same spinlock */
1092 _req_mod(req, TO_BE_SUBMITTED);
1093 /* but we need to give up the spinlock to submit */
1094 spin_unlock_irq(&mdev->tconn->req_lock);
1095 drbd_submit_req_private_bio(req);
Lars Ellenberga0d856d2012-01-24 17:19:42 +01001096 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001097 } else if (no_remote) {
1098nodata:
1099 if (__ratelimit(&drbd_ratelimit_state))
1100 dev_err(DEV, "IO ERROR: neither local nor remote disk\n");
1101 /* A write may have been queued for send_oos, however.
Lars Ellenberga0d856d2012-01-24 17:19:42 +01001102 * So we can not simply free it, we must go through drbd_req_put_completion_ref() */
Philipp Reisner67531712010-10-27 12:21:30 +02001103 }
1104
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001105out:
Lars Ellenberga0d856d2012-01-24 17:19:42 +01001106 if (drbd_req_put_completion_ref(req, &m, 1))
1107 kref_put(&req->kref, drbd_req_destroy);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001108 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001109
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001110 if (m.bio)
1111 complete_master_bio(mdev, &m);
Lars Ellenberg5df69ec2012-01-24 16:49:58 +01001112 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001113}
1114
Andreas Gruenbacher2f58dcf2010-12-13 17:48:19 +01001115int drbd_make_request(struct request_queue *q, struct bio *bio)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001116{
Philipp Reisnerb411b362009-09-25 16:07:19 -07001117 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
Philipp Reisneraeda1cd2010-11-09 17:45:06 +01001118 unsigned long start_time;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001119
Philipp Reisneraeda1cd2010-11-09 17:45:06 +01001120 start_time = jiffies;
1121
Philipp Reisnerb411b362009-09-25 16:07:19 -07001122 /*
1123 * what we "blindly" assume:
1124 */
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01001125 D_ASSERT(IS_ALIGNED(bio->bi_size, 512));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001126
Lars Ellenberg5df69ec2012-01-24 16:49:58 +01001127 inc_ap_bio(mdev);
1128 __drbd_make_request(mdev, bio, start_time);
Philipp Reisner69b6a3b2011-12-20 11:49:58 +01001129
1130 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001131}
1132
Lars Ellenberg23361cf2011-03-31 16:36:43 +02001133/* This is called by bio_add_page().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001134 *
Lars Ellenberg23361cf2011-03-31 16:36:43 +02001135 * q->max_hw_sectors and other global limits are already enforced there.
1136 *
1137 * We need to call down to our lower level device,
1138 * in case it has special restrictions.
1139 *
1140 * We also may need to enforce configured max-bio-bvecs limits.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141 *
1142 * As long as the BIO is empty we have to allow at least one bvec,
Lars Ellenberg23361cf2011-03-31 16:36:43 +02001143 * regardless of size and offset, so no need to ask lower levels.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001144 */
1145int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
1146{
1147 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001148 unsigned int bio_size = bvm->bi_size;
Lars Ellenberg23361cf2011-03-31 16:36:43 +02001149 int limit = DRBD_MAX_BIO_SIZE;
1150 int backing_limit;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001151
Lars Ellenberg23361cf2011-03-31 16:36:43 +02001152 if (bio_size && get_ldev(mdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001153 struct request_queue * const b =
1154 mdev->ldev->backing_bdev->bd_disk->queue;
Lars Ellenberga1c88d02010-05-14 19:16:41 +02001155 if (b->merge_bvec_fn) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001156 backing_limit = b->merge_bvec_fn(b, bvm, bvec);
1157 limit = min(limit, backing_limit);
1158 }
1159 put_ldev(mdev);
1160 }
1161 return limit;
1162}
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001163
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001164struct drbd_request *find_oldest_request(struct drbd_tconn *tconn)
1165{
1166 /* Walk the transfer log,
1167 * and find the oldest not yet completed request */
1168 struct drbd_request *r;
1169 list_for_each_entry(r, &tconn->transfer_log, tl_requests) {
Lars Ellenbergb4067772012-01-24 16:58:11 +01001170 if (atomic_read(&r->completion_ref))
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001171 return r;
1172 }
1173 return NULL;
1174}
1175
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001176void request_timer_fn(unsigned long data)
1177{
1178 struct drbd_conf *mdev = (struct drbd_conf *) data;
Philipp Reisner8b924f12011-03-01 11:08:28 +01001179 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001180 struct drbd_request *req; /* oldest request */
Philipp Reisner44ed1672011-04-19 17:10:19 +02001181 struct net_conf *nc;
Philipp Reisner3b03ad52011-07-15 13:53:06 +02001182 unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001183 unsigned long now;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001184
Philipp Reisner44ed1672011-04-19 17:10:19 +02001185 rcu_read_lock();
1186 nc = rcu_dereference(tconn->net_conf);
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001187 if (nc && mdev->state.conn >= C_WF_REPORT_PARAMS)
1188 ent = nc->timeout * HZ/10 * nc->ko_count;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001189
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001190 if (get_ldev(mdev)) { /* implicit state.disk >= D_INCONSISTENT */
Philipp Reisnercdfda632011-07-05 15:38:59 +02001191 dt = rcu_dereference(mdev->ldev->disk_conf)->disk_timeout * HZ / 10;
1192 put_ldev(mdev);
1193 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001194 rcu_read_unlock();
1195
Philipp Reisnercdfda632011-07-05 15:38:59 +02001196 et = min_not_zero(dt, ent);
1197
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001198 if (!et)
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001199 return; /* Recurring timer stopped */
1200
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001201 now = jiffies;
1202
Philipp Reisner8b924f12011-03-01 11:08:28 +01001203 spin_lock_irq(&tconn->req_lock);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001204 req = find_oldest_request(tconn);
1205 if (!req) {
Philipp Reisner8b924f12011-03-01 11:08:28 +01001206 spin_unlock_irq(&tconn->req_lock);
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001207 mod_timer(&mdev->request_timer, now + et);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001208 return;
1209 }
1210
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001211 /* The request is considered timed out, if
1212 * - we have some effective timeout from the configuration,
1213 * with above state restrictions applied,
1214 * - the oldest request is waiting for a response from the network
1215 * resp. the local disk,
1216 * - the oldest request is in fact older than the effective timeout,
1217 * - the connection was established (resp. disk was attached)
1218 * for longer than the timeout already.
1219 * Note that for 32bit jiffies and very stable connections/disks,
1220 * we may have a wrap around, which is catched by
1221 * !time_in_range(now, last_..._jif, last_..._jif + timeout).
1222 *
1223 * Side effect: once per 32bit wrap-around interval, which means every
1224 * ~198 days with 250 HZ, we have a window where the timeout would need
1225 * to expire twice (worst case) to become effective. Good enough.
1226 */
1227 if (ent && req->rq_state & RQ_NET_PENDING &&
1228 time_after(now, req->start_time + ent) &&
1229 !time_in_range(now, tconn->last_reconnect_jif, tconn->last_reconnect_jif + ent)) {
1230 dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
1231 _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001232 }
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001233 if (dt && req->rq_state & RQ_LOCAL_PENDING && req->w.mdev == mdev &&
1234 time_after(now, req->start_time + dt) &&
1235 !time_in_range(now, mdev->last_reattach_jif, mdev->last_reattach_jif + dt)) {
1236 dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
Lars Ellenberg0c849662012-07-30 09:07:28 +02001237 __drbd_chk_io_error(mdev, DRBD_FORCE_DETACH);
Philipp Reisnercdfda632011-07-05 15:38:59 +02001238 }
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001239 nt = (time_after(now, req->start_time + et) ? now : req->start_time) + et;
Philipp Reisner8b924f12011-03-01 11:08:28 +01001240 spin_unlock_irq(&tconn->req_lock);
Philipp Reisner3b03ad52011-07-15 13:53:06 +02001241 mod_timer(&mdev->request_timer, nt);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001242}