xref: /openbmc/linux/drivers/block/drbd/drbd_req.c (revision ca79522c)
1 /*
2    drbd_req.c
3 
4    This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5 
6    Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7    Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8    Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9 
10    drbd is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2, or (at your option)
13    any later version.
14 
15    drbd is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with drbd; see the file COPYING.  If not, write to
22    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 
24  */
25 
26 #include <linux/module.h>
27 
28 #include <linux/slab.h>
29 #include <linux/drbd.h>
30 #include "drbd_int.h"
31 #include "drbd_req.h"
32 
33 
34 static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size);
35 
36 /* Update disk stats at start of I/O request */
37 static void _drbd_start_io_acct(struct drbd_conf *mdev, struct drbd_request *req)
38 {
39 	const int rw = bio_data_dir(req->master_bio);
40 	int cpu;
41 	cpu = part_stat_lock();
42 	part_round_stats(cpu, &mdev->vdisk->part0);
43 	part_stat_inc(cpu, &mdev->vdisk->part0, ios[rw]);
44 	part_stat_add(cpu, &mdev->vdisk->part0, sectors[rw], req->i.size >> 9);
45 	(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... */
47 	part_inc_in_flight(&mdev->vdisk->part0, rw);
48 	part_stat_unlock();
49 }
50 
51 /* Update disk stats when completing request upwards */
52 static 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);
60 	part_dec_in_flight(&mdev->vdisk->part0, rw);
61 	part_stat_unlock();
62 }
63 
64 static 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;
75 	req->w.mdev      = mdev;
76 	req->master_bio  = bio_src;
77 	req->epoch       = 0;
78 
79 	drbd_clear_interval(&req->i);
80 	req->i.sector     = bio_src->bi_sector;
81 	req->i.size      = bio_src->bi_size;
82 	req->i.local = true;
83 	req->i.waiting = false;
84 
85 	INIT_LIST_HEAD(&req->tl_requests);
86 	INIT_LIST_HEAD(&req->w.list);
87 
88 	/* one reference to be put by __drbd_make_request */
89 	atomic_set(&req->completion_ref, 1);
90 	/* one kref as long as completion_ref > 0 */
91 	kref_init(&req->kref);
92 	return req;
93 }
94 
95 void drbd_req_destroy(struct kref *kref)
96 {
97 	struct drbd_request *req = container_of(kref, struct drbd_request, kref);
98 	struct drbd_conf *mdev = req->w.mdev;
99 	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 	}
109 
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 */
116 	list_del_init(&req->tl_requests);
117 
118 	/* 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. */
121 	if (s & RQ_WRITE) {
122 		/* 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 
127 		/* 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) {
134 			if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
135 				drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
136 
137 			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 		}
140 
141 		/* one might be tempted to move the drbd_al_complete_io
142 		 * to the local io completion callback drbd_request_endio.
143 		 * 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 		 */
151 		if (s & RQ_IN_ACT_LOG) {
152 			if (get_ldev_if_state(mdev, D_FAILED)) {
153 				drbd_al_complete_io(mdev, &req->i);
154 				put_ldev(mdev);
155 			} else if (__ratelimit(&drbd_ratelimit_state)) {
156 				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);
159 			}
160 		}
161 	}
162 
163 	mempool_free(req, drbd_request_mempool);
164 }
165 
166 static void wake_all_senders(struct drbd_tconn *tconn) {
167 	wake_up(&tconn->sender_work.q_wait);
168 }
169 
170 /* must hold resource->req_lock */
171 void start_new_tl_epoch(struct drbd_tconn *tconn)
172 {
173 	/* no point closing an epoch, if it is empty, anyways. */
174 	if (tconn->current_tle_writes == 0)
175 		return;
176 
177 	tconn->current_tle_writes = 0;
178 	atomic_inc(&tconn->current_tle_nr);
179 	wake_all_senders(tconn);
180 }
181 
182 void complete_master_bio(struct drbd_conf *mdev,
183 		struct bio_and_error *m)
184 {
185 	bio_endio(m->bio, m->error);
186 	dec_ap_bio(mdev);
187 }
188 
189 
190 static void drbd_remove_request_interval(struct rb_root *root,
191 					 struct drbd_request *req)
192 {
193 	struct drbd_conf *mdev = req->w.mdev;
194 	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 
203 /* 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  */
209 static
210 void drbd_req_complete(struct drbd_request *req, struct bio_and_error *m)
211 {
212 	const unsigned s = req->rq_state;
213 	struct drbd_conf *mdev = req->w.mdev;
214 	int rw;
215 	int error, ok;
216 
217 	/* 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 	 */
226 	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);
230 		return;
231 	}
232 
233 	if (!req->master_bio) {
234 		dev_err(DEV, "drbd_req_complete: Logic BUG, master_bio == NULL!\n");
235 		return;
236 	}
237 
238 	rw = bio_rw(req->master_bio);
239 
240 	/*
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);
255 
256 	/* 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;
260 
261 		if (rw == WRITE)
262 			root = &mdev->write_requests;
263 		else
264 			root = &mdev->read_requests;
265 		drbd_remove_request_interval(root, req);
266 	}
267 
268 	/* Before we can signal completion to the upper layers,
269 	 * we may need to close the current transfer log epoch.
270 	 * We are within the request lock, so we can simply compare
271 	 * the request epoch number with the current transfer log
272 	 * epoch number.  If they match, increase the current_tle_nr,
273 	 * and reset the transfer log epoch write_cnt.
274 	 */
275 	if (rw == WRITE &&
276 	    req->epoch == atomic_read(&mdev->tconn->current_tle_nr))
277 		start_new_tl_epoch(mdev->tconn);
278 
279 	/* Update disk stats */
280 	_drbd_end_io_acct(mdev, req);
281 
282 	/* If READ failed,
283 	 * have it be pushed back to the retry work queue,
284 	 * so it will re-enter __drbd_make_request(),
285 	 * and be re-assigned to a suitable local or remote path,
286 	 * or failed if we do not have access to good data anymore.
287 	 *
288 	 * Unless it was failed early by __drbd_make_request(),
289 	 * because no path was available, in which case
290 	 * it was not even added to the transfer_log.
291 	 *
292 	 * READA may fail, and will not be retried.
293 	 *
294 	 * WRITE should have used all available paths already.
295 	 */
296 	if (!ok && rw == READ && !list_empty(&req->tl_requests))
297 		req->rq_state |= RQ_POSTPONED;
298 
299 	if (!(req->rq_state & RQ_POSTPONED)) {
300 		m->error = ok ? 0 : (error ?: -EIO);
301 		m->bio = req->master_bio;
302 		req->master_bio = NULL;
303 	}
304 }
305 
306 static int drbd_req_put_completion_ref(struct drbd_request *req, struct bio_and_error *m, int put)
307 {
308 	struct drbd_conf *mdev = req->w.mdev;
309 	D_ASSERT(m || (req->rq_state & RQ_POSTPONED));
310 
311 	if (!atomic_sub_and_test(put, &req->completion_ref))
312 		return 0;
313 
314 	drbd_req_complete(req, m);
315 
316 	if (req->rq_state & RQ_POSTPONED) {
317 		/* don't destroy the req object just yet,
318 		 * but queue it for retry */
319 		drbd_restart_request(req);
320 		return 0;
321 	}
322 
323 	return 1;
324 }
325 
326 /* I'd like this to be the only place that manipulates
327  * req->completion_ref and req->kref. */
328 static void mod_rq_state(struct drbd_request *req, struct bio_and_error *m,
329 		int clear, int set)
330 {
331 	struct drbd_conf *mdev = req->w.mdev;
332 	unsigned s = req->rq_state;
333 	int c_put = 0;
334 	int k_put = 0;
335 
336 	if (drbd_suspended(mdev) && !((s | clear) & RQ_COMPLETION_SUSP))
337 		set |= RQ_COMPLETION_SUSP;
338 
339 	/* apply */
340 
341 	req->rq_state &= ~clear;
342 	req->rq_state |= set;
343 
344 	/* no change? */
345 	if (req->rq_state == s)
346 		return;
347 
348 	/* intent: get references */
349 
350 	if (!(s & RQ_LOCAL_PENDING) && (set & RQ_LOCAL_PENDING))
351 		atomic_inc(&req->completion_ref);
352 
353 	if (!(s & RQ_NET_PENDING) && (set & RQ_NET_PENDING)) {
354 		inc_ap_pending(mdev);
355 		atomic_inc(&req->completion_ref);
356 	}
357 
358 	if (!(s & RQ_NET_QUEUED) && (set & RQ_NET_QUEUED))
359 		atomic_inc(&req->completion_ref);
360 
361 	if (!(s & RQ_EXP_BARR_ACK) && (set & RQ_EXP_BARR_ACK))
362 		kref_get(&req->kref); /* wait for the DONE */
363 
364 	if (!(s & RQ_NET_SENT) && (set & RQ_NET_SENT))
365 		atomic_add(req->i.size >> 9, &mdev->ap_in_flight);
366 
367 	if (!(s & RQ_COMPLETION_SUSP) && (set & RQ_COMPLETION_SUSP))
368 		atomic_inc(&req->completion_ref);
369 
370 	/* progress: put references */
371 
372 	if ((s & RQ_COMPLETION_SUSP) && (clear & RQ_COMPLETION_SUSP))
373 		++c_put;
374 
375 	if (!(s & RQ_LOCAL_ABORTED) && (set & RQ_LOCAL_ABORTED)) {
376 		D_ASSERT(req->rq_state & RQ_LOCAL_PENDING);
377 		/* local completion may still come in later,
378 		 * we need to keep the req object around. */
379 		kref_get(&req->kref);
380 		++c_put;
381 	}
382 
383 	if ((s & RQ_LOCAL_PENDING) && (clear & RQ_LOCAL_PENDING)) {
384 		if (req->rq_state & RQ_LOCAL_ABORTED)
385 			++k_put;
386 		else
387 			++c_put;
388 	}
389 
390 	if ((s & RQ_NET_PENDING) && (clear & RQ_NET_PENDING)) {
391 		dec_ap_pending(mdev);
392 		++c_put;
393 	}
394 
395 	if ((s & RQ_NET_QUEUED) && (clear & RQ_NET_QUEUED))
396 		++c_put;
397 
398 	if ((s & RQ_EXP_BARR_ACK) && !(s & RQ_NET_DONE) && (set & RQ_NET_DONE)) {
399 		if (req->rq_state & RQ_NET_SENT)
400 			atomic_sub(req->i.size >> 9, &mdev->ap_in_flight);
401 		++k_put;
402 	}
403 
404 	/* potentially complete and destroy */
405 
406 	if (k_put || c_put) {
407 		/* Completion does it's own kref_put.  If we are going to
408 		 * kref_sub below, we need req to be still around then. */
409 		int at_least = k_put + !!c_put;
410 		int refcount = atomic_read(&req->kref.refcount);
411 		if (refcount < at_least)
412 			dev_err(DEV,
413 				"mod_rq_state: Logic BUG: %x -> %x: refcount = %d, should be >= %d\n",
414 				s, req->rq_state, refcount, at_least);
415 	}
416 
417 	/* If we made progress, retry conflicting peer requests, if any. */
418 	if (req->i.waiting)
419 		wake_up(&mdev->misc_wait);
420 
421 	if (c_put)
422 		k_put += drbd_req_put_completion_ref(req, m, c_put);
423 	if (k_put)
424 		kref_sub(&req->kref, k_put, drbd_req_destroy);
425 }
426 
427 static void drbd_report_io_error(struct drbd_conf *mdev, struct drbd_request *req)
428 {
429         char b[BDEVNAME_SIZE];
430 
431 	if (!__ratelimit(&drbd_ratelimit_state))
432 		return;
433 
434 	dev_warn(DEV, "local %s IO error sector %llu+%u on %s\n",
435 			(req->rq_state & RQ_WRITE) ? "WRITE" : "READ",
436 			(unsigned long long)req->i.sector,
437 			req->i.size >> 9,
438 			bdevname(mdev->ldev->backing_bdev, b));
439 }
440 
441 /* obviously this could be coded as many single functions
442  * instead of one huge switch,
443  * or by putting the code directly in the respective locations
444  * (as it has been before).
445  *
446  * but having it this way
447  *  enforces that it is all in this one place, where it is easier to audit,
448  *  it makes it obvious that whatever "event" "happens" to a request should
449  *  happen "atomically" within the req_lock,
450  *  and it enforces that we have to think in a very structured manner
451  *  about the "events" that may happen to a request during its life time ...
452  */
453 int __req_mod(struct drbd_request *req, enum drbd_req_event what,
454 		struct bio_and_error *m)
455 {
456 	struct drbd_conf *mdev = req->w.mdev;
457 	struct net_conf *nc;
458 	int p, rv = 0;
459 
460 	if (m)
461 		m->bio = NULL;
462 
463 	switch (what) {
464 	default:
465 		dev_err(DEV, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
466 		break;
467 
468 	/* does not happen...
469 	 * initialization done in drbd_req_new
470 	case CREATED:
471 		break;
472 		*/
473 
474 	case TO_BE_SENT: /* via network */
475 		/* reached via __drbd_make_request
476 		 * and from w_read_retry_remote */
477 		D_ASSERT(!(req->rq_state & RQ_NET_MASK));
478 		rcu_read_lock();
479 		nc = rcu_dereference(mdev->tconn->net_conf);
480 		p = nc->wire_protocol;
481 		rcu_read_unlock();
482 		req->rq_state |=
483 			p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
484 			p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
485 		mod_rq_state(req, m, 0, RQ_NET_PENDING);
486 		break;
487 
488 	case TO_BE_SUBMITTED: /* locally */
489 		/* reached via __drbd_make_request */
490 		D_ASSERT(!(req->rq_state & RQ_LOCAL_MASK));
491 		mod_rq_state(req, m, 0, RQ_LOCAL_PENDING);
492 		break;
493 
494 	case COMPLETED_OK:
495 		if (req->rq_state & RQ_WRITE)
496 			mdev->writ_cnt += req->i.size >> 9;
497 		else
498 			mdev->read_cnt += req->i.size >> 9;
499 
500 		mod_rq_state(req, m, RQ_LOCAL_PENDING,
501 				RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
502 		break;
503 
504 	case ABORT_DISK_IO:
505 		mod_rq_state(req, m, 0, RQ_LOCAL_ABORTED);
506 		break;
507 
508 	case WRITE_COMPLETED_WITH_ERROR:
509 		drbd_report_io_error(mdev, req);
510 		__drbd_chk_io_error(mdev, DRBD_WRITE_ERROR);
511 		mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
512 		break;
513 
514 	case READ_COMPLETED_WITH_ERROR:
515 		drbd_set_out_of_sync(mdev, req->i.sector, req->i.size);
516 		drbd_report_io_error(mdev, req);
517 		__drbd_chk_io_error(mdev, DRBD_READ_ERROR);
518 		/* fall through. */
519 	case READ_AHEAD_COMPLETED_WITH_ERROR:
520 		/* it is legal to fail READA, no __drbd_chk_io_error in that case. */
521 		mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
522 		break;
523 
524 	case QUEUE_FOR_NET_READ:
525 		/* READ or READA, and
526 		 * no local disk,
527 		 * or target area marked as invalid,
528 		 * or just got an io-error. */
529 		/* from __drbd_make_request
530 		 * or from bio_endio during read io-error recovery */
531 
532 		/* So we can verify the handle in the answer packet.
533 		 * Corresponding drbd_remove_request_interval is in
534 		 * drbd_req_complete() */
535 		D_ASSERT(drbd_interval_empty(&req->i));
536 		drbd_insert_interval(&mdev->read_requests, &req->i);
537 
538 		set_bit(UNPLUG_REMOTE, &mdev->flags);
539 
540 		D_ASSERT(req->rq_state & RQ_NET_PENDING);
541 		D_ASSERT((req->rq_state & RQ_LOCAL_MASK) == 0);
542 		mod_rq_state(req, m, 0, RQ_NET_QUEUED);
543 		req->w.cb = w_send_read_req;
544 		drbd_queue_work(&mdev->tconn->sender_work, &req->w);
545 		break;
546 
547 	case QUEUE_FOR_NET_WRITE:
548 		/* assert something? */
549 		/* from __drbd_make_request only */
550 
551 		/* Corresponding drbd_remove_request_interval is in
552 		 * drbd_req_complete() */
553 		D_ASSERT(drbd_interval_empty(&req->i));
554 		drbd_insert_interval(&mdev->write_requests, &req->i);
555 
556 		/* NOTE
557 		 * In case the req ended up on the transfer log before being
558 		 * queued on the worker, it could lead to this request being
559 		 * missed during cleanup after connection loss.
560 		 * So we have to do both operations here,
561 		 * within the same lock that protects the transfer log.
562 		 *
563 		 * _req_add_to_epoch(req); this has to be after the
564 		 * _maybe_start_new_epoch(req); which happened in
565 		 * __drbd_make_request, because we now may set the bit
566 		 * again ourselves to close the current epoch.
567 		 *
568 		 * Add req to the (now) current epoch (barrier). */
569 
570 		/* otherwise we may lose an unplug, which may cause some remote
571 		 * io-scheduler timeout to expire, increasing maximum latency,
572 		 * hurting performance. */
573 		set_bit(UNPLUG_REMOTE, &mdev->flags);
574 
575 		/* queue work item to send data */
576 		D_ASSERT(req->rq_state & RQ_NET_PENDING);
577 		mod_rq_state(req, m, 0, RQ_NET_QUEUED|RQ_EXP_BARR_ACK);
578 		req->w.cb =  w_send_dblock;
579 		drbd_queue_work(&mdev->tconn->sender_work, &req->w);
580 
581 		/* close the epoch, in case it outgrew the limit */
582 		rcu_read_lock();
583 		nc = rcu_dereference(mdev->tconn->net_conf);
584 		p = nc->max_epoch_size;
585 		rcu_read_unlock();
586 		if (mdev->tconn->current_tle_writes >= p)
587 			start_new_tl_epoch(mdev->tconn);
588 
589 		break;
590 
591 	case QUEUE_FOR_SEND_OOS:
592 		mod_rq_state(req, m, 0, RQ_NET_QUEUED);
593 		req->w.cb =  w_send_out_of_sync;
594 		drbd_queue_work(&mdev->tconn->sender_work, &req->w);
595 		break;
596 
597 	case READ_RETRY_REMOTE_CANCELED:
598 	case SEND_CANCELED:
599 	case SEND_FAILED:
600 		/* real cleanup will be done from tl_clear.  just update flags
601 		 * so it is no longer marked as on the worker queue */
602 		mod_rq_state(req, m, RQ_NET_QUEUED, 0);
603 		break;
604 
605 	case HANDED_OVER_TO_NETWORK:
606 		/* assert something? */
607 		if (bio_data_dir(req->master_bio) == WRITE &&
608 		    !(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK))) {
609 			/* this is what is dangerous about protocol A:
610 			 * pretend it was successfully written on the peer. */
611 			if (req->rq_state & RQ_NET_PENDING)
612 				mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
613 			/* else: neg-ack was faster... */
614 			/* it is still not yet RQ_NET_DONE until the
615 			 * corresponding epoch barrier got acked as well,
616 			 * so we know what to dirty on connection loss */
617 		}
618 		mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_SENT);
619 		break;
620 
621 	case OOS_HANDED_TO_NETWORK:
622 		/* Was not set PENDING, no longer QUEUED, so is now DONE
623 		 * as far as this connection is concerned. */
624 		mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_DONE);
625 		break;
626 
627 	case CONNECTION_LOST_WHILE_PENDING:
628 		/* transfer log cleanup after connection loss */
629 		mod_rq_state(req, m,
630 				RQ_NET_OK|RQ_NET_PENDING|RQ_COMPLETION_SUSP,
631 				RQ_NET_DONE);
632 		break;
633 
634 	case CONFLICT_RESOLVED:
635 		/* for superseded conflicting writes of multiple primaries,
636 		 * there is no need to keep anything in the tl, potential
637 		 * node crashes are covered by the activity log.
638 		 *
639 		 * If this request had been marked as RQ_POSTPONED before,
640 		 * it will actually not be completed, but "restarted",
641 		 * resubmitted from the retry worker context. */
642 		D_ASSERT(req->rq_state & RQ_NET_PENDING);
643 		D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
644 		mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_DONE|RQ_NET_OK);
645 		break;
646 
647 	case WRITE_ACKED_BY_PEER_AND_SIS:
648 		req->rq_state |= RQ_NET_SIS;
649 	case WRITE_ACKED_BY_PEER:
650 		D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
651 		/* protocol C; successfully written on peer.
652 		 * Nothing more to do here.
653 		 * We want to keep the tl in place for all protocols, to cater
654 		 * for volatile write-back caches on lower level devices. */
655 
656 		goto ack_common;
657 	case RECV_ACKED_BY_PEER:
658 		D_ASSERT(req->rq_state & RQ_EXP_RECEIVE_ACK);
659 		/* protocol B; pretends to be successfully written on peer.
660 		 * see also notes above in HANDED_OVER_TO_NETWORK about
661 		 * protocol != C */
662 	ack_common:
663 		D_ASSERT(req->rq_state & RQ_NET_PENDING);
664 		mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
665 		break;
666 
667 	case POSTPONE_WRITE:
668 		D_ASSERT(req->rq_state & RQ_EXP_WRITE_ACK);
669 		/* If this node has already detected the write conflict, the
670 		 * worker will be waiting on misc_wait.  Wake it up once this
671 		 * request has completed locally.
672 		 */
673 		D_ASSERT(req->rq_state & RQ_NET_PENDING);
674 		req->rq_state |= RQ_POSTPONED;
675 		if (req->i.waiting)
676 			wake_up(&mdev->misc_wait);
677 		/* Do not clear RQ_NET_PENDING. This request will make further
678 		 * progress via restart_conflicting_writes() or
679 		 * fail_postponed_requests(). Hopefully. */
680 		break;
681 
682 	case NEG_ACKED:
683 		mod_rq_state(req, m, RQ_NET_OK|RQ_NET_PENDING, 0);
684 		break;
685 
686 	case FAIL_FROZEN_DISK_IO:
687 		if (!(req->rq_state & RQ_LOCAL_COMPLETED))
688 			break;
689 		mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
690 		break;
691 
692 	case RESTART_FROZEN_DISK_IO:
693 		if (!(req->rq_state & RQ_LOCAL_COMPLETED))
694 			break;
695 
696 		mod_rq_state(req, m,
697 				RQ_COMPLETION_SUSP|RQ_LOCAL_COMPLETED,
698 				RQ_LOCAL_PENDING);
699 
700 		rv = MR_READ;
701 		if (bio_data_dir(req->master_bio) == WRITE)
702 			rv = MR_WRITE;
703 
704 		get_ldev(mdev); /* always succeeds in this call path */
705 		req->w.cb = w_restart_disk_io;
706 		drbd_queue_work(&mdev->tconn->sender_work, &req->w);
707 		break;
708 
709 	case RESEND:
710 		/* Simply complete (local only) READs. */
711 		if (!(req->rq_state & RQ_WRITE) && !req->w.cb) {
712 			mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
713 			break;
714 		}
715 
716 		/* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
717 		   before the connection loss (B&C only); only P_BARRIER_ACK
718 		   (or the local completion?) was missing when we suspended.
719 		   Throwing them out of the TL here by pretending we got a BARRIER_ACK.
720 		   During connection handshake, we ensure that the peer was not rebooted. */
721 		if (!(req->rq_state & RQ_NET_OK)) {
722 			/* FIXME could this possibly be a req->w.cb == w_send_out_of_sync?
723 			 * in that case we must not set RQ_NET_PENDING. */
724 
725 			mod_rq_state(req, m, RQ_COMPLETION_SUSP, RQ_NET_QUEUED|RQ_NET_PENDING);
726 			if (req->w.cb) {
727 				drbd_queue_work(&mdev->tconn->sender_work, &req->w);
728 				rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
729 			} /* else: FIXME can this happen? */
730 			break;
731 		}
732 		/* else, fall through to BARRIER_ACKED */
733 
734 	case BARRIER_ACKED:
735 		/* barrier ack for READ requests does not make sense */
736 		if (!(req->rq_state & RQ_WRITE))
737 			break;
738 
739 		if (req->rq_state & RQ_NET_PENDING) {
740 			/* barrier came in before all requests were acked.
741 			 * this is bad, because if the connection is lost now,
742 			 * we won't be able to clean them up... */
743 			dev_err(DEV, "FIXME (BARRIER_ACKED but pending)\n");
744 		}
745 		/* Allowed to complete requests, even while suspended.
746 		 * As this is called for all requests within a matching epoch,
747 		 * we need to filter, and only set RQ_NET_DONE for those that
748 		 * have actually been on the wire. */
749 		mod_rq_state(req, m, RQ_COMPLETION_SUSP,
750 				(req->rq_state & RQ_NET_MASK) ? RQ_NET_DONE : 0);
751 		break;
752 
753 	case DATA_RECEIVED:
754 		D_ASSERT(req->rq_state & RQ_NET_PENDING);
755 		mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK|RQ_NET_DONE);
756 		break;
757 
758 	case QUEUE_AS_DRBD_BARRIER:
759 		start_new_tl_epoch(mdev->tconn);
760 		mod_rq_state(req, m, 0, RQ_NET_OK|RQ_NET_DONE);
761 		break;
762 	};
763 
764 	return rv;
765 }
766 
767 /* we may do a local read if:
768  * - we are consistent (of course),
769  * - or we are generally inconsistent,
770  *   BUT we are still/already IN SYNC for this area.
771  *   since size may be bigger than BM_BLOCK_SIZE,
772  *   we may need to check several bits.
773  */
774 static bool drbd_may_do_local_read(struct drbd_conf *mdev, sector_t sector, int size)
775 {
776 	unsigned long sbnr, ebnr;
777 	sector_t esector, nr_sectors;
778 
779 	if (mdev->state.disk == D_UP_TO_DATE)
780 		return true;
781 	if (mdev->state.disk != D_INCONSISTENT)
782 		return false;
783 	esector = sector + (size >> 9) - 1;
784 	nr_sectors = drbd_get_capacity(mdev->this_bdev);
785 	D_ASSERT(sector  < nr_sectors);
786 	D_ASSERT(esector < nr_sectors);
787 
788 	sbnr = BM_SECT_TO_BIT(sector);
789 	ebnr = BM_SECT_TO_BIT(esector);
790 
791 	return drbd_bm_count_bits(mdev, sbnr, ebnr) == 0;
792 }
793 
794 static bool remote_due_to_read_balancing(struct drbd_conf *mdev, sector_t sector,
795 		enum drbd_read_balancing rbm)
796 {
797 	struct backing_dev_info *bdi;
798 	int stripe_shift;
799 
800 	switch (rbm) {
801 	case RB_CONGESTED_REMOTE:
802 		bdi = &mdev->ldev->backing_bdev->bd_disk->queue->backing_dev_info;
803 		return bdi_read_congested(bdi);
804 	case RB_LEAST_PENDING:
805 		return atomic_read(&mdev->local_cnt) >
806 			atomic_read(&mdev->ap_pending_cnt) + atomic_read(&mdev->rs_pending_cnt);
807 	case RB_32K_STRIPING:  /* stripe_shift = 15 */
808 	case RB_64K_STRIPING:
809 	case RB_128K_STRIPING:
810 	case RB_256K_STRIPING:
811 	case RB_512K_STRIPING:
812 	case RB_1M_STRIPING:   /* stripe_shift = 20 */
813 		stripe_shift = (rbm - RB_32K_STRIPING + 15);
814 		return (sector >> (stripe_shift - 9)) & 1;
815 	case RB_ROUND_ROBIN:
816 		return test_and_change_bit(READ_BALANCE_RR, &mdev->flags);
817 	case RB_PREFER_REMOTE:
818 		return true;
819 	case RB_PREFER_LOCAL:
820 	default:
821 		return false;
822 	}
823 }
824 
825 /*
826  * complete_conflicting_writes  -  wait for any conflicting write requests
827  *
828  * The write_requests tree contains all active write requests which we
829  * currently know about.  Wait for any requests to complete which conflict with
830  * the new one.
831  *
832  * Only way out: remove the conflicting intervals from the tree.
833  */
834 static void complete_conflicting_writes(struct drbd_request *req)
835 {
836 	DEFINE_WAIT(wait);
837 	struct drbd_conf *mdev = req->w.mdev;
838 	struct drbd_interval *i;
839 	sector_t sector = req->i.sector;
840 	int size = req->i.size;
841 
842 	i = drbd_find_overlap(&mdev->write_requests, sector, size);
843 	if (!i)
844 		return;
845 
846 	for (;;) {
847 		prepare_to_wait(&mdev->misc_wait, &wait, TASK_UNINTERRUPTIBLE);
848 		i = drbd_find_overlap(&mdev->write_requests, sector, size);
849 		if (!i)
850 			break;
851 		/* Indicate to wake up device->misc_wait on progress.  */
852 		i->waiting = true;
853 		spin_unlock_irq(&mdev->tconn->req_lock);
854 		schedule();
855 		spin_lock_irq(&mdev->tconn->req_lock);
856 	}
857 	finish_wait(&mdev->misc_wait, &wait);
858 }
859 
860 /* called within req_lock and rcu_read_lock() */
861 static void maybe_pull_ahead(struct drbd_conf *mdev)
862 {
863 	struct drbd_tconn *tconn = mdev->tconn;
864 	struct net_conf *nc;
865 	bool congested = false;
866 	enum drbd_on_congestion on_congestion;
867 
868 	rcu_read_lock();
869 	nc = rcu_dereference(tconn->net_conf);
870 	on_congestion = nc ? nc->on_congestion : OC_BLOCK;
871 	rcu_read_unlock();
872 	if (on_congestion == OC_BLOCK ||
873 	    tconn->agreed_pro_version < 96)
874 		return;
875 
876 	/* If I don't even have good local storage, we can not reasonably try
877 	 * to pull ahead of the peer. We also need the local reference to make
878 	 * sure mdev->act_log is there.
879 	 */
880 	if (!get_ldev_if_state(mdev, D_UP_TO_DATE))
881 		return;
882 
883 	if (nc->cong_fill &&
884 	    atomic_read(&mdev->ap_in_flight) >= nc->cong_fill) {
885 		dev_info(DEV, "Congestion-fill threshold reached\n");
886 		congested = true;
887 	}
888 
889 	if (mdev->act_log->used >= nc->cong_extents) {
890 		dev_info(DEV, "Congestion-extents threshold reached\n");
891 		congested = true;
892 	}
893 
894 	if (congested) {
895 		/* start a new epoch for non-mirrored writes */
896 		start_new_tl_epoch(mdev->tconn);
897 
898 		if (on_congestion == OC_PULL_AHEAD)
899 			_drbd_set_state(_NS(mdev, conn, C_AHEAD), 0, NULL);
900 		else  /*nc->on_congestion == OC_DISCONNECT */
901 			_drbd_set_state(_NS(mdev, conn, C_DISCONNECTING), 0, NULL);
902 	}
903 	put_ldev(mdev);
904 }
905 
906 /* If this returns false, and req->private_bio is still set,
907  * this should be submitted locally.
908  *
909  * If it returns false, but req->private_bio is not set,
910  * we do not have access to good data :(
911  *
912  * Otherwise, this destroys req->private_bio, if any,
913  * and returns true.
914  */
915 static bool do_remote_read(struct drbd_request *req)
916 {
917 	struct drbd_conf *mdev = req->w.mdev;
918 	enum drbd_read_balancing rbm;
919 
920 	if (req->private_bio) {
921 		if (!drbd_may_do_local_read(mdev,
922 					req->i.sector, req->i.size)) {
923 			bio_put(req->private_bio);
924 			req->private_bio = NULL;
925 			put_ldev(mdev);
926 		}
927 	}
928 
929 	if (mdev->state.pdsk != D_UP_TO_DATE)
930 		return false;
931 
932 	if (req->private_bio == NULL)
933 		return true;
934 
935 	/* TODO: improve read balancing decisions, take into account drbd
936 	 * protocol, pending requests etc. */
937 
938 	rcu_read_lock();
939 	rbm = rcu_dereference(mdev->ldev->disk_conf)->read_balancing;
940 	rcu_read_unlock();
941 
942 	if (rbm == RB_PREFER_LOCAL && req->private_bio)
943 		return false; /* submit locally */
944 
945 	if (remote_due_to_read_balancing(mdev, req->i.sector, rbm)) {
946 		if (req->private_bio) {
947 			bio_put(req->private_bio);
948 			req->private_bio = NULL;
949 			put_ldev(mdev);
950 		}
951 		return true;
952 	}
953 
954 	return false;
955 }
956 
957 /* returns number of connections (== 1, for drbd 8.4)
958  * expected to actually write this data,
959  * which does NOT include those that we are L_AHEAD for. */
960 static int drbd_process_write_request(struct drbd_request *req)
961 {
962 	struct drbd_conf *mdev = req->w.mdev;
963 	int remote, send_oos;
964 
965 	remote = drbd_should_do_remote(mdev->state);
966 	send_oos = drbd_should_send_out_of_sync(mdev->state);
967 
968 	/* 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);
977 		if (remote)
978 			_req_mod(req, QUEUE_AS_DRBD_BARRIER);
979 		return remote;
980 	}
981 
982 	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 
996 static void
997 drbd_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 
1023 static 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 
1031 /* 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  */
1036 struct drbd_request *
1037 drbd_request_prepare(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
1038 {
1039 	const int rw = bio_data_dir(bio);
1040 	struct drbd_request *req;
1041 
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);
1050 		return ERR_PTR(-ENOMEM);
1051 	}
1052 	req->start_time = start_time;
1053 
1054 	if (!get_ldev(mdev)) {
1055 		bio_put(req->private_bio);
1056 		req->private_bio = NULL;
1057 	}
1058 
1059 	/* Update disk stats */
1060 	_drbd_start_io_acct(mdev, req);
1061 
1062 	if (rw == WRITE && req->private_bio && req->i.size
1063 	&& !test_bit(AL_SUSPENDED, &mdev->flags)) {
1064 		if (!drbd_al_begin_io_fastpath(mdev, &req->i)) {
1065 			drbd_queue_write(mdev, req);
1066 			return NULL;
1067 		}
1068 		req->rq_state |= RQ_IN_ACT_LOG;
1069 	}
1070 
1071 	return req;
1072 }
1073 
1074 static 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 
1080 	spin_lock_irq(&mdev->tconn->req_lock);
1081 	if (rw == WRITE) {
1082 		/* 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);
1086 		/* no more giving up req_lock from now on! */
1087 
1088 		/* check for congestion, and potentially stop sending
1089 		 * full data updates, but start sending "dirty bits" only. */
1090 		maybe_pull_ahead(mdev);
1091 	}
1092 
1093 
1094 	if (drbd_suspended(mdev)) {
1095 		/* push back and retry: */
1096 		req->rq_state |= RQ_POSTPONED;
1097 		if (req->private_bio) {
1098 			bio_put(req->private_bio);
1099 			req->private_bio = NULL;
1100 			put_ldev(mdev);
1101 		}
1102 		goto out;
1103 	}
1104 
1105 	/* We fail READ/READA early, if we can not serve it.
1106 	 * We must do this before req is registered on any lists.
1107 	 * Otherwise, drbd_req_complete() will queue failed READ for retry. */
1108 	if (rw != WRITE) {
1109 		if (!do_remote_read(req) && !req->private_bio)
1110 			goto nodata;
1111 	}
1112 
1113 	/* which transfer log epoch does this belong to? */
1114 	req->epoch = atomic_read(&mdev->tconn->current_tle_nr);
1115 
1116 	/* no point in adding empty flushes to the transfer log,
1117 	 * they are mapped to drbd barriers already. */
1118 	if (likely(req->i.size!=0)) {
1119 		if (rw == WRITE)
1120 			mdev->tconn->current_tle_writes++;
1121 
1122 		list_add_tail(&req->tl_requests, &mdev->tconn->transfer_log);
1123 	}
1124 
1125 	if (rw == WRITE) {
1126 		if (!drbd_process_write_request(req))
1127 			no_remote = true;
1128 	} else {
1129 		/* We either have a private_bio, or we can read from remote.
1130 		 * Otherwise we had done the goto nodata above. */
1131 		if (req->private_bio == NULL) {
1132 			_req_mod(req, TO_BE_SENT);
1133 			_req_mod(req, QUEUE_FOR_NET_READ);
1134 		} else
1135 			no_remote = true;
1136 	}
1137 
1138 	if (req->private_bio) {
1139 		/* needs to be marked within the same spinlock */
1140 		_req_mod(req, TO_BE_SUBMITTED);
1141 		/* but we need to give up the spinlock to submit */
1142 		spin_unlock_irq(&mdev->tconn->req_lock);
1143 		drbd_submit_req_private_bio(req);
1144 		spin_lock_irq(&mdev->tconn->req_lock);
1145 	} else if (no_remote) {
1146 nodata:
1147 		if (__ratelimit(&drbd_ratelimit_state))
1148 			dev_err(DEV, "IO ERROR: neither local nor remote data, sector %llu+%u\n",
1149 					(unsigned long long)req->i.sector, req->i.size >> 9);
1150 		/* A write may have been queued for send_oos, however.
1151 		 * So we can not simply free it, we must go through drbd_req_put_completion_ref() */
1152 	}
1153 
1154 out:
1155 	if (drbd_req_put_completion_ref(req, &m, 1))
1156 		kref_put(&req->kref, drbd_req_destroy);
1157 	spin_unlock_irq(&mdev->tconn->req_lock);
1158 
1159 	if (m.bio)
1160 		complete_master_bio(mdev, &m);
1161 }
1162 
1163 void __drbd_make_request(struct drbd_conf *mdev, struct bio *bio, unsigned long start_time)
1164 {
1165 	struct drbd_request *req = drbd_request_prepare(mdev, bio, start_time);
1166 	if (IS_ERR_OR_NULL(req))
1167 		return;
1168 	drbd_send_and_submit(mdev, req);
1169 }
1170 
1171 static void submit_fast_path(struct drbd_conf *mdev, struct list_head *incoming)
1172 {
1173 	struct drbd_request *req, *tmp;
1174 	list_for_each_entry_safe(req, tmp, incoming, tl_requests) {
1175 		const int rw = bio_data_dir(req->master_bio);
1176 
1177 		if (rw == WRITE /* rw != WRITE should not even end up here! */
1178 		&& req->private_bio && req->i.size
1179 		&& !test_bit(AL_SUSPENDED, &mdev->flags)) {
1180 			if (!drbd_al_begin_io_fastpath(mdev, &req->i))
1181 				continue;
1182 
1183 			req->rq_state |= RQ_IN_ACT_LOG;
1184 		}
1185 
1186 		list_del_init(&req->tl_requests);
1187 		drbd_send_and_submit(mdev, req);
1188 	}
1189 }
1190 
1191 static bool prepare_al_transaction_nonblock(struct drbd_conf *mdev,
1192 					    struct list_head *incoming,
1193 					    struct list_head *pending)
1194 {
1195 	struct drbd_request *req, *tmp;
1196 	int wake = 0;
1197 	int err;
1198 
1199 	spin_lock_irq(&mdev->al_lock);
1200 	list_for_each_entry_safe(req, tmp, incoming, tl_requests) {
1201 		err = drbd_al_begin_io_nonblock(mdev, &req->i);
1202 		if (err == -EBUSY)
1203 			wake = 1;
1204 		if (err)
1205 			continue;
1206 		req->rq_state |= RQ_IN_ACT_LOG;
1207 		list_move_tail(&req->tl_requests, pending);
1208 	}
1209 	spin_unlock_irq(&mdev->al_lock);
1210 	if (wake)
1211 		wake_up(&mdev->al_wait);
1212 
1213 	return !list_empty(pending);
1214 }
1215 
1216 void do_submit(struct work_struct *ws)
1217 {
1218 	struct drbd_conf *mdev = container_of(ws, struct drbd_conf, submit.worker);
1219 	LIST_HEAD(incoming);
1220 	LIST_HEAD(pending);
1221 	struct drbd_request *req, *tmp;
1222 
1223 	for (;;) {
1224 		spin_lock(&mdev->submit.lock);
1225 		list_splice_tail_init(&mdev->submit.writes, &incoming);
1226 		spin_unlock(&mdev->submit.lock);
1227 
1228 		submit_fast_path(mdev, &incoming);
1229 		if (list_empty(&incoming))
1230 			break;
1231 
1232 		wait_event(mdev->al_wait, prepare_al_transaction_nonblock(mdev, &incoming, &pending));
1233 		/* Maybe more was queued, while we prepared the transaction?
1234 		 * Try to stuff them into this transaction as well.
1235 		 * Be strictly non-blocking here, no wait_event, we already
1236 		 * have something to commit.
1237 		 * Stop if we don't make any more progres.
1238 		 */
1239 		for (;;) {
1240 			LIST_HEAD(more_pending);
1241 			LIST_HEAD(more_incoming);
1242 			bool made_progress;
1243 
1244 			/* It is ok to look outside the lock,
1245 			 * it's only an optimization anyways */
1246 			if (list_empty(&mdev->submit.writes))
1247 				break;
1248 
1249 			spin_lock(&mdev->submit.lock);
1250 			list_splice_tail_init(&mdev->submit.writes, &more_incoming);
1251 			spin_unlock(&mdev->submit.lock);
1252 
1253 			if (list_empty(&more_incoming))
1254 				break;
1255 
1256 			made_progress = prepare_al_transaction_nonblock(mdev, &more_incoming, &more_pending);
1257 
1258 			list_splice_tail_init(&more_pending, &pending);
1259 			list_splice_tail_init(&more_incoming, &incoming);
1260 
1261 			if (!made_progress)
1262 				break;
1263 		}
1264 		drbd_al_begin_io_commit(mdev, false);
1265 
1266 		list_for_each_entry_safe(req, tmp, &pending, tl_requests) {
1267 			list_del_init(&req->tl_requests);
1268 			drbd_send_and_submit(mdev, req);
1269 		}
1270 	}
1271 }
1272 
1273 void drbd_make_request(struct request_queue *q, struct bio *bio)
1274 {
1275 	struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
1276 	unsigned long start_time;
1277 
1278 	start_time = jiffies;
1279 
1280 	/*
1281 	 * what we "blindly" assume:
1282 	 */
1283 	D_ASSERT(IS_ALIGNED(bio->bi_size, 512));
1284 
1285 	inc_ap_bio(mdev);
1286 	__drbd_make_request(mdev, bio, start_time);
1287 }
1288 
1289 /* This is called by bio_add_page().
1290  *
1291  * q->max_hw_sectors and other global limits are already enforced there.
1292  *
1293  * We need to call down to our lower level device,
1294  * in case it has special restrictions.
1295  *
1296  * We also may need to enforce configured max-bio-bvecs limits.
1297  *
1298  * As long as the BIO is empty we have to allow at least one bvec,
1299  * regardless of size and offset, so no need to ask lower levels.
1300  */
1301 int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec)
1302 {
1303 	struct drbd_conf *mdev = (struct drbd_conf *) q->queuedata;
1304 	unsigned int bio_size = bvm->bi_size;
1305 	int limit = DRBD_MAX_BIO_SIZE;
1306 	int backing_limit;
1307 
1308 	if (bio_size && get_ldev(mdev)) {
1309 		struct request_queue * const b =
1310 			mdev->ldev->backing_bdev->bd_disk->queue;
1311 		if (b->merge_bvec_fn) {
1312 			backing_limit = b->merge_bvec_fn(b, bvm, bvec);
1313 			limit = min(limit, backing_limit);
1314 		}
1315 		put_ldev(mdev);
1316 	}
1317 	return limit;
1318 }
1319 
1320 struct drbd_request *find_oldest_request(struct drbd_tconn *tconn)
1321 {
1322 	/* Walk the transfer log,
1323 	 * and find the oldest not yet completed request */
1324 	struct drbd_request *r;
1325 	list_for_each_entry(r, &tconn->transfer_log, tl_requests) {
1326 		if (atomic_read(&r->completion_ref))
1327 			return r;
1328 	}
1329 	return NULL;
1330 }
1331 
1332 void request_timer_fn(unsigned long data)
1333 {
1334 	struct drbd_conf *mdev = (struct drbd_conf *) data;
1335 	struct drbd_tconn *tconn = mdev->tconn;
1336 	struct drbd_request *req; /* oldest request */
1337 	struct net_conf *nc;
1338 	unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
1339 	unsigned long now;
1340 
1341 	rcu_read_lock();
1342 	nc = rcu_dereference(tconn->net_conf);
1343 	if (nc && mdev->state.conn >= C_WF_REPORT_PARAMS)
1344 		ent = nc->timeout * HZ/10 * nc->ko_count;
1345 
1346 	if (get_ldev(mdev)) { /* implicit state.disk >= D_INCONSISTENT */
1347 		dt = rcu_dereference(mdev->ldev->disk_conf)->disk_timeout * HZ / 10;
1348 		put_ldev(mdev);
1349 	}
1350 	rcu_read_unlock();
1351 
1352 	et = min_not_zero(dt, ent);
1353 
1354 	if (!et)
1355 		return; /* Recurring timer stopped */
1356 
1357 	now = jiffies;
1358 
1359 	spin_lock_irq(&tconn->req_lock);
1360 	req = find_oldest_request(tconn);
1361 	if (!req) {
1362 		spin_unlock_irq(&tconn->req_lock);
1363 		mod_timer(&mdev->request_timer, now + et);
1364 		return;
1365 	}
1366 
1367 	/* The request is considered timed out, if
1368 	 * - we have some effective timeout from the configuration,
1369 	 *   with above state restrictions applied,
1370 	 * - the oldest request is waiting for a response from the network
1371 	 *   resp. the local disk,
1372 	 * - the oldest request is in fact older than the effective timeout,
1373 	 * - the connection was established (resp. disk was attached)
1374 	 *   for longer than the timeout already.
1375 	 * Note that for 32bit jiffies and very stable connections/disks,
1376 	 * we may have a wrap around, which is catched by
1377 	 *   !time_in_range(now, last_..._jif, last_..._jif + timeout).
1378 	 *
1379 	 * Side effect: once per 32bit wrap-around interval, which means every
1380 	 * ~198 days with 250 HZ, we have a window where the timeout would need
1381 	 * to expire twice (worst case) to become effective. Good enough.
1382 	 */
1383 	if (ent && req->rq_state & RQ_NET_PENDING &&
1384 		 time_after(now, req->start_time + ent) &&
1385 		!time_in_range(now, tconn->last_reconnect_jif, tconn->last_reconnect_jif + ent)) {
1386 		dev_warn(DEV, "Remote failed to finish a request within ko-count * timeout\n");
1387 		_drbd_set_state(_NS(mdev, conn, C_TIMEOUT), CS_VERBOSE | CS_HARD, NULL);
1388 	}
1389 	if (dt && req->rq_state & RQ_LOCAL_PENDING && req->w.mdev == mdev &&
1390 		 time_after(now, req->start_time + dt) &&
1391 		!time_in_range(now, mdev->last_reattach_jif, mdev->last_reattach_jif + dt)) {
1392 		dev_warn(DEV, "Local backing device failed to meet the disk-timeout\n");
1393 		__drbd_chk_io_error(mdev, DRBD_FORCE_DETACH);
1394 	}
1395 	nt = (time_after(now, req->start_time + et) ? now : req->start_time) + et;
1396 	spin_unlock_irq(&tconn->req_lock);
1397 	mod_timer(&mdev->request_timer, nt);
1398 }
1399