xref: /OK3568_Linux_fs/kernel/drivers/block/drbd/drbd_req.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun    drbd_req.c
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun    This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun    Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
8*4882a593Smuzhiyun    Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
9*4882a593Smuzhiyun    Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/drbd.h>
18*4882a593Smuzhiyun #include "drbd_int.h"
19*4882a593Smuzhiyun #include "drbd_req.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun static bool drbd_may_do_local_read(struct drbd_device *device, sector_t sector, int size);
23*4882a593Smuzhiyun 
drbd_req_new(struct drbd_device * device,struct bio * bio_src)24*4882a593Smuzhiyun static struct drbd_request *drbd_req_new(struct drbd_device *device, struct bio *bio_src)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun 	struct drbd_request *req;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	req = mempool_alloc(&drbd_request_mempool, GFP_NOIO);
29*4882a593Smuzhiyun 	if (!req)
30*4882a593Smuzhiyun 		return NULL;
31*4882a593Smuzhiyun 	memset(req, 0, sizeof(*req));
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	drbd_req_make_private_bio(req, bio_src);
34*4882a593Smuzhiyun 	req->rq_state = (bio_data_dir(bio_src) == WRITE ? RQ_WRITE : 0)
35*4882a593Smuzhiyun 		      | (bio_op(bio_src) == REQ_OP_WRITE_SAME ? RQ_WSAME : 0)
36*4882a593Smuzhiyun 		      | (bio_op(bio_src) == REQ_OP_WRITE_ZEROES ? RQ_ZEROES : 0)
37*4882a593Smuzhiyun 		      | (bio_op(bio_src) == REQ_OP_DISCARD ? RQ_UNMAP : 0);
38*4882a593Smuzhiyun 	req->device = device;
39*4882a593Smuzhiyun 	req->master_bio = bio_src;
40*4882a593Smuzhiyun 	req->epoch = 0;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	drbd_clear_interval(&req->i);
43*4882a593Smuzhiyun 	req->i.sector     = bio_src->bi_iter.bi_sector;
44*4882a593Smuzhiyun 	req->i.size      = bio_src->bi_iter.bi_size;
45*4882a593Smuzhiyun 	req->i.local = true;
46*4882a593Smuzhiyun 	req->i.waiting = false;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	INIT_LIST_HEAD(&req->tl_requests);
49*4882a593Smuzhiyun 	INIT_LIST_HEAD(&req->w.list);
50*4882a593Smuzhiyun 	INIT_LIST_HEAD(&req->req_pending_master_completion);
51*4882a593Smuzhiyun 	INIT_LIST_HEAD(&req->req_pending_local);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	/* one reference to be put by __drbd_make_request */
54*4882a593Smuzhiyun 	atomic_set(&req->completion_ref, 1);
55*4882a593Smuzhiyun 	/* one kref as long as completion_ref > 0 */
56*4882a593Smuzhiyun 	kref_init(&req->kref);
57*4882a593Smuzhiyun 	return req;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
drbd_remove_request_interval(struct rb_root * root,struct drbd_request * req)60*4882a593Smuzhiyun static void drbd_remove_request_interval(struct rb_root *root,
61*4882a593Smuzhiyun 					 struct drbd_request *req)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	struct drbd_device *device = req->device;
64*4882a593Smuzhiyun 	struct drbd_interval *i = &req->i;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	drbd_remove_interval(root, i);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	/* Wake up any processes waiting for this request to complete.  */
69*4882a593Smuzhiyun 	if (i->waiting)
70*4882a593Smuzhiyun 		wake_up(&device->misc_wait);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
drbd_req_destroy(struct kref * kref)73*4882a593Smuzhiyun void drbd_req_destroy(struct kref *kref)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	struct drbd_request *req = container_of(kref, struct drbd_request, kref);
76*4882a593Smuzhiyun 	struct drbd_device *device = req->device;
77*4882a593Smuzhiyun 	const unsigned s = req->rq_state;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	if ((req->master_bio && !(s & RQ_POSTPONED)) ||
80*4882a593Smuzhiyun 		atomic_read(&req->completion_ref) ||
81*4882a593Smuzhiyun 		(s & RQ_LOCAL_PENDING) ||
82*4882a593Smuzhiyun 		((s & RQ_NET_MASK) && !(s & RQ_NET_DONE))) {
83*4882a593Smuzhiyun 		drbd_err(device, "drbd_req_destroy: Logic BUG rq_state = 0x%x, completion_ref = %d\n",
84*4882a593Smuzhiyun 				s, atomic_read(&req->completion_ref));
85*4882a593Smuzhiyun 		return;
86*4882a593Smuzhiyun 	}
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	/* If called from mod_rq_state (expected normal case) or
89*4882a593Smuzhiyun 	 * drbd_send_and_submit (the less likely normal path), this holds the
90*4882a593Smuzhiyun 	 * req_lock, and req->tl_requests will typicaly be on ->transfer_log,
91*4882a593Smuzhiyun 	 * though it may be still empty (never added to the transfer log).
92*4882a593Smuzhiyun 	 *
93*4882a593Smuzhiyun 	 * If called from do_retry(), we do NOT hold the req_lock, but we are
94*4882a593Smuzhiyun 	 * still allowed to unconditionally list_del(&req->tl_requests),
95*4882a593Smuzhiyun 	 * because it will be on a local on-stack list only. */
96*4882a593Smuzhiyun 	list_del_init(&req->tl_requests);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	/* finally remove the request from the conflict detection
99*4882a593Smuzhiyun 	 * respective block_id verification interval tree. */
100*4882a593Smuzhiyun 	if (!drbd_interval_empty(&req->i)) {
101*4882a593Smuzhiyun 		struct rb_root *root;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 		if (s & RQ_WRITE)
104*4882a593Smuzhiyun 			root = &device->write_requests;
105*4882a593Smuzhiyun 		else
106*4882a593Smuzhiyun 			root = &device->read_requests;
107*4882a593Smuzhiyun 		drbd_remove_request_interval(root, req);
108*4882a593Smuzhiyun 	} else if (s & (RQ_NET_MASK & ~RQ_NET_DONE) && req->i.size != 0)
109*4882a593Smuzhiyun 		drbd_err(device, "drbd_req_destroy: Logic BUG: interval empty, but: rq_state=0x%x, sect=%llu, size=%u\n",
110*4882a593Smuzhiyun 			s, (unsigned long long)req->i.sector, req->i.size);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/* if it was a write, we may have to set the corresponding
113*4882a593Smuzhiyun 	 * bit(s) out-of-sync first. If it had a local part, we need to
114*4882a593Smuzhiyun 	 * release the reference to the activity log. */
115*4882a593Smuzhiyun 	if (s & RQ_WRITE) {
116*4882a593Smuzhiyun 		/* Set out-of-sync unless both OK flags are set
117*4882a593Smuzhiyun 		 * (local only or remote failed).
118*4882a593Smuzhiyun 		 * Other places where we set out-of-sync:
119*4882a593Smuzhiyun 		 * READ with local io-error */
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 		/* There is a special case:
122*4882a593Smuzhiyun 		 * we may notice late that IO was suspended,
123*4882a593Smuzhiyun 		 * and postpone, or schedule for retry, a write,
124*4882a593Smuzhiyun 		 * before it even was submitted or sent.
125*4882a593Smuzhiyun 		 * In that case we do not want to touch the bitmap at all.
126*4882a593Smuzhiyun 		 */
127*4882a593Smuzhiyun 		if ((s & (RQ_POSTPONED|RQ_LOCAL_MASK|RQ_NET_MASK)) != RQ_POSTPONED) {
128*4882a593Smuzhiyun 			if (!(s & RQ_NET_OK) || !(s & RQ_LOCAL_OK))
129*4882a593Smuzhiyun 				drbd_set_out_of_sync(device, req->i.sector, req->i.size);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 			if ((s & RQ_NET_OK) && (s & RQ_LOCAL_OK) && (s & RQ_NET_SIS))
132*4882a593Smuzhiyun 				drbd_set_in_sync(device, req->i.sector, req->i.size);
133*4882a593Smuzhiyun 		}
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 		/* one might be tempted to move the drbd_al_complete_io
136*4882a593Smuzhiyun 		 * to the local io completion callback drbd_request_endio.
137*4882a593Smuzhiyun 		 * but, if this was a mirror write, we may only
138*4882a593Smuzhiyun 		 * drbd_al_complete_io after this is RQ_NET_DONE,
139*4882a593Smuzhiyun 		 * otherwise the extent could be dropped from the al
140*4882a593Smuzhiyun 		 * before it has actually been written on the peer.
141*4882a593Smuzhiyun 		 * if we crash before our peer knows about the request,
142*4882a593Smuzhiyun 		 * but after the extent has been dropped from the al,
143*4882a593Smuzhiyun 		 * we would forget to resync the corresponding extent.
144*4882a593Smuzhiyun 		 */
145*4882a593Smuzhiyun 		if (s & RQ_IN_ACT_LOG) {
146*4882a593Smuzhiyun 			if (get_ldev_if_state(device, D_FAILED)) {
147*4882a593Smuzhiyun 				drbd_al_complete_io(device, &req->i);
148*4882a593Smuzhiyun 				put_ldev(device);
149*4882a593Smuzhiyun 			} else if (__ratelimit(&drbd_ratelimit_state)) {
150*4882a593Smuzhiyun 				drbd_warn(device, "Should have called drbd_al_complete_io(, %llu, %u), "
151*4882a593Smuzhiyun 					 "but my Disk seems to have failed :(\n",
152*4882a593Smuzhiyun 					 (unsigned long long) req->i.sector, req->i.size);
153*4882a593Smuzhiyun 			}
154*4882a593Smuzhiyun 		}
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	mempool_free(req, &drbd_request_mempool);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
wake_all_senders(struct drbd_connection * connection)160*4882a593Smuzhiyun static void wake_all_senders(struct drbd_connection *connection)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	wake_up(&connection->sender_work.q_wait);
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /* must hold resource->req_lock */
start_new_tl_epoch(struct drbd_connection * connection)166*4882a593Smuzhiyun void start_new_tl_epoch(struct drbd_connection *connection)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	/* no point closing an epoch, if it is empty, anyways. */
169*4882a593Smuzhiyun 	if (connection->current_tle_writes == 0)
170*4882a593Smuzhiyun 		return;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	connection->current_tle_writes = 0;
173*4882a593Smuzhiyun 	atomic_inc(&connection->current_tle_nr);
174*4882a593Smuzhiyun 	wake_all_senders(connection);
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
complete_master_bio(struct drbd_device * device,struct bio_and_error * m)177*4882a593Smuzhiyun void complete_master_bio(struct drbd_device *device,
178*4882a593Smuzhiyun 		struct bio_and_error *m)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	if (unlikely(m->error))
181*4882a593Smuzhiyun 		m->bio->bi_status = errno_to_blk_status(m->error);
182*4882a593Smuzhiyun 	bio_endio(m->bio);
183*4882a593Smuzhiyun 	dec_ap_bio(device);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun /* Helper for __req_mod().
188*4882a593Smuzhiyun  * Set m->bio to the master bio, if it is fit to be completed,
189*4882a593Smuzhiyun  * or leave it alone (it is initialized to NULL in __req_mod),
190*4882a593Smuzhiyun  * if it has already been completed, or cannot be completed yet.
191*4882a593Smuzhiyun  * If m->bio is set, the error status to be returned is placed in m->error.
192*4882a593Smuzhiyun  */
193*4882a593Smuzhiyun static
drbd_req_complete(struct drbd_request * req,struct bio_and_error * m)194*4882a593Smuzhiyun void drbd_req_complete(struct drbd_request *req, struct bio_and_error *m)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	const unsigned s = req->rq_state;
197*4882a593Smuzhiyun 	struct drbd_device *device = req->device;
198*4882a593Smuzhiyun 	int error, ok;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	/* we must not complete the master bio, while it is
201*4882a593Smuzhiyun 	 *	still being processed by _drbd_send_zc_bio (drbd_send_dblock)
202*4882a593Smuzhiyun 	 *	not yet acknowledged by the peer
203*4882a593Smuzhiyun 	 *	not yet completed by the local io subsystem
204*4882a593Smuzhiyun 	 * these flags may get cleared in any order by
205*4882a593Smuzhiyun 	 *	the worker,
206*4882a593Smuzhiyun 	 *	the receiver,
207*4882a593Smuzhiyun 	 *	the bio_endio completion callbacks.
208*4882a593Smuzhiyun 	 */
209*4882a593Smuzhiyun 	if ((s & RQ_LOCAL_PENDING && !(s & RQ_LOCAL_ABORTED)) ||
210*4882a593Smuzhiyun 	    (s & RQ_NET_QUEUED) || (s & RQ_NET_PENDING) ||
211*4882a593Smuzhiyun 	    (s & RQ_COMPLETION_SUSP)) {
212*4882a593Smuzhiyun 		drbd_err(device, "drbd_req_complete: Logic BUG rq_state = 0x%x\n", s);
213*4882a593Smuzhiyun 		return;
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	if (!req->master_bio) {
217*4882a593Smuzhiyun 		drbd_err(device, "drbd_req_complete: Logic BUG, master_bio == NULL!\n");
218*4882a593Smuzhiyun 		return;
219*4882a593Smuzhiyun 	}
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	/*
222*4882a593Smuzhiyun 	 * figure out whether to report success or failure.
223*4882a593Smuzhiyun 	 *
224*4882a593Smuzhiyun 	 * report success when at least one of the operations succeeded.
225*4882a593Smuzhiyun 	 * or, to put the other way,
226*4882a593Smuzhiyun 	 * only report failure, when both operations failed.
227*4882a593Smuzhiyun 	 *
228*4882a593Smuzhiyun 	 * what to do about the failures is handled elsewhere.
229*4882a593Smuzhiyun 	 * what we need to do here is just: complete the master_bio.
230*4882a593Smuzhiyun 	 *
231*4882a593Smuzhiyun 	 * local completion error, if any, has been stored as ERR_PTR
232*4882a593Smuzhiyun 	 * in private_bio within drbd_request_endio.
233*4882a593Smuzhiyun 	 */
234*4882a593Smuzhiyun 	ok = (s & RQ_LOCAL_OK) || (s & RQ_NET_OK);
235*4882a593Smuzhiyun 	error = PTR_ERR(req->private_bio);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	/* Before we can signal completion to the upper layers,
238*4882a593Smuzhiyun 	 * we may need to close the current transfer log epoch.
239*4882a593Smuzhiyun 	 * We are within the request lock, so we can simply compare
240*4882a593Smuzhiyun 	 * the request epoch number with the current transfer log
241*4882a593Smuzhiyun 	 * epoch number.  If they match, increase the current_tle_nr,
242*4882a593Smuzhiyun 	 * and reset the transfer log epoch write_cnt.
243*4882a593Smuzhiyun 	 */
244*4882a593Smuzhiyun 	if (op_is_write(bio_op(req->master_bio)) &&
245*4882a593Smuzhiyun 	    req->epoch == atomic_read(&first_peer_device(device)->connection->current_tle_nr))
246*4882a593Smuzhiyun 		start_new_tl_epoch(first_peer_device(device)->connection);
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	/* Update disk stats */
249*4882a593Smuzhiyun 	bio_end_io_acct(req->master_bio, req->start_jif);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	/* If READ failed,
252*4882a593Smuzhiyun 	 * have it be pushed back to the retry work queue,
253*4882a593Smuzhiyun 	 * so it will re-enter __drbd_make_request(),
254*4882a593Smuzhiyun 	 * and be re-assigned to a suitable local or remote path,
255*4882a593Smuzhiyun 	 * or failed if we do not have access to good data anymore.
256*4882a593Smuzhiyun 	 *
257*4882a593Smuzhiyun 	 * Unless it was failed early by __drbd_make_request(),
258*4882a593Smuzhiyun 	 * because no path was available, in which case
259*4882a593Smuzhiyun 	 * it was not even added to the transfer_log.
260*4882a593Smuzhiyun 	 *
261*4882a593Smuzhiyun 	 * read-ahead may fail, and will not be retried.
262*4882a593Smuzhiyun 	 *
263*4882a593Smuzhiyun 	 * WRITE should have used all available paths already.
264*4882a593Smuzhiyun 	 */
265*4882a593Smuzhiyun 	if (!ok &&
266*4882a593Smuzhiyun 	    bio_op(req->master_bio) == REQ_OP_READ &&
267*4882a593Smuzhiyun 	    !(req->master_bio->bi_opf & REQ_RAHEAD) &&
268*4882a593Smuzhiyun 	    !list_empty(&req->tl_requests))
269*4882a593Smuzhiyun 		req->rq_state |= RQ_POSTPONED;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	if (!(req->rq_state & RQ_POSTPONED)) {
272*4882a593Smuzhiyun 		m->error = ok ? 0 : (error ?: -EIO);
273*4882a593Smuzhiyun 		m->bio = req->master_bio;
274*4882a593Smuzhiyun 		req->master_bio = NULL;
275*4882a593Smuzhiyun 		/* We leave it in the tree, to be able to verify later
276*4882a593Smuzhiyun 		 * write-acks in protocol != C during resync.
277*4882a593Smuzhiyun 		 * But we mark it as "complete", so it won't be counted as
278*4882a593Smuzhiyun 		 * conflict in a multi-primary setup. */
279*4882a593Smuzhiyun 		req->i.completed = true;
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	if (req->i.waiting)
283*4882a593Smuzhiyun 		wake_up(&device->misc_wait);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	/* Either we are about to complete to upper layers,
286*4882a593Smuzhiyun 	 * or we will restart this request.
287*4882a593Smuzhiyun 	 * In either case, the request object will be destroyed soon,
288*4882a593Smuzhiyun 	 * so better remove it from all lists. */
289*4882a593Smuzhiyun 	list_del_init(&req->req_pending_master_completion);
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun /* still holds resource->req_lock */
drbd_req_put_completion_ref(struct drbd_request * req,struct bio_and_error * m,int put)293*4882a593Smuzhiyun static void drbd_req_put_completion_ref(struct drbd_request *req, struct bio_and_error *m, int put)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	struct drbd_device *device = req->device;
296*4882a593Smuzhiyun 	D_ASSERT(device, m || (req->rq_state & RQ_POSTPONED));
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (!put)
299*4882a593Smuzhiyun 		return;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	if (!atomic_sub_and_test(put, &req->completion_ref))
302*4882a593Smuzhiyun 		return;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	drbd_req_complete(req, m);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	/* local completion may still come in later,
307*4882a593Smuzhiyun 	 * we need to keep the req object around. */
308*4882a593Smuzhiyun 	if (req->rq_state & RQ_LOCAL_ABORTED)
309*4882a593Smuzhiyun 		return;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	if (req->rq_state & RQ_POSTPONED) {
312*4882a593Smuzhiyun 		/* don't destroy the req object just yet,
313*4882a593Smuzhiyun 		 * but queue it for retry */
314*4882a593Smuzhiyun 		drbd_restart_request(req);
315*4882a593Smuzhiyun 		return;
316*4882a593Smuzhiyun 	}
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	kref_put(&req->kref, drbd_req_destroy);
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun 
set_if_null_req_next(struct drbd_peer_device * peer_device,struct drbd_request * req)321*4882a593Smuzhiyun static void set_if_null_req_next(struct drbd_peer_device *peer_device, struct drbd_request *req)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun 	struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
324*4882a593Smuzhiyun 	if (!connection)
325*4882a593Smuzhiyun 		return;
326*4882a593Smuzhiyun 	if (connection->req_next == NULL)
327*4882a593Smuzhiyun 		connection->req_next = req;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun 
advance_conn_req_next(struct drbd_peer_device * peer_device,struct drbd_request * req)330*4882a593Smuzhiyun static void advance_conn_req_next(struct drbd_peer_device *peer_device, struct drbd_request *req)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
333*4882a593Smuzhiyun 	if (!connection)
334*4882a593Smuzhiyun 		return;
335*4882a593Smuzhiyun 	if (connection->req_next != req)
336*4882a593Smuzhiyun 		return;
337*4882a593Smuzhiyun 	list_for_each_entry_continue(req, &connection->transfer_log, tl_requests) {
338*4882a593Smuzhiyun 		const unsigned s = req->rq_state;
339*4882a593Smuzhiyun 		if (s & RQ_NET_QUEUED)
340*4882a593Smuzhiyun 			break;
341*4882a593Smuzhiyun 	}
342*4882a593Smuzhiyun 	if (&req->tl_requests == &connection->transfer_log)
343*4882a593Smuzhiyun 		req = NULL;
344*4882a593Smuzhiyun 	connection->req_next = req;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun 
set_if_null_req_ack_pending(struct drbd_peer_device * peer_device,struct drbd_request * req)347*4882a593Smuzhiyun static void set_if_null_req_ack_pending(struct drbd_peer_device *peer_device, struct drbd_request *req)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun 	struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
350*4882a593Smuzhiyun 	if (!connection)
351*4882a593Smuzhiyun 		return;
352*4882a593Smuzhiyun 	if (connection->req_ack_pending == NULL)
353*4882a593Smuzhiyun 		connection->req_ack_pending = req;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun 
advance_conn_req_ack_pending(struct drbd_peer_device * peer_device,struct drbd_request * req)356*4882a593Smuzhiyun static void advance_conn_req_ack_pending(struct drbd_peer_device *peer_device, struct drbd_request *req)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun 	struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
359*4882a593Smuzhiyun 	if (!connection)
360*4882a593Smuzhiyun 		return;
361*4882a593Smuzhiyun 	if (connection->req_ack_pending != req)
362*4882a593Smuzhiyun 		return;
363*4882a593Smuzhiyun 	list_for_each_entry_continue(req, &connection->transfer_log, tl_requests) {
364*4882a593Smuzhiyun 		const unsigned s = req->rq_state;
365*4882a593Smuzhiyun 		if ((s & RQ_NET_SENT) && (s & RQ_NET_PENDING))
366*4882a593Smuzhiyun 			break;
367*4882a593Smuzhiyun 	}
368*4882a593Smuzhiyun 	if (&req->tl_requests == &connection->transfer_log)
369*4882a593Smuzhiyun 		req = NULL;
370*4882a593Smuzhiyun 	connection->req_ack_pending = req;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun 
set_if_null_req_not_net_done(struct drbd_peer_device * peer_device,struct drbd_request * req)373*4882a593Smuzhiyun static void set_if_null_req_not_net_done(struct drbd_peer_device *peer_device, struct drbd_request *req)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun 	struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
376*4882a593Smuzhiyun 	if (!connection)
377*4882a593Smuzhiyun 		return;
378*4882a593Smuzhiyun 	if (connection->req_not_net_done == NULL)
379*4882a593Smuzhiyun 		connection->req_not_net_done = req;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun 
advance_conn_req_not_net_done(struct drbd_peer_device * peer_device,struct drbd_request * req)382*4882a593Smuzhiyun static void advance_conn_req_not_net_done(struct drbd_peer_device *peer_device, struct drbd_request *req)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun 	struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
385*4882a593Smuzhiyun 	if (!connection)
386*4882a593Smuzhiyun 		return;
387*4882a593Smuzhiyun 	if (connection->req_not_net_done != req)
388*4882a593Smuzhiyun 		return;
389*4882a593Smuzhiyun 	list_for_each_entry_continue(req, &connection->transfer_log, tl_requests) {
390*4882a593Smuzhiyun 		const unsigned s = req->rq_state;
391*4882a593Smuzhiyun 		if ((s & RQ_NET_SENT) && !(s & RQ_NET_DONE))
392*4882a593Smuzhiyun 			break;
393*4882a593Smuzhiyun 	}
394*4882a593Smuzhiyun 	if (&req->tl_requests == &connection->transfer_log)
395*4882a593Smuzhiyun 		req = NULL;
396*4882a593Smuzhiyun 	connection->req_not_net_done = req;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun /* I'd like this to be the only place that manipulates
400*4882a593Smuzhiyun  * req->completion_ref and req->kref. */
mod_rq_state(struct drbd_request * req,struct bio_and_error * m,int clear,int set)401*4882a593Smuzhiyun static void mod_rq_state(struct drbd_request *req, struct bio_and_error *m,
402*4882a593Smuzhiyun 		int clear, int set)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun 	struct drbd_device *device = req->device;
405*4882a593Smuzhiyun 	struct drbd_peer_device *peer_device = first_peer_device(device);
406*4882a593Smuzhiyun 	unsigned s = req->rq_state;
407*4882a593Smuzhiyun 	int c_put = 0;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	if (drbd_suspended(device) && !((s | clear) & RQ_COMPLETION_SUSP))
410*4882a593Smuzhiyun 		set |= RQ_COMPLETION_SUSP;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/* apply */
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	req->rq_state &= ~clear;
415*4882a593Smuzhiyun 	req->rq_state |= set;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	/* no change? */
418*4882a593Smuzhiyun 	if (req->rq_state == s)
419*4882a593Smuzhiyun 		return;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	/* intent: get references */
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	kref_get(&req->kref);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	if (!(s & RQ_LOCAL_PENDING) && (set & RQ_LOCAL_PENDING))
426*4882a593Smuzhiyun 		atomic_inc(&req->completion_ref);
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	if (!(s & RQ_NET_PENDING) && (set & RQ_NET_PENDING)) {
429*4882a593Smuzhiyun 		inc_ap_pending(device);
430*4882a593Smuzhiyun 		atomic_inc(&req->completion_ref);
431*4882a593Smuzhiyun 	}
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	if (!(s & RQ_NET_QUEUED) && (set & RQ_NET_QUEUED)) {
434*4882a593Smuzhiyun 		atomic_inc(&req->completion_ref);
435*4882a593Smuzhiyun 		set_if_null_req_next(peer_device, req);
436*4882a593Smuzhiyun 	}
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	if (!(s & RQ_EXP_BARR_ACK) && (set & RQ_EXP_BARR_ACK))
439*4882a593Smuzhiyun 		kref_get(&req->kref); /* wait for the DONE */
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	if (!(s & RQ_NET_SENT) && (set & RQ_NET_SENT)) {
442*4882a593Smuzhiyun 		/* potentially already completed in the ack_receiver thread */
443*4882a593Smuzhiyun 		if (!(s & RQ_NET_DONE)) {
444*4882a593Smuzhiyun 			atomic_add(req->i.size >> 9, &device->ap_in_flight);
445*4882a593Smuzhiyun 			set_if_null_req_not_net_done(peer_device, req);
446*4882a593Smuzhiyun 		}
447*4882a593Smuzhiyun 		if (req->rq_state & RQ_NET_PENDING)
448*4882a593Smuzhiyun 			set_if_null_req_ack_pending(peer_device, req);
449*4882a593Smuzhiyun 	}
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	if (!(s & RQ_COMPLETION_SUSP) && (set & RQ_COMPLETION_SUSP))
452*4882a593Smuzhiyun 		atomic_inc(&req->completion_ref);
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	/* progress: put references */
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	if ((s & RQ_COMPLETION_SUSP) && (clear & RQ_COMPLETION_SUSP))
457*4882a593Smuzhiyun 		++c_put;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	if (!(s & RQ_LOCAL_ABORTED) && (set & RQ_LOCAL_ABORTED)) {
460*4882a593Smuzhiyun 		D_ASSERT(device, req->rq_state & RQ_LOCAL_PENDING);
461*4882a593Smuzhiyun 		++c_put;
462*4882a593Smuzhiyun 	}
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	if ((s & RQ_LOCAL_PENDING) && (clear & RQ_LOCAL_PENDING)) {
465*4882a593Smuzhiyun 		if (req->rq_state & RQ_LOCAL_ABORTED)
466*4882a593Smuzhiyun 			kref_put(&req->kref, drbd_req_destroy);
467*4882a593Smuzhiyun 		else
468*4882a593Smuzhiyun 			++c_put;
469*4882a593Smuzhiyun 		list_del_init(&req->req_pending_local);
470*4882a593Smuzhiyun 	}
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	if ((s & RQ_NET_PENDING) && (clear & RQ_NET_PENDING)) {
473*4882a593Smuzhiyun 		dec_ap_pending(device);
474*4882a593Smuzhiyun 		++c_put;
475*4882a593Smuzhiyun 		req->acked_jif = jiffies;
476*4882a593Smuzhiyun 		advance_conn_req_ack_pending(peer_device, req);
477*4882a593Smuzhiyun 	}
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	if ((s & RQ_NET_QUEUED) && (clear & RQ_NET_QUEUED)) {
480*4882a593Smuzhiyun 		++c_put;
481*4882a593Smuzhiyun 		advance_conn_req_next(peer_device, req);
482*4882a593Smuzhiyun 	}
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	if (!(s & RQ_NET_DONE) && (set & RQ_NET_DONE)) {
485*4882a593Smuzhiyun 		if (s & RQ_NET_SENT)
486*4882a593Smuzhiyun 			atomic_sub(req->i.size >> 9, &device->ap_in_flight);
487*4882a593Smuzhiyun 		if (s & RQ_EXP_BARR_ACK)
488*4882a593Smuzhiyun 			kref_put(&req->kref, drbd_req_destroy);
489*4882a593Smuzhiyun 		req->net_done_jif = jiffies;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 		/* in ahead/behind mode, or just in case,
492*4882a593Smuzhiyun 		 * before we finally destroy this request,
493*4882a593Smuzhiyun 		 * the caching pointers must not reference it anymore */
494*4882a593Smuzhiyun 		advance_conn_req_next(peer_device, req);
495*4882a593Smuzhiyun 		advance_conn_req_ack_pending(peer_device, req);
496*4882a593Smuzhiyun 		advance_conn_req_not_net_done(peer_device, req);
497*4882a593Smuzhiyun 	}
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	/* potentially complete and destroy */
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	/* If we made progress, retry conflicting peer requests, if any. */
502*4882a593Smuzhiyun 	if (req->i.waiting)
503*4882a593Smuzhiyun 		wake_up(&device->misc_wait);
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	drbd_req_put_completion_ref(req, m, c_put);
506*4882a593Smuzhiyun 	kref_put(&req->kref, drbd_req_destroy);
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun 
drbd_report_io_error(struct drbd_device * device,struct drbd_request * req)509*4882a593Smuzhiyun static void drbd_report_io_error(struct drbd_device *device, struct drbd_request *req)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun         char b[BDEVNAME_SIZE];
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	if (!__ratelimit(&drbd_ratelimit_state))
514*4882a593Smuzhiyun 		return;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	drbd_warn(device, "local %s IO error sector %llu+%u on %s\n",
517*4882a593Smuzhiyun 			(req->rq_state & RQ_WRITE) ? "WRITE" : "READ",
518*4882a593Smuzhiyun 			(unsigned long long)req->i.sector,
519*4882a593Smuzhiyun 			req->i.size >> 9,
520*4882a593Smuzhiyun 			bdevname(device->ldev->backing_bdev, b));
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun /* Helper for HANDED_OVER_TO_NETWORK.
524*4882a593Smuzhiyun  * Is this a protocol A write (neither WRITE_ACK nor RECEIVE_ACK expected)?
525*4882a593Smuzhiyun  * Is it also still "PENDING"?
526*4882a593Smuzhiyun  * --> If so, clear PENDING and set NET_OK below.
527*4882a593Smuzhiyun  * If it is a protocol A write, but not RQ_PENDING anymore, neg-ack was faster
528*4882a593Smuzhiyun  * (and we must not set RQ_NET_OK) */
is_pending_write_protocol_A(struct drbd_request * req)529*4882a593Smuzhiyun static inline bool is_pending_write_protocol_A(struct drbd_request *req)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun 	return (req->rq_state &
532*4882a593Smuzhiyun 		   (RQ_WRITE|RQ_NET_PENDING|RQ_EXP_WRITE_ACK|RQ_EXP_RECEIVE_ACK))
533*4882a593Smuzhiyun 		== (RQ_WRITE|RQ_NET_PENDING);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun /* obviously this could be coded as many single functions
537*4882a593Smuzhiyun  * instead of one huge switch,
538*4882a593Smuzhiyun  * or by putting the code directly in the respective locations
539*4882a593Smuzhiyun  * (as it has been before).
540*4882a593Smuzhiyun  *
541*4882a593Smuzhiyun  * but having it this way
542*4882a593Smuzhiyun  *  enforces that it is all in this one place, where it is easier to audit,
543*4882a593Smuzhiyun  *  it makes it obvious that whatever "event" "happens" to a request should
544*4882a593Smuzhiyun  *  happen "atomically" within the req_lock,
545*4882a593Smuzhiyun  *  and it enforces that we have to think in a very structured manner
546*4882a593Smuzhiyun  *  about the "events" that may happen to a request during its life time ...
547*4882a593Smuzhiyun  */
__req_mod(struct drbd_request * req,enum drbd_req_event what,struct bio_and_error * m)548*4882a593Smuzhiyun int __req_mod(struct drbd_request *req, enum drbd_req_event what,
549*4882a593Smuzhiyun 		struct bio_and_error *m)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun 	struct drbd_device *const device = req->device;
552*4882a593Smuzhiyun 	struct drbd_peer_device *const peer_device = first_peer_device(device);
553*4882a593Smuzhiyun 	struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL;
554*4882a593Smuzhiyun 	struct net_conf *nc;
555*4882a593Smuzhiyun 	int p, rv = 0;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	if (m)
558*4882a593Smuzhiyun 		m->bio = NULL;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	switch (what) {
561*4882a593Smuzhiyun 	default:
562*4882a593Smuzhiyun 		drbd_err(device, "LOGIC BUG in %s:%u\n", __FILE__ , __LINE__);
563*4882a593Smuzhiyun 		break;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	/* does not happen...
566*4882a593Smuzhiyun 	 * initialization done in drbd_req_new
567*4882a593Smuzhiyun 	case CREATED:
568*4882a593Smuzhiyun 		break;
569*4882a593Smuzhiyun 		*/
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	case TO_BE_SENT: /* via network */
572*4882a593Smuzhiyun 		/* reached via __drbd_make_request
573*4882a593Smuzhiyun 		 * and from w_read_retry_remote */
574*4882a593Smuzhiyun 		D_ASSERT(device, !(req->rq_state & RQ_NET_MASK));
575*4882a593Smuzhiyun 		rcu_read_lock();
576*4882a593Smuzhiyun 		nc = rcu_dereference(connection->net_conf);
577*4882a593Smuzhiyun 		p = nc->wire_protocol;
578*4882a593Smuzhiyun 		rcu_read_unlock();
579*4882a593Smuzhiyun 		req->rq_state |=
580*4882a593Smuzhiyun 			p == DRBD_PROT_C ? RQ_EXP_WRITE_ACK :
581*4882a593Smuzhiyun 			p == DRBD_PROT_B ? RQ_EXP_RECEIVE_ACK : 0;
582*4882a593Smuzhiyun 		mod_rq_state(req, m, 0, RQ_NET_PENDING);
583*4882a593Smuzhiyun 		break;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	case TO_BE_SUBMITTED: /* locally */
586*4882a593Smuzhiyun 		/* reached via __drbd_make_request */
587*4882a593Smuzhiyun 		D_ASSERT(device, !(req->rq_state & RQ_LOCAL_MASK));
588*4882a593Smuzhiyun 		mod_rq_state(req, m, 0, RQ_LOCAL_PENDING);
589*4882a593Smuzhiyun 		break;
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	case COMPLETED_OK:
592*4882a593Smuzhiyun 		if (req->rq_state & RQ_WRITE)
593*4882a593Smuzhiyun 			device->writ_cnt += req->i.size >> 9;
594*4882a593Smuzhiyun 		else
595*4882a593Smuzhiyun 			device->read_cnt += req->i.size >> 9;
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_LOCAL_PENDING,
598*4882a593Smuzhiyun 				RQ_LOCAL_COMPLETED|RQ_LOCAL_OK);
599*4882a593Smuzhiyun 		break;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	case ABORT_DISK_IO:
602*4882a593Smuzhiyun 		mod_rq_state(req, m, 0, RQ_LOCAL_ABORTED);
603*4882a593Smuzhiyun 		break;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	case WRITE_COMPLETED_WITH_ERROR:
606*4882a593Smuzhiyun 		drbd_report_io_error(device, req);
607*4882a593Smuzhiyun 		__drbd_chk_io_error(device, DRBD_WRITE_ERROR);
608*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
609*4882a593Smuzhiyun 		break;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	case READ_COMPLETED_WITH_ERROR:
612*4882a593Smuzhiyun 		drbd_set_out_of_sync(device, req->i.sector, req->i.size);
613*4882a593Smuzhiyun 		drbd_report_io_error(device, req);
614*4882a593Smuzhiyun 		__drbd_chk_io_error(device, DRBD_READ_ERROR);
615*4882a593Smuzhiyun 		fallthrough;
616*4882a593Smuzhiyun 	case READ_AHEAD_COMPLETED_WITH_ERROR:
617*4882a593Smuzhiyun 		/* it is legal to fail read-ahead, no __drbd_chk_io_error in that case. */
618*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
619*4882a593Smuzhiyun 		break;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	case DISCARD_COMPLETED_NOTSUPP:
622*4882a593Smuzhiyun 	case DISCARD_COMPLETED_WITH_ERROR:
623*4882a593Smuzhiyun 		/* I'd rather not detach from local disk just because it
624*4882a593Smuzhiyun 		 * failed a REQ_OP_DISCARD. */
625*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_LOCAL_PENDING, RQ_LOCAL_COMPLETED);
626*4882a593Smuzhiyun 		break;
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	case QUEUE_FOR_NET_READ:
629*4882a593Smuzhiyun 		/* READ, and
630*4882a593Smuzhiyun 		 * no local disk,
631*4882a593Smuzhiyun 		 * or target area marked as invalid,
632*4882a593Smuzhiyun 		 * or just got an io-error. */
633*4882a593Smuzhiyun 		/* from __drbd_make_request
634*4882a593Smuzhiyun 		 * or from bio_endio during read io-error recovery */
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 		/* So we can verify the handle in the answer packet.
637*4882a593Smuzhiyun 		 * Corresponding drbd_remove_request_interval is in
638*4882a593Smuzhiyun 		 * drbd_req_complete() */
639*4882a593Smuzhiyun 		D_ASSERT(device, drbd_interval_empty(&req->i));
640*4882a593Smuzhiyun 		drbd_insert_interval(&device->read_requests, &req->i);
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 		set_bit(UNPLUG_REMOTE, &device->flags);
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 		D_ASSERT(device, req->rq_state & RQ_NET_PENDING);
645*4882a593Smuzhiyun 		D_ASSERT(device, (req->rq_state & RQ_LOCAL_MASK) == 0);
646*4882a593Smuzhiyun 		mod_rq_state(req, m, 0, RQ_NET_QUEUED);
647*4882a593Smuzhiyun 		req->w.cb = w_send_read_req;
648*4882a593Smuzhiyun 		drbd_queue_work(&connection->sender_work,
649*4882a593Smuzhiyun 				&req->w);
650*4882a593Smuzhiyun 		break;
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	case QUEUE_FOR_NET_WRITE:
653*4882a593Smuzhiyun 		/* assert something? */
654*4882a593Smuzhiyun 		/* from __drbd_make_request only */
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 		/* Corresponding drbd_remove_request_interval is in
657*4882a593Smuzhiyun 		 * drbd_req_complete() */
658*4882a593Smuzhiyun 		D_ASSERT(device, drbd_interval_empty(&req->i));
659*4882a593Smuzhiyun 		drbd_insert_interval(&device->write_requests, &req->i);
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 		/* NOTE
662*4882a593Smuzhiyun 		 * In case the req ended up on the transfer log before being
663*4882a593Smuzhiyun 		 * queued on the worker, it could lead to this request being
664*4882a593Smuzhiyun 		 * missed during cleanup after connection loss.
665*4882a593Smuzhiyun 		 * So we have to do both operations here,
666*4882a593Smuzhiyun 		 * within the same lock that protects the transfer log.
667*4882a593Smuzhiyun 		 *
668*4882a593Smuzhiyun 		 * _req_add_to_epoch(req); this has to be after the
669*4882a593Smuzhiyun 		 * _maybe_start_new_epoch(req); which happened in
670*4882a593Smuzhiyun 		 * __drbd_make_request, because we now may set the bit
671*4882a593Smuzhiyun 		 * again ourselves to close the current epoch.
672*4882a593Smuzhiyun 		 *
673*4882a593Smuzhiyun 		 * Add req to the (now) current epoch (barrier). */
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 		/* otherwise we may lose an unplug, which may cause some remote
676*4882a593Smuzhiyun 		 * io-scheduler timeout to expire, increasing maximum latency,
677*4882a593Smuzhiyun 		 * hurting performance. */
678*4882a593Smuzhiyun 		set_bit(UNPLUG_REMOTE, &device->flags);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 		/* queue work item to send data */
681*4882a593Smuzhiyun 		D_ASSERT(device, req->rq_state & RQ_NET_PENDING);
682*4882a593Smuzhiyun 		mod_rq_state(req, m, 0, RQ_NET_QUEUED|RQ_EXP_BARR_ACK);
683*4882a593Smuzhiyun 		req->w.cb =  w_send_dblock;
684*4882a593Smuzhiyun 		drbd_queue_work(&connection->sender_work,
685*4882a593Smuzhiyun 				&req->w);
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 		/* close the epoch, in case it outgrew the limit */
688*4882a593Smuzhiyun 		rcu_read_lock();
689*4882a593Smuzhiyun 		nc = rcu_dereference(connection->net_conf);
690*4882a593Smuzhiyun 		p = nc->max_epoch_size;
691*4882a593Smuzhiyun 		rcu_read_unlock();
692*4882a593Smuzhiyun 		if (connection->current_tle_writes >= p)
693*4882a593Smuzhiyun 			start_new_tl_epoch(connection);
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 		break;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	case QUEUE_FOR_SEND_OOS:
698*4882a593Smuzhiyun 		mod_rq_state(req, m, 0, RQ_NET_QUEUED);
699*4882a593Smuzhiyun 		req->w.cb =  w_send_out_of_sync;
700*4882a593Smuzhiyun 		drbd_queue_work(&connection->sender_work,
701*4882a593Smuzhiyun 				&req->w);
702*4882a593Smuzhiyun 		break;
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	case READ_RETRY_REMOTE_CANCELED:
705*4882a593Smuzhiyun 	case SEND_CANCELED:
706*4882a593Smuzhiyun 	case SEND_FAILED:
707*4882a593Smuzhiyun 		/* real cleanup will be done from tl_clear.  just update flags
708*4882a593Smuzhiyun 		 * so it is no longer marked as on the worker queue */
709*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_NET_QUEUED, 0);
710*4882a593Smuzhiyun 		break;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	case HANDED_OVER_TO_NETWORK:
713*4882a593Smuzhiyun 		/* assert something? */
714*4882a593Smuzhiyun 		if (is_pending_write_protocol_A(req))
715*4882a593Smuzhiyun 			/* this is what is dangerous about protocol A:
716*4882a593Smuzhiyun 			 * pretend it was successfully written on the peer. */
717*4882a593Smuzhiyun 			mod_rq_state(req, m, RQ_NET_QUEUED|RQ_NET_PENDING,
718*4882a593Smuzhiyun 						RQ_NET_SENT|RQ_NET_OK);
719*4882a593Smuzhiyun 		else
720*4882a593Smuzhiyun 			mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_SENT);
721*4882a593Smuzhiyun 		/* It is still not yet RQ_NET_DONE until the
722*4882a593Smuzhiyun 		 * corresponding epoch barrier got acked as well,
723*4882a593Smuzhiyun 		 * so we know what to dirty on connection loss. */
724*4882a593Smuzhiyun 		break;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	case OOS_HANDED_TO_NETWORK:
727*4882a593Smuzhiyun 		/* Was not set PENDING, no longer QUEUED, so is now DONE
728*4882a593Smuzhiyun 		 * as far as this connection is concerned. */
729*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_NET_QUEUED, RQ_NET_DONE);
730*4882a593Smuzhiyun 		break;
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	case CONNECTION_LOST_WHILE_PENDING:
733*4882a593Smuzhiyun 		/* transfer log cleanup after connection loss */
734*4882a593Smuzhiyun 		mod_rq_state(req, m,
735*4882a593Smuzhiyun 				RQ_NET_OK|RQ_NET_PENDING|RQ_COMPLETION_SUSP,
736*4882a593Smuzhiyun 				RQ_NET_DONE);
737*4882a593Smuzhiyun 		break;
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	case CONFLICT_RESOLVED:
740*4882a593Smuzhiyun 		/* for superseded conflicting writes of multiple primaries,
741*4882a593Smuzhiyun 		 * there is no need to keep anything in the tl, potential
742*4882a593Smuzhiyun 		 * node crashes are covered by the activity log.
743*4882a593Smuzhiyun 		 *
744*4882a593Smuzhiyun 		 * If this request had been marked as RQ_POSTPONED before,
745*4882a593Smuzhiyun 		 * it will actually not be completed, but "restarted",
746*4882a593Smuzhiyun 		 * resubmitted from the retry worker context. */
747*4882a593Smuzhiyun 		D_ASSERT(device, req->rq_state & RQ_NET_PENDING);
748*4882a593Smuzhiyun 		D_ASSERT(device, req->rq_state & RQ_EXP_WRITE_ACK);
749*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_DONE|RQ_NET_OK);
750*4882a593Smuzhiyun 		break;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	case WRITE_ACKED_BY_PEER_AND_SIS:
753*4882a593Smuzhiyun 		req->rq_state |= RQ_NET_SIS;
754*4882a593Smuzhiyun 	case WRITE_ACKED_BY_PEER:
755*4882a593Smuzhiyun 		/* Normal operation protocol C: successfully written on peer.
756*4882a593Smuzhiyun 		 * During resync, even in protocol != C,
757*4882a593Smuzhiyun 		 * we requested an explicit write ack anyways.
758*4882a593Smuzhiyun 		 * Which means we cannot even assert anything here.
759*4882a593Smuzhiyun 		 * Nothing more to do here.
760*4882a593Smuzhiyun 		 * We want to keep the tl in place for all protocols, to cater
761*4882a593Smuzhiyun 		 * for volatile write-back caches on lower level devices. */
762*4882a593Smuzhiyun 		goto ack_common;
763*4882a593Smuzhiyun 	case RECV_ACKED_BY_PEER:
764*4882a593Smuzhiyun 		D_ASSERT(device, req->rq_state & RQ_EXP_RECEIVE_ACK);
765*4882a593Smuzhiyun 		/* protocol B; pretends to be successfully written on peer.
766*4882a593Smuzhiyun 		 * see also notes above in HANDED_OVER_TO_NETWORK about
767*4882a593Smuzhiyun 		 * protocol != C */
768*4882a593Smuzhiyun 	ack_common:
769*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK);
770*4882a593Smuzhiyun 		break;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	case POSTPONE_WRITE:
773*4882a593Smuzhiyun 		D_ASSERT(device, req->rq_state & RQ_EXP_WRITE_ACK);
774*4882a593Smuzhiyun 		/* If this node has already detected the write conflict, the
775*4882a593Smuzhiyun 		 * worker will be waiting on misc_wait.  Wake it up once this
776*4882a593Smuzhiyun 		 * request has completed locally.
777*4882a593Smuzhiyun 		 */
778*4882a593Smuzhiyun 		D_ASSERT(device, req->rq_state & RQ_NET_PENDING);
779*4882a593Smuzhiyun 		req->rq_state |= RQ_POSTPONED;
780*4882a593Smuzhiyun 		if (req->i.waiting)
781*4882a593Smuzhiyun 			wake_up(&device->misc_wait);
782*4882a593Smuzhiyun 		/* Do not clear RQ_NET_PENDING. This request will make further
783*4882a593Smuzhiyun 		 * progress via restart_conflicting_writes() or
784*4882a593Smuzhiyun 		 * fail_postponed_requests(). Hopefully. */
785*4882a593Smuzhiyun 		break;
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	case NEG_ACKED:
788*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_NET_OK|RQ_NET_PENDING, 0);
789*4882a593Smuzhiyun 		break;
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	case FAIL_FROZEN_DISK_IO:
792*4882a593Smuzhiyun 		if (!(req->rq_state & RQ_LOCAL_COMPLETED))
793*4882a593Smuzhiyun 			break;
794*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
795*4882a593Smuzhiyun 		break;
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	case RESTART_FROZEN_DISK_IO:
798*4882a593Smuzhiyun 		if (!(req->rq_state & RQ_LOCAL_COMPLETED))
799*4882a593Smuzhiyun 			break;
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 		mod_rq_state(req, m,
802*4882a593Smuzhiyun 				RQ_COMPLETION_SUSP|RQ_LOCAL_COMPLETED,
803*4882a593Smuzhiyun 				RQ_LOCAL_PENDING);
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 		rv = MR_READ;
806*4882a593Smuzhiyun 		if (bio_data_dir(req->master_bio) == WRITE)
807*4882a593Smuzhiyun 			rv = MR_WRITE;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 		get_ldev(device); /* always succeeds in this call path */
810*4882a593Smuzhiyun 		req->w.cb = w_restart_disk_io;
811*4882a593Smuzhiyun 		drbd_queue_work(&connection->sender_work,
812*4882a593Smuzhiyun 				&req->w);
813*4882a593Smuzhiyun 		break;
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	case RESEND:
816*4882a593Smuzhiyun 		/* Simply complete (local only) READs. */
817*4882a593Smuzhiyun 		if (!(req->rq_state & RQ_WRITE) && !req->w.cb) {
818*4882a593Smuzhiyun 			mod_rq_state(req, m, RQ_COMPLETION_SUSP, 0);
819*4882a593Smuzhiyun 			break;
820*4882a593Smuzhiyun 		}
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 		/* If RQ_NET_OK is already set, we got a P_WRITE_ACK or P_RECV_ACK
823*4882a593Smuzhiyun 		   before the connection loss (B&C only); only P_BARRIER_ACK
824*4882a593Smuzhiyun 		   (or the local completion?) was missing when we suspended.
825*4882a593Smuzhiyun 		   Throwing them out of the TL here by pretending we got a BARRIER_ACK.
826*4882a593Smuzhiyun 		   During connection handshake, we ensure that the peer was not rebooted. */
827*4882a593Smuzhiyun 		if (!(req->rq_state & RQ_NET_OK)) {
828*4882a593Smuzhiyun 			/* FIXME could this possibly be a req->dw.cb == w_send_out_of_sync?
829*4882a593Smuzhiyun 			 * in that case we must not set RQ_NET_PENDING. */
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 			mod_rq_state(req, m, RQ_COMPLETION_SUSP, RQ_NET_QUEUED|RQ_NET_PENDING);
832*4882a593Smuzhiyun 			if (req->w.cb) {
833*4882a593Smuzhiyun 				/* w.cb expected to be w_send_dblock, or w_send_read_req */
834*4882a593Smuzhiyun 				drbd_queue_work(&connection->sender_work,
835*4882a593Smuzhiyun 						&req->w);
836*4882a593Smuzhiyun 				rv = req->rq_state & RQ_WRITE ? MR_WRITE : MR_READ;
837*4882a593Smuzhiyun 			} /* else: FIXME can this happen? */
838*4882a593Smuzhiyun 			break;
839*4882a593Smuzhiyun 		}
840*4882a593Smuzhiyun 		fallthrough;	/* to BARRIER_ACKED */
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	case BARRIER_ACKED:
843*4882a593Smuzhiyun 		/* barrier ack for READ requests does not make sense */
844*4882a593Smuzhiyun 		if (!(req->rq_state & RQ_WRITE))
845*4882a593Smuzhiyun 			break;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 		if (req->rq_state & RQ_NET_PENDING) {
848*4882a593Smuzhiyun 			/* barrier came in before all requests were acked.
849*4882a593Smuzhiyun 			 * this is bad, because if the connection is lost now,
850*4882a593Smuzhiyun 			 * we won't be able to clean them up... */
851*4882a593Smuzhiyun 			drbd_err(device, "FIXME (BARRIER_ACKED but pending)\n");
852*4882a593Smuzhiyun 		}
853*4882a593Smuzhiyun 		/* Allowed to complete requests, even while suspended.
854*4882a593Smuzhiyun 		 * As this is called for all requests within a matching epoch,
855*4882a593Smuzhiyun 		 * we need to filter, and only set RQ_NET_DONE for those that
856*4882a593Smuzhiyun 		 * have actually been on the wire. */
857*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_COMPLETION_SUSP,
858*4882a593Smuzhiyun 				(req->rq_state & RQ_NET_MASK) ? RQ_NET_DONE : 0);
859*4882a593Smuzhiyun 		break;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	case DATA_RECEIVED:
862*4882a593Smuzhiyun 		D_ASSERT(device, req->rq_state & RQ_NET_PENDING);
863*4882a593Smuzhiyun 		mod_rq_state(req, m, RQ_NET_PENDING, RQ_NET_OK|RQ_NET_DONE);
864*4882a593Smuzhiyun 		break;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	case QUEUE_AS_DRBD_BARRIER:
867*4882a593Smuzhiyun 		start_new_tl_epoch(connection);
868*4882a593Smuzhiyun 		mod_rq_state(req, m, 0, RQ_NET_OK|RQ_NET_DONE);
869*4882a593Smuzhiyun 		break;
870*4882a593Smuzhiyun 	}
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 	return rv;
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun /* we may do a local read if:
876*4882a593Smuzhiyun  * - we are consistent (of course),
877*4882a593Smuzhiyun  * - or we are generally inconsistent,
878*4882a593Smuzhiyun  *   BUT we are still/already IN SYNC for this area.
879*4882a593Smuzhiyun  *   since size may be bigger than BM_BLOCK_SIZE,
880*4882a593Smuzhiyun  *   we may need to check several bits.
881*4882a593Smuzhiyun  */
drbd_may_do_local_read(struct drbd_device * device,sector_t sector,int size)882*4882a593Smuzhiyun static bool drbd_may_do_local_read(struct drbd_device *device, sector_t sector, int size)
883*4882a593Smuzhiyun {
884*4882a593Smuzhiyun 	unsigned long sbnr, ebnr;
885*4882a593Smuzhiyun 	sector_t esector, nr_sectors;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	if (device->state.disk == D_UP_TO_DATE)
888*4882a593Smuzhiyun 		return true;
889*4882a593Smuzhiyun 	if (device->state.disk != D_INCONSISTENT)
890*4882a593Smuzhiyun 		return false;
891*4882a593Smuzhiyun 	esector = sector + (size >> 9) - 1;
892*4882a593Smuzhiyun 	nr_sectors = get_capacity(device->vdisk);
893*4882a593Smuzhiyun 	D_ASSERT(device, sector  < nr_sectors);
894*4882a593Smuzhiyun 	D_ASSERT(device, esector < nr_sectors);
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	sbnr = BM_SECT_TO_BIT(sector);
897*4882a593Smuzhiyun 	ebnr = BM_SECT_TO_BIT(esector);
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	return drbd_bm_count_bits(device, sbnr, ebnr) == 0;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun 
remote_due_to_read_balancing(struct drbd_device * device,sector_t sector,enum drbd_read_balancing rbm)902*4882a593Smuzhiyun static bool remote_due_to_read_balancing(struct drbd_device *device, sector_t sector,
903*4882a593Smuzhiyun 		enum drbd_read_balancing rbm)
904*4882a593Smuzhiyun {
905*4882a593Smuzhiyun 	struct backing_dev_info *bdi;
906*4882a593Smuzhiyun 	int stripe_shift;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	switch (rbm) {
909*4882a593Smuzhiyun 	case RB_CONGESTED_REMOTE:
910*4882a593Smuzhiyun 		bdi = device->ldev->backing_bdev->bd_disk->queue->backing_dev_info;
911*4882a593Smuzhiyun 		return bdi_read_congested(bdi);
912*4882a593Smuzhiyun 	case RB_LEAST_PENDING:
913*4882a593Smuzhiyun 		return atomic_read(&device->local_cnt) >
914*4882a593Smuzhiyun 			atomic_read(&device->ap_pending_cnt) + atomic_read(&device->rs_pending_cnt);
915*4882a593Smuzhiyun 	case RB_32K_STRIPING:  /* stripe_shift = 15 */
916*4882a593Smuzhiyun 	case RB_64K_STRIPING:
917*4882a593Smuzhiyun 	case RB_128K_STRIPING:
918*4882a593Smuzhiyun 	case RB_256K_STRIPING:
919*4882a593Smuzhiyun 	case RB_512K_STRIPING:
920*4882a593Smuzhiyun 	case RB_1M_STRIPING:   /* stripe_shift = 20 */
921*4882a593Smuzhiyun 		stripe_shift = (rbm - RB_32K_STRIPING + 15);
922*4882a593Smuzhiyun 		return (sector >> (stripe_shift - 9)) & 1;
923*4882a593Smuzhiyun 	case RB_ROUND_ROBIN:
924*4882a593Smuzhiyun 		return test_and_change_bit(READ_BALANCE_RR, &device->flags);
925*4882a593Smuzhiyun 	case RB_PREFER_REMOTE:
926*4882a593Smuzhiyun 		return true;
927*4882a593Smuzhiyun 	case RB_PREFER_LOCAL:
928*4882a593Smuzhiyun 	default:
929*4882a593Smuzhiyun 		return false;
930*4882a593Smuzhiyun 	}
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun /*
934*4882a593Smuzhiyun  * complete_conflicting_writes  -  wait for any conflicting write requests
935*4882a593Smuzhiyun  *
936*4882a593Smuzhiyun  * The write_requests tree contains all active write requests which we
937*4882a593Smuzhiyun  * currently know about.  Wait for any requests to complete which conflict with
938*4882a593Smuzhiyun  * the new one.
939*4882a593Smuzhiyun  *
940*4882a593Smuzhiyun  * Only way out: remove the conflicting intervals from the tree.
941*4882a593Smuzhiyun  */
complete_conflicting_writes(struct drbd_request * req)942*4882a593Smuzhiyun static void complete_conflicting_writes(struct drbd_request *req)
943*4882a593Smuzhiyun {
944*4882a593Smuzhiyun 	DEFINE_WAIT(wait);
945*4882a593Smuzhiyun 	struct drbd_device *device = req->device;
946*4882a593Smuzhiyun 	struct drbd_interval *i;
947*4882a593Smuzhiyun 	sector_t sector = req->i.sector;
948*4882a593Smuzhiyun 	int size = req->i.size;
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	for (;;) {
951*4882a593Smuzhiyun 		drbd_for_each_overlap(i, &device->write_requests, sector, size) {
952*4882a593Smuzhiyun 			/* Ignore, if already completed to upper layers. */
953*4882a593Smuzhiyun 			if (i->completed)
954*4882a593Smuzhiyun 				continue;
955*4882a593Smuzhiyun 			/* Handle the first found overlap.  After the schedule
956*4882a593Smuzhiyun 			 * we have to restart the tree walk. */
957*4882a593Smuzhiyun 			break;
958*4882a593Smuzhiyun 		}
959*4882a593Smuzhiyun 		if (!i)	/* if any */
960*4882a593Smuzhiyun 			break;
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 		/* Indicate to wake up device->misc_wait on progress.  */
963*4882a593Smuzhiyun 		prepare_to_wait(&device->misc_wait, &wait, TASK_UNINTERRUPTIBLE);
964*4882a593Smuzhiyun 		i->waiting = true;
965*4882a593Smuzhiyun 		spin_unlock_irq(&device->resource->req_lock);
966*4882a593Smuzhiyun 		schedule();
967*4882a593Smuzhiyun 		spin_lock_irq(&device->resource->req_lock);
968*4882a593Smuzhiyun 	}
969*4882a593Smuzhiyun 	finish_wait(&device->misc_wait, &wait);
970*4882a593Smuzhiyun }
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun /* called within req_lock */
maybe_pull_ahead(struct drbd_device * device)973*4882a593Smuzhiyun static void maybe_pull_ahead(struct drbd_device *device)
974*4882a593Smuzhiyun {
975*4882a593Smuzhiyun 	struct drbd_connection *connection = first_peer_device(device)->connection;
976*4882a593Smuzhiyun 	struct net_conf *nc;
977*4882a593Smuzhiyun 	bool congested = false;
978*4882a593Smuzhiyun 	enum drbd_on_congestion on_congestion;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	rcu_read_lock();
981*4882a593Smuzhiyun 	nc = rcu_dereference(connection->net_conf);
982*4882a593Smuzhiyun 	on_congestion = nc ? nc->on_congestion : OC_BLOCK;
983*4882a593Smuzhiyun 	rcu_read_unlock();
984*4882a593Smuzhiyun 	if (on_congestion == OC_BLOCK ||
985*4882a593Smuzhiyun 	    connection->agreed_pro_version < 96)
986*4882a593Smuzhiyun 		return;
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	if (on_congestion == OC_PULL_AHEAD && device->state.conn == C_AHEAD)
989*4882a593Smuzhiyun 		return; /* nothing to do ... */
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 	/* If I don't even have good local storage, we can not reasonably try
992*4882a593Smuzhiyun 	 * to pull ahead of the peer. We also need the local reference to make
993*4882a593Smuzhiyun 	 * sure device->act_log is there.
994*4882a593Smuzhiyun 	 */
995*4882a593Smuzhiyun 	if (!get_ldev_if_state(device, D_UP_TO_DATE))
996*4882a593Smuzhiyun 		return;
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun 	if (nc->cong_fill &&
999*4882a593Smuzhiyun 	    atomic_read(&device->ap_in_flight) >= nc->cong_fill) {
1000*4882a593Smuzhiyun 		drbd_info(device, "Congestion-fill threshold reached\n");
1001*4882a593Smuzhiyun 		congested = true;
1002*4882a593Smuzhiyun 	}
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 	if (device->act_log->used >= nc->cong_extents) {
1005*4882a593Smuzhiyun 		drbd_info(device, "Congestion-extents threshold reached\n");
1006*4882a593Smuzhiyun 		congested = true;
1007*4882a593Smuzhiyun 	}
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	if (congested) {
1010*4882a593Smuzhiyun 		/* start a new epoch for non-mirrored writes */
1011*4882a593Smuzhiyun 		start_new_tl_epoch(first_peer_device(device)->connection);
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun 		if (on_congestion == OC_PULL_AHEAD)
1014*4882a593Smuzhiyun 			_drbd_set_state(_NS(device, conn, C_AHEAD), 0, NULL);
1015*4882a593Smuzhiyun 		else  /*nc->on_congestion == OC_DISCONNECT */
1016*4882a593Smuzhiyun 			_drbd_set_state(_NS(device, conn, C_DISCONNECTING), 0, NULL);
1017*4882a593Smuzhiyun 	}
1018*4882a593Smuzhiyun 	put_ldev(device);
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun /* If this returns false, and req->private_bio is still set,
1022*4882a593Smuzhiyun  * this should be submitted locally.
1023*4882a593Smuzhiyun  *
1024*4882a593Smuzhiyun  * If it returns false, but req->private_bio is not set,
1025*4882a593Smuzhiyun  * we do not have access to good data :(
1026*4882a593Smuzhiyun  *
1027*4882a593Smuzhiyun  * Otherwise, this destroys req->private_bio, if any,
1028*4882a593Smuzhiyun  * and returns true.
1029*4882a593Smuzhiyun  */
do_remote_read(struct drbd_request * req)1030*4882a593Smuzhiyun static bool do_remote_read(struct drbd_request *req)
1031*4882a593Smuzhiyun {
1032*4882a593Smuzhiyun 	struct drbd_device *device = req->device;
1033*4882a593Smuzhiyun 	enum drbd_read_balancing rbm;
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 	if (req->private_bio) {
1036*4882a593Smuzhiyun 		if (!drbd_may_do_local_read(device,
1037*4882a593Smuzhiyun 					req->i.sector, req->i.size)) {
1038*4882a593Smuzhiyun 			bio_put(req->private_bio);
1039*4882a593Smuzhiyun 			req->private_bio = NULL;
1040*4882a593Smuzhiyun 			put_ldev(device);
1041*4882a593Smuzhiyun 		}
1042*4882a593Smuzhiyun 	}
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 	if (device->state.pdsk != D_UP_TO_DATE)
1045*4882a593Smuzhiyun 		return false;
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	if (req->private_bio == NULL)
1048*4882a593Smuzhiyun 		return true;
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	/* TODO: improve read balancing decisions, take into account drbd
1051*4882a593Smuzhiyun 	 * protocol, pending requests etc. */
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	rcu_read_lock();
1054*4882a593Smuzhiyun 	rbm = rcu_dereference(device->ldev->disk_conf)->read_balancing;
1055*4882a593Smuzhiyun 	rcu_read_unlock();
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	if (rbm == RB_PREFER_LOCAL && req->private_bio)
1058*4882a593Smuzhiyun 		return false; /* submit locally */
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	if (remote_due_to_read_balancing(device, req->i.sector, rbm)) {
1061*4882a593Smuzhiyun 		if (req->private_bio) {
1062*4882a593Smuzhiyun 			bio_put(req->private_bio);
1063*4882a593Smuzhiyun 			req->private_bio = NULL;
1064*4882a593Smuzhiyun 			put_ldev(device);
1065*4882a593Smuzhiyun 		}
1066*4882a593Smuzhiyun 		return true;
1067*4882a593Smuzhiyun 	}
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	return false;
1070*4882a593Smuzhiyun }
1071*4882a593Smuzhiyun 
drbd_should_do_remote(union drbd_dev_state s)1072*4882a593Smuzhiyun bool drbd_should_do_remote(union drbd_dev_state s)
1073*4882a593Smuzhiyun {
1074*4882a593Smuzhiyun 	return s.pdsk == D_UP_TO_DATE ||
1075*4882a593Smuzhiyun 		(s.pdsk >= D_INCONSISTENT &&
1076*4882a593Smuzhiyun 		 s.conn >= C_WF_BITMAP_T &&
1077*4882a593Smuzhiyun 		 s.conn < C_AHEAD);
1078*4882a593Smuzhiyun 	/* Before proto 96 that was >= CONNECTED instead of >= C_WF_BITMAP_T.
1079*4882a593Smuzhiyun 	   That is equivalent since before 96 IO was frozen in the C_WF_BITMAP*
1080*4882a593Smuzhiyun 	   states. */
1081*4882a593Smuzhiyun }
1082*4882a593Smuzhiyun 
drbd_should_send_out_of_sync(union drbd_dev_state s)1083*4882a593Smuzhiyun static bool drbd_should_send_out_of_sync(union drbd_dev_state s)
1084*4882a593Smuzhiyun {
1085*4882a593Smuzhiyun 	return s.conn == C_AHEAD || s.conn == C_WF_BITMAP_S;
1086*4882a593Smuzhiyun 	/* pdsk = D_INCONSISTENT as a consequence. Protocol 96 check not necessary
1087*4882a593Smuzhiyun 	   since we enter state C_AHEAD only if proto >= 96 */
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun /* returns number of connections (== 1, for drbd 8.4)
1091*4882a593Smuzhiyun  * expected to actually write this data,
1092*4882a593Smuzhiyun  * which does NOT include those that we are L_AHEAD for. */
drbd_process_write_request(struct drbd_request * req)1093*4882a593Smuzhiyun static int drbd_process_write_request(struct drbd_request *req)
1094*4882a593Smuzhiyun {
1095*4882a593Smuzhiyun 	struct drbd_device *device = req->device;
1096*4882a593Smuzhiyun 	int remote, send_oos;
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	remote = drbd_should_do_remote(device->state);
1099*4882a593Smuzhiyun 	send_oos = drbd_should_send_out_of_sync(device->state);
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun 	/* Need to replicate writes.  Unless it is an empty flush,
1102*4882a593Smuzhiyun 	 * which is better mapped to a DRBD P_BARRIER packet,
1103*4882a593Smuzhiyun 	 * also for drbd wire protocol compatibility reasons.
1104*4882a593Smuzhiyun 	 * If this was a flush, just start a new epoch.
1105*4882a593Smuzhiyun 	 * Unless the current epoch was empty anyways, or we are not currently
1106*4882a593Smuzhiyun 	 * replicating, in which case there is no point. */
1107*4882a593Smuzhiyun 	if (unlikely(req->i.size == 0)) {
1108*4882a593Smuzhiyun 		/* The only size==0 bios we expect are empty flushes. */
1109*4882a593Smuzhiyun 		D_ASSERT(device, req->master_bio->bi_opf & REQ_PREFLUSH);
1110*4882a593Smuzhiyun 		if (remote)
1111*4882a593Smuzhiyun 			_req_mod(req, QUEUE_AS_DRBD_BARRIER);
1112*4882a593Smuzhiyun 		return remote;
1113*4882a593Smuzhiyun 	}
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 	if (!remote && !send_oos)
1116*4882a593Smuzhiyun 		return 0;
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	D_ASSERT(device, !(remote && send_oos));
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	if (remote) {
1121*4882a593Smuzhiyun 		_req_mod(req, TO_BE_SENT);
1122*4882a593Smuzhiyun 		_req_mod(req, QUEUE_FOR_NET_WRITE);
1123*4882a593Smuzhiyun 	} else if (drbd_set_out_of_sync(device, req->i.sector, req->i.size))
1124*4882a593Smuzhiyun 		_req_mod(req, QUEUE_FOR_SEND_OOS);
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun 	return remote;
1127*4882a593Smuzhiyun }
1128*4882a593Smuzhiyun 
drbd_process_discard_or_zeroes_req(struct drbd_request * req,int flags)1129*4882a593Smuzhiyun static void drbd_process_discard_or_zeroes_req(struct drbd_request *req, int flags)
1130*4882a593Smuzhiyun {
1131*4882a593Smuzhiyun 	int err = drbd_issue_discard_or_zero_out(req->device,
1132*4882a593Smuzhiyun 				req->i.sector, req->i.size >> 9, flags);
1133*4882a593Smuzhiyun 	if (err)
1134*4882a593Smuzhiyun 		req->private_bio->bi_status = BLK_STS_IOERR;
1135*4882a593Smuzhiyun 	bio_endio(req->private_bio);
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun static void
drbd_submit_req_private_bio(struct drbd_request * req)1139*4882a593Smuzhiyun drbd_submit_req_private_bio(struct drbd_request *req)
1140*4882a593Smuzhiyun {
1141*4882a593Smuzhiyun 	struct drbd_device *device = req->device;
1142*4882a593Smuzhiyun 	struct bio *bio = req->private_bio;
1143*4882a593Smuzhiyun 	unsigned int type;
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	if (bio_op(bio) != REQ_OP_READ)
1146*4882a593Smuzhiyun 		type = DRBD_FAULT_DT_WR;
1147*4882a593Smuzhiyun 	else if (bio->bi_opf & REQ_RAHEAD)
1148*4882a593Smuzhiyun 		type = DRBD_FAULT_DT_RA;
1149*4882a593Smuzhiyun 	else
1150*4882a593Smuzhiyun 		type = DRBD_FAULT_DT_RD;
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	bio_set_dev(bio, device->ldev->backing_bdev);
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	/* State may have changed since we grabbed our reference on the
1155*4882a593Smuzhiyun 	 * ->ldev member. Double check, and short-circuit to endio.
1156*4882a593Smuzhiyun 	 * In case the last activity log transaction failed to get on
1157*4882a593Smuzhiyun 	 * stable storage, and this is a WRITE, we may not even submit
1158*4882a593Smuzhiyun 	 * this bio. */
1159*4882a593Smuzhiyun 	if (get_ldev(device)) {
1160*4882a593Smuzhiyun 		if (drbd_insert_fault(device, type))
1161*4882a593Smuzhiyun 			bio_io_error(bio);
1162*4882a593Smuzhiyun 		else if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
1163*4882a593Smuzhiyun 			drbd_process_discard_or_zeroes_req(req, EE_ZEROOUT |
1164*4882a593Smuzhiyun 			    ((bio->bi_opf & REQ_NOUNMAP) ? 0 : EE_TRIM));
1165*4882a593Smuzhiyun 		else if (bio_op(bio) == REQ_OP_DISCARD)
1166*4882a593Smuzhiyun 			drbd_process_discard_or_zeroes_req(req, EE_TRIM);
1167*4882a593Smuzhiyun 		else
1168*4882a593Smuzhiyun 			submit_bio_noacct(bio);
1169*4882a593Smuzhiyun 		put_ldev(device);
1170*4882a593Smuzhiyun 	} else
1171*4882a593Smuzhiyun 		bio_io_error(bio);
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun 
drbd_queue_write(struct drbd_device * device,struct drbd_request * req)1174*4882a593Smuzhiyun static void drbd_queue_write(struct drbd_device *device, struct drbd_request *req)
1175*4882a593Smuzhiyun {
1176*4882a593Smuzhiyun 	spin_lock_irq(&device->resource->req_lock);
1177*4882a593Smuzhiyun 	list_add_tail(&req->tl_requests, &device->submit.writes);
1178*4882a593Smuzhiyun 	list_add_tail(&req->req_pending_master_completion,
1179*4882a593Smuzhiyun 			&device->pending_master_completion[1 /* WRITE */]);
1180*4882a593Smuzhiyun 	spin_unlock_irq(&device->resource->req_lock);
1181*4882a593Smuzhiyun 	queue_work(device->submit.wq, &device->submit.worker);
1182*4882a593Smuzhiyun 	/* do_submit() may sleep internally on al_wait, too */
1183*4882a593Smuzhiyun 	wake_up(&device->al_wait);
1184*4882a593Smuzhiyun }
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun /* returns the new drbd_request pointer, if the caller is expected to
1187*4882a593Smuzhiyun  * drbd_send_and_submit() it (to save latency), or NULL if we queued the
1188*4882a593Smuzhiyun  * request on the submitter thread.
1189*4882a593Smuzhiyun  * Returns ERR_PTR(-ENOMEM) if we cannot allocate a drbd_request.
1190*4882a593Smuzhiyun  */
1191*4882a593Smuzhiyun static struct drbd_request *
drbd_request_prepare(struct drbd_device * device,struct bio * bio,unsigned long start_jif)1192*4882a593Smuzhiyun drbd_request_prepare(struct drbd_device *device, struct bio *bio, unsigned long start_jif)
1193*4882a593Smuzhiyun {
1194*4882a593Smuzhiyun 	const int rw = bio_data_dir(bio);
1195*4882a593Smuzhiyun 	struct drbd_request *req;
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 	/* allocate outside of all locks; */
1198*4882a593Smuzhiyun 	req = drbd_req_new(device, bio);
1199*4882a593Smuzhiyun 	if (!req) {
1200*4882a593Smuzhiyun 		dec_ap_bio(device);
1201*4882a593Smuzhiyun 		/* only pass the error to the upper layers.
1202*4882a593Smuzhiyun 		 * if user cannot handle io errors, that's not our business. */
1203*4882a593Smuzhiyun 		drbd_err(device, "could not kmalloc() req\n");
1204*4882a593Smuzhiyun 		bio->bi_status = BLK_STS_RESOURCE;
1205*4882a593Smuzhiyun 		bio_endio(bio);
1206*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1207*4882a593Smuzhiyun 	}
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	/* Update disk stats */
1210*4882a593Smuzhiyun 	req->start_jif = bio_start_io_acct(req->master_bio);
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	if (!get_ldev(device)) {
1213*4882a593Smuzhiyun 		bio_put(req->private_bio);
1214*4882a593Smuzhiyun 		req->private_bio = NULL;
1215*4882a593Smuzhiyun 	}
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun 	/* process discards always from our submitter thread */
1218*4882a593Smuzhiyun 	if (bio_op(bio) == REQ_OP_WRITE_ZEROES ||
1219*4882a593Smuzhiyun 	    bio_op(bio) == REQ_OP_DISCARD)
1220*4882a593Smuzhiyun 		goto queue_for_submitter_thread;
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	if (rw == WRITE && req->private_bio && req->i.size
1223*4882a593Smuzhiyun 	&& !test_bit(AL_SUSPENDED, &device->flags)) {
1224*4882a593Smuzhiyun 		if (!drbd_al_begin_io_fastpath(device, &req->i))
1225*4882a593Smuzhiyun 			goto queue_for_submitter_thread;
1226*4882a593Smuzhiyun 		req->rq_state |= RQ_IN_ACT_LOG;
1227*4882a593Smuzhiyun 		req->in_actlog_jif = jiffies;
1228*4882a593Smuzhiyun 	}
1229*4882a593Smuzhiyun 	return req;
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun  queue_for_submitter_thread:
1232*4882a593Smuzhiyun 	atomic_inc(&device->ap_actlog_cnt);
1233*4882a593Smuzhiyun 	drbd_queue_write(device, req);
1234*4882a593Smuzhiyun 	return NULL;
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun /* Require at least one path to current data.
1238*4882a593Smuzhiyun  * We don't want to allow writes on C_STANDALONE D_INCONSISTENT:
1239*4882a593Smuzhiyun  * We would not allow to read what was written,
1240*4882a593Smuzhiyun  * we would not have bumped the data generation uuids,
1241*4882a593Smuzhiyun  * we would cause data divergence for all the wrong reasons.
1242*4882a593Smuzhiyun  *
1243*4882a593Smuzhiyun  * If we don't see at least one D_UP_TO_DATE, we will fail this request,
1244*4882a593Smuzhiyun  * which either returns EIO, or, if OND_SUSPEND_IO is set, suspends IO,
1245*4882a593Smuzhiyun  * and queues for retry later.
1246*4882a593Smuzhiyun  */
may_do_writes(struct drbd_device * device)1247*4882a593Smuzhiyun static bool may_do_writes(struct drbd_device *device)
1248*4882a593Smuzhiyun {
1249*4882a593Smuzhiyun 	const union drbd_dev_state s = device->state;
1250*4882a593Smuzhiyun 	return s.disk == D_UP_TO_DATE || s.pdsk == D_UP_TO_DATE;
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun struct drbd_plug_cb {
1254*4882a593Smuzhiyun 	struct blk_plug_cb cb;
1255*4882a593Smuzhiyun 	struct drbd_request *most_recent_req;
1256*4882a593Smuzhiyun 	/* do we need more? */
1257*4882a593Smuzhiyun };
1258*4882a593Smuzhiyun 
drbd_unplug(struct blk_plug_cb * cb,bool from_schedule)1259*4882a593Smuzhiyun static void drbd_unplug(struct blk_plug_cb *cb, bool from_schedule)
1260*4882a593Smuzhiyun {
1261*4882a593Smuzhiyun 	struct drbd_plug_cb *plug = container_of(cb, struct drbd_plug_cb, cb);
1262*4882a593Smuzhiyun 	struct drbd_resource *resource = plug->cb.data;
1263*4882a593Smuzhiyun 	struct drbd_request *req = plug->most_recent_req;
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	kfree(cb);
1266*4882a593Smuzhiyun 	if (!req)
1267*4882a593Smuzhiyun 		return;
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	spin_lock_irq(&resource->req_lock);
1270*4882a593Smuzhiyun 	/* In case the sender did not process it yet, raise the flag to
1271*4882a593Smuzhiyun 	 * have it followed with P_UNPLUG_REMOTE just after. */
1272*4882a593Smuzhiyun 	req->rq_state |= RQ_UNPLUG;
1273*4882a593Smuzhiyun 	/* but also queue a generic unplug */
1274*4882a593Smuzhiyun 	drbd_queue_unplug(req->device);
1275*4882a593Smuzhiyun 	kref_put(&req->kref, drbd_req_destroy);
1276*4882a593Smuzhiyun 	spin_unlock_irq(&resource->req_lock);
1277*4882a593Smuzhiyun }
1278*4882a593Smuzhiyun 
drbd_check_plugged(struct drbd_resource * resource)1279*4882a593Smuzhiyun static struct drbd_plug_cb* drbd_check_plugged(struct drbd_resource *resource)
1280*4882a593Smuzhiyun {
1281*4882a593Smuzhiyun 	/* A lot of text to say
1282*4882a593Smuzhiyun 	 * return (struct drbd_plug_cb*)blk_check_plugged(); */
1283*4882a593Smuzhiyun 	struct drbd_plug_cb *plug;
1284*4882a593Smuzhiyun 	struct blk_plug_cb *cb = blk_check_plugged(drbd_unplug, resource, sizeof(*plug));
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	if (cb)
1287*4882a593Smuzhiyun 		plug = container_of(cb, struct drbd_plug_cb, cb);
1288*4882a593Smuzhiyun 	else
1289*4882a593Smuzhiyun 		plug = NULL;
1290*4882a593Smuzhiyun 	return plug;
1291*4882a593Smuzhiyun }
1292*4882a593Smuzhiyun 
drbd_update_plug(struct drbd_plug_cb * plug,struct drbd_request * req)1293*4882a593Smuzhiyun static void drbd_update_plug(struct drbd_plug_cb *plug, struct drbd_request *req)
1294*4882a593Smuzhiyun {
1295*4882a593Smuzhiyun 	struct drbd_request *tmp = plug->most_recent_req;
1296*4882a593Smuzhiyun 	/* Will be sent to some peer.
1297*4882a593Smuzhiyun 	 * Remember to tag it with UNPLUG_REMOTE on unplug */
1298*4882a593Smuzhiyun 	kref_get(&req->kref);
1299*4882a593Smuzhiyun 	plug->most_recent_req = req;
1300*4882a593Smuzhiyun 	if (tmp)
1301*4882a593Smuzhiyun 		kref_put(&tmp->kref, drbd_req_destroy);
1302*4882a593Smuzhiyun }
1303*4882a593Smuzhiyun 
drbd_send_and_submit(struct drbd_device * device,struct drbd_request * req)1304*4882a593Smuzhiyun static void drbd_send_and_submit(struct drbd_device *device, struct drbd_request *req)
1305*4882a593Smuzhiyun {
1306*4882a593Smuzhiyun 	struct drbd_resource *resource = device->resource;
1307*4882a593Smuzhiyun 	const int rw = bio_data_dir(req->master_bio);
1308*4882a593Smuzhiyun 	struct bio_and_error m = { NULL, };
1309*4882a593Smuzhiyun 	bool no_remote = false;
1310*4882a593Smuzhiyun 	bool submit_private_bio = false;
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	spin_lock_irq(&resource->req_lock);
1313*4882a593Smuzhiyun 	if (rw == WRITE) {
1314*4882a593Smuzhiyun 		/* This may temporarily give up the req_lock,
1315*4882a593Smuzhiyun 		 * but will re-aquire it before it returns here.
1316*4882a593Smuzhiyun 		 * Needs to be before the check on drbd_suspended() */
1317*4882a593Smuzhiyun 		complete_conflicting_writes(req);
1318*4882a593Smuzhiyun 		/* no more giving up req_lock from now on! */
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 		/* check for congestion, and potentially stop sending
1321*4882a593Smuzhiyun 		 * full data updates, but start sending "dirty bits" only. */
1322*4882a593Smuzhiyun 		maybe_pull_ahead(device);
1323*4882a593Smuzhiyun 	}
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	if (drbd_suspended(device)) {
1327*4882a593Smuzhiyun 		/* push back and retry: */
1328*4882a593Smuzhiyun 		req->rq_state |= RQ_POSTPONED;
1329*4882a593Smuzhiyun 		if (req->private_bio) {
1330*4882a593Smuzhiyun 			bio_put(req->private_bio);
1331*4882a593Smuzhiyun 			req->private_bio = NULL;
1332*4882a593Smuzhiyun 			put_ldev(device);
1333*4882a593Smuzhiyun 		}
1334*4882a593Smuzhiyun 		goto out;
1335*4882a593Smuzhiyun 	}
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	/* We fail READ early, if we can not serve it.
1338*4882a593Smuzhiyun 	 * We must do this before req is registered on any lists.
1339*4882a593Smuzhiyun 	 * Otherwise, drbd_req_complete() will queue failed READ for retry. */
1340*4882a593Smuzhiyun 	if (rw != WRITE) {
1341*4882a593Smuzhiyun 		if (!do_remote_read(req) && !req->private_bio)
1342*4882a593Smuzhiyun 			goto nodata;
1343*4882a593Smuzhiyun 	}
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	/* which transfer log epoch does this belong to? */
1346*4882a593Smuzhiyun 	req->epoch = atomic_read(&first_peer_device(device)->connection->current_tle_nr);
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun 	/* no point in adding empty flushes to the transfer log,
1349*4882a593Smuzhiyun 	 * they are mapped to drbd barriers already. */
1350*4882a593Smuzhiyun 	if (likely(req->i.size!=0)) {
1351*4882a593Smuzhiyun 		if (rw == WRITE)
1352*4882a593Smuzhiyun 			first_peer_device(device)->connection->current_tle_writes++;
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun 		list_add_tail(&req->tl_requests, &first_peer_device(device)->connection->transfer_log);
1355*4882a593Smuzhiyun 	}
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun 	if (rw == WRITE) {
1358*4882a593Smuzhiyun 		if (req->private_bio && !may_do_writes(device)) {
1359*4882a593Smuzhiyun 			bio_put(req->private_bio);
1360*4882a593Smuzhiyun 			req->private_bio = NULL;
1361*4882a593Smuzhiyun 			put_ldev(device);
1362*4882a593Smuzhiyun 			goto nodata;
1363*4882a593Smuzhiyun 		}
1364*4882a593Smuzhiyun 		if (!drbd_process_write_request(req))
1365*4882a593Smuzhiyun 			no_remote = true;
1366*4882a593Smuzhiyun 	} else {
1367*4882a593Smuzhiyun 		/* We either have a private_bio, or we can read from remote.
1368*4882a593Smuzhiyun 		 * Otherwise we had done the goto nodata above. */
1369*4882a593Smuzhiyun 		if (req->private_bio == NULL) {
1370*4882a593Smuzhiyun 			_req_mod(req, TO_BE_SENT);
1371*4882a593Smuzhiyun 			_req_mod(req, QUEUE_FOR_NET_READ);
1372*4882a593Smuzhiyun 		} else
1373*4882a593Smuzhiyun 			no_remote = true;
1374*4882a593Smuzhiyun 	}
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 	if (no_remote == false) {
1377*4882a593Smuzhiyun 		struct drbd_plug_cb *plug = drbd_check_plugged(resource);
1378*4882a593Smuzhiyun 		if (plug)
1379*4882a593Smuzhiyun 			drbd_update_plug(plug, req);
1380*4882a593Smuzhiyun 	}
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 	/* If it took the fast path in drbd_request_prepare, add it here.
1383*4882a593Smuzhiyun 	 * The slow path has added it already. */
1384*4882a593Smuzhiyun 	if (list_empty(&req->req_pending_master_completion))
1385*4882a593Smuzhiyun 		list_add_tail(&req->req_pending_master_completion,
1386*4882a593Smuzhiyun 			&device->pending_master_completion[rw == WRITE]);
1387*4882a593Smuzhiyun 	if (req->private_bio) {
1388*4882a593Smuzhiyun 		/* needs to be marked within the same spinlock */
1389*4882a593Smuzhiyun 		req->pre_submit_jif = jiffies;
1390*4882a593Smuzhiyun 		list_add_tail(&req->req_pending_local,
1391*4882a593Smuzhiyun 			&device->pending_completion[rw == WRITE]);
1392*4882a593Smuzhiyun 		_req_mod(req, TO_BE_SUBMITTED);
1393*4882a593Smuzhiyun 		/* but we need to give up the spinlock to submit */
1394*4882a593Smuzhiyun 		submit_private_bio = true;
1395*4882a593Smuzhiyun 	} else if (no_remote) {
1396*4882a593Smuzhiyun nodata:
1397*4882a593Smuzhiyun 		if (__ratelimit(&drbd_ratelimit_state))
1398*4882a593Smuzhiyun 			drbd_err(device, "IO ERROR: neither local nor remote data, sector %llu+%u\n",
1399*4882a593Smuzhiyun 					(unsigned long long)req->i.sector, req->i.size >> 9);
1400*4882a593Smuzhiyun 		/* A write may have been queued for send_oos, however.
1401*4882a593Smuzhiyun 		 * So we can not simply free it, we must go through drbd_req_put_completion_ref() */
1402*4882a593Smuzhiyun 	}
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun out:
1405*4882a593Smuzhiyun 	drbd_req_put_completion_ref(req, &m, 1);
1406*4882a593Smuzhiyun 	spin_unlock_irq(&resource->req_lock);
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 	/* Even though above is a kref_put(), this is safe.
1409*4882a593Smuzhiyun 	 * As long as we still need to submit our private bio,
1410*4882a593Smuzhiyun 	 * we hold a completion ref, and the request cannot disappear.
1411*4882a593Smuzhiyun 	 * If however this request did not even have a private bio to submit
1412*4882a593Smuzhiyun 	 * (e.g. remote read), req may already be invalid now.
1413*4882a593Smuzhiyun 	 * That's why we cannot check on req->private_bio. */
1414*4882a593Smuzhiyun 	if (submit_private_bio)
1415*4882a593Smuzhiyun 		drbd_submit_req_private_bio(req);
1416*4882a593Smuzhiyun 	if (m.bio)
1417*4882a593Smuzhiyun 		complete_master_bio(device, &m);
1418*4882a593Smuzhiyun }
1419*4882a593Smuzhiyun 
__drbd_make_request(struct drbd_device * device,struct bio * bio,unsigned long start_jif)1420*4882a593Smuzhiyun void __drbd_make_request(struct drbd_device *device, struct bio *bio, unsigned long start_jif)
1421*4882a593Smuzhiyun {
1422*4882a593Smuzhiyun 	struct drbd_request *req = drbd_request_prepare(device, bio, start_jif);
1423*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(req))
1424*4882a593Smuzhiyun 		return;
1425*4882a593Smuzhiyun 	drbd_send_and_submit(device, req);
1426*4882a593Smuzhiyun }
1427*4882a593Smuzhiyun 
submit_fast_path(struct drbd_device * device,struct list_head * incoming)1428*4882a593Smuzhiyun static void submit_fast_path(struct drbd_device *device, struct list_head *incoming)
1429*4882a593Smuzhiyun {
1430*4882a593Smuzhiyun 	struct blk_plug plug;
1431*4882a593Smuzhiyun 	struct drbd_request *req, *tmp;
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	blk_start_plug(&plug);
1434*4882a593Smuzhiyun 	list_for_each_entry_safe(req, tmp, incoming, tl_requests) {
1435*4882a593Smuzhiyun 		const int rw = bio_data_dir(req->master_bio);
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun 		if (rw == WRITE /* rw != WRITE should not even end up here! */
1438*4882a593Smuzhiyun 		&& req->private_bio && req->i.size
1439*4882a593Smuzhiyun 		&& !test_bit(AL_SUSPENDED, &device->flags)) {
1440*4882a593Smuzhiyun 			if (!drbd_al_begin_io_fastpath(device, &req->i))
1441*4882a593Smuzhiyun 				continue;
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun 			req->rq_state |= RQ_IN_ACT_LOG;
1444*4882a593Smuzhiyun 			req->in_actlog_jif = jiffies;
1445*4882a593Smuzhiyun 			atomic_dec(&device->ap_actlog_cnt);
1446*4882a593Smuzhiyun 		}
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 		list_del_init(&req->tl_requests);
1449*4882a593Smuzhiyun 		drbd_send_and_submit(device, req);
1450*4882a593Smuzhiyun 	}
1451*4882a593Smuzhiyun 	blk_finish_plug(&plug);
1452*4882a593Smuzhiyun }
1453*4882a593Smuzhiyun 
prepare_al_transaction_nonblock(struct drbd_device * device,struct list_head * incoming,struct list_head * pending,struct list_head * later)1454*4882a593Smuzhiyun static bool prepare_al_transaction_nonblock(struct drbd_device *device,
1455*4882a593Smuzhiyun 					    struct list_head *incoming,
1456*4882a593Smuzhiyun 					    struct list_head *pending,
1457*4882a593Smuzhiyun 					    struct list_head *later)
1458*4882a593Smuzhiyun {
1459*4882a593Smuzhiyun 	struct drbd_request *req;
1460*4882a593Smuzhiyun 	int wake = 0;
1461*4882a593Smuzhiyun 	int err;
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	spin_lock_irq(&device->al_lock);
1464*4882a593Smuzhiyun 	while ((req = list_first_entry_or_null(incoming, struct drbd_request, tl_requests))) {
1465*4882a593Smuzhiyun 		err = drbd_al_begin_io_nonblock(device, &req->i);
1466*4882a593Smuzhiyun 		if (err == -ENOBUFS)
1467*4882a593Smuzhiyun 			break;
1468*4882a593Smuzhiyun 		if (err == -EBUSY)
1469*4882a593Smuzhiyun 			wake = 1;
1470*4882a593Smuzhiyun 		if (err)
1471*4882a593Smuzhiyun 			list_move_tail(&req->tl_requests, later);
1472*4882a593Smuzhiyun 		else
1473*4882a593Smuzhiyun 			list_move_tail(&req->tl_requests, pending);
1474*4882a593Smuzhiyun 	}
1475*4882a593Smuzhiyun 	spin_unlock_irq(&device->al_lock);
1476*4882a593Smuzhiyun 	if (wake)
1477*4882a593Smuzhiyun 		wake_up(&device->al_wait);
1478*4882a593Smuzhiyun 	return !list_empty(pending);
1479*4882a593Smuzhiyun }
1480*4882a593Smuzhiyun 
send_and_submit_pending(struct drbd_device * device,struct list_head * pending)1481*4882a593Smuzhiyun static void send_and_submit_pending(struct drbd_device *device, struct list_head *pending)
1482*4882a593Smuzhiyun {
1483*4882a593Smuzhiyun 	struct blk_plug plug;
1484*4882a593Smuzhiyun 	struct drbd_request *req;
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun 	blk_start_plug(&plug);
1487*4882a593Smuzhiyun 	while ((req = list_first_entry_or_null(pending, struct drbd_request, tl_requests))) {
1488*4882a593Smuzhiyun 		req->rq_state |= RQ_IN_ACT_LOG;
1489*4882a593Smuzhiyun 		req->in_actlog_jif = jiffies;
1490*4882a593Smuzhiyun 		atomic_dec(&device->ap_actlog_cnt);
1491*4882a593Smuzhiyun 		list_del_init(&req->tl_requests);
1492*4882a593Smuzhiyun 		drbd_send_and_submit(device, req);
1493*4882a593Smuzhiyun 	}
1494*4882a593Smuzhiyun 	blk_finish_plug(&plug);
1495*4882a593Smuzhiyun }
1496*4882a593Smuzhiyun 
do_submit(struct work_struct * ws)1497*4882a593Smuzhiyun void do_submit(struct work_struct *ws)
1498*4882a593Smuzhiyun {
1499*4882a593Smuzhiyun 	struct drbd_device *device = container_of(ws, struct drbd_device, submit.worker);
1500*4882a593Smuzhiyun 	LIST_HEAD(incoming);	/* from drbd_make_request() */
1501*4882a593Smuzhiyun 	LIST_HEAD(pending);	/* to be submitted after next AL-transaction commit */
1502*4882a593Smuzhiyun 	LIST_HEAD(busy);	/* blocked by resync requests */
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun 	/* grab new incoming requests */
1505*4882a593Smuzhiyun 	spin_lock_irq(&device->resource->req_lock);
1506*4882a593Smuzhiyun 	list_splice_tail_init(&device->submit.writes, &incoming);
1507*4882a593Smuzhiyun 	spin_unlock_irq(&device->resource->req_lock);
1508*4882a593Smuzhiyun 
1509*4882a593Smuzhiyun 	for (;;) {
1510*4882a593Smuzhiyun 		DEFINE_WAIT(wait);
1511*4882a593Smuzhiyun 
1512*4882a593Smuzhiyun 		/* move used-to-be-busy back to front of incoming */
1513*4882a593Smuzhiyun 		list_splice_init(&busy, &incoming);
1514*4882a593Smuzhiyun 		submit_fast_path(device, &incoming);
1515*4882a593Smuzhiyun 		if (list_empty(&incoming))
1516*4882a593Smuzhiyun 			break;
1517*4882a593Smuzhiyun 
1518*4882a593Smuzhiyun 		for (;;) {
1519*4882a593Smuzhiyun 			prepare_to_wait(&device->al_wait, &wait, TASK_UNINTERRUPTIBLE);
1520*4882a593Smuzhiyun 
1521*4882a593Smuzhiyun 			list_splice_init(&busy, &incoming);
1522*4882a593Smuzhiyun 			prepare_al_transaction_nonblock(device, &incoming, &pending, &busy);
1523*4882a593Smuzhiyun 			if (!list_empty(&pending))
1524*4882a593Smuzhiyun 				break;
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun 			schedule();
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 			/* If all currently "hot" activity log extents are kept busy by
1529*4882a593Smuzhiyun 			 * incoming requests, we still must not totally starve new
1530*4882a593Smuzhiyun 			 * requests to "cold" extents.
1531*4882a593Smuzhiyun 			 * Something left on &incoming means there had not been
1532*4882a593Smuzhiyun 			 * enough update slots available, and the activity log
1533*4882a593Smuzhiyun 			 * has been marked as "starving".
1534*4882a593Smuzhiyun 			 *
1535*4882a593Smuzhiyun 			 * Try again now, without looking for new requests,
1536*4882a593Smuzhiyun 			 * effectively blocking all new requests until we made
1537*4882a593Smuzhiyun 			 * at least _some_ progress with what we currently have.
1538*4882a593Smuzhiyun 			 */
1539*4882a593Smuzhiyun 			if (!list_empty(&incoming))
1540*4882a593Smuzhiyun 				continue;
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun 			/* Nothing moved to pending, but nothing left
1543*4882a593Smuzhiyun 			 * on incoming: all moved to busy!
1544*4882a593Smuzhiyun 			 * Grab new and iterate. */
1545*4882a593Smuzhiyun 			spin_lock_irq(&device->resource->req_lock);
1546*4882a593Smuzhiyun 			list_splice_tail_init(&device->submit.writes, &incoming);
1547*4882a593Smuzhiyun 			spin_unlock_irq(&device->resource->req_lock);
1548*4882a593Smuzhiyun 		}
1549*4882a593Smuzhiyun 		finish_wait(&device->al_wait, &wait);
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun 		/* If the transaction was full, before all incoming requests
1552*4882a593Smuzhiyun 		 * had been processed, skip ahead to commit, and iterate
1553*4882a593Smuzhiyun 		 * without splicing in more incoming requests from upper layers.
1554*4882a593Smuzhiyun 		 *
1555*4882a593Smuzhiyun 		 * Else, if all incoming have been processed,
1556*4882a593Smuzhiyun 		 * they have become either "pending" (to be submitted after
1557*4882a593Smuzhiyun 		 * next transaction commit) or "busy" (blocked by resync).
1558*4882a593Smuzhiyun 		 *
1559*4882a593Smuzhiyun 		 * Maybe more was queued, while we prepared the transaction?
1560*4882a593Smuzhiyun 		 * Try to stuff those into this transaction as well.
1561*4882a593Smuzhiyun 		 * Be strictly non-blocking here,
1562*4882a593Smuzhiyun 		 * we already have something to commit.
1563*4882a593Smuzhiyun 		 *
1564*4882a593Smuzhiyun 		 * Commit if we don't make any more progres.
1565*4882a593Smuzhiyun 		 */
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun 		while (list_empty(&incoming)) {
1568*4882a593Smuzhiyun 			LIST_HEAD(more_pending);
1569*4882a593Smuzhiyun 			LIST_HEAD(more_incoming);
1570*4882a593Smuzhiyun 			bool made_progress;
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun 			/* It is ok to look outside the lock,
1573*4882a593Smuzhiyun 			 * it's only an optimization anyways */
1574*4882a593Smuzhiyun 			if (list_empty(&device->submit.writes))
1575*4882a593Smuzhiyun 				break;
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun 			spin_lock_irq(&device->resource->req_lock);
1578*4882a593Smuzhiyun 			list_splice_tail_init(&device->submit.writes, &more_incoming);
1579*4882a593Smuzhiyun 			spin_unlock_irq(&device->resource->req_lock);
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun 			if (list_empty(&more_incoming))
1582*4882a593Smuzhiyun 				break;
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun 			made_progress = prepare_al_transaction_nonblock(device, &more_incoming, &more_pending, &busy);
1585*4882a593Smuzhiyun 
1586*4882a593Smuzhiyun 			list_splice_tail_init(&more_pending, &pending);
1587*4882a593Smuzhiyun 			list_splice_tail_init(&more_incoming, &incoming);
1588*4882a593Smuzhiyun 			if (!made_progress)
1589*4882a593Smuzhiyun 				break;
1590*4882a593Smuzhiyun 		}
1591*4882a593Smuzhiyun 
1592*4882a593Smuzhiyun 		drbd_al_begin_io_commit(device);
1593*4882a593Smuzhiyun 		send_and_submit_pending(device, &pending);
1594*4882a593Smuzhiyun 	}
1595*4882a593Smuzhiyun }
1596*4882a593Smuzhiyun 
drbd_submit_bio(struct bio * bio)1597*4882a593Smuzhiyun blk_qc_t drbd_submit_bio(struct bio *bio)
1598*4882a593Smuzhiyun {
1599*4882a593Smuzhiyun 	struct drbd_device *device = bio->bi_disk->private_data;
1600*4882a593Smuzhiyun 	unsigned long start_jif;
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 	blk_queue_split(&bio);
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun 	start_jif = jiffies;
1605*4882a593Smuzhiyun 
1606*4882a593Smuzhiyun 	/*
1607*4882a593Smuzhiyun 	 * what we "blindly" assume:
1608*4882a593Smuzhiyun 	 */
1609*4882a593Smuzhiyun 	D_ASSERT(device, IS_ALIGNED(bio->bi_iter.bi_size, 512));
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun 	inc_ap_bio(device);
1612*4882a593Smuzhiyun 	__drbd_make_request(device, bio, start_jif);
1613*4882a593Smuzhiyun 	return BLK_QC_T_NONE;
1614*4882a593Smuzhiyun }
1615*4882a593Smuzhiyun 
net_timeout_reached(struct drbd_request * net_req,struct drbd_connection * connection,unsigned long now,unsigned long ent,unsigned int ko_count,unsigned int timeout)1616*4882a593Smuzhiyun static bool net_timeout_reached(struct drbd_request *net_req,
1617*4882a593Smuzhiyun 		struct drbd_connection *connection,
1618*4882a593Smuzhiyun 		unsigned long now, unsigned long ent,
1619*4882a593Smuzhiyun 		unsigned int ko_count, unsigned int timeout)
1620*4882a593Smuzhiyun {
1621*4882a593Smuzhiyun 	struct drbd_device *device = net_req->device;
1622*4882a593Smuzhiyun 
1623*4882a593Smuzhiyun 	if (!time_after(now, net_req->pre_send_jif + ent))
1624*4882a593Smuzhiyun 		return false;
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	if (time_in_range(now, connection->last_reconnect_jif, connection->last_reconnect_jif + ent))
1627*4882a593Smuzhiyun 		return false;
1628*4882a593Smuzhiyun 
1629*4882a593Smuzhiyun 	if (net_req->rq_state & RQ_NET_PENDING) {
1630*4882a593Smuzhiyun 		drbd_warn(device, "Remote failed to finish a request within %ums > ko-count (%u) * timeout (%u * 0.1s)\n",
1631*4882a593Smuzhiyun 			jiffies_to_msecs(now - net_req->pre_send_jif), ko_count, timeout);
1632*4882a593Smuzhiyun 		return true;
1633*4882a593Smuzhiyun 	}
1634*4882a593Smuzhiyun 
1635*4882a593Smuzhiyun 	/* We received an ACK already (or are using protocol A),
1636*4882a593Smuzhiyun 	 * but are waiting for the epoch closing barrier ack.
1637*4882a593Smuzhiyun 	 * Check if we sent the barrier already.  We should not blame the peer
1638*4882a593Smuzhiyun 	 * for being unresponsive, if we did not even ask it yet. */
1639*4882a593Smuzhiyun 	if (net_req->epoch == connection->send.current_epoch_nr) {
1640*4882a593Smuzhiyun 		drbd_warn(device,
1641*4882a593Smuzhiyun 			"We did not send a P_BARRIER for %ums > ko-count (%u) * timeout (%u * 0.1s); drbd kernel thread blocked?\n",
1642*4882a593Smuzhiyun 			jiffies_to_msecs(now - net_req->pre_send_jif), ko_count, timeout);
1643*4882a593Smuzhiyun 		return false;
1644*4882a593Smuzhiyun 	}
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun 	/* Worst case: we may have been blocked for whatever reason, then
1647*4882a593Smuzhiyun 	 * suddenly are able to send a lot of requests (and epoch separating
1648*4882a593Smuzhiyun 	 * barriers) in quick succession.
1649*4882a593Smuzhiyun 	 * The timestamp of the net_req may be much too old and not correspond
1650*4882a593Smuzhiyun 	 * to the sending time of the relevant unack'ed barrier packet, so
1651*4882a593Smuzhiyun 	 * would trigger a spurious timeout.  The latest barrier packet may
1652*4882a593Smuzhiyun 	 * have a too recent timestamp to trigger the timeout, potentially miss
1653*4882a593Smuzhiyun 	 * a timeout.  Right now we don't have a place to conveniently store
1654*4882a593Smuzhiyun 	 * these timestamps.
1655*4882a593Smuzhiyun 	 * But in this particular situation, the application requests are still
1656*4882a593Smuzhiyun 	 * completed to upper layers, DRBD should still "feel" responsive.
1657*4882a593Smuzhiyun 	 * No need yet to kill this connection, it may still recover.
1658*4882a593Smuzhiyun 	 * If not, eventually we will have queued enough into the network for
1659*4882a593Smuzhiyun 	 * us to block. From that point of view, the timestamp of the last sent
1660*4882a593Smuzhiyun 	 * barrier packet is relevant enough.
1661*4882a593Smuzhiyun 	 */
1662*4882a593Smuzhiyun 	if (time_after(now, connection->send.last_sent_barrier_jif + ent)) {
1663*4882a593Smuzhiyun 		drbd_warn(device, "Remote failed to answer a P_BARRIER (sent at %lu jif; now=%lu jif) within %ums > ko-count (%u) * timeout (%u * 0.1s)\n",
1664*4882a593Smuzhiyun 			connection->send.last_sent_barrier_jif, now,
1665*4882a593Smuzhiyun 			jiffies_to_msecs(now - connection->send.last_sent_barrier_jif), ko_count, timeout);
1666*4882a593Smuzhiyun 		return true;
1667*4882a593Smuzhiyun 	}
1668*4882a593Smuzhiyun 	return false;
1669*4882a593Smuzhiyun }
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun /* A request is considered timed out, if
1672*4882a593Smuzhiyun  * - we have some effective timeout from the configuration,
1673*4882a593Smuzhiyun  *   with some state restrictions applied,
1674*4882a593Smuzhiyun  * - the oldest request is waiting for a response from the network
1675*4882a593Smuzhiyun  *   resp. the local disk,
1676*4882a593Smuzhiyun  * - the oldest request is in fact older than the effective timeout,
1677*4882a593Smuzhiyun  * - the connection was established (resp. disk was attached)
1678*4882a593Smuzhiyun  *   for longer than the timeout already.
1679*4882a593Smuzhiyun  * Note that for 32bit jiffies and very stable connections/disks,
1680*4882a593Smuzhiyun  * we may have a wrap around, which is catched by
1681*4882a593Smuzhiyun  *   !time_in_range(now, last_..._jif, last_..._jif + timeout).
1682*4882a593Smuzhiyun  *
1683*4882a593Smuzhiyun  * Side effect: once per 32bit wrap-around interval, which means every
1684*4882a593Smuzhiyun  * ~198 days with 250 HZ, we have a window where the timeout would need
1685*4882a593Smuzhiyun  * to expire twice (worst case) to become effective. Good enough.
1686*4882a593Smuzhiyun  */
1687*4882a593Smuzhiyun 
request_timer_fn(struct timer_list * t)1688*4882a593Smuzhiyun void request_timer_fn(struct timer_list *t)
1689*4882a593Smuzhiyun {
1690*4882a593Smuzhiyun 	struct drbd_device *device = from_timer(device, t, request_timer);
1691*4882a593Smuzhiyun 	struct drbd_connection *connection = first_peer_device(device)->connection;
1692*4882a593Smuzhiyun 	struct drbd_request *req_read, *req_write, *req_peer; /* oldest request */
1693*4882a593Smuzhiyun 	struct net_conf *nc;
1694*4882a593Smuzhiyun 	unsigned long oldest_submit_jif;
1695*4882a593Smuzhiyun 	unsigned long ent = 0, dt = 0, et, nt; /* effective timeout = ko_count * timeout */
1696*4882a593Smuzhiyun 	unsigned long now;
1697*4882a593Smuzhiyun 	unsigned int ko_count = 0, timeout = 0;
1698*4882a593Smuzhiyun 
1699*4882a593Smuzhiyun 	rcu_read_lock();
1700*4882a593Smuzhiyun 	nc = rcu_dereference(connection->net_conf);
1701*4882a593Smuzhiyun 	if (nc && device->state.conn >= C_WF_REPORT_PARAMS) {
1702*4882a593Smuzhiyun 		ko_count = nc->ko_count;
1703*4882a593Smuzhiyun 		timeout = nc->timeout;
1704*4882a593Smuzhiyun 	}
1705*4882a593Smuzhiyun 
1706*4882a593Smuzhiyun 	if (get_ldev(device)) { /* implicit state.disk >= D_INCONSISTENT */
1707*4882a593Smuzhiyun 		dt = rcu_dereference(device->ldev->disk_conf)->disk_timeout * HZ / 10;
1708*4882a593Smuzhiyun 		put_ldev(device);
1709*4882a593Smuzhiyun 	}
1710*4882a593Smuzhiyun 	rcu_read_unlock();
1711*4882a593Smuzhiyun 
1712*4882a593Smuzhiyun 
1713*4882a593Smuzhiyun 	ent = timeout * HZ/10 * ko_count;
1714*4882a593Smuzhiyun 	et = min_not_zero(dt, ent);
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 	if (!et)
1717*4882a593Smuzhiyun 		return; /* Recurring timer stopped */
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun 	now = jiffies;
1720*4882a593Smuzhiyun 	nt = now + et;
1721*4882a593Smuzhiyun 
1722*4882a593Smuzhiyun 	spin_lock_irq(&device->resource->req_lock);
1723*4882a593Smuzhiyun 	req_read = list_first_entry_or_null(&device->pending_completion[0], struct drbd_request, req_pending_local);
1724*4882a593Smuzhiyun 	req_write = list_first_entry_or_null(&device->pending_completion[1], struct drbd_request, req_pending_local);
1725*4882a593Smuzhiyun 
1726*4882a593Smuzhiyun 	/* maybe the oldest request waiting for the peer is in fact still
1727*4882a593Smuzhiyun 	 * blocking in tcp sendmsg.  That's ok, though, that's handled via the
1728*4882a593Smuzhiyun 	 * socket send timeout, requesting a ping, and bumping ko-count in
1729*4882a593Smuzhiyun 	 * we_should_drop_the_connection().
1730*4882a593Smuzhiyun 	 */
1731*4882a593Smuzhiyun 
1732*4882a593Smuzhiyun 	/* check the oldest request we did successfully sent,
1733*4882a593Smuzhiyun 	 * but which is still waiting for an ACK. */
1734*4882a593Smuzhiyun 	req_peer = connection->req_ack_pending;
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 	/* if we don't have such request (e.g. protocoll A)
1737*4882a593Smuzhiyun 	 * check the oldest requests which is still waiting on its epoch
1738*4882a593Smuzhiyun 	 * closing barrier ack. */
1739*4882a593Smuzhiyun 	if (!req_peer)
1740*4882a593Smuzhiyun 		req_peer = connection->req_not_net_done;
1741*4882a593Smuzhiyun 
1742*4882a593Smuzhiyun 	/* evaluate the oldest peer request only in one timer! */
1743*4882a593Smuzhiyun 	if (req_peer && req_peer->device != device)
1744*4882a593Smuzhiyun 		req_peer = NULL;
1745*4882a593Smuzhiyun 
1746*4882a593Smuzhiyun 	/* do we have something to evaluate? */
1747*4882a593Smuzhiyun 	if (req_peer == NULL && req_write == NULL && req_read == NULL)
1748*4882a593Smuzhiyun 		goto out;
1749*4882a593Smuzhiyun 
1750*4882a593Smuzhiyun 	oldest_submit_jif =
1751*4882a593Smuzhiyun 		(req_write && req_read)
1752*4882a593Smuzhiyun 		? ( time_before(req_write->pre_submit_jif, req_read->pre_submit_jif)
1753*4882a593Smuzhiyun 		  ? req_write->pre_submit_jif : req_read->pre_submit_jif )
1754*4882a593Smuzhiyun 		: req_write ? req_write->pre_submit_jif
1755*4882a593Smuzhiyun 		: req_read ? req_read->pre_submit_jif : now;
1756*4882a593Smuzhiyun 
1757*4882a593Smuzhiyun 	if (ent && req_peer && net_timeout_reached(req_peer, connection, now, ent, ko_count, timeout))
1758*4882a593Smuzhiyun 		_conn_request_state(connection, NS(conn, C_TIMEOUT), CS_VERBOSE | CS_HARD);
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun 	if (dt && oldest_submit_jif != now &&
1761*4882a593Smuzhiyun 		 time_after(now, oldest_submit_jif + dt) &&
1762*4882a593Smuzhiyun 		!time_in_range(now, device->last_reattach_jif, device->last_reattach_jif + dt)) {
1763*4882a593Smuzhiyun 		drbd_warn(device, "Local backing device failed to meet the disk-timeout\n");
1764*4882a593Smuzhiyun 		__drbd_chk_io_error(device, DRBD_FORCE_DETACH);
1765*4882a593Smuzhiyun 	}
1766*4882a593Smuzhiyun 
1767*4882a593Smuzhiyun 	/* Reschedule timer for the nearest not already expired timeout.
1768*4882a593Smuzhiyun 	 * Fallback to now + min(effective network timeout, disk timeout). */
1769*4882a593Smuzhiyun 	ent = (ent && req_peer && time_before(now, req_peer->pre_send_jif + ent))
1770*4882a593Smuzhiyun 		? req_peer->pre_send_jif + ent : now + et;
1771*4882a593Smuzhiyun 	dt = (dt && oldest_submit_jif != now && time_before(now, oldest_submit_jif + dt))
1772*4882a593Smuzhiyun 		? oldest_submit_jif + dt : now + et;
1773*4882a593Smuzhiyun 	nt = time_before(ent, dt) ? ent : dt;
1774*4882a593Smuzhiyun out:
1775*4882a593Smuzhiyun 	spin_unlock_irq(&device->resource->req_lock);
1776*4882a593Smuzhiyun 	mod_timer(&device->request_timer, nt);
1777*4882a593Smuzhiyun }
1778