blob: d72f2fef1cbaec418e0e4dd95230d3204c99e4c3 [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 */
Lars Ellenberg6d9febe2013-03-19 18:16:50 +010037static void _drbd_start_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
Philipp Reisnerb411b362009-09-25 16:07:19 -070038{
Lars Ellenberg6d9febe2013-03-19 18:16:50 +010039 const int rw = bio_data_dir(req->master_bio);
Philipp Reisnerb411b362009-09-25 16:07:19 -070040 int cpu;
41 cpu = part_stat_lock();
Lars Ellenberg031a7c12012-01-21 16:50:25 +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]);
Lars Ellenberg6d9febe2013-03-19 18:16:50 +010044 part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], req->i.size >> 9);
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)
Philipp Reisnerb411b362009-09-25 16:07:19 -070066{
Andreas Gruenbacher9e204cd2011-01-26 18:45:11 +010067 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 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700126
Lars Ellenberg70f17b62012-09-03 14:08:35 +0200127 /* There is a special case:
128 * we may notice late that IO was suspended,
129 * and postpone, or schedule for retry, a write,
130 * before it even was submitted or sent.
131 * In that case we do not want to touch the bitmap at all.
132 */
133 if ((s & (RQ_POSTPONED|RQ_LOCAL_MASK|RQ_NET_MASK)) != RQ_POSTPONED) {
Philipp Reisnerd7644012012-08-28 14:39:44 +0200134 if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
135 drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700136
Philipp Reisnerd7644012012-08-28 14:39:44 +0200137 if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
138 drbd_set_in_sync(mdev, req->i.sector, req->i.size);
139 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700140
141 /* one might be tempted to move the drbd_al_complete_io
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +0100142 * to the local io completion callback drbd_request_endio.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700143 * but, if this was a mirror write, we may only
144 * drbd_al_complete_io after this is RQ_NET_DONE,
145 * otherwise the extent could be dropped from the al
146 * before it has actually been written on the peer.
147 * if we crash before our peer knows about the request,
148 * but after the extent has been dropped from the al,
149 * we would forget to resync the corresponding extent.
150 */
Philipp Reisner76590cd2012-08-29 15:23:14 +0200151 if (s & RQ_IN_ACT_LOG) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700152 if (get_ldev_if_state(mdev, D_FAILED)) {
Philipp Reisner76590cd2012-08-29 15:23:14 +0200153 drbd_al_complete_io(mdev, &req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700154 put_ldev(mdev);
155 } else if (__ratelimit(&drbd_ratelimit_state)) {
Lars Ellenberg181286a2011-03-31 15:18:56 +0200156 dev_warn(DEV, "Should have called drbd_al_complete_io(, %llu, %u), "
157 "but my Disk seems to have failed :(\n",
158 (unsigned long long) req->i.sector, req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700159 }
160 }
161 }
162
Lars Ellenberg9a278a72012-07-24 10:12:36 +0200163 mempool_free(req, drbd_request_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700164}
165
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100166static void wake_all_senders(struct drbd_tconn *tconn) {
167 wake_up(&tconn->sender_work.q_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700168}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700169
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100170/* must hold resource->req_lock */
Lars Ellenberg2681f7f2013-01-21 15:43:41 +0100171void start_new_tl_epoch(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700172{
Lars Ellenberg99b4d8f2012-08-07 06:42:09 +0200173 /* no point closing an epoch, if it is empty, anyways. */
174 if (tconn->current_tle_writes == 0)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700175 return;
176
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100177 tconn->current_tle_writes = 0;
178 atomic_inc(&tconn->current_tle_nr);
179 wake_all_senders(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700180}
181
182void complete_master_bio(struct drbd_conf *mdev,
183 struct bio_and_error *m)
184{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700185 bio_endio(m->bio, m->error);
186 dec_ap_bio(mdev);
187}
188
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100189
190static void drbd_remove_request_interval(struct rb_root *root,
191 struct drbd_request *req)
192{
Philipp Reisnera21e9292011-02-08 15:08:49 +0100193 struct drbd_conf *mdev = req->w.mdev;
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100194 struct drbd_interval *i = &req->i;
195
196 drbd_remove_interval(root, i);
197
198 /* Wake up any processes waiting for this request to complete. */
199 if (i->waiting)
200 wake_up(&mdev->misc_wait);
201}
202
Philipp Reisnerb411b362009-09-25 16:07:19 -0700203/* Helper for __req_mod().
204 * Set m->bio to the master bio, if it is fit to be completed,
205 * or leave it alone (it is initialized to NULL in __req_mod),
206 * if it has already been completed, or cannot be completed yet.
207 * If m->bio is set, the error status to be returned is placed in m->error.
208 */
Lars Ellenberg6870ca62012-03-26 17:02:45 +0200209static
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100210void drbd_req_complete(struct drbd_request *req, struct bio_and_error *m)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700211{
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100212 const unsigned s = req->rq_state;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100213 struct drbd_conf *mdev = req->w.mdev;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100214 int rw;
215 int error, ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700216
Philipp Reisnerb411b362009-09-25 16:07:19 -0700217 /* we must not complete the master bio, while it is
218 * still being processed by _drbd_send_zc_bio (drbd_send_dblock)
219 * not yet acknowledged by the peer
220 * not yet completed by the local io subsystem
221 * these flags may get cleared in any order by
222 * the worker,
223 * the receiver,
224 * the bio_endio completion callbacks.
225 */
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100226 if ((s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED)) ||
227 (s & RQ_NET_QUEUED) || (s & RQ_NET_PENDING) ||
228 (s & RQ_COMPLETION_SUSP)) {
229 dev_err(DEV, "drbd_req_complete: Logic BUG rq_state = 0x%x\n", s);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700230 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700231 }
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100232
233 if (!req->master_bio) {
234 dev_err(DEV, "drbd_req_complete: Logic BUG, master_bio == NULL!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700235 return;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100236 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700237
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100238 rw = bio_rw(req->master_bio);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700239
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100240 /*
241 * figure out whether to report success or failure.
242 *
243 * report success when at least one of the operations succeeded.
244 * or, to put the other way,
245 * only report failure, when both operations failed.
246 *
247 * what to do about the failures is handled elsewhere.
248 * what we need to do here is just: complete the master_bio.
249 *
250 * local completion error, if any, has been stored as ERR_PTR
251 * in private_bio within drbd_request_endio.
252 */
253 ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
254 error = PTR_ERR(req->private_bio);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700255
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100256 /* remove the request from the conflict detection
257 * respective block_id verification hash */
258 if (!drbd_interval_empty(&req->i)) {
259 struct rb_root *root;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260
Philipp Reisnerb411b362009-09-25 16:07:19 -0700261 if (rw == WRITE)
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100262 root = &mdev->write_requests;
263 else
264 root = &mdev->read_requests;
265 drbd_remove_request_interval(root, req);
266 } else if (!(s & RQ_POSTPONED))
267 D_ASSERT((s & (RQ_NET_MASK & ~RQ_NET_DONE)) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700268
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100269 /* Before we can signal completion to the upper layers,
270 * we may need to close the current transfer log epoch.
271 * We are within the request lock, so we can simply compare
272 * the request epoch number with the current transfer log
273 * epoch number. If they match, increase the current_tle_nr,
274 * and reset the transfer log epoch write_cnt.
275 */
276 if (rw == WRITE &&
277 req->epoch == atomic_read(&mdev->tconn->current_tle_nr))
278 start_new_tl_epoch(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700279
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100280 /* Update disk stats */
281 _drbd_end_io_acct(mdev, req);
282
283 /* If READ failed,
284 * have it be pushed back to the retry work queue,
285 * so it will re-enter __drbd_make_request(),
286 * and be re-assigned to a suitable local or remote path,
287 * or failed if we do not have access to good data anymore.
288 *
289 * Unless it was failed early by __drbd_make_request(),
290 * because no path was available, in which case
291 * it was not even added to the transfer_log.
292 *
293 * READA may fail, and will not be retried.
294 *
295 * WRITE should have used all available paths already.
296 */
297 if (!ok && rw == READ && !list_empty(&req->tl_requests))
298 req->rq_state |= RQ_POSTPONED;
299
300 if (!(req->rq_state & RQ_POSTPONED)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700301 m->error = ok ? 0 : (error ?: -EIO);
302 m->bio = req->master_bio;
303 req->master_bio = NULL;
304 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700305}
306
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100307static int drbd_req_put_completion_ref(struct drbd_request *req, struct bio_and_error *m, int put)
Philipp Reisnercfa03412010-06-23 17:18:51 +0200308{
Philipp Reisnera21e9292011-02-08 15:08:49 +0100309 struct drbd_conf *mdev = req->w.mdev;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100310 D_ASSERT(m || (req->rq_state & RQ_POSTPONED));
Philipp Reisnercfa03412010-06-23 17:18:51 +0200311
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100312 if (!atomic_sub_and_test(put, &req->completion_ref))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700313 return 0;
314
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100315 drbd_req_complete(req, m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700316
Lars Ellenberg9a278a72012-07-24 10:12:36 +0200317 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;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700322 }
323
Philipp Reisnerb411b362009-09-25 16:07:19 -0700324 return 1;
325}
326
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100327/* 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
Philipp Reisner5af2e8c2012-08-14 11:28:52 +0200337 if (drbd_suspended(mdev) && !((s | clear) & RQ_COMPLETION_SUSP))
338 set |= RQ_COMPLETION_SUSP;
339
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100340 /* apply */
341
342 req->rq_state &= ~clear;
343 req->rq_state |= set;
344
345 /* no change? */
346 if (req->rq_state == s)
347 return;
348
349 /* intent: get references */
350
351 if (!(s & RQ_LOCAL_PENDING) && (set & RQ_LOCAL_PENDING))
352 atomic_inc(&req->completion_ref);
353
354 if (!(s & RQ_NET_PENDING) && (set & RQ_NET_PENDING)) {
355 inc_ap_pending(mdev);
356 atomic_inc(&req->completion_ref);
357 }
358
359 if (!(s & RQ_NET_QUEUED) && (set & RQ_NET_QUEUED))
360 atomic_inc(&req->completion_ref);
361
362 if (!(s & RQ_EXP_BARR_ACK) && (set & RQ_EXP_BARR_ACK))
363 kref_get(&req->kref); /* wait for the DONE */
364
365 if (!(s & RQ_NET_SENT) && (set & RQ_NET_SENT))
366 atomic_add(req->i.size >> 9, &mdev->ap_in_flight);
367
Philipp Reisner5af2e8c2012-08-14 11:28:52 +0200368 if (!(s & RQ_COMPLETION_SUSP) && (set & RQ_COMPLETION_SUSP))
369 atomic_inc(&req->completion_ref);
370
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100371 /* progress: put references */
372
373 if ((s & RQ_COMPLETION_SUSP) && (clear & RQ_COMPLETION_SUSP))
374 ++c_put;
375
376 if (!(s & RQ_LOCAL_ABORTED) && (set & RQ_LOCAL_ABORTED)) {
377 D_ASSERT(req->rq_state & RQ_LOCAL_PENDING);
378 /* local completion may still come in later,
379 * we need to keep the req object around. */
380 kref_get(&req->kref);
381 ++c_put;
382 }
383
384 if ((s & RQ_LOCAL_PENDING) && (clear & RQ_LOCAL_PENDING)) {
385 if (req->rq_state & RQ_LOCAL_ABORTED)
386 ++k_put;
387 else
388 ++c_put;
389 }
390
391 if ((s & RQ_NET_PENDING) && (clear & RQ_NET_PENDING)) {
392 dec_ap_pending(mdev);
393 ++c_put;
394 }
395
396 if ((s & RQ_NET_QUEUED) && (clear & RQ_NET_QUEUED))
397 ++c_put;
398
399 if ((s & RQ_EXP_BARR_ACK) && !(s & RQ_NET_DONE) && (set & RQ_NET_DONE)) {
400 if (req->rq_state & RQ_NET_SENT)
401 atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
402 ++k_put;
403 }
404
405 /* potentially complete and destroy */
406
407 if (k_put || c_put) {
408 /* Completion does it's own kref_put. If we are going to
409 * kref_sub below, we need req to be still around then. */
410 int at_least = k_put + !!c_put;
411 int refcount = atomic_read(&req->kref.refcount);
412 if (refcount < at_least)
413 dev_err(DEV,
414 "mod_rq_state: Logic BUG: %x -> %x: refcount = %d, should be >= %d\n",
415 s, req->rq_state, refcount, at_least);
416 }
417
418 /* If we made progress, retry conflicting peer requests, if any. */
419 if (req->i.waiting)
420 wake_up(&mdev->misc_wait);
421
422 if (c_put)
423 k_put += drbd_req_put_completion_ref(req, m, c_put);
424 if (k_put)
425 kref_sub(&req->kref, k_put, drbd_req_destroy);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700426}
427
Lars Ellenbergccae7862012-09-26 14:07:04 +0200428static void drbd_report_io_error(struct drbd_conf *mdev, struct drbd_request *req)
429{
430 char b[BDEVNAME_SIZE];
431
Lars Ellenberg42839f62012-09-27 15:19:38 +0200432 if (!__ratelimit(&drbd_ratelimit_state))
Lars Ellenbergccae7862012-09-26 14:07:04 +0200433 return;
434
435 dev_warn(DEV, "local %s IO error sector %llu+%u on %s\n",
436 (req->rq_state & RQ_WRITE) ? "WRITE" : "READ",
Lars Ellenberg42839f62012-09-27 15:19:38 +0200437 (unsigned long long)req->i.sector,
438 req->i.size >> 9,
Lars Ellenbergccae7862012-09-26 14:07:04 +0200439 bdevname(mdev->ldev->backing_bdev, b));
440}
441
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442/* obviously this could be coded as many single functions
443 * instead of one huge switch,
444 * or by putting the code directly in the respective locations
445 * (as it has been before).
446 *
447 * but having it this way
448 * enforces that it is all in this one place, where it is easier to audit,
449 * it makes it obvious that whatever "event" "happens" to a request should
450 * happen "atomically" within the req_lock,
451 * and it enforces that we have to think in a very structured manner
452 * about the "events" that may happen to a request during its life time ...
453 */
Philipp Reisner2a806992010-06-09 14:07:43 +0200454int __req_mod(struct drbd_request *req, enum drbd_req_event what,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700455 struct bio_and_error *m)
456{
Philipp Reisnera21e9292011-02-08 15:08:49 +0100457 struct drbd_conf *mdev = req->w.mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200458 struct net_conf *nc;
Philipp Reisner303d1442011-04-13 16:24:47 -0700459 int p, rv = 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100460
461 if (m)
462 m->bio = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700463
Philipp Reisnerb411b362009-09-25 16:07:19 -0700464 switch (what) {
465 default:
466 dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
467 break;
468
469 /* does not happen...
470 * initialization done in drbd_req_new
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100471 case CREATED:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700472 break;
473 */
474
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100475 case TO_BE_SENT: /* via network */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100476 /* reached via __drbd_make_request
Philipp Reisnerb411b362009-09-25 16:07:19 -0700477 * and from w_read_retry_remote */
478 D_ASSERT(!(req->rq_state & RQ_NET_MASK));
Philipp Reisner44ed1672011-04-19 17:10:19 +0200479 rcu_read_lock();
480 nc = rcu_dereference(mdev->tconn->net_conf);
481 p = nc->wire_protocol;
482 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -0700483 req->rq_state |=
484 p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
485 p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100486 mod_rq_state(req, m, 0, RQ_NET_PENDING);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700487 break;
488
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100489 case TO_BE_SUBMITTED: /* locally */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100490 /* reached via __drbd_make_request */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700491 D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100492 mod_rq_state(req, m, 0, RQ_LOCAL_PENDING);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700493 break;
494
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100495 case COMPLETED_OK:
Philipp Reisner2b4dd362011-03-14 13:01:50 +0100496 if (req->rq_state & RQ_WRITE)
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100497 mdev->writ_cnt += req->i.size >> 9;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700498 else
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100499 mdev->read_cnt += req->i.size >> 9;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700500
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100501 mod_rq_state(req, m, RQ_LOCAL_PENDING,
502 RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700503 break;
504
Philipp Reisnercdfda632011-07-05 15:38:59 +0200505 case ABORT_DISK_IO:
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100506 mod_rq_state(req, m, 0, RQ_LOCAL_ABORTED);
Philipp Reisner2b4dd362011-03-14 13:01:50 +0100507 break;
508
Lars Ellenbergedc9f5e2012-09-27 15:18:21 +0200509 case WRITE_COMPLETED_WITH_ERROR:
Lars Ellenbergccae7862012-09-26 14:07:04 +0200510 drbd_report_io_error(mdev, req);
Lars Ellenberga2a3c74f2012-09-22 12:26:57 +0200511 __drbd_chk_io_error(mdev, DRBD_WRITE_ERROR);
Lars Ellenbergedc9f5e2012-09-27 15:18:21 +0200512 mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700513 break;
514
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100515 case READ_COMPLETED_WITH_ERROR:
Andreas Gruenbacherace652a2011-01-03 17:09:58 +0100516 drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
Lars Ellenbergccae7862012-09-26 14:07:04 +0200517 drbd_report_io_error(mdev, req);
Lars Ellenberga2a3c74f2012-09-22 12:26:57 +0200518 __drbd_chk_io_error(mdev, DRBD_READ_ERROR);
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100519 /* fall through. */
520 case READ_AHEAD_COMPLETED_WITH_ERROR:
521 /* it is legal to fail READA, no __drbd_chk_io_error in that case. */
522 mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
Lars Ellenberg4439c402012-03-26 17:29:30 +0200523 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700524
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100525 case QUEUE_FOR_NET_READ:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526 /* READ or READA, and
527 * no local disk,
528 * or target area marked as invalid,
529 * or just got an io-error. */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100530 /* from __drbd_make_request
Philipp Reisnerb411b362009-09-25 16:07:19 -0700531 * or from bio_endio during read io-error recovery */
532
Lars Ellenberg6870ca62012-03-26 17:02:45 +0200533 /* So we can verify the handle in the answer packet.
534 * Corresponding drbd_remove_request_interval is in
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100535 * drbd_req_complete() */
Lars Ellenberg97ddb682011-07-15 23:52:44 +0200536 D_ASSERT(drbd_interval_empty(&req->i));
Andreas Gruenbacherdac13892011-01-21 17:18:39 +0100537 drbd_insert_interval(&mdev->read_requests, &req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700538
Lars Ellenberg83c38832009-11-03 02:22:06 +0100539 set_bit(UNPLUG_REMOTE, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540
541 D_ASSERT(req->rq_state & RQ_NET_PENDING);
Lars Ellenberg4439c402012-03-26 17:29:30 +0200542 D_ASSERT((req->rq_state & RQ_LOCAL_MASK) == 0);
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100543 mod_rq_state(req, m, 0, RQ_NET_QUEUED);
Lars Ellenberg4439c402012-03-26 17:29:30 +0200544 req->w.cb = w_send_read_req;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100545 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700546 break;
547
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100548 case QUEUE_FOR_NET_WRITE:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700549 /* assert something? */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100550 /* from __drbd_make_request only */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700551
Lars Ellenberg6870ca62012-03-26 17:02:45 +0200552 /* Corresponding drbd_remove_request_interval is in
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100553 * drbd_req_complete() */
Lars Ellenberg97ddb682011-07-15 23:52:44 +0200554 D_ASSERT(drbd_interval_empty(&req->i));
Andreas Gruenbacherde696712011-01-20 15:00:24 +0100555 drbd_insert_interval(&mdev->write_requests, &req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700556
557 /* NOTE
558 * In case the req ended up on the transfer log before being
559 * queued on the worker, it could lead to this request being
560 * missed during cleanup after connection loss.
561 * So we have to do both operations here,
562 * within the same lock that protects the transfer log.
563 *
564 * _req_add_to_epoch(req); this has to be after the
565 * _maybe_start_new_epoch(req); which happened in
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100566 * __drbd_make_request, because we now may set the bit
Philipp Reisnerb411b362009-09-25 16:07:19 -0700567 * again ourselves to close the current epoch.
568 *
569 * Add req to the (now) current epoch (barrier). */
570
Lars Ellenberg83c38832009-11-03 02:22:06 +0100571 /* otherwise we may lose an unplug, which may cause some remote
572 * io-scheduler timeout to expire, increasing maximum latency,
573 * hurting performance. */
574 set_bit(UNPLUG_REMOTE, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700575
576 /* queue work item to send data */
577 D_ASSERT(req->rq_state & RQ_NET_PENDING);
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100578 mod_rq_state(req, m, 0, RQ_NET_QUEUED|RQ_EXP_BARR_ACK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700579 req->w.cb = w_send_dblock;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100580 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700581
582 /* close the epoch, in case it outgrew the limit */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200583 rcu_read_lock();
584 nc = rcu_dereference(mdev->tconn->net_conf);
585 p = nc->max_epoch_size;
586 rcu_read_unlock();
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +0100587 if (mdev->tconn->current_tle_writes >= p)
588 start_new_tl_epoch(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700589
590 break;
591
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100592 case QUEUE_FOR_SEND_OOS:
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100593 mod_rq_state(req, m, 0, RQ_NET_QUEUED);
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +0100594 req->w.cb = w_send_out_of_sync;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100595 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200596 break;
597
Lars Ellenbergea9d6722012-03-26 16:46:39 +0200598 case READ_RETRY_REMOTE_CANCELED:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100599 case SEND_CANCELED:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100600 case SEND_FAILED:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700601 /* real cleanup will be done from tl_clear. just update flags
602 * so it is no longer marked as on the worker queue */
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100603 mod_rq_state(req, m, RQ_NET_QUEUED, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700604 break;
605
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100606 case HANDED_OVER_TO_NETWORK:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700607 /* assert something? */
608 if (bio_data_dir(req->master_bio) == WRITE &&
Philipp Reisner303d1442011-04-13 16:24:47 -0700609 !(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700610 /* this is what is dangerous about protocol A:
611 * pretend it was successfully written on the peer. */
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100612 if (req->rq_state & RQ_NET_PENDING)
613 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
614 /* else: neg-ack was faster... */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700615 /* it is still not yet RQ_NET_DONE until the
616 * corresponding epoch barrier got acked as well,
617 * so we know what to dirty on connection loss */
618 }
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100619 mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_SENT);
Lars Ellenberg6d49e102012-01-11 09:43:25 +0100620 break;
621
Lars Ellenberg27a434f2012-03-26 16:44:59 +0200622 case OOS_HANDED_TO_NETWORK:
Lars Ellenberg6d49e102012-01-11 09:43:25 +0100623 /* Was not set PENDING, no longer QUEUED, so is now DONE
624 * as far as this connection is concerned. */
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100625 mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_DONE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700626 break;
627
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100628 case CONNECTION_LOST_WHILE_PENDING:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700629 /* transfer log cleanup after connection loss */
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100630 mod_rq_state(req, m,
631 RQ_NET_OK|RQ_NET_PENDING|RQ_COMPLETION_SUSP,
632 RQ_NET_DONE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700633 break;
634
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +0200635 case CONFLICT_RESOLVED:
636 /* for superseded conflicting writes of multiple primaries,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700637 * there is no need to keep anything in the tl, potential
Lars Ellenberg934722a2012-07-24 09:31:18 +0200638 * node crashes are covered by the activity log.
639 *
640 * If this request had been marked as RQ_POSTPONED before,
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +0200641 * it will actually not be completed, but "restarted",
Lars Ellenberg934722a2012-07-24 09:31:18 +0200642 * resubmitted from the retry worker context. */
643 D_ASSERT(req->rq_state & RQ_NET_PENDING);
644 D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
645 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_DONE|RQ_NET_OK);
646 break;
647
Lars Ellenberg0afd5692012-03-26 16:51:11 +0200648 case WRITE_ACKED_BY_PEER_AND_SIS:
Lars Ellenberg934722a2012-07-24 09:31:18 +0200649 req->rq_state |= RQ_NET_SIS;
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100650 case WRITE_ACKED_BY_PEER:
Philipp Reisner303d1442011-04-13 16:24:47 -0700651 D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700652 /* protocol C; successfully written on peer.
Lars Ellenbergd64957c2012-03-23 14:42:19 +0100653 * Nothing more to do here.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700654 * We want to keep the tl in place for all protocols, to cater
Lars Ellenbergd64957c2012-03-23 14:42:19 +0100655 * for volatile write-back caches on lower level devices. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700656
Philipp Reisner303d1442011-04-13 16:24:47 -0700657 goto ack_common;
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100658 case RECV_ACKED_BY_PEER:
Philipp Reisner303d1442011-04-13 16:24:47 -0700659 D_ASSERT(req->rq_state & RQ_EXP_RECEIVE_ACK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700660 /* protocol B; pretends to be successfully written on peer.
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100661 * see also notes above in HANDED_OVER_TO_NETWORK about
Philipp Reisnerb411b362009-09-25 16:07:19 -0700662 * protocol != C */
Philipp Reisner303d1442011-04-13 16:24:47 -0700663 ack_common:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700664 D_ASSERT(req->rq_state & RQ_NET_PENDING);
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100665 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 break;
667
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100668 case POSTPONE_WRITE:
Philipp Reisner303d1442011-04-13 16:24:47 -0700669 D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
670 /* If this node has already detected the write conflict, the
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100671 * worker will be waiting on misc_wait. Wake it up once this
672 * request has completed locally.
673 */
674 D_ASSERT(req->rq_state & RQ_NET_PENDING);
675 req->rq_state |= RQ_POSTPONED;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100676 if (req->i.waiting)
677 wake_up(&mdev->misc_wait);
678 /* Do not clear RQ_NET_PENDING. This request will make further
679 * progress via restart_conflicting_writes() or
680 * fail_postponed_requests(). Hopefully. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700681 break;
682
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100683 case NEG_ACKED:
Lars Ellenberg46e21bb2012-08-07 06:47:14 +0200684 mod_rq_state(req, m, RQ_NET_OK|RQ_NET_PENDING, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700685 break;
686
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100687 case FAIL_FROZEN_DISK_IO:
Philipp Reisner265be2d2010-05-31 10:14:17 +0200688 if (!(req->rq_state & RQ_LOCAL_COMPLETED))
689 break;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100690 mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
Philipp Reisner265be2d2010-05-31 10:14:17 +0200691 break;
692
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100693 case RESTART_FROZEN_DISK_IO:
Philipp Reisner265be2d2010-05-31 10:14:17 +0200694 if (!(req->rq_state & RQ_LOCAL_COMPLETED))
695 break;
696
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100697 mod_rq_state(req, m,
698 RQ_COMPLETION_SUSP|RQ_LOCAL_COMPLETED,
699 RQ_LOCAL_PENDING);
Philipp Reisner265be2d2010-05-31 10:14:17 +0200700
701 rv = MR_READ;
702 if (bio_data_dir(req->master_bio) == WRITE)
703 rv = MR_WRITE;
704
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100705 get_ldev(mdev); /* always succeeds in this call path */
Philipp Reisner265be2d2010-05-31 10:14:17 +0200706 req->w.cb = w_restart_disk_io;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100707 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
Philipp Reisner265be2d2010-05-31 10:14:17 +0200708 break;
709
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100710 case RESEND:
Philipp Reisner509fc012012-07-31 11:22:58 +0200711 /* Simply complete (local only) READs. */
712 if (!(req->rq_state & RQ_WRITE) && !req->w.cb) {
Philipp Reisner8a0bab22012-08-07 13:28:00 +0200713 mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
Philipp Reisner509fc012012-07-31 11:22:58 +0200714 break;
715 }
716
Philipp Reisner11b58e72010-05-12 17:08:26 +0200717 /* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100718 before the connection loss (B&C only); only P_BARRIER_ACK
719 (or the local completion?) was missing when we suspended.
Lars Ellenberg6870ca62012-03-26 17:02:45 +0200720 Throwing them out of the TL here by pretending we got a BARRIER_ACK.
721 During connection handshake, we ensure that the peer was not rebooted. */
Philipp Reisner11b58e72010-05-12 17:08:26 +0200722 if (!(req->rq_state & RQ_NET_OK)) {
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100723 /* FIXME could this possibly be a req->w.cb == w_send_out_of_sync?
724 * in that case we must not set RQ_NET_PENDING. */
725
726 mod_rq_state(req, m, RQ_COMPLETION_SUSP, RQ_NET_QUEUED|RQ_NET_PENDING);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200727 if (req->w.cb) {
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100728 drbd_queue_work(&mdev->tconn->sender_work, &req->w);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200729 rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100730 } /* else: FIXME can this happen? */
Philipp Reisner11b58e72010-05-12 17:08:26 +0200731 break;
732 }
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100733 /* else, fall through to BARRIER_ACKED */
Philipp Reisner11b58e72010-05-12 17:08:26 +0200734
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100735 case BARRIER_ACKED:
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100736 /* barrier ack for READ requests does not make sense */
Philipp Reisner288f4222010-05-27 15:07:43 +0200737 if (!(req->rq_state & RQ_WRITE))
738 break;
739
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740 if (req->rq_state & RQ_NET_PENDING) {
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +0200741 /* barrier came in before all requests were acked.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700742 * this is bad, because if the connection is lost now,
743 * we won't be able to clean them up... */
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100744 dev_err(DEV, "FIXME (BARRIER_ACKED but pending)\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700745 }
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100746 /* Allowed to complete requests, even while suspended.
747 * As this is called for all requests within a matching epoch,
748 * we need to filter, and only set RQ_NET_DONE for those that
749 * have actually been on the wire. */
750 mod_rq_state(req, m, RQ_COMPLETION_SUSP,
751 (req->rq_state & RQ_NET_MASK) ? RQ_NET_DONE : 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700752 break;
753
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100754 case DATA_RECEIVED:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700755 D_ASSERT(req->rq_state & RQ_NET_PENDING);
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100756 mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK|RQ_NET_DONE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700757 break;
758 };
Philipp Reisner2a806992010-06-09 14:07:43 +0200759
760 return rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700761}
762
763/* we may do a local read if:
764 * - we are consistent (of course),
765 * - or we are generally inconsistent,
766 * BUT we are still/already IN SYNC for this area.
767 * since size may be bigger than BM_BLOCK_SIZE,
768 * we may need to check several bits.
769 */
Andreas Gruenbacher0da34df2010-12-19 20:48:29 +0100770static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700771{
772 unsigned long sbnr, ebnr;
773 sector_t esector, nr_sectors;
774
775 if (mdev->state.disk == D_UP_TO_DATE)
Andreas Gruenbacher0da34df2010-12-19 20:48:29 +0100776 return true;
Lars Ellenberg8c387de2011-02-18 14:13:07 +0100777 if (mdev->state.disk != D_INCONSISTENT)
Andreas Gruenbacher0da34df2010-12-19 20:48:29 +0100778 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700779 esector = sector + (size >> 9) - 1;
Andreas Gruenbacher8ca98442011-02-21 12:34:58 +0100780 nr_sectors = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700781 D_ASSERT(sector < nr_sectors);
782 D_ASSERT(esector < nr_sectors);
783
784 sbnr = BM_SECT_TO_BIT(sector);
785 ebnr = BM_SECT_TO_BIT(esector);
786
Andreas Gruenbacher0da34df2010-12-19 20:48:29 +0100787 return drbd_bm_count_bits(mdev, sbnr, ebnr) == 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700788}
789
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200790static bool remote_due_to_read_balancing(struct drbd_conf *mdev, sector_t sector,
791 enum drbd_read_balancing rbm)
Philipp Reisner380207d2011-11-11 12:31:20 +0100792{
Philipp Reisner380207d2011-11-11 12:31:20 +0100793 struct backing_dev_info *bdi;
Philipp Reisnerd60de032011-11-17 10:12:31 +0100794 int stripe_shift;
Philipp Reisner380207d2011-11-11 12:31:20 +0100795
Philipp Reisner380207d2011-11-11 12:31:20 +0100796 switch (rbm) {
797 case RB_CONGESTED_REMOTE:
798 bdi = &mdev->ldev->backing_bdev->bd_disk->queue->backing_dev_info;
799 return bdi_read_congested(bdi);
800 case RB_LEAST_PENDING:
801 return atomic_read(&mdev->local_cnt) >
802 atomic_read(&mdev->ap_pending_cnt) + atomic_read(&mdev->rs_pending_cnt);
Philipp Reisnerd60de032011-11-17 10:12:31 +0100803 case RB_32K_STRIPING: /* stripe_shift = 15 */
804 case RB_64K_STRIPING:
805 case RB_128K_STRIPING:
806 case RB_256K_STRIPING:
807 case RB_512K_STRIPING:
808 case RB_1M_STRIPING: /* stripe_shift = 20 */
809 stripe_shift = (rbm - RB_32K_STRIPING + 15);
810 return (sector >> (stripe_shift - 9)) & 1;
Philipp Reisner380207d2011-11-11 12:31:20 +0100811 case RB_ROUND_ROBIN:
812 return test_and_change_bit(READ_BALANCE_RR, &mdev->flags);
813 case RB_PREFER_REMOTE:
814 return true;
815 case RB_PREFER_LOCAL:
816 default:
817 return false;
818 }
819}
820
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100821/*
822 * complete_conflicting_writes - wait for any conflicting write requests
823 *
824 * The write_requests tree contains all active write requests which we
825 * currently know about. Wait for any requests to complete which conflict with
826 * the new one.
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200827 *
828 * Only way out: remove the conflicting intervals from the tree.
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100829 */
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200830static void complete_conflicting_writes(struct drbd_request *req)
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100831{
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200832 DEFINE_WAIT(wait);
833 struct drbd_conf *mdev = req->w.mdev;
834 struct drbd_interval *i;
835 sector_t sector = req->i.sector;
836 int size = req->i.size;
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100837
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200838 i = drbd_find_overlap(&mdev->write_requests, sector, size);
839 if (!i)
840 return;
841
842 for (;;) {
843 prepare_to_wait(&mdev->misc_wait, &wait, TASK_UNINTERRUPTIBLE);
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100844 i = drbd_find_overlap(&mdev->write_requests, sector, size);
845 if (!i)
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200846 break;
847 /* Indicate to wake up device->misc_wait on progress. */
848 i->waiting = true;
849 spin_unlock_irq(&mdev->tconn->req_lock);
850 schedule();
851 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100852 }
Lars Ellenberg648e46b2012-03-26 20:12:24 +0200853 finish_wait(&mdev->misc_wait, &wait);
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +0100854}
855
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200856/* called within req_lock and rcu_read_lock() */
Lars Ellenberg0d5934e2012-06-08 14:17:36 +0200857static void maybe_pull_ahead(struct drbd_conf *mdev)
858{
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200859 struct drbd_tconn *tconn = mdev->tconn;
860 struct net_conf *nc;
861 bool congested = false;
862 enum drbd_on_congestion on_congestion;
863
864 nc = rcu_dereference(tconn->net_conf);
865 on_congestion = nc ? nc->on_congestion : OC_BLOCK;
866 if (on_congestion == OC_BLOCK ||
867 tconn->agreed_pro_version < 96)
Lars Ellenberg3b9ef852012-07-30 09:06:26 +0200868 return;
Lars Ellenberg0d5934e2012-06-08 14:17:36 +0200869
870 /* If I don't even have good local storage, we can not reasonably try
871 * to pull ahead of the peer. We also need the local reference to make
872 * sure mdev->act_log is there.
Lars Ellenberg0d5934e2012-06-08 14:17:36 +0200873 */
874 if (!get_ldev_if_state(mdev, D_UP_TO_DATE))
875 return;
876
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200877 if (nc->cong_fill &&
878 atomic_read(&mdev->ap_in_flight) >= nc->cong_fill) {
Lars Ellenberg0d5934e2012-06-08 14:17:36 +0200879 dev_info(DEV, "Congestion-fill threshold reached\n");
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200880 congested = true;
Lars Ellenberg0d5934e2012-06-08 14:17:36 +0200881 }
882
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200883 if (mdev->act_log->used >= nc->cong_extents) {
Lars Ellenberg0d5934e2012-06-08 14:17:36 +0200884 dev_info(DEV, "Congestion-extents threshold reached\n");
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200885 congested = true;
Lars Ellenberg0d5934e2012-06-08 14:17:36 +0200886 }
887
888 if (congested) {
Lars Ellenberg99b4d8f2012-08-07 06:42:09 +0200889 /* start a new epoch for non-mirrored writes */
890 start_new_tl_epoch(mdev->tconn);
Lars Ellenberg0d5934e2012-06-08 14:17:36 +0200891
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200892 if (on_congestion == OC_PULL_AHEAD)
Lars Ellenberg0d5934e2012-06-08 14:17:36 +0200893 _drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200894 else /*nc->on_congestion == OC_DISCONNECT */
Lars Ellenberg0d5934e2012-06-08 14:17:36 +0200895 _drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
896 }
897 put_ldev(mdev);
898}
899
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200900/* If this returns false, and req->private_bio is still set,
901 * this should be submitted locally.
902 *
903 * If it returns false, but req->private_bio is not set,
904 * we do not have access to good data :(
905 *
906 * Otherwise, this destroys req->private_bio, if any,
907 * and returns true.
908 */
909static bool do_remote_read(struct drbd_request *req)
910{
911 struct drbd_conf *mdev = req->w.mdev;
912 enum drbd_read_balancing rbm;
913
914 if (req->private_bio) {
915 if (!drbd_may_do_local_read(mdev,
916 req->i.sector, req->i.size)) {
917 bio_put(req->private_bio);
918 req->private_bio = NULL;
919 put_ldev(mdev);
920 }
921 }
922
923 if (mdev->state.pdsk != D_UP_TO_DATE)
924 return false;
925
Lars Ellenberga0d856d2012-01-24 17:19:42 +0100926 if (req->private_bio == NULL)
927 return true;
928
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200929 /* TODO: improve read balancing decisions, take into account drbd
930 * protocol, pending requests etc. */
931
932 rcu_read_lock();
933 rbm = rcu_dereference(mdev->ldev->disk_conf)->read_balancing;
934 rcu_read_unlock();
935
936 if (rbm == RB_PREFER_LOCAL && req->private_bio)
937 return false; /* submit locally */
938
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200939 if (remote_due_to_read_balancing(mdev, req->i.sector, rbm)) {
940 if (req->private_bio) {
941 bio_put(req->private_bio);
942 req->private_bio = NULL;
943 put_ldev(mdev);
944 }
945 return true;
946 }
947
948 return false;
949}
950
951/* returns number of connections (== 1, for drbd 8.4)
952 * expected to actually write this data,
953 * which does NOT include those that we are L_AHEAD for. */
954static int drbd_process_write_request(struct drbd_request *req)
955{
956 struct drbd_conf *mdev = req->w.mdev;
957 int remote, send_oos;
958
959 rcu_read_lock();
960 remote = drbd_should_do_remote(mdev->state);
961 if (remote) {
Lars Ellenberg3b9ef852012-07-30 09:06:26 +0200962 maybe_pull_ahead(mdev);
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200963 remote = drbd_should_do_remote(mdev->state);
964 }
965 send_oos = drbd_should_send_out_of_sync(mdev->state);
966 rcu_read_unlock();
967
Lars Ellenberg519b6d32012-08-03 02:19:09 +0200968 /* Need to replicate writes. Unless it is an empty flush,
969 * which is better mapped to a DRBD P_BARRIER packet,
970 * also for drbd wire protocol compatibility reasons.
971 * If this was a flush, just start a new epoch.
972 * Unless the current epoch was empty anyways, or we are not currently
973 * replicating, in which case there is no point. */
974 if (unlikely(req->i.size == 0)) {
975 /* The only size==0 bios we expect are empty flushes. */
976 D_ASSERT(req->master_bio->bi_rw & REQ_FLUSH);
Lars Ellenberg99b4d8f2012-08-07 06:42:09 +0200977 if (remote)
Lars Ellenberg519b6d32012-08-03 02:19:09 +0200978 start_new_tl_epoch(mdev->tconn);
979 return 0;
980 }
981
Lars Ellenberg5da9c832012-03-29 17:04:14 +0200982 if (!remote && !send_oos)
983 return 0;
984
985 D_ASSERT(!(remote && send_oos));
986
987 if (remote) {
988 _req_mod(req, TO_BE_SENT);
989 _req_mod(req, QUEUE_FOR_NET_WRITE);
990 } else if (drbd_set_out_of_sync(mdev, req->i.sector, req->i.size))
991 _req_mod(req, QUEUE_FOR_SEND_OOS);
992
993 return remote;
994}
995
996static void
997drbd_submit_req_private_bio(struct drbd_request *req)
998{
999 struct drbd_conf *mdev = req->w.mdev;
1000 struct bio *bio = req->private_bio;
1001 const int rw = bio_rw(bio);
1002
1003 bio->bi_bdev = mdev->ldev->backing_bdev;
1004
1005 /* State may have changed since we grabbed our reference on the
1006 * ->ldev member. Double check, and short-circuit to endio.
1007 * In case the last activity log transaction failed to get on
1008 * stable storage, and this is a WRITE, we may not even submit
1009 * this bio. */
1010 if (get_ldev(mdev)) {
1011 if (drbd_insert_fault(mdev,
1012 rw == WRITE ? DRBD_FAULT_DT_WR
1013 : rw == READ ? DRBD_FAULT_DT_RD
1014 : DRBD_FAULT_DT_RA))
1015 bio_endio(bio, -EIO);
1016 else
1017 generic_make_request(bio);
1018 put_ldev(mdev);
1019 } else
1020 bio_endio(bio, -EIO);
1021}
1022
Lars Ellenberg779b3fe2013-03-19 18:16:54 +01001023static void drbd_queue_write(struct drbd_conf *mdev, struct drbd_request *req)
1024{
1025 spin_lock(&mdev->submit.lock);
1026 list_add_tail(&req->tl_requests, &mdev->submit.writes);
1027 spin_unlock(&mdev->submit.lock);
1028 queue_work(mdev->submit.wq, &mdev->submit.worker);
1029}
1030
Lars Ellenberg6d9febe2013-03-19 18:16:50 +01001031/* returns the new drbd_request pointer, if the caller is expected to
1032 * drbd_send_and_submit() it (to save latency), or NULL if we queued the
1033 * request on the submitter thread.
1034 * Returns ERR_PTR(-ENOMEM) if we cannot allocate a drbd_request.
1035 */
1036struct drbd_request *
1037drbd_request_prepare(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001038{
Lars Ellenberg6d9febe2013-03-19 18:16:50 +01001039 const int rw = bio_data_dir(bio);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001040 struct drbd_request *req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001041
1042 /* allocate outside of all locks; */
1043 req = drbd_req_new(mdev, bio);
1044 if (!req) {
1045 dec_ap_bio(mdev);
1046 /* only pass the error to the upper layers.
1047 * if user cannot handle io errors, that's not our business. */
1048 dev_err(DEV, "could not kmalloc() req\n");
1049 bio_endio(bio, -ENOMEM);
Lars Ellenberg6d9febe2013-03-19 18:16:50 +01001050 return ERR_PTR(-ENOMEM);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001051 }
Philipp Reisneraeda1cd62010-11-09 17:45:06 +01001052 req->start_time = start_time;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001053
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001054 if (!get_ldev(mdev)) {
1055 bio_put(req->private_bio);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001056 req->private_bio = NULL;
1057 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001058
Lars Ellenberg7e8c2882013-03-19 18:16:57 +01001059 /* Update disk stats */
1060 _drbd_start_io_acct(mdev, req);
1061
Lars Ellenberg519b6d32012-08-03 02:19:09 +02001062 if (rw == WRITE && req->private_bio && req->i.size
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001063 && !test_bit(AL_SUSPENDED, &mdev->flags)) {
Lars Ellenberg779b3fe2013-03-19 18:16:54 +01001064 if (!drbd_al_begin_io_fastpath(mdev, &req->i)) {
1065 drbd_queue_write(mdev, req);
1066 return NULL;
1067 }
Philipp Reisner07782862010-08-31 12:00:50 +02001068 req->rq_state |= RQ_IN_ACT_LOG;
Philipp Reisner07782862010-08-31 12:00:50 +02001069 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001070
Lars Ellenberg6d9febe2013-03-19 18:16:50 +01001071 return req;
1072}
1073
1074static void drbd_send_and_submit(struct drbd_conf *mdev, struct drbd_request *req)
1075{
1076 const int rw = bio_rw(req->master_bio);
1077 struct bio_and_error m = { NULL, };
1078 bool no_remote = false;
1079
Philipp Reisner87eeee42011-01-19 14:16:30 +01001080 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher6024fec2011-01-28 15:53:51 +01001081 if (rw == WRITE) {
Lars Ellenberg648e46b2012-03-26 20:12:24 +02001082 /* This may temporarily give up the req_lock,
1083 * but will re-aquire it before it returns here.
1084 * Needs to be before the check on drbd_suspended() */
1085 complete_conflicting_writes(req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001086 }
1087
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001088 /* no more giving up req_lock from now on! */
1089
Philipp Reisner2aebfab2011-03-28 16:48:11 +02001090 if (drbd_suspended(mdev)) {
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001091 /* push back and retry: */
1092 req->rq_state |= RQ_POSTPONED;
1093 if (req->private_bio) {
1094 bio_put(req->private_bio);
1095 req->private_bio = NULL;
Philipp Reisnerd7644012012-08-28 14:39:44 +02001096 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001097 }
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001098 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001099 }
1100
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001101 /* We fail READ/READA early, if we can not serve it.
1102 * We must do this before req is registered on any lists.
Lars Ellenberga0d856d2012-01-24 17:19:42 +01001103 * Otherwise, drbd_req_complete() will queue failed READ for retry. */
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001104 if (rw != WRITE) {
1105 if (!do_remote_read(req) && !req->private_bio)
1106 goto nodata;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001107 }
1108
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001109 /* which transfer log epoch does this belong to? */
1110 req->epoch = atomic_read(&mdev->tconn->current_tle_nr);
Philipp Reisner288f4222010-05-27 15:07:43 +02001111
Lars Ellenberg227f0522012-07-31 09:31:11 +02001112 /* no point in adding empty flushes to the transfer log,
1113 * they are mapped to drbd barriers already. */
Lars Ellenberg99b4d8f2012-08-07 06:42:09 +02001114 if (likely(req->i.size!=0)) {
1115 if (rw == WRITE)
1116 mdev->tconn->current_tle_writes++;
Philipp Reisner288f4222010-05-27 15:07:43 +02001117
Lars Ellenberg519b6d32012-08-03 02:19:09 +02001118 list_add_tail(&req->tl_requests, &mdev->tconn->transfer_log);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001119 }
Philipp Reisner67531712010-10-27 12:21:30 +02001120
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001121 if (rw == WRITE) {
1122 if (!drbd_process_write_request(req))
1123 no_remote = true;
1124 } else {
1125 /* We either have a private_bio, or we can read from remote.
1126 * Otherwise we had done the goto nodata above. */
1127 if (req->private_bio == NULL) {
1128 _req_mod(req, TO_BE_SENT);
1129 _req_mod(req, QUEUE_FOR_NET_READ);
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001130 } else
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001131 no_remote = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001132 }
1133
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001134 if (req->private_bio) {
1135 /* needs to be marked within the same spinlock */
1136 _req_mod(req, TO_BE_SUBMITTED);
1137 /* but we need to give up the spinlock to submit */
1138 spin_unlock_irq(&mdev->tconn->req_lock);
1139 drbd_submit_req_private_bio(req);
Lars Ellenberga0d856d2012-01-24 17:19:42 +01001140 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001141 } else if (no_remote) {
1142nodata:
1143 if (__ratelimit(&drbd_ratelimit_state))
Lars Ellenberg42839f62012-09-27 15:19:38 +02001144 dev_err(DEV, "IO ERROR: neither local nor remote data, sector %llu+%u\n",
1145 (unsigned long long)req->i.sector, req->i.size >> 9);
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001146 /* A write may have been queued for send_oos, however.
Lars Ellenberga0d856d2012-01-24 17:19:42 +01001147 * So we can not simply free it, we must go through drbd_req_put_completion_ref() */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001148 }
1149
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001150out:
Lars Ellenberga0d856d2012-01-24 17:19:42 +01001151 if (drbd_req_put_completion_ref(req, &m, 1))
1152 kref_put(&req->kref, drbd_req_destroy);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001153 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001154
Lars Ellenberg5da9c832012-03-29 17:04:14 +02001155 if (m.bio)
1156 complete_master_bio(mdev, &m);
Lars Ellenberg6d9febe2013-03-19 18:16:50 +01001157}
1158
1159void __drbd_make_request(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
1160{
1161 struct drbd_request *req = drbd_request_prepare(mdev, bio, start_time);
1162 if (IS_ERR_OR_NULL(req))
1163 return;
1164 drbd_send_and_submit(mdev, req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001165}
1166
Lars Ellenberg08a1dda2013-03-19 18:16:56 +01001167static void submit_fast_path(struct drbd_conf *mdev, struct list_head *incoming)
Lars Ellenberg113fef92013-03-22 18:14:40 -06001168{
Lars Ellenberg08a1dda2013-03-19 18:16:56 +01001169 struct drbd_request *req, *tmp;
1170 list_for_each_entry_safe(req, tmp, incoming, tl_requests) {
1171 const int rw = bio_data_dir(req->master_bio);
Lars Ellenberg113fef92013-03-22 18:14:40 -06001172
Lars Ellenberg08a1dda2013-03-19 18:16:56 +01001173 if (rw == WRITE /* rw != WRITE should not even end up here! */
1174 && req->private_bio && req->i.size
1175 && !test_bit(AL_SUSPENDED, &mdev->flags)) {
1176 if (!drbd_al_begin_io_fastpath(mdev, &req->i))
1177 continue;
1178
1179 req->rq_state |= RQ_IN_ACT_LOG;
1180 }
1181
1182 list_del_init(&req->tl_requests);
1183 drbd_send_and_submit(mdev, req);
Lars Ellenberg113fef92013-03-22 18:14:40 -06001184 }
Lars Ellenberg113fef92013-03-22 18:14:40 -06001185}
1186
Lars Ellenberg08a1dda2013-03-19 18:16:56 +01001187static bool prepare_al_transaction_nonblock(struct drbd_conf *mdev,
1188 struct list_head *incoming,
1189 struct list_head *pending)
1190{
1191 struct drbd_request *req, *tmp;
1192 int wake = 0;
1193 int err;
1194
1195 spin_lock_irq(&mdev->al_lock);
1196 list_for_each_entry_safe(req, tmp, incoming, tl_requests) {
1197 err = drbd_al_begin_io_nonblock(mdev, &req->i);
1198 if (err == -EBUSY)
1199 wake = 1;
1200 if (err)
1201 continue;
1202 req->rq_state |= RQ_IN_ACT_LOG;
1203 list_move_tail(&req->tl_requests, pending);
1204 }
1205 spin_unlock_irq(&mdev->al_lock);
1206 if (wake)
1207 wake_up(&mdev->al_wait);
1208
1209 return !list_empty(pending);
1210}
Lars Ellenberg113fef92013-03-22 18:14:40 -06001211
1212void do_submit(struct work_struct *ws)
1213{
1214 struct drbd_conf *mdev = container_of(ws, struct drbd_conf, submit.worker);
Lars Ellenberg08a1dda2013-03-19 18:16:56 +01001215 LIST_HEAD(incoming);
1216 LIST_HEAD(pending);
Lars Ellenberg113fef92013-03-22 18:14:40 -06001217 struct drbd_request *req, *tmp;
1218
Lars Ellenberg08a1dda2013-03-19 18:16:56 +01001219 for (;;) {
1220 spin_lock(&mdev->submit.lock);
1221 list_splice_tail_init(&mdev->submit.writes, &incoming);
1222 spin_unlock(&mdev->submit.lock);
Lars Ellenberg113fef92013-03-22 18:14:40 -06001223
Lars Ellenberg08a1dda2013-03-19 18:16:56 +01001224 submit_fast_path(mdev, &incoming);
1225 if (list_empty(&incoming))
1226 break;
1227
1228 wait_event(mdev->al_wait, prepare_al_transaction_nonblock(mdev, &incoming, &pending));
1229 drbd_al_begin_io_commit(mdev, false);
1230
1231 list_for_each_entry_safe(req, tmp, &pending, tl_requests) {
1232 list_del_init(&req->tl_requests);
1233 drbd_send_and_submit(mdev, req);
1234 }
Lars Ellenberg113fef92013-03-22 18:14:40 -06001235 }
1236}
1237
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001238void drbd_make_request(struct request_queue *q, struct bio *bio)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001239{
Philipp Reisnerb411b362009-09-25 16:07:19 -07001240 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
Philipp Reisneraeda1cd62010-11-09 17:45:06 +01001241 unsigned long start_time;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242
Philipp Reisneraeda1cd62010-11-09 17:45:06 +01001243 start_time = jiffies;
1244
Philipp Reisnerb411b362009-09-25 16:07:19 -07001245 /*
1246 * what we "blindly" assume:
1247 */
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01001248 D_ASSERT(IS_ALIGNED(bio->bi_size, 512));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001249
Lars Ellenberg5df69ec2012-01-24 16:49:58 +01001250 inc_ap_bio(mdev);
1251 __drbd_make_request(mdev, bio, start_time);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001252}
1253
Lars Ellenberg23361cf2011-03-31 16:36:43 +02001254/* This is called by bio_add_page().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001255 *
Lars Ellenberg23361cf2011-03-31 16:36:43 +02001256 * q->max_hw_sectors and other global limits are already enforced there.
1257 *
1258 * We need to call down to our lower level device,
1259 * in case it has special restrictions.
1260 *
1261 * We also may need to enforce configured max-bio-bvecs limits.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001262 *
1263 * As long as the BIO is empty we have to allow at least one bvec,
Lars Ellenberg23361cf2011-03-31 16:36:43 +02001264 * regardless of size and offset, so no need to ask lower levels.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001265 */
1266int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
1267{
1268 struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269 unsigned int bio_size = bvm->bi_size;
Lars Ellenberg23361cf2011-03-31 16:36:43 +02001270 int limit = DRBD_MAX_BIO_SIZE;
1271 int backing_limit;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001272
Lars Ellenberg23361cf2011-03-31 16:36:43 +02001273 if (bio_size && get_ldev(mdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274 struct request_queue * const b =
1275 mdev->ldev->backing_bdev->bd_disk->queue;
Lars Ellenberga1c88d02010-05-14 19:16:41 +02001276 if (b->merge_bvec_fn) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001277 backing_limit = b->merge_bvec_fn(b, bvm, bvec);
1278 limit = min(limit, backing_limit);
1279 }
1280 put_ldev(mdev);
1281 }
1282 return limit;
1283}
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001284
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001285struct drbd_request *find_oldest_request(struct drbd_tconn *tconn)
1286{
1287 /* Walk the transfer log,
1288 * and find the oldest not yet completed request */
1289 struct drbd_request *r;
1290 list_for_each_entry(r, &tconn->transfer_log, tl_requests) {
Lars Ellenbergb4067772012-01-24 16:58:11 +01001291 if (atomic_read(&r->completion_ref))
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001292 return r;
1293 }
1294 return NULL;
1295}
1296
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001297void request_timer_fn(unsigned long data)
1298{
1299 struct drbd_conf *mdev = (struct drbd_conf *) data;
Philipp Reisner8b924f12011-03-01 11:08:28 +01001300 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001301 struct drbd_request *req; /* oldest request */
Philipp Reisner44ed1672011-04-19 17:10:19 +02001302 struct net_conf *nc;
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001303 unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
Lars Ellenbergba280c02012-04-25 11:46:14 +02001304 unsigned long now;
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001305
Philipp Reisner44ed1672011-04-19 17:10:19 +02001306 rcu_read_lock();
1307 nc = rcu_dereference(tconn->net_conf);
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001308 if (nc && mdev->state.conn >= C_WF_REPORT_PARAMS)
1309 ent = nc->timeout * HZ/10 * nc->ko_count;
Philipp Reisnercdfda632011-07-05 15:38:59 +02001310
Lars Ellenbergba280c02012-04-25 11:46:14 +02001311 if (get_ldev(mdev)) { /* implicit state.disk >= D_INCONSISTENT */
Philipp Reisnercdfda632011-07-05 15:38:59 +02001312 dt = rcu_dereference(mdev->ldev->disk_conf)->disk_timeout * HZ / 10;
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001313 put_ldev(mdev);
1314 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02001315 rcu_read_unlock();
1316
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001317 et = min_not_zero(dt, ent);
1318
Lars Ellenbergba280c02012-04-25 11:46:14 +02001319 if (!et)
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001320 return; /* Recurring timer stopped */
1321
Lars Ellenbergba280c02012-04-25 11:46:14 +02001322 now = jiffies;
1323
Philipp Reisner8b924f12011-03-01 11:08:28 +01001324 spin_lock_irq(&tconn->req_lock);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001325 req = find_oldest_request(tconn);
1326 if (!req) {
Philipp Reisner8b924f12011-03-01 11:08:28 +01001327 spin_unlock_irq(&tconn->req_lock);
Lars Ellenbergba280c02012-04-25 11:46:14 +02001328 mod_timer(&mdev->request_timer, now + et);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001329 return;
1330 }
1331
Lars Ellenbergba280c02012-04-25 11:46:14 +02001332 /* The request is considered timed out, if
1333 * - we have some effective timeout from the configuration,
1334 * with above state restrictions applied,
1335 * - the oldest request is waiting for a response from the network
1336 * resp. the local disk,
1337 * - the oldest request is in fact older than the effective timeout,
1338 * - the connection was established (resp. disk was attached)
1339 * for longer than the timeout already.
1340 * Note that for 32bit jiffies and very stable connections/disks,
1341 * we may have a wrap around, which is catched by
1342 * !time_in_range(now, last_..._jif, last_..._jif + timeout).
1343 *
1344 * Side effect: once per 32bit wrap-around interval, which means every
1345 * ~198 days with 250 HZ, we have a window where the timeout would need
1346 * to expire twice (worst case) to become effective. Good enough.
1347 */
1348 if (ent && req->rq_state & RQ_NET_PENDING &&
1349 time_after(now, req->start_time + ent) &&
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001350 !time_in_range(now, tconn->last_reconnect_jif, tconn->last_reconnect_jif + ent)) {
Lars Ellenbergba280c02012-04-25 11:46:14 +02001351 dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
1352 _drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001353 }
Lars Ellenberg07be15b2012-05-07 11:53:08 +02001354 if (dt && req->rq_state & RQ_LOCAL_PENDING && req->w.mdev == mdev &&
Lars Ellenbergba280c02012-04-25 11:46:14 +02001355 time_after(now, req->start_time + dt) &&
1356 !time_in_range(now, mdev->last_reattach_jif, mdev->last_reattach_jif + dt)) {
1357 dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
Lars Ellenberg383606e2012-06-14 14:21:32 +02001358 __drbd_chk_io_error(mdev, DRBD_FORCE_DETACH);
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001359 }
Lars Ellenbergba280c02012-04-25 11:46:14 +02001360 nt = (time_after(now, req->start_time + et) ? now : req->start_time) + et;
Philipp Reisner8b924f12011-03-01 11:08:28 +01001361 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerdfa8bed2011-06-29 14:06:08 +02001362 mod_timer(&mdev->request_timer, nt);
Philipp Reisner7fde2be2011-03-01 11:08:28 +01001363}