1b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/*
2b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   drbd_worker.c
3b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
4b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
6b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
10b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   drbd is free software; you can redistribute it and/or modify
11b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   it under the terms of the GNU General Public License as published by
12b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   the Free Software Foundation; either version 2, or (at your option)
13b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   any later version.
14b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
15b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   drbd is distributed in the hope that it will be useful,
16b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   but WITHOUT ANY WARRANTY; without even the implied warranty of
17b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   GNU General Public License for more details.
19b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
20b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   You should have received a copy of the GNU General Public License
21b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   along with drbd; see the file COPYING.  If not, write to
22b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
2484b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher*/
25b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
26b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/module.h>
27b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/drbd.h>
28b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/sched.h>
29b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/wait.h>
30b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/mm.h>
31b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/memcontrol.h>
32b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/mm_inline.h>
33b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/slab.h>
34b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/random.h>
35b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/string.h>
36b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/scatterlist.h>
37b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
38b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include "drbd_int.h"
39a3603a6e3b4f2f0fb5529821134424e2eeec88fdAndreas Gruenbacher#include "drbd_protocol.h"
40b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include "drbd_req.h"
41b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
42d448a2e1e3d02f8f19111191d490b7e0a5eb70eaAndreas Gruenbacherstatic int make_ov_request(struct drbd_device *, int);
43d448a2e1e3d02f8f19111191d490b7e0a5eb70eaAndreas Gruenbacherstatic int make_resync_request(struct drbd_device *, int);
44b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
45c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher/* endio handlers:
46ed15b795092637f61c08fd21dc011b5334d7974cAndreas Gruenbacher *   drbd_md_endio (defined here)
47fcefa62e4c26e70c70b9e8252a4bc9b9031a4182Andreas Gruenbacher *   drbd_request_endio (defined here)
48fcefa62e4c26e70c70b9e8252a4bc9b9031a4182Andreas Gruenbacher *   drbd_peer_request_endio (defined here)
49ed15b795092637f61c08fd21dc011b5334d7974cAndreas Gruenbacher *   drbd_bm_endio (defined in drbd_bitmap.c)
50c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *
51b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * For all these callbacks, note the following:
52b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * The callbacks will be called in irq context by the IDE drivers,
53b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * and in Softirqs/Tasklets/BH context by the SCSI drivers.
54b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Try to get the locking right :)
55b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
56b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
57b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
58b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
59b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* About the global_state_lock
60b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   Each state transition on an device holds a read lock. In case we have
6195f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher   to evaluate the resync after dependencies, we grab a write lock, because
62b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   we need stable states on all devices for that.  */
63b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerrwlock_t global_state_lock;
64b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
65b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* used for synchronous meta data and bitmap IO
66b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * submitted by drbd_md_sync_page_io()
67b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
68ed15b795092637f61c08fd21dc011b5334d7974cAndreas Gruenbachervoid drbd_md_endio(struct bio *bio, int error)
69b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
70b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device;
71b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
72e37d2438d8e5e4c1225cf94d45347fa207835447Lars Ellenberg	device = bio->bi_private;
73e37d2438d8e5e4c1225cf94d45347fa207835447Lars Ellenberg	device->md_io.error = error;
74b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
750cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	/* We grabbed an extra reference in _drbd_md_sync_page_io() to be able
760cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * to timeout on the lower level device, and eventually detach from it.
770cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * If this io completion runs after that timeout expired, this
780cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * drbd_md_put_buffer() may allow us to finally try and re-attach.
790cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * During normal operation, this only puts that extra reference
800cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * down to 1 again.
810cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * Make sure we first drop the reference, and only then signal
820cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * completion, or we may (in drbd_al_read_log()) cycle so fast into the
830cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * next drbd_md_sync_page_io(), that we trigger the
84b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	 * ASSERT(atomic_read(&device->md_io_in_use) == 1) there.
850cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 */
86b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_md_put_buffer(device);
87e37d2438d8e5e4c1225cf94d45347fa207835447Lars Ellenberg	device->md_io.done = 1;
88b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	wake_up(&device->misc_wait);
89cdfda633d235028e9b27381dedb65416409e8729Philipp Reisner	bio_put(bio);
90b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->ldev) /* special case: drbd_md_read() during drbd_adm_attach() */
91b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		put_ldev(device);
92b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
93b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
94b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* reads on behalf of the partner,
95b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * "submitted" by the receiver
96b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
97a186e47856c7877fa684d75901436c1d35ec35e0Rashika Kheriastatic void drbd_endio_read_sec_final(struct drbd_peer_request *peer_req) __releases(local)
98b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
99b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags = 0;
1006780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_peer_device *peer_device = peer_req->peer_device;
1016780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_device *device = peer_device->device;
102b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1030500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_lock_irqsave(&device->resource->req_lock, flags);
104b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->read_cnt += peer_req->i.size >> 9;
105a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher	list_del(&peer_req->w.list);
106b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (list_empty(&device->read_ee))
107b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		wake_up(&device->ee_wait);
108db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (test_bit(__EE_WAS_ERROR, &peer_req->flags))
109b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		__drbd_chk_io_error(device, DRBD_READ_ERROR);
1100500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_unlock_irqrestore(&device->resource->req_lock, flags);
111b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1126780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	drbd_queue_work(&peer_device->connection->sender_work, &peer_req->w);
113b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
114b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
115b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
116b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* writes on behalf of the partner, or resync writes,
11745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg * "submitted" by the receiver, final stage.  */
118a0fb3c47a1aae5d38a88ea858f14d6d088d05e07Lars Ellenbergvoid drbd_endio_write_sec_final(struct drbd_peer_request *peer_req) __releases(local)
119b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
120b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags = 0;
1216780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_peer_device *peer_device = peer_req->peer_device;
1226780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_device *device = peer_device->device;
123181286ad22bf9bfb85de625e8501285de5261b35Lars Ellenberg	struct drbd_interval i;
124b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int do_wake;
125579b57ed730819970a3542b4bbcc2d4176f25c72Andreas Gruenbacher	u64 block_id;
126b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int do_al_complete_io;
127b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
128db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	/* after we moved peer_req to done_ee,
129b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * we may no longer access it,
130b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * it may be freed/reused already!
131b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * (as soon as we release the req_lock) */
132181286ad22bf9bfb85de625e8501285de5261b35Lars Ellenberg	i = peer_req->i;
133db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	do_al_complete_io = peer_req->flags & EE_CALL_AL_COMPLETE_IO;
134db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	block_id = peer_req->block_id;
13521ae5d7f95aa1a64f35b03c91f8714ced3ab61a9Lars Ellenberg	peer_req->flags &= ~EE_CALL_AL_COMPLETE_IO;
136b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1370500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_lock_irqsave(&device->resource->req_lock, flags);
138b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->writ_cnt += peer_req->i.size >> 9;
139a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher	list_move_tail(&peer_req->w.list, &device->done_ee);
140b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
141bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	/*
1425e4722645afb27ee749ea65988544450f08f78baAndreas Gruenbacher	 * Do not remove from the write_requests tree here: we did not send the
143bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * Ack yet and did not wake possibly waiting conflicting requests.
144bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * Removed from the tree from "drbd_process_done_ee" within the
14584b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	 * appropriate dw.cb (e_end_block/e_end_resync_block) or from
146bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * _drbd_clear_done_ee.
147bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 */
148b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
149b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	do_wake = list_empty(block_id == ID_SYNCER ? &device->sync_ee : &device->active_ee);
150b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
151a0fb3c47a1aae5d38a88ea858f14d6d088d05e07Lars Ellenberg	/* FIXME do we want to detach for failed REQ_DISCARD?
152a0fb3c47a1aae5d38a88ea858f14d6d088d05e07Lars Ellenberg	 * ((peer_req->flags & (EE_WAS_ERROR|EE_IS_TRIM)) == EE_WAS_ERROR) */
153a0fb3c47a1aae5d38a88ea858f14d6d088d05e07Lars Ellenberg	if (peer_req->flags & EE_WAS_ERROR)
154b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		__drbd_chk_io_error(device, DRBD_WRITE_ERROR);
1550500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_unlock_irqrestore(&device->resource->req_lock, flags);
156b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
157579b57ed730819970a3542b4bbcc2d4176f25c72Andreas Gruenbacher	if (block_id == ID_SYNCER)
158b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_complete_io(device, i.sector);
159b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
160b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (do_wake)
161b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		wake_up(&device->ee_wait);
162b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
163b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (do_al_complete_io)
164b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_al_complete_io(device, &i);
165b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1666780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	wake_asender(peer_device->connection);
167b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
16845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg}
169b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
17045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg/* writes on behalf of the partner, or resync writes,
17145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg * "submitted" by the receiver.
17245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg */
173fcefa62e4c26e70c70b9e8252a4bc9b9031a4182Andreas Gruenbachervoid drbd_peer_request_endio(struct bio *bio, int error)
17445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg{
175db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = bio->bi_private;
176a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher	struct drbd_device *device = peer_req->peer_device->device;
17745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	int uptodate = bio_flagged(bio, BIO_UPTODATE);
17845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	int is_write = bio_data_dir(bio) == WRITE;
179a0fb3c47a1aae5d38a88ea858f14d6d088d05e07Lars Ellenberg	int is_discard = !!(bio->bi_rw & REQ_DISCARD);
18045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
181071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg	if (error && __ratelimit(&drbd_ratelimit_state))
182d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher		drbd_warn(device, "%s: error=%d s=%llus\n",
183a0fb3c47a1aae5d38a88ea858f14d6d088d05e07Lars Ellenberg				is_write ? (is_discard ? "discard" : "write")
184a0fb3c47a1aae5d38a88ea858f14d6d088d05e07Lars Ellenberg					: "read", error,
185db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher				(unsigned long long)peer_req->i.sector);
18645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (!error && !uptodate) {
187071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg		if (__ratelimit(&drbd_ratelimit_state))
188d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher			drbd_warn(device, "%s: setting error to -EIO s=%llus\n",
189071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg					is_write ? "write" : "read",
190db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher					(unsigned long long)peer_req->i.sector);
19145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		/* strange behavior of some lower level drivers...
19245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		 * fail the request by clearing the uptodate flag,
19345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		 * but do not return any error?! */
19445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		error = -EIO;
19545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
19645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
19745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (error)
198db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		set_bit(__EE_WAS_ERROR, &peer_req->flags);
19945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
20045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	bio_put(bio); /* no need for the bio anymore */
201db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (atomic_dec_and_test(&peer_req->pending_bios)) {
20245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		if (is_write)
203db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			drbd_endio_write_sec_final(peer_req);
20445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		else
205db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			drbd_endio_read_sec_final(peer_req);
20645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
207b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
208b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
209b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* read, readA or write requests on R_PRIMARY coming from drbd_make_request
210b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
211fcefa62e4c26e70c70b9e8252a4bc9b9031a4182Andreas Gruenbachervoid drbd_request_endio(struct bio *bio, int error)
212b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
213a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	unsigned long flags;
214b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = bio->bi_private;
21584b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device *device = req->device;
216a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	struct bio_and_error m;
217b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	enum drbd_req_event what;
218b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int uptodate = bio_flagged(bio, BIO_UPTODATE);
219b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
220b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!error && !uptodate) {
221d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher		drbd_warn(device, "p %s: setting error to -EIO\n",
222b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 bio_data_dir(bio) == WRITE ? "write" : "read");
223b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* strange behavior of some lower level drivers...
224b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * fail the request by clearing the uptodate flag,
225b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * but do not return any error?! */
226b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		error = -EIO;
227b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
228b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
2291b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner
2301b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	/* If this request was aborted locally before,
2311b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * but now was completed "successfully",
2321b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * chances are that this caused arbitrary data corruption.
2331b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2341b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * "aborting" requests, or force-detaching the disk, is intended for
2351b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * completely blocked/hung local backing devices which do no longer
2361b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * complete requests at all, not even do error completions.  In this
2371b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * situation, usually a hard-reset and failover is the only way out.
2381b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2391b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * By "aborting", basically faking a local error-completion,
2401b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * we allow for a more graceful swichover by cleanly migrating services.
2411b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * Still the affected node has to be rebooted "soon".
2421b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2431b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * By completing these requests, we allow the upper layers to re-use
2441b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * the associated data pages.
2451b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2461b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * If later the local backing device "recovers", and now DMAs some data
2471b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * from disk into the original request pages, in the best case it will
2481b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * just put random data into unused pages; but typically it will corrupt
2491b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * meanwhile completely unrelated data, causing all sorts of damage.
2501b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2511b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * Which means delayed successful completion,
2521b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * especially for READ requests,
2531b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * is a reason to panic().
2541b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2551b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * We assume that a delayed *error* completion is OK,
2561b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * though we still will complain noisily about it.
2571b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 */
2581b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	if (unlikely(req->rq_state & RQ_LOCAL_ABORTED)) {
2591b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
260d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher			drbd_emerg(device, "delayed completion of aborted local request; disk-timeout may be too aggressive\n");
2611b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner
2621b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner		if (!error)
2631b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner			panic("possible random memory corruption caused by delayed completion of aborted local request\n");
2641b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	}
2651b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner
266b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* to avoid recursion in __req_mod */
267b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(error)) {
2682f632aeb5302da93f760d965e970600b35907026Lars Ellenberg		if (bio->bi_rw & REQ_DISCARD)
2692f632aeb5302da93f760d965e970600b35907026Lars Ellenberg			what = (error == -EOPNOTSUPP)
2702f632aeb5302da93f760d965e970600b35907026Lars Ellenberg				? DISCARD_COMPLETED_NOTSUPP
2712f632aeb5302da93f760d965e970600b35907026Lars Ellenberg				: DISCARD_COMPLETED_WITH_ERROR;
2722f632aeb5302da93f760d965e970600b35907026Lars Ellenberg		else
2732f632aeb5302da93f760d965e970600b35907026Lars Ellenberg			what = (bio_data_dir(bio) == WRITE)
2748554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			? WRITE_COMPLETED_WITH_ERROR
2755c3c7e64bbcb60db39d0d55c8fab39ef41d41c30Lars Ellenberg			: (bio_rw(bio) == READ)
2768554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			  ? READ_COMPLETED_WITH_ERROR
2778554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			  : READ_AHEAD_COMPLETED_WITH_ERROR;
278b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else
2798554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		what = COMPLETED_OK;
280b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
281b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	bio_put(req->private_bio);
282b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	req->private_bio = ERR_PTR(error);
283b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
284a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	/* not req_mod(), we need irqsave here! */
2850500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_lock_irqsave(&device->resource->req_lock, flags);
286a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	__req_mod(req, what, &m);
2870500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_unlock_irqrestore(&device->resource->req_lock, flags);
288b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
289a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg
290a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	if (m.bio)
291b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		complete_master_bio(device, &m);
292b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
293b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
29479a3c8d38cabd1a900340852e527b0a4ce8a459dAndreas Gruenbachervoid drbd_csum_ee(struct crypto_hash *tfm, struct drbd_peer_request *peer_req, void *digest)
29545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg{
29645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct hash_desc desc;
29745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct scatterlist sg;
298db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct page *page = peer_req->pages;
29945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct page *tmp;
30045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	unsigned len;
30145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
30245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	desc.tfm = tfm;
30345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	desc.flags = 0;
30445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
30545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	sg_init_table(&sg, 1);
30645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_init(&desc);
30745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
30845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	while ((tmp = page_chain_next(page))) {
30945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		/* all but the last page will be fully used */
31045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		sg_set_page(&sg, page, PAGE_SIZE, 0);
31145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		crypto_hash_update(&desc, &sg, sg.length);
31245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		page = tmp;
31345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
31445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	/* and now the last, possibly only partially used page */
315db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	len = peer_req->i.size & (PAGE_SIZE - 1);
31645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	sg_set_page(&sg, page, len ?: PAGE_SIZE, 0);
31745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_update(&desc, &sg, sg.length);
31845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_final(&desc, digest);
31945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg}
32045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
32179a3c8d38cabd1a900340852e527b0a4ce8a459dAndreas Gruenbachervoid drbd_csum_bio(struct crypto_hash *tfm, struct bio *bio, void *digest)
322b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
323b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct hash_desc desc;
324b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct scatterlist sg;
3257988613b0e5b2638caf6cd493cc78e9595eba19cKent Overstreet	struct bio_vec bvec;
3267988613b0e5b2638caf6cd493cc78e9595eba19cKent Overstreet	struct bvec_iter iter;
327b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
328b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	desc.tfm = tfm;
329b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	desc.flags = 0;
330b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
331b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sg_init_table(&sg, 1);
332b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	crypto_hash_init(&desc);
333b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
3347988613b0e5b2638caf6cd493cc78e9595eba19cKent Overstreet	bio_for_each_segment(bvec, bio, iter) {
3357988613b0e5b2638caf6cd493cc78e9595eba19cKent Overstreet		sg_set_page(&sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
336b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		crypto_hash_update(&desc, &sg, sg.length);
337b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
338b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	crypto_hash_final(&desc, digest);
339b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
340b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
3419676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg/* MAYBE merge common code with w_e_end_ov_req */
34299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherstatic int w_e_send_csum(struct drbd_work *w, int cancel)
343b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
344a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
3456780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_peer_device *peer_device = peer_req->peer_device;
3466780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_device *device = peer_device->device;
347b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
348b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
34999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err = 0;
350b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
35153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	if (unlikely(cancel))
35253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		goto out;
353b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
3549676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg	if (unlikely((peer_req->flags & EE_WAS_ERROR) != 0))
35553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		goto out;
356b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
3576780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	digest_size = crypto_hash_digestsize(peer_device->connection->csums_tfm);
35853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	digest = kmalloc(digest_size, GFP_NOIO);
35953ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	if (digest) {
360db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		sector_t sector = peer_req->i.sector;
361db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		unsigned int size = peer_req->i.size;
3626780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher		drbd_csum_ee(peer_device->connection->csums_tfm, peer_req, digest);
3639676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg		/* Free peer_req and pages before send.
36453ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * In case we block on congestion, we could otherwise run into
36553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * some distributed deadlock, if the other side blocks on
36653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * congestion as well, because our receiver blocks in
367c37c8ecfee685fa42de8fd418ad8ca1e66408bd8Andreas Gruenbacher		 * drbd_alloc_pages due to pp_in_use > max_buffers. */
368b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
369db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		peer_req = NULL;
370b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		inc_rs_pending(device);
3716780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher		err = drbd_send_drequest_csum(peer_device, sector, size,
372db1b0b724e56f34608b76197191ef0577a1ddd45Andreas Gruenbacher					      digest, digest_size,
373db1b0b724e56f34608b76197191ef0577a1ddd45Andreas Gruenbacher					      P_CSUM_RS_REQUEST);
37453ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		kfree(digest);
37553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	} else {
376d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher		drbd_err(device, "kmalloc() of digest failed.\n");
37799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		err = -ENOMEM;
37853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	}
379b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
38053ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenbergout:
381db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (peer_req)
382b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
383b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
38499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	if (unlikely(err))
385d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher		drbd_err(device, "drbd_send_drequest(..., csum) failed\n");
38699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
387b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
388b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
389b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#define GFP_TRY	(__GFP_HIGHMEM | __GFP_NOWARN)
390b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
39169a227731a378f34bc5a8192158bd94d1581ae3dAndreas Gruenbacherstatic int read_for_csum(struct drbd_peer_device *peer_device, sector_t sector, int size)
392b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
39369a227731a378f34bc5a8192158bd94d1581ae3dAndreas Gruenbacher	struct drbd_device *device = peer_device->device;
394db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req;
395b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
396b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (!get_ldev(device))
39780a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		return -EIO;
398b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
399b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* GFP_TRY, because if there is no memory available right now, this may
400b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * be rescheduled for later. It is "only" background resync, after all. */
40169a227731a378f34bc5a8192158bd94d1581ae3dAndreas Gruenbacher	peer_req = drbd_alloc_peer_req(peer_device, ID_SYNCER /* unused */, sector,
402a0fb3c47a1aae5d38a88ea858f14d6d088d05e07Lars Ellenberg				       size, true /* has real payload */, GFP_TRY);
403db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (!peer_req)
40480a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		goto defer;
405b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
406a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher	peer_req->w.cb = w_e_send_csum;
4070500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_lock_irq(&device->resource->req_lock);
408b9ed7080d7d29112c898c64bad778b84eec0ed2dLars Ellenberg	list_add_tail(&peer_req->w.list, &device->read_ee);
4090500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_unlock_irq(&device->resource->req_lock);
410b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
411b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	atomic_add(size >> 9, &device->rs_sect_ev);
412b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (drbd_submit_peer_request(device, peer_req, READ, DRBD_FAULT_RS_RD) == 0)
41380a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		return 0;
414b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
41510f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	/* If it failed because of ENOMEM, retry should help.  If it failed
41610f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * because bio_add_page failed (probably broken lower level driver),
41710f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * retry may or may not help.
41810f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * If it does not, you may need to force disconnect. */
4190500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_lock_irq(&device->resource->req_lock);
420a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher	list_del(&peer_req->w.list);
4210500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_unlock_irq(&device->resource->req_lock);
42222cc37a943832c948808884604ec6f5ff2594c1dLars Ellenberg
423b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_free_peer_req(device, peer_req);
42480a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenbergdefer:
425b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
42680a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg	return -EAGAIN;
427b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
428b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
42999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_resync_timer(struct drbd_work *w, int cancel)
430b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
43184b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device *device =
43284b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher		container_of(w, struct drbd_device, resync_work);
43384b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher
434b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	switch (device->state.conn) {
43563106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner	case C_VERIFY_S:
436d448a2e1e3d02f8f19111191d490b7e0a5eb70eaAndreas Gruenbacher		make_ov_request(device, cancel);
43763106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner		break;
43863106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner	case C_SYNC_TARGET:
439d448a2e1e3d02f8f19111191d490b7e0a5eb70eaAndreas Gruenbacher		make_resync_request(device, cancel);
44063106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner		break;
441b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
442b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
44399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
444794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner}
445794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner
446794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisnervoid resync_timer_fn(unsigned long data)
447794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner{
448b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = (struct drbd_device *) data;
449794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner
45015e26f6a3c6de2c665b4a30b9a70a902111f281fLars Ellenberg	drbd_queue_work_if_unqueued(
45115e26f6a3c6de2c665b4a30b9a70a902111f281fLars Ellenberg		&first_peer_device(device)->connection->sender_work,
45215e26f6a3c6de2c665b4a30b9a70a902111f281fLars Ellenberg		&device->resync_work);
453b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
454b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
455778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic void fifo_set(struct fifo_buffer *fb, int value)
456778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
457778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int i;
458778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
459778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	for (i = 0; i < fb->size; i++)
460f10f262349762c96ab247b6108af3a30b52b6f5aPhilipp Reisner		fb->values[i] = value;
461778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
462778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
463778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic int fifo_push(struct fifo_buffer *fb, int value)
464778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
465778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int ov;
466778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
467778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	ov = fb->values[fb->head_index];
468778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	fb->values[fb->head_index++] = value;
469778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
470778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (fb->head_index >= fb->size)
471778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fb->head_index = 0;
472778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
473778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	return ov;
474778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
475778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
476778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic void fifo_add_val(struct fifo_buffer *fb, int value)
477778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
478778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int i;
479778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
480778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	for (i = 0; i < fb->size; i++)
481778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fb->values[i] += value;
482778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
483778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
4849958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisnerstruct fifo_buffer *fifo_alloc(int fifo_size)
4859958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner{
4869958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	struct fifo_buffer *fb;
4879958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner
4888747d30af97232f9ff4cde78b8d259cc715a9b7aLars Ellenberg	fb = kzalloc(sizeof(struct fifo_buffer) + sizeof(int) * fifo_size, GFP_NOIO);
4899958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	if (!fb)
4909958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner		return NULL;
4919958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner
4929958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	fb->head_index = 0;
4939958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	fb->size = fifo_size;
4949958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	fb->total = 0;
4959958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner
4969958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	return fb;
4979958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner}
4989958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner
4990e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenbergstatic int drbd_rs_controller(struct drbd_device *device, unsigned int sect_in)
500778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
501daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner	struct disk_conf *dc;
5027f34f61490ee87a470cf229069d59b0987f42a59Lars Ellenberg	unsigned int want;     /* The number of sectors we want in-flight */
503778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int req_sect; /* Number of sectors to request in this turn */
5047f34f61490ee87a470cf229069d59b0987f42a59Lars Ellenberg	int correction; /* Number of sectors more we need in-flight */
505778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int cps; /* correction per invocation of drbd_rs_controller() */
506778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int steps; /* Number of time steps to plan ahead */
507778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int curr_corr;
508778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int max_sect;
509813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	struct fifo_buffer *plan;
510778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
511b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dc = rcu_dereference(device->ldev->disk_conf);
512b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	plan = rcu_dereference(device->rs_plan_s);
513778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
514813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	steps = plan->size; /* (dc->c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
515778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
516b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->rs_in_flight + sect_in == 0) { /* At start of resync */
517daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		want = ((dc->resync_rate * 2 * SLEEP_TIME) / HZ) * steps;
518778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	} else { /* normal path */
519daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		want = dc->c_fill_target ? dc->c_fill_target :
520daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner			sect_in * dc->c_delay_target * HZ / (SLEEP_TIME * 10);
521778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	}
522778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
523b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	correction = want - device->rs_in_flight - plan->total;
524778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
525778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/* Plan ahead */
526778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	cps = correction / steps;
527813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	fifo_add_val(plan, cps);
528813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	plan->total += cps * steps;
529778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
530778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/* What we do in this step */
531813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	curr_corr = fifo_push(plan, 0);
532813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	plan->total -= curr_corr;
533778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
534778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	req_sect = sect_in + curr_corr;
535778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (req_sect < 0)
536778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		req_sect = 0;
537778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
538daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner	max_sect = (dc->c_max_rate * 2 * SLEEP_TIME) / HZ;
539778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (req_sect > max_sect)
540778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		req_sect = max_sect;
541778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
542778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/*
543d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher	drbd_warn(device, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
544b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		 sect_in, device->rs_in_flight, want, correction,
545b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		 steps, cps, device->rs_planed, curr_corr, req_sect);
546778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	*/
547778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
548778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	return req_sect;
549778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
550778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
551b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic int drbd_rs_number_requests(struct drbd_device *device)
552e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg{
5530e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg	unsigned int sect_in;  /* Number of sectors that came in since the last turn */
5540e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg	int number, mxb;
5550e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg
5560e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg	sect_in = atomic_xchg(&device->rs_sect_in, 0);
5570e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg	device->rs_in_flight -= sect_in;
558813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner
559813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	rcu_read_lock();
5600e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg	mxb = drbd_get_max_buffers(device) / 2;
561b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (rcu_dereference(device->rs_plan_s)->size) {
5620e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg		number = drbd_rs_controller(device, sect_in) >> (BM_BLOCK_SHIFT - 9);
563b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
564e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	} else {
565b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->c_sync_rate = rcu_dereference(device->ldev->disk_conf)->resync_rate;
566b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		number = SLEEP_TIME * device->c_sync_rate  / ((BM_BLOCK_SIZE / 1024) * HZ);
567e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	}
568813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	rcu_read_unlock();
569e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg
5700e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg	/* Don't have more than "max-buffers"/2 in-flight.
5710e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg	 * Otherwise we may cause the remote site to stall on drbd_alloc_pages(),
5720e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg	 * potentially causing a distributed deadlock on congestion during
5730e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg	 * online-verify or (checksum-based) resync, if max-buffers,
5740e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg	 * socket buffer sizes and resync rate settings are mis-configured. */
5757f34f61490ee87a470cf229069d59b0987f42a59Lars Ellenberg
5767f34f61490ee87a470cf229069d59b0987f42a59Lars Ellenberg	/* note that "number" is in units of "BM_BLOCK_SIZE" (which is 4k),
5777f34f61490ee87a470cf229069d59b0987f42a59Lars Ellenberg	 * mxb (as used here, and in drbd_alloc_pages on the peer) is
5787f34f61490ee87a470cf229069d59b0987f42a59Lars Ellenberg	 * "number of pages" (typically also 4k),
5797f34f61490ee87a470cf229069d59b0987f42a59Lars Ellenberg	 * but "rs_in_flight" is in "sectors" (512 Byte). */
5807f34f61490ee87a470cf229069d59b0987f42a59Lars Ellenberg	if (mxb - device->rs_in_flight/8 < number)
5817f34f61490ee87a470cf229069d59b0987f42a59Lars Ellenberg		number = mxb - device->rs_in_flight/8;
5820e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg
583e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	return number;
584e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg}
585e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg
58644a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenbergstatic int make_resync_request(struct drbd_device *const device, int cancel)
587b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
58844a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	struct drbd_peer_device *const peer_device = first_peer_device(device);
58944a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL;
590b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long bit;
591b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t sector;
592b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	const sector_t capacity = drbd_get_capacity(device->this_bdev);
5931816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg	int max_bio_size;
594e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	int number, rollback_i, size;
595506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg	int align, requeue = 0;
5960f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg	int i = 0;
597b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
598b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
59999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
600b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
601b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->rs_total == 0) {
602af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg		/* empty resync? */
603b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_resync_finished(device);
60499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
605af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg	}
606af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
607b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (!get_ldev(device)) {
608b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		/* Since we only need to access device->rsync a
609b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		   get_ldev_if_state(device,D_FAILED) would be sufficient, but
610b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   to continue resync with a broken disk makes no sense at
611b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   all */
612d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher		drbd_err(device, "Disk broke down during resync!\n");
61399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
614b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
615b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
616b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	max_bio_size = queue_max_hw_sectors(device->rq_queue) << 9;
617b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	number = drbd_rs_number_requests(device);
6180e49d7b014c5d591a053d08888a455bd74a88646Lars Ellenberg	if (number <= 0)
6190f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg		goto requeue;
620b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
621b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < number; i++) {
622506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg		/* Stop generating RS requests when half of the send buffer is filled,
623506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg		 * but notify TCP that we'd like to have more space. */
62444a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg		mutex_lock(&connection->data.mutex);
62544a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg		if (connection->data.socket) {
626506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg			struct sock *sk = connection->data.socket->sk;
627506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg			int queued = sk->sk_wmem_queued;
628506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg			int sndbuf = sk->sk_sndbuf;
629506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg			if (queued > sndbuf / 2) {
630506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg				requeue = 1;
631506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg				if (sk->sk_socket)
632506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg					set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
633506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg			}
634506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg		} else
635506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg			requeue = 1;
63644a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg		mutex_unlock(&connection->data.mutex);
637506afb6248af577eb702c73f3da52a12f4c56a38Lars Ellenberg		if (requeue)
638b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
639b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
640b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnernext_sector:
641b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		size = BM_BLOCK_SIZE;
642b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		bit  = drbd_bm_find_next(device, device->bm_resync_fo);
643b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
6444b0715f09655e76ca24c35a9e25e7c464c2f7346Lars Ellenberg		if (bit == DRBD_END_OF_BITMAP) {
645b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->bm_resync_fo = drbd_bm_bits(device);
646b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			put_ldev(device);
64799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher			return 0;
648b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
649b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
650b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sector = BM_BIT_TO_SECT(bit);
651b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
652ad3fee790088d36ad862e31535b5b99c25adeef4Lars Ellenberg		if (drbd_try_rs_begin_io(device, sector)) {
653b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->bm_resync_fo = bit;
654b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
655b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
656b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->bm_resync_fo = bit + 1;
657b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
658b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		if (unlikely(drbd_bm_test_bit(device, bit) == 0)) {
659b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			drbd_rs_complete_io(device, sector);
660b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto next_sector;
661b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
662b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
6631816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg#if DRBD_MAX_BIO_SIZE > BM_BLOCK_SIZE
664b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* try to find some adjacent bits.
665b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * we stop if we have already the maximum req size.
666b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 *
667b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * Additionally always align bigger requests, in order to
668b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * be prepared for all stripe sizes of software RAIDs.
669b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 */
670b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		align = 1;
671d207450cf2731c6a2afa8c78fb31c7206cd35ebaPhilipp Reisner		rollback_i = i;
6726377b9235056452cd5d592c3739baa379a8735feLars Ellenberg		while (i < number) {
6731816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg			if (size + BM_BLOCK_SIZE > max_bio_size)
674b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
675b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
676b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* Be always aligned */
677b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (sector & ((1<<(align+3))-1))
678b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
679b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
680b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* do not cross extent boundaries */
681b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
682b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
683b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* now, is it actually dirty, after all?
684b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * caution, drbd_bm_test_bit is tri-state for some
685b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * obscure reason; ( b == 0 ) would get the out-of-band
686b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * only accidentally right because of the "oddly sized"
687b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * adjustment below */
688b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			if (drbd_bm_test_bit(device, bit+1) != 1)
689b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
690b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			bit++;
691b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size += BM_BLOCK_SIZE;
692b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if ((BM_BLOCK_SIZE << align) <= size)
693b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				align++;
694b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			i++;
695b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
696b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* if we merged some,
697b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * reset the offset to start the next drbd_bm_find_next from */
698b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (size > BM_BLOCK_SIZE)
699b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->bm_resync_fo = bit + 1;
700b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#endif
701b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
702b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* adjust very last sectors, in case we are oddly sized */
703b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector + (size>>9) > capacity)
704b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size = (capacity-sector)<<9;
705aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg
706aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg		if (device->use_csums) {
70744a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg			switch (read_for_csum(peer_device, sector, size)) {
70880a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case -EIO: /* Disk failure */
709b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				put_ldev(device);
71099920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher				return -EIO;
71180a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case -EAGAIN: /* allocation failed, or ldev busy */
712b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				drbd_rs_complete_io(device, sector);
713b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				device->bm_resync_fo = BM_SECT_TO_BIT(sector);
714d207450cf2731c6a2afa8c78fb31c7206cd35ebaPhilipp Reisner				i = rollback_i;
715b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				goto requeue;
71680a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case 0:
71780a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				/* everything ok */
71880a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				break;
71980a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			default:
72080a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				BUG();
721b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
722b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
72399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher			int err;
72499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher
725b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			inc_rs_pending(device);
72644a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg			err = drbd_send_drequest(peer_device, P_RS_DATA_REQUEST,
72799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher						 sector, size, ID_SYNCER);
72899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher			if (err) {
729d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher				drbd_err(device, "drbd_send_drequest() failed, aborting...\n");
730b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				dec_rs_pending(device);
731b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				put_ldev(device);
73299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher				return err;
733b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
734b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
735b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
736b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
737b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->bm_resync_fo >= drbd_bm_bits(device)) {
738b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* last syncer _request_ was sent,
739b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * but the P_RS_DATA_REPLY not yet received.  sync will end (and
740b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * next sync group will resume), as soon as we receive the last
741b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * resync data block, and the last bit is cleared.
742b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * until then resync "work" is "inactive" ...
743b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 */
744b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		put_ldev(device);
74599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
746b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
747b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
748b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner requeue:
749b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
750b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	mod_timer(&device->resync_timer, jiffies + SLEEP_TIME);
751b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
75299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
753b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
754b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
755d448a2e1e3d02f8f19111191d490b7e0a5eb70eaAndreas Gruenbacherstatic int make_ov_request(struct drbd_device *device, int cancel)
756b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
757b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int number, i, size;
758b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t sector;
759b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	const sector_t capacity = drbd_get_capacity(device->this_bdev);
76058ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	bool stop_sector_reached = false;
761b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
762b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
763b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
764b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
765b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	number = drbd_rs_number_requests(device);
766b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
767b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	sector = device->ov_position;
768b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < number; i++) {
76958ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		if (sector >= capacity)
770b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
77158ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg
77258ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		/* We check for "finished" only in the reply path:
77358ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		 * w_e_end_ov_reply().
77458ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		 * We need to send at least one request out. */
77558ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		stop_sector_reached = i > 0
776b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			&& verify_can_do_stop_sector(device)
777b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			&& sector >= device->ov_stop_sector;
77858ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		if (stop_sector_reached)
77958ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg			break;
780b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
781b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		size = BM_BLOCK_SIZE;
782b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
783ad3fee790088d36ad862e31535b5b99c25adeef4Lars Ellenberg		if (drbd_try_rs_begin_io(device, sector)) {
784b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->ov_position = sector;
785b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
786b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
787b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
788b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector + (size>>9) > capacity)
789b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size = (capacity-sector)<<9;
790b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
791b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		inc_rs_pending(device);
79269a227731a378f34bc5a8192158bd94d1581ae3dAndreas Gruenbacher		if (drbd_send_ov_request(first_peer_device(device), sector, size)) {
793b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			dec_rs_pending(device);
794b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 0;
795b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
796b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sector += BM_SECT_PER_BIT;
797b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
798b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->ov_position = sector;
799b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
800b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner requeue:
801b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
80258ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	if (i == 0 || !stop_sector_reached)
803b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		mod_timer(&device->resync_timer, jiffies + SLEEP_TIME);
804b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
805b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
806b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
80799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_ov_finished(struct drbd_work *w, int cancel)
808b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
80984b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device_work *dw =
81084b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher		container_of(w, struct drbd_device_work, w);
81184b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device *device = dw->device;
81284b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	kfree(dw);
813b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	ov_out_of_sync_print(device);
814b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_resync_finished(device);
815b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
81699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
817b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
818b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
81999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherstatic int w_resync_finished(struct drbd_work *w, int cancel)
820b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
82184b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device_work *dw =
82284b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher		container_of(w, struct drbd_device_work, w);
82384b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device *device = dw->device;
82484b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	kfree(dw);
825b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
826b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_resync_finished(device);
827b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
82899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
829b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
830b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
831b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic void ping_peer(struct drbd_device *device)
832af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg{
833a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	struct drbd_connection *connection = first_peer_device(device)->connection;
8342a67d8b93b3363d4a5608d16d510a4bf6b3863fbPhilipp Reisner
835bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	clear_bit(GOT_PING_ACK, &connection->flags);
836bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	request_ping(connection);
837bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	wait_event(connection->ping_wait,
838bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		   test_bit(GOT_PING_ACK, &connection->flags) || device->state.conn < C_CONNECTED);
839af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg}
840af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
841b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherint drbd_resync_finished(struct drbd_device *device)
842b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
843b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long db, dt, dbdt;
844b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long n_oos;
845b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	union drbd_state os, ns;
84684b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device_work *dw;
847b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	char *khelper_cmd = NULL;
84826525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	int verify_done = 0;
849b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
850b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* Remove all elements from the resync LRU. Since future actions
851b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * might set bits in the (main) bitmap, then the entries in the
852b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * resync LRU would be wrong. */
853b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (drbd_rs_del_all(device)) {
854b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* In case this is not possible now, most probably because
855b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * there are P_RS_DATA_REPLY Packets lingering on the worker's
856b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * queue (or even the read operations for those packets
857b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * is not finished by now).   Retry in 100ms. */
858b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
85920ee639024e3d33111df0e343050b218c656bf16Philipp Reisner		schedule_timeout_interruptible(HZ / 10);
86084b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher		dw = kmalloc(sizeof(struct drbd_device_work), GFP_ATOMIC);
86184b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher		if (dw) {
86284b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher			dw->w.cb = w_resync_finished;
86384b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher			dw->device = device;
86484b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher			drbd_queue_work(&first_peer_device(device)->connection->sender_work,
86584b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher					&dw->w);
866b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
867b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
86884b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher		drbd_err(device, "Warn failed to drbd_rs_del_all() and to kmalloc(dw).\n");
869b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
870b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
871b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dt = (jiffies - device->rs_start - device->rs_paused) / HZ;
872b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (dt <= 0)
873b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dt = 1;
87484b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher
875b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	db = device->rs_total;
87658ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	/* adjust for verify start and stop sectors, respective reached position */
877b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->state.conn == C_VERIFY_S || device->state.conn == C_VERIFY_T)
878b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		db -= device->ov_left;
87958ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg
880b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dbdt = Bit2KB(db/dt);
881b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_paused /= HZ;
882b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
883b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (!get_ldev(device))
884b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out;
885b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
886b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	ping_peer(device);
887af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
8880500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_lock_irq(&device->resource->req_lock);
889b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	os = drbd_read_state(device);
890b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
89126525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	verify_done = (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T);
89226525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg
893b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* This protects us against multiple calls (that can happen in the presence
894b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	   of application IO), and against connectivity loss just before we arrive here. */
895b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (os.conn <= C_CONNECTED)
896b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out_unlock;
897b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
898b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns = os;
899b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.conn = C_CONNECTED;
900b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
901d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher	drbd_info(device, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
90258ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	     verify_done ? "Online verify" : "Resync",
903b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	     dt + device->rs_paused, device->rs_paused, dbdt);
904b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
905b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	n_oos = drbd_bm_total_weight(device);
906b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
907b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
908b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (n_oos) {
909d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher			drbd_alert(device, "Online verify found %lu %dk block out of sync!\n",
910b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			      n_oos, Bit2KB(1));
911b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			khelper_cmd = "out-of-sync";
912b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
913b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
9140b0ba1efc7b887bc2bd767ef822979fe2dae620eAndreas Gruenbacher		D_ASSERT(device, (n_oos - device->rs_failed) == 0);
915b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
916b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
917b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			khelper_cmd = "after-resync-target";
918b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
919aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg		if (device->use_csums && device->rs_total) {
920b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			const unsigned long s = device->rs_same_csum;
921b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			const unsigned long t = device->rs_total;
922b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			const int ratio =
923b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				(t == 0)     ? 0 :
924b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			(t < 100000) ? ((s*100)/t) : (s/(t/100));
925d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher			drbd_info(device, "%u %% had equal checksums, eliminated: %luK; "
926b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     "transferred %luK total %luK\n",
927b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     ratio,
928b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			     Bit2KB(device->rs_same_csum),
929b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			     Bit2KB(device->rs_total - device->rs_same_csum),
930b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			     Bit2KB(device->rs_total));
931b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
932b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
933b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
934b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->rs_failed) {
935d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher		drbd_info(device, "            %lu failed blocks\n", device->rs_failed);
936b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
937b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
938b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.disk = D_INCONSISTENT;
939b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.pdsk = D_UP_TO_DATE;
940b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
941b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.disk = D_UP_TO_DATE;
942b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.pdsk = D_INCONSISTENT;
943b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
944b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
945b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.disk = D_UP_TO_DATE;
946b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.pdsk = D_UP_TO_DATE;
947b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
948b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
949b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			if (device->p_uuid) {
950b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				int i;
951b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
952b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher					_drbd_uuid_set(device, i, device->p_uuid[i]);
953b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				drbd_uuid_set(device, UI_BITMAP, device->ldev->md.uuid[UI_CURRENT]);
954b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				_drbd_uuid_set(device, UI_CURRENT, device->p_uuid[UI_CURRENT]);
955b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			} else {
956d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher				drbd_err(device, "device->p_uuid is NULL! BUG\n");
957b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
958b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
959b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
96062b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg		if (!(os.conn == C_VERIFY_S || os.conn == C_VERIFY_T)) {
96162b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			/* for verify runs, we don't update uuids here,
96262b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			 * so there would be nothing to report. */
963b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			drbd_uuid_set_bm(device, 0UL);
964b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			drbd_print_uuids(device, "updated UUIDs");
965b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			if (device->p_uuid) {
96662b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				/* Now the two UUID sets are equal, update what we
96762b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				 * know of the peer. */
96862b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				int i;
96962b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				for (i = UI_CURRENT ; i <= UI_HISTORY_END ; i++)
970b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher					device->p_uuid[i] = device->ldev->md.uuid[i];
97162b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			}
972b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
973b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
974b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
975b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	_drbd_set_state(device, ns, CS_VERBOSE, NULL);
976b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout_unlock:
9770500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher	spin_unlock_irq(&device->resource->req_lock);
978b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
979b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout:
980b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_total  = 0;
981b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_failed = 0;
982b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_paused = 0;
98358ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg
98458ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	/* reset start sector, if we reached end of device */
985b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (verify_done && device->ov_left == 0)
986b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->ov_start_sector = 0;
987b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
988b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_md_sync(device);
98913d42685bec1f012dcbc5d187490eb1d15ec8219Lars Ellenberg
990b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (khelper_cmd)
991b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_khelper(device, khelper_cmd);
992b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
993b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
994b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
995b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
996b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* helper */
997b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic void move_to_net_ee_or_free(struct drbd_device *device, struct drbd_peer_request *peer_req)
998b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
999045417f75c718a4ac97fd44106b8aafcbca5a6daAndreas Gruenbacher	if (drbd_peer_req_has_active_page(peer_req)) {
1000b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* This might happen if sendpage() has not finished */
1001db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		int i = (peer_req->i.size + PAGE_SIZE -1) >> PAGE_SHIFT;
1002b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		atomic_add(i, &device->pp_in_use_by_net);
1003b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		atomic_sub(i, &device->pp_in_use);
10040500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher		spin_lock_irq(&device->resource->req_lock);
1005a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher		list_add_tail(&peer_req->w.list, &device->net_ee);
10060500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher		spin_unlock_irq(&device->resource->req_lock);
1007435f07402b3165b90592073bc0f8c6f8fa160ff9Lars Ellenberg		wake_up(&drbd_pp_wait);
1008b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else
1009b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
1010b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1011b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1012b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1013b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
1014b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher * @device:	DRBD device.
1015b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1016b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1017b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
101899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_e_end_data_req(struct drbd_work *w, int cancel)
1019b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1020a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
10216780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_peer_device *peer_device = peer_req->peer_device;
10226780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_device *device = peer_device->device;
102399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err;
1024b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1025b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1026b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
1027b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		dec_unacked(device);
102899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1029b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1030b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1031db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
10326780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher		err = drbd_send_block(peer_device, P_DATA_REPLY, peer_req);
1033b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1034b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
1035d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher			drbd_err(device, "Sending NegDReply. sector=%llus.\n",
1036db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			    (unsigned long long)peer_req->i.sector);
1037b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
10386780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher		err = drbd_send_ack(peer_device, P_NEG_DREPLY, peer_req);
1039b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1040b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1041b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dec_unacked(device);
1042b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1043b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	move_to_net_ee_or_free(device, peer_req);
1044b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
104599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	if (unlikely(err))
1046d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher		drbd_err(device, "drbd_send_block() failed\n");
104799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1048b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1049b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1050b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1051a209b4aec31d4b672b7a70f5de272ebf6ce40e1bAndreas Gruenbacher * w_e_end_rsdata_req() - Worker callback to send a P_RS_DATA_REPLY packet in response to a P_RS_DATA_REQUEST
1052b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1053b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1054b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
105599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_e_end_rsdata_req(struct drbd_work *w, int cancel)
1056b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1057a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
10586780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_peer_device *peer_device = peer_req->peer_device;
10596780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_device *device = peer_device->device;
106099920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err;
1061b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1062b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1063b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
1064b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		dec_unacked(device);
106599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1066b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1067b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1068b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (get_ldev_if_state(device, D_FAILED)) {
1069b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_complete_io(device, peer_req->i.sector);
1070b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		put_ldev(device);
1071b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1072b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1073b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->state.conn == C_AHEAD) {
10746780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher		err = drbd_send_ack(peer_device, P_RS_CANCEL, peer_req);
1075db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	} else if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1076b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		if (likely(device->state.pdsk >= D_INCONSISTENT)) {
1077b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			inc_rs_pending(device);
10786780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher			err = drbd_send_block(peer_device, P_RS_DATA_REPLY, peer_req);
1079b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
1080b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (__ratelimit(&drbd_ratelimit_state))
1081d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher				drbd_err(device, "Not sending RSDataReply, "
1082b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				    "partner DISKLESS!\n");
108399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher			err = 0;
1084b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1085b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1086b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
1087d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher			drbd_err(device, "Sending NegRSDReply. sector %llus.\n",
1088db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			    (unsigned long long)peer_req->i.sector);
1089b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
10906780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher		err = drbd_send_ack(peer_device, P_NEG_RS_DREPLY, peer_req);
1091b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1092b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* update resync data with failure */
1093b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_failed_io(device, peer_req->i.sector, peer_req->i.size);
1094b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1095b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1096b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dec_unacked(device);
1097b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1098b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	move_to_net_ee_or_free(device, peer_req);
1099b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
110099920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	if (unlikely(err))
1101d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher		drbd_err(device, "drbd_send_block() failed\n");
110299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1103b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1104b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
110599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_e_end_csum_rs_req(struct drbd_work *w, int cancel)
1106b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1107a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
11086780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_peer_device *peer_device = peer_req->peer_device;
11096780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_device *device = peer_device->device;
1110b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct digest_info *di;
1111b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
1112b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest = NULL;
111399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err, eq = 0;
1114b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1115b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1116b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
1117b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		dec_unacked(device);
111899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1119b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1120b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1121b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (get_ldev(device)) {
1122b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_complete_io(device, peer_req->i.sector);
1123b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		put_ldev(device);
11241d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	}
1125b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1126db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	di = peer_req->digest;
1127b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1128db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1129b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* quick hack to try to avoid a race against reconfiguration.
1130b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * a real fix would be much more involved,
1131b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * introducing more locking mechanisms */
11326780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher		if (peer_device->connection->csums_tfm) {
11336780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher			digest_size = crypto_hash_digestsize(peer_device->connection->csums_tfm);
11340b0ba1efc7b887bc2bd767ef822979fe2dae620eAndreas Gruenbacher			D_ASSERT(device, digest_size == di->digest_size);
1135b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			digest = kmalloc(digest_size, GFP_NOIO);
1136b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1137b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (digest) {
11386780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher			drbd_csum_ee(peer_device->connection->csums_tfm, peer_req, digest);
1139b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			eq = !memcmp(digest, di->digest, digest_size);
1140b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			kfree(digest);
1141b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1142b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1143b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (eq) {
1144b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			drbd_set_in_sync(device, peer_req->i.sector, peer_req->i.size);
1145676396d545350a70d922605ec23c2ed26124334aLars Ellenberg			/* rs_same_csums unit is BM_BLOCK_SIZE */
1146b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->rs_same_csum += peer_req->i.size >> BM_BLOCK_SHIFT;
11476780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher			err = drbd_send_ack(peer_device, P_RS_IS_IN_SYNC, peer_req);
1148b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
1149b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			inc_rs_pending(device);
1150db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			peer_req->block_id = ID_SYNCER; /* By setting block_id, digest pointer becomes invalid! */
1151db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			peer_req->flags &= ~EE_HAS_DIGEST; /* This peer request no longer has a digest pointer */
1152204bba9965c4cc175bf5bc65ddd19889e9085c72Philipp Reisner			kfree(di);
11536780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher			err = drbd_send_block(peer_device, P_RS_DATA_REPLY, peer_req);
1154b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1155b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
11566780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher		err = drbd_send_ack(peer_device, P_NEG_RS_DREPLY, peer_req);
1157b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
1158d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher			drbd_err(device, "Sending NegDReply. I guess it gets messy.\n");
1159b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1160b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1161b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dec_unacked(device);
1162b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	move_to_net_ee_or_free(device, peer_req);
1163b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
116499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	if (unlikely(err))
1165d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher		drbd_err(device, "drbd_send_block/ack() failed\n");
116699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1167b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1168b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
116999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_e_end_ov_req(struct drbd_work *w, int cancel)
1170b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1171a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
11726780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_peer_device *peer_device = peer_req->peer_device;
11736780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_device *device = peer_device->device;
1174db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	sector_t sector = peer_req->i.sector;
1175db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	unsigned int size = peer_req->i.size;
1176b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
1177b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
117899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err = 0;
1179b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1180b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
1181b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out;
1182b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
11836780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	digest_size = crypto_hash_digestsize(peer_device->connection->verify_tfm);
1184b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	digest = kmalloc(digest_size, GFP_NOIO);
11858f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	if (!digest) {
118699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		err = 1;	/* terminate the connection in case the allocation failed */
11878f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		goto out;
1188b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1189b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1190db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely(!(peer_req->flags & EE_WAS_ERROR)))
11916780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher		drbd_csum_ee(peer_device->connection->verify_tfm, peer_req, digest);
11928f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	else
11938f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		memset(digest, 0, digest_size);
11948f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner
119553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	/* Free e and pages before send.
119653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * In case we block on congestion, we could otherwise run into
119753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * some distributed deadlock, if the other side blocks on
119853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * congestion as well, because our receiver blocks in
1199c37c8ecfee685fa42de8fd418ad8ca1e66408bd8Andreas Gruenbacher	 * drbd_alloc_pages due to pp_in_use > max_buffers. */
1200b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_free_peer_req(device, peer_req);
1201db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	peer_req = NULL;
1202b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	inc_rs_pending(device);
12036780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	err = drbd_send_drequest_csum(peer_device, sector, size, digest, digest_size, P_OV_REPLY);
120499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	if (err)
1205b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		dec_rs_pending(device);
12068f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	kfree(digest);
12078f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner
1208b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout:
1209db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (peer_req)
1210b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
1211b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dec_unacked(device);
121299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1213b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1214b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1215b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid drbd_ov_out_of_sync_found(struct drbd_device *device, sector_t sector, int size)
1216b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1217b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->ov_last_oos_start + device->ov_last_oos_size == sector) {
1218b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->ov_last_oos_size += size>>9;
1219b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1220b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->ov_last_oos_start = sector;
1221b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->ov_last_oos_size = size>>9;
1222b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1223b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_set_out_of_sync(device, sector, size);
1224b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1225b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
122699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_e_end_ov_reply(struct drbd_work *w, int cancel)
1227b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1228a8cd15ba7919eaf1f416857f983a502cc261af26Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
12296780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_peer_device *peer_device = peer_req->peer_device;
12306780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	struct drbd_device *device = peer_device->device;
1231b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct digest_info *di;
1232b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
1233db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	sector_t sector = peer_req->i.sector;
1234db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	unsigned int size = peer_req->i.size;
123553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	int digest_size;
123699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err, eq = 0;
123758ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	bool stop_sector_reached = false;
1238b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1239b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1240b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
1241b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		dec_unacked(device);
124299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1243b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1244b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1245b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1246b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * the resync lru has been cleaned up already */
1247b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (get_ldev(device)) {
1248b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_complete_io(device, peer_req->i.sector);
1249b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		put_ldev(device);
12501d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	}
1251b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1252db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	di = peer_req->digest;
1253b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1254db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
12556780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher		digest_size = crypto_hash_digestsize(peer_device->connection->verify_tfm);
1256b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		digest = kmalloc(digest_size, GFP_NOIO);
1257b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (digest) {
12586780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher			drbd_csum_ee(peer_device->connection->verify_tfm, peer_req, digest);
1259b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
12600b0ba1efc7b887bc2bd767ef822979fe2dae620eAndreas Gruenbacher			D_ASSERT(device, digest_size == di->digest_size);
1261b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			eq = !memcmp(digest, di->digest, digest_size);
1262b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			kfree(digest);
1263b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1264b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1265b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
12669676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg	/* Free peer_req and pages before send.
12679676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg	 * In case we block on congestion, we could otherwise run into
12689676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg	 * some distributed deadlock, if the other side blocks on
12699676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg	 * congestion as well, because our receiver blocks in
1270c37c8ecfee685fa42de8fd418ad8ca1e66408bd8Andreas Gruenbacher	 * drbd_alloc_pages due to pp_in_use > max_buffers. */
1271b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_free_peer_req(device, peer_req);
1272b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!eq)
1273b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_ov_out_of_sync_found(device, sector, size);
1274b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	else
1275b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		ov_out_of_sync_print(device);
1276b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
12776780139c0ab96fc9c605bea33db30fc9378016b7Andreas Gruenbacher	err = drbd_send_ack_ex(peer_device, P_OV_RESULT, sector, size,
1278fa79abd893f21f458c74af8bca015aa2ef7486a5Andreas Gruenbacher			       eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
1279b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1280b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dec_unacked(device);
1281b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1282b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	--device->ov_left;
1283ea5442aff68c559c951373739201721185191748Lars Ellenberg
1284ea5442aff68c559c951373739201721185191748Lars Ellenberg	/* let's advance progress step marks only for every other megabyte */
1285b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if ((device->ov_left & 0x200) == 0x200)
1286b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_advance_rs_marks(device, device->ov_left);
1287ea5442aff68c559c951373739201721185191748Lars Ellenberg
1288b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	stop_sector_reached = verify_can_do_stop_sector(device) &&
1289b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		(sector + (size>>9)) >= device->ov_stop_sector;
129058ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg
1291b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->ov_left == 0 || stop_sector_reached) {
1292b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		ov_out_of_sync_print(device);
1293b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_resync_finished(device);
1294b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1295b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
129699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1297b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1298b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1299b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg/* FIXME
1300b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg * We need to track the number of pending barrier acks,
1301b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg * and to be able to wait for them.
1302b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg * See also comment in drbd_adm_attach before drbd_suspend_io.
1303b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg */
1304bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacherstatic int drbd_send_barrier(struct drbd_connection *connection)
1305b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
13069f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher	struct p_barrier *p;
1307b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	struct drbd_socket *sock;
1308b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1309bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	sock = &connection->data;
1310bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	p = conn_prepare_command(connection, sock);
13119f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher	if (!p)
13129f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher		return -EIO;
1313bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	p->barrier = connection->send.current_epoch_nr;
1314b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	p->pad = 0;
1315bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	connection->send.current_epoch_writes = 0;
1316b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1317bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	return conn_send_command(connection, sock, P_BARRIER, sizeof(*p), NULL, 0);
1318b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1319b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
132099920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_send_write_hint(struct drbd_work *w, int cancel)
1321b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
132284b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device *device =
132384b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher		container_of(w, struct drbd_device, unplug_work);
13249f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher	struct drbd_socket *sock;
13259f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher
1326b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (cancel)
132799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1328a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	sock = &first_peer_device(device)->connection->data;
132969a227731a378f34bc5a8192158bd94d1581ae3dAndreas Gruenbacher	if (!drbd_prepare_command(first_peer_device(device), sock))
13309f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher		return -EIO;
133169a227731a378f34bc5a8192158bd94d1581ae3dAndreas Gruenbacher	return drbd_send_command(first_peer_device(device), sock, P_UNPLUG_REMOTE, 0, NULL, 0);
1332b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1333b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1334bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacherstatic void re_init_if_first_write(struct drbd_connection *connection, unsigned int epoch)
13354eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg{
1336bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	if (!connection->send.seen_any_write_yet) {
1337bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		connection->send.seen_any_write_yet = true;
1338bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		connection->send.current_epoch_nr = epoch;
1339bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		connection->send.current_epoch_writes = 0;
13404eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg	}
13414eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg}
13424eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg
1343bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacherstatic void maybe_send_barrier(struct drbd_connection *connection, unsigned int epoch)
13444eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg{
13454eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg	/* re-init if first write on this connection */
1346bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	if (!connection->send.seen_any_write_yet)
13474eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg		return;
1348bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	if (connection->send.current_epoch_nr != epoch) {
1349bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		if (connection->send.current_epoch_writes)
1350bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher			drbd_send_barrier(connection);
1351bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		connection->send.current_epoch_nr = epoch;
13524eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg	}
13534eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg}
13544eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg
13558f7bed77740c7418074e6ba82c646a7dd035e6cfAndreas Gruenbacherint w_send_out_of_sync(struct drbd_work *w, int cancel)
135673a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner{
135773a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
135884b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device *device = req->device;
135944a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	struct drbd_peer_device *const peer_device = first_peer_device(device);
136044a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	struct drbd_connection *const connection = peer_device->connection;
136199920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err;
136273a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
136373a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	if (unlikely(cancel)) {
13648554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
136599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
136673a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	}
1367e5f891b2234dbab8c8797111a61519d0728ef855Lars Ellenberg	req->pre_send_jif = jiffies;
136873a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
1369bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	/* this time, no connection->send.current_epoch_writes++;
1370b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * If it was sent, it was the closing barrier for the last
1371b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * replicated epoch, before we went into AHEAD mode.
1372b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * No more barriers will be sent, until we leave AHEAD mode again. */
1373bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	maybe_send_barrier(connection, req->epoch);
1374b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
137544a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	err = drbd_send_out_of_sync(peer_device, req);
13768554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher	req_mod(req, OOS_HANDED_TO_NETWORK);
137773a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
137899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
137973a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner}
138073a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
1381b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1382b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
1383b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1384b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1385b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
138699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_send_dblock(struct drbd_work *w, int cancel)
1387b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1388b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
138984b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device *device = req->device;
139044a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	struct drbd_peer_device *const peer_device = first_peer_device(device);
139144a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	struct drbd_connection *connection = peer_device->connection;
139299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err;
1393b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1394b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
13958554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
139699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1397b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1398e5f891b2234dbab8c8797111a61519d0728ef855Lars Ellenberg	req->pre_send_jif = jiffies;
1399b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1400bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	re_init_if_first_write(connection, req->epoch);
1401bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	maybe_send_barrier(connection, req->epoch);
1402bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	connection->send.current_epoch_writes++;
1403b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
140444a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	err = drbd_send_dblock(peer_device, req);
140599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
1406b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
140799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1408b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1409b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1410b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1411b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
1412b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1413b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1414b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
141599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_send_read_req(struct drbd_work *w, int cancel)
1416b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1417b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
141884b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device *device = req->device;
141944a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	struct drbd_peer_device *const peer_device = first_peer_device(device);
142044a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	struct drbd_connection *connection = peer_device->connection;
142199920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err;
1422b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1423b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
14248554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
142599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1426b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1427e5f891b2234dbab8c8797111a61519d0728ef855Lars Ellenberg	req->pre_send_jif = jiffies;
1428b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1429b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	/* Even read requests may close a write epoch,
1430b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * if there was any yet. */
1431bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	maybe_send_barrier(connection, req->epoch);
1432b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
143344a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	err = drbd_send_drequest(peer_device, P_DATA_REQUEST, req->i.sector, req->i.size,
14346c1005e74d4142511a165edae72cb6648aa308c5Andreas Gruenbacher				 (unsigned long)req);
1435b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
143699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
1437b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
143899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1439b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1440b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
144199920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_restart_disk_io(struct drbd_work *w, int cancel)
1442265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner{
1443265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
144484b8c06b6591e73250e6ab4834a02a86c8994b91Andreas Gruenbacher	struct drbd_device *device = req->device;
1445265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
14460778286a133d2d3f81861a4e5db308e359583006Philipp Reisner	if (bio_data_dir(req->master_bio) == WRITE && req->rq_state & RQ_IN_ACT_LOG)
14474dd726f02928ded116f6c9aaf6392a400ef0d9f7Lars Ellenberg		drbd_al_begin_io(device, &req->i);
1448265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1449265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	drbd_req_make_private_bio(req, req->master_bio);
1450b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	req->private_bio->bi_bdev = device->ldev->backing_bdev;
1451265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	generic_make_request(req->private_bio);
1452265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
145399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
1454265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner}
1455265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1456b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic int _drbd_may_sync_now(struct drbd_device *device)
1457b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1458b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *odev = device;
145995f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher	int resync_after;
1460b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1461b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (1) {
1462a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		if (!odev->ldev || odev->state.disk == D_DISKLESS)
1463438c8374ae3e87f44d945a2ac2901e3b14aec1a8Philipp Reisner			return 1;
1464daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		rcu_read_lock();
146595f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher		resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
1466daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		rcu_read_unlock();
146795f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher		if (resync_after == -1)
1468b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
1469b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		odev = minor_to_device(resync_after);
1470a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		if (!odev)
1471841ce241fa355048f66172a47e356bb6e9159c9dAndreas Gruenbacher			return 1;
1472b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if ((odev->state.conn >= C_SYNC_SOURCE &&
1473b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     odev->state.conn <= C_PAUSED_SYNC_T) ||
1474b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		    odev->state.aftr_isp || odev->state.peer_isp ||
1475b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		    odev->state.user_isp)
1476b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 0;
1477b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1478b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1479b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1480b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1481b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * _drbd_pause_after() - Pause resync on all devices that may not resync now
1482b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher * @device:	DRBD device.
1483b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1484b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Called from process context only (admin command and after_state_ch).
1485b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1486b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic int _drbd_pause_after(struct drbd_device *device)
1487b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1488547616979372b65646d691e8dab90e850be582feAndreas Gruenbacher	struct drbd_device *odev;
1489b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i, rv = 0;
1490b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1491695d08fa94ce5bb8d9880e260445fbcf50fa41b4Philipp Reisner	rcu_read_lock();
149205a10ec7900dbdba008a24bf56b3490c4b568d2cAndreas Gruenbacher	idr_for_each_entry(&drbd_devices, odev, i) {
1493b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1494b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1495b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!_drbd_may_sync_now(odev))
1496b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			rv |= (__drbd_set_state(_NS(odev, aftr_isp, 1), CS_HARD, NULL)
1497b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			       != SS_NOTHING_TO_DO);
1498b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1499695d08fa94ce5bb8d9880e260445fbcf50fa41b4Philipp Reisner	rcu_read_unlock();
1500b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1501b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return rv;
1502b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1503b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1504b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1505b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * _drbd_resume_next() - Resume resync on all devices that may resync now
1506b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher * @device:	DRBD device.
1507b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1508b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Called from process context only (admin command and worker).
1509b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1510b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic int _drbd_resume_next(struct drbd_device *device)
1511b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1512547616979372b65646d691e8dab90e850be582feAndreas Gruenbacher	struct drbd_device *odev;
1513b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i, rv = 0;
1514b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1515695d08fa94ce5bb8d9880e260445fbcf50fa41b4Philipp Reisner	rcu_read_lock();
151605a10ec7900dbdba008a24bf56b3490c4b568d2cAndreas Gruenbacher	idr_for_each_entry(&drbd_devices, odev, i) {
1517b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1518b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1519b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.aftr_isp) {
1520b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (_drbd_may_sync_now(odev))
1521b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				rv |= (__drbd_set_state(_NS(odev, aftr_isp, 0),
1522b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner							CS_HARD, NULL)
1523b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				       != SS_NOTHING_TO_DO) ;
1524b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1525b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1526695d08fa94ce5bb8d9880e260445fbcf50fa41b4Philipp Reisner	rcu_read_unlock();
1527b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return rv;
1528b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1529b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1530b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid resume_next_sg(struct drbd_device *device)
1531b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1532b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1533b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	_drbd_resume_next(device);
1534b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1535b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1536b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1537b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid suspend_other_sg(struct drbd_device *device)
1538b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1539b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1540b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	_drbd_pause_after(device);
1541b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1542b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1543b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1544dc97b70801667ea8b1432b37f5c122405c8d6f96Philipp Reisner/* caller must hold global_state_lock */
1545b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherenum drbd_ret_code drbd_resync_after_valid(struct drbd_device *device, int o_minor)
1546b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1547547616979372b65646d691e8dab90e850be582feAndreas Gruenbacher	struct drbd_device *odev;
154895f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher	int resync_after;
1549b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1550b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (o_minor == -1)
1551b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return NO_ERROR;
1552a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg	if (o_minor < -1 || o_minor > MINORMASK)
155395f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher		return ERR_RESYNC_AFTER;
1554b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1555b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* check for loops */
1556b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	odev = minor_to_device(o_minor);
1557b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (1) {
1558b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		if (odev == device)
155995f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher			return ERR_RESYNC_AFTER_CYCLE;
1560b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1561a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		/* You are free to depend on diskless, non-existing,
1562a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		 * or not yet/no longer existing minors.
1563a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		 * We only reject dependency loops.
1564a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		 * We cannot follow the dependency chain beyond a detached or
1565a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		 * missing minor.
1566a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		 */
1567a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		if (!odev || !odev->ldev || odev->state.disk == D_DISKLESS)
1568a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg			return NO_ERROR;
1569a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg
1570daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		rcu_read_lock();
157195f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher		resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
1572daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		rcu_read_unlock();
1573b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* dependency chain ends here, no cycles. */
157495f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher		if (resync_after == -1)
1575b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return NO_ERROR;
1576b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1577b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* follow the dependency chain */
1578b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		odev = minor_to_device(resync_after);
1579b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1580b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1581b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1582dc97b70801667ea8b1432b37f5c122405c8d6f96Philipp Reisner/* caller must hold global_state_lock */
1583b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid drbd_resync_after_changed(struct drbd_device *device)
1584b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1585b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int changes;
1586b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1587dc97b70801667ea8b1432b37f5c122405c8d6f96Philipp Reisner	do {
1588b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		changes  = _drbd_pause_after(device);
1589b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		changes |= _drbd_resume_next(device);
1590dc97b70801667ea8b1432b37f5c122405c8d6f96Philipp Reisner	} while (changes);
1591b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1592b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1593b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid drbd_rs_controller_reset(struct drbd_device *device)
15949bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg{
1595813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	struct fifo_buffer *plan;
1596813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner
1597b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	atomic_set(&device->rs_sect_in, 0);
1598b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	atomic_set(&device->rs_sect_ev, 0);
1599b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_in_flight = 0;
1600813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner
1601813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	/* Updating the RCU protected object in place is necessary since
1602813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	   this function gets called from atomic context.
1603813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	   It is valid since all other updates also lead to an completely
1604813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	   empty fifo */
1605813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	rcu_read_lock();
1606b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	plan = rcu_dereference(device->rs_plan_s);
1607813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	plan->total = 0;
1608813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	fifo_set(plan, 0);
1609813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	rcu_read_unlock();
16109bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg}
16119bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg
16121f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisnervoid start_resync_timer_fn(unsigned long data)
16131f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner{
1614b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = (struct drbd_device *) data;
1615ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg	drbd_device_post_work(device, RS_START);
16161f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner}
16171f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner
1618ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenbergstatic void do_start_resync(struct drbd_device *device)
16191f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner{
1620b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (atomic_read(&device->unacked_cnt) || atomic_read(&device->rs_pending_cnt)) {
1621ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg		drbd_warn(device, "postponing start_resync ...\n");
1622b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->start_resync_timer.expires = jiffies + HZ/10;
1623b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		add_timer(&device->start_resync_timer);
1624ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg		return;
16251f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner	}
16261f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner
1627b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_start_resync(device, C_SYNC_SOURCE);
1628b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	clear_bit(AHEAD_TO_SYNC_SOURCE, &device->flags);
16291f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner}
16301f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner
1631aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenbergstatic bool use_checksum_based_resync(struct drbd_connection *connection, struct drbd_device *device)
1632aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg{
1633aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg	bool csums_after_crash_only;
1634aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg	rcu_read_lock();
1635aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg	csums_after_crash_only = rcu_dereference(connection->net_conf)->csums_after_crash_only;
1636aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg	rcu_read_unlock();
1637aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg	return connection->agreed_pro_version >= 89 &&		/* supported? */
1638aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg		connection->csums_tfm &&			/* configured? */
1639aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg		(csums_after_crash_only == 0			/* use for each resync? */
1640aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg		 || test_bit(CRASHED_PRIMARY, &device->flags));	/* or only after Primary crash? */
1641aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg}
1642aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg
1643b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1644b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * drbd_start_resync() - Start the resync process
1645b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher * @device:	DRBD device.
1646b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @side:	Either C_SYNC_SOURCE or C_SYNC_TARGET
1647b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1648b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * This function might bring you directly into one of the
1649b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * C_PAUSED_SYNC_* states.
1650b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1651b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid drbd_start_resync(struct drbd_device *device, enum drbd_conns side)
1652b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
165344a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	struct drbd_peer_device *peer_device = first_peer_device(device);
165444a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
1655b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	union drbd_state ns;
1656b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int r;
1657b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1658b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->state.conn >= C_SYNC_SOURCE && device->state.conn < C_AHEAD) {
1659d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher		drbd_err(device, "Resync already running!\n");
1660b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return;
1661b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1662b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1663b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (!test_bit(B_RS_H_DONE, &device->flags)) {
1664e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		if (side == C_SYNC_TARGET) {
1665e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			/* Since application IO was locked out during C_WF_BITMAP_T and
1666e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			   C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1667e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			   we check that we might make the data inconsistent. */
1668b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			r = drbd_khelper(device, "before-resync-target");
1669e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			r = (r >> 8) & 0xff;
1670e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			if (r > 0) {
1671d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher				drbd_info(device, "before-resync-target handler returned %d, "
167209b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner					 "dropping connection.\n", r);
167344a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg				conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
167409b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner				return;
167509b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner			}
1676e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		} else /* C_SYNC_SOURCE */ {
1677b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			r = drbd_khelper(device, "before-resync-source");
1678e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			r = (r >> 8) & 0xff;
1679e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			if (r > 0) {
1680e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner				if (r == 3) {
1681d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher					drbd_info(device, "before-resync-source handler returned %d, "
1682e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner						 "ignoring. Old userland tools?", r);
1683e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner				} else {
1684d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher					drbd_info(device, "before-resync-source handler returned %d, "
1685e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner						 "dropping connection.\n", r);
168644a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg					conn_request_state(connection,
1687a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher							   NS(conn, C_DISCONNECTING), CS_HARD);
1688e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner					return;
1689e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner				}
1690e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			}
169109b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner		}
1692b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1693b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
169444a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg	if (current == connection->worker.task) {
1695dad20554812e73a2bfbe45d1b161d5d3c249e597Philipp Reisner		/* The worker should not sleep waiting for state_mutex,
1696e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		   that can take long */
1697b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		if (!mutex_trylock(device->state_mutex)) {
1698b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			set_bit(B_RS_H_DONE, &device->flags);
1699b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->start_resync_timer.expires = jiffies + HZ/5;
1700b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			add_timer(&device->start_resync_timer);
1701e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			return;
1702e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		}
1703e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner	} else {
1704b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		mutex_lock(device->state_mutex);
1705e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner	}
1706b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	clear_bit(B_RS_H_DONE, &device->flags);
1707b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1708074f4afeb2277bd5ecb9fa7f91eaffa55e262126Lars Ellenberg	/* req_lock: serialize with drbd_send_and_submit() and others
1709074f4afeb2277bd5ecb9fa7f91eaffa55e262126Lars Ellenberg	 * global_state_lock: for stable sync-after dependencies */
1710074f4afeb2277bd5ecb9fa7f91eaffa55e262126Lars Ellenberg	spin_lock_irq(&device->resource->req_lock);
1711074f4afeb2277bd5ecb9fa7f91eaffa55e262126Lars Ellenberg	write_lock(&global_state_lock);
1712a700471bf335965e7603273fd51034415553246aPhilipp Reisner	/* Did some connection breakage or IO error race with us? */
1713b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->state.conn < C_CONNECTED
1714b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	|| !get_ldev_if_state(device, D_NEGOTIATING)) {
1715074f4afeb2277bd5ecb9fa7f91eaffa55e262126Lars Ellenberg		write_unlock(&global_state_lock);
1716074f4afeb2277bd5ecb9fa7f91eaffa55e262126Lars Ellenberg		spin_unlock_irq(&device->resource->req_lock);
1717b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		mutex_unlock(device->state_mutex);
1718b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return;
1719b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1720b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1721b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	ns = drbd_read_state(device);
1722b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1723b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	ns.aftr_isp = !_drbd_may_sync_now(device);
1724b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1725b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.conn = side;
1726b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1727b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (side == C_SYNC_TARGET)
1728b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.disk = D_INCONSISTENT;
1729b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	else /* side == C_SYNC_SOURCE */
1730b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.pdsk = D_INCONSISTENT;
1731b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1732b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	r = __drbd_set_state(device, ns, CS_VERBOSE, NULL);
1733b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	ns = drbd_read_state(device);
1734b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1735b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (ns.conn < C_CONNECTED)
1736b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		r = SS_UNKNOWN_ERROR;
1737b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1738b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (r == SS_SUCCESS) {
1739b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		unsigned long tw = drbd_bm_total_weight(device);
17401d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		unsigned long now = jiffies;
17411d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		int i;
17421d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg
1743b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_failed    = 0;
1744b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_paused    = 0;
1745b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_same_csum = 0;
1746b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_last_events = 0;
1747b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_last_sect_ev = 0;
1748b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_total     = tw;
1749b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_start     = now;
17501d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1751b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->rs_mark_left[i] = tw;
1752b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->rs_mark_time[i] = now;
17531d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		}
1754b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		_drbd_pause_after(device);
17555ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg		/* Forget potentially stale cached per resync extent bit-counts.
17565ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg		 * Open coded drbd_rs_cancel_all(device), we already have IRQs
17575ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg		 * disabled, and know the disk state is ok. */
17585ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg		spin_lock(&device->al_lock);
17595ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg		lc_reset(device->resync);
17605ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg		device->resync_locked = 0;
17615ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg		device->resync_wenr = LC_FREE;
17625ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg		spin_unlock(&device->al_lock);
1763b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1764074f4afeb2277bd5ecb9fa7f91eaffa55e262126Lars Ellenberg	write_unlock(&global_state_lock);
1765074f4afeb2277bd5ecb9fa7f91eaffa55e262126Lars Ellenberg	spin_unlock_irq(&device->resource->req_lock);
17665a22db8968a69bec835d1ed9a96ab3381719e0c0Lars Ellenberg
1767b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (r == SS_SUCCESS) {
17685ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg		wake_up(&device->al_wait); /* for lc_reset() above */
1769328e0f125bf41f4f33f684db22015f92cb44fe56Philipp Reisner		/* reset rs_last_bcast when a resync or verify is started,
1770328e0f125bf41f4f33f684db22015f92cb44fe56Philipp Reisner		 * to deal with potential jiffies wrap. */
1771b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_last_bcast = jiffies - HZ;
1772328e0f125bf41f4f33f684db22015f92cb44fe56Philipp Reisner
1773d01801710265cfb7bd8928ae7c3be4d9d15ceeb0Andreas Gruenbacher		drbd_info(device, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
1774b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     drbd_conn_str(ns.conn),
1775b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		     (unsigned long) device->rs_total << (BM_BLOCK_SHIFT-10),
1776b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		     (unsigned long) device->rs_total);
1777aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg		if (side == C_SYNC_TARGET) {
1778b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->bm_resync_fo = 0;
1779aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg			device->use_csums = use_checksum_based_resync(connection, device);
1780aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg		} else {
1781aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg			device->use_csums = 0;
1782aaaba34576407857f6146ff6c330f06e63fb2bf2Lars Ellenberg		}
17836c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg
17846c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		/* Since protocol 96, we must serialize drbd_gen_and_send_sync_uuid
17856c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * with w_send_oos, or the sync target will get confused as to
17866c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * how much bits to resync.  We cannot do that always, because for an
17876c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * empty resync and protocol < 95, we need to do it here, as we call
17886c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * drbd_resync_finished from here in that case.
17896c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * We drbd_gen_and_send_sync_uuid here for protocol < 96,
17906c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * and from after_state_ch otherwise. */
179144a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg		if (side == C_SYNC_SOURCE && connection->agreed_pro_version < 96)
179244a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg			drbd_gen_and_send_sync_uuid(peer_device);
1793b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
179444a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg		if (connection->agreed_pro_version < 95 && device->rs_total == 0) {
1795af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			/* This still has a race (about when exactly the peers
1796af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * detect connection loss) that can lead to a full sync
1797af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * on next handshake. In 8.3.9 we fixed this with explicit
1798af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * resync-finished notifications, but the fix
1799af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * introduces a protocol change.  Sleeping for some
1800af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * time longer than the ping interval + timeout on the
1801af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * SyncSource, to give the SyncTarget the chance to
1802af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * detect connection loss, then waiting for a ping
1803af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * response (implicit in drbd_resync_finished) reduces
1804af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * the race considerably, but does not solve it. */
180544ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner			if (side == C_SYNC_SOURCE) {
180644ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				struct net_conf *nc;
180744ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				int timeo;
180844ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner
180944ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				rcu_read_lock();
181044a4d551846b8c61aa430b9432c1fcdf88444708Lars Ellenberg				nc = rcu_dereference(connection->net_conf);
181144ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				timeo = nc->ping_int * HZ + nc->ping_timeo * HZ / 9;
181244ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				rcu_read_unlock();
181344ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				schedule_timeout_interruptible(timeo);
181444ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner			}
1815b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			drbd_resync_finished(device);
1816b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1817b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1818b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_controller_reset(device);
1819b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		/* ns.conn may already be != device->state.conn,
1820b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * we may have been paused in between, or become paused until
1821b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * the timer triggers.
1822b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * No matter, that is handled in resync_timer_fn() */
1823b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (ns.conn == C_SYNC_TARGET)
1824b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			mod_timer(&device->resync_timer, jiffies);
1825b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1826b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_md_sync(device);
1827b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1828b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
1829b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	mutex_unlock(device->state_mutex);
1830b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1831b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1832e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenbergstatic void update_on_disk_bitmap(struct drbd_device *device, bool resync_done)
1833c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg{
1834c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
1835c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	device->rs_last_bcast = jiffies;
1836c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg
1837c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	if (!get_ldev(device))
1838c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg		return;
1839c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg
1840c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	drbd_bm_write_lazy(device, 0);
18415ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg	if (resync_done && is_sync_state(device->state.conn))
1842c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg		drbd_resync_finished(device);
18435ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg
1844c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	drbd_bcast_event(device, &sib);
1845c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	/* update timestamp, in case it took a while to write out stuff */
1846c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	device->rs_last_bcast = jiffies;
1847c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	put_ldev(device);
1848c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg}
1849c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg
1850e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenbergstatic void drbd_ldev_destroy(struct drbd_device *device)
1851e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg{
1852e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	lc_destroy(device->resync);
1853e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	device->resync = NULL;
1854e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	lc_destroy(device->act_log);
1855e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	device->act_log = NULL;
1856d1b8085356391d1d5151670ab96baae6234d1e20Andreas Gruenbacher
1857d1b8085356391d1d5151670ab96baae6234d1e20Andreas Gruenbacher	__acquire(local);
1858d1b8085356391d1d5151670ab96baae6234d1e20Andreas Gruenbacher	drbd_free_ldev(device->ldev);
1859d1b8085356391d1d5151670ab96baae6234d1e20Andreas Gruenbacher	device->ldev = NULL;
1860d1b8085356391d1d5151670ab96baae6234d1e20Andreas Gruenbacher	__release(local);
1861d1b8085356391d1d5151670ab96baae6234d1e20Andreas Gruenbacher
1862e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	clear_bit(GOING_DISKLESS, &device->flags);
1863e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	wake_up(&device->misc_wait);
1864e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg}
1865e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg
1866e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenbergstatic void go_diskless(struct drbd_device *device)
1867e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg{
1868e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	D_ASSERT(device, device->state.disk == D_FAILED);
1869e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	/* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
1870e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
1871e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 * the protected members anymore, though, so once put_ldev reaches zero
1872e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 * again, it will be safe to free them. */
1873e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg
1874e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	/* Try to write changed bitmap pages, read errors may have just
1875e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 * set some bits outside the area covered by the activity log.
1876e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 *
1877e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 * If we have an IO error during the bitmap writeout,
1878e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 * we will want a full sync next time, just in case.
1879e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 * (Do we want a specific meta data flag for this?)
1880e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 *
1881e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 * If that does not make it to stable storage either,
1882e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 * we cannot do anything about that anymore.
1883e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 *
1884e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 * We still need to check if both bitmap and ldev are present, we may
1885e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 * end up here after a failed attach, before ldev was even assigned.
1886e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	 */
1887e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	if (device->bitmap && device->ldev) {
1888e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		/* An interrupted resync or similar is allowed to recounts bits
1889e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		 * while we detach.
1890e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		 * Any modifications would not be expected anymore, though.
1891e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		 */
1892e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		if (drbd_bitmap_io_from_worker(device, drbd_bm_write,
1893e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg					"detach", BM_LOCKED_TEST_ALLOWED)) {
1894e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg			if (test_bit(WAS_READ_ERROR, &device->flags)) {
1895e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg				drbd_md_set_flag(device, MDF_FULL_SYNC);
1896e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg				drbd_md_sync(device);
1897e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg			}
1898e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		}
1899e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	}
1900e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg
1901e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	drbd_force_state(device, NS(disk, D_DISKLESS));
1902e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg}
1903e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg
1904ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenbergstatic int do_md_sync(struct drbd_device *device)
1905ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg{
1906ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg	drbd_warn(device, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
1907ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg	drbd_md_sync(device);
1908ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg	return 0;
1909ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg}
1910ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg
1911944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg/* only called from drbd_worker thread, no locking */
1912944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenbergvoid __update_timing_details(
1913944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg		struct drbd_thread_timing_details *tdp,
1914944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg		unsigned int *cb_nr,
1915944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg		void *cb,
1916944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg		const char *fn, const unsigned int line)
1917944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg{
1918944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg	unsigned int i = *cb_nr % DRBD_THREAD_DETAILS_HIST;
1919944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg	struct drbd_thread_timing_details *td = tdp + i;
1920944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg
1921944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg	td->start_jif = jiffies;
1922944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg	td->cb_addr = cb;
1923944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg	td->caller_fn = fn;
1924944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg	td->line = line;
1925944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg	td->cb_nr = *cb_nr;
1926944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg
1927944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg	i = (i+1) % DRBD_THREAD_DETAILS_HIST;
1928944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg	td = tdp + i;
1929944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg	memset(td, 0, sizeof(*td));
1930944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg
1931944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg	++(*cb_nr);
1932944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg}
1933944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg
1934e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenbergstatic void do_device_work(struct drbd_device *device, const unsigned long todo)
1935e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg{
1936b47a06d10561bfe7317b1355b4b8e4168fc6b4b7Andreas Gruenbacher	if (test_bit(MD_SYNC, &todo))
1937ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg		do_md_sync(device);
1938b47a06d10561bfe7317b1355b4b8e4168fc6b4b7Andreas Gruenbacher	if (test_bit(RS_DONE, &todo) ||
1939b47a06d10561bfe7317b1355b4b8e4168fc6b4b7Andreas Gruenbacher	    test_bit(RS_PROGRESS, &todo))
1940b47a06d10561bfe7317b1355b4b8e4168fc6b4b7Andreas Gruenbacher		update_on_disk_bitmap(device, test_bit(RS_DONE, &todo));
1941b47a06d10561bfe7317b1355b4b8e4168fc6b4b7Andreas Gruenbacher	if (test_bit(GO_DISKLESS, &todo))
1942e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		go_diskless(device);
1943b47a06d10561bfe7317b1355b4b8e4168fc6b4b7Andreas Gruenbacher	if (test_bit(DESTROY_DISK, &todo))
1944e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		drbd_ldev_destroy(device);
1945b47a06d10561bfe7317b1355b4b8e4168fc6b4b7Andreas Gruenbacher	if (test_bit(RS_START, &todo))
1946ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg		do_start_resync(device);
1947e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg}
1948e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg
1949e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg#define DRBD_DEVICE_WORK_MASK	\
1950e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	((1UL << GO_DISKLESS)	\
1951e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	|(1UL << DESTROY_DISK)	\
1952ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg	|(1UL << MD_SYNC)	\
1953ac0acb9e39ac41575cc6a344d04295436fd4eb4eLars Ellenberg	|(1UL << RS_START)	\
1954e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	|(1UL << RS_PROGRESS)	\
1955e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	|(1UL << RS_DONE)	\
1956e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	)
1957e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg
1958e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenbergstatic unsigned long get_work_bits(unsigned long *flags)
1959e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg{
1960e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	unsigned long old, new;
1961e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	do {
1962e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		old = *flags;
1963e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		new = old & ~DRBD_DEVICE_WORK_MASK;
1964e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	} while (cmpxchg(flags, old, new) != old);
1965e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	return old & DRBD_DEVICE_WORK_MASK;
1966e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg}
1967e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg
1968e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenbergstatic void do_unqueued_work(struct drbd_connection *connection)
1969c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg{
1970c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	struct drbd_peer_device *peer_device;
1971c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	int vnr;
1972c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg
1973c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	rcu_read_lock();
1974c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1975c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg		struct drbd_device *device = peer_device->device;
1976e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		unsigned long todo = get_work_bits(&device->flags);
1977e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		if (!todo)
1978c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg			continue;
19795ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg
1980c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg		kref_get(&device->kref);
1981c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg		rcu_read_unlock();
1982e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		do_device_work(device, todo);
1983c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg		kref_put(&device->kref, drbd_destroy_device);
1984c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg		rcu_read_lock();
1985c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	}
1986c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg	rcu_read_unlock();
1987c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg}
1988c7a58db4e9dc523b18bbfbc3aa311d8308acc293Lars Ellenberg
1989a186e47856c7877fa684d75901436c1d35ec35e0Rashika Kheriastatic bool dequeue_work_batch(struct drbd_work_queue *queue, struct list_head *work_list)
19908c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg{
19918c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	spin_lock_irq(&queue->q_lock);
199215e26f6a3c6de2c665b4a30b9a70a902111f281fLars Ellenberg	list_splice_tail_init(&queue->q, work_list);
19938c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	spin_unlock_irq(&queue->q_lock);
19948c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	return !list_empty(work_list);
19958c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg}
19968c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg
1997bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacherstatic void wait_for_work(struct drbd_connection *connection, struct list_head *work_list)
1998b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg{
1999b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	DEFINE_WAIT(wait);
2000b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	struct net_conf *nc;
2001b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	int uncork, cork;
2002b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
2003abde9cc6a59cb7f07fda4c77fee2150314e423faLars Ellenberg	dequeue_work_batch(&connection->sender_work, work_list);
2004b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	if (!list_empty(work_list))
2005b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		return;
2006b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
2007b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	/* Still nothing to do?
2008b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * Maybe we still need to close the current epoch,
2009b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * even if no new requests are queued yet.
2010b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 *
2011b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * Also, poke TCP, just in case.
2012b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * Then wait for new work (or signal). */
2013b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	rcu_read_lock();
2014b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	nc = rcu_dereference(connection->net_conf);
2015b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	uncork = nc ? nc->tcp_cork : 0;
2016b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	rcu_read_unlock();
2017b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	if (uncork) {
2018b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		mutex_lock(&connection->data.mutex);
2019b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		if (connection->data.socket)
2020b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg			drbd_tcp_uncork(connection->data.socket);
2021b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		mutex_unlock(&connection->data.mutex);
2022b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	}
2023b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
2024b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	for (;;) {
2025b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		int send_barrier;
2026b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		prepare_to_wait(&connection->sender_work.q_wait, &wait, TASK_INTERRUPTIBLE);
20270500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher		spin_lock_irq(&connection->resource->req_lock);
2028b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		spin_lock(&connection->sender_work.q_lock);	/* FIXME get rid of this one? */
2029bc317a9ecd641b78a4b237cb22b30ecf11443c77Lars Ellenberg		if (!list_empty(&connection->sender_work.q))
20304dd726f02928ded116f6c9aaf6392a400ef0d9f7Lars Ellenberg			list_splice_tail_init(&connection->sender_work.q, work_list);
2031b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		spin_unlock(&connection->sender_work.q_lock);	/* FIXME get rid of this one? */
2032b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		if (!list_empty(work_list) || signal_pending(current)) {
20330500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher			spin_unlock_irq(&connection->resource->req_lock);
2034b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg			break;
2035b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		}
2036f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg
2037f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg		/* We found nothing new to do, no to-be-communicated request,
2038f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg		 * no other work item.  We may still need to close the last
2039f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg		 * epoch.  Next incoming request epoch will be connection ->
2040f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg		 * current transfer log epoch number.  If that is different
2041f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg		 * from the epoch of the last request we communicated, it is
2042f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg		 * safe to send the epoch separating barrier now.
2043f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg		 */
2044f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg		send_barrier =
2045f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg			atomic_read(&connection->current_tle_nr) !=
2046f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg			connection->send.current_epoch_nr;
20470500813fe0c9a617ace86d91344e36839050dad6Andreas Gruenbacher		spin_unlock_irq(&connection->resource->req_lock);
2048f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg
2049f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg		if (send_barrier)
2050f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg			maybe_send_barrier(connection,
2051f9c78128f833ae3057884ca219259c8ae5db8898Lars Ellenberg					connection->send.current_epoch_nr + 1);
20525ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg
2053e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg		if (test_bit(DEVICE_WORK_PENDING, &connection->flags))
20545ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg			break;
20555ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg
2056a80ca1ae81fc52e304e753f6de4ef248df364f9eLars Ellenberg		/* drbd_send() may have called flush_signals() */
2057a80ca1ae81fc52e304e753f6de4ef248df364f9eLars Ellenberg		if (get_t_state(&connection->worker) != RUNNING)
2058a80ca1ae81fc52e304e753f6de4ef248df364f9eLars Ellenberg			break;
20595ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg
2060b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		schedule();
2061b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		/* may be woken up for other things but new work, too,
2062b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		 * e.g. if the current epoch got closed.
2063b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		 * In which case we send the barrier above. */
2064b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	}
2065b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	finish_wait(&connection->sender_work.q_wait, &wait);
2066b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
2067b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	/* someone may have changed the config while we have been waiting above. */
2068b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	rcu_read_lock();
2069b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	nc = rcu_dereference(connection->net_conf);
2070b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	cork = nc ? nc->tcp_cork : 0;
2071b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	rcu_read_unlock();
2072b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	mutex_lock(&connection->data.mutex);
2073b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	if (connection->data.socket) {
2074b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		if (cork)
2075b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg			drbd_tcp_cork(connection->data.socket);
2076b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		else if (!uncork)
2077b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg			drbd_tcp_uncork(connection->data.socket);
2078b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	}
2079b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	mutex_unlock(&connection->data.mutex);
2080b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg}
2081b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
2082b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint drbd_worker(struct drbd_thread *thi)
2083b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
2084bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	struct drbd_connection *connection = thi->connection;
20856db7e50a8a40d2210544b4a09f3d4988127c20adAndreas Gruenbacher	struct drbd_work *w = NULL;
2086c06ece6ba6f1bb2e01616e111303c3ae5f80fdbeAndreas Gruenbacher	struct drbd_peer_device *peer_device;
2087b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	LIST_HEAD(work_list);
20888c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	int vnr;
2089b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
2090e77a0a5cc1e6961f485b5623ef42f3b910969675Andreas Gruenbacher	while (get_t_state(thi) == RUNNING) {
2091808222845d62e551630699a1381bbf8a1fd4a286Philipp Reisner		drbd_thread_current_set_cpu(thi);
2092b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
2093944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg		if (list_empty(&work_list)) {
2094944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg			update_worker_timing_details(connection, wait_for_work);
2095bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher			wait_for_work(connection, &work_list);
2096944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg		}
2097b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
2098944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg		if (test_and_clear_bit(DEVICE_WORK_PENDING, &connection->flags)) {
2099944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg			update_worker_timing_details(connection, do_unqueued_work);
2100e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg			do_unqueued_work(connection);
2101944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg		}
21025ab7d2c005135849cf0bb1485d954c98f2cca57cLars Ellenberg
21038c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg		if (signal_pending(current)) {
2104b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			flush_signals(current);
210519393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			if (get_t_state(thi) == RUNNING) {
21061ec861ebd0889263841b822ee3f3eb49caf23656Andreas Gruenbacher				drbd_warn(connection, "Worker got an unexpected signal\n");
2107b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				continue;
210819393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			}
2109b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			break;
2110b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
2111b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
2112e77a0a5cc1e6961f485b5623ef42f3b910969675Andreas Gruenbacher		if (get_t_state(thi) != RUNNING)
2113b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			break;
2114b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
2115729e8b87bac63dee09302ddffc05a7ba0e50c9adLars Ellenberg		if (!list_empty(&work_list)) {
21166db7e50a8a40d2210544b4a09f3d4988127c20adAndreas Gruenbacher			w = list_first_entry(&work_list, struct drbd_work, list);
21176db7e50a8a40d2210544b4a09f3d4988127c20adAndreas Gruenbacher			list_del_init(&w->list);
2118944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg			update_worker_timing_details(connection, w->cb);
21196db7e50a8a40d2210544b4a09f3d4988127c20adAndreas Gruenbacher			if (w->cb(w, connection->cstate < C_WF_REPORT_PARAMS) == 0)
21208c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg				continue;
2121bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher			if (connection->cstate >= C_WF_REPORT_PARAMS)
2122bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher				conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD);
2123b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
2124b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
2125b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
21268c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	do {
2127944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg		if (test_and_clear_bit(DEVICE_WORK_PENDING, &connection->flags)) {
2128944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg			update_worker_timing_details(connection, do_unqueued_work);
2129e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg			do_unqueued_work(connection);
2130944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg		}
2131729e8b87bac63dee09302ddffc05a7ba0e50c9adLars Ellenberg		if (!list_empty(&work_list)) {
21326db7e50a8a40d2210544b4a09f3d4988127c20adAndreas Gruenbacher			w = list_first_entry(&work_list, struct drbd_work, list);
21336db7e50a8a40d2210544b4a09f3d4988127c20adAndreas Gruenbacher			list_del_init(&w->list);
2134944410e97cfcec38369eeb5f77d0e8da91d68afbLars Ellenberg			update_worker_timing_details(connection, w->cb);
21356db7e50a8a40d2210544b4a09f3d4988127c20adAndreas Gruenbacher			w->cb(w, 1);
2136729e8b87bac63dee09302ddffc05a7ba0e50c9adLars Ellenberg		} else
2137729e8b87bac63dee09302ddffc05a7ba0e50c9adLars Ellenberg			dequeue_work_batch(&connection->sender_work, &work_list);
2138e334f55095b908f12c8bad991433f5d609e919d1Lars Ellenberg	} while (!list_empty(&work_list) || test_bit(DEVICE_WORK_PENDING, &connection->flags));
2139b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
2140c141ebda031a0550d75634f7c94f7c85c2d5c9f5Philipp Reisner	rcu_read_lock();
2141c06ece6ba6f1bb2e01616e111303c3ae5f80fdbeAndreas Gruenbacher	idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2142c06ece6ba6f1bb2e01616e111303c3ae5f80fdbeAndreas Gruenbacher		struct drbd_device *device = peer_device->device;
21430b0ba1efc7b887bc2bd767ef822979fe2dae620eAndreas Gruenbacher		D_ASSERT(device, device->state.disk == D_DISKLESS && device->state.conn == C_STANDALONE);
2144b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		kref_get(&device->kref);
2145c141ebda031a0550d75634f7c94f7c85c2d5c9f5Philipp Reisner		rcu_read_unlock();
2146b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_device_cleanup(device);
214705a10ec7900dbdba008a24bf56b3490c4b568d2cAndreas Gruenbacher		kref_put(&device->kref, drbd_destroy_device);
2148c141ebda031a0550d75634f7c94f7c85c2d5c9f5Philipp Reisner		rcu_read_lock();
21490e29d163f7ec8369b3f1fb70900d29b1c4a1dc8bPhilipp Reisner	}
2150c141ebda031a0550d75634f7c94f7c85c2d5c9f5Philipp Reisner	rcu_read_unlock();
2151b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
2152b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 0;
2153b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
2154